Skip to content

Commit 656d50f

Browse files
bpo-40637: Don't test builtin PBKDF2 without builtin hashes (GH-20980)
Skip testing of pure Python PBKDF2 when one or more builtin hash module is not available. Otherwise the import of hashlib prints noise on stderr. Signed-off-by: Christian Heimes <christian@python.org> (cherry picked from commit 975022b) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 2c38e49 commit 656d50f

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Lib/test/test_hashlib.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,26 @@
2424
# Were we compiled --with-pydebug or with #define Py_DEBUG?
2525
COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
2626

27-
c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
28-
py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
29-
27+
# default builtin hash module
28+
default_builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'}
29+
# --with-builtin-hashlib-hashes override
3030
builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
3131
if builtin_hashes is None:
32-
builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'}
32+
builtin_hashes = default_builtin_hashes
3333
else:
3434
builtin_hashes = {
3535
m.strip() for m in builtin_hashes.strip('"').lower().split(",")
3636
}
3737

38+
# hashlib with and without OpenSSL backend for PBKDF2
39+
# only import builtin_hashlib when all builtin hashes are available.
40+
# Otherwise import prints noise on stderr
41+
openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
42+
if builtin_hashes == default_builtin_hashes:
43+
builtin_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
44+
else:
45+
builtin_hashlib = None
46+
3847
try:
3948
from _hashlib import HASH, HASHXOF, openssl_md_meth_names
4049
except ImportError:
@@ -1030,16 +1039,16 @@ def _test_pbkdf2_hmac(self, pbkdf2, supported):
10301039
iterations=1, dklen=None)
10311040
self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
10321041

1042+
@unittest.skipIf(builtin_hashlib is None, "test requires builtin_hashlib")
10331043
def test_pbkdf2_hmac_py(self):
1034-
self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac, builtin_hashes)
1044+
self._test_pbkdf2_hmac(builtin_hashlib.pbkdf2_hmac, builtin_hashes)
10351045

1036-
@unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'),
1046+
@unittest.skipUnless(hasattr(openssl_hashlib, 'pbkdf2_hmac'),
10371047
' test requires OpenSSL > 1.0')
10381048
def test_pbkdf2_hmac_c(self):
1039-
self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac, openssl_md_meth_names)
1040-
1049+
self._test_pbkdf2_hmac(openssl_hashlib.pbkdf2_hmac, openssl_md_meth_names)
10411050

1042-
@unittest.skipUnless(hasattr(c_hashlib, 'scrypt'),
1051+
@unittest.skipUnless(hasattr(hashlib, 'scrypt'),
10431052
' test requires OpenSSL > 1.1')
10441053
def test_scrypt(self):
10451054
for password, salt, n, r, p, expected in self.scrypt_test_vectors:

0 commit comments

Comments
 (0)