Home | History | Annotate | Download | only in test
      1 # Testing md5 module
      2 import warnings
      3 warnings.filterwarnings("ignore", "the md5 module is deprecated.*",
      4                         DeprecationWarning)
      5 
      6 import unittest
      7 from md5 import md5
      8 from test import test_support
      9 
     10 def hexstr(s):
     11     import string
     12     h = string.hexdigits
     13     r = ''
     14     for c in s:
     15         i = ord(c)
     16         r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
     17     return r
     18 
     19 class MD5_Test(unittest.TestCase):
     20 
     21     def md5test(self, s, expected):
     22         self.assertEqual(hexstr(md5(s).digest()), expected)
     23         self.assertEqual(md5(s).hexdigest(), expected)
     24 
     25     def test_basics(self):
     26         eq = self.md5test
     27         eq('', 'd41d8cd98f00b204e9800998ecf8427e')
     28         eq('a', '0cc175b9c0f1b6a831c399e269772661')
     29         eq('abc', '900150983cd24fb0d6963f7d28e17f72')
     30         eq('message digest', 'f96b697d7cb7938d525a2f31aaf161d0')
     31         eq('abcdefghijklmnopqrstuvwxyz', 'c3fcd3d76192e4007dfb496cca67e13b')
     32         eq('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
     33            'd174ab98d277d9f5a5611c2c9f419d9f')
     34         eq('12345678901234567890123456789012345678901234567890123456789012345678901234567890',
     35            '57edf4a22be3c955ac49da2e2107b67a')
     36 
     37     def test_hexdigest(self):
     38         # hexdigest is new with Python 2.0
     39         m = md5('testing the hexdigest method')
     40         h = m.hexdigest()
     41         self.assertEqual(hexstr(m.digest()), h)
     42 
     43     def test_large_update(self):
     44         aas = 'a' * 64
     45         bees = 'b' * 64
     46         cees = 'c' * 64
     47 
     48         m1 = md5()
     49         m1.update(aas)
     50         m1.update(bees)
     51         m1.update(cees)
     52 
     53         m2 = md5()
     54         m2.update(aas + bees + cees)
     55         self.assertEqual(m1.digest(), m2.digest())
     56 
     57 def test_main():
     58     test_support.run_unittest(MD5_Test)
     59 
     60 if __name__ == '__main__':
     61     test_main()
     62