示例#1
0
 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)
示例#2
0
 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")
示例#3
0
 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")
示例#4
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)
示例#5
0
 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")))
示例#6
0
 def test_broadcast(self):
     self.assertEqual(
         Ip4Network("192.168.9.100/24").broadcast,
         Ip4Address("192.168.9.255"))
示例#7
0
 def test_address(self):
     self.assertEqual(
         Ip4Network("192.168.9.100/24").address, Ip4Address("192.168.9.0"))
示例#8
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")))
示例#9
0
 def test_version(self):
     self.assertEqual(Ip4Address("192.168.9.1").version, 4)
示例#10
0
 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"))
示例#11
0
 def test___hash__(self):
     self.assertEqual(hash(Ip4Address("192.168.9.1")), hash(3232237825))
示例#12
0
 def test___eq__(self):
     self.assertEqual(Ip4Address("192.168.9.1"), Ip4Address("192.168.9.1"))
示例#13
0
 def test___bytes__(self):
     self.assertEqual(bytes(Ip4Address("192.168.9.1")), b"\xc0\xa8\t\x01")
示例#14
0
 def test___repr__(self):
     self.assertEqual(repr(Ip4Address("192.168.9.1")),
                      "Ip4Address('192.168.9.1')")
示例#15
0
 def test___str__(self):
     self.assertEqual(str(Ip4Address("192.168.9.1")), "192.168.9.1")
示例#16
0
 def test_address(self):
     self.assertEqual(
         Ip4Host("192.168.9.100/24").address, Ip4Address("192.168.9.100"))
示例#17
0
    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),
        ]
示例#18
0
 def test_unspecified(self):
     self.assertEqual(
         Ip4Address("192.168.9.1").unspecified, Ip4Address("0.0.0.0"))