diff options
| -rw-r--r-- | src/mailman/rules/dmarc.py | 19 | ||||
| -rw-r--r-- | src/mailman/rules/tests/data/org_domain.txt | 2 | ||||
| -rw-r--r-- | src/mailman/rules/tests/test_dmarc.py | 21 |
3 files changed, 29 insertions, 13 deletions
diff --git a/src/mailman/rules/dmarc.py b/src/mailman/rules/dmarc.py index 2739ff937..a213a235b 100644 --- a/src/mailman/rules/dmarc.py +++ b/src/mailman/rules/dmarc.py @@ -102,12 +102,13 @@ def ensure_current_suffix_list(): return cached_copy_path -def parse_suffix_list(): +def parse_suffix_list(filename=None): # Parse the suffix list into a per process cache. - cached_copy_path = ensure_current_suffix_list() + if filename is None: + filename = ensure_current_suffix_list() # At this point the cached copy must exist and is as valid as possible. # Read and return the contents as a UTF-8 string. - with open(cached_copy_path, 'r', encoding='utf-8') as fp: + with open(filename, 'r', encoding='utf-8') as fp: for line in fp: if not line.strip() or line.startswith('//'): continue @@ -121,8 +122,8 @@ def parse_suffix_list(): else: exception = False parts.reverse() - k = DOT.join(parts) - suffix_cache[k] = exception + key = DOT.join(parts) + suffix_cache[key] = exception def _get_dom(d, l): @@ -227,7 +228,13 @@ def _DMARCProhibited(mlist, email, dmarc_domain, org=False): if mo: policy = mo.group(1).lower() else: - continue + # This continue does actually get covered by + # TestDMARCRules.test_domain_with_subdomain_policy() and + # TestDMARCRules.test_no_policy() but because of + # Coverage BitBucket issue #198 and + # http://bugs.python.org/issue2506 coverage cannot report + # it as such, so just pragma it away. + continue # pragma: no cover if policy in ('reject', 'quarantine'): vlog.info( """%s: DMARC lookup for %s (%s) diff --git a/src/mailman/rules/tests/data/org_domain.txt b/src/mailman/rules/tests/data/org_domain.txt index 4e6f7816e..e10db8a32 100644 --- a/src/mailman/rules/tests/data/org_domain.txt +++ b/src/mailman/rules/tests/data/org_domain.txt @@ -17,7 +17,7 @@ org !city.kobe.jp // A line with a trailing comment for testing -co.uk some nonesence +co.uk some nonsense // A line with leading white space (ignored) example.biz diff --git a/src/mailman/rules/tests/test_dmarc.py b/src/mailman/rules/tests/test_dmarc.py index 26eab5e56..d2ce28806 100644 --- a/src/mailman/rules/tests/test_dmarc.py +++ b/src/mailman/rules/tests/test_dmarc.py @@ -240,8 +240,7 @@ To: ant@example.com """) rule = dmarc.DMARCMitigation() - with get_dns_resolver( - rdata=b'v=DMARC1; sp=quarantine;'): + with get_dns_resolver(rdata=b'v=DMARC1; sp=quarantine;'): self.assertFalse(rule.check(mlist, msg, {})) def test_org_domain_with_subdomain_policy(self): @@ -254,8 +253,7 @@ To: ant@example.com """) rule = dmarc.DMARCMitigation() - with get_dns_resolver( - rdata=b'v=DMARC1; sp=quarantine;'): + with get_dns_resolver(rdata=b'v=DMARC1; sp=quarantine;'): self.assertTrue(rule.check(mlist, msg, {})) def test_wrong_dmarc_version(self): @@ -268,8 +266,7 @@ To: ant@example.com """) rule = dmarc.DMARCMitigation() - with get_dns_resolver( - rdata=b'v=DMARC01; p=reject;'): + with get_dns_resolver(rdata=b'v=DMARC01; p=reject;'): self.assertFalse(rule.check(mlist, msg, {})) def test_multiple_records(self): @@ -336,6 +333,18 @@ To: ant@example.com with get_dns_resolver(rdata=b'v=DMARC1; pct=100;'): self.assertFalse(rule.check(mlist, msg, {})) + def test_parser(self): + data_file = resource_filename( + 'mailman.rules.tests.data', 'org_domain.txt') + dmarc.parse_suffix_list(data_file) + # There is no entry for example.biz because that line starts with + # whitespace. + self.assertNotIn('biz.example', self.cache) + # The file had !city.kobe.jp so the flag says there's an exception. + self.assertTrue(self.cache['jp.kobe.city']) + # The file had *.kobe.jp so there's no exception. + self.assertFalse(self.cache['jp.kobe.*']) + # New in Python 3.5. try: |
