2008-03-17 22:00:07 -03:00
|
|
|
"""
|
|
|
|
Test suite to check compilance with PEP 247, the standard API
|
|
|
|
for hashing algorithms
|
|
|
|
"""
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2007-05-30 19:24:28 -03:00
|
|
|
import warnings
|
2008-03-17 22:00:07 -03:00
|
|
|
warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
|
2007-05-30 19:24:28 -03:00
|
|
|
DeprecationWarning)
|
2008-03-17 22:00:07 -03:00
|
|
|
warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
|
2007-05-31 16:20:00 -03:00
|
|
|
DeprecationWarning)
|
2007-05-30 19:24:28 -03:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
import hmac
|
|
|
|
import md5
|
|
|
|
import sha
|
2008-03-02 23:26:43 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
class Pep247Test(unittest.TestCase):
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
def check_module(self, module, key=None):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hasattr(module, 'digest_size'))
|
|
|
|
self.assertTrue(module.digest_size is None or module.digest_size > 0)
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
if not key is None:
|
|
|
|
obj1 = module.new(key)
|
|
|
|
obj2 = module.new(key, 'string')
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
h1 = module.new(key, 'string').digest()
|
|
|
|
obj3 = module.new(key)
|
|
|
|
obj3.update('string')
|
|
|
|
h2 = obj3.digest()
|
|
|
|
else:
|
|
|
|
obj1 = module.new()
|
|
|
|
obj2 = module.new('string')
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
h1 = module.new('string').digest()
|
|
|
|
obj3 = module.new()
|
|
|
|
obj3.update('string')
|
|
|
|
h2 = obj3.digest()
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(h1, h2)
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hasattr(obj1, 'digest_size'))
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
if not module.digest_size is None:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(obj1.digest_size, module.digest_size)
|
2001-11-02 17:46:17 -04:00
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(obj1.digest_size, len(h1))
|
2008-03-17 22:00:07 -03:00
|
|
|
obj1.update('string')
|
|
|
|
obj_copy = obj1.copy()
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(obj1.digest(), obj_copy.digest())
|
|
|
|
self.assertEqual(obj1.hexdigest(), obj_copy.hexdigest())
|
2008-03-17 22:00:07 -03:00
|
|
|
|
|
|
|
digest, hexdigest = obj1.digest(), obj1.hexdigest()
|
|
|
|
hd2 = ""
|
|
|
|
for byte in digest:
|
|
|
|
hd2 += '%02x' % ord(byte)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(hd2, hexdigest)
|
2008-03-17 22:00:07 -03:00
|
|
|
|
|
|
|
def test_md5(self):
|
|
|
|
self.check_module(md5)
|
2008-03-02 23:26:43 -04:00
|
|
|
|
2008-03-17 22:00:07 -03:00
|
|
|
def test_sha(self):
|
|
|
|
self.check_module(sha)
|
|
|
|
|
|
|
|
def test_hmac(self):
|
|
|
|
self.check_module(hmac, key='abc')
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(Pep247Test)
|
2008-03-02 23:26:43 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|