06d9ec1b7ebf694d947d0f0644726d038e78eb09
[python-cryptoplus.git] / src / CryptoPlus / SelfTest / __init__.py
blob06d9ec1b7ebf694d947d0f0644726d038e78eb09
1 # -*- coding: utf-8 -*-
3 # SelfTest/__init__.py: Self-test for PyCryptoPlus
5 # =======================================================================
6 # Copyright (C) 2008 Dwayne C. Litzenberger <dlitz@dlitz.net>
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 # =======================================================================
29 """Self tests
31 These tests should perform quickly and can ideally be used every time an
32 application runs.
33 """
34 __revision__ = "$Id$"
36 #import st_common
38 #__all__ = ["st_common"]
40 import sys
41 import unittest
42 import StringIO
44 class SelfTestError(Exception):
45 def __init__(self, message, result):
46 Exception.__init__(self, message, result)
47 self.message = message
48 self.result = result
50 def run(module=None, verbosity=0, stream=None, **kwargs):
51 """Execute self-tests.
53 This raises SelfTestError if any test is unsuccessful.
55 You may optionally pass in a sub-module of SelfTest if you only want to
56 perform some of the tests. For example, the following would test only the
57 hash modules:
59 CryptoPlus.SelfTest.run(CryptoPlus.SelfTest.Hash)
61 """
62 suite = unittest.TestSuite()
63 if module is None:
64 suite.addTests(get_tests())
65 else:
66 suite.addTests(module.get_tests())
67 if stream is None:
68 kwargs['stream'] = StringIO.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
77 def get_tests():
78 tests = []
79 import Cipher; tests += Cipher.get_tests()
80 import Hash; tests += Hash.get_tests()
81 import PublicKey; tests += PublicKey.get_tests()
82 import Random; tests += Random.get_tests()
83 import Util; tests += Util.get_tests()
84 return tests
86 if __name__ == '__main__':
87 suite = lambda: unittest.TestSuite(get_tests())
88 unittest.main(defaultTest='suite')
90 # vim:set ts=4 sw=4 sts=4 expandtab: