PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/email/test/test_email_renamed.py

http://unladen-swallow.googlecode.com/
Python | 3284 lines | 3205 code | 41 blank | 38 comment | 10 complexity | bdf971ce6de3457afd007a1b0a5daa4a MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  1. # Copyright (C) 2001-2007 Python Software Foundation
  2. # Contact: email-sig@python.org
  3. # email package unit tests
  4. import os
  5. import sys
  6. import time
  7. import base64
  8. import difflib
  9. import unittest
  10. import warnings
  11. from cStringIO import StringIO
  12. import email
  13. from email.charset import Charset
  14. from email.header import Header, decode_header, make_header
  15. from email.parser import Parser, HeaderParser
  16. from email.generator import Generator, DecodedGenerator
  17. from email.message import Message
  18. from email.mime.application import MIMEApplication
  19. from email.mime.audio import MIMEAudio
  20. from email.mime.text import MIMEText
  21. from email.mime.image import MIMEImage
  22. from email.mime.base import MIMEBase
  23. from email.mime.message import MIMEMessage
  24. from email.mime.multipart import MIMEMultipart
  25. from email import utils
  26. from email import errors
  27. from email import encoders
  28. from email import iterators
  29. from email import base64mime
  30. from email import quoprimime
  31. from test.test_support import findfile, run_unittest
  32. from email.test import __file__ as landmark
  33. NL = '\n'
  34. EMPTYSTRING = ''
  35. SPACE = ' '
  36. def openfile(filename, mode='r'):
  37. path = os.path.join(os.path.dirname(landmark), 'data', filename)
  38. return open(path, mode)
  39. # Base test class
  40. class TestEmailBase(unittest.TestCase):
  41. def ndiffAssertEqual(self, first, second):
  42. """Like failUnlessEqual except use ndiff for readable output."""
  43. if first <> second:
  44. sfirst = str(first)
  45. ssecond = str(second)
  46. diff = difflib.ndiff(sfirst.splitlines(), ssecond.splitlines())
  47. fp = StringIO()
  48. print >> fp, NL, NL.join(diff)
  49. raise self.failureException, fp.getvalue()
  50. def _msgobj(self, filename):
  51. fp = openfile(findfile(filename))
  52. try:
  53. msg = email.message_from_file(fp)
  54. finally:
  55. fp.close()
  56. return msg
  57. # Test various aspects of the Message class's API
  58. class TestMessageAPI(TestEmailBase):
  59. def test_get_all(self):
  60. eq = self.assertEqual
  61. msg = self._msgobj('msg_20.txt')
  62. eq(msg.get_all('cc'), ['ccc@zzz.org', 'ddd@zzz.org', 'eee@zzz.org'])
  63. eq(msg.get_all('xx', 'n/a'), 'n/a')
  64. def test_getset_charset(self):
  65. eq = self.assertEqual
  66. msg = Message()
  67. eq(msg.get_charset(), None)
  68. charset = Charset('iso-8859-1')
  69. msg.set_charset(charset)
  70. eq(msg['mime-version'], '1.0')
  71. eq(msg.get_content_type(), 'text/plain')
  72. eq(msg['content-type'], 'text/plain; charset="iso-8859-1"')
  73. eq(msg.get_param('charset'), 'iso-8859-1')
  74. eq(msg['content-transfer-encoding'], 'quoted-printable')
  75. eq(msg.get_charset().input_charset, 'iso-8859-1')
  76. # Remove the charset
  77. msg.set_charset(None)
  78. eq(msg.get_charset(), None)
  79. eq(msg['content-type'], 'text/plain')
  80. # Try adding a charset when there's already MIME headers present
  81. msg = Message()
  82. msg['MIME-Version'] = '2.0'
  83. msg['Content-Type'] = 'text/x-weird'
  84. msg['Content-Transfer-Encoding'] = 'quinted-puntable'
  85. msg.set_charset(charset)
  86. eq(msg['mime-version'], '2.0')
  87. eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"')
  88. eq(msg['content-transfer-encoding'], 'quinted-puntable')
  89. def test_set_charset_from_string(self):
  90. eq = self.assertEqual
  91. msg = Message()
  92. msg.set_charset('us-ascii')
  93. eq(msg.get_charset().input_charset, 'us-ascii')
  94. eq(msg['content-type'], 'text/plain; charset="us-ascii"')
  95. def test_set_payload_with_charset(self):
  96. msg = Message()
  97. charset = Charset('iso-8859-1')
  98. msg.set_payload('This is a string payload', charset)
  99. self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1')
  100. def test_get_charsets(self):
  101. eq = self.assertEqual
  102. msg = self._msgobj('msg_08.txt')
  103. charsets = msg.get_charsets()
  104. eq(charsets, [None, 'us-ascii', 'iso-8859-1', 'iso-8859-2', 'koi8-r'])
  105. msg = self._msgobj('msg_09.txt')
  106. charsets = msg.get_charsets('dingbat')
  107. eq(charsets, ['dingbat', 'us-ascii', 'iso-8859-1', 'dingbat',
  108. 'koi8-r'])
  109. msg = self._msgobj('msg_12.txt')
  110. charsets = msg.get_charsets()
  111. eq(charsets, [None, 'us-ascii', 'iso-8859-1', None, 'iso-8859-2',
  112. 'iso-8859-3', 'us-ascii', 'koi8-r'])
  113. def test_get_filename(self):
  114. eq = self.assertEqual
  115. msg = self._msgobj('msg_04.txt')
  116. filenames = [p.get_filename() for p in msg.get_payload()]
  117. eq(filenames, ['msg.txt', 'msg.txt'])
  118. msg = self._msgobj('msg_07.txt')
  119. subpart = msg.get_payload(1)
  120. eq(subpart.get_filename(), 'dingusfish.gif')
  121. def test_get_filename_with_name_parameter(self):
  122. eq = self.assertEqual
  123. msg = self._msgobj('msg_44.txt')
  124. filenames = [p.get_filename() for p in msg.get_payload()]
  125. eq(filenames, ['msg.txt', 'msg.txt'])
  126. def test_get_boundary(self):
  127. eq = self.assertEqual
  128. msg = self._msgobj('msg_07.txt')
  129. # No quotes!
  130. eq(msg.get_boundary(), 'BOUNDARY')
  131. def test_set_boundary(self):
  132. eq = self.assertEqual
  133. # This one has no existing boundary parameter, but the Content-Type:
  134. # header appears fifth.
  135. msg = self._msgobj('msg_01.txt')
  136. msg.set_boundary('BOUNDARY')
  137. header, value = msg.items()[4]
  138. eq(header.lower(), 'content-type')
  139. eq(value, 'text/plain; charset="us-ascii"; boundary="BOUNDARY"')
  140. # This one has a Content-Type: header, with a boundary, stuck in the
  141. # middle of its headers. Make sure the order is preserved; it should
  142. # be fifth.
  143. msg = self._msgobj('msg_04.txt')
  144. msg.set_boundary('BOUNDARY')
  145. header, value = msg.items()[4]
  146. eq(header.lower(), 'content-type')
  147. eq(value, 'multipart/mixed; boundary="BOUNDARY"')
  148. # And this one has no Content-Type: header at all.
  149. msg = self._msgobj('msg_03.txt')
  150. self.assertRaises(errors.HeaderParseError,
  151. msg.set_boundary, 'BOUNDARY')
  152. def test_get_decoded_payload(self):
  153. eq = self.assertEqual
  154. msg = self._msgobj('msg_10.txt')
  155. # The outer message is a multipart
  156. eq(msg.get_payload(decode=True), None)
  157. # Subpart 1 is 7bit encoded
  158. eq(msg.get_payload(0).get_payload(decode=True),
  159. 'This is a 7bit encoded message.\n')
  160. # Subpart 2 is quopri
  161. eq(msg.get_payload(1).get_payload(decode=True),
  162. '\xa1This is a Quoted Printable encoded message!\n')
  163. # Subpart 3 is base64
  164. eq(msg.get_payload(2).get_payload(decode=True),
  165. 'This is a Base64 encoded message.')
  166. # Subpart 4 has no Content-Transfer-Encoding: header.
  167. eq(msg.get_payload(3).get_payload(decode=True),
  168. 'This has no Content-Transfer-Encoding: header.\n')
  169. def test_get_decoded_uu_payload(self):
  170. eq = self.assertEqual
  171. msg = Message()
  172. msg.set_payload('begin 666 -\n+:&5L;&\\@=V]R;&0 \n \nend\n')
  173. for cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
  174. msg['content-transfer-encoding'] = cte
  175. eq(msg.get_payload(decode=True), 'hello world')
  176. # Now try some bogus data
  177. msg.set_payload('foo')
  178. eq(msg.get_payload(decode=True), 'foo')
  179. def test_decoded_generator(self):
  180. eq = self.assertEqual
  181. msg = self._msgobj('msg_07.txt')
  182. fp = openfile('msg_17.txt')
  183. try:
  184. text = fp.read()
  185. finally:
  186. fp.close()
  187. s = StringIO()
  188. g = DecodedGenerator(s)
  189. g.flatten(msg)
  190. eq(s.getvalue(), text)
  191. def test__contains__(self):
  192. msg = Message()
  193. msg['From'] = 'Me'
  194. msg['to'] = 'You'
  195. # Check for case insensitivity
  196. self.failUnless('from' in msg)
  197. self.failUnless('From' in msg)
  198. self.failUnless('FROM' in msg)
  199. self.failUnless('to' in msg)
  200. self.failUnless('To' in msg)
  201. self.failUnless('TO' in msg)
  202. def test_as_string(self):
  203. eq = self.assertEqual
  204. msg = self._msgobj('msg_01.txt')
  205. fp = openfile('msg_01.txt')
  206. try:
  207. text = fp.read()
  208. finally:
  209. fp.close()
  210. eq(text, msg.as_string())
  211. fullrepr = str(msg)
  212. lines = fullrepr.split('\n')
  213. self.failUnless(lines[0].startswith('From '))
  214. eq(text, NL.join(lines[1:]))
  215. def test_bad_param(self):
  216. msg = email.message_from_string("Content-Type: blarg; baz; boo\n")
  217. self.assertEqual(msg.get_param('baz'), '')
  218. def test_missing_filename(self):
  219. msg = email.message_from_string("From: foo\n")
  220. self.assertEqual(msg.get_filename(), None)
  221. def test_bogus_filename(self):
  222. msg = email.message_from_string(
  223. "Content-Disposition: blarg; filename\n")
  224. self.assertEqual(msg.get_filename(), '')
  225. def test_missing_boundary(self):
  226. msg = email.message_from_string("From: foo\n")
  227. self.assertEqual(msg.get_boundary(), None)
  228. def test_get_params(self):
  229. eq = self.assertEqual
  230. msg = email.message_from_string(
  231. 'X-Header: foo=one; bar=two; baz=three\n')
  232. eq(msg.get_params(header='x-header'),
  233. [('foo', 'one'), ('bar', 'two'), ('baz', 'three')])
  234. msg = email.message_from_string(
  235. 'X-Header: foo; bar=one; baz=two\n')
  236. eq(msg.get_params(header='x-header'),
  237. [('foo', ''), ('bar', 'one'), ('baz', 'two')])
  238. eq(msg.get_params(), None)
  239. msg = email.message_from_string(
  240. 'X-Header: foo; bar="one"; baz=two\n')
  241. eq(msg.get_params(header='x-header'),
  242. [('foo', ''), ('bar', 'one'), ('baz', 'two')])
  243. def test_get_param_liberal(self):
  244. msg = Message()
  245. msg['Content-Type'] = 'Content-Type: Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"'
  246. self.assertEqual(msg.get_param('boundary'), 'CPIMSSMTPC06p5f3tG')
  247. def test_get_param(self):
  248. eq = self.assertEqual
  249. msg = email.message_from_string(
  250. "X-Header: foo=one; bar=two; baz=three\n")
  251. eq(msg.get_param('bar', header='x-header'), 'two')
  252. eq(msg.get_param('quuz', header='x-header'), None)
  253. eq(msg.get_param('quuz'), None)
  254. msg = email.message_from_string(
  255. 'X-Header: foo; bar="one"; baz=two\n')
  256. eq(msg.get_param('foo', header='x-header'), '')
  257. eq(msg.get_param('bar', header='x-header'), 'one')
  258. eq(msg.get_param('baz', header='x-header'), 'two')
  259. # XXX: We are not RFC-2045 compliant! We cannot parse:
  260. # msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"'
  261. # msg.get_param("weird")
  262. # yet.
  263. def test_get_param_funky_continuation_lines(self):
  264. msg = self._msgobj('msg_22.txt')
  265. self.assertEqual(msg.get_payload(1).get_param('name'), 'wibble.JPG')
  266. def test_get_param_with_semis_in_quotes(self):
  267. msg = email.message_from_string(
  268. 'Content-Type: image/pjpeg; name="Jim&amp;&amp;Jill"\n')
  269. self.assertEqual(msg.get_param('name'), 'Jim&amp;&amp;Jill')
  270. self.assertEqual(msg.get_param('name', unquote=False),
  271. '"Jim&amp;&amp;Jill"')
  272. def test_has_key(self):
  273. msg = email.message_from_string('Header: exists')
  274. self.failUnless(msg.has_key('header'))
  275. self.failUnless(msg.has_key('Header'))
  276. self.failUnless(msg.has_key('HEADER'))
  277. self.failIf(msg.has_key('headeri'))
  278. def test_set_param(self):
  279. eq = self.assertEqual
  280. msg = Message()
  281. msg.set_param('charset', 'iso-2022-jp')
  282. eq(msg.get_param('charset'), 'iso-2022-jp')
  283. msg.set_param('importance', 'high value')
  284. eq(msg.get_param('importance'), 'high value')
  285. eq(msg.get_param('importance', unquote=False), '"high value"')
  286. eq(msg.get_params(), [('text/plain', ''),
  287. ('charset', 'iso-2022-jp'),
  288. ('importance', 'high value')])
  289. eq(msg.get_params(unquote=False), [('text/plain', ''),
  290. ('charset', '"iso-2022-jp"'),
  291. ('importance', '"high value"')])
  292. msg.set_param('charset', 'iso-9999-xx', header='X-Jimmy')
  293. eq(msg.get_param('charset', header='X-Jimmy'), 'iso-9999-xx')
  294. def test_del_param(self):
  295. eq = self.assertEqual
  296. msg = self._msgobj('msg_05.txt')
  297. eq(msg.get_params(),
  298. [('multipart/report', ''), ('report-type', 'delivery-status'),
  299. ('boundary', 'D1690A7AC1.996856090/mail.example.com')])
  300. old_val = msg.get_param("report-type")
  301. msg.del_param("report-type")
  302. eq(msg.get_params(),
  303. [('multipart/report', ''),
  304. ('boundary', 'D1690A7AC1.996856090/mail.example.com')])
  305. msg.set_param("report-type", old_val)
  306. eq(msg.get_params(),
  307. [('multipart/report', ''),
  308. ('boundary', 'D1690A7AC1.996856090/mail.example.com'),
  309. ('report-type', old_val)])
  310. def test_del_param_on_other_header(self):
  311. msg = Message()
  312. msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
  313. msg.del_param('filename', 'content-disposition')
  314. self.assertEqual(msg['content-disposition'], 'attachment')
  315. def test_set_type(self):
  316. eq = self.assertEqual
  317. msg = Message()
  318. self.assertRaises(ValueError, msg.set_type, 'text')
  319. msg.set_type('text/plain')
  320. eq(msg['content-type'], 'text/plain')
  321. msg.set_param('charset', 'us-ascii')
  322. eq(msg['content-type'], 'text/plain; charset="us-ascii"')
  323. msg.set_type('text/html')
  324. eq(msg['content-type'], 'text/html; charset="us-ascii"')
  325. def test_set_type_on_other_header(self):
  326. msg = Message()
  327. msg['X-Content-Type'] = 'text/plain'
  328. msg.set_type('application/octet-stream', 'X-Content-Type')
  329. self.assertEqual(msg['x-content-type'], 'application/octet-stream')
  330. def test_get_content_type_missing(self):
  331. msg = Message()
  332. self.assertEqual(msg.get_content_type(), 'text/plain')
  333. def test_get_content_type_missing_with_default_type(self):
  334. msg = Message()
  335. msg.set_default_type('message/rfc822')
  336. self.assertEqual(msg.get_content_type(), 'message/rfc822')
  337. def test_get_content_type_from_message_implicit(self):
  338. msg = self._msgobj('msg_30.txt')
  339. self.assertEqual(msg.get_payload(0).get_content_type(),
  340. 'message/rfc822')
  341. def test_get_content_type_from_message_explicit(self):
  342. msg = self._msgobj('msg_28.txt')
  343. self.assertEqual(msg.get_payload(0).get_content_type(),
  344. 'message/rfc822')
  345. def test_get_content_type_from_message_text_plain_implicit(self):
  346. msg = self._msgobj('msg_03.txt')
  347. self.assertEqual(msg.get_content_type(), 'text/plain')
  348. def test_get_content_type_from_message_text_plain_explicit(self):
  349. msg = self._msgobj('msg_01.txt')
  350. self.assertEqual(msg.get_content_type(), 'text/plain')
  351. def test_get_content_maintype_missing(self):
  352. msg = Message()
  353. self.assertEqual(msg.get_content_maintype(), 'text')
  354. def test_get_content_maintype_missing_with_default_type(self):
  355. msg = Message()
  356. msg.set_default_type('message/rfc822')
  357. self.assertEqual(msg.get_content_maintype(), 'message')
  358. def test_get_content_maintype_from_message_implicit(self):
  359. msg = self._msgobj('msg_30.txt')
  360. self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message')
  361. def test_get_content_maintype_from_message_explicit(self):
  362. msg = self._msgobj('msg_28.txt')
  363. self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message')
  364. def test_get_content_maintype_from_message_text_plain_implicit(self):
  365. msg = self._msgobj('msg_03.txt')
  366. self.assertEqual(msg.get_content_maintype(), 'text')
  367. def test_get_content_maintype_from_message_text_plain_explicit(self):
  368. msg = self._msgobj('msg_01.txt')
  369. self.assertEqual(msg.get_content_maintype(), 'text')
  370. def test_get_content_subtype_missing(self):
  371. msg = Message()
  372. self.assertEqual(msg.get_content_subtype(), 'plain')
  373. def test_get_content_subtype_missing_with_default_type(self):
  374. msg = Message()
  375. msg.set_default_type('message/rfc822')
  376. self.assertEqual(msg.get_content_subtype(), 'rfc822')
  377. def test_get_content_subtype_from_message_implicit(self):
  378. msg = self._msgobj('msg_30.txt')
  379. self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822')
  380. def test_get_content_subtype_from_message_explicit(self):
  381. msg = self._msgobj('msg_28.txt')
  382. self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822')
  383. def test_get_content_subtype_from_message_text_plain_implicit(self):
  384. msg = self._msgobj('msg_03.txt')
  385. self.assertEqual(msg.get_content_subtype(), 'plain')
  386. def test_get_content_subtype_from_message_text_plain_explicit(self):
  387. msg = self._msgobj('msg_01.txt')
  388. self.assertEqual(msg.get_content_subtype(), 'plain')
  389. def test_get_content_maintype_error(self):
  390. msg = Message()
  391. msg['Content-Type'] = 'no-slash-in-this-string'
  392. self.assertEqual(msg.get_content_maintype(), 'text')
  393. def test_get_content_subtype_error(self):
  394. msg = Message()
  395. msg['Content-Type'] = 'no-slash-in-this-string'
  396. self.assertEqual(msg.get_content_subtype(), 'plain')
  397. def test_replace_header(self):
  398. eq = self.assertEqual
  399. msg = Message()
  400. msg.add_header('First', 'One')
  401. msg.add_header('Second', 'Two')
  402. msg.add_header('Third', 'Three')
  403. eq(msg.keys(), ['First', 'Second', 'Third'])
  404. eq(msg.values(), ['One', 'Two', 'Three'])
  405. msg.replace_header('Second', 'Twenty')
  406. eq(msg.keys(), ['First', 'Second', 'Third'])
  407. eq(msg.values(), ['One', 'Twenty', 'Three'])
  408. msg.add_header('First', 'Eleven')
  409. msg.replace_header('First', 'One Hundred')
  410. eq(msg.keys(), ['First', 'Second', 'Third', 'First'])
  411. eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven'])
  412. self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing')
  413. def test_broken_base64_payload(self):
  414. x = 'AwDp0P7//y6LwKEAcPa/6Q=9'
  415. msg = Message()
  416. msg['content-type'] = 'audio/x-midi'
  417. msg['content-transfer-encoding'] = 'base64'
  418. msg.set_payload(x)
  419. self.assertEqual(msg.get_payload(decode=True), x)
  420. # Test the email.encoders module
  421. class TestEncoders(unittest.TestCase):
  422. def test_encode_empty_payload(self):
  423. eq = self.assertEqual
  424. msg = Message()
  425. msg.set_charset('us-ascii')
  426. eq(msg['content-transfer-encoding'], '7bit')
  427. def test_default_cte(self):
  428. eq = self.assertEqual
  429. msg = MIMEText('hello world')
  430. eq(msg['content-transfer-encoding'], '7bit')
  431. def test_default_cte(self):
  432. eq = self.assertEqual
  433. # With no explicit _charset its us-ascii, and all are 7-bit
  434. msg = MIMEText('hello world')
  435. eq(msg['content-transfer-encoding'], '7bit')
  436. # Similar, but with 8-bit data
  437. msg = MIMEText('hello \xf8 world')
  438. eq(msg['content-transfer-encoding'], '8bit')
  439. # And now with a different charset
  440. msg = MIMEText('hello \xf8 world', _charset='iso-8859-1')
  441. eq(msg['content-transfer-encoding'], 'quoted-printable')
  442. # Test long header wrapping
  443. class TestLongHeaders(TestEmailBase):
  444. def test_split_long_continuation(self):
  445. eq = self.ndiffAssertEqual
  446. msg = email.message_from_string("""\
  447. Subject: bug demonstration
  448. \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
  449. \tmore text
  450. test
  451. """)
  452. sfp = StringIO()
  453. g = Generator(sfp)
  454. g.flatten(msg)
  455. eq(sfp.getvalue(), """\
  456. Subject: bug demonstration
  457. \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
  458. \tmore text
  459. test
  460. """)
  461. def test_another_long_almost_unsplittable_header(self):
  462. eq = self.ndiffAssertEqual
  463. hstr = """\
  464. bug demonstration
  465. \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
  466. \tmore text"""
  467. h = Header(hstr, continuation_ws='\t')
  468. eq(h.encode(), """\
  469. bug demonstration
  470. \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
  471. \tmore text""")
  472. h = Header(hstr)
  473. eq(h.encode(), """\
  474. bug demonstration
  475. 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
  476. more text""")
  477. def test_long_nonstring(self):
  478. eq = self.ndiffAssertEqual
  479. g = Charset("iso-8859-1")
  480. cz = Charset("iso-8859-2")
  481. utf8 = Charset("utf-8")
  482. g_head = "Die Mieter treten hier ein werden mit einem Foerderband komfortabel den Korridor entlang, an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, gegen die rotierenden Klingen bef\xf6rdert. "
  483. cz_head = "Finan\xe8ni metropole se hroutily pod tlakem jejich d\xf9vtipu.. "
  484. utf8_head = u"\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066\u3044\u307e\u3059\u3002".encode("utf-8")
  485. h = Header(g_head, g, header_name='Subject')
  486. h.append(cz_head, cz)
  487. h.append(utf8_head, utf8)
  488. msg = Message()
  489. msg['Subject'] = h
  490. sfp = StringIO()
  491. g = Generator(sfp)
  492. g.flatten(msg)
  493. eq(sfp.getvalue(), """\
  494. Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerd?=
  495. =?iso-8859-1?q?erband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndi?=
  496. =?iso-8859-1?q?schen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Kling?=
  497. =?iso-8859-1?q?en_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_met?=
  498. =?iso-8859-2?q?ropole_se_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?=
  499. =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE?=
  500. =?utf-8?b?44G+44Gb44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB?=
  501. =?utf-8?b?44GC44Go44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CM?=
  502. =?utf-8?q?Wenn_ist_das_Nunstuck_git_und_Slotermeyer=3F_Ja!_Beiherhund_das?=
  503. =?utf-8?b?IE9kZXIgZGllIEZsaXBwZXJ3YWxkdCBnZXJzcHV0LuOAjeOBqOiogOOBow==?=
  504. =?utf-8?b?44Gm44GE44G+44GZ44CC?=
  505. """)
  506. eq(h.encode(), """\
  507. =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerd?=
  508. =?iso-8859-1?q?erband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndi?=
  509. =?iso-8859-1?q?schen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Kling?=
  510. =?iso-8859-1?q?en_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_met?=
  511. =?iso-8859-2?q?ropole_se_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?=
  512. =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE?=
  513. =?utf-8?b?44G+44Gb44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB?=
  514. =?utf-8?b?44GC44Go44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CM?=
  515. =?utf-8?q?Wenn_ist_das_Nunstuck_git_und_Slotermeyer=3F_Ja!_Beiherhund_das?=
  516. =?utf-8?b?IE9kZXIgZGllIEZsaXBwZXJ3YWxkdCBnZXJzcHV0LuOAjeOBqOiogOOBow==?=
  517. =?utf-8?b?44Gm44GE44G+44GZ44CC?=""")
  518. def test_long_header_encode(self):
  519. eq = self.ndiffAssertEqual
  520. h = Header('wasnipoop; giraffes="very-long-necked-animals"; '
  521. 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
  522. header_name='X-Foobar-Spoink-Defrobnit')
  523. eq(h.encode(), '''\
  524. wasnipoop; giraffes="very-long-necked-animals";
  525. spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
  526. def test_long_header_encode_with_tab_continuation(self):
  527. eq = self.ndiffAssertEqual
  528. h = Header('wasnipoop; giraffes="very-long-necked-animals"; '
  529. 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
  530. header_name='X-Foobar-Spoink-Defrobnit',
  531. continuation_ws='\t')
  532. eq(h.encode(), '''\
  533. wasnipoop; giraffes="very-long-necked-animals";
  534. \tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
  535. def test_header_splitter(self):
  536. eq = self.ndiffAssertEqual
  537. msg = MIMEText('')
  538. # It'd be great if we could use add_header() here, but that doesn't
  539. # guarantee an order of the parameters.
  540. msg['X-Foobar-Spoink-Defrobnit'] = (
  541. 'wasnipoop; giraffes="very-long-necked-animals"; '
  542. 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"')
  543. sfp = StringIO()
  544. g = Generator(sfp)
  545. g.flatten(msg)
  546. eq(sfp.getvalue(), '''\
  547. Content-Type: text/plain; charset="us-ascii"
  548. MIME-Version: 1.0
  549. Content-Transfer-Encoding: 7bit
  550. X-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals";
  551. \tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"
  552. ''')
  553. def test_no_semis_header_splitter(self):
  554. eq = self.ndiffAssertEqual
  555. msg = Message()
  556. msg['From'] = 'test@dom.ain'
  557. msg['References'] = SPACE.join(['<%d@dom.ain>' % i for i in range(10)])
  558. msg.set_payload('Test')
  559. sfp = StringIO()
  560. g = Generator(sfp)
  561. g.flatten(msg)
  562. eq(sfp.getvalue(), """\
  563. From: test@dom.ain
  564. References: <0@dom.ain> <1@dom.ain> <2@dom.ain> <3@dom.ain> <4@dom.ain>
  565. \t<5@dom.ain> <6@dom.ain> <7@dom.ain> <8@dom.ain> <9@dom.ain>
  566. Test""")
  567. def test_no_split_long_header(self):
  568. eq = self.ndiffAssertEqual
  569. hstr = 'References: ' + 'x' * 80
  570. h = Header(hstr, continuation_ws='\t')
  571. eq(h.encode(), """\
  572. References: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""")
  573. def test_splitting_multiple_long_lines(self):
  574. eq = self.ndiffAssertEqual
  575. hstr = """\
  576. from 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)
  577. \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)
  578. \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)
  579. """
  580. h = Header(hstr, continuation_ws='\t')
  581. eq(h.encode(), """\
  582. from babylon.socal-raves.org (localhost [127.0.0.1]);
  583. \tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
  584. \tfor <mailman-admin@babylon.socal-raves.org>;
  585. \tSat, 2 Feb 2002 17:00:06 -0800 (PST)
  586. \tfrom babylon.socal-raves.org (localhost [127.0.0.1]);
  587. \tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
  588. \tfor <mailman-admin@babylon.socal-raves.org>;
  589. \tSat, 2 Feb 2002 17:00:06 -0800 (PST)
  590. \tfrom babylon.socal-raves.org (localhost [127.0.0.1]);
  591. \tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
  592. \tfor <mailman-admin@babylon.socal-raves.org>;
  593. \tSat, 2 Feb 2002 17:00:06 -0800 (PST)""")
  594. def test_splitting_first_line_only_is_long(self):
  595. eq = self.ndiffAssertEqual
  596. hstr = """\
  597. from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] helo=cthulhu.gerg.ca)
  598. \tby kronos.mems-exchange.org with esmtp (Exim 4.05)
  599. \tid 17k4h5-00034i-00
  600. \tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400"""
  601. h = Header(hstr, maxlinelen=78, header_name='Received',
  602. continuation_ws='\t')
  603. eq(h.encode(), """\
  604. from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93]
  605. \thelo=cthulhu.gerg.ca)
  606. \tby kronos.mems-exchange.org with esmtp (Exim 4.05)
  607. \tid 17k4h5-00034i-00
  608. \tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400""")
  609. def test_long_8bit_header(self):
  610. eq = self.ndiffAssertEqual
  611. msg = Message()
  612. h = Header('Britische Regierung gibt', 'iso-8859-1',
  613. header_name='Subject')
  614. h.append('gr\xfcnes Licht f\xfcr Offshore-Windkraftprojekte')
  615. msg['Subject'] = h
  616. eq(msg.as_string(), """\
  617. Subject: =?iso-8859-1?q?Britische_Regierung_gibt?= =?iso-8859-1?q?gr=FCnes?=
  618. =?iso-8859-1?q?_Licht_f=FCr_Offshore-Windkraftprojekte?=
  619. """)
  620. def test_long_8bit_header_no_charset(self):
  621. eq = self.ndiffAssertEqual
  622. msg = Message()
  623. msg['Reply-To'] = 'Britische Regierung gibt gr\xfcnes Licht f\xfcr Offshore-Windkraftprojekte <a-very-long-address@example.com>'
  624. eq(msg.as_string(), """\
  625. Reply-To: Britische Regierung gibt gr\xfcnes Licht f\xfcr Offshore-Windkraftprojekte <a-very-long-address@example.com>
  626. """)
  627. def test_long_to_header(self):
  628. eq = self.ndiffAssertEqual
  629. to = '"Someone Test #A" <someone@eecs.umich.edu>,<someone@eecs.umich.edu>,"Someone Test #B" <someone@umich.edu>, "Someone Test #C" <someone@eecs.umich.edu>, "Someone Test #D" <someone@eecs.umich.edu>'
  630. msg = Message()
  631. msg['To'] = to
  632. eq(msg.as_string(0), '''\
  633. To: "Someone Test #A" <someone@eecs.umich.edu>, <someone@eecs.umich.edu>,
  634. \t"Someone Test #B" <someone@umich.edu>,
  635. \t"Someone Test #C" <someone@eecs.umich.edu>,
  636. \t"Someone Test #D" <someone@eecs.umich.edu>
  637. ''')
  638. def test_long_line_after_append(self):
  639. eq = self.ndiffAssertEqual
  640. s = 'This is an example of string which has almost the limit of header length.'
  641. h = Header(s)
  642. h.append('Add another line.')
  643. eq(h.encode(), """\
  644. This is an example of string which has almost the limit of header length.
  645. Add another line.""")
  646. def test_shorter_line_with_append(self):
  647. eq = self.ndiffAssertEqual
  648. s = 'This is a shorter line.'
  649. h = Header(s)
  650. h.append('Add another sentence. (Surprise?)')
  651. eq(h.encode(),
  652. 'This is a shorter line. Add another sentence. (Surprise?)')
  653. def test_long_field_name(self):
  654. eq = self.ndiffAssertEqual
  655. fn = 'X-Very-Very-Very-Long-Header-Name'
  656. gs = "Die Mieter treten hier ein werden mit einem Foerderband komfortabel den Korridor entlang, an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, gegen die rotierenden Klingen bef\xf6rdert. "
  657. h = Header(gs, 'iso-8859-1', header_name=fn)
  658. # BAW: this seems broken because the first line is too long
  659. eq(h.encode(), """\
  660. =?iso-8859-1?q?Die_Mieter_treten_hier_?=
  661. =?iso-8859-1?q?ein_werden_mit_einem_Foerderband_komfortabel_den_Korridor_?=
  662. =?iso-8859-1?q?entlang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei=2C_g?=
  663. =?iso-8859-1?q?egen_die_rotierenden_Klingen_bef=F6rdert=2E_?=""")
  664. def test_long_received_header(self):
  665. h = 'from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; Wed, 05 Mar 2003 18:10:18 -0700'
  666. msg = Message()
  667. msg['Received-1'] = Header(h, continuation_ws='\t')
  668. msg['Received-2'] = h
  669. self.assertEqual(msg.as_string(), """\
  670. Received-1: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by
  671. \throthgar.la.mastaler.com (tmda-ofmipd) with ESMTP;
  672. \tWed, 05 Mar 2003 18:10:18 -0700
  673. Received-2: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by
  674. \throthgar.la.mastaler.com (tmda-ofmipd) with ESMTP;
  675. \tWed, 05 Mar 2003 18:10:18 -0700
  676. """)
  677. def test_string_headerinst_eq(self):
  678. h = '<15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David Bremner\'s message of "Thu, 6 Mar 2003 13:58:21 +0100")'
  679. msg = Message()
  680. msg['Received-1'] = Header(h, header_name='Received-1',
  681. continuation_ws='\t')
  682. msg['Received-2'] = h
  683. self.assertEqual(msg.as_string(), """\
  684. Received-1: <15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de>
  685. \t(David Bremner's message of "Thu, 6 Mar 2003 13:58:21 +0100")
  686. Received-2: <15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de>
  687. \t(David Bremner's message of "Thu, 6 Mar 2003 13:58:21 +0100")
  688. """)
  689. def test_long_unbreakable_lines_with_continuation(self):
  690. eq = self.ndiffAssertEqual
  691. msg = Message()
  692. t = """\
  693. iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9
  694. locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp"""
  695. msg['Face-1'] = t
  696. msg['Face-2'] = Header(t, header_name='Face-2')
  697. eq(msg.as_string(), """\
  698. Face-1: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9
  699. \tlocQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp
  700. Face-2: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9
  701. locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp
  702. """)
  703. def test_another_long_multiline_header(self):
  704. eq = self.ndiffAssertEqual
  705. m = '''\
  706. Received: from siimage.com ([172.25.1.3]) by zima.siliconimage.com with Microsoft SMTPSVC(5.0.2195.4905);
  707. \tWed, 16 Oct 2002 07:41:11 -0700'''
  708. msg = email.message_from_string(m)
  709. eq(msg.as_string(), '''\
  710. Received: from siimage.com ([172.25.1.3]) by zima.siliconimage.com with
  711. \tMicrosoft SMTPSVC(5.0.2195.4905); Wed, 16 Oct 2002 07:41:11 -0700
  712. ''')
  713. def test_long_lines_with_different_header(self):
  714. eq = self.ndiffAssertEqual
  715. h = """\
  716. List-Unsubscribe: <https://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,
  717. <mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe>"""
  718. msg = Message()
  719. msg['List'] = h
  720. msg['List'] = Header(h, header_name='List')
  721. eq(msg.as_string(), """\
  722. List: List-Unsubscribe: <https://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,
  723. \t<mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe>
  724. List: List-Unsubscribe: <https://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,
  725. <mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe>
  726. """)
  727. # Test mangling of "From " lines in the body of a message
  728. class TestFromMangling(unittest.TestCase):
  729. def setUp(self):
  730. self.msg = Message()
  731. self.msg['From'] = 'aaa@bbb.org'
  732. self.msg.set_payload("""\
  733. From the desk of A.A.A.:
  734. Blah blah blah
  735. """)
  736. def test_mangled_from(self):
  737. s = StringIO()
  738. g = Generator(s, mangle_from_=True)
  739. g.flatten(self.msg)
  740. self.assertEqual(s.getvalue(), """\
  741. From: aaa@bbb.org
  742. >From the desk of A.A.A.:
  743. Blah blah blah
  744. """)
  745. def test_dont_mangle_from(self):
  746. s = StringIO()
  747. g = Generator(s, mangle_from_=False)
  748. g.flatten(self.msg)
  749. self.assertEqual(s.getvalue(), """\
  750. From: aaa@bbb.org
  751. From the desk of A.A.A.:
  752. Blah blah blah
  753. """)
  754. # Test the basic MIMEAudio class
  755. class TestMIMEAudio(unittest.TestCase):
  756. def setUp(self):
  757. # Make sure we pick up the audiotest.au that lives in email/test/data.
  758. # In Python, there's an audiotest.au living in Lib/test but that isn't
  759. # included in some binary distros that don't include the test
  760. # package. The trailing empty string on the .join() is significant
  761. # since findfile() will do a dirname().
  762. datadir = os.path.join(os.path.dirname(landmark), 'data', '')
  763. fp = open(findfile('audiotest.au', datadir), 'rb')
  764. try:
  765. self._audiodata = fp.read()
  766. finally:
  767. fp.close()
  768. self._au = MIMEAudio(self._audiodata)
  769. def test_guess_minor_type(self):
  770. self.assertEqual(self._au.get_content_type(), 'audio/basic')
  771. def test_encoding(self):
  772. payload = self._au.get_payload()
  773. self.assertEqual(base64.decodestring(payload), self._audiodata)
  774. def test_checkSetMinor(self):
  775. au = MIMEAudio(self._audiodata, 'fish')
  776. self.assertEqual(au.get_content_type(), 'audio/fish')
  777. def test_add_header(self):
  778. eq = self.assertEqual
  779. unless = self.failUnless
  780. self._au.add_header('Content-Disposition', 'attachment',
  781. filename='audiotest.au')
  782. eq(self._au['content-disposition'],
  783. 'attachment; filename="audiotest.au"')
  784. eq(self._au.get_params(header='content-disposition'),
  785. [('attachment', ''), ('filename', 'audiotest.au')])
  786. eq(self._au.get_param('filename', header='content-disposition'),
  787. 'audiotest.au')
  788. missing = []
  789. eq(self._au.get_param('attachment', header='content-disposition'), '')
  790. unless(self._au.get_param('foo', failobj=missing,
  791. header='content-disposition') is missing)
  792. # Try some missing stuff
  793. unless(self._au.get_param('foobar', missing) is missing)
  794. unless(self._au.get_param('attachment', missing,
  795. header='foobar') is missing)
  796. # Test the basic MIMEImage class
  797. class TestMIMEImage(unittest.TestCase):
  798. def setUp(self):
  799. fp = openfile('PyBanner048.gif')
  800. try:
  801. self._imgdata = fp.read()
  802. finally:
  803. fp.close()
  804. self._im = MIMEImage(self._imgdata)
  805. def test_guess_minor_type(self):
  806. self.assertEqual(self._im.get_content_type(), 'image/gif')
  807. def test_encoding(self):
  808. payload = self._im.get_payload()
  809. self.assertEqual(base64.decodestring(payload), self._imgdata)
  810. def test_checkSetMinor(self):
  811. im = MIMEImage(self._imgdata, 'fish')
  812. self.assertEqual(im.get_content_type(), 'image/fish')
  813. def test_add_header(self):
  814. eq = self.assertEqual
  815. unless = self.failUnless
  816. self._im.add_header('Content-Disposition', 'attachment',
  817. filename='dingusfish.gif')
  818. eq(self._im['content-disposition'],
  819. 'attachment; filename="dingusfish.gif"')
  820. eq(self._im.get_params(header='content-disposition'),
  821. [('attachment', ''), ('filename', 'dingusfish.gif')])
  822. eq(self._im.get_param('filename', header='content-disposition'),
  823. 'dingusfish.gif')
  824. missing = []
  825. eq(self._im.get_param('attachment', header='content-disposition'), '')
  826. unless(self._im.get_param('foo', failobj=missing,
  827. header='content-disposition') is missing)
  828. # Try some missing stuff
  829. unless(self._im.get_param('foobar', missing) is missing)
  830. unless(self._im.get_param('attachment', missing,
  831. header='foobar') is missing)
  832. # Test the basic MIMEApplication class
  833. class TestMIMEApplication(unittest.TestCase):
  834. def test_headers(self):
  835. eq = self.assertEqual
  836. msg = MIMEApplication('\xfa\xfb\xfc\xfd\xfe\xff')
  837. eq(msg.get_content_type(), 'application/octet-stream')
  838. eq(msg['content-transfer-encoding'], 'base64')
  839. def test_body(self):
  840. eq = self.assertEqual
  841. bytes = '\xfa\xfb\xfc\xfd\xfe\xff'
  842. msg = MIMEApplication(bytes)
  843. eq(msg.get_payload(), '+vv8/f7/')
  844. eq(msg.get_payload(decode=True), bytes)
  845. # Test the basic MIMEText class
  846. class TestMIMEText(unittest.TestCase):
  847. def setUp(self):
  848. self._msg = MIMEText('hello there')
  849. def test_types(self):
  850. eq = self.assertEqual
  851. unless = self.failUnless
  852. eq(self._msg.get_content_type(), 'text/plain')
  853. eq(self._msg.get_param('charset'), 'us-ascii')
  854. missing = []
  855. unless(self._msg.get_param('foobar', missing) is missing)
  856. unless(self._msg.get_param('charset', missing, header='foobar')
  857. is missing)
  858. def test_payload(self):
  859. self.assertEqual(self._msg.get_payload(), 'hello there')
  860. self.failUnless(not self._msg.is_multipart())
  861. def test_charset(self):
  862. eq = self.assertEqual
  863. msg = MIMEText('hello there', _charset='us-ascii')
  864. eq(msg.get_charset().input_charset, 'us-ascii')
  865. eq(msg['content-type'], 'text/plain; charset="us-ascii"')
  866. # Test complicated multipart/* messages
  867. class TestMultipart(TestEmailBase):
  868. def setUp(self):
  869. fp = openfile('PyBanner048.gif')
  870. try:
  871. data = fp.read()
  872. finally:
  873. fp.close()
  874. container = MIMEBase('multipart', 'mixed', boundary='BOUNDARY')
  875. image = MIMEImage(data, name='dingusfish.gif')
  876. image.add_header('content-disposition', 'attachment',
  877. filename='dingusfish.gif')
  878. intro = MIMEText('''\
  879. Hi there,
  880. This is the dingus fish.
  881. ''')
  882. container.attach(intro)
  883. container.attach(image)
  884. container['From'] = 'Barry <barry@digicool.com>'
  885. container['To'] = 'Dingus Lovers <cravindogs@cravindogs.com>'
  886. container['Subject'] = 'Here is your dingus fish'
  887. now = 987809702.54848599
  888. timetuple = time.localtime(now)
  889. if timetuple[-1] == 0:
  890. tzsecs = time.timezone
  891. else:
  892. tzsecs = time.altzone
  893. if tzsecs > 0:
  894. sign = '-'
  895. else:
  896. sign = '+'
  897. tzoffset = ' %s%04d' % (sign, tzsecs / 36)
  898. container['Date'] = time.strftime(
  899. '%a, %d %b %Y %H:%M:%S',
  900. time.localtime(now)) + tzoffset
  901. self._msg = container
  902. self._im = image
  903. self._txt = intro
  904. def test_hierarchy(self):
  905. # convenience
  906. eq = self.assertEqual
  907. unless = self.failUnless
  908. raises = self.assertRaises
  909. # tests
  910. m = self._msg
  911. unless(m.is_multipart())
  912. eq(m.get_content_type(), 'multipart/mixed')
  913. eq(len(m.get_payload()), 2)
  914. raises(IndexError, m.get_payload, 2)
  915. m0 = m.get_payload(0)
  916. m1 = m.get_payload(1)
  917. unless(m0 is self._txt)
  918. unless(m1 is self._im)
  919. eq(m.get_payload(), [m0, m1])
  920. unless(not m0.is_multipart())
  921. unless(not m1.is_multipart())
  922. def test_empty_multipart_idempotent(self):
  923. text = """\
  924. Content-Type: multipart/mixed; boundary="BOUNDARY"
  925. MIME-Version: 1.0
  926. Subject: A subject
  927. To: aperson@dom.ain
  928. From: bperson@dom.ain
  929. --BOUNDARY
  930. --BOUNDARY--
  931. """
  932. msg = Parser().parsestr(text)
  933. self.ndiffAssertEqual(text, msg.as_string())
  934. def test_no_parts_in_a_multipart_with_none_epilogue(self):
  935. outer = MIMEBase('multipart', 'mixed')
  936. outer['Subject'] = 'A subject'
  937. outer['To'] = 'aperson@dom.ain'
  938. outer['From'] = 'bperson@dom.ain'
  939. outer.set_boundary('BOUNDARY')
  940. self.ndiffAssertEqual(outer.as_string(), '''\
  941. Content-Type: multipart/mixed; boundary="BOUNDARY"
  942. MIME-Version: 1.0
  943. Subject: A subject
  944. To: aperson@dom.ain
  945. From: bperson@dom.ain
  946. --BOUNDARY
  947. --BOUNDARY--''')
  948. def test_no_parts_in_a_multipart_with_empty_epilogue(self):
  949. outer = MIMEBase('multipart', 'mixed')
  950. outer['Subject'] = 'A subject'
  951. outer['To'] = 'aperson@dom.ain'
  952. outer['From'] = 'bperson@dom.ain'
  953. outer.preamble = ''
  954. outer.epilogue = ''
  955. outer.set_boundary('BOUNDARY')
  956. self.ndiffAssertEqual(outer.as_string(), '''\
  957. Content-Type: multipart/mixed; boundary="BOUNDARY"
  958. MIME-Version: 1.0
  959. Subject: A subject
  960. To: aperson@dom.ain
  961. From: bperson@dom.ain
  962. --BOUNDARY
  963. --BOUNDARY--
  964. ''')
  965. def test_one_part_in_a_multipart(self):
  966. eq = self.ndiffAssertEqual
  967. outer = MIMEBase('multipart', 'mixed')
  968. outer['Subject'] = 'A subject'
  969. outer['To'] = 'aperson@dom.ain'
  970. outer['From'] = 'bperson@dom.ain'
  971. outer.set_boundary('BOUNDARY')
  972. msg = MIMEText('hello world')
  973. outer.attach(msg)
  974. eq(outer.as_string(), '''\
  975. Content-Type: multipart/mixed; boundary="BOUNDARY"
  976. MIME-Version: 1.0
  977. Subject: A subject
  978. To: aperson@dom.ain
  979. From: bperson@dom.ain
  980. --BOUNDARY
  981. Content-Type: text/plain; charset="us-ascii"
  982. MIME-Version: 1.0
  983. Content-Transfer-Encoding: 7bit
  984. hello world
  985. --BOUNDARY--''')
  986. def test_seq_parts_in_a_multipart_with_empty_preamble(self):
  987. eq = self.ndiffAssertEqual
  988. outer = MIMEBase('multipart', 'mixed')
  989. outer['Subject'] = 'A subject'
  990. outer['To'] = 'aperson@dom.ain'
  991. outer['From'] = 'bperson@dom.ain'
  992. outer.preamble = ''
  993. msg = MIMEText('hello world')
  994. outer.attach(msg)
  995. outer.set_boundary('BOUNDARY')
  996. eq(outer.as_string(), '''\
  997. Content-Type: multipart/mixed; boundary="BOUNDARY"
  998. MIME-Version: 1.0
  999. Subject: A subject
  1000. To: aperson@dom.ain
  1001. From: bperson@dom.ain
  1002. --BOUNDARY
  1003. Content-Type: text/plain; charset="us-ascii"
  1004. MIME-Version: 1.0
  1005. Content-Transfer-Encoding: 7bit
  1006. hello world
  1007. --BOUNDARY--''')
  1008. def test_seq_parts_in_a_multipart_with_none_preamble(self):
  1009. eq = self.ndiffAssertEqual
  1010. outer = MIMEBase('multipart', 'mixed')
  1011. outer['Subject'] = 'A subject'
  1012. outer['To'] = 'aperson@dom.ain'
  1013. outer['From'] = 'bperson@dom.ain'
  1014. outer.preamble = None
  1015. msg = MIMEText('hello world')
  1016. outer.attach(msg)
  1017. outer.set_boundary('BOUNDARY')
  1018. eq(outer.as_string(), '''\
  1019. Content-Type: multipart/mixed; boundary="BOUNDARY"
  1020. MIME-Version: 1.0
  1021. Subject: A subject
  1022. To: aperson@dom.ain
  1023. From: bperson@dom.ain
  1024. --BOUNDARY
  1025. Content-Type: text/plain; charset="us-ascii"
  1026. MIME-Version: 1.0
  1027. Content-Transfer-Encoding: 7bit
  1028. hello world
  1029. --BOUNDARY--''')
  1030. def test_seq_parts_in_a_multipart_with_none_epilogue(self):
  1031. eq = self.ndiffAssertEqual
  1032. outer = MIMEBase('multipart', 'mixed')
  1033. outer['Subject'] = 'A subject'
  1034. outer['To'] = 'aperson@dom.ain'
  1035. outer['From'] = 'bperson@dom.ain'
  1036. outer.epilogue = None
  1037. msg = MIMEText('hello world')
  1038. outer.attach(msg)
  1039. outer.set_boundary('BOUNDARY')
  1040. eq(outer.as_string(), '''\
  1041. Content-Type: multipart/mixed; boundary="BOUNDARY"
  1042. MIME-Version: 1.0
  1043. Subject: A subject
  1044. To: aperson@dom.ain
  1045. From: bperson@dom.ain
  1046. --BOUNDARY
  1047. Content-Type: text/plain; charset="us-ascii"
  1048. MIME-Version: 1.0
  1049. Content-Transfer-Encoding: 7bit
  1050. hello world
  1051. --BOUNDARY--''')
  1052. def test_seq_parts_in_a_multipart_with_empty_epilogue(self):
  1053. eq = self.ndiffAssertEqual
  1054. outer = MIMEBase('multipart', 'mixed')
  1055. outer['Subject'] = 'A subject'
  1056. outer['To'] = 'aperson@dom.ain'
  1057. outer['From'] = 'bperson@dom.ain'
  1058. outer.epilogue = ''
  1059. msg = MIMEText('hello world')
  1060. outer.attach(msg)
  1061. outer.set_boundary('BOUNDARY')
  1062. eq(outer.as_string(), '''\
  1063. Content-Type: multipart/mixed; boundary="BOUNDARY"
  1064. MIME-Version: 1.0
  1065. Subject: A subject
  1066. To: aperson@dom.ain
  1067. From: bperson@dom.ain
  1068. --BOUNDARY
  1069. Content-Type: text/plain; charset="us-ascii"
  1070. MIME-Version: 1.0
  1071. Content-Transfer-Encoding: 7bit
  1072. hello world
  1073. --BOUNDARY--
  1074. ''')
  1075. def test_seq_parts_in_a_multipart_with_nl_epilogue(self):
  1076. eq = self.ndiffAssertEqual
  1077. outer = MIMEBase('multipart', 'mixed')
  1078. outer['Subject'] = 'A subject'
  1079. outer['To'] = 'aperson@dom.ain'
  1080. outer['From'] = 'bperson@dom.ain'
  1081. outer.epilogue = '\n'
  1082. msg = MIMEText('hello world')
  1083. outer.attach(msg)
  1084. outer.set_boundary('BOUNDARY')
  1085. eq(outer.as_string(), '''\
  1086. Content-Type: multipart/mixed; boundary="BOUNDARY"
  1087. MIME-Version: 1.0
  1088. Subject: A subject
  1089. To: aperson@dom.ain
  1090. From: bperson@dom.ain
  1091. --BOUNDARY
  1092. Content-Type: text/plain; charset="us-ascii"
  1093. MIME-Version: 1.0
  1094. Content-Transfer-Encoding: 7bit
  1095. hello world
  1096. --BOUNDARY--
  1097. ''')
  1098. def test_message_external_body(self):
  1099. eq = self.assertEqual
  1100. msg = self._msgobj('msg_36.txt')
  1101. eq(len(msg.get_payload()), 2)
  1102. msg1 = msg.get_payload(1)
  1103. eq(msg1.get_content_type(), 'multipart/alternative')
  1104. eq(len(msg1.get_payload()), 2)
  1105. for subpart in msg1.get_payload():
  1106. eq(subpart.get_content_type(), 'message/external-body')
  1107. eq(len(subpart.get_payload()), 1)
  1108. subsubpart = subpart.get_payload(0)
  1109. eq(subsubpart.get_content_type(), 'text/plain')
  1110. def test_double_boundary(self):
  1111. # msg_37.txt is a multipart that contains two dash-boundary's in a
  1112. # row. Our interpretation of RFC 2046 calls for ignoring the second
  1113. # and subsequent boundaries.
  1114. msg = self._msgobj('msg_37.txt')
  1115. self.assertEqual(len(msg.get_payload()), 3)
  1116. def test_nested_inner_contains_outer_boundary(self):
  1117. eq = self.ndiffAssertEqual
  1118. # msg_38.txt has an inner part that contains outer boundaries. My
  1119. # interpretation of RFC 2046 (based on sections 5.1 and 5.1.2) say
  1120. # these are illegal and should be interpreted as unterminated inner
  1121. # parts.
  1122. msg = self._msgobj('msg_38.txt')
  1123. sfp = StringIO()
  1124. iterators._structure(msg, sfp)
  1125. eq(sfp.getvalue(), """\
  1126. multipart/mixed
  1127. multipart/mixed
  1128. multipart/alternative
  1129. text/plain
  1130. text/plain
  1131. text/plain
  1132. text/plain
  1133. """)
  1134. def test_nested_with_same_boundary(self):
  1135. eq = self.ndiffAssertEqual
  1136. # msg 39.txt is similarly evil in that it's got inner parts that use
  1137. # the same boundary as outer parts. Again, I believe the way this is
  1138. # parsed is closest to the spirit of RFC 2046
  1139. msg = self._msgobj('msg_39.txt')
  1140. sfp = StringIO()
  1141. iterators._structure(msg, sfp)
  1142. eq(sfp.getvalue(), """\
  1143. multipart/mixed
  1144. multipart/mixed
  1145. multipart/alternative
  1146. application/octet-stream
  1147. application/octet-stream
  1148. text/plain
  1149. """)
  1150. def test_boundary_in_non_multipart(self):
  1151. msg = self._msgobj('msg_40.txt')
  1152. self.assertEqual(msg.as_string(), '''\
  1153. MIME-Version: 1.0
  1154. Content-Type: text/html; boundary="--961284236552522269"
  1155. ----961284236552522269
  1156. Content-Type: text/html;
  1157. Content-Transfer-Encoding: 7Bit
  1158. <html></html>
  1159. ----961284236552522269--
  1160. ''')
  1161. def test_boundary_with_leading_space(self):
  1162. eq = self.assertEqual
  1163. msg = email.message_from_string('''\
  1164. MIME-Version: 1.0
  1165. Content-Type: multipart/mixed; boundary=" XXXX"
  1166. -- XXXX
  1167. Content-Type: text/plain
  1168. -- XXXX
  1169. Content-Type: text/plain
  1170. -- XXXX--
  1171. ''')
  1172. self.failUnless(msg.is_multipart())
  1173. eq(msg.get_boundary(), ' XXXX')
  1174. eq(len(msg.get_payload()), 2)
  1175. def test_boundary_without_trailing_newline(self):
  1176. m = Parser().parsestr("""\
  1177. Content-Type: multipart/mixed; boundary="===============0012394164=="
  1178. MIME-Version: 1.0
  1179. --===============0012394164==
  1180. Content-Type: image/file1.jpg
  1181. MIME-Version: 1.0
  1182. Content-Transfer-Encoding: base64
  1183. YXNkZg==
  1184. --===============0012394164==--""")
  1185. self.assertEquals(m.get_payload(0).get_payload(), 'YXNkZg==')
  1186. # Test some badly formatted messages
  1187. class TestNonConformant(TestEmailBase):
  1188. def test_parse_missing_minor_type(self):
  1189. eq = self.assertEqual
  1190. msg = self._msgobj('msg_14.txt')
  1191. eq(msg.get_content_type(), 'text/plain')
  1192. eq(msg.get_content_maintype(), 'text')
  1193. eq(msg.get_content_subtype(), 'plain')
  1194. def test_same_boundary_inner_outer(self):
  1195. unless = self.failUnless
  1196. msg = self._msgobj('msg_15.txt')
  1197. # XXX We can probably eventually do better
  1198. inner = msg.get_payload(0)
  1199. unless(hasattr(inner, 'defects'))
  1200. self.assertEqual(len(inner.defects), 1)
  1201. unless(isinstance(inner.defects[0],
  1202. errors.StartBoundaryNotFoundDefect))
  1203. def test_multipart_no_boundary(self):
  1204. unless = self.failUnless
  1205. msg = self._msgobj('msg_25.txt')
  1206. unless(isinstance(msg.get_payload(), str))
  1207. self.assertEqual(len(msg.defects), 2)
  1208. unless(isinstance(msg.defects[0], errors.NoBoundaryInMultipartDefect))
  1209. unless(isinstance(msg.defects[1],
  1210. errors.MultipartInvariantViolationDefect))
  1211. def test_invalid_content_type(self):
  1212. eq = self.assertEqual
  1213. neq = self.ndiffAssertEqual
  1214. msg = Message()
  1215. # RFC 2045, $5.2 says invalid yields text/plain
  1216. msg['Content-Type'] = 'text'
  1217. eq(msg.get_content_maintype(), 'text')
  1218. eq(msg.get_content_subtype(), 'plain')
  1219. eq(msg.get_content_type(), 'text/plain')
  1220. # Clear the old value and try something /really/ invalid
  1221. del msg['content-type']
  1222. msg['Content-Type'] = 'foo'
  1223. eq(msg.get_content_maintype(), 'text')
  1224. eq(msg.get_content_subtype(), 'plain')
  1225. eq(msg.get_content_type(), 'text/plain')
  1226. # Still, make sure that the message is idempotently generated
  1227. s = StringIO()
  1228. g = Generator(s)
  1229. g.flatten(msg)
  1230. neq(s.getvalue(), 'Content-Type: foo\n\n')
  1231. def test_no_start_boundary(self):
  1232. eq = self.ndiffAssertEqual
  1233. msg = self._msgobj('msg_31.txt')
  1234. eq(msg.get_payload(), """\
  1235. --BOUNDARY
  1236. Content-Type: text/plain
  1237. message 1
  1238. --BOUNDARY
  1239. Content-Type: text/plain
  1240. message 2
  1241. --BOUNDARY--
  1242. """)
  1243. def test_no_separating_blank_line(self):
  1244. eq = self.ndiffAssertEqual
  1245. msg = self._msgobj('msg_35.txt')
  1246. eq(msg.as_string(), """\
  1247. From: aperson@dom.ain
  1248. To: bperson@dom.ain
  1249. Subject: here's something interesting
  1250. counter to RFC 2822, there's no separating newline here
  1251. """)
  1252. def test_lying_multipart(self):
  1253. unless = self.failUnless
  1254. msg = self._msgobj('msg_41.txt')
  1255. unless(hasattr(msg, 'defects'))
  1256. self.assertEqual(len(msg.defects), 2)
  1257. unless(isinstance(msg.defects[0], errors.NoBoundaryInMultipartDefect))
  1258. unless(isinstance(msg.defects[1],
  1259. errors.MultipartInvariantViolationDefect))
  1260. def test_missing_start_boundary(self):
  1261. outer = self._msgobj('msg_42.txt')
  1262. # The message structure is:
  1263. #
  1264. # multipart/mixed
  1265. # text/plain
  1266. # message/rfc822
  1267. # multipart/mixed [*]
  1268. #
  1269. # [*] This message is missing its start boundary
  1270. bad = outer.get_payload(1).get_payload(0)
  1271. self.assertEqual(len(bad.defects), 1)
  1272. self.failUnless(isinstance(bad.defects[0],
  1273. errors.StartBoundaryNotFoundDefect))
  1274. def test_first_line_is_continuation_header(self):
  1275. eq = self.assertEqual
  1276. m = ' Line 1\nLine 2\nLine 3'
  1277. msg = email.message_from_string(m)
  1278. eq(msg.keys(), [])
  1279. eq(msg.get_payload(), 'Line 2\nLine 3')
  1280. eq(len(msg.defects), 1)
  1281. self.failUnless(isinstance(msg.defects[0],
  1282. errors.FirstHeaderLineIsContinuationDefect))
  1283. eq(msg.defects[0].line, ' Line 1\n')
  1284. # Test RFC 2047 header encoding and decoding
  1285. class TestRFC2047(unittest.TestCase):
  1286. def test_rfc2047_multiline(self):
  1287. eq = self.assertEqual
  1288. s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz
  1289. foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?="""
  1290. dh = decode_header(s)
  1291. eq(dh, [
  1292. ('Re:', None),
  1293. ('r\x8aksm\x9arg\x8cs', 'mac-iceland'),
  1294. ('baz foo bar', None),
  1295. ('r\x8aksm\x9arg\x8cs', 'mac-iceland')])
  1296. eq(str(make_header(dh)),
  1297. """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar
  1298. =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""")
  1299. def test_whitespace_eater_unicode(self):
  1300. eq = self.assertEqual
  1301. s = '=?ISO-8859-1?Q?Andr=E9?= Pirard <pirard@dom.ain>'
  1302. dh = decode_header(s)
  1303. eq(dh, [('Andr\xe9', 'iso-8859-1'), ('Pirard <pirard@dom.ain>', None)])
  1304. hu = unicode(make_header(dh)).encode('latin-1')
  1305. eq(hu, 'Andr\xe9 Pirard <pirard@dom.ain>')
  1306. def test_whitespace_eater_unicode_2(self):
  1307. eq = self.assertEqual
  1308. s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?='
  1309. dh = decode_header(s)
  1310. eq(dh, [('The', None), ('quick brown fox', 'iso-8859-1'),
  1311. ('jumped over the', None), ('lazy dog', 'iso-8859-1')])
  1312. hu = make_header(dh).__unicode__()
  1313. eq(hu, u'The quick brown fox jumped over the lazy dog')
  1314. def test_rfc2047_missing_whitespace(self):
  1315. s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord'
  1316. dh = decode_header(s)
  1317. self.assertEqual(dh, [(s, None)])
  1318. def test_rfc2047_with_whitespace(self):
  1319. s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord'
  1320. dh = decode_header(s)
  1321. self.assertEqual(dh, [('Sm', None), ('\xf6', 'iso-8859-1'),
  1322. ('rg', None), ('\xe5', 'iso-8859-1'),
  1323. ('sbord', None)])
  1324. # Test the MIMEMessage class
  1325. class TestMIMEMessage(TestEmailBase):
  1326. def setUp(self):
  1327. fp = openfile('msg_11.txt')
  1328. try:
  1329. self._text = fp.read()
  1330. finally:
  1331. fp.close()
  1332. def test_type_error(self):
  1333. self.assertRaises(TypeError, MIMEMessage, 'a plain string')
  1334. def test_valid_argument(self):
  1335. eq = self.assertEqual
  1336. unless = self.failUnless
  1337. subject = 'A sub-message'
  1338. m = Message()
  1339. m['Subject'] = subject
  1340. r = MIMEMessage(m)
  1341. eq(r.get_content_type(), 'message/rfc822')
  1342. payload = r.get_payload()
  1343. unless(isinstance(payload, list))
  1344. eq(len(payload), 1)
  1345. subpart = payload[0]
  1346. unless(subpart is m)
  1347. eq(subpart['subject'], subject)
  1348. def test_bad_multipart(self):
  1349. eq = self.assertEqual
  1350. msg1 = Message()
  1351. msg1['Subject'] = 'subpart 1'
  1352. msg2 = Message()
  1353. msg2['Subject'] = 'subpart 2'
  1354. r = MIMEMessage(msg1)
  1355. self.assertRaises(errors.MultipartConversionError, r.attach, msg2)
  1356. def test_generate(self):
  1357. # First craft the message to be encapsulated
  1358. m = Message()
  1359. m['Subject'] = 'An enclosed message'
  1360. m.set_payload('Here is the body of the message.\n')
  1361. r = MIMEMessage(m)
  1362. r['Subject'] = 'The enclosing message'
  1363. s = StringIO()
  1364. g = Generator(s)
  1365. g.flatten(r)
  1366. self.assertEqual(s.getvalue(), """\
  1367. Content-Type: message/rfc822
  1368. MIME-Version: 1.0
  1369. Subject: The enclosing message
  1370. Subject: An enclosed message
  1371. Here is the body of the message.
  1372. """)
  1373. def test_parse_message_rfc822(self):
  1374. eq = self.assertEqual
  1375. unless = self.failUnless
  1376. msg = self._msgobj('msg_11.txt')
  1377. eq(msg.get_content_type(), 'message/rfc822')
  1378. payload = msg.get_payload()
  1379. unless(isinstance(payload, list))
  1380. eq(len(payload), 1)
  1381. submsg = payload[0]
  1382. self.failUnless(isinstance(submsg, Message))
  1383. eq(submsg['subject'], 'An enclosed message')
  1384. eq(submsg.get_payload(), 'Here is the body of the message.\n')
  1385. def test_dsn(self):
  1386. eq = self.assertEqual
  1387. unless = self.failUnless
  1388. # msg 16 is a Delivery Status Notification, see RFC 1894
  1389. msg = self._msgobj('msg_16.txt')
  1390. eq(msg.get_content_type(), 'multipart/report')
  1391. unless(msg.is_multipart())
  1392. eq(len(msg.get_payload()), 3)
  1393. # Subpart 1 is a text/plain, human readable section
  1394. subpart = msg.get_payload(0)
  1395. eq(subpart.get_content_type(), 'text/plain')
  1396. eq(subpart.get_payload(), """\
  1397. This report relates to a message you sent with the following header fields:
  1398. Message-id: <002001c144a6$8752e060$56104586@oxy.edu>
  1399. Date: Sun, 23 Sep 2001 20:10:55 -0700
  1400. From: "Ian T. Henry" <henryi@oxy.edu>
  1401. To: SoCal Raves <scr@socal-raves.org>
  1402. Subject: [scr] yeah for Ians!!
  1403. Your message cannot be delivered to the following recipients:
  1404. Recipient address: jangel1@cougar.noc.ucla.edu
  1405. Reason: recipient reached disk quota
  1406. """)
  1407. # Subpart 2 contains the machine parsable DSN information. It
  1408. # consists of two blocks of headers, represented by two nested Message
  1409. # objects.
  1410. subpart = msg.get_payload(1)
  1411. eq(subpart.get_content_type(), 'message/delivery-status')
  1412. eq(len(subpart.get_payload()), 2)
  1413. # message/delivery-status should treat each block as a bunch of
  1414. # headers, i.e. a bunch of Message objects.
  1415. dsn1 = subpart.get_payload(0)
  1416. unless(isinstance(dsn1, Message))
  1417. eq(dsn1['original-envelope-id'], '0GK500B4HD0888@cougar.noc.ucla.edu')
  1418. eq(dsn1.get_param('dns', header='reporting-mta'), '')
  1419. # Try a missing one <wink>
  1420. eq(dsn1.get_param('nsd', header='reporting-mta'), None)
  1421. dsn2 = subpart.get_payload(1)
  1422. unless(isinstance(dsn2, Message))
  1423. eq(dsn2['action'], 'failed')
  1424. eq(dsn2.get_params(header='original-recipient'),
  1425. [('rfc822', ''), ('jangel1@cougar.noc.ucla.edu', '')])
  1426. eq(dsn2.get_param('rfc822', header='final-recipient'), '')
  1427. # Subpart 3 is the original message
  1428. subpart = msg.get_payload(2)
  1429. eq(subpart.get_content_type(), 'message/rfc822')
  1430. payload = subpart.get_payload()
  1431. unless(isinstance(payload, list))
  1432. eq(len(payload), 1)
  1433. subsubpart = payload[0]
  1434. unless(isinstance(subsubpart, Message))
  1435. eq(subsubpart.get_content_type(), 'text/plain')
  1436. eq(subsubpart['message-id'],
  1437. '<002001c144a6$8752e060$56104586@oxy.edu>')
  1438. def test_epilogue(self):
  1439. eq = self.ndiffAssertEqual
  1440. fp = openfile('msg_21.txt')
  1441. try:
  1442. text = fp.read()
  1443. finally:
  1444. fp.close()
  1445. msg = Message()
  1446. msg['From'] = 'aperson@dom.ain'
  1447. msg['To'] = 'bperson@dom.ain'
  1448. msg['Subject'] = 'Test'
  1449. msg.preamble = 'MIME message'
  1450. msg.epilogue = 'End of MIME message\n'
  1451. msg1 = MIMEText('One')
  1452. msg2 = MIMEText('Two')
  1453. msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY')
  1454. msg.attach(msg1)
  1455. msg.attach(msg2)
  1456. sfp = StringIO()
  1457. g = Generator(sfp)
  1458. g.flatten(msg)
  1459. eq(sfp.getvalue(), text)
  1460. def test_no_nl_preamble(self):
  1461. eq = self.ndiffAssertEqual
  1462. msg = Message()
  1463. msg['From'] = 'aperson@dom.ain'
  1464. msg['To'] = 'bperson@dom.ain'
  1465. msg['Subject'] = 'Test'
  1466. msg.preamble = 'MIME message'
  1467. msg.epilogue = ''
  1468. msg1 = MIMEText('One')
  1469. msg2 = MIMEText('Two')
  1470. msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY')
  1471. msg.attach(msg1)
  1472. msg.attach(msg2)
  1473. eq(msg.as_string(), """\
  1474. From: aperson@dom.ain
  1475. To: bperson@dom.ain
  1476. Subject: Test
  1477. Content-Type: multipart/mixed; boundary="BOUNDARY"
  1478. MIME message
  1479. --BOUNDARY
  1480. Content-Type: text/plain; charset="us-ascii"
  1481. MIME-Version: 1.0
  1482. Content-Transfer-Encoding: 7bit
  1483. One
  1484. --BOUNDARY
  1485. Content-Type: text/plain; charset="us-ascii"
  1486. MIME-Version: 1.0
  1487. Content-Transfer-Encoding: 7bit
  1488. Two
  1489. --BOUNDARY--
  1490. """)
  1491. def test_default_type(self):
  1492. eq = self.assertEqual
  1493. fp = openfile('msg_30.txt')
  1494. try:
  1495. msg = email.message_from_file(fp)
  1496. finally:
  1497. fp.close()
  1498. container1 = msg.get_payload(0)
  1499. eq(container1.get_default_type(), 'message/rfc822')
  1500. eq(container1.get_content_type(), 'message/rfc822')
  1501. container2 = msg.get_payload(1)
  1502. eq(container2.get_default_type(), 'message/rfc822')
  1503. eq(container2.get_content_type(), 'message/rfc822')
  1504. container1a = container1.get_payload(0)
  1505. eq(container1a.get_default_type(), 'text/plain')
  1506. eq(container1a.get_content_type(), 'text/plain')
  1507. container2a = container2.get_payload(0)
  1508. eq(container2a.get_default_type(), 'text/plain')
  1509. eq(container2a.get_content_type(), 'text/plain')
  1510. def test_default_type_with_explicit_container_type(self):
  1511. eq = self.assertEqual
  1512. fp = openfile('msg_28.txt')
  1513. try:
  1514. msg = email.message_from_file(fp)
  1515. finally:
  1516. fp.close()
  1517. container1 = msg.get_payload(0)
  1518. eq(container1.get_default_type(), 'message/rfc822')
  1519. eq(container1.get_content_type(), 'message/rfc822')
  1520. container2 = msg.get_payload(1)
  1521. eq(container2.get_default_type(), 'message/rfc822')
  1522. eq(container2.get_content_type(), 'message/rfc822')
  1523. container1a = container1.get_payload(0)
  1524. eq(container1a.get_default_type(), 'text/plain')
  1525. eq(container1a.get_content_type(), 'text/plain')
  1526. container2a = container2.get_payload(0)
  1527. eq(container2a.get_default_type(), 'text/plain')
  1528. eq(container2a.get_content_type(), 'text/plain')
  1529. def test_default_type_non_parsed(self):
  1530. eq = self.assertEqual
  1531. neq = self.ndiffAssertEqual
  1532. # Set up container
  1533. container = MIMEMultipart('digest', 'BOUNDARY')
  1534. container.epilogue = ''
  1535. # Set up subparts
  1536. subpart1a = MIMEText('message 1\n')
  1537. subpart2a = MIMEText('message 2\n')
  1538. subpart1 = MIMEMessage(subpart1a)
  1539. subpart2 = MIMEMessage(subpart2a)
  1540. container.attach(subpart1)
  1541. container.attach(subpart2)
  1542. eq(subpart1.get_content_type(), 'message/rfc822')
  1543. eq(subpart1.get_default_type(), 'message/rfc822')
  1544. eq(subpart2.get_content_type(), 'message/rfc822')
  1545. eq(subpart2.get_default_type(), 'message/rfc822')
  1546. neq(container.as_string(0), '''\
  1547. Content-Type: multipart/digest; boundary="BOUNDARY"
  1548. MIME-Version: 1.0
  1549. --BOUNDARY
  1550. Content-Type: message/rfc822
  1551. MIME-Version: 1.0
  1552. Content-Type: text/plain; charset="us-ascii"
  1553. MIME-Version: 1.0
  1554. Content-Transfer-Encoding: 7bit
  1555. message 1
  1556. --BOUNDARY
  1557. Content-Type: message/rfc822
  1558. MIME-Version: 1.0
  1559. Content-Type: text/plain; charset="us-ascii"
  1560. MIME-Version: 1.0
  1561. Content-Transfer-Encoding: 7bit
  1562. message 2
  1563. --BOUNDARY--
  1564. ''')
  1565. del subpart1['content-type']
  1566. del subpart1['mime-version']
  1567. del subpart2['content-type']
  1568. del subpart2['mime-version']
  1569. eq(subpart1.get_content_type(), 'message/rfc822')
  1570. eq(subpart1.get_default_type(), 'message/rfc822')
  1571. eq(subpart2.get_content_type(), 'message/rfc822')
  1572. eq(subpart2.get_default_type(), 'message/rfc822')
  1573. neq(container.as_string(0), '''\
  1574. Content-Type: multipart/digest; boundary="BOUNDARY"
  1575. MIME-Version: 1.0
  1576. --BOUNDARY
  1577. Content-Type: text/plain; charset="us-ascii"
  1578. MIME-Version: 1.0
  1579. Content-Transfer-Encoding: 7bit
  1580. message 1
  1581. --BOUNDARY
  1582. Content-Type: text/plain; charset="us-ascii"
  1583. MIME-Version: 1.0
  1584. Content-Transfer-Encoding: 7bit
  1585. message 2
  1586. --BOUNDARY--
  1587. ''')
  1588. def test_mime_attachments_in_constructor(self):
  1589. eq = self.assertEqual
  1590. text1 = MIMEText('')
  1591. text2 = MIMEText('')
  1592. msg = MIMEMultipart(_subparts=(text1, text2))
  1593. eq(len(msg.get_payload()), 2)
  1594. eq(msg.get_payload(0), text1)
  1595. eq(msg.get_payload(1), text2)
  1596. # A general test of parser->model->generator idempotency. IOW, read a message
  1597. # in, parse it into a message object tree, then without touching the tree,
  1598. # regenerate the plain text. The original text and the transformed text
  1599. # should be identical. Note: that we ignore the Unix-From since that may
  1600. # contain a changed date.
  1601. class TestIdempotent(TestEmailBase):
  1602. def _msgobj(self, filename):
  1603. fp = openfile(filename)
  1604. try:
  1605. data = fp.read()
  1606. finally:
  1607. fp.close()
  1608. msg = email.message_from_string(data)
  1609. return msg, data
  1610. def _idempotent(self, msg, text):
  1611. eq = self.ndiffAssertEqual
  1612. s = StringIO()
  1613. g = Generator(s, maxheaderlen=0)
  1614. g.flatten(msg)
  1615. eq(text, s.getvalue())
  1616. def test_parse_text_message(self):
  1617. eq = self.assertEquals
  1618. msg, text = self._msgobj('msg_01.txt')
  1619. eq(msg.get_content_type(), 'text/plain')
  1620. eq(msg.get_content_maintype(), 'text')
  1621. eq(msg.get_content_subtype(), 'plain')
  1622. eq(msg.get_params()[1], ('charset', 'us-ascii'))
  1623. eq(msg.get_param('charset'), 'us-ascii')
  1624. eq(msg.preamble, None)
  1625. eq(msg.epilogue, None)
  1626. self._idempotent(msg, text)
  1627. def test_parse_untyped_message(self):
  1628. eq = self.assertEquals
  1629. msg, text = self._msgobj('msg_03.txt')
  1630. eq(msg.get_content_type(), 'text/plain')
  1631. eq(msg.get_params(), None)
  1632. eq(msg.get_param('charset'), None)
  1633. self._idempotent(msg, text)
  1634. def test_simple_multipart(self):
  1635. msg, text = self._msgobj('msg_04.txt')
  1636. self._idempotent(msg, text)
  1637. def test_MIME_digest(self):
  1638. msg, text = self._msgobj('msg_02.txt')
  1639. self._idempotent(msg, text)
  1640. def test_long_header(self):
  1641. msg, text = self._msgobj('msg_27.txt')
  1642. self._idempotent(msg, text)
  1643. def test_MIME_digest_with_part_headers(self):
  1644. msg, text = self._msgobj('msg_28.txt')
  1645. self._idempotent(msg, text)
  1646. def test_mixed_with_image(self):
  1647. msg, text = self._msgobj('msg_06.txt')
  1648. self._idempotent(msg, text)
  1649. def test_multipart_report(self):
  1650. msg, text = self._msgobj('msg_05.txt')
  1651. self._idempotent(msg, text)
  1652. def test_dsn(self):
  1653. msg, text = self._msgobj('msg_16.txt')
  1654. self._idempotent(msg, text)
  1655. def test_preamble_epilogue(self):
  1656. msg, text = self._msgobj('msg_21.txt')
  1657. self._idempotent(msg, text)
  1658. def test_multipart_one_part(self):
  1659. msg, text = self._msgobj('msg_23.txt')
  1660. self._idempotent(msg, text)
  1661. def test_multipart_no_parts(self):
  1662. msg, text = self._msgobj('msg_24.txt')
  1663. self._idempotent(msg, text)
  1664. def test_no_start_boundary(self):
  1665. msg, text = self._msgobj('msg_31.txt')
  1666. self._idempotent(msg, text)
  1667. def test_rfc2231_charset(self):
  1668. msg, text = self._msgobj('msg_32.txt')
  1669. self._idempotent(msg, text)
  1670. def test_more_rfc2231_parameters(self):
  1671. msg, text = self._msgobj('msg_33.txt')
  1672. self._idempotent(msg, text)
  1673. def test_text_plain_in_a_multipart_digest(self):
  1674. msg, text = self._msgobj('msg_34.txt')
  1675. self._idempotent(msg, text)
  1676. def test_nested_multipart_mixeds(self):
  1677. msg, text = self._msgobj('msg_12a.txt')
  1678. self._idempotent(msg, text)
  1679. def test_message_external_body_idempotent(self):
  1680. msg, text = self._msgobj('msg_36.txt')
  1681. self._idempotent(msg, text)
  1682. def test_content_type(self):
  1683. eq = self.assertEquals
  1684. unless = self.failUnless
  1685. # Get a message object and reset the seek pointer for other tests
  1686. msg, text = self._msgobj('msg_05.txt')
  1687. eq(msg.get_content_type(), 'multipart/report')
  1688. # Test the Content-Type: parameters
  1689. params = {}
  1690. for pk, pv in msg.get_params():
  1691. params[pk] = pv
  1692. eq(params['report-type'], 'delivery-status')
  1693. eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com')
  1694. eq(msg.preamble, 'This is a MIME-encapsulated message.\n')
  1695. eq(msg.epilogue, '\n')
  1696. eq(len(msg.get_payload()), 3)
  1697. # Make sure the subparts are what we expect
  1698. msg1 = msg.get_payload(0)
  1699. eq(msg1.get_content_type(), 'text/plain')
  1700. eq(msg1.get_payload(), 'Yadda yadda yadda\n')
  1701. msg2 = msg.get_payload(1)
  1702. eq(msg2.get_content_type(), 'text/plain')
  1703. eq(msg2.get_payload(), 'Yadda yadda yadda\n')
  1704. msg3 = msg.get_payload(2)
  1705. eq(msg3.get_content_type(), 'message/rfc822')
  1706. self.failUnless(isinstance(msg3, Message))
  1707. payload = msg3.get_payload()
  1708. unless(isinstance(payload, list))
  1709. eq(len(payload), 1)
  1710. msg4 = payload[0]
  1711. unless(isinstance(msg4, Message))
  1712. eq(msg4.get_payload(), 'Yadda yadda yadda\n')
  1713. def test_parser(self):
  1714. eq = self.assertEquals
  1715. unless = self.failUnless
  1716. msg, text = self._msgobj('msg_06.txt')
  1717. # Check some of the outer headers
  1718. eq(msg.get_content_type(), 'message/rfc822')
  1719. # Make sure the payload is a list of exactly one sub-Message, and that
  1720. # that submessage has a type of text/plain
  1721. payload = msg.get_payload()
  1722. unless(isinstance(payload, list))
  1723. eq(len(payload), 1)
  1724. msg1 = payload[0]
  1725. self.failUnless(isinstance(msg1, Message))
  1726. eq(msg1.get_content_type(), 'text/plain')
  1727. self.failUnless(isinstance(msg1.get_payload(), str))
  1728. eq(msg1.get_payload(), '\n')
  1729. # Test various other bits of the package's functionality
  1730. class TestMiscellaneous(TestEmailBase):
  1731. def test_message_from_string(self):
  1732. fp = openfile('msg_01.txt')
  1733. try:
  1734. text = fp.read()
  1735. finally:
  1736. fp.close()
  1737. msg = email.message_from_string(text)
  1738. s = StringIO()
  1739. # Don't wrap/continue long headers since we're trying to test
  1740. # idempotency.
  1741. g = Generator(s, maxheaderlen=0)
  1742. g.flatten(msg)
  1743. self.assertEqual(text, s.getvalue())
  1744. def test_message_from_file(self):
  1745. fp = openfile('msg_01.txt')
  1746. try:
  1747. text = fp.read()
  1748. fp.seek(0)
  1749. msg = email.message_from_file(fp)
  1750. s = StringIO()
  1751. # Don't wrap/continue long headers since we're trying to test
  1752. # idempotency.
  1753. g = Generator(s, maxheaderlen=0)
  1754. g.flatten(msg)
  1755. self.assertEqual(text, s.getvalue())
  1756. finally:
  1757. fp.close()
  1758. def test_message_from_string_with_class(self):
  1759. unless = self.failUnless
  1760. fp = openfile('msg_01.txt')
  1761. try:
  1762. text = fp.read()
  1763. finally:
  1764. fp.close()
  1765. # Create a subclass
  1766. class MyMessage(Message):
  1767. pass
  1768. msg = email.message_from_string(text, MyMessage)
  1769. unless(isinstance(msg, MyMessage))
  1770. # Try something more complicated
  1771. fp = openfile('msg_02.txt')
  1772. try:
  1773. text = fp.read()
  1774. finally:
  1775. fp.close()
  1776. msg = email.message_from_string(text, MyMessage)
  1777. for subpart in msg.walk():
  1778. unless(isinstance(subpart, MyMessage))
  1779. def test_message_from_file_with_class(self):
  1780. unless = self.failUnless
  1781. # Create a subclass
  1782. class MyMessage(Message):
  1783. pass
  1784. fp = openfile('msg_01.txt')
  1785. try:
  1786. msg = email.message_from_file(fp, MyMessage)
  1787. finally:
  1788. fp.close()
  1789. unless(isinstance(msg, MyMessage))
  1790. # Try something more complicated
  1791. fp = openfile('msg_02.txt')
  1792. try:
  1793. msg = email.message_from_file(fp, MyMessage)
  1794. finally:
  1795. fp.close()
  1796. for subpart in msg.walk():
  1797. unless(isinstance(subpart, MyMessage))
  1798. def test__all__(self):
  1799. module = __import__('email')
  1800. # Can't use sorted() here due to Python 2.3 compatibility
  1801. all = module.__all__[:]
  1802. all.sort()
  1803. self.assertEqual(all, [
  1804. # Old names
  1805. 'Charset', 'Encoders', 'Errors', 'Generator',
  1806. 'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
  1807. 'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
  1808. 'MIMENonMultipart', 'MIMEText', 'Message',
  1809. 'Parser', 'Utils', 'base64MIME',
  1810. # new names
  1811. 'base64mime', 'charset', 'encoders', 'errors', 'generator',
  1812. 'header', 'iterators', 'message', 'message_from_file',
  1813. 'message_from_string', 'mime', 'parser',
  1814. 'quopriMIME', 'quoprimime', 'utils',
  1815. ])
  1816. def test_formatdate(self):
  1817. now = time.time()
  1818. self.assertEqual(utils.parsedate(utils.formatdate(now))[:6],
  1819. time.gmtime(now)[:6])
  1820. def test_formatdate_localtime(self):
  1821. now = time.time()
  1822. self.assertEqual(
  1823. utils.parsedate(utils.formatdate(now, localtime=True))[:6],
  1824. time.localtime(now)[:6])
  1825. def test_formatdate_usegmt(self):
  1826. now = time.time()
  1827. self.assertEqual(
  1828. utils.formatdate(now, localtime=False),
  1829. time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
  1830. self.assertEqual(
  1831. utils.formatdate(now, localtime=False, usegmt=True),
  1832. time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
  1833. def test_parsedate_none(self):
  1834. self.assertEqual(utils.parsedate(''), None)
  1835. def test_parsedate_compact(self):
  1836. # The FWS after the comma is optional
  1837. self.assertEqual(utils.parsedate('Wed,3 Apr 2002 14:58:26 +0800'),
  1838. utils.parsedate('Wed, 3 Apr 2002 14:58:26 +0800'))
  1839. def test_parsedate_no_dayofweek(self):
  1840. eq = self.assertEqual
  1841. eq(utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'),
  1842. (2003, 2, 25, 13, 47, 26, 0, 1, -1, -28800))
  1843. def test_parsedate_compact_no_dayofweek(self):
  1844. eq = self.assertEqual
  1845. eq(utils.parsedate_tz('5 Feb 2003 13:47:26 -0800'),
  1846. (2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800))
  1847. def test_parsedate_acceptable_to_time_functions(self):
  1848. eq = self.assertEqual
  1849. timetup = utils.parsedate('5 Feb 2003 13:47:26 -0800')
  1850. t = int(time.mktime(timetup))
  1851. eq(time.localtime(t)[:6], timetup[:6])
  1852. eq(int(time.strftime('%Y', timetup)), 2003)
  1853. timetup = utils.parsedate_tz('5 Feb 2003 13:47:26 -0800')
  1854. t = int(time.mktime(timetup[:9]))
  1855. eq(time.localtime(t)[:6], timetup[:6])
  1856. eq(int(time.strftime('%Y', timetup[:9])), 2003)
  1857. def test_parseaddr_empty(self):
  1858. self.assertEqual(utils.parseaddr('<>'), ('', ''))
  1859. self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '')
  1860. def test_noquote_dump(self):
  1861. self.assertEqual(
  1862. utils.formataddr(('A Silly Person', 'person@dom.ain')),
  1863. 'A Silly Person <person@dom.ain>')
  1864. def test_escape_dump(self):
  1865. self.assertEqual(
  1866. utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
  1867. r'"A \(Very\) Silly Person" <person@dom.ain>')
  1868. a = r'A \(Special\) Person'
  1869. b = 'person@dom.ain'
  1870. self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b))
  1871. def test_escape_backslashes(self):
  1872. self.assertEqual(
  1873. utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
  1874. r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
  1875. a = r'Arthur \Backslash\ Foobar'
  1876. b = 'person@dom.ain'
  1877. self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b))
  1878. def test_name_with_dot(self):
  1879. x = 'John X. Doe <jxd@example.com>'
  1880. y = '"John X. Doe" <jxd@example.com>'
  1881. a, b = ('John X. Doe', 'jxd@example.com')
  1882. self.assertEqual(utils.parseaddr(x), (a, b))
  1883. self.assertEqual(utils.parseaddr(y), (a, b))
  1884. # formataddr() quotes the name if there's a dot in it
  1885. self.assertEqual(utils.formataddr((a, b)), y)
  1886. def test_multiline_from_comment(self):
  1887. x = """\
  1888. Foo
  1889. \tBar <foo@example.com>"""
  1890. self.assertEqual(utils.parseaddr(x), ('Foo Bar', 'foo@example.com'))
  1891. def test_quote_dump(self):
  1892. self.assertEqual(
  1893. utils.formataddr(('A Silly; Person', 'person@dom.ain')),
  1894. r'"A Silly; Person" <person@dom.ain>')
  1895. def test_fix_eols(self):
  1896. eq = self.assertEqual
  1897. eq(utils.fix_eols('hello'), 'hello')
  1898. eq(utils.fix_eols('hello\n'), 'hello\r\n')
  1899. eq(utils.fix_eols('hello\r'), 'hello\r\n')
  1900. eq(utils.fix_eols('hello\r\n'), 'hello\r\n')
  1901. eq(utils.fix_eols('hello\n\r'), 'hello\r\n\r\n')
  1902. def test_charset_richcomparisons(self):
  1903. eq = self.assertEqual
  1904. ne = self.failIfEqual
  1905. cset1 = Charset()
  1906. cset2 = Charset()
  1907. eq(cset1, 'us-ascii')
  1908. eq(cset1, 'US-ASCII')
  1909. eq(cset1, 'Us-AsCiI')
  1910. eq('us-ascii', cset1)
  1911. eq('US-ASCII', cset1)
  1912. eq('Us-AsCiI', cset1)
  1913. ne(cset1, 'usascii')
  1914. ne(cset1, 'USASCII')
  1915. ne(cset1, 'UsAsCiI')
  1916. ne('usascii', cset1)
  1917. ne('USASCII', cset1)
  1918. ne('UsAsCiI', cset1)
  1919. eq(cset1, cset2)
  1920. eq(cset2, cset1)
  1921. def test_getaddresses(self):
  1922. eq = self.assertEqual
  1923. eq(utils.getaddresses(['aperson@dom.ain (Al Person)',
  1924. 'Bud Person <bperson@dom.ain>']),
  1925. [('Al Person', 'aperson@dom.ain'),
  1926. ('Bud Person', 'bperson@dom.ain')])
  1927. def test_getaddresses_nasty(self):
  1928. eq = self.assertEqual
  1929. eq(utils.getaddresses(['foo: ;']), [('', '')])
  1930. eq(utils.getaddresses(
  1931. ['[]*-- =~$']),
  1932. [('', ''), ('', ''), ('', '*--')])
  1933. eq(utils.getaddresses(
  1934. ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
  1935. [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')])
  1936. def test_getaddresses_embedded_comment(self):
  1937. """Test proper handling of a nested comment"""
  1938. eq = self.assertEqual
  1939. addrs = utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
  1940. eq(addrs[0][1], 'foo@bar.com')
  1941. def test_utils_quote_unquote(self):
  1942. eq = self.assertEqual
  1943. msg = Message()
  1944. msg.add_header('content-disposition', 'attachment',
  1945. filename='foo\\wacky"name')
  1946. eq(msg.get_filename(), 'foo\\wacky"name')
  1947. def test_get_body_encoding_with_bogus_charset(self):
  1948. charset = Charset('not a charset')
  1949. self.assertEqual(charset.get_body_encoding(), 'base64')
  1950. def test_get_body_encoding_with_uppercase_charset(self):
  1951. eq = self.assertEqual
  1952. msg = Message()
  1953. msg['Content-Type'] = 'text/plain; charset=UTF-8'
  1954. eq(msg['content-type'], 'text/plain; charset=UTF-8')
  1955. charsets = msg.get_charsets()
  1956. eq(len(charsets), 1)
  1957. eq(charsets[0], 'utf-8')
  1958. charset = Charset(charsets[0])
  1959. eq(charset.get_body_encoding(), 'base64')
  1960. msg.set_payload('hello world', charset=charset)
  1961. eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n')
  1962. eq(msg.get_payload(decode=True), 'hello world')
  1963. eq(msg['content-transfer-encoding'], 'base64')
  1964. # Try another one
  1965. msg = Message()
  1966. msg['Content-Type'] = 'text/plain; charset="US-ASCII"'
  1967. charsets = msg.get_charsets()
  1968. eq(len(charsets), 1)
  1969. eq(charsets[0], 'us-ascii')
  1970. charset = Charset(charsets[0])
  1971. eq(charset.get_body_encoding(), encoders.encode_7or8bit)
  1972. msg.set_payload('hello world', charset=charset)
  1973. eq(msg.get_payload(), 'hello world')
  1974. eq(msg['content-transfer-encoding'], '7bit')
  1975. def test_charsets_case_insensitive(self):
  1976. lc = Charset('us-ascii')
  1977. uc = Charset('US-ASCII')
  1978. self.assertEqual(lc.get_body_encoding(), uc.get_body_encoding())
  1979. def test_partial_falls_inside_message_delivery_status(self):
  1980. eq = self.ndiffAssertEqual
  1981. # The Parser interface provides chunks of data to FeedParser in 8192
  1982. # byte gulps. SF bug #1076485 found one of those chunks inside
  1983. # message/delivery-status header block, which triggered an
  1984. # unreadline() of NeedMoreData.
  1985. msg = self._msgobj('msg_43.txt')
  1986. sfp = StringIO()
  1987. iterators._structure(msg, sfp)
  1988. eq(sfp.getvalue(), """\
  1989. multipart/report
  1990. text/plain
  1991. message/delivery-status
  1992. text/plain
  1993. text/plain
  1994. text/plain
  1995. text/plain
  1996. text/plain
  1997. text/plain
  1998. text/plain
  1999. text/plain
  2000. text/plain
  2001. text/plain
  2002. text/plain
  2003. text/plain
  2004. text/plain
  2005. text/plain
  2006. text/plain
  2007. text/plain
  2008. text/plain
  2009. text/plain
  2010. text/plain
  2011. text/plain
  2012. text/plain
  2013. text/plain
  2014. text/plain
  2015. text/plain
  2016. text/plain
  2017. text/plain
  2018. text/rfc822-headers
  2019. """)
  2020. # Test the iterator/generators
  2021. class TestIterators(TestEmailBase):
  2022. def test_body_line_iterator(self):
  2023. eq = self.assertEqual
  2024. neq = self.ndiffAssertEqual
  2025. # First a simple non-multipart message
  2026. msg = self._msgobj('msg_01.txt')
  2027. it = iterators.body_line_iterator(msg)
  2028. lines = list(it)
  2029. eq(len(lines), 6)
  2030. neq(EMPTYSTRING.join(lines), msg.get_payload())
  2031. # Now a more complicated multipart
  2032. msg = self._msgobj('msg_02.txt')
  2033. it = iterators.body_line_iterator(msg)
  2034. lines = list(it)
  2035. eq(len(lines), 43)
  2036. fp = openfile('msg_19.txt')
  2037. try:
  2038. neq(EMPTYSTRING.join(lines), fp.read())
  2039. finally:
  2040. fp.close()
  2041. def test_typed_subpart_iterator(self):
  2042. eq = self.assertEqual
  2043. msg = self._msgobj('msg_04.txt')
  2044. it = iterators.typed_subpart_iterator(msg, 'text')
  2045. lines = []
  2046. subparts = 0
  2047. for subpart in it:
  2048. subparts += 1
  2049. lines.append(subpart.get_payload())
  2050. eq(subparts, 2)
  2051. eq(EMPTYSTRING.join(lines), """\
  2052. a simple kind of mirror
  2053. to reflect upon our own
  2054. a simple kind of mirror
  2055. to reflect upon our own
  2056. """)
  2057. def test_typed_subpart_iterator_default_type(self):
  2058. eq = self.assertEqual
  2059. msg = self._msgobj('msg_03.txt')
  2060. it = iterators.typed_subpart_iterator(msg, 'text', 'plain')
  2061. lines = []
  2062. subparts = 0
  2063. for subpart in it:
  2064. subparts += 1
  2065. lines.append(subpart.get_payload())
  2066. eq(subparts, 1)
  2067. eq(EMPTYSTRING.join(lines), """\
  2068. Hi,
  2069. Do you like this message?
  2070. -Me
  2071. """)
  2072. class TestParsers(TestEmailBase):
  2073. def test_header_parser(self):
  2074. eq = self.assertEqual
  2075. # Parse only the headers of a complex multipart MIME document
  2076. fp = openfile('msg_02.txt')
  2077. try:
  2078. msg = HeaderParser().parse(fp)
  2079. finally:
  2080. fp.close()
  2081. eq(msg['from'], 'ppp-request@zzz.org')
  2082. eq(msg['to'], 'ppp@zzz.org')
  2083. eq(msg.get_content_type(), 'multipart/mixed')
  2084. self.failIf(msg.is_multipart())
  2085. self.failUnless(isinstance(msg.get_payload(), str))
  2086. def test_whitespace_continuation(self):
  2087. eq = self.assertEqual
  2088. # This message contains a line after the Subject: header that has only
  2089. # whitespace, but it is not empty!
  2090. msg = email.message_from_string("""\
  2091. From: aperson@dom.ain
  2092. To: bperson@dom.ain
  2093. Subject: the next line has a space on it
  2094. \x20
  2095. Date: Mon, 8 Apr 2002 15:09:19 -0400
  2096. Message-ID: spam
  2097. Here's the message body
  2098. """)
  2099. eq(msg['subject'], 'the next line has a space on it\n ')
  2100. eq(msg['message-id'], 'spam')
  2101. eq(msg.get_payload(), "Here's the message body\n")
  2102. def test_whitespace_continuation_last_header(self):
  2103. eq = self.assertEqual
  2104. # Like the previous test, but the subject line is the last
  2105. # header.
  2106. msg = email.message_from_string("""\
  2107. From: aperson@dom.ain
  2108. To: bperson@dom.ain
  2109. Date: Mon, 8 Apr 2002 15:09:19 -0400
  2110. Message-ID: spam
  2111. Subject: the next line has a space on it
  2112. \x20
  2113. Here's the message body
  2114. """)
  2115. eq(msg['subject'], 'the next line has a space on it\n ')
  2116. eq(msg['message-id'], 'spam')
  2117. eq(msg.get_payload(), "Here's the message body\n")
  2118. def test_crlf_separation(self):
  2119. eq = self.assertEqual
  2120. fp = openfile('msg_26.txt', mode='rb')
  2121. try:
  2122. msg = Parser().parse(fp)
  2123. finally:
  2124. fp.close()
  2125. eq(len(msg.get_payload()), 2)
  2126. part1 = msg.get_payload(0)
  2127. eq(part1.get_content_type(), 'text/plain')
  2128. eq(part1.get_payload(), 'Simple email with attachment.\r\n\r\n')
  2129. part2 = msg.get_payload(1)
  2130. eq(part2.get_content_type(), 'application/riscos')
  2131. def test_multipart_digest_with_extra_mime_headers(self):
  2132. eq = self.assertEqual
  2133. neq = self.ndiffAssertEqual
  2134. fp = openfile('msg_28.txt')
  2135. try:
  2136. msg = email.message_from_file(fp)
  2137. finally:
  2138. fp.close()
  2139. # Structure is:
  2140. # multipart/digest
  2141. # message/rfc822
  2142. # text/plain
  2143. # message/rfc822
  2144. # text/plain
  2145. eq(msg.is_multipart(), 1)
  2146. eq(len(msg.get_payload()), 2)
  2147. part1 = msg.get_payload(0)
  2148. eq(part1.get_content_type(), 'message/rfc822')
  2149. eq(part1.is_multipart(), 1)
  2150. eq(len(part1.get_payload()), 1)
  2151. part1a = part1.get_payload(0)
  2152. eq(part1a.is_multipart(), 0)
  2153. eq(part1a.get_content_type(), 'text/plain')
  2154. neq(part1a.get_payload(), 'message 1\n')
  2155. # next message/rfc822
  2156. part2 = msg.get_payload(1)
  2157. eq(part2.get_content_type(), 'message/rfc822')
  2158. eq(part2.is_multipart(), 1)
  2159. eq(len(part2.get_payload()), 1)
  2160. part2a = part2.get_payload(0)
  2161. eq(part2a.is_multipart(), 0)
  2162. eq(part2a.get_content_type(), 'text/plain')
  2163. neq(part2a.get_payload(), 'message 2\n')
  2164. def test_three_lines(self):
  2165. # A bug report by Andrew McNamara
  2166. lines = ['From: Andrew Person <aperson@dom.ain',
  2167. 'Subject: Test',
  2168. 'Date: Tue, 20 Aug 2002 16:43:45 +1000']
  2169. msg = email.message_from_string(NL.join(lines))
  2170. self.assertEqual(msg['date'], 'Tue, 20 Aug 2002 16:43:45 +1000')
  2171. def test_strip_line_feed_and_carriage_return_in_headers(self):
  2172. eq = self.assertEqual
  2173. # For [ 1002475 ] email message parser doesn't handle \r\n correctly
  2174. value1 = 'text'
  2175. value2 = 'more text'
  2176. m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % (
  2177. value1, value2)
  2178. msg = email.message_from_string(m)
  2179. eq(msg.get('Header'), value1)
  2180. eq(msg.get('Next-Header'), value2)
  2181. def test_rfc2822_header_syntax(self):
  2182. eq = self.assertEqual
  2183. m = '>From: foo\nFrom: bar\n!"#QUX;~: zoo\n\nbody'
  2184. msg = email.message_from_string(m)
  2185. eq(len(msg.keys()), 3)
  2186. keys = msg.keys()
  2187. keys.sort()
  2188. eq(keys, ['!"#QUX;~', '>From', 'From'])
  2189. eq(msg.get_payload(), 'body')
  2190. def test_rfc2822_space_not_allowed_in_header(self):
  2191. eq = self.assertEqual
  2192. m = '>From foo@example.com 11:25:53\nFrom: bar\n!"#QUX;~: zoo\n\nbody'
  2193. msg = email.message_from_string(m)
  2194. eq(len(msg.keys()), 0)
  2195. def test_rfc2822_one_character_header(self):
  2196. eq = self.assertEqual
  2197. m = 'A: first header\nB: second header\nCC: third header\n\nbody'
  2198. msg = email.message_from_string(m)
  2199. headers = msg.keys()
  2200. headers.sort()
  2201. eq(headers, ['A', 'B', 'CC'])
  2202. eq(msg.get_payload(), 'body')
  2203. class TestBase64(unittest.TestCase):
  2204. def test_len(self):
  2205. eq = self.assertEqual
  2206. eq(base64mime.base64_len('hello'),
  2207. len(base64mime.encode('hello', eol='')))
  2208. for size in range(15):
  2209. if size == 0 : bsize = 0
  2210. elif size <= 3 : bsize = 4
  2211. elif size <= 6 : bsize = 8
  2212. elif size <= 9 : bsize = 12
  2213. elif size <= 12: bsize = 16
  2214. else : bsize = 20
  2215. eq(base64mime.base64_len('x'*size), bsize)
  2216. def test_decode(self):
  2217. eq = self.assertEqual
  2218. eq(base64mime.decode(''), '')
  2219. eq(base64mime.decode('aGVsbG8='), 'hello')
  2220. eq(base64mime.decode('aGVsbG8=', 'X'), 'hello')
  2221. eq(base64mime.decode('aGVsbG8NCndvcmxk\n', 'X'), 'helloXworld')
  2222. def test_encode(self):
  2223. eq = self.assertEqual
  2224. eq(base64mime.encode(''), '')
  2225. eq(base64mime.encode('hello'), 'aGVsbG8=\n')
  2226. # Test the binary flag
  2227. eq(base64mime.encode('hello\n'), 'aGVsbG8K\n')
  2228. eq(base64mime.encode('hello\n', 0), 'aGVsbG8NCg==\n')
  2229. # Test the maxlinelen arg
  2230. eq(base64mime.encode('xxxx ' * 20, maxlinelen=40), """\
  2231. eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
  2232. eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
  2233. eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
  2234. eHh4eCB4eHh4IA==
  2235. """)
  2236. # Test the eol argument
  2237. eq(base64mime.encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\
  2238. eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
  2239. eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
  2240. eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
  2241. eHh4eCB4eHh4IA==\r
  2242. """)
  2243. def test_header_encode(self):
  2244. eq = self.assertEqual
  2245. he = base64mime.header_encode
  2246. eq(he('hello'), '=?iso-8859-1?b?aGVsbG8=?=')
  2247. eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8NCndvcmxk?=')
  2248. # Test the charset option
  2249. eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?b?aGVsbG8=?=')
  2250. # Test the keep_eols flag
  2251. eq(he('hello\nworld', keep_eols=True),
  2252. '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=')
  2253. # Test the maxlinelen argument
  2254. eq(he('xxxx ' * 20, maxlinelen=40), """\
  2255. =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHggeHg=?=
  2256. =?iso-8859-1?b?eHggeHh4eCB4eHh4IHh4eHg=?=
  2257. =?iso-8859-1?b?IHh4eHggeHh4eCB4eHh4IHg=?=
  2258. =?iso-8859-1?b?eHh4IHh4eHggeHh4eCB4eHg=?=
  2259. =?iso-8859-1?b?eCB4eHh4IHh4eHggeHh4eCA=?=
  2260. =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHgg?=""")
  2261. # Test the eol argument
  2262. eq(he('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\
  2263. =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHggeHg=?=\r
  2264. =?iso-8859-1?b?eHggeHh4eCB4eHh4IHh4eHg=?=\r
  2265. =?iso-8859-1?b?IHh4eHggeHh4eCB4eHh4IHg=?=\r
  2266. =?iso-8859-1?b?eHh4IHh4eHggeHh4eCB4eHg=?=\r
  2267. =?iso-8859-1?b?eCB4eHh4IHh4eHggeHh4eCA=?=\r
  2268. =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHgg?=""")
  2269. class TestQuopri(unittest.TestCase):
  2270. def setUp(self):
  2271. self.hlit = [chr(x) for x in range(ord('a'), ord('z')+1)] + \
  2272. [chr(x) for x in range(ord('A'), ord('Z')+1)] + \
  2273. [chr(x) for x in range(ord('0'), ord('9')+1)] + \
  2274. ['!', '*', '+', '-', '/', ' ']
  2275. self.hnon = [chr(x) for x in range(256) if chr(x) not in self.hlit]
  2276. assert len(self.hlit) + len(self.hnon) == 256
  2277. self.blit = [chr(x) for x in range(ord(' '), ord('~')+1)] + ['\t']
  2278. self.blit.remove('=')
  2279. self.bnon = [chr(x) for x in range(256) if chr(x) not in self.blit]
  2280. assert len(self.blit) + len(self.bnon) == 256
  2281. def test_header_quopri_check(self):
  2282. for c in self.hlit:
  2283. self.failIf(quoprimime.header_quopri_check(c))
  2284. for c in self.hnon:
  2285. self.failUnless(quoprimime.header_quopri_check(c))
  2286. def test_body_quopri_check(self):
  2287. for c in self.blit:
  2288. self.failIf(quoprimime.body_quopri_check(c))
  2289. for c in self.bnon:
  2290. self.failUnless(quoprimime.body_quopri_check(c))
  2291. def test_header_quopri_len(self):
  2292. eq = self.assertEqual
  2293. hql = quoprimime.header_quopri_len
  2294. enc = quoprimime.header_encode
  2295. for s in ('hello', 'h@e@l@l@o@'):
  2296. # Empty charset and no line-endings. 7 == RFC chrome
  2297. eq(hql(s), len(enc(s, charset='', eol=''))-7)
  2298. for c in self.hlit:
  2299. eq(hql(c), 1)
  2300. for c in self.hnon:
  2301. eq(hql(c), 3)
  2302. def test_body_quopri_len(self):
  2303. eq = self.assertEqual
  2304. bql = quoprimime.body_quopri_len
  2305. for c in self.blit:
  2306. eq(bql(c), 1)
  2307. for c in self.bnon:
  2308. eq(bql(c), 3)
  2309. def test_quote_unquote_idempotent(self):
  2310. for x in range(256):
  2311. c = chr(x)
  2312. self.assertEqual(quoprimime.unquote(quoprimime.quote(c)), c)
  2313. def test_header_encode(self):
  2314. eq = self.assertEqual
  2315. he = quoprimime.header_encode
  2316. eq(he('hello'), '=?iso-8859-1?q?hello?=')
  2317. eq(he('hello\nworld'), '=?iso-8859-1?q?hello=0D=0Aworld?=')
  2318. # Test the charset option
  2319. eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?q?hello?=')
  2320. # Test the keep_eols flag
  2321. eq(he('hello\nworld', keep_eols=True), '=?iso-8859-1?q?hello=0Aworld?=')
  2322. # Test a non-ASCII character
  2323. eq(he('hello\xc7there'), '=?iso-8859-1?q?hello=C7there?=')
  2324. # Test the maxlinelen argument
  2325. eq(he('xxxx ' * 20, maxlinelen=40), """\
  2326. =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?=
  2327. =?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?=
  2328. =?iso-8859-1?q?_xxxx_xxxx_xxxx_xxxx_x?=
  2329. =?iso-8859-1?q?xxx_xxxx_xxxx_xxxx_xxx?=
  2330. =?iso-8859-1?q?x_xxxx_xxxx_?=""")
  2331. # Test the eol argument
  2332. eq(he('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\
  2333. =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?=\r
  2334. =?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?=\r
  2335. =?iso-8859-1?q?_xxxx_xxxx_xxxx_xxxx_x?=\r
  2336. =?iso-8859-1?q?xxx_xxxx_xxxx_xxxx_xxx?=\r
  2337. =?iso-8859-1?q?x_xxxx_xxxx_?=""")
  2338. def test_decode(self):
  2339. eq = self.assertEqual
  2340. eq(quoprimime.decode(''), '')
  2341. eq(quoprimime.decode('hello'), 'hello')
  2342. eq(quoprimime.decode('hello', 'X'), 'hello')
  2343. eq(quoprimime.decode('hello\nworld', 'X'), 'helloXworld')
  2344. def test_encode(self):
  2345. eq = self.assertEqual
  2346. eq(quoprimime.encode(''), '')
  2347. eq(quoprimime.encode('hello'), 'hello')
  2348. # Test the binary flag
  2349. eq(quoprimime.encode('hello\r\nworld'), 'hello\nworld')
  2350. eq(quoprimime.encode('hello\r\nworld', 0), 'hello\nworld')
  2351. # Test the maxlinelen arg
  2352. eq(quoprimime.encode('xxxx ' * 20, maxlinelen=40), """\
  2353. xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=
  2354. xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=
  2355. x xxxx xxxx xxxx xxxx=20""")
  2356. # Test the eol argument
  2357. eq(quoprimime.encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\
  2358. xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=\r
  2359. xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=\r
  2360. x xxxx xxxx xxxx xxxx=20""")
  2361. eq(quoprimime.encode("""\
  2362. one line
  2363. two line"""), """\
  2364. one line
  2365. two line""")
  2366. # Test the Charset class
  2367. class TestCharset(unittest.TestCase):
  2368. def tearDown(self):
  2369. from email import charset as CharsetModule
  2370. try:
  2371. del CharsetModule.CHARSETS['fake']
  2372. except KeyError:
  2373. pass
  2374. def test_idempotent(self):
  2375. eq = self.assertEqual
  2376. # Make sure us-ascii = no Unicode conversion
  2377. c = Charset('us-ascii')
  2378. s = 'Hello World!'
  2379. sp = c.to_splittable(s)
  2380. eq(s, c.from_splittable(sp))
  2381. # test 8-bit idempotency with us-ascii
  2382. s = '\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa'
  2383. sp = c.to_splittable(s)
  2384. eq(s, c.from_splittable(sp))
  2385. def test_body_encode(self):
  2386. eq = self.assertEqual
  2387. # Try a charset with QP body encoding
  2388. c = Charset('iso-8859-1')
  2389. eq('hello w=F6rld', c.body_encode('hello w\xf6rld'))
  2390. # Try a charset with Base64 body encoding
  2391. c = Charset('utf-8')
  2392. eq('aGVsbG8gd29ybGQ=\n', c.body_encode('hello world'))
  2393. # Try a charset with None body encoding
  2394. c = Charset('us-ascii')
  2395. eq('hello world', c.body_encode('hello world'))
  2396. # Try the convert argument, where input codec <> output codec
  2397. c = Charset('euc-jp')
  2398. # With apologies to Tokio Kikuchi ;)
  2399. try:
  2400. eq('\x1b$B5FCO;~IW\x1b(B',
  2401. c.body_encode('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7'))
  2402. eq('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7',
  2403. c.body_encode('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7', False))
  2404. except LookupError:
  2405. # We probably don't have the Japanese codecs installed
  2406. pass
  2407. # Testing SF bug #625509, which we have to fake, since there are no
  2408. # built-in encodings where the header encoding is QP but the body
  2409. # encoding is not.
  2410. from email import charset as CharsetModule
  2411. CharsetModule.add_charset('fake', CharsetModule.QP, None)
  2412. c = Charset('fake')
  2413. eq('hello w\xf6rld', c.body_encode('hello w\xf6rld'))
  2414. def test_unicode_charset_name(self):
  2415. charset = Charset(u'us-ascii')
  2416. self.assertEqual(str(charset), 'us-ascii')
  2417. self.assertRaises(errors.CharsetError, Charset, 'asc\xffii')
  2418. # Test multilingual MIME headers.
  2419. class TestHeader(TestEmailBase):
  2420. def test_simple(self):
  2421. eq = self.ndiffAssertEqual
  2422. h = Header('Hello World!')
  2423. eq(h.encode(), 'Hello World!')
  2424. h.append(' Goodbye World!')
  2425. eq(h.encode(), 'Hello World! Goodbye World!')
  2426. def test_simple_surprise(self):
  2427. eq = self.ndiffAssertEqual
  2428. h = Header('Hello World!')
  2429. eq(h.encode(), 'Hello World!')
  2430. h.append('Goodbye World!')
  2431. eq(h.encode(), 'Hello World! Goodbye World!')
  2432. def test_header_needs_no_decoding(self):
  2433. h = 'no decoding needed'
  2434. self.assertEqual(decode_header(h), [(h, None)])
  2435. def test_long(self):
  2436. 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.",
  2437. maxlinelen=76)
  2438. for l in h.encode(splitchars=' ').split('\n '):
  2439. self.failUnless(len(l) <= 76)
  2440. def test_multilingual(self):
  2441. eq = self.ndiffAssertEqual
  2442. g = Charset("iso-8859-1")
  2443. cz = Charset("iso-8859-2")
  2444. utf8 = Charset("utf-8")
  2445. g_head = "Die Mieter treten hier ein werden mit einem Foerderband komfortabel den Korridor entlang, an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, gegen die rotierenden Klingen bef\xf6rdert. "
  2446. cz_head = "Finan\xe8ni metropole se hroutily pod tlakem jejich d\xf9vtipu.. "
  2447. utf8_head = u"\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066\u3044\u307e\u3059\u3002".encode("utf-8")
  2448. h = Header(g_head, g)
  2449. h.append(cz_head, cz)
  2450. h.append(utf8_head, utf8)
  2451. enc = h.encode()
  2452. eq(enc, """\
  2453. =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderband_ko?=
  2454. =?iso-8859-1?q?mfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen_Wan?=
  2455. =?iso-8859-1?q?dgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef=F6?=
  2456. =?iso-8859-1?q?rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutily?=
  2457. =?iso-8859-2?q?_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?=
  2458. =?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC?=
  2459. =?utf-8?b?5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn?=
  2460. =?utf-8?b?44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFz?=
  2461. =?utf-8?q?_Nunstuck_git_und_Slotermeyer=3F_Ja!_Beiherhund_das_Oder_die_Fl?=
  2462. =?utf-8?b?aXBwZXJ3YWxkdCBnZXJzcHV0LuOAjeOBqOiogOOBo+OBpuOBhOOBvuOBmQ==?=
  2463. =?utf-8?b?44CC?=""")
  2464. eq(decode_header(enc),
  2465. [(g_head, "iso-8859-1"), (cz_head, "iso-8859-2"),
  2466. (utf8_head, "utf-8")])
  2467. ustr = unicode(h)
  2468. eq(ustr.encode('utf-8'),
  2469. 'Die Mieter treten hier ein werden mit einem Foerderband '
  2470. 'komfortabel den Korridor entlang, an s\xc3\xbcdl\xc3\xbcndischen '
  2471. 'Wandgem\xc3\xa4lden vorbei, gegen die rotierenden Klingen '
  2472. 'bef\xc3\xb6rdert. Finan\xc4\x8dni metropole se hroutily pod '
  2473. 'tlakem jejich d\xc5\xafvtipu.. \xe6\xad\xa3\xe7\xa2\xba\xe3\x81'
  2474. '\xab\xe8\xa8\x80\xe3\x81\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3'
  2475. '\xe3\x81\xaf\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3'
  2476. '\x81\xbe\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
  2477. '\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8\xaa\x9e'
  2478. '\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81\xe3\x81\x82\xe3'
  2479. '\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81\x9f\xe3\x82\x89\xe3\x82'
  2480. '\x81\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\xe5\xae\x9f\xe9\x9a\x9b'
  2481. '\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x8cWenn ist das Nunstuck git '
  2482. 'und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt '
  2483. 'gersput.\xe3\x80\x8d\xe3\x81\xa8\xe8\xa8\x80\xe3\x81\xa3\xe3\x81'
  2484. '\xa6\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82')
  2485. # Test make_header()
  2486. newh = make_header(decode_header(enc))
  2487. eq(newh, enc)
  2488. def test_header_ctor_default_args(self):
  2489. eq = self.ndiffAssertEqual
  2490. h = Header()
  2491. eq(h, '')
  2492. h.append('foo', Charset('iso-8859-1'))
  2493. eq(h, '=?iso-8859-1?q?foo?=')
  2494. def test_explicit_maxlinelen(self):
  2495. eq = self.ndiffAssertEqual
  2496. hstr = 'A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior'
  2497. h = Header(hstr)
  2498. eq(h.encode(), '''\
  2499. A very long line that must get split to something other than at the 76th
  2500. character boundary to test the non-default behavior''')
  2501. h = Header(hstr, header_name='Subject')
  2502. eq(h.encode(), '''\
  2503. A very long line that must get split to something other than at the
  2504. 76th character boundary to test the non-default behavior''')
  2505. h = Header(hstr, maxlinelen=1024, header_name='Subject')
  2506. eq(h.encode(), hstr)
  2507. def test_us_ascii_header(self):
  2508. eq = self.assertEqual
  2509. s = 'hello'
  2510. x = decode_header(s)
  2511. eq(x, [('hello', None)])
  2512. h = make_header(x)
  2513. eq(s, h.encode())
  2514. def test_string_charset(self):
  2515. eq = self.assertEqual
  2516. h = Header()
  2517. h.append('hello', 'iso-8859-1')
  2518. eq(h, '=?iso-8859-1?q?hello?=')
  2519. ## def test_unicode_error(self):
  2520. ## raises = self.assertRaises
  2521. ## raises(UnicodeError, Header, u'[P\xf6stal]', 'us-ascii')
  2522. ## raises(UnicodeError, Header, '[P\xf6stal]', 'us-ascii')
  2523. ## h = Header()
  2524. ## raises(UnicodeError, h.append, u'[P\xf6stal]', 'us-ascii')
  2525. ## raises(UnicodeError, h.append, '[P\xf6stal]', 'us-ascii')
  2526. ## raises(UnicodeError, Header, u'\u83ca\u5730\u6642\u592b', 'iso-8859-1')
  2527. def test_utf8_shortest(self):
  2528. eq = self.assertEqual
  2529. h = Header(u'p\xf6stal', 'utf-8')
  2530. eq(h.encode(), '=?utf-8?q?p=C3=B6stal?=')
  2531. h = Header(u'\u83ca\u5730\u6642\u592b', 'utf-8')
  2532. eq(h.encode(), '=?utf-8?b?6I+K5Zyw5pmC5aSr?=')
  2533. def test_bad_8bit_header(self):
  2534. raises = self.assertRaises
  2535. eq = self.assertEqual
  2536. x = 'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
  2537. raises(UnicodeError, Header, x)
  2538. h = Header()
  2539. raises(UnicodeError, h.append, x)
  2540. eq(str(Header(x, errors='replace')), x)
  2541. h.append(x, errors='replace')
  2542. eq(str(h), x)
  2543. def test_encoded_adjacent_nonencoded(self):
  2544. eq = self.assertEqual
  2545. h = Header()
  2546. h.append('hello', 'iso-8859-1')
  2547. h.append('world')
  2548. s = h.encode()
  2549. eq(s, '=?iso-8859-1?q?hello?= world')
  2550. h = make_header(decode_header(s))
  2551. eq(h.encode(), s)
  2552. def test_whitespace_eater(self):
  2553. eq = self.assertEqual
  2554. s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.'
  2555. parts = decode_header(s)
  2556. eq(parts, [('Subject:', None), ('\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), ('zz.', None)])
  2557. hdr = make_header(parts)
  2558. eq(hdr.encode(),
  2559. 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.')
  2560. def test_broken_base64_header(self):
  2561. raises = self.assertRaises
  2562. s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3IQ?='
  2563. raises(errors.HeaderParseError, decode_header, s)
  2564. # Test RFC 2231 header parameters (en/de)coding
  2565. class TestRFC2231(TestEmailBase):
  2566. def test_get_param(self):
  2567. eq = self.assertEqual
  2568. msg = self._msgobj('msg_29.txt')
  2569. eq(msg.get_param('title'),
  2570. ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!'))
  2571. eq(msg.get_param('title', unquote=False),
  2572. ('us-ascii', 'en', '"This is even more ***fun*** isn\'t it!"'))
  2573. def test_set_param(self):
  2574. eq = self.assertEqual
  2575. msg = Message()
  2576. msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
  2577. charset='us-ascii')
  2578. eq(msg.get_param('title'),
  2579. ('us-ascii', '', 'This is even more ***fun*** isn\'t it!'))
  2580. msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
  2581. charset='us-ascii', language='en')
  2582. eq(msg.get_param('title'),
  2583. ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!'))
  2584. msg = self._msgobj('msg_01.txt')
  2585. msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
  2586. charset='us-ascii', language='en')
  2587. eq(msg.as_string(), """\
  2588. Return-Path: <bbb@zzz.org>
  2589. Delivered-To: bbb@zzz.org
  2590. Received: by mail.zzz.org (Postfix, from userid 889)
  2591. \tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT)
  2592. MIME-Version: 1.0
  2593. Content-Transfer-Encoding: 7bit
  2594. Message-ID: <15090.61304.110929.45684@aaa.zzz.org>
  2595. From: bbb@ddd.com (John X. Doe)
  2596. To: bbb@zzz.org
  2597. Subject: This is a test message
  2598. Date: Fri, 4 May 2001 14:05:44 -0400
  2599. Content-Type: text/plain; charset=us-ascii;
  2600. \ttitle*="us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21"
  2601. Hi,
  2602. Do you like this message?
  2603. -Me
  2604. """)
  2605. def test_del_param(self):
  2606. eq = self.ndiffAssertEqual
  2607. msg = self._msgobj('msg_01.txt')
  2608. msg.set_param('foo', 'bar', charset='us-ascii', language='en')
  2609. msg.set_param('title', 'This is even more ***fun*** isn\'t it!',
  2610. charset='us-ascii', language='en')
  2611. msg.del_param('foo', header='Content-Type')
  2612. eq(msg.as_string(), """\
  2613. Return-Path: <bbb@zzz.org>
  2614. Delivered-To: bbb@zzz.org
  2615. Received: by mail.zzz.org (Postfix, from userid 889)
  2616. \tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT)
  2617. MIME-Version: 1.0
  2618. Content-Transfer-Encoding: 7bit
  2619. Message-ID: <15090.61304.110929.45684@aaa.zzz.org>
  2620. From: bbb@ddd.com (John X. Doe)
  2621. To: bbb@zzz.org
  2622. Subject: This is a test message
  2623. Date: Fri, 4 May 2001 14:05:44 -0400
  2624. Content-Type: text/plain; charset="us-ascii";
  2625. \ttitle*="us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21"
  2626. Hi,
  2627. Do you like this message?
  2628. -Me
  2629. """)
  2630. def test_rfc2231_get_content_charset(self):
  2631. eq = self.assertEqual
  2632. msg = self._msgobj('msg_32.txt')
  2633. eq(msg.get_content_charset(), 'us-ascii')
  2634. def test_rfc2231_no_language_or_charset(self):
  2635. m = '''\
  2636. Content-Transfer-Encoding: 8bit
  2637. Content-Disposition: inline; filename="file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm"
  2638. Content-Type: text/html; NAME*0=file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEM; NAME*1=P_nsmail.htm
  2639. '''
  2640. msg = email.message_from_string(m)
  2641. param = msg.get_param('NAME')
  2642. self.failIf(isinstance(param, tuple))
  2643. self.assertEqual(
  2644. param,
  2645. 'file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm')
  2646. def test_rfc2231_no_language_or_charset_in_filename(self):
  2647. m = '''\
  2648. Content-Disposition: inline;
  2649. \tfilename*0*="''This%20is%20even%20more%20";
  2650. \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2651. \tfilename*2="is it not.pdf"
  2652. '''
  2653. msg = email.message_from_string(m)
  2654. self.assertEqual(msg.get_filename(),
  2655. 'This is even more ***fun*** is it not.pdf')
  2656. def test_rfc2231_no_language_or_charset_in_filename_encoded(self):
  2657. m = '''\
  2658. Content-Disposition: inline;
  2659. \tfilename*0*="''This%20is%20even%20more%20";
  2660. \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2661. \tfilename*2="is it not.pdf"
  2662. '''
  2663. msg = email.message_from_string(m)
  2664. self.assertEqual(msg.get_filename(),
  2665. 'This is even more ***fun*** is it not.pdf')
  2666. def test_rfc2231_partly_encoded(self):
  2667. m = '''\
  2668. Content-Disposition: inline;
  2669. \tfilename*0="''This%20is%20even%20more%20";
  2670. \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2671. \tfilename*2="is it not.pdf"
  2672. '''
  2673. msg = email.message_from_string(m)
  2674. self.assertEqual(
  2675. msg.get_filename(),
  2676. 'This%20is%20even%20more%20***fun*** is it not.pdf')
  2677. def test_rfc2231_partly_nonencoded(self):
  2678. m = '''\
  2679. Content-Disposition: inline;
  2680. \tfilename*0="This%20is%20even%20more%20";
  2681. \tfilename*1="%2A%2A%2Afun%2A%2A%2A%20";
  2682. \tfilename*2="is it not.pdf"
  2683. '''
  2684. msg = email.message_from_string(m)
  2685. self.assertEqual(
  2686. msg.get_filename(),
  2687. 'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20is it not.pdf')
  2688. def test_rfc2231_no_language_or_charset_in_boundary(self):
  2689. m = '''\
  2690. Content-Type: multipart/alternative;
  2691. \tboundary*0*="''This%20is%20even%20more%20";
  2692. \tboundary*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2693. \tboundary*2="is it not.pdf"
  2694. '''
  2695. msg = email.message_from_string(m)
  2696. self.assertEqual(msg.get_boundary(),
  2697. 'This is even more ***fun*** is it not.pdf')
  2698. def test_rfc2231_no_language_or_charset_in_charset(self):
  2699. # This is a nonsensical charset value, but tests the code anyway
  2700. m = '''\
  2701. Content-Type: text/plain;
  2702. \tcharset*0*="This%20is%20even%20more%20";
  2703. \tcharset*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2704. \tcharset*2="is it not.pdf"
  2705. '''
  2706. msg = email.message_from_string(m)
  2707. self.assertEqual(msg.get_content_charset(),
  2708. 'this is even more ***fun*** is it not.pdf')
  2709. def test_rfc2231_bad_encoding_in_filename(self):
  2710. m = '''\
  2711. Content-Disposition: inline;
  2712. \tfilename*0*="bogus'xx'This%20is%20even%20more%20";
  2713. \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2714. \tfilename*2="is it not.pdf"
  2715. '''
  2716. msg = email.message_from_string(m)
  2717. self.assertEqual(msg.get_filename(),
  2718. 'This is even more ***fun*** is it not.pdf')
  2719. def test_rfc2231_bad_encoding_in_charset(self):
  2720. m = """\
  2721. Content-Type: text/plain; charset*=bogus''utf-8%E2%80%9D
  2722. """
  2723. msg = email.message_from_string(m)
  2724. # This should return None because non-ascii characters in the charset
  2725. # are not allowed.
  2726. self.assertEqual(msg.get_content_charset(), None)
  2727. def test_rfc2231_bad_character_in_charset(self):
  2728. m = """\
  2729. Content-Type: text/plain; charset*=ascii''utf-8%E2%80%9D
  2730. """
  2731. msg = email.message_from_string(m)
  2732. # This should return None because non-ascii characters in the charset
  2733. # are not allowed.
  2734. self.assertEqual(msg.get_content_charset(), None)
  2735. def test_rfc2231_bad_character_in_filename(self):
  2736. m = '''\
  2737. Content-Disposition: inline;
  2738. \tfilename*0*="ascii'xx'This%20is%20even%20more%20";
  2739. \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20";
  2740. \tfilename*2*="is it not.pdf%E2"
  2741. '''
  2742. msg = email.message_from_string(m)
  2743. self.assertEqual(msg.get_filename(),
  2744. u'This is even more ***fun*** is it not.pdf\ufffd')
  2745. def test_rfc2231_unknown_encoding(self):
  2746. m = """\
  2747. Content-Transfer-Encoding: 8bit
  2748. Content-Disposition: inline; filename*=X-UNKNOWN''myfile.txt
  2749. """
  2750. msg = email.message_from_string(m)
  2751. self.assertEqual(msg.get_filename(), 'myfile.txt')
  2752. def test_rfc2231_single_tick_in_filename_extended(self):
  2753. eq = self.assertEqual
  2754. m = """\
  2755. Content-Type: application/x-foo;
  2756. \tname*0*=\"Frank's\"; name*1*=\" Document\"
  2757. """
  2758. msg = email.message_from_string(m)
  2759. charset, language, s = msg.get_param('name')
  2760. eq(charset, None)
  2761. eq(language, None)
  2762. eq(s, "Frank's Document")
  2763. def test_rfc2231_single_tick_in_filename(self):
  2764. m = """\
  2765. Content-Type: application/x-foo; name*0=\"Frank's\"; name*1=\" Document\"
  2766. """
  2767. msg = email.message_from_string(m)
  2768. param = msg.get_param('name')
  2769. self.failIf(isinstance(param, tuple))
  2770. self.assertEqual(param, "Frank's Document")
  2771. def test_rfc2231_tick_attack_extended(self):
  2772. eq = self.assertEqual
  2773. m = """\
  2774. Content-Type: application/x-foo;
  2775. \tname*0*=\"us-ascii'en-us'Frank's\"; name*1*=\" Document\"
  2776. """
  2777. msg = email.message_from_string(m)
  2778. charset, language, s = msg.get_param('name')
  2779. eq(charset, 'us-ascii')
  2780. eq(language, 'en-us')
  2781. eq(s, "Frank's Document")
  2782. def test_rfc2231_tick_attack(self):
  2783. m = """\
  2784. Content-Type: application/x-foo;
  2785. \tname*0=\"us-ascii'en-us'Frank's\"; name*1=\" Document\"
  2786. """
  2787. msg = email.message_from_string(m)
  2788. param = msg.get_param('name')
  2789. self.failIf(isinstance(param, tuple))
  2790. self.assertEqual(param, "us-ascii'en-us'Frank's Document")
  2791. def test_rfc2231_no_extended_values(self):
  2792. eq = self.assertEqual
  2793. m = """\
  2794. Content-Type: application/x-foo; name=\"Frank's Document\"
  2795. """
  2796. msg = email.message_from_string(m)
  2797. eq(msg.get_param('name'), "Frank's Document")
  2798. def test_rfc2231_encoded_then_unencoded_segments(self):
  2799. eq = self.assertEqual
  2800. m = """\
  2801. Content-Type: application/x-foo;
  2802. \tname*0*=\"us-ascii'en-us'My\";
  2803. \tname*1=\" Document\";
  2804. \tname*2*=\" For You\"
  2805. """
  2806. msg = email.message_from_string(m)
  2807. charset, language, s = msg.get_param('name')
  2808. eq(charset, 'us-ascii')
  2809. eq(language, 'en-us')
  2810. eq(s, 'My Document For You')
  2811. def test_rfc2231_unencoded_then_encoded_segments(self):
  2812. eq = self.assertEqual
  2813. m = """\
  2814. Content-Type: application/x-foo;
  2815. \tname*0=\"us-ascii'en-us'My\";
  2816. \tname*1*=\" Document\";
  2817. \tname*2*=\" For You\"
  2818. """
  2819. msg = email.message_from_string(m)
  2820. charset, language, s = msg.get_param('name')
  2821. eq(charset, 'us-ascii')
  2822. eq(language, 'en-us')
  2823. eq(s, 'My Document For You')
  2824. def _testclasses():
  2825. mod = sys.modules[__name__]
  2826. return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
  2827. def suite():
  2828. suite = unittest.TestSuite()
  2829. for testclass in _testclasses():
  2830. suite.addTest(unittest.makeSuite(testclass))
  2831. return suite
  2832. def test_main():
  2833. for testclass in _testclasses():
  2834. run_unittest(testclass)
  2835. if __name__ == '__main__':
  2836. unittest.main(defaultTest='suite')