Debian Codegraph @

test/util/mail_diff

raw
1 #!/usr/bin/env python3
2
3 import difflib
4 import email.parser
5 import email.policy
6 import itertools
7 import mailbox
8 import sys
9
10
11 def flexible_eq(expected, got):
12 """Compare two strings, supporting wildcards.
13
14 This functions compares two strings, but supports wildcards on the
15 expected string. The following characters have special meaning:
16
17 - ? matches any character.
18 - * matches anything until the end of the line.
19
20 Returns True if equal (considering wildcards), False otherwise.
21 """
22 posG = 0
23 for c in expected:
24 if posG >= len(got):
25 return False
26
27 if c == "?":
28 posG += 1
29 continue
30 if c == "*":
31 while posG < len(got) and got[posG] != "\t":
32 posG += 1
33 continue
34 continue
35
36 if c != got[posG]:
37 return False
38
39 posG += 1
40
41 if posG != len(got):
42 # We got more than we expected.
43 return False
44
45 return True
46
47
48 def msg_equals(expected, msg):
49 """Compare two messages recursively, using flexible_eq()."""
50 diff = False
51 for h, val in expected.items():
52 if h not in msg:
53 print("Header missing: %r" % h)
54 diff = True
55 continue
56
57 if expected[h] == "*":
58 continue
59
60 msg_hdr_vals = msg.get_all(h, [])
61 if len(msg_hdr_vals) == 1:
62 if not flexible_eq(val, msg[h]):
63 print("Header %r differs:" % h)
64 print("Exp: %r" % val)
65 print("Got: %r" % msg[h])
66 diff = True
67 else:
68 # We have multiple values for this header, so we need to check each
69 # one, and only return a diff if none of them match.
70 # Note this will result in a false positive if two headers match
71 # the same expected one, but this is good enough for now.
72 for msg_hdr_val in msg_hdr_vals:
73 if flexible_eq(val, msg_hdr_val):
74 break
75 else:
76 print("Header %r differs, no matching header found" % h)
77 print("Exp: %r" % val)
78 for i, msg_hdr_val in enumerate(msg_hdr_vals):
79 print("Got %d: %r" % (i, msg_hdr_val))
80 diff = True
81
82 if diff:
83 return False
84
85 if expected.is_multipart() != msg.is_multipart():
86 print(
87 "Multipart differs, expected %s, got %s"
88 % (expected.is_multipart(), msg.is_multipart())
89 )
90 return False
91
92 if expected.is_multipart():
93 for exp, got in itertools.zip_longest(
94 expected.get_payload(), msg.get_payload()
95 ):
96 if not msg_equals(exp, got):
97 return False
98 else:
99 if not flexible_eq(expected.get_payload(), msg.get_payload()):
100 exp = expected.get_payload().splitlines()
101 got = msg.get_payload().splitlines()
102 print("Payload differs:")
103 for l in difflib.ndiff(exp, got):
104 print(l)
105 return False
106
107 return True
108
109
110 if __name__ == "__main__":
111 f1, f2 = sys.argv[1:3]
112
113 # We use a custom strict policy to do more strict content validation.
114 policy = email.policy.EmailPolicy(
115 utf8=True, linesep="\r\n", refold_source="none", raise_on_defect=True
116 )
117
118 expected = email.parser.Parser(policy=policy).parse(open(f1))
119 msg = email.parser.Parser(policy=policy).parse(open(f2))
120
121 sys.exit(0 if msg_equals(expected, msg) else 1)