Migrate requires_hashdigest() to helper

This commit is contained in:
Padraic Fanning
2022-02-06 16:53:09 -05:00
parent 2740675228
commit 576b08a124
3 changed files with 5 additions and 32 deletions

View File

@@ -88,7 +88,7 @@ __all__ = [
"create_empty_file", "can_symlink", "fs_is_case_insensitive",
# unittest
"is_resource_enabled", "requires", "requires_freebsd_version",
"requires_linux_version", "requires_mac_ver", "requires_hashdigest",
"requires_linux_version", "requires_mac_ver",
"check_syntax_error", "check_syntax_warning",
"TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset",
"transient_internet", "BasicTestRunner", "run_unittest", "run_doctest",
@@ -588,35 +588,6 @@ def skip_if_buildbot(reason=None):
isbuildbot = os.environ.get('USER') == 'buildbot'
return unittest.skipIf(isbuildbot, reason)
def requires_hashdigest(digestname, openssl=None):
"""Decorator raising SkipTest if a hashing algorithm is not available
The hashing algorithm could be missing or blocked by a strict crypto
policy.
If 'openssl' is True, then the decorator checks that OpenSSL provides
the algorithm. Otherwise the check falls back to built-in
implementations.
ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS
ValueError: unsupported hash type md4
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
if openssl and _hashlib is not None:
_hashlib.new(digestname)
else:
hashlib.new(digestname)
except ValueError:
raise unittest.SkipTest(
f"hash digest '{digestname}' is not available."
)
return func(*args, **kwargs)
return wrapper
return decorator
HOST = "localhost"
HOSTv4 = "127.0.0.1"

View File

@@ -11,7 +11,8 @@ import unittest.mock
import tarfile
from test import support
from test.support import script_helper, requires_hashdigest, os_helper
from test.support import os_helper
from test.support import script_helper
# Check for our compression modules.
try:

View File

@@ -9,6 +9,7 @@ import unittest
import hashlib
from test import support
from test.support import hashlib_helper
try:
import ssl
@@ -315,6 +316,7 @@ class BasicAuthTests(unittest.TestCase):
self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, self.server_url)
@hashlib_helper.requires_hashdigest("md5", openssl=True)
class ProxyAuthTests(unittest.TestCase):
URL = "http://localhost"
@@ -322,7 +324,6 @@ class ProxyAuthTests(unittest.TestCase):
PASSWD = "test123"
REALM = "TestRealm"
@support.requires_hashdigest("md5")
def setUp(self):
super(ProxyAuthTests, self).setUp()
# Ignore proxy bypass settings in the environment.