Skip to content

Commit

Permalink
dns.ipv6.inet_ntoa() should return a string.
Browse files Browse the repository at this point in the history
[issue #167]
  • Loading branch information
rthalley committed May 31, 2016
1 parent 995a46c commit 9c9ae1c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -58,4 +58,4 @@ tags:
check: test

test:
cd tests; make test
cd tests; make PYTHON=${PYTHON} test
4 changes: 4 additions & 0 deletions dns/_compat.py
Expand Up @@ -14,8 +14,12 @@
binary_type = bytes
string_types = (str,)
unichr = chr
def maybe_decode(x):
return x.decode()
else:
text_type = unicode
binary_type = str
string_types = (basestring,)
unichr = unichr
def maybe_decode(x):
return x
4 changes: 2 additions & 2 deletions dns/ipv6.py
Expand Up @@ -20,7 +20,7 @@

import dns.exception
import dns.ipv4
from ._compat import xrange, binary_type
from ._compat import xrange, binary_type, maybe_decode

_leading_zero = re.compile(b'0+([0-9a-f]+)')

Expand Down Expand Up @@ -89,7 +89,7 @@ def inet_ntoa(address):
b':'.join(chunks[best_start + best_len:])
else:
hex = b':'.join(chunks)
return hex
return maybe_decode(hex)

_v4_ending = re.compile(b'(.*):(\d+\.\d+\.\d+\.\d+)$')
_colon_colon_start = re.compile(b'::.*')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_name.py
Expand Up @@ -682,7 +682,7 @@ def testForwardIPv4(self):

def testForwardIPv6(self):
n = dns.name.from_text('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.')
e = b'::1'
e = '::1'
text = dns.reversename.to_address(n)
self.assertEqual(text, e)

Expand Down
30 changes: 15 additions & 15 deletions tests/test_ntoaaton.py
Expand Up @@ -90,72 +90,72 @@ def test_aton4(self):
def test_ntoa1(self):
b = binascii.unhexlify(b'00010002000300040005000600070008')
t = ntoa6(b)
self.assertEqual(t, b'1:2:3:4:5:6:7:8')
self.assertEqual(t, '1:2:3:4:5:6:7:8')

def test_ntoa2(self):
b = b'\x00' * 16
t = ntoa6(b)
self.assertEqual(t, b'::')
self.assertEqual(t, '::')

def test_ntoa3(self):
b = b'\x00' * 15 + b'\x01'
t = ntoa6(b)
self.assertEqual(t, b'::1')
self.assertEqual(t, '::1')

def test_ntoa4(self):
b = b'\x80' + b'\x00' * 15
t = ntoa6(b)
self.assertEqual(t, b'8000::')
self.assertEqual(t, '8000::')

def test_ntoa5(self):
b = b'\x01\xcd' + b'\x00' * 12 + b'\x03\xef'
t = ntoa6(b)
self.assertEqual(t, b'1cd::3ef')
self.assertEqual(t, '1cd::3ef')

def test_ntoa6(self):
b = binascii.unhexlify(b'ffff00000000ffff000000000000ffff')
t = ntoa6(b)
self.assertEqual(t, b'ffff:0:0:ffff::ffff')
self.assertEqual(t, 'ffff:0:0:ffff::ffff')

def test_ntoa7(self):
b = binascii.unhexlify(b'00000000ffff000000000000ffffffff')
t = ntoa6(b)
self.assertEqual(t, b'0:0:ffff::ffff:ffff')
self.assertEqual(t, '0:0:ffff::ffff:ffff')

def test_ntoa8(self):
b = binascii.unhexlify(b'ffff0000ffff00000000ffff00000000')
t = ntoa6(b)
self.assertEqual(t, b'ffff:0:ffff::ffff:0:0')
self.assertEqual(t, 'ffff:0:ffff::ffff:0:0')

def test_ntoa9(self):
b = binascii.unhexlify(b'0000000000000000000000000a000001')
t = ntoa6(b)
self.assertEqual(t, b'::10.0.0.1')
self.assertEqual(t, '::10.0.0.1')

def test_ntoa10(self):
b = binascii.unhexlify(b'0000000000000000000000010a000001')
t = ntoa6(b)
self.assertEqual(t, b'::1:a00:1')
self.assertEqual(t, '::1:a00:1')

def test_ntoa11(self):
b = binascii.unhexlify(b'00000000000000000000ffff0a000001')
t = ntoa6(b)
self.assertEqual(t, b'::ffff:10.0.0.1')
self.assertEqual(t, '::ffff:10.0.0.1')

def test_ntoa12(self):
b = binascii.unhexlify(b'000000000000000000000000ffffffff')
t = ntoa6(b)
self.assertEqual(t, b'::255.255.255.255')
self.assertEqual(t, '::255.255.255.255')

def test_ntoa13(self):
b = binascii.unhexlify(b'00000000000000000000ffffffffffff')
t = ntoa6(b)
self.assertEqual(t, b'::ffff:255.255.255.255')
self.assertEqual(t, '::ffff:255.255.255.255')

def test_ntoa14(self):
b = binascii.unhexlify(b'0000000000000000000000000001ffff')
t = ntoa6(b)
self.assertEqual(t, b'::0.1.255.255')
self.assertEqual(t, '::0.1.255.255')

def test_bad_ntoa1(self):
def bad():
Expand Down Expand Up @@ -199,7 +199,7 @@ def bad():
self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr))

def test_rfc5952_section_4_2_2(self):
addr = b'2001:db8:0:1:1:1:1:1'
addr = '2001:db8:0:1:1:1:1:1'
b1 = aton6(addr)
t1 = ntoa6(b1)
self.assertEqual(t1, addr)
Expand Down

0 comments on commit 9c9ae1c

Please sign in to comment.