test.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import unittest
  2. import ipinnet
  3. IPv4_TESTS = [
  4. ("192.168.10.1", "192.168.10.0", 24, ipinnet.MATCH),
  5. ("192.168.10.1", "192.168.11.0", 24, ipinnet.NO_MATCH),
  6. ("127.0.0.1", "192.168.11.0", 11, ipinnet.NO_MATCH),
  7. ("185.100.1.1", "176.0.0.0", 8, ipinnet.NO_MATCH),
  8. ("185.100.1.1", "185.0.0.0", 8, ipinnet.MATCH)
  9. ]
  10. IPv6_TESTS = [
  11. ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 32,
  12. ipinnet.MATCH),
  13. ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8:0101::", 33,
  14. ipinnet.NO_MATCH),
  15. ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 128,
  16. ipinnet.NO_MATCH),
  17. ("2001:0db8:85a3:0000:0000:8a2e:0370:7334",
  18. "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 128,
  19. ipinnet.MATCH),
  20. ("201f:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 11,
  21. ipinnet.MATCH),
  22. ("202f:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8::", 11,
  23. ipinnet.NO_MATCH)
  24. ]
  25. def test_ip_in_net(cls, items, method):
  26. for item in items:
  27. result = method(item[0], item[1], item[2])
  28. cls.assertEqual(result, item[3])
  29. class TestInNet(unittest.TestCase):
  30. def test_ipinnet_ipv4(self):
  31. test_ip_in_net(self, IPv4_TESTS, ipinnet.ipv4_in_net)
  32. def test_ipinnet_ipv6(self):
  33. test_ip_in_net(self, IPv6_TESTS, ipinnet.ipv6_in_net)
  34. if __name__ == '__main__':
  35. unittest.main()