Skip to content
Merged
Changes from 2 commits
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
28 changes: 24 additions & 4 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ def __init__(self, *args, **kwargs):
algorithms.add(algorithm.lower())

_blake2 = self._conditional_import_module('_blake2')
blake2_hashes = {'blake2b', 'blake2s'}
if _blake2:
algorithms.update({'blake2b', 'blake2s'})
algorithms.update(blake2_hashes)
else:
algorithms.difference_update(blake2_hashes)

self.constructors_to_test = {}
for algorithm in algorithms:
Expand Down Expand Up @@ -232,7 +235,18 @@ def test_algorithms_available(self):
# all available algorithms must be loadable, bpo-47101
self.assertNotIn("undefined", hashlib.algorithms_available)
for name in hashlib.algorithms_available:
digest = hashlib.new(name, usedforsecurity=False)
with self.subTest(name):
try:
digest = hashlib.new(name, usedforsecurity=False)
assert digest is not None
Comment thread
gpshead marked this conversation as resolved.
Outdated
except ValueError as verr:
# builtins may be absent if python built with
# a subset of --with-builtin-hashlib-hashes or none.
if ("blake2" in name and
"blake2" not in sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES").split(",")):
Comment thread
gpshead marked this conversation as resolved.
Outdated
self.skipTest(verr)
else:
raise

def test_usedforsecurity_true(self):
hashlib.new("sha256", usedforsecurity=True)
Expand Down Expand Up @@ -504,6 +518,7 @@ def test_sha3_256_update_over_4gb(self):
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")

@requires_resource('cpu')
@requires_blake2
def test_blake2_update_over_4gb(self):
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
# common loop macro logic.
Expand Down Expand Up @@ -1052,7 +1067,9 @@ def test_gil(self):
# for multithreaded operation. Currently, all cryptographic modules
# have the same constant value (2048) but in the future it might not
# be the case.
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib']
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_hashlib']
if _blake2:
mods.append('_blake2')
Comment thread
gpshead marked this conversation as resolved.
Outdated
gil_minsize = hashlib_helper.find_gil_minsize(mods)
for cons in self.hash_constructors:
# constructors belong to one of the above modules
Expand Down Expand Up @@ -1080,7 +1097,10 @@ def test_sha256_gil(self):
def test_threaded_hashing_fast(self):
# Same as test_threaded_hashing_slow() but only tests some functions
# since otherwise test_hashlib.py becomes too slow during development.
for name in ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']:
algos = ['md5', 'sha1', 'sha256', 'sha3_256']
if _blake2:
algos.append('blake2s')
Comment thread
gpshead marked this conversation as resolved.
Outdated
for name in algos:
if constructor := getattr(hashlib, name, None):
with self.subTest(name):
self.do_test_threaded_hashing(constructor, is_shake=False)
Expand Down
Loading