| 1 |
import unittest |
| 2 |
import uuid |
| 3 |
|
| 4 |
from PSLPrivateDomainsProcessor import PSLPrivateDomainsProcessor, check_dns_status, get_whois_data, check_psl_txt_record |
| 5 |
|
| 6 |
|
| 7 |
class TestPSLPrivateDomainsProcessor(unittest.TestCase): |
| 8 |
|
| 9 |
def setUp(self): |
| 10 |
self.processor = PSLPrivateDomainsProcessor() |
| 11 |
# Populate icann_domains for testing |
| 12 |
self.processor.icann_domains = { |
| 13 |
"com", "co.uk", "ac.uk", "net", "org" |
| 14 |
} |
| 15 |
|
| 16 |
def test_parse_domain_icann_domain(self): |
| 17 |
# Test cases where domains should be parsed correctly |
| 18 |
test_cases = [ |
| 19 |
("*.example.com", "example.com"), |
| 20 |
("sub.example.com", "example.com"), |
| 21 |
("*.sub.example.com", "example.com"), |
| 22 |
("example.com", "example.com"), |
| 23 |
("example.co.uk", "example.co.uk"), |
| 24 |
("sub.example.co.uk", "example.co.uk"), |
| 25 |
("*.example.co.uk", "example.co.uk"), |
| 26 |
("*.sub.example.co.uk", "example.co.uk"), |
| 27 |
("abc.ac.uk", "abc.ac.uk"), |
| 28 |
("a.b.com", "b.com") |
| 29 |
] |
| 30 |
|
| 31 |
for domain, expected in test_cases: |
| 32 |
with self.subTest(domain=domain): |
| 33 |
result = self.processor.parse_domain(domain) |
| 34 |
self.assertEqual(expected, result) |
| 35 |
|
| 36 |
def test_parse_domain_no_icann(self): |
| 37 |
# Test case where no valid ICANN domain is found |
| 38 |
self.processor.icann_domains.remove("com") |
| 39 |
with self.assertRaises(ValueError): |
| 40 |
self.processor.parse_domain("example.com") |
| 41 |
|
| 42 |
def test_parse_domain_edge_cases(self): |
| 43 |
# Additional edge case testing |
| 44 |
self.assertEqual("example.org", self.processor.parse_domain("sub.example.org")) |
| 45 |
self.assertEqual("example.com", self.processor.parse_domain("example.com")) |
| 46 |
self.assertEqual("example.ac.uk", self.processor.parse_domain("sub.example.ac.uk")) |
| 47 |
|
| 48 |
def test_parse_domain_invalid(self): |
| 49 |
# Test invalid domains which should raise ValueError |
| 50 |
invalid_domains = ["invalid.test", "*.invalid.test", "sub.invalid.test"] |
| 51 |
for domain in invalid_domains: |
| 52 |
with self.subTest(domain=domain): |
| 53 |
with self.assertRaises(ValueError): |
| 54 |
self.processor.parse_domain(domain) |
| 55 |
|
| 56 |
def test_check_dns_status(self): |
| 57 |
# Test with a known good domain |
| 58 |
self.assertEqual("ok", check_dns_status("mozilla.org")) |
| 59 |
# Test with a likely non-existent domain |
| 60 |
random_domain = "nxdomain-" + str(uuid.uuid4()) + ".edu" |
| 61 |
self.assertEqual("NXDOMAIN", check_dns_status(random_domain)) |
| 62 |
|
| 63 |
def test_check_psl_txt_record(self): |
| 64 |
# Test with a known domain having a valid _psl TXT record |
| 65 |
self.assertEqual("valid", check_psl_txt_record("cdn.cloudflare.net")) |
| 66 |
# Test with a domain without a _psl TXT record |
| 67 |
random_domain = "invalid-" + str(uuid.uuid4()) + ".edu" |
| 68 |
self.assertEqual("invalid", check_psl_txt_record(random_domain)) |
| 69 |
|
| 70 |
def test_get_whois_data(self): |
| 71 |
whois_data = get_whois_data("example.com") |
| 72 |
self.assertEqual("ok", whois_data[2]) |
| 73 |
|
| 74 |
|
| 75 |
if __name__ == "__main__": |
| 76 |
unittest.main() |