__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/__init__.py: Self-test for PyCrypto
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self tests
  25. These tests should perform quickly and can ideally be used every time an
  26. application runs.
  27. """
  28. import sys
  29. import unittest
  30. from importlib import import_module
  31. from Crypto.Util.py3compat import StringIO
  32. class SelfTestError(Exception):
  33. def __init__(self, message, result):
  34. Exception.__init__(self, message, result)
  35. self.message = message
  36. self.result = result
  37. def run(module=None, verbosity=0, stream=None, tests=None, config=None, **kwargs):
  38. """Execute self-tests.
  39. This raises SelfTestError if any test is unsuccessful.
  40. You may optionally pass in a sub-module of SelfTest if you only want to
  41. perform some of the tests. For example, the following would test only the
  42. hash modules:
  43. Crypto.SelfTest.run(Crypto.SelfTest.Hash)
  44. """
  45. if config is None:
  46. config = {}
  47. suite = unittest.TestSuite()
  48. if module is None:
  49. if tests is None:
  50. tests = get_tests(config=config)
  51. suite.addTests(tests)
  52. else:
  53. if tests is None:
  54. suite.addTests(module.get_tests(config=config))
  55. else:
  56. raise ValueError("'module' and 'tests' arguments are mutually exclusive")
  57. if stream is None:
  58. kwargs['stream'] = StringIO()
  59. else:
  60. kwargs['stream'] = stream
  61. runner = unittest.TextTestRunner(verbosity=verbosity, **kwargs)
  62. result = runner.run(suite)
  63. if not result.wasSuccessful():
  64. if stream is None:
  65. sys.stderr.write(kwargs['stream'].getvalue())
  66. raise SelfTestError("Self-test failed", result)
  67. return result
  68. def get_tests(config={}):
  69. tests = []
  70. module_names = [
  71. "Cipher", "Hash", "Protocol", "PublicKey", "Random",
  72. "Util", "Signature", "IO", "Math",
  73. ]
  74. for name in module_names:
  75. module = import_module("Crypto.SelfTest." + name)
  76. tests += module.get_tests(config=config)
  77. return tests
  78. if __name__ == '__main__':
  79. def suite():
  80. return unittest.TestSuite(get_tests())
  81. unittest.main(defaultTest='suite')
  82. # vim:set ts=4 sw=4 sts=4 expandtab: