10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 20a8c90248264a8b26970b4473770bcc3df8515fJosh GaoTest suite to check compilance with PEP 247, the standard API 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor hashing algorithms 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport warnings 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowarnings.filterwarnings('ignore', 'the md5 module is deprecated.*', 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DeprecationWarning) 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowarnings.filterwarnings('ignore', 'the sha module is deprecated.*', 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DeprecationWarning) 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport hmac 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport md5 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sha 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Pep247Test(unittest.TestCase): 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def check_module(self, module, key=None): 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(hasattr(module, 'digest_size')) 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(module.digest_size is None or module.digest_size > 0) 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not key is None: 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj1 = module.new(key) 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj2 = module.new(key, 'string') 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao h1 = module.new(key, 'string').digest() 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj3 = module.new(key) 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj3.update('string') 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao h2 = obj3.digest() 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj1 = module.new() 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj2 = module.new('string') 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao h1 = module.new('string').digest() 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj3 = module.new() 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj3.update('string') 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao h2 = obj3.digest() 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(h1, h2) 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(hasattr(obj1, 'digest_size')) 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not module.digest_size is None: 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(obj1.digest_size, module.digest_size) 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(obj1.digest_size, len(h1)) 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj1.update('string') 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj_copy = obj1.copy() 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(obj1.digest(), obj_copy.digest()) 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(obj1.hexdigest(), obj_copy.hexdigest()) 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digest, hexdigest = obj1.digest(), obj1.hexdigest() 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hd2 = "" 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for byte in digest: 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hd2 += '%02x' % ord(byte) 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(hd2, hexdigest) 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_md5(self): 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_module(md5) 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sha(self): 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_module(sha) 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hmac(self): 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_module(hmac, key='abc') 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(): 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.run_unittest(Pep247Test) 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_main() 75