Home | History | Annotate | Download | only in test
      1 # Copyright (C) 2002-2004 Python Software Foundation
      2 #
      3 # A torture test of the email package.  This should not be run as part of the
      4 # standard Python test suite since it requires several meg of email messages
      5 # collected in the wild.  These source messages are not checked into the
      6 # Python distro, but are available as part of the standalone email package at
      7 # http://sf.net/projects/mimelib
      8 
      9 import sys
     10 import os
     11 import unittest
     12 from cStringIO import StringIO
     13 from types import ListType
     14 
     15 from email.test.test_email import TestEmailBase
     16 from test.test_support import TestSkipped, run_unittest
     17 
     18 import email
     19 from email import __file__ as testfile
     20 from email.iterators import _structure
     21 
     22 def openfile(filename):
     23     from os.path import join, dirname, abspath
     24     path = abspath(join(dirname(testfile), os.pardir, 'moredata', filename))
     25     return open(path, 'r')
     26 
     27 # Prevent this test from running in the Python distro
     28 try:
     29     openfile('crispin-torture.txt')
     30 except IOError:
     31     raise TestSkipped
     32 
     33 
     34 
     36 class TortureBase(TestEmailBase):
     37     def _msgobj(self, filename):
     38         fp = openfile(filename)
     39         try:
     40             msg = email.message_from_file(fp)
     41         finally:
     42             fp.close()
     43         return msg
     44 
     45 
     46 
     48 class TestCrispinTorture(TortureBase):
     49     # Mark Crispin's torture test from the SquirrelMail project
     50     def test_mondo_message(self):
     51         eq = self.assertEqual
     52         neq = self.ndiffAssertEqual
     53         msg = self._msgobj('crispin-torture.txt')
     54         payload = msg.get_payload()
     55         eq(type(payload), ListType)
     56         eq(len(payload), 12)
     57         eq(msg.preamble, None)
     58         eq(msg.epilogue, '\n')
     59         # Probably the best way to verify the message is parsed correctly is to
     60         # dump its structure and compare it against the known structure.
     61         fp = StringIO()
     62         _structure(msg, fp=fp)
     63         neq(fp.getvalue(), """\
     64 multipart/mixed
     65     text/plain
     66     message/rfc822
     67         multipart/alternative
     68             text/plain
     69             multipart/mixed
     70                 text/richtext
     71             application/andrew-inset
     72     message/rfc822
     73         audio/basic
     74     audio/basic
     75     image/pbm
     76     message/rfc822
     77         multipart/mixed
     78             multipart/mixed
     79                 text/plain
     80                 audio/x-sun
     81             multipart/mixed
     82                 image/gif
     83                 image/gif
     84                 application/x-be2
     85                 application/atomicmail
     86             audio/x-sun
     87     message/rfc822
     88         multipart/mixed
     89             text/plain
     90             image/pgm
     91             text/plain
     92     message/rfc822
     93         multipart/mixed
     94             text/plain
     95             image/pbm
     96     message/rfc822
     97         application/postscript
     98     image/gif
     99     message/rfc822
    100         multipart/mixed
    101             audio/basic
    102             audio/basic
    103     message/rfc822
    104         multipart/mixed
    105             application/postscript
    106             text/plain
    107             message/rfc822
    108                 multipart/mixed
    109                     text/plain
    110                     multipart/parallel
    111                         image/gif
    112                         audio/basic
    113                     application/atomicmail
    114                     message/rfc822
    115                         audio/x-sun
    116 """)
    117 
    118 
    120 def _testclasses():
    121     mod = sys.modules[__name__]
    122     return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
    123 
    124 
    125 def suite():
    126     suite = unittest.TestSuite()
    127     for testclass in _testclasses():
    128         suite.addTest(unittest.makeSuite(testclass))
    129     return suite
    130 
    131 
    132 def test_main():
    133     for testclass in _testclasses():
    134         run_unittest(testclass)
    135 
    136 
    137 
    139 if __name__ == '__main__':
    140     unittest.main(defaultTest='suite')
    141