2019-12-18 22:46:00 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import database
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import csv
|
|
|
|
|
2020-12-06 22:13:05 +01:00
|
|
|
TESTS_DIR = "tests"
|
2019-12-18 22:46:00 +01:00
|
|
|
|
2020-12-06 22:13:05 +01:00
|
|
|
if __name__ == "__main__":
|
2019-12-18 22:46:00 +01:00
|
|
|
|
|
|
|
DB = database.Database()
|
2020-12-06 22:13:05 +01:00
|
|
|
log = logging.getLogger("tests")
|
2019-12-18 22:46:00 +01:00
|
|
|
|
|
|
|
for filename in os.listdir(TESTS_DIR):
|
2020-12-06 22:13:05 +01:00
|
|
|
if not filename.lower().endswith(".csv"):
|
|
|
|
continue
|
2019-12-18 22:46:00 +01:00
|
|
|
log.info("")
|
|
|
|
log.info("Running tests from %s", filename)
|
|
|
|
path = os.path.join(TESTS_DIR, filename)
|
2020-12-06 22:13:05 +01:00
|
|
|
with open(path, "rt") as fdesc:
|
|
|
|
count_ent = 0
|
|
|
|
count_all = 0
|
|
|
|
count_den = 0
|
|
|
|
pass_ent = 0
|
|
|
|
pass_all = 0
|
|
|
|
pass_den = 0
|
2019-12-18 22:46:00 +01:00
|
|
|
reader = csv.DictReader(fdesc)
|
|
|
|
for test in reader:
|
2020-12-06 22:13:05 +01:00
|
|
|
log.debug("Testing %s (%s)", test["url"], test["comment"])
|
|
|
|
count_ent += 1
|
|
|
|
passed = True
|
2019-12-18 22:46:00 +01:00
|
|
|
|
2020-12-06 22:13:05 +01:00
|
|
|
for allow in test["allow"].split(":"):
|
2020-12-06 21:29:48 +01:00
|
|
|
if not allow:
|
2019-12-18 22:46:00 +01:00
|
|
|
continue
|
2020-12-06 22:13:05 +01:00
|
|
|
count_all += 1
|
2020-12-06 21:29:48 +01:00
|
|
|
if any(DB.get_domain(allow)):
|
|
|
|
log.error("False positive: %s", allow)
|
2020-12-06 22:13:05 +01:00
|
|
|
passed = False
|
|
|
|
else:
|
|
|
|
pass_all += 1
|
2019-12-18 22:46:00 +01:00
|
|
|
|
2020-12-06 22:13:05 +01:00
|
|
|
for deny in test["deny"].split(":"):
|
2020-12-06 21:29:48 +01:00
|
|
|
if not deny:
|
2019-12-18 22:46:00 +01:00
|
|
|
continue
|
2020-12-06 22:13:05 +01:00
|
|
|
count_den += 1
|
2020-12-06 21:29:48 +01:00
|
|
|
if not any(DB.get_domain(deny)):
|
|
|
|
log.error("False negative: %s", deny)
|
2020-12-06 22:13:05 +01:00
|
|
|
passed = False
|
|
|
|
else:
|
|
|
|
pass_den += 1
|
|
|
|
|
|
|
|
if passed:
|
|
|
|
pass_ent += 1
|
|
|
|
perc_ent = (100 * pass_ent / count_ent) if count_ent else 100
|
|
|
|
perc_all = (100 * pass_all / count_all) if count_all else 100
|
|
|
|
perc_den = (100 * pass_den / count_den) if count_den else 100
|
|
|
|
log.info(
|
2021-08-14 23:35:51 +02:00
|
|
|
(
|
|
|
|
"%s: Entries %d/%d (%.2f%%)"
|
|
|
|
" | Allow %d/%d (%.2f%%)"
|
|
|
|
"| Deny %d/%d (%.2f%%)"
|
|
|
|
),
|
2020-12-06 22:13:05 +01:00
|
|
|
filename,
|
|
|
|
pass_ent,
|
|
|
|
count_ent,
|
|
|
|
perc_ent,
|
|
|
|
pass_all,
|
|
|
|
count_all,
|
|
|
|
perc_all,
|
|
|
|
pass_den,
|
|
|
|
count_den,
|
|
|
|
perc_den,
|
|
|
|
)
|