Generates a host list of first-party trackers for ad-blocking.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

  1. #!/usr/bin/env python3
  2. import database
  3. import os
  4. import logging
  5. import csv
  6. TESTS_DIR = "tests"
  7. if __name__ == "__main__":
  8. DB = database.Database()
  9. log = logging.getLogger("tests")
  10. for filename in os.listdir(TESTS_DIR):
  11. if not filename.lower().endswith(".csv"):
  12. continue
  13. log.info("")
  14. log.info("Running tests from %s", filename)
  15. path = os.path.join(TESTS_DIR, filename)
  16. with open(path, "rt") as fdesc:
  17. count_ent = 0
  18. count_all = 0
  19. count_den = 0
  20. pass_ent = 0
  21. pass_all = 0
  22. pass_den = 0
  23. reader = csv.DictReader(fdesc)
  24. for test in reader:
  25. log.debug("Testing %s (%s)", test["url"], test["comment"])
  26. count_ent += 1
  27. passed = True
  28. for allow in test["allow"].split(":"):
  29. if not allow:
  30. continue
  31. count_all += 1
  32. if any(DB.get_domain(allow)):
  33. log.error("False positive: %s", allow)
  34. passed = False
  35. else:
  36. pass_all += 1
  37. for deny in test["deny"].split(":"):
  38. if not deny:
  39. continue
  40. count_den += 1
  41. if not any(DB.get_domain(deny)):
  42. log.error("False negative: %s", deny)
  43. passed = False
  44. else:
  45. pass_den += 1
  46. if passed:
  47. pass_ent += 1
  48. perc_ent = (100 * pass_ent / count_ent) if count_ent else 100
  49. perc_all = (100 * pass_all / count_all) if count_all else 100
  50. perc_den = (100 * pass_den / count_den) if count_den else 100
  51. log.info(
  52. (
  53. "%s: Entries %d/%d (%.2f%%)"
  54. " | Allow %d/%d (%.2f%%)"
  55. "| Deny %d/%d (%.2f%%)"
  56. ),
  57. filename,
  58. pass_ent,
  59. count_ent,
  60. perc_ent,
  61. pass_all,
  62. count_all,
  63. perc_all,
  64. pass_den,
  65. count_den,
  66. perc_den,
  67. )