0001 # Testing md5 module 0002 0003 import unittest 0004 from md5 import md5 0005 from test import test_support 0006 0007 def hexstr(s): 0008 import string 0009 h = string.hexdigits 0010 r = '' 0011 for c in s: 0012 i = ord(c) 0013 r = r + h[(i >> 4) & 0xF] + h[i & 0xF] 0014 return r 0015 0016 class MD5_Test(unittest.TestCase): 0017 0018 def md5test(self, s, expected): 0019 self.assertEqual(hexstr(md5(s).digest()), expected) 0020 self.assertEqual(md5(s).hexdigest(), expected) 0021 0022 def test_basics(self): 0023 eq = self.md5test 0024 eq('', 'd41d8cd98f00b204e9800998ecf8427e') 0025 eq('a', '0cc175b9c0f1b6a831c399e269772661') 0026 eq('abc', '900150983cd24fb0d6963f7d28e17f72') 0027 eq('message digest', 'f96b697d7cb7938d525a2f31aaf161d0') 0028 eq('abcdefghijklmnopqrstuvwxyz', 'c3fcd3d76192e4007dfb496cca67e13b') 0029 eq('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 0030 'd174ab98d277d9f5a5611c2c9f419d9f') 0031 eq('12345678901234567890123456789012345678901234567890123456789012345678901234567890', 0032 '57edf4a22be3c955ac49da2e2107b67a') 0033 0034 def test_hexdigest(self): 0035 # hexdigest is new with Python 2.0 0036 m = md5('testing the hexdigest method') 0037 h = m.hexdigest() 0038 self.assertEqual(hexstr(m.digest()), h) 0039 0040 def test_large_update(self): 0041 aas = 'a' * 64 0042 bees = 'b' * 64 0043 cees = 'c' * 64 0044 0045 m1 = md5() 0046 m1.update(aas) 0047 m1.update(bees) 0048 m1.update(cees) 0049 0050 m2 = md5() 0051 m2.update(aas + bees + cees) 0052 self.assertEqual(m1.digest(), m2.digest()) 0053 0054 def test_main(): 0055 test_support.run_unittest(MD5_Test) 0056 0057 if __name__ == '__main__': 0058 test_main() 0059
Generated by PyXR 0.9.4