blob: d28490d7dab5d6f78913205df3b546c683401c79 [file] [log] [blame]
Guido van Rossumf1669942001-09-11 15:54:16 +00001import hmac
Guido van Rossume7ba4952007-06-06 23:52:48 +00002from hashlib import sha1
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00003import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00004from test import test_support
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00005
6class TestVectorsTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +00007
Jeremy Hylton893801e2003-05-27 16:16:41 +00008 def test_md5_vectors(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +00009 # Test the HMAC module against test vectors from the RFC.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000010
11 def md5test(key, data, digest):
12 h = hmac.HMAC(key, data)
Jeremy Hylton893801e2003-05-27 16:16:41 +000013 self.assertEqual(h.hexdigest().upper(), digest.upper())
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000014
15 md5test(chr(0x0b) * 16,
16 "Hi There",
17 "9294727A3638BB1C13F48EF8158BFC9D")
18
19 md5test("Jefe",
20 "what do ya want for nothing?",
21 "750c783e6ab0b503eaa86e310a5db738")
22
23 md5test(chr(0xAA)*16,
24 chr(0xDD)*50,
25 "56be34521d144c88dbb8c733f0e8b3f6")
26
Jeremy Hylton893801e2003-05-27 16:16:41 +000027 md5test("".join([chr(i) for i in range(1, 26)]),
28 chr(0xCD) * 50,
29 "697eaf0aca3a3aea3a75164746ffaa79")
30
31 md5test(chr(0x0C) * 16,
32 "Test With Truncation",
33 "56461ef2342edc00f9bab995690efd4c")
34
35 md5test(chr(0xAA) * 80,
36 "Test Using Larger Than Block-Size Key - Hash Key First",
37 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
38
39 md5test(chr(0xAA) * 80,
40 ("Test Using Larger Than Block-Size Key "
41 "and Larger Than One Block-Size Data"),
42 "6f630fad67cda0ee1fb1f562db3aa53e")
43
44 def test_sha_vectors(self):
45 def shatest(key, data, digest):
Guido van Rossume7ba4952007-06-06 23:52:48 +000046 h = hmac.HMAC(key, data, digestmod=sha1)
Jeremy Hylton893801e2003-05-27 16:16:41 +000047 self.assertEqual(h.hexdigest().upper(), digest.upper())
48
49 shatest(chr(0x0b) * 20,
50 "Hi There",
51 "b617318655057264e28bc0b6fb378c8ef146be00")
52
53 shatest("Jefe",
54 "what do ya want for nothing?",
55 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
56
57 shatest(chr(0xAA)*20,
58 chr(0xDD)*50,
59 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
60
61 shatest("".join([chr(i) for i in range(1, 26)]),
62 chr(0xCD) * 50,
63 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
64
65 shatest(chr(0x0C) * 20,
66 "Test With Truncation",
67 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
68
69 shatest(chr(0xAA) * 80,
70 "Test Using Larger Than Block-Size Key - Hash Key First",
71 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
72
73 shatest(chr(0xAA) * 80,
74 ("Test Using Larger Than Block-Size Key "
75 "and Larger Than One Block-Size Data"),
76 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
77
78
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000079class ConstructorTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000080
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000081 def test_normal(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000082 # Standard constructor call.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000083 failed = 0
84 try:
85 h = hmac.HMAC("key")
86 except:
87 self.fail("Standard constructor call raised exception.")
88
89 def test_withtext(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000090 # Constructor call with text.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000091 try:
92 h = hmac.HMAC("key", "hash this!")
93 except:
94 self.fail("Constructor call with text argument raised exception.")
95
96 def test_withmodule(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000097 # Constructor call with text and digest module.
Guido van Rossume7ba4952007-06-06 23:52:48 +000098 from hashlib import sha1
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000099 try:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000100 h = hmac.HMAC("key", "", sha1)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000101 except:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000102 self.fail("Constructor call with hashlib.sha1 raised exception.")
Tim Peters88768482001-11-13 21:51:26 +0000103
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000104class SanityTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000105
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000106 def test_default_is_md5(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000107 # Testing if HMAC defaults to MD5 algorithm.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000108 # NOTE: this whitebox test depends on the hmac class internals
109 import hashlib
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000110 h = hmac.HMAC("key")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000111 self.failUnless(h.digest_cons == hashlib.md5)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000112
113 def test_exercise_all_methods(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000114 # Exercising all methods once.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000115 # This must not raise any exceptions
116 try:
117 h = hmac.HMAC("my secret key")
118 h.update("compute the hash of this text!")
119 dig = h.digest()
120 dig = h.hexdigest()
121 h2 = h.copy()
122 except:
Neal Norwitz28bb5722002-04-01 19:00:50 +0000123 self.fail("Exception raised during normal usage of HMAC class.")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000124
125class CopyTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000126
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000127 def test_attributes(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000128 # Testing if attributes are of same type.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000129 h1 = hmac.HMAC("key")
130 h2 = h1.copy()
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000131 self.failUnless(h1.digest_cons == h2.digest_cons,
132 "digest constructors don't match.")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000133 self.failUnless(type(h1.inner) == type(h2.inner),
134 "Types of inner don't match.")
135 self.failUnless(type(h1.outer) == type(h2.outer),
136 "Types of outer don't match.")
137
138 def test_realcopy(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000139 # Testing if the copy method created a real copy.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000140 h1 = hmac.HMAC("key")
141 h2 = h1.copy()
142 # Using id() in case somebody has overridden __cmp__.
143 self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
144 self.failUnless(id(h1.inner) != id(h2.inner),
145 "No real copy of the attribute 'inner'.")
146 self.failUnless(id(h1.outer) != id(h2.outer),
147 "No real copy of the attribute 'outer'.")
148
149 def test_equality(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000150 # Testing if the copy has the same digests.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000151 h1 = hmac.HMAC("key")
152 h1.update("some random text")
153 h2 = h1.copy()
154 self.failUnless(h1.digest() == h2.digest(),
155 "Digest of copy doesn't match original digest.")
156 self.failUnless(h1.hexdigest() == h2.hexdigest(),
157 "Hexdigest of copy doesn't match original hexdigest.")
158
159def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000160 test_support.run_unittest(
161 TestVectorsTestCase,
162 ConstructorTestCase,
163 SanityTestCase,
164 CopyTestCase
165 )
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000166
167if __name__ == "__main__":
168 test_main()