Debian Codegraph @

Lib/test/test_email/test_policy.py

raw
1 import io
2 import types
3 import textwrap
4 import unittest
5 import email.errors
6 import email.policy
7 import email.parser
8 import email.generator
9 import email.message
10 from email import headerregistry
11
12 def make_defaults(base_defaults, differences):
13 defaults = base_defaults.copy()
14 defaults.update(differences)
15 return defaults
16
17 class PolicyAPITests(unittest.TestCase):
18
19 longMessage = True
20
21 # Base default values.
22 compat32_defaults = {
23 'max_line_length': 78,
24 'linesep': '\n',
25 'cte_type': '8bit',
26 'raise_on_defect': False,
27 'mangle_from_': True,
28 'message_factory': None,
29 'verify_generated_headers': True,
30 }
31 # These default values are the ones set on email.policy.default.
32 # If any of these defaults change, the docs must be updated.
33 policy_defaults = compat32_defaults.copy()
34 policy_defaults.update({
35 'utf8': False,
36 'raise_on_defect': False,
37 'header_factory': email.policy.EmailPolicy.header_factory,
38 'refold_source': 'long',
39 'content_manager': email.policy.EmailPolicy.content_manager,
40 'mangle_from_': False,
41 'message_factory': email.message.EmailMessage,
42 })
43
44 # For each policy under test, we give here what we expect the defaults to
45 # be for that policy. The second argument to make defaults is the
46 # difference between the base defaults and that for the particular policy.
47 new_policy = email.policy.EmailPolicy()
48 policies = {
49 email.policy.compat32: make_defaults(compat32_defaults, {}),
50 email.policy.default: make_defaults(policy_defaults, {}),
51 email.policy.SMTP: make_defaults(policy_defaults,
52 {'linesep': '\r\n'}),
53 email.policy.SMTPUTF8: make_defaults(policy_defaults,
54 {'linesep': '\r\n',
55 'utf8': True}),
56 email.policy.HTTP: make_defaults(policy_defaults,
57 {'linesep': '\r\n',
58 'max_line_length': None}),
59 email.policy.strict: make_defaults(policy_defaults,
60 {'raise_on_defect': True}),
61 new_policy: make_defaults(policy_defaults, {}),
62 }
63 # Creating a new policy creates a new header factory. There is a test
64 # later that proves this.
65 policies[new_policy]['header_factory'] = new_policy.header_factory
66
67 def test_defaults(self):
68 for policy, expected in self.policies.items():
69 for attr, value in expected.items():
70 with self.subTest(policy=policy, attr=attr):
71 self.assertEqual(getattr(policy, attr), value,
72 ("change {} docs/docstrings if defaults have "
73 "changed").format(policy))
74
75 def test_all_attributes_covered(self):
76 for policy, expected in self.policies.items():
77 for attr in dir(policy):
78 with self.subTest(policy=policy, attr=attr):
79 if (attr.startswith('_') or
80 isinstance(getattr(email.policy.EmailPolicy, attr),
81 types.FunctionType)):
82 continue
83 else:
84 self.assertIn(attr, expected,
85 "{} is not fully tested".format(attr))
86
87 def test_abc(self):
88 with self.assertRaises(TypeError) as cm:
89 email.policy.Policy()
90 msg = str(cm.exception)
91 abstract_methods = ('fold',
92 'fold_binary',
93 'header_fetch_parse',
94 'header_source_parse',
95 'header_store_parse')
96 for method in abstract_methods:
97 self.assertIn(method, msg)
98
99 def test_policy_is_immutable(self):
100 for policy, defaults in self.policies.items():
101 for attr in defaults:
102 with self.assertRaisesRegex(AttributeError, attr+".*read-only"):
103 setattr(policy, attr, None)
104 with self.assertRaisesRegex(AttributeError, 'no attribute.*foo'):
105 policy.foo = None
106
107 def test_set_policy_attrs_when_cloned(self):
108 # None of the attributes has a default value of None, so we set them
109 # all to None in the clone call and check that it worked.
110 for policyclass, defaults in self.policies.items():
111 testattrdict = {attr: None for attr in defaults}
112 policy = policyclass.clone(**testattrdict)
113 for attr in defaults:
114 self.assertIsNone(getattr(policy, attr))
115
116 def test_reject_non_policy_keyword_when_called(self):
117 for policyclass in self.policies:
118 with self.assertRaises(TypeError):
119 policyclass(this_keyword_should_not_be_valid=None)
120 with self.assertRaises(TypeError):
121 policyclass(newtline=None)
122
123 def test_policy_addition(self):
124 expected = self.policy_defaults.copy()
125 p1 = email.policy.default.clone(max_line_length=100)
126 p2 = email.policy.default.clone(max_line_length=50)
127 added = p1 + p2
128 expected.update(max_line_length=50)
129 for attr, value in expected.items():
130 self.assertEqual(getattr(added, attr), value)
131 added = p2 + p1
132 expected.update(max_line_length=100)
133 for attr, value in expected.items():
134 self.assertEqual(getattr(added, attr), value)
135 added = added + email.policy.default
136 for attr, value in expected.items():
137 self.assertEqual(getattr(added, attr), value)
138
139 def test_fold_utf8(self):
140 expected_ascii = 'Subject: =?utf-8?q?=C3=A1?=\n'
141 expected_utf8 = 'Subject: á\n'
142
143 msg = email.message.EmailMessage()
144 s = 'á'
145 msg['Subject'] = s
146
147 p_ascii = email.policy.default.clone()
148 p_utf8 = email.policy.default.clone(utf8=True)
149
150 self.assertEqual(p_ascii.fold('Subject', msg['Subject']), expected_ascii)
151 self.assertEqual(p_utf8.fold('Subject', msg['Subject']), expected_utf8)
152
153 self.assertEqual(p_ascii.fold('Subject', s), expected_ascii)
154 self.assertEqual(p_utf8.fold('Subject', s), expected_utf8)
155
156 def test_fold_zero_max_line_length(self):
157 expected = 'Subject: =?utf-8?q?=C3=A1?=\n'
158
159 msg = email.message.EmailMessage()
160 msg['Subject'] = 'á'
161
162 p1 = email.policy.default.clone(max_line_length=0)
163 p2 = email.policy.default.clone(max_line_length=None)
164
165 self.assertEqual(p1.fold('Subject', msg['Subject']), expected)
166 self.assertEqual(p2.fold('Subject', msg['Subject']), expected)
167
168 def test_register_defect(self):
169 class Dummy:
170 def __init__(self):
171 self.defects = []
172 obj = Dummy()
173 defect = object()
174 policy = email.policy.EmailPolicy()
175 policy.register_defect(obj, defect)
176 self.assertEqual(obj.defects, [defect])
177 defect2 = object()
178 policy.register_defect(obj, defect2)
179 self.assertEqual(obj.defects, [defect, defect2])
180
181 class MyObj:
182 def __init__(self):
183 self.defects = []
184
185 class MyDefect(Exception):
186 pass
187
188 def test_handle_defect_raises_on_strict(self):
189 foo = self.MyObj()
190 defect = self.MyDefect("the telly is broken")
191 with self.assertRaisesRegex(self.MyDefect, "the telly is broken"):
192 email.policy.strict.handle_defect(foo, defect)
193
194 def test_handle_defect_registers_defect(self):
195 foo = self.MyObj()
196 defect1 = self.MyDefect("one")
197 email.policy.default.handle_defect(foo, defect1)
198 self.assertEqual(foo.defects, [defect1])
199 defect2 = self.MyDefect("two")
200 email.policy.default.handle_defect(foo, defect2)
201 self.assertEqual(foo.defects, [defect1, defect2])
202
203 class MyPolicy(email.policy.EmailPolicy):
204 defects = None
205 def __init__(self, *args, **kw):
206 super().__init__(*args, defects=[], **kw)
207 def register_defect(self, obj, defect):
208 self.defects.append(defect)
209
210 def test_overridden_register_defect_still_raises(self):
211 foo = self.MyObj()
212 defect = self.MyDefect("the telly is broken")
213 with self.assertRaisesRegex(self.MyDefect, "the telly is broken"):
214 self.MyPolicy(raise_on_defect=True).handle_defect(foo, defect)
215
216 def test_overridden_register_defect_works(self):
217 foo = self.MyObj()
218 defect1 = self.MyDefect("one")
219 my_policy = self.MyPolicy()
220 my_policy.handle_defect(foo, defect1)
221 self.assertEqual(my_policy.defects, [defect1])
222 self.assertEqual(foo.defects, [])
223 defect2 = self.MyDefect("two")
224 my_policy.handle_defect(foo, defect2)
225 self.assertEqual(my_policy.defects, [defect1, defect2])
226 self.assertEqual(foo.defects, [])
227
228 def test_default_header_factory(self):
229 h = email.policy.default.header_factory('Test', 'test')
230 self.assertEqual(h.name, 'Test')
231 self.assertIsInstance(h, headerregistry.UnstructuredHeader)
232 self.assertIsInstance(h, headerregistry.BaseHeader)
233
234 class Foo:
235 parse = headerregistry.UnstructuredHeader.parse
236
237 def test_each_Policy_gets_unique_factory(self):
238 policy1 = email.policy.EmailPolicy()
239 policy2 = email.policy.EmailPolicy()
240 policy1.header_factory.map_to_type('foo', self.Foo)
241 h = policy1.header_factory('foo', 'test')
242 self.assertIsInstance(h, self.Foo)
243 self.assertNotIsInstance(h, headerregistry.UnstructuredHeader)
244 h = policy2.header_factory('foo', 'test')
245 self.assertNotIsInstance(h, self.Foo)
246 self.assertIsInstance(h, headerregistry.UnstructuredHeader)
247
248 def test_clone_copies_factory(self):
249 policy1 = email.policy.EmailPolicy()
250 policy2 = policy1.clone()
251 policy1.header_factory.map_to_type('foo', self.Foo)
252 h = policy1.header_factory('foo', 'test')
253 self.assertIsInstance(h, self.Foo)
254 h = policy2.header_factory('foo', 'test')
255 self.assertIsInstance(h, self.Foo)
256
257 def test_new_factory_overrides_default(self):
258 mypolicy = email.policy.EmailPolicy()
259 myfactory = mypolicy.header_factory
260 newpolicy = mypolicy + email.policy.strict
261 self.assertEqual(newpolicy.header_factory, myfactory)
262 newpolicy = email.policy.strict + mypolicy
263 self.assertEqual(newpolicy.header_factory, myfactory)
264
265 def test_adding_default_policies_preserves_default_factory(self):
266 newpolicy = email.policy.default + email.policy.strict
267 self.assertEqual(newpolicy.header_factory,
268 email.policy.EmailPolicy.header_factory)
269 self.assertEqual(newpolicy.__dict__, {'raise_on_defect': True})
270
271 def test_non_ascii_chars_do_not_cause_inf_loop(self):
272 policy = email.policy.default.clone(max_line_length=20)
273 actual = policy.fold('Subject', 'ą' * 12)
274 self.assertEqual(
275 actual,
276 'Subject:\n' +
277 12 * ' =?utf-8?q?=C4=85?=\n')
278
279 def test_short_maxlen_error(self):
280 # RFC 2047 chrome takes up 7 characters, plus the length of the charset
281 # name, so folding should fail if maxlen is lower than the minimum
282 # required length for a line.
283
284 # Note: This is only triggered when there is a single word longer than
285 # max_line_length, hence the 1234567890 at the end of this whimsical
286 # subject. This is because when we encounter a word longer than
287 # max_line_length, it is broken down into encoded words to fit
288 # max_line_length. If the max_line_length isn't large enough to even
289 # contain the RFC 2047 chrome (`?=<charset>?q??=`), we fail.
290 subject = "Melt away the pounds with this one simple trick! 1234567890"
291
292 for maxlen in [3, 7, 9]:
293 with self.subTest(maxlen=maxlen):
294 policy = email.policy.default.clone(max_line_length=maxlen)
295 with self.assertRaises(email.errors.HeaderParseError):
296 policy.fold("Subject", subject)
297
298 def test_verify_generated_headers(self):
299 # Turning protection off allows header injection
300 policy = email.policy.default.clone(verify_generated_headers=False)
301 for text in (
302 'Header: Value\r\nBad: Injection\r\n',
303 'Header: NoNewLine'
304 ):
305 with self.subTest(text=text):
306 message = email.message_from_string(
307 "Header: Value\r\n\r\nBody",
308 policy=policy,
309 )
310 class LiteralHeader(str):
311 name = 'Header'
312 def fold(self, **kwargs):
313 return self
314
315 del message['Header']
316 message['Header'] = LiteralHeader(text)
317
318 self.assertEqual(
319 message.as_string(),
320 f"{text}\nBody",
321 )
322 self.assertEqual(
323 message.as_bytes(),
324 f"{text}\nBody".encode(),
325 )
326
327 # XXX: Need subclassing tests.
328 # For adding subclassed objects, make sure the usual rules apply (subclass
329 # wins), but that the order still works (right overrides left).
330
331
332 class TestException(Exception):
333 pass
334
335 class TestPolicyPropagation(unittest.TestCase):
336
337 # The abstract methods are used by the parser but not by the wrapper
338 # functions that call it, so if the exception gets raised we know that the
339 # policy was actually propagated all the way to feedparser.
340 class MyPolicy(email.policy.Policy):
341 def badmethod(self, *args, **kw):
342 raise TestException("test")
343 fold = fold_binary = header_fetch_parser = badmethod
344 header_source_parse = header_store_parse = badmethod
345
346 def test_message_from_string(self):
347 with self.assertRaisesRegex(TestException, "^test$"):
348 email.message_from_string("Subject: test\n\n",
349 policy=self.MyPolicy)
350
351 def test_message_from_bytes(self):
352 with self.assertRaisesRegex(TestException, "^test$"):
353 email.message_from_bytes(b"Subject: test\n\n",
354 policy=self.MyPolicy)
355
356 def test_message_from_file(self):
357 f = io.StringIO('Subject: test\n\n')
358 with self.assertRaisesRegex(TestException, "^test$"):
359 email.message_from_file(f, policy=self.MyPolicy)
360
361 def test_message_from_binary_file(self):
362 f = io.BytesIO(b'Subject: test\n\n')
363 with self.assertRaisesRegex(TestException, "^test$"):
364 email.message_from_binary_file(f, policy=self.MyPolicy)
365
366 # These are redundant, but we need them for black-box completeness.
367
368 def test_parser(self):
369 p = email.parser.Parser(policy=self.MyPolicy)
370 with self.assertRaisesRegex(TestException, "^test$"):
371 p.parsestr('Subject: test\n\n')
372
373 def test_bytes_parser(self):
374 p = email.parser.BytesParser(policy=self.MyPolicy)
375 with self.assertRaisesRegex(TestException, "^test$"):
376 p.parsebytes(b'Subject: test\n\n')
377
378 # Now that we've established that all the parse methods get the
379 # policy in to feedparser, we can use message_from_string for
380 # the rest of the propagation tests.
381
382 def _make_msg(self, source='Subject: test\n\n', policy=None):
383 self.policy = email.policy.default.clone() if policy is None else policy
384 return email.message_from_string(source, policy=self.policy)
385
386 def test_parser_propagates_policy_to_message(self):
387 msg = self._make_msg()
388 self.assertIs(msg.policy, self.policy)
389
390 def test_parser_propagates_policy_to_sub_messages(self):
391 msg = self._make_msg(textwrap.dedent("""\
392 Subject: mime test
393 MIME-Version: 1.0
394 Content-Type: multipart/mixed, boundary="XXX"
395
396 --XXX
397 Content-Type: text/plain
398
399 test
400 --XXX
401 Content-Type: text/plain
402
403 test2
404 --XXX--
405 """))
406 for part in msg.walk():
407 self.assertIs(part.policy, self.policy)
408
409 def test_message_policy_propagates_to_generator(self):
410 msg = self._make_msg("Subject: test\nTo: foo\n\n",
411 policy=email.policy.default.clone(linesep='X'))
412 s = io.StringIO()
413 g = email.generator.Generator(s)
414 g.flatten(msg)
415 self.assertEqual(s.getvalue(), "Subject: testXTo: fooXX")
416
417 def test_message_policy_used_by_as_string(self):
418 msg = self._make_msg("Subject: test\nTo: foo\n\n",
419 policy=email.policy.default.clone(linesep='X'))
420 self.assertEqual(msg.as_string(), "Subject: testXTo: fooXX")
421
422
423 class TestConcretePolicies(unittest.TestCase):
424
425 def test_header_store_parse_rejects_newlines(self):
426 instance = email.policy.EmailPolicy()
427 self.assertRaises(ValueError,
428 instance.header_store_parse,
429 'From', 'spam\[email protected]')
430
431
432 if __name__ == '__main__':
433 unittest.main()