checkpoint
[python-rwhoisd.git] / test / TestCidr.py
index 83ce7e2..47e13d1 100644 (file)
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 # USA
 
-import sys, os
 
-this_path = sys.path[0]
-rwhoisd_path = os.path.join(this_path, "..", "rwhoisd")
-if os.path.isdir(rwhoisd_path):
-    sys.path.append(rwhoisd_path)
-
-import Cidr
+import path
+import Cidr, socket
 import unittest, types
 
-class TestCidr(unittest.TestCase):
+class CreateCidrTest(unittest.TestCase):
+    """Test basic creation of Cidr objects."""
 
     known_good_v4_cidr = [ ("127.00.000.1/24",      0x7F000000, 0xFFFFFF00),
                            (("127.0.0.1", 32),      0x7F000001, 0xFFFFFFFF),
@@ -38,22 +34,6 @@ class TestCidr(unittest.TestCase):
                            ("127.0.0.2/31",         0x7F000002, 0xFFFFFFFE),
                            ("127.0.0.16/32",        0x7F000010, 0xFFFFFFFF) ]
 
-    known_good_v6_cidr = [ ("3ffe:4:201e:beef::0/64", 
-                            0x3FFE0004201EBEEF0000000000000000L, 
-                            0xFFFFFFFFFFFFFFFF0000000000000000L),
-                           ("2001:3c01::/32", 
-                            0x20013C01000000000000000000000000L, 
-                            0xFFFFFFFF000000000000000000000000L),
-                           (("3ffe:b03d:fc1e::2002:1010:deaC", 126),
-                            0x3FFEB03DFC1E0000000020021010DEACL,
-                            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCL),
-                           ("3ffe:b03d:fc1e:2002:1010:2a2a:7:1",
-                            0x3FFEB03dFC1E200210102A2A00070001L,
-                            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL),
-                           ("3ffe:b03d:fc1e:2002:1010:2a2a:7:1/32",
-                            0x3FFEB03D000000000000000000000000L,
-                            0xFFFFFFFF000000000000000000000000L) ]
-
 
     def testCreateV4(self):
         """createV4 should give known results with known input."""
@@ -65,6 +45,26 @@ class TestCidr(unittest.TestCase):
             self.assertEquals(cidr.numaddr, value)
             self.assertEquals(cidr.mask, mask)
 
+
+    known_good_v6_cidr = [ ("3ffe:4:201e:beef::0/64",
+                            0x3FFE0004201EBEEF0000000000000000L,
+                            0xFFFFFFFFFFFFFFFF0000000000000000L),
+                           ("2001:3c01::/32",
+                            0x20013C01000000000000000000000000L,
+                            0xFFFFFFFF000000000000000000000000L),
+                           (("3ffe:b03d:fc1e::2002:1010:deaC", 126),
+                            0x3FFEB03DFC1E0000000020021010DEACL,
+                            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCL),
+                           ("3ffe:b03d:fc1e:2002:1010:2a2a:7:1",
+                            0x3FFEB03dFC1E200210102A2A00070001L,
+                            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL),
+                           ("3ffe:b03d:fc1e:2002:1010:2a2a:7:1/32",
+                            0x3FFEB03D000000000000000000000000L,
+                            0xFFFFFFFF000000000000000000000000L),
+                           ("3ffe:b03d::affd:127.0.0.1",
+                            0x3FFEB03D000000000000AFFD7F000001L,
+                            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL)]
+
     def testCreateV6(self):
         "createV6 should give known results with known input."""
         for args, value, mask in self.known_good_v6_cidr:
@@ -87,8 +87,89 @@ class TestCidr(unittest.TestCase):
             self.assertEquals(cidr.mask, mask)
 
 
+    known_bad_v4_cidr = [ ("24.261.119.0", 32), # 2nd octet too large
+                          "",                   # empty string
+                          "22.a.231.11/24",     # contains alpha
+                          "45.61.22.230.1/32",  # too many octets
+                          "70.140.81.0/34",     # length too long
+                          "3ffe:3c01:4::1/32"   # ipv6
+                          ]
+
+    def testCreateBadV4(self):
+        for c in self.known_bad_v4_cidr:
+            if type(c) == types.TupleType:
+                self.assertRaises(socket.error, Cidr.CidrV4, c[0], c[1])
+            else:
+                self.assertRaises(socket.error, Cidr.CidrV4, c)
+
+    known_bad_v6_cidr = [
+        ":/32",
+        ":::/64",
+        "1::2::3",
+        "::3::/128",
+        "12345::1",
+        "2001:100g::1/64",
+        "1:2:3:4:5:6:7:4.3.2.1",
+        "127.0.0.1/24"
+        ]
+
+    def testCreateBadV6(self):
+        for c in self.known_bad_v6_cidr:
+            self.assertRaises(socket.error, Cidr.CidrV6, c)
+
+    def testValidCidr(self):
+        for c in [ x[0] for x in self.known_good_v4_cidr ]:
+            if type(c) == types.TupleType:
+                c = "%s/%d" % (c[0], c[1])
+            res = Cidr.valid_cidr(c)
+            self.assert_(isinstance(res, Cidr.Cidr))
+        for c in [ x[0] for x in self.known_good_v6_cidr ]:
+            if type(c) == types.TupleType:
+                c = "%s/%d" % (c[0], c[1])
+            res = Cidr.valid_cidr(c)
+            self.assert_(isinstance(res, Cidr.Cidr))
+        for c in self.known_bad_v4_cidr:
+            if type(c) == types.TupleType:
+                c = "%s/%d" % (c[0], c[1])
+            if ':' in c:
+                continue
+            res = Cidr.valid_cidr(c)
+            self.assertEquals(res, False)
+        for c in self.known_bad_v6_cidr:
+            res = Cidr.valid_cidr(c)
+            if not ':' in c:
+                continue
+            self.failIf(res)
+
+class TestCidrSort(unittest.TestCase):
+
+    unsorted_list = [ "127.0.0.0/24",
+                      "127.0.0.0/23",
+                      "127.0.0.0/25",
+                      "80.32.23.71/32",
+                      "8.44.55.66/8",
+                      "2001:2c01::1/64",
+                      "3ffe:4::5",
+                      "1:2:3:4:5:6:7:8/127" ]
+    sorted_list = [ "8.44.55.66/8",
+                    "80.32.23.71/32",
+                    "127.0.0.0/23",
+                    "127.0.0.0/24",
+                    "127.0.0.0/25",
+                    "1:2:3:4:5:6:7:8/127",
+                    "2001:2c01::1/64",
+                    "3ffe:4::5" ]
+
+    def setUp(self):
+        self.unsorted_list = [ Cidr.new(x) for x in self.unsorted_list ]
+        self.sorted_list = [ Cidr.new(x) for x in self.sorted_list ]
+
     def testSort(self):
-        pass
+        self.unsorted_list.sort()
+        self.assertEquals(self.unsorted_list, self.sorted_list)
+
+
+class TestCidrSearches(unittest.TestCase):
 
     def testSupernet(self):
         pass
@@ -96,6 +177,8 @@ class TestCidr(unittest.TestCase):
     def testSubnet(self):
         pass
 
+class testNetblockConversion(unittest.TestCase):
+
     def testNetblockV4(self):
         pass