Skip to content

Commit 740fe81

Browse files
committed
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
1 parent 85364b4 commit 740fe81

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Lib/test/test_hmac.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def hmac_new_by_name(self, key, msg=None, hashname=None):
151151
implementation which only recognizes underlying hash functions
152152
by their name (all HMAC implementations must at least recognize
153153
hash functions by their names but some may use aliases such as
154-
`hashlib.sha1` instead of "sha1".
154+
`hashlib.sha1` instead of "sha1").
155155
"""
156156
self.assertIsInstance(hashname, str | None)
157157
return self.hmac_new(key, msg, digestmod=hashname)
@@ -169,13 +169,17 @@ def assert_hmac(
169169
The 'hashfunc' and 'hashname' are used as 'digestmod' values,
170170
thereby allowing to test the underlying dispatching mechanism.
171171
172-
At most one of 'hashfunc' or 'hashname' value can be None, in which
173-
case it is ignored.
172+
Note that 'hashfunc' may be a string, a callable, or a PEP-257
173+
module. Note that not all HMAC implementations may recognize the
174+
same set of types for 'hashfunc', but they should always accept
175+
a hash function by its name.
174176
"""
175-
digestmods = list(filter(None, [hashfunc, hashname]))
176-
self.assertNotEqual(digestmods, [],
177-
"at least one implementation must be tested")
178-
for digestmod in digestmods:
177+
if hashfunc == hashname:
178+
choices = [hashname]
179+
else:
180+
choices = [hashfunc, hashname]
181+
182+
for digestmod in choices:
179183
with self.subTest(digestmod=digestmod):
180184
self.assert_hmac_new(
181185
key, msg, hexdigest, digestmod,
@@ -268,7 +272,7 @@ def assert_hmac_extra_cases(
268272
self, key, msg, hexdigest, digestmod, hashname, digest_size, block_size
269273
):
270274
"""Extra tests that can be added in subclasses."""
271-
h1 = self.hmac_new(key, digestmod=digestmod)
275+
h1 = self.hmac_new_by_name(key, hashname=hashname)
272276
h2 = h1.copy()
273277
h2.update(b"test update should not affect original")
274278
h1.update(msg)

0 commit comments

Comments
 (0)