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
- import unittest .mock as mock
28
26
import warnings
29
27
from _operator import _compare_digest as operator_compare_digest
28
+ from test .support import _4G , bigmemtest
30
29
from test .support import check_disallow_instantiation
30
+ from test .support import hashlib_helper , import_helper
31
31
from test .support .hashlib_helper import (
32
32
BuiltinHashFunctionsTrait ,
33
33
HashFunctionsTrait ,
34
34
NamedHashFunctionsTrait ,
35
35
OpenSSLHashFunctionsTrait ,
36
36
)
37
- from test .support .import_helper import import_fresh_module , import_module
37
+ from test .support .import_helper import import_fresh_module
38
+ from unittest .mock import patch
38
39
39
40
try :
40
41
import _hashlib
@@ -727,7 +728,7 @@ def setUpClass(cls):
727
728
super ().setUpClass ()
728
729
for meth in ['_init_openssl_hmac' , '_init_builtin_hmac' ]:
729
730
fn = getattr (cls .hmac .HMAC , meth )
730
- cm = mock . patch .object (cls .hmac .HMAC , meth , autospec = True , wraps = fn )
731
+ cm = patch .object (cls .hmac .HMAC , meth , autospec = True , wraps = fn )
731
732
cls .enterClassContext (cm )
732
733
733
734
@classmethod
@@ -949,7 +950,11 @@ class PyConstructorTestCase(ThroughObjectMixin, PyConstructorBaseMixin,
949
950
950
951
class PyModuleConstructorTestCase (ThroughModuleAPIMixin , PyConstructorBaseMixin ,
951
952
unittest .TestCase ):
952
- """Test the hmac.new() and hmac.digest() functions."""
953
+ """Test the hmac.new() and hmac.digest() functions.
954
+
955
+ Note that "self.hmac" is imported by blocking "_hashlib" and "_hmac".
956
+ For testing functions in "hmac", extend PyMiscellaneousTests instead.
957
+ """
953
958
954
959
def test_hmac_digest_digestmod_parameter (self ):
955
960
func = self .hmac_digest
@@ -1445,9 +1450,8 @@ def test_hmac_constructor_uses_builtin(self):
1445
1450
hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" ])
1446
1451
1447
1452
def watch_method (cls , name ):
1448
- return mock .patch .object (
1449
- cls , name , autospec = True , wraps = getattr (cls , name )
1450
- )
1453
+ wraps = getattr (cls , name )
1454
+ return patch .object (cls , name , autospec = True , wraps = wraps )
1451
1455
1452
1456
with (
1453
1457
watch_method (hmac .HMAC , '_init_openssl_hmac' ) as f ,
@@ -1499,6 +1503,52 @@ def test_with_fallback(self):
1499
1503
finally :
1500
1504
cache .pop ('foo' )
1501
1505
1506
+ @hashlib_helper .requires_openssl_hashdigest ("md5" )
1507
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1508
+ def test_hmac_digest_overflow_error_openssl_only (self , size ):
1509
+ self .do_test_hmac_digest_overflow_error_fast (size , openssl = True )
1510
+
1511
+ @hashlib_helper .requires_builtin_hashdigest ("_md5" , "md5" )
1512
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1513
+ def test_hmac_digest_overflow_error_builtin_only (self , size ):
1514
+ self .do_test_hmac_digest_overflow_error_fast (size , openssl = False )
1515
+
1516
+ def do_test_hmac_digest_overflow_error_fast (self , size , * , openssl ):
1517
+ """Check that C hmac.digest() works for large inputs."""
1518
+
1519
+ if openssl :
1520
+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" ])
1521
+ c_module_name , c_method_name = "_hmac" , "new"
1522
+ else :
1523
+ hmac = import_fresh_module ("hmac" , blocked = ["_hmac" ])
1524
+ c_module_name , c_method_name = "_hashlib" , "hmac_new"
1525
+
1526
+ cext = import_helper .import_module (c_module_name )
1527
+ cnew = getattr (cext , c_method_name )
1528
+
1529
+ bigkey = b'K' * size
1530
+ bigmsg = b'M' * size
1531
+
1532
+ with patch .object (hmac , "_compute_digest_fallback" ) as slow :
1533
+ with patch .object (cext , c_method_name , wraps = cnew ) as new :
1534
+ self .assertIsInstance (hmac .digest (bigkey , b'm' , "md5" ), bytes )
1535
+ new .assert_called_once ()
1536
+ with patch .object (cext , c_method_name , wraps = cnew ) as new :
1537
+ self .assertIsInstance (hmac .digest (b'k' , bigmsg , "md5" ), bytes )
1538
+ new .assert_called_once ()
1539
+ slow .assert_not_called ()
1540
+
1541
+ @hashlib_helper .requires_hashdigest ("md5" , openssl = True )
1542
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1543
+ def test_hmac_digest_no_overflow_error_in_fallback (self , size ):
1544
+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" , "_hmac" ])
1545
+
1546
+ for key , msg in [(b'K' * size , b'm' ), (b'k' , b'M' * size )]:
1547
+ with self .subTest (keysize = len (key ), msgsize = len (msg )):
1548
+ with patch .object (hmac , "_compute_digest_fallback" ) as slow :
1549
+ self .assertIsInstance (hmac .digest (key , msg , "md5" ), bytes )
1550
+ slow .assert_called_once ()
1551
+
1502
1552
1503
1553
class BuiltinMiscellaneousTests (BuiltinModuleMixin , unittest .TestCase ):
1504
1554
"""HMAC-BLAKE2 is not standardized as BLAKE2 is a keyed hash function.
@@ -1511,7 +1561,7 @@ class BuiltinMiscellaneousTests(BuiltinModuleMixin, unittest.TestCase):
1511
1561
@classmethod
1512
1562
def setUpClass (cls ):
1513
1563
super ().setUpClass ()
1514
- cls .blake2 = import_module ("_blake2" )
1564
+ cls .blake2 = import_helper . import_module ("_blake2" )
1515
1565
cls .blake2b = cls .blake2 .blake2b
1516
1566
cls .blake2s = cls .blake2 .blake2s
1517
1567
0 commit comments