def test__parse_stack_ip4_host_candidate(self, _): sample = [ ("192.168.9.7/24", "192.168.9.1"), # valid address and valid gateway [pass] ("192.168.9.77/24", "192.168.9.1"), # valid address and valid gateway [pass] ("224.0.0.1/24", "192.168.9.1"), # invalid address [fail] ("DUPA", "192.168.9.1"), # malformed address [fail] ("192.168.9.99/24", "DUPA"), # malformed gateway [fail] ("192.168.9.77/24", "192.168.9.1"), # duplicate address [fail] ("192.168.9.170/24", "10.0.0.1"), # valid address but invalid gateway [fail] ("192.168.9.171/24", "192.168.9.0"), # valid address but test invalid gateway [fail] ("192.168.9.172/24", "192.168.9.172"), # valid address but invalid gateway [fail] ("192.168.9.173/24", "192.168.9.255"), # valid address but invalid gateway [fail] ("192.168.9.0/24", "192.168.9.1"), # invalid address [fail] ("192.168.9.255/24", "192.168.9.1"), # invalid address [fail] ("0.0.0.0/0", None), # invalid address [fail] ("192.168.9.102/24", None), # valid address and no gateway [pass] ("172.16.17.7/24", "172.16.17.1"), # valid address and valid gateway [pass] ("10.10.10.7/24", "10.10.10.1"), # valid address and valid gateway [pass] ] expected = [ Ip4Host("192.168.9.7/24"), Ip4Host("192.168.9.77/24"), Ip4Host("192.168.9.102/24"), Ip4Host("172.16.17.7/24"), Ip4Host("10.10.10.7/24"), ] result = self.packet_handler._parse_stack_ip4_host_candidate(sample) self.assertEqual(result, expected) expected = [Ip4Address("192.168.9.1"), Ip4Address("192.168.9.1"), None, Ip4Address("172.16.17.1"), Ip4Address("10.10.10.1")] result = [ip4_host.gateway for ip4_host in result] self.assertEqual(result, expected)
def test___init__(self): self.assertEqual( Ip4Host("192.168.9.100/24")._address, Ip4Address("192.168.9.100")) self.assertEqual( Ip4Host("192.168.9.100/24")._network, Ip4Network("192.168.9.0/24")) self.assertEqual( Ip4Host((Ip4Address("192.168.9.100"), Ip4Mask("255.255.255.0")))._address, Ip4Address("192.168.9.100")) self.assertEqual( Ip4Host((Ip4Address("192.168.9.100"), Ip4Mask("255.255.255.0")))._network, Ip4Network("192.168.9.0/24")) self.assertEqual( Ip4Host((Ip4Address("192.168.9.100"), Ip4Network("192.168.9.0/24")))._address, Ip4Address("192.168.9.100")) self.assertEqual( Ip4Host((Ip4Address("192.168.9.100"), Ip4Network("192.168.9.0/24")))._network, Ip4Network("192.168.9.0/24")) self.assertEqual( Ip4Host(Ip4Host("192.168.9.100/24"))._address, Ip4Address("192.168.9.100")) self.assertEqual( Ip4Host(Ip4Host("192.168.9.100/24"))._network, Ip4Network("192.168.9.0/24")) self.assertRaises(Ip4HostFormatError, Ip4Host, "192.168.9.5//32") self.assertRaises(Ip4HostFormatError, Ip4Host, "192.168.9.5")
def test___init__(self): self.assertEqual( Ip4Network("192.168.9.100/24")._address, Ip4Address("192.168.9.0")) self.assertEqual( Ip4Network("192.168.9.100/24")._mask, Ip4Mask("255.255.255.0")) self.assertEqual( Ip4Network(Ip4Network("192.168.9.100/24"))._address, Ip4Address("192.168.9.0")) self.assertEqual( Ip4Network(Ip4Network("192.168.9.100/24"))._mask, Ip4Mask("255.255.255.0")) self.assertEqual( Ip4Network((Ip4Address("192.168.9.100"), Ip4Mask("255.255.255.0")))._address, Ip4Address("192.168.9.0")) self.assertEqual( Ip4Network( (Ip4Address("192.168.9.100"), Ip4Mask("255.255.255.0")))._mask, Ip4Mask("255.255.255.0")) self.assertEqual( Ip4Network("192.168.9.100/24")._address, Ip4Address("192.168.9.0")) self.assertEqual( Ip4Network("192.168.9.100/24")._mask, Ip4Mask("255.255.255.0")) self.assertEqual( Ip4Network("192.168.9.100/0")._address, Ip4Address("0.0.0.0")) self.assertEqual( Ip4Network("192.168.9.100/0")._mask, Ip4Mask("0.0.0.0")) self.assertRaises(Ip4NetworkFormatError, Ip4Network, "192.168.9.0//32") self.assertRaises(Ip4NetworkFormatError, Ip4Network, "192.168.9.0/321") self.assertRaises(Ip4NetworkFormatError, Ip4Network, "192.168.9.0")
def test___init__(self): self.assertEqual(Ip4Address("192.168.9.1")._address, 3232237825) self.assertEqual( Ip4Address(Ip4Address("192.168.9.1"))._address, 3232237825) self.assertEqual(Ip4Address(b"\xc0\xa8\t\x01")._address, 3232237825) self.assertEqual( Ip4Address(bytearray(b"\xc0\xa8\t\x01"))._address, 3232237825) self.assertEqual( Ip4Address(memoryview(b"\xc0\xa8\t\x01"))._address, 3232237825) self.assertEqual(Ip4Address(3232237825)._address, 3232237825) self.assertRaises(Ip4AddressFormatError, Ip4Address, "10.10.10.256") self.assertRaises(Ip4AddressFormatError, Ip4Address, "10.10..10") self.assertRaises(Ip4AddressFormatError, Ip4Address, b"\xff\xff\xff") self.assertRaises(Ip4AddressFormatError, Ip4Address, b"\xff\xff\xff\xff\xff") self.assertRaises(Ip4AddressFormatError, Ip4Address, -1) self.assertRaises(Ip4AddressFormatError, Ip4Address, 4294967296)
def test___hash__(self): self.assertEqual( hash(Ip4Host("10.0.0.1/8")), hash(Ip4Address("10.0.0.1")) ^ hash(Ip4Network("10.0.0.0/8")))
def test_broadcast(self): self.assertEqual( Ip4Network("192.168.9.100/24").broadcast, Ip4Address("192.168.9.255"))
def test_address(self): self.assertEqual( Ip4Network("192.168.9.100/24").address, Ip4Address("192.168.9.0"))
def test___hash__(self): self.assertEqual( hash(Ip4Network("10.0.0.0/8")), hash(Ip4Address("10.0.0.0")) ^ hash(Ip4Mask("255.0.0.0")))
def test_version(self): self.assertEqual(Ip4Address("192.168.9.1").version, 4)
def test___contains__(self): self.assertIn(Ip4Address("192.168.9.7"), Ip4Network("192.168.9.0/24")) self.assertNotIn(Ip4Address("192.168.9.7"), Ip4Network("172.16.0.0/12")) self.assertNotIn(Ip4Address("192.168.9.7"), Ip4Network("10.0.0.0/8"))
def test___hash__(self): self.assertEqual(hash(Ip4Address("192.168.9.1")), hash(3232237825))
def test___eq__(self): self.assertEqual(Ip4Address("192.168.9.1"), Ip4Address("192.168.9.1"))
def test___bytes__(self): self.assertEqual(bytes(Ip4Address("192.168.9.1")), b"\xc0\xa8\t\x01")
def test___repr__(self): self.assertEqual(repr(Ip4Address("192.168.9.1")), "Ip4Address('192.168.9.1')")
def test___str__(self): self.assertEqual(str(Ip4Address("192.168.9.1")), "192.168.9.1")
def test_address(self): self.assertEqual( Ip4Host("192.168.9.100/24").address, Ip4Address("192.168.9.100"))
def setUp(self): @dataclass class Ip4Sample: ip4_address: Ip4Address is_global: bool = False is_link_local: bool = False is_loopback: bool = False is_multicast: bool = False is_private: bool = False is_unspecified: bool = False is_reserved: bool = False is_limited_broadcast: bool = False is_unicast: bool = False is_invalid: bool = False self.ip4_samples = [ Ip4Sample(Ip4Address("0.0.0.0"), is_unspecified=True), Ip4Sample(Ip4Address("0.0.0.1"), is_invalid=True), Ip4Sample(Ip4Address("0.255.255.255"), is_invalid=True), Ip4Sample(Ip4Address("1.0.0.0"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("8.8.8.8"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("169.253.255.255"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("169.255.0.0"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("126.255.255.255"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("128.0.0.0"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("9.255.255.255"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("11.0.0.0"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("172.15.255.255"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("172.32.0.0"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("192.167.255.255"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("192.169.0.0"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("223.255.255.255"), is_global=True, is_unicast=True), Ip4Sample(Ip4Address("169.254.0.0"), is_link_local=True, is_unicast=True), Ip4Sample(Ip4Address("169.254.100.10"), is_link_local=True, is_unicast=True), Ip4Sample(Ip4Address("169.254.255.255"), is_link_local=True, is_unicast=True), Ip4Sample(Ip4Address("127.0.0.0"), is_loopback=True, is_unicast=True), Ip4Sample(Ip4Address("127.100.50.25"), is_loopback=True, is_unicast=True), Ip4Sample(Ip4Address("127.255.255.255"), is_loopback=True, is_unicast=True), Ip4Sample(Ip4Address("224.0.0.0"), is_multicast=True), Ip4Sample(Ip4Address("230.0.0.5"), is_multicast=True), Ip4Sample(Ip4Address("239.255.255.255"), is_multicast=True), Ip4Sample(Ip4Address("192.168.0.0"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("192.168.100.100"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("192.168.255.255"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("172.16.0.0"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("172.16.100.100"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("172.31.255.255"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("10.0.0.0"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("10.100.100.100"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("10.255.255.255"), is_private=True, is_unicast=True), Ip4Sample(Ip4Address("240.0.0.0"), is_reserved=True), Ip4Sample(Ip4Address("255.255.255.254"), is_reserved=True), Ip4Sample(Ip4Address("255.255.255.255"), is_limited_broadcast=True), ]
def test_unspecified(self): self.assertEqual( Ip4Address("192.168.9.1").unspecified, Ip4Address("0.0.0.0"))