blob: 733109a16e796aa3a4b60d3ee3a6defbdadf334f [file] [log] [blame]
Christian Heimesb186d002008-03-18 15:15:01 +00001"""
2Test suite to check compilance with PEP 247, the standard API
3for hashing algorithms
4"""
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +00005
Guido van Rossuma8add0e2007-05-14 22:03:55 +00006import hmac
Christian Heimesb186d002008-03-18 15:15:01 +00007import unittest
Neal Norwitzc598b6f2008-03-18 20:47:14 +00008from hashlib import md5, sha1, sha224, sha256, sha384, sha512
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000010
Christian Heimesb186d002008-03-18 15:15:01 +000011class Pep247Test(unittest.TestCase):
Christian Heimes180510d2008-03-03 19:15:45 +000012
Christian Heimesb186d002008-03-18 15:15:01 +000013 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 Norwitzc598b6f2008-03-18 20:47:14 +000016 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 Heimesb186d002008-03-18 15:15:01 +000025 h2 = obj3.digest()
26 else:
Neal Norwitzc598b6f2008-03-18 20:47:14 +000027 obj1 = cls()
28 obj2 = cls(b'string')
29 h1 = cls(b'string').digest()
30 obj3 = cls()
31 obj3.update(b'string')
Christian Heimesb186d002008-03-18 15:15:01 +000032 h2 = obj3.digest()
33 self.assertEquals(h1, h2)
34 self.assert_(hasattr(obj1, 'digest_size'))
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000035
Neal Norwitzc598b6f2008-03-18 20:47:14 +000036 if digest_size is not None:
37 self.assertEquals(obj1.digest_size, digest_size)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000038
Christian Heimesb186d002008-03-18 15:15:01 +000039 self.assertEquals(obj1.digest_size, len(h1))
Neal Norwitzc598b6f2008-03-18 20:47:14 +000040 obj1.update(b'string')
Christian Heimesb186d002008-03-18 15:15:01 +000041 obj_copy = obj1.copy()
42 self.assertEquals(obj1.digest(), obj_copy.digest())
43 self.assertEquals(obj1.hexdigest(), obj_copy.hexdigest())
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000044
Christian Heimesb186d002008-03-18 15:15:01 +000045 digest, hexdigest = obj1.digest(), obj1.hexdigest()
46 hd2 = ""
47 for byte in digest:
Neal Norwitzc598b6f2008-03-18 20:47:14 +000048 hd2 += '%02x' % byte
Christian Heimesb186d002008-03-18 15:15:01 +000049 self.assertEquals(hd2, hexdigest)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000050
Christian Heimesb186d002008-03-18 15:15:01 +000051 def test_md5(self):
Neal Norwitzc598b6f2008-03-18 20:47:14 +000052 self.check_object(md5, None, None)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000053
Christian Heimesb186d002008-03-18 15:15:01 +000054 def test_sha(self):
Neal Norwitzc598b6f2008-03-18 20:47:14 +000055 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. Kuchlinga0b60352001-11-02 21:46:17 +000060
Christian Heimesb186d002008-03-18 15:15:01 +000061 def test_hmac(self):
Neal Norwitzc598b6f2008-03-18 20:47:14 +000062 self.check_module(hmac, key=b'abc')
Christian Heimes180510d2008-03-03 19:15:45 +000063
64def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000065 support.run_unittest(Pep247Test)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000066
67if __name__ == '__main__':
Christian Heimes180510d2008-03-03 19:15:45 +000068 test_main()