From 249291cbc8921ae126a2e4d092710a1c0d37c870 Mon Sep 17 00:00:00 2001 From: David Blacka Date: Fri, 27 Jun 2008 22:40:05 -0400 Subject: [PATCH] checkpoint --- rwhoisd/Cidr.py | 7 ++- test/.gitignore | 1 + test/TestCidr.py | 133 ++++++++++++++++++++++++++++++++++++++--------- test/path.py | 6 +++ 4 files changed, 118 insertions(+), 29 deletions(-) create mode 100644 test/.gitignore create mode 100644 test/path.py diff --git a/rwhoisd/Cidr.py b/rwhoisd/Cidr.py index fb86a10..e98d8bd 100644 --- a/rwhoisd/Cidr.py +++ b/rwhoisd/Cidr.py @@ -47,7 +47,7 @@ def valid_cidr(address): try: c = new(address) return c - except (ValueError, socket.error): + except socket.error: return False @@ -65,7 +65,7 @@ class Cidr: return if not self.is_valid_cidr(address): - raise ValueError, \ + raise socket.error, \ repr(address) + " is not a valid CIDR representation" if netlen < 0: @@ -99,7 +99,7 @@ class Cidr: address is numerically less or if the block is larger. That is, supernets will sort before subnets. This ordering allows for an efficient search for subnets of a given network.""" - + if not isinstance(other, Cidr): return -1 res = self._base_mask(self.numaddr) - other._base_mask(other.numaddr) if res == 0: res = self.netlen - other.netlen if res < 0: return -1 @@ -236,7 +236,6 @@ class CidrV6(Cidr): self._convert_ipstr(address) return True except socket.error, e: - print "Failed to convert address string '%s': " + str(e) % (address) return False def _base_mask(self, numaddr): diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/test/TestCidr.py b/test/TestCidr.py index 83ce7e2..47e13d1 100644 --- a/test/TestCidr.py +++ b/test/TestCidr.py @@ -17,17 +17,13 @@ # 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 diff --git a/test/path.py b/test/path.py new file mode 100644 index 0000000..4a00750 --- /dev/null +++ b/test/path.py @@ -0,0 +1,6 @@ +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) -- 2.36.6