Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 1 | # |
| 2 | # Test suite to check compliance with PEP 247, the standard API for |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 3 | # hashing algorithms. |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 4 | # |
| 5 | |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 6 | import hmac |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 7 | |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame^] | 8 | import hmac |
| 9 | from test.test_support import verbose |
| 10 | |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 11 | def check_hash_module(module, key=None): |
| 12 | assert hasattr(module, 'digest_size'), "Must have digest_size" |
| 13 | assert (module.digest_size is None or |
| 14 | module.digest_size > 0), "digest_size must be None or positive" |
| 15 | |
| 16 | if key is not None: |
| 17 | obj1 = module.new(key) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 18 | obj2 = module.new(key, b"string") |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 19 | |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 20 | h1 = module.new(key, b"string").digest() |
| 21 | obj3 = module.new(key) ; obj3.update(b"string") ; h2 = obj3.digest() |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 22 | assert h1 == h2, "Hashes must match" |
| 23 | |
| 24 | else: |
| 25 | obj1 = module.new() |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 26 | obj2 = module.new(b"string") |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 27 | |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 28 | h1 = module.new(b"string").digest() |
| 29 | obj3 = module.new() ; obj3.update(b"string") ; h2 = obj3.digest() |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 30 | assert h1 == h2, "Hashes must match" |
| 31 | |
| 32 | assert hasattr(obj1, 'digest_size'), "Objects must have digest_size attr" |
| 33 | if module.digest_size is not None: |
| 34 | assert obj1.digest_size == module.digest_size, "digest_size must match" |
| 35 | assert obj1.digest_size == len(h1), "digest_size must match actual size" |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 36 | obj1.update(b"string") |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 37 | obj_copy = obj1.copy() |
| 38 | assert obj1.digest() == obj_copy.digest(), "Copied objects must match" |
| 39 | assert obj1.hexdigest() == obj_copy.hexdigest(), \ |
| 40 | "Copied objects must match" |
| 41 | digest, hexdigest = obj1.digest(), obj1.hexdigest() |
| 42 | hd2 = "" |
| 43 | for byte in digest: |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 44 | hd2 += "%02x" % byte |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 45 | assert hd2 == hexdigest, "hexdigest doesn't appear correct" |
| 46 | |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame^] | 47 | if verbose: |
| 48 | print('Module', module.__name__, 'seems to comply with PEP 247') |
| 49 | |
| 50 | |
| 51 | def test_main(): |
| 52 | check_hash_module(hmac, key=b'abc') |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | if __name__ == '__main__': |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame^] | 56 | test_main() |