source: python/trunk/Lib/test/test_pep247.py

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 2.0 KB
Line 
1"""
2Test suite to check compilance with PEP 247, the standard API
3for hashing algorithms
4"""
5
6import warnings
7warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
8 DeprecationWarning)
9warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
10 DeprecationWarning)
11
12import hmac
13import md5
14import sha
15
16import unittest
17from test import test_support
18
19class Pep247Test(unittest.TestCase):
20
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)
24
25 if not key is None:
26 obj1 = module.new(key)
27 obj2 = module.new(key, 'string')
28
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')
36
37 h1 = module.new('string').digest()
38 obj3 = module.new()
39 obj3.update('string')
40 h2 = obj3.digest()
41
42 self.assertEqual(h1, h2)
43
44 self.assertTrue(hasattr(obj1, 'digest_size'))
45
46 if not module.digest_size is None:
47 self.assertEqual(obj1.digest_size, module.digest_size)
48
49 self.assertEqual(obj1.digest_size, len(h1))
50 obj1.update('string')
51 obj_copy = obj1.copy()
52 self.assertEqual(obj1.digest(), obj_copy.digest())
53 self.assertEqual(obj1.hexdigest(), obj_copy.hexdigest())
54
55 digest, hexdigest = obj1.digest(), obj1.hexdigest()
56 hd2 = ""
57 for byte in digest:
58 hd2 += '%02x' % ord(byte)
59 self.assertEqual(hd2, hexdigest)
60
61 def test_md5(self):
62 self.check_module(md5)
63
64 def test_sha(self):
65 self.check_module(sha)
66
67 def test_hmac(self):
68 self.check_module(hmac, key='abc')
69
70def test_main():
71 test_support.run_unittest(Pep247Test)
72
73if __name__ == '__main__':
74 test_main()
Note: See TracBrowser for help on using the repository browser.