[3.13] gh-141907: Better handle support for SHA3 for test_hashlib (GH-141908) (#141919)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if the ABI has changed (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run

gh-141907: Better handle support for SHA3 for test_hashlib (GH-141908)

* test_hashlib: better handle support for SHA3

It's possible that the SSL library supports only SHA3 algo and doesn't
have SHAKE one.

The current test wrongly detect this and set both HASH and HASHXOF to
None expecting to have the extra SHA3 attributes present but this should
only be true for SHAKE algo.

To better handle this, move the HASH condition to a dedicated try-expect
condition and check if HASHXOF is None in the relevant code effectively
checking if SHA3 is supported by the SSL library but SHAKE algo needs to
use the sha3module one.

---------
(cherry picked from commit fee7782650)

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Co-authored-by: Christian Marangi <ansuelsmth@gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
Miss Islington (bot) 2025-11-25 02:32:55 +01:00 committed by GitHub
parent 9127013b56
commit c0c13f206c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -40,12 +40,15 @@ else:
openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
try:
from _hashlib import HASH, HASHXOF, openssl_md_meth_names, get_fips_mode
import _hashlib
except ImportError:
HASH = None
HASHXOF = None
openssl_md_meth_names = frozenset()
_hashlib = None
# The extension module may exist but only define some of these. gh-141907
HASH = getattr(_hashlib, 'HASH', None)
HASHXOF = getattr(_hashlib, 'HASHXOF', None)
openssl_md_meth_names = getattr(_hashlib, 'openssl_md_meth_names', frozenset())
get_fips_mode = getattr(_hashlib, 'get_fips_mode', None)
if not get_fips_mode:
def get_fips_mode():
return 0
@ -558,9 +561,14 @@ class HashLibTestCase(unittest.TestCase):
constructors = self.constructors_to_test[name]
for hash_object_constructor in constructors:
m = hash_object_constructor()
if HASH is not None and isinstance(m, HASH):
# _hashopenssl's variant does not have extra SHA3 attributes
continue
if name.startswith('shake_'):
if HASHXOF is not None and isinstance(m, HASHXOF):
# _hashopenssl's variant does not have extra SHA3 attributes
continue
else:
if HASH is not None and isinstance(m, HASH):
# _hashopenssl's variant does not have extra SHA3 attributes
continue
self.assertEqual(capacity + rate, 1600)
self.assertEqual(m._capacity_bits, capacity)
self.assertEqual(m._rate_bits, rate)
@ -1057,7 +1065,8 @@ class HashLibTestCase(unittest.TestCase):
def test_hash_disallow_instantiation(self):
# internal types like _hashlib.HASH are not constructable
support.check_disallow_instantiation(self, HASH)
support.check_disallow_instantiation(self, HASHXOF)
if HASHXOF is not None:
support.check_disallow_instantiation(self, HASHXOF)
def test_readonly_types(self):
for algorithm, constructors in self.constructors_to_test.items():