-
Notifications
You must be signed in to change notification settings - Fork 78
/
tests.py
executable file
·360 lines (304 loc) · 11.9 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Automated tests"""
import unittest
from collections import OrderedDict
import checkdmarc
import checkdmarc.utils
import checkdmarc.spf
import checkdmarc.dmarc
import checkdmarc.dnssec
import checkdmarc.bimi
known_good_domains = ["fbi.gov", "pm.me"]
class Test(unittest.TestCase):
@unittest.skip
def testKnownGood(self):
"""Domains with known good STARTTLS support, SPF and DMARC records"""
results = checkdmarc.check_domains(known_good_domains)
for result in results:
spf_error = None
dmarc_error = None
for mx in result["mx"]["hosts"]:
self.assertEqual(
mx["starttls"],
True,
"Host of known good domain {0} failed STARTTLS check: {1}"
"\n\n{0}".format(result["domain"], mx["hostname"]),
)
if "error" in result["spf"]:
spf_error = result["spf"]["error"]
if "error" in result["dmarc"]:
dmarc_error = result["dmarc"]["error"]
self.assertEqual(
result["spf"]["valid"],
True,
"Known good domain {0} failed SPF check:"
"\n\n{1}".format(result["domain"], spf_error),
)
self.assertEqual(
result["dmarc"]["valid"],
True,
"Known good domain {0} failed DMARC check:"
"\n\n{1}".format(result["domain"], dmarc_error),
)
def testDMARCMixedFormatting(self):
"""DMARC records with extra spaces and mixed case are still valid"""
examples = [
"v=DMARC1;p=ReJect",
"v = DMARC1;p=reject;",
"v = DMARC1\t;\tp=reject\t;",
"v = DMARC1\t;\tp\t\t\t=\t\t\treject\t;",
"V=DMARC1;p=reject;",
]
for example in examples:
parsed_record = checkdmarc.dmarc.parse_dmarc_record(example, "")
self.assertIsInstance(parsed_record, OrderedDict)
def testGetBaseDomain(self):
subdomain = "foo.example.com"
result = checkdmarc.utils.get_base_domain(subdomain)
assert result == "example.com"
# Test reserved domains
subdomain = "_dmarc.nonauth-rua.invalid.example"
result = checkdmarc.utils.get_base_domain(subdomain)
assert result == "invalid.example"
subdomain = "_dmarc.nonauth-rua.invalid.test"
result = checkdmarc.utils.get_base_domain(subdomain)
assert result == "invalid.test"
subdomain = "_dmarc.nonauth-rua.invalid.invalid"
result = checkdmarc.utils.get_base_domain(subdomain)
assert result == "invalid.invalid"
subdomain = "_dmarc.nonauth-rua.invalid.localhost"
result = checkdmarc.utils.get_base_domain(subdomain)
assert result == "invalid.localhost"
# Test newer PSL entries
subdomain = "e3191.c.akamaiedge.net"
result = checkdmarc.utils.get_base_domain(subdomain)
assert result == "c.akamaiedge.net"
def testUppercaseSPFMechanism(self):
"""Treat uppercase SPF mechanisms as valid"""
spf_record = "v=spf1 IP4:147.75.8.208 -ALL"
domain = "example.no"
results = checkdmarc.spf.parse_spf_record(spf_record, domain)
self.assertEqual(len(results["warnings"]), 0)
def testSplitSPFRecord(self):
"""Split SPF records are parsed properly"""
rec = '"v=spf1 ip4:147.75.8.208 " "include:_spf.salesforce.com -all"'
parsed_record = checkdmarc.spf.parse_spf_record(rec, "example.com")
self.assertEqual(parsed_record["parsed"]["all"], "fail")
def testJunkAfterAll(self):
"""Ignore any mechanisms after the all mechanism, but warn about it"""
rec = "v=spf1 ip4:213.5.39.110 -all MS=83859DAEBD1978F9A7A67D3"
domain = "avd.dk"
parsed_record = checkdmarc.spf.parse_spf_record(rec, domain)
self.assertEqual(len(parsed_record["warnings"]), 1)
@unittest.skip
def testDNSSEC(self):
"""Test known good DNSSEC"""
self.assertEqual(checkdmarc.dnssec.test_dnssec("fbi.gov"), True)
def testIncludeMissingSPF(self):
"""SPF records that include domains that are missing SPF records
raise SPFRecordNotFound"""
spf_record = (
'"v=spf1 include:spf.comendosystems.com '
"include:bounce.peytz.dk include:etrack.indicia.dk "
"include:etrack1.com include:mail1.dialogportal.com "
"include:mail2.dialogportal.com a:mailrelay.jppol.dk "
'a:sendmail.jppol.dk ?all"'
)
domain = "ekstrabladet.dk"
self.assertRaises(
checkdmarc.spf.SPFRecordNotFound,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testTooManySPFDNSLookups(self):
"""SPF records with > 10 SPF mechanisms that cause DNS lookups raise
SPFTooManyDNSLookups"""
spf_record = (
"v=spf1 a include:_spf.salesforce.com "
"include:spf.protection.outlook.com "
"include:spf.constantcontact.com "
"include:_spf.elasticemail.com "
"include:servers.mcsv.net "
"include:_spf.google.com "
"~all"
)
domain = "example.com"
self.assertRaises(
checkdmarc.spf.SPFTooManyDNSLookups,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testTooManySPFVoidDNSLookups(self):
"""SPF records with > 2 void DNS lookups"""
spf_record = (
"v=spf1 a:13Mk4olS9VWhQqXRl90fKJrD.example.com "
"mx:SfGiqBnQfRbOMapQJhozxo2B.example.com "
"a:VAFeyU9N2KJX518aGsN3w6VS.example.com "
"~all"
)
domain = "example.com"
self.assertRaises(
checkdmarc.spf.SPFTooManyVoidDNSLookups,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFSyntaxErrors(self):
"""SPF record syntax errors raise SPFSyntaxError"""
spf_record = (
'"v=spf1 mx a:mail.cohaesio.net ' 'include: trustpilotservice.com ~all"'
)
domain = "2021.ai"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFInvalidIPv4(self):
"""Invalid ipv4 SPF mechanism values raise SPFSyntaxError"""
spf_record = (
"v=spf1 ip4:78.46.96.236 +a +mx +ip4:138.201.239.158 "
"+ip4:78.46.224.83 "
"+ip4:relay.mailchannels.net +ip4:138.201.60.20 ~all"
)
domain = "surftown.dk"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFInvalidIPv6inIPv4(self):
"""Invalid ipv4 SPF mechanism values raise SPFSyntaxError"""
spf_record = "v=spf1 ip4:1200:0000:AB00:1234:0000:2552:7777:1313 ~all"
domain = "surftown.dk"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFInvalidIPv4Range(self):
"""Invalid ipv4 SPF mechanism values raise SPFSyntaxError"""
spf_record = "v=spf1 ip4:78.46.96.236/99 ~all"
domain = "surftown.dk"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFInvalidIPv6(self):
"""Invalid ipv6 SPF mechanism values raise SPFSyntaxError"""
spf_record = "v=spf1 ip6:1200:0000:AB00:1234:O000:2552:7777:1313 ~all"
domain = "surftown.dk"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFInvalidIPv4inIPv6(self):
"""Invalid ipv6 SPF mechanism values raise SPFSyntaxError"""
spf_record = "v=spf1 ip6:78.46.96.236 ~all"
domain = "surftown.dk"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFInvalidIPv6Range(self):
"""Invalid ipv6 SPF mechanism values raise SPFSyntaxError"""
record = "v=spf1 ip6:1200:0000:AB00:1234:0000:2552:7777:1313/130 ~all"
domain = "surftown.dk"
self.assertRaises(
checkdmarc.spf.SPFSyntaxError,
checkdmarc.spf.parse_spf_record,
record,
domain,
)
def testSPFIncludeLoop(self):
"""SPF record with include loop raises SPFIncludeLoop"""
spf_record = '"v=spf1 include:example.com"'
domain = "example.com"
self.assertRaises(
checkdmarc.spf.SPFIncludeLoop,
checkdmarc.spf.parse_spf_record,
spf_record,
domain,
)
def testSPFMissingMXRecord(self):
"""A warning is issued if an SPF record contains a mx mechanism
pointing to a domain that has no MX records"""
spf_record = '"v=spf1 mx ~all"'
domain = "seanthegeek.net"
results = checkdmarc.spf.parse_spf_record(spf_record, domain)
self.assertIn(
"{0} does not have any MX records".format(domain), results["warnings"]
)
def testSPFMissingARecord(self):
"""A warning is issued if an SPF record contains a mx mechanism
pointing to a domain that has no A records"""
spf_record = '"v=spf1 a ~all"'
domain = "cardinalhealth.net"
results = checkdmarc.spf.parse_spf_record(spf_record, domain)
self.assertIn(
"cardinalhealth.net does not have any A/AAAA records", results["warnings"]
)
def testDMARCPctLessThan100Warning(self):
"""A warning is issued if the DMARC pvt value is less than 100"""
dmarc_record = (
"v=DMARC1; p=none; sp=none; fo=1; pct=50; adkim=r; "
"aspf=r; rf=afrf; ri=86400; "
"rua=mailto:[email protected]; "
"ruf=mailto:[email protected]"
)
domain = "energy.gov"
results = checkdmarc.dmarc.parse_dmarc_record(dmarc_record, domain)
self.assertIn("pct value is less than 100", results["warnings"][0])
def testInvalidDMARCURI(self):
"""An invalid DMARC report URI raises InvalidDMARCReportURI"""
dmarc_record = (
"v=DMARC1; p=none; [email protected],"
"mailto:[email protected]"
)
domain = "dea.gov"
self.assertRaises(
checkdmarc.dmarc.InvalidDMARCReportURI,
checkdmarc.dmarc.parse_dmarc_record,
dmarc_record,
domain,
)
dmarc_record = (
"v=DMARC1; p=none; rua=__"
"mailto:[email protected],"
"mailto:[email protected]"
)
self.assertRaises(
checkdmarc.dmarc.InvalidDMARCReportURI,
checkdmarc.dmarc.parse_dmarc_record,
dmarc_record,
domain,
)
def testInvalidDMARCPolicyValue(self):
"""An invalid DMARC policy value raises InvalidDMARCTagValue"""
dmarc_record = "v=DMARC1; p=foo; rua=mailto:[email protected]"
domain = "example.com"
self.assertRaises(
checkdmarc.dmarc.InvalidDMARCTagValue,
checkdmarc.dmarc.parse_dmarc_record,
dmarc_record,
domain,
)
def testBIMI(self):
"""Test BIMI checks"""
domain = "chase.com"
results = checkdmarc.bimi.check_bimi(domain)
self.assertEqual(len(results["warnings"]), 0)
if __name__ == "__main__":
unittest.main(verbosity=2)