gh-130149: refactor tests for HMAC (#130150)

Since we plan to introduce a built-in implementation for HMAC based on HACL*,
it becomes important for the HMAC tests to be flexible enough to avoid code
duplication.

In addition to the new layout based on mixin classes, we extend test coverage by
also testing the `__repr__` of HMAC objects and the HMAC one-shot functions.

We also fix the import to `_sha256` which, since gh-101924, resulted in some tests being
skipped as the module is no more available (its content was moved to the `_sha2` module).
This commit is contained in:
Bénédikt Tran 2025-03-03 11:22:05 +01:00 committed by GitHub
parent a105f99019
commit 8f11af45de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 809 additions and 408 deletions

View file

@ -8,6 +8,10 @@ except ImportError:
_hashlib = None
def requires_hashlib():
return unittest.skipIf(_hashlib is None, "requires _hashlib")
def requires_hashdigest(digestname, openssl=None, usedforsecurity=True):
"""Decorator raising SkipTest if a hashing algorithm is not available
@ -44,7 +48,7 @@ def requires_hashdigest(digestname, openssl=None, usedforsecurity=True):
hashlib.new(digestname, usedforsecurity=usedforsecurity)
except ValueError:
raise unittest.SkipTest(
f"hash digest '{digestname}' is not available."
f"hash digest {digestname!r} is not available."
)
return func_or_class(*args, **kwargs)
return wrapper