Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 1 | """ |
| 2 | Test suite to check compilance with PEP 247, the standard API |
| 3 | for hashing algorithms |
| 4 | """ |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 5 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 6 | import warnings |
| 7 | warnings.filterwarnings('ignore', 'the md5 module is deprecated.*', |
| 8 | DeprecationWarning) |
| 9 | warnings.filterwarnings('ignore', 'the sha module is deprecated.*', |
| 10 | DeprecationWarning) |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 11 | import hmac |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 12 | import md5 |
| 13 | import sha |
| 14 | import unittest |
| 15 | from test import test_support |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 16 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 17 | class Pep247Test(unittest.TestCase): |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 18 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 19 | def check_module(self, module, key=None): |
| 20 | self.assert_(hasattr(module, 'digest_size')) |
| 21 | self.assert_(module.digest_size is None or module.digest_size > 0) |
| 22 | if not key is None: |
| 23 | obj1 = module.new(key) |
| 24 | obj2 = module.new(key, 'string') |
| 25 | h1 = module.new(key, 'string').digest() |
| 26 | obj3 = module.new(key) |
| 27 | obj3.update('string') |
| 28 | h2 = obj3.digest() |
| 29 | else: |
| 30 | obj1 = module.new() |
| 31 | obj2 = module.new('string') |
| 32 | h1 = module.new('string').digest() |
| 33 | obj3 = module.new() |
| 34 | obj3.update('string') |
| 35 | h2 = obj3.digest() |
| 36 | self.assertEquals(h1, h2) |
| 37 | self.assert_(hasattr(obj1, 'digest_size')) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 38 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 39 | if not module.digest_size is None: |
| 40 | self.assertEquals(obj1.digest_size, module.digest_size) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 41 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 42 | self.assertEquals(obj1.digest_size, len(h1)) |
| 43 | obj1.update('string') |
| 44 | obj_copy = obj1.copy() |
| 45 | self.assertEquals(obj1.digest(), obj_copy.digest()) |
| 46 | self.assertEquals(obj1.hexdigest(), obj_copy.hexdigest()) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 47 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 48 | digest, hexdigest = obj1.digest(), obj1.hexdigest() |
| 49 | hd2 = "" |
| 50 | for byte in digest: |
| 51 | hd2 += '%02x' % ord(byte) |
| 52 | self.assertEquals(hd2, hexdigest) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 53 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 54 | def test_md5(self): |
| 55 | self.check_module(md5) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 56 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 57 | def test_sha(self): |
| 58 | self.check_module(sha) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 59 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 60 | def test_hmac(self): |
| 61 | self.check_module(hmac, key='abc') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 62 | |
| 63 | def test_main(): |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame^] | 64 | test_support.run_unittest(Pep247Test) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 65 | |
| 66 | if __name__ == '__main__': |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 67 | test_main() |