blob: 691c85a3914a80eae16910c860373e32fda46fb6 [file] [log] [blame]
Brett Cannon6eeaddc2008-03-18 01:00:07 +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
Brett Cannon7eec2172007-05-30 22:24:28 +00006import warnings
Brett Cannon6eeaddc2008-03-18 01:00:07 +00007warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
Brett Cannon7eec2172007-05-30 22:24:28 +00008 DeprecationWarning)
Brett Cannon6eeaddc2008-03-18 01:00:07 +00009warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
Brett Cannonc2aa09a2007-05-31 19:20:00 +000010 DeprecationWarning)
Brett Cannon7eec2172007-05-30 22:24:28 +000011
Brett Cannon6eeaddc2008-03-18 01:00:07 +000012import hmac
13import md5
14import sha
Brett Cannon963c80f2008-03-03 03:26:43 +000015
Brett Cannon6eeaddc2008-03-18 01:00:07 +000016import unittest
17from test import test_support
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000018
Brett Cannon6eeaddc2008-03-18 01:00:07 +000019class Pep247Test(unittest.TestCase):
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000020
Brett Cannon6eeaddc2008-03-18 01:00:07 +000021 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)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000024
Brett Cannon6eeaddc2008-03-18 01:00:07 +000025 if not key is None:
26 obj1 = module.new(key)
27 obj2 = module.new(key, 'string')
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000028
Brett Cannon6eeaddc2008-03-18 01:00:07 +000029 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')
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000036
Brett Cannon6eeaddc2008-03-18 01:00:07 +000037 h1 = module.new('string').digest()
38 obj3 = module.new()
39 obj3.update('string')
40 h2 = obj3.digest()
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000041
Brett Cannon6eeaddc2008-03-18 01:00:07 +000042 self.assertEquals(h1, h2)
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000043
Brett Cannon6eeaddc2008-03-18 01:00:07 +000044 self.assert_(hasattr(obj1, 'digest_size'))
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000045
Brett Cannon6eeaddc2008-03-18 01:00:07 +000046 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')
Andrew M. Kuchlinga0b60352001-11-02 21:46:17 +000069
Brett Cannon963c80f2008-03-03 03:26:43 +000070def test_main():
Brett Cannon6eeaddc2008-03-18 01:00:07 +000071 test_support.run_unittest(Pep247Test)
Brett Cannon963c80f2008-03-03 03:26:43 +000072
73if __name__ == '__main__':
74 test_main()