2121import hmac
2222import hashlib
2323import random
24- import test . support . hashlib_helper as hashlib_helper
24+ import sys
2525import types
2626import unittest
2727import unittest .mock as mock
2828import warnings
2929from _operator import _compare_digest as operator_compare_digest
30+ from test .support import _4G , bigmemtest
3031from test .support import check_disallow_instantiation
32+ from test .support import hashlib_helper , import_helper
3133from test .support .hashlib_helper import (
3234 BuiltinHashFunctionsTrait ,
3335 HashFunctionsTrait ,
3436 NamedHashFunctionsTrait ,
3537 OpenSSLHashFunctionsTrait ,
3638)
37- from test .support .import_helper import import_fresh_module , import_module
39+ from test .support .import_helper import import_fresh_module
3840
3941try :
4042 import _hashlib
@@ -949,7 +951,11 @@ class PyConstructorTestCase(ThroughObjectMixin, PyConstructorBaseMixin,
949951
950952class PyModuleConstructorTestCase (ThroughModuleAPIMixin , PyConstructorBaseMixin ,
951953 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+ """
953959
954960 def test_hmac_digest_digestmod_parameter (self ):
955961 func = self .hmac_digest
@@ -1499,6 +1505,49 @@ def test_with_fallback(self):
14991505 finally :
15001506 cache .pop ('foo' )
15011507
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+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1523+ def test_hmac_digest_overflow_error_no_builtin (self , size ):
1524+ capi = import_helper .import_module ("_testcapi" )
1525+ hmac = import_fresh_module ("hmac" , blocked = ["_hmac" ])
1526+
1527+ INT_MAX = capi .INT_MAX
1528+ self .assertLessEqual (INT_MAX , size )
1529+ bigkey = b'K' * INT_MAX
1530+ bigmsg = b'M' * INT_MAX
1531+
1532+ with unittest .mock .patch .object (hmac , "_compute_digest_fallback" ) as f :
1533+ self .assertRaises (OverflowError , hmac .digest , bigkey , b'm' , "md5" )
1534+ self .assertRaises (OverflowError , hmac .digest , b'k' , bigmsg , "md5" )
1535+ f .assert_not_called ()
1536+
1537+ @bigmemtest (size = _4G , memuse = 2 , dry_run = False )
1538+ def test_hmac_digest_overflow_error_no_openssl_no_builtin (self , size ):
1539+ hmac = import_fresh_module ("hmac" , blocked = ["_hashlib" , "_hmac" ])
1540+
1541+ bigkey = b'K' * size
1542+ bigmsg = b'M' * size
1543+
1544+ with unittest .mock .patch .object (hmac , "_compute_digest_fallback" ) as f :
1545+ hmac .digest (bigkey , b'm' , "md5" )
1546+ f .assert_called_once ()
1547+ with unittest .mock .patch .object (hmac , "_compute_digest_fallback" ) as f :
1548+ hmac .digest (b'k' , bigmsg , "md5" )
1549+ f .assert_called_once ()
1550+
15021551
15031552class BuiltinMiscellaneousTests (BuiltinModuleMixin , unittest .TestCase ):
15041553 """HMAC-BLAKE2 is not standardized as BLAKE2 is a keyed hash function.
@@ -1511,7 +1560,7 @@ class BuiltinMiscellaneousTests(BuiltinModuleMixin, unittest.TestCase):
15111560 @classmethod
15121561 def setUpClass (cls ):
15131562 super ().setUpClass ()
1514- cls .blake2 = import_module ("_blake2" )
1563+ cls .blake2 = import_helper . import_module ("_blake2" )
15151564 cls .blake2b = cls .blake2 .blake2b
15161565 cls .blake2s = cls .blake2 .blake2s
15171566
0 commit comments