4
4
import unittest
5
5
import warnings
6
6
7
+ from test .support import requires_hashdigest
8
+
7
9
8
10
def ignore_warning (func ):
9
11
@functools .wraps (func )
@@ -17,6 +19,7 @@ def wrapper(*args, **kwargs):
17
19
18
20
class TestVectorsTestCase (unittest .TestCase ):
19
21
22
+ @requires_hashdigest ('md5' )
20
23
def test_md5_vectors (self ):
21
24
# Test the HMAC module against test vectors from the RFC.
22
25
@@ -63,6 +66,7 @@ def md5test(key, data, digest):
63
66
b"and Larger Than One Block-Size Data" ),
64
67
"6f630fad67cda0ee1fb1f562db3aa53e" )
65
68
69
+ @requires_hashdigest ('sha1' )
66
70
def test_sha_vectors (self ):
67
71
def shatest (key , data , digest ):
68
72
h = hmac .HMAC (key , data , digestmod = hashlib .sha1 )
@@ -230,23 +234,28 @@ def hmactest(key, data, hexdigests):
230
234
'134676fb6de0446065c97440fa8c6a58' ,
231
235
})
232
236
237
+ @requires_hashdigest ('sha224' )
233
238
def test_sha224_rfc4231 (self ):
234
239
self ._rfc4231_test_cases (hashlib .sha224 , 'sha224' , 28 , 64 )
235
240
241
+ @requires_hashdigest ('sha256' )
236
242
def test_sha256_rfc4231 (self ):
237
243
self ._rfc4231_test_cases (hashlib .sha256 , 'sha256' , 32 , 64 )
238
244
245
+ @requires_hashdigest ('sha384' )
239
246
def test_sha384_rfc4231 (self ):
240
247
self ._rfc4231_test_cases (hashlib .sha384 , 'sha384' , 48 , 128 )
241
248
249
+ @requires_hashdigest ('sha512' )
242
250
def test_sha512_rfc4231 (self ):
243
251
self ._rfc4231_test_cases (hashlib .sha512 , 'sha512' , 64 , 128 )
244
252
253
+ @requires_hashdigest ('sha256' )
245
254
def test_legacy_block_size_warnings (self ):
246
255
class MockCrazyHash (object ):
247
256
"""Ain't no block_size attribute here."""
248
257
def __init__ (self , * args ):
249
- self ._x = hashlib .sha1 (* args )
258
+ self ._x = hashlib .sha256 (* args )
250
259
self .digest_size = self ._x .digest_size
251
260
def update (self , v ):
252
261
self ._x .update (v )
@@ -273,88 +282,94 @@ def test_with_digestmod_warning(self):
273
282
self .assertEqual (h .hexdigest ().upper (), digest )
274
283
275
284
285
+
276
286
class ConstructorTestCase (unittest .TestCase ):
277
287
288
+ expected = (
289
+ "6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdeb"
290
+ )
278
291
@ignore_warning
292
+ @requires_hashdigest ('sha256' )
279
293
def test_normal (self ):
280
294
# Standard constructor call.
281
- failed = 0
282
295
try :
283
- h = hmac .HMAC (b"key" )
296
+ hmac .HMAC (b"key" , digestmod = 'sha256' )
284
297
except Exception :
285
298
self .fail ("Standard constructor call raised exception." )
286
299
287
300
@ignore_warning
301
+ @requires_hashdigest ('sha256' )
288
302
def test_with_str_key (self ):
289
303
# Pass a key of type str, which is an error, because it expects a key
290
304
# of type bytes
291
305
with self .assertRaises (TypeError ):
292
- h = hmac .HMAC ("key" )
306
+ h = hmac .HMAC ("key" , digestmod = 'sha256' )
293
307
294
- @ignore_warning
308
+ @requires_hashdigest ( 'sha256' )
295
309
def test_dot_new_with_str_key (self ):
296
310
# Pass a key of type str, which is an error, because it expects a key
297
311
# of type bytes
298
312
with self .assertRaises (TypeError ):
299
- h = hmac .new ("key" )
313
+ h = hmac .HMAC ("key" , digestmod = 'sha256' )
300
314
301
315
@ignore_warning
316
+ @requires_hashdigest ('sha256' )
302
317
def test_withtext (self ):
303
318
# Constructor call with text.
304
319
try :
305
- h = hmac .HMAC (b"key" , b"hash this!" )
320
+ h = hmac .HMAC (b"key" , b"hash this!" , digestmod = 'sha256' )
306
321
except Exception :
307
322
self .fail ("Constructor call with text argument raised exception." )
308
- self .assertEqual (h .hexdigest (), '34325b639da4cfd95735b381e28cb864' )
323
+ self .assertEqual (h .hexdigest (), self . expected )
309
324
325
+ @requires_hashdigest ('sha256' )
310
326
def test_with_bytearray (self ):
311
327
try :
312
328
h = hmac .HMAC (bytearray (b"key" ), bytearray (b"hash this!" ),
313
- digestmod = "md5 " )
329
+ digestmod = "sha256 " )
314
330
except Exception :
315
331
self .fail ("Constructor call with bytearray arguments raised exception." )
316
- self .assertEqual (h .hexdigest (), '34325b639da4cfd95735b381e28cb864' )
332
+ self .assertEqual (h .hexdigest (), self . expected )
317
333
334
+ @requires_hashdigest ('sha256' )
318
335
def test_with_memoryview_msg (self ):
319
336
try :
320
- h = hmac .HMAC (b"key" , memoryview (b"hash this!" ), digestmod = "md5 " )
337
+ h = hmac .HMAC (b"key" , memoryview (b"hash this!" ), digestmod = "sha256 " )
321
338
except Exception :
322
339
self .fail ("Constructor call with memoryview msg raised exception." )
323
- self .assertEqual (h .hexdigest (), '34325b639da4cfd95735b381e28cb864' )
340
+ self .assertEqual (h .hexdigest (), self . expected )
324
341
342
+ @requires_hashdigest ('sha256' )
325
343
def test_withmodule (self ):
326
344
# Constructor call with text and digest module.
327
345
try :
328
- h = hmac .HMAC (b"key" , b"" , hashlib .sha1 )
346
+ h = hmac .HMAC (b"key" , b"" , hashlib .sha256 )
329
347
except Exception :
330
- self .fail ("Constructor call with hashlib.sha1 raised exception." )
348
+ self .fail ("Constructor call with hashlib.sha256 raised exception." )
331
349
332
- class SanityTestCase (unittest .TestCase ):
333
350
334
- @ignore_warning
335
- def test_default_is_md5 (self ):
336
- # Testing if HMAC defaults to MD5 algorithm.
337
- # NOTE: this whitebox test depends on the hmac class internals
338
- h = hmac .HMAC (b"key" )
339
- self .assertEqual (h .digest_cons , hashlib .md5 )
351
+ class SanityTestCase (unittest .TestCase ):
340
352
353
+ @requires_hashdigest ('sha256' )
341
354
def test_exercise_all_methods (self ):
342
355
# Exercising all methods once.
343
356
# This must not raise any exceptions
344
357
try :
345
- h = hmac .HMAC (b"my secret key" , digestmod = "md5 " )
358
+ h = hmac .HMAC (b"my secret key" , digestmod = "sha256 " )
346
359
h .update (b"compute the hash of this text!" )
347
360
dig = h .digest ()
348
361
dig = h .hexdigest ()
349
362
h2 = h .copy ()
350
363
except Exception :
351
364
self .fail ("Exception raised during normal usage of HMAC class." )
352
365
366
+
353
367
class CopyTestCase (unittest .TestCase ):
354
368
369
+ @requires_hashdigest ('sha256' )
355
370
def test_attributes (self ):
356
371
# Testing if attributes are of same type.
357
- h1 = hmac .HMAC (b"key" , digestmod = "md5 " )
372
+ h1 = hmac .HMAC (b"key" , digestmod = "sha256 " )
358
373
h2 = h1 .copy ()
359
374
self .assertTrue (h1 .digest_cons == h2 .digest_cons ,
360
375
"digest constructors don't match." )
@@ -363,9 +378,10 @@ def test_attributes(self):
363
378
self .assertEqual (type (h1 .outer ), type (h2 .outer ),
364
379
"Types of outer don't match." )
365
380
381
+ @requires_hashdigest ('sha256' )
366
382
def test_realcopy (self ):
367
383
# Testing if the copy method created a real copy.
368
- h1 = hmac .HMAC (b"key" , digestmod = "md5 " )
384
+ h1 = hmac .HMAC (b"key" , digestmod = "sha256 " )
369
385
h2 = h1 .copy ()
370
386
# Using id() in case somebody has overridden __eq__/__ne__.
371
387
self .assertTrue (id (h1 ) != id (h2 ), "No real copy of the HMAC instance." )
@@ -374,9 +390,10 @@ def test_realcopy(self):
374
390
self .assertTrue (id (h1 .outer ) != id (h2 .outer ),
375
391
"No real copy of the attribute 'outer'." )
376
392
393
+ @requires_hashdigest ('sha256' )
377
394
def test_equality (self ):
378
395
# Testing if the copy has the same digests.
379
- h1 = hmac .HMAC (b"key" , digestmod = "md5 " )
396
+ h1 = hmac .HMAC (b"key" , digestmod = "sha256 " )
380
397
h1 .update (b"some random text" )
381
398
h2 = h1 .copy ()
382
399
self .assertEqual (h1 .digest (), h2 .digest (),
0 commit comments