Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d80b1b1
add helper for _hashlib detection
picnixz Feb 13, 2025
73b7756
remove unused function `ignore_warning`
picnixz Feb 13, 2025
fe7a300
cleanup whitespaces
picnixz Feb 13, 2025
72f956f
clean imports and test guards
picnixz Feb 13, 2025
3e827a7
Add mixins for interacting with HMAC
picnixz Feb 13, 2025
7f4fcc8
add mixin for RFC test cases
picnixz Feb 13, 2025
e47dd8b
use RFC mixin classes
picnixz Feb 13, 2025
acda122
refactor `CompareDigestTestCase`
picnixz Feb 13, 2025
d2eb48a
refactor `CopyTestCase`
picnixz Feb 13, 2025
105f6ce
refactor `UpdateTestCase`
picnixz Feb 13, 2025
412f5c2
refactor `SanityTestCase`
picnixz Feb 13, 2025
c682945
refactor `ConstructorTestCase`
picnixz Feb 13, 2025
7e62a58
Merge branch 'main' into test/hmac/refactor-99108
picnixz Feb 15, 2025
c04233d
update comments
picnixz Feb 15, 2025
fb7e577
extend coverage
picnixz Feb 15, 2025
168d018
Merge branch 'main' into test/hmac/refactor-99108
picnixz Feb 24, 2025
4177afa
add regression test for HMAC `repr()`
picnixz Feb 25, 2025
644f577
change a bit the digestmod tests
picnixz Feb 25, 2025
e71f03d
Merge branch 'main' into test/hmac/refactor-99108
picnixz Mar 2, 2025
dbe3ce4
remove mixin that is not yet needed
picnixz Mar 3, 2025
ca0822a
add comments
picnixz Mar 3, 2025
a0b3569
semantics: 'digest' -> 'hexdigest'
picnixz Mar 3, 2025
321a7a9
use `__init_subclass__` instead of `setUpClass`
picnixz Mar 3, 2025
ca7851c
document and reorganize method order in `DigestModTestCaseMixin`
picnixz Mar 3, 2025
a347ee5
use separate factories for creating bad digestmod cases
picnixz Mar 3, 2025
85364b4
minor tweak on `CompareDigestMixin`
picnixz Mar 3, 2025
740fe81
do not allow 'hashfunc' or 'hashname' to be None for RFC tests
picnixz Mar 3, 2025
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
do not allow 'hashfunc' or 'hashname' to be None for RFC tests
I previously allowed it to be None for future compatibility, but
I think it's better to construct a fake hash function instead, or
  • Loading branch information
picnixz committed Mar 3, 2025
commit 740fe8183156abb34473548e55c21ece763edb0b
20 changes: 12 additions & 8 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def hmac_new_by_name(self, key, msg=None, hashname=None):
implementation which only recognizes underlying hash functions
by their name (all HMAC implementations must at least recognize
hash functions by their names but some may use aliases such as
`hashlib.sha1` instead of "sha1".
`hashlib.sha1` instead of "sha1").
"""
self.assertIsInstance(hashname, str | None)
return self.hmac_new(key, msg, digestmod=hashname)
Expand All @@ -169,13 +169,17 @@ def assert_hmac(
The 'hashfunc' and 'hashname' are used as 'digestmod' values,
thereby allowing to test the underlying dispatching mechanism.

At most one of 'hashfunc' or 'hashname' value can be None, in which
case it is ignored.
Note that 'hashfunc' may be a string, a callable, or a PEP-257
module. Note that not all HMAC implementations may recognize the
same set of types for 'hashfunc', but they should always accept
a hash function by its name.
"""
digestmods = list(filter(None, [hashfunc, hashname]))
self.assertNotEqual(digestmods, [],
"at least one implementation must be tested")
for digestmod in digestmods:
if hashfunc == hashname:
choices = [hashname]
else:
choices = [hashfunc, hashname]

for digestmod in choices:
with self.subTest(digestmod=digestmod):
self.assert_hmac_new(
key, msg, hexdigest, digestmod,
Expand Down Expand Up @@ -268,7 +272,7 @@ def assert_hmac_extra_cases(
self, key, msg, hexdigest, digestmod, hashname, digest_size, block_size
):
"""Extra tests that can be added in subclasses."""
h1 = self.hmac_new(key, digestmod=digestmod)
h1 = self.hmac_new_by_name(key, hashname=hashname)
h2 = h1.copy()
h2.update(b"test update should not affect original")
h1.update(msg)
Expand Down