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 |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 9 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 10 | class Pep247Test(unittest.TestCase): |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 11 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12 | def check_module(self, module, key=None): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 13 | self.assertTrue(hasattr(module, 'digest_size')) |
| 14 | self.assertTrue(module.digest_size is None or module.digest_size > 0) |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 15 | self.check_object(module.new, module.digest_size, key) |
| 16 | |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 17 | def check_object(self, cls, digest_size, key, digestmod=None): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 18 | if key is not None: |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 19 | if digestmod is None: |
| 20 | digestmod = md5 |
| 21 | obj1 = cls(key, digestmod=digestmod) |
| 22 | obj2 = cls(key, b'string', digestmod=digestmod) |
| 23 | h1 = cls(key, b'string', digestmod=digestmod).digest() |
| 24 | obj3 = cls(key, digestmod=digestmod) |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 25 | obj3.update(b'string') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 26 | h2 = obj3.digest() |
| 27 | else: |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 28 | obj1 = cls() |
| 29 | obj2 = cls(b'string') |
| 30 | h1 = cls(b'string').digest() |
| 31 | obj3 = cls() |
| 32 | obj3.update(b'string') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 33 | h2 = obj3.digest() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 34 | self.assertEqual(h1, h2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertTrue(hasattr(obj1, 'digest_size')) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 36 | |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 37 | if digest_size is not None: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 38 | self.assertEqual(obj1.digest_size, digest_size) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 39 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 40 | self.assertEqual(obj1.digest_size, len(h1)) |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 41 | obj1.update(b'string') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 42 | obj_copy = obj1.copy() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 43 | self.assertEqual(obj1.digest(), obj_copy.digest()) |
| 44 | self.assertEqual(obj1.hexdigest(), obj_copy.hexdigest()) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 45 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 46 | digest, hexdigest = obj1.digest(), obj1.hexdigest() |
| 47 | hd2 = "" |
| 48 | for byte in digest: |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 49 | hd2 += '%02x' % byte |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 50 | self.assertEqual(hd2, hexdigest) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 51 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 52 | def test_md5(self): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 53 | self.check_object(md5, None, None) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 54 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 55 | def test_sha(self): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 56 | self.check_object(sha1, None, None) |
| 57 | self.check_object(sha224, None, None) |
| 58 | self.check_object(sha256, None, None) |
| 59 | self.check_object(sha384, None, None) |
| 60 | self.check_object(sha512, None, None) |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 61 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 62 | def test_hmac(self): |
Neal Norwitz | c598b6f | 2008-03-18 20:47:14 +0000 | [diff] [blame] | 63 | self.check_module(hmac, key=b'abc') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 64 | |
Andrew M. Kuchling | a0b6035 | 2001-11-02 21:46:17 +0000 | [diff] [blame] | 65 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 66 | unittest.main() |