| 1 |
# Copyright (C) 2001 Python Software Foundation |
| 2 |
# Contact: [email protected] |
| 3 |
# email package unit tests |
| 4 |
|
| 5 |
import re |
| 6 |
import time |
| 7 |
import base64 |
| 8 |
import unittest |
| 9 |
import textwrap |
| 10 |
|
| 11 |
from io import StringIO, BytesIO |
| 12 |
from itertools import chain |
| 13 |
from random import choice |
| 14 |
from threading import Thread |
| 15 |
from unittest.mock import patch |
| 16 |
|
| 17 |
import email |
| 18 |
import email.policy |
| 19 |
import email.utils |
| 20 |
|
| 21 |
from email.charset import Charset |
| 22 |
from email.generator import Generator, DecodedGenerator, BytesGenerator |
| 23 |
from email.header import Header, decode_header, make_header |
| 24 |
from email.headerregistry import HeaderRegistry |
| 25 |
from email.message import Message |
| 26 |
from email.mime.application import MIMEApplication |
| 27 |
from email.mime.audio import MIMEAudio |
| 28 |
from email.mime.base import MIMEBase |
| 29 |
from email.mime.image import MIMEImage |
| 30 |
from email.mime.message import MIMEMessage |
| 31 |
from email.mime.multipart import MIMEMultipart |
| 32 |
from email.mime.nonmultipart import MIMENonMultipart |
| 33 |
from email.mime.text import MIMEText |
| 34 |
from email.parser import Parser, HeaderParser |
| 35 |
from email import base64mime |
| 36 |
from email import encoders |
| 37 |
from email import errors |
| 38 |
from email import iterators |
| 39 |
from email import quoprimime |
| 40 |
from email import utils |
| 41 |
|
| 42 |
from test import support |
| 43 |
from test.support import threading_helper |
| 44 |
from test.support.os_helper import unlink |
| 45 |
from test.test_email import openfile, TestEmailBase |
| 46 |
|
| 47 |
# These imports are documented to work, but we are testing them using a |
| 48 |
# different path, so we import them here just to make sure they are importable. |
| 49 |
from email.parser import FeedParser |
| 50 |
|
| 51 |
NL = '\n' |
| 52 |
EMPTYSTRING = '' |
| 53 |
SPACE = ' ' |
| 54 |
|
| 55 |
|
| 56 |
# Test various aspects of the Message class's API |
| 57 |
class TestMessageAPI(TestEmailBase): |
| 58 |
def test_get_all(self): |
| 59 |
eq = self.assertEqual |
| 60 |
msg = self._msgobj('msg_20.txt') |
| 61 |
eq(msg.get_all('cc'), ['[email protected]', '[email protected]', '[email protected]']) |
| 62 |
eq(msg.get_all('xx', 'n/a'), 'n/a') |
| 63 |
|
| 64 |
def test_getset_charset(self): |
| 65 |
eq = self.assertEqual |
| 66 |
msg = Message() |
| 67 |
eq(msg.get_charset(), None) |
| 68 |
charset = Charset('iso-8859-1') |
| 69 |
msg.set_charset(charset) |
| 70 |
eq(msg['mime-version'], '1.0') |
| 71 |
eq(msg.get_content_type(), 'text/plain') |
| 72 |
eq(msg['content-type'], 'text/plain; charset="iso-8859-1"') |
| 73 |
eq(msg.get_param('charset'), 'iso-8859-1') |
| 74 |
eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 75 |
eq(msg.get_charset().input_charset, 'iso-8859-1') |
| 76 |
# Remove the charset |
| 77 |
msg.set_charset(None) |
| 78 |
eq(msg.get_charset(), None) |
| 79 |
eq(msg['content-type'], 'text/plain') |
| 80 |
# Try adding a charset when there's already MIME headers present |
| 81 |
msg = Message() |
| 82 |
msg['MIME-Version'] = '2.0' |
| 83 |
msg['Content-Type'] = 'text/x-weird' |
| 84 |
msg['Content-Transfer-Encoding'] = 'quinted-puntable' |
| 85 |
msg.set_charset(charset) |
| 86 |
eq(msg['mime-version'], '2.0') |
| 87 |
eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"') |
| 88 |
eq(msg['content-transfer-encoding'], 'quinted-puntable') |
| 89 |
|
| 90 |
def test_set_charset_from_string(self): |
| 91 |
eq = self.assertEqual |
| 92 |
msg = Message() |
| 93 |
msg.set_charset('us-ascii') |
| 94 |
eq(msg.get_charset().input_charset, 'us-ascii') |
| 95 |
eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 96 |
|
| 97 |
def test_set_payload_with_charset(self): |
| 98 |
msg = Message() |
| 99 |
charset = Charset('iso-8859-1') |
| 100 |
msg.set_payload('This is a string payload', charset) |
| 101 |
self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1') |
| 102 |
|
| 103 |
def test_set_payload_with_8bit_data_and_charset(self): |
| 104 |
data = b'\xd0\x90\xd0\x91\xd0\x92' |
| 105 |
charset = Charset('utf-8') |
| 106 |
msg = Message() |
| 107 |
msg.set_payload(data, charset) |
| 108 |
self.assertEqual(msg['content-transfer-encoding'], 'base64') |
| 109 |
self.assertEqual(msg.get_payload(decode=True), data) |
| 110 |
self.assertEqual(msg.get_payload(), '0JDQkdCS\n') |
| 111 |
|
| 112 |
def test_set_payload_with_non_ascii_and_charset_body_encoding_none(self): |
| 113 |
data = b'\xd0\x90\xd0\x91\xd0\x92' |
| 114 |
charset = Charset('utf-8') |
| 115 |
charset.body_encoding = None # Disable base64 encoding |
| 116 |
msg = Message() |
| 117 |
msg.set_payload(data.decode('utf-8'), charset) |
| 118 |
self.assertEqual(msg['content-transfer-encoding'], '8bit') |
| 119 |
self.assertEqual(msg.get_payload(decode=True), data) |
| 120 |
|
| 121 |
def test_set_payload_with_8bit_data_and_charset_body_encoding_none(self): |
| 122 |
data = b'\xd0\x90\xd0\x91\xd0\x92' |
| 123 |
charset = Charset('utf-8') |
| 124 |
charset.body_encoding = None # Disable base64 encoding |
| 125 |
msg = Message() |
| 126 |
msg.set_payload(data, charset) |
| 127 |
self.assertEqual(msg['content-transfer-encoding'], '8bit') |
| 128 |
self.assertEqual(msg.get_payload(decode=True), data) |
| 129 |
|
| 130 |
def test_set_payload_to_list(self): |
| 131 |
msg = Message() |
| 132 |
msg.set_payload([]) |
| 133 |
self.assertEqual(msg.get_payload(), []) |
| 134 |
|
| 135 |
def test_attach_when_payload_is_string(self): |
| 136 |
msg = Message() |
| 137 |
msg['Content-Type'] = 'multipart/mixed' |
| 138 |
msg.set_payload('string payload') |
| 139 |
sub_msg = MIMEMessage(Message()) |
| 140 |
self.assertRaisesRegex(TypeError, "[Aa]ttach.*non-multipart", |
| 141 |
msg.attach, sub_msg) |
| 142 |
|
| 143 |
def test_get_charsets(self): |
| 144 |
eq = self.assertEqual |
| 145 |
|
| 146 |
msg = self._msgobj('msg_08.txt') |
| 147 |
charsets = msg.get_charsets() |
| 148 |
eq(charsets, [None, 'us-ascii', 'iso-8859-1', 'iso-8859-2', 'koi8-r']) |
| 149 |
|
| 150 |
msg = self._msgobj('msg_09.txt') |
| 151 |
charsets = msg.get_charsets('dingbat') |
| 152 |
eq(charsets, ['dingbat', 'us-ascii', 'iso-8859-1', 'dingbat', |
| 153 |
'koi8-r']) |
| 154 |
|
| 155 |
msg = self._msgobj('msg_12.txt') |
| 156 |
charsets = msg.get_charsets() |
| 157 |
eq(charsets, [None, 'us-ascii', 'iso-8859-1', None, 'iso-8859-2', |
| 158 |
'iso-8859-3', 'us-ascii', 'koi8-r']) |
| 159 |
|
| 160 |
def test_get_filename(self): |
| 161 |
eq = self.assertEqual |
| 162 |
|
| 163 |
msg = self._msgobj('msg_04.txt') |
| 164 |
filenames = [p.get_filename() for p in msg.get_payload()] |
| 165 |
eq(filenames, ['msg.txt', 'msg.txt']) |
| 166 |
|
| 167 |
msg = self._msgobj('msg_07.txt') |
| 168 |
subpart = msg.get_payload(1) |
| 169 |
eq(subpart.get_filename(), 'dingusfish.gif') |
| 170 |
|
| 171 |
def test_get_filename_with_name_parameter(self): |
| 172 |
eq = self.assertEqual |
| 173 |
|
| 174 |
msg = self._msgobj('msg_44.txt') |
| 175 |
filenames = [p.get_filename() for p in msg.get_payload()] |
| 176 |
eq(filenames, ['msg.txt', 'msg.txt']) |
| 177 |
|
| 178 |
def test_get_boundary(self): |
| 179 |
eq = self.assertEqual |
| 180 |
msg = self._msgobj('msg_07.txt') |
| 181 |
# No quotes! |
| 182 |
eq(msg.get_boundary(), 'BOUNDARY') |
| 183 |
|
| 184 |
def test_set_boundary(self): |
| 185 |
eq = self.assertEqual |
| 186 |
# This one has no existing boundary parameter, but the Content-Type: |
| 187 |
# header appears fifth. |
| 188 |
msg = self._msgobj('msg_01.txt') |
| 189 |
msg.set_boundary('BOUNDARY') |
| 190 |
header, value = msg.items()[4] |
| 191 |
eq(header.lower(), 'content-type') |
| 192 |
eq(value, 'text/plain; charset="us-ascii"; boundary="BOUNDARY"') |
| 193 |
# This one has a Content-Type: header, with a boundary, stuck in the |
| 194 |
# middle of its headers. Make sure the order is preserved; it should |
| 195 |
# be fifth. |
| 196 |
msg = self._msgobj('msg_04.txt') |
| 197 |
msg.set_boundary('BOUNDARY') |
| 198 |
header, value = msg.items()[4] |
| 199 |
eq(header.lower(), 'content-type') |
| 200 |
eq(value, 'multipart/mixed; boundary="BOUNDARY"') |
| 201 |
# And this one has no Content-Type: header at all. |
| 202 |
msg = self._msgobj('msg_03.txt') |
| 203 |
self.assertRaises(errors.HeaderParseError, |
| 204 |
msg.set_boundary, 'BOUNDARY') |
| 205 |
|
| 206 |
def test_make_boundary(self): |
| 207 |
msg = MIMEMultipart('form-data') |
| 208 |
# Note that when the boundary gets created is an implementation |
| 209 |
# detail and might change. |
| 210 |
self.assertEqual(msg.items()[0][1], 'multipart/form-data') |
| 211 |
# Trigger creation of boundary |
| 212 |
msg.as_string() |
| 213 |
self.assertStartsWith(msg.items()[0][1], |
| 214 |
'multipart/form-data; boundary="==') |
| 215 |
# XXX: there ought to be tests of the uniqueness of the boundary, too. |
| 216 |
|
| 217 |
def test_message_rfc822_only(self): |
| 218 |
# Issue 7970: message/rfc822 not in multipart parsed by |
| 219 |
# HeaderParser caused an exception when flattened. |
| 220 |
with openfile('msg_46.txt', encoding="utf-8") as fp: |
| 221 |
msgdata = fp.read() |
| 222 |
parser = HeaderParser() |
| 223 |
msg = parser.parsestr(msgdata) |
| 224 |
out = StringIO() |
| 225 |
gen = Generator(out, True, 0) |
| 226 |
gen.flatten(msg, False) |
| 227 |
self.assertEqual(out.getvalue(), msgdata) |
| 228 |
|
| 229 |
def test_byte_message_rfc822_only(self): |
| 230 |
# Make sure new bytes header parser also passes this. |
| 231 |
with openfile('msg_46.txt', encoding="utf-8") as fp: |
| 232 |
msgdata = fp.read().encode('ascii') |
| 233 |
parser = email.parser.BytesHeaderParser() |
| 234 |
msg = parser.parsebytes(msgdata) |
| 235 |
out = BytesIO() |
| 236 |
gen = email.generator.BytesGenerator(out) |
| 237 |
gen.flatten(msg) |
| 238 |
self.assertEqual(out.getvalue(), msgdata) |
| 239 |
|
| 240 |
def test_get_decoded_payload(self): |
| 241 |
eq = self.assertEqual |
| 242 |
msg = self._msgobj('msg_10.txt') |
| 243 |
# The outer message is a multipart |
| 244 |
eq(msg.get_payload(decode=True), None) |
| 245 |
# Subpart 1 is 7bit encoded |
| 246 |
eq(msg.get_payload(0).get_payload(decode=True), |
| 247 |
b'This is a 7bit encoded message.\n') |
| 248 |
# Subpart 2 is quopri |
| 249 |
eq(msg.get_payload(1).get_payload(decode=True), |
| 250 |
b'\xa1This is a Quoted Printable encoded message!\n') |
| 251 |
# Subpart 3 is base64 |
| 252 |
eq(msg.get_payload(2).get_payload(decode=True), |
| 253 |
b'This is a Base64 encoded message.') |
| 254 |
# Subpart 4 is base64 with a trailing newline, which |
| 255 |
# used to be stripped (issue 7143). |
| 256 |
eq(msg.get_payload(3).get_payload(decode=True), |
| 257 |
b'This is a Base64 encoded message.\n') |
| 258 |
# Subpart 5 has no Content-Transfer-Encoding: header. |
| 259 |
eq(msg.get_payload(4).get_payload(decode=True), |
| 260 |
b'This has no Content-Transfer-Encoding: header.\n') |
| 261 |
|
| 262 |
def test_get_decoded_uu_payload(self): |
| 263 |
eq = self.assertEqual |
| 264 |
msg = Message() |
| 265 |
msg.set_payload('begin 666 -\n+:&5L;&\\@=V]R;&0 \n \nend\n') |
| 266 |
for cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): |
| 267 |
msg['content-transfer-encoding'] = cte |
| 268 |
eq(msg.get_payload(decode=True), b'hello world') |
| 269 |
# Now try some bogus data |
| 270 |
msg.set_payload('foo') |
| 271 |
eq(msg.get_payload(decode=True), b'foo') |
| 272 |
|
| 273 |
def test_get_payload_n_raises_on_non_multipart(self): |
| 274 |
msg = Message() |
| 275 |
self.assertRaises(TypeError, msg.get_payload, 1) |
| 276 |
|
| 277 |
def test_decoded_generator(self): |
| 278 |
eq = self.assertEqual |
| 279 |
msg = self._msgobj('msg_07.txt') |
| 280 |
with openfile('msg_17.txt', encoding="utf-8") as fp: |
| 281 |
text = fp.read() |
| 282 |
s = StringIO() |
| 283 |
g = DecodedGenerator(s) |
| 284 |
g.flatten(msg) |
| 285 |
eq(s.getvalue(), text) |
| 286 |
|
| 287 |
def test__contains__(self): |
| 288 |
msg = Message() |
| 289 |
msg['From'] = 'Me' |
| 290 |
msg['to'] = 'You' |
| 291 |
# Check for case insensitivity |
| 292 |
self.assertIn('from', msg) |
| 293 |
self.assertIn('From', msg) |
| 294 |
self.assertIn('FROM', msg) |
| 295 |
self.assertIn('to', msg) |
| 296 |
self.assertIn('To', msg) |
| 297 |
self.assertIn('TO', msg) |
| 298 |
|
| 299 |
def test_as_string(self): |
| 300 |
msg = self._msgobj('msg_01.txt') |
| 301 |
with openfile('msg_01.txt', encoding="utf-8") as fp: |
| 302 |
text = fp.read() |
| 303 |
self.assertEqual(text, str(msg)) |
| 304 |
fullrepr = msg.as_string(unixfrom=True) |
| 305 |
lines = fullrepr.split('\n') |
| 306 |
self.assertStartsWith(lines[0], 'From ') |
| 307 |
self.assertEqual(text, NL.join(lines[1:])) |
| 308 |
|
| 309 |
def test_as_string_policy(self): |
| 310 |
msg = self._msgobj('msg_01.txt') |
| 311 |
newpolicy = msg.policy.clone(linesep='\r\n') |
| 312 |
fullrepr = msg.as_string(policy=newpolicy) |
| 313 |
s = StringIO() |
| 314 |
g = Generator(s, policy=newpolicy) |
| 315 |
g.flatten(msg) |
| 316 |
self.assertEqual(fullrepr, s.getvalue()) |
| 317 |
|
| 318 |
def test_nonascii_as_string_without_cte(self): |
| 319 |
m = textwrap.dedent("""\ |
| 320 |
MIME-Version: 1.0 |
| 321 |
Content-type: text/plain; charset="iso-8859-1" |
| 322 |
|
| 323 |
Test if non-ascii messages with no Content-Transfer-Encoding set |
| 324 |
can be as_string'd: |
| 325 |
Föö bär |
| 326 |
""") |
| 327 |
source = m.encode('iso-8859-1') |
| 328 |
expected = textwrap.dedent("""\ |
| 329 |
MIME-Version: 1.0 |
| 330 |
Content-type: text/plain; charset="iso-8859-1" |
| 331 |
Content-Transfer-Encoding: quoted-printable |
| 332 |
|
| 333 |
Test if non-ascii messages with no Content-Transfer-Encoding set |
| 334 |
can be as_string'd: |
| 335 |
F=F6=F6 b=E4r |
| 336 |
""") |
| 337 |
msg = email.message_from_bytes(source) |
| 338 |
self.assertEqual(msg.as_string(), expected) |
| 339 |
|
| 340 |
def test_nonascii_as_string_with_ascii_charset(self): |
| 341 |
m = textwrap.dedent("""\ |
| 342 |
MIME-Version: 1.0 |
| 343 |
Content-type: text/plain; charset="us-ascii" |
| 344 |
Content-Transfer-Encoding: 8bit |
| 345 |
|
| 346 |
Test if non-ascii messages with no Content-Transfer-Encoding set |
| 347 |
can be as_string'd: |
| 348 |
Föö bär |
| 349 |
""") |
| 350 |
source = m.encode('iso-8859-1') |
| 351 |
expected = source.decode('ascii', 'replace') |
| 352 |
msg = email.message_from_bytes(source) |
| 353 |
self.assertEqual(msg.as_string(), expected) |
| 354 |
|
| 355 |
def test_nonascii_as_string_without_content_type_and_cte(self): |
| 356 |
m = textwrap.dedent("""\ |
| 357 |
MIME-Version: 1.0 |
| 358 |
|
| 359 |
Test if non-ascii messages with no Content-Type nor |
| 360 |
Content-Transfer-Encoding set can be as_string'd: |
| 361 |
Föö bär |
| 362 |
""") |
| 363 |
source = m.encode('iso-8859-1') |
| 364 |
expected = source.decode('ascii', 'replace') |
| 365 |
msg = email.message_from_bytes(source) |
| 366 |
self.assertEqual(msg.as_string(), expected) |
| 367 |
|
| 368 |
def test_as_bytes(self): |
| 369 |
msg = self._msgobj('msg_01.txt') |
| 370 |
with openfile('msg_01.txt', encoding="utf-8") as fp: |
| 371 |
data = fp.read().encode('ascii') |
| 372 |
self.assertEqual(data, bytes(msg)) |
| 373 |
fullrepr = msg.as_bytes(unixfrom=True) |
| 374 |
lines = fullrepr.split(b'\n') |
| 375 |
self.assertStartsWith(lines[0], b'From ') |
| 376 |
self.assertEqual(data, b'\n'.join(lines[1:])) |
| 377 |
|
| 378 |
def test_as_bytes_policy(self): |
| 379 |
msg = self._msgobj('msg_01.txt') |
| 380 |
newpolicy = msg.policy.clone(linesep='\r\n') |
| 381 |
fullrepr = msg.as_bytes(policy=newpolicy) |
| 382 |
s = BytesIO() |
| 383 |
g = BytesGenerator(s,policy=newpolicy) |
| 384 |
g.flatten(msg) |
| 385 |
self.assertEqual(fullrepr, s.getvalue()) |
| 386 |
|
| 387 |
# test_headerregistry.TestContentTypeHeader.bad_params |
| 388 |
def test_bad_param(self): |
| 389 |
msg = email.message_from_string("Content-Type: blarg; baz; boo\n") |
| 390 |
self.assertEqual(msg.get_param('baz'), '') |
| 391 |
|
| 392 |
def test_continuation_sorting_part_order(self): |
| 393 |
msg = email.message_from_string( |
| 394 |
"Content-Disposition: attachment; " |
| 395 |
"filename*=\"ignored\"; " |
| 396 |
"filename*0*=\"utf-8''foo%20\"; " |
| 397 |
"filename*1*=\"bar.txt\"\n" |
| 398 |
) |
| 399 |
filename = msg.get_filename() |
| 400 |
self.assertEqual(filename, 'foo bar.txt') |
| 401 |
|
| 402 |
def test_sorting_no_continuations(self): |
| 403 |
msg = email.message_from_string( |
| 404 |
"Content-Disposition: attachment; " |
| 405 |
"filename*=\"bar.txt\"; " |
| 406 |
) |
| 407 |
filename = msg.get_filename() |
| 408 |
self.assertEqual(filename, 'bar.txt') |
| 409 |
|
| 410 |
def test_missing_filename(self): |
| 411 |
msg = email.message_from_string("From: foo\n") |
| 412 |
self.assertEqual(msg.get_filename(), None) |
| 413 |
|
| 414 |
def test_bogus_filename(self): |
| 415 |
msg = email.message_from_string( |
| 416 |
"Content-Disposition: blarg; filename\n") |
| 417 |
self.assertEqual(msg.get_filename(), '') |
| 418 |
|
| 419 |
def test_missing_boundary(self): |
| 420 |
msg = email.message_from_string("From: foo\n") |
| 421 |
self.assertEqual(msg.get_boundary(), None) |
| 422 |
|
| 423 |
def test_get_params(self): |
| 424 |
eq = self.assertEqual |
| 425 |
msg = email.message_from_string( |
| 426 |
'X-Header: foo=one; bar=two; baz=three\n') |
| 427 |
eq(msg.get_params(header='x-header'), |
| 428 |
[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]) |
| 429 |
msg = email.message_from_string( |
| 430 |
'X-Header: foo; bar=one; baz=two\n') |
| 431 |
eq(msg.get_params(header='x-header'), |
| 432 |
[('foo', ''), ('bar', 'one'), ('baz', 'two')]) |
| 433 |
eq(msg.get_params(), None) |
| 434 |
msg = email.message_from_string( |
| 435 |
'X-Header: foo; bar="one"; baz=two\n') |
| 436 |
eq(msg.get_params(header='x-header'), |
| 437 |
[('foo', ''), ('bar', 'one'), ('baz', 'two')]) |
| 438 |
|
| 439 |
# test_headerregistry.TestContentTypeHeader.spaces_around_param_equals |
| 440 |
def test_get_param_liberal(self): |
| 441 |
msg = Message() |
| 442 |
msg['Content-Type'] = 'Content-Type: Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"' |
| 443 |
self.assertEqual(msg.get_param('boundary'), 'CPIMSSMTPC06p5f3tG') |
| 444 |
|
| 445 |
def test_get_param(self): |
| 446 |
eq = self.assertEqual |
| 447 |
msg = email.message_from_string( |
| 448 |
"X-Header: foo=one; bar=two; baz=three\n") |
| 449 |
eq(msg.get_param('bar', header='x-header'), 'two') |
| 450 |
eq(msg.get_param('quuz', header='x-header'), None) |
| 451 |
eq(msg.get_param('quuz'), None) |
| 452 |
msg = email.message_from_string( |
| 453 |
'X-Header: foo; bar="one"; baz=two\n') |
| 454 |
eq(msg.get_param('foo', header='x-header'), '') |
| 455 |
eq(msg.get_param('bar', header='x-header'), 'one') |
| 456 |
eq(msg.get_param('baz', header='x-header'), 'two') |
| 457 |
# XXX: We are not RFC-2045 compliant! We cannot parse: |
| 458 |
# msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"' |
| 459 |
# msg.get_param("weird") |
| 460 |
# yet. |
| 461 |
|
| 462 |
# test_headerregistry.TestContentTypeHeader.spaces_around_semis |
| 463 |
def test_get_param_funky_continuation_lines(self): |
| 464 |
msg = self._msgobj('msg_22.txt') |
| 465 |
self.assertEqual(msg.get_payload(1).get_param('name'), 'wibble.JPG') |
| 466 |
|
| 467 |
# test_headerregistry.TestContentTypeHeader.semis_inside_quotes |
| 468 |
def test_get_param_with_semis_in_quotes(self): |
| 469 |
msg = email.message_from_string( |
| 470 |
'Content-Type: image/pjpeg; name="Jim&&Jill"\n') |
| 471 |
self.assertEqual(msg.get_param('name'), 'Jim&&Jill') |
| 472 |
self.assertEqual(msg.get_param('name', unquote=False), |
| 473 |
'"Jim&&Jill"') |
| 474 |
|
| 475 |
# test_headerregistry.TestContentTypeHeader.quotes_inside_rfc2231_value |
| 476 |
def test_get_param_with_quotes(self): |
| 477 |
msg = email.message_from_string( |
| 478 |
'Content-Type: foo; bar*0="baz\\"foobar"; bar*1="\\"baz"') |
| 479 |
self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz') |
| 480 |
msg = email.message_from_string( |
| 481 |
"Content-Type: foo; bar*0=\"baz\\\"foobar\"; bar*1=\"\\\"baz\"") |
| 482 |
self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz') |
| 483 |
|
| 484 |
def test_get_param_linear_complexity(self): |
| 485 |
# Ensure that email.message._parseparam() is fast. |
| 486 |
# See https://github.com/python/cpython/issues/136063. |
| 487 |
N = 100_000 |
| 488 |
for s, r in [ |
| 489 |
("", ""), |
| 490 |
("foo=bar", "foo=bar"), |
| 491 |
(" FOO = bar ", "foo=bar"), |
| 492 |
]: |
| 493 |
with self.subTest(s=s, r=r, N=N): |
| 494 |
src = f'{s};' * (N - 1) + s |
| 495 |
res = email.message._parseparam(src) |
| 496 |
self.assertEqual(len(res), N) |
| 497 |
self.assertEqual(len(set(res)), 1) |
| 498 |
self.assertEqual(res[0], r) |
| 499 |
|
| 500 |
# This will be considered as a single parameter. |
| 501 |
malformed = 's="' + ';' * (N - 1) |
| 502 |
res = email.message._parseparam(malformed) |
| 503 |
self.assertEqual(res, [malformed]) |
| 504 |
|
| 505 |
def test_field_containment(self): |
| 506 |
msg = email.message_from_string('Header: exists') |
| 507 |
self.assertIn('header', msg) |
| 508 |
self.assertIn('Header', msg) |
| 509 |
self.assertIn('HEADER', msg) |
| 510 |
self.assertNotIn('headerx', msg) |
| 511 |
|
| 512 |
def test_set_param(self): |
| 513 |
eq = self.assertEqual |
| 514 |
msg = Message() |
| 515 |
msg.set_param('charset', 'iso-2022-jp') |
| 516 |
eq(msg.get_param('charset'), 'iso-2022-jp') |
| 517 |
msg.set_param('importance', 'high value') |
| 518 |
eq(msg.get_param('importance'), 'high value') |
| 519 |
eq(msg.get_param('importance', unquote=False), '"high value"') |
| 520 |
eq(msg.get_params(), [('text/plain', ''), |
| 521 |
('charset', 'iso-2022-jp'), |
| 522 |
('importance', 'high value')]) |
| 523 |
eq(msg.get_params(unquote=False), [('text/plain', ''), |
| 524 |
('charset', '"iso-2022-jp"'), |
| 525 |
('importance', '"high value"')]) |
| 526 |
msg.set_param('charset', 'iso-9999-xx', header='X-Jimmy') |
| 527 |
eq(msg.get_param('charset', header='X-Jimmy'), 'iso-9999-xx') |
| 528 |
|
| 529 |
def test_del_param(self): |
| 530 |
eq = self.assertEqual |
| 531 |
msg = self._msgobj('msg_05.txt') |
| 532 |
eq(msg.get_params(), |
| 533 |
[('multipart/report', ''), ('report-type', 'delivery-status'), |
| 534 |
('boundary', 'D1690A7AC1.996856090/mail.example.com')]) |
| 535 |
old_val = msg.get_param("report-type") |
| 536 |
msg.del_param("report-type") |
| 537 |
eq(msg.get_params(), |
| 538 |
[('multipart/report', ''), |
| 539 |
('boundary', 'D1690A7AC1.996856090/mail.example.com')]) |
| 540 |
msg.set_param("report-type", old_val) |
| 541 |
eq(msg.get_params(), |
| 542 |
[('multipart/report', ''), |
| 543 |
('boundary', 'D1690A7AC1.996856090/mail.example.com'), |
| 544 |
('report-type', old_val)]) |
| 545 |
|
| 546 |
def test_del_param_on_other_header(self): |
| 547 |
msg = Message() |
| 548 |
msg.add_header('Content-Disposition', 'attachment', filename='bud.gif') |
| 549 |
msg.del_param('filename', 'content-disposition') |
| 550 |
self.assertEqual(msg['content-disposition'], 'attachment') |
| 551 |
|
| 552 |
def test_del_param_on_nonexistent_header(self): |
| 553 |
msg = Message() |
| 554 |
# Deleting param on empty msg should not raise exception. |
| 555 |
msg.del_param('filename', 'content-disposition') |
| 556 |
|
| 557 |
def test_del_nonexistent_param(self): |
| 558 |
msg = Message() |
| 559 |
msg.add_header('Content-Type', 'text/plain', charset='utf-8') |
| 560 |
existing_header = msg['Content-Type'] |
| 561 |
msg.del_param('foobar', header='Content-Type') |
| 562 |
self.assertEqual(msg['Content-Type'], existing_header) |
| 563 |
|
| 564 |
def test_set_type(self): |
| 565 |
eq = self.assertEqual |
| 566 |
msg = Message() |
| 567 |
self.assertRaises(ValueError, msg.set_type, 'text') |
| 568 |
msg.set_type('text/plain') |
| 569 |
eq(msg['content-type'], 'text/plain') |
| 570 |
msg.set_param('charset', 'us-ascii') |
| 571 |
eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 572 |
msg.set_type('text/html') |
| 573 |
eq(msg['content-type'], 'text/html; charset="us-ascii"') |
| 574 |
|
| 575 |
def test_set_type_on_other_header(self): |
| 576 |
msg = Message() |
| 577 |
msg['X-Content-Type'] = 'text/plain' |
| 578 |
msg.set_type('application/octet-stream', 'X-Content-Type') |
| 579 |
self.assertEqual(msg['x-content-type'], 'application/octet-stream') |
| 580 |
|
| 581 |
def test_get_content_type_missing(self): |
| 582 |
msg = Message() |
| 583 |
self.assertEqual(msg.get_content_type(), 'text/plain') |
| 584 |
|
| 585 |
def test_get_content_type_missing_with_default_type(self): |
| 586 |
msg = Message() |
| 587 |
msg.set_default_type('message/rfc822') |
| 588 |
self.assertEqual(msg.get_content_type(), 'message/rfc822') |
| 589 |
|
| 590 |
def test_get_content_type_from_message_implicit(self): |
| 591 |
msg = self._msgobj('msg_30.txt') |
| 592 |
self.assertEqual(msg.get_payload(0).get_content_type(), |
| 593 |
'message/rfc822') |
| 594 |
|
| 595 |
def test_get_content_type_from_message_explicit(self): |
| 596 |
msg = self._msgobj('msg_28.txt') |
| 597 |
self.assertEqual(msg.get_payload(0).get_content_type(), |
| 598 |
'message/rfc822') |
| 599 |
|
| 600 |
def test_get_content_type_from_message_text_plain_implicit(self): |
| 601 |
msg = self._msgobj('msg_03.txt') |
| 602 |
self.assertEqual(msg.get_content_type(), 'text/plain') |
| 603 |
|
| 604 |
def test_get_content_type_from_message_text_plain_explicit(self): |
| 605 |
msg = self._msgobj('msg_01.txt') |
| 606 |
self.assertEqual(msg.get_content_type(), 'text/plain') |
| 607 |
|
| 608 |
def test_get_content_maintype_missing(self): |
| 609 |
msg = Message() |
| 610 |
self.assertEqual(msg.get_content_maintype(), 'text') |
| 611 |
|
| 612 |
def test_get_content_maintype_missing_with_default_type(self): |
| 613 |
msg = Message() |
| 614 |
msg.set_default_type('message/rfc822') |
| 615 |
self.assertEqual(msg.get_content_maintype(), 'message') |
| 616 |
|
| 617 |
def test_get_content_maintype_from_message_implicit(self): |
| 618 |
msg = self._msgobj('msg_30.txt') |
| 619 |
self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message') |
| 620 |
|
| 621 |
def test_get_content_maintype_from_message_explicit(self): |
| 622 |
msg = self._msgobj('msg_28.txt') |
| 623 |
self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message') |
| 624 |
|
| 625 |
def test_get_content_maintype_from_message_text_plain_implicit(self): |
| 626 |
msg = self._msgobj('msg_03.txt') |
| 627 |
self.assertEqual(msg.get_content_maintype(), 'text') |
| 628 |
|
| 629 |
def test_get_content_maintype_from_message_text_plain_explicit(self): |
| 630 |
msg = self._msgobj('msg_01.txt') |
| 631 |
self.assertEqual(msg.get_content_maintype(), 'text') |
| 632 |
|
| 633 |
def test_get_content_subtype_missing(self): |
| 634 |
msg = Message() |
| 635 |
self.assertEqual(msg.get_content_subtype(), 'plain') |
| 636 |
|
| 637 |
def test_get_content_subtype_missing_with_default_type(self): |
| 638 |
msg = Message() |
| 639 |
msg.set_default_type('message/rfc822') |
| 640 |
self.assertEqual(msg.get_content_subtype(), 'rfc822') |
| 641 |
|
| 642 |
def test_get_content_subtype_from_message_implicit(self): |
| 643 |
msg = self._msgobj('msg_30.txt') |
| 644 |
self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822') |
| 645 |
|
| 646 |
def test_get_content_subtype_from_message_explicit(self): |
| 647 |
msg = self._msgobj('msg_28.txt') |
| 648 |
self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822') |
| 649 |
|
| 650 |
def test_get_content_subtype_from_message_text_plain_implicit(self): |
| 651 |
msg = self._msgobj('msg_03.txt') |
| 652 |
self.assertEqual(msg.get_content_subtype(), 'plain') |
| 653 |
|
| 654 |
def test_get_content_subtype_from_message_text_plain_explicit(self): |
| 655 |
msg = self._msgobj('msg_01.txt') |
| 656 |
self.assertEqual(msg.get_content_subtype(), 'plain') |
| 657 |
|
| 658 |
def test_get_content_maintype_error(self): |
| 659 |
msg = Message() |
| 660 |
msg['Content-Type'] = 'no-slash-in-this-string' |
| 661 |
self.assertEqual(msg.get_content_maintype(), 'text') |
| 662 |
|
| 663 |
def test_get_content_subtype_error(self): |
| 664 |
msg = Message() |
| 665 |
msg['Content-Type'] = 'no-slash-in-this-string' |
| 666 |
self.assertEqual(msg.get_content_subtype(), 'plain') |
| 667 |
|
| 668 |
def test_replace_header(self): |
| 669 |
eq = self.assertEqual |
| 670 |
msg = Message() |
| 671 |
msg.add_header('First', 'One') |
| 672 |
msg.add_header('Second', 'Two') |
| 673 |
msg.add_header('Third', 'Three') |
| 674 |
eq(msg.keys(), ['First', 'Second', 'Third']) |
| 675 |
eq(msg.values(), ['One', 'Two', 'Three']) |
| 676 |
msg.replace_header('Second', 'Twenty') |
| 677 |
eq(msg.keys(), ['First', 'Second', 'Third']) |
| 678 |
eq(msg.values(), ['One', 'Twenty', 'Three']) |
| 679 |
msg.add_header('First', 'Eleven') |
| 680 |
msg.replace_header('First', 'One Hundred') |
| 681 |
eq(msg.keys(), ['First', 'Second', 'Third', 'First']) |
| 682 |
eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven']) |
| 683 |
self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing') |
| 684 |
|
| 685 |
def test_get_content_disposition(self): |
| 686 |
msg = Message() |
| 687 |
self.assertIsNone(msg.get_content_disposition()) |
| 688 |
msg.add_header('Content-Disposition', 'attachment', |
| 689 |
filename='random.avi') |
| 690 |
self.assertEqual(msg.get_content_disposition(), 'attachment') |
| 691 |
msg.replace_header('Content-Disposition', 'inline') |
| 692 |
self.assertEqual(msg.get_content_disposition(), 'inline') |
| 693 |
msg.replace_header('Content-Disposition', 'InlinE') |
| 694 |
self.assertEqual(msg.get_content_disposition(), 'inline') |
| 695 |
|
| 696 |
# test_defect_handling:test_invalid_chars_in_base64_payload |
| 697 |
def test_broken_base64_payload(self): |
| 698 |
x = 'AwDp0P7//y6LwKEAcPa/6Q=9' |
| 699 |
msg = Message() |
| 700 |
msg['content-type'] = 'audio/x-midi' |
| 701 |
msg['content-transfer-encoding'] = 'base64' |
| 702 |
msg.set_payload(x) |
| 703 |
self.assertEqual(msg.get_payload(decode=True), |
| 704 |
(b'\x03\x00\xe9\xd0\xfe\xff\xff.\x8b\xc0' |
| 705 |
b'\xa1\x00p\xf6\xbf\xe9\x0f')) |
| 706 |
self.assertIsInstance(msg.defects[0], |
| 707 |
errors.InvalidBase64CharactersDefect) |
| 708 |
|
| 709 |
def test_broken_unicode_payload(self): |
| 710 |
# This test improves coverage but is not a compliance test. |
| 711 |
# The behavior in this situation is currently undefined by the API. |
| 712 |
x = 'this is a br\xf6ken thing to do' |
| 713 |
msg = Message() |
| 714 |
msg['content-type'] = 'text/plain' |
| 715 |
msg['content-transfer-encoding'] = '8bit' |
| 716 |
msg.set_payload(x) |
| 717 |
self.assertEqual(msg.get_payload(decode=True), |
| 718 |
bytes(x, 'raw-unicode-escape')) |
| 719 |
|
| 720 |
def test_questionable_bytes_payload(self): |
| 721 |
# This test improves coverage but is not a compliance test, |
| 722 |
# since it involves poking inside the black box. |
| 723 |
x = 'this is a quéstionable thing to do'.encode('utf-8') |
| 724 |
msg = Message() |
| 725 |
msg['content-type'] = 'text/plain; charset="utf-8"' |
| 726 |
msg['content-transfer-encoding'] = '8bit' |
| 727 |
msg._payload = x |
| 728 |
self.assertEqual(msg.get_payload(decode=True), x) |
| 729 |
|
| 730 |
# Issue 1078919 |
| 731 |
def test_ascii_add_header(self): |
| 732 |
msg = Message() |
| 733 |
msg.add_header('Content-Disposition', 'attachment', |
| 734 |
filename='bud.gif') |
| 735 |
self.assertEqual('attachment; filename="bud.gif"', |
| 736 |
msg['Content-Disposition']) |
| 737 |
|
| 738 |
def test_noascii_add_header(self): |
| 739 |
msg = Message() |
| 740 |
msg.add_header('Content-Disposition', 'attachment', |
| 741 |
filename="Fußballer.ppt") |
| 742 |
self.assertEqual( |
| 743 |
'attachment; filename*=utf-8\'\'Fu%C3%9Fballer.ppt', |
| 744 |
msg['Content-Disposition']) |
| 745 |
|
| 746 |
def test_nonascii_add_header_via_triple(self): |
| 747 |
msg = Message() |
| 748 |
msg.add_header('Content-Disposition', 'attachment', |
| 749 |
filename=('iso-8859-1', '', 'Fußballer.ppt')) |
| 750 |
self.assertEqual( |
| 751 |
'attachment; filename*=iso-8859-1\'\'Fu%DFballer.ppt', |
| 752 |
msg['Content-Disposition']) |
| 753 |
|
| 754 |
def test_ascii_add_header_with_tspecial(self): |
| 755 |
msg = Message() |
| 756 |
msg.add_header('Content-Disposition', 'attachment', |
| 757 |
filename="windows [filename].ppt") |
| 758 |
self.assertEqual( |
| 759 |
'attachment; filename="windows [filename].ppt"', |
| 760 |
msg['Content-Disposition']) |
| 761 |
|
| 762 |
def test_nonascii_add_header_with_tspecial(self): |
| 763 |
msg = Message() |
| 764 |
msg.add_header('Content-Disposition', 'attachment', |
| 765 |
filename="Fußballer [filename].ppt") |
| 766 |
self.assertEqual( |
| 767 |
"attachment; filename*=utf-8''Fu%C3%9Fballer%20%5Bfilename%5D.ppt", |
| 768 |
msg['Content-Disposition']) |
| 769 |
|
| 770 |
def test_invalid_header_names(self): |
| 771 |
invalid_headers = [ |
| 772 |
('Invalid Header', 'contains space'), |
| 773 |
('Tab\tHeader', 'contains tab'), |
| 774 |
('Colon:Header', 'contains colon'), |
| 775 |
('', 'Empty name'), |
| 776 |
(' LeadingSpace', 'starts with space'), |
| 777 |
('TrailingSpace ', 'ends with space'), |
| 778 |
('Header\x7F', 'Non-ASCII character'), |
| 779 |
('Header\x80', 'Extended ASCII'), |
| 780 |
] |
| 781 |
for policy in (email.policy.default, email.policy.compat32): |
| 782 |
for setter in (Message.__setitem__, Message.add_header): |
| 783 |
for name, value in invalid_headers: |
| 784 |
self.do_test_invalid_header_names( |
| 785 |
policy, setter,name, value) |
| 786 |
|
| 787 |
def do_test_invalid_header_names(self, policy, setter, name, value): |
| 788 |
with self.subTest(policy=policy, setter=setter, name=name, value=value): |
| 789 |
message = Message(policy=policy) |
| 790 |
pattern = r'(?i)(?=.*invalid)(?=.*header)(?=.*name)' |
| 791 |
with self.assertRaisesRegex(ValueError, pattern) as cm: |
| 792 |
setter(message, name, value) |
| 793 |
self.assertIn(f"{name!r}", str(cm.exception)) |
| 794 |
|
| 795 |
def test_binary_quopri_payload(self): |
| 796 |
for charset in ('latin-1', 'ascii'): |
| 797 |
msg = Message() |
| 798 |
msg['content-type'] = 'text/plain; charset=%s' % charset |
| 799 |
msg['content-transfer-encoding'] = 'quoted-printable' |
| 800 |
msg.set_payload(b'foo=e6=96=87bar') |
| 801 |
self.assertEqual( |
| 802 |
msg.get_payload(decode=True), |
| 803 |
b'foo\xe6\x96\x87bar', |
| 804 |
'get_payload returns wrong result with charset %s.' % charset) |
| 805 |
|
| 806 |
def test_binary_base64_payload(self): |
| 807 |
for charset in ('latin-1', 'ascii'): |
| 808 |
msg = Message() |
| 809 |
msg['content-type'] = 'text/plain; charset=%s' % charset |
| 810 |
msg['content-transfer-encoding'] = 'base64' |
| 811 |
msg.set_payload(b'Zm9v5paHYmFy') |
| 812 |
self.assertEqual( |
| 813 |
msg.get_payload(decode=True), |
| 814 |
b'foo\xe6\x96\x87bar', |
| 815 |
'get_payload returns wrong result with charset %s.' % charset) |
| 816 |
|
| 817 |
def test_binary_uuencode_payload(self): |
| 818 |
for charset in ('latin-1', 'ascii'): |
| 819 |
for encoding in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): |
| 820 |
msg = Message() |
| 821 |
msg['content-type'] = 'text/plain; charset=%s' % charset |
| 822 |
msg['content-transfer-encoding'] = encoding |
| 823 |
msg.set_payload(b"begin 666 -\n)9F]OYI:'8F%R\n \nend\n") |
| 824 |
self.assertEqual( |
| 825 |
msg.get_payload(decode=True), |
| 826 |
b'foo\xe6\x96\x87bar', |
| 827 |
str(('get_payload returns wrong result ', |
| 828 |
'with charset {0} and encoding {1}.')).\ |
| 829 |
format(charset, encoding)) |
| 830 |
|
| 831 |
def test_add_header_with_name_only_param(self): |
| 832 |
msg = Message() |
| 833 |
msg.add_header('Content-Disposition', 'inline', foo_bar=None) |
| 834 |
self.assertEqual("inline; foo-bar", msg['Content-Disposition']) |
| 835 |
|
| 836 |
def test_add_header_with_no_value(self): |
| 837 |
msg = Message() |
| 838 |
msg.add_header('X-Status', None) |
| 839 |
self.assertEqual('', msg['X-Status']) |
| 840 |
|
| 841 |
# Issue 5871: reject an attempt to embed a header inside a header value |
| 842 |
# (header injection attack). |
| 843 |
def test_embedded_header_via_Header_rejected(self): |
| 844 |
msg = Message() |
| 845 |
msg['Dummy'] = Header('dummy\nX-Injected-Header: test') |
| 846 |
self.assertRaises(errors.HeaderParseError, msg.as_string) |
| 847 |
|
| 848 |
def test_embedded_header_via_string_rejected(self): |
| 849 |
msg = Message() |
| 850 |
msg['Dummy'] = 'dummy\nX-Injected-Header: test' |
| 851 |
self.assertRaises(errors.HeaderParseError, msg.as_string) |
| 852 |
|
| 853 |
def test_unicode_header_defaults_to_utf8_encoding(self): |
| 854 |
# Issue 14291 |
| 855 |
m = MIMEText('abc\n') |
| 856 |
m['Subject'] = 'É test' |
| 857 |
self.assertEqual(str(m),textwrap.dedent("""\ |
| 858 |
Content-Type: text/plain; charset="us-ascii" |
| 859 |
MIME-Version: 1.0 |
| 860 |
Content-Transfer-Encoding: 7bit |
| 861 |
Subject: =?utf-8?q?=C3=89_test?= |
| 862 |
|
| 863 |
abc |
| 864 |
""")) |
| 865 |
|
| 866 |
def test_unicode_body_defaults_to_utf8_encoding(self): |
| 867 |
# Issue 14291 |
| 868 |
m = MIMEText('É testabc\n') |
| 869 |
self.assertEqual(str(m),textwrap.dedent("""\ |
| 870 |
Content-Type: text/plain; charset="utf-8" |
| 871 |
MIME-Version: 1.0 |
| 872 |
Content-Transfer-Encoding: base64 |
| 873 |
|
| 874 |
w4kgdGVzdGFiYwo= |
| 875 |
""")) |
| 876 |
|
| 877 |
def test_string_payload_with_base64_cte(self): |
| 878 |
msg = email.message_from_string(textwrap.dedent("""\ |
| 879 |
Content-Transfer-Encoding: base64 |
| 880 |
|
| 881 |
SGVsbG8uIFRlc3Rpbmc= |
| 882 |
"""), policy=email.policy.default) |
| 883 |
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing") |
| 884 |
self.assertDefectsEqual(msg['content-transfer-encoding'].defects, []) |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
# Test the email.encoders module |
| 889 |
class TestEncoders(unittest.TestCase): |
| 890 |
|
| 891 |
def test_EncodersEncode_base64(self): |
| 892 |
with openfile('python.gif', 'rb') as fp: |
| 893 |
bindata = fp.read() |
| 894 |
mimed = email.mime.image.MIMEImage(bindata) |
| 895 |
base64ed = mimed.get_payload() |
| 896 |
# the transfer-encoded body lines should all be <=76 characters |
| 897 |
lines = base64ed.split('\n') |
| 898 |
self.assertLessEqual(max([ len(x) for x in lines ]), 76) |
| 899 |
|
| 900 |
def test_encode_empty_payload(self): |
| 901 |
eq = self.assertEqual |
| 902 |
msg = Message() |
| 903 |
msg.set_charset('us-ascii') |
| 904 |
eq(msg['content-transfer-encoding'], '7bit') |
| 905 |
|
| 906 |
def test_default_cte(self): |
| 907 |
eq = self.assertEqual |
| 908 |
# 7bit data and the default us-ascii _charset |
| 909 |
msg = MIMEText('hello world') |
| 910 |
eq(msg['content-transfer-encoding'], '7bit') |
| 911 |
# Similar, but with 8bit data |
| 912 |
msg = MIMEText('hello \xf8 world') |
| 913 |
eq(msg['content-transfer-encoding'], 'base64') |
| 914 |
# And now with a different charset |
| 915 |
msg = MIMEText('hello \xf8 world', _charset='iso-8859-1') |
| 916 |
eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 917 |
|
| 918 |
def test_encode7or8bit(self): |
| 919 |
# Make sure a charset whose input character set is 8bit but |
| 920 |
# whose output character set is 7bit gets a transfer-encoding |
| 921 |
# of 7bit. |
| 922 |
eq = self.assertEqual |
| 923 |
msg = MIMEText('文\n', _charset='euc-jp') |
| 924 |
eq(msg['content-transfer-encoding'], '7bit') |
| 925 |
eq(msg.as_string(), textwrap.dedent("""\ |
| 926 |
MIME-Version: 1.0 |
| 927 |
Content-Type: text/plain; charset="iso-2022-jp" |
| 928 |
Content-Transfer-Encoding: 7bit |
| 929 |
|
| 930 |
\x1b$BJ8\x1b(B |
| 931 |
""")) |
| 932 |
|
| 933 |
def test_qp_encode_latin1(self): |
| 934 |
msg = MIMEText('\xe1\xf6\n', 'text', 'ISO-8859-1') |
| 935 |
self.assertEqual(str(msg), textwrap.dedent("""\ |
| 936 |
MIME-Version: 1.0 |
| 937 |
Content-Type: text/text; charset="iso-8859-1" |
| 938 |
Content-Transfer-Encoding: quoted-printable |
| 939 |
|
| 940 |
=E1=F6 |
| 941 |
""")) |
| 942 |
|
| 943 |
def test_qp_encode_non_latin1(self): |
| 944 |
# Issue 16948 |
| 945 |
msg = MIMEText('\u017c\n', 'text', 'ISO-8859-2') |
| 946 |
self.assertEqual(str(msg), textwrap.dedent("""\ |
| 947 |
MIME-Version: 1.0 |
| 948 |
Content-Type: text/text; charset="iso-8859-2" |
| 949 |
Content-Transfer-Encoding: quoted-printable |
| 950 |
|
| 951 |
=BF |
| 952 |
""")) |
| 953 |
|
| 954 |
|
| 955 |
# Test long header wrapping |
| 956 |
class TestLongHeaders(TestEmailBase): |
| 957 |
|
| 958 |
maxDiff = None |
| 959 |
|
| 960 |
def test_split_long_continuation(self): |
| 961 |
eq = self.ndiffAssertEqual |
| 962 |
msg = email.message_from_string("""\ |
| 963 |
Subject: bug demonstration |
| 964 |
\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 965 |
\tmore text |
| 966 |
|
| 967 |
test |
| 968 |
""") |
| 969 |
sfp = StringIO() |
| 970 |
g = Generator(sfp) |
| 971 |
g.flatten(msg) |
| 972 |
eq(sfp.getvalue(), """\ |
| 973 |
Subject: bug demonstration |
| 974 |
\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 975 |
\tmore text |
| 976 |
|
| 977 |
test |
| 978 |
""") |
| 979 |
|
| 980 |
def test_another_long_almost_unsplittable_header(self): |
| 981 |
eq = self.ndiffAssertEqual |
| 982 |
hstr = """\ |
| 983 |
bug demonstration |
| 984 |
\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 985 |
\tmore text""" |
| 986 |
h = Header(hstr, continuation_ws='\t') |
| 987 |
eq(h.encode(), """\ |
| 988 |
bug demonstration |
| 989 |
\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 990 |
\tmore text""") |
| 991 |
h = Header(hstr.replace('\t', ' ')) |
| 992 |
eq(h.encode(), """\ |
| 993 |
bug demonstration |
| 994 |
12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 995 |
more text""") |
| 996 |
|
| 997 |
def test_long_nonstring(self): |
| 998 |
eq = self.ndiffAssertEqual |
| 999 |
g = Charset("iso-8859-1") |
| 1000 |
cz = Charset("iso-8859-2") |
| 1001 |
utf8 = Charset("utf-8") |
| 1002 |
g_head = (b'Die Mieter treten hier ein werden mit einem Foerderband ' |
| 1003 |
b'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen ' |
| 1004 |
b'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen ' |
| 1005 |
b'bef\xf6rdert. ') |
| 1006 |
cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich ' |
| 1007 |
b'd\xf9vtipu.. ') |
| 1008 |
utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 1009 |
'\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 1010 |
'\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 1011 |
'\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 1012 |
'\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das ' |
| 1013 |
'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder ' |
| 1014 |
'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066' |
| 1015 |
'\u3044\u307e\u3059\u3002') |
| 1016 |
h = Header(g_head, g, header_name='Subject') |
| 1017 |
h.append(cz_head, cz) |
| 1018 |
h.append(utf8_head, utf8) |
| 1019 |
msg = Message() |
| 1020 |
msg['Subject'] = h |
| 1021 |
sfp = StringIO() |
| 1022 |
g = Generator(sfp) |
| 1023 |
g.flatten(msg) |
| 1024 |
eq(sfp.getvalue(), """\ |
| 1025 |
Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderb?= |
| 1026 |
=?iso-8859-1?q?and_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen?= |
| 1027 |
=?iso-8859-1?q?_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef?= |
| 1028 |
=?iso-8859-1?q?=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hrouti?= |
| 1029 |
=?iso-8859-2?q?ly_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?= |
| 1030 |
=?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?= |
| 1031 |
=?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn44Gf44KJ?= |
| 1032 |
=?utf-8?b?44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFzIE51bnN0dWNr?= |
| 1033 |
=?utf-8?b?IGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5kIGRhcyBPZGVyIGRp?= |
| 1034 |
=?utf-8?b?ZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIDjgaPjgabjgYTjgb7jgZk=?= |
| 1035 |
=?utf-8?b?44CC?= |
| 1036 |
|
| 1037 |
""") |
| 1038 |
eq(h.encode(maxlinelen=76), """\ |
| 1039 |
=?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerde?= |
| 1040 |
=?iso-8859-1?q?rband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndis?= |
| 1041 |
=?iso-8859-1?q?chen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klinge?= |
| 1042 |
=?iso-8859-1?q?n_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se?= |
| 1043 |
=?iso-8859-2?q?_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= |
| 1044 |
=?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb?= |
| 1045 |
=?utf-8?b?44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go?= |
| 1046 |
=?utf-8?b?44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBp?= |
| 1047 |
=?utf-8?b?c3QgZGFzIE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWlo?= |
| 1048 |
=?utf-8?b?ZXJodW5kIGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI0=?= |
| 1049 |
=?utf-8?b?44Go6KiA44Gj44Gm44GE44G+44GZ44CC?=""") |
| 1050 |
|
| 1051 |
def test_long_header_encode(self): |
| 1052 |
eq = self.ndiffAssertEqual |
| 1053 |
h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' |
| 1054 |
'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 1055 |
header_name='X-Foobar-Spoink-Defrobnit') |
| 1056 |
eq(h.encode(), '''\ |
| 1057 |
wasnipoop; giraffes="very-long-necked-animals"; |
| 1058 |
spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 1059 |
|
| 1060 |
def test_long_header_encode_with_tab_continuation_is_just_a_hint(self): |
| 1061 |
eq = self.ndiffAssertEqual |
| 1062 |
h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' |
| 1063 |
'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 1064 |
header_name='X-Foobar-Spoink-Defrobnit', |
| 1065 |
continuation_ws='\t') |
| 1066 |
eq(h.encode(), '''\ |
| 1067 |
wasnipoop; giraffes="very-long-necked-animals"; |
| 1068 |
spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 1069 |
|
| 1070 |
def test_long_header_encode_with_tab_continuation(self): |
| 1071 |
eq = self.ndiffAssertEqual |
| 1072 |
h = Header('wasnipoop; giraffes="very-long-necked-animals";\t' |
| 1073 |
'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 1074 |
header_name='X-Foobar-Spoink-Defrobnit', |
| 1075 |
continuation_ws='\t') |
| 1076 |
eq(h.encode(), '''\ |
| 1077 |
wasnipoop; giraffes="very-long-necked-animals"; |
| 1078 |
\tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 1079 |
|
| 1080 |
def test_header_encode_with_different_output_charset(self): |
| 1081 |
h = Header('文', 'euc-jp') |
| 1082 |
self.assertEqual(h.encode(), "=?iso-2022-jp?b?GyRCSjgbKEI=?=") |
| 1083 |
|
| 1084 |
def test_long_header_encode_with_different_output_charset(self): |
| 1085 |
h = Header(b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5\xa4\xec\xa4' |
| 1086 |
b'\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2\xf1\xbc\xd4' |
| 1087 |
b'\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3\xa4\xc6\xa4' |
| 1088 |
b'\xa4\xa4\xde\xa4\xb9'.decode('euc-jp'), 'euc-jp') |
| 1089 |
res = """\ |
| 1090 |
=?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKMnE8VCROPjUbKEI=?= |
| 1091 |
=?iso-2022-jp?b?GyRCRyckckJUJEMkRiQkJF4kORsoQg==?=""" |
| 1092 |
self.assertEqual(h.encode(), res) |
| 1093 |
|
| 1094 |
def test_header_splitter(self): |
| 1095 |
eq = self.ndiffAssertEqual |
| 1096 |
msg = MIMEText('') |
| 1097 |
# It'd be great if we could use add_header() here, but that doesn't |
| 1098 |
# guarantee an order of the parameters. |
| 1099 |
msg['X-Foobar-Spoink-Defrobnit'] = ( |
| 1100 |
'wasnipoop; giraffes="very-long-necked-animals"; ' |
| 1101 |
'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"') |
| 1102 |
sfp = StringIO() |
| 1103 |
g = Generator(sfp) |
| 1104 |
g.flatten(msg) |
| 1105 |
eq(sfp.getvalue(), '''\ |
| 1106 |
Content-Type: text/plain; charset="us-ascii" |
| 1107 |
MIME-Version: 1.0 |
| 1108 |
Content-Transfer-Encoding: 7bit |
| 1109 |
X-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals"; |
| 1110 |
spooge="yummy"; hippos="gargantuan"; marshmallows="gooey" |
| 1111 |
|
| 1112 |
''') |
| 1113 |
|
| 1114 |
def test_no_semis_header_splitter(self): |
| 1115 |
eq = self.ndiffAssertEqual |
| 1116 |
msg = Message() |
| 1117 |
msg['From'] = '[email protected]' |
| 1118 |
msg['References'] = SPACE.join('<%[email protected]>' % i for i in range(10)) |
| 1119 |
msg.set_payload('Test') |
| 1120 |
sfp = StringIO() |
| 1121 |
g = Generator(sfp) |
| 1122 |
g.flatten(msg) |
| 1123 |
eq(sfp.getvalue(), """\ |
| 1124 |
From: [email protected] |
| 1125 |
References: <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> |
| 1126 |
<[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> |
| 1127 |
|
| 1128 |
Test""") |
| 1129 |
|
| 1130 |
def test_last_split_chunk_does_not_fit(self): |
| 1131 |
eq = self.ndiffAssertEqual |
| 1132 |
h = Header('Subject: the first part of this is short, but_the_second' |
| 1133 |
'_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line' |
| 1134 |
'_all_by_itself') |
| 1135 |
eq(h.encode(), """\ |
| 1136 |
Subject: the first part of this is short, |
| 1137 |
but_the_second_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself""") |
| 1138 |
|
| 1139 |
def test_splittable_leading_char_followed_by_overlong_unsplittable(self): |
| 1140 |
eq = self.ndiffAssertEqual |
| 1141 |
h = Header(', but_the_second' |
| 1142 |
'_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line' |
| 1143 |
'_all_by_itself') |
| 1144 |
eq(h.encode(), """\ |
| 1145 |
, |
| 1146 |
but_the_second_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself""") |
| 1147 |
|
| 1148 |
def test_multiple_splittable_leading_char_followed_by_overlong_unsplittable(self): |
| 1149 |
eq = self.ndiffAssertEqual |
| 1150 |
h = Header(', , but_the_second' |
| 1151 |
'_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line' |
| 1152 |
'_all_by_itself') |
| 1153 |
eq(h.encode(), """\ |
| 1154 |
, , |
| 1155 |
but_the_second_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself""") |
| 1156 |
|
| 1157 |
def test_trailing_splittable_on_overlong_unsplittable(self): |
| 1158 |
eq = self.ndiffAssertEqual |
| 1159 |
h = Header('this_part_does_not_fit_within_maxlinelen_and_thus_should_' |
| 1160 |
'be_on_a_line_all_by_itself;') |
| 1161 |
eq(h.encode(), "this_part_does_not_fit_within_maxlinelen_and_thus_should_" |
| 1162 |
"be_on_a_line_all_by_itself;") |
| 1163 |
|
| 1164 |
def test_trailing_splittable_on_overlong_unsplittable_with_leading_splittable(self): |
| 1165 |
eq = self.ndiffAssertEqual |
| 1166 |
h = Header('; ' |
| 1167 |
'this_part_does_not_fit_within_maxlinelen_and_thus_should_' |
| 1168 |
'be_on_a_line_all_by_itself; ') |
| 1169 |
eq(h.encode(), """\ |
| 1170 |
; |
| 1171 |
this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself; """) |
| 1172 |
|
| 1173 |
def test_long_header_with_multiple_sequential_split_chars(self): |
| 1174 |
eq = self.ndiffAssertEqual |
| 1175 |
h = Header('This is a long line that has two whitespaces in a row. ' |
| 1176 |
'This used to cause truncation of the header when folded') |
| 1177 |
eq(h.encode(), """\ |
| 1178 |
This is a long line that has two whitespaces in a row. This used to cause |
| 1179 |
truncation of the header when folded""") |
| 1180 |
|
| 1181 |
def test_splitter_split_on_punctuation_only_if_fws_with_header(self): |
| 1182 |
eq = self.ndiffAssertEqual |
| 1183 |
h = Header('thisverylongheaderhas;semicolons;and,commas,but' |
| 1184 |
'they;arenotlegal;fold,points') |
| 1185 |
eq(h.encode(), "thisverylongheaderhas;semicolons;and,commas,butthey;" |
| 1186 |
"arenotlegal;fold,points") |
| 1187 |
|
| 1188 |
def test_leading_splittable_in_the_middle_just_before_overlong_last_part(self): |
| 1189 |
eq = self.ndiffAssertEqual |
| 1190 |
h = Header('this is a test where we need to have more than one line ' |
| 1191 |
'before; our final line that is just too big to fit;; ' |
| 1192 |
'this_part_does_not_fit_within_maxlinelen_and_thus_should_' |
| 1193 |
'be_on_a_line_all_by_itself;') |
| 1194 |
eq(h.encode(), """\ |
| 1195 |
this is a test where we need to have more than one line before; |
| 1196 |
our final line that is just too big to fit;; |
| 1197 |
this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself;""") |
| 1198 |
|
| 1199 |
def test_overlong_last_part_followed_by_split_point(self): |
| 1200 |
eq = self.ndiffAssertEqual |
| 1201 |
h = Header('this_part_does_not_fit_within_maxlinelen_and_thus_should_' |
| 1202 |
'be_on_a_line_all_by_itself ') |
| 1203 |
eq(h.encode(), "this_part_does_not_fit_within_maxlinelen_and_thus_" |
| 1204 |
"should_be_on_a_line_all_by_itself ") |
| 1205 |
|
| 1206 |
def test_multiline_with_overlong_parts_separated_by_two_split_points(self): |
| 1207 |
eq = self.ndiffAssertEqual |
| 1208 |
h = Header('this_is_a__test_where_we_need_to_have_more_than_one_line_' |
| 1209 |
'before_our_final_line_; ; ' |
| 1210 |
'this_part_does_not_fit_within_maxlinelen_and_thus_should_' |
| 1211 |
'be_on_a_line_all_by_itself; ') |
| 1212 |
eq(h.encode(), """\ |
| 1213 |
this_is_a__test_where_we_need_to_have_more_than_one_line_before_our_final_line_; |
| 1214 |
; |
| 1215 |
this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself; """) |
| 1216 |
|
| 1217 |
def test_multiline_with_overlong_last_part_followed_by_split_point(self): |
| 1218 |
eq = self.ndiffAssertEqual |
| 1219 |
h = Header('this is a test where we need to have more than one line ' |
| 1220 |
'before our final line; ; ' |
| 1221 |
'this_part_does_not_fit_within_maxlinelen_and_thus_should_' |
| 1222 |
'be_on_a_line_all_by_itself; ') |
| 1223 |
eq(h.encode(), """\ |
| 1224 |
this is a test where we need to have more than one line before our final line; |
| 1225 |
; |
| 1226 |
this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself; """) |
| 1227 |
|
| 1228 |
def test_long_header_with_whitespace_runs(self): |
| 1229 |
eq = self.ndiffAssertEqual |
| 1230 |
msg = Message() |
| 1231 |
msg['From'] = '[email protected]' |
| 1232 |
msg['References'] = SPACE.join(['<[email protected]> '] * 10) |
| 1233 |
msg.set_payload('Test') |
| 1234 |
sfp = StringIO() |
| 1235 |
g = Generator(sfp) |
| 1236 |
g.flatten(msg) |
| 1237 |
eq(sfp.getvalue(), """\ |
| 1238 |
From: [email protected] |
| 1239 |
References: <[email protected]> <[email protected]> <[email protected]> <[email protected]> |
| 1240 |
<[email protected]> <[email protected]> <[email protected]> <[email protected]> |
| 1241 |
<[email protected]> <[email protected]>\x20\x20 |
| 1242 |
|
| 1243 |
Test""") |
| 1244 |
|
| 1245 |
def test_long_run_with_semi_header_splitter(self): |
| 1246 |
eq = self.ndiffAssertEqual |
| 1247 |
msg = Message() |
| 1248 |
msg['From'] = '[email protected]' |
| 1249 |
msg['References'] = SPACE.join(['<[email protected]>'] * 10) + '; abc' |
| 1250 |
msg.set_payload('Test') |
| 1251 |
sfp = StringIO() |
| 1252 |
g = Generator(sfp) |
| 1253 |
g.flatten(msg) |
| 1254 |
eq(sfp.getvalue(), """\ |
| 1255 |
From: [email protected] |
| 1256 |
References: <[email protected]> <[email protected]> <[email protected]> <[email protected]> |
| 1257 |
<[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> |
| 1258 |
<[email protected]>; abc |
| 1259 |
|
| 1260 |
Test""") |
| 1261 |
|
| 1262 |
def test_splitter_split_on_punctuation_only_if_fws(self): |
| 1263 |
eq = self.ndiffAssertEqual |
| 1264 |
msg = Message() |
| 1265 |
msg['From'] = '[email protected]' |
| 1266 |
msg['References'] = ('thisverylongheaderhas;semicolons;and,commas,but' |
| 1267 |
'they;arenotlegal;fold,points') |
| 1268 |
msg.set_payload('Test') |
| 1269 |
sfp = StringIO() |
| 1270 |
g = Generator(sfp) |
| 1271 |
g.flatten(msg) |
| 1272 |
# XXX the space after the header should not be there. |
| 1273 |
eq(sfp.getvalue(), """\ |
| 1274 |
From: [email protected] |
| 1275 |
References:\x20 |
| 1276 |
thisverylongheaderhas;semicolons;and,commas,butthey;arenotlegal;fold,points |
| 1277 |
|
| 1278 |
Test""") |
| 1279 |
|
| 1280 |
def test_no_split_long_header(self): |
| 1281 |
eq = self.ndiffAssertEqual |
| 1282 |
hstr = 'References: ' + 'x' * 80 |
| 1283 |
h = Header(hstr) |
| 1284 |
# These come on two lines because Headers are really field value |
| 1285 |
# classes and don't really know about their field names. |
| 1286 |
eq(h.encode(), """\ |
| 1287 |
References: |
| 1288 |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""") |
| 1289 |
h = Header('x' * 80) |
| 1290 |
eq(h.encode(), 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') |
| 1291 |
|
| 1292 |
def test_splitting_multiple_long_lines(self): |
| 1293 |
eq = self.ndiffAssertEqual |
| 1294 |
hstr = """\ |
| 1295 |
from babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <[email protected]>; Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 1296 |
\tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <[email protected]>; Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 1297 |
\tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <[email protected]>; Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 1298 |
""" |
| 1299 |
h = Header(hstr, continuation_ws='\t') |
| 1300 |
eq(h.encode(), """\ |
| 1301 |
from babylon.socal-raves.org (localhost [127.0.0.1]); |
| 1302 |
by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 1303 |
for <[email protected]>; |
| 1304 |
Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 1305 |
\tfrom babylon.socal-raves.org (localhost [127.0.0.1]); |
| 1306 |
by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 1307 |
for <[email protected]>; |
| 1308 |
Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 1309 |
\tfrom babylon.socal-raves.org (localhost [127.0.0.1]); |
| 1310 |
by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 1311 |
for <[email protected]>; |
| 1312 |
Sat, 2 Feb 2002 17:00:06 -0800 (PST)""") |
| 1313 |
|
| 1314 |
def test_splitting_first_line_only_is_long(self): |
| 1315 |
eq = self.ndiffAssertEqual |
| 1316 |
hstr = """\ |
| 1317 |
from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] helo=cthulhu.gerg.ca) |
| 1318 |
\tby kronos.mems-exchange.org with esmtp (Exim 4.05) |
| 1319 |
\tid 17k4h5-00034i-00 |
| 1320 |
\tfor [email protected]; Wed, 28 Aug 2002 11:25:20 -0400""" |
| 1321 |
h = Header(hstr, maxlinelen=78, header_name='Received', |
| 1322 |
continuation_ws='\t') |
| 1323 |
eq(h.encode(), """\ |
| 1324 |
from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] |
| 1325 |
helo=cthulhu.gerg.ca) |
| 1326 |
\tby kronos.mems-exchange.org with esmtp (Exim 4.05) |
| 1327 |
\tid 17k4h5-00034i-00 |
| 1328 |
\tfor [email protected]; Wed, 28 Aug 2002 11:25:20 -0400""") |
| 1329 |
|
| 1330 |
def test_long_8bit_header(self): |
| 1331 |
eq = self.ndiffAssertEqual |
| 1332 |
msg = Message() |
| 1333 |
h = Header('Britische Regierung gibt', 'iso-8859-1', |
| 1334 |
header_name='Subject') |
| 1335 |
h.append('gr\xfcnes Licht f\xfcr Offshore-Windkraftprojekte') |
| 1336 |
eq(h.encode(maxlinelen=76), """\ |
| 1337 |
=?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offs?= |
| 1338 |
=?iso-8859-1?q?hore-Windkraftprojekte?=""") |
| 1339 |
msg['Subject'] = h |
| 1340 |
eq(msg.as_string(maxheaderlen=76), """\ |
| 1341 |
Subject: =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offs?= |
| 1342 |
=?iso-8859-1?q?hore-Windkraftprojekte?= |
| 1343 |
|
| 1344 |
""") |
| 1345 |
eq(msg.as_string(maxheaderlen=0), """\ |
| 1346 |
Subject: =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offshore-Windkraftprojekte?= |
| 1347 |
|
| 1348 |
""") |
| 1349 |
|
| 1350 |
def test_long_8bit_header_no_charset(self): |
| 1351 |
eq = self.ndiffAssertEqual |
| 1352 |
msg = Message() |
| 1353 |
header_string = ('Britische Regierung gibt gr\xfcnes Licht ' |
| 1354 |
'f\xfcr Offshore-Windkraftprojekte ' |
| 1355 |
'<[email protected]>') |
| 1356 |
msg['Reply-To'] = header_string |
| 1357 |
eq(msg.as_string(maxheaderlen=78), """\ |
| 1358 |
Reply-To: =?utf-8?q?Britische_Regierung_gibt_gr=C3=BCnes_Licht_f=C3=BCr_Offs?= |
| 1359 |
=?utf-8?q?hore-Windkraftprojekte_=3Ca-very-long-address=40example=2Ecom=3E?= |
| 1360 |
|
| 1361 |
""") |
| 1362 |
msg = Message() |
| 1363 |
msg['Reply-To'] = Header(header_string, |
| 1364 |
header_name='Reply-To') |
| 1365 |
eq(msg.as_string(maxheaderlen=78), """\ |
| 1366 |
Reply-To: =?utf-8?q?Britische_Regierung_gibt_gr=C3=BCnes_Licht_f=C3=BCr_Offs?= |
| 1367 |
=?utf-8?q?hore-Windkraftprojekte_=3Ca-very-long-address=40example=2Ecom=3E?= |
| 1368 |
|
| 1369 |
""") |
| 1370 |
|
| 1371 |
def test_long_to_header(self): |
| 1372 |
eq = self.ndiffAssertEqual |
| 1373 |
to = ('"Someone Test #A" <[email protected]>,' |
| 1374 |
'<[email protected]>, ' |
| 1375 |
'"Someone Test #B" <[email protected]>, ' |
| 1376 |
'"Someone Test #C" <[email protected]>, ' |
| 1377 |
'"Someone Test #D" <[email protected]>') |
| 1378 |
msg = Message() |
| 1379 |
msg['To'] = to |
| 1380 |
eq(msg.as_string(maxheaderlen=78), '''\ |
| 1381 |
To: "Someone Test #A" <[email protected]>,<[email protected]>, |
| 1382 |
"Someone Test #B" <[email protected]>, |
| 1383 |
"Someone Test #C" <[email protected]>, |
| 1384 |
"Someone Test #D" <[email protected]> |
| 1385 |
|
| 1386 |
''') |
| 1387 |
|
| 1388 |
def test_long_line_after_append(self): |
| 1389 |
eq = self.ndiffAssertEqual |
| 1390 |
s = 'This is an example of string which has almost the limit of header length.' |
| 1391 |
h = Header(s) |
| 1392 |
h.append('Add another line.') |
| 1393 |
eq(h.encode(maxlinelen=76), """\ |
| 1394 |
This is an example of string which has almost the limit of header length. |
| 1395 |
Add another line.""") |
| 1396 |
|
| 1397 |
def test_shorter_line_with_append(self): |
| 1398 |
eq = self.ndiffAssertEqual |
| 1399 |
s = 'This is a shorter line.' |
| 1400 |
h = Header(s) |
| 1401 |
h.append('Add another sentence. (Surprise?)') |
| 1402 |
eq(h.encode(), |
| 1403 |
'This is a shorter line. Add another sentence. (Surprise?)') |
| 1404 |
|
| 1405 |
def test_long_field_name(self): |
| 1406 |
eq = self.ndiffAssertEqual |
| 1407 |
fn = 'X-Very-Very-Very-Long-Header-Name' |
| 1408 |
gs = ('Die Mieter treten hier ein werden mit einem Foerderband ' |
| 1409 |
'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen ' |
| 1410 |
'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen ' |
| 1411 |
'bef\xf6rdert. ') |
| 1412 |
h = Header(gs, 'iso-8859-1', header_name=fn) |
| 1413 |
# BAW: this seems broken because the first line is too long |
| 1414 |
eq(h.encode(maxlinelen=76), """\ |
| 1415 |
=?iso-8859-1?q?Die_Mieter_treten_hier_e?= |
| 1416 |
=?iso-8859-1?q?in_werden_mit_einem_Foerderband_komfortabel_den_Korridor_e?= |
| 1417 |
=?iso-8859-1?q?ntlang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei=2C_ge?= |
| 1418 |
=?iso-8859-1?q?gen_die_rotierenden_Klingen_bef=F6rdert=2E_?=""") |
| 1419 |
|
| 1420 |
def test_long_received_header(self): |
| 1421 |
h = ('from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) ' |
| 1422 |
'by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; ' |
| 1423 |
'Wed, 05 Mar 2003 18:10:18 -0700') |
| 1424 |
msg = Message() |
| 1425 |
msg['Received-1'] = Header(h, continuation_ws='\t') |
| 1426 |
msg['Received-2'] = h |
| 1427 |
# This should be splitting on spaces not semicolons. |
| 1428 |
self.ndiffAssertEqual(msg.as_string(maxheaderlen=78), """\ |
| 1429 |
Received-1: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by |
| 1430 |
hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; |
| 1431 |
Wed, 05 Mar 2003 18:10:18 -0700 |
| 1432 |
Received-2: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by |
| 1433 |
hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; |
| 1434 |
Wed, 05 Mar 2003 18:10:18 -0700 |
| 1435 |
|
| 1436 |
""") |
| 1437 |
|
| 1438 |
def test_string_headerinst_eq(self): |
| 1439 |
h = ('<[email protected].' |
| 1440 |
'tu-muenchen.de> (David Bremner\'s message of ' |
| 1441 |
'"Thu, 6 Mar 2003 13:58:21 +0100")') |
| 1442 |
msg = Message() |
| 1443 |
msg['Received-1'] = Header(h, header_name='Received-1', |
| 1444 |
continuation_ws='\t') |
| 1445 |
msg['Received-2'] = h |
| 1446 |
# XXX The space after the ':' should not be there. |
| 1447 |
self.ndiffAssertEqual(msg.as_string(maxheaderlen=78), """\ |
| 1448 |
Received-1:\x20 |
| 1449 |
<15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David |
| 1450 |
Bremner's message of \"Thu, 6 Mar 2003 13:58:21 +0100\") |
| 1451 |
Received-2:\x20 |
| 1452 |
<15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David |
| 1453 |
Bremner's message of \"Thu, 6 Mar 2003 13:58:21 +0100\") |
| 1454 |
|
| 1455 |
""") |
| 1456 |
|
| 1457 |
def test_long_unbreakable_lines_with_continuation(self): |
| 1458 |
eq = self.ndiffAssertEqual |
| 1459 |
msg = Message() |
| 1460 |
t = """\ |
| 1461 |
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
| 1462 |
locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp""" |
| 1463 |
msg['Face-1'] = t |
| 1464 |
msg['Face-2'] = Header(t, header_name='Face-2') |
| 1465 |
msg['Face-3'] = ' ' + t |
| 1466 |
# XXX This splitting is all wrong. It the first value line should be |
| 1467 |
# snug against the field name or the space after the header not there. |
| 1468 |
eq(msg.as_string(maxheaderlen=78), """\ |
| 1469 |
Face-1:\x20 |
| 1470 |
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
| 1471 |
locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp |
| 1472 |
Face-2:\x20 |
| 1473 |
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
| 1474 |
locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp |
| 1475 |
Face-3:\x20 |
| 1476 |
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
| 1477 |
locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp |
| 1478 |
|
| 1479 |
""") |
| 1480 |
|
| 1481 |
def test_another_long_multiline_header(self): |
| 1482 |
eq = self.ndiffAssertEqual |
| 1483 |
m = ('Received: from siimage.com ' |
| 1484 |
'([172.25.1.3]) by zima.siliconimage.com with ' |
| 1485 |
'Microsoft SMTPSVC(5.0.2195.4905); ' |
| 1486 |
'Wed, 16 Oct 2002 07:41:11 -0700') |
| 1487 |
msg = email.message_from_string(m) |
| 1488 |
eq(msg.as_string(maxheaderlen=78), '''\ |
| 1489 |
Received: from siimage.com ([172.25.1.3]) by zima.siliconimage.com with |
| 1490 |
Microsoft SMTPSVC(5.0.2195.4905); Wed, 16 Oct 2002 07:41:11 -0700 |
| 1491 |
|
| 1492 |
''') |
| 1493 |
|
| 1494 |
def test_long_lines_with_different_header(self): |
| 1495 |
eq = self.ndiffAssertEqual |
| 1496 |
h = ('List-Unsubscribe: ' |
| 1497 |
'<http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,' |
| 1498 |
' <mailto:[email protected]' |
| 1499 |
'?subject=unsubscribe>') |
| 1500 |
msg = Message() |
| 1501 |
msg['List'] = h |
| 1502 |
msg['List'] = Header(h, header_name='List') |
| 1503 |
eq(msg.as_string(maxheaderlen=78), """\ |
| 1504 |
List: List-Unsubscribe: |
| 1505 |
<http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>, |
| 1506 |
<mailto:[email protected]?subject=unsubscribe> |
| 1507 |
List: List-Unsubscribe: |
| 1508 |
<http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>, |
| 1509 |
<mailto:[email protected]?subject=unsubscribe> |
| 1510 |
|
| 1511 |
""") |
| 1512 |
|
| 1513 |
def test_long_rfc2047_header_with_embedded_fws(self): |
| 1514 |
h = Header(textwrap.dedent("""\ |
| 1515 |
We're going to pretend this header is in a non-ascii character set |
| 1516 |
\tto see if line wrapping with encoded words and embedded |
| 1517 |
folding white space works"""), |
| 1518 |
charset='utf-8', |
| 1519 |
header_name='Test') |
| 1520 |
self.assertEqual(h.encode()+'\n', textwrap.dedent("""\ |
| 1521 |
=?utf-8?q?We=27re_going_to_pretend_this_header_is_in_a_non-ascii_chara?= |
| 1522 |
=?utf-8?q?cter_set?= |
| 1523 |
=?utf-8?q?_to_see_if_line_wrapping_with_encoded_words_and_embedded?= |
| 1524 |
=?utf-8?q?_folding_white_space_works?=""")+'\n') |
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
# Test mangling of "From " lines in the body of a message |
| 1529 |
class TestFromMangling(unittest.TestCase): |
| 1530 |
def setUp(self): |
| 1531 |
self.msg = Message() |
| 1532 |
self.msg['From'] = '[email protected]' |
| 1533 |
self.msg.set_payload("""\ |
| 1534 |
From the desk of A.A.A.: |
| 1535 |
Blah blah blah |
| 1536 |
""") |
| 1537 |
|
| 1538 |
def test_mangled_from(self): |
| 1539 |
s = StringIO() |
| 1540 |
g = Generator(s, mangle_from_=True) |
| 1541 |
g.flatten(self.msg) |
| 1542 |
self.assertEqual(s.getvalue(), """\ |
| 1543 |
From: [email protected] |
| 1544 |
|
| 1545 |
>From the desk of A.A.A.: |
| 1546 |
Blah blah blah |
| 1547 |
""") |
| 1548 |
|
| 1549 |
def test_dont_mangle_from(self): |
| 1550 |
s = StringIO() |
| 1551 |
g = Generator(s, mangle_from_=False) |
| 1552 |
g.flatten(self.msg) |
| 1553 |
self.assertEqual(s.getvalue(), """\ |
| 1554 |
From: [email protected] |
| 1555 |
|
| 1556 |
From the desk of A.A.A.: |
| 1557 |
Blah blah blah |
| 1558 |
""") |
| 1559 |
|
| 1560 |
def test_mangle_from_in_preamble_and_epilog(self): |
| 1561 |
s = StringIO() |
| 1562 |
g = Generator(s, mangle_from_=True) |
| 1563 |
msg = email.message_from_string(textwrap.dedent("""\ |
| 1564 |
From: [email protected] |
| 1565 |
Mime-Version: 1.0 |
| 1566 |
Content-Type: multipart/mixed; boundary=XXX |
| 1567 |
|
| 1568 |
From somewhere unknown |
| 1569 |
|
| 1570 |
--XXX |
| 1571 |
Content-Type: text/plain |
| 1572 |
|
| 1573 |
foo |
| 1574 |
|
| 1575 |
--XXX-- |
| 1576 |
|
| 1577 |
From somewhere unknowable |
| 1578 |
""")) |
| 1579 |
g.flatten(msg) |
| 1580 |
self.assertEqual(len([1 for x in s.getvalue().split('\n') |
| 1581 |
if x.startswith('>From ')]), 2) |
| 1582 |
|
| 1583 |
def test_mangled_from_with_bad_bytes(self): |
| 1584 |
source = textwrap.dedent("""\ |
| 1585 |
Content-Type: text/plain; charset="utf-8" |
| 1586 |
MIME-Version: 1.0 |
| 1587 |
Content-Transfer-Encoding: 8bit |
| 1588 |
From: [email protected] |
| 1589 |
|
| 1590 |
""").encode('utf-8') |
| 1591 |
msg = email.message_from_bytes(source + b'From R\xc3\xb6lli\n') |
| 1592 |
b = BytesIO() |
| 1593 |
g = BytesGenerator(b, mangle_from_=True) |
| 1594 |
g.flatten(msg) |
| 1595 |
self.assertEqual(b.getvalue(), source + b'>From R\xc3\xb6lli\n') |
| 1596 |
|
| 1597 |
def test_multipart_with_bad_bytes_in_cte(self): |
| 1598 |
# bpo30835 |
| 1599 |
source = textwrap.dedent("""\ |
| 1600 |
From: [email protected] |
| 1601 |
Content-Type: multipart/mixed; boundary="1" |
| 1602 |
Content-Transfer-Encoding: \xc8 |
| 1603 |
""").encode('utf-8') |
| 1604 |
msg = email.message_from_bytes(source) |
| 1605 |
|
| 1606 |
|
| 1607 |
# Test the basic MIMEAudio class |
| 1608 |
class TestMIMEAudio(unittest.TestCase): |
| 1609 |
def _make_audio(self, ext): |
| 1610 |
with openfile(f'sndhdr.{ext}', 'rb') as fp: |
| 1611 |
self._audiodata = fp.read() |
| 1612 |
self._au = MIMEAudio(self._audiodata) |
| 1613 |
|
| 1614 |
def test_guess_minor_type(self): |
| 1615 |
for ext, subtype in { |
| 1616 |
'aifc': 'x-aiff', |
| 1617 |
'aiff': 'x-aiff', |
| 1618 |
'wav': 'x-wav', |
| 1619 |
'au': 'basic', |
| 1620 |
}.items(): |
| 1621 |
self._make_audio(ext) |
| 1622 |
subtype = ext if subtype is None else subtype |
| 1623 |
self.assertEqual(self._au.get_content_type(), f'audio/{subtype}') |
| 1624 |
|
| 1625 |
def test_encoding(self): |
| 1626 |
self._make_audio('au') |
| 1627 |
payload = self._au.get_payload() |
| 1628 |
self.assertEqual(base64.decodebytes(bytes(payload, 'ascii')), |
| 1629 |
self._audiodata) |
| 1630 |
|
| 1631 |
def test_checkSetMinor(self): |
| 1632 |
self._make_audio('au') |
| 1633 |
au = MIMEAudio(self._audiodata, 'fish') |
| 1634 |
self.assertEqual(au.get_content_type(), 'audio/fish') |
| 1635 |
|
| 1636 |
def test_add_header(self): |
| 1637 |
self._make_audio('au') |
| 1638 |
eq = self.assertEqual |
| 1639 |
self._au.add_header('Content-Disposition', 'attachment', |
| 1640 |
filename='sndhdr.au') |
| 1641 |
eq(self._au['content-disposition'], |
| 1642 |
'attachment; filename="sndhdr.au"') |
| 1643 |
eq(self._au.get_params(header='content-disposition'), |
| 1644 |
[('attachment', ''), ('filename', 'sndhdr.au')]) |
| 1645 |
eq(self._au.get_param('filename', header='content-disposition'), |
| 1646 |
'sndhdr.au') |
| 1647 |
missing = [] |
| 1648 |
eq(self._au.get_param('attachment', header='content-disposition'), '') |
| 1649 |
self.assertIs(self._au.get_param( |
| 1650 |
'foo', failobj=missing, |
| 1651 |
header='content-disposition'), missing) |
| 1652 |
# Try some missing stuff |
| 1653 |
self.assertIs(self._au.get_param('foobar', missing), missing) |
| 1654 |
self.assertIs(self._au.get_param('attachment', missing, |
| 1655 |
header='foobar'), missing) |
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
# Test the basic MIMEImage class |
| 1660 |
class TestMIMEImage(unittest.TestCase): |
| 1661 |
def _make_image(self, ext): |
| 1662 |
with openfile(f'python.{ext}', 'rb') as fp: |
| 1663 |
self._imgdata = fp.read() |
| 1664 |
self._im = MIMEImage(self._imgdata) |
| 1665 |
|
| 1666 |
def test_guess_minor_type(self): |
| 1667 |
for ext, subtype in { |
| 1668 |
'bmp': None, |
| 1669 |
'exr': None, |
| 1670 |
'gif': None, |
| 1671 |
'jpg': 'jpeg', |
| 1672 |
'pbm': None, |
| 1673 |
'pgm': None, |
| 1674 |
'png': None, |
| 1675 |
'ppm': None, |
| 1676 |
'ras': 'rast', |
| 1677 |
'sgi': 'rgb', |
| 1678 |
'tiff': None, |
| 1679 |
'webp': None, |
| 1680 |
'xbm': None, |
| 1681 |
}.items(): |
| 1682 |
self._make_image(ext) |
| 1683 |
subtype = ext if subtype is None else subtype |
| 1684 |
self.assertEqual(self._im.get_content_type(), f'image/{subtype}') |
| 1685 |
|
| 1686 |
def test_encoding(self): |
| 1687 |
self._make_image('gif') |
| 1688 |
payload = self._im.get_payload() |
| 1689 |
self.assertEqual(base64.decodebytes(bytes(payload, 'ascii')), |
| 1690 |
self._imgdata) |
| 1691 |
|
| 1692 |
def test_checkSetMinor(self): |
| 1693 |
self._make_image('gif') |
| 1694 |
im = MIMEImage(self._imgdata, 'fish') |
| 1695 |
self.assertEqual(im.get_content_type(), 'image/fish') |
| 1696 |
|
| 1697 |
def test_add_header(self): |
| 1698 |
self._make_image('gif') |
| 1699 |
eq = self.assertEqual |
| 1700 |
self._im.add_header('Content-Disposition', 'attachment', |
| 1701 |
filename='dingusfish.gif') |
| 1702 |
eq(self._im['content-disposition'], |
| 1703 |
'attachment; filename="dingusfish.gif"') |
| 1704 |
eq(self._im.get_params(header='content-disposition'), |
| 1705 |
[('attachment', ''), ('filename', 'dingusfish.gif')]) |
| 1706 |
eq(self._im.get_param('filename', header='content-disposition'), |
| 1707 |
'dingusfish.gif') |
| 1708 |
missing = [] |
| 1709 |
eq(self._im.get_param('attachment', header='content-disposition'), '') |
| 1710 |
self.assertIs(self._im.get_param('foo', failobj=missing, |
| 1711 |
header='content-disposition'), missing) |
| 1712 |
# Try some missing stuff |
| 1713 |
self.assertIs(self._im.get_param('foobar', missing), missing) |
| 1714 |
self.assertIs(self._im.get_param('attachment', missing, |
| 1715 |
header='foobar'), missing) |
| 1716 |
|
| 1717 |
|
| 1718 |
# Test the basic MIMEApplication class |
| 1719 |
class TestMIMEApplication(unittest.TestCase): |
| 1720 |
def test_headers(self): |
| 1721 |
eq = self.assertEqual |
| 1722 |
msg = MIMEApplication(b'\xfa\xfb\xfc\xfd\xfe\xff') |
| 1723 |
eq(msg.get_content_type(), 'application/octet-stream') |
| 1724 |
eq(msg['content-transfer-encoding'], 'base64') |
| 1725 |
|
| 1726 |
def test_body(self): |
| 1727 |
eq = self.assertEqual |
| 1728 |
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' |
| 1729 |
msg = MIMEApplication(bytesdata) |
| 1730 |
# whitespace in the cte encoded block is RFC-irrelevant. |
| 1731 |
eq(msg.get_payload().strip(), '+vv8/f7/') |
| 1732 |
eq(msg.get_payload(decode=True), bytesdata) |
| 1733 |
|
| 1734 |
def test_binary_body_with_encode_7or8bit(self): |
| 1735 |
# Issue 17171. |
| 1736 |
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' |
| 1737 |
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit) |
| 1738 |
# Treated as a string, this will be invalid code points. |
| 1739 |
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata)) |
| 1740 |
self.assertEqual(msg.get_payload(decode=True), bytesdata) |
| 1741 |
self.assertEqual(msg['Content-Transfer-Encoding'], '8bit') |
| 1742 |
s = BytesIO() |
| 1743 |
g = BytesGenerator(s) |
| 1744 |
g.flatten(msg) |
| 1745 |
wireform = s.getvalue() |
| 1746 |
msg2 = email.message_from_bytes(wireform) |
| 1747 |
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata)) |
| 1748 |
self.assertEqual(msg2.get_payload(decode=True), bytesdata) |
| 1749 |
self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit') |
| 1750 |
|
| 1751 |
def test_binary_body_with_encode_noop(self): |
| 1752 |
# Issue 16564: This does not produce an RFC valid message, since to be |
| 1753 |
# valid it should have a CTE of binary. But the below works in |
| 1754 |
# Python2, and is documented as working this way. |
| 1755 |
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' |
| 1756 |
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop) |
| 1757 |
# Treated as a string, this will be invalid code points. |
| 1758 |
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata)) |
| 1759 |
self.assertEqual(msg.get_payload(decode=True), bytesdata) |
| 1760 |
s = BytesIO() |
| 1761 |
g = BytesGenerator(s) |
| 1762 |
g.flatten(msg) |
| 1763 |
wireform = s.getvalue() |
| 1764 |
msg2 = email.message_from_bytes(wireform) |
| 1765 |
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata)) |
| 1766 |
self.assertEqual(msg2.get_payload(decode=True), bytesdata) |
| 1767 |
|
| 1768 |
def test_binary_body_with_unicode_linend_encode_noop(self): |
| 1769 |
# Issue 19003: This is a variation on #16564. |
| 1770 |
bytesdata = b'\x0b\xfa\xfb\xfc\xfd\xfe\xff' |
| 1771 |
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop) |
| 1772 |
self.assertEqual(msg.get_payload(decode=True), bytesdata) |
| 1773 |
s = BytesIO() |
| 1774 |
g = BytesGenerator(s) |
| 1775 |
g.flatten(msg) |
| 1776 |
wireform = s.getvalue() |
| 1777 |
msg2 = email.message_from_bytes(wireform) |
| 1778 |
self.assertEqual(msg2.get_payload(decode=True), bytesdata) |
| 1779 |
|
| 1780 |
def test_binary_body_with_encode_quopri(self): |
| 1781 |
# Issue 14360. |
| 1782 |
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff ' |
| 1783 |
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_quopri) |
| 1784 |
self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20') |
| 1785 |
self.assertEqual(msg.get_payload(decode=True), bytesdata) |
| 1786 |
self.assertEqual(msg['Content-Transfer-Encoding'], 'quoted-printable') |
| 1787 |
s = BytesIO() |
| 1788 |
g = BytesGenerator(s) |
| 1789 |
g.flatten(msg) |
| 1790 |
wireform = s.getvalue() |
| 1791 |
msg2 = email.message_from_bytes(wireform) |
| 1792 |
self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20') |
| 1793 |
self.assertEqual(msg2.get_payload(decode=True), bytesdata) |
| 1794 |
self.assertEqual(msg2['Content-Transfer-Encoding'], 'quoted-printable') |
| 1795 |
|
| 1796 |
def test_binary_body_with_encode_base64(self): |
| 1797 |
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' |
| 1798 |
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_base64) |
| 1799 |
self.assertEqual(msg.get_payload(), '+vv8/f7/\n') |
| 1800 |
self.assertEqual(msg.get_payload(decode=True), bytesdata) |
| 1801 |
s = BytesIO() |
| 1802 |
g = BytesGenerator(s) |
| 1803 |
g.flatten(msg) |
| 1804 |
wireform = s.getvalue() |
| 1805 |
msg2 = email.message_from_bytes(wireform) |
| 1806 |
self.assertEqual(msg.get_payload(), '+vv8/f7/\n') |
| 1807 |
self.assertEqual(msg2.get_payload(decode=True), bytesdata) |
| 1808 |
|
| 1809 |
|
| 1810 |
# Test the basic MIMEText class |
| 1811 |
class TestMIMEText(unittest.TestCase): |
| 1812 |
def setUp(self): |
| 1813 |
self._msg = MIMEText('hello there') |
| 1814 |
|
| 1815 |
def test_types(self): |
| 1816 |
eq = self.assertEqual |
| 1817 |
eq(self._msg.get_content_type(), 'text/plain') |
| 1818 |
eq(self._msg.get_param('charset'), 'us-ascii') |
| 1819 |
missing = [] |
| 1820 |
self.assertIs(self._msg.get_param('foobar', missing), missing) |
| 1821 |
self.assertIs(self._msg.get_param('charset', missing, header='foobar'), |
| 1822 |
missing) |
| 1823 |
|
| 1824 |
def test_payload(self): |
| 1825 |
self.assertEqual(self._msg.get_payload(), 'hello there') |
| 1826 |
self.assertFalse(self._msg.is_multipart()) |
| 1827 |
|
| 1828 |
def test_charset(self): |
| 1829 |
eq = self.assertEqual |
| 1830 |
msg = MIMEText('hello there', _charset='us-ascii') |
| 1831 |
eq(msg.get_charset().input_charset, 'us-ascii') |
| 1832 |
eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 1833 |
# Also accept a Charset instance |
| 1834 |
charset = Charset('utf-8') |
| 1835 |
charset.body_encoding = None |
| 1836 |
msg = MIMEText('hello there', _charset=charset) |
| 1837 |
eq(msg.get_charset().input_charset, 'utf-8') |
| 1838 |
eq(msg['content-type'], 'text/plain; charset="utf-8"') |
| 1839 |
eq(msg.get_payload(), 'hello there') |
| 1840 |
|
| 1841 |
def test_7bit_input(self): |
| 1842 |
eq = self.assertEqual |
| 1843 |
msg = MIMEText('hello there', _charset='us-ascii') |
| 1844 |
eq(msg.get_charset().input_charset, 'us-ascii') |
| 1845 |
eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 1846 |
|
| 1847 |
def test_7bit_input_no_charset(self): |
| 1848 |
eq = self.assertEqual |
| 1849 |
msg = MIMEText('hello there') |
| 1850 |
eq(msg.get_charset(), 'us-ascii') |
| 1851 |
eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 1852 |
self.assertIn('hello there', msg.as_string()) |
| 1853 |
|
| 1854 |
def test_utf8_input(self): |
| 1855 |
teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430' |
| 1856 |
eq = self.assertEqual |
| 1857 |
msg = MIMEText(teststr, _charset='utf-8') |
| 1858 |
eq(msg.get_charset().output_charset, 'utf-8') |
| 1859 |
eq(msg['content-type'], 'text/plain; charset="utf-8"') |
| 1860 |
eq(msg.get_payload(decode=True), teststr.encode('utf-8')) |
| 1861 |
|
| 1862 |
@unittest.skip("can't fix because of backward compat in email5, " |
| 1863 |
"will fix in email6") |
| 1864 |
def test_utf8_input_no_charset(self): |
| 1865 |
teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430' |
| 1866 |
self.assertRaises(UnicodeEncodeError, MIMEText, teststr) |
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
# Test complicated multipart/* messages |
| 1871 |
class TestMultipart(TestEmailBase): |
| 1872 |
def setUp(self): |
| 1873 |
with openfile('python.gif', 'rb') as fp: |
| 1874 |
data = fp.read() |
| 1875 |
container = MIMEBase('multipart', 'mixed', boundary='BOUNDARY') |
| 1876 |
image = MIMEImage(data, name='dingusfish.gif') |
| 1877 |
image.add_header('content-disposition', 'attachment', |
| 1878 |
filename='dingusfish.gif') |
| 1879 |
intro = MIMEText('''\ |
| 1880 |
Hi there, |
| 1881 |
|
| 1882 |
This is the dingus fish. |
| 1883 |
''') |
| 1884 |
container.attach(intro) |
| 1885 |
container.attach(image) |
| 1886 |
container['From'] = 'Barry <[email protected]>' |
| 1887 |
container['To'] = 'Dingus Lovers <[email protected]>' |
| 1888 |
container['Subject'] = 'Here is your dingus fish' |
| 1889 |
|
| 1890 |
now = 987809702.54848599 |
| 1891 |
timetuple = time.localtime(now) |
| 1892 |
if timetuple[-1] == 0: |
| 1893 |
tzsecs = time.timezone |
| 1894 |
else: |
| 1895 |
tzsecs = time.altzone |
| 1896 |
if tzsecs > 0: |
| 1897 |
sign = '-' |
| 1898 |
else: |
| 1899 |
sign = '+' |
| 1900 |
tzoffset = ' %s%04d' % (sign, tzsecs / 36) |
| 1901 |
container['Date'] = time.strftime( |
| 1902 |
'%a, %d %b %Y %H:%M:%S', |
| 1903 |
time.localtime(now)) + tzoffset |
| 1904 |
self._msg = container |
| 1905 |
self._im = image |
| 1906 |
self._txt = intro |
| 1907 |
|
| 1908 |
def test_hierarchy(self): |
| 1909 |
# convenience |
| 1910 |
eq = self.assertEqual |
| 1911 |
raises = self.assertRaises |
| 1912 |
# tests |
| 1913 |
m = self._msg |
| 1914 |
self.assertTrue(m.is_multipart()) |
| 1915 |
eq(m.get_content_type(), 'multipart/mixed') |
| 1916 |
eq(len(m.get_payload()), 2) |
| 1917 |
raises(IndexError, m.get_payload, 2) |
| 1918 |
m0 = m.get_payload(0) |
| 1919 |
m1 = m.get_payload(1) |
| 1920 |
self.assertIs(m0, self._txt) |
| 1921 |
self.assertIs(m1, self._im) |
| 1922 |
eq(m.get_payload(), [m0, m1]) |
| 1923 |
self.assertFalse(m0.is_multipart()) |
| 1924 |
self.assertFalse(m1.is_multipart()) |
| 1925 |
|
| 1926 |
def test_empty_multipart_idempotent(self): |
| 1927 |
text = """\ |
| 1928 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1929 |
MIME-Version: 1.0 |
| 1930 |
Subject: A subject |
| 1931 |
To: [email protected] |
| 1932 |
From: [email protected] |
| 1933 |
|
| 1934 |
|
| 1935 |
--BOUNDARY |
| 1936 |
|
| 1937 |
|
| 1938 |
--BOUNDARY-- |
| 1939 |
""" |
| 1940 |
msg = Parser().parsestr(text) |
| 1941 |
self.ndiffAssertEqual(text, msg.as_string()) |
| 1942 |
|
| 1943 |
def test_no_parts_in_a_multipart_with_none_epilogue(self): |
| 1944 |
outer = MIMEBase('multipart', 'mixed') |
| 1945 |
outer['Subject'] = 'A subject' |
| 1946 |
outer['To'] = '[email protected]' |
| 1947 |
outer['From'] = '[email protected]' |
| 1948 |
outer.set_boundary('BOUNDARY') |
| 1949 |
self.ndiffAssertEqual(outer.as_string(), '''\ |
| 1950 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1951 |
MIME-Version: 1.0 |
| 1952 |
Subject: A subject |
| 1953 |
To: [email protected] |
| 1954 |
From: [email protected] |
| 1955 |
|
| 1956 |
--BOUNDARY |
| 1957 |
|
| 1958 |
--BOUNDARY-- |
| 1959 |
''') |
| 1960 |
|
| 1961 |
def test_no_parts_in_a_multipart_with_empty_epilogue(self): |
| 1962 |
outer = MIMEBase('multipart', 'mixed') |
| 1963 |
outer['Subject'] = 'A subject' |
| 1964 |
outer['To'] = '[email protected]' |
| 1965 |
outer['From'] = '[email protected]' |
| 1966 |
outer.preamble = '' |
| 1967 |
outer.epilogue = '' |
| 1968 |
outer.set_boundary('BOUNDARY') |
| 1969 |
self.ndiffAssertEqual(outer.as_string(), '''\ |
| 1970 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1971 |
MIME-Version: 1.0 |
| 1972 |
Subject: A subject |
| 1973 |
To: [email protected] |
| 1974 |
From: [email protected] |
| 1975 |
|
| 1976 |
|
| 1977 |
--BOUNDARY |
| 1978 |
|
| 1979 |
--BOUNDARY-- |
| 1980 |
''') |
| 1981 |
|
| 1982 |
def test_one_part_in_a_multipart(self): |
| 1983 |
eq = self.ndiffAssertEqual |
| 1984 |
outer = MIMEBase('multipart', 'mixed') |
| 1985 |
outer['Subject'] = 'A subject' |
| 1986 |
outer['To'] = '[email protected]' |
| 1987 |
outer['From'] = '[email protected]' |
| 1988 |
outer.set_boundary('BOUNDARY') |
| 1989 |
msg = MIMEText('hello world') |
| 1990 |
outer.attach(msg) |
| 1991 |
eq(outer.as_string(), '''\ |
| 1992 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1993 |
MIME-Version: 1.0 |
| 1994 |
Subject: A subject |
| 1995 |
To: [email protected] |
| 1996 |
From: [email protected] |
| 1997 |
|
| 1998 |
--BOUNDARY |
| 1999 |
Content-Type: text/plain; charset="us-ascii" |
| 2000 |
MIME-Version: 1.0 |
| 2001 |
Content-Transfer-Encoding: 7bit |
| 2002 |
|
| 2003 |
hello world |
| 2004 |
--BOUNDARY-- |
| 2005 |
''') |
| 2006 |
|
| 2007 |
def test_seq_parts_in_a_multipart_with_empty_preamble(self): |
| 2008 |
eq = self.ndiffAssertEqual |
| 2009 |
outer = MIMEBase('multipart', 'mixed') |
| 2010 |
outer['Subject'] = 'A subject' |
| 2011 |
outer['To'] = '[email protected]' |
| 2012 |
outer['From'] = '[email protected]' |
| 2013 |
outer.preamble = '' |
| 2014 |
msg = MIMEText('hello world') |
| 2015 |
outer.attach(msg) |
| 2016 |
outer.set_boundary('BOUNDARY') |
| 2017 |
eq(outer.as_string(), '''\ |
| 2018 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 2019 |
MIME-Version: 1.0 |
| 2020 |
Subject: A subject |
| 2021 |
To: [email protected] |
| 2022 |
From: [email protected] |
| 2023 |
|
| 2024 |
|
| 2025 |
--BOUNDARY |
| 2026 |
Content-Type: text/plain; charset="us-ascii" |
| 2027 |
MIME-Version: 1.0 |
| 2028 |
Content-Transfer-Encoding: 7bit |
| 2029 |
|
| 2030 |
hello world |
| 2031 |
--BOUNDARY-- |
| 2032 |
''') |
| 2033 |
|
| 2034 |
|
| 2035 |
def test_seq_parts_in_a_multipart_with_none_preamble(self): |
| 2036 |
eq = self.ndiffAssertEqual |
| 2037 |
outer = MIMEBase('multipart', 'mixed') |
| 2038 |
outer['Subject'] = 'A subject' |
| 2039 |
outer['To'] = '[email protected]' |
| 2040 |
outer['From'] = '[email protected]' |
| 2041 |
outer.preamble = None |
| 2042 |
msg = MIMEText('hello world') |
| 2043 |
outer.attach(msg) |
| 2044 |
outer.set_boundary('BOUNDARY') |
| 2045 |
eq(outer.as_string(), '''\ |
| 2046 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 2047 |
MIME-Version: 1.0 |
| 2048 |
Subject: A subject |
| 2049 |
To: [email protected] |
| 2050 |
From: [email protected] |
| 2051 |
|
| 2052 |
--BOUNDARY |
| 2053 |
Content-Type: text/plain; charset="us-ascii" |
| 2054 |
MIME-Version: 1.0 |
| 2055 |
Content-Transfer-Encoding: 7bit |
| 2056 |
|
| 2057 |
hello world |
| 2058 |
--BOUNDARY-- |
| 2059 |
''') |
| 2060 |
|
| 2061 |
|
| 2062 |
def test_seq_parts_in_a_multipart_with_none_epilogue(self): |
| 2063 |
eq = self.ndiffAssertEqual |
| 2064 |
outer = MIMEBase('multipart', 'mixed') |
| 2065 |
outer['Subject'] = 'A subject' |
| 2066 |
outer['To'] = '[email protected]' |
| 2067 |
outer['From'] = '[email protected]' |
| 2068 |
outer.epilogue = None |
| 2069 |
msg = MIMEText('hello world') |
| 2070 |
outer.attach(msg) |
| 2071 |
outer.set_boundary('BOUNDARY') |
| 2072 |
eq(outer.as_string(), '''\ |
| 2073 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 2074 |
MIME-Version: 1.0 |
| 2075 |
Subject: A subject |
| 2076 |
To: [email protected] |
| 2077 |
From: [email protected] |
| 2078 |
|
| 2079 |
--BOUNDARY |
| 2080 |
Content-Type: text/plain; charset="us-ascii" |
| 2081 |
MIME-Version: 1.0 |
| 2082 |
Content-Transfer-Encoding: 7bit |
| 2083 |
|
| 2084 |
hello world |
| 2085 |
--BOUNDARY-- |
| 2086 |
''') |
| 2087 |
|
| 2088 |
|
| 2089 |
def test_seq_parts_in_a_multipart_with_empty_epilogue(self): |
| 2090 |
eq = self.ndiffAssertEqual |
| 2091 |
outer = MIMEBase('multipart', 'mixed') |
| 2092 |
outer['Subject'] = 'A subject' |
| 2093 |
outer['To'] = '[email protected]' |
| 2094 |
outer['From'] = '[email protected]' |
| 2095 |
outer.epilogue = '' |
| 2096 |
msg = MIMEText('hello world') |
| 2097 |
outer.attach(msg) |
| 2098 |
outer.set_boundary('BOUNDARY') |
| 2099 |
eq(outer.as_string(), '''\ |
| 2100 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 2101 |
MIME-Version: 1.0 |
| 2102 |
Subject: A subject |
| 2103 |
To: [email protected] |
| 2104 |
From: [email protected] |
| 2105 |
|
| 2106 |
--BOUNDARY |
| 2107 |
Content-Type: text/plain; charset="us-ascii" |
| 2108 |
MIME-Version: 1.0 |
| 2109 |
Content-Transfer-Encoding: 7bit |
| 2110 |
|
| 2111 |
hello world |
| 2112 |
--BOUNDARY-- |
| 2113 |
''') |
| 2114 |
|
| 2115 |
|
| 2116 |
def test_seq_parts_in_a_multipart_with_nl_epilogue(self): |
| 2117 |
eq = self.ndiffAssertEqual |
| 2118 |
outer = MIMEBase('multipart', 'mixed') |
| 2119 |
outer['Subject'] = 'A subject' |
| 2120 |
outer['To'] = '[email protected]' |
| 2121 |
outer['From'] = '[email protected]' |
| 2122 |
outer.epilogue = '\n' |
| 2123 |
msg = MIMEText('hello world') |
| 2124 |
outer.attach(msg) |
| 2125 |
outer.set_boundary('BOUNDARY') |
| 2126 |
eq(outer.as_string(), '''\ |
| 2127 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 2128 |
MIME-Version: 1.0 |
| 2129 |
Subject: A subject |
| 2130 |
To: [email protected] |
| 2131 |
From: [email protected] |
| 2132 |
|
| 2133 |
--BOUNDARY |
| 2134 |
Content-Type: text/plain; charset="us-ascii" |
| 2135 |
MIME-Version: 1.0 |
| 2136 |
Content-Transfer-Encoding: 7bit |
| 2137 |
|
| 2138 |
hello world |
| 2139 |
--BOUNDARY-- |
| 2140 |
|
| 2141 |
''') |
| 2142 |
|
| 2143 |
def test_message_external_body(self): |
| 2144 |
eq = self.assertEqual |
| 2145 |
msg = self._msgobj('msg_36.txt') |
| 2146 |
eq(len(msg.get_payload()), 2) |
| 2147 |
msg1 = msg.get_payload(1) |
| 2148 |
eq(msg1.get_content_type(), 'multipart/alternative') |
| 2149 |
eq(len(msg1.get_payload()), 2) |
| 2150 |
for subpart in msg1.get_payload(): |
| 2151 |
eq(subpart.get_content_type(), 'message/external-body') |
| 2152 |
eq(len(subpart.get_payload()), 1) |
| 2153 |
subsubpart = subpart.get_payload(0) |
| 2154 |
eq(subsubpart.get_content_type(), 'text/plain') |
| 2155 |
|
| 2156 |
def test_double_boundary(self): |
| 2157 |
# msg_37.txt is a multipart that contains two dash-boundary's in a |
| 2158 |
# row. Our interpretation of RFC 2046 calls for ignoring the second |
| 2159 |
# and subsequent boundaries. |
| 2160 |
msg = self._msgobj('msg_37.txt') |
| 2161 |
self.assertEqual(len(msg.get_payload()), 3) |
| 2162 |
|
| 2163 |
def test_nested_inner_contains_outer_boundary(self): |
| 2164 |
eq = self.ndiffAssertEqual |
| 2165 |
# msg_38.txt has an inner part that contains outer boundaries. My |
| 2166 |
# interpretation of RFC 2046 (based on sections 5.1 and 5.1.2) say |
| 2167 |
# these are illegal and should be interpreted as unterminated inner |
| 2168 |
# parts. |
| 2169 |
msg = self._msgobj('msg_38.txt') |
| 2170 |
sfp = StringIO() |
| 2171 |
iterators._structure(msg, sfp) |
| 2172 |
eq(sfp.getvalue(), """\ |
| 2173 |
multipart/mixed |
| 2174 |
multipart/mixed |
| 2175 |
multipart/alternative |
| 2176 |
text/plain |
| 2177 |
text/plain |
| 2178 |
text/plain |
| 2179 |
text/plain |
| 2180 |
""") |
| 2181 |
|
| 2182 |
def test_nested_with_same_boundary(self): |
| 2183 |
eq = self.ndiffAssertEqual |
| 2184 |
# msg 39.txt is similarly evil in that it's got inner parts that use |
| 2185 |
# the same boundary as outer parts. Again, I believe the way this is |
| 2186 |
# parsed is closest to the spirit of RFC 2046 |
| 2187 |
msg = self._msgobj('msg_39.txt') |
| 2188 |
sfp = StringIO() |
| 2189 |
iterators._structure(msg, sfp) |
| 2190 |
eq(sfp.getvalue(), """\ |
| 2191 |
multipart/mixed |
| 2192 |
multipart/mixed |
| 2193 |
multipart/alternative |
| 2194 |
application/octet-stream |
| 2195 |
application/octet-stream |
| 2196 |
text/plain |
| 2197 |
""") |
| 2198 |
|
| 2199 |
def test_boundary_in_non_multipart(self): |
| 2200 |
msg = self._msgobj('msg_40.txt') |
| 2201 |
self.assertEqual(msg.as_string(), '''\ |
| 2202 |
MIME-Version: 1.0 |
| 2203 |
Content-Type: text/html; boundary="--961284236552522269" |
| 2204 |
|
| 2205 |
----961284236552522269 |
| 2206 |
Content-Type: text/html; |
| 2207 |
Content-Transfer-Encoding: 7Bit |
| 2208 |
|
| 2209 |
<html></html> |
| 2210 |
|
| 2211 |
----961284236552522269-- |
| 2212 |
''') |
| 2213 |
|
| 2214 |
def test_boundary_with_leading_space(self): |
| 2215 |
eq = self.assertEqual |
| 2216 |
msg = email.message_from_string('''\ |
| 2217 |
MIME-Version: 1.0 |
| 2218 |
Content-Type: multipart/mixed; boundary=" XXXX" |
| 2219 |
|
| 2220 |
-- XXXX |
| 2221 |
Content-Type: text/plain |
| 2222 |
|
| 2223 |
|
| 2224 |
-- XXXX |
| 2225 |
Content-Type: text/plain |
| 2226 |
|
| 2227 |
-- XXXX-- |
| 2228 |
''') |
| 2229 |
self.assertTrue(msg.is_multipart()) |
| 2230 |
eq(msg.get_boundary(), ' XXXX') |
| 2231 |
eq(len(msg.get_payload()), 2) |
| 2232 |
|
| 2233 |
def test_boundary_without_trailing_newline(self): |
| 2234 |
m = Parser().parsestr("""\ |
| 2235 |
Content-Type: multipart/mixed; boundary="===============0012394164==" |
| 2236 |
MIME-Version: 1.0 |
| 2237 |
|
| 2238 |
--===============0012394164== |
| 2239 |
Content-Type: image/file1.jpg |
| 2240 |
MIME-Version: 1.0 |
| 2241 |
Content-Transfer-Encoding: base64 |
| 2242 |
|
| 2243 |
YXNkZg== |
| 2244 |
--===============0012394164==--""") |
| 2245 |
self.assertEqual(m.get_payload(0).get_payload(), 'YXNkZg==') |
| 2246 |
|
| 2247 |
def test_mimebase_default_policy(self): |
| 2248 |
m = MIMEBase('multipart', 'mixed') |
| 2249 |
self.assertIs(m.policy, email.policy.compat32) |
| 2250 |
|
| 2251 |
def test_mimebase_custom_policy(self): |
| 2252 |
m = MIMEBase('multipart', 'mixed', policy=email.policy.default) |
| 2253 |
self.assertIs(m.policy, email.policy.default) |
| 2254 |
|
| 2255 |
# Test some badly formatted messages |
| 2256 |
class TestNonConformant(TestEmailBase): |
| 2257 |
|
| 2258 |
def test_parse_missing_minor_type(self): |
| 2259 |
eq = self.assertEqual |
| 2260 |
msg = self._msgobj('msg_14.txt') |
| 2261 |
eq(msg.get_content_type(), 'text/plain') |
| 2262 |
eq(msg.get_content_maintype(), 'text') |
| 2263 |
eq(msg.get_content_subtype(), 'plain') |
| 2264 |
|
| 2265 |
# test_headerregistry.TestContentTypeHeader invalid_1 and invalid_2. |
| 2266 |
def test_invalid_content_type(self): |
| 2267 |
eq = self.assertEqual |
| 2268 |
neq = self.ndiffAssertEqual |
| 2269 |
msg = Message() |
| 2270 |
# RFC 2045, $5.2 says invalid yields text/plain |
| 2271 |
msg['Content-Type'] = 'text' |
| 2272 |
eq(msg.get_content_maintype(), 'text') |
| 2273 |
eq(msg.get_content_subtype(), 'plain') |
| 2274 |
eq(msg.get_content_type(), 'text/plain') |
| 2275 |
# Clear the old value and try something /really/ invalid |
| 2276 |
del msg['content-type'] |
| 2277 |
msg['Content-Type'] = 'foo' |
| 2278 |
eq(msg.get_content_maintype(), 'text') |
| 2279 |
eq(msg.get_content_subtype(), 'plain') |
| 2280 |
eq(msg.get_content_type(), 'text/plain') |
| 2281 |
# Still, make sure that the message is idempotently generated |
| 2282 |
s = StringIO() |
| 2283 |
g = Generator(s) |
| 2284 |
g.flatten(msg) |
| 2285 |
neq(s.getvalue(), 'Content-Type: foo\n\n') |
| 2286 |
|
| 2287 |
def test_no_start_boundary(self): |
| 2288 |
eq = self.ndiffAssertEqual |
| 2289 |
msg = self._msgobj('msg_31.txt') |
| 2290 |
eq(msg.get_payload(), """\ |
| 2291 |
--BOUNDARY |
| 2292 |
Content-Type: text/plain |
| 2293 |
|
| 2294 |
message 1 |
| 2295 |
|
| 2296 |
--BOUNDARY |
| 2297 |
Content-Type: text/plain |
| 2298 |
|
| 2299 |
message 2 |
| 2300 |
|
| 2301 |
--BOUNDARY-- |
| 2302 |
""") |
| 2303 |
|
| 2304 |
def test_no_separating_blank_line(self): |
| 2305 |
eq = self.ndiffAssertEqual |
| 2306 |
msg = self._msgobj('msg_35.txt') |
| 2307 |
eq(msg.as_string(), """\ |
| 2308 |
From: [email protected] |
| 2309 |
To: [email protected] |
| 2310 |
Subject: here's something interesting |
| 2311 |
|
| 2312 |
counter to RFC 5322, there's no separating newline here |
| 2313 |
""") |
| 2314 |
|
| 2315 |
# test_defect_handling |
| 2316 |
def test_lying_multipart(self): |
| 2317 |
msg = self._msgobj('msg_41.txt') |
| 2318 |
self.assertHasAttr(msg, 'defects') |
| 2319 |
self.assertEqual(len(msg.defects), 2) |
| 2320 |
self.assertIsInstance(msg.defects[0], |
| 2321 |
errors.NoBoundaryInMultipartDefect) |
| 2322 |
self.assertIsInstance(msg.defects[1], |
| 2323 |
errors.MultipartInvariantViolationDefect) |
| 2324 |
|
| 2325 |
# test_defect_handling |
| 2326 |
def test_missing_start_boundary(self): |
| 2327 |
outer = self._msgobj('msg_42.txt') |
| 2328 |
# The message structure is: |
| 2329 |
# |
| 2330 |
# multipart/mixed |
| 2331 |
# text/plain |
| 2332 |
# message/rfc822 |
| 2333 |
# multipart/mixed [*] |
| 2334 |
# |
| 2335 |
# [*] This message is missing its start boundary |
| 2336 |
bad = outer.get_payload(1).get_payload(0) |
| 2337 |
self.assertEqual(len(bad.defects), 1) |
| 2338 |
self.assertIsInstance(bad.defects[0], |
| 2339 |
errors.StartBoundaryNotFoundDefect) |
| 2340 |
|
| 2341 |
def test_string_payload_with_extra_space_after_cte(self): |
| 2342 |
# https://github.com/python/cpython/issues/98188 |
| 2343 |
cte = "base64 " |
| 2344 |
msg = email.message_from_string(textwrap.dedent(f"""\ |
| 2345 |
Content-Transfer-Encoding: {cte} |
| 2346 |
|
| 2347 |
SGVsbG8uIFRlc3Rpbmc= |
| 2348 |
"""), policy=email.policy.default) |
| 2349 |
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing") |
| 2350 |
self.assertDefectsEqual(msg['content-transfer-encoding'].defects, []) |
| 2351 |
|
| 2352 |
def test_string_payload_with_extra_text_after_cte(self): |
| 2353 |
msg = email.message_from_string(textwrap.dedent("""\ |
| 2354 |
Content-Transfer-Encoding: base64 some text |
| 2355 |
|
| 2356 |
SGVsbG8uIFRlc3Rpbmc= |
| 2357 |
"""), policy=email.policy.default) |
| 2358 |
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing") |
| 2359 |
cte = msg['content-transfer-encoding'] |
| 2360 |
self.assertDefectsEqual(cte.defects, [email.errors.InvalidHeaderDefect]) |
| 2361 |
|
| 2362 |
def test_string_payload_with_extra_space_after_cte_compat32(self): |
| 2363 |
cte = "base64 " |
| 2364 |
msg = email.message_from_string(textwrap.dedent(f"""\ |
| 2365 |
Content-Transfer-Encoding: {cte} |
| 2366 |
|
| 2367 |
SGVsbG8uIFRlc3Rpbmc= |
| 2368 |
"""), policy=email.policy.compat32) |
| 2369 |
pasted_cte = msg['content-transfer-encoding'] |
| 2370 |
self.assertEqual(pasted_cte, cte) |
| 2371 |
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing") |
| 2372 |
self.assertDefectsEqual(msg.defects, []) |
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
# Test RFC 2047 header encoding and decoding |
| 2377 |
class TestRFC2047(TestEmailBase): |
| 2378 |
def test_rfc2047_multiline(self): |
| 2379 |
eq = self.assertEqual |
| 2380 |
s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz |
| 2381 |
foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""" |
| 2382 |
dh = decode_header(s) |
| 2383 |
eq(dh, [ |
| 2384 |
(b'Re: ', None), |
| 2385 |
(b'r\x8aksm\x9arg\x8cs', 'mac-iceland'), |
| 2386 |
(b' baz foo bar ', None), |
| 2387 |
(b'r\x8aksm\x9arg\x8cs', 'mac-iceland')]) |
| 2388 |
header = make_header(dh) |
| 2389 |
eq(str(header), |
| 2390 |
'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s') |
| 2391 |
self.ndiffAssertEqual(header.encode(maxlinelen=76), """\ |
| 2392 |
Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?= |
| 2393 |
=?mac-iceland?q?=9Arg=8Cs?=""") |
| 2394 |
|
| 2395 |
def test_whitespace_keeper_unicode(self): |
| 2396 |
eq = self.assertEqual |
| 2397 |
s = '=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>' |
| 2398 |
dh = decode_header(s) |
| 2399 |
eq(dh, [(b'Andr\xe9', 'iso-8859-1'), |
| 2400 |
(b' Pirard <[email protected]>', None)]) |
| 2401 |
header = str(make_header(dh)) |
| 2402 |
eq(header, 'Andr\xe9 Pirard <[email protected]>') |
| 2403 |
|
| 2404 |
def test_whitespace_keeper_unicode_2(self): |
| 2405 |
eq = self.assertEqual |
| 2406 |
s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?=' |
| 2407 |
dh = decode_header(s) |
| 2408 |
eq(dh, [(b'The ', None), (b'quick brown fox', 'iso-8859-1'), |
| 2409 |
(b' jumped over the ', None), (b'lazy dog', 'iso-8859-1')]) |
| 2410 |
hu = str(make_header(dh)) |
| 2411 |
eq(hu, 'The quick brown fox jumped over the lazy dog') |
| 2412 |
|
| 2413 |
def test_rfc2047_missing_whitespace(self): |
| 2414 |
s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord' |
| 2415 |
dh = decode_header(s) |
| 2416 |
self.assertEqual(dh, [(b'Sm', None), (b'\xf6', 'iso-8859-1'), |
| 2417 |
(b'rg', None), (b'\xe5', 'iso-8859-1'), |
| 2418 |
(b'sbord', None)]) |
| 2419 |
|
| 2420 |
def test_rfc2047_with_whitespace(self): |
| 2421 |
s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord' |
| 2422 |
dh = decode_header(s) |
| 2423 |
self.assertEqual(dh, [(b'Sm ', None), (b'\xf6', 'iso-8859-1'), |
| 2424 |
(b' rg ', None), (b'\xe5', 'iso-8859-1'), |
| 2425 |
(b' sbord', None)]) |
| 2426 |
|
| 2427 |
def test_rfc2047_B_bad_padding(self): |
| 2428 |
s = '=?iso-8859-1?B?%s?=' |
| 2429 |
data = [ # only test complete bytes |
| 2430 |
('dm==', b'v'), ('dm=', b'v'), ('dm', b'v'), |
| 2431 |
('dmk=', b'vi'), ('dmk', b'vi') |
| 2432 |
] |
| 2433 |
for q, a in data: |
| 2434 |
dh = decode_header(s % q) |
| 2435 |
self.assertEqual(dh, [(a, 'iso-8859-1')]) |
| 2436 |
|
| 2437 |
def test_rfc2047_Q_invalid_digits(self): |
| 2438 |
# issue 10004. |
| 2439 |
s = '=?iso-8859-1?Q?andr=e9=zz?=' |
| 2440 |
self.assertEqual(decode_header(s), |
| 2441 |
[(b'andr\xe9=zz', 'iso-8859-1')]) |
| 2442 |
|
| 2443 |
def test_rfc2047_rfc2047_1(self): |
| 2444 |
# 1st testcase at end of RFC 2047 |
| 2445 |
s = '(=?ISO-8859-1?Q?a?=)' |
| 2446 |
self.assertEqual(decode_header(s), |
| 2447 |
[(b'(', None), (b'a', 'iso-8859-1'), (b')', None)]) |
| 2448 |
|
| 2449 |
def test_rfc2047_rfc2047_2(self): |
| 2450 |
# 2nd testcase at end of RFC 2047 |
| 2451 |
s = '(=?ISO-8859-1?Q?a?= b)' |
| 2452 |
self.assertEqual(decode_header(s), |
| 2453 |
[(b'(', None), (b'a', 'iso-8859-1'), (b' b)', None)]) |
| 2454 |
|
| 2455 |
def test_rfc2047_rfc2047_3(self): |
| 2456 |
# 3rd testcase at end of RFC 2047 |
| 2457 |
s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)' |
| 2458 |
self.assertEqual(decode_header(s), |
| 2459 |
[(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) |
| 2460 |
|
| 2461 |
def test_rfc2047_rfc2047_4(self): |
| 2462 |
# 4th testcase at end of RFC 2047 |
| 2463 |
s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)' |
| 2464 |
self.assertEqual(decode_header(s), |
| 2465 |
[(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) |
| 2466 |
|
| 2467 |
def test_rfc2047_rfc2047_5a(self): |
| 2468 |
# 5th testcase at end of RFC 2047 newline is \r\n |
| 2469 |
s = '(=?ISO-8859-1?Q?a?=\r\n =?ISO-8859-1?Q?b?=)' |
| 2470 |
self.assertEqual(decode_header(s), |
| 2471 |
[(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) |
| 2472 |
|
| 2473 |
def test_rfc2047_rfc2047_5b(self): |
| 2474 |
# 5th testcase at end of RFC 2047 newline is \n |
| 2475 |
s = '(=?ISO-8859-1?Q?a?=\n =?ISO-8859-1?Q?b?=)' |
| 2476 |
self.assertEqual(decode_header(s), |
| 2477 |
[(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) |
| 2478 |
|
| 2479 |
def test_rfc2047_rfc2047_6(self): |
| 2480 |
# 6th testcase at end of RFC 2047 |
| 2481 |
s = '(=?ISO-8859-1?Q?a_b?=)' |
| 2482 |
self.assertEqual(decode_header(s), |
| 2483 |
[(b'(', None), (b'a b', 'iso-8859-1'), (b')', None)]) |
| 2484 |
|
| 2485 |
def test_rfc2047_rfc2047_7(self): |
| 2486 |
# 7th testcase at end of RFC 2047 |
| 2487 |
s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=)' |
| 2488 |
self.assertEqual(decode_header(s), |
| 2489 |
[(b'(', None), (b'a', 'iso-8859-1'), (b' b', 'iso-8859-2'), |
| 2490 |
(b')', None)]) |
| 2491 |
self.assertEqual(make_header(decode_header(s)).encode(), s.lower()) |
| 2492 |
self.assertEqual(str(make_header(decode_header(s))), '(a b)') |
| 2493 |
|
| 2494 |
def test_multiline_header(self): |
| 2495 |
s = '=?windows-1252?q?=22M=FCller_T=22?=\r\n <[email protected]>' |
| 2496 |
self.assertEqual(decode_header(s), |
| 2497 |
[(b'"M\xfcller T"', 'windows-1252'), |
| 2498 |
(b'<[email protected]>', None)]) |
| 2499 |
self.assertEqual(make_header(decode_header(s)).encode(), |
| 2500 |
''.join(s.splitlines())) |
| 2501 |
self.assertEqual(str(make_header(decode_header(s))), |
| 2502 |
'"Müller T" <[email protected]>') |
| 2503 |
|
| 2504 |
def test_unencoded_ascii(self): |
| 2505 |
# bpo-22833/gh-67022: returns [(str, None)] rather than [(bytes, None)] |
| 2506 |
s = 'header without encoded words' |
| 2507 |
self.assertEqual(decode_header(s), |
| 2508 |
[('header without encoded words', None)]) |
| 2509 |
|
| 2510 |
def test_unencoded_utf8(self): |
| 2511 |
# bpo-22833/gh-67022: returns [(str, None)] rather than [(bytes, None)] |
| 2512 |
s = 'header with unexpected non ASCII caract\xe8res' |
| 2513 |
self.assertEqual(decode_header(s), |
| 2514 |
[('header with unexpected non ASCII caract\xe8res', None)]) |
| 2515 |
|
| 2516 |
|
| 2517 |
# Test the MIMEMessage class |
| 2518 |
class TestMIMEMessage(TestEmailBase): |
| 2519 |
def setUp(self): |
| 2520 |
with openfile('msg_11.txt', encoding="utf-8") as fp: |
| 2521 |
self._text = fp.read() |
| 2522 |
|
| 2523 |
def test_type_error(self): |
| 2524 |
self.assertRaises(TypeError, MIMEMessage, 'a plain string') |
| 2525 |
|
| 2526 |
def test_valid_argument(self): |
| 2527 |
eq = self.assertEqual |
| 2528 |
subject = 'A sub-message' |
| 2529 |
m = Message() |
| 2530 |
m['Subject'] = subject |
| 2531 |
r = MIMEMessage(m) |
| 2532 |
eq(r.get_content_type(), 'message/rfc822') |
| 2533 |
payload = r.get_payload() |
| 2534 |
self.assertIsInstance(payload, list) |
| 2535 |
eq(len(payload), 1) |
| 2536 |
subpart = payload[0] |
| 2537 |
self.assertIs(subpart, m) |
| 2538 |
eq(subpart['subject'], subject) |
| 2539 |
|
| 2540 |
def test_bad_multipart(self): |
| 2541 |
msg1 = Message() |
| 2542 |
msg1['Subject'] = 'subpart 1' |
| 2543 |
msg2 = Message() |
| 2544 |
msg2['Subject'] = 'subpart 2' |
| 2545 |
r = MIMEMessage(msg1) |
| 2546 |
self.assertRaises(errors.MultipartConversionError, r.attach, msg2) |
| 2547 |
|
| 2548 |
def test_generate(self): |
| 2549 |
# First craft the message to be encapsulated |
| 2550 |
m = Message() |
| 2551 |
m['Subject'] = 'An enclosed message' |
| 2552 |
m.set_payload('Here is the body of the message.\n') |
| 2553 |
r = MIMEMessage(m) |
| 2554 |
r['Subject'] = 'The enclosing message' |
| 2555 |
s = StringIO() |
| 2556 |
g = Generator(s) |
| 2557 |
g.flatten(r) |
| 2558 |
self.assertEqual(s.getvalue(), """\ |
| 2559 |
Content-Type: message/rfc822 |
| 2560 |
MIME-Version: 1.0 |
| 2561 |
Subject: The enclosing message |
| 2562 |
|
| 2563 |
Subject: An enclosed message |
| 2564 |
|
| 2565 |
Here is the body of the message. |
| 2566 |
""") |
| 2567 |
|
| 2568 |
def test_parse_message_rfc822(self): |
| 2569 |
eq = self.assertEqual |
| 2570 |
msg = self._msgobj('msg_11.txt') |
| 2571 |
eq(msg.get_content_type(), 'message/rfc822') |
| 2572 |
payload = msg.get_payload() |
| 2573 |
self.assertIsInstance(payload, list) |
| 2574 |
eq(len(payload), 1) |
| 2575 |
submsg = payload[0] |
| 2576 |
self.assertIsInstance(submsg, Message) |
| 2577 |
eq(submsg['subject'], 'An enclosed message') |
| 2578 |
eq(submsg.get_payload(), 'Here is the body of the message.\n') |
| 2579 |
|
| 2580 |
def test_dsn(self): |
| 2581 |
eq = self.assertEqual |
| 2582 |
# msg 16 is a Delivery Status Notification, see RFC 1894 |
| 2583 |
msg = self._msgobj('msg_16.txt') |
| 2584 |
eq(msg.get_content_type(), 'multipart/report') |
| 2585 |
self.assertTrue(msg.is_multipart()) |
| 2586 |
eq(len(msg.get_payload()), 3) |
| 2587 |
# Subpart 1 is a text/plain, human readable section |
| 2588 |
subpart = msg.get_payload(0) |
| 2589 |
eq(subpart.get_content_type(), 'text/plain') |
| 2590 |
eq(subpart.get_payload(), """\ |
| 2591 |
This report relates to a message you sent with the following header fields: |
| 2592 |
|
| 2593 |
Message-id: <[email protected]> |
| 2594 |
Date: Sun, 23 Sep 2001 20:10:55 -0700 |
| 2595 |
From: "Ian T. Henry" <[email protected]> |
| 2596 |
To: SoCal Raves <[email protected]> |
| 2597 |
Subject: [scr] yeah for Ians!! |
| 2598 |
|
| 2599 |
Your message cannot be delivered to the following recipients: |
| 2600 |
|
| 2601 |
Recipient address: [email protected] |
| 2602 |
Reason: recipient reached disk quota |
| 2603 |
|
| 2604 |
""") |
| 2605 |
# Subpart 2 contains the machine parsable DSN information. It |
| 2606 |
# consists of two blocks of headers, represented by two nested Message |
| 2607 |
# objects. |
| 2608 |
subpart = msg.get_payload(1) |
| 2609 |
eq(subpart.get_content_type(), 'message/delivery-status') |
| 2610 |
eq(len(subpart.get_payload()), 2) |
| 2611 |
# message/delivery-status should treat each block as a bunch of |
| 2612 |
# headers, i.e. a bunch of Message objects. |
| 2613 |
dsn1 = subpart.get_payload(0) |
| 2614 |
self.assertIsInstance(dsn1, Message) |
| 2615 |
eq(dsn1['original-envelope-id'], '[email protected]') |
| 2616 |
eq(dsn1.get_param('dns', header='reporting-mta'), '') |
| 2617 |
# Try a missing one <wink> |
| 2618 |
eq(dsn1.get_param('nsd', header='reporting-mta'), None) |
| 2619 |
dsn2 = subpart.get_payload(1) |
| 2620 |
self.assertIsInstance(dsn2, Message) |
| 2621 |
eq(dsn2['action'], 'failed') |
| 2622 |
eq(dsn2.get_params(header='original-recipient'), |
| 2623 |
[('rfc822', ''), ('[email protected]', '')]) |
| 2624 |
eq(dsn2.get_param('rfc822', header='final-recipient'), '') |
| 2625 |
# Subpart 3 is the original message |
| 2626 |
subpart = msg.get_payload(2) |
| 2627 |
eq(subpart.get_content_type(), 'message/rfc822') |
| 2628 |
payload = subpart.get_payload() |
| 2629 |
self.assertIsInstance(payload, list) |
| 2630 |
eq(len(payload), 1) |
| 2631 |
subsubpart = payload[0] |
| 2632 |
self.assertIsInstance(subsubpart, Message) |
| 2633 |
eq(subsubpart.get_content_type(), 'text/plain') |
| 2634 |
eq(subsubpart['message-id'], |
| 2635 |
'<[email protected]>') |
| 2636 |
|
| 2637 |
def test_epilogue(self): |
| 2638 |
eq = self.ndiffAssertEqual |
| 2639 |
with openfile('msg_21.txt', encoding="utf-8") as fp: |
| 2640 |
text = fp.read() |
| 2641 |
msg = Message() |
| 2642 |
msg['From'] = '[email protected]' |
| 2643 |
msg['To'] = '[email protected]' |
| 2644 |
msg['Subject'] = 'Test' |
| 2645 |
msg.preamble = 'MIME message' |
| 2646 |
msg.epilogue = 'End of MIME message\n' |
| 2647 |
msg1 = MIMEText('One') |
| 2648 |
msg2 = MIMEText('Two') |
| 2649 |
msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY') |
| 2650 |
msg.attach(msg1) |
| 2651 |
msg.attach(msg2) |
| 2652 |
sfp = StringIO() |
| 2653 |
g = Generator(sfp) |
| 2654 |
g.flatten(msg) |
| 2655 |
eq(sfp.getvalue(), text) |
| 2656 |
|
| 2657 |
def test_no_nl_preamble(self): |
| 2658 |
eq = self.ndiffAssertEqual |
| 2659 |
msg = Message() |
| 2660 |
msg['From'] = '[email protected]' |
| 2661 |
msg['To'] = '[email protected]' |
| 2662 |
msg['Subject'] = 'Test' |
| 2663 |
msg.preamble = 'MIME message' |
| 2664 |
msg.epilogue = '' |
| 2665 |
msg1 = MIMEText('One') |
| 2666 |
msg2 = MIMEText('Two') |
| 2667 |
msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY') |
| 2668 |
msg.attach(msg1) |
| 2669 |
msg.attach(msg2) |
| 2670 |
eq(msg.as_string(), """\ |
| 2671 |
From: [email protected] |
| 2672 |
To: [email protected] |
| 2673 |
Subject: Test |
| 2674 |
Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 2675 |
|
| 2676 |
MIME message |
| 2677 |
--BOUNDARY |
| 2678 |
Content-Type: text/plain; charset="us-ascii" |
| 2679 |
MIME-Version: 1.0 |
| 2680 |
Content-Transfer-Encoding: 7bit |
| 2681 |
|
| 2682 |
One |
| 2683 |
--BOUNDARY |
| 2684 |
Content-Type: text/plain; charset="us-ascii" |
| 2685 |
MIME-Version: 1.0 |
| 2686 |
Content-Transfer-Encoding: 7bit |
| 2687 |
|
| 2688 |
Two |
| 2689 |
--BOUNDARY-- |
| 2690 |
""") |
| 2691 |
|
| 2692 |
def test_default_type(self): |
| 2693 |
eq = self.assertEqual |
| 2694 |
with openfile('msg_30.txt', encoding="utf-8") as fp: |
| 2695 |
msg = email.message_from_file(fp) |
| 2696 |
container1 = msg.get_payload(0) |
| 2697 |
eq(container1.get_default_type(), 'message/rfc822') |
| 2698 |
eq(container1.get_content_type(), 'message/rfc822') |
| 2699 |
container2 = msg.get_payload(1) |
| 2700 |
eq(container2.get_default_type(), 'message/rfc822') |
| 2701 |
eq(container2.get_content_type(), 'message/rfc822') |
| 2702 |
container1a = container1.get_payload(0) |
| 2703 |
eq(container1a.get_default_type(), 'text/plain') |
| 2704 |
eq(container1a.get_content_type(), 'text/plain') |
| 2705 |
container2a = container2.get_payload(0) |
| 2706 |
eq(container2a.get_default_type(), 'text/plain') |
| 2707 |
eq(container2a.get_content_type(), 'text/plain') |
| 2708 |
|
| 2709 |
def test_default_type_with_explicit_container_type(self): |
| 2710 |
eq = self.assertEqual |
| 2711 |
with openfile('msg_28.txt', encoding="utf-8") as fp: |
| 2712 |
msg = email.message_from_file(fp) |
| 2713 |
container1 = msg.get_payload(0) |
| 2714 |
eq(container1.get_default_type(), 'message/rfc822') |
| 2715 |
eq(container1.get_content_type(), 'message/rfc822') |
| 2716 |
container2 = msg.get_payload(1) |
| 2717 |
eq(container2.get_default_type(), 'message/rfc822') |
| 2718 |
eq(container2.get_content_type(), 'message/rfc822') |
| 2719 |
container1a = container1.get_payload(0) |
| 2720 |
eq(container1a.get_default_type(), 'text/plain') |
| 2721 |
eq(container1a.get_content_type(), 'text/plain') |
| 2722 |
container2a = container2.get_payload(0) |
| 2723 |
eq(container2a.get_default_type(), 'text/plain') |
| 2724 |
eq(container2a.get_content_type(), 'text/plain') |
| 2725 |
|
| 2726 |
def test_default_type_non_parsed(self): |
| 2727 |
eq = self.assertEqual |
| 2728 |
neq = self.ndiffAssertEqual |
| 2729 |
# Set up container |
| 2730 |
container = MIMEMultipart('digest', 'BOUNDARY') |
| 2731 |
container.epilogue = '' |
| 2732 |
# Set up subparts |
| 2733 |
subpart1a = MIMEText('message 1\n') |
| 2734 |
subpart2a = MIMEText('message 2\n') |
| 2735 |
subpart1 = MIMEMessage(subpart1a) |
| 2736 |
subpart2 = MIMEMessage(subpart2a) |
| 2737 |
container.attach(subpart1) |
| 2738 |
container.attach(subpart2) |
| 2739 |
eq(subpart1.get_content_type(), 'message/rfc822') |
| 2740 |
eq(subpart1.get_default_type(), 'message/rfc822') |
| 2741 |
eq(subpart2.get_content_type(), 'message/rfc822') |
| 2742 |
eq(subpart2.get_default_type(), 'message/rfc822') |
| 2743 |
neq(container.as_string(0), '''\ |
| 2744 |
Content-Type: multipart/digest; boundary="BOUNDARY" |
| 2745 |
MIME-Version: 1.0 |
| 2746 |
|
| 2747 |
--BOUNDARY |
| 2748 |
Content-Type: message/rfc822 |
| 2749 |
MIME-Version: 1.0 |
| 2750 |
|
| 2751 |
Content-Type: text/plain; charset="us-ascii" |
| 2752 |
MIME-Version: 1.0 |
| 2753 |
Content-Transfer-Encoding: 7bit |
| 2754 |
|
| 2755 |
message 1 |
| 2756 |
|
| 2757 |
--BOUNDARY |
| 2758 |
Content-Type: message/rfc822 |
| 2759 |
MIME-Version: 1.0 |
| 2760 |
|
| 2761 |
Content-Type: text/plain; charset="us-ascii" |
| 2762 |
MIME-Version: 1.0 |
| 2763 |
Content-Transfer-Encoding: 7bit |
| 2764 |
|
| 2765 |
message 2 |
| 2766 |
|
| 2767 |
--BOUNDARY-- |
| 2768 |
''') |
| 2769 |
del subpart1['content-type'] |
| 2770 |
del subpart1['mime-version'] |
| 2771 |
del subpart2['content-type'] |
| 2772 |
del subpart2['mime-version'] |
| 2773 |
eq(subpart1.get_content_type(), 'message/rfc822') |
| 2774 |
eq(subpart1.get_default_type(), 'message/rfc822') |
| 2775 |
eq(subpart2.get_content_type(), 'message/rfc822') |
| 2776 |
eq(subpart2.get_default_type(), 'message/rfc822') |
| 2777 |
neq(container.as_string(0), '''\ |
| 2778 |
Content-Type: multipart/digest; boundary="BOUNDARY" |
| 2779 |
MIME-Version: 1.0 |
| 2780 |
|
| 2781 |
--BOUNDARY |
| 2782 |
|
| 2783 |
Content-Type: text/plain; charset="us-ascii" |
| 2784 |
MIME-Version: 1.0 |
| 2785 |
Content-Transfer-Encoding: 7bit |
| 2786 |
|
| 2787 |
message 1 |
| 2788 |
|
| 2789 |
--BOUNDARY |
| 2790 |
|
| 2791 |
Content-Type: text/plain; charset="us-ascii" |
| 2792 |
MIME-Version: 1.0 |
| 2793 |
Content-Transfer-Encoding: 7bit |
| 2794 |
|
| 2795 |
message 2 |
| 2796 |
|
| 2797 |
--BOUNDARY-- |
| 2798 |
''') |
| 2799 |
|
| 2800 |
def test_mime_attachments_in_constructor(self): |
| 2801 |
eq = self.assertEqual |
| 2802 |
text1 = MIMEText('') |
| 2803 |
text2 = MIMEText('') |
| 2804 |
msg = MIMEMultipart(_subparts=(text1, text2)) |
| 2805 |
eq(len(msg.get_payload()), 2) |
| 2806 |
eq(msg.get_payload(0), text1) |
| 2807 |
eq(msg.get_payload(1), text2) |
| 2808 |
|
| 2809 |
def test_default_multipart_constructor(self): |
| 2810 |
msg = MIMEMultipart() |
| 2811 |
self.assertTrue(msg.is_multipart()) |
| 2812 |
|
| 2813 |
def test_multipart_default_policy(self): |
| 2814 |
msg = MIMEMultipart() |
| 2815 |
msg['To'] = '[email protected]' |
| 2816 |
msg['To'] = '[email protected]' |
| 2817 |
self.assertEqual(msg.get_all('to'), ['[email protected]', '[email protected]']) |
| 2818 |
|
| 2819 |
def test_multipart_custom_policy(self): |
| 2820 |
msg = MIMEMultipart(policy=email.policy.default) |
| 2821 |
msg['To'] = '[email protected]' |
| 2822 |
with self.assertRaises(ValueError) as cm: |
| 2823 |
msg['To'] = '[email protected]' |
| 2824 |
self.assertEqual(str(cm.exception), |
| 2825 |
'There may be at most 1 To headers in a message') |
| 2826 |
|
| 2827 |
|
| 2828 |
# Test the NonMultipart class |
| 2829 |
class TestNonMultipart(TestEmailBase): |
| 2830 |
def test_nonmultipart_is_not_multipart(self): |
| 2831 |
msg = MIMENonMultipart('text', 'plain') |
| 2832 |
self.assertFalse(msg.is_multipart()) |
| 2833 |
|
| 2834 |
def test_attach_raises_exception(self): |
| 2835 |
msg = Message() |
| 2836 |
msg['Subject'] = 'subpart 1' |
| 2837 |
r = MIMENonMultipart('text', 'plain') |
| 2838 |
self.assertRaises(errors.MultipartConversionError, r.attach, msg) |
| 2839 |
|
| 2840 |
|
| 2841 |
# A general test of parser->model->generator idempotency. IOW, read a message |
| 2842 |
# in, parse it into a message object tree, then without touching the tree, |
| 2843 |
# regenerate the plain text. The original text and the transformed text |
| 2844 |
# should be identical. Note: that we ignore the Unix-From since that may |
| 2845 |
# contain a changed date. |
| 2846 |
class TestIdempotent(TestEmailBase): |
| 2847 |
|
| 2848 |
linesep = '\n' |
| 2849 |
|
| 2850 |
def _msgobj(self, filename): |
| 2851 |
with openfile(filename, encoding="utf-8") as fp: |
| 2852 |
data = fp.read() |
| 2853 |
msg = email.message_from_string(data) |
| 2854 |
return msg, data |
| 2855 |
|
| 2856 |
def _idempotent(self, msg, text, unixfrom=False): |
| 2857 |
eq = self.ndiffAssertEqual |
| 2858 |
s = StringIO() |
| 2859 |
g = Generator(s, maxheaderlen=0) |
| 2860 |
g.flatten(msg, unixfrom=unixfrom) |
| 2861 |
eq(text, s.getvalue()) |
| 2862 |
|
| 2863 |
def test_parse_text_message(self): |
| 2864 |
eq = self.assertEqual |
| 2865 |
msg, text = self._msgobj('msg_01.txt') |
| 2866 |
eq(msg.get_content_type(), 'text/plain') |
| 2867 |
eq(msg.get_content_maintype(), 'text') |
| 2868 |
eq(msg.get_content_subtype(), 'plain') |
| 2869 |
eq(msg.get_params()[1], ('charset', 'us-ascii')) |
| 2870 |
eq(msg.get_param('charset'), 'us-ascii') |
| 2871 |
eq(msg.preamble, None) |
| 2872 |
eq(msg.epilogue, None) |
| 2873 |
self._idempotent(msg, text) |
| 2874 |
|
| 2875 |
def test_parse_untyped_message(self): |
| 2876 |
eq = self.assertEqual |
| 2877 |
msg, text = self._msgobj('msg_03.txt') |
| 2878 |
eq(msg.get_content_type(), 'text/plain') |
| 2879 |
eq(msg.get_params(), None) |
| 2880 |
eq(msg.get_param('charset'), None) |
| 2881 |
self._idempotent(msg, text) |
| 2882 |
|
| 2883 |
def test_simple_multipart(self): |
| 2884 |
msg, text = self._msgobj('msg_04.txt') |
| 2885 |
self._idempotent(msg, text) |
| 2886 |
|
| 2887 |
def test_MIME_digest(self): |
| 2888 |
msg, text = self._msgobj('msg_02.txt') |
| 2889 |
self._idempotent(msg, text) |
| 2890 |
|
| 2891 |
def test_long_header(self): |
| 2892 |
msg, text = self._msgobj('msg_27.txt') |
| 2893 |
self._idempotent(msg, text) |
| 2894 |
|
| 2895 |
def test_MIME_digest_with_part_headers(self): |
| 2896 |
msg, text = self._msgobj('msg_28.txt') |
| 2897 |
self._idempotent(msg, text) |
| 2898 |
|
| 2899 |
def test_mixed_with_image(self): |
| 2900 |
msg, text = self._msgobj('msg_06.txt') |
| 2901 |
self._idempotent(msg, text) |
| 2902 |
|
| 2903 |
def test_multipart_report(self): |
| 2904 |
msg, text = self._msgobj('msg_05.txt') |
| 2905 |
self._idempotent(msg, text) |
| 2906 |
|
| 2907 |
def test_dsn(self): |
| 2908 |
msg, text = self._msgobj('msg_16.txt') |
| 2909 |
self._idempotent(msg, text) |
| 2910 |
|
| 2911 |
def test_preamble_epilogue(self): |
| 2912 |
msg, text = self._msgobj('msg_21.txt') |
| 2913 |
self._idempotent(msg, text) |
| 2914 |
|
| 2915 |
def test_multipart_one_part(self): |
| 2916 |
msg, text = self._msgobj('msg_23.txt') |
| 2917 |
self._idempotent(msg, text) |
| 2918 |
|
| 2919 |
def test_multipart_no_parts(self): |
| 2920 |
msg, text = self._msgobj('msg_24.txt') |
| 2921 |
self._idempotent(msg, text) |
| 2922 |
|
| 2923 |
def test_no_start_boundary(self): |
| 2924 |
msg, text = self._msgobj('msg_31.txt') |
| 2925 |
self._idempotent(msg, text) |
| 2926 |
|
| 2927 |
def test_rfc2231_charset(self): |
| 2928 |
msg, text = self._msgobj('msg_32.txt') |
| 2929 |
self._idempotent(msg, text) |
| 2930 |
|
| 2931 |
def test_more_rfc2231_parameters(self): |
| 2932 |
msg, text = self._msgobj('msg_33.txt') |
| 2933 |
self._idempotent(msg, text) |
| 2934 |
|
| 2935 |
def test_text_plain_in_a_multipart_digest(self): |
| 2936 |
msg, text = self._msgobj('msg_34.txt') |
| 2937 |
self._idempotent(msg, text) |
| 2938 |
|
| 2939 |
def test_nested_multipart_mixeds(self): |
| 2940 |
msg, text = self._msgobj('msg_12a.txt') |
| 2941 |
self._idempotent(msg, text) |
| 2942 |
|
| 2943 |
def test_message_external_body_idempotent(self): |
| 2944 |
msg, text = self._msgobj('msg_36.txt') |
| 2945 |
self._idempotent(msg, text) |
| 2946 |
|
| 2947 |
def test_message_delivery_status(self): |
| 2948 |
msg, text = self._msgobj('msg_43.txt') |
| 2949 |
self._idempotent(msg, text, unixfrom=True) |
| 2950 |
|
| 2951 |
def test_message_signed_idempotent(self): |
| 2952 |
msg, text = self._msgobj('msg_45.txt') |
| 2953 |
self._idempotent(msg, text) |
| 2954 |
|
| 2955 |
def test_content_type(self): |
| 2956 |
eq = self.assertEqual |
| 2957 |
# Get a message object and reset the seek pointer for other tests |
| 2958 |
msg, text = self._msgobj('msg_05.txt') |
| 2959 |
eq(msg.get_content_type(), 'multipart/report') |
| 2960 |
# Test the Content-Type: parameters |
| 2961 |
params = {} |
| 2962 |
for pk, pv in msg.get_params(): |
| 2963 |
params[pk] = pv |
| 2964 |
eq(params['report-type'], 'delivery-status') |
| 2965 |
eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com') |
| 2966 |
eq(msg.preamble, 'This is a MIME-encapsulated message.' + self.linesep) |
| 2967 |
eq(msg.epilogue, self.linesep) |
| 2968 |
eq(len(msg.get_payload()), 3) |
| 2969 |
# Make sure the subparts are what we expect |
| 2970 |
msg1 = msg.get_payload(0) |
| 2971 |
eq(msg1.get_content_type(), 'text/plain') |
| 2972 |
eq(msg1.get_payload(), 'Yadda yadda yadda' + self.linesep) |
| 2973 |
msg2 = msg.get_payload(1) |
| 2974 |
eq(msg2.get_content_type(), 'text/plain') |
| 2975 |
eq(msg2.get_payload(), 'Yadda yadda yadda' + self.linesep) |
| 2976 |
msg3 = msg.get_payload(2) |
| 2977 |
eq(msg3.get_content_type(), 'message/rfc822') |
| 2978 |
self.assertIsInstance(msg3, Message) |
| 2979 |
payload = msg3.get_payload() |
| 2980 |
self.assertIsInstance(payload, list) |
| 2981 |
eq(len(payload), 1) |
| 2982 |
msg4 = payload[0] |
| 2983 |
self.assertIsInstance(msg4, Message) |
| 2984 |
eq(msg4.get_payload(), 'Yadda yadda yadda' + self.linesep) |
| 2985 |
|
| 2986 |
def test_parser(self): |
| 2987 |
eq = self.assertEqual |
| 2988 |
msg, text = self._msgobj('msg_06.txt') |
| 2989 |
# Check some of the outer headers |
| 2990 |
eq(msg.get_content_type(), 'message/rfc822') |
| 2991 |
# Make sure the payload is a list of exactly one sub-Message, and that |
| 2992 |
# that submessage has a type of text/plain |
| 2993 |
payload = msg.get_payload() |
| 2994 |
self.assertIsInstance(payload, list) |
| 2995 |
eq(len(payload), 1) |
| 2996 |
msg1 = payload[0] |
| 2997 |
self.assertIsInstance(msg1, Message) |
| 2998 |
eq(msg1.get_content_type(), 'text/plain') |
| 2999 |
self.assertIsInstance(msg1.get_payload(), str) |
| 3000 |
eq(msg1.get_payload(), self.linesep) |
| 3001 |
|
| 3002 |
|
| 3003 |
|
| 3004 |
# Test various other bits of the package's functionality |
| 3005 |
class TestMiscellaneous(TestEmailBase): |
| 3006 |
def test_message_from_string(self): |
| 3007 |
with openfile('msg_01.txt', encoding="utf-8") as fp: |
| 3008 |
text = fp.read() |
| 3009 |
msg = email.message_from_string(text) |
| 3010 |
s = StringIO() |
| 3011 |
# Don't wrap/continue long headers since we're trying to test |
| 3012 |
# idempotency. |
| 3013 |
g = Generator(s, maxheaderlen=0) |
| 3014 |
g.flatten(msg) |
| 3015 |
self.assertEqual(text, s.getvalue()) |
| 3016 |
|
| 3017 |
def test_message_from_file(self): |
| 3018 |
with openfile('msg_01.txt', encoding="utf-8") as fp: |
| 3019 |
text = fp.read() |
| 3020 |
fp.seek(0) |
| 3021 |
msg = email.message_from_file(fp) |
| 3022 |
s = StringIO() |
| 3023 |
# Don't wrap/continue long headers since we're trying to test |
| 3024 |
# idempotency. |
| 3025 |
g = Generator(s, maxheaderlen=0) |
| 3026 |
g.flatten(msg) |
| 3027 |
self.assertEqual(text, s.getvalue()) |
| 3028 |
|
| 3029 |
def test_message_from_string_with_class(self): |
| 3030 |
with openfile('msg_01.txt', encoding="utf-8") as fp: |
| 3031 |
text = fp.read() |
| 3032 |
|
| 3033 |
# Create a subclass |
| 3034 |
class MyMessage(Message): |
| 3035 |
pass |
| 3036 |
|
| 3037 |
msg = email.message_from_string(text, MyMessage) |
| 3038 |
self.assertIsInstance(msg, MyMessage) |
| 3039 |
# Try something more complicated |
| 3040 |
with openfile('msg_02.txt', encoding="utf-8") as fp: |
| 3041 |
text = fp.read() |
| 3042 |
msg = email.message_from_string(text, MyMessage) |
| 3043 |
for subpart in msg.walk(): |
| 3044 |
self.assertIsInstance(subpart, MyMessage) |
| 3045 |
|
| 3046 |
def test_message_from_file_with_class(self): |
| 3047 |
# Create a subclass |
| 3048 |
class MyMessage(Message): |
| 3049 |
pass |
| 3050 |
|
| 3051 |
with openfile('msg_01.txt', encoding="utf-8") as fp: |
| 3052 |
msg = email.message_from_file(fp, MyMessage) |
| 3053 |
self.assertIsInstance(msg, MyMessage) |
| 3054 |
# Try something more complicated |
| 3055 |
with openfile('msg_02.txt', encoding="utf-8") as fp: |
| 3056 |
msg = email.message_from_file(fp, MyMessage) |
| 3057 |
for subpart in msg.walk(): |
| 3058 |
self.assertIsInstance(subpart, MyMessage) |
| 3059 |
|
| 3060 |
def test_custom_message_does_not_require_arguments(self): |
| 3061 |
class MyMessage(Message): |
| 3062 |
def __init__(self): |
| 3063 |
super().__init__() |
| 3064 |
msg = self._str_msg("Subject: test\n\ntest", MyMessage) |
| 3065 |
self.assertIsInstance(msg, MyMessage) |
| 3066 |
|
| 3067 |
def test__all__(self): |
| 3068 |
module = __import__('email') |
| 3069 |
self.assertEqual(sorted(module.__all__), [ |
| 3070 |
'base64mime', 'charset', 'encoders', 'errors', 'feedparser', |
| 3071 |
'generator', 'header', 'iterators', 'message', |
| 3072 |
'message_from_binary_file', 'message_from_bytes', |
| 3073 |
'message_from_file', 'message_from_string', 'mime', 'parser', |
| 3074 |
'quoprimime', 'utils', |
| 3075 |
]) |
| 3076 |
|
| 3077 |
def test_formatdate(self): |
| 3078 |
now = time.time() |
| 3079 |
self.assertEqual(utils.parsedate(utils.formatdate(now))[:6], |
| 3080 |
time.gmtime(now)[:6]) |
| 3081 |
|
| 3082 |
def test_formatdate_localtime(self): |
| 3083 |
now = time.time() |
| 3084 |
self.assertEqual( |
| 3085 |
utils.parsedate(utils.formatdate(now, localtime=True))[:6], |
| 3086 |
time.localtime(now)[:6]) |
| 3087 |
|
| 3088 |
def test_formatdate_usegmt(self): |
| 3089 |
now = time.time() |
| 3090 |
self.assertEqual( |
| 3091 |
utils.formatdate(now, localtime=False), |
| 3092 |
time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now))) |
| 3093 |
self.assertEqual( |
| 3094 |
utils.formatdate(now, localtime=False, usegmt=True), |
| 3095 |
time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) |
| 3096 |
|
| 3097 |
# parsedate and parsedate_tz will become deprecated interfaces someday |
| 3098 |
def test_parsedate_returns_None_for_invalid_strings(self): |
| 3099 |
# See also test_parsedate_to_datetime_with_invalid_raises_valueerror |
| 3100 |
# in test_utils. |
| 3101 |
invalid_dates = [ |
| 3102 |
'', |
| 3103 |
' ', |
| 3104 |
'0', |
| 3105 |
'A Complete Waste of Time', |
| 3106 |
'Wed, 3 Apr 2002 12.34.56.78+0800', |
| 3107 |
'17 June , 2022', |
| 3108 |
'Friday, -Nov-82 16:14:55 EST', |
| 3109 |
'Friday, Nov--82 16:14:55 EST', |
| 3110 |
'Friday, 19-Nov- 16:14:55 EST', |
| 3111 |
] |
| 3112 |
for dtstr in invalid_dates: |
| 3113 |
with self.subTest(dtstr=dtstr): |
| 3114 |
self.assertIsNone(utils.parsedate(dtstr)) |
| 3115 |
self.assertIsNone(utils.parsedate_tz(dtstr)) |
| 3116 |
# Not a part of the spec but, but this has historically worked: |
| 3117 |
self.assertIsNone(utils.parsedate(None)) |
| 3118 |
self.assertIsNone(utils.parsedate_tz(None)) |
| 3119 |
|
| 3120 |
def test_parsedate_compact(self): |
| 3121 |
self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58:26 +0800'), |
| 3122 |
(2002, 4, 3, 14, 58, 26, 0, 1, -1, 28800)) |
| 3123 |
# The FWS after the comma is optional |
| 3124 |
self.assertEqual(utils.parsedate_tz('Wed,3 Apr 2002 14:58:26 +0800'), |
| 3125 |
(2002, 4, 3, 14, 58, 26, 0, 1, -1, 28800)) |
| 3126 |
# The comma is optional |
| 3127 |
self.assertEqual(utils.parsedate_tz('Wed 3 Apr 2002 14:58:26 +0800'), |
| 3128 |
(2002, 4, 3, 14, 58, 26, 0, 1, -1, 28800)) |
| 3129 |
|
| 3130 |
def test_parsedate_no_dayofweek(self): |
| 3131 |
eq = self.assertEqual |
| 3132 |
eq(utils.parsedate_tz('5 Feb 2003 13:47:26 -0800'), |
| 3133 |
(2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800)) |
| 3134 |
eq(utils.parsedate_tz('February 5, 2003 13:47:26 -0800'), |
| 3135 |
(2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800)) |
| 3136 |
|
| 3137 |
def test_parsedate_no_space_before_positive_offset(self): |
| 3138 |
self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58:26+0800'), |
| 3139 |
(2002, 4, 3, 14, 58, 26, 0, 1, -1, 28800)) |
| 3140 |
|
| 3141 |
def test_parsedate_no_space_before_negative_offset(self): |
| 3142 |
# Issue 1155362: we already handled '+' for this case. |
| 3143 |
self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58:26-0800'), |
| 3144 |
(2002, 4, 3, 14, 58, 26, 0, 1, -1, -28800)) |
| 3145 |
|
| 3146 |
def test_parsedate_accepts_time_with_dots(self): |
| 3147 |
eq = self.assertEqual |
| 3148 |
eq(utils.parsedate_tz('5 Feb 2003 13.47.26 -0800'), |
| 3149 |
(2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800)) |
| 3150 |
eq(utils.parsedate_tz('5 Feb 2003 13.47 -0800'), |
| 3151 |
(2003, 2, 5, 13, 47, 0, 0, 1, -1, -28800)) |
| 3152 |
|
| 3153 |
def test_parsedate_rfc_850(self): |
| 3154 |
self.assertEqual(utils.parsedate_tz('Friday, 19-Nov-82 16:14:55 EST'), |
| 3155 |
(1982, 11, 19, 16, 14, 55, 0, 1, -1, -18000)) |
| 3156 |
|
| 3157 |
def test_parsedate_no_seconds(self): |
| 3158 |
self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58 +0800'), |
| 3159 |
(2002, 4, 3, 14, 58, 0, 0, 1, -1, 28800)) |
| 3160 |
|
| 3161 |
def test_parsedate_dot_time_delimiter(self): |
| 3162 |
self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14.58.26 +0800'), |
| 3163 |
(2002, 4, 3, 14, 58, 26, 0, 1, -1, 28800)) |
| 3164 |
self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14.58 +0800'), |
| 3165 |
(2002, 4, 3, 14, 58, 0, 0, 1, -1, 28800)) |
| 3166 |
|
| 3167 |
def test_parsedate_acceptable_to_time_functions(self): |
| 3168 |
eq = self.assertEqual |
| 3169 |
timetup = utils.parsedate('5 Feb 2003 13:47:26 -0800') |
| 3170 |
t = int(time.mktime(timetup)) |
| 3171 |
eq(time.localtime(t)[:6], timetup[:6]) |
| 3172 |
eq(int(time.strftime('%Y', timetup)), 2003) |
| 3173 |
timetup = utils.parsedate_tz('5 Feb 2003 13:47:26 -0800') |
| 3174 |
t = int(time.mktime(timetup[:9])) |
| 3175 |
eq(time.localtime(t)[:6], timetup[:6]) |
| 3176 |
eq(int(time.strftime('%Y', timetup[:9])), 2003) |
| 3177 |
|
| 3178 |
def test_mktime_tz(self): |
| 3179 |
self.assertEqual(utils.mktime_tz((1970, 1, 1, 0, 0, 0, |
| 3180 |
-1, -1, -1, 0)), 0) |
| 3181 |
self.assertEqual(utils.mktime_tz((1970, 1, 1, 0, 0, 0, |
| 3182 |
-1, -1, -1, 1234)), -1234) |
| 3183 |
|
| 3184 |
def test_parsedate_y2k(self): |
| 3185 |
"""Test for parsing a date with a two-digit year. |
| 3186 |
|
| 3187 |
Parsing a date with a two-digit year should return the correct |
| 3188 |
four-digit year. RFC 822 allows two-digit years, but RFC 5322 (which |
| 3189 |
obsoletes RFC 2822, which obsoletes RFC 822) requires four-digit years. |
| 3190 |
|
| 3191 |
""" |
| 3192 |
self.assertEqual(utils.parsedate_tz('25 Feb 03 13:47:26 -0800'), |
| 3193 |
utils.parsedate_tz('25 Feb 2003 13:47:26 -0800')) |
| 3194 |
self.assertEqual(utils.parsedate_tz('25 Feb 71 13:47:26 -0800'), |
| 3195 |
utils.parsedate_tz('25 Feb 1971 13:47:26 -0800')) |
| 3196 |
|
| 3197 |
def test_parseaddr_empty(self): |
| 3198 |
self.assertEqual(utils.parseaddr('<>'), ('', '')) |
| 3199 |
self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '') |
| 3200 |
|
| 3201 |
def test_parseaddr_multiple_domains(self): |
| 3202 |
self.assertEqual( |
| 3203 |
utils.parseaddr('a@b@c'), |
| 3204 |
('', '') |
| 3205 |
) |
| 3206 |
self.assertEqual( |
| 3207 |
utils.parseaddr('[email protected]@c'), |
| 3208 |
('', '') |
| 3209 |
) |
| 3210 |
self.assertEqual( |
| 3211 |
utils.parseaddr('[email protected]@c'), |
| 3212 |
('', '') |
| 3213 |
) |
| 3214 |
|
| 3215 |
def test_noquote_dump(self): |
| 3216 |
self.assertEqual( |
| 3217 |
utils.formataddr(('A Silly Person', '[email protected]')), |
| 3218 |
'A Silly Person <[email protected]>') |
| 3219 |
|
| 3220 |
def test_escape_dump(self): |
| 3221 |
self.assertEqual( |
| 3222 |
utils.formataddr(('A (Very) Silly Person', '[email protected]')), |
| 3223 |
r'"A (Very) Silly Person" <[email protected]>') |
| 3224 |
self.assertEqual( |
| 3225 |
utils.parseaddr(r'"A \(Very\) Silly Person" <[email protected]>'), |
| 3226 |
('A (Very) Silly Person', '[email protected]')) |
| 3227 |
a = r'A \(Special\) Person' |
| 3228 |
b = '[email protected]' |
| 3229 |
self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b)) |
| 3230 |
|
| 3231 |
def test_escape_backslashes(self): |
| 3232 |
self.assertEqual( |
| 3233 |
utils.formataddr((r'Arthur \Backslash\ Foobar', '[email protected]')), |
| 3234 |
r'"Arthur \\Backslash\\ Foobar" <[email protected]>') |
| 3235 |
a = r'Arthur \Backslash\ Foobar' |
| 3236 |
b = '[email protected]' |
| 3237 |
self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b)) |
| 3238 |
|
| 3239 |
def test_quotes_unicode_names(self): |
| 3240 |
# issue 1690608. email.utils.formataddr() should be RFC 2047 aware. |
| 3241 |
name = "H\u00e4ns W\u00fcrst" |
| 3242 |
addr = '[email protected]' |
| 3243 |
utf8_base64 = "=?utf-8?b?SMOkbnMgV8O8cnN0?= <[email protected]>" |
| 3244 |
latin1_quopri = "=?iso-8859-1?q?H=E4ns_W=FCrst?= <[email protected]>" |
| 3245 |
self.assertEqual(utils.formataddr((name, addr)), utf8_base64) |
| 3246 |
self.assertEqual(utils.formataddr((name, addr), 'iso-8859-1'), |
| 3247 |
latin1_quopri) |
| 3248 |
|
| 3249 |
def test_accepts_any_charset_like_object(self): |
| 3250 |
# issue 1690608. email.utils.formataddr() should be RFC 2047 aware. |
| 3251 |
name = "H\u00e4ns W\u00fcrst" |
| 3252 |
addr = '[email protected]' |
| 3253 |
utf8_base64 = "=?utf-8?b?SMOkbnMgV8O8cnN0?= <[email protected]>" |
| 3254 |
foobar = "FOOBAR" |
| 3255 |
class CharsetMock: |
| 3256 |
def header_encode(self, string): |
| 3257 |
return foobar |
| 3258 |
mock = CharsetMock() |
| 3259 |
mock_expected = "%s <%s>" % (foobar, addr) |
| 3260 |
self.assertEqual(utils.formataddr((name, addr), mock), mock_expected) |
| 3261 |
self.assertEqual(utils.formataddr((name, addr), Charset('utf-8')), |
| 3262 |
utf8_base64) |
| 3263 |
|
| 3264 |
def test_invalid_charset_like_object_raises_error(self): |
| 3265 |
# issue 1690608. email.utils.formataddr() should be RFC 2047 aware. |
| 3266 |
name = "H\u00e4ns W\u00fcrst" |
| 3267 |
addr = '[email protected]' |
| 3268 |
# An object without a header_encode method: |
| 3269 |
bad_charset = object() |
| 3270 |
self.assertRaises(AttributeError, utils.formataddr, (name, addr), |
| 3271 |
bad_charset) |
| 3272 |
|
| 3273 |
def test_unicode_address_raises_error(self): |
| 3274 |
# issue 1690608. email.utils.formataddr() should be RFC 2047 aware. |
| 3275 |
addr = 'pers\[email protected]' |
| 3276 |
self.assertRaises(UnicodeError, utils.formataddr, (None, addr)) |
| 3277 |
self.assertRaises(UnicodeError, utils.formataddr, ("Name", addr)) |
| 3278 |
|
| 3279 |
def test_name_with_dot(self): |
| 3280 |
x = 'John X. Doe <[email protected]>' |
| 3281 |
y = '"John X. Doe" <[email protected]>' |
| 3282 |
a, b = ('John X. Doe', '[email protected]') |
| 3283 |
self.assertEqual(utils.parseaddr(x), (a, b)) |
| 3284 |
self.assertEqual(utils.parseaddr(y), (a, b)) |
| 3285 |
# formataddr() quotes the name if there's a dot in it |
| 3286 |
self.assertEqual(utils.formataddr((a, b)), y) |
| 3287 |
|
| 3288 |
def test_parseaddr_preserves_quoted_pairs_in_addresses(self): |
| 3289 |
# issue 10005. Note that in the third test the second pair of |
| 3290 |
# backslashes is not actually a quoted pair because it is not inside a |
| 3291 |
# comment or quoted string: the address being parsed has a quoted |
| 3292 |
# string containing a quoted backslash, followed by 'example' and two |
| 3293 |
# backslashes, followed by another quoted string containing a space and |
| 3294 |
# the word 'example'. parseaddr copies those two backslashes |
| 3295 |
# literally. Per RFC 5322 this is not technically correct since a \ may |
| 3296 |
# not appear in an address outside of a quoted string. It is probably |
| 3297 |
# a sensible Postel interpretation, though. |
| 3298 |
eq = self.assertEqual |
| 3299 |
eq(utils.parseaddr('""example" example"@example.com'), |
| 3300 |
('', '""example" example"@example.com')) |
| 3301 |
eq(utils.parseaddr('"\\"example\\" example"@example.com'), |
| 3302 |
('', '"\\"example\\" example"@example.com')) |
| 3303 |
eq(utils.parseaddr('"\\\\"example\\\\" example"@example.com'), |
| 3304 |
('', '"\\\\"example\\\\" example"@example.com')) |
| 3305 |
|
| 3306 |
def test_parseaddr_preserves_spaces_in_local_part(self): |
| 3307 |
# issue 9286. A normal RFC 5322 local part should not contain any |
| 3308 |
# folding white space, but legacy local parts can (they are a sequence |
| 3309 |
# of atoms, not dotatoms). On the other hand we strip whitespace from |
| 3310 |
# before the @ and around dots, on the assumption that the whitespace |
| 3311 |
# around the punctuation is a mistake in what would otherwise be |
| 3312 |
# an RFC 5322 local part. Leading whitespace is, usual, stripped as well. |
| 3313 |
self.assertEqual(('', "merwok [email protected]"), |
| 3314 |
utils.parseaddr("merwok [email protected]")) |
| 3315 |
self.assertEqual(('', "merwok [email protected]"), |
| 3316 |
utils.parseaddr("merwok [email protected]")) |
| 3317 |
self.assertEqual(('', "merwok [email protected]"), |
| 3318 |
utils.parseaddr(" merwok wok @xample.com")) |
| 3319 |
self.assertEqual(('', 'merwok"wok" [email protected]'), |
| 3320 |
utils.parseaddr('merwok"wok" [email protected]')) |
| 3321 |
self.assertEqual(('', '[email protected]'), |
| 3322 |
utils.parseaddr('merwok. wok . [email protected]')) |
| 3323 |
|
| 3324 |
def test_formataddr_does_not_quote_parens_in_quoted_string(self): |
| 3325 |
addr = ("'[email protected]' ([email protected])", |
| 3326 |
'[email protected]') |
| 3327 |
addrstr = ('"\'[email protected]\' ' |
| 3328 |
'([email protected])" <[email protected]>') |
| 3329 |
self.assertEqual(utils.parseaddr(addrstr), addr) |
| 3330 |
self.assertEqual(utils.formataddr(addr), addrstr) |
| 3331 |
|
| 3332 |
|
| 3333 |
def test_multiline_from_comment(self): |
| 3334 |
x = """\ |
| 3335 |
Foo |
| 3336 |
\tBar <[email protected]>""" |
| 3337 |
self.assertEqual(utils.parseaddr(x), ('Foo Bar', '[email protected]')) |
| 3338 |
|
| 3339 |
def test_quote_dump(self): |
| 3340 |
self.assertEqual( |
| 3341 |
utils.formataddr(('A Silly; Person', '[email protected]')), |
| 3342 |
r'"A Silly; Person" <[email protected]>') |
| 3343 |
|
| 3344 |
def test_charset_richcomparisons(self): |
| 3345 |
eq = self.assertEqual |
| 3346 |
ne = self.assertNotEqual |
| 3347 |
cset1 = Charset() |
| 3348 |
cset2 = Charset() |
| 3349 |
eq(cset1, 'us-ascii') |
| 3350 |
eq(cset1, 'US-ASCII') |
| 3351 |
eq(cset1, 'Us-AsCiI') |
| 3352 |
eq('us-ascii', cset1) |
| 3353 |
eq('US-ASCII', cset1) |
| 3354 |
eq('Us-AsCiI', cset1) |
| 3355 |
ne(cset1, 'usascii') |
| 3356 |
ne(cset1, 'USASCII') |
| 3357 |
ne(cset1, 'UsAsCiI') |
| 3358 |
ne('usascii', cset1) |
| 3359 |
ne('USASCII', cset1) |
| 3360 |
ne('UsAsCiI', cset1) |
| 3361 |
eq(cset1, cset2) |
| 3362 |
eq(cset2, cset1) |
| 3363 |
|
| 3364 |
def test_getaddresses(self): |
| 3365 |
eq = self.assertEqual |
| 3366 |
eq(utils.getaddresses(['[email protected] (Al Person)', |
| 3367 |
'Bud Person <[email protected]>']), |
| 3368 |
[('Al Person', '[email protected]'), |
| 3369 |
('Bud Person', '[email protected]')]) |
| 3370 |
|
| 3371 |
def test_getaddresses_comma_in_name(self): |
| 3372 |
"""GH-106669 regression test.""" |
| 3373 |
self.assertEqual( |
| 3374 |
utils.getaddresses( |
| 3375 |
[ |
| 3376 |
'"Bud, Person" <[email protected]>', |
| 3377 |
'[email protected] (Al Person)', |
| 3378 |
'"Mariusz Felisiak" <[email protected]>', |
| 3379 |
] |
| 3380 |
), |
| 3381 |
[ |
| 3382 |
('Bud, Person', '[email protected]'), |
| 3383 |
('Al Person', '[email protected]'), |
| 3384 |
('Mariusz Felisiak', '[email protected]'), |
| 3385 |
], |
| 3386 |
) |
| 3387 |
|
| 3388 |
def test_parsing_errors(self): |
| 3389 |
"""Test for parsing errors from CVE-2023-27043 and CVE-2019-16056""" |
| 3390 |
alice = '[email protected]' |
| 3391 |
bob = '[email protected]' |
| 3392 |
empty = ('', '') |
| 3393 |
|
| 3394 |
# Test utils.getaddresses() and utils.parseaddr() on malformed email |
| 3395 |
# addresses: default behavior (strict=True) rejects malformed address, |
| 3396 |
# and strict=False which tolerates malformed address. |
| 3397 |
for invalid_separator, expected_non_strict in ( |
| 3398 |
('(', [(f'<{bob}>', alice)]), |
| 3399 |
(')', [('', alice), empty, ('', bob)]), |
| 3400 |
('<', [('', alice), empty, ('', bob), empty]), |
| 3401 |
('>', [('', alice), empty, ('', bob)]), |
| 3402 |
('[', [('', f'{alice}[<{bob}>]')]), |
| 3403 |
(']', [('', alice), empty, ('', bob)]), |
| 3404 |
('@', [empty, empty, ('', bob)]), |
| 3405 |
(';', [('', alice), empty, ('', bob)]), |
| 3406 |
(':', [('', alice), ('', bob)]), |
| 3407 |
('.', [('', alice + '.'), ('', bob)]), |
| 3408 |
('"', [('', alice), ('', f'<{bob}>')]), |
| 3409 |
): |
| 3410 |
address = f'{alice}{invalid_separator}<{bob}>' |
| 3411 |
with self.subTest(address=address): |
| 3412 |
self.assertEqual(utils.getaddresses([address]), |
| 3413 |
[empty]) |
| 3414 |
self.assertEqual(utils.getaddresses([address], strict=False), |
| 3415 |
expected_non_strict) |
| 3416 |
|
| 3417 |
self.assertEqual(utils.parseaddr([address]), |
| 3418 |
empty) |
| 3419 |
self.assertEqual(utils.parseaddr([address], strict=False), |
| 3420 |
('', address)) |
| 3421 |
|
| 3422 |
# Comma (',') is treated differently depending on strict parameter. |
| 3423 |
# Comma without quotes. |
| 3424 |
address = f'{alice},<{bob}>' |
| 3425 |
self.assertEqual(utils.getaddresses([address]), |
| 3426 |
[('', alice), ('', bob)]) |
| 3427 |
self.assertEqual(utils.getaddresses([address], strict=False), |
| 3428 |
[('', alice), ('', bob)]) |
| 3429 |
self.assertEqual(utils.parseaddr([address]), |
| 3430 |
empty) |
| 3431 |
self.assertEqual(utils.parseaddr([address], strict=False), |
| 3432 |
('', address)) |
| 3433 |
|
| 3434 |
# Real name between quotes containing comma. |
| 3435 |
address = '"Alice, [email protected]" <[email protected]>' |
| 3436 |
expected_strict = ('Alice, [email protected]', '[email protected]') |
| 3437 |
self.assertEqual(utils.getaddresses([address]), [expected_strict]) |
| 3438 |
self.assertEqual(utils.getaddresses([address], strict=False), [expected_strict]) |
| 3439 |
self.assertEqual(utils.parseaddr([address]), expected_strict) |
| 3440 |
self.assertEqual(utils.parseaddr([address], strict=False), |
| 3441 |
('', address)) |
| 3442 |
|
| 3443 |
# Valid parenthesis in comments. |
| 3444 |
address = '[email protected] (Alice)' |
| 3445 |
expected_strict = ('Alice', '[email protected]') |
| 3446 |
self.assertEqual(utils.getaddresses([address]), [expected_strict]) |
| 3447 |
self.assertEqual(utils.getaddresses([address], strict=False), [expected_strict]) |
| 3448 |
self.assertEqual(utils.parseaddr([address]), expected_strict) |
| 3449 |
self.assertEqual(utils.parseaddr([address], strict=False), |
| 3450 |
('', address)) |
| 3451 |
|
| 3452 |
# Invalid parenthesis in comments. |
| 3453 |
address = '[email protected] )Alice(' |
| 3454 |
self.assertEqual(utils.getaddresses([address]), [empty]) |
| 3455 |
self.assertEqual(utils.getaddresses([address], strict=False), |
| 3456 |
[('', '[email protected]'), ('', ''), ('', 'Alice')]) |
| 3457 |
self.assertEqual(utils.parseaddr([address]), empty) |
| 3458 |
self.assertEqual(utils.parseaddr([address], strict=False), |
| 3459 |
('', address)) |
| 3460 |
|
| 3461 |
# Two addresses with quotes separated by comma. |
| 3462 |
address = '"Jane Doe" <[email protected]>, "John Doe" <[email protected]>' |
| 3463 |
self.assertEqual(utils.getaddresses([address]), |
| 3464 |
[('Jane Doe', '[email protected]'), |
| 3465 |
('John Doe', '[email protected]')]) |
| 3466 |
self.assertEqual(utils.getaddresses([address], strict=False), |
| 3467 |
[('Jane Doe', '[email protected]'), |
| 3468 |
('John Doe', '[email protected]')]) |
| 3469 |
self.assertEqual(utils.parseaddr([address]), empty) |
| 3470 |
self.assertEqual(utils.parseaddr([address], strict=False), |
| 3471 |
('', address)) |
| 3472 |
|
| 3473 |
# Test email.utils.supports_strict_parsing attribute |
| 3474 |
self.assertEqual(email.utils.supports_strict_parsing, True) |
| 3475 |
|
| 3476 |
def test_getaddresses_nasty(self): |
| 3477 |
for addresses, expected in ( |
| 3478 |
(['"Sürname, Firstname" <[email protected]>'], |
| 3479 |
[('Sürname, Firstname', '[email protected]')]), |
| 3480 |
|
| 3481 |
(['foo: ;'], |
| 3482 |
[('', '')]), |
| 3483 |
|
| 3484 |
(['foo: ;', '"Jason R. Mastaler" <[email protected]>'], |
| 3485 |
[('', ''), ('Jason R. Mastaler', '[email protected]')]), |
| 3486 |
|
| 3487 |
([r'Pete(A nice \) chap) <pete(his account)@silly.test(his host)>'], |
| 3488 |
[('Pete (A nice ) chap his account his host)', '[email protected]')]), |
| 3489 |
|
| 3490 |
(['(Empty list)(start)Undisclosed recipients :(nobody(I know))'], |
| 3491 |
[('', '')]), |
| 3492 |
|
| 3493 |
(['Mary <@machine.tld:[email protected]>, , jdoe@test . example'], |
| 3494 |
[('Mary', '[email protected]'), ('', ''), ('', '[email protected]')]), |
| 3495 |
|
| 3496 |
(['John Doe <jdoe@machine(comment). example>'], |
| 3497 |
[('John Doe (comment)', '[email protected]')]), |
| 3498 |
|
| 3499 |
(['"Mary Smith: Personal Account" <[email protected]>'], |
| 3500 |
[('Mary Smith: Personal Account', '[email protected]')]), |
| 3501 |
|
| 3502 |
(['Undisclosed recipients:;'], |
| 3503 |
[('', '')]), |
| 3504 |
|
| 3505 |
([r'<[email protected]>, "Giant; \"Big\" Box" <[email protected]>'], |
| 3506 |
[('', '[email protected]'), ('Giant; "Big" Box', '[email protected]')]), |
| 3507 |
): |
| 3508 |
with self.subTest(addresses=addresses): |
| 3509 |
self.assertEqual(utils.getaddresses(addresses), |
| 3510 |
expected) |
| 3511 |
self.assertEqual(utils.getaddresses(addresses, strict=False), |
| 3512 |
expected) |
| 3513 |
|
| 3514 |
addresses = ['[]*-- =~$'] |
| 3515 |
self.assertEqual(utils.getaddresses(addresses), |
| 3516 |
[('', '')]) |
| 3517 |
self.assertEqual(utils.getaddresses(addresses, strict=False), |
| 3518 |
[('', ''), ('', ''), ('', '*--')]) |
| 3519 |
|
| 3520 |
def test_getaddresses_embedded_comment(self): |
| 3521 |
"""Test proper handling of a nested comment""" |
| 3522 |
eq = self.assertEqual |
| 3523 |
addrs = utils.getaddresses(['User ((nested comment)) <[email protected]>']) |
| 3524 |
eq(addrs[0][1], '[email protected]') |
| 3525 |
|
| 3526 |
def test_getaddresses_header_obj(self): |
| 3527 |
"""Test the handling of a Header object.""" |
| 3528 |
addrs = utils.getaddresses([Header('Al Person <[email protected]>')]) |
| 3529 |
self.assertEqual(addrs[0][1], '[email protected]') |
| 3530 |
|
| 3531 |
@threading_helper.requires_working_threading() |
| 3532 |
@support.requires_resource('cpu') |
| 3533 |
def test_make_msgid_collisions(self): |
| 3534 |
# Test make_msgid uniqueness, even with multiple threads |
| 3535 |
class MsgidsThread(Thread): |
| 3536 |
def run(self): |
| 3537 |
# generate msgids for 3 seconds |
| 3538 |
self.msgids = [] |
| 3539 |
append = self.msgids.append |
| 3540 |
make_msgid = utils.make_msgid |
| 3541 |
clock = time.monotonic |
| 3542 |
tfin = clock() + 3.0 |
| 3543 |
while clock() < tfin: |
| 3544 |
append(make_msgid(domain='testdomain-string')) |
| 3545 |
|
| 3546 |
threads = [MsgidsThread() for i in range(5)] |
| 3547 |
with threading_helper.start_threads(threads): |
| 3548 |
pass |
| 3549 |
all_ids = sum([t.msgids for t in threads], []) |
| 3550 |
self.assertEqual(len(set(all_ids)), len(all_ids)) |
| 3551 |
|
| 3552 |
def test_utils_quote_unquote(self): |
| 3553 |
eq = self.assertEqual |
| 3554 |
msg = Message() |
| 3555 |
msg.add_header('content-disposition', 'attachment', |
| 3556 |
filename='foo\\wacky"name') |
| 3557 |
eq(msg.get_filename(), 'foo\\wacky"name') |
| 3558 |
|
| 3559 |
def test_get_body_encoding_with_bogus_charset(self): |
| 3560 |
charset = Charset('not a charset') |
| 3561 |
self.assertEqual(charset.get_body_encoding(), 'base64') |
| 3562 |
|
| 3563 |
def test_get_body_encoding_with_uppercase_charset(self): |
| 3564 |
eq = self.assertEqual |
| 3565 |
msg = Message() |
| 3566 |
msg['Content-Type'] = 'text/plain; charset=UTF-8' |
| 3567 |
eq(msg['content-type'], 'text/plain; charset=UTF-8') |
| 3568 |
charsets = msg.get_charsets() |
| 3569 |
eq(len(charsets), 1) |
| 3570 |
eq(charsets[0], 'utf-8') |
| 3571 |
charset = Charset(charsets[0]) |
| 3572 |
eq(charset.get_body_encoding(), 'base64') |
| 3573 |
msg.set_payload(b'hello world', charset=charset) |
| 3574 |
eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n') |
| 3575 |
eq(msg.get_payload(decode=True), b'hello world') |
| 3576 |
eq(msg['content-transfer-encoding'], 'base64') |
| 3577 |
# Try another one |
| 3578 |
msg = Message() |
| 3579 |
msg['Content-Type'] = 'text/plain; charset="US-ASCII"' |
| 3580 |
charsets = msg.get_charsets() |
| 3581 |
eq(len(charsets), 1) |
| 3582 |
eq(charsets[0], 'us-ascii') |
| 3583 |
charset = Charset(charsets[0]) |
| 3584 |
eq(charset.get_body_encoding(), encoders.encode_7or8bit) |
| 3585 |
msg.set_payload('hello world', charset=charset) |
| 3586 |
eq(msg.get_payload(), 'hello world') |
| 3587 |
eq(msg['content-transfer-encoding'], '7bit') |
| 3588 |
|
| 3589 |
def test_charsets_case_insensitive(self): |
| 3590 |
lc = Charset('us-ascii') |
| 3591 |
uc = Charset('US-ASCII') |
| 3592 |
self.assertEqual(lc.get_body_encoding(), uc.get_body_encoding()) |
| 3593 |
|
| 3594 |
def test_partial_falls_inside_message_delivery_status(self): |
| 3595 |
eq = self.ndiffAssertEqual |
| 3596 |
# The Parser interface provides chunks of data to FeedParser in 8192 |
| 3597 |
# byte gulps. SF bug #1076485 found one of those chunks inside |
| 3598 |
# message/delivery-status header block, which triggered an |
| 3599 |
# unreadline() of NeedMoreData. |
| 3600 |
msg = self._msgobj('msg_43.txt') |
| 3601 |
sfp = StringIO() |
| 3602 |
iterators._structure(msg, sfp) |
| 3603 |
eq(sfp.getvalue(), """\ |
| 3604 |
multipart/report |
| 3605 |
text/plain |
| 3606 |
message/delivery-status |
| 3607 |
text/plain |
| 3608 |
text/plain |
| 3609 |
text/plain |
| 3610 |
text/plain |
| 3611 |
text/plain |
| 3612 |
text/plain |
| 3613 |
text/plain |
| 3614 |
text/plain |
| 3615 |
text/plain |
| 3616 |
text/plain |
| 3617 |
text/plain |
| 3618 |
text/plain |
| 3619 |
text/plain |
| 3620 |
text/plain |
| 3621 |
text/plain |
| 3622 |
text/plain |
| 3623 |
text/plain |
| 3624 |
text/plain |
| 3625 |
text/plain |
| 3626 |
text/plain |
| 3627 |
text/plain |
| 3628 |
text/plain |
| 3629 |
text/plain |
| 3630 |
text/plain |
| 3631 |
text/plain |
| 3632 |
text/plain |
| 3633 |
text/rfc822-headers |
| 3634 |
""") |
| 3635 |
|
| 3636 |
def test_make_msgid_domain(self): |
| 3637 |
self.assertEqual( |
| 3638 |
email.utils.make_msgid(domain='testdomain-string')[-19:], |
| 3639 |
'@testdomain-string>') |
| 3640 |
|
| 3641 |
def test_make_msgid_idstring(self): |
| 3642 |
self.assertEqual( |
| 3643 |
email.utils.make_msgid(idstring='test-idstring', |
| 3644 |
domain='testdomain-string')[-33:], |
| 3645 |
'.test-idstring@testdomain-string>') |
| 3646 |
|
| 3647 |
def test_make_msgid_default_domain(self): |
| 3648 |
with patch('socket.getfqdn') as mock_getfqdn: |
| 3649 |
mock_getfqdn.return_value = domain = 'pythontest.example.com' |
| 3650 |
self.assertEndsWith(email.utils.make_msgid(), '@' + domain + '>') |
| 3651 |
|
| 3652 |
def test_Generator_linend(self): |
| 3653 |
# Issue 14645. |
| 3654 |
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as f: |
| 3655 |
msgtxt = f.read() |
| 3656 |
msgtxt_nl = msgtxt.replace('\r\n', '\n') |
| 3657 |
msg = email.message_from_string(msgtxt) |
| 3658 |
s = StringIO() |
| 3659 |
g = email.generator.Generator(s) |
| 3660 |
g.flatten(msg) |
| 3661 |
self.assertEqual(s.getvalue(), msgtxt_nl) |
| 3662 |
|
| 3663 |
def test_BytesGenerator_linend(self): |
| 3664 |
# Issue 14645. |
| 3665 |
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as f: |
| 3666 |
msgtxt = f.read() |
| 3667 |
msgtxt_nl = msgtxt.replace('\r\n', '\n') |
| 3668 |
msg = email.message_from_string(msgtxt_nl) |
| 3669 |
s = BytesIO() |
| 3670 |
g = email.generator.BytesGenerator(s) |
| 3671 |
g.flatten(msg, linesep='\r\n') |
| 3672 |
self.assertEqual(s.getvalue().decode('ascii'), msgtxt) |
| 3673 |
|
| 3674 |
def test_BytesGenerator_linend_with_non_ascii(self): |
| 3675 |
# Issue 14645. |
| 3676 |
with openfile('msg_26.txt', 'rb') as f: |
| 3677 |
msgtxt = f.read() |
| 3678 |
msgtxt = msgtxt.replace(b'with attachment', b'fo\xf6') |
| 3679 |
msgtxt_nl = msgtxt.replace(b'\r\n', b'\n') |
| 3680 |
msg = email.message_from_bytes(msgtxt_nl) |
| 3681 |
s = BytesIO() |
| 3682 |
g = email.generator.BytesGenerator(s) |
| 3683 |
g.flatten(msg, linesep='\r\n') |
| 3684 |
self.assertEqual(s.getvalue(), msgtxt) |
| 3685 |
|
| 3686 |
def test_mime_classes_policy_argument(self): |
| 3687 |
with openfile('sndhdr.au', 'rb') as fp: |
| 3688 |
audiodata = fp.read() |
| 3689 |
with openfile('python.gif', 'rb') as fp: |
| 3690 |
bindata = fp.read() |
| 3691 |
classes = [ |
| 3692 |
(MIMEApplication, ('',)), |
| 3693 |
(MIMEAudio, (audiodata,)), |
| 3694 |
(MIMEImage, (bindata,)), |
| 3695 |
(MIMEMessage, (Message(),)), |
| 3696 |
(MIMENonMultipart, ('multipart', 'mixed')), |
| 3697 |
(MIMEText, ('',)), |
| 3698 |
] |
| 3699 |
for cls, constructor in classes: |
| 3700 |
with self.subTest(cls=cls.__name__, policy='compat32'): |
| 3701 |
m = cls(*constructor) |
| 3702 |
self.assertIs(m.policy, email.policy.compat32) |
| 3703 |
with self.subTest(cls=cls.__name__, policy='default'): |
| 3704 |
m = cls(*constructor, policy=email.policy.default) |
| 3705 |
self.assertIs(m.policy, email.policy.default) |
| 3706 |
|
| 3707 |
def test_iter_escaped_chars(self): |
| 3708 |
self.assertEqual(list(utils._iter_escaped_chars(r'a\\b\"c\\"d')), |
| 3709 |
[(0, 'a'), |
| 3710 |
(2, '\\\\'), |
| 3711 |
(3, 'b'), |
| 3712 |
(5, '\\"'), |
| 3713 |
(6, 'c'), |
| 3714 |
(8, '\\\\'), |
| 3715 |
(9, '"'), |
| 3716 |
(10, 'd')]) |
| 3717 |
self.assertEqual(list(utils._iter_escaped_chars('a\\')), |
| 3718 |
[(0, 'a'), (1, '\\')]) |
| 3719 |
|
| 3720 |
def test_strip_quoted_realnames(self): |
| 3721 |
def check(addr, expected): |
| 3722 |
self.assertEqual(utils._strip_quoted_realnames(addr), expected) |
| 3723 |
|
| 3724 |
check('"Jane Doe" <[email protected]>, "John Doe" <[email protected]>', |
| 3725 |
' <[email protected]>, <[email protected]>') |
| 3726 |
check(r'"Jane \"Doe\"." <[email protected]>', |
| 3727 |
' <[email protected]>') |
| 3728 |
|
| 3729 |
# special cases |
| 3730 |
check(r'before"name"after', 'beforeafter') |
| 3731 |
check(r'before"name"', 'before') |
| 3732 |
check(r'b"name"', 'b') # single char |
| 3733 |
check(r'"name"after', 'after') |
| 3734 |
check(r'"name"a', 'a') # single char |
| 3735 |
check(r'"name"', '') |
| 3736 |
|
| 3737 |
# no change |
| 3738 |
for addr in ( |
| 3739 |
'Jane Doe <[email protected]>, John Doe <[email protected]>', |
| 3740 |
'lone " quote', |
| 3741 |
): |
| 3742 |
self.assertEqual(utils._strip_quoted_realnames(addr), addr) |
| 3743 |
|
| 3744 |
|
| 3745 |
def test_check_parenthesis(self): |
| 3746 |
addr = '[email protected]' |
| 3747 |
self.assertTrue(utils._check_parenthesis(f'{addr} (Alice)')) |
| 3748 |
self.assertFalse(utils._check_parenthesis(f'{addr} )Alice(')) |
| 3749 |
self.assertFalse(utils._check_parenthesis(f'{addr} (Alice))')) |
| 3750 |
self.assertFalse(utils._check_parenthesis(f'{addr} ((Alice)')) |
| 3751 |
|
| 3752 |
# Ignore real name between quotes |
| 3753 |
self.assertTrue(utils._check_parenthesis(f'")Alice((" {addr}')) |
| 3754 |
|
| 3755 |
|
| 3756 |
# Test the iterator/generators |
| 3757 |
class TestIterators(TestEmailBase): |
| 3758 |
def test_body_line_iterator(self): |
| 3759 |
eq = self.assertEqual |
| 3760 |
neq = self.ndiffAssertEqual |
| 3761 |
# First a simple non-multipart message |
| 3762 |
msg = self._msgobj('msg_01.txt') |
| 3763 |
it = iterators.body_line_iterator(msg) |
| 3764 |
lines = list(it) |
| 3765 |
eq(len(lines), 6) |
| 3766 |
neq(EMPTYSTRING.join(lines), msg.get_payload()) |
| 3767 |
# Now a more complicated multipart |
| 3768 |
msg = self._msgobj('msg_02.txt') |
| 3769 |
it = iterators.body_line_iterator(msg) |
| 3770 |
lines = list(it) |
| 3771 |
eq(len(lines), 43) |
| 3772 |
with openfile('msg_19.txt', encoding="utf-8") as fp: |
| 3773 |
neq(EMPTYSTRING.join(lines), fp.read()) |
| 3774 |
|
| 3775 |
def test_typed_subpart_iterator(self): |
| 3776 |
eq = self.assertEqual |
| 3777 |
msg = self._msgobj('msg_04.txt') |
| 3778 |
it = iterators.typed_subpart_iterator(msg, 'text') |
| 3779 |
lines = [] |
| 3780 |
subparts = 0 |
| 3781 |
for subpart in it: |
| 3782 |
subparts += 1 |
| 3783 |
lines.append(subpart.get_payload()) |
| 3784 |
eq(subparts, 2) |
| 3785 |
eq(EMPTYSTRING.join(lines), """\ |
| 3786 |
a simple kind of mirror |
| 3787 |
to reflect upon our own |
| 3788 |
a simple kind of mirror |
| 3789 |
to reflect upon our own |
| 3790 |
""") |
| 3791 |
|
| 3792 |
def test_typed_subpart_iterator_default_type(self): |
| 3793 |
eq = self.assertEqual |
| 3794 |
msg = self._msgobj('msg_03.txt') |
| 3795 |
it = iterators.typed_subpart_iterator(msg, 'text', 'plain') |
| 3796 |
lines = [] |
| 3797 |
subparts = 0 |
| 3798 |
for subpart in it: |
| 3799 |
subparts += 1 |
| 3800 |
lines.append(subpart.get_payload()) |
| 3801 |
eq(subparts, 1) |
| 3802 |
eq(EMPTYSTRING.join(lines), """\ |
| 3803 |
|
| 3804 |
Hi, |
| 3805 |
|
| 3806 |
Do you like this message? |
| 3807 |
|
| 3808 |
-Me |
| 3809 |
""") |
| 3810 |
|
| 3811 |
def test_pushCR_LF(self): |
| 3812 |
'''FeedParser BufferedSubFile.push() assumed it received complete |
| 3813 |
line endings. A CR ending one push() followed by a LF starting |
| 3814 |
the next push() added an empty line. |
| 3815 |
''' |
| 3816 |
imt = [ |
| 3817 |
("a\r \n", 2), |
| 3818 |
("b", 0), |
| 3819 |
("c\n", 1), |
| 3820 |
("", 0), |
| 3821 |
("d\r\n", 1), |
| 3822 |
("e\r", 0), |
| 3823 |
("\nf", 1), |
| 3824 |
("\r\n", 1), |
| 3825 |
] |
| 3826 |
from email.feedparser import BufferedSubFile, NeedMoreData |
| 3827 |
bsf = BufferedSubFile() |
| 3828 |
om = [] |
| 3829 |
nt = 0 |
| 3830 |
for il, n in imt: |
| 3831 |
bsf.push(il) |
| 3832 |
nt += n |
| 3833 |
n1 = 0 |
| 3834 |
for ol in iter(bsf.readline, NeedMoreData): |
| 3835 |
om.append(ol) |
| 3836 |
n1 += 1 |
| 3837 |
self.assertEqual(n, n1) |
| 3838 |
self.assertEqual(len(om), nt) |
| 3839 |
self.assertEqual(''.join([il for il, n in imt]), ''.join(om)) |
| 3840 |
|
| 3841 |
def test_push_random(self): |
| 3842 |
from email.feedparser import BufferedSubFile, NeedMoreData |
| 3843 |
|
| 3844 |
n = 10000 |
| 3845 |
chunksize = 5 |
| 3846 |
chars = 'abcd \t\r\n' |
| 3847 |
|
| 3848 |
s = ''.join(choice(chars) for i in range(n)) + '\n' |
| 3849 |
target = s.splitlines(True) |
| 3850 |
|
| 3851 |
bsf = BufferedSubFile() |
| 3852 |
lines = [] |
| 3853 |
for i in range(0, len(s), chunksize): |
| 3854 |
chunk = s[i:i+chunksize] |
| 3855 |
bsf.push(chunk) |
| 3856 |
lines.extend(iter(bsf.readline, NeedMoreData)) |
| 3857 |
self.assertEqual(lines, target) |
| 3858 |
|
| 3859 |
|
| 3860 |
class TestFeedParsers(TestEmailBase): |
| 3861 |
|
| 3862 |
def parse(self, chunks): |
| 3863 |
feedparser = FeedParser() |
| 3864 |
for chunk in chunks: |
| 3865 |
feedparser.feed(chunk) |
| 3866 |
return feedparser.close() |
| 3867 |
|
| 3868 |
def test_empty_header_name_handled(self): |
| 3869 |
# Issue 19996 |
| 3870 |
msg = self.parse("First: val\n: bad\nSecond: val") |
| 3871 |
self.assertEqual(msg['First'], 'val') |
| 3872 |
self.assertEqual(msg['Second'], 'val') |
| 3873 |
|
| 3874 |
def test_newlines(self): |
| 3875 |
m = self.parse(['a:\nb:\rc:\r\nd:\n']) |
| 3876 |
self.assertEqual(m.keys(), ['a', 'b', 'c', 'd']) |
| 3877 |
m = self.parse(['a:\nb:\rc:\r\nd:']) |
| 3878 |
self.assertEqual(m.keys(), ['a', 'b', 'c', 'd']) |
| 3879 |
m = self.parse(['a:\rb', 'c:\n']) |
| 3880 |
self.assertEqual(m.keys(), ['a', 'bc']) |
| 3881 |
m = self.parse(['a:\r', 'b:\n']) |
| 3882 |
self.assertEqual(m.keys(), ['a', 'b']) |
| 3883 |
m = self.parse(['a:\r', '\nb:\n']) |
| 3884 |
self.assertEqual(m.keys(), ['a', 'b']) |
| 3885 |
|
| 3886 |
# Only CR and LF should break header fields |
| 3887 |
m = self.parse(['a:\x85b:\u2028c:\n']) |
| 3888 |
self.assertEqual(m.items(), [('a', '\x85b:\u2028c:')]) |
| 3889 |
m = self.parse(['a:\r', 'b:\x85', 'c:\n']) |
| 3890 |
self.assertEqual(m.items(), [('a', ''), ('b', '\x85c:')]) |
| 3891 |
|
| 3892 |
def test_long_lines(self): |
| 3893 |
# Expected peak memory use on 32-bit platform: 6*N*M bytes. |
| 3894 |
M, N = 1000, 20000 |
| 3895 |
m = self.parse(['a:b\n\n'] + ['x'*M] * N) |
| 3896 |
self.assertEqual(m.items(), [('a', 'b')]) |
| 3897 |
self.assertEqual(m.get_payload(), 'x'*M*N) |
| 3898 |
m = self.parse(['a:b\r\r'] + ['x'*M] * N) |
| 3899 |
self.assertEqual(m.items(), [('a', 'b')]) |
| 3900 |
self.assertEqual(m.get_payload(), 'x'*M*N) |
| 3901 |
m = self.parse(['a:b\r\r'] + ['x'*M+'\x85'] * N) |
| 3902 |
self.assertEqual(m.items(), [('a', 'b')]) |
| 3903 |
self.assertEqual(m.get_payload(), ('x'*M+'\x85')*N) |
| 3904 |
m = self.parse(['a:\r', 'b: '] + ['x'*M] * N) |
| 3905 |
self.assertEqual(m.items(), [('a', ''), ('b', 'x'*M*N)]) |
| 3906 |
|
| 3907 |
|
| 3908 |
class TestParsers(TestEmailBase): |
| 3909 |
|
| 3910 |
def test_header_parser(self): |
| 3911 |
eq = self.assertEqual |
| 3912 |
# Parse only the headers of a complex multipart MIME document |
| 3913 |
with openfile('msg_02.txt', encoding="utf-8") as fp: |
| 3914 |
msg = HeaderParser().parse(fp) |
| 3915 |
eq(msg['from'], '[email protected]') |
| 3916 |
eq(msg['to'], '[email protected]') |
| 3917 |
eq(msg.get_content_type(), 'multipart/mixed') |
| 3918 |
self.assertFalse(msg.is_multipart()) |
| 3919 |
self.assertIsInstance(msg.get_payload(), str) |
| 3920 |
|
| 3921 |
def test_bytes_header_parser(self): |
| 3922 |
eq = self.assertEqual |
| 3923 |
# Parse only the headers of a complex multipart MIME document |
| 3924 |
with openfile('msg_02.txt', 'rb') as fp: |
| 3925 |
msg = email.parser.BytesHeaderParser().parse(fp) |
| 3926 |
eq(msg['from'], '[email protected]') |
| 3927 |
eq(msg['to'], '[email protected]') |
| 3928 |
eq(msg.get_content_type(), 'multipart/mixed') |
| 3929 |
self.assertFalse(msg.is_multipart()) |
| 3930 |
self.assertIsInstance(msg.get_payload(), str) |
| 3931 |
self.assertIsInstance(msg.get_payload(decode=True), bytes) |
| 3932 |
|
| 3933 |
def test_header_parser_multipart_is_valid(self): |
| 3934 |
# Don't flag valid multipart emails as having defects |
| 3935 |
with openfile('msg_47.txt', encoding="utf-8") as fp: |
| 3936 |
msgdata = fp.read() |
| 3937 |
|
| 3938 |
parser = email.parser.Parser(policy=email.policy.default) |
| 3939 |
parsed_msg = parser.parsestr(msgdata, headersonly=True) |
| 3940 |
|
| 3941 |
self.assertEqual(parsed_msg.defects, []) |
| 3942 |
|
| 3943 |
def test_bytes_parser_does_not_close_file(self): |
| 3944 |
with openfile('msg_02.txt', 'rb') as fp: |
| 3945 |
email.parser.BytesParser().parse(fp) |
| 3946 |
self.assertFalse(fp.closed) |
| 3947 |
|
| 3948 |
def test_bytes_parser_on_exception_does_not_close_file(self): |
| 3949 |
with openfile('msg_15.txt', 'rb') as fp: |
| 3950 |
bytesParser = email.parser.BytesParser |
| 3951 |
self.assertRaises(email.errors.StartBoundaryNotFoundDefect, |
| 3952 |
bytesParser(policy=email.policy.strict).parse, |
| 3953 |
fp) |
| 3954 |
self.assertFalse(fp.closed) |
| 3955 |
|
| 3956 |
def test_parser_does_not_close_file(self): |
| 3957 |
with openfile('msg_02.txt', encoding="utf-8") as fp: |
| 3958 |
email.parser.Parser().parse(fp) |
| 3959 |
self.assertFalse(fp.closed) |
| 3960 |
|
| 3961 |
def test_parser_on_exception_does_not_close_file(self): |
| 3962 |
with openfile('msg_15.txt', encoding="utf-8") as fp: |
| 3963 |
parser = email.parser.Parser |
| 3964 |
self.assertRaises(email.errors.StartBoundaryNotFoundDefect, |
| 3965 |
parser(policy=email.policy.strict).parse, fp) |
| 3966 |
self.assertFalse(fp.closed) |
| 3967 |
|
| 3968 |
def test_whitespace_continuation(self): |
| 3969 |
eq = self.assertEqual |
| 3970 |
# This message contains a line after the Subject: header that has only |
| 3971 |
# whitespace, but it is not empty! |
| 3972 |
msg = email.message_from_string("""\ |
| 3973 |
From: [email protected] |
| 3974 |
To: [email protected] |
| 3975 |
Subject: the next line has a space on it |
| 3976 |
\x20 |
| 3977 |
Date: Mon, 8 Apr 2002 15:09:19 -0400 |
| 3978 |
Message-ID: spam |
| 3979 |
|
| 3980 |
Here's the message body |
| 3981 |
""") |
| 3982 |
eq(msg['subject'], 'the next line has a space on it\n ') |
| 3983 |
eq(msg['message-id'], 'spam') |
| 3984 |
eq(msg.get_payload(), "Here's the message body\n") |
| 3985 |
|
| 3986 |
def test_whitespace_continuation_last_header(self): |
| 3987 |
eq = self.assertEqual |
| 3988 |
# Like the previous test, but the subject line is the last |
| 3989 |
# header. |
| 3990 |
msg = email.message_from_string("""\ |
| 3991 |
From: [email protected] |
| 3992 |
To: [email protected] |
| 3993 |
Date: Mon, 8 Apr 2002 15:09:19 -0400 |
| 3994 |
Message-ID: spam |
| 3995 |
Subject: the next line has a space on it |
| 3996 |
\x20 |
| 3997 |
|
| 3998 |
Here's the message body |
| 3999 |
""") |
| 4000 |
eq(msg['subject'], 'the next line has a space on it\n ') |
| 4001 |
eq(msg['message-id'], 'spam') |
| 4002 |
eq(msg.get_payload(), "Here's the message body\n") |
| 4003 |
|
| 4004 |
def test_crlf_separation(self): |
| 4005 |
eq = self.assertEqual |
| 4006 |
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as fp: |
| 4007 |
msg = Parser().parse(fp) |
| 4008 |
eq(len(msg.get_payload()), 2) |
| 4009 |
part1 = msg.get_payload(0) |
| 4010 |
eq(part1.get_content_type(), 'text/plain') |
| 4011 |
eq(part1.get_payload(), 'Simple email with attachment.\r\n\r\n') |
| 4012 |
part2 = msg.get_payload(1) |
| 4013 |
eq(part2.get_content_type(), 'application/riscos') |
| 4014 |
|
| 4015 |
def test_crlf_flatten(self): |
| 4016 |
# Using newline='\n' preserves the crlfs in this input file. |
| 4017 |
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as fp: |
| 4018 |
text = fp.read() |
| 4019 |
msg = email.message_from_string(text) |
| 4020 |
s = StringIO() |
| 4021 |
g = Generator(s) |
| 4022 |
g.flatten(msg, linesep='\r\n') |
| 4023 |
self.assertEqual(s.getvalue(), text) |
| 4024 |
|
| 4025 |
maxDiff = None |
| 4026 |
|
| 4027 |
def test_multipart_digest_with_extra_mime_headers(self): |
| 4028 |
eq = self.assertEqual |
| 4029 |
neq = self.ndiffAssertEqual |
| 4030 |
with openfile('msg_28.txt', encoding="utf-8") as fp: |
| 4031 |
msg = email.message_from_file(fp) |
| 4032 |
# Structure is: |
| 4033 |
# multipart/digest |
| 4034 |
# message/rfc822 |
| 4035 |
# text/plain |
| 4036 |
# message/rfc822 |
| 4037 |
# text/plain |
| 4038 |
eq(msg.is_multipart(), 1) |
| 4039 |
eq(len(msg.get_payload()), 2) |
| 4040 |
part1 = msg.get_payload(0) |
| 4041 |
eq(part1.get_content_type(), 'message/rfc822') |
| 4042 |
eq(part1.is_multipart(), 1) |
| 4043 |
eq(len(part1.get_payload()), 1) |
| 4044 |
part1a = part1.get_payload(0) |
| 4045 |
eq(part1a.is_multipart(), 0) |
| 4046 |
eq(part1a.get_content_type(), 'text/plain') |
| 4047 |
neq(part1a.get_payload(), 'message 1\n') |
| 4048 |
# next message/rfc822 |
| 4049 |
part2 = msg.get_payload(1) |
| 4050 |
eq(part2.get_content_type(), 'message/rfc822') |
| 4051 |
eq(part2.is_multipart(), 1) |
| 4052 |
eq(len(part2.get_payload()), 1) |
| 4053 |
part2a = part2.get_payload(0) |
| 4054 |
eq(part2a.is_multipart(), 0) |
| 4055 |
eq(part2a.get_content_type(), 'text/plain') |
| 4056 |
neq(part2a.get_payload(), 'message 2\n') |
| 4057 |
|
| 4058 |
def test_three_lines(self): |
| 4059 |
# A bug report by Andrew McNamara |
| 4060 |
lines = ['From: Andrew Person <[email protected]', |
| 4061 |
'Subject: Test', |
| 4062 |
'Date: Tue, 20 Aug 2002 16:43:45 +1000'] |
| 4063 |
msg = email.message_from_string(NL.join(lines)) |
| 4064 |
self.assertEqual(msg['date'], 'Tue, 20 Aug 2002 16:43:45 +1000') |
| 4065 |
|
| 4066 |
def test_strip_line_feed_and_carriage_return_in_headers(self): |
| 4067 |
eq = self.assertEqual |
| 4068 |
# For [ 1002475 ] email message parser doesn't handle \r\n correctly |
| 4069 |
value1 = 'text' |
| 4070 |
value2 = 'more text' |
| 4071 |
m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % ( |
| 4072 |
value1, value2) |
| 4073 |
msg = email.message_from_string(m) |
| 4074 |
eq(msg.get('Header'), value1) |
| 4075 |
eq(msg.get('Next-Header'), value2) |
| 4076 |
|
| 4077 |
def test_rfc2822_header_syntax(self): |
| 4078 |
eq = self.assertEqual |
| 4079 |
m = '>From: foo\nFrom: bar\n!"#QUX;~: zoo\n\nbody' |
| 4080 |
msg = email.message_from_string(m) |
| 4081 |
eq(len(msg), 3) |
| 4082 |
eq(sorted(field for field in msg), ['!"#QUX;~', '>From', 'From']) |
| 4083 |
eq(msg.get_payload(), 'body') |
| 4084 |
|
| 4085 |
def test_rfc2822_space_not_allowed_in_header(self): |
| 4086 |
eq = self.assertEqual |
| 4087 |
m = '>From [email protected] 11:25:53\nFrom: bar\n!"#QUX;~: zoo\n\nbody' |
| 4088 |
msg = email.message_from_string(m) |
| 4089 |
eq(len(msg.keys()), 0) |
| 4090 |
|
| 4091 |
def test_rfc2822_one_character_header(self): |
| 4092 |
eq = self.assertEqual |
| 4093 |
m = 'A: first header\nB: second header\nCC: third header\n\nbody' |
| 4094 |
msg = email.message_from_string(m) |
| 4095 |
headers = msg.keys() |
| 4096 |
headers.sort() |
| 4097 |
eq(headers, ['A', 'B', 'CC']) |
| 4098 |
eq(msg.get_payload(), 'body') |
| 4099 |
|
| 4100 |
def test_CRLFLF_at_end_of_part(self): |
| 4101 |
# issue 5610: feedparser should not eat two chars from body part ending |
| 4102 |
# with "\r\n\n". |
| 4103 |
m = ( |
| 4104 |
"From: [email protected]\n" |
| 4105 |
"To: baz\n" |
| 4106 |
"Mime-Version: 1.0\n" |
| 4107 |
"Content-Type: multipart/mixed; boundary=BOUNDARY\n" |
| 4108 |
"\n" |
| 4109 |
"--BOUNDARY\n" |
| 4110 |
"Content-Type: text/plain\n" |
| 4111 |
"\n" |
| 4112 |
"body ending with CRLF newline\r\n" |
| 4113 |
"\n" |
| 4114 |
"--BOUNDARY--\n" |
| 4115 |
) |
| 4116 |
msg = email.message_from_string(m) |
| 4117 |
self.assertEndsWith(msg.get_payload(0).get_payload(), '\r\n') |
| 4118 |
|
| 4119 |
|
| 4120 |
class Test8BitBytesHandling(TestEmailBase): |
| 4121 |
# In Python3 all input is string, but that doesn't work if the actual input |
| 4122 |
# uses an 8bit transfer encoding. To hack around that, in email 5.1 we |
| 4123 |
# decode byte streams using the surrogateescape error handler, and |
| 4124 |
# reconvert to binary at appropriate places if we detect surrogates. This |
| 4125 |
# doesn't allow us to transform headers with 8bit bytes (they get munged), |
| 4126 |
# but it does allow us to parse and preserve them, and to decode body |
| 4127 |
# parts that use an 8bit CTE. |
| 4128 |
|
| 4129 |
bodytest_msg = textwrap.dedent("""\ |
| 4130 |
From: [email protected] |
| 4131 |
To: baz |
| 4132 |
Mime-Version: 1.0 |
| 4133 |
Content-Type: text/plain; charset={charset} |
| 4134 |
Content-Transfer-Encoding: {cte} |
| 4135 |
|
| 4136 |
{bodyline} |
| 4137 |
""") |
| 4138 |
|
| 4139 |
def test_known_8bit_CTE(self): |
| 4140 |
m = self.bodytest_msg.format(charset='utf-8', |
| 4141 |
cte='8bit', |
| 4142 |
bodyline='pöstal').encode('utf-8') |
| 4143 |
msg = email.message_from_bytes(m) |
| 4144 |
self.assertEqual(msg.get_payload(), "pöstal\n") |
| 4145 |
self.assertEqual(msg.get_payload(decode=True), |
| 4146 |
"pöstal\n".encode('utf-8')) |
| 4147 |
|
| 4148 |
def test_unknown_8bit_CTE(self): |
| 4149 |
m = self.bodytest_msg.format(charset='notavalidcharset', |
| 4150 |
cte='8bit', |
| 4151 |
bodyline='pöstal').encode('utf-8') |
| 4152 |
msg = email.message_from_bytes(m) |
| 4153 |
self.assertEqual(msg.get_payload(), "p\uFFFD\uFFFDstal\n") |
| 4154 |
self.assertEqual(msg.get_payload(decode=True), |
| 4155 |
"pöstal\n".encode('utf-8')) |
| 4156 |
|
| 4157 |
def test_8bit_in_quopri_body(self): |
| 4158 |
# This is non-RFC compliant data...without 'decode' the library code |
| 4159 |
# decodes the body using the charset from the headers, and because the |
| 4160 |
# source byte really is utf-8 this works. This is likely to fail |
| 4161 |
# against real dirty data (ie: produce mojibake), but the data is |
| 4162 |
# invalid anyway so it is as good a guess as any. But this means that |
| 4163 |
# this test just confirms the current behavior; that behavior is not |
| 4164 |
# necessarily the best possible behavior. With 'decode' it is |
| 4165 |
# returning the raw bytes, so that test should be of correct behavior, |
| 4166 |
# or at least produce the same result that email4 did. |
| 4167 |
m = self.bodytest_msg.format(charset='utf-8', |
| 4168 |
cte='quoted-printable', |
| 4169 |
bodyline='p=C3=B6stál').encode('utf-8') |
| 4170 |
msg = email.message_from_bytes(m) |
| 4171 |
self.assertEqual(msg.get_payload(), 'p=C3=B6stál\n') |
| 4172 |
self.assertEqual(msg.get_payload(decode=True), |
| 4173 |
'pöstál\n'.encode('utf-8')) |
| 4174 |
|
| 4175 |
def test_invalid_8bit_in_non_8bit_cte_uses_replace(self): |
| 4176 |
# This is similar to the previous test, but proves that if the 8bit |
| 4177 |
# byte is undecodeable in the specified charset, it gets replaced |
| 4178 |
# by the unicode 'unknown' character. Again, this may or may not |
| 4179 |
# be the ideal behavior. Note that if decode=False none of the |
| 4180 |
# decoders will get involved, so this is the only test we need |
| 4181 |
# for this behavior. |
| 4182 |
m = self.bodytest_msg.format(charset='ascii', |
| 4183 |
cte='quoted-printable', |
| 4184 |
bodyline='p=C3=B6stál').encode('utf-8') |
| 4185 |
msg = email.message_from_bytes(m) |
| 4186 |
self.assertEqual(msg.get_payload(), 'p=C3=B6st\uFFFD\uFFFDl\n') |
| 4187 |
self.assertEqual(msg.get_payload(decode=True), |
| 4188 |
'pöstál\n'.encode('utf-8')) |
| 4189 |
|
| 4190 |
# test_defect_handling:test_invalid_chars_in_base64_payload |
| 4191 |
def test_8bit_in_base64_body(self): |
| 4192 |
# If we get 8bit bytes in a base64 body, we can just ignore them |
| 4193 |
# as being outside the base64 alphabet and decode anyway. But |
| 4194 |
# we register a defect. |
| 4195 |
m = self.bodytest_msg.format(charset='utf-8', |
| 4196 |
cte='base64', |
| 4197 |
bodyline='cMO2c3RhbAá=').encode('utf-8') |
| 4198 |
msg = email.message_from_bytes(m) |
| 4199 |
self.assertEqual(msg.get_payload(decode=True), |
| 4200 |
'pöstal'.encode('utf-8')) |
| 4201 |
self.assertIsInstance(msg.defects[0], |
| 4202 |
errors.InvalidBase64CharactersDefect) |
| 4203 |
|
| 4204 |
def test_8bit_in_uuencode_body(self): |
| 4205 |
# Sticking an 8bit byte in a uuencode block makes it undecodable by |
| 4206 |
# normal means, so the block is returned undecoded, but as bytes. |
| 4207 |
m = self.bodytest_msg.format(charset='utf-8', |
| 4208 |
cte='uuencode', |
| 4209 |
bodyline='<,.V<W1A; á ').encode('utf-8') |
| 4210 |
msg = email.message_from_bytes(m) |
| 4211 |
self.assertEqual(msg.get_payload(decode=True), |
| 4212 |
'<,.V<W1A; á \n'.encode('utf-8')) |
| 4213 |
|
| 4214 |
def test_rfc2231_charset_8bit_CTE(self): |
| 4215 |
m = textwrap.dedent("""\ |
| 4216 |
From: [email protected] |
| 4217 |
To: baz |
| 4218 |
Mime-Version: 1.0 |
| 4219 |
Content-Type: text/plain; charset*=ansi-x3.4-1968''utf-8 |
| 4220 |
Content-Transfer-Encoding: 8bit |
| 4221 |
|
| 4222 |
pöstal |
| 4223 |
""").encode('utf-8') |
| 4224 |
msg = email.message_from_bytes(m) |
| 4225 |
self.assertEqual(msg.get_payload(), "pöstal\n") |
| 4226 |
self.assertEqual(msg.get_payload(decode=True), |
| 4227 |
"pöstal\n".encode('utf-8')) |
| 4228 |
|
| 4229 |
|
| 4230 |
headertest_headers = ( |
| 4231 |
('From: [email protected]', ('From', '[email protected]')), |
| 4232 |
('To: báz', ('To', '=?unknown-8bit?q?b=C3=A1z?=')), |
| 4233 |
('Subject: Maintenant je vous présente mon collègue, le pouf célèbre\n' |
| 4234 |
'\tJean de Baddie', |
| 4235 |
('Subject', '=?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_' |
| 4236 |
'coll=C3=A8gue=2C_le_pouf_c=C3=A9l=C3=A8bre?=\n' |
| 4237 |
' =?unknown-8bit?q?_Jean_de_Baddie?=')), |
| 4238 |
('From: göst', ('From', '=?unknown-8bit?b?Z8O2c3Q=?=')), |
| 4239 |
) |
| 4240 |
headertest_msg = ('\n'.join([src for (src, _) in headertest_headers]) + |
| 4241 |
'\nYes, they are flying.\n').encode('utf-8') |
| 4242 |
|
| 4243 |
def test_get_8bit_header(self): |
| 4244 |
msg = email.message_from_bytes(self.headertest_msg) |
| 4245 |
self.assertEqual(str(msg.get('to')), 'b\uFFFD\uFFFDz') |
| 4246 |
self.assertEqual(str(msg['to']), 'b\uFFFD\uFFFDz') |
| 4247 |
|
| 4248 |
def test_print_8bit_headers(self): |
| 4249 |
msg = email.message_from_bytes(self.headertest_msg) |
| 4250 |
self.assertEqual(str(msg), |
| 4251 |
textwrap.dedent("""\ |
| 4252 |
From: {} |
| 4253 |
To: {} |
| 4254 |
Subject: {} |
| 4255 |
From: {} |
| 4256 |
|
| 4257 |
Yes, they are flying. |
| 4258 |
""").format(*[expected[1] for (_, expected) in |
| 4259 |
self.headertest_headers])) |
| 4260 |
|
| 4261 |
def test_values_with_8bit_headers(self): |
| 4262 |
msg = email.message_from_bytes(self.headertest_msg) |
| 4263 |
self.assertListEqual([str(x) for x in msg.values()], |
| 4264 |
['[email protected]', |
| 4265 |
'b\uFFFD\uFFFDz', |
| 4266 |
'Maintenant je vous pr\uFFFD\uFFFDsente mon ' |
| 4267 |
'coll\uFFFD\uFFFDgue, le pouf ' |
| 4268 |
'c\uFFFD\uFFFDl\uFFFD\uFFFDbre\n' |
| 4269 |
'\tJean de Baddie', |
| 4270 |
"g\uFFFD\uFFFDst"]) |
| 4271 |
|
| 4272 |
def test_items_with_8bit_headers(self): |
| 4273 |
msg = email.message_from_bytes(self.headertest_msg) |
| 4274 |
self.assertListEqual([(str(x), str(y)) for (x, y) in msg.items()], |
| 4275 |
[('From', '[email protected]'), |
| 4276 |
('To', 'b\uFFFD\uFFFDz'), |
| 4277 |
('Subject', 'Maintenant je vous ' |
| 4278 |
'pr\uFFFD\uFFFDsente ' |
| 4279 |
'mon coll\uFFFD\uFFFDgue, le pouf ' |
| 4280 |
'c\uFFFD\uFFFDl\uFFFD\uFFFDbre\n' |
| 4281 |
'\tJean de Baddie'), |
| 4282 |
('From', 'g\uFFFD\uFFFDst')]) |
| 4283 |
|
| 4284 |
def test_get_all_with_8bit_headers(self): |
| 4285 |
msg = email.message_from_bytes(self.headertest_msg) |
| 4286 |
self.assertListEqual([str(x) for x in msg.get_all('from')], |
| 4287 |
['[email protected]', |
| 4288 |
'g\uFFFD\uFFFDst']) |
| 4289 |
|
| 4290 |
def test_get_content_type_with_8bit(self): |
| 4291 |
msg = email.message_from_bytes(textwrap.dedent("""\ |
| 4292 |
Content-Type: text/pl\xA7in; charset=utf-8 |
| 4293 |
""").encode('latin-1')) |
| 4294 |
self.assertEqual(msg.get_content_type(), "text/pl\uFFFDin") |
| 4295 |
self.assertEqual(msg.get_content_maintype(), "text") |
| 4296 |
self.assertEqual(msg.get_content_subtype(), "pl\uFFFDin") |
| 4297 |
|
| 4298 |
# test_headerregistry.TestContentTypeHeader.non_ascii_in_params |
| 4299 |
def test_get_params_with_8bit(self): |
| 4300 |
msg = email.message_from_bytes( |
| 4301 |
'X-Header: foo=\xa7ne; b\xa7r=two; baz=three\n'.encode('latin-1')) |
| 4302 |
self.assertEqual(msg.get_params(header='x-header'), |
| 4303 |
[('foo', '\uFFFDne'), ('b\uFFFDr', 'two'), ('baz', 'three')]) |
| 4304 |
self.assertEqual(msg.get_param('Foo', header='x-header'), '\uFFFdne') |
| 4305 |
# XXX: someday you might be able to get 'b\xa7r', for now you can't. |
| 4306 |
self.assertEqual(msg.get_param('b\xa7r', header='x-header'), None) |
| 4307 |
|
| 4308 |
# test_headerregistry.TestContentTypeHeader.non_ascii_in_rfc2231_value |
| 4309 |
def test_get_rfc2231_params_with_8bit(self): |
| 4310 |
msg = email.message_from_bytes(textwrap.dedent("""\ |
| 4311 |
Content-Type: text/plain; charset=us-ascii; |
| 4312 |
title*=us-ascii'en'This%20is%20not%20f\xa7n""" |
| 4313 |
).encode('latin-1')) |
| 4314 |
self.assertEqual(msg.get_param('title'), |
| 4315 |
('us-ascii', 'en', 'This is not f\uFFFDn')) |
| 4316 |
|
| 4317 |
def test_set_rfc2231_params_with_8bit(self): |
| 4318 |
msg = email.message_from_bytes(textwrap.dedent("""\ |
| 4319 |
Content-Type: text/plain; charset=us-ascii; |
| 4320 |
title*=us-ascii'en'This%20is%20not%20f\xa7n""" |
| 4321 |
).encode('latin-1')) |
| 4322 |
msg.set_param('title', 'test') |
| 4323 |
self.assertEqual(msg.get_param('title'), 'test') |
| 4324 |
|
| 4325 |
def test_del_rfc2231_params_with_8bit(self): |
| 4326 |
msg = email.message_from_bytes(textwrap.dedent("""\ |
| 4327 |
Content-Type: text/plain; charset=us-ascii; |
| 4328 |
title*=us-ascii'en'This%20is%20not%20f\xa7n""" |
| 4329 |
).encode('latin-1')) |
| 4330 |
msg.del_param('title') |
| 4331 |
self.assertEqual(msg.get_param('title'), None) |
| 4332 |
self.assertEqual(msg.get_content_maintype(), 'text') |
| 4333 |
|
| 4334 |
def test_get_payload_with_8bit_cte_header(self): |
| 4335 |
msg = email.message_from_bytes(textwrap.dedent("""\ |
| 4336 |
Content-Transfer-Encoding: b\xa7se64 |
| 4337 |
Content-Type: text/plain; charset=latin-1 |
| 4338 |
|
| 4339 |
payload |
| 4340 |
""").encode('latin-1')) |
| 4341 |
self.assertEqual(msg.get_payload(), 'payload\n') |
| 4342 |
self.assertEqual(msg.get_payload(decode=True), b'payload\n') |
| 4343 |
|
| 4344 |
non_latin_bin_msg = textwrap.dedent("""\ |
| 4345 |
From: [email protected] |
| 4346 |
To: báz |
| 4347 |
Subject: Maintenant je vous présente mon collègue, le pouf célèbre |
| 4348 |
\tJean de Baddie |
| 4349 |
Mime-Version: 1.0 |
| 4350 |
Content-Type: text/plain; charset="utf-8" |
| 4351 |
Content-Transfer-Encoding: 8bit |
| 4352 |
|
| 4353 |
Да, они летят. |
| 4354 |
""").encode('utf-8') |
| 4355 |
|
| 4356 |
def test_bytes_generator(self): |
| 4357 |
msg = email.message_from_bytes(self.non_latin_bin_msg) |
| 4358 |
out = BytesIO() |
| 4359 |
email.generator.BytesGenerator(out).flatten(msg) |
| 4360 |
self.assertEqual(out.getvalue(), self.non_latin_bin_msg) |
| 4361 |
|
| 4362 |
def test_bytes_generator_handles_None_body(self): |
| 4363 |
#Issue 11019 |
| 4364 |
msg = email.message.Message() |
| 4365 |
out = BytesIO() |
| 4366 |
email.generator.BytesGenerator(out).flatten(msg) |
| 4367 |
self.assertEqual(out.getvalue(), b"\n") |
| 4368 |
|
| 4369 |
non_latin_bin_msg_as7bit_wrapped = textwrap.dedent("""\ |
| 4370 |
From: [email protected] |
| 4371 |
To: =?unknown-8bit?q?b=C3=A1z?= |
| 4372 |
Subject: =?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_coll=C3=A8gue?= |
| 4373 |
=?unknown-8bit?q?=2C_le_pouf_c=C3=A9l=C3=A8bre?= |
| 4374 |
=?unknown-8bit?q?_Jean_de_Baddie?= |
| 4375 |
Mime-Version: 1.0 |
| 4376 |
Content-Type: text/plain; charset="utf-8" |
| 4377 |
Content-Transfer-Encoding: base64 |
| 4378 |
|
| 4379 |
0JTQsCwg0L7QvdC4INC70LXRgtGP0YIuCg== |
| 4380 |
""") |
| 4381 |
|
| 4382 |
def test_generator_handles_8bit(self): |
| 4383 |
msg = email.message_from_bytes(self.non_latin_bin_msg) |
| 4384 |
out = StringIO() |
| 4385 |
email.generator.Generator(out).flatten(msg) |
| 4386 |
self.assertEqual(out.getvalue(), self.non_latin_bin_msg_as7bit_wrapped) |
| 4387 |
|
| 4388 |
def test_str_generator_should_not_mutate_msg_when_handling_8bit(self): |
| 4389 |
msg = email.message_from_bytes(self.non_latin_bin_msg) |
| 4390 |
out = BytesIO() |
| 4391 |
BytesGenerator(out).flatten(msg) |
| 4392 |
orig_value = out.getvalue() |
| 4393 |
Generator(StringIO()).flatten(msg) # Should not mutate msg! |
| 4394 |
out = BytesIO() |
| 4395 |
BytesGenerator(out).flatten(msg) |
| 4396 |
self.assertEqual(out.getvalue(), orig_value) |
| 4397 |
|
| 4398 |
def test_bytes_generator_with_unix_from(self): |
| 4399 |
# The unixfrom contains a current date, so we can't check it |
| 4400 |
# literally. Just make sure the first word is 'From' and the |
| 4401 |
# rest of the message matches the input. |
| 4402 |
msg = email.message_from_bytes(self.non_latin_bin_msg) |
| 4403 |
out = BytesIO() |
| 4404 |
email.generator.BytesGenerator(out).flatten(msg, unixfrom=True) |
| 4405 |
lines = out.getvalue().split(b'\n') |
| 4406 |
self.assertEqual(lines[0].split()[0], b'From') |
| 4407 |
self.assertEqual(b'\n'.join(lines[1:]), self.non_latin_bin_msg) |
| 4408 |
|
| 4409 |
non_latin_bin_msg_as7bit = non_latin_bin_msg_as7bit_wrapped.split('\n') |
| 4410 |
non_latin_bin_msg_as7bit[2:4] = [ |
| 4411 |
'Subject: =?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_' |
| 4412 |
'coll=C3=A8gue=2C_le_pouf_c=C3=A9l=C3=A8bre?='] |
| 4413 |
non_latin_bin_msg_as7bit = '\n'.join(non_latin_bin_msg_as7bit) |
| 4414 |
|
| 4415 |
def test_message_from_binary_file(self): |
| 4416 |
fn = 'test.msg' |
| 4417 |
self.addCleanup(unlink, fn) |
| 4418 |
with open(fn, 'wb') as testfile: |
| 4419 |
testfile.write(self.non_latin_bin_msg) |
| 4420 |
with open(fn, 'rb') as testfile: |
| 4421 |
m = email.parser.BytesParser().parse(testfile) |
| 4422 |
self.assertEqual(str(m), self.non_latin_bin_msg_as7bit) |
| 4423 |
|
| 4424 |
latin_bin_msg = textwrap.dedent("""\ |
| 4425 |
From: [email protected] |
| 4426 |
To: Dinsdale |
| 4427 |
Subject: Nudge nudge, wink, wink |
| 4428 |
Mime-Version: 1.0 |
| 4429 |
Content-Type: text/plain; charset="latin-1" |
| 4430 |
Content-Transfer-Encoding: 8bit |
| 4431 |
|
| 4432 |
oh là là, know what I mean, know what I mean? |
| 4433 |
""").encode('latin-1') |
| 4434 |
|
| 4435 |
latin_bin_msg_as7bit = textwrap.dedent("""\ |
| 4436 |
From: [email protected] |
| 4437 |
To: Dinsdale |
| 4438 |
Subject: Nudge nudge, wink, wink |
| 4439 |
Mime-Version: 1.0 |
| 4440 |
Content-Type: text/plain; charset="iso-8859-1" |
| 4441 |
Content-Transfer-Encoding: quoted-printable |
| 4442 |
|
| 4443 |
oh l=E0 l=E0, know what I mean, know what I mean? |
| 4444 |
""") |
| 4445 |
|
| 4446 |
def test_string_generator_reencodes_to_quopri_when_appropriate(self): |
| 4447 |
m = email.message_from_bytes(self.latin_bin_msg) |
| 4448 |
self.assertEqual(str(m), self.latin_bin_msg_as7bit) |
| 4449 |
|
| 4450 |
def test_decoded_generator_emits_unicode_body(self): |
| 4451 |
m = email.message_from_bytes(self.latin_bin_msg) |
| 4452 |
out = StringIO() |
| 4453 |
email.generator.DecodedGenerator(out).flatten(m) |
| 4454 |
#DecodedHeader output contains an extra blank line compared |
| 4455 |
#to the input message. RDM: not sure if this is a bug or not, |
| 4456 |
#but it is not specific to the 8bit->7bit conversion. |
| 4457 |
self.assertEqual(out.getvalue(), |
| 4458 |
self.latin_bin_msg.decode('latin-1')+'\n') |
| 4459 |
|
| 4460 |
def test_bytes_feedparser(self): |
| 4461 |
bfp = email.feedparser.BytesFeedParser() |
| 4462 |
for i in range(0, len(self.latin_bin_msg), 10): |
| 4463 |
bfp.feed(self.latin_bin_msg[i:i+10]) |
| 4464 |
m = bfp.close() |
| 4465 |
self.assertEqual(str(m), self.latin_bin_msg_as7bit) |
| 4466 |
|
| 4467 |
def test_crlf_flatten(self): |
| 4468 |
with openfile('msg_26.txt', 'rb') as fp: |
| 4469 |
text = fp.read() |
| 4470 |
msg = email.message_from_bytes(text) |
| 4471 |
s = BytesIO() |
| 4472 |
g = email.generator.BytesGenerator(s) |
| 4473 |
g.flatten(msg, linesep='\r\n') |
| 4474 |
self.assertEqual(s.getvalue(), text) |
| 4475 |
|
| 4476 |
def test_8bit_multipart(self): |
| 4477 |
# Issue 11605 |
| 4478 |
source = textwrap.dedent("""\ |
| 4479 |
Date: Fri, 18 Mar 2011 17:15:43 +0100 |
| 4480 |
To: [email protected] |
| 4481 |
From: foodwatch-Newsletter <[email protected]> |
| 4482 |
Subject: Aktuelles zu Japan, Klonfleisch und Smiley-System |
| 4483 |
Message-ID: <[email protected]> |
| 4484 |
MIME-Version: 1.0 |
| 4485 |
Content-Type: multipart/alternative; |
| 4486 |
boundary="b1_76a486bee62b0d200f33dc2ca08220ad" |
| 4487 |
|
| 4488 |
--b1_76a486bee62b0d200f33dc2ca08220ad |
| 4489 |
Content-Type: text/plain; charset="utf-8" |
| 4490 |
Content-Transfer-Encoding: 8bit |
| 4491 |
|
| 4492 |
Guten Tag, , |
| 4493 |
|
| 4494 |
mit großer Betroffenheit verfolgen auch wir im foodwatch-Team die |
| 4495 |
Nachrichten aus Japan. |
| 4496 |
|
| 4497 |
|
| 4498 |
--b1_76a486bee62b0d200f33dc2ca08220ad |
| 4499 |
Content-Type: text/html; charset="utf-8" |
| 4500 |
Content-Transfer-Encoding: 8bit |
| 4501 |
|
| 4502 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
| 4503 |
"http://www.w3.org/TR/html4/loose.dtd"> |
| 4504 |
<html lang="de"> |
| 4505 |
<head> |
| 4506 |
<title>foodwatch - Newsletter</title> |
| 4507 |
</head> |
| 4508 |
<body> |
| 4509 |
<p>mit großer Betroffenheit verfolgen auch wir im foodwatch-Team |
| 4510 |
die Nachrichten aus Japan.</p> |
| 4511 |
</body> |
| 4512 |
</html> |
| 4513 |
--b1_76a486bee62b0d200f33dc2ca08220ad-- |
| 4514 |
|
| 4515 |
""").encode('utf-8') |
| 4516 |
msg = email.message_from_bytes(source) |
| 4517 |
s = BytesIO() |
| 4518 |
g = email.generator.BytesGenerator(s) |
| 4519 |
g.flatten(msg) |
| 4520 |
self.assertEqual(s.getvalue(), source) |
| 4521 |
|
| 4522 |
def test_bytes_generator_b_encoding_linesep(self): |
| 4523 |
# Issue 14062: b encoding was tacking on an extra \n. |
| 4524 |
m = Message() |
| 4525 |
# This has enough non-ascii that it should always end up b encoded. |
| 4526 |
m['Subject'] = Header('žluťoučký kůň') |
| 4527 |
s = BytesIO() |
| 4528 |
g = email.generator.BytesGenerator(s) |
| 4529 |
g.flatten(m, linesep='\r\n') |
| 4530 |
self.assertEqual( |
| 4531 |
s.getvalue(), |
| 4532 |
b'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n') |
| 4533 |
|
| 4534 |
def test_generator_b_encoding_linesep(self): |
| 4535 |
# Since this broke in ByteGenerator, test Generator for completeness. |
| 4536 |
m = Message() |
| 4537 |
# This has enough non-ascii that it should always end up b encoded. |
| 4538 |
m['Subject'] = Header('žluťoučký kůň') |
| 4539 |
s = StringIO() |
| 4540 |
g = email.generator.Generator(s) |
| 4541 |
g.flatten(m, linesep='\r\n') |
| 4542 |
self.assertEqual( |
| 4543 |
s.getvalue(), |
| 4544 |
'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n') |
| 4545 |
|
| 4546 |
maxDiff = None |
| 4547 |
|
| 4548 |
|
| 4549 |
class BaseTestBytesGeneratorIdempotent: |
| 4550 |
|
| 4551 |
maxDiff = None |
| 4552 |
|
| 4553 |
def _msgobj(self, filename): |
| 4554 |
with openfile(filename, 'rb') as fp: |
| 4555 |
data = fp.read() |
| 4556 |
data = self.normalize_linesep_regex.sub(self.blinesep, data) |
| 4557 |
msg = email.message_from_bytes(data) |
| 4558 |
return msg, data |
| 4559 |
|
| 4560 |
def _idempotent(self, msg, data, unixfrom=False): |
| 4561 |
b = BytesIO() |
| 4562 |
g = email.generator.BytesGenerator(b, maxheaderlen=0) |
| 4563 |
g.flatten(msg, unixfrom=unixfrom, linesep=self.linesep) |
| 4564 |
self.assertEqual(data, b.getvalue()) |
| 4565 |
|
| 4566 |
|
| 4567 |
class TestBytesGeneratorIdempotentNL(BaseTestBytesGeneratorIdempotent, |
| 4568 |
TestIdempotent): |
| 4569 |
linesep = '\n' |
| 4570 |
blinesep = b'\n' |
| 4571 |
normalize_linesep_regex = re.compile(br'\r\n') |
| 4572 |
|
| 4573 |
|
| 4574 |
class TestBytesGeneratorIdempotentCRLF(BaseTestBytesGeneratorIdempotent, |
| 4575 |
TestIdempotent): |
| 4576 |
linesep = '\r\n' |
| 4577 |
blinesep = b'\r\n' |
| 4578 |
normalize_linesep_regex = re.compile(br'(?<!\r)\n') |
| 4579 |
|
| 4580 |
|
| 4581 |
class TestBase64(unittest.TestCase): |
| 4582 |
def test_len(self): |
| 4583 |
eq = self.assertEqual |
| 4584 |
eq(base64mime.header_length('hello'), |
| 4585 |
len(base64mime.body_encode(b'hello', eol=''))) |
| 4586 |
for size in range(15): |
| 4587 |
if size == 0 : bsize = 0 |
| 4588 |
elif size <= 3 : bsize = 4 |
| 4589 |
elif size <= 6 : bsize = 8 |
| 4590 |
elif size <= 9 : bsize = 12 |
| 4591 |
elif size <= 12: bsize = 16 |
| 4592 |
else : bsize = 20 |
| 4593 |
eq(base64mime.header_length('x' * size), bsize) |
| 4594 |
|
| 4595 |
def test_decode(self): |
| 4596 |
eq = self.assertEqual |
| 4597 |
eq(base64mime.decode(''), b'') |
| 4598 |
eq(base64mime.decode('aGVsbG8='), b'hello') |
| 4599 |
|
| 4600 |
def test_encode(self): |
| 4601 |
eq = self.assertEqual |
| 4602 |
eq(base64mime.body_encode(b''), '') |
| 4603 |
eq(base64mime.body_encode(b'hello'), 'aGVsbG8=\n') |
| 4604 |
# Test the binary flag |
| 4605 |
eq(base64mime.body_encode(b'hello\n'), 'aGVsbG8K\n') |
| 4606 |
# Test the maxlinelen arg |
| 4607 |
eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40), """\ |
| 4608 |
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 4609 |
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 4610 |
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 4611 |
eHh4eCB4eHh4IA== |
| 4612 |
""") |
| 4613 |
# Test the eol argument |
| 4614 |
eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40, eol='\r\n'), |
| 4615 |
"""\ |
| 4616 |
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 4617 |
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 4618 |
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 4619 |
eHh4eCB4eHh4IA==\r |
| 4620 |
""") |
| 4621 |
|
| 4622 |
def test_header_encode(self): |
| 4623 |
eq = self.assertEqual |
| 4624 |
he = base64mime.header_encode |
| 4625 |
eq(he('hello'), '=?iso-8859-1?b?aGVsbG8=?=') |
| 4626 |
eq(he('hello\r\nworld'), '=?iso-8859-1?b?aGVsbG8NCndvcmxk?=') |
| 4627 |
eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=') |
| 4628 |
# Test the charset option |
| 4629 |
eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?b?aGVsbG8=?=') |
| 4630 |
eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=') |
| 4631 |
|
| 4632 |
|
| 4633 |
class TestQuopri(unittest.TestCase): |
| 4634 |
def setUp(self): |
| 4635 |
# Set of characters (as byte integers) that don't need to be encoded |
| 4636 |
# in headers. |
| 4637 |
self.hlit = list(chain( |
| 4638 |
range(ord('a'), ord('z') + 1), |
| 4639 |
range(ord('A'), ord('Z') + 1), |
| 4640 |
range(ord('0'), ord('9') + 1), |
| 4641 |
(c for c in b'!*+-/'))) |
| 4642 |
# Set of characters (as byte integers) that do need to be encoded in |
| 4643 |
# headers. |
| 4644 |
self.hnon = [c for c in range(256) if c not in self.hlit] |
| 4645 |
assert len(self.hlit) + len(self.hnon) == 256 |
| 4646 |
# Set of characters (as byte integers) that don't need to be encoded |
| 4647 |
# in bodies. |
| 4648 |
self.blit = list(range(ord(' '), ord('~') + 1)) |
| 4649 |
self.blit.append(ord('\t')) |
| 4650 |
self.blit.remove(ord('=')) |
| 4651 |
# Set of characters (as byte integers) that do need to be encoded in |
| 4652 |
# bodies. |
| 4653 |
self.bnon = [c for c in range(256) if c not in self.blit] |
| 4654 |
assert len(self.blit) + len(self.bnon) == 256 |
| 4655 |
|
| 4656 |
def test_quopri_header_check(self): |
| 4657 |
for c in self.hlit: |
| 4658 |
self.assertFalse(quoprimime.header_check(c), |
| 4659 |
'Should not be header quopri encoded: %s' % chr(c)) |
| 4660 |
for c in self.hnon: |
| 4661 |
self.assertTrue(quoprimime.header_check(c), |
| 4662 |
'Should be header quopri encoded: %s' % chr(c)) |
| 4663 |
|
| 4664 |
def test_quopri_body_check(self): |
| 4665 |
for c in self.blit: |
| 4666 |
self.assertFalse(quoprimime.body_check(c), |
| 4667 |
'Should not be body quopri encoded: %s' % chr(c)) |
| 4668 |
for c in self.bnon: |
| 4669 |
self.assertTrue(quoprimime.body_check(c), |
| 4670 |
'Should be body quopri encoded: %s' % chr(c)) |
| 4671 |
|
| 4672 |
def test_header_quopri_len(self): |
| 4673 |
eq = self.assertEqual |
| 4674 |
eq(quoprimime.header_length(b'hello'), 5) |
| 4675 |
# RFC 2047 chrome is not included in header_length(). |
| 4676 |
eq(len(quoprimime.header_encode(b'hello', charset='xxx')), |
| 4677 |
quoprimime.header_length(b'hello') + |
| 4678 |
# =?xxx?q?...?= means 10 extra characters |
| 4679 |
10) |
| 4680 |
eq(quoprimime.header_length(b'h@e@l@l@o@'), 20) |
| 4681 |
# RFC 2047 chrome is not included in header_length(). |
| 4682 |
eq(len(quoprimime.header_encode(b'h@e@l@l@o@', charset='xxx')), |
| 4683 |
quoprimime.header_length(b'h@e@l@l@o@') + |
| 4684 |
# =?xxx?q?...?= means 10 extra characters |
| 4685 |
10) |
| 4686 |
for c in self.hlit: |
| 4687 |
eq(quoprimime.header_length(bytes([c])), 1, |
| 4688 |
'expected length 1 for %r' % chr(c)) |
| 4689 |
for c in self.hnon: |
| 4690 |
# Space is special; it's encoded to _ |
| 4691 |
if c == ord(' '): |
| 4692 |
continue |
| 4693 |
eq(quoprimime.header_length(bytes([c])), 3, |
| 4694 |
'expected length 3 for %r' % chr(c)) |
| 4695 |
eq(quoprimime.header_length(b' '), 1) |
| 4696 |
|
| 4697 |
def test_body_quopri_len(self): |
| 4698 |
eq = self.assertEqual |
| 4699 |
for c in self.blit: |
| 4700 |
eq(quoprimime.body_length(bytes([c])), 1) |
| 4701 |
for c in self.bnon: |
| 4702 |
eq(quoprimime.body_length(bytes([c])), 3) |
| 4703 |
|
| 4704 |
def test_quote_unquote_idempotent(self): |
| 4705 |
for x in range(256): |
| 4706 |
c = chr(x) |
| 4707 |
self.assertEqual(quoprimime.unquote(quoprimime.quote(c)), c) |
| 4708 |
|
| 4709 |
def _test_header_encode(self, header, expected_encoded_header, charset=None): |
| 4710 |
if charset is None: |
| 4711 |
encoded_header = quoprimime.header_encode(header) |
| 4712 |
else: |
| 4713 |
encoded_header = quoprimime.header_encode(header, charset) |
| 4714 |
self.assertEqual(encoded_header, expected_encoded_header) |
| 4715 |
|
| 4716 |
def test_header_encode_null(self): |
| 4717 |
self._test_header_encode(b'', '') |
| 4718 |
|
| 4719 |
def test_header_encode_one_word(self): |
| 4720 |
self._test_header_encode(b'hello', '=?iso-8859-1?q?hello?=') |
| 4721 |
|
| 4722 |
def test_header_encode_two_lines(self): |
| 4723 |
self._test_header_encode(b'hello\nworld', |
| 4724 |
'=?iso-8859-1?q?hello=0Aworld?=') |
| 4725 |
|
| 4726 |
def test_header_encode_non_ascii(self): |
| 4727 |
self._test_header_encode(b'hello\xc7there', |
| 4728 |
'=?iso-8859-1?q?hello=C7there?=') |
| 4729 |
|
| 4730 |
def test_header_encode_alt_charset(self): |
| 4731 |
self._test_header_encode(b'hello', '=?iso-8859-2?q?hello?=', |
| 4732 |
charset='iso-8859-2') |
| 4733 |
|
| 4734 |
def _test_header_decode(self, encoded_header, expected_decoded_header): |
| 4735 |
decoded_header = quoprimime.header_decode(encoded_header) |
| 4736 |
self.assertEqual(decoded_header, expected_decoded_header) |
| 4737 |
|
| 4738 |
def test_header_decode_null(self): |
| 4739 |
self._test_header_decode('', '') |
| 4740 |
|
| 4741 |
def test_header_decode_one_word(self): |
| 4742 |
self._test_header_decode('hello', 'hello') |
| 4743 |
|
| 4744 |
def test_header_decode_two_lines(self): |
| 4745 |
self._test_header_decode('hello=0Aworld', 'hello\nworld') |
| 4746 |
|
| 4747 |
def test_header_decode_non_ascii(self): |
| 4748 |
self._test_header_decode('hello=C7there', 'hello\xc7there') |
| 4749 |
|
| 4750 |
def test_header_decode_re_bug_18380(self): |
| 4751 |
# Issue 18380: Call re.sub with a positional argument for flags in the wrong position |
| 4752 |
self.assertEqual(quoprimime.header_decode('=30' * 257), '0' * 257) |
| 4753 |
|
| 4754 |
def _test_decode(self, encoded, expected_decoded, eol=None): |
| 4755 |
if eol is None: |
| 4756 |
decoded = quoprimime.decode(encoded) |
| 4757 |
else: |
| 4758 |
decoded = quoprimime.decode(encoded, eol=eol) |
| 4759 |
self.assertEqual(decoded, expected_decoded) |
| 4760 |
|
| 4761 |
def test_decode_null_word(self): |
| 4762 |
self._test_decode('', '') |
| 4763 |
|
| 4764 |
def test_decode_null_line_null_word(self): |
| 4765 |
self._test_decode('\r\n', '\n') |
| 4766 |
|
| 4767 |
def test_decode_one_word(self): |
| 4768 |
self._test_decode('hello', 'hello') |
| 4769 |
|
| 4770 |
def test_decode_one_word_eol(self): |
| 4771 |
self._test_decode('hello', 'hello', eol='X') |
| 4772 |
|
| 4773 |
def test_decode_one_line(self): |
| 4774 |
self._test_decode('hello\r\n', 'hello\n') |
| 4775 |
|
| 4776 |
def test_decode_one_line_lf(self): |
| 4777 |
self._test_decode('hello\n', 'hello\n') |
| 4778 |
|
| 4779 |
def test_decode_one_line_cr(self): |
| 4780 |
self._test_decode('hello\r', 'hello\n') |
| 4781 |
|
| 4782 |
def test_decode_one_line_nl(self): |
| 4783 |
self._test_decode('hello\n', 'helloX', eol='X') |
| 4784 |
|
| 4785 |
def test_decode_one_line_crnl(self): |
| 4786 |
self._test_decode('hello\r\n', 'helloX', eol='X') |
| 4787 |
|
| 4788 |
def test_decode_one_line_one_word(self): |
| 4789 |
self._test_decode('hello\r\nworld', 'hello\nworld') |
| 4790 |
|
| 4791 |
def test_decode_one_line_one_word_eol(self): |
| 4792 |
self._test_decode('hello\r\nworld', 'helloXworld', eol='X') |
| 4793 |
|
| 4794 |
def test_decode_two_lines(self): |
| 4795 |
self._test_decode('hello\r\nworld\r\n', 'hello\nworld\n') |
| 4796 |
|
| 4797 |
def test_decode_two_lines_eol(self): |
| 4798 |
self._test_decode('hello\r\nworld\r\n', 'helloXworldX', eol='X') |
| 4799 |
|
| 4800 |
def test_decode_one_long_line(self): |
| 4801 |
self._test_decode('Spam' * 250, 'Spam' * 250) |
| 4802 |
|
| 4803 |
def test_decode_one_space(self): |
| 4804 |
self._test_decode(' ', '') |
| 4805 |
|
| 4806 |
def test_decode_multiple_spaces(self): |
| 4807 |
self._test_decode(' ' * 5, '') |
| 4808 |
|
| 4809 |
def test_decode_one_line_trailing_spaces(self): |
| 4810 |
self._test_decode('hello \r\n', 'hello\n') |
| 4811 |
|
| 4812 |
def test_decode_two_lines_trailing_spaces(self): |
| 4813 |
self._test_decode('hello \r\nworld \r\n', 'hello\nworld\n') |
| 4814 |
|
| 4815 |
def test_decode_quoted_word(self): |
| 4816 |
self._test_decode('=22quoted=20words=22', '"quoted words"') |
| 4817 |
|
| 4818 |
def test_decode_uppercase_quoting(self): |
| 4819 |
self._test_decode('ab=CD=EF', 'ab\xcd\xef') |
| 4820 |
|
| 4821 |
def test_decode_lowercase_quoting(self): |
| 4822 |
self._test_decode('ab=cd=ef', 'ab\xcd\xef') |
| 4823 |
|
| 4824 |
def test_decode_soft_line_break(self): |
| 4825 |
self._test_decode('soft line=\r\nbreak', 'soft linebreak') |
| 4826 |
|
| 4827 |
def test_decode_false_quoting(self): |
| 4828 |
self._test_decode('A=1,B=A ==> A+B==2', 'A=1,B=A ==> A+B==2') |
| 4829 |
|
| 4830 |
def test_decode_crlf_eol_no_trailing_newline(self): |
| 4831 |
self._test_decode('abc', 'abc', eol='\r\n') |
| 4832 |
|
| 4833 |
def test_decode_crlf_eol_multiline_no_trailing_newline(self): |
| 4834 |
self._test_decode('a\r\nb', 'a\r\nb', eol='\r\n') |
| 4835 |
|
| 4836 |
def test_decode_crlf_eol_with_trailing_newline(self): |
| 4837 |
self._test_decode('abc\r\n', 'abc\r\n', eol='\r\n') |
| 4838 |
|
| 4839 |
def _test_encode(self, body, expected_encoded_body, maxlinelen=None, eol=None): |
| 4840 |
kwargs = {} |
| 4841 |
if maxlinelen is None: |
| 4842 |
# Use body_encode's default. |
| 4843 |
maxlinelen = 76 |
| 4844 |
else: |
| 4845 |
kwargs['maxlinelen'] = maxlinelen |
| 4846 |
if eol is None: |
| 4847 |
# Use body_encode's default. |
| 4848 |
eol = '\n' |
| 4849 |
else: |
| 4850 |
kwargs['eol'] = eol |
| 4851 |
encoded_body = quoprimime.body_encode(body, **kwargs) |
| 4852 |
self.assertEqual(encoded_body, expected_encoded_body) |
| 4853 |
if eol == '\n' or eol == '\r\n': |
| 4854 |
# We know how to split the result back into lines, so maxlinelen |
| 4855 |
# can be checked. |
| 4856 |
for line in encoded_body.splitlines(): |
| 4857 |
self.assertLessEqual(len(line), maxlinelen) |
| 4858 |
|
| 4859 |
def test_encode_null(self): |
| 4860 |
self._test_encode('', '') |
| 4861 |
|
| 4862 |
def test_encode_null_lines(self): |
| 4863 |
self._test_encode('\n\n', '\n\n') |
| 4864 |
|
| 4865 |
def test_encode_one_line(self): |
| 4866 |
self._test_encode('hello\n', 'hello\n') |
| 4867 |
|
| 4868 |
def test_encode_one_line_crlf(self): |
| 4869 |
self._test_encode('hello\r\n', 'hello\n') |
| 4870 |
|
| 4871 |
def test_encode_one_line_eol(self): |
| 4872 |
self._test_encode('hello\n', 'hello\r\n', eol='\r\n') |
| 4873 |
|
| 4874 |
def test_encode_one_line_eol_after_non_ascii(self): |
| 4875 |
# issue 20206; see changeset 0cf700464177 for why the encode/decode. |
| 4876 |
self._test_encode('hello\u03c5\n'.encode('utf-8').decode('latin1'), |
| 4877 |
'hello=CF=85\r\n', eol='\r\n') |
| 4878 |
|
| 4879 |
def test_encode_one_space(self): |
| 4880 |
self._test_encode(' ', '=20') |
| 4881 |
|
| 4882 |
def test_encode_one_line_one_space(self): |
| 4883 |
self._test_encode(' \n', '=20\n') |
| 4884 |
|
| 4885 |
# XXX: body_encode() expect strings, but uses ord(char) from these strings |
| 4886 |
# to index into a 256-entry list. For code points above 255, this will fail. |
| 4887 |
# Should there be a check for 8-bit only ord() values in body, or at least |
| 4888 |
# a comment about the expected input? |
| 4889 |
|
| 4890 |
def test_encode_two_lines_one_space(self): |
| 4891 |
self._test_encode(' \n \n', '=20\n=20\n') |
| 4892 |
|
| 4893 |
def test_encode_one_word_trailing_spaces(self): |
| 4894 |
self._test_encode('hello ', 'hello =20') |
| 4895 |
|
| 4896 |
def test_encode_one_line_trailing_spaces(self): |
| 4897 |
self._test_encode('hello \n', 'hello =20\n') |
| 4898 |
|
| 4899 |
def test_encode_one_word_trailing_tab(self): |
| 4900 |
self._test_encode('hello \t', 'hello =09') |
| 4901 |
|
| 4902 |
def test_encode_one_line_trailing_tab(self): |
| 4903 |
self._test_encode('hello \t\n', 'hello =09\n') |
| 4904 |
|
| 4905 |
def test_encode_trailing_space_before_maxlinelen(self): |
| 4906 |
self._test_encode('abcd \n1234', 'abcd =\n\n1234', maxlinelen=6) |
| 4907 |
|
| 4908 |
def test_encode_trailing_space_at_maxlinelen(self): |
| 4909 |
self._test_encode('abcd \n1234', 'abcd=\n=20\n1234', maxlinelen=5) |
| 4910 |
|
| 4911 |
def test_encode_trailing_space_beyond_maxlinelen(self): |
| 4912 |
self._test_encode('abcd \n1234', 'abc=\nd=20\n1234', maxlinelen=4) |
| 4913 |
|
| 4914 |
def test_encode_whitespace_lines(self): |
| 4915 |
self._test_encode(' \n' * 5, '=20\n' * 5) |
| 4916 |
|
| 4917 |
def test_encode_quoted_equals(self): |
| 4918 |
self._test_encode('a = b', 'a =3D b') |
| 4919 |
|
| 4920 |
def test_encode_one_long_string(self): |
| 4921 |
self._test_encode('x' * 100, 'x' * 75 + '=\n' + 'x' * 25) |
| 4922 |
|
| 4923 |
def test_encode_one_long_line(self): |
| 4924 |
self._test_encode('x' * 100 + '\n', 'x' * 75 + '=\n' + 'x' * 25 + '\n') |
| 4925 |
|
| 4926 |
def test_encode_one_very_long_line(self): |
| 4927 |
self._test_encode('x' * 200 + '\n', |
| 4928 |
2 * ('x' * 75 + '=\n') + 'x' * 50 + '\n') |
| 4929 |
|
| 4930 |
def test_encode_shortest_maxlinelen(self): |
| 4931 |
self._test_encode('=' * 5, '=3D=\n' * 4 + '=3D', maxlinelen=4) |
| 4932 |
|
| 4933 |
def test_encode_maxlinelen_too_small(self): |
| 4934 |
self.assertRaises(ValueError, self._test_encode, '', '', maxlinelen=3) |
| 4935 |
|
| 4936 |
def test_encode(self): |
| 4937 |
eq = self.assertEqual |
| 4938 |
eq(quoprimime.body_encode(''), '') |
| 4939 |
eq(quoprimime.body_encode('hello'), 'hello') |
| 4940 |
# Test the binary flag |
| 4941 |
eq(quoprimime.body_encode('hello\r\nworld'), 'hello\nworld') |
| 4942 |
# Test the maxlinelen arg |
| 4943 |
eq(quoprimime.body_encode('xxxx ' * 20, maxlinelen=40), """\ |
| 4944 |
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx= |
| 4945 |
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx= |
| 4946 |
x xxxx xxxx xxxx xxxx=20""") |
| 4947 |
# Test the eol argument |
| 4948 |
eq(quoprimime.body_encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), |
| 4949 |
"""\ |
| 4950 |
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=\r |
| 4951 |
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=\r |
| 4952 |
x xxxx xxxx xxxx xxxx=20""") |
| 4953 |
eq(quoprimime.body_encode("""\ |
| 4954 |
one line |
| 4955 |
|
| 4956 |
two line"""), """\ |
| 4957 |
one line |
| 4958 |
|
| 4959 |
two line""") |
| 4960 |
|
| 4961 |
|
| 4962 |
|
| 4963 |
# Test the Charset class |
| 4964 |
class TestCharset(unittest.TestCase): |
| 4965 |
def tearDown(self): |
| 4966 |
from email import charset as CharsetModule |
| 4967 |
try: |
| 4968 |
del CharsetModule.CHARSETS['fake'] |
| 4969 |
except KeyError: |
| 4970 |
pass |
| 4971 |
|
| 4972 |
def test_codec_encodeable(self): |
| 4973 |
eq = self.assertEqual |
| 4974 |
# Make sure us-ascii = no Unicode conversion |
| 4975 |
c = Charset('us-ascii') |
| 4976 |
eq(c.header_encode('Hello World!'), 'Hello World!') |
| 4977 |
# Test 8-bit idempotency with us-ascii |
| 4978 |
s = '\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa' |
| 4979 |
self.assertRaises(UnicodeError, c.header_encode, s) |
| 4980 |
c = Charset('utf-8') |
| 4981 |
eq(c.header_encode(s), '=?utf-8?b?wqTCosKkwqTCpMKmwqTCqMKkwqo=?=') |
| 4982 |
|
| 4983 |
def test_body_encode(self): |
| 4984 |
eq = self.assertEqual |
| 4985 |
# Try a charset with QP body encoding |
| 4986 |
c = Charset('iso-8859-1') |
| 4987 |
eq('hello w=F6rld', c.body_encode('hello w\xf6rld')) |
| 4988 |
# Try a charset with Base64 body encoding |
| 4989 |
c = Charset('utf-8') |
| 4990 |
eq('aGVsbG8gd29ybGQ=\n', c.body_encode(b'hello world')) |
| 4991 |
# Try a charset with None body encoding |
| 4992 |
c = Charset('us-ascii') |
| 4993 |
eq('hello world', c.body_encode('hello world')) |
| 4994 |
# Try the convert argument, where input codec != output codec |
| 4995 |
c = Charset('euc-jp') |
| 4996 |
# With apologies to Tokio Kikuchi ;) |
| 4997 |
eq('\x1b$B5FCO;~IW\x1b(B', |
| 4998 |
c.body_encode('\u83ca\u5730\u6642\u592b')) |
| 4999 |
# Testing SF bug #625509, which we have to fake, since there are no |
| 5000 |
# built-in encodings where the header encoding is QP but the body |
| 5001 |
# encoding is not. |
| 5002 |
from email import charset as CharsetModule |
| 5003 |
CharsetModule.add_charset('fake', CharsetModule.QP, None, 'utf-8') |
| 5004 |
c = Charset('fake') |
| 5005 |
eq('hello world', c.body_encode('hello world')) |
| 5006 |
|
| 5007 |
def test_unicode_charset_name(self): |
| 5008 |
charset = Charset('us-ascii') |
| 5009 |
self.assertEqual(str(charset), 'us-ascii') |
| 5010 |
self.assertRaises(errors.CharsetError, Charset, 'asc\xffii') |
| 5011 |
|
| 5012 |
|
| 5013 |
|
| 5014 |
# Test multilingual MIME headers. |
| 5015 |
class TestHeader(TestEmailBase): |
| 5016 |
def test_simple(self): |
| 5017 |
eq = self.ndiffAssertEqual |
| 5018 |
h = Header('Hello World!') |
| 5019 |
eq(h.encode(), 'Hello World!') |
| 5020 |
h.append(' Goodbye World!') |
| 5021 |
eq(h.encode(), 'Hello World! Goodbye World!') |
| 5022 |
|
| 5023 |
def test_simple_surprise(self): |
| 5024 |
eq = self.ndiffAssertEqual |
| 5025 |
h = Header('Hello World!') |
| 5026 |
eq(h.encode(), 'Hello World!') |
| 5027 |
h.append('Goodbye World!') |
| 5028 |
eq(h.encode(), 'Hello World! Goodbye World!') |
| 5029 |
|
| 5030 |
def test_header_needs_no_decoding(self): |
| 5031 |
h = 'no decoding needed' |
| 5032 |
self.assertEqual(decode_header(h), [(h, None)]) |
| 5033 |
|
| 5034 |
def test_long(self): |
| 5035 |
h = Header("I am the very model of a modern Major-General; I've information vegetable, animal, and mineral; I know the kings of England, and I quote the fights historical from Marathon to Waterloo, in order categorical; I'm very well acquainted, too, with matters mathematical; I understand equations, both the simple and quadratical; about binomial theorem I'm teeming with a lot o' news, with many cheerful facts about the square of the hypotenuse.", |
| 5036 |
maxlinelen=76) |
| 5037 |
for l in h.encode(splitchars=' ').split('\n '): |
| 5038 |
self.assertLessEqual(len(l), 76) |
| 5039 |
|
| 5040 |
def test_multilingual(self): |
| 5041 |
eq = self.ndiffAssertEqual |
| 5042 |
g = Charset("iso-8859-1") |
| 5043 |
cz = Charset("iso-8859-2") |
| 5044 |
utf8 = Charset("utf-8") |
| 5045 |
g_head = (b'Die Mieter treten hier ein werden mit einem ' |
| 5046 |
b'Foerderband komfortabel den Korridor entlang, ' |
| 5047 |
b'an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, ' |
| 5048 |
b'gegen die rotierenden Klingen bef\xf6rdert. ') |
| 5049 |
cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich ' |
| 5050 |
b'd\xf9vtipu.. ') |
| 5051 |
utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 5052 |
'\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 5053 |
'\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 5054 |
'\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 5055 |
'\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das ' |
| 5056 |
'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder ' |
| 5057 |
'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066' |
| 5058 |
'\u3044\u307e\u3059\u3002') |
| 5059 |
h = Header(g_head, g) |
| 5060 |
h.append(cz_head, cz) |
| 5061 |
h.append(utf8_head, utf8) |
| 5062 |
enc = h.encode(maxlinelen=76) |
| 5063 |
eq(enc, """\ |
| 5064 |
=?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderband_kom?= |
| 5065 |
=?iso-8859-1?q?fortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen_Wand?= |
| 5066 |
=?iso-8859-1?q?gem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef=F6r?= |
| 5067 |
=?iso-8859-1?q?dert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutily?= |
| 5068 |
=?iso-8859-2?q?_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?= |
| 5069 |
=?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC?= |
| 5070 |
=?utf-8?b?5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn?= |
| 5071 |
=?utf-8?b?44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFz?= |
| 5072 |
=?utf-8?b?IE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5k?= |
| 5073 |
=?utf-8?b?IGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIA=?= |
| 5074 |
=?utf-8?b?44Gj44Gm44GE44G+44GZ44CC?=""") |
| 5075 |
decoded = decode_header(enc) |
| 5076 |
eq(len(decoded), 3) |
| 5077 |
eq(decoded[0], (g_head, 'iso-8859-1')) |
| 5078 |
eq(decoded[1], (cz_head, 'iso-8859-2')) |
| 5079 |
eq(decoded[2], (utf8_head.encode('utf-8'), 'utf-8')) |
| 5080 |
ustr = str(h) |
| 5081 |
eq(ustr, |
| 5082 |
(b'Die Mieter treten hier ein werden mit einem Foerderband ' |
| 5083 |
b'komfortabel den Korridor entlang, an s\xc3\xbcdl\xc3\xbcndischen ' |
| 5084 |
b'Wandgem\xc3\xa4lden vorbei, gegen die rotierenden Klingen ' |
| 5085 |
b'bef\xc3\xb6rdert. Finan\xc4\x8dni metropole se hroutily pod ' |
| 5086 |
b'tlakem jejich d\xc5\xafvtipu.. \xe6\xad\xa3\xe7\xa2\xba\xe3\x81' |
| 5087 |
b'\xab\xe8\xa8\x80\xe3\x81\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3' |
| 5088 |
b'\xe3\x81\xaf\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3' |
| 5089 |
b'\x81\xbe\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 5090 |
b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8\xaa\x9e' |
| 5091 |
b'\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81\xe3\x81\x82\xe3' |
| 5092 |
b'\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81\x9f\xe3\x82\x89\xe3\x82' |
| 5093 |
b'\x81\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\xe5\xae\x9f\xe9\x9a\x9b' |
| 5094 |
b'\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x8cWenn ist das Nunstuck git ' |
| 5095 |
b'und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt ' |
| 5096 |
b'gersput.\xe3\x80\x8d\xe3\x81\xa8\xe8\xa8\x80\xe3\x81\xa3\xe3\x81' |
| 5097 |
b'\xa6\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82' |
| 5098 |
).decode('utf-8')) |
| 5099 |
# Test make_header() |
| 5100 |
newh = make_header(decode_header(enc)) |
| 5101 |
eq(newh, h) |
| 5102 |
|
| 5103 |
def test_empty_header_encode(self): |
| 5104 |
h = Header() |
| 5105 |
self.assertEqual(h.encode(), '') |
| 5106 |
|
| 5107 |
def test_header_ctor_default_args(self): |
| 5108 |
eq = self.ndiffAssertEqual |
| 5109 |
h = Header() |
| 5110 |
eq(h, '') |
| 5111 |
h.append('foo', Charset('iso-8859-1')) |
| 5112 |
eq(h, 'foo') |
| 5113 |
|
| 5114 |
def test_explicit_maxlinelen(self): |
| 5115 |
eq = self.ndiffAssertEqual |
| 5116 |
hstr = ('A very long line that must get split to something other ' |
| 5117 |
'than at the 76th character boundary to test the non-default ' |
| 5118 |
'behavior') |
| 5119 |
h = Header(hstr) |
| 5120 |
eq(h.encode(), '''\ |
| 5121 |
A very long line that must get split to something other than at the 76th |
| 5122 |
character boundary to test the non-default behavior''') |
| 5123 |
eq(str(h), hstr) |
| 5124 |
h = Header(hstr, header_name='Subject') |
| 5125 |
eq(h.encode(), '''\ |
| 5126 |
A very long line that must get split to something other than at the |
| 5127 |
76th character boundary to test the non-default behavior''') |
| 5128 |
eq(str(h), hstr) |
| 5129 |
h = Header(hstr, maxlinelen=1024, header_name='Subject') |
| 5130 |
eq(h.encode(), hstr) |
| 5131 |
eq(str(h), hstr) |
| 5132 |
|
| 5133 |
def test_quopri_splittable(self): |
| 5134 |
eq = self.ndiffAssertEqual |
| 5135 |
h = Header(charset='iso-8859-1', maxlinelen=20) |
| 5136 |
x = 'xxxx ' * 20 |
| 5137 |
h.append(x) |
| 5138 |
s = h.encode() |
| 5139 |
eq(s, """\ |
| 5140 |
=?iso-8859-1?q?xxx?= |
| 5141 |
=?iso-8859-1?q?x_?= |
| 5142 |
=?iso-8859-1?q?xx?= |
| 5143 |
=?iso-8859-1?q?xx?= |
| 5144 |
=?iso-8859-1?q?_x?= |
| 5145 |
=?iso-8859-1?q?xx?= |
| 5146 |
=?iso-8859-1?q?x_?= |
| 5147 |
=?iso-8859-1?q?xx?= |
| 5148 |
=?iso-8859-1?q?xx?= |
| 5149 |
=?iso-8859-1?q?_x?= |
| 5150 |
=?iso-8859-1?q?xx?= |
| 5151 |
=?iso-8859-1?q?x_?= |
| 5152 |
=?iso-8859-1?q?xx?= |
| 5153 |
=?iso-8859-1?q?xx?= |
| 5154 |
=?iso-8859-1?q?_x?= |
| 5155 |
=?iso-8859-1?q?xx?= |
| 5156 |
=?iso-8859-1?q?x_?= |
| 5157 |
=?iso-8859-1?q?xx?= |
| 5158 |
=?iso-8859-1?q?xx?= |
| 5159 |
=?iso-8859-1?q?_x?= |
| 5160 |
=?iso-8859-1?q?xx?= |
| 5161 |
=?iso-8859-1?q?x_?= |
| 5162 |
=?iso-8859-1?q?xx?= |
| 5163 |
=?iso-8859-1?q?xx?= |
| 5164 |
=?iso-8859-1?q?_x?= |
| 5165 |
=?iso-8859-1?q?xx?= |
| 5166 |
=?iso-8859-1?q?x_?= |
| 5167 |
=?iso-8859-1?q?xx?= |
| 5168 |
=?iso-8859-1?q?xx?= |
| 5169 |
=?iso-8859-1?q?_x?= |
| 5170 |
=?iso-8859-1?q?xx?= |
| 5171 |
=?iso-8859-1?q?x_?= |
| 5172 |
=?iso-8859-1?q?xx?= |
| 5173 |
=?iso-8859-1?q?xx?= |
| 5174 |
=?iso-8859-1?q?_x?= |
| 5175 |
=?iso-8859-1?q?xx?= |
| 5176 |
=?iso-8859-1?q?x_?= |
| 5177 |
=?iso-8859-1?q?xx?= |
| 5178 |
=?iso-8859-1?q?xx?= |
| 5179 |
=?iso-8859-1?q?_x?= |
| 5180 |
=?iso-8859-1?q?xx?= |
| 5181 |
=?iso-8859-1?q?x_?= |
| 5182 |
=?iso-8859-1?q?xx?= |
| 5183 |
=?iso-8859-1?q?xx?= |
| 5184 |
=?iso-8859-1?q?_x?= |
| 5185 |
=?iso-8859-1?q?xx?= |
| 5186 |
=?iso-8859-1?q?x_?= |
| 5187 |
=?iso-8859-1?q?xx?= |
| 5188 |
=?iso-8859-1?q?xx?= |
| 5189 |
=?iso-8859-1?q?_?=""") |
| 5190 |
eq(x, str(make_header(decode_header(s)))) |
| 5191 |
h = Header(charset='iso-8859-1', maxlinelen=40) |
| 5192 |
h.append('xxxx ' * 20) |
| 5193 |
s = h.encode() |
| 5194 |
eq(s, """\ |
| 5195 |
=?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xxx?= |
| 5196 |
=?iso-8859-1?q?x_xxxx_xxxx_xxxx_xxxx_?= |
| 5197 |
=?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?= |
| 5198 |
=?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?= |
| 5199 |
=?iso-8859-1?q?_xxxx_xxxx_?=""") |
| 5200 |
eq(x, str(make_header(decode_header(s)))) |
| 5201 |
|
| 5202 |
def test_base64_splittable(self): |
| 5203 |
eq = self.ndiffAssertEqual |
| 5204 |
h = Header(charset='koi8-r', maxlinelen=20) |
| 5205 |
x = 'xxxx ' * 20 |
| 5206 |
h.append(x) |
| 5207 |
s = h.encode() |
| 5208 |
eq(s, """\ |
| 5209 |
=?koi8-r?b?eHh4?= |
| 5210 |
=?koi8-r?b?eCB4?= |
| 5211 |
=?koi8-r?b?eHh4?= |
| 5212 |
=?koi8-r?b?IHh4?= |
| 5213 |
=?koi8-r?b?eHgg?= |
| 5214 |
=?koi8-r?b?eHh4?= |
| 5215 |
=?koi8-r?b?eCB4?= |
| 5216 |
=?koi8-r?b?eHh4?= |
| 5217 |
=?koi8-r?b?IHh4?= |
| 5218 |
=?koi8-r?b?eHgg?= |
| 5219 |
=?koi8-r?b?eHh4?= |
| 5220 |
=?koi8-r?b?eCB4?= |
| 5221 |
=?koi8-r?b?eHh4?= |
| 5222 |
=?koi8-r?b?IHh4?= |
| 5223 |
=?koi8-r?b?eHgg?= |
| 5224 |
=?koi8-r?b?eHh4?= |
| 5225 |
=?koi8-r?b?eCB4?= |
| 5226 |
=?koi8-r?b?eHh4?= |
| 5227 |
=?koi8-r?b?IHh4?= |
| 5228 |
=?koi8-r?b?eHgg?= |
| 5229 |
=?koi8-r?b?eHh4?= |
| 5230 |
=?koi8-r?b?eCB4?= |
| 5231 |
=?koi8-r?b?eHh4?= |
| 5232 |
=?koi8-r?b?IHh4?= |
| 5233 |
=?koi8-r?b?eHgg?= |
| 5234 |
=?koi8-r?b?eHh4?= |
| 5235 |
=?koi8-r?b?eCB4?= |
| 5236 |
=?koi8-r?b?eHh4?= |
| 5237 |
=?koi8-r?b?IHh4?= |
| 5238 |
=?koi8-r?b?eHgg?= |
| 5239 |
=?koi8-r?b?eHh4?= |
| 5240 |
=?koi8-r?b?eCB4?= |
| 5241 |
=?koi8-r?b?eHh4?= |
| 5242 |
=?koi8-r?b?IA==?=""") |
| 5243 |
eq(x, str(make_header(decode_header(s)))) |
| 5244 |
h = Header(charset='koi8-r', maxlinelen=40) |
| 5245 |
h.append(x) |
| 5246 |
s = h.encode() |
| 5247 |
eq(s, """\ |
| 5248 |
=?koi8-r?b?eHh4eCB4eHh4IHh4eHggeHh4?= |
| 5249 |
=?koi8-r?b?eCB4eHh4IHh4eHggeHh4eCB4?= |
| 5250 |
=?koi8-r?b?eHh4IHh4eHggeHh4eCB4eHh4?= |
| 5251 |
=?koi8-r?b?IHh4eHggeHh4eCB4eHh4IHh4?= |
| 5252 |
=?koi8-r?b?eHggeHh4eCB4eHh4IHh4eHgg?= |
| 5253 |
=?koi8-r?b?eHh4eCB4eHh4IA==?=""") |
| 5254 |
eq(x, str(make_header(decode_header(s)))) |
| 5255 |
|
| 5256 |
def test_us_ascii_header(self): |
| 5257 |
eq = self.assertEqual |
| 5258 |
s = 'hello' |
| 5259 |
x = decode_header(s) |
| 5260 |
eq(x, [('hello', None)]) |
| 5261 |
h = make_header(x) |
| 5262 |
eq(s, h.encode()) |
| 5263 |
|
| 5264 |
def test_string_charset(self): |
| 5265 |
eq = self.assertEqual |
| 5266 |
h = Header() |
| 5267 |
h.append('hello', 'iso-8859-1') |
| 5268 |
eq(h, 'hello') |
| 5269 |
|
| 5270 |
## def test_unicode_error(self): |
| 5271 |
## raises = self.assertRaises |
| 5272 |
## raises(UnicodeError, Header, u'[P\xf6stal]', 'us-ascii') |
| 5273 |
## raises(UnicodeError, Header, '[P\xf6stal]', 'us-ascii') |
| 5274 |
## h = Header() |
| 5275 |
## raises(UnicodeError, h.append, u'[P\xf6stal]', 'us-ascii') |
| 5276 |
## raises(UnicodeError, h.append, '[P\xf6stal]', 'us-ascii') |
| 5277 |
## raises(UnicodeError, Header, u'\u83ca\u5730\u6642\u592b', 'iso-8859-1') |
| 5278 |
|
| 5279 |
def test_utf8_shortest(self): |
| 5280 |
eq = self.assertEqual |
| 5281 |
h = Header('p\xf6stal', 'utf-8') |
| 5282 |
eq(h.encode(), '=?utf-8?q?p=C3=B6stal?=') |
| 5283 |
h = Header('\u83ca\u5730\u6642\u592b', 'utf-8') |
| 5284 |
eq(h.encode(), '=?utf-8?b?6I+K5Zyw5pmC5aSr?=') |
| 5285 |
|
| 5286 |
def test_bad_8bit_header(self): |
| 5287 |
raises = self.assertRaises |
| 5288 |
eq = self.assertEqual |
| 5289 |
x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big' |
| 5290 |
raises(UnicodeError, Header, x) |
| 5291 |
h = Header() |
| 5292 |
raises(UnicodeError, h.append, x) |
| 5293 |
e = x.decode('utf-8', 'replace') |
| 5294 |
eq(str(Header(x, errors='replace')), e) |
| 5295 |
h.append(x, errors='replace') |
| 5296 |
eq(str(h), e) |
| 5297 |
|
| 5298 |
def test_escaped_8bit_header(self): |
| 5299 |
x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big' |
| 5300 |
e = x.decode('ascii', 'surrogateescape') |
| 5301 |
h = Header(e, charset=email.charset.UNKNOWN8BIT) |
| 5302 |
self.assertEqual(str(h), |
| 5303 |
'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big') |
| 5304 |
self.assertEqual(email.header.decode_header(h), [(x, 'unknown-8bit')]) |
| 5305 |
|
| 5306 |
def test_header_handles_binary_unknown8bit(self): |
| 5307 |
x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big' |
| 5308 |
h = Header(x, charset=email.charset.UNKNOWN8BIT) |
| 5309 |
self.assertEqual(str(h), |
| 5310 |
'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big') |
| 5311 |
self.assertEqual(email.header.decode_header(h), [(x, 'unknown-8bit')]) |
| 5312 |
|
| 5313 |
def test_make_header_handles_binary_unknown8bit(self): |
| 5314 |
x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big' |
| 5315 |
h = Header(x, charset=email.charset.UNKNOWN8BIT) |
| 5316 |
h2 = email.header.make_header(email.header.decode_header(h)) |
| 5317 |
self.assertEqual(str(h2), |
| 5318 |
'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big') |
| 5319 |
self.assertEqual(email.header.decode_header(h2), [(x, 'unknown-8bit')]) |
| 5320 |
|
| 5321 |
def test_modify_returned_list_does_not_change_header(self): |
| 5322 |
h = Header('test') |
| 5323 |
chunks = email.header.decode_header(h) |
| 5324 |
chunks.append(('ascii', 'test2')) |
| 5325 |
self.assertEqual(str(h), 'test') |
| 5326 |
|
| 5327 |
def test_encoded_adjacent_nonencoded(self): |
| 5328 |
eq = self.assertEqual |
| 5329 |
h = Header() |
| 5330 |
h.append('hello', 'iso-8859-1') |
| 5331 |
h.append('world') |
| 5332 |
s = h.encode() |
| 5333 |
eq(s, '=?iso-8859-1?q?hello?= world') |
| 5334 |
h = make_header(decode_header(s)) |
| 5335 |
eq(h.encode(), s) |
| 5336 |
|
| 5337 |
def test_whitespace_keeper(self): |
| 5338 |
eq = self.assertEqual |
| 5339 |
s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.' |
| 5340 |
parts = decode_header(s) |
| 5341 |
eq(parts, [(b'Subject: ', None), (b'\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), (b' zz.', None)]) |
| 5342 |
hdr = make_header(parts) |
| 5343 |
eq(hdr.encode(), |
| 5344 |
'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.') |
| 5345 |
|
| 5346 |
def test_broken_base64_header(self): |
| 5347 |
raises = self.assertRaises |
| 5348 |
s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3I ?=' |
| 5349 |
raises(errors.HeaderParseError, decode_header, s) |
| 5350 |
|
| 5351 |
def test_shift_jis_charset(self): |
| 5352 |
h = Header('文', charset='shift_jis') |
| 5353 |
self.assertEqual(h.encode(), '=?iso-2022-jp?b?GyRCSjgbKEI=?=') |
| 5354 |
|
| 5355 |
def test_flatten_header_with_no_value(self): |
| 5356 |
# Issue 11401 (regression from email 4.x) Note that the space after |
| 5357 |
# the header doesn't reflect the input, but this is also the way |
| 5358 |
# email 4.x behaved. At some point it would be nice to fix that. |
| 5359 |
msg = email.message_from_string("EmptyHeader:") |
| 5360 |
self.assertEqual(str(msg), "EmptyHeader: \n\n") |
| 5361 |
|
| 5362 |
def test_encode_preserves_leading_ws_on_value(self): |
| 5363 |
msg = Message() |
| 5364 |
msg['SomeHeader'] = ' value with leading ws' |
| 5365 |
self.assertEqual(str(msg), "SomeHeader: value with leading ws\n\n") |
| 5366 |
|
| 5367 |
def test_whitespace_header(self): |
| 5368 |
self.assertEqual(Header(' ').encode(), ' ') |
| 5369 |
|
| 5370 |
|
| 5371 |
|
| 5372 |
# Test RFC 2231 header parameters (en/de)coding |
| 5373 |
class TestRFC2231(TestEmailBase): |
| 5374 |
|
| 5375 |
# test_headerregistry.TestContentTypeHeader.rfc2231_encoded_with_double_quotes |
| 5376 |
# test_headerregistry.TestContentTypeHeader.rfc2231_single_quote_inside_double_quotes |
| 5377 |
def test_get_param(self): |
| 5378 |
eq = self.assertEqual |
| 5379 |
msg = self._msgobj('msg_29.txt') |
| 5380 |
eq(msg.get_param('title'), |
| 5381 |
('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!')) |
| 5382 |
eq(msg.get_param('title', unquote=False), |
| 5383 |
('us-ascii', 'en', '"This is even more ***fun*** isn\'t it!"')) |
| 5384 |
|
| 5385 |
def test_set_param(self): |
| 5386 |
eq = self.ndiffAssertEqual |
| 5387 |
msg = Message() |
| 5388 |
msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 5389 |
charset='us-ascii') |
| 5390 |
eq(msg.get_param('title'), |
| 5391 |
('us-ascii', '', 'This is even more ***fun*** isn\'t it!')) |
| 5392 |
msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 5393 |
charset='us-ascii', language='en') |
| 5394 |
eq(msg.get_param('title'), |
| 5395 |
('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!')) |
| 5396 |
msg = self._msgobj('msg_01.txt') |
| 5397 |
msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 5398 |
charset='us-ascii', language='en') |
| 5399 |
eq(msg.as_string(maxheaderlen=78), """\ |
| 5400 |
Return-Path: <[email protected]> |
| 5401 |
Delivered-To: [email protected] |
| 5402 |
Received: by mail.zzz.org (Postfix, from userid 889) |
| 5403 |
\tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT) |
| 5404 |
MIME-Version: 1.0 |
| 5405 |
Content-Transfer-Encoding: 7bit |
| 5406 |
Message-ID: <[email protected]> |
| 5407 |
From: [email protected] (John X. Doe) |
| 5408 |
To: [email protected] |
| 5409 |
Subject: This is a test message |
| 5410 |
Date: Fri, 4 May 2001 14:05:44 -0400 |
| 5411 |
Content-Type: text/plain; charset=us-ascii; |
| 5412 |
title*=us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21 |
| 5413 |
|
| 5414 |
|
| 5415 |
Hi, |
| 5416 |
|
| 5417 |
Do you like this message? |
| 5418 |
|
| 5419 |
-Me |
| 5420 |
""") |
| 5421 |
|
| 5422 |
def test_set_param_requote(self): |
| 5423 |
msg = Message() |
| 5424 |
msg.set_param('title', 'foo') |
| 5425 |
self.assertEqual(msg['content-type'], 'text/plain; title="foo"') |
| 5426 |
msg.set_param('title', 'bar', requote=False) |
| 5427 |
self.assertEqual(msg['content-type'], 'text/plain; title=bar') |
| 5428 |
# tspecial is still quoted. |
| 5429 |
msg.set_param('title', "(bar)bell", requote=False) |
| 5430 |
self.assertEqual(msg['content-type'], 'text/plain; title="(bar)bell"') |
| 5431 |
|
| 5432 |
def test_del_param(self): |
| 5433 |
eq = self.ndiffAssertEqual |
| 5434 |
msg = self._msgobj('msg_01.txt') |
| 5435 |
msg.set_param('foo', 'bar', charset='us-ascii', language='en') |
| 5436 |
msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 5437 |
charset='us-ascii', language='en') |
| 5438 |
msg.del_param('foo', header='Content-Type') |
| 5439 |
eq(msg.as_string(maxheaderlen=78), """\ |
| 5440 |
Return-Path: <[email protected]> |
| 5441 |
Delivered-To: [email protected] |
| 5442 |
Received: by mail.zzz.org (Postfix, from userid 889) |
| 5443 |
\tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT) |
| 5444 |
MIME-Version: 1.0 |
| 5445 |
Content-Transfer-Encoding: 7bit |
| 5446 |
Message-ID: <[email protected]> |
| 5447 |
From: [email protected] (John X. Doe) |
| 5448 |
To: [email protected] |
| 5449 |
Subject: This is a test message |
| 5450 |
Date: Fri, 4 May 2001 14:05:44 -0400 |
| 5451 |
Content-Type: text/plain; charset="us-ascii"; |
| 5452 |
title*=us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21 |
| 5453 |
|
| 5454 |
|
| 5455 |
Hi, |
| 5456 |
|
| 5457 |
Do you like this message? |
| 5458 |
|
| 5459 |
-Me |
| 5460 |
""") |
| 5461 |
|
| 5462 |
# test_headerregistry.TestContentTypeHeader.rfc2231_encoded_charset |
| 5463 |
# I changed the charset name, though, because the one in the file isn't |
| 5464 |
# a legal charset name. Should add a test for an illegal charset. |
| 5465 |
def test_rfc2231_get_content_charset(self): |
| 5466 |
eq = self.assertEqual |
| 5467 |
msg = self._msgobj('msg_32.txt') |
| 5468 |
eq(msg.get_content_charset(), 'us-ascii') |
| 5469 |
|
| 5470 |
# test_headerregistry.TestContentTypeHeader.rfc2231_encoded_no_double_quotes |
| 5471 |
def test_rfc2231_parse_rfc_quoting(self): |
| 5472 |
m = textwrap.dedent('''\ |
| 5473 |
Content-Disposition: inline; |
| 5474 |
\tfilename*0*=''This%20is%20even%20more%20; |
| 5475 |
\tfilename*1*=%2A%2A%2Afun%2A%2A%2A%20; |
| 5476 |
\tfilename*2="is it not.pdf" |
| 5477 |
|
| 5478 |
''') |
| 5479 |
msg = email.message_from_string(m) |
| 5480 |
self.assertEqual(msg.get_filename(), |
| 5481 |
'This is even more ***fun*** is it not.pdf') |
| 5482 |
self.assertEqual(m, msg.as_string()) |
| 5483 |
|
| 5484 |
# test_headerregistry.TestContentTypeHeader.rfc2231_encoded_with_double_quotes |
| 5485 |
def test_rfc2231_parse_extra_quoting(self): |
| 5486 |
m = textwrap.dedent('''\ |
| 5487 |
Content-Disposition: inline; |
| 5488 |
\tfilename*0*="''This%20is%20even%20more%20"; |
| 5489 |
\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5490 |
\tfilename*2="is it not.pdf" |
| 5491 |
|
| 5492 |
''') |
| 5493 |
msg = email.message_from_string(m) |
| 5494 |
self.assertEqual(msg.get_filename(), |
| 5495 |
'This is even more ***fun*** is it not.pdf') |
| 5496 |
self.assertEqual(m, msg.as_string()) |
| 5497 |
|
| 5498 |
# test_headerregistry.TestContentTypeHeader.rfc2231_no_language_or_charset |
| 5499 |
# but new test uses *0* because otherwise lang/charset is not valid. |
| 5500 |
# test_headerregistry.TestContentTypeHeader.rfc2231_segmented_normal_values |
| 5501 |
def test_rfc2231_no_language_or_charset(self): |
| 5502 |
m = '''\ |
| 5503 |
Content-Transfer-Encoding: 8bit |
| 5504 |
Content-Disposition: inline; filename="file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm" |
| 5505 |
Content-Type: text/html; NAME*0=file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEM; NAME*1=P_nsmail.htm |
| 5506 |
|
| 5507 |
''' |
| 5508 |
msg = email.message_from_string(m) |
| 5509 |
param = msg.get_param('NAME') |
| 5510 |
self.assertNotIsInstance(param, tuple) |
| 5511 |
self.assertEqual( |
| 5512 |
param, |
| 5513 |
'file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm') |
| 5514 |
|
| 5515 |
# test_headerregistry.TestContentTypeHeader.rfc2231_encoded_no_charset |
| 5516 |
def test_rfc2231_no_language_or_charset_in_filename(self): |
| 5517 |
m = '''\ |
| 5518 |
Content-Disposition: inline; |
| 5519 |
\tfilename*0*="''This%20is%20even%20more%20"; |
| 5520 |
\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5521 |
\tfilename*2="is it not.pdf" |
| 5522 |
|
| 5523 |
''' |
| 5524 |
msg = email.message_from_string(m) |
| 5525 |
self.assertEqual(msg.get_filename(), |
| 5526 |
'This is even more ***fun*** is it not.pdf') |
| 5527 |
|
| 5528 |
# Duplicate of previous test? |
| 5529 |
def test_rfc2231_no_language_or_charset_in_filename_encoded(self): |
| 5530 |
m = '''\ |
| 5531 |
Content-Disposition: inline; |
| 5532 |
\tfilename*0*="''This%20is%20even%20more%20"; |
| 5533 |
\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5534 |
\tfilename*2="is it not.pdf" |
| 5535 |
|
| 5536 |
''' |
| 5537 |
msg = email.message_from_string(m) |
| 5538 |
self.assertEqual(msg.get_filename(), |
| 5539 |
'This is even more ***fun*** is it not.pdf') |
| 5540 |
|
| 5541 |
# test_headerregistry.TestContentTypeHeader.rfc2231_partly_encoded, |
| 5542 |
# but the test below is wrong (the first part should be decoded). |
| 5543 |
def test_rfc2231_partly_encoded(self): |
| 5544 |
m = '''\ |
| 5545 |
Content-Disposition: inline; |
| 5546 |
\tfilename*0="''This%20is%20even%20more%20"; |
| 5547 |
\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5548 |
\tfilename*2="is it not.pdf" |
| 5549 |
|
| 5550 |
''' |
| 5551 |
msg = email.message_from_string(m) |
| 5552 |
self.assertEqual( |
| 5553 |
msg.get_filename(), |
| 5554 |
'This%20is%20even%20more%20***fun*** is it not.pdf') |
| 5555 |
|
| 5556 |
def test_rfc2231_partly_nonencoded(self): |
| 5557 |
m = '''\ |
| 5558 |
Content-Disposition: inline; |
| 5559 |
\tfilename*0="This%20is%20even%20more%20"; |
| 5560 |
\tfilename*1="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5561 |
\tfilename*2="is it not.pdf" |
| 5562 |
|
| 5563 |
''' |
| 5564 |
msg = email.message_from_string(m) |
| 5565 |
self.assertEqual( |
| 5566 |
msg.get_filename(), |
| 5567 |
'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20is it not.pdf') |
| 5568 |
|
| 5569 |
def test_rfc2231_no_language_or_charset_in_boundary(self): |
| 5570 |
m = '''\ |
| 5571 |
Content-Type: multipart/alternative; |
| 5572 |
\tboundary*0*="''This%20is%20even%20more%20"; |
| 5573 |
\tboundary*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5574 |
\tboundary*2="is it not.pdf" |
| 5575 |
|
| 5576 |
''' |
| 5577 |
msg = email.message_from_string(m) |
| 5578 |
self.assertEqual(msg.get_boundary(), |
| 5579 |
'This is even more ***fun*** is it not.pdf') |
| 5580 |
|
| 5581 |
def test_rfc2231_no_language_or_charset_in_charset(self): |
| 5582 |
# This is a nonsensical charset value, but tests the code anyway |
| 5583 |
m = '''\ |
| 5584 |
Content-Type: text/plain; |
| 5585 |
\tcharset*0*="This%20is%20even%20more%20"; |
| 5586 |
\tcharset*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5587 |
\tcharset*2="is it not.pdf" |
| 5588 |
|
| 5589 |
''' |
| 5590 |
msg = email.message_from_string(m) |
| 5591 |
self.assertEqual(msg.get_content_charset(), |
| 5592 |
'this is even more ***fun*** is it not.pdf') |
| 5593 |
|
| 5594 |
# test_headerregistry.TestContentTypeHeader.rfc2231_unknown_charset_treated_as_ascii |
| 5595 |
def test_rfc2231_bad_encoding_in_filename(self): |
| 5596 |
m = '''\ |
| 5597 |
Content-Disposition: inline; |
| 5598 |
\tfilename*0*="bogus'xx'This%20is%20even%20more%20"; |
| 5599 |
\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5600 |
\tfilename*2="is it not.pdf" |
| 5601 |
|
| 5602 |
''' |
| 5603 |
msg = email.message_from_string(m) |
| 5604 |
self.assertEqual(msg.get_filename(), |
| 5605 |
'This is even more ***fun*** is it not.pdf') |
| 5606 |
|
| 5607 |
def test_rfc2231_bad_encoding_in_charset(self): |
| 5608 |
m = """\ |
| 5609 |
Content-Type: text/plain; charset*=bogus''utf-8%E2%80%9D |
| 5610 |
|
| 5611 |
""" |
| 5612 |
msg = email.message_from_string(m) |
| 5613 |
# This should return None because non-ascii characters in the charset |
| 5614 |
# are not allowed. |
| 5615 |
self.assertEqual(msg.get_content_charset(), None) |
| 5616 |
|
| 5617 |
def test_rfc2231_bad_character_in_charset(self): |
| 5618 |
m = """\ |
| 5619 |
Content-Type: text/plain; charset*=ascii''utf-8%E2%80%9D |
| 5620 |
|
| 5621 |
""" |
| 5622 |
msg = email.message_from_string(m) |
| 5623 |
# This should return None because non-ascii characters in the charset |
| 5624 |
# are not allowed. |
| 5625 |
self.assertEqual(msg.get_content_charset(), None) |
| 5626 |
|
| 5627 |
def test_rfc2231_bad_character_in_filename(self): |
| 5628 |
m = '''\ |
| 5629 |
Content-Disposition: inline; |
| 5630 |
\tfilename*0*="ascii'xx'This%20is%20even%20more%20"; |
| 5631 |
\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 5632 |
\tfilename*2*="is it not.pdf%E2" |
| 5633 |
|
| 5634 |
''' |
| 5635 |
msg = email.message_from_string(m) |
| 5636 |
self.assertEqual(msg.get_filename(), |
| 5637 |
'This is even more ***fun*** is it not.pdf\ufffd') |
| 5638 |
|
| 5639 |
def test_rfc2231_unknown_encoding(self): |
| 5640 |
m = """\ |
| 5641 |
Content-Transfer-Encoding: 8bit |
| 5642 |
Content-Disposition: inline; filename*=X-UNKNOWN''myfile.txt |
| 5643 |
|
| 5644 |
""" |
| 5645 |
msg = email.message_from_string(m) |
| 5646 |
self.assertEqual(msg.get_filename(), 'myfile.txt') |
| 5647 |
|
| 5648 |
def test_rfc2231_bad_character_in_encoding(self): |
| 5649 |
m = """\ |
| 5650 |
Content-Transfer-Encoding: 8bit |
| 5651 |
Content-Disposition: inline; filename*=utf-8\udce2\udc80\udc9d''myfile.txt |
| 5652 |
|
| 5653 |
""" |
| 5654 |
msg = email.message_from_string(m) |
| 5655 |
self.assertEqual(msg.get_filename(), 'myfile.txt') |
| 5656 |
|
| 5657 |
def test_rfc2231_single_tick_in_filename_extended(self): |
| 5658 |
eq = self.assertEqual |
| 5659 |
m = """\ |
| 5660 |
Content-Type: application/x-foo; |
| 5661 |
\tname*0*=\"Frank's\"; name*1*=\" Document\" |
| 5662 |
|
| 5663 |
""" |
| 5664 |
msg = email.message_from_string(m) |
| 5665 |
charset, language, s = msg.get_param('name') |
| 5666 |
eq(charset, None) |
| 5667 |
eq(language, None) |
| 5668 |
eq(s, "Frank's Document") |
| 5669 |
|
| 5670 |
# test_headerregistry.TestContentTypeHeader.rfc2231_single_quote_inside_double_quotes |
| 5671 |
def test_rfc2231_single_tick_in_filename(self): |
| 5672 |
m = """\ |
| 5673 |
Content-Type: application/x-foo; name*0=\"Frank's\"; name*1=\" Document\" |
| 5674 |
|
| 5675 |
""" |
| 5676 |
msg = email.message_from_string(m) |
| 5677 |
param = msg.get_param('name') |
| 5678 |
self.assertNotIsInstance(param, tuple) |
| 5679 |
self.assertEqual(param, "Frank's Document") |
| 5680 |
|
| 5681 |
def test_rfc2231_missing_tick(self): |
| 5682 |
m = '''\ |
| 5683 |
Content-Disposition: inline; |
| 5684 |
\tfilename*0*="'This%20is%20broken"; |
| 5685 |
''' |
| 5686 |
msg = email.message_from_string(m) |
| 5687 |
self.assertEqual( |
| 5688 |
msg.get_filename(), |
| 5689 |
"'This is broken") |
| 5690 |
|
| 5691 |
def test_rfc2231_missing_tick_with_encoded_non_ascii(self): |
| 5692 |
m = '''\ |
| 5693 |
Content-Disposition: inline; |
| 5694 |
\tfilename*0*="'This%20is%E2broken"; |
| 5695 |
''' |
| 5696 |
msg = email.message_from_string(m) |
| 5697 |
self.assertEqual( |
| 5698 |
msg.get_filename(), |
| 5699 |
"'This is\ufffdbroken") |
| 5700 |
|
| 5701 |
# test_headerregistry.TestContentTypeHeader.rfc2231_single_quote_in_value_with_charset_and_lang |
| 5702 |
def test_rfc2231_tick_attack_extended(self): |
| 5703 |
eq = self.assertEqual |
| 5704 |
m = """\ |
| 5705 |
Content-Type: application/x-foo; |
| 5706 |
\tname*0*=\"us-ascii'en-us'Frank's\"; name*1*=\" Document\" |
| 5707 |
|
| 5708 |
""" |
| 5709 |
msg = email.message_from_string(m) |
| 5710 |
charset, language, s = msg.get_param('name') |
| 5711 |
eq(charset, 'us-ascii') |
| 5712 |
eq(language, 'en-us') |
| 5713 |
eq(s, "Frank's Document") |
| 5714 |
|
| 5715 |
# test_headerregistry.TestContentTypeHeader.rfc2231_single_quote_in_non_encoded_value |
| 5716 |
def test_rfc2231_tick_attack(self): |
| 5717 |
m = """\ |
| 5718 |
Content-Type: application/x-foo; |
| 5719 |
\tname*0=\"us-ascii'en-us'Frank's\"; name*1=\" Document\" |
| 5720 |
|
| 5721 |
""" |
| 5722 |
msg = email.message_from_string(m) |
| 5723 |
param = msg.get_param('name') |
| 5724 |
self.assertNotIsInstance(param, tuple) |
| 5725 |
self.assertEqual(param, "us-ascii'en-us'Frank's Document") |
| 5726 |
|
| 5727 |
# test_headerregistry.TestContentTypeHeader.rfc2231_single_quotes_inside_quotes |
| 5728 |
def test_rfc2231_no_extended_values(self): |
| 5729 |
eq = self.assertEqual |
| 5730 |
m = """\ |
| 5731 |
Content-Type: application/x-foo; name=\"Frank's Document\" |
| 5732 |
|
| 5733 |
""" |
| 5734 |
msg = email.message_from_string(m) |
| 5735 |
eq(msg.get_param('name'), "Frank's Document") |
| 5736 |
|
| 5737 |
# test_headerregistry.TestContentTypeHeader.rfc2231_encoded_then_unencoded_segments |
| 5738 |
def test_rfc2231_encoded_then_unencoded_segments(self): |
| 5739 |
eq = self.assertEqual |
| 5740 |
m = """\ |
| 5741 |
Content-Type: application/x-foo; |
| 5742 |
\tname*0*=\"us-ascii'en-us'My\"; |
| 5743 |
\tname*1=\" Document\"; |
| 5744 |
\tname*2*=\" For You\" |
| 5745 |
|
| 5746 |
""" |
| 5747 |
msg = email.message_from_string(m) |
| 5748 |
charset, language, s = msg.get_param('name') |
| 5749 |
eq(charset, 'us-ascii') |
| 5750 |
eq(language, 'en-us') |
| 5751 |
eq(s, 'My Document For You') |
| 5752 |
|
| 5753 |
# test_headerregistry.TestContentTypeHeader.rfc2231_unencoded_then_encoded_segments |
| 5754 |
# test_headerregistry.TestContentTypeHeader.rfc2231_quoted_unencoded_then_encoded_segments |
| 5755 |
def test_rfc2231_unencoded_then_encoded_segments(self): |
| 5756 |
eq = self.assertEqual |
| 5757 |
m = """\ |
| 5758 |
Content-Type: application/x-foo; |
| 5759 |
\tname*0=\"us-ascii'en-us'My\"; |
| 5760 |
\tname*1*=\" Document\"; |
| 5761 |
\tname*2*=\" For You\" |
| 5762 |
|
| 5763 |
""" |
| 5764 |
msg = email.message_from_string(m) |
| 5765 |
charset, language, s = msg.get_param('name') |
| 5766 |
eq(charset, 'us-ascii') |
| 5767 |
eq(language, 'en-us') |
| 5768 |
eq(s, 'My Document For You') |
| 5769 |
|
| 5770 |
def test_should_not_hang_on_invalid_ew_messages(self): |
| 5771 |
messages = ["""From: [email protected] |
| 5772 |
To: [email protected] |
| 5773 |
Bad-Header: |
| 5774 |
=?us-ascii?Q?LCSwrV11+IB0rSbSker+M9vWR7wEDSuGqmHD89Gt=ea0nJFSaiz4vX3XMJPT4vrE?= |
| 5775 |
=?us-ascii?Q?xGUZeOnp0o22pLBB7CYLH74Js=wOlK6Tfru2U47qR?= |
| 5776 |
=?us-ascii?Q?72OfyEY2p2=2FrA9xNFyvH+fBTCmazxwzF8nGkK6D?= |
| 5777 |
|
| 5778 |
Hello! |
| 5779 |
""", """From: ����� �������� <xxx@xxx> |
| 5780 |
To: "xxx" <xxx@xxx> |
| 5781 |
Subject: ��� ���������� ����� ����� � ��������� �� ���� |
| 5782 |
MIME-Version: 1.0 |
| 5783 |
Content-Type: text/plain; charset="windows-1251"; |
| 5784 |
Content-Transfer-Encoding: 8bit |
| 5785 |
|
| 5786 |
�� ����� � ���� ������ ��� �������� |
| 5787 |
"""] |
| 5788 |
for m in messages: |
| 5789 |
with self.subTest(m=m): |
| 5790 |
msg = email.message_from_string(m) |
| 5791 |
|
| 5792 |
|
| 5793 |
# Tests to ensure that signed parts of an email are completely preserved, as |
| 5794 |
# required by RFC1847 section 2.1. Note that these are incomplete, because the |
| 5795 |
# email package does not currently always preserve the body. See issue 1670765. |
| 5796 |
class TestSigned(TestEmailBase): |
| 5797 |
|
| 5798 |
def _msg_and_obj(self, filename): |
| 5799 |
with openfile(filename, encoding="utf-8") as fp: |
| 5800 |
original = fp.read() |
| 5801 |
msg = email.message_from_string(original) |
| 5802 |
return original, msg |
| 5803 |
|
| 5804 |
def _signed_parts_eq(self, original, result): |
| 5805 |
# Extract the first mime part of each message |
| 5806 |
import re |
| 5807 |
repart = re.compile(r'^--([^\n]+)\n(.*?)\n--\1$', re.S | re.M) |
| 5808 |
inpart = repart.search(original).group(2) |
| 5809 |
outpart = repart.search(result).group(2) |
| 5810 |
self.assertEqual(outpart, inpart) |
| 5811 |
|
| 5812 |
def test_long_headers_as_string(self): |
| 5813 |
original, msg = self._msg_and_obj('msg_45.txt') |
| 5814 |
result = msg.as_string() |
| 5815 |
self._signed_parts_eq(original, result) |
| 5816 |
|
| 5817 |
def test_long_headers_as_string_maxheaderlen(self): |
| 5818 |
original, msg = self._msg_and_obj('msg_45.txt') |
| 5819 |
result = msg.as_string(maxheaderlen=60) |
| 5820 |
self._signed_parts_eq(original, result) |
| 5821 |
|
| 5822 |
def test_long_headers_flatten(self): |
| 5823 |
original, msg = self._msg_and_obj('msg_45.txt') |
| 5824 |
fp = StringIO() |
| 5825 |
Generator(fp).flatten(msg) |
| 5826 |
result = fp.getvalue() |
| 5827 |
self._signed_parts_eq(original, result) |
| 5828 |
|
| 5829 |
class TestHeaderRegistry(TestEmailBase): |
| 5830 |
# See issue gh-93010. |
| 5831 |
def test_HeaderRegistry(self): |
| 5832 |
reg = HeaderRegistry() |
| 5833 |
a = reg('Content-Disposition', 'attachment; 0*00="foo"') |
| 5834 |
self.assertIsInstance(a.defects[0], errors.InvalidHeaderDefect) |
| 5835 |
|
| 5836 |
if __name__ == '__main__': |
| 5837 |
unittest.main() |