blob: 245f65999b55ded199588d65630cf7815042f834 [file] [log] [blame]
Benjamin Peterson46a99002010-01-09 18:45:30 +00001# Copyright (C) 2001-2010 Python Software Foundation
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002# Contact: email-sig@python.org
3# email package unit tests
4
5import os
R. David Murray719a4492010-11-21 16:53:48 +00006import re
Guido van Rossum8b3febe2007-08-30 01:15:14 +00007import sys
8import time
9import base64
10import difflib
11import unittest
12import warnings
R. David Murray96fd54e2010-10-08 15:55:28 +000013import textwrap
Guido van Rossum8b3febe2007-08-30 01:15:14 +000014
R. David Murray96fd54e2010-10-08 15:55:28 +000015from io import StringIO, BytesIO
Guido van Rossum8b3febe2007-08-30 01:15:14 +000016from itertools import chain
17
18import email
19
20from email.charset import Charset
21from email.header import Header, decode_header, make_header
22from email.parser import Parser, HeaderParser
23from email.generator import Generator, DecodedGenerator
24from email.message import Message
25from email.mime.application import MIMEApplication
26from email.mime.audio import MIMEAudio
27from email.mime.text import MIMEText
28from email.mime.image import MIMEImage
29from email.mime.base import MIMEBase
30from email.mime.message import MIMEMessage
31from email.mime.multipart import MIMEMultipart
32from email import utils
33from email import errors
34from email import encoders
35from email import iterators
36from email import base64mime
37from email import quoprimime
38
R. David Murray96fd54e2010-10-08 15:55:28 +000039from test.support import findfile, run_unittest, unlink
Guido van Rossum8b3febe2007-08-30 01:15:14 +000040from email.test import __file__ as landmark
41
42
43NL = '\n'
44EMPTYSTRING = ''
45SPACE = ' '
46
47
Ezio Melottib3aedd42010-11-20 19:04:17 +000048
Guido van Rossum8b3febe2007-08-30 01:15:14 +000049def openfile(filename, *args, **kws):
50 path = os.path.join(os.path.dirname(landmark), 'data', filename)
51 return open(path, *args, **kws)
52
53
Ezio Melottib3aedd42010-11-20 19:04:17 +000054
Guido van Rossum8b3febe2007-08-30 01:15:14 +000055# Base test class
56class TestEmailBase(unittest.TestCase):
57 def ndiffAssertEqual(self, first, second):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000058 """Like assertEqual except use ndiff for readable output."""
Guido van Rossum8b3febe2007-08-30 01:15:14 +000059 if first != second:
60 sfirst = str(first)
61 ssecond = str(second)
62 rfirst = [repr(line) for line in sfirst.splitlines()]
63 rsecond = [repr(line) for line in ssecond.splitlines()]
64 diff = difflib.ndiff(rfirst, rsecond)
65 raise self.failureException(NL + NL.join(diff))
66
67 def _msgobj(self, filename):
68 with openfile(findfile(filename)) as fp:
69 return email.message_from_file(fp)
70
71
Ezio Melottib3aedd42010-11-20 19:04:17 +000072
Guido van Rossum8b3febe2007-08-30 01:15:14 +000073# Test various aspects of the Message class's API
74class TestMessageAPI(TestEmailBase):
75 def test_get_all(self):
76 eq = self.assertEqual
77 msg = self._msgobj('msg_20.txt')
78 eq(msg.get_all('cc'), ['ccc@zzz.org', 'ddd@zzz.org', 'eee@zzz.org'])
79 eq(msg.get_all('xx', 'n/a'), 'n/a')
80
R. David Murraye5db2632010-11-20 15:10:13 +000081 def test_getset_charset(self):
Guido van Rossum8b3febe2007-08-30 01:15:14 +000082 eq = self.assertEqual
83 msg = Message()
84 eq(msg.get_charset(), None)
85 charset = Charset('iso-8859-1')
86 msg.set_charset(charset)
87 eq(msg['mime-version'], '1.0')
88 eq(msg.get_content_type(), 'text/plain')
89 eq(msg['content-type'], 'text/plain; charset="iso-8859-1"')
90 eq(msg.get_param('charset'), 'iso-8859-1')
91 eq(msg['content-transfer-encoding'], 'quoted-printable')
92 eq(msg.get_charset().input_charset, 'iso-8859-1')
93 # Remove the charset
94 msg.set_charset(None)
95 eq(msg.get_charset(), None)
96 eq(msg['content-type'], 'text/plain')
97 # Try adding a charset when there's already MIME headers present
98 msg = Message()
99 msg['MIME-Version'] = '2.0'
100 msg['Content-Type'] = 'text/x-weird'
101 msg['Content-Transfer-Encoding'] = 'quinted-puntable'
102 msg.set_charset(charset)
103 eq(msg['mime-version'], '2.0')
104 eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"')
105 eq(msg['content-transfer-encoding'], 'quinted-puntable')
106
107 def test_set_charset_from_string(self):
108 eq = self.assertEqual
109 msg = Message()
110 msg.set_charset('us-ascii')
111 eq(msg.get_charset().input_charset, 'us-ascii')
112 eq(msg['content-type'], 'text/plain; charset="us-ascii"')
113
114 def test_set_payload_with_charset(self):
115 msg = Message()
116 charset = Charset('iso-8859-1')
117 msg.set_payload('This is a string payload', charset)
118 self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1')
119
120 def test_get_charsets(self):
121 eq = self.assertEqual
122
123 msg = self._msgobj('msg_08.txt')
124 charsets = msg.get_charsets()
125 eq(charsets, [None, 'us-ascii', 'iso-8859-1', 'iso-8859-2', 'koi8-r'])
126
127 msg = self._msgobj('msg_09.txt')
128 charsets = msg.get_charsets('dingbat')
129 eq(charsets, ['dingbat', 'us-ascii', 'iso-8859-1', 'dingbat',
130 'koi8-r'])
131
132 msg = self._msgobj('msg_12.txt')
133 charsets = msg.get_charsets()
134 eq(charsets, [None, 'us-ascii', 'iso-8859-1', None, 'iso-8859-2',
135 'iso-8859-3', 'us-ascii', 'koi8-r'])
136
137 def test_get_filename(self):
138 eq = self.assertEqual
139
140 msg = self._msgobj('msg_04.txt')
141 filenames = [p.get_filename() for p in msg.get_payload()]
142 eq(filenames, ['msg.txt', 'msg.txt'])
143
144 msg = self._msgobj('msg_07.txt')
145 subpart = msg.get_payload(1)
146 eq(subpart.get_filename(), 'dingusfish.gif')
147
148 def test_get_filename_with_name_parameter(self):
149 eq = self.assertEqual
150
151 msg = self._msgobj('msg_44.txt')
152 filenames = [p.get_filename() for p in msg.get_payload()]
153 eq(filenames, ['msg.txt', 'msg.txt'])
154
155 def test_get_boundary(self):
156 eq = self.assertEqual
157 msg = self._msgobj('msg_07.txt')
158 # No quotes!
159 eq(msg.get_boundary(), 'BOUNDARY')
160
161 def test_set_boundary(self):
162 eq = self.assertEqual
163 # This one has no existing boundary parameter, but the Content-Type:
164 # header appears fifth.
165 msg = self._msgobj('msg_01.txt')
166 msg.set_boundary('BOUNDARY')
167 header, value = msg.items()[4]
168 eq(header.lower(), 'content-type')
169 eq(value, 'text/plain; charset="us-ascii"; boundary="BOUNDARY"')
170 # This one has a Content-Type: header, with a boundary, stuck in the
171 # middle of its headers. Make sure the order is preserved; it should
172 # be fifth.
173 msg = self._msgobj('msg_04.txt')
174 msg.set_boundary('BOUNDARY')
175 header, value = msg.items()[4]
176 eq(header.lower(), 'content-type')
177 eq(value, 'multipart/mixed; boundary="BOUNDARY"')
178 # And this one has no Content-Type: header at all.
179 msg = self._msgobj('msg_03.txt')
180 self.assertRaises(errors.HeaderParseError,
181 msg.set_boundary, 'BOUNDARY')
182
R. David Murray73a559d2010-12-21 18:07:59 +0000183 def test_make_boundary(self):
184 msg = MIMEMultipart('form-data')
185 # Note that when the boundary gets created is an implementation
186 # detail and might change.
187 self.assertEqual(msg.items()[0][1], 'multipart/form-data')
188 # Trigger creation of boundary
189 msg.as_string()
190 self.assertEqual(msg.items()[0][1][:33],
191 'multipart/form-data; boundary="==')
192 # XXX: there ought to be tests of the uniqueness of the boundary, too.
193
R. David Murray57c45ac2010-02-21 04:39:40 +0000194 def test_message_rfc822_only(self):
195 # Issue 7970: message/rfc822 not in multipart parsed by
196 # HeaderParser caused an exception when flattened.
Brett Cannon384917a2010-10-29 23:08:36 +0000197 with openfile(findfile('msg_46.txt')) as fp:
198 msgdata = fp.read()
R. David Murray57c45ac2010-02-21 04:39:40 +0000199 parser = HeaderParser()
200 msg = parser.parsestr(msgdata)
201 out = StringIO()
202 gen = Generator(out, True, 0)
203 gen.flatten(msg, False)
204 self.assertEqual(out.getvalue(), msgdata)
205
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000206 def test_get_decoded_payload(self):
207 eq = self.assertEqual
208 msg = self._msgobj('msg_10.txt')
209 # The outer message is a multipart
210 eq(msg.get_payload(decode=True), None)
211 # Subpart 1 is 7bit encoded
212 eq(msg.get_payload(0).get_payload(decode=True),
213 b'This is a 7bit encoded message.\n')
214 # Subpart 2 is quopri
215 eq(msg.get_payload(1).get_payload(decode=True),
216 b'\xa1This is a Quoted Printable encoded message!\n')
217 # Subpart 3 is base64
218 eq(msg.get_payload(2).get_payload(decode=True),
219 b'This is a Base64 encoded message.')
R. David Murray57a4b982010-03-08 02:17:03 +0000220 # Subpart 4 is base64 with a trailing newline, which
221 # used to be stripped (issue 7143).
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000222 eq(msg.get_payload(3).get_payload(decode=True),
R. David Murray57a4b982010-03-08 02:17:03 +0000223 b'This is a Base64 encoded message.\n')
224 # Subpart 5 has no Content-Transfer-Encoding: header.
225 eq(msg.get_payload(4).get_payload(decode=True),
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000226 b'This has no Content-Transfer-Encoding: header.\n')
227
228 def test_get_decoded_uu_payload(self):
229 eq = self.assertEqual
230 msg = Message()
231 msg.set_payload('begin 666 -\n+:&5L;&\\@=V]R;&0 \n \nend\n')
232 for cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
233 msg['content-transfer-encoding'] = cte
234 eq(msg.get_payload(decode=True), b'hello world')
235 # Now try some bogus data
236 msg.set_payload('foo')
237 eq(msg.get_payload(decode=True), b'foo')
238
239 def test_decoded_generator(self):
240 eq = self.assertEqual
241 msg = self._msgobj('msg_07.txt')
242 with openfile('msg_17.txt') as fp:
243 text = fp.read()
244 s = StringIO()
245 g = DecodedGenerator(s)
246 g.flatten(msg)
247 eq(s.getvalue(), text)
248
249 def test__contains__(self):
250 msg = Message()
251 msg['From'] = 'Me'
252 msg['to'] = 'You'
253 # Check for case insensitivity
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000254 self.assertTrue('from' in msg)
255 self.assertTrue('From' in msg)
256 self.assertTrue('FROM' in msg)
257 self.assertTrue('to' in msg)
258 self.assertTrue('To' in msg)
259 self.assertTrue('TO' in msg)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000260
261 def test_as_string(self):
262 eq = self.ndiffAssertEqual
263 msg = self._msgobj('msg_01.txt')
264 with openfile('msg_01.txt') as fp:
265 text = fp.read()
266 eq(text, str(msg))
267 fullrepr = msg.as_string(unixfrom=True)
268 lines = fullrepr.split('\n')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000269 self.assertTrue(lines[0].startswith('From '))
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000270 eq(text, NL.join(lines[1:]))
271
272 def test_bad_param(self):
273 msg = email.message_from_string("Content-Type: blarg; baz; boo\n")
274 self.assertEqual(msg.get_param('baz'), '')
275
276 def test_missing_filename(self):
277 msg = email.message_from_string("From: foo\n")
278 self.assertEqual(msg.get_filename(), None)
279
280 def test_bogus_filename(self):
281 msg = email.message_from_string(
282 "Content-Disposition: blarg; filename\n")
283 self.assertEqual(msg.get_filename(), '')
284
285 def test_missing_boundary(self):
286 msg = email.message_from_string("From: foo\n")
287 self.assertEqual(msg.get_boundary(), None)
288
289 def test_get_params(self):
290 eq = self.assertEqual
291 msg = email.message_from_string(
292 'X-Header: foo=one; bar=two; baz=three\n')
293 eq(msg.get_params(header='x-header'),
294 [('foo', 'one'), ('bar', 'two'), ('baz', 'three')])
295 msg = email.message_from_string(
296 'X-Header: foo; bar=one; baz=two\n')
297 eq(msg.get_params(header='x-header'),
298 [('foo', ''), ('bar', 'one'), ('baz', 'two')])
299 eq(msg.get_params(), None)
300 msg = email.message_from_string(
301 'X-Header: foo; bar="one"; baz=two\n')
302 eq(msg.get_params(header='x-header'),
303 [('foo', ''), ('bar', 'one'), ('baz', 'two')])
304
305 def test_get_param_liberal(self):
306 msg = Message()
307 msg['Content-Type'] = 'Content-Type: Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"'
308 self.assertEqual(msg.get_param('boundary'), 'CPIMSSMTPC06p5f3tG')
309
310 def test_get_param(self):
311 eq = self.assertEqual
312 msg = email.message_from_string(
313 "X-Header: foo=one; bar=two; baz=three\n")
314 eq(msg.get_param('bar', header='x-header'), 'two')
315 eq(msg.get_param('quuz', header='x-header'), None)
316 eq(msg.get_param('quuz'), None)
317 msg = email.message_from_string(
318 'X-Header: foo; bar="one"; baz=two\n')
319 eq(msg.get_param('foo', header='x-header'), '')
320 eq(msg.get_param('bar', header='x-header'), 'one')
321 eq(msg.get_param('baz', header='x-header'), 'two')
322 # XXX: We are not RFC-2045 compliant! We cannot parse:
323 # msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"'
324 # msg.get_param("weird")
325 # yet.
326
327 def test_get_param_funky_continuation_lines(self):
328 msg = self._msgobj('msg_22.txt')
329 self.assertEqual(msg.get_payload(1).get_param('name'), 'wibble.JPG')
330
331 def test_get_param_with_semis_in_quotes(self):
332 msg = email.message_from_string(
333 'Content-Type: image/pjpeg; name="Jim&amp;&amp;Jill"\n')
334 self.assertEqual(msg.get_param('name'), 'Jim&amp;&amp;Jill')
335 self.assertEqual(msg.get_param('name', unquote=False),
336 '"Jim&amp;&amp;Jill"')
337
R. David Murrayd48739f2010-04-14 18:59:18 +0000338 def test_get_param_with_quotes(self):
339 msg = email.message_from_string(
340 'Content-Type: foo; bar*0="baz\\"foobar"; bar*1="\\"baz"')
341 self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')
342 msg = email.message_from_string(
343 "Content-Type: foo; bar*0=\"baz\\\"foobar\"; bar*1=\"\\\"baz\"")
344 self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')
345
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000346 def test_field_containment(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000347 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000348 msg = email.message_from_string('Header: exists')
349 unless('header' in msg)
350 unless('Header' in msg)
351 unless('HEADER' in msg)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000352 self.assertFalse('headerx' in msg)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000353
354 def test_set_param(self):
355 eq = self.assertEqual
356 msg = Message()
357 msg.set_param('charset', 'iso-2022-jp')
358 eq(msg.get_param('charset'), 'iso-2022-jp')
359 msg.set_param('importance', 'high value')
360 eq(msg.get_param('importance'), 'high value')
361 eq(msg.get_param('importance', unquote=False), '"high value"')
362 eq(msg.get_params(), [('text/plain', ''),
363 ('charset', 'iso-2022-jp'),
364 ('importance', 'high value')])
365 eq(msg.get_params(unquote=False), [('text/plain', ''),
366 ('charset', '"iso-2022-jp"'),
367 ('importance', '"high value"')])
368 msg.set_param('charset', 'iso-9999-xx', header='X-Jimmy')
369 eq(msg.get_param('charset', header='X-Jimmy'), 'iso-9999-xx')
370
371 def test_del_param(self):
372 eq = self.assertEqual
373 msg = self._msgobj('msg_05.txt')
374 eq(msg.get_params(),
375 [('multipart/report', ''), ('report-type', 'delivery-status'),
376 ('boundary', 'D1690A7AC1.996856090/mail.example.com')])
377 old_val = msg.get_param("report-type")
378 msg.del_param("report-type")
379 eq(msg.get_params(),
380 [('multipart/report', ''),
381 ('boundary', 'D1690A7AC1.996856090/mail.example.com')])
382 msg.set_param("report-type", old_val)
383 eq(msg.get_params(),
384 [('multipart/report', ''),
385 ('boundary', 'D1690A7AC1.996856090/mail.example.com'),
386 ('report-type', old_val)])
387
388 def test_del_param_on_other_header(self):
389 msg = Message()
390 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
391 msg.del_param('filename', 'content-disposition')
392 self.assertEqual(msg['content-disposition'], 'attachment')
393
394 def test_set_type(self):
395 eq = self.assertEqual
396 msg = Message()
397 self.assertRaises(ValueError, msg.set_type, 'text')
398 msg.set_type('text/plain')
399 eq(msg['content-type'], 'text/plain')
400 msg.set_param('charset', 'us-ascii')
401 eq(msg['content-type'], 'text/plain; charset="us-ascii"')
402 msg.set_type('text/html')
403 eq(msg['content-type'], 'text/html; charset="us-ascii"')
404
405 def test_set_type_on_other_header(self):
406 msg = Message()
407 msg['X-Content-Type'] = 'text/plain'
408 msg.set_type('application/octet-stream', 'X-Content-Type')
409 self.assertEqual(msg['x-content-type'], 'application/octet-stream')
410
411 def test_get_content_type_missing(self):
412 msg = Message()
413 self.assertEqual(msg.get_content_type(), 'text/plain')
414
415 def test_get_content_type_missing_with_default_type(self):
416 msg = Message()
417 msg.set_default_type('message/rfc822')
418 self.assertEqual(msg.get_content_type(), 'message/rfc822')
419
420 def test_get_content_type_from_message_implicit(self):
421 msg = self._msgobj('msg_30.txt')
422 self.assertEqual(msg.get_payload(0).get_content_type(),
423 'message/rfc822')
424
425 def test_get_content_type_from_message_explicit(self):
426 msg = self._msgobj('msg_28.txt')
427 self.assertEqual(msg.get_payload(0).get_content_type(),
428 'message/rfc822')
429
430 def test_get_content_type_from_message_text_plain_implicit(self):
431 msg = self._msgobj('msg_03.txt')
432 self.assertEqual(msg.get_content_type(), 'text/plain')
433
434 def test_get_content_type_from_message_text_plain_explicit(self):
435 msg = self._msgobj('msg_01.txt')
436 self.assertEqual(msg.get_content_type(), 'text/plain')
437
438 def test_get_content_maintype_missing(self):
439 msg = Message()
440 self.assertEqual(msg.get_content_maintype(), 'text')
441
442 def test_get_content_maintype_missing_with_default_type(self):
443 msg = Message()
444 msg.set_default_type('message/rfc822')
445 self.assertEqual(msg.get_content_maintype(), 'message')
446
447 def test_get_content_maintype_from_message_implicit(self):
448 msg = self._msgobj('msg_30.txt')
449 self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message')
450
451 def test_get_content_maintype_from_message_explicit(self):
452 msg = self._msgobj('msg_28.txt')
453 self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message')
454
455 def test_get_content_maintype_from_message_text_plain_implicit(self):
456 msg = self._msgobj('msg_03.txt')
457 self.assertEqual(msg.get_content_maintype(), 'text')
458
459 def test_get_content_maintype_from_message_text_plain_explicit(self):
460 msg = self._msgobj('msg_01.txt')
461 self.assertEqual(msg.get_content_maintype(), 'text')
462
463 def test_get_content_subtype_missing(self):
464 msg = Message()
465 self.assertEqual(msg.get_content_subtype(), 'plain')
466
467 def test_get_content_subtype_missing_with_default_type(self):
468 msg = Message()
469 msg.set_default_type('message/rfc822')
470 self.assertEqual(msg.get_content_subtype(), 'rfc822')
471
472 def test_get_content_subtype_from_message_implicit(self):
473 msg = self._msgobj('msg_30.txt')
474 self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822')
475
476 def test_get_content_subtype_from_message_explicit(self):
477 msg = self._msgobj('msg_28.txt')
478 self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822')
479
480 def test_get_content_subtype_from_message_text_plain_implicit(self):
481 msg = self._msgobj('msg_03.txt')
482 self.assertEqual(msg.get_content_subtype(), 'plain')
483
484 def test_get_content_subtype_from_message_text_plain_explicit(self):
485 msg = self._msgobj('msg_01.txt')
486 self.assertEqual(msg.get_content_subtype(), 'plain')
487
488 def test_get_content_maintype_error(self):
489 msg = Message()
490 msg['Content-Type'] = 'no-slash-in-this-string'
491 self.assertEqual(msg.get_content_maintype(), 'text')
492
493 def test_get_content_subtype_error(self):
494 msg = Message()
495 msg['Content-Type'] = 'no-slash-in-this-string'
496 self.assertEqual(msg.get_content_subtype(), 'plain')
497
498 def test_replace_header(self):
499 eq = self.assertEqual
500 msg = Message()
501 msg.add_header('First', 'One')
502 msg.add_header('Second', 'Two')
503 msg.add_header('Third', 'Three')
504 eq(msg.keys(), ['First', 'Second', 'Third'])
505 eq(msg.values(), ['One', 'Two', 'Three'])
506 msg.replace_header('Second', 'Twenty')
507 eq(msg.keys(), ['First', 'Second', 'Third'])
508 eq(msg.values(), ['One', 'Twenty', 'Three'])
509 msg.add_header('First', 'Eleven')
510 msg.replace_header('First', 'One Hundred')
511 eq(msg.keys(), ['First', 'Second', 'Third', 'First'])
512 eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven'])
513 self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing')
514
515 def test_broken_base64_payload(self):
516 x = 'AwDp0P7//y6LwKEAcPa/6Q=9'
517 msg = Message()
518 msg['content-type'] = 'audio/x-midi'
519 msg['content-transfer-encoding'] = 'base64'
520 msg.set_payload(x)
521 self.assertEqual(msg.get_payload(decode=True),
Guido van Rossum9604e662007-08-30 03:46:43 +0000522 bytes(x, 'raw-unicode-escape'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000523
R. David Murray7ec754b2010-12-13 23:51:19 +0000524 # Issue 1078919
525 def test_ascii_add_header(self):
526 msg = Message()
527 msg.add_header('Content-Disposition', 'attachment',
528 filename='bud.gif')
529 self.assertEqual('attachment; filename="bud.gif"',
530 msg['Content-Disposition'])
531
532 def test_noascii_add_header(self):
533 msg = Message()
534 msg.add_header('Content-Disposition', 'attachment',
535 filename="Fußballer.ppt")
536 self.assertEqual(
R. David Murraydfd7eb02010-12-24 22:36:49 +0000537 'attachment; filename*=utf-8\'\'Fu%C3%9Fballer.ppt',
R. David Murray7ec754b2010-12-13 23:51:19 +0000538 msg['Content-Disposition'])
539
540 def test_nonascii_add_header_via_triple(self):
541 msg = Message()
542 msg.add_header('Content-Disposition', 'attachment',
543 filename=('iso-8859-1', '', 'Fußballer.ppt'))
544 self.assertEqual(
R. David Murraydfd7eb02010-12-24 22:36:49 +0000545 'attachment; filename*=iso-8859-1\'\'Fu%DFballer.ppt',
546 msg['Content-Disposition'])
547
548 def test_ascii_add_header_with_tspecial(self):
549 msg = Message()
550 msg.add_header('Content-Disposition', 'attachment',
551 filename="windows [filename].ppt")
552 self.assertEqual(
553 'attachment; filename="windows [filename].ppt"',
554 msg['Content-Disposition'])
555
556 def test_nonascii_add_header_with_tspecial(self):
557 msg = Message()
558 msg.add_header('Content-Disposition', 'attachment',
559 filename="Fußballer [filename].ppt")
560 self.assertEqual(
561 "attachment; filename*=utf-8''Fu%C3%9Fballer%20%5Bfilename%5D.ppt",
R. David Murray7ec754b2010-12-13 23:51:19 +0000562 msg['Content-Disposition'])
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000563
R. David Murray5b2d9dd2011-01-09 02:35:24 +0000564 # Issue 5871: reject an attempt to embed a header inside a header value
565 # (header injection attack).
566 def test_embeded_header_via_Header_rejected(self):
567 msg = Message()
568 msg['Dummy'] = Header('dummy\nX-Injected-Header: test')
569 self.assertRaises(errors.HeaderParseError, msg.as_string)
570
571 def test_embeded_header_via_string_rejected(self):
572 msg = Message()
573 msg['Dummy'] = 'dummy\nX-Injected-Header: test'
574 self.assertRaises(errors.HeaderParseError, msg.as_string)
575
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000576# Test the email.encoders module
577class TestEncoders(unittest.TestCase):
R David Murray6d94bd42011-03-16 15:52:22 -0400578
579 def test_EncodersEncode_base64(self):
580 with openfile('PyBanner048.gif', 'rb') as fp:
581 bindata = fp.read()
582 mimed = email.mime.image.MIMEImage(bindata)
583 base64ed = mimed.get_payload()
584 # the transfer-encoded body lines should all be <=76 characters
585 lines = base64ed.split('\n')
586 self.assertLessEqual(max([ len(x) for x in lines ]), 76)
587
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000588 def test_encode_empty_payload(self):
589 eq = self.assertEqual
590 msg = Message()
591 msg.set_charset('us-ascii')
592 eq(msg['content-transfer-encoding'], '7bit')
593
594 def test_default_cte(self):
595 eq = self.assertEqual
Ezio Melottic303c122010-04-22 11:57:12 +0000596 # 7bit data and the default us-ascii _charset
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000597 msg = MIMEText('hello world')
598 eq(msg['content-transfer-encoding'], '7bit')
Ezio Melottic303c122010-04-22 11:57:12 +0000599 # Similar, but with 8bit data
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000600 msg = MIMEText('hello \xf8 world')
601 eq(msg['content-transfer-encoding'], '8bit')
602 # And now with a different charset
603 msg = MIMEText('hello \xf8 world', _charset='iso-8859-1')
604 eq(msg['content-transfer-encoding'], 'quoted-printable')
605
R. David Murraye85200d2010-05-06 01:41:14 +0000606 def test_encode7or8bit(self):
607 # Make sure a charset whose input character set is 8bit but
608 # whose output character set is 7bit gets a transfer-encoding
609 # of 7bit.
610 eq = self.assertEqual
R. David Murray850fc852010-06-03 01:58:28 +0000611 msg = MIMEText('文', _charset='euc-jp')
R. David Murraye85200d2010-05-06 01:41:14 +0000612 eq(msg['content-transfer-encoding'], '7bit')
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000613
Ezio Melottib3aedd42010-11-20 19:04:17 +0000614
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000615# Test long header wrapping
616class TestLongHeaders(TestEmailBase):
617 def test_split_long_continuation(self):
618 eq = self.ndiffAssertEqual
619 msg = email.message_from_string("""\
620Subject: bug demonstration
621\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
622\tmore text
623
624test
625""")
626 sfp = StringIO()
627 g = Generator(sfp)
628 g.flatten(msg)
629 eq(sfp.getvalue(), """\
630Subject: bug demonstration
631\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
632\tmore text
633
634test
635""")
636
637 def test_another_long_almost_unsplittable_header(self):
638 eq = self.ndiffAssertEqual
639 hstr = """\
640bug demonstration
641\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
642\tmore text"""
643 h = Header(hstr, continuation_ws='\t')
644 eq(h.encode(), """\
645bug demonstration
646\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
647\tmore text""")
648 h = Header(hstr.replace('\t', ' '))
649 eq(h.encode(), """\
650bug demonstration
651 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
652 more text""")
653
654 def test_long_nonstring(self):
655 eq = self.ndiffAssertEqual
656 g = Charset("iso-8859-1")
657 cz = Charset("iso-8859-2")
658 utf8 = Charset("utf-8")
659 g_head = (b'Die Mieter treten hier ein werden mit einem Foerderband '
660 b'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen '
661 b'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen '
662 b'bef\xf6rdert. ')
663 cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich '
664 b'd\xf9vtipu.. ')
665 utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
666 '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
667 '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
668 '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
669 '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das '
670 'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder '
671 'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066'
672 '\u3044\u307e\u3059\u3002')
673 h = Header(g_head, g, header_name='Subject')
674 h.append(cz_head, cz)
675 h.append(utf8_head, utf8)
676 msg = Message()
677 msg['Subject'] = h
678 sfp = StringIO()
679 g = Generator(sfp)
680 g.flatten(msg)
681 eq(sfp.getvalue(), """\
Guido van Rossum9604e662007-08-30 03:46:43 +0000682Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderb?=
683 =?iso-8859-1?q?and_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen?=
684 =?iso-8859-1?q?_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef?=
685 =?iso-8859-1?q?=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hrouti?=
686 =?iso-8859-2?q?ly_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?=
687 =?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?=
688 =?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn44Gf44KJ?=
689 =?utf-8?b?44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFzIE51bnN0dWNr?=
690 =?utf-8?b?IGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5kIGRhcyBPZGVyIGRp?=
691 =?utf-8?b?ZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIDjgaPjgabjgYTjgb7jgZk=?=
692 =?utf-8?b?44CC?=
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000693
694""")
Guido van Rossum9604e662007-08-30 03:46:43 +0000695 eq(h.encode(maxlinelen=76), """\
696=?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerde?=
697 =?iso-8859-1?q?rband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndis?=
698 =?iso-8859-1?q?chen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klinge?=
699 =?iso-8859-1?q?n_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se?=
700 =?iso-8859-2?q?_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?=
701 =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb?=
702 =?utf-8?b?44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go?=
703 =?utf-8?b?44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBp?=
704 =?utf-8?b?c3QgZGFzIE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWlo?=
705 =?utf-8?b?ZXJodW5kIGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI0=?=
706 =?utf-8?b?44Go6KiA44Gj44Gm44GE44G+44GZ44CC?=""")
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000707
708 def test_long_header_encode(self):
709 eq = self.ndiffAssertEqual
710 h = Header('wasnipoop; giraffes="very-long-necked-animals"; '
711 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
712 header_name='X-Foobar-Spoink-Defrobnit')
713 eq(h.encode(), '''\
714wasnipoop; giraffes="very-long-necked-animals";
715 spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
716
717 def test_long_header_encode_with_tab_continuation_is_just_a_hint(self):
718 eq = self.ndiffAssertEqual
719 h = Header('wasnipoop; giraffes="very-long-necked-animals"; '
720 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
721 header_name='X-Foobar-Spoink-Defrobnit',
722 continuation_ws='\t')
723 eq(h.encode(), '''\
724wasnipoop; giraffes="very-long-necked-animals";
725 spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
726
727 def test_long_header_encode_with_tab_continuation(self):
728 eq = self.ndiffAssertEqual
729 h = Header('wasnipoop; giraffes="very-long-necked-animals";\t'
730 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
731 header_name='X-Foobar-Spoink-Defrobnit',
732 continuation_ws='\t')
733 eq(h.encode(), '''\
734wasnipoop; giraffes="very-long-necked-animals";
735\tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
736
R David Murray3a6152f2011-03-14 21:13:03 -0400737 def test_header_encode_with_different_output_charset(self):
738 h = Header('文', 'euc-jp')
739 self.assertEqual(h.encode(), "=?iso-2022-jp?b?GyRCSjgbKEI=?=")
740
741 def test_long_header_encode_with_different_output_charset(self):
742 h = Header(b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5\xa4\xec\xa4'
743 b'\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2\xf1\xbc\xd4'
744 b'\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3\xa4\xc6\xa4'
745 b'\xa4\xa4\xde\xa4\xb9'.decode('euc-jp'), 'euc-jp')
746 res = """\
747=?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKMnE8VCROPjUbKEI=?=
748 =?iso-2022-jp?b?GyRCRyckckJUJEMkRiQkJF4kORsoQg==?="""
749 self.assertEqual(h.encode(), res)
750
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000751 def test_header_splitter(self):
752 eq = self.ndiffAssertEqual
753 msg = MIMEText('')
754 # It'd be great if we could use add_header() here, but that doesn't
755 # guarantee an order of the parameters.
756 msg['X-Foobar-Spoink-Defrobnit'] = (
757 'wasnipoop; giraffes="very-long-necked-animals"; '
758 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"')
759 sfp = StringIO()
760 g = Generator(sfp)
761 g.flatten(msg)
762 eq(sfp.getvalue(), '''\
763Content-Type: text/plain; charset="us-ascii"
764MIME-Version: 1.0
765Content-Transfer-Encoding: 7bit
766X-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals";
767 spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"
768
769''')
770
771 def test_no_semis_header_splitter(self):
772 eq = self.ndiffAssertEqual
773 msg = Message()
774 msg['From'] = 'test@dom.ain'
775 msg['References'] = SPACE.join('<%d@dom.ain>' % i for i in range(10))
776 msg.set_payload('Test')
777 sfp = StringIO()
778 g = Generator(sfp)
779 g.flatten(msg)
780 eq(sfp.getvalue(), """\
781From: test@dom.ain
782References: <0@dom.ain> <1@dom.ain> <2@dom.ain> <3@dom.ain> <4@dom.ain>
783 <5@dom.ain> <6@dom.ain> <7@dom.ain> <8@dom.ain> <9@dom.ain>
784
785Test""")
786
787 def test_no_split_long_header(self):
788 eq = self.ndiffAssertEqual
789 hstr = 'References: ' + 'x' * 80
Guido van Rossum9604e662007-08-30 03:46:43 +0000790 h = Header(hstr)
791 # These come on two lines because Headers are really field value
792 # classes and don't really know about their field names.
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000793 eq(h.encode(), """\
Guido van Rossum9604e662007-08-30 03:46:43 +0000794References:
795 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""")
796 h = Header('x' * 80)
797 eq(h.encode(), 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000798
799 def test_splitting_multiple_long_lines(self):
800 eq = self.ndiffAssertEqual
801 hstr = """\
802from babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST)
803\tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST)
804\tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST)
805"""
806 h = Header(hstr, continuation_ws='\t')
807 eq(h.encode(), """\
808from babylon.socal-raves.org (localhost [127.0.0.1]);
809 by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
810 for <mailman-admin@babylon.socal-raves.org>;
811 Sat, 2 Feb 2002 17:00:06 -0800 (PST)
812\tfrom babylon.socal-raves.org (localhost [127.0.0.1]);
813 by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
814 for <mailman-admin@babylon.socal-raves.org>;
815 Sat, 2 Feb 2002 17:00:06 -0800 (PST)
816\tfrom babylon.socal-raves.org (localhost [127.0.0.1]);
817 by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
818 for <mailman-admin@babylon.socal-raves.org>;
819 Sat, 2 Feb 2002 17:00:06 -0800 (PST)""")
820
821 def test_splitting_first_line_only_is_long(self):
822 eq = self.ndiffAssertEqual
823 hstr = """\
824from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] helo=cthulhu.gerg.ca)
825\tby kronos.mems-exchange.org with esmtp (Exim 4.05)
826\tid 17k4h5-00034i-00
827\tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400"""
828 h = Header(hstr, maxlinelen=78, header_name='Received',
829 continuation_ws='\t')
830 eq(h.encode(), """\
831from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93]
832 helo=cthulhu.gerg.ca)
833\tby kronos.mems-exchange.org with esmtp (Exim 4.05)
834\tid 17k4h5-00034i-00
835\tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400""")
836
837 def test_long_8bit_header(self):
838 eq = self.ndiffAssertEqual
839 msg = Message()
840 h = Header('Britische Regierung gibt', 'iso-8859-1',
841 header_name='Subject')
842 h.append('gr\xfcnes Licht f\xfcr Offshore-Windkraftprojekte')
Guido van Rossum9604e662007-08-30 03:46:43 +0000843 eq(h.encode(maxlinelen=76), """\
844=?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offs?=
845 =?iso-8859-1?q?hore-Windkraftprojekte?=""")
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000846 msg['Subject'] = h
Guido van Rossum9604e662007-08-30 03:46:43 +0000847 eq(msg.as_string(maxheaderlen=76), """\
848Subject: =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offs?=
849 =?iso-8859-1?q?hore-Windkraftprojekte?=
850
851""")
852 eq(msg.as_string(maxheaderlen=0), """\
853Subject: =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offshore-Windkraftprojekte?=
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000854
855""")
856
857 def test_long_8bit_header_no_charset(self):
858 eq = self.ndiffAssertEqual
859 msg = Message()
Barry Warsaw8c571042007-08-30 19:17:18 +0000860 header_string = ('Britische Regierung gibt gr\xfcnes Licht '
861 'f\xfcr Offshore-Windkraftprojekte '
862 '<a-very-long-address@example.com>')
863 msg['Reply-To'] = header_string
864 self.assertRaises(UnicodeEncodeError, msg.as_string)
865 msg = Message()
866 msg['Reply-To'] = Header(header_string, 'utf-8',
867 header_name='Reply-To')
868 eq(msg.as_string(maxheaderlen=78), """\
869Reply-To: =?utf-8?q?Britische_Regierung_gibt_gr=C3=BCnes_Licht_f=C3=BCr_Offs?=
870 =?utf-8?q?hore-Windkraftprojekte_=3Ca-very-long-address=40example=2Ecom=3E?=
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000871
872""")
873
874 def test_long_to_header(self):
875 eq = self.ndiffAssertEqual
876 to = ('"Someone Test #A" <someone@eecs.umich.edu>,'
877 '<someone@eecs.umich.edu>,'
878 '"Someone Test #B" <someone@umich.edu>, '
879 '"Someone Test #C" <someone@eecs.umich.edu>, '
880 '"Someone Test #D" <someone@eecs.umich.edu>')
881 msg = Message()
882 msg['To'] = to
883 eq(msg.as_string(maxheaderlen=78), '''\
Guido van Rossum9604e662007-08-30 03:46:43 +0000884To: "Someone Test #A" <someone@eecs.umich.edu>,<someone@eecs.umich.edu>,
Barry Warsaw70d61ce2009-03-30 23:12:30 +0000885 "Someone Test #B" <someone@umich.edu>,
Guido van Rossum9604e662007-08-30 03:46:43 +0000886 "Someone Test #C" <someone@eecs.umich.edu>,
887 "Someone Test #D" <someone@eecs.umich.edu>
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000888
889''')
890
891 def test_long_line_after_append(self):
892 eq = self.ndiffAssertEqual
893 s = 'This is an example of string which has almost the limit of header length.'
894 h = Header(s)
895 h.append('Add another line.')
Guido van Rossum9604e662007-08-30 03:46:43 +0000896 eq(h.encode(maxlinelen=76), """\
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000897This is an example of string which has almost the limit of header length.
898 Add another line.""")
899
900 def test_shorter_line_with_append(self):
901 eq = self.ndiffAssertEqual
902 s = 'This is a shorter line.'
903 h = Header(s)
904 h.append('Add another sentence. (Surprise?)')
905 eq(h.encode(),
906 'This is a shorter line. Add another sentence. (Surprise?)')
907
908 def test_long_field_name(self):
909 eq = self.ndiffAssertEqual
910 fn = 'X-Very-Very-Very-Long-Header-Name'
Guido van Rossum9604e662007-08-30 03:46:43 +0000911 gs = ('Die Mieter treten hier ein werden mit einem Foerderband '
912 'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen '
913 'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen '
914 'bef\xf6rdert. ')
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000915 h = Header(gs, 'iso-8859-1', header_name=fn)
916 # BAW: this seems broken because the first line is too long
Guido van Rossum9604e662007-08-30 03:46:43 +0000917 eq(h.encode(maxlinelen=76), """\
918=?iso-8859-1?q?Die_Mieter_treten_hier_e?=
919 =?iso-8859-1?q?in_werden_mit_einem_Foerderband_komfortabel_den_Korridor_e?=
920 =?iso-8859-1?q?ntlang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei=2C_ge?=
921 =?iso-8859-1?q?gen_die_rotierenden_Klingen_bef=F6rdert=2E_?=""")
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000922
923 def test_long_received_header(self):
924 h = ('from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) '
925 'by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; '
926 'Wed, 05 Mar 2003 18:10:18 -0700')
927 msg = Message()
928 msg['Received-1'] = Header(h, continuation_ws='\t')
929 msg['Received-2'] = h
Barry Warsawbef9d212007-08-31 10:55:37 +0000930 # This should be splitting on spaces not semicolons.
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000931 self.ndiffAssertEqual(msg.as_string(maxheaderlen=78), """\
Barry Warsawbef9d212007-08-31 10:55:37 +0000932Received-1: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP;
933 Wed, 05 Mar 2003 18:10:18 -0700
934Received-2: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP;
935 Wed, 05 Mar 2003 18:10:18 -0700
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000936
937""")
938
939 def test_string_headerinst_eq(self):
940 h = ('<15975.17901.207240.414604@sgigritzmann1.mathematik.'
941 'tu-muenchen.de> (David Bremner\'s message of '
942 '"Thu, 6 Mar 2003 13:58:21 +0100")')
943 msg = Message()
944 msg['Received-1'] = Header(h, header_name='Received-1',
945 continuation_ws='\t')
946 msg['Received-2'] = h
Barry Warsawbef9d212007-08-31 10:55:37 +0000947 # XXX This should be splitting on spaces not commas.
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000948 self.ndiffAssertEqual(msg.as_string(maxheaderlen=78), """\
Barry Warsawbef9d212007-08-31 10:55:37 +0000949Received-1: <15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David Bremner's message of \"Thu,
950 6 Mar 2003 13:58:21 +0100\")
951Received-2: <15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David Bremner's message of \"Thu,
952 6 Mar 2003 13:58:21 +0100\")
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000953
954""")
955
956 def test_long_unbreakable_lines_with_continuation(self):
957 eq = self.ndiffAssertEqual
958 msg = Message()
959 t = """\
960iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9
961 locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp"""
962 msg['Face-1'] = t
963 msg['Face-2'] = Header(t, header_name='Face-2')
Barry Warsawbef9d212007-08-31 10:55:37 +0000964 # XXX This splitting is all wrong. It the first value line should be
965 # snug against the field name.
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000966 eq(msg.as_string(maxheaderlen=78), """\
Barry Warsawc5a6a302007-08-31 11:19:21 +0000967Face-1:\x20
Barry Warsaw70d61ce2009-03-30 23:12:30 +0000968 iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000969 locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp
Barry Warsawc5a6a302007-08-31 11:19:21 +0000970Face-2:\x20
Barry Warsawbef9d212007-08-31 10:55:37 +0000971 iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000972 locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp
973
974""")
975
976 def test_another_long_multiline_header(self):
977 eq = self.ndiffAssertEqual
978 m = ('Received: from siimage.com '
979 '([172.25.1.3]) by zima.siliconimage.com with '
Guido van Rossum9604e662007-08-30 03:46:43 +0000980 'Microsoft SMTPSVC(5.0.2195.4905); '
981 'Wed, 16 Oct 2002 07:41:11 -0700')
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000982 msg = email.message_from_string(m)
983 eq(msg.as_string(maxheaderlen=78), '''\
Barry Warsawbef9d212007-08-31 10:55:37 +0000984Received: from siimage.com ([172.25.1.3]) by zima.siliconimage.com with Microsoft SMTPSVC(5.0.2195.4905);
985 Wed, 16 Oct 2002 07:41:11 -0700
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000986
987''')
988
989 def test_long_lines_with_different_header(self):
990 eq = self.ndiffAssertEqual
991 h = ('List-Unsubscribe: '
992 '<http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,'
993 ' <mailto:spamassassin-talk-request@lists.sourceforge.net'
994 '?subject=unsubscribe>')
995 msg = Message()
996 msg['List'] = h
997 msg['List'] = Header(h, header_name='List')
998 eq(msg.as_string(maxheaderlen=78), """\
999List: List-Unsubscribe: <http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,
Barry Warsawbef9d212007-08-31 10:55:37 +00001000 <mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe>
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001001List: List-Unsubscribe: <http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,
Barry Warsawbef9d212007-08-31 10:55:37 +00001002 <mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe>
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001003
1004""")
1005
R. David Murray6f0022d2011-01-07 21:57:25 +00001006 def test_long_rfc2047_header_with_embedded_fws(self):
1007 h = Header(textwrap.dedent("""\
1008 We're going to pretend this header is in a non-ascii character set
1009 \tto see if line wrapping with encoded words and embedded
1010 folding white space works"""),
1011 charset='utf-8',
1012 header_name='Test')
1013 self.assertEqual(h.encode()+'\n', textwrap.dedent("""\
1014 =?utf-8?q?We=27re_going_to_pretend_this_header_is_in_a_non-ascii_chara?=
1015 =?utf-8?q?cter_set?=
1016 =?utf-8?q?_to_see_if_line_wrapping_with_encoded_words_and_embedded?=
1017 =?utf-8?q?_folding_white_space_works?=""")+'\n')
1018
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001019
Ezio Melottib3aedd42010-11-20 19:04:17 +00001020
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001021# Test mangling of "From " lines in the body of a message
1022class TestFromMangling(unittest.TestCase):
1023 def setUp(self):
1024 self.msg = Message()
1025 self.msg['From'] = 'aaa@bbb.org'
1026 self.msg.set_payload("""\
1027From the desk of A.A.A.:
1028Blah blah blah
1029""")
1030
1031 def test_mangled_from(self):
1032 s = StringIO()
1033 g = Generator(s, mangle_from_=True)
1034 g.flatten(self.msg)
1035 self.assertEqual(s.getvalue(), """\
1036From: aaa@bbb.org
1037
1038>From the desk of A.A.A.:
1039Blah blah blah
1040""")
1041
1042 def test_dont_mangle_from(self):
1043 s = StringIO()
1044 g = Generator(s, mangle_from_=False)
1045 g.flatten(self.msg)
1046 self.assertEqual(s.getvalue(), """\
1047From: aaa@bbb.org
1048
1049From the desk of A.A.A.:
1050Blah blah blah
1051""")
1052
1053
Ezio Melottib3aedd42010-11-20 19:04:17 +00001054
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001055# Test the basic MIMEAudio class
1056class TestMIMEAudio(unittest.TestCase):
1057 def setUp(self):
1058 # Make sure we pick up the audiotest.au that lives in email/test/data.
1059 # In Python, there's an audiotest.au living in Lib/test but that isn't
1060 # included in some binary distros that don't include the test
1061 # package. The trailing empty string on the .join() is significant
1062 # since findfile() will do a dirname().
1063 datadir = os.path.join(os.path.dirname(landmark), 'data', '')
1064 with open(findfile('audiotest.au', datadir), 'rb') as fp:
1065 self._audiodata = fp.read()
1066 self._au = MIMEAudio(self._audiodata)
1067
1068 def test_guess_minor_type(self):
1069 self.assertEqual(self._au.get_content_type(), 'audio/basic')
1070
1071 def test_encoding(self):
1072 payload = self._au.get_payload()
R. David Murray7da8f062010-06-04 16:11:08 +00001073 self.assertEqual(base64.decodebytes(bytes(payload, 'ascii')),
1074 self._audiodata)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001075
1076 def test_checkSetMinor(self):
1077 au = MIMEAudio(self._audiodata, 'fish')
1078 self.assertEqual(au.get_content_type(), 'audio/fish')
1079
1080 def test_add_header(self):
1081 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001082 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001083 self._au.add_header('Content-Disposition', 'attachment',
1084 filename='audiotest.au')
1085 eq(self._au['content-disposition'],
1086 'attachment; filename="audiotest.au"')
1087 eq(self._au.get_params(header='content-disposition'),
1088 [('attachment', ''), ('filename', 'audiotest.au')])
1089 eq(self._au.get_param('filename', header='content-disposition'),
1090 'audiotest.au')
1091 missing = []
1092 eq(self._au.get_param('attachment', header='content-disposition'), '')
1093 unless(self._au.get_param('foo', failobj=missing,
1094 header='content-disposition') is missing)
1095 # Try some missing stuff
1096 unless(self._au.get_param('foobar', missing) is missing)
1097 unless(self._au.get_param('attachment', missing,
1098 header='foobar') is missing)
1099
1100
Ezio Melottib3aedd42010-11-20 19:04:17 +00001101
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001102# Test the basic MIMEImage class
1103class TestMIMEImage(unittest.TestCase):
1104 def setUp(self):
1105 with openfile('PyBanner048.gif', 'rb') as fp:
1106 self._imgdata = fp.read()
1107 self._im = MIMEImage(self._imgdata)
1108
1109 def test_guess_minor_type(self):
1110 self.assertEqual(self._im.get_content_type(), 'image/gif')
1111
1112 def test_encoding(self):
1113 payload = self._im.get_payload()
R. David Murray7da8f062010-06-04 16:11:08 +00001114 self.assertEqual(base64.decodebytes(bytes(payload, 'ascii')),
1115 self._imgdata)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001116
1117 def test_checkSetMinor(self):
1118 im = MIMEImage(self._imgdata, 'fish')
1119 self.assertEqual(im.get_content_type(), 'image/fish')
1120
1121 def test_add_header(self):
1122 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001123 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001124 self._im.add_header('Content-Disposition', 'attachment',
1125 filename='dingusfish.gif')
1126 eq(self._im['content-disposition'],
1127 'attachment; filename="dingusfish.gif"')
1128 eq(self._im.get_params(header='content-disposition'),
1129 [('attachment', ''), ('filename', 'dingusfish.gif')])
1130 eq(self._im.get_param('filename', header='content-disposition'),
1131 'dingusfish.gif')
1132 missing = []
1133 eq(self._im.get_param('attachment', header='content-disposition'), '')
1134 unless(self._im.get_param('foo', failobj=missing,
1135 header='content-disposition') is missing)
1136 # Try some missing stuff
1137 unless(self._im.get_param('foobar', missing) is missing)
1138 unless(self._im.get_param('attachment', missing,
1139 header='foobar') is missing)
1140
1141
Ezio Melottib3aedd42010-11-20 19:04:17 +00001142
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001143# Test the basic MIMEApplication class
1144class TestMIMEApplication(unittest.TestCase):
1145 def test_headers(self):
1146 eq = self.assertEqual
Barry Warsaw8b2af272007-08-31 03:04:26 +00001147 msg = MIMEApplication(b'\xfa\xfb\xfc\xfd\xfe\xff')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001148 eq(msg.get_content_type(), 'application/octet-stream')
1149 eq(msg['content-transfer-encoding'], 'base64')
1150
1151 def test_body(self):
1152 eq = self.assertEqual
R David Murray6d94bd42011-03-16 15:52:22 -04001153 bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
1154 msg = MIMEApplication(bytesdata)
1155 # whitespace in the cte encoded block is RFC-irrelevant.
1156 eq(msg.get_payload().strip(), '+vv8/f7/')
1157 eq(msg.get_payload(decode=True), bytesdata)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001158
1159
Ezio Melottib3aedd42010-11-20 19:04:17 +00001160
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001161# Test the basic MIMEText class
1162class TestMIMEText(unittest.TestCase):
1163 def setUp(self):
1164 self._msg = MIMEText('hello there')
1165
1166 def test_types(self):
1167 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001168 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001169 eq(self._msg.get_content_type(), 'text/plain')
1170 eq(self._msg.get_param('charset'), 'us-ascii')
1171 missing = []
1172 unless(self._msg.get_param('foobar', missing) is missing)
1173 unless(self._msg.get_param('charset', missing, header='foobar')
1174 is missing)
1175
1176 def test_payload(self):
1177 self.assertEqual(self._msg.get_payload(), 'hello there')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001178 self.assertTrue(not self._msg.is_multipart())
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001179
1180 def test_charset(self):
1181 eq = self.assertEqual
1182 msg = MIMEText('hello there', _charset='us-ascii')
1183 eq(msg.get_charset().input_charset, 'us-ascii')
1184 eq(msg['content-type'], 'text/plain; charset="us-ascii"')
1185
R. David Murray850fc852010-06-03 01:58:28 +00001186 def test_7bit_input(self):
1187 eq = self.assertEqual
1188 msg = MIMEText('hello there', _charset='us-ascii')
1189 eq(msg.get_charset().input_charset, 'us-ascii')
1190 eq(msg['content-type'], 'text/plain; charset="us-ascii"')
1191
1192 def test_7bit_input_no_charset(self):
1193 eq = self.assertEqual
1194 msg = MIMEText('hello there')
1195 eq(msg.get_charset(), 'us-ascii')
1196 eq(msg['content-type'], 'text/plain; charset="us-ascii"')
1197 self.assertTrue('hello there' in msg.as_string())
1198
1199 def test_utf8_input(self):
1200 teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430'
1201 eq = self.assertEqual
1202 msg = MIMEText(teststr, _charset='utf-8')
1203 eq(msg.get_charset().output_charset, 'utf-8')
1204 eq(msg['content-type'], 'text/plain; charset="utf-8"')
1205 eq(msg.get_payload(decode=True), teststr.encode('utf-8'))
1206
1207 @unittest.skip("can't fix because of backward compat in email5, "
1208 "will fix in email6")
1209 def test_utf8_input_no_charset(self):
1210 teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430'
1211 self.assertRaises(UnicodeEncodeError, MIMEText, teststr)
1212
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001213
Ezio Melottib3aedd42010-11-20 19:04:17 +00001214
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001215# Test complicated multipart/* messages
1216class TestMultipart(TestEmailBase):
1217 def setUp(self):
1218 with openfile('PyBanner048.gif', 'rb') as fp:
1219 data = fp.read()
1220 container = MIMEBase('multipart', 'mixed', boundary='BOUNDARY')
1221 image = MIMEImage(data, name='dingusfish.gif')
1222 image.add_header('content-disposition', 'attachment',
1223 filename='dingusfish.gif')
1224 intro = MIMEText('''\
1225Hi there,
1226
1227This is the dingus fish.
1228''')
1229 container.attach(intro)
1230 container.attach(image)
1231 container['From'] = 'Barry <barry@digicool.com>'
1232 container['To'] = 'Dingus Lovers <cravindogs@cravindogs.com>'
1233 container['Subject'] = 'Here is your dingus fish'
1234
1235 now = 987809702.54848599
1236 timetuple = time.localtime(now)
1237 if timetuple[-1] == 0:
1238 tzsecs = time.timezone
1239 else:
1240 tzsecs = time.altzone
1241 if tzsecs > 0:
1242 sign = '-'
1243 else:
1244 sign = '+'
1245 tzoffset = ' %s%04d' % (sign, tzsecs / 36)
1246 container['Date'] = time.strftime(
1247 '%a, %d %b %Y %H:%M:%S',
1248 time.localtime(now)) + tzoffset
1249 self._msg = container
1250 self._im = image
1251 self._txt = intro
1252
1253 def test_hierarchy(self):
1254 # convenience
1255 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001256 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001257 raises = self.assertRaises
1258 # tests
1259 m = self._msg
1260 unless(m.is_multipart())
1261 eq(m.get_content_type(), 'multipart/mixed')
1262 eq(len(m.get_payload()), 2)
1263 raises(IndexError, m.get_payload, 2)
1264 m0 = m.get_payload(0)
1265 m1 = m.get_payload(1)
1266 unless(m0 is self._txt)
1267 unless(m1 is self._im)
1268 eq(m.get_payload(), [m0, m1])
1269 unless(not m0.is_multipart())
1270 unless(not m1.is_multipart())
1271
1272 def test_empty_multipart_idempotent(self):
1273 text = """\
1274Content-Type: multipart/mixed; boundary="BOUNDARY"
1275MIME-Version: 1.0
1276Subject: A subject
1277To: aperson@dom.ain
1278From: bperson@dom.ain
1279
1280
1281--BOUNDARY
1282
1283
1284--BOUNDARY--
1285"""
1286 msg = Parser().parsestr(text)
1287 self.ndiffAssertEqual(text, msg.as_string())
1288
1289 def test_no_parts_in_a_multipart_with_none_epilogue(self):
1290 outer = MIMEBase('multipart', 'mixed')
1291 outer['Subject'] = 'A subject'
1292 outer['To'] = 'aperson@dom.ain'
1293 outer['From'] = 'bperson@dom.ain'
1294 outer.set_boundary('BOUNDARY')
1295 self.ndiffAssertEqual(outer.as_string(), '''\
1296Content-Type: multipart/mixed; boundary="BOUNDARY"
1297MIME-Version: 1.0
1298Subject: A subject
1299To: aperson@dom.ain
1300From: bperson@dom.ain
1301
1302--BOUNDARY
1303
1304--BOUNDARY--''')
1305
1306 def test_no_parts_in_a_multipart_with_empty_epilogue(self):
1307 outer = MIMEBase('multipart', 'mixed')
1308 outer['Subject'] = 'A subject'
1309 outer['To'] = 'aperson@dom.ain'
1310 outer['From'] = 'bperson@dom.ain'
1311 outer.preamble = ''
1312 outer.epilogue = ''
1313 outer.set_boundary('BOUNDARY')
1314 self.ndiffAssertEqual(outer.as_string(), '''\
1315Content-Type: multipart/mixed; boundary="BOUNDARY"
1316MIME-Version: 1.0
1317Subject: A subject
1318To: aperson@dom.ain
1319From: bperson@dom.ain
1320
1321
1322--BOUNDARY
1323
1324--BOUNDARY--
1325''')
1326
1327 def test_one_part_in_a_multipart(self):
1328 eq = self.ndiffAssertEqual
1329 outer = MIMEBase('multipart', 'mixed')
1330 outer['Subject'] = 'A subject'
1331 outer['To'] = 'aperson@dom.ain'
1332 outer['From'] = 'bperson@dom.ain'
1333 outer.set_boundary('BOUNDARY')
1334 msg = MIMEText('hello world')
1335 outer.attach(msg)
1336 eq(outer.as_string(), '''\
1337Content-Type: multipart/mixed; boundary="BOUNDARY"
1338MIME-Version: 1.0
1339Subject: A subject
1340To: aperson@dom.ain
1341From: bperson@dom.ain
1342
1343--BOUNDARY
1344Content-Type: text/plain; charset="us-ascii"
1345MIME-Version: 1.0
1346Content-Transfer-Encoding: 7bit
1347
1348hello world
1349--BOUNDARY--''')
1350
1351 def test_seq_parts_in_a_multipart_with_empty_preamble(self):
1352 eq = self.ndiffAssertEqual
1353 outer = MIMEBase('multipart', 'mixed')
1354 outer['Subject'] = 'A subject'
1355 outer['To'] = 'aperson@dom.ain'
1356 outer['From'] = 'bperson@dom.ain'
1357 outer.preamble = ''
1358 msg = MIMEText('hello world')
1359 outer.attach(msg)
1360 outer.set_boundary('BOUNDARY')
1361 eq(outer.as_string(), '''\
1362Content-Type: multipart/mixed; boundary="BOUNDARY"
1363MIME-Version: 1.0
1364Subject: A subject
1365To: aperson@dom.ain
1366From: bperson@dom.ain
1367
1368
1369--BOUNDARY
1370Content-Type: text/plain; charset="us-ascii"
1371MIME-Version: 1.0
1372Content-Transfer-Encoding: 7bit
1373
1374hello world
1375--BOUNDARY--''')
1376
1377
1378 def test_seq_parts_in_a_multipart_with_none_preamble(self):
1379 eq = self.ndiffAssertEqual
1380 outer = MIMEBase('multipart', 'mixed')
1381 outer['Subject'] = 'A subject'
1382 outer['To'] = 'aperson@dom.ain'
1383 outer['From'] = 'bperson@dom.ain'
1384 outer.preamble = None
1385 msg = MIMEText('hello world')
1386 outer.attach(msg)
1387 outer.set_boundary('BOUNDARY')
1388 eq(outer.as_string(), '''\
1389Content-Type: multipart/mixed; boundary="BOUNDARY"
1390MIME-Version: 1.0
1391Subject: A subject
1392To: aperson@dom.ain
1393From: bperson@dom.ain
1394
1395--BOUNDARY
1396Content-Type: text/plain; charset="us-ascii"
1397MIME-Version: 1.0
1398Content-Transfer-Encoding: 7bit
1399
1400hello world
1401--BOUNDARY--''')
1402
1403
1404 def test_seq_parts_in_a_multipart_with_none_epilogue(self):
1405 eq = self.ndiffAssertEqual
1406 outer = MIMEBase('multipart', 'mixed')
1407 outer['Subject'] = 'A subject'
1408 outer['To'] = 'aperson@dom.ain'
1409 outer['From'] = 'bperson@dom.ain'
1410 outer.epilogue = None
1411 msg = MIMEText('hello world')
1412 outer.attach(msg)
1413 outer.set_boundary('BOUNDARY')
1414 eq(outer.as_string(), '''\
1415Content-Type: multipart/mixed; boundary="BOUNDARY"
1416MIME-Version: 1.0
1417Subject: A subject
1418To: aperson@dom.ain
1419From: bperson@dom.ain
1420
1421--BOUNDARY
1422Content-Type: text/plain; charset="us-ascii"
1423MIME-Version: 1.0
1424Content-Transfer-Encoding: 7bit
1425
1426hello world
1427--BOUNDARY--''')
1428
1429
1430 def test_seq_parts_in_a_multipart_with_empty_epilogue(self):
1431 eq = self.ndiffAssertEqual
1432 outer = MIMEBase('multipart', 'mixed')
1433 outer['Subject'] = 'A subject'
1434 outer['To'] = 'aperson@dom.ain'
1435 outer['From'] = 'bperson@dom.ain'
1436 outer.epilogue = ''
1437 msg = MIMEText('hello world')
1438 outer.attach(msg)
1439 outer.set_boundary('BOUNDARY')
1440 eq(outer.as_string(), '''\
1441Content-Type: multipart/mixed; boundary="BOUNDARY"
1442MIME-Version: 1.0
1443Subject: A subject
1444To: aperson@dom.ain
1445From: bperson@dom.ain
1446
1447--BOUNDARY
1448Content-Type: text/plain; charset="us-ascii"
1449MIME-Version: 1.0
1450Content-Transfer-Encoding: 7bit
1451
1452hello world
1453--BOUNDARY--
1454''')
1455
1456
1457 def test_seq_parts_in_a_multipart_with_nl_epilogue(self):
1458 eq = self.ndiffAssertEqual
1459 outer = MIMEBase('multipart', 'mixed')
1460 outer['Subject'] = 'A subject'
1461 outer['To'] = 'aperson@dom.ain'
1462 outer['From'] = 'bperson@dom.ain'
1463 outer.epilogue = '\n'
1464 msg = MIMEText('hello world')
1465 outer.attach(msg)
1466 outer.set_boundary('BOUNDARY')
1467 eq(outer.as_string(), '''\
1468Content-Type: multipart/mixed; boundary="BOUNDARY"
1469MIME-Version: 1.0
1470Subject: A subject
1471To: aperson@dom.ain
1472From: bperson@dom.ain
1473
1474--BOUNDARY
1475Content-Type: text/plain; charset="us-ascii"
1476MIME-Version: 1.0
1477Content-Transfer-Encoding: 7bit
1478
1479hello world
1480--BOUNDARY--
1481
1482''')
1483
1484 def test_message_external_body(self):
1485 eq = self.assertEqual
1486 msg = self._msgobj('msg_36.txt')
1487 eq(len(msg.get_payload()), 2)
1488 msg1 = msg.get_payload(1)
1489 eq(msg1.get_content_type(), 'multipart/alternative')
1490 eq(len(msg1.get_payload()), 2)
1491 for subpart in msg1.get_payload():
1492 eq(subpart.get_content_type(), 'message/external-body')
1493 eq(len(subpart.get_payload()), 1)
1494 subsubpart = subpart.get_payload(0)
1495 eq(subsubpart.get_content_type(), 'text/plain')
1496
1497 def test_double_boundary(self):
1498 # msg_37.txt is a multipart that contains two dash-boundary's in a
1499 # row. Our interpretation of RFC 2046 calls for ignoring the second
1500 # and subsequent boundaries.
1501 msg = self._msgobj('msg_37.txt')
1502 self.assertEqual(len(msg.get_payload()), 3)
1503
1504 def test_nested_inner_contains_outer_boundary(self):
1505 eq = self.ndiffAssertEqual
1506 # msg_38.txt has an inner part that contains outer boundaries. My
1507 # interpretation of RFC 2046 (based on sections 5.1 and 5.1.2) say
1508 # these are illegal and should be interpreted as unterminated inner
1509 # parts.
1510 msg = self._msgobj('msg_38.txt')
1511 sfp = StringIO()
1512 iterators._structure(msg, sfp)
1513 eq(sfp.getvalue(), """\
1514multipart/mixed
1515 multipart/mixed
1516 multipart/alternative
1517 text/plain
1518 text/plain
1519 text/plain
1520 text/plain
1521""")
1522
1523 def test_nested_with_same_boundary(self):
1524 eq = self.ndiffAssertEqual
1525 # msg 39.txt is similarly evil in that it's got inner parts that use
1526 # the same boundary as outer parts. Again, I believe the way this is
1527 # parsed is closest to the spirit of RFC 2046
1528 msg = self._msgobj('msg_39.txt')
1529 sfp = StringIO()
1530 iterators._structure(msg, sfp)
1531 eq(sfp.getvalue(), """\
1532multipart/mixed
1533 multipart/mixed
1534 multipart/alternative
1535 application/octet-stream
1536 application/octet-stream
1537 text/plain
1538""")
1539
1540 def test_boundary_in_non_multipart(self):
1541 msg = self._msgobj('msg_40.txt')
1542 self.assertEqual(msg.as_string(), '''\
1543MIME-Version: 1.0
1544Content-Type: text/html; boundary="--961284236552522269"
1545
1546----961284236552522269
1547Content-Type: text/html;
1548Content-Transfer-Encoding: 7Bit
1549
1550<html></html>
1551
1552----961284236552522269--
1553''')
1554
1555 def test_boundary_with_leading_space(self):
1556 eq = self.assertEqual
1557 msg = email.message_from_string('''\
1558MIME-Version: 1.0
1559Content-Type: multipart/mixed; boundary=" XXXX"
1560
1561-- XXXX
1562Content-Type: text/plain
1563
1564
1565-- XXXX
1566Content-Type: text/plain
1567
1568-- XXXX--
1569''')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001570 self.assertTrue(msg.is_multipart())
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001571 eq(msg.get_boundary(), ' XXXX')
1572 eq(len(msg.get_payload()), 2)
1573
1574 def test_boundary_without_trailing_newline(self):
1575 m = Parser().parsestr("""\
1576Content-Type: multipart/mixed; boundary="===============0012394164=="
1577MIME-Version: 1.0
1578
1579--===============0012394164==
1580Content-Type: image/file1.jpg
1581MIME-Version: 1.0
1582Content-Transfer-Encoding: base64
1583
1584YXNkZg==
1585--===============0012394164==--""")
Ezio Melottib3aedd42010-11-20 19:04:17 +00001586 self.assertEqual(m.get_payload(0).get_payload(), 'YXNkZg==')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001587
1588
Ezio Melottib3aedd42010-11-20 19:04:17 +00001589
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001590# Test some badly formatted messages
1591class TestNonConformant(TestEmailBase):
1592 def test_parse_missing_minor_type(self):
1593 eq = self.assertEqual
1594 msg = self._msgobj('msg_14.txt')
1595 eq(msg.get_content_type(), 'text/plain')
1596 eq(msg.get_content_maintype(), 'text')
1597 eq(msg.get_content_subtype(), 'plain')
1598
1599 def test_same_boundary_inner_outer(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001600 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001601 msg = self._msgobj('msg_15.txt')
1602 # XXX We can probably eventually do better
1603 inner = msg.get_payload(0)
1604 unless(hasattr(inner, 'defects'))
1605 self.assertEqual(len(inner.defects), 1)
1606 unless(isinstance(inner.defects[0],
1607 errors.StartBoundaryNotFoundDefect))
1608
1609 def test_multipart_no_boundary(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001610 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001611 msg = self._msgobj('msg_25.txt')
1612 unless(isinstance(msg.get_payload(), str))
1613 self.assertEqual(len(msg.defects), 2)
1614 unless(isinstance(msg.defects[0], errors.NoBoundaryInMultipartDefect))
1615 unless(isinstance(msg.defects[1],
1616 errors.MultipartInvariantViolationDefect))
1617
1618 def test_invalid_content_type(self):
1619 eq = self.assertEqual
1620 neq = self.ndiffAssertEqual
1621 msg = Message()
1622 # RFC 2045, $5.2 says invalid yields text/plain
1623 msg['Content-Type'] = 'text'
1624 eq(msg.get_content_maintype(), 'text')
1625 eq(msg.get_content_subtype(), 'plain')
1626 eq(msg.get_content_type(), 'text/plain')
1627 # Clear the old value and try something /really/ invalid
1628 del msg['content-type']
1629 msg['Content-Type'] = 'foo'
1630 eq(msg.get_content_maintype(), 'text')
1631 eq(msg.get_content_subtype(), 'plain')
1632 eq(msg.get_content_type(), 'text/plain')
1633 # Still, make sure that the message is idempotently generated
1634 s = StringIO()
1635 g = Generator(s)
1636 g.flatten(msg)
1637 neq(s.getvalue(), 'Content-Type: foo\n\n')
1638
1639 def test_no_start_boundary(self):
1640 eq = self.ndiffAssertEqual
1641 msg = self._msgobj('msg_31.txt')
1642 eq(msg.get_payload(), """\
1643--BOUNDARY
1644Content-Type: text/plain
1645
1646message 1
1647
1648--BOUNDARY
1649Content-Type: text/plain
1650
1651message 2
1652
1653--BOUNDARY--
1654""")
1655
1656 def test_no_separating_blank_line(self):
1657 eq = self.ndiffAssertEqual
1658 msg = self._msgobj('msg_35.txt')
1659 eq(msg.as_string(), """\
1660From: aperson@dom.ain
1661To: bperson@dom.ain
1662Subject: here's something interesting
1663
1664counter to RFC 2822, there's no separating newline here
1665""")
1666
1667 def test_lying_multipart(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001668 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001669 msg = self._msgobj('msg_41.txt')
1670 unless(hasattr(msg, 'defects'))
1671 self.assertEqual(len(msg.defects), 2)
1672 unless(isinstance(msg.defects[0], errors.NoBoundaryInMultipartDefect))
1673 unless(isinstance(msg.defects[1],
1674 errors.MultipartInvariantViolationDefect))
1675
1676 def test_missing_start_boundary(self):
1677 outer = self._msgobj('msg_42.txt')
1678 # The message structure is:
1679 #
1680 # multipart/mixed
1681 # text/plain
1682 # message/rfc822
1683 # multipart/mixed [*]
1684 #
1685 # [*] This message is missing its start boundary
1686 bad = outer.get_payload(1).get_payload(0)
1687 self.assertEqual(len(bad.defects), 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001688 self.assertTrue(isinstance(bad.defects[0],
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001689 errors.StartBoundaryNotFoundDefect))
1690
1691 def test_first_line_is_continuation_header(self):
1692 eq = self.assertEqual
1693 m = ' Line 1\nLine 2\nLine 3'
1694 msg = email.message_from_string(m)
1695 eq(msg.keys(), [])
1696 eq(msg.get_payload(), 'Line 2\nLine 3')
1697 eq(len(msg.defects), 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001698 self.assertTrue(isinstance(msg.defects[0],
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001699 errors.FirstHeaderLineIsContinuationDefect))
1700 eq(msg.defects[0].line, ' Line 1\n')
1701
1702
Ezio Melottib3aedd42010-11-20 19:04:17 +00001703
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001704# Test RFC 2047 header encoding and decoding
Guido van Rossum9604e662007-08-30 03:46:43 +00001705class TestRFC2047(TestEmailBase):
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001706 def test_rfc2047_multiline(self):
1707 eq = self.assertEqual
1708 s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz
1709 foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?="""
1710 dh = decode_header(s)
1711 eq(dh, [
1712 (b'Re:', None),
1713 (b'r\x8aksm\x9arg\x8cs', 'mac-iceland'),
1714 (b'baz foo bar', None),
1715 (b'r\x8aksm\x9arg\x8cs', 'mac-iceland')])
1716 header = make_header(dh)
1717 eq(str(header),
1718 'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s')
Barry Warsaw00b34222007-08-31 02:35:00 +00001719 self.ndiffAssertEqual(header.encode(maxlinelen=76), """\
Guido van Rossum9604e662007-08-30 03:46:43 +00001720Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?=
1721 =?mac-iceland?q?=9Arg=8Cs?=""")
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001722
1723 def test_whitespace_eater_unicode(self):
1724 eq = self.assertEqual
1725 s = '=?ISO-8859-1?Q?Andr=E9?= Pirard <pirard@dom.ain>'
1726 dh = decode_header(s)
1727 eq(dh, [(b'Andr\xe9', 'iso-8859-1'),
1728 (b'Pirard <pirard@dom.ain>', None)])
1729 header = str(make_header(dh))
1730 eq(header, 'Andr\xe9 Pirard <pirard@dom.ain>')
1731
1732 def test_whitespace_eater_unicode_2(self):
1733 eq = self.assertEqual
1734 s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?='
1735 dh = decode_header(s)
1736 eq(dh, [(b'The', None), (b'quick brown fox', 'iso-8859-1'),
1737 (b'jumped over the', None), (b'lazy dog', 'iso-8859-1')])
1738 hu = str(make_header(dh))
1739 eq(hu, 'The quick brown fox jumped over the lazy dog')
1740
1741 def test_rfc2047_missing_whitespace(self):
1742 s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord'
1743 dh = decode_header(s)
1744 self.assertEqual(dh, [(s, None)])
1745
1746 def test_rfc2047_with_whitespace(self):
1747 s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord'
1748 dh = decode_header(s)
1749 self.assertEqual(dh, [(b'Sm', None), (b'\xf6', 'iso-8859-1'),
1750 (b'rg', None), (b'\xe5', 'iso-8859-1'),
1751 (b'sbord', None)])
1752
R. David Murrayc4e69cc2010-08-03 22:14:10 +00001753 def test_rfc2047_B_bad_padding(self):
1754 s = '=?iso-8859-1?B?%s?='
1755 data = [ # only test complete bytes
1756 ('dm==', b'v'), ('dm=', b'v'), ('dm', b'v'),
1757 ('dmk=', b'vi'), ('dmk', b'vi')
1758 ]
1759 for q, a in data:
1760 dh = decode_header(s % q)
1761 self.assertEqual(dh, [(a, 'iso-8859-1')])
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001762
R. David Murray31e984c2010-10-01 15:40:20 +00001763 def test_rfc2047_Q_invalid_digits(self):
1764 # issue 10004.
1765 s = '=?iso-8659-1?Q?andr=e9=zz?='
1766 self.assertEqual(decode_header(s),
1767 [(b'andr\xe9=zz', 'iso-8659-1')])
1768
Ezio Melottib3aedd42010-11-20 19:04:17 +00001769
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001770# Test the MIMEMessage class
1771class TestMIMEMessage(TestEmailBase):
1772 def setUp(self):
1773 with openfile('msg_11.txt') as fp:
1774 self._text = fp.read()
1775
1776 def test_type_error(self):
1777 self.assertRaises(TypeError, MIMEMessage, 'a plain string')
1778
1779 def test_valid_argument(self):
1780 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001781 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001782 subject = 'A sub-message'
1783 m = Message()
1784 m['Subject'] = subject
1785 r = MIMEMessage(m)
1786 eq(r.get_content_type(), 'message/rfc822')
1787 payload = r.get_payload()
1788 unless(isinstance(payload, list))
1789 eq(len(payload), 1)
1790 subpart = payload[0]
1791 unless(subpart is m)
1792 eq(subpart['subject'], subject)
1793
1794 def test_bad_multipart(self):
1795 eq = self.assertEqual
1796 msg1 = Message()
1797 msg1['Subject'] = 'subpart 1'
1798 msg2 = Message()
1799 msg2['Subject'] = 'subpart 2'
1800 r = MIMEMessage(msg1)
1801 self.assertRaises(errors.MultipartConversionError, r.attach, msg2)
1802
1803 def test_generate(self):
1804 # First craft the message to be encapsulated
1805 m = Message()
1806 m['Subject'] = 'An enclosed message'
1807 m.set_payload('Here is the body of the message.\n')
1808 r = MIMEMessage(m)
1809 r['Subject'] = 'The enclosing message'
1810 s = StringIO()
1811 g = Generator(s)
1812 g.flatten(r)
1813 self.assertEqual(s.getvalue(), """\
1814Content-Type: message/rfc822
1815MIME-Version: 1.0
1816Subject: The enclosing message
1817
1818Subject: An enclosed message
1819
1820Here is the body of the message.
1821""")
1822
1823 def test_parse_message_rfc822(self):
1824 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001825 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001826 msg = self._msgobj('msg_11.txt')
1827 eq(msg.get_content_type(), 'message/rfc822')
1828 payload = msg.get_payload()
1829 unless(isinstance(payload, list))
1830 eq(len(payload), 1)
1831 submsg = payload[0]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001832 self.assertTrue(isinstance(submsg, Message))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001833 eq(submsg['subject'], 'An enclosed message')
1834 eq(submsg.get_payload(), 'Here is the body of the message.\n')
1835
1836 def test_dsn(self):
1837 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001838 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001839 # msg 16 is a Delivery Status Notification, see RFC 1894
1840 msg = self._msgobj('msg_16.txt')
1841 eq(msg.get_content_type(), 'multipart/report')
1842 unless(msg.is_multipart())
1843 eq(len(msg.get_payload()), 3)
1844 # Subpart 1 is a text/plain, human readable section
1845 subpart = msg.get_payload(0)
1846 eq(subpart.get_content_type(), 'text/plain')
1847 eq(subpart.get_payload(), """\
1848This report relates to a message you sent with the following header fields:
1849
1850 Message-id: <002001c144a6$8752e060$56104586@oxy.edu>
1851 Date: Sun, 23 Sep 2001 20:10:55 -0700
1852 From: "Ian T. Henry" <henryi@oxy.edu>
1853 To: SoCal Raves <scr@socal-raves.org>
1854 Subject: [scr] yeah for Ians!!
1855
1856Your message cannot be delivered to the following recipients:
1857
1858 Recipient address: jangel1@cougar.noc.ucla.edu
1859 Reason: recipient reached disk quota
1860
1861""")
1862 # Subpart 2 contains the machine parsable DSN information. It
1863 # consists of two blocks of headers, represented by two nested Message
1864 # objects.
1865 subpart = msg.get_payload(1)
1866 eq(subpart.get_content_type(), 'message/delivery-status')
1867 eq(len(subpart.get_payload()), 2)
1868 # message/delivery-status should treat each block as a bunch of
1869 # headers, i.e. a bunch of Message objects.
1870 dsn1 = subpart.get_payload(0)
1871 unless(isinstance(dsn1, Message))
1872 eq(dsn1['original-envelope-id'], '0GK500B4HD0888@cougar.noc.ucla.edu')
1873 eq(dsn1.get_param('dns', header='reporting-mta'), '')
1874 # Try a missing one <wink>
1875 eq(dsn1.get_param('nsd', header='reporting-mta'), None)
1876 dsn2 = subpart.get_payload(1)
1877 unless(isinstance(dsn2, Message))
1878 eq(dsn2['action'], 'failed')
1879 eq(dsn2.get_params(header='original-recipient'),
1880 [('rfc822', ''), ('jangel1@cougar.noc.ucla.edu', '')])
1881 eq(dsn2.get_param('rfc822', header='final-recipient'), '')
1882 # Subpart 3 is the original message
1883 subpart = msg.get_payload(2)
1884 eq(subpart.get_content_type(), 'message/rfc822')
1885 payload = subpart.get_payload()
1886 unless(isinstance(payload, list))
1887 eq(len(payload), 1)
1888 subsubpart = payload[0]
1889 unless(isinstance(subsubpart, Message))
1890 eq(subsubpart.get_content_type(), 'text/plain')
1891 eq(subsubpart['message-id'],
1892 '<002001c144a6$8752e060$56104586@oxy.edu>')
1893
1894 def test_epilogue(self):
1895 eq = self.ndiffAssertEqual
1896 with openfile('msg_21.txt') as fp:
1897 text = fp.read()
1898 msg = Message()
1899 msg['From'] = 'aperson@dom.ain'
1900 msg['To'] = 'bperson@dom.ain'
1901 msg['Subject'] = 'Test'
1902 msg.preamble = 'MIME message'
1903 msg.epilogue = 'End of MIME message\n'
1904 msg1 = MIMEText('One')
1905 msg2 = MIMEText('Two')
1906 msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY')
1907 msg.attach(msg1)
1908 msg.attach(msg2)
1909 sfp = StringIO()
1910 g = Generator(sfp)
1911 g.flatten(msg)
1912 eq(sfp.getvalue(), text)
1913
1914 def test_no_nl_preamble(self):
1915 eq = self.ndiffAssertEqual
1916 msg = Message()
1917 msg['From'] = 'aperson@dom.ain'
1918 msg['To'] = 'bperson@dom.ain'
1919 msg['Subject'] = 'Test'
1920 msg.preamble = 'MIME message'
1921 msg.epilogue = ''
1922 msg1 = MIMEText('One')
1923 msg2 = MIMEText('Two')
1924 msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY')
1925 msg.attach(msg1)
1926 msg.attach(msg2)
1927 eq(msg.as_string(), """\
1928From: aperson@dom.ain
1929To: bperson@dom.ain
1930Subject: Test
1931Content-Type: multipart/mixed; boundary="BOUNDARY"
1932
1933MIME message
1934--BOUNDARY
1935Content-Type: text/plain; charset="us-ascii"
1936MIME-Version: 1.0
1937Content-Transfer-Encoding: 7bit
1938
1939One
1940--BOUNDARY
1941Content-Type: text/plain; charset="us-ascii"
1942MIME-Version: 1.0
1943Content-Transfer-Encoding: 7bit
1944
1945Two
1946--BOUNDARY--
1947""")
1948
1949 def test_default_type(self):
1950 eq = self.assertEqual
1951 with openfile('msg_30.txt') as fp:
1952 msg = email.message_from_file(fp)
1953 container1 = msg.get_payload(0)
1954 eq(container1.get_default_type(), 'message/rfc822')
1955 eq(container1.get_content_type(), 'message/rfc822')
1956 container2 = msg.get_payload(1)
1957 eq(container2.get_default_type(), 'message/rfc822')
1958 eq(container2.get_content_type(), 'message/rfc822')
1959 container1a = container1.get_payload(0)
1960 eq(container1a.get_default_type(), 'text/plain')
1961 eq(container1a.get_content_type(), 'text/plain')
1962 container2a = container2.get_payload(0)
1963 eq(container2a.get_default_type(), 'text/plain')
1964 eq(container2a.get_content_type(), 'text/plain')
1965
1966 def test_default_type_with_explicit_container_type(self):
1967 eq = self.assertEqual
1968 with openfile('msg_28.txt') as fp:
1969 msg = email.message_from_file(fp)
1970 container1 = msg.get_payload(0)
1971 eq(container1.get_default_type(), 'message/rfc822')
1972 eq(container1.get_content_type(), 'message/rfc822')
1973 container2 = msg.get_payload(1)
1974 eq(container2.get_default_type(), 'message/rfc822')
1975 eq(container2.get_content_type(), 'message/rfc822')
1976 container1a = container1.get_payload(0)
1977 eq(container1a.get_default_type(), 'text/plain')
1978 eq(container1a.get_content_type(), 'text/plain')
1979 container2a = container2.get_payload(0)
1980 eq(container2a.get_default_type(), 'text/plain')
1981 eq(container2a.get_content_type(), 'text/plain')
1982
1983 def test_default_type_non_parsed(self):
1984 eq = self.assertEqual
1985 neq = self.ndiffAssertEqual
1986 # Set up container
1987 container = MIMEMultipart('digest', 'BOUNDARY')
1988 container.epilogue = ''
1989 # Set up subparts
1990 subpart1a = MIMEText('message 1\n')
1991 subpart2a = MIMEText('message 2\n')
1992 subpart1 = MIMEMessage(subpart1a)
1993 subpart2 = MIMEMessage(subpart2a)
1994 container.attach(subpart1)
1995 container.attach(subpart2)
1996 eq(subpart1.get_content_type(), 'message/rfc822')
1997 eq(subpart1.get_default_type(), 'message/rfc822')
1998 eq(subpart2.get_content_type(), 'message/rfc822')
1999 eq(subpart2.get_default_type(), 'message/rfc822')
2000 neq(container.as_string(0), '''\
2001Content-Type: multipart/digest; boundary="BOUNDARY"
2002MIME-Version: 1.0
2003
2004--BOUNDARY
2005Content-Type: message/rfc822
2006MIME-Version: 1.0
2007
2008Content-Type: text/plain; charset="us-ascii"
2009MIME-Version: 1.0
2010Content-Transfer-Encoding: 7bit
2011
2012message 1
2013
2014--BOUNDARY
2015Content-Type: message/rfc822
2016MIME-Version: 1.0
2017
2018Content-Type: text/plain; charset="us-ascii"
2019MIME-Version: 1.0
2020Content-Transfer-Encoding: 7bit
2021
2022message 2
2023
2024--BOUNDARY--
2025''')
2026 del subpart1['content-type']
2027 del subpart1['mime-version']
2028 del subpart2['content-type']
2029 del subpart2['mime-version']
2030 eq(subpart1.get_content_type(), 'message/rfc822')
2031 eq(subpart1.get_default_type(), 'message/rfc822')
2032 eq(subpart2.get_content_type(), 'message/rfc822')
2033 eq(subpart2.get_default_type(), 'message/rfc822')
2034 neq(container.as_string(0), '''\
2035Content-Type: multipart/digest; boundary="BOUNDARY"
2036MIME-Version: 1.0
2037
2038--BOUNDARY
2039
2040Content-Type: text/plain; charset="us-ascii"
2041MIME-Version: 1.0
2042Content-Transfer-Encoding: 7bit
2043
2044message 1
2045
2046--BOUNDARY
2047
2048Content-Type: text/plain; charset="us-ascii"
2049MIME-Version: 1.0
2050Content-Transfer-Encoding: 7bit
2051
2052message 2
2053
2054--BOUNDARY--
2055''')
2056
2057 def test_mime_attachments_in_constructor(self):
2058 eq = self.assertEqual
2059 text1 = MIMEText('')
2060 text2 = MIMEText('')
2061 msg = MIMEMultipart(_subparts=(text1, text2))
2062 eq(len(msg.get_payload()), 2)
2063 eq(msg.get_payload(0), text1)
2064 eq(msg.get_payload(1), text2)
2065
Christian Heimes587c2bf2008-01-19 16:21:02 +00002066 def test_default_multipart_constructor(self):
2067 msg = MIMEMultipart()
2068 self.assertTrue(msg.is_multipart())
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002069
Ezio Melottib3aedd42010-11-20 19:04:17 +00002070
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002071# A general test of parser->model->generator idempotency. IOW, read a message
2072# in, parse it into a message object tree, then without touching the tree,
2073# regenerate the plain text. The original text and the transformed text
2074# should be identical. Note: that we ignore the Unix-From since that may
2075# contain a changed date.
2076class TestIdempotent(TestEmailBase):
R. David Murray719a4492010-11-21 16:53:48 +00002077
2078 linesep = '\n'
2079
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002080 def _msgobj(self, filename):
2081 with openfile(filename) as fp:
2082 data = fp.read()
2083 msg = email.message_from_string(data)
2084 return msg, data
2085
R. David Murray719a4492010-11-21 16:53:48 +00002086 def _idempotent(self, msg, text, unixfrom=False):
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002087 eq = self.ndiffAssertEqual
2088 s = StringIO()
2089 g = Generator(s, maxheaderlen=0)
R. David Murray719a4492010-11-21 16:53:48 +00002090 g.flatten(msg, unixfrom=unixfrom)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002091 eq(text, s.getvalue())
2092
2093 def test_parse_text_message(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +00002094 eq = self.assertEqual
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002095 msg, text = self._msgobj('msg_01.txt')
2096 eq(msg.get_content_type(), 'text/plain')
2097 eq(msg.get_content_maintype(), 'text')
2098 eq(msg.get_content_subtype(), 'plain')
2099 eq(msg.get_params()[1], ('charset', 'us-ascii'))
2100 eq(msg.get_param('charset'), 'us-ascii')
2101 eq(msg.preamble, None)
2102 eq(msg.epilogue, None)
2103 self._idempotent(msg, text)
2104
2105 def test_parse_untyped_message(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +00002106 eq = self.assertEqual
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002107 msg, text = self._msgobj('msg_03.txt')
2108 eq(msg.get_content_type(), 'text/plain')
2109 eq(msg.get_params(), None)
2110 eq(msg.get_param('charset'), None)
2111 self._idempotent(msg, text)
2112
2113 def test_simple_multipart(self):
2114 msg, text = self._msgobj('msg_04.txt')
2115 self._idempotent(msg, text)
2116
2117 def test_MIME_digest(self):
2118 msg, text = self._msgobj('msg_02.txt')
2119 self._idempotent(msg, text)
2120
2121 def test_long_header(self):
2122 msg, text = self._msgobj('msg_27.txt')
2123 self._idempotent(msg, text)
2124
2125 def test_MIME_digest_with_part_headers(self):
2126 msg, text = self._msgobj('msg_28.txt')
2127 self._idempotent(msg, text)
2128
2129 def test_mixed_with_image(self):
2130 msg, text = self._msgobj('msg_06.txt')
2131 self._idempotent(msg, text)
2132
2133 def test_multipart_report(self):
2134 msg, text = self._msgobj('msg_05.txt')
2135 self._idempotent(msg, text)
2136
2137 def test_dsn(self):
2138 msg, text = self._msgobj('msg_16.txt')
2139 self._idempotent(msg, text)
2140
2141 def test_preamble_epilogue(self):
2142 msg, text = self._msgobj('msg_21.txt')
2143 self._idempotent(msg, text)
2144
2145 def test_multipart_one_part(self):
2146 msg, text = self._msgobj('msg_23.txt')
2147 self._idempotent(msg, text)
2148
2149 def test_multipart_no_parts(self):
2150 msg, text = self._msgobj('msg_24.txt')
2151 self._idempotent(msg, text)
2152
2153 def test_no_start_boundary(self):
2154 msg, text = self._msgobj('msg_31.txt')
2155 self._idempotent(msg, text)
2156
2157 def test_rfc2231_charset(self):
2158 msg, text = self._msgobj('msg_32.txt')
2159 self._idempotent(msg, text)
2160
2161 def test_more_rfc2231_parameters(self):
2162 msg, text = self._msgobj('msg_33.txt')
2163 self._idempotent(msg, text)
2164
2165 def test_text_plain_in_a_multipart_digest(self):
2166 msg, text = self._msgobj('msg_34.txt')
2167 self._idempotent(msg, text)
2168
2169 def test_nested_multipart_mixeds(self):
2170 msg, text = self._msgobj('msg_12a.txt')
2171 self._idempotent(msg, text)
2172
2173 def test_message_external_body_idempotent(self):
2174 msg, text = self._msgobj('msg_36.txt')
2175 self._idempotent(msg, text)
2176
R. David Murray719a4492010-11-21 16:53:48 +00002177 def test_message_delivery_status(self):
2178 msg, text = self._msgobj('msg_43.txt')
2179 self._idempotent(msg, text, unixfrom=True)
2180
R. David Murray96fd54e2010-10-08 15:55:28 +00002181 def test_message_signed_idempotent(self):
2182 msg, text = self._msgobj('msg_45.txt')
2183 self._idempotent(msg, text)
2184
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002185 def test_content_type(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +00002186 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002187 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002188 # Get a message object and reset the seek pointer for other tests
2189 msg, text = self._msgobj('msg_05.txt')
2190 eq(msg.get_content_type(), 'multipart/report')
2191 # Test the Content-Type: parameters
2192 params = {}
2193 for pk, pv in msg.get_params():
2194 params[pk] = pv
2195 eq(params['report-type'], 'delivery-status')
2196 eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com')
R. David Murray719a4492010-11-21 16:53:48 +00002197 eq(msg.preamble, 'This is a MIME-encapsulated message.' + self.linesep)
2198 eq(msg.epilogue, self.linesep)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002199 eq(len(msg.get_payload()), 3)
2200 # Make sure the subparts are what we expect
2201 msg1 = msg.get_payload(0)
2202 eq(msg1.get_content_type(), 'text/plain')
R. David Murray719a4492010-11-21 16:53:48 +00002203 eq(msg1.get_payload(), 'Yadda yadda yadda' + self.linesep)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002204 msg2 = msg.get_payload(1)
2205 eq(msg2.get_content_type(), 'text/plain')
R. David Murray719a4492010-11-21 16:53:48 +00002206 eq(msg2.get_payload(), 'Yadda yadda yadda' + self.linesep)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002207 msg3 = msg.get_payload(2)
2208 eq(msg3.get_content_type(), 'message/rfc822')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002209 self.assertTrue(isinstance(msg3, Message))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002210 payload = msg3.get_payload()
2211 unless(isinstance(payload, list))
2212 eq(len(payload), 1)
2213 msg4 = payload[0]
2214 unless(isinstance(msg4, Message))
R. David Murray719a4492010-11-21 16:53:48 +00002215 eq(msg4.get_payload(), 'Yadda yadda yadda' + self.linesep)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002216
2217 def test_parser(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +00002218 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002219 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002220 msg, text = self._msgobj('msg_06.txt')
2221 # Check some of the outer headers
2222 eq(msg.get_content_type(), 'message/rfc822')
2223 # Make sure the payload is a list of exactly one sub-Message, and that
2224 # that submessage has a type of text/plain
2225 payload = msg.get_payload()
2226 unless(isinstance(payload, list))
2227 eq(len(payload), 1)
2228 msg1 = payload[0]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002229 self.assertTrue(isinstance(msg1, Message))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002230 eq(msg1.get_content_type(), 'text/plain')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002231 self.assertTrue(isinstance(msg1.get_payload(), str))
R. David Murray719a4492010-11-21 16:53:48 +00002232 eq(msg1.get_payload(), self.linesep)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002233
2234
Ezio Melottib3aedd42010-11-20 19:04:17 +00002235
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002236# Test various other bits of the package's functionality
2237class TestMiscellaneous(TestEmailBase):
2238 def test_message_from_string(self):
2239 with openfile('msg_01.txt') as fp:
2240 text = fp.read()
2241 msg = email.message_from_string(text)
2242 s = StringIO()
2243 # Don't wrap/continue long headers since we're trying to test
2244 # idempotency.
2245 g = Generator(s, maxheaderlen=0)
2246 g.flatten(msg)
2247 self.assertEqual(text, s.getvalue())
2248
2249 def test_message_from_file(self):
2250 with openfile('msg_01.txt') as fp:
2251 text = fp.read()
2252 fp.seek(0)
2253 msg = email.message_from_file(fp)
2254 s = StringIO()
2255 # Don't wrap/continue long headers since we're trying to test
2256 # idempotency.
2257 g = Generator(s, maxheaderlen=0)
2258 g.flatten(msg)
2259 self.assertEqual(text, s.getvalue())
2260
2261 def test_message_from_string_with_class(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002262 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002263 with openfile('msg_01.txt') as fp:
2264 text = fp.read()
2265
2266 # Create a subclass
2267 class MyMessage(Message):
2268 pass
2269
2270 msg = email.message_from_string(text, MyMessage)
2271 unless(isinstance(msg, MyMessage))
2272 # Try something more complicated
2273 with openfile('msg_02.txt') as fp:
2274 text = fp.read()
2275 msg = email.message_from_string(text, MyMessage)
2276 for subpart in msg.walk():
2277 unless(isinstance(subpart, MyMessage))
2278
2279 def test_message_from_file_with_class(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002280 unless = self.assertTrue
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002281 # Create a subclass
2282 class MyMessage(Message):
2283 pass
2284
2285 with openfile('msg_01.txt') as fp:
2286 msg = email.message_from_file(fp, MyMessage)
2287 unless(isinstance(msg, MyMessage))
2288 # Try something more complicated
2289 with openfile('msg_02.txt') as fp:
2290 msg = email.message_from_file(fp, MyMessage)
2291 for subpart in msg.walk():
2292 unless(isinstance(subpart, MyMessage))
2293
2294 def test__all__(self):
2295 module = __import__('email')
2296 # Can't use sorted() here due to Python 2.3 compatibility
2297 all = module.__all__[:]
2298 all.sort()
2299 self.assertEqual(all, [
2300 'base64mime', 'charset', 'encoders', 'errors', 'generator',
R. David Murray96fd54e2010-10-08 15:55:28 +00002301 'header', 'iterators', 'message', 'message_from_binary_file',
2302 'message_from_bytes', 'message_from_file',
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002303 'message_from_string', 'mime', 'parser',
2304 'quoprimime', 'utils',
2305 ])
2306
2307 def test_formatdate(self):
2308 now = time.time()
2309 self.assertEqual(utils.parsedate(utils.formatdate(now))[:6],
2310 time.gmtime(now)[:6])
2311
2312 def test_formatdate_localtime(self):
2313 now = time.time()
2314 self.assertEqual(
2315 utils.parsedate(utils.formatdate(now, localtime=True))[:6],
2316 time.localtime(now)[:6])
2317
2318 def test_formatdate_usegmt(self):
2319 now = time.time()
2320 self.assertEqual(
2321 utils.formatdate(now, localtime=False),
2322 time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
2323 self.assertEqual(
2324 utils.formatdate(now, localtime=False, usegmt=True),
2325 time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
2326
2327 def test_parsedate_none(self):
2328 self.assertEqual(utils.parsedate(''), None)
2329
2330 def test_parsedate_compact(self):
2331 # The FWS after the comma is optional
2332 self.assertEqual(utils.parsedate('Wed,3 Apr 2002 14:58:26 +0800'),
2333 utils.parsedate('Wed, 3 Apr 2002 14:58:26 +0800'))
2334
2335 def test_parsedate_no_dayofweek(self):
2336 eq = self.assertEqual
2337 eq(utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'),
2338 (2003, 2, 25, 13, 47, 26, 0, 1, -1, -28800))
2339
2340 def test_parsedate_compact_no_dayofweek(self):
2341 eq = self.assertEqual
2342 eq(utils.parsedate_tz('5 Feb 2003 13:47:26 -0800'),
2343 (2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800))
2344
R. David Murray4a62e892010-12-23 20:35:46 +00002345 def test_parsedate_no_space_before_positive_offset(self):
2346 self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58:26+0800'),
2347 (2002, 4, 3, 14, 58, 26, 0, 1, -1, 28800))
2348
2349 def test_parsedate_no_space_before_negative_offset(self):
2350 # Issue 1155362: we already handled '+' for this case.
2351 self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58:26-0800'),
2352 (2002, 4, 3, 14, 58, 26, 0, 1, -1, -28800))
2353
2354
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002355 def test_parsedate_acceptable_to_time_functions(self):
2356 eq = self.assertEqual
2357 timetup = utils.parsedate('5 Feb 2003 13:47:26 -0800')
2358 t = int(time.mktime(timetup))
2359 eq(time.localtime(t)[:6], timetup[:6])
2360 eq(int(time.strftime('%Y', timetup)), 2003)
2361 timetup = utils.parsedate_tz('5 Feb 2003 13:47:26 -0800')
2362 t = int(time.mktime(timetup[:9]))
2363 eq(time.localtime(t)[:6], timetup[:6])
2364 eq(int(time.strftime('%Y', timetup[:9])), 2003)
2365
R. David Murray219d1c82010-08-25 00:45:55 +00002366 def test_parsedate_y2k(self):
2367 """Test for parsing a date with a two-digit year.
2368
2369 Parsing a date with a two-digit year should return the correct
2370 four-digit year. RFC822 allows two-digit years, but RFC2822 (which
2371 obsoletes RFC822) requires four-digit years.
2372
2373 """
2374 self.assertEqual(utils.parsedate_tz('25 Feb 03 13:47:26 -0800'),
2375 utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'))
2376 self.assertEqual(utils.parsedate_tz('25 Feb 71 13:47:26 -0800'),
2377 utils.parsedate_tz('25 Feb 1971 13:47:26 -0800'))
2378
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002379 def test_parseaddr_empty(self):
2380 self.assertEqual(utils.parseaddr('<>'), ('', ''))
2381 self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '')
2382
2383 def test_noquote_dump(self):
2384 self.assertEqual(
2385 utils.formataddr(('A Silly Person', 'person@dom.ain')),
2386 'A Silly Person <person@dom.ain>')
2387
2388 def test_escape_dump(self):
2389 self.assertEqual(
2390 utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
2391 r'"A \(Very\) Silly Person" <person@dom.ain>')
2392 a = r'A \(Special\) Person'
2393 b = 'person@dom.ain'
2394 self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b))
2395
2396 def test_escape_backslashes(self):
2397 self.assertEqual(
2398 utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
2399 r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
2400 a = r'Arthur \Backslash\ Foobar'
2401 b = 'person@dom.ain'
2402 self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b))
2403
2404 def test_name_with_dot(self):
2405 x = 'John X. Doe <jxd@example.com>'
2406 y = '"John X. Doe" <jxd@example.com>'
2407 a, b = ('John X. Doe', 'jxd@example.com')
2408 self.assertEqual(utils.parseaddr(x), (a, b))
2409 self.assertEqual(utils.parseaddr(y), (a, b))
2410 # formataddr() quotes the name if there's a dot in it
2411 self.assertEqual(utils.formataddr((a, b)), y)
2412
R. David Murray5397e862010-10-02 15:58:26 +00002413 def test_parseaddr_preserves_quoted_pairs_in_addresses(self):
2414 # issue 10005. Note that in the third test the second pair of
2415 # backslashes is not actually a quoted pair because it is not inside a
2416 # comment or quoted string: the address being parsed has a quoted
2417 # string containing a quoted backslash, followed by 'example' and two
2418 # backslashes, followed by another quoted string containing a space and
2419 # the word 'example'. parseaddr copies those two backslashes
2420 # literally. Per rfc5322 this is not technically correct since a \ may
2421 # not appear in an address outside of a quoted string. It is probably
2422 # a sensible Postel interpretation, though.
2423 eq = self.assertEqual
2424 eq(utils.parseaddr('""example" example"@example.com'),
2425 ('', '""example" example"@example.com'))
2426 eq(utils.parseaddr('"\\"example\\" example"@example.com'),
2427 ('', '"\\"example\\" example"@example.com'))
2428 eq(utils.parseaddr('"\\\\"example\\\\" example"@example.com'),
2429 ('', '"\\\\"example\\\\" example"@example.com'))
2430
R. David Murray63563cd2010-12-18 18:25:38 +00002431 def test_parseaddr_preserves_spaces_in_local_part(self):
2432 # issue 9286. A normal RFC5322 local part should not contain any
2433 # folding white space, but legacy local parts can (they are a sequence
2434 # of atoms, not dotatoms). On the other hand we strip whitespace from
2435 # before the @ and around dots, on the assumption that the whitespace
2436 # around the punctuation is a mistake in what would otherwise be
2437 # an RFC5322 local part. Leading whitespace is, usual, stripped as well.
2438 self.assertEqual(('', "merwok wok@xample.com"),
2439 utils.parseaddr("merwok wok@xample.com"))
2440 self.assertEqual(('', "merwok wok@xample.com"),
2441 utils.parseaddr("merwok wok@xample.com"))
2442 self.assertEqual(('', "merwok wok@xample.com"),
2443 utils.parseaddr(" merwok wok @xample.com"))
2444 self.assertEqual(('', 'merwok"wok" wok@xample.com'),
2445 utils.parseaddr('merwok"wok" wok@xample.com'))
2446 self.assertEqual(('', 'merwok.wok.wok@xample.com'),
2447 utils.parseaddr('merwok. wok . wok@xample.com'))
2448
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002449 def test_multiline_from_comment(self):
2450 x = """\
2451Foo
2452\tBar <foo@example.com>"""
2453 self.assertEqual(utils.parseaddr(x), ('Foo Bar', 'foo@example.com'))
2454
2455 def test_quote_dump(self):
2456 self.assertEqual(
2457 utils.formataddr(('A Silly; Person', 'person@dom.ain')),
2458 r'"A Silly; Person" <person@dom.ain>')
2459
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002460 def test_charset_richcomparisons(self):
2461 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002462 ne = self.assertNotEqual
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002463 cset1 = Charset()
2464 cset2 = Charset()
2465 eq(cset1, 'us-ascii')
2466 eq(cset1, 'US-ASCII')
2467 eq(cset1, 'Us-AsCiI')
2468 eq('us-ascii', cset1)
2469 eq('US-ASCII', cset1)
2470 eq('Us-AsCiI', cset1)
2471 ne(cset1, 'usascii')
2472 ne(cset1, 'USASCII')
2473 ne(cset1, 'UsAsCiI')
2474 ne('usascii', cset1)
2475 ne('USASCII', cset1)
2476 ne('UsAsCiI', cset1)
2477 eq(cset1, cset2)
2478 eq(cset2, cset1)
2479
2480 def test_getaddresses(self):
2481 eq = self.assertEqual
2482 eq(utils.getaddresses(['aperson@dom.ain (Al Person)',
2483 'Bud Person <bperson@dom.ain>']),
2484 [('Al Person', 'aperson@dom.ain'),
2485 ('Bud Person', 'bperson@dom.ain')])
2486
2487 def test_getaddresses_nasty(self):
2488 eq = self.assertEqual
2489 eq(utils.getaddresses(['foo: ;']), [('', '')])
2490 eq(utils.getaddresses(
2491 ['[]*-- =~$']),
2492 [('', ''), ('', ''), ('', '*--')])
2493 eq(utils.getaddresses(
2494 ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
2495 [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')])
2496
2497 def test_getaddresses_embedded_comment(self):
2498 """Test proper handling of a nested comment"""
2499 eq = self.assertEqual
2500 addrs = utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
2501 eq(addrs[0][1], 'foo@bar.com')
2502
2503 def test_utils_quote_unquote(self):
2504 eq = self.assertEqual
2505 msg = Message()
2506 msg.add_header('content-disposition', 'attachment',
2507 filename='foo\\wacky"name')
2508 eq(msg.get_filename(), 'foo\\wacky"name')
2509
2510 def test_get_body_encoding_with_bogus_charset(self):
2511 charset = Charset('not a charset')
2512 self.assertEqual(charset.get_body_encoding(), 'base64')
2513
2514 def test_get_body_encoding_with_uppercase_charset(self):
2515 eq = self.assertEqual
2516 msg = Message()
2517 msg['Content-Type'] = 'text/plain; charset=UTF-8'
2518 eq(msg['content-type'], 'text/plain; charset=UTF-8')
2519 charsets = msg.get_charsets()
2520 eq(len(charsets), 1)
2521 eq(charsets[0], 'utf-8')
2522 charset = Charset(charsets[0])
2523 eq(charset.get_body_encoding(), 'base64')
Martin v. Löwis15b16a32008-12-02 06:00:15 +00002524 msg.set_payload(b'hello world', charset=charset)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002525 eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n')
2526 eq(msg.get_payload(decode=True), b'hello world')
2527 eq(msg['content-transfer-encoding'], 'base64')
2528 # Try another one
2529 msg = Message()
2530 msg['Content-Type'] = 'text/plain; charset="US-ASCII"'
2531 charsets = msg.get_charsets()
2532 eq(len(charsets), 1)
2533 eq(charsets[0], 'us-ascii')
2534 charset = Charset(charsets[0])
2535 eq(charset.get_body_encoding(), encoders.encode_7or8bit)
2536 msg.set_payload('hello world', charset=charset)
2537 eq(msg.get_payload(), 'hello world')
2538 eq(msg['content-transfer-encoding'], '7bit')
2539
2540 def test_charsets_case_insensitive(self):
2541 lc = Charset('us-ascii')
2542 uc = Charset('US-ASCII')
2543 self.assertEqual(lc.get_body_encoding(), uc.get_body_encoding())
2544
2545 def test_partial_falls_inside_message_delivery_status(self):
2546 eq = self.ndiffAssertEqual
2547 # The Parser interface provides chunks of data to FeedParser in 8192
2548 # byte gulps. SF bug #1076485 found one of those chunks inside
2549 # message/delivery-status header block, which triggered an
2550 # unreadline() of NeedMoreData.
2551 msg = self._msgobj('msg_43.txt')
2552 sfp = StringIO()
2553 iterators._structure(msg, sfp)
2554 eq(sfp.getvalue(), """\
2555multipart/report
2556 text/plain
2557 message/delivery-status
2558 text/plain
2559 text/plain
2560 text/plain
2561 text/plain
2562 text/plain
2563 text/plain
2564 text/plain
2565 text/plain
2566 text/plain
2567 text/plain
2568 text/plain
2569 text/plain
2570 text/plain
2571 text/plain
2572 text/plain
2573 text/plain
2574 text/plain
2575 text/plain
2576 text/plain
2577 text/plain
2578 text/plain
2579 text/plain
2580 text/plain
2581 text/plain
2582 text/plain
2583 text/plain
2584 text/rfc822-headers
2585""")
2586
R. David Murraya0b44b52010-12-02 21:47:19 +00002587 def test_make_msgid_domain(self):
2588 self.assertEqual(
2589 email.utils.make_msgid(domain='testdomain-string')[-19:],
2590 '@testdomain-string>')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002591
Ezio Melottib3aedd42010-11-20 19:04:17 +00002592
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002593# Test the iterator/generators
2594class TestIterators(TestEmailBase):
2595 def test_body_line_iterator(self):
2596 eq = self.assertEqual
2597 neq = self.ndiffAssertEqual
2598 # First a simple non-multipart message
2599 msg = self._msgobj('msg_01.txt')
2600 it = iterators.body_line_iterator(msg)
2601 lines = list(it)
2602 eq(len(lines), 6)
2603 neq(EMPTYSTRING.join(lines), msg.get_payload())
2604 # Now a more complicated multipart
2605 msg = self._msgobj('msg_02.txt')
2606 it = iterators.body_line_iterator(msg)
2607 lines = list(it)
2608 eq(len(lines), 43)
2609 with openfile('msg_19.txt') as fp:
2610 neq(EMPTYSTRING.join(lines), fp.read())
2611
2612 def test_typed_subpart_iterator(self):
2613 eq = self.assertEqual
2614 msg = self._msgobj('msg_04.txt')
2615 it = iterators.typed_subpart_iterator(msg, 'text')
2616 lines = []
2617 subparts = 0
2618 for subpart in it:
2619 subparts += 1
2620 lines.append(subpart.get_payload())
2621 eq(subparts, 2)
2622 eq(EMPTYSTRING.join(lines), """\
2623a simple kind of mirror
2624to reflect upon our own
2625a simple kind of mirror
2626to reflect upon our own
2627""")
2628
2629 def test_typed_subpart_iterator_default_type(self):
2630 eq = self.assertEqual
2631 msg = self._msgobj('msg_03.txt')
2632 it = iterators.typed_subpart_iterator(msg, 'text', 'plain')
2633 lines = []
2634 subparts = 0
2635 for subpart in it:
2636 subparts += 1
2637 lines.append(subpart.get_payload())
2638 eq(subparts, 1)
2639 eq(EMPTYSTRING.join(lines), """\
2640
2641Hi,
2642
2643Do you like this message?
2644
2645-Me
2646""")
2647
R. David Murray45bf773f2010-07-17 01:19:57 +00002648 def test_pushCR_LF(self):
2649 '''FeedParser BufferedSubFile.push() assumed it received complete
2650 line endings. A CR ending one push() followed by a LF starting
2651 the next push() added an empty line.
2652 '''
2653 imt = [
2654 ("a\r \n", 2),
2655 ("b", 0),
2656 ("c\n", 1),
2657 ("", 0),
2658 ("d\r\n", 1),
2659 ("e\r", 0),
2660 ("\nf", 1),
2661 ("\r\n", 1),
2662 ]
2663 from email.feedparser import BufferedSubFile, NeedMoreData
2664 bsf = BufferedSubFile()
2665 om = []
2666 nt = 0
2667 for il, n in imt:
2668 bsf.push(il)
2669 nt += n
2670 n1 = 0
2671 while True:
2672 ol = bsf.readline()
2673 if ol == NeedMoreData:
2674 break
2675 om.append(ol)
2676 n1 += 1
2677 self.assertTrue(n == n1)
2678 self.assertTrue(len(om) == nt)
2679 self.assertTrue(''.join([il for il, n in imt]) == ''.join(om))
2680
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002681
Ezio Melottib3aedd42010-11-20 19:04:17 +00002682
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002683class TestParsers(TestEmailBase):
2684 def test_header_parser(self):
2685 eq = self.assertEqual
2686 # Parse only the headers of a complex multipart MIME document
2687 with openfile('msg_02.txt') as fp:
2688 msg = HeaderParser().parse(fp)
2689 eq(msg['from'], 'ppp-request@zzz.org')
2690 eq(msg['to'], 'ppp@zzz.org')
2691 eq(msg.get_content_type(), 'multipart/mixed')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002692 self.assertFalse(msg.is_multipart())
2693 self.assertTrue(isinstance(msg.get_payload(), str))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002694
2695 def test_whitespace_continuation(self):
2696 eq = self.assertEqual
2697 # This message contains a line after the Subject: header that has only
2698 # whitespace, but it is not empty!
2699 msg = email.message_from_string("""\
2700From: aperson@dom.ain
2701To: bperson@dom.ain
2702Subject: the next line has a space on it
2703\x20
2704Date: Mon, 8 Apr 2002 15:09:19 -0400
2705Message-ID: spam
2706
2707Here's the message body
2708""")
2709 eq(msg['subject'], 'the next line has a space on it\n ')
2710 eq(msg['message-id'], 'spam')
2711 eq(msg.get_payload(), "Here's the message body\n")
2712
2713 def test_whitespace_continuation_last_header(self):
2714 eq = self.assertEqual
2715 # Like the previous test, but the subject line is the last
2716 # header.
2717 msg = email.message_from_string("""\
2718From: aperson@dom.ain
2719To: bperson@dom.ain
2720Date: Mon, 8 Apr 2002 15:09:19 -0400
2721Message-ID: spam
2722Subject: the next line has a space on it
2723\x20
2724
2725Here's the message body
2726""")
2727 eq(msg['subject'], 'the next line has a space on it\n ')
2728 eq(msg['message-id'], 'spam')
2729 eq(msg.get_payload(), "Here's the message body\n")
2730
2731 def test_crlf_separation(self):
2732 eq = self.assertEqual
Guido van Rossum98297ee2007-11-06 21:34:58 +00002733 with openfile('msg_26.txt', newline='\n') as fp:
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002734 msg = Parser().parse(fp)
2735 eq(len(msg.get_payload()), 2)
2736 part1 = msg.get_payload(0)
2737 eq(part1.get_content_type(), 'text/plain')
2738 eq(part1.get_payload(), 'Simple email with attachment.\r\n\r\n')
2739 part2 = msg.get_payload(1)
2740 eq(part2.get_content_type(), 'application/riscos')
2741
R. David Murray8451c4b2010-10-23 22:19:56 +00002742 def test_crlf_flatten(self):
2743 # Using newline='\n' preserves the crlfs in this input file.
2744 with openfile('msg_26.txt', newline='\n') as fp:
2745 text = fp.read()
2746 msg = email.message_from_string(text)
2747 s = StringIO()
2748 g = Generator(s)
2749 g.flatten(msg, linesep='\r\n')
2750 self.assertEqual(s.getvalue(), text)
2751
2752 maxDiff = None
2753
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002754 def test_multipart_digest_with_extra_mime_headers(self):
2755 eq = self.assertEqual
2756 neq = self.ndiffAssertEqual
2757 with openfile('msg_28.txt') as fp:
2758 msg = email.message_from_file(fp)
2759 # Structure is:
2760 # multipart/digest
2761 # message/rfc822
2762 # text/plain
2763 # message/rfc822
2764 # text/plain
2765 eq(msg.is_multipart(), 1)
2766 eq(len(msg.get_payload()), 2)
2767 part1 = msg.get_payload(0)
2768 eq(part1.get_content_type(), 'message/rfc822')
2769 eq(part1.is_multipart(), 1)
2770 eq(len(part1.get_payload()), 1)
2771 part1a = part1.get_payload(0)
2772 eq(part1a.is_multipart(), 0)
2773 eq(part1a.get_content_type(), 'text/plain')
2774 neq(part1a.get_payload(), 'message 1\n')
2775 # next message/rfc822
2776 part2 = msg.get_payload(1)
2777 eq(part2.get_content_type(), 'message/rfc822')
2778 eq(part2.is_multipart(), 1)
2779 eq(len(part2.get_payload()), 1)
2780 part2a = part2.get_payload(0)
2781 eq(part2a.is_multipart(), 0)
2782 eq(part2a.get_content_type(), 'text/plain')
2783 neq(part2a.get_payload(), 'message 2\n')
2784
2785 def test_three_lines(self):
2786 # A bug report by Andrew McNamara
2787 lines = ['From: Andrew Person <aperson@dom.ain',
2788 'Subject: Test',
2789 'Date: Tue, 20 Aug 2002 16:43:45 +1000']
2790 msg = email.message_from_string(NL.join(lines))
2791 self.assertEqual(msg['date'], 'Tue, 20 Aug 2002 16:43:45 +1000')
2792
2793 def test_strip_line_feed_and_carriage_return_in_headers(self):
2794 eq = self.assertEqual
2795 # For [ 1002475 ] email message parser doesn't handle \r\n correctly
2796 value1 = 'text'
2797 value2 = 'more text'
2798 m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % (
2799 value1, value2)
2800 msg = email.message_from_string(m)
2801 eq(msg.get('Header'), value1)
2802 eq(msg.get('Next-Header'), value2)
2803
2804 def test_rfc2822_header_syntax(self):
2805 eq = self.assertEqual
2806 m = '>From: foo\nFrom: bar\n!"#QUX;~: zoo\n\nbody'
2807 msg = email.message_from_string(m)
2808 eq(len(msg), 3)
2809 eq(sorted(field for field in msg), ['!"#QUX;~', '>From', 'From'])
2810 eq(msg.get_payload(), 'body')
2811
2812 def test_rfc2822_space_not_allowed_in_header(self):
2813 eq = self.assertEqual
2814 m = '>From foo@example.com 11:25:53\nFrom: bar\n!"#QUX;~: zoo\n\nbody'
2815 msg = email.message_from_string(m)
2816 eq(len(msg.keys()), 0)
2817
2818 def test_rfc2822_one_character_header(self):
2819 eq = self.assertEqual
2820 m = 'A: first header\nB: second header\nCC: third header\n\nbody'
2821 msg = email.message_from_string(m)
2822 headers = msg.keys()
2823 headers.sort()
2824 eq(headers, ['A', 'B', 'CC'])
2825 eq(msg.get_payload(), 'body')
2826
R. David Murray45e0e142010-06-16 02:19:40 +00002827 def test_CRLFLF_at_end_of_part(self):
2828 # issue 5610: feedparser should not eat two chars from body part ending
2829 # with "\r\n\n".
2830 m = (
2831 "From: foo@bar.com\n"
2832 "To: baz\n"
2833 "Mime-Version: 1.0\n"
2834 "Content-Type: multipart/mixed; boundary=BOUNDARY\n"
2835 "\n"
2836 "--BOUNDARY\n"
2837 "Content-Type: text/plain\n"
2838 "\n"
2839 "body ending with CRLF newline\r\n"
2840 "\n"
2841 "--BOUNDARY--\n"
2842 )
2843 msg = email.message_from_string(m)
2844 self.assertTrue(msg.get_payload(0).get_payload().endswith('\r\n'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002845
Ezio Melottib3aedd42010-11-20 19:04:17 +00002846
R. David Murray96fd54e2010-10-08 15:55:28 +00002847class Test8BitBytesHandling(unittest.TestCase):
2848 # In Python3 all input is string, but that doesn't work if the actual input
2849 # uses an 8bit transfer encoding. To hack around that, in email 5.1 we
2850 # decode byte streams using the surrogateescape error handler, and
2851 # reconvert to binary at appropriate places if we detect surrogates. This
2852 # doesn't allow us to transform headers with 8bit bytes (they get munged),
2853 # but it does allow us to parse and preserve them, and to decode body
2854 # parts that use an 8bit CTE.
2855
2856 bodytest_msg = textwrap.dedent("""\
2857 From: foo@bar.com
2858 To: baz
2859 Mime-Version: 1.0
2860 Content-Type: text/plain; charset={charset}
2861 Content-Transfer-Encoding: {cte}
2862
2863 {bodyline}
2864 """)
2865
2866 def test_known_8bit_CTE(self):
2867 m = self.bodytest_msg.format(charset='utf-8',
2868 cte='8bit',
2869 bodyline='pöstal').encode('utf-8')
2870 msg = email.message_from_bytes(m)
2871 self.assertEqual(msg.get_payload(), "pöstal\n")
2872 self.assertEqual(msg.get_payload(decode=True),
2873 "pöstal\n".encode('utf-8'))
2874
2875 def test_unknown_8bit_CTE(self):
2876 m = self.bodytest_msg.format(charset='notavalidcharset',
2877 cte='8bit',
2878 bodyline='pöstal').encode('utf-8')
2879 msg = email.message_from_bytes(m)
R. David Murray92532142011-01-07 23:25:30 +00002880 self.assertEqual(msg.get_payload(), "p\uFFFD\uFFFDstal\n")
R. David Murray96fd54e2010-10-08 15:55:28 +00002881 self.assertEqual(msg.get_payload(decode=True),
2882 "pöstal\n".encode('utf-8'))
2883
2884 def test_8bit_in_quopri_body(self):
2885 # This is non-RFC compliant data...without 'decode' the library code
2886 # decodes the body using the charset from the headers, and because the
2887 # source byte really is utf-8 this works. This is likely to fail
2888 # against real dirty data (ie: produce mojibake), but the data is
2889 # invalid anyway so it is as good a guess as any. But this means that
2890 # this test just confirms the current behavior; that behavior is not
2891 # necessarily the best possible behavior. With 'decode' it is
2892 # returning the raw bytes, so that test should be of correct behavior,
2893 # or at least produce the same result that email4 did.
2894 m = self.bodytest_msg.format(charset='utf-8',
2895 cte='quoted-printable',
2896 bodyline='p=C3=B6stál').encode('utf-8')
2897 msg = email.message_from_bytes(m)
2898 self.assertEqual(msg.get_payload(), 'p=C3=B6stál\n')
2899 self.assertEqual(msg.get_payload(decode=True),
2900 'pöstál\n'.encode('utf-8'))
2901
2902 def test_invalid_8bit_in_non_8bit_cte_uses_replace(self):
2903 # This is similar to the previous test, but proves that if the 8bit
2904 # byte is undecodeable in the specified charset, it gets replaced
2905 # by the unicode 'unknown' character. Again, this may or may not
2906 # be the ideal behavior. Note that if decode=False none of the
2907 # decoders will get involved, so this is the only test we need
2908 # for this behavior.
2909 m = self.bodytest_msg.format(charset='ascii',
2910 cte='quoted-printable',
2911 bodyline='p=C3=B6stál').encode('utf-8')
2912 msg = email.message_from_bytes(m)
R. David Murray92532142011-01-07 23:25:30 +00002913 self.assertEqual(msg.get_payload(), 'p=C3=B6st\uFFFD\uFFFDl\n')
R. David Murray96fd54e2010-10-08 15:55:28 +00002914 self.assertEqual(msg.get_payload(decode=True),
2915 'pöstál\n'.encode('utf-8'))
2916
2917 def test_8bit_in_base64_body(self):
2918 # Sticking an 8bit byte in a base64 block makes it undecodable by
2919 # normal means, so the block is returned undecoded, but as bytes.
2920 m = self.bodytest_msg.format(charset='utf-8',
2921 cte='base64',
2922 bodyline='cMO2c3RhbAá=').encode('utf-8')
2923 msg = email.message_from_bytes(m)
2924 self.assertEqual(msg.get_payload(decode=True),
2925 'cMO2c3RhbAá=\n'.encode('utf-8'))
2926
2927 def test_8bit_in_uuencode_body(self):
2928 # Sticking an 8bit byte in a uuencode block makes it undecodable by
2929 # normal means, so the block is returned undecoded, but as bytes.
2930 m = self.bodytest_msg.format(charset='utf-8',
2931 cte='uuencode',
2932 bodyline='<,.V<W1A; á ').encode('utf-8')
2933 msg = email.message_from_bytes(m)
2934 self.assertEqual(msg.get_payload(decode=True),
2935 '<,.V<W1A; á \n'.encode('utf-8'))
2936
2937
R. David Murray92532142011-01-07 23:25:30 +00002938 headertest_headers = (
2939 ('From: foo@bar.com', ('From', 'foo@bar.com')),
2940 ('To: báz', ('To', '=?unknown-8bit?q?b=C3=A1z?=')),
2941 ('Subject: Maintenant je vous présente mon collègue, le pouf célèbre\n'
2942 '\tJean de Baddie',
2943 ('Subject', '=?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_'
2944 'coll=C3=A8gue=2C_le_pouf_c=C3=A9l=C3=A8bre?=\n'
2945 ' =?unknown-8bit?q?_Jean_de_Baddie?=')),
2946 ('From: göst', ('From', '=?unknown-8bit?b?Z8O2c3Q=?=')),
2947 )
2948 headertest_msg = ('\n'.join([src for (src, _) in headertest_headers]) +
2949 '\nYes, they are flying.\n').encode('utf-8')
R. David Murray96fd54e2010-10-08 15:55:28 +00002950
2951 def test_get_8bit_header(self):
2952 msg = email.message_from_bytes(self.headertest_msg)
R. David Murray92532142011-01-07 23:25:30 +00002953 self.assertEqual(str(msg.get('to')), 'b\uFFFD\uFFFDz')
2954 self.assertEqual(str(msg['to']), 'b\uFFFD\uFFFDz')
R. David Murray96fd54e2010-10-08 15:55:28 +00002955
2956 def test_print_8bit_headers(self):
2957 msg = email.message_from_bytes(self.headertest_msg)
2958 self.assertEqual(str(msg),
R. David Murray92532142011-01-07 23:25:30 +00002959 textwrap.dedent("""\
2960 From: {}
2961 To: {}
2962 Subject: {}
2963 From: {}
2964
2965 Yes, they are flying.
2966 """).format(*[expected[1] for (_, expected) in
2967 self.headertest_headers]))
R. David Murray96fd54e2010-10-08 15:55:28 +00002968
2969 def test_values_with_8bit_headers(self):
2970 msg = email.message_from_bytes(self.headertest_msg)
R. David Murray92532142011-01-07 23:25:30 +00002971 self.assertListEqual([str(x) for x in msg.values()],
R. David Murray96fd54e2010-10-08 15:55:28 +00002972 ['foo@bar.com',
R. David Murray92532142011-01-07 23:25:30 +00002973 'b\uFFFD\uFFFDz',
2974 'Maintenant je vous pr\uFFFD\uFFFDsente mon '
2975 'coll\uFFFD\uFFFDgue, le pouf '
2976 'c\uFFFD\uFFFDl\uFFFD\uFFFDbre\n'
R. David Murray96fd54e2010-10-08 15:55:28 +00002977 '\tJean de Baddie',
R. David Murray92532142011-01-07 23:25:30 +00002978 "g\uFFFD\uFFFDst"])
R. David Murray96fd54e2010-10-08 15:55:28 +00002979
2980 def test_items_with_8bit_headers(self):
2981 msg = email.message_from_bytes(self.headertest_msg)
R. David Murray92532142011-01-07 23:25:30 +00002982 self.assertListEqual([(str(x), str(y)) for (x, y) in msg.items()],
R. David Murray96fd54e2010-10-08 15:55:28 +00002983 [('From', 'foo@bar.com'),
R. David Murray92532142011-01-07 23:25:30 +00002984 ('To', 'b\uFFFD\uFFFDz'),
2985 ('Subject', 'Maintenant je vous '
2986 'pr\uFFFD\uFFFDsente '
2987 'mon coll\uFFFD\uFFFDgue, le pouf '
2988 'c\uFFFD\uFFFDl\uFFFD\uFFFDbre\n'
2989 '\tJean de Baddie'),
2990 ('From', 'g\uFFFD\uFFFDst')])
R. David Murray96fd54e2010-10-08 15:55:28 +00002991
2992 def test_get_all_with_8bit_headers(self):
2993 msg = email.message_from_bytes(self.headertest_msg)
R. David Murray92532142011-01-07 23:25:30 +00002994 self.assertListEqual([str(x) for x in msg.get_all('from')],
R. David Murray96fd54e2010-10-08 15:55:28 +00002995 ['foo@bar.com',
R. David Murray92532142011-01-07 23:25:30 +00002996 'g\uFFFD\uFFFDst'])
R. David Murray96fd54e2010-10-08 15:55:28 +00002997
R David Murraya2150232011-03-16 21:11:23 -04002998 def test_get_content_type_with_8bit(self):
2999 msg = email.message_from_bytes(textwrap.dedent("""\
3000 Content-Type: text/pl\xA7in; charset=utf-8
3001 """).encode('latin-1'))
3002 self.assertEqual(msg.get_content_type(), "text/pl\uFFFDin")
3003 self.assertEqual(msg.get_content_maintype(), "text")
3004 self.assertEqual(msg.get_content_subtype(), "pl\uFFFDin")
3005
3006 def test_get_params_with_8bit(self):
3007 msg = email.message_from_bytes(
3008 'X-Header: foo=\xa7ne; b\xa7r=two; baz=three\n'.encode('latin-1'))
3009 self.assertEqual(msg.get_params(header='x-header'),
3010 [('foo', '\uFFFDne'), ('b\uFFFDr', 'two'), ('baz', 'three')])
3011 self.assertEqual(msg.get_param('Foo', header='x-header'), '\uFFFdne')
3012 # XXX: someday you might be able to get 'b\xa7r', for now you can't.
3013 self.assertEqual(msg.get_param('b\xa7r', header='x-header'), None)
3014
3015 def test_get_rfc2231_params_with_8bit(self):
3016 msg = email.message_from_bytes(textwrap.dedent("""\
3017 Content-Type: text/plain; charset=us-ascii;
3018 title*=us-ascii'en'This%20is%20not%20f\xa7n"""
3019 ).encode('latin-1'))
3020 self.assertEqual(msg.get_param('title'),
3021 ('us-ascii', 'en', 'This is not f\uFFFDn'))
3022
3023 def test_set_rfc2231_params_with_8bit(self):
3024 msg = email.message_from_bytes(textwrap.dedent("""\
3025 Content-Type: text/plain; charset=us-ascii;
3026 title*=us-ascii'en'This%20is%20not%20f\xa7n"""
3027 ).encode('latin-1'))
3028 msg.set_param('title', 'test')
3029 self.assertEqual(msg.get_param('title'), 'test')
3030
3031 def test_del_rfc2231_params_with_8bit(self):
3032 msg = email.message_from_bytes(textwrap.dedent("""\
3033 Content-Type: text/plain; charset=us-ascii;
3034 title*=us-ascii'en'This%20is%20not%20f\xa7n"""
3035 ).encode('latin-1'))
3036 msg.del_param('title')
3037 self.assertEqual(msg.get_param('title'), None)
3038 self.assertEqual(msg.get_content_maintype(), 'text')
3039
3040 def test_get_payload_with_8bit_cte_header(self):
3041 msg = email.message_from_bytes(textwrap.dedent("""\
3042 Content-Transfer-Encoding: b\xa7se64
3043 Content-Type: text/plain; charset=latin-1
3044
3045 payload
3046 """).encode('latin-1'))
3047 self.assertEqual(msg.get_payload(), 'payload\n')
3048 self.assertEqual(msg.get_payload(decode=True), b'payload\n')
3049
R. David Murray96fd54e2010-10-08 15:55:28 +00003050 non_latin_bin_msg = textwrap.dedent("""\
3051 From: foo@bar.com
3052 To: báz
3053 Subject: Maintenant je vous présente mon collègue, le pouf célèbre
3054 \tJean de Baddie
3055 Mime-Version: 1.0
3056 Content-Type: text/plain; charset="utf-8"
3057 Content-Transfer-Encoding: 8bit
3058
3059 Да, они летят.
3060 """).encode('utf-8')
3061
3062 def test_bytes_generator(self):
3063 msg = email.message_from_bytes(self.non_latin_bin_msg)
3064 out = BytesIO()
3065 email.generator.BytesGenerator(out).flatten(msg)
3066 self.assertEqual(out.getvalue(), self.non_latin_bin_msg)
3067
R. David Murray7372a072011-01-26 21:21:32 +00003068 def test_bytes_generator_handles_None_body(self):
3069 #Issue 11019
3070 msg = email.message.Message()
3071 out = BytesIO()
3072 email.generator.BytesGenerator(out).flatten(msg)
3073 self.assertEqual(out.getvalue(), b"\n")
3074
R. David Murray92532142011-01-07 23:25:30 +00003075 non_latin_bin_msg_as7bit_wrapped = textwrap.dedent("""\
R. David Murray96fd54e2010-10-08 15:55:28 +00003076 From: foo@bar.com
R. David Murray92532142011-01-07 23:25:30 +00003077 To: =?unknown-8bit?q?b=C3=A1z?=
3078 Subject: =?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_coll=C3=A8gue?=
3079 =?unknown-8bit?q?=2C_le_pouf_c=C3=A9l=C3=A8bre?=
3080 =?unknown-8bit?q?_Jean_de_Baddie?=
R. David Murray96fd54e2010-10-08 15:55:28 +00003081 Mime-Version: 1.0
3082 Content-Type: text/plain; charset="utf-8"
3083 Content-Transfer-Encoding: base64
3084
3085 0JTQsCwg0L7QvdC4INC70LXRgtGP0YIuCg==
3086 """)
3087
3088 def test_generator_handles_8bit(self):
3089 msg = email.message_from_bytes(self.non_latin_bin_msg)
3090 out = StringIO()
3091 email.generator.Generator(out).flatten(msg)
R. David Murray92532142011-01-07 23:25:30 +00003092 self.assertEqual(out.getvalue(), self.non_latin_bin_msg_as7bit_wrapped)
R. David Murray96fd54e2010-10-08 15:55:28 +00003093
3094 def test_bytes_generator_with_unix_from(self):
3095 # The unixfrom contains a current date, so we can't check it
3096 # literally. Just make sure the first word is 'From' and the
3097 # rest of the message matches the input.
3098 msg = email.message_from_bytes(self.non_latin_bin_msg)
3099 out = BytesIO()
3100 email.generator.BytesGenerator(out).flatten(msg, unixfrom=True)
3101 lines = out.getvalue().split(b'\n')
3102 self.assertEqual(lines[0].split()[0], b'From')
3103 self.assertEqual(b'\n'.join(lines[1:]), self.non_latin_bin_msg)
3104
R. David Murray92532142011-01-07 23:25:30 +00003105 non_latin_bin_msg_as7bit = non_latin_bin_msg_as7bit_wrapped.split('\n')
3106 non_latin_bin_msg_as7bit[2:4] = [
3107 'Subject: =?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_'
3108 'coll=C3=A8gue=2C_le_pouf_c=C3=A9l=C3=A8bre?=']
3109 non_latin_bin_msg_as7bit = '\n'.join(non_latin_bin_msg_as7bit)
3110
R. David Murray96fd54e2010-10-08 15:55:28 +00003111 def test_message_from_binary_file(self):
3112 fn = 'test.msg'
3113 self.addCleanup(unlink, fn)
3114 with open(fn, 'wb') as testfile:
3115 testfile.write(self.non_latin_bin_msg)
Brett Cannon384917a2010-10-29 23:08:36 +00003116 with open(fn, 'rb') as testfile:
3117 m = email.parser.BytesParser().parse(testfile)
R. David Murray96fd54e2010-10-08 15:55:28 +00003118 self.assertEqual(str(m), self.non_latin_bin_msg_as7bit)
3119
3120 latin_bin_msg = textwrap.dedent("""\
3121 From: foo@bar.com
3122 To: Dinsdale
3123 Subject: Nudge nudge, wink, wink
3124 Mime-Version: 1.0
3125 Content-Type: text/plain; charset="latin-1"
3126 Content-Transfer-Encoding: 8bit
3127
3128 oh là là, know what I mean, know what I mean?
3129 """).encode('latin-1')
3130
3131 latin_bin_msg_as7bit = textwrap.dedent("""\
3132 From: foo@bar.com
3133 To: Dinsdale
3134 Subject: Nudge nudge, wink, wink
3135 Mime-Version: 1.0
3136 Content-Type: text/plain; charset="iso-8859-1"
3137 Content-Transfer-Encoding: quoted-printable
3138
3139 oh l=E0 l=E0, know what I mean, know what I mean?
3140 """)
3141
3142 def test_string_generator_reencodes_to_quopri_when_appropriate(self):
3143 m = email.message_from_bytes(self.latin_bin_msg)
3144 self.assertEqual(str(m), self.latin_bin_msg_as7bit)
3145
3146 def test_decoded_generator_emits_unicode_body(self):
3147 m = email.message_from_bytes(self.latin_bin_msg)
3148 out = StringIO()
3149 email.generator.DecodedGenerator(out).flatten(m)
3150 #DecodedHeader output contains an extra blank line compared
3151 #to the input message. RDM: not sure if this is a bug or not,
3152 #but it is not specific to the 8bit->7bit conversion.
3153 self.assertEqual(out.getvalue(),
3154 self.latin_bin_msg.decode('latin-1')+'\n')
3155
3156 def test_bytes_feedparser(self):
3157 bfp = email.feedparser.BytesFeedParser()
3158 for i in range(0, len(self.latin_bin_msg), 10):
3159 bfp.feed(self.latin_bin_msg[i:i+10])
3160 m = bfp.close()
3161 self.assertEqual(str(m), self.latin_bin_msg_as7bit)
3162
R. David Murray8451c4b2010-10-23 22:19:56 +00003163 def test_crlf_flatten(self):
3164 with openfile('msg_26.txt', 'rb') as fp:
3165 text = fp.read()
3166 msg = email.message_from_bytes(text)
3167 s = BytesIO()
3168 g = email.generator.BytesGenerator(s)
3169 g.flatten(msg, linesep='\r\n')
3170 self.assertEqual(s.getvalue(), text)
3171 maxDiff = None
3172
Ezio Melottib3aedd42010-11-20 19:04:17 +00003173
R. David Murray719a4492010-11-21 16:53:48 +00003174class BaseTestBytesGeneratorIdempotent:
R. David Murray96fd54e2010-10-08 15:55:28 +00003175
R. David Murraye5db2632010-11-20 15:10:13 +00003176 maxDiff = None
3177
R. David Murray96fd54e2010-10-08 15:55:28 +00003178 def _msgobj(self, filename):
3179 with openfile(filename, 'rb') as fp:
3180 data = fp.read()
R. David Murray719a4492010-11-21 16:53:48 +00003181 data = self.normalize_linesep_regex.sub(self.blinesep, data)
R. David Murray96fd54e2010-10-08 15:55:28 +00003182 msg = email.message_from_bytes(data)
3183 return msg, data
3184
R. David Murray719a4492010-11-21 16:53:48 +00003185 def _idempotent(self, msg, data, unixfrom=False):
R. David Murray96fd54e2010-10-08 15:55:28 +00003186 b = BytesIO()
3187 g = email.generator.BytesGenerator(b, maxheaderlen=0)
R. David Murray719a4492010-11-21 16:53:48 +00003188 g.flatten(msg, unixfrom=unixfrom, linesep=self.linesep)
R. David Murraye5db2632010-11-20 15:10:13 +00003189 self.assertByteStringsEqual(data, b.getvalue())
R. David Murray96fd54e2010-10-08 15:55:28 +00003190
R. David Murraye5db2632010-11-20 15:10:13 +00003191 def assertByteStringsEqual(self, str1, str2):
R. David Murray719a4492010-11-21 16:53:48 +00003192 # Not using self.blinesep here is intentional. This way the output
3193 # is more useful when the failure results in mixed line endings.
R. David Murray96fd54e2010-10-08 15:55:28 +00003194 self.assertListEqual(str1.split(b'\n'), str2.split(b'\n'))
3195
3196
R. David Murray719a4492010-11-21 16:53:48 +00003197class TestBytesGeneratorIdempotentNL(BaseTestBytesGeneratorIdempotent,
3198 TestIdempotent):
3199 linesep = '\n'
3200 blinesep = b'\n'
3201 normalize_linesep_regex = re.compile(br'\r\n')
3202
3203
3204class TestBytesGeneratorIdempotentCRLF(BaseTestBytesGeneratorIdempotent,
3205 TestIdempotent):
3206 linesep = '\r\n'
3207 blinesep = b'\r\n'
3208 normalize_linesep_regex = re.compile(br'(?<!\r)\n')
3209
Ezio Melottib3aedd42010-11-20 19:04:17 +00003210
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003211class TestBase64(unittest.TestCase):
3212 def test_len(self):
3213 eq = self.assertEqual
Guido van Rossum9604e662007-08-30 03:46:43 +00003214 eq(base64mime.header_length('hello'),
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003215 len(base64mime.body_encode(b'hello', eol='')))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003216 for size in range(15):
3217 if size == 0 : bsize = 0
3218 elif size <= 3 : bsize = 4
3219 elif size <= 6 : bsize = 8
3220 elif size <= 9 : bsize = 12
3221 elif size <= 12: bsize = 16
3222 else : bsize = 20
Guido van Rossum9604e662007-08-30 03:46:43 +00003223 eq(base64mime.header_length('x' * size), bsize)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003224
3225 def test_decode(self):
3226 eq = self.assertEqual
Barry Warsaw2cc1f6d2007-08-30 14:28:55 +00003227 eq(base64mime.decode(''), b'')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003228 eq(base64mime.decode('aGVsbG8='), b'hello')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003229
3230 def test_encode(self):
3231 eq = self.assertEqual
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003232 eq(base64mime.body_encode(b''), b'')
3233 eq(base64mime.body_encode(b'hello'), 'aGVsbG8=\n')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003234 # Test the binary flag
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003235 eq(base64mime.body_encode(b'hello\n'), 'aGVsbG8K\n')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003236 # Test the maxlinelen arg
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003237 eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40), """\
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003238eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
3239eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
3240eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
3241eHh4eCB4eHh4IA==
3242""")
3243 # Test the eol argument
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003244 eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40, eol='\r\n'),
Barry Warsaw7aa02e62007-08-31 03:26:19 +00003245 """\
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003246eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
3247eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
3248eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
3249eHh4eCB4eHh4IA==\r
3250""")
3251
3252 def test_header_encode(self):
3253 eq = self.assertEqual
3254 he = base64mime.header_encode
3255 eq(he('hello'), '=?iso-8859-1?b?aGVsbG8=?=')
Guido van Rossum9604e662007-08-30 03:46:43 +00003256 eq(he('hello\r\nworld'), '=?iso-8859-1?b?aGVsbG8NCndvcmxk?=')
3257 eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003258 # Test the charset option
3259 eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?b?aGVsbG8=?=')
3260 eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003261
3262
Ezio Melottib3aedd42010-11-20 19:04:17 +00003263
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003264class TestQuopri(unittest.TestCase):
3265 def setUp(self):
3266 # Set of characters (as byte integers) that don't need to be encoded
3267 # in headers.
3268 self.hlit = list(chain(
3269 range(ord('a'), ord('z') + 1),
3270 range(ord('A'), ord('Z') + 1),
3271 range(ord('0'), ord('9') + 1),
Guido van Rossum9604e662007-08-30 03:46:43 +00003272 (c for c in b'!*+-/')))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003273 # Set of characters (as byte integers) that do need to be encoded in
3274 # headers.
3275 self.hnon = [c for c in range(256) if c not in self.hlit]
3276 assert len(self.hlit) + len(self.hnon) == 256
3277 # Set of characters (as byte integers) that don't need to be encoded
3278 # in bodies.
3279 self.blit = list(range(ord(' '), ord('~') + 1))
3280 self.blit.append(ord('\t'))
3281 self.blit.remove(ord('='))
3282 # Set of characters (as byte integers) that do need to be encoded in
3283 # bodies.
3284 self.bnon = [c for c in range(256) if c not in self.blit]
3285 assert len(self.blit) + len(self.bnon) == 256
3286
Guido van Rossum9604e662007-08-30 03:46:43 +00003287 def test_quopri_header_check(self):
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003288 for c in self.hlit:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00003289 self.assertFalse(quoprimime.header_check(c),
Guido van Rossum9604e662007-08-30 03:46:43 +00003290 'Should not be header quopri encoded: %s' % chr(c))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003291 for c in self.hnon:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00003292 self.assertTrue(quoprimime.header_check(c),
Guido van Rossum9604e662007-08-30 03:46:43 +00003293 'Should be header quopri encoded: %s' % chr(c))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003294
Guido van Rossum9604e662007-08-30 03:46:43 +00003295 def test_quopri_body_check(self):
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003296 for c in self.blit:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00003297 self.assertFalse(quoprimime.body_check(c),
Guido van Rossum9604e662007-08-30 03:46:43 +00003298 'Should not be body quopri encoded: %s' % chr(c))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003299 for c in self.bnon:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00003300 self.assertTrue(quoprimime.body_check(c),
Guido van Rossum9604e662007-08-30 03:46:43 +00003301 'Should be body quopri encoded: %s' % chr(c))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003302
3303 def test_header_quopri_len(self):
3304 eq = self.assertEqual
Guido van Rossum9604e662007-08-30 03:46:43 +00003305 eq(quoprimime.header_length(b'hello'), 5)
3306 # RFC 2047 chrome is not included in header_length().
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003307 eq(len(quoprimime.header_encode(b'hello', charset='xxx')),
Guido van Rossum9604e662007-08-30 03:46:43 +00003308 quoprimime.header_length(b'hello') +
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003309 # =?xxx?q?...?= means 10 extra characters
3310 10)
Guido van Rossum9604e662007-08-30 03:46:43 +00003311 eq(quoprimime.header_length(b'h@e@l@l@o@'), 20)
3312 # RFC 2047 chrome is not included in header_length().
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003313 eq(len(quoprimime.header_encode(b'h@e@l@l@o@', charset='xxx')),
Guido van Rossum9604e662007-08-30 03:46:43 +00003314 quoprimime.header_length(b'h@e@l@l@o@') +
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003315 # =?xxx?q?...?= means 10 extra characters
3316 10)
3317 for c in self.hlit:
Guido van Rossum9604e662007-08-30 03:46:43 +00003318 eq(quoprimime.header_length(bytes([c])), 1,
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003319 'expected length 1 for %r' % chr(c))
3320 for c in self.hnon:
Guido van Rossum9604e662007-08-30 03:46:43 +00003321 # Space is special; it's encoded to _
3322 if c == ord(' '):
3323 continue
3324 eq(quoprimime.header_length(bytes([c])), 3,
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003325 'expected length 3 for %r' % chr(c))
Guido van Rossum9604e662007-08-30 03:46:43 +00003326 eq(quoprimime.header_length(b' '), 1)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003327
3328 def test_body_quopri_len(self):
3329 eq = self.assertEqual
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003330 for c in self.blit:
Guido van Rossum9604e662007-08-30 03:46:43 +00003331 eq(quoprimime.body_length(bytes([c])), 1)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003332 for c in self.bnon:
Guido van Rossum9604e662007-08-30 03:46:43 +00003333 eq(quoprimime.body_length(bytes([c])), 3)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003334
3335 def test_quote_unquote_idempotent(self):
3336 for x in range(256):
3337 c = chr(x)
3338 self.assertEqual(quoprimime.unquote(quoprimime.quote(c)), c)
3339
R David Murrayec1b5b82011-03-23 14:19:05 -04003340 def _test_header_encode(self, header, expected_encoded_header, charset=None):
3341 if charset is None:
3342 encoded_header = quoprimime.header_encode(header)
3343 else:
3344 encoded_header = quoprimime.header_encode(header, charset)
3345 self.assertEqual(encoded_header, expected_encoded_header)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003346
R David Murraycafd79d2011-03-23 15:25:55 -04003347 def test_header_encode_null(self):
3348 self._test_header_encode(b'', '')
3349
R David Murrayec1b5b82011-03-23 14:19:05 -04003350 def test_header_encode_one_word(self):
3351 self._test_header_encode(b'hello', '=?iso-8859-1?q?hello?=')
3352
3353 def test_header_encode_two_lines(self):
3354 self._test_header_encode(b'hello\nworld',
3355 '=?iso-8859-1?q?hello=0Aworld?=')
3356
3357 def test_header_encode_non_ascii(self):
3358 self._test_header_encode(b'hello\xc7there',
3359 '=?iso-8859-1?q?hello=C7there?=')
3360
3361 def test_header_encode_alt_charset(self):
3362 self._test_header_encode(b'hello', '=?iso-8859-2?q?hello?=',
3363 charset='iso-8859-2')
3364
3365 def _test_header_decode(self, encoded_header, expected_decoded_header):
3366 decoded_header = quoprimime.header_decode(encoded_header)
3367 self.assertEqual(decoded_header, expected_decoded_header)
3368
3369 def test_header_decode_null(self):
3370 self._test_header_decode('', '')
3371
3372 def test_header_decode_one_word(self):
3373 self._test_header_decode('hello', 'hello')
3374
3375 def test_header_decode_two_lines(self):
3376 self._test_header_decode('hello=0Aworld', 'hello\nworld')
3377
3378 def test_header_decode_non_ascii(self):
3379 self._test_header_decode('hello=C7there', 'hello\xc7there')
3380
3381 def _test_decode(self, encoded, expected_decoded, eol=None):
3382 if eol is None:
3383 decoded = quoprimime.decode(encoded)
3384 else:
3385 decoded = quoprimime.decode(encoded, eol=eol)
3386 self.assertEqual(decoded, expected_decoded)
3387
3388 def test_decode_null_word(self):
3389 self._test_decode('', '')
3390
3391 def test_decode_null_line_null_word(self):
3392 self._test_decode('\r\n', '\n')
3393
3394 def test_decode_one_word(self):
3395 self._test_decode('hello', 'hello')
3396
3397 def test_decode_one_word_eol(self):
3398 self._test_decode('hello', 'hello', eol='X')
3399
3400 def test_decode_one_line(self):
3401 self._test_decode('hello\r\n', 'hello\n')
3402
3403 def test_decode_one_line_lf(self):
3404 self._test_decode('hello\n', 'hello\n')
3405
R David Murraycafd79d2011-03-23 15:25:55 -04003406 def test_decode_one_line_cr(self):
3407 self._test_decode('hello\r', 'hello\n')
3408
3409 def test_decode_one_line_nl(self):
3410 self._test_decode('hello\n', 'helloX', eol='X')
3411
3412 def test_decode_one_line_crnl(self):
3413 self._test_decode('hello\r\n', 'helloX', eol='X')
3414
R David Murrayec1b5b82011-03-23 14:19:05 -04003415 def test_decode_one_line_one_word(self):
3416 self._test_decode('hello\r\nworld', 'hello\nworld')
3417
3418 def test_decode_one_line_one_word_eol(self):
3419 self._test_decode('hello\r\nworld', 'helloXworld', eol='X')
3420
3421 def test_decode_two_lines(self):
3422 self._test_decode('hello\r\nworld\r\n', 'hello\nworld\n')
3423
R David Murraycafd79d2011-03-23 15:25:55 -04003424 def test_decode_two_lines_eol(self):
3425 self._test_decode('hello\r\nworld\r\n', 'helloXworldX', eol='X')
3426
R David Murrayec1b5b82011-03-23 14:19:05 -04003427 def test_decode_one_long_line(self):
3428 self._test_decode('Spam' * 250, 'Spam' * 250)
3429
3430 def test_decode_one_space(self):
3431 self._test_decode(' ', '')
3432
3433 def test_decode_multiple_spaces(self):
3434 self._test_decode(' ' * 5, '')
3435
3436 def test_decode_one_line_trailing_spaces(self):
3437 self._test_decode('hello \r\n', 'hello\n')
3438
3439 def test_decode_two_lines_trailing_spaces(self):
3440 self._test_decode('hello \r\nworld \r\n', 'hello\nworld\n')
3441
3442 def test_decode_quoted_word(self):
3443 self._test_decode('=22quoted=20words=22', '"quoted words"')
3444
3445 def test_decode_uppercase_quoting(self):
3446 self._test_decode('ab=CD=EF', 'ab\xcd\xef')
3447
3448 def test_decode_lowercase_quoting(self):
3449 self._test_decode('ab=cd=ef', 'ab\xcd\xef')
3450
3451 def test_decode_soft_line_break(self):
3452 self._test_decode('soft line=\r\nbreak', 'soft linebreak')
3453
3454 def test_decode_false_quoting(self):
3455 self._test_decode('A=1,B=A ==> A+B==2', 'A=1,B=A ==> A+B==2')
3456
3457 def _test_encode(self, body, expected_encoded_body, maxlinelen=None, eol=None):
3458 kwargs = {}
3459 if maxlinelen is None:
3460 # Use body_encode's default.
3461 maxlinelen = 76
3462 else:
3463 kwargs['maxlinelen'] = maxlinelen
3464 if eol is None:
3465 # Use body_encode's default.
3466 eol = '\n'
3467 else:
3468 kwargs['eol'] = eol
3469 encoded_body = quoprimime.body_encode(body, **kwargs)
3470 self.assertEqual(encoded_body, expected_encoded_body)
3471 if eol == '\n' or eol == '\r\n':
3472 # We know how to split the result back into lines, so maxlinelen
3473 # can be checked.
3474 for line in encoded_body.splitlines():
3475 self.assertLessEqual(len(line), maxlinelen)
3476
3477 def test_encode_null(self):
3478 self._test_encode('', '')
3479
3480 def test_encode_null_lines(self):
3481 self._test_encode('\n\n', '\n\n')
3482
3483 def test_encode_one_line(self):
3484 self._test_encode('hello\n', 'hello\n')
3485
3486 def test_encode_one_line_crlf(self):
3487 self._test_encode('hello\r\n', 'hello\n')
3488
3489 def test_encode_one_line_eol(self):
3490 self._test_encode('hello\n', 'hello\r\n', eol='\r\n')
3491
3492 def test_encode_one_space(self):
3493 self._test_encode(' ', '=20')
3494
3495 def test_encode_one_line_one_space(self):
3496 self._test_encode(' \n', '=20\n')
3497
R David Murrayb938c8c2011-03-24 12:19:26 -04003498# XXX: body_encode() expect strings, but uses ord(char) from these strings
3499# to index into a 256-entry list. For code points above 255, this will fail.
3500# Should there be a check for 8-bit only ord() values in body, or at least
3501# a comment about the expected input?
3502
3503 def test_encode_two_lines_one_space(self):
3504 self._test_encode(' \n \n', '=20\n=20\n')
3505
R David Murrayec1b5b82011-03-23 14:19:05 -04003506 def test_encode_one_word_trailing_spaces(self):
3507 self._test_encode('hello ', 'hello =20')
3508
3509 def test_encode_one_line_trailing_spaces(self):
3510 self._test_encode('hello \n', 'hello =20\n')
3511
3512 def test_encode_one_word_trailing_tab(self):
3513 self._test_encode('hello \t', 'hello =09')
3514
3515 def test_encode_one_line_trailing_tab(self):
3516 self._test_encode('hello \t\n', 'hello =09\n')
3517
3518 def test_encode_trailing_space_before_maxlinelen(self):
3519 self._test_encode('abcd \n1234', 'abcd =\n\n1234', maxlinelen=6)
3520
R David Murrayb938c8c2011-03-24 12:19:26 -04003521 def test_encode_trailing_space_at_maxlinelen(self):
3522 self._test_encode('abcd \n1234', 'abcd=\n=20\n1234', maxlinelen=5)
3523
R David Murrayec1b5b82011-03-23 14:19:05 -04003524 def test_encode_trailing_space_beyond_maxlinelen(self):
R David Murrayb938c8c2011-03-24 12:19:26 -04003525 self._test_encode('abcd \n1234', 'abc=\nd=20\n1234', maxlinelen=4)
3526
3527 def test_encode_whitespace_lines(self):
3528 self._test_encode(' \n' * 5, '=20\n' * 5)
R David Murrayec1b5b82011-03-23 14:19:05 -04003529
3530 def test_encode_quoted_equals(self):
3531 self._test_encode('a = b', 'a =3D b')
3532
3533 def test_encode_one_long_string(self):
3534 self._test_encode('x' * 100, 'x' * 75 + '=\n' + 'x' * 25)
3535
3536 def test_encode_one_long_line(self):
3537 self._test_encode('x' * 100 + '\n', 'x' * 75 + '=\n' + 'x' * 25 + '\n')
3538
3539 def test_encode_one_very_long_line(self):
3540 self._test_encode('x' * 200 + '\n',
3541 2 * ('x' * 75 + '=\n') + 'x' * 50 + '\n')
3542
3543 def test_encode_one_long_line(self):
3544 self._test_encode('x' * 100 + '\n', 'x' * 75 + '=\n' + 'x' * 25 + '\n')
3545
3546 def test_encode_shortest_maxlinelen(self):
3547 self._test_encode('=' * 5, '=3D=\n' * 4 + '=3D', maxlinelen=4)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003548
R David Murrayb938c8c2011-03-24 12:19:26 -04003549 def test_encode_maxlinelen_too_small(self):
3550 self.assertRaises(ValueError, self._test_encode, '', '', maxlinelen=3)
3551
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003552 def test_encode(self):
3553 eq = self.assertEqual
Guido van Rossum9604e662007-08-30 03:46:43 +00003554 eq(quoprimime.body_encode(''), '')
3555 eq(quoprimime.body_encode('hello'), 'hello')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003556 # Test the binary flag
Guido van Rossum9604e662007-08-30 03:46:43 +00003557 eq(quoprimime.body_encode('hello\r\nworld'), 'hello\nworld')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003558 # Test the maxlinelen arg
Guido van Rossum9604e662007-08-30 03:46:43 +00003559 eq(quoprimime.body_encode('xxxx ' * 20, maxlinelen=40), """\
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003560xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=
3561 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=
3562x xxxx xxxx xxxx xxxx=20""")
3563 # Test the eol argument
Guido van Rossum9604e662007-08-30 03:46:43 +00003564 eq(quoprimime.body_encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'),
3565 """\
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003566xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=\r
3567 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=\r
3568x xxxx xxxx xxxx xxxx=20""")
Guido van Rossum9604e662007-08-30 03:46:43 +00003569 eq(quoprimime.body_encode("""\
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003570one line
3571
3572two line"""), """\
3573one line
3574
3575two line""")
3576
3577
Ezio Melottib3aedd42010-11-20 19:04:17 +00003578
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003579# Test the Charset class
3580class TestCharset(unittest.TestCase):
3581 def tearDown(self):
3582 from email import charset as CharsetModule
3583 try:
3584 del CharsetModule.CHARSETS['fake']
3585 except KeyError:
3586 pass
3587
Guido van Rossum9604e662007-08-30 03:46:43 +00003588 def test_codec_encodeable(self):
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003589 eq = self.assertEqual
3590 # Make sure us-ascii = no Unicode conversion
3591 c = Charset('us-ascii')
Guido van Rossum9604e662007-08-30 03:46:43 +00003592 eq(c.header_encode('Hello World!'), 'Hello World!')
3593 # Test 8-bit idempotency with us-ascii
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003594 s = '\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa'
Guido van Rossum9604e662007-08-30 03:46:43 +00003595 self.assertRaises(UnicodeError, c.header_encode, s)
3596 c = Charset('utf-8')
3597 eq(c.header_encode(s), '=?utf-8?b?wqTCosKkwqTCpMKmwqTCqMKkwqo=?=')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003598
3599 def test_body_encode(self):
3600 eq = self.assertEqual
3601 # Try a charset with QP body encoding
3602 c = Charset('iso-8859-1')
Barry Warsaw7aa02e62007-08-31 03:26:19 +00003603 eq('hello w=F6rld', c.body_encode('hello w\xf6rld'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003604 # Try a charset with Base64 body encoding
3605 c = Charset('utf-8')
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003606 eq('aGVsbG8gd29ybGQ=\n', c.body_encode(b'hello world'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003607 # Try a charset with None body encoding
3608 c = Charset('us-ascii')
Barry Warsaw7aa02e62007-08-31 03:26:19 +00003609 eq('hello world', c.body_encode('hello world'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003610 # Try the convert argument, where input codec != output codec
3611 c = Charset('euc-jp')
3612 # With apologies to Tokio Kikuchi ;)
Barry Warsawbef9d212007-08-31 10:55:37 +00003613 # XXX FIXME
3614## try:
3615## eq('\x1b$B5FCO;~IW\x1b(B',
3616## c.body_encode('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7'))
3617## eq('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7',
3618## c.body_encode('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7', False))
3619## except LookupError:
3620## # We probably don't have the Japanese codecs installed
3621## pass
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003622 # Testing SF bug #625509, which we have to fake, since there are no
3623 # built-in encodings where the header encoding is QP but the body
3624 # encoding is not.
3625 from email import charset as CharsetModule
R David Murray56a9d7e2011-03-15 12:20:02 -04003626 CharsetModule.add_charset('fake', CharsetModule.QP, None, 'utf-8')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003627 c = Charset('fake')
R David Murray56a9d7e2011-03-15 12:20:02 -04003628 eq('hello world', c.body_encode('hello world'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003629
3630 def test_unicode_charset_name(self):
3631 charset = Charset('us-ascii')
3632 self.assertEqual(str(charset), 'us-ascii')
3633 self.assertRaises(errors.CharsetError, Charset, 'asc\xffii')
3634
3635
Ezio Melottib3aedd42010-11-20 19:04:17 +00003636
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003637# Test multilingual MIME headers.
3638class TestHeader(TestEmailBase):
3639 def test_simple(self):
3640 eq = self.ndiffAssertEqual
3641 h = Header('Hello World!')
3642 eq(h.encode(), 'Hello World!')
3643 h.append(' Goodbye World!')
3644 eq(h.encode(), 'Hello World! Goodbye World!')
3645
3646 def test_simple_surprise(self):
3647 eq = self.ndiffAssertEqual
3648 h = Header('Hello World!')
3649 eq(h.encode(), 'Hello World!')
3650 h.append('Goodbye World!')
3651 eq(h.encode(), 'Hello World! Goodbye World!')
3652
3653 def test_header_needs_no_decoding(self):
3654 h = 'no decoding needed'
3655 self.assertEqual(decode_header(h), [(h, None)])
3656
3657 def test_long(self):
3658 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.",
3659 maxlinelen=76)
3660 for l in h.encode(splitchars=' ').split('\n '):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00003661 self.assertTrue(len(l) <= 76)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003662
3663 def test_multilingual(self):
3664 eq = self.ndiffAssertEqual
3665 g = Charset("iso-8859-1")
3666 cz = Charset("iso-8859-2")
3667 utf8 = Charset("utf-8")
3668 g_head = (b'Die Mieter treten hier ein werden mit einem '
3669 b'Foerderband komfortabel den Korridor entlang, '
3670 b'an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, '
3671 b'gegen die rotierenden Klingen bef\xf6rdert. ')
3672 cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich '
3673 b'd\xf9vtipu.. ')
3674 utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
3675 '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
3676 '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
3677 '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
3678 '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das '
3679 'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder '
3680 'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066'
3681 '\u3044\u307e\u3059\u3002')
3682 h = Header(g_head, g)
3683 h.append(cz_head, cz)
3684 h.append(utf8_head, utf8)
Guido van Rossum9604e662007-08-30 03:46:43 +00003685 enc = h.encode(maxlinelen=76)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003686 eq(enc, """\
Guido van Rossum9604e662007-08-30 03:46:43 +00003687=?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderband_kom?=
3688 =?iso-8859-1?q?fortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen_Wand?=
3689 =?iso-8859-1?q?gem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef=F6r?=
3690 =?iso-8859-1?q?dert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutily?=
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003691 =?iso-8859-2?q?_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?=
3692 =?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC?=
3693 =?utf-8?b?5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn?=
3694 =?utf-8?b?44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFz?=
Guido van Rossum9604e662007-08-30 03:46:43 +00003695 =?utf-8?b?IE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5k?=
3696 =?utf-8?b?IGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIA=?=
3697 =?utf-8?b?44Gj44Gm44GE44G+44GZ44CC?=""")
3698 decoded = decode_header(enc)
3699 eq(len(decoded), 3)
3700 eq(decoded[0], (g_head, 'iso-8859-1'))
3701 eq(decoded[1], (cz_head, 'iso-8859-2'))
3702 eq(decoded[2], (utf8_head.encode('utf-8'), 'utf-8'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003703 ustr = str(h)
Guido van Rossum9604e662007-08-30 03:46:43 +00003704 eq(ustr,
3705 (b'Die Mieter treten hier ein werden mit einem Foerderband '
3706 b'komfortabel den Korridor entlang, an s\xc3\xbcdl\xc3\xbcndischen '
3707 b'Wandgem\xc3\xa4lden vorbei, gegen die rotierenden Klingen '
3708 b'bef\xc3\xb6rdert. Finan\xc4\x8dni metropole se hroutily pod '
3709 b'tlakem jejich d\xc5\xafvtipu.. \xe6\xad\xa3\xe7\xa2\xba\xe3\x81'
3710 b'\xab\xe8\xa8\x80\xe3\x81\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3'
3711 b'\xe3\x81\xaf\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3'
3712 b'\x81\xbe\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
3713 b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8\xaa\x9e'
3714 b'\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81\xe3\x81\x82\xe3'
3715 b'\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81\x9f\xe3\x82\x89\xe3\x82'
3716 b'\x81\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\xe5\xae\x9f\xe9\x9a\x9b'
3717 b'\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x8cWenn ist das Nunstuck git '
3718 b'und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt '
3719 b'gersput.\xe3\x80\x8d\xe3\x81\xa8\xe8\xa8\x80\xe3\x81\xa3\xe3\x81'
3720 b'\xa6\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82'
3721 ).decode('utf-8'))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003722 # Test make_header()
3723 newh = make_header(decode_header(enc))
Guido van Rossum9604e662007-08-30 03:46:43 +00003724 eq(newh, h)
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003725
3726 def test_empty_header_encode(self):
3727 h = Header()
3728 self.assertEqual(h.encode(), '')
Barry Warsaw8b3d6592007-08-30 02:10:49 +00003729
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003730 def test_header_ctor_default_args(self):
3731 eq = self.ndiffAssertEqual
3732 h = Header()
3733 eq(h, '')
3734 h.append('foo', Charset('iso-8859-1'))
Guido van Rossum9604e662007-08-30 03:46:43 +00003735 eq(h, 'foo')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003736
3737 def test_explicit_maxlinelen(self):
3738 eq = self.ndiffAssertEqual
3739 hstr = ('A very long line that must get split to something other '
3740 'than at the 76th character boundary to test the non-default '
3741 'behavior')
3742 h = Header(hstr)
3743 eq(h.encode(), '''\
3744A very long line that must get split to something other than at the 76th
3745 character boundary to test the non-default behavior''')
3746 eq(str(h), hstr)
3747 h = Header(hstr, header_name='Subject')
3748 eq(h.encode(), '''\
3749A very long line that must get split to something other than at the
3750 76th character boundary to test the non-default behavior''')
3751 eq(str(h), hstr)
3752 h = Header(hstr, maxlinelen=1024, header_name='Subject')
3753 eq(h.encode(), hstr)
3754 eq(str(h), hstr)
3755
Guido van Rossum9604e662007-08-30 03:46:43 +00003756 def test_quopri_splittable(self):
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003757 eq = self.ndiffAssertEqual
3758 h = Header(charset='iso-8859-1', maxlinelen=20)
Guido van Rossum9604e662007-08-30 03:46:43 +00003759 x = 'xxxx ' * 20
3760 h.append(x)
3761 s = h.encode()
3762 eq(s, """\
3763=?iso-8859-1?q?xxx?=
3764 =?iso-8859-1?q?x_?=
3765 =?iso-8859-1?q?xx?=
3766 =?iso-8859-1?q?xx?=
3767 =?iso-8859-1?q?_x?=
3768 =?iso-8859-1?q?xx?=
3769 =?iso-8859-1?q?x_?=
3770 =?iso-8859-1?q?xx?=
3771 =?iso-8859-1?q?xx?=
3772 =?iso-8859-1?q?_x?=
3773 =?iso-8859-1?q?xx?=
3774 =?iso-8859-1?q?x_?=
3775 =?iso-8859-1?q?xx?=
3776 =?iso-8859-1?q?xx?=
3777 =?iso-8859-1?q?_x?=
3778 =?iso-8859-1?q?xx?=
3779 =?iso-8859-1?q?x_?=
3780 =?iso-8859-1?q?xx?=
3781 =?iso-8859-1?q?xx?=
3782 =?iso-8859-1?q?_x?=
3783 =?iso-8859-1?q?xx?=
3784 =?iso-8859-1?q?x_?=
3785 =?iso-8859-1?q?xx?=
3786 =?iso-8859-1?q?xx?=
3787 =?iso-8859-1?q?_x?=
3788 =?iso-8859-1?q?xx?=
3789 =?iso-8859-1?q?x_?=
3790 =?iso-8859-1?q?xx?=
3791 =?iso-8859-1?q?xx?=
3792 =?iso-8859-1?q?_x?=
3793 =?iso-8859-1?q?xx?=
3794 =?iso-8859-1?q?x_?=
3795 =?iso-8859-1?q?xx?=
3796 =?iso-8859-1?q?xx?=
3797 =?iso-8859-1?q?_x?=
3798 =?iso-8859-1?q?xx?=
3799 =?iso-8859-1?q?x_?=
3800 =?iso-8859-1?q?xx?=
3801 =?iso-8859-1?q?xx?=
3802 =?iso-8859-1?q?_x?=
3803 =?iso-8859-1?q?xx?=
3804 =?iso-8859-1?q?x_?=
3805 =?iso-8859-1?q?xx?=
3806 =?iso-8859-1?q?xx?=
3807 =?iso-8859-1?q?_x?=
3808 =?iso-8859-1?q?xx?=
3809 =?iso-8859-1?q?x_?=
3810 =?iso-8859-1?q?xx?=
3811 =?iso-8859-1?q?xx?=
3812 =?iso-8859-1?q?_?=""")
3813 eq(x, str(make_header(decode_header(s))))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003814 h = Header(charset='iso-8859-1', maxlinelen=40)
3815 h.append('xxxx ' * 20)
Guido van Rossum9604e662007-08-30 03:46:43 +00003816 s = h.encode()
3817 eq(s, """\
3818=?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xxx?=
3819 =?iso-8859-1?q?x_xxxx_xxxx_xxxx_xxxx_?=
3820 =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?=
3821 =?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?=
3822 =?iso-8859-1?q?_xxxx_xxxx_?=""")
3823 eq(x, str(make_header(decode_header(s))))
3824
3825 def test_base64_splittable(self):
3826 eq = self.ndiffAssertEqual
3827 h = Header(charset='koi8-r', maxlinelen=20)
3828 x = 'xxxx ' * 20
3829 h.append(x)
3830 s = h.encode()
3831 eq(s, """\
3832=?koi8-r?b?eHh4?=
3833 =?koi8-r?b?eCB4?=
3834 =?koi8-r?b?eHh4?=
3835 =?koi8-r?b?IHh4?=
3836 =?koi8-r?b?eHgg?=
3837 =?koi8-r?b?eHh4?=
3838 =?koi8-r?b?eCB4?=
3839 =?koi8-r?b?eHh4?=
3840 =?koi8-r?b?IHh4?=
3841 =?koi8-r?b?eHgg?=
3842 =?koi8-r?b?eHh4?=
3843 =?koi8-r?b?eCB4?=
3844 =?koi8-r?b?eHh4?=
3845 =?koi8-r?b?IHh4?=
3846 =?koi8-r?b?eHgg?=
3847 =?koi8-r?b?eHh4?=
3848 =?koi8-r?b?eCB4?=
3849 =?koi8-r?b?eHh4?=
3850 =?koi8-r?b?IHh4?=
3851 =?koi8-r?b?eHgg?=
3852 =?koi8-r?b?eHh4?=
3853 =?koi8-r?b?eCB4?=
3854 =?koi8-r?b?eHh4?=
3855 =?koi8-r?b?IHh4?=
3856 =?koi8-r?b?eHgg?=
3857 =?koi8-r?b?eHh4?=
3858 =?koi8-r?b?eCB4?=
3859 =?koi8-r?b?eHh4?=
3860 =?koi8-r?b?IHh4?=
3861 =?koi8-r?b?eHgg?=
3862 =?koi8-r?b?eHh4?=
3863 =?koi8-r?b?eCB4?=
3864 =?koi8-r?b?eHh4?=
3865 =?koi8-r?b?IA==?=""")
3866 eq(x, str(make_header(decode_header(s))))
3867 h = Header(charset='koi8-r', maxlinelen=40)
3868 h.append(x)
3869 s = h.encode()
3870 eq(s, """\
3871=?koi8-r?b?eHh4eCB4eHh4IHh4eHggeHh4?=
3872 =?koi8-r?b?eCB4eHh4IHh4eHggeHh4eCB4?=
3873 =?koi8-r?b?eHh4IHh4eHggeHh4eCB4eHh4?=
3874 =?koi8-r?b?IHh4eHggeHh4eCB4eHh4IHh4?=
3875 =?koi8-r?b?eHggeHh4eCB4eHh4IHh4eHgg?=
3876 =?koi8-r?b?eHh4eCB4eHh4IA==?=""")
3877 eq(x, str(make_header(decode_header(s))))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003878
3879 def test_us_ascii_header(self):
3880 eq = self.assertEqual
3881 s = 'hello'
3882 x = decode_header(s)
3883 eq(x, [('hello', None)])
3884 h = make_header(x)
3885 eq(s, h.encode())
3886
3887 def test_string_charset(self):
3888 eq = self.assertEqual
3889 h = Header()
3890 h.append('hello', 'iso-8859-1')
Guido van Rossum9604e662007-08-30 03:46:43 +00003891 eq(h, 'hello')
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003892
3893## def test_unicode_error(self):
3894## raises = self.assertRaises
3895## raises(UnicodeError, Header, u'[P\xf6stal]', 'us-ascii')
3896## raises(UnicodeError, Header, '[P\xf6stal]', 'us-ascii')
3897## h = Header()
3898## raises(UnicodeError, h.append, u'[P\xf6stal]', 'us-ascii')
3899## raises(UnicodeError, h.append, '[P\xf6stal]', 'us-ascii')
3900## raises(UnicodeError, Header, u'\u83ca\u5730\u6642\u592b', 'iso-8859-1')
3901
3902 def test_utf8_shortest(self):
3903 eq = self.assertEqual
3904 h = Header('p\xf6stal', 'utf-8')
3905 eq(h.encode(), '=?utf-8?q?p=C3=B6stal?=')
3906 h = Header('\u83ca\u5730\u6642\u592b', 'utf-8')
3907 eq(h.encode(), '=?utf-8?b?6I+K5Zyw5pmC5aSr?=')
3908
3909 def test_bad_8bit_header(self):
3910 raises = self.assertRaises
3911 eq = self.assertEqual
3912 x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
3913 raises(UnicodeError, Header, x)
3914 h = Header()
3915 raises(UnicodeError, h.append, x)
3916 e = x.decode('utf-8', 'replace')
3917 eq(str(Header(x, errors='replace')), e)
3918 h.append(x, errors='replace')
3919 eq(str(h), e)
3920
R David Murray041015c2011-03-25 15:10:55 -04003921 def test_escaped_8bit_header(self):
3922 x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
3923 x = x.decode('ascii', 'surrogateescape')
3924 h = Header(x, charset=email.charset.UNKNOWN8BIT)
3925 self.assertEqual(str(h),
3926 'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big')
3927 self.assertEqual(email.header.decode_header(h), [(x, 'unknown-8bit')])
3928
3929 def test_modify_returned_list_does_not_change_header(self):
3930 h = Header('test')
3931 chunks = email.header.decode_header(h)
3932 chunks.append(('ascii', 'test2'))
3933 self.assertEqual(str(h), 'test')
3934
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003935 def test_encoded_adjacent_nonencoded(self):
3936 eq = self.assertEqual
3937 h = Header()
3938 h.append('hello', 'iso-8859-1')
3939 h.append('world')
3940 s = h.encode()
3941 eq(s, '=?iso-8859-1?q?hello?= world')
3942 h = make_header(decode_header(s))
3943 eq(h.encode(), s)
3944
3945 def test_whitespace_eater(self):
3946 eq = self.assertEqual
3947 s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.'
3948 parts = decode_header(s)
3949 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)])
3950 hdr = make_header(parts)
3951 eq(hdr.encode(),
3952 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.')
3953
3954 def test_broken_base64_header(self):
3955 raises = self.assertRaises
R. David Murrayc4e69cc2010-08-03 22:14:10 +00003956 s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3I ?='
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003957 raises(errors.HeaderParseError, decode_header, s)
3958
R. David Murray477efb32011-01-05 01:39:32 +00003959 def test_shift_jis_charset(self):
3960 h = Header('文', charset='shift_jis')
3961 self.assertEqual(h.encode(), '=?iso-2022-jp?b?GyRCSjgbKEI=?=')
3962
R David Murrayde912762011-03-16 18:26:23 -04003963 def test_flatten_header_with_no_value(self):
3964 # Issue 11401 (regression from email 4.x) Note that the space after
3965 # the header doesn't reflect the input, but this is also the way
3966 # email 4.x behaved. At some point it would be nice to fix that.
3967 msg = email.message_from_string("EmptyHeader:")
3968 self.assertEqual(str(msg), "EmptyHeader: \n\n")
3969
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003970
Ezio Melottib3aedd42010-11-20 19:04:17 +00003971
Guido van Rossum8b3febe2007-08-30 01:15:14 +00003972# Test RFC 2231 header parameters (en/de)coding
3973class TestRFC2231(TestEmailBase):
3974 def test_get_param(self):
3975 eq = self.assertEqual
3976 msg = self._msgobj('msg_29.txt')
3977 eq(msg.get_param('title'),
3978 ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!'))
3979 eq(msg.get_param('title', unquote=False),
3980 ('us-ascii', 'en', '"This is even more ***fun*** isn\'t it!"'))
3981
3982 def test_set_param(self):
3983 eq = self.ndiffAssertEqual
3984 msg = Message()
3985 msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
3986 charset='us-ascii')
3987 eq(msg.get_param('title'),
3988 ('us-ascii', '', 'This is even more ***fun*** isn\'t it!'))
3989 msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
3990 charset='us-ascii', language='en')
3991 eq(msg.get_param('title'),
3992 ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!'))
3993 msg = self._msgobj('msg_01.txt')
3994 msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
3995 charset='us-ascii', language='en')
3996 eq(msg.as_string(maxheaderlen=78), """\
3997Return-Path: <bbb@zzz.org>
3998Delivered-To: bbb@zzz.org
3999Received: by mail.zzz.org (Postfix, from userid 889)
4000\tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT)
4001MIME-Version: 1.0
4002Content-Transfer-Encoding: 7bit
4003Message-ID: <15090.61304.110929.45684@aaa.zzz.org>
4004From: bbb@ddd.com (John X. Doe)
4005To: bbb@zzz.org
4006Subject: This is a test message
4007Date: Fri, 4 May 2001 14:05:44 -0400
4008Content-Type: text/plain; charset=us-ascii;
R. David Murraydfd7eb02010-12-24 22:36:49 +00004009 title*=us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004010
4011
4012Hi,
4013
4014Do you like this message?
4015
4016-Me
4017""")
4018
4019 def test_del_param(self):
4020 eq = self.ndiffAssertEqual
4021 msg = self._msgobj('msg_01.txt')
4022 msg.set_param('foo', 'bar', charset='us-ascii', language='en')
4023 msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
4024 charset='us-ascii', language='en')
4025 msg.del_param('foo', header='Content-Type')
4026 eq(msg.as_string(maxheaderlen=78), """\
4027Return-Path: <bbb@zzz.org>
4028Delivered-To: bbb@zzz.org
4029Received: by mail.zzz.org (Postfix, from userid 889)
4030\tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT)
4031MIME-Version: 1.0
4032Content-Transfer-Encoding: 7bit
4033Message-ID: <15090.61304.110929.45684@aaa.zzz.org>
4034From: bbb@ddd.com (John X. Doe)
4035To: bbb@zzz.org
4036Subject: This is a test message
4037Date: Fri, 4 May 2001 14:05:44 -0400
4038Content-Type: text/plain; charset="us-ascii";
R. David Murraydfd7eb02010-12-24 22:36:49 +00004039 title*=us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004040
4041
4042Hi,
4043
4044Do you like this message?
4045
4046-Me
4047""")
4048
4049 def test_rfc2231_get_content_charset(self):
4050 eq = self.assertEqual
4051 msg = self._msgobj('msg_32.txt')
4052 eq(msg.get_content_charset(), 'us-ascii')
4053
R. David Murraydfd7eb02010-12-24 22:36:49 +00004054 def test_rfc2231_parse_rfc_quoting(self):
4055 m = textwrap.dedent('''\
4056 Content-Disposition: inline;
4057 \tfilename*0*=''This%20is%20even%20more%20;
4058 \tfilename*1*=%2A%2A%2Afun%2A%2A%2A%20;
4059 \tfilename*2="is it not.pdf"
4060
4061 ''')
4062 msg = email.message_from_string(m)
4063 self.assertEqual(msg.get_filename(),
4064 'This is even more ***fun*** is it not.pdf')
4065 self.assertEqual(m, msg.as_string())
4066
4067 def test_rfc2231_parse_extra_quoting(self):
4068 m = textwrap.dedent('''\
4069 Content-Disposition: inline;
4070 \tfilename*0*="''This%20is%20even%20more%20";
4071 \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
4072 \tfilename*2="is it not.pdf"
4073
4074 ''')
4075 msg = email.message_from_string(m)
4076 self.assertEqual(msg.get_filename(),
4077 'This is even more ***fun*** is it not.pdf')
4078 self.assertEqual(m, msg.as_string())
4079
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004080 def test_rfc2231_no_language_or_charset(self):
4081 m = '''\
4082Content-Transfer-Encoding: 8bit
4083Content-Disposition: inline; filename="file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm"
4084Content-Type: text/html; NAME*0=file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEM; NAME*1=P_nsmail.htm
4085
4086'''
4087 msg = email.message_from_string(m)
4088 param = msg.get_param('NAME')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00004089 self.assertFalse(isinstance(param, tuple))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004090 self.assertEqual(
4091 param,
4092 'file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm')
4093
4094 def test_rfc2231_no_language_or_charset_in_filename(self):
4095 m = '''\
4096Content-Disposition: inline;
4097\tfilename*0*="''This%20is%20even%20more%20";
4098\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
4099\tfilename*2="is it not.pdf"
4100
4101'''
4102 msg = email.message_from_string(m)
4103 self.assertEqual(msg.get_filename(),
4104 'This is even more ***fun*** is it not.pdf')
4105
4106 def test_rfc2231_no_language_or_charset_in_filename_encoded(self):
4107 m = '''\
4108Content-Disposition: inline;
4109\tfilename*0*="''This%20is%20even%20more%20";
4110\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
4111\tfilename*2="is it not.pdf"
4112
4113'''
4114 msg = email.message_from_string(m)
4115 self.assertEqual(msg.get_filename(),
4116 'This is even more ***fun*** is it not.pdf')
4117
4118 def test_rfc2231_partly_encoded(self):
4119 m = '''\
4120Content-Disposition: inline;
4121\tfilename*0="''This%20is%20even%20more%20";
4122\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
4123\tfilename*2="is it not.pdf"
4124
4125'''
4126 msg = email.message_from_string(m)
4127 self.assertEqual(
4128 msg.get_filename(),
4129 'This%20is%20even%20more%20***fun*** is it not.pdf')
4130
4131 def test_rfc2231_partly_nonencoded(self):
4132 m = '''\
4133Content-Disposition: inline;
4134\tfilename*0="This%20is%20even%20more%20";
4135\tfilename*1="%2A%2A%2Afun%2A%2A%2A%20";
4136\tfilename*2="is it not.pdf"
4137
4138'''
4139 msg = email.message_from_string(m)
4140 self.assertEqual(
4141 msg.get_filename(),
4142 'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20is it not.pdf')
4143
4144 def test_rfc2231_no_language_or_charset_in_boundary(self):
4145 m = '''\
4146Content-Type: multipart/alternative;
4147\tboundary*0*="''This%20is%20even%20more%20";
4148\tboundary*1*="%2A%2A%2Afun%2A%2A%2A%20";
4149\tboundary*2="is it not.pdf"
4150
4151'''
4152 msg = email.message_from_string(m)
4153 self.assertEqual(msg.get_boundary(),
4154 'This is even more ***fun*** is it not.pdf')
4155
4156 def test_rfc2231_no_language_or_charset_in_charset(self):
4157 # This is a nonsensical charset value, but tests the code anyway
4158 m = '''\
4159Content-Type: text/plain;
4160\tcharset*0*="This%20is%20even%20more%20";
4161\tcharset*1*="%2A%2A%2Afun%2A%2A%2A%20";
4162\tcharset*2="is it not.pdf"
4163
4164'''
4165 msg = email.message_from_string(m)
4166 self.assertEqual(msg.get_content_charset(),
4167 'this is even more ***fun*** is it not.pdf')
4168
4169 def test_rfc2231_bad_encoding_in_filename(self):
4170 m = '''\
4171Content-Disposition: inline;
4172\tfilename*0*="bogus'xx'This%20is%20even%20more%20";
4173\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
4174\tfilename*2="is it not.pdf"
4175
4176'''
4177 msg = email.message_from_string(m)
4178 self.assertEqual(msg.get_filename(),
4179 'This is even more ***fun*** is it not.pdf')
4180
4181 def test_rfc2231_bad_encoding_in_charset(self):
4182 m = """\
4183Content-Type: text/plain; charset*=bogus''utf-8%E2%80%9D
4184
4185"""
4186 msg = email.message_from_string(m)
4187 # This should return None because non-ascii characters in the charset
4188 # are not allowed.
4189 self.assertEqual(msg.get_content_charset(), None)
4190
4191 def test_rfc2231_bad_character_in_charset(self):
4192 m = """\
4193Content-Type: text/plain; charset*=ascii''utf-8%E2%80%9D
4194
4195"""
4196 msg = email.message_from_string(m)
4197 # This should return None because non-ascii characters in the charset
4198 # are not allowed.
4199 self.assertEqual(msg.get_content_charset(), None)
4200
4201 def test_rfc2231_bad_character_in_filename(self):
4202 m = '''\
4203Content-Disposition: inline;
4204\tfilename*0*="ascii'xx'This%20is%20even%20more%20";
4205\tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
4206\tfilename*2*="is it not.pdf%E2"
4207
4208'''
4209 msg = email.message_from_string(m)
4210 self.assertEqual(msg.get_filename(),
4211 'This is even more ***fun*** is it not.pdf\ufffd')
4212
4213 def test_rfc2231_unknown_encoding(self):
4214 m = """\
4215Content-Transfer-Encoding: 8bit
4216Content-Disposition: inline; filename*=X-UNKNOWN''myfile.txt
4217
4218"""
4219 msg = email.message_from_string(m)
4220 self.assertEqual(msg.get_filename(), 'myfile.txt')
4221
4222 def test_rfc2231_single_tick_in_filename_extended(self):
4223 eq = self.assertEqual
4224 m = """\
4225Content-Type: application/x-foo;
4226\tname*0*=\"Frank's\"; name*1*=\" Document\"
4227
4228"""
4229 msg = email.message_from_string(m)
4230 charset, language, s = msg.get_param('name')
4231 eq(charset, None)
4232 eq(language, None)
4233 eq(s, "Frank's Document")
4234
4235 def test_rfc2231_single_tick_in_filename(self):
4236 m = """\
4237Content-Type: application/x-foo; name*0=\"Frank's\"; name*1=\" Document\"
4238
4239"""
4240 msg = email.message_from_string(m)
4241 param = msg.get_param('name')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00004242 self.assertFalse(isinstance(param, tuple))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004243 self.assertEqual(param, "Frank's Document")
4244
4245 def test_rfc2231_tick_attack_extended(self):
4246 eq = self.assertEqual
4247 m = """\
4248Content-Type: application/x-foo;
4249\tname*0*=\"us-ascii'en-us'Frank's\"; name*1*=\" Document\"
4250
4251"""
4252 msg = email.message_from_string(m)
4253 charset, language, s = msg.get_param('name')
4254 eq(charset, 'us-ascii')
4255 eq(language, 'en-us')
4256 eq(s, "Frank's Document")
4257
4258 def test_rfc2231_tick_attack(self):
4259 m = """\
4260Content-Type: application/x-foo;
4261\tname*0=\"us-ascii'en-us'Frank's\"; name*1=\" Document\"
4262
4263"""
4264 msg = email.message_from_string(m)
4265 param = msg.get_param('name')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00004266 self.assertFalse(isinstance(param, tuple))
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004267 self.assertEqual(param, "us-ascii'en-us'Frank's Document")
4268
4269 def test_rfc2231_no_extended_values(self):
4270 eq = self.assertEqual
4271 m = """\
4272Content-Type: application/x-foo; name=\"Frank's Document\"
4273
4274"""
4275 msg = email.message_from_string(m)
4276 eq(msg.get_param('name'), "Frank's Document")
4277
4278 def test_rfc2231_encoded_then_unencoded_segments(self):
4279 eq = self.assertEqual
4280 m = """\
4281Content-Type: application/x-foo;
4282\tname*0*=\"us-ascii'en-us'My\";
4283\tname*1=\" Document\";
4284\tname*2*=\" For You\"
4285
4286"""
4287 msg = email.message_from_string(m)
4288 charset, language, s = msg.get_param('name')
4289 eq(charset, 'us-ascii')
4290 eq(language, 'en-us')
4291 eq(s, 'My Document For You')
4292
4293 def test_rfc2231_unencoded_then_encoded_segments(self):
4294 eq = self.assertEqual
4295 m = """\
4296Content-Type: application/x-foo;
4297\tname*0=\"us-ascii'en-us'My\";
4298\tname*1*=\" Document\";
4299\tname*2*=\" For You\"
4300
4301"""
4302 msg = email.message_from_string(m)
4303 charset, language, s = msg.get_param('name')
4304 eq(charset, 'us-ascii')
4305 eq(language, 'en-us')
4306 eq(s, 'My Document For You')
4307
4308
Ezio Melottib3aedd42010-11-20 19:04:17 +00004309
R. David Murraya8f480f2010-01-16 18:30:03 +00004310# Tests to ensure that signed parts of an email are completely preserved, as
4311# required by RFC1847 section 2.1. Note that these are incomplete, because the
4312# email package does not currently always preserve the body. See issue 1670765.
4313class TestSigned(TestEmailBase):
4314
4315 def _msg_and_obj(self, filename):
4316 with openfile(findfile(filename)) as fp:
4317 original = fp.read()
4318 msg = email.message_from_string(original)
4319 return original, msg
4320
4321 def _signed_parts_eq(self, original, result):
4322 # Extract the first mime part of each message
4323 import re
4324 repart = re.compile(r'^--([^\n]+)\n(.*?)\n--\1$', re.S | re.M)
4325 inpart = repart.search(original).group(2)
4326 outpart = repart.search(result).group(2)
4327 self.assertEqual(outpart, inpart)
4328
4329 def test_long_headers_as_string(self):
4330 original, msg = self._msg_and_obj('msg_45.txt')
4331 result = msg.as_string()
4332 self._signed_parts_eq(original, result)
4333
4334 def test_long_headers_as_string_maxheaderlen(self):
4335 original, msg = self._msg_and_obj('msg_45.txt')
4336 result = msg.as_string(maxheaderlen=60)
4337 self._signed_parts_eq(original, result)
4338
4339 def test_long_headers_flatten(self):
4340 original, msg = self._msg_and_obj('msg_45.txt')
4341 fp = StringIO()
4342 Generator(fp).flatten(msg)
4343 result = fp.getvalue()
4344 self._signed_parts_eq(original, result)
4345
4346
Ezio Melottib3aedd42010-11-20 19:04:17 +00004347
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004348def _testclasses():
4349 mod = sys.modules[__name__]
4350 return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
4351
4352
4353def suite():
4354 suite = unittest.TestSuite()
4355 for testclass in _testclasses():
4356 suite.addTest(unittest.makeSuite(testclass))
4357 return suite
4358
4359
4360def test_main():
4361 for testclass in _testclasses():
4362 run_unittest(testclass)
4363
4364
Ezio Melottib3aedd42010-11-20 19:04:17 +00004365
Guido van Rossum8b3febe2007-08-30 01:15:14 +00004366if __name__ == '__main__':
4367 unittest.main(defaultTest='suite')