123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import unittest
- import ipinnet
- IPv4_TESTS = [
- ("192.168.10.1", "192.168.10.0", 24, ipinnet.MATCH),
- ("192.168.10.1", "192.168.11.0", 24, ipinnet.NO_MATCH),
- ("127.0.0.1", "192.168.11.0", 11, ipinnet.NO_MATCH),
- ("185.100.1.1", "176.0.0.0", 8, ipinnet.NO_MATCH),
- ("185.100.1.1", "185.0.0.0", 8, ipinnet.MATCH)
- ]
- IPv6_TESTS = [
- ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 32,
- ipinnet.MATCH),
- ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8:0101::", 33,
- ipinnet.NO_MATCH),
- ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 128,
- ipinnet.NO_MATCH),
- ("2001:0db8:85a3:0000:0000:8a2e:0370:7334",
- "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 128,
- ipinnet.MATCH),
- ("201f:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 11,
- ipinnet.MATCH),
- ("202f:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 11,
- ipinnet.NO_MATCH)
- ]
- def test_ip_in_net(cls, items, method):
- for item in items:
- result = method(item[0], item[1], item[2])
- cls.assertEqual(result, item[3])
- class TestInNet(unittest.TestCase):
- def test_ipinnet_ipv4(self):
- test_ip_in_net(self, IPv4_TESTS, ipinnet.ipv4_in_net)
- def test_ipinnet_ipv6(self):
- test_ip_in_net(self, IPv6_TESTS, ipinnet.ipv6_in_net)
- if __name__ == '__main__':
- unittest.main()
|