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>
This commit is contained in:
Christian Heimes 2020-11-17 15:40:35 +01:00 committed by GitHub
parent 2ffba2a102
commit 975022b77b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 9 deletions

View File

@ -26,17 +26,26 @@ from http.client import HTTPException
# Were we compiled --with-pydebug or with #define Py_DEBUG? # Were we compiled --with-pydebug or with #define Py_DEBUG?
COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib']) # default builtin hash module
py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib']) default_builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'}
# --with-builtin-hashlib-hashes override
builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES") builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
if builtin_hashes is None: if builtin_hashes is None:
builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'} builtin_hashes = default_builtin_hashes
else: else:
builtin_hashes = { builtin_hashes = {
m.strip() for m in builtin_hashes.strip('"').lower().split(",") m.strip() for m in builtin_hashes.strip('"').lower().split(",")
} }
# hashlib with and without OpenSSL backend for PBKDF2
# only import builtin_hashlib when all builtin hashes are available.
# Otherwise import prints noise on stderr
openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
if builtin_hashes == default_builtin_hashes:
builtin_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
else:
builtin_hashlib = None
try: try:
from _hashlib import HASH, HASHXOF, openssl_md_meth_names from _hashlib import HASH, HASHXOF, openssl_md_meth_names
except ImportError: except ImportError:
@ -1032,16 +1041,16 @@ class KDFTests(unittest.TestCase):
iterations=1, dklen=None) iterations=1, dklen=None)
self.assertEqual(out, self.pbkdf2_results['sha1'][0][0]) self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
@unittest.skipIf(builtin_hashlib is None, "test requires builtin_hashlib")
def test_pbkdf2_hmac_py(self): def test_pbkdf2_hmac_py(self):
self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac, builtin_hashes) self._test_pbkdf2_hmac(builtin_hashlib.pbkdf2_hmac, builtin_hashes)
@unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'), @unittest.skipUnless(hasattr(openssl_hashlib, 'pbkdf2_hmac'),
' test requires OpenSSL > 1.0') ' test requires OpenSSL > 1.0')
def test_pbkdf2_hmac_c(self): def test_pbkdf2_hmac_c(self):
self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac, openssl_md_meth_names) self._test_pbkdf2_hmac(openssl_hashlib.pbkdf2_hmac, openssl_md_meth_names)
@unittest.skipUnless(hasattr(hashlib, 'scrypt'),
@unittest.skipUnless(hasattr(c_hashlib, 'scrypt'),
' test requires OpenSSL > 1.1') ' test requires OpenSSL > 1.1')
def test_scrypt(self): def test_scrypt(self):
for password, salt, n, r, p, expected in self.scrypt_test_vectors: for password, salt, n, r, p, expected in self.scrypt_test_vectors: