From: David Blacka Date: Sun, 13 Jul 2008 19:28:36 +0000 (-0400) Subject: Add some test cases for better unit test code coverage. Fix bug found by new TestCom... X-Git-Url: https://blacka.com/cgi-bin/gitweb.cgi?p=python-rwhoisd.git;a=commitdiff_plain;h=4b00a29a6f1d5e2320f4fde8e9044581cc389f7f Add some test cases for better unit test code coverage. Fix bug found by new TestCompat test case (typo) --- diff --git a/rwhoisd/v6addr.py b/rwhoisd/v6addr.py index 2a33dc0..a169e5e 100644 --- a/rwhoisd/v6addr.py +++ b/rwhoisd/v6addr.py @@ -133,7 +133,7 @@ def inet_pton(af, ip): def inet_ntop(af, packed_ip): if af == socket.AF_INET: - return socket.inet.ntoa(packed_ip) + return socket.inet_ntoa(packed_ip) if af == socket.AF_INET6: return v6addr_to_str(packed_ip) raise socket.error("Address family not supported by protocol") diff --git a/test/TestCidr.py b/test/TestCidr.py index 37eac36..c177880 100644 --- a/test/TestCidr.py +++ b/test/TestCidr.py @@ -86,6 +86,10 @@ class CreateCidrTest(unittest.TestCase): self.assertEquals(cidr.numaddr, value) self.assertEquals(cidr.mask, mask) + def testClone(self): + cidr1 = Cidr.new("127.0.0.0/32") + cidr2 = cidr1.clone() + self.assertEquals(cidr1, cidr2) known_bad_v4_cidr = [ ("24.261.119.0", 32), # 2nd octet too large "", # empty string @@ -255,6 +259,14 @@ class testNetblockConversion(unittest.TestCase): self.assertEquals(start, s) self.assertEquals(end, e) + def testBadnetblock(self): + res = Cidr.netblock_to_cidr("127.0.0.257", "127.0.0.10.0") + assert(res == None) + res = Cidr.netblock_to_cidr("127.0.0.1", "127.0.256.255") + assert(res == None) + res = Cidr.netblock_to_cidr("::1", "127.0.0.2") + assert(res == None) + if __name__ == "__main__": unittest.main() diff --git a/test/TestV6addr.py b/test/TestV6addr.py index adc103f..a99fdf5 100644 --- a/test/TestV6addr.py +++ b/test/TestV6addr.py @@ -39,8 +39,8 @@ class TestGood(unittest.TestCase): class TestBad(unittest.TestCase): - data = [ ":", ":::", "1::2::3", "::3::", "::1.2.3", "12345::1", - "1:2:3:4:5:6:7:4.3.2.1" ] + data = [ ":", ":::", "::0:", "1::2::3", "::3::", "::1.2.3", "12345::1", + "1:2:3:4:5:6:7:4.3.2.1", "1:2:3:4:5:6:7:8:9" ] def testBad(self): for addr in self.data: @@ -61,5 +61,30 @@ class TestNormalize(unittest.TestCase): a = v6addr.v6addr_to_str(numaddr) self.assertEquals(a, b) +class TestCompat(unittest.TestCase): + + datav6 = [ ("0:0:0:0:0:0:0:0", "::"), ("0::0:0:7", "::7"), + ("00ab:00:00:c:00:00:00:d", "ab:0:0:c::d"), + ("00ab:0000:0000:0000:000c:0000:0000:000d", "ab::c:0:0:d"), + ("1:02:3:04:5:006:7:0008", "1:2:3:4:5:6:7:8"), + ("::FFFF:1.2.3.4", "::ffff:102:304") ] + datav4 = [ ("001.002.003.004", "1.2.3.4") ] + + def testCompat(self): + for addr, b in self.datav6: + numaddr = v6addr.inet_pton(socket.AF_INET6, addr) + a = v6addr.inet_ntop(socket.AF_INET6, numaddr) + self.assertEquals(a, b) + for addr, b in self.datav4: + numaddr = v6addr.inet_pton(socket.AF_INET, addr) + a = v6addr.inet_ntop(socket.AF_INET, numaddr) + self.assertEquals(a, b) + + def testInvalidFamily(self): + self.assertRaises(socket.error, v6addr.inet_pton, + socket.AF_UNIX, "1.2.3.4") + self.assertRaises(socket.error, v6addr.inet_ntop, + socket.AF_UNIX, "\01\02\03\03") + if __name__ == "__main__": unittest.main()