6
6
import unittest .mock
7
7
import warnings
8
8
9
+ from test .support import requires_hashdigest
10
+
9
11
10
12
def ignore_warning (func ):
11
13
@functools .wraps (func )
@@ -19,6 +21,7 @@ def wrapper(*args, **kwargs):
19
21
20
22
class TestVectorsTestCase (unittest .TestCase ):
21
23
24
+ @requires_hashdigest ('md5' )
22
25
def test_md5_vectors (self ):
23
26
# Test the HMAC module against test vectors from the RFC.
24
27
@@ -76,6 +79,7 @@ def md5test(key, data, digest):
76
79
b"and Larger Than One Block-Size Data" ),
77
80
"6f630fad67cda0ee1fb1f562db3aa53e" )
78
81
82
+ @requires_hashdigest ('sha1' )
79
83
def test_sha_vectors (self ):
80
84
def shatest (key , data , digest ):
81
85
h = hmac .HMAC (key , data , digestmod = hashlib .sha1 )
@@ -268,23 +272,28 @@ def hmactest(key, data, hexdigests):
268
272
'134676fb6de0446065c97440fa8c6a58' ,
269
273
})
270
274
275
+ @requires_hashdigest ('sha224' )
271
276
def test_sha224_rfc4231 (self ):
272
277
self ._rfc4231_test_cases (hashlib .sha224 , 'sha224' , 28 , 64 )
273
278
279
+ @requires_hashdigest ('sha256' )
274
280
def test_sha256_rfc4231 (self ):
275
281
self ._rfc4231_test_cases (hashlib .sha256 , 'sha256' , 32 , 64 )
276
282
283
+ @requires_hashdigest ('sha384' )
277
284
def test_sha384_rfc4231 (self ):
278
285
self ._rfc4231_test_cases (hashlib .sha384 , 'sha384' , 48 , 128 )
279
286
287
+ @requires_hashdigest ('sha512' )
280
288
def test_sha512_rfc4231 (self ):
281
289
self ._rfc4231_test_cases (hashlib .sha512 , 'sha512' , 64 , 128 )
282
290
291
+ @requires_hashdigest ('sha256' )
283
292
def test_legacy_block_size_warnings (self ):
284
293
class MockCrazyHash (object ):
285
294
"""Ain't no block_size attribute here."""
286
295
def __init__ (self , * args ):
287
- self ._x = hashlib .sha1 (* args )
296
+ self ._x = hashlib .sha256 (* args )
288
297
self .digest_size = self ._x .digest_size
289
298
def update (self , v ):
290
299
self ._x .update (v )
@@ -308,77 +317,92 @@ def test_with_digestmod_no_default(self):
308
317
data = b"Hi There"
309
318
hmac .HMAC (key , data , digestmod = None )
310
319
320
+
311
321
class ConstructorTestCase (unittest .TestCase ):
312
322
323
+ expected = (
324
+ "6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdeb"
325
+ )
326
+
327
+ @requires_hashdigest ('sha256' )
313
328
def test_normal (self ):
314
329
# Standard constructor call.
315
- failed = 0
316
330
try :
317
- h = hmac .HMAC (b"key" , digestmod = 'md5 ' )
331
+ hmac .HMAC (b"key" , digestmod = 'sha256 ' )
318
332
except Exception :
319
333
self .fail ("Standard constructor call raised exception." )
320
334
335
+ @requires_hashdigest ('sha256' )
321
336
def test_with_str_key (self ):
322
337
# Pass a key of type str, which is an error, because it expects a key
323
338
# of type bytes
324
339
with self .assertRaises (TypeError ):
325
- h = hmac .HMAC ("key" , digestmod = 'md5 ' )
340
+ h = hmac .HMAC ("key" , digestmod = 'sha256 ' )
326
341
342
+ @requires_hashdigest ('sha256' )
327
343
def test_dot_new_with_str_key (self ):
328
344
# Pass a key of type str, which is an error, because it expects a key
329
345
# of type bytes
330
346
with self .assertRaises (TypeError ):
331
- h = hmac .new ("key" , digestmod = 'md5 ' )
347
+ h = hmac .new ("key" , digestmod = 'sha256 ' )
332
348
349
+ @requires_hashdigest ('sha256' )
333
350
def test_withtext (self ):
334
351
# Constructor call with text.
335
352
try :
336
- h = hmac .HMAC (b"key" , b"hash this!" , digestmod = 'md5 ' )
353
+ h = hmac .HMAC (b"key" , b"hash this!" , digestmod = 'sha256 ' )
337
354
except Exception :
338
355
self .fail ("Constructor call with text argument raised exception." )
339
- self .assertEqual (h .hexdigest (), '34325b639da4cfd95735b381e28cb864' )
356
+ self .assertEqual (h .hexdigest (), self . expected )
340
357
358
+ @requires_hashdigest ('sha256' )
341
359
def test_with_bytearray (self ):
342
360
try :
343
361
h = hmac .HMAC (bytearray (b"key" ), bytearray (b"hash this!" ),
344
- digestmod = "md5 " )
362
+ digestmod = "sha256 " )
345
363
except Exception :
346
364
self .fail ("Constructor call with bytearray arguments raised exception." )
347
- self .assertEqual (h .hexdigest (), '34325b639da4cfd95735b381e28cb864' )
365
+ self .assertEqual (h .hexdigest (), self . expected )
348
366
367
+ @requires_hashdigest ('sha256' )
349
368
def test_with_memoryview_msg (self ):
350
369
try :
351
- h = hmac .HMAC (b"key" , memoryview (b"hash this!" ), digestmod = "md5 " )
370
+ h = hmac .HMAC (b"key" , memoryview (b"hash this!" ), digestmod = "sha256 " )
352
371
except Exception :
353
372
self .fail ("Constructor call with memoryview msg raised exception." )
354
- self .assertEqual (h .hexdigest (), '34325b639da4cfd95735b381e28cb864' )
373
+ self .assertEqual (h .hexdigest (), self . expected )
355
374
375
+ @requires_hashdigest ('sha256' )
356
376
def test_withmodule (self ):
357
377
# Constructor call with text and digest module.
358
378
try :
359
- h = hmac .HMAC (b"key" , b"" , hashlib .sha1 )
379
+ h = hmac .HMAC (b"key" , b"" , hashlib .sha256 )
360
380
except Exception :
361
- self .fail ("Constructor call with hashlib.sha1 raised exception." )
381
+ self .fail ("Constructor call with hashlib.sha256 raised exception." )
382
+
362
383
363
384
class SanityTestCase (unittest .TestCase ):
364
385
386
+ @requires_hashdigest ('sha256' )
365
387
def test_exercise_all_methods (self ):
366
388
# Exercising all methods once.
367
389
# This must not raise any exceptions
368
390
try :
369
- h = hmac .HMAC (b"my secret key" , digestmod = "md5 " )
391
+ h = hmac .HMAC (b"my secret key" , digestmod = "sha256 " )
370
392
h .update (b"compute the hash of this text!" )
371
393
dig = h .digest ()
372
394
dig = h .hexdigest ()
373
395
h2 = h .copy ()
374
396
except Exception :
375
397
self .fail ("Exception raised during normal usage of HMAC class." )
376
398
399
+
377
400
class CopyTestCase (unittest .TestCase ):
378
401
402
+ @requires_hashdigest ('sha256' )
379
403
def test_attributes (self ):
380
404
# Testing if attributes are of same type.
381
- h1 = hmac .HMAC (b"key" , digestmod = "md5 " )
405
+ h1 = hmac .HMAC (b"key" , digestmod = "sha256 " )
382
406
h2 = h1 .copy ()
383
407
self .assertTrue (h1 .digest_cons == h2 .digest_cons ,
384
408
"digest constructors don't match." )
@@ -387,9 +411,10 @@ def test_attributes(self):
387
411
self .assertEqual (type (h1 .outer ), type (h2 .outer ),
388
412
"Types of outer don't match." )
389
413
414
+ @requires_hashdigest ('sha256' )
390
415
def test_realcopy (self ):
391
416
# Testing if the copy method created a real copy.
392
- h1 = hmac .HMAC (b"key" , digestmod = "md5 " )
417
+ h1 = hmac .HMAC (b"key" , digestmod = "sha256 " )
393
418
h2 = h1 .copy ()
394
419
# Using id() in case somebody has overridden __eq__/__ne__.
395
420
self .assertTrue (id (h1 ) != id (h2 ), "No real copy of the HMAC instance." )
@@ -398,9 +423,10 @@ def test_realcopy(self):
398
423
self .assertTrue (id (h1 .outer ) != id (h2 .outer ),
399
424
"No real copy of the attribute 'outer'." )
400
425
426
+ @requires_hashdigest ('sha256' )
401
427
def test_equality (self ):
402
428
# Testing if the copy has the same digests.
403
- h1 = hmac .HMAC (b"key" , digestmod = "md5 " )
429
+ h1 = hmac .HMAC (b"key" , digestmod = "sha256 " )
404
430
h1 .update (b"some random text" )
405
431
h2 = h1 .copy ()
406
432
self .assertEqual (h1 .digest (), h2 .digest (),
0 commit comments