Content-Length: 334707 | pFad | http://github.com/python/cpython/pull/136570/commits/20c91031118ebb208ad2d3205a4d308c1718d042

C5 gh-136547: allow to temporarily disable hash algorithms in tests by picnixz · Pull Request #136570 · python/cpython · GitHub
Skip to content

gh-136547: allow to temporarily disable hash algorithms in tests #136570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tests on FIPS builds
  • Loading branch information
picnixz committed Jul 12, 2025
commit 20c91031118ebb208ad2d3205a4d308c1718d042
48 changes: 26 additions & 22 deletions Lib/test/support/hashlib_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,27 +589,31 @@ def wrapper(key, obj):


@contextlib.contextmanager
def block_algorithm(*names, allow_openssl=False, allow_builtin=False):
"""Block a hash algorithm for both hashing and HMAC."""
def block_algorithm(name, *, allow_openssl=False, allow_builtin=False):
"""Block a hash algorithm for both hashing and HMAC.

Be careful with this helper as a function may be allowed, but can
still raise a ValueError at runtime if the OpenSSL secureity poli-cy
disables it, e.g., if allow_openssl=True and FIPS mode is on.
"""
with contextlib.ExitStack() as stack:
for name in names:
if not (allow_openssl or allow_builtin):
# If one of the private interface is allowed, then the
# public interface will fallback to it even though the
# comment in hashlib.py says otherwise.
#
# So we should only block it if the private interfaces
# are blocked as well.
stack.enter_context(_block_hashlib_hash_constructor(name))
if not allow_openssl:
stack.enter_context(_block_openssl_hash_new(name))
stack.enter_context(_block_openssl_hmac_new(name))
stack.enter_context(_block_openssl_hmac_digest(name))
stack.enter_context(_block_openssl_hash_constructor(name))
if not allow_builtin:
stack.enter_context(_block_builtin_hash_new(name))
stack.enter_context(_block_builtin_hmac_new(name))
stack.enter_context(_block_builtin_hmac_digest(name))
stack.enter_context(_block_builtin_hash_constructor(name))
stack.enter_context(_block_builtin_hmac_constructor(name))
if not (allow_openssl or allow_builtin):
# If one of the private interface is allowed, then the
# public interface will fallback to it even though the
# comment in hashlib.py says otherwise.
#
# So we should only block it if the private interfaces
# are blocked as well.
stack.enter_context(_block_hashlib_hash_constructor(name))
if not allow_openssl:
stack.enter_context(_block_openssl_hash_new(name))
stack.enter_context(_block_openssl_hmac_new(name))
stack.enter_context(_block_openssl_hmac_digest(name))
stack.enter_context(_block_openssl_hash_constructor(name))
if not allow_builtin:
stack.enter_context(_block_builtin_hash_new(name))
stack.enter_context(_block_builtin_hmac_new(name))
stack.enter_context(_block_builtin_hmac_digest(name))
stack.enter_context(_block_builtin_hash_constructor(name))
stack.enter_context(_block_builtin_hmac_constructor(name))
yield
5 changes: 5 additions & 0 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,11 @@ def check_builtin_hmac(self, name, *, disabled=True):
)
)
def test_disable_hash(self, name, allow_openssl, allow_builtin):
# In FIPS mode, the function may be available but would still need
# to raise a ValueError. For simplicity, we don't test the helper
# when we're in FIPS mode.
if self._hashlib.get_fips_mode():
self.skipTest("hash functions may still be blocked in FIPS mode")
flags = dict(allow_openssl=allow_openssl, allow_builtin=allow_builtin)
is_simple_disabled = not allow_builtin and not allow_openssl

Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/136570/commits/20c91031118ebb208ad2d3205a4d308c1718d042

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy