/Lib/email/test/test_email_torture.py

http://unladen-swallow.googlecode.com/ · Python · 136 lines · 109 code · 16 blank · 11 comment · 9 complexity · eee02dc605b546a1d129b227add2a5ed MD5 · raw file

  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. import sys
  9. import os
  10. import unittest
  11. from cStringIO import StringIO
  12. from types import ListType
  13. from email.test.test_email import TestEmailBase
  14. from test.test_support import TestSkipped
  15. import email
  16. from email import __file__ as testfile
  17. from email.iterators import _structure
  18. def openfile(filename):
  19. from os.path import join, dirname, abspath
  20. path = abspath(join(dirname(testfile), os.pardir, 'moredata', filename))
  21. return open(path, 'r')
  22. # Prevent this test from running in the Python distro
  23. try:
  24. openfile('crispin-torture.txt')
  25. except IOError:
  26. raise TestSkipped
  27. class TortureBase(TestEmailBase):
  28. def _msgobj(self, filename):
  29. fp = openfile(filename)
  30. try:
  31. msg = email.message_from_file(fp)
  32. finally:
  33. fp.close()
  34. return msg
  35. class TestCrispinTorture(TortureBase):
  36. # Mark Crispin's torture test from the SquirrelMail project
  37. def test_mondo_message(self):
  38. eq = self.assertEqual
  39. neq = self.ndiffAssertEqual
  40. msg = self._msgobj('crispin-torture.txt')
  41. payload = msg.get_payload()
  42. eq(type(payload), ListType)
  43. eq(len(payload), 12)
  44. eq(msg.preamble, None)
  45. eq(msg.epilogue, '\n')
  46. # Probably the best way to verify the message is parsed correctly is to
  47. # dump its structure and compare it against the known structure.
  48. fp = StringIO()
  49. _structure(msg, fp=fp)
  50. neq(fp.getvalue(), """\
  51. multipart/mixed
  52. text/plain
  53. message/rfc822
  54. multipart/alternative
  55. text/plain
  56. multipart/mixed
  57. text/richtext
  58. application/andrew-inset
  59. message/rfc822
  60. audio/basic
  61. audio/basic
  62. image/pbm
  63. message/rfc822
  64. multipart/mixed
  65. multipart/mixed
  66. text/plain
  67. audio/x-sun
  68. multipart/mixed
  69. image/gif
  70. image/gif
  71. application/x-be2
  72. application/atomicmail
  73. audio/x-sun
  74. message/rfc822
  75. multipart/mixed
  76. text/plain
  77. image/pgm
  78. text/plain
  79. message/rfc822
  80. multipart/mixed
  81. text/plain
  82. image/pbm
  83. message/rfc822
  84. application/postscript
  85. image/gif
  86. message/rfc822
  87. multipart/mixed
  88. audio/basic
  89. audio/basic
  90. message/rfc822
  91. multipart/mixed
  92. application/postscript
  93. text/plain
  94. message/rfc822
  95. multipart/mixed
  96. text/plain
  97. multipart/parallel
  98. image/gif
  99. audio/basic
  100. application/atomicmail
  101. message/rfc822
  102. audio/x-sun
  103. """)
  104. def _testclasses():
  105. mod = sys.modules[__name__]
  106. return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
  107. def suite():
  108. suite = unittest.TestSuite()
  109. for testclass in _testclasses():
  110. suite.addTest(unittest.makeSuite(testclass))
  111. return suite
  112. def test_main():
  113. for testclass in _testclasses():
  114. test_support.run_unittest(testclass)
  115. if __name__ == '__main__':
  116. unittest.main(defaultTest='suite')