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 | |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 6 | import hmac |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 7 | import unittest |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 8 | from hashlib import md5, sha1, sha224, sha256, sha384, sha512 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 10 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11 | class Pep247Test(unittest.TestCase): |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 12 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 13 | def check_module(self, module, key=None): |
| 14 | self.assert_(hasattr(module, 'digest_size')) |
| 15 | self.assert_(module.digest_size is None or module.digest_size > 0) |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 16 | self.check_object(module.new, module.digest_size, key) |
| 17 | |
| 18 | def check_object(self, cls, digest_size, key): |
| 19 | if key is not None: |
| 20 | obj1 = cls(key) |
| 21 | obj2 = cls(key, b'string') |
| 22 | h1 = cls(key, b'string').digest() |
| 23 | obj3 = cls(key) |
| 24 | obj3.update(b'string') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 25 | h2 = obj3.digest() |
| 26 | else: |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 27 | obj1 = cls() |
| 28 | obj2 = cls(b'string') |
| 29 | h1 = cls(b'string').digest() |
| 30 | obj3 = cls() |
| 31 | obj3.update(b'string') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 32 | h2 = obj3.digest() |
| 33 | self.assertEquals(h1, h2) |
| 34 | self.assert_(hasattr(obj1, 'digest_size')) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 35 | |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 36 | if digest_size is not None: |
| 37 | self.assertEquals(obj1.digest_size, 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 | self.assertEquals(obj1.digest_size, len(h1)) |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 40 | obj1.update(b'string') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 41 | obj_copy = obj1.copy() |
| 42 | self.assertEquals(obj1.digest(), obj_copy.digest()) |
| 43 | self.assertEquals(obj1.hexdigest(), obj_copy.hexdigest()) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 44 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 45 | digest, hexdigest = obj1.digest(), obj1.hexdigest() |
| 46 | hd2 = "" |
| 47 | for byte in digest: |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 48 | hd2 += '%02x' % byte |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 49 | self.assertEquals(hd2, hexdigest) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 50 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 51 | def test_md5(self): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 52 | self.check_object(md5, None, None) |
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_sha(self): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 55 | self.check_object(sha1, None, None) |
| 56 | self.check_object(sha224, None, None) |
| 57 | self.check_object(sha256, None, None) |
| 58 | self.check_object(sha384, None, None) |
| 59 | self.check_object(sha512, None, None) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 60 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 61 | def test_hmac(self): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 62 | self.check_module(hmac, key=b'abc') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 63 | |
| 64 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 65 | support.run_unittest(Pep247Test) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 66 | |
| 67 | if __name__ == '__main__': |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 68 | test_main() |