start of test driver impl.
authorDavid Blacka <david@blacka.com>
Fri, 27 Jun 2008 02:54:53 +0000 (22:54 -0400)
committerDavid Blacka <david@blacka.com>
Fri, 27 Jun 2008 02:54:53 +0000 (22:54 -0400)
rwhoisd/Cidr.py
test/TestCidr.py [new file with mode: 0644]

index 88d46d3..fb86a10 100644 (file)
@@ -37,6 +37,20 @@ def new(address, netlen = -1):
         return CidrV6(address, netlen)
     return CidrV4(address, netlen)
 
+def valid_cidr(address):
+    """Returns the converted Cidr object if 'address' is valid CIDR
+    notation, False if not.  For the purposes of this module, valid
+    CIDR notation consists of a IPv4 or IPv6 address with an optional
+    trailing "/netlen"."""
+
+    if isinstance(address, Cidr): return address
+    try:
+        c = new(address)
+        return c
+    except (ValueError, socket.error):
+        return False
+
+
 class Cidr:
     """A class representing a generic CIDRized network value."""
 
@@ -246,19 +260,6 @@ class CidrV6(Cidr):
         return self._base_mask(CidrV6.base_mask << (128 - len))
 
 
-def valid_cidr(address):
-    """Returns the converted Cidr object if 'address' is valid CIDR
-    notation, False if not.  For the purposes of this module, valid
-    CIDR notation consists of a IPv4 or IPv6 address with an optional
-    trailing "/netlen"."""
-
-    if isinstance(address, Cidr): return address
-    try:
-        c = new(address)
-        return c
-    except (ValueError, socket.error):
-        return False
-
 def netblock_to_cidr(start, end):
     """Convert an arbitrary network block expressed as a start and end
     address (inclusive) into a series of valid CIDR blocks."""
diff --git a/test/TestCidr.py b/test/TestCidr.py
new file mode 100644 (file)
index 0000000..83ce7e2
--- /dev/null
@@ -0,0 +1,107 @@
+# This file is part of python-rwhoisd
+#
+# Copyright (C) 2008 David E. Blacka
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# 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 unittest, types
+
+class TestCidr(unittest.TestCase):
+
+    known_good_v4_cidr = [ ("127.00.000.1/24",      0x7F000000, 0xFFFFFF00),
+                           (("127.0.0.1", 32),      0x7F000001, 0xFFFFFFFF),
+                           (("24.232.119.192", 26), 0x18E877C0, 0xFFFFFFC0),
+                           (("24.232.119.0", 24),   0x18E87700, 0xFFFFFF00),
+                           (("24.224.0.0", 11),     0x18E00000, 0xFFE00000),
+                           ("216.168.111.0/27",     0xD8A86F00, 0xFFFFFFE0),
+                           ("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."""
+        for args, value, mask in self.known_good_v4_cidr:
+            if type(args) == types.TupleType:
+                cidr = Cidr.CidrV4(args[0], args[1])
+            else:
+                cidr = Cidr.CidrV4(args)
+            self.assertEquals(cidr.numaddr, value)
+            self.assertEquals(cidr.mask, mask)
+
+    def testCreateV6(self):
+        "createV6 should give known results with known input."""
+        for args, value, mask in self.known_good_v6_cidr:
+            if type(args) == types.TupleType:
+                cidr = Cidr.CidrV6(args[0], args[1])
+            else:
+                cidr = Cidr.CidrV6(args)
+            self.assertEquals(cidr.numaddr, value)
+            self.assertEquals(cidr.mask, mask)
+
+    def testCreate(self):
+        """create should give known results with known input."""
+        all_good = self.known_good_v4_cidr + self.known_good_v6_cidr
+        for args, value, mask in all_good:
+            if type(args) == types.TupleType:
+                cidr = Cidr.new(args[0], args[1])
+            else:
+                cidr = Cidr.new(args)
+            self.assertEquals(cidr.numaddr, value)
+            self.assertEquals(cidr.mask, mask)
+
+
+    def testSort(self):
+        pass
+
+    def testSupernet(self):
+        pass
+
+    def testSubnet(self):
+        pass
+
+    def testNetblockV4(self):
+        pass
+
+    def testNetblockV6(self):
+        pass
+
+
+if __name__ == "__main__":
+    unittest.main()