Block a buitin-in hash name from the hashlib.new() interface.
(name)
| 901 | |
| 902 | |
| 903 | def _block_builtin_hash_new(name): |
| 904 | """Block a buitin-in hash name from the hashlib.new() interface.""" |
| 905 | assert isinstance(name, str), name |
| 906 | assert name.lower() == name, f"invalid name: {name}" |
| 907 | assert name in _HashId, f"invalid hash: {name}" |
| 908 | |
| 909 | # Re-import 'hashlib' in case it was mocked |
| 910 | hashlib = importlib.import_module('hashlib') |
| 911 | builtin_constructor_cache = getattr(hashlib, '__builtin_constructor_cache') |
| 912 | builtin_constructor_cache_mock = builtin_constructor_cache.copy() |
| 913 | builtin_constructor_cache_mock.pop(name, None) |
| 914 | builtin_constructor_cache_mock.pop(name.upper(), None) |
| 915 | |
| 916 | # __get_builtin_constructor() imports the HACL* modules on demand, |
| 917 | # so we need to block the possibility of importing it, but only |
| 918 | # during the call to __get_builtin_constructor(). |
| 919 | get_builtin_constructor = getattr(hashlib, '__get_builtin_constructor') |
| 920 | builtin_module_name = get_hash_func_info(name).builtin.module_name |
| 921 | |
| 922 | @functools.wraps(get_builtin_constructor) |
| 923 | def get_builtin_constructor_mock(name): |
| 924 | with import_helper.isolated_modules(): |
| 925 | sys = importlib.import_module("sys") |
| 926 | sys.modules[builtin_module_name] = None # block module's import |
| 927 | return get_builtin_constructor(name) |
| 928 | |
| 929 | return unittest.mock.patch.multiple( |
| 930 | hashlib, |
| 931 | __get_builtin_constructor=get_builtin_constructor_mock, |
| 932 | __builtin_constructor_cache=builtin_constructor_cache_mock, |
| 933 | ) |
| 934 | |
| 935 | |
| 936 | def _block_builtin_hmac_new(blocked_name): |
no test coverage detected
searching dependent graphs…