66import unittest .mock
77import warnings
88
9- from test .support import requires_hashdigest
9+ from test .support import hashlib_helper
1010
1111
1212def ignore_warning (func ):
@@ -21,7 +21,7 @@ def wrapper(*args, **kwargs):
2121
2222class TestVectorsTestCase (unittest .TestCase ):
2323
24- @requires_hashdigest ('md5' , openssl = True )
24+ @hashlib_helper . requires_hashdigest ('md5' , openssl = True )
2525 def test_md5_vectors (self ):
2626 # Test the HMAC module against test vectors from the RFC.
2727
@@ -79,7 +79,7 @@ def md5test(key, data, digest):
7979 b"and Larger Than One Block-Size Data" ),
8080 "6f630fad67cda0ee1fb1f562db3aa53e" )
8181
82- @requires_hashdigest ('sha1' , openssl = True )
82+ @hashlib_helper . requires_hashdigest ('sha1' , openssl = True )
8383 def test_sha_vectors (self ):
8484 def shatest (key , data , digest ):
8585 h = hmac .HMAC (key , data , digestmod = hashlib .sha1 )
@@ -272,23 +272,23 @@ def hmactest(key, data, hexdigests):
272272 '134676fb6de0446065c97440fa8c6a58' ,
273273 })
274274
275- @requires_hashdigest ('sha224' , openssl = True )
275+ @hashlib_helper . requires_hashdigest ('sha224' , openssl = True )
276276 def test_sha224_rfc4231 (self ):
277277 self ._rfc4231_test_cases (hashlib .sha224 , 'sha224' , 28 , 64 )
278278
279- @requires_hashdigest ('sha256' , openssl = True )
279+ @hashlib_helper . requires_hashdigest ('sha256' , openssl = True )
280280 def test_sha256_rfc4231 (self ):
281281 self ._rfc4231_test_cases (hashlib .sha256 , 'sha256' , 32 , 64 )
282282
283- @requires_hashdigest ('sha384' , openssl = True )
283+ @hashlib_helper . requires_hashdigest ('sha384' , openssl = True )
284284 def test_sha384_rfc4231 (self ):
285285 self ._rfc4231_test_cases (hashlib .sha384 , 'sha384' , 48 , 128 )
286286
287- @requires_hashdigest ('sha512' , openssl = True )
287+ @hashlib_helper . requires_hashdigest ('sha512' , openssl = True )
288288 def test_sha512_rfc4231 (self ):
289289 self ._rfc4231_test_cases (hashlib .sha512 , 'sha512' , 64 , 128 )
290290
291- @requires_hashdigest ('sha256' )
291+ @hashlib_helper . requires_hashdigest ('sha256' )
292292 def test_legacy_block_size_warnings (self ):
293293 class MockCrazyHash (object ):
294294 """Ain't no block_size attribute here."""
@@ -329,29 +329,29 @@ class ConstructorTestCase(unittest.TestCase):
329329 "6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdeb"
330330 )
331331
332- @requires_hashdigest ('sha256' )
332+ @hashlib_helper . requires_hashdigest ('sha256' )
333333 def test_normal (self ):
334334 # Standard constructor call.
335335 try :
336336 hmac .HMAC (b"key" , digestmod = 'sha256' )
337337 except Exception :
338338 self .fail ("Standard constructor call raised exception." )
339339
340- @requires_hashdigest ('sha256' )
340+ @hashlib_helper . requires_hashdigest ('sha256' )
341341 def test_with_str_key (self ):
342342 # Pass a key of type str, which is an error, because it expects a key
343343 # of type bytes
344344 with self .assertRaises (TypeError ):
345345 h = hmac .HMAC ("key" , digestmod = 'sha256' )
346346
347- @requires_hashdigest ('sha256' )
347+ @hashlib_helper . requires_hashdigest ('sha256' )
348348 def test_dot_new_with_str_key (self ):
349349 # Pass a key of type str, which is an error, because it expects a key
350350 # of type bytes
351351 with self .assertRaises (TypeError ):
352352 h = hmac .new ("key" , digestmod = 'sha256' )
353353
354- @requires_hashdigest ('sha256' )
354+ @hashlib_helper . requires_hashdigest ('sha256' )
355355 def test_withtext (self ):
356356 # Constructor call with text.
357357 try :
@@ -360,7 +360,7 @@ def test_withtext(self):
360360 self .fail ("Constructor call with text argument raised exception." )
361361 self .assertEqual (h .hexdigest (), self .expected )
362362
363- @requires_hashdigest ('sha256' )
363+ @hashlib_helper . requires_hashdigest ('sha256' )
364364 def test_with_bytearray (self ):
365365 try :
366366 h = hmac .HMAC (bytearray (b"key" ), bytearray (b"hash this!" ),
@@ -369,15 +369,15 @@ def test_with_bytearray(self):
369369 self .fail ("Constructor call with bytearray arguments raised exception." )
370370 self .assertEqual (h .hexdigest (), self .expected )
371371
372- @requires_hashdigest ('sha256' )
372+ @hashlib_helper . requires_hashdigest ('sha256' )
373373 def test_with_memoryview_msg (self ):
374374 try :
375375 h = hmac .HMAC (b"key" , memoryview (b"hash this!" ), digestmod = "sha256" )
376376 except Exception :
377377 self .fail ("Constructor call with memoryview msg raised exception." )
378378 self .assertEqual (h .hexdigest (), self .expected )
379379
380- @requires_hashdigest ('sha256' )
380+ @hashlib_helper . requires_hashdigest ('sha256' )
381381 def test_withmodule (self ):
382382 # Constructor call with text and digest module.
383383 try :
@@ -388,7 +388,7 @@ def test_withmodule(self):
388388
389389class SanityTestCase (unittest .TestCase ):
390390
391- @requires_hashdigest ('sha256' )
391+ @hashlib_helper . requires_hashdigest ('sha256' )
392392 def test_exercise_all_methods (self ):
393393 # Exercising all methods once.
394394 # This must not raise any exceptions
@@ -404,7 +404,7 @@ def test_exercise_all_methods(self):
404404
405405class CopyTestCase (unittest .TestCase ):
406406
407- @requires_hashdigest ('sha256' )
407+ @hashlib_helper . requires_hashdigest ('sha256' )
408408 def test_attributes (self ):
409409 # Testing if attributes are of same type.
410410 h1 = hmac .HMAC (b"key" , digestmod = "sha256" )
@@ -416,7 +416,7 @@ def test_attributes(self):
416416 self .assertEqual (type (h1 .outer ), type (h2 .outer ),
417417 "Types of outer don't match." )
418418
419- @requires_hashdigest ('sha256' )
419+ @hashlib_helper . requires_hashdigest ('sha256' )
420420 def test_realcopy (self ):
421421 # Testing if the copy method created a real copy.
422422 h1 = hmac .HMAC (b"key" , digestmod = "sha256" )
@@ -428,7 +428,7 @@ def test_realcopy(self):
428428 self .assertTrue (id (h1 .outer ) != id (h2 .outer ),
429429 "No real copy of the attribute 'outer'." )
430430
431- @requires_hashdigest ('sha256' )
431+ @hashlib_helper . requires_hashdigest ('sha256' )
432432 def test_equality (self ):
433433 # Testing if the copy has the same digests.
434434 h1 = hmac .HMAC (b"key" , digestmod = "sha256" )
0 commit comments