PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/Lib/test/test_pep247.py

http://unladen-swallow.googlecode.com/
Python | 74 lines | 57 code | 13 blank | 4 comment | 5 complexity | f3cb16e5028f50b6578d50a978c1fbe2 MD5 | raw file
 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.assert_(hasattr(module, 'digest_size'))
23        self.assert_(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.assertEquals(h1, h2)
43
44        self.assert_(hasattr(obj1, 'digest_size'))
45
46        if not module.digest_size is None:
47            self.assertEquals(obj1.digest_size, module.digest_size)
48
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())
54
55        digest, hexdigest = obj1.digest(), obj1.hexdigest()
56        hd2 = ""
57        for byte in digest:
58            hd2 += '%02x' % ord(byte)
59        self.assertEquals(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()