Debian Codegraph @

Lib/test/test_email/test_generator.py

raw
1 import io
2 import textwrap
3 import unittest
4 import random
5 import sys
6 from email import message_from_string, message_from_bytes
7 from email.message import EmailMessage
8 from email.mime.multipart import MIMEMultipart
9 from email.mime.text import MIMEText
10 from email.generator import Generator, BytesGenerator
11 import email.generator
12 from email.headerregistry import Address
13 from email import policy
14 import email.errors
15 from test.test_email import TestEmailBase, parameterize
16 import test.support
17
18
19
20 @parameterize
21 class TestGeneratorBase:
22
23 policy = policy.default
24
25 def msgmaker(self, msg, policy=None):
26 policy = self.policy if policy is None else policy
27 return self.msgfunc(msg, policy=policy)
28
29 refold_long_expected = {
30 0: textwrap.dedent("""\
31 To: [email protected]
32 From: [email protected]
33 Subject: We the willing led by the unknowing are doing the
34 impossible for the ungrateful. We have done so much for so long with so little
35 we are now qualified to do anything with nothing.
36
37 None
38 """),
39 40: textwrap.dedent("""\
40 To: [email protected]
41 From:
42 [email protected]
43 Subject: We the willing led by the
44 unknowing are doing the impossible for
45 the ungrateful. We have done so much
46 for so long with so little we are now
47 qualified to do anything with nothing.
48
49 None
50 """),
51 20: textwrap.dedent("""\
52 To:
53 [email protected]
54 From:
55 [email protected]
56 Subject: We the
57 willing led by the
58 unknowing are doing
59 the impossible for
60 the ungrateful. We
61 have done so much
62 for so long with so
63 little we are now
64 qualified to do
65 anything with
66 nothing.
67
68 None
69 """),
70 }
71 refold_long_expected[100] = refold_long_expected[0]
72
73 refold_all_expected = refold_long_expected.copy()
74 refold_all_expected[0] = (
75 "To: [email protected]\n"
76 "From: [email protected]\n"
77 "Subject: We the willing led by the unknowing are doing the "
78 "impossible for the ungrateful. We have done so much for "
79 "so long with so little we are now qualified to do anything "
80 "with nothing.\n"
81 "\n"
82 "None\n")
83 refold_all_expected[100] = (
84 "To: [email protected]\n"
85 "From: [email protected]\n"
86 "Subject: We the willing led by the unknowing are doing the "
87 "impossible for the ungrateful. We have\n"
88 " done so much for so long with so little we are now qualified "
89 "to do anything with nothing.\n"
90 "\n"
91 "None\n")
92
93 length_params = [n for n in refold_long_expected]
94
95 def length_as_maxheaderlen_parameter(self, n):
96 msg = self.msgmaker(self.typ(self.refold_long_expected[0]))
97 s = self.ioclass()
98 g = self.genclass(s, maxheaderlen=n, policy=self.policy)
99 g.flatten(msg)
100 self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n]))
101
102 def length_as_max_line_length_policy(self, n):
103 msg = self.msgmaker(self.typ(self.refold_long_expected[0]))
104 s = self.ioclass()
105 g = self.genclass(s, policy=self.policy.clone(max_line_length=n))
106 g.flatten(msg)
107 self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n]))
108
109 def length_as_maxheaderlen_parm_overrides_policy(self, n):
110 msg = self.msgmaker(self.typ(self.refold_long_expected[0]))
111 s = self.ioclass()
112 g = self.genclass(s, maxheaderlen=n,
113 policy=self.policy.clone(max_line_length=10))
114 g.flatten(msg)
115 self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n]))
116
117 def length_as_max_line_length_with_refold_none_does_not_fold(self, n):
118 msg = self.msgmaker(self.typ(self.refold_long_expected[0]))
119 s = self.ioclass()
120 g = self.genclass(s, policy=self.policy.clone(refold_source='none',
121 max_line_length=n))
122 g.flatten(msg)
123 self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[0]))
124
125 def length_as_max_line_length_with_refold_all_folds(self, n):
126 msg = self.msgmaker(self.typ(self.refold_long_expected[0]))
127 s = self.ioclass()
128 g = self.genclass(s, policy=self.policy.clone(refold_source='all',
129 max_line_length=n))
130 g.flatten(msg)
131 self.assertEqual(s.getvalue(), self.typ(self.refold_all_expected[n]))
132
133 def test_crlf_control_via_policy(self):
134 source = "Subject: test\r\n\r\ntest body\r\n"
135 expected = source
136 msg = self.msgmaker(self.typ(source))
137 s = self.ioclass()
138 g = self.genclass(s, policy=policy.SMTP)
139 g.flatten(msg)
140 self.assertEqual(s.getvalue(), self.typ(expected))
141
142 def test_flatten_linesep_overrides_policy(self):
143 source = "Subject: test\n\ntest body\n"
144 expected = source
145 msg = self.msgmaker(self.typ(source))
146 s = self.ioclass()
147 g = self.genclass(s, policy=policy.SMTP)
148 g.flatten(msg, linesep='\n')
149 self.assertEqual(s.getvalue(), self.typ(expected))
150
151 def test_flatten_linesep(self):
152 source = 'Subject: one\n two\r three\r\n four\r\n\r\ntest body\r\n'
153 msg = self.msgmaker(self.typ(source))
154 self.assertEqual(msg['Subject'], 'one two three four')
155
156 expected = 'Subject: one\n two\n three\n four\n\ntest body\n'
157 s = self.ioclass()
158 g = self.genclass(s)
159 g.flatten(msg)
160 self.assertEqual(s.getvalue(), self.typ(expected))
161
162 expected = 'Subject: one two three four\n\ntest body\n'
163 s = self.ioclass()
164 g = self.genclass(s, policy=self.policy.clone(refold_source='all'))
165 g.flatten(msg)
166 self.assertEqual(s.getvalue(), self.typ(expected))
167
168 def test_flatten_control_linesep(self):
169 source = 'Subject: one\v two\f three\x1c four\x1d five\x1e six\r\n\r\ntest body\r\n'
170 msg = self.msgmaker(self.typ(source))
171 self.assertEqual(msg['Subject'], 'one\v two\f three\x1c four\x1d five\x1e six')
172
173 expected = 'Subject: one\v two\f three\x1c four\x1d five\x1e six\n\ntest body\n'
174 s = self.ioclass()
175 g = self.genclass(s)
176 g.flatten(msg)
177 self.assertEqual(s.getvalue(), self.typ(expected))
178
179 s = self.ioclass()
180 g = self.genclass(s, policy=self.policy.clone(refold_source='all'))
181 g.flatten(msg)
182 self.assertEqual(s.getvalue(), self.typ(expected))
183
184 def test_set_mangle_from_via_policy(self):
185 source = textwrap.dedent("""\
186 Subject: test that
187 from is mangled in the body!
188
189 From time to time I write a rhyme.
190 """)
191 variants = (
192 (None, True),
193 (policy.compat32, True),
194 (policy.default, False),
195 (policy.default.clone(mangle_from_=True), True),
196 )
197 for p, mangle in variants:
198 expected = source.replace('From ', '>From ') if mangle else source
199 with self.subTest(policy=p, mangle_from_=mangle):
200 msg = self.msgmaker(self.typ(source))
201 s = self.ioclass()
202 g = self.genclass(s, policy=p)
203 g.flatten(msg)
204 self.assertEqual(s.getvalue(), self.typ(expected))
205
206 def test_compat32_max_line_length_does_not_fold_when_none(self):
207 msg = self.msgmaker(self.typ(self.refold_long_expected[0]))
208 s = self.ioclass()
209 g = self.genclass(s, policy=policy.compat32.clone(max_line_length=None))
210 g.flatten(msg)
211 self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[0]))
212
213 def test_rfc2231_wrapping(self):
214 # This is pretty much just to make sure we don't have an infinite
215 # loop; I don't expect anyone to hit this in the field.
216 msg = self.msgmaker(self.typ(textwrap.dedent("""\
217 To: nobody
218 Content-Disposition: attachment;
219 filename="afilenamelongenoghtowraphere"
220
221 None
222 """)))
223 expected = textwrap.dedent("""\
224 To: nobody
225 Content-Disposition: attachment;
226 filename*0*=us-ascii''afilename;
227 filename*1*=longenoghtowraphere
228
229 None
230 """)
231 s = self.ioclass()
232 g = self.genclass(s, policy=self.policy.clone(max_line_length=33))
233 g.flatten(msg)
234 self.assertEqual(s.getvalue(), self.typ(expected))
235
236 def test_rfc2231_wrapping_switches_to_default_len_if_too_narrow(self):
237 # This is just to make sure we don't have an infinite loop; I don't
238 # expect anyone to hit this in the field, so I'm not bothering to make
239 # the result optimal (the encoding isn't needed).
240 msg = self.msgmaker(self.typ(textwrap.dedent("""\
241 To: nobody
242 Content-Disposition: attachment;
243 filename="afilenamelongenoghtowraphere"
244
245 None
246 """)))
247 expected = textwrap.dedent("""\
248 To: nobody
249 Content-Disposition:
250 attachment;
251 filename*0*=us-ascii''afilenamelongenoghtowraphere
252
253 None
254 """)
255 s = self.ioclass()
256 g = self.genclass(s, policy=self.policy.clone(max_line_length=20))
257 g.flatten(msg)
258 self.assertEqual(s.getvalue(), self.typ(expected))
259
260 def test_keep_encoded_newlines(self):
261 msg = self.msgmaker(self.typ(textwrap.dedent("""\
262 To: nobody
263 Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: [email protected]
264
265 None
266 """)))
267 expected = textwrap.dedent("""\
268 To: nobody
269 Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: [email protected]
270
271 None
272 """)
273 s = self.ioclass()
274 g = self.genclass(s, policy=self.policy.clone(max_line_length=80))
275 g.flatten(msg)
276 self.assertEqual(s.getvalue(), self.typ(expected))
277
278 def test_keep_long_encoded_newlines(self):
279 msg = self.msgmaker(self.typ(textwrap.dedent("""\
280 To: nobody
281 Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: [email protected]
282
283 None
284 """)))
285 expected = textwrap.dedent("""\
286 To: nobody
287 Subject: Bad subject
288 =?utf-8?q?=0A?=Bcc:
289 [email protected]
290
291 None
292 """)
293 s = self.ioclass()
294 g = self.genclass(s, policy=self.policy.clone(max_line_length=30))
295 g.flatten(msg)
296 self.assertEqual(s.getvalue(), self.typ(expected))
297
298 def _test_boundary_detection(self, linesep):
299 # Generate a boundary token in the same way as _make_boundary
300 token = random.randrange(sys.maxsize)
301
302 def _patch_random_randrange(*args, **kwargs):
303 return token
304
305 with test.support.swap_attr(
306 random, "randrange", _patch_random_randrange
307 ):
308 boundary = self.genclass._make_boundary(text=None)
309 boundary_in_part = (
310 "this goes before the boundary\n--"
311 + boundary
312 + "\nthis goes after\n"
313 )
314 msg = MIMEMultipart()
315 msg.attach(MIMEText(boundary_in_part))
316 self.genclass(self.ioclass()).flatten(msg, linesep=linesep)
317 # Generator checks the message content for the string it is about
318 # to use as a boundary ('token' in this test) and when it finds it
319 # in our attachment appends .0 to make the boundary it uses unique.
320 self.assertEqual(msg.get_boundary(), boundary + ".0")
321
322 def test_lf_boundary_detection(self):
323 self._test_boundary_detection("\n")
324
325 def test_crlf_boundary_detection(self):
326 self._test_boundary_detection("\r\n")
327
328
329 class TestGenerator(TestGeneratorBase, TestEmailBase):
330
331 msgfunc = staticmethod(message_from_string)
332 genclass = Generator
333 ioclass = io.StringIO
334 typ = str
335
336 def test_flatten_unicode_linesep(self):
337 source = 'Subject: one\x85 two\u2028 three\u2029 four\r\n\r\ntest body\r\n'
338 msg = self.msgmaker(self.typ(source))
339 self.assertEqual(msg['Subject'], 'one\x85 two\u2028 three\u2029 four')
340
341 expected = 'Subject: =?utf-8?b?b25lwoUgdHdv4oCoIHRocmVl4oCp?= four\n\ntest body\n'
342 s = self.ioclass()
343 g = self.genclass(s)
344 g.flatten(msg)
345 self.assertEqual(s.getvalue(), self.typ(expected))
346
347 s = self.ioclass()
348 g = self.genclass(s, policy=self.policy.clone(refold_source='all'))
349 g.flatten(msg)
350 self.assertEqual(s.getvalue(), self.typ(expected))
351
352 def test_verify_generated_headers(self):
353 # gh-121650: by default the generator prevents header injection
354 class LiteralHeader(str):
355 name = 'Header'
356 def fold(self, **kwargs):
357 return self
358
359 for text in (
360 'Value\r\nBad Injection\r\n',
361 'NoNewLine'
362 ):
363 with self.subTest(text=text):
364 message = message_from_string(
365 "Header: Value\r\n\r\nBody",
366 policy=self.policy,
367 )
368
369 del message['Header']
370 message['Header'] = LiteralHeader(text)
371
372 with self.assertRaises(email.errors.HeaderWriteError):
373 message.as_string()
374 with self.assertRaises(email.errors.HeaderWriteError):
375 message.as_bytes()
376
377
378 class TestBytesGenerator(TestGeneratorBase, TestEmailBase):
379
380 msgfunc = staticmethod(message_from_bytes)
381 genclass = BytesGenerator
382 ioclass = io.BytesIO
383 typ = lambda self, x: x.encode('ascii')
384
385 def test_defaults_handle_spaces_between_encoded_words_when_folded(self):
386 source = ("Уведомление о принятии в работу обращения для"
387 " подключения услуги")
388 expected = ('Subject: =?utf-8?b?0KPQstC10LTQvtC80LvQtdC90LjQtSDQviDQv9GA0LjQvdGP0YLQuNC4?=\n'
389 ' =?utf-8?b?INCyINGA0LDQsdC+0YLRgyDQvtCx0YDQsNGJ0LXQvdC40Y8g0LTQu9GPINC/0L4=?=\n'
390 ' =?utf-8?b?0LTQutC70Y7Rh9C10L3QuNGPINGD0YHQu9GD0LPQuA==?=\n\n').encode('ascii')
391 msg = EmailMessage()
392 msg['Subject'] = source
393 s = io.BytesIO()
394 g = BytesGenerator(s)
395 g.flatten(msg)
396 self.assertEqual(s.getvalue(), expected)
397
398 def test_defaults_handle_spaces_when_encoded_words_is_folded_in_middle(self):
399 source = ('A very long long long long long long long long long long long long '
400 'long long long long long long long long long long long súmmäry')
401 expected = ('Subject: A very long long long long long long long long long long long long\n'
402 ' long long long long long long long long long long long =?utf-8?q?s=C3=BAmm?=\n'
403 ' =?utf-8?q?=C3=A4ry?=\n\n').encode('ascii')
404 msg = EmailMessage()
405 msg['Subject'] = source
406 s = io.BytesIO()
407 g = BytesGenerator(s)
408 g.flatten(msg)
409 self.assertEqual(s.getvalue(), expected)
410
411 def test_defaults_handle_spaces_at_start_of_subject(self):
412 source = " Уведомление"
413 expected = b"Subject: =?utf-8?b?0KPQstC10LTQvtC80LvQtdC90LjQtQ==?=\n\n"
414 msg = EmailMessage()
415 msg['Subject'] = source
416 s = io.BytesIO()
417 g = BytesGenerator(s)
418 g.flatten(msg)
419 self.assertEqual(s.getvalue(), expected)
420
421 def test_defaults_handle_spaces_at_start_of_continuation_line(self):
422 source = " ф ффффффффффффффффффф ф ф"
423 expected = (b"Subject: "
424 b"=?utf-8?b?0YQg0YTRhNGE0YTRhNGE0YTRhNGE0YTRhNGE0YTRhNGE0YTRhNGE0YQ=?=\n"
425 b" =?utf-8?b?INGEINGE?=\n\n")
426 msg = EmailMessage()
427 msg['Subject'] = source
428 s = io.BytesIO()
429 g = BytesGenerator(s)
430 g.flatten(msg)
431 self.assertEqual(s.getvalue(), expected)
432
433 # gh-144156: fold between non-encoded and encoded words don't need to encoded
434 # the separating space
435 def test_defaults_handle_spaces_at_start_of_continuation_line_2(self):
436 source = ("Re: [SOS-1495488] Commande et livraison - Demande de retour - "
437 "bibijolie - 251210-AABBCC - Abo actualités digitales 20 semaines "
438 "d’abonnement à 24 heures, Bilan, Tribune de Genève et tous les titres Tamedia")
439 expected = (
440 b"Subject: "
441 b"Re: [SOS-1495488] Commande et livraison - Demande de retour -\n"
442 b" bibijolie - 251210-AABBCC - Abo =?utf-8?q?actualit=C3=A9s?= digitales 20\n"
443 b" semaines =?utf-8?q?d=E2=80=99abonnement_=C3=A0?= 24 heures, Bilan, Tribune de\n"
444 b" =?utf-8?q?Gen=C3=A8ve?= et tous les titres Tamedia\n\n"
445 )
446 msg = EmailMessage()
447 msg['Subject'] = source
448 s = io.BytesIO()
449 g = BytesGenerator(s)
450 g.flatten(msg)
451 self.assertEqual(s.getvalue(), expected)
452
453 def test_ew_folding_round_trip_1(self):
454 print()
455 source = "aaaaaaaaa фффффффф "
456 msg = EmailMessage()
457 msg['Subject'] = source
458 s = io.BytesIO()
459 g = BytesGenerator(s, maxheaderlen=30)
460 g.flatten(msg)
461 flat = s.getvalue()
462 reparsed = message_from_bytes(flat, policy=policy.default)['Subject']
463 self.assertMultiLineEqual(reparsed, source)
464
465 def test_ew_folding_round_trip_2(self):
466 print()
467 source = "aaa aaaaaaa aaa ффф фффф "
468 msg = EmailMessage()
469 msg['Subject'] = source
470 s = io.BytesIO()
471 g = BytesGenerator(s, maxheaderlen=30)
472 g.flatten(msg)
473 flat = s.getvalue()
474 reparsed = message_from_bytes(flat, policy=policy.default)['Subject']
475 self.assertMultiLineEqual(reparsed, source)
476
477 def test_cte_type_7bit_handles_unknown_8bit(self):
478 source = ("Subject: Maintenant je vous présente mon "
479 "collègue\n\n").encode('utf-8')
480 expected = ('Subject: Maintenant je vous =?unknown-8bit?q?'
481 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii')
482 msg = message_from_bytes(source)
483 s = io.BytesIO()
484 g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit'))
485 g.flatten(msg)
486 self.assertEqual(s.getvalue(), expected)
487
488 def test_cte_type_7bit_transforms_8bit_cte(self):
489 source = textwrap.dedent("""\
490 From: [email protected]
491 To: Dinsdale
492 Subject: Nudge nudge, wink, wink
493 Mime-Version: 1.0
494 Content-Type: text/plain; charset="latin-1"
495 Content-Transfer-Encoding: 8bit
496
497 oh là là, know what I mean, know what I mean?
498 """).encode('latin1')
499 msg = message_from_bytes(source)
500 expected = textwrap.dedent("""\
501 From: [email protected]
502 To: Dinsdale
503 Subject: Nudge nudge, wink, wink
504 Mime-Version: 1.0
505 Content-Type: text/plain; charset="iso-8859-1"
506 Content-Transfer-Encoding: quoted-printable
507
508 oh l=E0 l=E0, know what I mean, know what I mean?
509 """).encode('ascii')
510 s = io.BytesIO()
511 g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit',
512 linesep='\n'))
513 g.flatten(msg)
514 self.assertEqual(s.getvalue(), expected)
515
516 def test_smtputf8_policy(self):
517 msg = EmailMessage()
518 msg['From'] = "Páolo <fő[email protected]>"
519 msg['To'] = 'Dinsdale'
520 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
521 msg.set_content("oh là là, know what I mean, know what I mean?")
522 expected = textwrap.dedent("""\
523 From: Páolo <fő[email protected]>
524 To: Dinsdale
525 Subject: Nudge nudge, wink, wink \u1F609
526 Content-Type: text/plain; charset="utf-8"
527 Content-Transfer-Encoding: 8bit
528 MIME-Version: 1.0
529
530 oh là là, know what I mean, know what I mean?
531 """).encode('utf-8').replace(b'\n', b'\r\n')
532 s = io.BytesIO()
533 g = BytesGenerator(s, policy=policy.SMTPUTF8)
534 g.flatten(msg)
535 self.assertEqual(s.getvalue(), expected)
536
537 def test_smtp_policy(self):
538 msg = EmailMessage()
539 msg["From"] = Address(addr_spec="[email protected]", display_name="Páolo")
540 msg["To"] = Address(addr_spec="[email protected]", display_name="Dinsdale")
541 msg["Subject"] = "Nudge nudge, wink, wink"
542 msg.set_content("oh boy, know what I mean, know what I mean?")
543 expected = textwrap.dedent("""\
544 From: =?utf-8?q?P=C3=A1olo?= <[email protected]>
545 To: Dinsdale <[email protected]>
546 Subject: Nudge nudge, wink, wink
547 Content-Type: text/plain; charset="utf-8"
548 Content-Transfer-Encoding: 7bit
549 MIME-Version: 1.0
550
551 oh boy, know what I mean, know what I mean?
552 """).encode().replace(b"\n", b"\r\n")
553 s = io.BytesIO()
554 g = BytesGenerator(s, policy=policy.SMTP)
555 g.flatten(msg)
556 self.assertEqual(s.getvalue(), expected)
557
558
559 if __name__ == '__main__':
560 unittest.main()