Skip to content

Commit 5ae0915

Browse files
committed
test_hashlib: better handle support for SHA3
It's possible that the SSL library supports only SHA3 algo and doesn't have SHAKE one. The current test wrongly detect this and set both HASH and HASHXOF to None expecting to have the extra SHA3 attributes present but this should only be true for SHAKE algo. To better handle this, move the HASH condition to a dedicated try-expect condition and check if HASHXOF is None in the relevant code effectively checking if SHA3 is supported by the SSL library but SHAKE algo needs to use the sha3module one. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
1 parent 04e3670 commit 5ae0915

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Lib/test/test_hashlib.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040
openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
4141

4242
try:
43-
from _hashlib import HASH, HASHXOF, openssl_md_meth_names, get_fips_mode
43+
from _hashlib import HASH
4444
except ImportError:
4545
HASH = None
46+
47+
try:
48+
from _hashlib import HASHXOF, openssl_md_meth_names, get_fips_mode
49+
except ImportError:
4650
HASHXOF = None
4751
openssl_md_meth_names = frozenset()
4852

@@ -631,9 +635,14 @@ def check_sha3(self, name, capacity, rate, suffix):
631635
constructors = self.constructors_to_test[name]
632636
for hash_object_constructor in constructors:
633637
m = hash_object_constructor()
634-
if HASH is not None and isinstance(m, HASH):
635-
# _hashopenssl's variant does not have extra SHA3 attributes
636-
continue
638+
if name.startswith('shake_'):
639+
if HASHXOF is not None and isinstance(m, HASHXOF):
640+
# _hashopenssl's variant does not have extra SHA3 attributes
641+
continue
642+
else:
643+
if HASH is not None and isinstance(m, HASH):
644+
# _hashopenssl's variant does not have extra SHA3 attributes
645+
continue
637646
self.assertEqual(capacity + rate, 1600)
638647
self.assertEqual(m._capacity_bits, capacity)
639648
self.assertEqual(m._rate_bits, rate)
@@ -1156,7 +1165,8 @@ def test_disallow_instantiation(self):
11561165
def test_hash_disallow_instantiation(self):
11571166
# internal types like _hashlib.HASH are not constructable
11581167
support.check_disallow_instantiation(self, HASH)
1159-
support.check_disallow_instantiation(self, HASHXOF)
1168+
if HASHXOF is not None:
1169+
support.check_disallow_instantiation(self, HASHXOF)
11601170

11611171
def test_readonly_types(self):
11621172
for algorithm, constructors in self.constructors_to_test.items():

0 commit comments

Comments
 (0)