OLD | NEW |
(Empty) | |
| 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 |
| 25 """Self tests |
| 26 |
| 27 These tests should perform quickly and can ideally be used every time an |
| 28 application runs. |
| 29 """ |
| 30 |
| 31 __revision__ = "$Id$" |
| 32 |
| 33 import sys |
| 34 import unittest |
| 35 from StringIO import StringIO |
| 36 |
| 37 class SelfTestError(Exception): |
| 38 def __init__(self, message, result): |
| 39 Exception.__init__(self, message, result) |
| 40 self.message = message |
| 41 self.result = result |
| 42 |
| 43 def run(module=None, verbosity=0, stream=None, tests=None, config=None, **kwargs
): |
| 44 """Execute self-tests. |
| 45 |
| 46 This raises SelfTestError if any test is unsuccessful. |
| 47 |
| 48 You may optionally pass in a sub-module of SelfTest if you only want to |
| 49 perform some of the tests. For example, the following would test only the |
| 50 hash modules: |
| 51 |
| 52 Crypto.SelfTest.run(Crypto.SelfTest.Hash) |
| 53 |
| 54 """ |
| 55 if config is None: |
| 56 config = {} |
| 57 suite = unittest.TestSuite() |
| 58 if module is None: |
| 59 if tests is None: |
| 60 tests = get_tests(config=config) |
| 61 suite.addTests(tests) |
| 62 else: |
| 63 if tests is None: |
| 64 suite.addTests(module.get_tests(config=config)) |
| 65 else: |
| 66 raise ValueError("'module' and 'tests' arguments are mutually exclus
ive") |
| 67 if stream is None: |
| 68 kwargs['stream'] = StringIO() |
| 69 runner = unittest.TextTestRunner(verbosity=verbosity, **kwargs) |
| 70 result = runner.run(suite) |
| 71 if not result.wasSuccessful(): |
| 72 if stream is None: |
| 73 sys.stderr.write(stream.getvalue()) |
| 74 raise SelfTestError("Self-test failed", result) |
| 75 return result |
| 76 |
| 77 def get_tests(config={}): |
| 78 tests = [] |
| 79 from Crypto.SelfTest import Cipher; tests += Cipher.get_tests(config=config) |
| 80 from Crypto.SelfTest import Hash; tests += Hash.get_tests(config=config) |
| 81 from Crypto.SelfTest import Protocol; tests += Protocol.get_tests(config=con
fig) |
| 82 from Crypto.SelfTest import PublicKey; tests += PublicKey.get_tests(config=c
onfig) |
| 83 from Crypto.SelfTest import Random; tests += Random.get_tests(config=config) |
| 84 from Crypto.SelfTest import Util; tests += Util.get_tests(config=config) |
| 85 from Crypto.SelfTest import Signature; tests += Signature.get_tests(config
=config) |
| 86 return tests |
| 87 |
| 88 if __name__ == '__main__': |
| 89 suite = lambda: unittest.TestSuite(get_tests()) |
| 90 unittest.main(defaultTest='suite') |
| 91 |
| 92 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |