test_email.py 130 KB

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