PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Lib/email/test/test_email.py

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