blob: cde56fd76f17b31bab1e608034ab59c72b256406 [file] [log] [blame]
Christian Heimes217f5c42013-11-24 23:14:16 +01001import functools
Guido van Rossumf1669942001-09-11 15:54:16 +00002import hmac
Guido van Rossuma19f80c2007-11-06 20:51:31 +00003import hashlib
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00004import unittest
Guido van Rossuma19f80c2007-11-06 20:51:31 +00005import warnings
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00007
Christian Heimes217f5c42013-11-24 23:14:16 +01008
9def ignore_warning(func):
10 @functools.wraps(func)
11 def wrapper(*args, **kwargs):
12 with warnings.catch_warnings():
13 warnings.filterwarnings("ignore",
14 category=PendingDeprecationWarning)
15 return func(*args, **kwargs)
16 return wrapper
17
18
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000019class TestVectorsTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000020
Jeremy Hylton893801e2003-05-27 16:16:41 +000021 def test_md5_vectors(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000022 # Test the HMAC module against test vectors from the RFC.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000023
24 def md5test(key, data, digest):
Christian Heimes634919a2013-11-20 17:23:06 +010025 h = hmac.HMAC(key, data, digestmod=hashlib.md5)
26 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimesc4ab1102013-11-20 17:35:06 +010027 self.assertEqual(h.name, "hmac-md5")
28 self.assertEqual(h.digest_size, 16)
29 self.assertEqual(h.block_size, 64)
30
Christian Heimes634919a2013-11-20 17:23:06 +010031 h = hmac.HMAC(key, data, digestmod='md5')
Jeremy Hylton893801e2003-05-27 16:16:41 +000032 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimesc4ab1102013-11-20 17:35:06 +010033 self.assertEqual(h.name, "hmac-md5")
34 self.assertEqual(h.digest_size, 16)
35 self.assertEqual(h.block_size, 64)
36
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000037
Guido van Rossum3f429082007-07-10 13:35:52 +000038 md5test(b"\x0b" * 16,
39 b"Hi There",
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000040 "9294727A3638BB1C13F48EF8158BFC9D")
41
Guido van Rossum3f429082007-07-10 13:35:52 +000042 md5test(b"Jefe",
43 b"what do ya want for nothing?",
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000044 "750c783e6ab0b503eaa86e310a5db738")
45
Guido van Rossum3f429082007-07-10 13:35:52 +000046 md5test(b"\xaa" * 16,
47 b"\xdd" * 50,
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000048 "56be34521d144c88dbb8c733f0e8b3f6")
49
Guido van Rossum39478e82007-08-27 17:23:59 +000050 md5test(bytes(range(1, 26)),
Guido van Rossum3f429082007-07-10 13:35:52 +000051 b"\xcd" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000052 "697eaf0aca3a3aea3a75164746ffaa79")
53
Guido van Rossum39478e82007-08-27 17:23:59 +000054 md5test(b"\x0C" * 16,
55 b"Test With Truncation",
Jeremy Hylton893801e2003-05-27 16:16:41 +000056 "56461ef2342edc00f9bab995690efd4c")
57
Guido van Rossum3f429082007-07-10 13:35:52 +000058 md5test(b"\xaa" * 80,
Guido van Rossum39478e82007-08-27 17:23:59 +000059 b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton893801e2003-05-27 16:16:41 +000060 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
61
Guido van Rossum3f429082007-07-10 13:35:52 +000062 md5test(b"\xaa" * 80,
Guido van Rossum39478e82007-08-27 17:23:59 +000063 (b"Test Using Larger Than Block-Size Key "
64 b"and Larger Than One Block-Size Data"),
Jeremy Hylton893801e2003-05-27 16:16:41 +000065 "6f630fad67cda0ee1fb1f562db3aa53e")
66
67 def test_sha_vectors(self):
68 def shatest(key, data, digest):
Guido van Rossuma19f80c2007-11-06 20:51:31 +000069 h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
Jeremy Hylton893801e2003-05-27 16:16:41 +000070 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimesc4ab1102013-11-20 17:35:06 +010071 self.assertEqual(h.name, "hmac-sha1")
72 self.assertEqual(h.digest_size, 20)
73 self.assertEqual(h.block_size, 64)
74
Christian Heimes634919a2013-11-20 17:23:06 +010075 h = hmac.HMAC(key, data, digestmod='sha1')
76 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimesc4ab1102013-11-20 17:35:06 +010077 self.assertEqual(h.name, "hmac-sha1")
78 self.assertEqual(h.digest_size, 20)
79 self.assertEqual(h.block_size, 64)
Christian Heimes634919a2013-11-20 17:23:06 +010080
Jeremy Hylton893801e2003-05-27 16:16:41 +000081
Guido van Rossum3f429082007-07-10 13:35:52 +000082 shatest(b"\x0b" * 20,
83 b"Hi There",
Jeremy Hylton893801e2003-05-27 16:16:41 +000084 "b617318655057264e28bc0b6fb378c8ef146be00")
85
Guido van Rossum3f429082007-07-10 13:35:52 +000086 shatest(b"Jefe",
87 b"what do ya want for nothing?",
Jeremy Hylton893801e2003-05-27 16:16:41 +000088 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
89
Guido van Rossum3f429082007-07-10 13:35:52 +000090 shatest(b"\xAA" * 20,
91 b"\xDD" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000092 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
93
Guido van Rossum3f429082007-07-10 13:35:52 +000094 shatest(bytes(range(1, 26)),
95 b"\xCD" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000096 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
97
Guido van Rossum39478e82007-08-27 17:23:59 +000098 shatest(b"\x0C" * 20,
99 b"Test With Truncation",
Jeremy Hylton893801e2003-05-27 16:16:41 +0000100 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
101
Guido van Rossum3f429082007-07-10 13:35:52 +0000102 shatest(b"\xAA" * 80,
103 b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton893801e2003-05-27 16:16:41 +0000104 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
105
Guido van Rossum3f429082007-07-10 13:35:52 +0000106 shatest(b"\xAA" * 80,
107 (b"Test Using Larger Than Block-Size Key "
108 b"and Larger Than One Block-Size Data"),
Jeremy Hylton893801e2003-05-27 16:16:41 +0000109 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
110
Christian Heimesc4ab1102013-11-20 17:35:06 +0100111 def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000112 def hmactest(key, data, hexdigests):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100113 hmac_name = "hmac-" + hash_name
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000114 h = hmac.HMAC(key, data, digestmod=hashfunc)
115 self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
Christian Heimesc4ab1102013-11-20 17:35:06 +0100116 self.assertEqual(h.name, hmac_name)
117 self.assertEqual(h.digest_size, digest_size)
118 self.assertEqual(h.block_size, block_size)
119
120 h = hmac.HMAC(key, data, digestmod=hash_name)
Christian Heimes634919a2013-11-20 17:23:06 +0100121 self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
Christian Heimesc4ab1102013-11-20 17:35:06 +0100122 self.assertEqual(h.name, hmac_name)
123 self.assertEqual(h.digest_size, digest_size)
124 self.assertEqual(h.block_size, block_size)
Christian Heimes634919a2013-11-20 17:23:06 +0100125
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000126
127 # 4.2. Test Case 1
128 hmactest(key = b'\x0b'*20,
129 data = b'Hi There',
130 hexdigests = {
131 hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
132 '47b4b1169912ba4f53684b22',
133 hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
134 '881dc200c9833da726e9376c2e32cff7',
135 hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
136 '15f9dadbe4101ec682aa034c7cebc59c'
137 'faea9ea9076ede7f4af152e8b2fa9cb6',
138 hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
139 '2379f4e2ce4ec2787ad0b30545e17cde'
140 'daa833b7d6b8a702038b274eaea3f4e4'
141 'be9d914eeb61f1702e696c203a126854',
142 })
143
144 # 4.3. Test Case 2
145 hmactest(key = b'Jefe',
146 data = b'what do ya want for nothing?',
147 hexdigests = {
148 hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
149 '8bbea2a39e6148008fd05e44',
150 hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
151 '5a003f089d2739839dec58b964ec3843',
152 hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
153 '9c7ef464f5a01b47e42ec3736322445e'
154 '8e2240ca5e69e2c78b3239ecfab21649',
155 hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
156 '87bd64222e831fd610270cd7ea250554'
157 '9758bf75c05a994a6d034f65f8f0e6fd'
158 'caeab1a34d4a6b4b636e070a38bce737',
159 })
160
161 # 4.4. Test Case 3
162 hmactest(key = b'\xaa'*20,
163 data = b'\xdd'*50,
164 hexdigests = {
165 hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
166 '9365b0c1f65d69d1ec8333ea',
167 hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
168 '2959098b3ef8c122d9635514ced565fe',
169 hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
170 '0aa635d947ac9febe83ef4e55966144b'
171 '2a5ab39dc13814b94e3ab6e101a34f27',
172 hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
173 'b1b5dbdd8ee81a3655f83e33b2279d39'
174 'bf3e848279a722c806b485a47e67c807'
175 'b946a337bee8942674278859e13292fb',
176 })
177
178 # 4.5. Test Case 4
179 hmactest(key = bytes(x for x in range(0x01, 0x19+1)),
180 data = b'\xcd'*50,
181 hexdigests = {
182 hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
183 'ec6a90d86efc012de7afec5a',
184 hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
185 '85f0faa3e578f8077a2e3ff46729665b',
186 hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
187 '7a9981480850009cc5577c6e1f573b4e'
188 '6801dd23c4a7d679ccf8a386c674cffb',
189 hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
190 'e576d97ff94b872de76f8050361ee3db'
191 'a91ca5c11aa25eb4d679275cc5788063'
192 'a5f19741120c4f2de2adebeb10a298dd',
193 })
194
195 # 4.7. Test Case 6
196 hmactest(key = b'\xaa'*131,
197 data = b'Test Using Larger Than Block-Siz'
198 b'e Key - Hash Key First',
199 hexdigests = {
200 hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
201 'd499f112f2d2b7273fa6870e',
202 hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
203 '8e0bc6213728c5140546040f0ee37f54',
204 hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
205 '4f9ef1012a2b588f3cd11f05033ac4c6'
206 '0c2ef6ab4030fe8296248df163f44952',
207 hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
208 '9b46d1f41b4aeec1121b013783f8f352'
209 '6b56d037e05f2598bd0fd2215d6a1e52'
210 '95e64f73f63f0aec8b915a985d786598',
211 })
212
213 # 4.8. Test Case 7
214 hmactest(key = b'\xaa'*131,
215 data = b'This is a test using a larger th'
216 b'an block-size key and a larger t'
217 b'han block-size data. The key nee'
218 b'ds to be hashed before being use'
219 b'd by the HMAC algorithm.',
220 hexdigests = {
221 hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
222 '946770db9c2b95c9f6f565d1',
223 hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
224 'bfdc63644f0713938a7f51535c3a35e2',
225 hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
226 '602420feb0b8fb9adccebb82461e99c5'
227 'a678cc31e799176d3860e6110c46523e',
228 hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
229 'debd71f8867289865df5a32d20cdc944'
230 'b6022cac3c4982b10d5eeb55c3e4de15'
231 '134676fb6de0446065c97440fa8c6a58',
232 })
233
234 def test_sha224_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100235 self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000236
237 def test_sha256_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100238 self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000239
240 def test_sha384_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100241 self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000242
243 def test_sha512_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100244 self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000245
246 def test_legacy_block_size_warnings(self):
247 class MockCrazyHash(object):
248 """Ain't no block_size attribute here."""
249 def __init__(self, *args):
250 self._x = hashlib.sha1(*args)
251 self.digest_size = self._x.digest_size
252 def update(self, v):
253 self._x.update(v)
254 def digest(self):
255 return self._x.digest()
256
Brett Cannon1cd02472008-09-09 01:52:27 +0000257 with warnings.catch_warnings():
Christian Heimese25f35e2008-03-20 10:49:03 +0000258 warnings.simplefilter('error', RuntimeWarning)
Florent Xicluna41fe6152010-04-02 18:52:12 +0000259 with self.assertRaises(RuntimeWarning):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000260 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000261 self.fail('Expected warning about missing block_size')
262
263 MockCrazyHash.block_size = 1
Florent Xicluna41fe6152010-04-02 18:52:12 +0000264 with self.assertRaises(RuntimeWarning):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000265 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000266 self.fail('Expected warning about small block_size')
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000267
Christian Heimes634919a2013-11-20 17:23:06 +0100268 def test_with_digestmod_warning(self):
269 with self.assertWarns(PendingDeprecationWarning):
270 key = b"\x0b" * 16
271 data = b"Hi There"
272 digest = "9294727A3638BB1C13F48EF8158BFC9D"
273 h = hmac.HMAC(key, data)
274 self.assertEqual(h.hexdigest().upper(), digest)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000275
Jeremy Hylton893801e2003-05-27 16:16:41 +0000276
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000277class ConstructorTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000278
Christian Heimes217f5c42013-11-24 23:14:16 +0100279 @ignore_warning
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000280 def test_normal(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000281 # Standard constructor call.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000282 failed = 0
283 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000284 h = hmac.HMAC(b"key")
Christian Heimes217f5c42013-11-24 23:14:16 +0100285 except Exception:
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000286 self.fail("Standard constructor call raised exception.")
287
Christian Heimes217f5c42013-11-24 23:14:16 +0100288 @ignore_warning
Antoine Pitrou24ef3e92012-06-30 17:27:56 +0200289 def test_with_str_key(self):
290 # Pass a key of type str, which is an error, because it expects a key
291 # of type bytes
292 with self.assertRaises(TypeError):
293 h = hmac.HMAC("key")
294
Christian Heimes217f5c42013-11-24 23:14:16 +0100295 @ignore_warning
Antoine Pitrou24ef3e92012-06-30 17:27:56 +0200296 def test_dot_new_with_str_key(self):
297 # Pass a key of type str, which is an error, because it expects a key
298 # of type bytes
299 with self.assertRaises(TypeError):
300 h = hmac.new("key")
301
Christian Heimes217f5c42013-11-24 23:14:16 +0100302 @ignore_warning
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000303 def test_withtext(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000304 # Constructor call with text.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000305 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000306 h = hmac.HMAC(b"key", b"hash this!")
Christian Heimes217f5c42013-11-24 23:14:16 +0100307 except Exception:
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000308 self.fail("Constructor call with text argument raised exception.")
Christian Heimes217f5c42013-11-24 23:14:16 +0100309 self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000310
Christian Heimes04926ae2013-07-01 13:08:42 +0200311 def test_with_bytearray(self):
312 try:
Christian Heimes217f5c42013-11-24 23:14:16 +0100313 h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!"),
314 digestmod="md5")
315 except Exception:
Christian Heimes04926ae2013-07-01 13:08:42 +0200316 self.fail("Constructor call with bytearray arguments raised exception.")
Christian Heimes217f5c42013-11-24 23:14:16 +0100317 self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
Christian Heimes04926ae2013-07-01 13:08:42 +0200318
319 def test_with_memoryview_msg(self):
320 try:
Christian Heimes217f5c42013-11-24 23:14:16 +0100321 h = hmac.HMAC(b"key", memoryview(b"hash this!"), digestmod="md5")
322 except Exception:
Christian Heimes04926ae2013-07-01 13:08:42 +0200323 self.fail("Constructor call with memoryview msg raised exception.")
Christian Heimes217f5c42013-11-24 23:14:16 +0100324 self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
Christian Heimes04926ae2013-07-01 13:08:42 +0200325
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000326 def test_withmodule(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000327 # Constructor call with text and digest module.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000328 try:
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000329 h = hmac.HMAC(b"key", b"", hashlib.sha1)
Christian Heimes217f5c42013-11-24 23:14:16 +0100330 except Exception:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000331 self.fail("Constructor call with hashlib.sha1 raised exception.")
Tim Peters88768482001-11-13 21:51:26 +0000332
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000333class SanityTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000334
Christian Heimes217f5c42013-11-24 23:14:16 +0100335 @ignore_warning
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000336 def test_default_is_md5(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000337 # Testing if HMAC defaults to MD5 algorithm.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000338 # NOTE: this whitebox test depends on the hmac class internals
Guido van Rossum39478e82007-08-27 17:23:59 +0000339 h = hmac.HMAC(b"key")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000340 self.assertEqual(h.digest_cons, hashlib.md5)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000341
342 def test_exercise_all_methods(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000343 # Exercising all methods once.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000344 # This must not raise any exceptions
345 try:
Christian Heimes217f5c42013-11-24 23:14:16 +0100346 h = hmac.HMAC(b"my secret key", digestmod="md5")
Guido van Rossum39478e82007-08-27 17:23:59 +0000347 h.update(b"compute the hash of this text!")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000348 dig = h.digest()
349 dig = h.hexdigest()
350 h2 = h.copy()
Christian Heimes217f5c42013-11-24 23:14:16 +0100351 except Exception:
Neal Norwitz28bb5722002-04-01 19:00:50 +0000352 self.fail("Exception raised during normal usage of HMAC class.")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000353
354class CopyTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000355
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000356 def test_attributes(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000357 # Testing if attributes are of same type.
Christian Heimes217f5c42013-11-24 23:14:16 +0100358 h1 = hmac.HMAC(b"key", digestmod="md5")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000359 h2 = h1.copy()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000360 self.assertTrue(h1.digest_cons == h2.digest_cons,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000361 "digest constructors don't match.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000362 self.assertEqual(type(h1.inner), type(h2.inner),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000363 "Types of inner don't match.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000364 self.assertEqual(type(h1.outer), type(h2.outer),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000365 "Types of outer don't match.")
366
367 def test_realcopy(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000368 # Testing if the copy method created a real copy.
Christian Heimes217f5c42013-11-24 23:14:16 +0100369 h1 = hmac.HMAC(b"key", digestmod="md5")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000370 h2 = h1.copy()
Mark Dickinsona56c4672009-01-27 18:17:45 +0000371 # Using id() in case somebody has overridden __eq__/__ne__.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000372 self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
373 self.assertTrue(id(h1.inner) != id(h2.inner),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000374 "No real copy of the attribute 'inner'.")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000375 self.assertTrue(id(h1.outer) != id(h2.outer),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000376 "No real copy of the attribute 'outer'.")
377
378 def test_equality(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000379 # Testing if the copy has the same digests.
Christian Heimes217f5c42013-11-24 23:14:16 +0100380 h1 = hmac.HMAC(b"key", digestmod="md5")
Guido van Rossum39478e82007-08-27 17:23:59 +0000381 h1.update(b"some random text")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000382 h2 = h1.copy()
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000383 self.assertEqual(h1.digest(), h2.digest(),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000384 "Digest of copy doesn't match original digest.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000385 self.assertEqual(h1.hexdigest(), h2.hexdigest(),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000386 "Hexdigest of copy doesn't match original hexdigest.")
387
Nick Coghlan807770e2012-06-15 21:14:08 +1000388class CompareDigestTestCase(unittest.TestCase):
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200389
Christian Heimes6cea6552012-06-24 13:48:32 +0200390 def test_compare_digest(self):
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200391 # Testing input type exception handling
392 a, b = 100, 200
Nick Coghlan807770e2012-06-15 21:14:08 +1000393 self.assertRaises(TypeError, hmac.compare_digest, a, b)
394 a, b = 100, b"foobar"
395 self.assertRaises(TypeError, hmac.compare_digest, a, b)
396 a, b = b"foobar", 200
397 self.assertRaises(TypeError, hmac.compare_digest, a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200398 a, b = "foobar", b"foobar"
Nick Coghlan807770e2012-06-15 21:14:08 +1000399 self.assertRaises(TypeError, hmac.compare_digest, a, b)
400 a, b = b"foobar", "foobar"
401 self.assertRaises(TypeError, hmac.compare_digest, a, b)
Nick Coghlan807770e2012-06-15 21:14:08 +1000402
403 # Testing bytes of different lengths
404 a, b = b"foobar", b"foo"
405 self.assertFalse(hmac.compare_digest(a, b))
406 a, b = b"\xde\xad\xbe\xef", b"\xde\xad"
407 self.assertFalse(hmac.compare_digest(a, b))
408
409 # Testing bytes of same lengths, different values
410 a, b = b"foobar", b"foobaz"
411 self.assertFalse(hmac.compare_digest(a, b))
412 a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea"
413 self.assertFalse(hmac.compare_digest(a, b))
414
415 # Testing bytes of same lengths, same values
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200416 a, b = b"foobar", b"foobar"
Nick Coghlan807770e2012-06-15 21:14:08 +1000417 self.assertTrue(hmac.compare_digest(a, b))
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200418 a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef"
Nick Coghlan807770e2012-06-15 21:14:08 +1000419 self.assertTrue(hmac.compare_digest(a, b))
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200420
Christian Heimes6cea6552012-06-24 13:48:32 +0200421 # Testing bytearrays of same lengths, same values
422 a, b = bytearray(b"foobar"), bytearray(b"foobar")
423 self.assertTrue(hmac.compare_digest(a, b))
424
425 # Testing bytearrays of diffeent lengths
426 a, b = bytearray(b"foobar"), bytearray(b"foo")
427 self.assertFalse(hmac.compare_digest(a, b))
428
429 # Testing bytearrays of same lengths, different values
430 a, b = bytearray(b"foobar"), bytearray(b"foobaz")
431 self.assertFalse(hmac.compare_digest(a, b))
432
433 # Testing byte and bytearray of same lengths, same values
434 a, b = bytearray(b"foobar"), b"foobar"
435 self.assertTrue(hmac.compare_digest(a, b))
436 self.assertTrue(hmac.compare_digest(b, a))
437
438 # Testing byte bytearray of diffeent lengths
439 a, b = bytearray(b"foobar"), b"foo"
440 self.assertFalse(hmac.compare_digest(a, b))
441 self.assertFalse(hmac.compare_digest(b, a))
442
443 # Testing byte and bytearray of same lengths, different values
444 a, b = bytearray(b"foobar"), b"foobaz"
445 self.assertFalse(hmac.compare_digest(a, b))
446 self.assertFalse(hmac.compare_digest(b, a))
447
448 # Testing str of same lengths
449 a, b = "foobar", "foobar"
450 self.assertTrue(hmac.compare_digest(a, b))
451
452 # Testing str of diffeent lengths
453 a, b = "foo", "foobar"
454 self.assertFalse(hmac.compare_digest(a, b))
455
456 # Testing bytes of same lengths, different values
457 a, b = "foobar", "foobaz"
458 self.assertFalse(hmac.compare_digest(a, b))
459
460 # Testing error cases
461 a, b = "foobar", b"foobar"
462 self.assertRaises(TypeError, hmac.compare_digest, a, b)
463 a, b = b"foobar", "foobar"
464 self.assertRaises(TypeError, hmac.compare_digest, a, b)
465 a, b = b"foobar", 1
466 self.assertRaises(TypeError, hmac.compare_digest, a, b)
467 a, b = 100, 200
468 self.assertRaises(TypeError, hmac.compare_digest, a, b)
469 a, b = "fooä", "fooä"
470 self.assertRaises(TypeError, hmac.compare_digest, a, b)
471
472 # subclasses are supported by ignore __eq__
473 class mystr(str):
474 def __eq__(self, other):
475 return False
476
477 a, b = mystr("foobar"), mystr("foobar")
478 self.assertTrue(hmac.compare_digest(a, b))
479 a, b = mystr("foobar"), "foobar"
480 self.assertTrue(hmac.compare_digest(a, b))
481 a, b = mystr("foobar"), mystr("foobaz")
482 self.assertFalse(hmac.compare_digest(a, b))
483
484 class mybytes(bytes):
485 def __eq__(self, other):
486 return False
487
488 a, b = mybytes(b"foobar"), mybytes(b"foobar")
489 self.assertTrue(hmac.compare_digest(a, b))
490 a, b = mybytes(b"foobar"), b"foobar"
491 self.assertTrue(hmac.compare_digest(a, b))
492 a, b = mybytes(b"foobar"), mybytes(b"foobaz")
493 self.assertFalse(hmac.compare_digest(a, b))
494
495
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000496def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000497 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000498 TestVectorsTestCase,
499 ConstructorTestCase,
500 SanityTestCase,
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200501 CopyTestCase,
Nick Coghlan807770e2012-06-15 21:14:08 +1000502 CompareDigestTestCase
Walter Dörwald21d3a322003-05-01 17:45:56 +0000503 )
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000504
505if __name__ == "__main__":
506 test_main()