symbian-qemu-0.9.1-12/python-2.6.1/Lib/test/test_pep247.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 """
       
     2 Test suite to check compilance with PEP 247, the standard API
       
     3 for hashing algorithms
       
     4 """
       
     5 
       
     6 import warnings
       
     7 warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
       
     8                         DeprecationWarning)
       
     9 warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
       
    10                         DeprecationWarning)
       
    11 
       
    12 import hmac
       
    13 import md5
       
    14 import sha
       
    15 
       
    16 import unittest
       
    17 from test import test_support
       
    18 
       
    19 class 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 
       
    70 def test_main():
       
    71     test_support.run_unittest(Pep247Test)
       
    72 
       
    73 if __name__ == '__main__':
       
    74     test_main()