def test_comparison(self): self.assertEqual(IPv4('1.2.3.4'), IPv4('1.2.3.4')) self.assertEqual(IPv4('1.2.3.4/24'), IPv4('1.2.3.4/24')) self.assertNotEqual(IPv4('1.2.3.4'), IPv4('1.2.3.5')) self.assertNotEqual(IPv4('1.2.3.4/24'), IPv4('1.2.3.4/32')) self.assertNotEqual(IPv4('1.2.3.4'), IPv6('::1.2.3.4')) self.assertTrue(IPv4('1.2.3.0') < IPv4('1.2.3.4')) self.assertTrue(IPv4('1.2.3.4/16') < IPv4('1.2.3.4/24')) self.assertTrue(IPv4('1.2.3.4') < IPv6('::1.2.3.4')) self.assertFalse(IPv4('1.2.4.0') < IPv4('1.2.3.4')) self.assertTrue(IPv4('1.2.3.4') <= IPv4('1.2.3.4')) self.assertTrue(IPv4('1.2.3.4/24') <= IPv4('1.2.3.4/24')) self.assertTrue(IPv4('1.2.3.0') <= IPv4('1.2.3.4')) self.assertTrue(IPv4('1.2.3.4/16') <= IPv4('1.2.3.4/24')) self.assertTrue(IPv4('1.2.3.4') <= IPv6('::1.2.3.4')) self.assertFalse(IPv4('1.2.4.0') <= IPv4('1.2.3.4')) self.assertTrue(IPv4('1.2.3.4') > IPv4('1.2.3.0')) self.assertTrue(IPv4('1.2.3.4/24') > IPv4('1.2.3.4/16')) self.assertTrue(IPv6('::1.2.3.4') > IPv4('1.2.3.4')) self.assertFalse(IPv4('1.2.3.0') > IPv4('1.2.4.0')) self.assertTrue(IPv4('1.2.3.4') >= IPv4('1.2.3.4')) self.assertTrue(IPv4('1.2.3.4/24') >= IPv4('1.2.3.4/24')) self.assertTrue(IPv4('1.2.3.4') >= IPv4('1.2.3.0')) self.assertTrue(IPv4('1.2.3.4/24') >= IPv4('1.2.3.4/16')) self.assertTrue(IPv6('::1.2.3.4') >= IPv4('1.2.3.4')) self.assertFalse(IPv4('1.2.3.0') >= IPv4('1.2.4.0'))
def test_network(self): self.assertEqual(IPv4('1.2.3.4/32').network, Prefix('1.2.3.4/32')) self.assertEqual(IPv4('1.2.3.4/24').network, Prefix('1.2.3.0/24')) self.assertEqual(IPv4('1.2.3.4/0').network, Prefix('0.0.0.0/0')) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/128').network, Prefix('1:2:3:4:5:6:7:8')) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/64').network, Prefix('1:2:3:4::/64')) self.assertEqual(IPv6('1:2:3:4:5:6:7:8/0').network, Prefix('::/0'))
def test_hostmask(self): self.assertEqual(IPv4('1.2.3.4/32').hostmask, Mask4(0)) self.assertEqual(IPv4('1.2.3.4/24').hostmask, Mask4(0xff)) self.assertEqual(IPv4('1.2.3.4/0').hostmask, Mask4(0xffffffff)) self.assertEqual(IPv6('1:2:3:4:5:6:7:8/128').hostmask, Mask6(0)) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/64').hostmask, Mask6(0x0000000000000000ffffffffffffffff)) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/0').hostmask, Mask6(0xffffffffffffffffffffffffffffffff))
def test_format(self): # Most format tests taken care of by test_parse. self.assertEqual(IPv4('1.2.3.4').format(), '1.2.3.4') self.assertEqual( IPv4('1.2.3.4').format(always_prefix=True), '1.2.3.4/32') self.assertEqual(IPv4('1.2.3.4/31').format(), '1.2.3.4/31') self.assertEqual(IPv6('1:2:3:4:5:6:7:8').format(), '1:2:3:4:5:6:7:8') self.assertEqual( IPv6('1:2:3:4:5:6:7:8').format(always_prefix=True), '1:2:3:4:5:6:7:8/128') self.assertEqual( IPv6('1:2:3:4:5:6:7:8/127').format(), '1:2:3:4:5:6:7:8/127')
def test_contains(self): self.assertTrue(IPv4('1.2.3.4') in Prefix('1.2.3.0/24')) self.assertTrue(IPv4('1.2.3.0') in Prefix('1.2.3.0/24')) self.assertTrue(IPv4('1.2.3.255') in Prefix('1.2.3.0/24')) self.assertFalse(IPv4('1.2.4.1') in Prefix('1.2.3.0/24')) self.assertFalse(IPv6('::1.2.3.4') in Prefix('1.2.3.0/24'))
def test_size(self): r = IPRange(IPv4('1.2.3.4'), IPv4('1.2.3.10')) self.assertEqual(r.size(), 7) r = IPRange(IPv4('0.0.0.0'), IPv4('255.255.255.255')) self.assertEqual(r.size(), 2**32) r = IPRange(IPv6('1::'), IPv6('2::')) self.assertEqual(r.size(), 2**112 + 1) r = IPGlob('1.2.3.4') self.assertEqual(r.size(), 1) r = Prefix('1.2.3.4') self.assertEqual(r.size(), 1) r = Prefix('2001:db8::/32') self.assertEqual(r.size(), 2**96)
def test_subnet(self): self.assertEqual(IPv4('1.2.3.4').subnet(), [IPv4('1.2.3.4')]) self.assertEqual( IPv4('1.2.3.4/8').subnet(), [IPv4('1.0.0.0/9'), IPv4('1.128.0.0/9')]) self.assertEqual( IPv4('1.2.3.4/8').subnet(2), [ IPv4('1.0.0.0/10'), IPv4('1.64.0.0/10'), IPv4('1.128.0.0/10'), IPv4('1.192.0.0/10') ]) self.assertEqual(IPv6('::').subnet(), [IPv6('::')]) self.assertEqual( IPv6('2001:db8::/32').subnet(), [IPv6('2001:db8::/33'), IPv6('2001:db8:8000::/33')])
def test_getitem(self): # This also implicitly tests iterator(). r = Prefix('1.2.3.0/24') self.assertEqual(r[0], IPv4('1.2.3.0')) self.assertEqual(r[-1], IPv4('1.2.3.255')) self.assertEqual(list(r[:5]), [ IPv4('1.2.3.0'), IPv4('1.2.3.1'), IPv4('1.2.3.2'), IPv4('1.2.3.3'), IPv4('1.2.3.4'), ]) self.assertEqual(list(r[254:]), [IPv4('1.2.3.254'), IPv4('1.2.3.255')]) self.assertEqual(list(r[3:5]), [IPv4('1.2.3.3'), IPv4('1.2.3.4')]) self.assertEqual(list(r[3:20:5]), [ IPv4('1.2.3.3'), IPv4('1.2.3.8'), IPv4('1.2.3.13'), IPv4('1.2.3.18'), ]) r = Prefix('2001:db8::/32') self.assertEqual(r[0], IPv6('2001:db8::')) self.assertEqual(r[-1], IPv6('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff'))
def test_broadcast(self): self.assertEqual(IPv4('1.2.3.4/32').broadcast, IPv4('1.2.3.4')) self.assertEqual(IPv4('1.2.3.4/24').broadcast, IPv4('1.2.3.255')) self.assertEqual(IPv4('1.2.3.4/0').broadcast, IPv4('255.255.255.255')) # Broadcast doesn't really make sense in IPv6. self.assertEqual( IPv6('1:2:3:4:5:6:7:8/128').broadcast, IPv6('1:2:3:4:5:6:7:8')) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/64').broadcast, IPv6('1:2:3:4:ffff:ffff:ffff:ffff')) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/0').broadcast, IPv6('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'))
def test_netmask(self): self.assertEqual(IPv4('1.2.3.4/32').netmask, Mask4(0xffffffff)) self.assertEqual(IPv4('1.2.3.4/24').netmask, Mask4(0xffffff00)) self.assertEqual(IPv4('1.2.3.4/0').netmask, Mask4(0)) self.assertEqual( IPv4('1.2.3.4/24').netmask, IP('1.2.3.4', '24').netmask) self.assertEqual(IPv4('1.2.3.4/32').netmask_int, 0xffffffff) self.assertEqual( IPv4('1.2.3.4/32').netmask_int, IP('1.2.3.4', '32').netmask_int) self.assertEqual(IPv4('1.2.3.4/24').netmask_int, 0xffffff00) self.assertEqual(IPv4('1.2.3.4/0').netmask_int, 0) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/128').netmask, Mask6(0xffffffffffffffffffffffffffffffff)) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/128').netmask, IP('1:2:3:4:5:6:7:8', '128').netmask) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/64').netmask, Mask6(0xffffffffffffffff0000000000000000)) self.assertEqual(IPv6('1:2:3:4:5:6:7:8/0').netmask, Mask6(0)) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/128').netmask_int, 0xffffffffffffffffffffffffffffffff) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/64').netmask_int, 0xffffffffffffffff0000000000000000) self.assertEqual( IPv6('1:2:3:4:5:6:7:8/64').netmask_int, IP('1:2:3:4:5:6:7:8', '64').netmask_int) self.assertEqual(IPv6('1:2:3:4:5:6:7:8/0').netmask_int, 0)
def test_sub(self): sub_e = [ ('1.2.3.4', 1, '1.2.3.3'), ('255.255.255.255', 255, '255.255.255.0'), ('1.2.3.0', IPv4('0.0.0.255'), '1.2.2.1'), ('1.2.3.0', -1, '1.2.3.1'), ('::1', 1, '::'), ('1:2:3:4:5:6:7:8', IPv6('::5:6:7:8'), '1:2:3:4::'), ('2001:db8::', -1, '2001:db8::1'), ] for value, diff, output in sub_e: i = IP(value) p = i - diff self.assertEqual(p, IP(output)) invalid = [ ('0.0.0.0', 1), ('255.255.255.255', -1), ('::', 1), ('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', -1), ] for value, diff in invalid: i = IP(value) self.assertRaises(IPValidationError, lambda: i - diff)
def test_localhost(self): self.assertEqual(IPv4.localhost, IPv4('127.0.0.1')) self.assertEqual(IPv6.localhost, IPv6('::1'))
def test_explicit_netmask(self): x = IPv4('1.2.3.4', netmask='0xffffff00') self.assertEqual(int(x.netmask), 0xffffff00) self.assertEqual(x.prefixlen, 24) x = IPv4('1.2.3.4', netmask=0xffffff00) self.assertEqual(int(x.netmask), 0xffffff00) self.assertEqual(x.prefixlen, 24) x = IPv4('1.2.3.4', netmask=0) self.assertEqual(int(x.netmask), 0) self.assertEqual(x.prefixlen, 0) x = IPv4('1.2.3.4', netmask='0xffffffff') self.assertEqual(int(x.netmask), 0xffffffff) self.assertEqual(x.prefixlen, 32) x = IPv4('1.2.3.4', netmask='255.255.255.0') self.assertEqual(int(x.netmask), 0xffffff00) self.assertEqual(x.prefixlen, 24) mask = Mask4('255.255.0.0') x = IPv4('1.2.3.4', netmask=mask) self.assertEqual(int(x.netmask), 0xffff0000) self.assertEqual(x.prefixlen, 16) x = IPv4(0x01020304, netmask='0xffffff00') self.assertEqual(int(x.netmask), 0xffffff00) self.assertEqual(x.prefixlen, 24) self.assertRaises(MaskValidationError, IPv4, '1.2.3.4', netmask='foo') self.assertRaises(MaskValidationError, IPv4, '1.2.3.4/24', netmask='255.255.255.0') self.assertRaises(MaskValidationError, IPv4, '1.2.3.4', netmask=Mask6(0)) self.assertRaises(MaskValidationError, IPv4, '1.2.3.4', netmask=-1) self.assertRaises(MaskValidationError, IPv4, '1.2.3.4', netmask=0xffffffff + 1) # Hostmask not allowed. self.assertRaises(MaskValidationError, IPv4, '1.2.3.4', netmask=Mask4(0xff)) ###################################################################### x = IPv6('1:2:3:4:5:6:7:8', netmask=0xffffffffffffffff0000000000000000) self.assertEqual(int(x.netmask), 0xffffffffffffffff0000000000000000) self.assertEqual(x.prefixlen, 64) x = IPv6('1:2:3:4:5:6:7:8', netmask=0) self.assertEqual(int(x.netmask), 0) self.assertEqual(x.prefixlen, 0) x = IPv6('1:2:3:4:5:6:7:8', netmask=0xffffffffffffffffffffffffffffffff) self.assertEqual(int(x.netmask), 0xffffffffffffffffffffffffffffffff) self.assertEqual(x.prefixlen, 128) mask = Mask6(0xffffffffffffffffffffffff00000000) x = IPv6('1:2:3:4:5:6:7:8', netmask=mask) self.assertEqual(int(x.netmask), 0xffffffffffffffffffffffff00000000) self.assertEqual(x.prefixlen, 96) x = IPv6(0x0102030405060708, netmask=0xffffffffffff00000000000000000000) self.assertEqual(int(x.netmask), 0xffffffffffff00000000000000000000L) self.assertEqual(x.prefixlen, 48) self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8', netmask='foo') self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8/64', netmask=0xffffffffffffffff0000000000000000) self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8', netmask=Mask4(0)) self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8', netmask=-1) self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8', netmask=0xffffffffffffffffffffffffffffffff + 1) # Strings currently not supported. self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8', netmask='0xffffffffffffffff0000000000000000') # Hostmask not allowed. self.assertRaises(MaskValidationError, IPv6, '1:2:3:4:5:6:7:8', netmask=Mask6(0xff))