summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Halley <halley@nominum.com>2011-07-12 19:00:13 -0700
committerBob Halley <halley@nominum.com>2011-07-12 19:00:13 -0700
commitc1bc09d236fe7784a303cd601a8a973d1758e3a6 (patch)
tree3381a21cb972701b7b1e47e3c538048680efe02c
parente189940dec42495869483f71b0be119bf8956d60 (diff)
downloaddnspython-c1bc09d236fe7784a303cd601a8a973d1758e3a6.tar.gz
more v6 strictness; more unit tests
-rw-r--r--dns/ipv6.py15
-rw-r--r--tests/name.py4
-rw-r--r--tests/ntoaaton.py97
3 files changed, 75 insertions, 41 deletions
diff --git a/dns/ipv6.py b/dns/ipv6.py
index 78fe82e..69db34a 100644
--- a/dns/ipv6.py
+++ b/dns/ipv6.py
@@ -89,7 +89,7 @@ def inet_ntoa(address):
hex = ':'.join(chunks)
return hex
-_v4_ending = re.compile(r'(.*):(\d+)\.(\d+)\.(\d+)\.(\d+)$')
+_v4_ending = re.compile(r'(.*):(\d+\.\d+\.\d+\.\d+)$')
_colon_colon_start = re.compile(r'::.*')
_colon_colon_end = re.compile(r'.*::$')
@@ -113,16 +113,9 @@ def inet_aton(text):
#
m = _v4_ending.match(text)
if not m is None:
- try:
- b1 = int(m.group(2))
- b2 = int(m.group(3))
- b3 = int(m.group(4))
- b4 = int(m.group(5))
- except:
- raise dns.exception.SyntaxError
- if b1 > 255 or b2 > 255 or b3 > 255 or b4 > 255:
- raise dns.exception.SyntaxError
- text = "%s:%04x:%04x" % (m.group(1), b1 * 256 + b2, b3 * 256 + b4)
+ b = dns.ipv4.inet_aton(m.group(2))
+ text = "%s:%02x%02x:%02x%02x" % (m.group(1), ord(b[0]), ord(b[1]),
+ ord(b[2]), ord(b[3]))
#
# Try to turn '::<whatever>' into ':<whatever>'; if no match try to
# turn '<whatever>::' into '<whatever>:'
diff --git a/tests/name.py b/tests/name.py
index 55815f8..e30e43d 100644
--- a/tests/name.py
+++ b/tests/name.py
@@ -662,12 +662,12 @@ class NameTestCase(unittest.TestCase):
def testBadReverseIPv4(self):
def bad():
n = dns.reversename.from_address('127.0.foo.1')
- self.failUnlessRaises(socket.error, bad)
+ self.failUnlessRaises(dns.exception.SyntaxError, bad)
def testBadReverseIPv6(self):
def bad():
n = dns.reversename.from_address('::1::1')
- self.failUnlessRaises(socket.error, bad)
+ self.failUnlessRaises(dns.exception.SyntaxError, bad)
def testForwardIPv4(self):
n = dns.name.from_text('1.0.0.127.in-addr.arpa.')
diff --git a/tests/ntoaaton.py b/tests/ntoaaton.py
index a1c191f..9d8bedd 100644
--- a/tests/ntoaaton.py
+++ b/tests/ntoaaton.py
@@ -16,141 +16,182 @@
import unittest
import dns.exception
+import dns.ipv4
import dns.ipv6
+# for convenience
+aton4 = dns.ipv4.inet_aton
+ntoa4 = dns.ipv4.inet_ntoa
+aton6 = dns.ipv6.inet_aton
+ntoa6 = dns.ipv6.inet_ntoa
+
+v4_bad_addrs = ['256.1.1.1', '1.1.1', '1.1.1.1.1', '01.1.1.1',
+ '+1.1.1.1', '1.1.1.1+', '1..2.3.4', '.1.2.3.4',
+ '1.2.3.4.']
+
class NtoAAtoNTestCase(unittest.TestCase):
def test_aton1(self):
- a = dns.ipv6.inet_aton('::')
+ a = aton6('::')
self.failUnless(a == '\x00' * 16)
def test_aton2(self):
- a = dns.ipv6.inet_aton('::1')
+ a = aton6('::1')
self.failUnless(a == '\x00' * 15 + '\x01')
def test_aton3(self):
- a = dns.ipv6.inet_aton('::10.0.0.1')
+ a = aton6('::10.0.0.1')
self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01')
def test_aton4(self):
- a = dns.ipv6.inet_aton('abcd::dcba')
+ a = aton6('abcd::dcba')
self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
def test_aton5(self):
- a = dns.ipv6.inet_aton('1:2:3:4:5:6:7:8')
+ a = aton6('1:2:3:4:5:6:7:8')
self.failUnless(a == \
'00010002000300040005000600070008'.decode('hex_codec'))
def test_bad_aton1(self):
def bad():
- a = dns.ipv6.inet_aton('abcd:dcba')
+ a = aton6('abcd:dcba')
self.failUnlessRaises(dns.exception.SyntaxError, bad)
def test_bad_aton2(self):
def bad():
- a = dns.ipv6.inet_aton('abcd::dcba::1')
+ a = aton6('abcd::dcba::1')
self.failUnlessRaises(dns.exception.SyntaxError, bad)
def test_bad_aton3(self):
def bad():
- a = dns.ipv6.inet_aton('1:2:3:4:5:6:7:8:9')
+ a = aton6('1:2:3:4:5:6:7:8:9')
self.failUnlessRaises(dns.exception.SyntaxError, bad)
def test_aton1(self):
- a = dns.ipv6.inet_aton('::')
+ a = aton6('::')
self.failUnless(a == '\x00' * 16)
def test_aton2(self):
- a = dns.ipv6.inet_aton('::1')
+ a = aton6('::1')
self.failUnless(a == '\x00' * 15 + '\x01')
def test_aton3(self):
- a = dns.ipv6.inet_aton('::10.0.0.1')
+ a = aton6('::10.0.0.1')
self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01')
def test_aton4(self):
- a = dns.ipv6.inet_aton('abcd::dcba')
+ a = aton6('abcd::dcba')
self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
def test_ntoa1(self):
b = '00010002000300040005000600070008'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '1:2:3:4:5:6:7:8')
def test_ntoa2(self):
b = '\x00' * 16
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::')
def test_ntoa3(self):
b = '\x00' * 15 + '\x01'
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::1')
def test_ntoa4(self):
b = '\x80' + '\x00' * 15
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '8000::')
def test_ntoa5(self):
b = '\x01\xcd' + '\x00' * 12 + '\x03\xef'
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '1cd::3ef')
def test_ntoa6(self):
b = 'ffff00000000ffff000000000000ffff'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == 'ffff:0:0:ffff::ffff')
def test_ntoa7(self):
b = '00000000ffff000000000000ffffffff'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '0:0:ffff::ffff:ffff')
def test_ntoa8(self):
b = 'ffff0000ffff00000000ffff00000000'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == 'ffff:0:ffff::ffff:0:0')
def test_ntoa9(self):
b = '0000000000000000000000000a000001'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::10.0.0.1')
def test_ntoa10(self):
b = '0000000000000000000000010a000001'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::1:a00:1')
def test_ntoa11(self):
b = '00000000000000000000ffff0a000001'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::ffff:10.0.0.1')
def test_ntoa12(self):
b = '000000000000000000000000ffffffff'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::255.255.255.255')
def test_ntoa13(self):
b = '00000000000000000000ffffffffffff'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::ffff:255.255.255.255')
def test_ntoa14(self):
b = '0000000000000000000000000001ffff'.decode('hex_codec')
- t = dns.ipv6.inet_ntoa(b)
+ t = ntoa6(b)
self.failUnless(t == '::0.1.255.255')
def test_bad_ntoa1(self):
def bad():
- a = dns.ipv6.inet_ntoa('')
+ a = ntoa6('')
self.failUnlessRaises(ValueError, bad)
def test_bad_ntoa2(self):
def bad():
- a = dns.ipv6.inet_ntoa('\x00' * 17)
+ a = ntoa6('\x00' * 17)
self.failUnlessRaises(ValueError, bad)
+ def test_good_v4_aton(self):
+ pairs = [('1.2.3.4', '\x01\x02\x03\x04'),
+ ('255.255.255.255', '\xff\xff\xff\xff'),
+ ('0.0.0.0', '\x00\x00\x00\x00')]
+ for (t, b) in pairs:
+ b1 = aton4(t)
+ t1 = ntoa4(b1)
+ self.failUnless(b1 == b)
+ self.failUnless(t1 == t)
+
+ def test_bad_v4_aton(self):
+ def make_bad(a):
+ def bad():
+ return aton4(a)
+ return bad
+ for addr in v4_bad_addrs:
+ self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr))
+
+ def test_bad_v6_aton(self):
+ addrs = ['+::0', '0::0::', '::0::', '1:2:3:4:5:6:7:8:9',
+ ':::::::']
+ embedded = ['::' + x for x in v4_bad_addrs]
+ addrs.extend(embedded)
+ def make_bad(a):
+ def bad():
+ x = aton6(a)
+ return bad
+ for addr in addrs:
+ self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr))
+
if __name__ == '__main__':
unittest.main()