]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """\r |
2 | Test suite to check compilance with PEP 247, the standard API\r | |
3 | for hashing algorithms\r | |
4 | """\r | |
5 | \r | |
6 | import warnings\r | |
7 | warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',\r | |
8 | DeprecationWarning)\r | |
9 | warnings.filterwarnings('ignore', 'the sha module is deprecated.*',\r | |
10 | DeprecationWarning)\r | |
11 | \r | |
12 | import hmac\r | |
13 | import md5\r | |
14 | import sha\r | |
15 | \r | |
16 | import unittest\r | |
17 | from test import test_support\r | |
18 | \r | |
19 | class Pep247Test(unittest.TestCase):\r | |
20 | \r | |
21 | def check_module(self, module, key=None):\r | |
22 | self.assertTrue(hasattr(module, 'digest_size'))\r | |
23 | self.assertTrue(module.digest_size is None or module.digest_size > 0)\r | |
24 | \r | |
25 | if not key is None:\r | |
26 | obj1 = module.new(key)\r | |
27 | obj2 = module.new(key, 'string')\r | |
28 | \r | |
29 | h1 = module.new(key, 'string').digest()\r | |
30 | obj3 = module.new(key)\r | |
31 | obj3.update('string')\r | |
32 | h2 = obj3.digest()\r | |
33 | else:\r | |
34 | obj1 = module.new()\r | |
35 | obj2 = module.new('string')\r | |
36 | \r | |
37 | h1 = module.new('string').digest()\r | |
38 | obj3 = module.new()\r | |
39 | obj3.update('string')\r | |
40 | h2 = obj3.digest()\r | |
41 | \r | |
42 | self.assertEqual(h1, h2)\r | |
43 | \r | |
44 | self.assertTrue(hasattr(obj1, 'digest_size'))\r | |
45 | \r | |
46 | if not module.digest_size is None:\r | |
47 | self.assertEqual(obj1.digest_size, module.digest_size)\r | |
48 | \r | |
49 | self.assertEqual(obj1.digest_size, len(h1))\r | |
50 | obj1.update('string')\r | |
51 | obj_copy = obj1.copy()\r | |
52 | self.assertEqual(obj1.digest(), obj_copy.digest())\r | |
53 | self.assertEqual(obj1.hexdigest(), obj_copy.hexdigest())\r | |
54 | \r | |
55 | digest, hexdigest = obj1.digest(), obj1.hexdigest()\r | |
56 | hd2 = ""\r | |
57 | for byte in digest:\r | |
58 | hd2 += '%02x' % ord(byte)\r | |
59 | self.assertEqual(hd2, hexdigest)\r | |
60 | \r | |
61 | def test_md5(self):\r | |
62 | self.check_module(md5)\r | |
63 | \r | |
64 | def test_sha(self):\r | |
65 | self.check_module(sha)\r | |
66 | \r | |
67 | def test_hmac(self):\r | |
68 | self.check_module(hmac, key='abc')\r | |
69 | \r | |
70 | def test_main():\r | |
71 | test_support.run_unittest(Pep247Test)\r | |
72 | \r | |
73 | if __name__ == '__main__':\r | |
74 | test_main()\r |