def testHash(self): self.assertEquals(hash(ipaddr.IP('10.1.1.0/24')), hash(ipaddr.IP('10.1.1.0/24'))) dummy = {} dummy[self.ipv4] = None dummy[self.ipv6] = None self.assertTrue(self.ipv4 in dummy)
def testCopyConstructor(self): addr1 = ipaddr.IP('10.1.1.0/24') addr2 = ipaddr.IP(addr1) addr3 = ipaddr.IP('2001:658:22a:cafe:200::1/64') addr4 = ipaddr.IP(addr3) self.assertEqual(addr1, addr2) self.assertEqual(addr3, addr4)
def testAddrExclude(self): addr1 = ipaddr.IP('10.1.1.0/24') addr2 = ipaddr.IP('10.1.1.0/26') addr3 = ipaddr.IP('10.2.1.0/24') self.assertEqual(addr1.address_exclude(addr2), [ipaddr.IP('10.1.1.64/26'), ipaddr.IP('10.1.1.128/25')]) self.assertRaises(ValueError, addr1.address_exclude, addr3)
def testIPv6PrefixFromInt(self): addr1 = ipaddr.IP('2001:0658:022a:cafe:0200::1/64') addr2 = ipaddr.IPv6(addr1.ip) # clone prefix addr2.set_prefix(addr1.prefixlen) addr3 = ipaddr.IP(123456) self.assertEqual(123456, addr3.ip) self.assertRaises(ipaddr.IPv6NetmaskValidationError, addr2.set_prefix, -1L) self.assertEqual(addr1, addr2) self.assertEqual(str(addr1), str(addr2))
def testIPv4PrefixFromInt(self): addr1 = ipaddr.IP('10.1.1.0/24') addr2 = ipaddr.IPv4(addr1.ip) # clone prefix addr2.set_prefix(addr1.prefixlen) addr3 = ipaddr.IP(123456) self.assertEqual(123456, addr3.ip) self.assertRaises(ipaddr.IPv4NetmaskValidationError, addr2.set_prefix, -1L) self.assertEqual(addr1, addr2) self.assertEqual(str(addr1), str(addr2))
def testIpFromInt(self): self.assertEqual(self.ipv4.ip, ipaddr.IPv4(16909060).ip) self.assertRaises(ipaddr.IPv4IpValidationError, ipaddr.IPv4, 2**32) self.assertRaises(ipaddr.IPv4IpValidationError, ipaddr.IPv4, -1) self.assertEqual( self.ipv6.ip, ipaddr.IPv6(42540616829182469433547762482097946625).ip) self.assertRaises(ipaddr.IPv6IpValidationError, ipaddr.IPv6, 2**128) self.assertRaises(ipaddr.IPv6IpValidationError, ipaddr.IPv6, -1) self.assertEqual(ipaddr.IP(self.ipv4.ip).version, 4) self.assertEqual(ipaddr.IP(self.ipv6.ip).version, 6)
def testNetworkComparison(self): # ip1 and ip2 have the same network address ip1 = ipaddr.IPv4('1.1.1.0/24') ip2 = ipaddr.IPv4('1.1.1.1/24') ip3 = ipaddr.IPv4('1.1.2.0/24') self.assertTrue(ip1 < ip3) self.assertTrue(ip3 > ip2) self.assertEquals(ip1.compare_networks(ip2), 0) self.assertTrue(ip1._get_networks_key() == ip2._get_networks_key()) self.assertEquals(ip1.compare_networks(ip3), -1) self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) ip1 = ipaddr.IPv6('2001::2000/96') ip2 = ipaddr.IPv6('2001::2001/96') ip3 = ipaddr.IPv6('2001:ffff::2000/96') self.assertTrue(ip1 < ip3) self.assertTrue(ip3 > ip2) self.assertEquals(ip1.compare_networks(ip2), 0) self.assertTrue(ip1._get_networks_key() == ip2._get_networks_key()) self.assertEquals(ip1.compare_networks(ip3), -1) self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) # Test comparing different protocols ipv6 = ipaddr.IPv6('::/0') ipv4 = ipaddr.IPv4('0.0.0.0/0') self.assertTrue(ipv6 > ipv4) self.assertTrue(ipv4 < ipv6) # Regression test for issue 19. ip1 = ipaddr.IP('10.1.2.128/25') self.assertFalse(ip1 < ip1) self.assertFalse(ip1 > ip1) ip2 = ipaddr.IP('10.1.3.0/24') self.assertTrue(ip1 < ip2) self.assertFalse(ip2 < ip1) self.assertFalse(ip1 > ip2) self.assertTrue(ip2 > ip1) ip3 = ipaddr.IP('10.1.3.0/25') self.assertTrue(ip2 < ip3) self.assertFalse(ip3 < ip2) self.assertFalse(ip2 > ip3) self.assertTrue(ip3 > ip2)
def Validate(self, value): """Validates a subnet.""" if value is None: raise validation.MissingAttribute('subnet must be specified') try: ipaddr.IP(value) except ValueError: raise validation.ValidationError('%s is not a valid IPv4 or IPv6 subnet' % value) else: return value
def testReservedIpv4(self): self.assertEquals(True, ipaddr.IP('224.1.1.1/31').is_multicast) self.assertEquals(False, ipaddr.IP('240.0.0.0').is_multicast) self.assertEquals(True, ipaddr.IP('192.168.1.1/17').is_private) self.assertEquals(False, ipaddr.IP('192.169.0.0').is_private) self.assertEquals(True, ipaddr.IP('10.255.255.255').is_private) self.assertEquals(False, ipaddr.IP('11.0.0.0').is_private) self.assertEquals(True, ipaddr.IP('172.31.255.255').is_private) self.assertEquals(False, ipaddr.IP('172.32.0.0').is_private) self.assertEquals(True, ipaddr.IP('169.254.100.200/24').is_link_local) self.assertEquals(False, ipaddr.IP('169.255.100.200/24').is_link_local) self.assertEquals(True, ipaddr.IP('127.100.200.254/32').is_loopback) self.assertEquals(True, ipaddr.IP('127.42.0.0/16').is_loopback) self.assertEquals(False, ipaddr.IP('128.0.0.0').is_loopback)
def testIpType(self): ipv4 = ipaddr.IP('1.2.3.4') ipv6 = ipaddr.IP('::1.2.3.4') self.assertEquals(ipaddr.IPv4, type(ipv4)) self.assertEquals(ipaddr.IPv6, type(ipv6))