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
38
39
39
40
try :
40
41
import _hashlib
@@ -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
@@ -1499,6 +1504,51 @@ def test_with_fallback(self):
1499
1504
finally :
1500
1505
cache .pop ('foo' )
1501
1506
1507
+ @hashlib_helper .requires_builtin_hashdigest ("_md5" , "md5" )
1508
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1509
+ def test_hmac_digest_overflow_error_no_openssl (self , size ):
1510
+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" ])
1511
+
1512
+ UINT32_MAX = (1 << 32 ) - 1
1513
+ self .assertEqual (UINT32_MAX , size - 1 )
1514
+ bigkey = b'K' * UINT32_MAX
1515
+ bigmsg = b'M' * UINT32_MAX
1516
+
1517
+ with unittest .mock .patch .object (hmac , "_compute_digest_fallback" ) as f :
1518
+ self .assertRaises (OverflowError , hmac .digest , bigkey , b'm' , "md5" )
1519
+ self .assertRaises (OverflowError , hmac .digest , b'k' , bigmsg , "md5" )
1520
+ f .assert_not_called ()
1521
+
1522
+ @hashlib_helper .requires_openssl_hashdigest ("md5" )
1523
+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1524
+ def test_hmac_digest_overflow_error_no_builtin (self , size ):
1525
+ capi = import_helper .import_module ("_testcapi" )
1526
+ hmac = import_fresh_module ("hmac" , blocked = ["_hmac" ])
1527
+
1528
+ INT_MAX = capi .INT_MAX
1529
+ self .assertLessEqual (INT_MAX , size )
1530
+ bigkey = b'K' * INT_MAX
1531
+ bigmsg = b'M' * INT_MAX
1532
+
1533
+ with unittest .mock .patch .object (hmac , "_compute_digest_fallback" ) as f :
1534
+ self .assertRaises (OverflowError , hmac .digest , bigkey , b'm' , "md5" )
1535
+ self .assertRaises (OverflowError , hmac .digest , b'k' , bigmsg , "md5" )
1536
+ f .assert_not_called ()
1537
+
1538
+ @hashlib_helper .requires_hashdigest ("md5" , openssl = True )
1539
+ @bigmemtest (size = _4G + 1 , memuse = 2 , dry_run = False )
1540
+ def test_hmac_digest_no_overflow_error_in_fallback (self , size ):
1541
+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" , "_hmac" ])
1542
+
1543
+ for key , msg in [(b'K' * size , b'm' ), (b'k' , b'M' * size )]:
1544
+ with self .subTest (keysize = len (key ), msgsize = len (msg )):
1545
+ with unittest .mock .patch .object (
1546
+ hmac , "_compute_digest_fallback" ,
1547
+ wraps = hmac ._compute_digest_fallback ,
1548
+ ) as f :
1549
+ self .assertIsInstance (hmac .digest (key , msg , "md5" ), bytes )
1550
+ f .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