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
Next Next commit
clean imports and test guards
- Use `_hashlib` instead of `_hashopenssl` naming.
- Use `_hashlib.*` instead of top-level imports.
- Consistently use new hashlib helper guards.
  • Loading branch information
picnixz committed Feb 13, 2025
commit 72f956fd4c5f6db496b4499593794c7a17712586
45 changes: 23 additions & 22 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
import functools
import hmac
import hashlib
import test.support.hashlib_helper as hashlib_helper
import unittest
import unittest.mock
import warnings

from test.support import hashlib_helper, check_disallow_instantiation

from _operator import _compare_digest as operator_compare_digest
from test.support import check_disallow_instantiation
from test.support.import_helper import import_fresh_module

try:
import _hashlib as _hashopenssl
from _hashlib import HMAC as C_HMAC
from _hashlib import hmac_new as c_hmac_new
import _hashlib
from _hashlib import compare_digest as openssl_compare_digest
except ImportError:
_hashopenssl = None
C_HMAC = None
c_hmac_new = None
_hashlib = None
openssl_compare_digest = None

try:
import _sha256 as sha256_module
import _sha2 as sha2
except ImportError:
sha256_module = None
sha2 = None


def requires_builtin_sha2():
return unittest.skipIf(sha2 is None, "requires _sha2")


class TestVectorsTestCase(unittest.TestCase):
Expand Down Expand Up @@ -88,6 +88,7 @@ def assert_hmac(
h, digest, hashname, digest_size, block_size
)

c_hmac_new = getattr(_hashlib, 'hmac_new', None)
if c_hmac_new is not None:
h = c_hmac_new(key, data, digestmod=hashname)
self.assert_hmac_internals(
Expand All @@ -100,7 +101,7 @@ def assert_hmac(
h.update(data)
self.assertEqual(h.hexdigest().upper(), digest.upper())

func = getattr(_hashopenssl, f"openssl_{hashname}")
func = getattr(_hashlib, f"openssl_{hashname}")
h = c_hmac_new(key, data, digestmod=func)
self.assert_hmac_internals(
h, digest, hashname, digest_size, block_size
Expand Down Expand Up @@ -435,20 +436,20 @@ def test_withmodule(self):
except Exception:
self.fail("Constructor call with hashlib.sha256 raised exception.")

@unittest.skipUnless(C_HMAC is not None, 'need _hashlib')
@hashlib_helper.requires_hashlib()
def test_internal_types(self):
# internal types like _hashlib.C_HMAC are not constructable
check_disallow_instantiation(self, C_HMAC)
# internal C types like are not constructable
check_disallow_instantiation(self, _hashlib.HMAC)
with self.assertRaisesRegex(TypeError, "immutable type"):
C_HMAC.value = None
_hashlib.HMAC.value = None

@unittest.skipUnless(sha256_module is not None, 'need _sha256')
def test_with_sha256_module(self):
h = hmac.HMAC(b"key", b"hash this!", digestmod=sha256_module.sha256)
@requires_builtin_sha2()
def test_with_sha2(self):
h = hmac.HMAC(b"key", b"hash this!", digestmod=sha2.sha256)
self.assertEqual(h.hexdigest(), self.expected)
self.assertEqual(h.name, "hmac-sha256")

digest = hmac.digest(b"key", b"hash this!", sha256_module.sha256)
digest = hmac.digest(b"key", b"hash this!", sha2.sha256)
self.assertEqual(digest, binascii.unhexlify(self.expected))


Expand Down Expand Up @@ -503,7 +504,7 @@ def test_realcopy_old(self):
"No real copy of the attribute 'outer'.")
self.assertIs(h1._hmac, None)

@unittest.skipIf(_hashopenssl is None, "test requires _hashopenssl")
@hashlib_helper.requires_hashlib()
@hashlib_helper.requires_hashdigest('sha256')
def test_realcopy_hmac(self):
h1 = hmac.HMAC.__new__(hmac.HMAC)
Expand Down Expand Up @@ -549,7 +550,7 @@ def test_hmac_compare_digest(self):
def test_operator_compare_digest(self):
self._test_compare_digest(operator_compare_digest)

@unittest.skipIf(openssl_compare_digest is None, "test requires _hashlib")
@hashlib_helper.requires_hashlib()
def test_openssl_compare_digest(self):
self._test_compare_digest(openssl_compare_digest)

Expand Down