Skip to content

Commit f3f71b6

Browse files
committed
strengthen contract on hmac_new_by_name and hmac_digest_by_name
1 parent 4d27d7b commit f3f71b6

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

Lib/test/test_hmac.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def check_hexdigest(self, h, hexdigest, digest_size):
152152
class TestVectorsMixin(CreatorMixin, DigestMixin, CheckerMixin):
153153
"""Mixin class for common tests."""
154154

155-
def hmac_new_by_name(self, key, msg=None, hashname=None):
155+
def hmac_new_by_name(self, key, msg=None, *, hashname):
156156
"""Alternative implementation of hmac_new().
157157
158158
This is typically useful when one needs to test against an HMAC
@@ -165,18 +165,17 @@ def hmac_new_by_name(self, key, msg=None, hashname=None):
165165
as it should only be used in tests that are expected to create
166166
a HMAC object.
167167
"""
168-
self.assertIsInstance(hashname, str | None)
168+
self.assertIsInstance(hashname, str)
169169
return self.hmac_new(key, msg, digestmod=hashname)
170170

171-
def hmac_digest_by_name(self, key, msg=None, hashname=None):
172-
"""Alternative implementation of hmac_digest()."""
173-
self.assertIsInstance(hashname, str | None)
171+
def hmac_digest_by_name(self, key, msg=None, *, hashname):
174172
"""Alternative implementation of hmac_digest().
175173
176174
Unlike hmac_digest(), this method may assert the type of 'hashname'
177175
as it should only be used in tests that are expected to compute a
178176
HMAC digest.
179177
"""
178+
self.assertIsInstance(hashname, str)
180179
return self.hmac_digest(key, msg, digestmod=hashname)
181180

182181
def assert_hmac(
@@ -311,20 +310,15 @@ def assert_hmac_extra_cases(
311310
self.check_object(h, hexdigest, hashname, digest_size, block_size)
312311

313312

314-
class OpenSSLTestVectorsMixin(TestVectorsMixin):
315-
316-
def hmac_new(self, key, msg=None, digestmod=None):
317-
return _hashlib.hmac_new(key, msg, digestmod=digestmod)
318-
319-
def hmac_digest(self, key, msg=None, digestmod=None):
320-
return _hashlib.hmac_digest(key, msg, digest=digestmod)
313+
class OpenSSLTestVectorsMixin(ThroughOpenSSLAPIMixin, TestVectorsMixin):
321314

322-
def hmac_new_by_name(self, key, msg=None, hashname=None):
323-
# ignore 'digestmod' and use the exact openssl function
315+
def hmac_new_by_name(self, key, msg=None, *, hashname):
316+
self.assertIsInstance(hashname, str)
324317
openssl_func = getattr(_hashlib, f"openssl_{hashname}")
325318
return self.hmac_new(key, msg, digestmod=openssl_func)
326319

327-
def hmac_digest_by_name(self, key, msg=None, hashname=None):
320+
def hmac_digest_by_name(self, key, msg=None, *, hashname):
321+
self.assertIsInstance(hashname, str)
328322
openssl_func = getattr(_hashlib, f"openssl_{hashname}")
329323
return self.hmac_digest(key, msg, digestmod=openssl_func)
330324

0 commit comments

Comments
 (0)