]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
more v6 strictness; more unit tests
authorBob Halley <halley@nominum.com>
Wed, 13 Jul 2011 02:00:28 +0000 (19:00 -0700)
committerBob Halley <halley@nominum.com>
Wed, 13 Jul 2011 02:00:28 +0000 (19:00 -0700)
dns/ipv6.py
tests/name.py
tests/ntoaaton.py

index 4b9312b3e28f1240f5403c752c58ff564cd2c924..e2bcecd111210e6d824725ae3cc9f0fb9ea074b4 100644 (file)
@@ -84,7 +84,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'.*::$')
 
@@ -108,16 +108,8 @@ 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), b[0], b[1], b[2], b[3])
     #
     # Try to turn '::<whatever>' into ':<whatever>'; if no match try to
     # turn '<whatever>::' into '<whatever>:'
@@ -161,5 +153,5 @@ def inet_aton(text):
     #
     try:
         return bytes.fromhex(text)
-    except TypeError:
+    except:
         raise dns.exception.SyntaxError
index cdb2488047004a93e420c59d7f9c324b1941b9cc..213940932e21f927617e5e3c41966164c46533b2 100644 (file)
@@ -663,12 +663,12 @@ class NameTestCase(unittest.TestCase):
     def testBadReverseIPv4(self):
         def bad():
             n = dns.reversename.from_address('127.0.foo.1')
-        self.assertRaises(socket.error, bad)
+        self.assertRaises(dns.exception.SyntaxError, bad)
 
     def testBadReverseIPv6(self):
         def bad():
             n = dns.reversename.from_address('::1::1')
-        self.assertRaises(socket.error, bad)
+        self.assertRaises(dns.exception.SyntaxError, bad)
 
     def testForwardIPv4(self):
         n = dns.name.from_text('1.0.0.127.in-addr.arpa.')
index b3cf33f79e3c80e7abf41848d1e1837a0d5cd1cd..b103c21a851598659a66d5ce87d1c65d47e738b8 100644 (file)
 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.assertTrue(a == b'\x00' * 16)
 
     def test_aton2(self):
-        a = dns.ipv6.inet_aton('::1')
+        a = aton6('::1')
         self.assertTrue(a == b'\x00' * 15 + b'\x01')
 
     def test_aton3(self):
-        a = dns.ipv6.inet_aton('::10.0.0.1')
+        a = aton6('::10.0.0.1')
         self.assertTrue(a == b'\x00' * 12 + b'\x0a\x00\x00\x01')
 
     def test_aton4(self):
-        a = dns.ipv6.inet_aton('abcd::dcba')
+        a = aton6('abcd::dcba')
         self.assertTrue(a == b'\xab\xcd' + b'\x00' * 12 + b'\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.assertTrue(a == \
                         bytes.fromhex('00010002000300040005000600070008'))
 
     def test_bad_aton1(self):
         def bad():
-            a = dns.ipv6.inet_aton('abcd:dcba')
+            a = aton6('abcd:dcba')
         self.assertRaises(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.assertRaises(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.assertRaises(dns.exception.SyntaxError, bad)
 
     def test_aton1(self):
-        a = dns.ipv6.inet_aton('::')
+        a = aton6('::')
         self.assertTrue(a == b'\x00' * 16)
 
     def test_aton2(self):
-        a = dns.ipv6.inet_aton('::1')
+        a = aton6('::1')
         self.assertTrue(a == b'\x00' * 15 + b'\x01')
 
     def test_aton3(self):
-        a = dns.ipv6.inet_aton('::10.0.0.1')
+        a = aton6('::10.0.0.1')
         self.assertTrue(a == b'\x00' * 12 + b'\x0a\x00\x00\x01')
 
     def test_aton4(self):
-        a = dns.ipv6.inet_aton('abcd::dcba')
+        a = aton6('abcd::dcba')
         self.assertTrue(a == b'\xab\xcd' + b'\x00' * 12 + b'\xdc\xba')
 
     def test_ntoa1(self):
         b = bytes.fromhex('00010002000300040005000600070008')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '1:2:3:4:5:6:7:8')
 
     def test_ntoa2(self):
         b = b'\x00' * 16
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::')
 
     def test_ntoa3(self):
         b = b'\x00' * 15 + b'\x01'
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::1')
 
     def test_ntoa4(self):
         b = b'\x80' + b'\x00' * 15
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '8000::')
 
     def test_ntoa5(self):
         b = b'\x01\xcd' + b'\x00' * 12 + b'\x03\xef'
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '1cd::3ef')
 
     def test_ntoa6(self):
         b = bytes.fromhex('ffff00000000ffff000000000000ffff')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == 'ffff:0:0:ffff::ffff')
 
     def test_ntoa7(self):
         b = bytes.fromhex('00000000ffff000000000000ffffffff')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '0:0:ffff::ffff:ffff')
 
     def test_ntoa8(self):
         b = bytes.fromhex('ffff0000ffff00000000ffff00000000')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == 'ffff:0:ffff::ffff:0:0')
 
     def test_ntoa9(self):
         b = bytes.fromhex('0000000000000000000000000a000001')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::10.0.0.1')
 
     def test_ntoa10(self):
         b = bytes.fromhex('0000000000000000000000010a000001')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::1:a00:1')
 
     def test_ntoa11(self):
         b = bytes.fromhex('00000000000000000000ffff0a000001')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::ffff:10.0.0.1')
 
     def test_ntoa12(self):
         b = bytes.fromhex('000000000000000000000000ffffffff')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::255.255.255.255')
 
     def test_ntoa13(self):
         b = bytes.fromhex('00000000000000000000ffffffffffff')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::ffff:255.255.255.255')
 
     def test_ntoa14(self):
         b = bytes.fromhex('0000000000000000000000000001ffff')
-        t = dns.ipv6.inet_ntoa(b)
+        t = ntoa6(b)
         self.assertTrue(t == '::0.1.255.255')
 
     def test_bad_ntoa1(self):
         def bad():
-            a = dns.ipv6.inet_ntoa('')
+            a = ntoa6('')
         self.assertRaises(ValueError, bad)
 
     def test_bad_ntoa2(self):
         def bad():
-            a = dns.ipv6.inet_ntoa(b'\x00' * 17)
+            a = ntoa6(b'\x00' * 17)
         self.assertRaises(ValueError, bad)
 
+    def test_good_v4_aton(self):
+        pairs = [('1.2.3.4', b'\x01\x02\x03\x04'),
+                 ('255.255.255.255', b'\xff\xff\xff\xff'),
+                 ('0.0.0.0', b'\x00\x00\x00\x00')]
+        for (t, b) in pairs:
+            b1 = aton4(t)
+            t1 = ntoa4(b1)
+            self.assertTrue(b1 == b)
+            self.assertTrue(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.assertRaises(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.assertRaises(dns.exception.SyntaxError, make_bad(addr))
+
 if __name__ == '__main__':
     unittest.main()