blob: f36db4ea6367c51894659d3da076b4f4f4fbb300 [file] [log] [blame]
Guido van Rossumf1669942001-09-11 15:54:16 +00001import hmac
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00002import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00004
5class TestVectorsTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +00006
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00007 def test_vectors(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +00008 # Test the HMAC module against test vectors from the RFC.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00009
10 def md5test(key, data, digest):
11 h = hmac.HMAC(key, data)
12 self.failUnless(h.hexdigest().upper() == digest.upper())
13
14 md5test(chr(0x0b) * 16,
15 "Hi There",
16 "9294727A3638BB1C13F48EF8158BFC9D")
17
18 md5test("Jefe",
19 "what do ya want for nothing?",
20 "750c783e6ab0b503eaa86e310a5db738")
21
22 md5test(chr(0xAA)*16,
23 chr(0xDD)*50,
24 "56be34521d144c88dbb8c733f0e8b3f6")
25
26class ConstructorTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000027
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000028 def test_normal(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000029 # Standard constructor call.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000030 failed = 0
31 try:
32 h = hmac.HMAC("key")
33 except:
34 self.fail("Standard constructor call raised exception.")
35
36 def test_withtext(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000037 # Constructor call with text.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000038 try:
39 h = hmac.HMAC("key", "hash this!")
40 except:
41 self.fail("Constructor call with text argument raised exception.")
42
43 def test_withmodule(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000044 # Constructor call with text and digest module.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000045 import sha
46 try:
47 h = hmac.HMAC("key", "", sha)
48 except:
49 self.fail("Constructor call with sha module raised exception.")
Tim Peters88768482001-11-13 21:51:26 +000050
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000051class SanityTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000052
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000053 def test_default_is_md5(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000054 # Testing if HMAC defaults to MD5 algorithm.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000055 import md5
56 h = hmac.HMAC("key")
57 self.failUnless(h.digestmod == md5)
58
59 def test_exercise_all_methods(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000060 # Exercising all methods once.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000061 # This must not raise any exceptions
62 try:
63 h = hmac.HMAC("my secret key")
64 h.update("compute the hash of this text!")
65 dig = h.digest()
66 dig = h.hexdigest()
67 h2 = h.copy()
68 except:
Neal Norwitz28bb5722002-04-01 19:00:50 +000069 self.fail("Exception raised during normal usage of HMAC class.")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000070
71class CopyTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000072
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000073 def test_attributes(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000074 # Testing if attributes are of same type.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000075 h1 = hmac.HMAC("key")
76 h2 = h1.copy()
77 self.failUnless(h1.digestmod == h2.digestmod,
78 "Modules don't match.")
79 self.failUnless(type(h1.inner) == type(h2.inner),
80 "Types of inner don't match.")
81 self.failUnless(type(h1.outer) == type(h2.outer),
82 "Types of outer don't match.")
83
84 def test_realcopy(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000085 # Testing if the copy method created a real copy.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000086 h1 = hmac.HMAC("key")
87 h2 = h1.copy()
88 # Using id() in case somebody has overridden __cmp__.
89 self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
90 self.failUnless(id(h1.inner) != id(h2.inner),
91 "No real copy of the attribute 'inner'.")
92 self.failUnless(id(h1.outer) != id(h2.outer),
93 "No real copy of the attribute 'outer'.")
94
95 def test_equality(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000096 # Testing if the copy has the same digests.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000097 h1 = hmac.HMAC("key")
98 h1.update("some random text")
99 h2 = h1.copy()
100 self.failUnless(h1.digest() == h2.digest(),
101 "Digest of copy doesn't match original digest.")
102 self.failUnless(h1.hexdigest() == h2.hexdigest(),
103 "Hexdigest of copy doesn't match original hexdigest.")
104
105def test_main():
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000106 suite = unittest.TestSuite()
107 suite.addTest(unittest.makeSuite(TestVectorsTestCase))
108 suite.addTest(unittest.makeSuite(ConstructorTestCase))
109 suite.addTest(unittest.makeSuite(SanityTestCase))
110 suite.addTest(unittest.makeSuite(CopyTestCase))
111 test_support.run_suite(suite)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000112
113if __name__ == "__main__":
114 test_main()