mirror of
https://github.com/python/cpython.git
synced 2025-11-11 14:44:57 +00:00
svn+ssh://pythondev@svn.python.org/python/trunk ........ r61189 | brett.cannon | 2008-03-03 01:38:58 +0100 (Mon, 03 Mar 2008) | 5 lines Refactor test_logging to use unittest. This should finally solve the flakiness issues. Thanks to Antoine Pitrou for the patch. ........ r61190 | jeffrey.yasskin | 2008-03-03 02:27:03 +0100 (Mon, 03 Mar 2008) | 3 lines compile.c always emits END_FINALLY after WITH_CLEANUP, so predict that in ceval.c. This is worth about a .03-.04us speedup on a simple with block. ........ r61192 | brett.cannon | 2008-03-03 03:41:40 +0100 (Mon, 03 Mar 2008) | 4 lines Move test_largefile over to using 'with' statements for open files. Also rename the driver function to test_main() instead of main_test(). ........ r61194 | brett.cannon | 2008-03-03 04:24:48 +0100 (Mon, 03 Mar 2008) | 3 lines Add a note in the main test class' docstring that the order of execution of the tests is important. ........ r61195 | brett.cannon | 2008-03-03 04:26:43 +0100 (Mon, 03 Mar 2008) | 3 lines Add a note in the main test class' docstring that the order of execution of the tests is important. ........ r61198 | brett.cannon | 2008-03-03 05:19:29 +0100 (Mon, 03 Mar 2008) | 4 lines Add test_main() functions to various tests where it was simple to do. Done so that regrtest can execute the test_main() directly instead of relying on import side-effects. ........ r61199 | neal.norwitz | 2008-03-03 05:37:45 +0100 (Mon, 03 Mar 2008) | 1 line Only DECREF if ret != NULL ........
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
#
|
|
# Test suite to check compliance with PEP 247, the standard API for
|
|
# hashing algorithms.
|
|
#
|
|
|
|
import hmac
|
|
|
|
import hmac
|
|
from test.test_support import verbose
|
|
|
|
def check_hash_module(module, key=None):
|
|
assert hasattr(module, 'digest_size'), "Must have digest_size"
|
|
assert (module.digest_size is None or
|
|
module.digest_size > 0), "digest_size must be None or positive"
|
|
|
|
if key is not None:
|
|
obj1 = module.new(key)
|
|
obj2 = module.new(key, b"string")
|
|
|
|
h1 = module.new(key, b"string").digest()
|
|
obj3 = module.new(key) ; obj3.update(b"string") ; h2 = obj3.digest()
|
|
assert h1 == h2, "Hashes must match"
|
|
|
|
else:
|
|
obj1 = module.new()
|
|
obj2 = module.new(b"string")
|
|
|
|
h1 = module.new(b"string").digest()
|
|
obj3 = module.new() ; obj3.update(b"string") ; h2 = obj3.digest()
|
|
assert h1 == h2, "Hashes must match"
|
|
|
|
assert hasattr(obj1, 'digest_size'), "Objects must have digest_size attr"
|
|
if module.digest_size is not None:
|
|
assert obj1.digest_size == module.digest_size, "digest_size must match"
|
|
assert obj1.digest_size == len(h1), "digest_size must match actual size"
|
|
obj1.update(b"string")
|
|
obj_copy = obj1.copy()
|
|
assert obj1.digest() == obj_copy.digest(), "Copied objects must match"
|
|
assert obj1.hexdigest() == obj_copy.hexdigest(), \
|
|
"Copied objects must match"
|
|
digest, hexdigest = obj1.digest(), obj1.hexdigest()
|
|
hd2 = ""
|
|
for byte in digest:
|
|
hd2 += "%02x" % byte
|
|
assert hd2 == hexdigest, "hexdigest doesn't appear correct"
|
|
|
|
if verbose:
|
|
print('Module', module.__name__, 'seems to comply with PEP 247')
|
|
|
|
|
|
def test_main():
|
|
check_hash_module(hmac, key=b'abc')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
test_main()
|