__init__.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. __revision__ = "$Id$"
  29. import sys
  30. import unittest
  31. from Cryptodome.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. Cryptodome.SelfTest.run(Cryptodome.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. from Cryptodome.SelfTest import Cipher; tests += Cipher.get_tests(config=config)
  71. from Cryptodome.SelfTest import Hash; tests += Hash.get_tests(config=config)
  72. from Cryptodome.SelfTest import Protocol; tests += Protocol.get_tests(config=config)
  73. from Cryptodome.SelfTest import PublicKey; tests += PublicKey.get_tests(config=config)
  74. from Cryptodome.SelfTest import Random; tests += Random.get_tests(config=config)
  75. from Cryptodome.SelfTest import Util; tests += Util.get_tests(config=config)
  76. from Cryptodome.SelfTest import Signature; tests += Signature.get_tests(config=config)
  77. from Cryptodome.SelfTest import IO; tests += IO.get_tests(config=config)
  78. from Cryptodome.SelfTest import Math; tests += Math.get_tests(config=config)
  79. return tests
  80. if __name__ == '__main__':
  81. suite = lambda: unittest.TestSuite(get_tests())
  82. unittest.main(defaultTest='suite')
  83. # vim:set ts=4 sw=4 sts=4 expandtab: