gh-132388: Increase test coverage for HMAC (#132389)

- Correctly test missing `digestmod` and `digest` parameters.
- Test when chunks of length > 2048 are passed to `update()`.
- Test one-shot HMAC-BLAKE2.
This commit is contained in:
Bénédikt Tran 2025-04-12 19:43:11 +02:00 committed by GitHub
parent 842ab81517
commit 9634085af3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 156 additions and 53 deletions

View file

@ -81,13 +81,13 @@ class HMAC:
try:
self._init_openssl_hmac(key, msg, digestmod)
return
except _hashopenssl.UnsupportedDigestmodError:
except _hashopenssl.UnsupportedDigestmodError: # pragma: no cover
pass
if _hmac and isinstance(digestmod, str):
try:
self._init_builtin_hmac(key, msg, digestmod)
return
except _hmac.UnknownHashError:
except _hmac.UnknownHashError: # pragma: no cover
pass
self._init_old(key, msg, digestmod)
@ -121,12 +121,12 @@ class HMAC:
warnings.warn(f"block_size of {blocksize} seems too small; "
f"using our default of {self.blocksize}.",
RuntimeWarning, 2)
blocksize = self.blocksize
blocksize = self.blocksize # pragma: no cover
else:
warnings.warn("No block_size attribute on given digest object; "
f"Assuming {self.blocksize}.",
RuntimeWarning, 2)
blocksize = self.blocksize
blocksize = self.blocksize # pragma: no cover
if len(key) > blocksize:
key = digest_cons(key).digest()