Skip to content

Commit 14e5d3d

Browse files
authored
Merge branch 'main' into branch-unique-reference-tracking
2 parents 0b07ac4 + 273d506 commit 14e5d3d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Lib/test/test_hashlib.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ def __init__(self, *args, **kwargs):
134134
algorithms.add(algorithm.lower())
135135

136136
_blake2 = self._conditional_import_module('_blake2')
137+
blake2_hashes = {'blake2b', 'blake2s'}
137138
if _blake2:
138-
algorithms.update({'blake2b', 'blake2s'})
139+
algorithms.update(blake2_hashes)
140+
else:
141+
algorithms.difference_update(blake2_hashes)
139142

140143
self.constructors_to_test = {}
141144
for algorithm in algorithms:
@@ -232,7 +235,12 @@ def test_algorithms_available(self):
232235
# all available algorithms must be loadable, bpo-47101
233236
self.assertNotIn("undefined", hashlib.algorithms_available)
234237
for name in hashlib.algorithms_available:
235-
digest = hashlib.new(name, usedforsecurity=False)
238+
with self.subTest(name):
239+
try:
240+
_ = hashlib.new(name, usedforsecurity=False)
241+
except ValueError as exc:
242+
self.skip_if_blake2_not_builtin(name, exc)
243+
raise
236244

237245
def test_usedforsecurity_true(self):
238246
hashlib.new("sha256", usedforsecurity=True)
@@ -504,6 +512,7 @@ def test_sha3_256_update_over_4gb(self):
504512
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")
505513

506514
@requires_resource('cpu')
515+
@requires_blake2
507516
def test_blake2_update_over_4gb(self):
508517
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
509518
# common loop macro logic.
@@ -798,6 +807,12 @@ def test_case_sha512_3(self):
798807
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
799808
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
800809

810+
def skip_if_blake2_not_builtin(self, name, skip_reason):
811+
# blake2 builtins may be absent if python built with
812+
# a subset of --with-builtin-hashlib-hashes or none.
813+
if "blake2" in name and "blake2" not in builtin_hashes:
814+
self.skipTest(skip_reason)
815+
801816
def check_blake2(self, constructor, salt_size, person_size, key_size,
802817
digest_size, max_offset):
803818
self.assertEqual(constructor.SALT_SIZE, salt_size)
@@ -1080,10 +1095,16 @@ def test_sha256_gil(self):
10801095
def test_threaded_hashing_fast(self):
10811096
# Same as test_threaded_hashing_slow() but only tests some functions
10821097
# since otherwise test_hashlib.py becomes too slow during development.
1083-
for name in ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']:
1098+
algos = ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']
1099+
for name in algos:
10841100
if constructor := getattr(hashlib, name, None):
10851101
with self.subTest(name):
1086-
self.do_test_threaded_hashing(constructor, is_shake=False)
1102+
try:
1103+
self.do_test_threaded_hashing(constructor, is_shake=False)
1104+
except ValueError as exc:
1105+
self.skip_if_blake2_not_builtin(name, exc)
1106+
raise
1107+
10871108
if shake_128 := getattr(hashlib, 'shake_128', None):
10881109
self.do_test_threaded_hashing(shake_128, is_shake=True)
10891110

0 commit comments

Comments
 (0)