blob: fb59d6a1ca9614565c60bf550c2f6cf466fb0ab1 [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
Christian Heimesb186d002008-03-18 15:15:01 +00006import warnings
7warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
8 DeprecationWarning)
9warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
10 DeprecationWarning)
Guido van Rossuma8add0e2007-05-14 22:03:55 +000011import hmac
Christian Heimesb186d002008-03-18 15:15:01 +000012import md5
13import sha
14import unittest
15from test import test_support
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000016
Christian Heimesb186d002008-03-18 15:15:01 +000017class Pep247Test(unittest.TestCase):
Christian Heimes180510d2008-03-03 19:15:45 +000018
Christian Heimesb186d002008-03-18 15:15:01 +000019 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. Kuchlinga0b60352001-11-02 21:46:17 +000038
Christian Heimesb186d002008-03-18 15:15:01 +000039 if not module.digest_size is None:
40 self.assertEquals(obj1.digest_size, module.digest_size)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000041
Christian Heimesb186d002008-03-18 15:15:01 +000042 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. Kuchlinga0b60352001-11-02 21:46:17 +000047
Christian Heimesb186d002008-03-18 15:15:01 +000048 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. Kuchlinga0b60352001-11-02 21:46:17 +000053
Christian Heimesb186d002008-03-18 15:15:01 +000054 def test_md5(self):
55 self.check_module(md5)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000056
Christian Heimesb186d002008-03-18 15:15:01 +000057 def test_sha(self):
58 self.check_module(sha)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000059
Christian Heimesb186d002008-03-18 15:15:01 +000060 def test_hmac(self):
61 self.check_module(hmac, key='abc')
Christian Heimes180510d2008-03-03 19:15:45 +000062
63def test_main():
Christian Heimesb186d002008-03-18 15:15:01 +000064 test_support.run_unittest(Pep247Test)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000065
66if __name__ == '__main__':
Christian Heimes180510d2008-03-03 19:15:45 +000067 test_main()