| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | import functools | |
| 2 | 2 | import hashlib | |
| 3 | 3 | import unittest | |
| 4 | + from test.support.import_helper import import_module | ||
| 4 | 5 | ||
| 5 | 6 | try: | |
| 6 | 7 | import _hashlib | |
@@ -77,3 +78,47 @@ def wrapper(*args, **kwargs): | |||
| 77 | 78 | def decorator(func_or_class): | |
| 78 | 79 | return _decorate_func_or_class(func_or_class, decorator_func) | |
| 79 | 80 | 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -372,8 +372,7 @@ def setUpClass(cls): | |||
| 372 | 372 | ||
| 373 | 373 | for name in cls.ALGORITHMS: | |
| 374 | 374 | @property | |
| 375 | - @hashlib_helper.requires_hashlib() | ||
| 376 | - @hashlib_helper.requires_hashdigest(name, openssl=True) | ||
| 375 | + @hashlib_helper.requires_openssl_hashdigest(name) | ||
| 377 | 376 | def func(self, *, __name=name): # __name needed to bind 'name' | |
| 378 | 377 | return getattr(_hashlib, f'openssl_{__name}') | |
| 379 | 378 | setattr(cls, name, func) | |
@@ -889,7 +888,7 @@ def test_repr(self): | |||
| 889 | 888 | self.assertStartsWith(repr(h), "<hmac.HMAC object at") | |
| 890 | 889 | ||
| 891 | 890 | ||
| 892 | - @hashlib_helper.requires_hashdigest('sha256', openssl=True) | ||
| 891 | + @hashlib_helper.requires_openssl_hashdigest('sha256') | ||
| 893 | 892 | class OpenSSLSanityTestCase(ThroughOpenSSLAPIMixin, SanityTestCaseMixin, | |
| 894 | 893 | unittest.TestCase): | |
| 895 | 894 | ||
@@ -955,8 +954,7 @@ def HMAC(self, key, msg=None): | |||
| 955 | 954 | return self.hmac.HMAC(key, msg, digestmod='sha256') | |
| 956 | 955 | ||
| 957 | 956 | ||
| 958 | - @hashlib_helper.requires_hashlib() | ||
| 959 | - @hashlib_helper.requires_hashdigest('sha256', openssl=True) | ||
| 957 | + @hashlib_helper.requires_openssl_hashdigest('sha256') | ||
| 960 | 958 | class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase): | |
| 961 | 959 | ||
| 962 | 960 | def HMAC(self, key, msg=None): | |
@@ -1055,8 +1053,7 @@ def test_realcopy(self): | |||
| 1055 | 1053 | self.assertNotEqual(id(h1._hmac), id(h2._hmac)) | |
| 1056 | 1054 | ||
| 1057 | 1055 | ||
| 1058 | - @hashlib_helper.requires_hashlib() | ||
| 1059 | - @hashlib_helper.requires_hashdigest('sha256', openssl=True) | ||
| 1056 | + @hashlib_helper.requires_openssl_hashdigest('sha256') | ||
| 1060 | 1057 | class OpenSSLCopyTestCase(ExtensionCopyTestCase, unittest.TestCase): | |
| 1061 | 1058 | ||
| 1062 | 1059 | def init(self, h): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments