test.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. """DNS resolver pool: enough entries, randomness, valid IPs."""
  3. import sys, re, ipaddress
  4. err = []
  5. from core._dns_pool import (PUBLIC_DNS_RESOLVERS, OPENDNS_RESOLVERS,
  6. DUMMY_ROUTABLE_HOSTS, random_resolvers,
  7. random_opendns, random_dummy_host)
  8. if len(PUBLIC_DNS_RESOLVERS) < 50:
  9. err.append(f"PUBLIC_DNS_RESOLVERS has only {len(PUBLIC_DNS_RESOLVERS)} entries (expected >=50)")
  10. for ip in PUBLIC_DNS_RESOLVERS + OPENDNS_RESOLVERS + DUMMY_ROUTABLE_HOSTS:
  11. try:
  12. ipaddress.IPv4Address(ip)
  13. except ValueError:
  14. err.append(f"invalid IPv4 in pool: {ip!r}")
  15. samples = set()
  16. for _ in range(200):
  17. samples.add(tuple(random_resolvers(2)))
  18. if len(samples) < 30:
  19. err.append(f"random_resolvers seems non-random: only {len(samples)} unique pairs in 200 draws")
  20. for _ in range(10):
  21. pair = random_resolvers(2)
  22. if len(pair) != 2:
  23. err.append(f"random_resolvers(2) returned {len(pair)} items")
  24. if pair[0] == pair[1]:
  25. err.append("random_resolvers(2) returned duplicates")
  26. od = random_opendns()
  27. for ip in od:
  28. if ip not in OPENDNS_RESOLVERS:
  29. err.append(f"random_opendns returned non-opendns IP: {ip}")
  30. h = random_dummy_host()
  31. if h not in DUMMY_ROUTABLE_HOSTS:
  32. err.append(f"random_dummy_host returned unknown: {h}")
  33. print(f"pool size: {len(PUBLIC_DNS_RESOLVERS)}")
  34. print(f"unique 2-tuples in 200 draws: {len(samples)}")
  35. print(f"opendns pool: {len(OPENDNS_RESOLVERS)}")
  36. print(f"dummy hosts: {len(DUMMY_ROUTABLE_HOSTS)}")
  37. for e in err:
  38. print("FAIL:", e)
  39. sys.exit(0 if not err else 1)