2013-02-04 16:22:53 -04:00
|
|
|
"""Test the parser and generator are inverses.
|
|
|
|
|
|
|
|
Note that this is only strictly true if we are parsing RFC valid messages and
|
|
|
|
producing RFC valid messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import io
|
|
|
|
import unittest
|
|
|
|
from email import policy, message_from_bytes
|
2016-09-09 16:00:09 -03:00
|
|
|
from email.message import EmailMessage
|
2013-02-04 16:22:53 -04:00
|
|
|
from email.generator import BytesGenerator
|
|
|
|
from test.test_email import TestEmailBase, parameterize
|
|
|
|
|
|
|
|
# This is like textwrap.dedent for bytes, except that it uses \r\n for the line
|
|
|
|
# separators on the rebuilt string.
|
|
|
|
def dedent(bstr):
|
|
|
|
lines = bstr.splitlines()
|
|
|
|
if not lines[0].strip():
|
|
|
|
raise ValueError("First line must contain text")
|
|
|
|
stripamt = len(lines[0]) - len(lines[0].lstrip())
|
|
|
|
return b'\r\n'.join(
|
|
|
|
[x[stripamt:] if len(x)>=stripamt else b''
|
|
|
|
for x in lines])
|
|
|
|
|
|
|
|
|
|
|
|
@parameterize
|
2016-09-09 16:00:09 -03:00
|
|
|
class TestInversion(TestEmailBase):
|
|
|
|
|
|
|
|
policy = policy.default
|
|
|
|
message = EmailMessage
|
2013-02-04 16:22:53 -04:00
|
|
|
|
|
|
|
def msg_as_input(self, msg):
|
|
|
|
m = message_from_bytes(msg, policy=policy.SMTP)
|
|
|
|
b = io.BytesIO()
|
|
|
|
g = BytesGenerator(b)
|
|
|
|
g.flatten(m)
|
|
|
|
self.assertEqual(b.getvalue(), msg)
|
|
|
|
|
|
|
|
# XXX: spaces are not preserved correctly here yet in the general case.
|
|
|
|
msg_params = {
|
|
|
|
'header_with_one_space_body': (dedent(b"""\
|
|
|
|
From: abc@xyz.com
|
|
|
|
X-Status:\x20
|
|
|
|
Subject: test
|
|
|
|
|
|
|
|
foo
|
|
|
|
"""),),
|
|
|
|
|
bpo-30681: Support invalid date format or value in email Date header (GH-22090)
I am re-submitting an older PR which was abandoned but is still relevant, #10783 by @timb07.
The issue being solved () is still relevant. The original PR #10783 was closed as
the final request changes were not applied and since abandoned.
In this new PR I have re-used the original patch plus applied both comments from the review, by @maxking and @pganssle.
For reference, here is the original PR description:
In email.utils.parsedate_to_datetime(), a failure to parse the date, or invalid date components (such as hour outside 0..23) raises an exception. Document this behaviour, and add tests to test_email/test_utils.py to confirm this behaviour.
In email.headerregistry.DateHeader.parse(), check when parsedate_to_datetime() raises an exception and add a new defect InvalidDateDefect; preserve the invalid value as the string value of the header, but set the datetime attribute to None.
Add tests to test_email/test_headerregistry.py to confirm this behaviour; also added test to test_email/test_inversion.py to confirm emails with such defective date headers round trip successfully.
This pull request incorporates feedback gratefully received from @bitdancer, @brettcannon, @Mariatta and @warsaw, and replaces the earlier PR #2254.
Automerge-Triggered-By: GH:warsaw
2020-10-26 21:31:06 -03:00
|
|
|
'header_with_invalid_date': (dedent(b"""\
|
|
|
|
Date: Tue, 06 Jun 2017 27:39:33 +0600
|
|
|
|
From: abc@xyz.com
|
|
|
|
Subject: timezones
|
|
|
|
|
|
|
|
How do they work even?
|
|
|
|
"""),),
|
|
|
|
|
2013-02-04 16:22:53 -04:00
|
|
|
}
|
2013-07-16 12:45:31 -03:00
|
|
|
|
2016-09-09 16:00:09 -03:00
|
|
|
payload_params = {
|
|
|
|
'plain_text': dict(payload='This is a test\n'*20),
|
|
|
|
'base64_text': dict(payload=(('xy a'*40+'\n')*5), cte='base64'),
|
|
|
|
'qp_text': dict(payload=(('xy a'*40+'\n')*5), cte='quoted-printable'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def payload_as_body(self, payload, **kw):
|
|
|
|
msg = self._make_message()
|
|
|
|
msg['From'] = 'foo'
|
|
|
|
msg['To'] = 'bar'
|
|
|
|
msg['Subject'] = 'payload round trip test'
|
|
|
|
msg.set_content(payload, **kw)
|
|
|
|
b = bytes(msg)
|
|
|
|
msg2 = message_from_bytes(b, policy=self.policy)
|
|
|
|
self.assertEqual(bytes(msg2), b)
|
|
|
|
self.assertEqual(msg2.get_content(), payload)
|
|
|
|
|
2013-07-16 12:45:31 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|