21
21
import hmac
22
22
import hashlib
23
23
import random
24
- import test .support .hashlib_helper as hashlib_helper
25
24
import types
26
25
import unittest
27
26
import unittest .mock as mock
28
27
import warnings
29
28
from _operator import _compare_digest as operator_compare_digest
29
+ from test .support import _4G , bigmemtest
30
30
from test .support import check_disallow_instantiation
31
+ from test .support import hashlib_helper , import_helper
31
32
from test .support .hashlib_helper import (
32
33
BuiltinHashFunctionsTrait ,
33
34
HashFunctionsTrait ,
34
35
NamedHashFunctionsTrait ,
35
36
OpenSSLHashFunctionsTrait ,
36
37
)
37
- from test .support .import_helper import import_fresh_module , import_module
38
+ from test .support .import_helper import import_fresh_module
39
+ from unittest .mock import patch
38
40
39
41
try :
40
42
import _hashlib
@@ -949,7 +951,11 @@ class PyConstructorTestCase(ThroughObjectMixin, PyConstructorBaseMixin,
949
951
950
952
class PyModuleConstructorTestCase (ThroughModuleAPIMixin , PyConstructorBaseMixin ,
951
953
unittest .TestCase ):
952
- """Test the hmac.new() and hmac.digest() functions."""
954
+ """Test the hmac.new() and hmac.digest() functions.
955
+
956
+ Note that "self.hmac" is imported by blocking "_hashlib" and "_hmac".
957
+ For testing functions in "hmac", extend PyMiscellaneousTests instead.
958
+ """
953
959
954
960
def test_hmac_digest_digestmod_parameter (self ):
955
961
func = self .hmac_digest
@@ -1499,6 +1505,55 @@ def test_with_fallback(self):
1499
1505
finally :
1500
1506
cache .pop ('foo' )
1501
1507
1508
+ @hashlib_helper .requires_openssl_hashdigest ("md5" )
1509
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1510
+ def test_hmac_digest_overflow_error_openssl_only (self , size ):
1511
+ self .do_test_hmac_digest_overflow_error_fast (size , openssl = True )
1512
+
1513
+ @hashlib_helper .requires_builtin_hashdigest ("_md5" , "md5" )
1514
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1515
+ def test_hmac_digest_overflow_error_builtin_only (self , size ):
1516
+ self .do_test_hmac_digest_overflow_error_fast (size , openssl = False )
1517
+
1518
+ def do_test_hmac_digest_overflow_error_fast (self , size , * , openssl ):
1519
+ """Check that C hmac.digest() works for large inputs."""
1520
+
1521
+ if openssl :
1522
+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" ])
1523
+ c_module_name , c_method_name = "_hmac" , "new"
1524
+ else :
1525
+ hmac = import_fresh_module ("hmac" , blocked = ["_hmac" ])
1526
+ c_module_name , c_method_name = "_hashlib" , "hmac_new"
1527
+
1528
+ cext = import_helper .import_module (c_module_name )
1529
+ cnew = getattr (cext , c_method_name )
1530
+
1531
+ bigkey = b'K' * size
1532
+ bigmsg = b'M' * size
1533
+
1534
+ with patch .object (hmac , "_compute_digest_fallback" ) as slow :
1535
+ with patch .object (cext , c_method_name , wraps = cnew ) as new :
1536
+ self .assertIsInstance (hmac .digest (bigkey , b'm' , "md5" ), bytes )
1537
+ new .assert_called_once ()
1538
+ with patch .object (cext , c_method_name , wraps = cnew ) as new :
1539
+ self .assertIsInstance (hmac .digest (b'k' , bigmsg , "md5" ), bytes )
1540
+ new .assert_called_once ()
1541
+ slow .assert_not_called ()
1542
+
1543
+ @hashlib_helper .requires_hashdigest ("md5" , openssl = True )
1544
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1545
+ def test_hmac_digest_no_overflow_error_in_fallback (self , size ):
1546
+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" , "_hmac" ])
1547
+
1548
+ for key , msg in [(b'K' * size , b'm' ), (b'k' , b'M' * size )]:
1549
+ with self .subTest (keysize = len (key ), msgsize = len (msg )):
1550
+ with unittest .mock .patch .object (
1551
+ hmac , "_compute_digest_fallback" ,
1552
+ wraps = hmac ._compute_digest_fallback ,
1553
+ ) as f :
1554
+ self .assertIsInstance (hmac .digest (key , msg , "md5" ), bytes )
1555
+ f .assert_called_once ()
1556
+
1502
1557
1503
1558
class BuiltinMiscellaneousTests (BuiltinModuleMixin , unittest .TestCase ):
1504
1559
"""HMAC-BLAKE2 is not standardized as BLAKE2 is a keyed hash function.
@@ -1511,7 +1566,7 @@ class BuiltinMiscellaneousTests(BuiltinModuleMixin, unittest.TestCase):
1511
1566
@classmethod
1512
1567
def setUpClass (cls ):
1513
1568
super ().setUpClass ()
1514
- cls .blake2 = import_module ("_blake2" )
1569
+ cls .blake2 = import_helper . import_module ("_blake2" )
1515
1570
cls .blake2b = cls .blake2 .blake2b
1516
1571
cls .blake2s = cls .blake2 .blake2s
1517
1572
0 commit comments