move sections
[python/dscho.git] / Lib / test / test_pep247.py
blob7d7425e72c4bac3ce362785f1834612e0c7222fd
1 """
2 Test suite to check compilance with PEP 247, the standard API
3 for hashing algorithms
4 """
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)
12 import hmac
13 import md5
14 import sha
16 import unittest
17 from test import test_support
19 class Pep247Test(unittest.TestCase):
21 def check_module(self, module, key=None):
22 self.assertTrue(hasattr(module, 'digest_size'))
23 self.assertTrue(module.digest_size is None or module.digest_size > 0)
25 if not key is None:
26 obj1 = module.new(key)
27 obj2 = module.new(key, 'string')
29 h1 = module.new(key, 'string').digest()
30 obj3 = module.new(key)
31 obj3.update('string')
32 h2 = obj3.digest()
33 else:
34 obj1 = module.new()
35 obj2 = module.new('string')
37 h1 = module.new('string').digest()
38 obj3 = module.new()
39 obj3.update('string')
40 h2 = obj3.digest()
42 self.assertEquals(h1, h2)
44 self.assertTrue(hasattr(obj1, 'digest_size'))
46 if not module.digest_size is None:
47 self.assertEquals(obj1.digest_size, module.digest_size)
49 self.assertEquals(obj1.digest_size, len(h1))
50 obj1.update('string')
51 obj_copy = obj1.copy()
52 self.assertEquals(obj1.digest(), obj_copy.digest())
53 self.assertEquals(obj1.hexdigest(), obj_copy.hexdigest())
55 digest, hexdigest = obj1.digest(), obj1.hexdigest()
56 hd2 = ""
57 for byte in digest:
58 hd2 += '%02x' % ord(byte)
59 self.assertEquals(hd2, hexdigest)
61 def test_md5(self):
62 self.check_module(md5)
64 def test_sha(self):
65 self.check_module(sha)
67 def test_hmac(self):
68 self.check_module(hmac, key='abc')
70 def test_main():
71 test_support.run_unittest(Pep247Test)
73 if __name__ == '__main__':
74 test_main()