@@ -531,40 +531,6 @@ def hmactest(key, data, hexdigests):
531531 '134676fb6de0446065c97440fa8c6a58' ,
532532 })
533533
534- @hashlib_helper .requires_hashdigest ('sha256' )
535- def test_legacy_block_size_warnings (self ):
536- class MockCrazyHash (object ):
537- """Ain't no block_size attribute here."""
538- def __init__ (self , * args ):
539- self ._x = hashlib .sha256 (* args )
540- self .digest_size = self ._x .digest_size
541- def update (self , v ):
542- self ._x .update (v )
543- def digest (self ):
544- return self ._x .digest ()
545-
546- with warnings .catch_warnings ():
547- warnings .simplefilter ('error' , RuntimeWarning )
548- with self .assertRaises (RuntimeWarning ):
549- hmac .HMAC (b'a' , b'b' , digestmod = MockCrazyHash )
550- self .fail ('Expected warning about missing block_size' )
551-
552- MockCrazyHash .block_size = 1
553- with self .assertRaises (RuntimeWarning ):
554- hmac .HMAC (b'a' , b'b' , digestmod = MockCrazyHash )
555- self .fail ('Expected warning about small block_size' )
556-
557- def test_with_fallback (self ):
558- cache = getattr (hashlib , '__builtin_constructor_cache' )
559- try :
560- cache ['foo' ] = hashlib .sha256
561- hexdigest = hmac .digest (b'key' , b'message' , 'foo' ).hex ()
562- expected = ('6e9ef29b75fffc5b7abae527d58fdadb'
563- '2fe42e7219011976917343065f58ed4a' )
564- self .assertEqual (hexdigest , expected )
565- finally :
566- cache .pop ('foo' )
567-
568534
569535class RFCWithOpenSSLHashFunctionTestCasesMixin (RFCTestCasesMixin ):
570536
@@ -1087,5 +1053,44 @@ class OperatorCompareDigestTestCase(CompareDigestMixin, unittest.TestCase):
10871053 compare_digest = operator_compare_digest
10881054
10891055
1056+ class PyMiscellaneousTests (unittest .TestCase ):
1057+ """Miscellaneous tests for the pure Python HMAC module."""
1058+
1059+ @hashlib_helper .requires_hashdigest ('sha256' )
1060+ def test_legacy_block_size_warnings (self ):
1061+ class MockCrazyHash (object ):
1062+ """Ain't no block_size attribute here."""
1063+ def __init__ (self , * args ):
1064+ self ._x = hashlib .sha256 (* args )
1065+ self .digest_size = self ._x .digest_size
1066+ def update (self , v ):
1067+ self ._x .update (v )
1068+ def digest (self ):
1069+ return self ._x .digest ()
1070+
1071+ with warnings .catch_warnings ():
1072+ warnings .simplefilter ('error' , RuntimeWarning )
1073+ with self .assertRaises (RuntimeWarning ):
1074+ hmac .HMAC (b'a' , b'b' , digestmod = MockCrazyHash )
1075+ self .fail ('Expected warning about missing block_size' )
1076+
1077+ MockCrazyHash .block_size = 1
1078+ with self .assertRaises (RuntimeWarning ):
1079+ hmac .HMAC (b'a' , b'b' , digestmod = MockCrazyHash )
1080+ self .fail ('Expected warning about small block_size' )
1081+
1082+ @hashlib_helper .requires_hashdigest ('sha256' )
1083+ def test_with_fallback (self ):
1084+ cache = getattr (hashlib , '__builtin_constructor_cache' )
1085+ try :
1086+ cache ['foo' ] = hashlib .sha256
1087+ hexdigest = hmac .digest (b'key' , b'message' , 'foo' ).hex ()
1088+ expected = ('6e9ef29b75fffc5b7abae527d58fdadb'
1089+ '2fe42e7219011976917343065f58ed4a' )
1090+ self .assertEqual (hexdigest , expected )
1091+ finally :
1092+ cache .pop ('foo' )
1093+
1094+
10901095if __name__ == "__main__" :
10911096 unittest .main ()
0 commit comments