Fix compat, add tests

This commit is contained in:
twangboy 2019-03-05 18:38:51 -07:00
parent 2837bb9c17
commit a791901775
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB
2 changed files with 15 additions and 6 deletions

View file

@ -7,8 +7,8 @@ Salt compatibility code
# Import python libs
from __future__ import absolute_import, unicode_literals, print_function
import sys
import types
import logging
import binascii
# Import 3rd-party libs
from salt.exceptions import SaltException
@ -173,7 +173,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address):
self._ip = address
elif self._is_packed_binary(address):
self._check_packed_address(address, 16)
self._ip = ipaddress._int_from_bytes(address, 'big')
self._ip = int(binascii.hexlify(address), 16)
else:
address = str(address)
if '/' in address:
@ -190,10 +190,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address):
packed = False
if isinstance(data, bytes) and len(data) == 16 and b':' not in data:
try:
if PY3:
packed = bool(int(str(bytearray(data, 'utf-8')).encode('utf-8').hex(), 16))
else:
packed = bool(int(str(bytearray(data)).encode('hex'), 16))
packed = bool(int(binascii.hexlify(data), 16))
except ValueError:
pass

View file

@ -69,3 +69,15 @@ class CompatTestCase(TestCase):
else:
expected = 'StringIO.StringIO instance'
self.assertTrue(expected in repr(ret))
def test_ipv6_class__is_packed_binary(self):
ipv6 = compat.IPv6AddressScoped('2001:db8::')
self.assertEqual(str(ipv6), '2001:db8::')
def test_ipv6_class__is_packed_binary_integer(self):
ipv6 = compat.IPv6AddressScoped(42540766411282592856903984951653826560)
self.assertEqual(str(ipv6), '2001:db8::')
def test_ipv6_class__is_packed_binary__issue_51831(self):
ipv6 = compat.IPv6AddressScoped(b'sixteen.digit.bn')
self.assertEqual(str(ipv6), '7369:7874:6565:6e2e:6469:6769:742e:626e')