Skip to content

Commit a608ff8

Browse files
committed
simplify how OpenSSL hash digests are requested
1 parent 8c88360 commit a608ff8

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import hashlib
33
import unittest
4+
from test.support.import_helper import import_module
45

56
try:
67
import _hashlib
@@ -68,3 +69,25 @@ def wrapper(*args, **kwargs):
6869
def decorator(func_or_class):
6970
return _decorate_func_or_class(func_or_class, decorator_func)
7071
return decorator
72+
73+
74+
def requires_openssl_hashdigest(digestname, *, usedforsecurity=True):
75+
"""Decorator raising SkipTest if an OpenSSL hashing algorithm is missing.
76+
77+
The hashing algorithm may be missing or blocked by a strict crypto policy.
78+
"""
79+
def decorator_func(func):
80+
@requires_hashlib()
81+
@functools.wraps(func)
82+
def wrapper(*args, **kwargs):
83+
try:
84+
_hashlib.new(digestname, usedforsecurity=usedforsecurity)
85+
except ValueError:
86+
msg = f"missing OpenSSL hash algorithm: {digestname!r}"
87+
raise unittest.SkipTest(msg)
88+
return func(*args, **kwargs)
89+
return wrapper
90+
91+
def decorator(func_or_class):
92+
return _decorate_func_or_class(func_or_class, decorator_func)
93+
return decorator

Lib/test/test_hmac.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ def setUpClass(cls):
358358

359359
for name in cls.ALGORITHMS:
360360
@property
361-
@hashlib_helper.requires_hashlib()
362-
@hashlib_helper.requires_hashdigest(name, openssl=True)
361+
@hashlib_helper.requires_openssl_hashdigest(name)
363362
def func(self, *, __name=name): # __name needed to bind 'name'
364363
return getattr(_hashlib, f'openssl_{__name}')
365364
setattr(cls, name, func)
@@ -845,7 +844,7 @@ def test_repr(self):
845844
self.assertStartsWith(repr(h), "<hmac.HMAC object at")
846845

847846

848-
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
847+
@hashlib_helper.requires_openssl_hashdigest('sha256')
849848
class OpenSSLSanityTestCase(ThroughOpenSSLAPIMixin, SanityTestCaseMixin,
850849
unittest.TestCase):
851850

@@ -897,8 +896,7 @@ def HMAC(self, key, msg=None):
897896
return self.hmac.HMAC(key, msg, digestmod='sha256')
898897

899898

900-
@hashlib_helper.requires_hashlib()
901-
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
899+
@hashlib_helper.requires_openssl_hashdigest('sha256')
902900
class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase):
903901

904902
def HMAC(self, key, msg=None):
@@ -988,8 +986,7 @@ def test_realcopy(self):
988986
self.assertNotEqual(id(h1._hmac), id(h2._hmac))
989987

990988

991-
@hashlib_helper.requires_hashlib()
992-
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
989+
@hashlib_helper.requires_openssl_hashdigest('sha256')
993990
class OpenSSLCopyTestCase(ExtensionCopyTestCase, unittest.TestCase):
994991

995992
def init(self, h):

0 commit comments

Comments
 (0)