Skip to content

Commit c0af56f

Browse files
committed
[3.14] gh-145028: Fix blake2 tests in test_hashlib when it is missing due to build config (GH-145029)
specifically configure --without-builtin-hashlib-hashes means the otherwise guaranteed available blake2 family will not exist. this allows the test suite to still pass. (cherry picked from commit 273d506) Co-authored-by: Rafael Santos <tucif@users.noreply.github.com>
1 parent 1f3ea54 commit c0af56f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Lib/test/test_hashlib.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ def __init__(self, *args, **kwargs):
127127
algorithms.add(algorithm.lower())
128128

129129
_blake2 = self._conditional_import_module('_blake2')
130+
blake2_hashes = {'blake2b', 'blake2s'}
130131
if _blake2:
131-
algorithms.update({'blake2b', 'blake2s'})
132+
algorithms.update(blake2_hashes)
133+
else:
134+
algorithms.difference_update(blake2_hashes)
132135

133136
self.constructors_to_test = {}
134137
for algorithm in algorithms:
@@ -220,7 +223,12 @@ def test_algorithms_available(self):
220223
# all available algorithms must be loadable, bpo-47101
221224
self.assertNotIn("undefined", hashlib.algorithms_available)
222225
for name in hashlib.algorithms_available:
223-
digest = hashlib.new(name, usedforsecurity=False)
226+
with self.subTest(name):
227+
try:
228+
_ = hashlib.new(name, usedforsecurity=False)
229+
except ValueError as exc:
230+
self.skip_if_blake2_not_builtin(name, exc)
231+
raise
224232

225233
def test_usedforsecurity_true(self):
226234
hashlib.new("sha256", usedforsecurity=True)
@@ -443,6 +451,7 @@ def test_sha3_256_update_over_4gb(self):
443451
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")
444452

445453
@requires_resource('cpu')
454+
@requires_blake2
446455
def test_blake2_update_over_4gb(self):
447456
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
448457
# common loop macro logic.
@@ -733,6 +742,12 @@ def test_case_sha512_3(self):
733742
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
734743
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
735744

745+
def skip_if_blake2_not_builtin(self, name, skip_reason):
746+
# blake2 builtins may be absent if python built with
747+
# a subset of --with-builtin-hashlib-hashes or none.
748+
if "blake2" in name and "blake2" not in builtin_hashes:
749+
self.skipTest(skip_reason)
750+
736751
def check_blake2(self, constructor, salt_size, person_size, key_size,
737752
digest_size, max_offset):
738753
self.assertEqual(constructor.SALT_SIZE, salt_size)

0 commit comments

Comments
 (0)