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 # =======================================================================
31 These tests should perform quickly and can ideally be used every time an
38 #__all__ = ["st_common"]
44 class SelfTestError(Exception):
45 def __init__(self
, message
, result
):
46 Exception.__init
__(self
, message
, result
)
47 self
.message
= message
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
59 CryptoPlus.SelfTest.run(CryptoPlus.SelfTest.Hash)
62 suite
= unittest
.TestSuite()
64 suite
.addTests(get_tests())
66 suite
.addTests(module
.get_tests())
68 kwargs
['stream'] = StringIO
.StringIO()
69 runner
= unittest
.TextTestRunner(verbosity
=verbosity
, **kwargs
)
70 result
= runner
.run(suite
)
71 if not result
.wasSuccessful():
73 sys
.stderr
.write(stream
.getvalue())
74 raise SelfTestError("Self-test failed", result
)
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()
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: