block_len -= cur_len
netlen = largest_prefix(block_len + 1, max_netlen, msb_mask)
return res
-
-# test driver
-if __name__ == "__main__":
- import sys
- a = new("127.00.000.1/24")
- b = new("127.0.0.1", 32)
- c = new("24.232.119.192", 26)
- d = new("24.232.119.0", 24)
- e = new("24.224.0.0", 11)
- f = new("216.168.111.0/27");
- g = new("127.0.0.2/31");
- h = new("127.0.0.16/32")
- i = new("3ffe:4:201e:beef::0/64");
- j = new("2001:3c01::/32")
-
- print f.addr
- print j.addr
-
- try:
- bad = new("24.261.119.0", 32)
- except ValueError, x:
- print "error:", x
-
- print "cidr:", a, "num addresses:", a.length(), "ending address", \
- a.end(), "netmask", a.netmask()
-
- print "cidr:", j, "num addresses:", j.length(), "ending address", \
- j.end(), "netmask", j.netmask()
-
- clist = [a, b, c, d, e, f, g, h, i , j]
- print "unsorted list of cidr objects:\n ", clist
-
-
- clist.sort()
- print "sorted list of cidr object:\n ", clist
-
- k = new("2001:3c01::1:0", 120)
- print "supernet: ", str(j), " supernet of ", str(k), "? ", \
- str(j.is_supernet(k))
- print "supernet: ", str(k), " supernet of ", str(j), "? ", \
- str(k.is_supernet(j))
- print "subnet: ", str(j), " subnet of ", str(k), "? ", \
- str(j.is_subnet(k))
- print "subnet: ", str(k), " subnet of ", str(j), "? ", \
- str(k.is_subnet(j))
-
- netblocks = [ ("192.168.10.0", "192.168.10.255"),
- ("192.168.10.0", "192.168.10.63"),
- ("172.16.0.0", "172.16.127.255"),
- ("24.33.41.22", "24.33.41.37"),
- ("196.11.1.0", "196.11.30.255"),
- ("192.247.1.0", "192.247.10.255"),
- ("10.131.43.3", "10.131.44.7"),
- ("3ffe:4:5::", "3ffe:4:5::ffff"),
- ("3ffe:4:5::", "3ffe:4:6::1")]
-
- for start, end in netblocks:
- print "netblock %s - %s:" % (start, end)
- blocks = netblock_to_cidr(start, end)
- print blocks
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",
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__":