PyXR

c:\python24\lib \ email \ test \ test_email_torture.py



0001 # Copyright (C) 2002-2004 Python Software Foundation
0002 #
0003 # A torture test of the email package.  This should not be run as part of the
0004 # standard Python test suite since it requires several meg of email messages
0005 # collected in the wild.  These source messages are not checked into the
0006 # Python distro, but are available as part of the standalone email package at
0007 # http://sf.net/projects/mimelib
0008 
0009 import sys
0010 import os
0011 import unittest
0012 from cStringIO import StringIO
0013 from types import ListType
0014 
0015 from email.test.test_email import TestEmailBase
0016 from test.test_support import TestSkipped
0017 
0018 import email
0019 from email import __file__ as testfile
0020 from email.Iterators import _structure
0021 
0022 def openfile(filename):
0023     from os.path import join, dirname, abspath
0024     path = abspath(join(dirname(testfile), os.pardir, 'moredata', filename))
0025     return open(path, 'r')
0026 
0027 # Prevent this test from running in the Python distro
0028 try:
0029     openfile('crispin-torture.txt')
0030 except IOError:
0031     raise TestSkipped
0032 
0033 
0034 
0035 class TortureBase(TestEmailBase):
0036     def _msgobj(self, filename):
0037         fp = openfile(filename)
0038         try:
0039             msg = email.message_from_file(fp)
0040         finally:
0041             fp.close()
0042         return msg
0043 
0044 
0045 
0046 class TestCrispinTorture(TortureBase):
0047     # Mark Crispin's torture test from the SquirrelMail project
0048     def test_mondo_message(self):
0049         eq = self.assertEqual
0050         neq = self.ndiffAssertEqual
0051         msg = self._msgobj('crispin-torture.txt')
0052         payload = msg.get_payload()
0053         eq(type(payload), ListType)
0054         eq(len(payload), 12)
0055         eq(msg.preamble, None)
0056         eq(msg.epilogue, '\n')
0057         # Probably the best way to verify the message is parsed correctly is to
0058         # dump its structure and compare it against the known structure.
0059         fp = StringIO()
0060         _structure(msg, fp=fp)
0061         neq(fp.getvalue(), """\
0062 multipart/mixed
0063     text/plain
0064     message/rfc822
0065         multipart/alternative
0066             text/plain
0067             multipart/mixed
0068                 text/richtext
0069             application/andrew-inset
0070     message/rfc822
0071         audio/basic
0072     audio/basic
0073     image/pbm
0074     message/rfc822
0075         multipart/mixed
0076             multipart/mixed
0077                 text/plain
0078                 audio/x-sun
0079             multipart/mixed
0080                 image/gif
0081                 image/gif
0082                 application/x-be2
0083                 application/atomicmail
0084             audio/x-sun
0085     message/rfc822
0086         multipart/mixed
0087             text/plain
0088             image/pgm
0089             text/plain
0090     message/rfc822
0091         multipart/mixed
0092             text/plain
0093             image/pbm
0094     message/rfc822
0095         application/postscript
0096     image/gif
0097     message/rfc822
0098         multipart/mixed
0099             audio/basic
0100             audio/basic
0101     message/rfc822
0102         multipart/mixed
0103             application/postscript
0104             text/plain
0105             message/rfc822
0106                 multipart/mixed
0107                     text/plain
0108                     multipart/parallel
0109                         image/gif
0110                         audio/basic
0111                     application/atomicmail
0112                     message/rfc822
0113                         audio/x-sun
0114 """)
0115 
0116 
0117 def _testclasses():
0118     mod = sys.modules[__name__]
0119     return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
0120 
0121 
0122 def suite():
0123     suite = unittest.TestSuite()
0124     for testclass in _testclasses():
0125         suite.addTest(unittest.makeSuite(testclass))
0126     return suite
0127 
0128 
0129 def test_main():
0130     for testclass in _testclasses():
0131         test_support.run_unittest(testclass)
0132 
0133 
0134 
0135 if __name__ == '__main__':
0136     unittest.main(defaultTest='suite')
0137 

Generated by PyXR 0.9.4
SourceForge.net Logo