8000 gh-99108: Refactor _sha256 & _sha512 into _sha2. by gpshead · Pull Request #101924 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99108: Refactor _sha256 & _sha512 into _sha2. #101924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Explicitly test that direct type constructors fail.
  • Loading branch information
gpshead authored Feb 15, 2023
commit 40fd92ac15f4cf37217857af1fa1504efab1a198
18 changes: 11 additions & 7 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Test hashlib module
#
# $Id$
# Test the hashlib module.
#
# Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
# Licensed to PSF under a Contributor Agreement.
Expand Down Expand Up @@ -28,7 +26,6 @@
from http.client import HTTPException


# default builtin hash module
default_builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'}
# --with-builtin-hashlib-hashes override
builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
Expand Down Expand Up @@ -66,6 +63,7 @@ def get_fips_mode():
requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2')

# bpo-46913: Don't test the _sha3 extension on a Python UBSAN build
# TODO(gh-99108): Revisit this after _sha3 uses HACL*.
SKIP_SHA3 = support.check_sanitizer(ub=True)
requires_sha3 = unittest.skipUnless(not SKIP_SHA3, 'requires _sha3')

Expand Down Expand Up @@ -107,7 +105,7 @@ class HashLibTestCase(unittest.TestCase):

shakes = {'shake_128', 'shake_256'}

# Issue #14693: fallback modules are always compiled under POSIX
# gh-58898: Fallback modules are always compiled under POSIX.
_warn_on_extension_import = (os.name == 'posix' or support.Py_DEBUG)

def _conditional_import_module(self, module_name):
Expand All @@ -116,7 +114,7 @@ def _conditional_import_module(self, module_name):
return importlib.import_module(module_name)
except ModuleNotFoundError as error:
if self._warn_on_extension_import and module_name in builtin_hashes:
warnings.warn('Did a C extension fail to compile? %s' % error)
warnings.warn(f'Did a C extension fail to compile? {error}')
return None

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -147,7 +145,7 @@ def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs):
_hashlib = self._conditional_import_module('_hashlib')
self._hashlib = _hashlib
if _hashlib:
# These two algorithms should always be present when this module
# These algorithms should always be present when this module
# is compiled. If not, something was compiled wrong.
self.assertTrue(hasattr(_hashlib, 'openssl_md5'))
self.assertTrue(hasattr(_hashlib, 'openssl_sha1'))
Expand Down Expand Up @@ -203,6 +201,12 @@ def hash_constructors(self):
def is_fips_mode(self):
return get_fips_mode()

def test_types_unconstructable(self):
for h in self.hash_constructors:
implementation = type(h(b'', usedforsecurity=False))
with self.assertRaises(TypeError):
implementation()

def test_hash_array(self):
a = array.array("b", range(10))
for cons in self.hash_constructors:
Expand Down
0