Skip to content

Commit 22e3003

Browse files
committed
simplify how OpenSSL hash digests are requested
1 parent e9016c3 commit 22e3003

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 45 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
@@ -77,3 +78,47 @@ def wrapper(*args, **kwargs):
7778
def decorator(func_or_class):
7879
return _decorate_func_or_class(func_or_class, decorator_func)
7980
return decorator
81+
82+
83+
def requires_openssl_hashdigest(digestname, *, usedforsecurity=True):
84+
"""Decorator raising SkipTest if an OpenSSL hashing algorithm is missing.
85+
86+
The hashing algorithm may be missing or blocked by a strict crypto policy.
87+
"""
88+
def decorator_func(func):
89+
@requires_hashlib()
90+
@functools.wraps(func)
91+
def wrapper(*args, **kwargs):
92+
try:
93+
_hashlib.new(digestname, usedforsecurity=usedforsecurity)
94+
except ValueError:
95+
msg = f"missing OpenSSL hash algorithm: {digestname!r}"
96+
raise unittest.SkipTest(msg)
97+
return func(*args, **kwargs)
98+
return wrapper
99+
100+
def decorator(func_or_class):
101+
return _decorate_func_or_class(func_or_class, decorator_func)
102+
return decorator
103+
104+
105+
def requires_builtin_hashdigest(module, name):
106+
"""Decorator raising SkipTest if a HACL* hashing algorithm is missing.
107+
108+
The 'module' is the module where 'name' should be imported from.
109+
"""
110+
def decorator_func(func):
111+
@functools.wraps(func)
112+
def wrapper(*args, **kwargs):
113+
mod = import_module(module) # may raise SkipTest
114+
try:
115+
getattr(mod, name)()
116+
except (AttributeError, ValueError):
117+
msg = f"missing HACL* hash algorithm: {module}.{name}"
118+
raise unittest.SkipTest(msg)
119+
return func(*args, **kwargs)
120+
return wrapper
121+
122+
def decorator(func_or_class):
123+
return _decorate_func_or_class(func_or_class, decorator_func)
124+
return decorator

Lib/test/test_hmac.py

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

373373
for name in cls.ALGORITHMS:
374374
@property
375-
@hashlib_helper.requires_hashlib()
376-
@hashlib_helper.requires_hashdigest(name, openssl=True)
375+
@hashlib_helper.requires_openssl_hashdigest(name)
377376
def func(self, *, __name=name): # __name needed to bind 'name'
378377
return getattr(_hashlib, f'openssl_{__name}')
379378
setattr(cls, name, func)
@@ -889,7 +888,7 @@ def test_repr(self):
889888
self.assertStartsWith(repr(h), "<hmac.HMAC object at")
890889

891890

892-
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
891+
@hashlib_helper.requires_openssl_hashdigest('sha256')
893892
class OpenSSLSanityTestCase(ThroughOpenSSLAPIMixin, SanityTestCaseMixin,
894893
unittest.TestCase):
895894

@@ -955,8 +954,7 @@ def HMAC(self, key, msg=None):
955954
return self.hmac.HMAC(key, msg, digestmod='sha256')
956955

957956

958-
@hashlib_helper.requires_hashlib()
959-
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
957+
@hashlib_helper.requires_openssl_hashdigest('sha256')
960958
class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase):
961959

962960
def HMAC(self, key, msg=None):
@@ -1055,8 +1053,7 @@ def test_realcopy(self):
10551053
self.assertNotEqual(id(h1._hmac), id(h2._hmac))
10561054

10571055

1058-
@hashlib_helper.requires_hashlib()
1059-
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
1056+
@hashlib_helper.requires_openssl_hashdigest('sha256')
10601057
class OpenSSLCopyTestCase(ExtensionCopyTestCase, unittest.TestCase):
10611058

10621059
def init(self, h):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy