3
0

test_ipgenerator.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from ID2TLib.Generator import IPGenerator
  2. import unittest
  3. # FIXME: These tests rely on randomness. They are NOT repeatable, and therefore unsuitable as unittests!
  4. class IPGeneratorTestCase(unittest.TestCase):
  5. IP_GENERATOR = None
  6. IP_SAMPLES = None
  7. IP_SAMPLES_NUM = 1000
  8. @classmethod
  9. def setUpClass(cls):
  10. cls.IP_GENERATOR = IPGenerator()
  11. cls.IP_SAMPLES = [cls.IP_GENERATOR.random_ip() for _ in range(cls.IP_SAMPLES_NUM)]
  12. def test_valid_ips(self):
  13. for ip in self.IP_SAMPLES:
  14. with self.subTest(ip=ip):
  15. parts = ip.split(".")
  16. self.assertEqual(len(parts), 4)
  17. numbers = [int(i) for i in parts]
  18. for n in numbers:
  19. self.assertIn(n, range(256))
  20. def test_generates_localhost_ip(self):
  21. self.assertFalse(any(ip.startswith("127.") for ip in self.IP_SAMPLES))
  22. def test_generates_private_ip(self):
  23. def private_ip(ip):
  24. private_starts = ["10.", "192.168."] + ["172.%i." % i for i in range(16, 32)]
  25. return any(ip.startswith(start) for start in private_starts)
  26. self.assertFalse(any(map(private_ip, self.IP_SAMPLES)))
  27. def test_unique_ips(self):
  28. self.assertTrue(len(self.IP_SAMPLES) == len(set(self.IP_SAMPLES)))
  29. def test_blacklist(self):
  30. generator = IPGenerator(blacklist=["42.0.0.0/8"])
  31. self.assertFalse(any(generator.random_ip().startswith("42.") for _ in range(self.IP_SAMPLES_NUM)))