|
1 | 1 | import io |
2 | 2 | import textwrap |
3 | 3 | import unittest |
| 4 | +import random |
| 5 | +import sys |
4 | 6 | from email import message_from_string, message_from_bytes |
5 | 7 | from email.message import EmailMessage |
| 8 | +from email.mime.multipart import MIMEMultipart |
| 9 | +from email.mime.text import MIMEText |
6 | 10 | from email.generator import Generator, BytesGenerator |
| 11 | +import email.generator |
7 | 12 | from email.headerregistry import Address |
8 | 13 | from email import policy |
9 | 14 | import email.errors |
10 | 15 | from test.test_email import TestEmailBase, parameterize |
| 16 | +import test.support |
| 17 | + |
11 | 18 |
|
12 | 19 |
|
13 | 20 | @parameterize |
@@ -288,6 +295,36 @@ def test_keep_long_encoded_newlines(self): |
288 | 295 | g.flatten(msg) |
289 | 296 | self.assertEqual(s.getvalue(), self.typ(expected)) |
290 | 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 | + |
291 | 328 |
|
292 | 329 | class TestGenerator(TestGeneratorBase, TestEmailBase): |
293 | 330 |
|
|
0 commit comments