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