def testNetworkElementCaching(self): # V4 - make sure we're empty self.assertFalse('network' in self.ipv4._cache) self.assertFalse('broadcast' in self.ipv4._cache) self.assertFalse('hostmask' in self.ipv4._cache) # V4 - populate and test self.assertEqual(self.ipv4.network, ipaddr.IPv4Address('1.2.3.0')) self.assertEqual(self.ipv4.broadcast, ipaddr.IPv4Address('1.2.3.255')) self.assertEqual(self.ipv4.hostmask, ipaddr.IPv4Address('0.0.0.255')) # V4 - check we're cached self.assertTrue('network' in self.ipv4._cache) self.assertTrue('broadcast' in self.ipv4._cache) self.assertTrue('hostmask' in self.ipv4._cache) # V6 - make sure we're empty self.assertFalse('network' in self.ipv6._cache) self.assertFalse('broadcast' in self.ipv6._cache) self.assertFalse('hostmask' in self.ipv6._cache) # V6 - populate and test self.assertEqual(self.ipv6.network, ipaddr.IPv6Address('2001:658:22a:cafe::')) self.assertEqual(self.ipv6.broadcast, ipaddr.IPv6Address( '2001:658:22a:cafe:ffff:ffff:ffff:ffff')) self.assertEqual(self.ipv6.hostmask, ipaddr.IPv6Address('::ffff:ffff:ffff:ffff')) # V6 - check we're cached self.assertTrue('network' in self.ipv6._cache) self.assertTrue('broadcast' in self.ipv6._cache) self.assertTrue('hostmask' in self.ipv6._cache)
def testAddressIntMath(self): self.assertEqual(ipaddr.IPv4Address('1.1.1.1') + 255, ipaddr.IPv4Address('1.1.2.0')) self.assertEqual(ipaddr.IPv4Address('1.1.1.1') - 256, ipaddr.IPv4Address('1.1.0.1')) self.assertEqual(ipaddr.IPv6Address('::1') + (2**16 - 2), ipaddr.IPv6Address('::ffff')) self.assertEqual(ipaddr.IPv6Address('::ffff') - (2**16 - 2), ipaddr.IPv6Address('::1'))
def testCopyConstructor(self): addr1 = ipaddr.IPNetwork('10.1.1.0/24') addr2 = ipaddr.IPNetwork(addr1) addr3 = ipaddr.IPNetwork('2001:658:22a:cafe:200::1/64') addr4 = ipaddr.IPNetwork(addr3) addr5 = ipaddr.IPv4Address('1.1.1.1') addr6 = ipaddr.IPv6Address('2001:658:22a:cafe:200::1') self.assertEqual(addr1, addr2) self.assertEqual(addr3, addr4) self.assertEqual(addr5, ipaddr.IPv4Address(addr5)) self.assertEqual(addr6, ipaddr.IPv6Address(addr6))
def testTeredo(self): # stolen from wikipedia server = ipaddr.IPv4Address('65.54.227.120') client = ipaddr.IPv4Address('192.0.2.45') teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2' self.assertEqual((server, client), ipaddr.IPAddress(teredo_addr).teredo) bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2' self.assertFalse(ipaddr.IPAddress(bad_addr).teredo) bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2' self.assertFalse(ipaddr.IPAddress(bad_addr).teredo) # i77 teredo_addr = ipaddr.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') self.assertEqual((ipaddr.IPv4Address('94.245.121.253'), ipaddr.IPv4Address('95.26.244.94')), teredo_addr.teredo)
def testContains(self): self.assertTrue(ipaddr.IPv4Network('1.2.3.128/25') in self.ipv4) self.assertFalse(ipaddr.IPv4Network('1.2.4.1/24') in self.ipv4) self.assertTrue(self.ipv4 in self.ipv4) self.assertTrue(self.ipv6 in self.ipv6) # We can test addresses and string as well. addr1 = ipaddr.IPv4Address('1.2.3.37') self.assertTrue(addr1 in self.ipv4) # issue 61, bad network comparison on like-ip'd network objects # with identical broadcast addresses. self.assertFalse(ipaddr.IPv4Network('1.1.0.0/16').__contains__( ipaddr.IPv4Network('1.0.0.0/15')))
def testInvalidStrings(self): def AssertInvalidIP(ip_str): self.assertRaises(ValueError, ipaddr.IPAddress, ip_str) AssertInvalidIP("") AssertInvalidIP("016.016.016.016") AssertInvalidIP("016.016.016") AssertInvalidIP("016.016") AssertInvalidIP("016") AssertInvalidIP("000.000.000.000") AssertInvalidIP("000") AssertInvalidIP("0x0a.0x0a.0x0a.0x0a") AssertInvalidIP("0x0a.0x0a.0x0a") AssertInvalidIP("0x0a.0x0a") AssertInvalidIP("0x0a") AssertInvalidIP("42.42.42.42.42") AssertInvalidIP("42.42.42") AssertInvalidIP("42.42") AssertInvalidIP("42") AssertInvalidIP("42..42.42") AssertInvalidIP("42..42.42.42") AssertInvalidIP("42.42.42.42.") AssertInvalidIP("42.42.42.42...") AssertInvalidIP(".42.42.42.42") AssertInvalidIP("...42.42.42.42") AssertInvalidIP("42.42.42.-0") AssertInvalidIP("42.42.42.+0") AssertInvalidIP(".") AssertInvalidIP("...") AssertInvalidIP("bogus") AssertInvalidIP("bogus.com") AssertInvalidIP("192.168.0.1.com") AssertInvalidIP("12345.67899.-54321.-98765") AssertInvalidIP("257.0.0.0") AssertInvalidIP("42.42.42.-42") AssertInvalidIP("3ffe::1.net") AssertInvalidIP("3ffe::1::1") AssertInvalidIP("1::2::3::4:5") AssertInvalidIP("::7:6:5:4:3:2:") AssertInvalidIP(":6:5:4:3:2:1::") AssertInvalidIP("2001::db:::1") AssertInvalidIP("FEDC:9878") AssertInvalidIP("+1.+2.+3.4") AssertInvalidIP("1.2.3.4e0") AssertInvalidIP("::7:6:5:4:3:2:1:0") AssertInvalidIP("7:6:5:4:3:2:1:0::") AssertInvalidIP("9:8:7:6:5:4:3::2:1") AssertInvalidIP("0:1:2:3::4:5:6:7") AssertInvalidIP("3ffe:0:0:0:0:0:0:0:1") AssertInvalidIP("3ffe::10000") AssertInvalidIP("3ffe::goog") AssertInvalidIP("3ffe::-0") AssertInvalidIP("3ffe::+0") AssertInvalidIP("3ffe::-1") AssertInvalidIP(":") AssertInvalidIP(":::") AssertInvalidIP("::1.2.3") AssertInvalidIP("::1.2.3.4.5") AssertInvalidIP("::1.2.3.4:") AssertInvalidIP("1.2.3.4::") AssertInvalidIP("2001:db8::1:") AssertInvalidIP(":2001:db8::1") AssertInvalidIP(":1:2:3:4:5:6:7") AssertInvalidIP("1:2:3:4:5:6:7:") AssertInvalidIP(":1:2:3:4:5:6:") AssertInvalidIP("192.0.2.1/32") AssertInvalidIP("2001:db8::1/128") self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Network, '') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Network, 'google.com') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Network, '::1.2.3.4') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, 'google.com') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '1.2.3.4') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, 'cafe:cafe::/128/190') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '1234:axy::b') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Address, '1234:axy::b') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Address, '2001:db8:::1') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Address, '2001:888888::1') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Address(1)._ip_int_from_string, '1.a.2.3') self.assertEqual(False, ipaddr.IPv4Network(1)._is_hostmask('1.a.2.3'))
def testCollapsing(self): # test only IP addresses including some duplicates ip1 = ipaddr.IPv4Address('1.1.1.0') ip2 = ipaddr.IPv4Address('1.1.1.1') ip3 = ipaddr.IPv4Address('1.1.1.2') ip4 = ipaddr.IPv4Address('1.1.1.3') ip5 = ipaddr.IPv4Address('1.1.1.4') ip6 = ipaddr.IPv4Address('1.1.1.0') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.1.0/30'), ipaddr.IPv4Network('1.1.1.4/32')]) # test a mix of IP addresses and networks including some duplicates ip1 = ipaddr.IPv4Address('1.1.1.0') ip2 = ipaddr.IPv4Address('1.1.1.1') ip3 = ipaddr.IPv4Address('1.1.1.2') ip4 = ipaddr.IPv4Address('1.1.1.3') ip5 = ipaddr.IPv4Network('1.1.1.4/30') ip6 = ipaddr.IPv4Network('1.1.1.4/30') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip5, ip1, ip2, ip3, ip4, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.1.0/29')]) # test only IP networks ip1 = ipaddr.IPv4Network('1.1.0.0/24') ip2 = ipaddr.IPv4Network('1.1.1.0/24') ip3 = ipaddr.IPv4Network('1.1.2.0/24') ip4 = ipaddr.IPv4Network('1.1.3.0/24') ip5 = ipaddr.IPv4Network('1.1.4.0/24') # stored in no particular order b/c we want CollapseAddr to call [].sort ip6 = ipaddr.IPv4Network('1.1.0.0/22') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.0.0/22'), ipaddr.IPv4Network('1.1.4.0/24')]) # test that two addresses are supernet'ed properly collapsed = ipaddr.collapse_address_list([ip1, ip2]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.0.0/23')]) # test same IP networks ip_same1 = ip_same2 = ipaddr.IPv4Network('1.1.1.1/32') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) # test same IP addresses ip_same1 = ip_same2 = ipaddr.IPv4Address('1.1.1.1') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ipaddr.IPNetwork('1.1.1.1/32')]) ip1 = ipaddr.IPv6Network('::2001:1/100') ip2 = ipaddr.IPv6Network('::2002:1/120') ip3 = ipaddr.IPv6Network('::2001:1/96') # test that ipv6 addresses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) self.assertEqual(collapsed, [ip3]) # the toejam test ip1 = ipaddr.IPAddress('1.1.1.1') ip2 = ipaddr.IPAddress('::1') self.assertRaises(TypeError, ipaddr.collapse_address_list, [ip1, ip2])
def testIterHosts(self): self.assertEqual([ipaddr.IPv4Address('2.0.0.0'), ipaddr.IPv4Address('2.0.0.1')], list(ipaddr.IPNetwork('2.0.0.0/31').iterhosts()))
def testsixtofour(self): sixtofouraddr = ipaddr.IPAddress('2002:ac1d:2d64::1') bad_addr = ipaddr.IPAddress('2000:ac1d:2d64::1') self.assertEqual(ipaddr.IPv4Address('172.29.45.100'), sixtofouraddr.sixtofour) self.assertFalse(bad_addr.sixtofour)