done with unit tests for Cidr
[python-rwhoisd.git] / test / TestCidr.py
index 47e13d1..37eac36 100644 (file)
@@ -141,6 +141,44 @@ class CreateCidrTest(unittest.TestCase):
                 continue
             self.failIf(res)
 
+class TestCidrMethods(unittest.TestCase):
+
+    data = [ "127.0.0.0/24", "127.0.0.1/32", "24.232.119.192/26",
+             "24.232.119.0/24", "24.224.0.0/11", "216.168.111.0/27",
+             "127.0.0.2/31", "127.0.0.16/32", "3ffe:4:201e:beef::/64",
+             "2001:3c01::/32", "3ffe:b03d:fc1e::2002:1010:deac/128",
+             "3ffe:b03d:fc1e:2002:1010:2a2a:7:1/128",
+             "3ffe:b03d::/32" ]
+
+    def teststr(self):
+        for a in self.data:
+            c = Cidr.valid_cidr(a)
+            self.assert_(c)
+            self.assertEquals(a, str(c))
+
+    def testrepr(self):
+        for a in self.data:
+            c = Cidr.valid_cidr(a)
+            self.assert_(c)
+            self.assertEquals("<" + a + ">", repr(c))
+
+    def testcmp(self):
+        a = Cidr.valid_cidr("127.0.0.0/24")
+        b = Cidr.valid_cidr("127.0.0.0/23")
+        self.assert_(a)
+        self.assert_(b)
+        self.assert_(a > b)
+        self.assert_(b < a)
+        c = Cidr.valid_cidr("127.0.0.100/24")
+        self.assert_(c)
+        self.assert_(a == c)
+        b.netlen = 24
+        b.calc()
+        self.assert_(b == a)
+        self.assertEquals(a.netmask(), "255.255.255.0")
+        self.assertEquals(a.length(), 256)
+        self.assertEquals(a.end(), "127.0.0.255")
+
 class TestCidrSort(unittest.TestCase):
 
     unsorted_list = [ "127.0.0.0/24",
@@ -169,21 +207,53 @@ class TestCidrSort(unittest.TestCase):
         self.assertEquals(self.unsorted_list, self.sorted_list)
 
 
-class TestCidrSearches(unittest.TestCase):
+class TestCidrScope(unittest.TestCase):
 
-    def testSupernet(self):
-        pass
+    def testScope(self):
+        data = [ ("127.0.0.1/24", "127.0.0.15/25"),
+                 ("127.0.0.0/23", "127.0.0.0/24"),
+                 ("224.45.0.0/16", "224.45.126.87"),
+                 ("0.0.0.0/0", "1.2.3.4/32"),
+                 ("2001:23c1::0/32", "2001:23c1:45:ffff::0/64"),
+                 ("2001:23c1::0/32", "2001:23c1::0/33") ]
 
-    def testSubnet(self):
-        pass
+        for a, b in data:
+            a = Cidr.valid_cidr(a)
+            b = Cidr.valid_cidr(b)
+            self.assert_(a.is_supernet(b))
+            self.assert_(b.is_subnet(a))
 
 class testNetblockConversion(unittest.TestCase):
 
-    def testNetblockV4(self):
-        pass
+    data = [ ("192.168.10.0", "192.168.10.255", 256),
+             ("192.168.10.0", "192.168.10.63", 64),
+             ("172.16.0.0", "172.16.127.255", 32768),
+             ("24.33.41.22", "24.33.41.37", 16),
+             ("196.11.1.0", "196.11.30.255", 7680),
+             ("192.247.1.0", "192.247.10.255", 2560),
+             ("10.131.43.4", "10.131.44.7", 260),
+             ("3ffe:4:5::", "3ffe:4:5::ffff", 65536),
+             ("3ffe:4:5::", "3ffe:4:6::1", 1208925819614629174706178L) ]
+
+    def blocks_to_length(self, blocks):
+        l = 0;
+        for b in blocks:
+            l += b.length()
+        return l
+
+    def blocks_to_netblock(self, blocks):
+        start = blocks[0].to_netblock()[0]
+        end = blocks[-1].to_netblock()[1]
+        return (start, end)
 
-    def testNetblockV6(self):
-        pass
+    def testNetblocks(self):
+        for start, end, length in self.data:
+            blocks = Cidr.netblock_to_cidr(start, end)
+            l = self.blocks_to_length(blocks)
+            self.assertEquals(l, length)
+            s, e = self.blocks_to_netblock(blocks)
+            self.assertEquals(start, s)
+            self.assertEquals(end, e)
 
 
 if __name__ == "__main__":