Skip to content

Commit da3a3e3

Browse files
committed
test one-shot HMAC functions
1 parent aab2b0b commit da3a3e3

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

Lib/test/test_hmac.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hashlib
55
import random
66
import test.support.hashlib_helper as hashlib_helper
7+
import types
78
import unittest
89
import unittest.mock
910
import warnings
@@ -215,6 +216,10 @@ def assert_hmac(
215216
self.assert_hmac_hexdigest(
216217
key, msg, hexdigest, digestmod, digest_size
217218
)
219+
self.assert_hmac_common_cases(
220+
key, msg, hexdigest, digestmod,
221+
hashname, digest_size, block_size
222+
)
218223
self.assert_hmac_extra_cases(
219224
key, msg, hexdigest, digestmod,
220225
hashname, digest_size, block_size
@@ -234,7 +239,7 @@ def assert_hmac_new(
234239
235240
This test uses the `hmac_new()` method to create HMAC objects.
236241
"""
237-
self._check_hmac_new(
242+
self.check_hmac_new(
238243
key, msg, hexdigest, hashname, digest_size, block_size,
239244
hmac_new_func=self.hmac_new,
240245
hmac_new_kwds={'digestmod': digestmod},
@@ -247,15 +252,15 @@ def assert_hmac_new_by_name(
247252
248253
This test uses the `hmac_new_by_name()` method to create HMAC objects.
249254
"""
250-
self._check_hmac_new(
255+
self.check_hmac_new(
251256
key, msg, hexdigest, hashname, digest_size, block_size,
252257
hmac_new_func=self.hmac_new_by_name,
253258
hmac_new_kwds={'hashname': hashname},
254259
)
255260

256-
def _check_hmac_new(
261+
def check_hmac_new(
257262
self, key, msg, hexdigest, hashname, digest_size, block_size,
258-
hmac_new_func, hmac_new_kwds,
263+
hmac_new_func, hmac_new_kwds=types.MappingProxyType({}),
259264
):
260265
"""Check that HMAC(key, msg) == digest.
261266
@@ -282,7 +287,7 @@ def assert_hmac_hexdigest(
282287
self, key, msg, hexdigest, digestmod, digest_size,
283288
):
284289
"""Check a HMAC digest computed by hmac_digest()."""
285-
self._check_hmac_hexdigest(
290+
self.check_hmac_hexdigest(
286291
key, msg, hexdigest, digest_size,
287292
hmac_digest_func=self.hmac_digest,
288293
hmac_digest_kwds={'digestmod': digestmod},
@@ -293,40 +298,50 @@ def assert_hmac_hexdigest_by_name(
293298
):
294299
"""Check a HMAC digest computed by hmac_digest_by_name()."""
295300
self.assertIsInstance(hashname, str)
296-
self._check_hmac_hexdigest(
301+
self.check_hmac_hexdigest(
297302
key, msg, hexdigest, digest_size,
298303
hmac_digest_func=self.hmac_digest_by_name,
299304
hmac_digest_kwds={'hashname': hashname},
300305
)
301306

302-
def _check_hmac_hexdigest(
307+
def check_hmac_hexdigest(
303308
self, key, msg, hexdigest, digest_size,
304-
hmac_digest_func, hmac_digest_kwds,
309+
hmac_digest_func, hmac_digest_kwds=types.MappingProxyType({}),
305310
):
311+
"""Check and return a HMAC digest computed by hmac_digest_func().
312+
313+
This HMAC digest is computed by:
314+
315+
hmac_digest_func(key, msg, **hmac_digest_kwds)
316+
317+
This is typically useful for checking one-shot HMAC functions.
318+
"""
306319
d = hmac_digest_func(key, msg, **hmac_digest_kwds)
307320
self.assertEqual(len(d), digest_size)
308321
self.assertEqual(d, binascii.unhexlify(hexdigest))
322+
return d
309323

310-
def assert_hmac_extra_cases(
324+
def assert_hmac_common_cases(
311325
self, key, msg, hexdigest, digestmod, hashname, digest_size, block_size
312326
):
313-
"""Extra tests that can be added in subclasses."""
327+
"""Extra common tests executed by all subclasses."""
314328
h1 = self.hmac_new_by_name(key, hashname=hashname)
315329
h2 = h1.copy()
316330
h2.update(b"test update should not affect original")
317331
h1.update(msg)
318332
self.check_object(h1, hexdigest, hashname, digest_size, block_size)
319333

334+
def assert_hmac_extra_cases(
335+
self, key, msg, hexdigest, digestmod, hashname, digest_size, block_size
336+
):
337+
"""Extra tests that can be added in subclasses."""
338+
320339

321340
class PyAssertersMixin(PyModuleMixin, AssertersMixin):
322341

323342
def assert_hmac_extra_cases(
324343
self, key, msg, hexdigest, digestmod, hashname, digest_size, block_size
325344
):
326-
super().assert_hmac_extra_cases(
327-
key, msg, hexdigest, digestmod, hashname, digest_size, block_size
328-
)
329-
330345
h = self.hmac.HMAC.__new__(self.hmac.HMAC)
331346
h._init_old(key, msg, digestmod=digestmod)
332347
self.check_object(h, hexdigest, hashname, digest_size, block_size)
@@ -741,6 +756,15 @@ class BuiltinRFCTestCase(BuiltinAssertersMixin,
741756
The underlying hash functions are also HACL*-based.
742757
"""
743758

759+
def assert_hmac_extra_cases(
760+
self, key, msg, hexdigest, digestmod, hashname, digest_size, block_size
761+
):
762+
# assert one-shot HMAC at the same time
763+
with self.subTest(key=key, msg=msg, hashname=hashname):
764+
func = eval(f'self.hmac.compute_{hashname}')
765+
self.assertTrue(callable(func))
766+
self.check_hmac_hexdigest(key, msg, hexdigest, digest_size, func)
767+
744768

745769
# TODO(picnixz): once we have a HACL* HMAC, we should also test the Python
746770
# implementation of HMAC with a HACL*-based hash function. For now, we only

0 commit comments

Comments
 (0)