summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_email
diff options
context:
space:
mode:
authorAbhilash Raj <maxking@users.noreply.github.com>2019-06-04 17:41:34 (GMT)
committerBarry Warsaw <barry@python.org>2019-06-04 17:41:34 (GMT)
commit46d88a113142b26c01c95c93846a89318ba87ffc (patch)
tree756e002f21f3fa01483b309ddf85b0ed37e2e19d /Lib/test/test_email
parentbc6469f79ca13217b784fb47da7ec83484a3debe (diff)
downloadcpython-46d88a113142b26c01c95c93846a89318ba87ffc.zip
cpython-46d88a113142b26c01c95c93846a89318ba87ffc.tar.gz
cpython-46d88a113142b26c01c95c93846a89318ba87ffc.tar.bz2
bpo-35805: Add parser for Message-ID email header. (GH-13397)
* bpo-35805: Add parser for Message-ID header. This parser is based on the definition of Identification Fields from RFC 5322 Sec 3.6.4. This should also prevent folding of Message-ID header using RFC 2047 encoded words and hence fix bpo-35805. * Prevent folding of non-ascii message-id headers. * Add fold method to MsgID token to prevent folding.
Diffstat (limited to 'Lib/test/test_email')
-rw-r--r--Lib/test/test_email/test__header_value_parser.py72
-rw-r--r--Lib/test/test_email/test_headerregistry.py28
2 files changed, 100 insertions, 0 deletions
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
index 676732b..12da3cf 100644
--- a/Lib/test/test_email/test__header_value_parser.py
+++ b/Lib/test/test_email/test__header_value_parser.py
@@ -2494,6 +2494,78 @@ class TestParser(TestParserMixin, TestEmailBase):
";foo", ";foo", ";foo", [errors.InvalidHeaderDefect]*3
)
+ # get_msg_id
+
+ def test_get_msg_id_valid(self):
+ msg_id = self._test_get_x(
+ parser.get_msg_id,
+ "<simeple.local@example.something.com>",
+ "<simeple.local@example.something.com>",
+ "<simeple.local@example.something.com>",
+ [],
+ '',
+ )
+ self.assertEqual(msg_id.token_type, 'msg-id')
+
+ def test_get_msg_id_obsolete_local(self):
+ msg_id = self._test_get_x(
+ parser.get_msg_id,
+ '<"simeple.local"@example.com>',
+ '<"simeple.local"@example.com>',
+ '<simeple.local@example.com>',
+ [errors.ObsoleteHeaderDefect],
+ '',
+ )
+ self.assertEqual(msg_id.token_type, 'msg-id')
+
+ def test_get_msg_id_non_folding_literal_domain(self):
+ msg_id = self._test_get_x(
+ parser.get_msg_id,
+ "<simple.local@[someexamplecom.domain]>",
+ "<simple.local@[someexamplecom.domain]>",
+ "<simple.local@[someexamplecom.domain]>",
+ [],
+ "",
+ )
+ self.assertEqual(msg_id.token_type, 'msg-id')
+
+
+ def test_get_msg_id_obsolete_domain_part(self):
+ msg_id = self._test_get_x(
+ parser.get_msg_id,
+ "<simplelocal@(old)example.com>",
+ "<simplelocal@(old)example.com>",
+ "<simplelocal@ example.com>",
+ [errors.ObsoleteHeaderDefect],
+ ""
+ )
+
+ def test_get_msg_id_no_id_right_part(self):
+ msg_id = self._test_get_x(
+ parser.get_msg_id,
+ "<simplelocal>",
+ "<simplelocal>",
+ "<simplelocal>",
+ [errors.InvalidHeaderDefect],
+ ""
+ )
+ self.assertEqual(msg_id.token_type, 'msg-id')
+
+ def test_get_msg_id_no_angle_start(self):
+ with self.assertRaises(errors.HeaderParseError):
+ parser.get_msg_id("msgwithnoankle")
+
+ def test_get_msg_id_no_angle_end(self):
+ msg_id = self._test_get_x(
+ parser.get_msg_id,
+ "<simplelocal@domain",
+ "<simplelocal@domain>",
+ "<simplelocal@domain>",
+ [errors.InvalidHeaderDefect],
+ ""
+ )
+ self.assertEqual(msg_id.token_type, 'msg-id')
+
@parameterize
class Test_parse_mime_parameters(TestParserMixin, TestEmailBase):
diff --git a/Lib/test/test_email/test_headerregistry.py b/Lib/test/test_email/test_headerregistry.py
index d100709..7550546 100644
--- a/Lib/test/test_email/test_headerregistry.py
+++ b/Lib/test/test_email/test_headerregistry.py
@@ -1648,6 +1648,34 @@ class TestFolding(TestHeaderBase):
'xxxxxxxxxxxxxxxxxxxx=3D=3D-xxx-xx-xx?=\n'
' =?utf-8?q?=3E?=\n')
+ def test_message_id_header_is_not_folded(self):
+ h = self.make_header(
+ 'Message-ID',
+ '<somemessageidlongerthan@maxlinelength.com>')
+ self.assertEqual(
+ h.fold(policy=policy.default.clone(max_line_length=20)),
+ 'Message-ID: <somemessageidlongerthan@maxlinelength.com>\n')
+
+ # Test message-id isn't folded when id-right is no-fold-literal.
+ h = self.make_header(
+ 'Message-ID',
+ '<somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>')
+ self.assertEqual(
+ h.fold(policy=policy.default.clone(max_line_length=20)),
+ 'Message-ID: <somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>\n')
+
+ # Test message-id isn't folded when id-right is non-ascii characters.
+ h = self.make_header('Message-ID', '<ईमेल@wők.com>')
+ self.assertEqual(
+ h.fold(policy=policy.default.clone(max_line_length=30)),
+ 'Message-ID: <ईमेल@wők.com>\n')
+
+ # Test message-id is folded without breaking the msg-id token into
+ # encoded words, *even* if they don't fit into max_line_length.
+ h = self.make_header('Message-ID', '<ईमेलfromMessage@wők.com>')
+ self.assertEqual(
+ h.fold(policy=policy.default.clone(max_line_length=20)),
+ 'Message-ID:\n <ईमेलfromMessage@wők.com>\n')
if __name__ == '__main__':
unittest.main()