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
refactor CopyTestCase
  • Loading branch information
picnixz committed Feb 13, 2025
commit d2eb48a282108ab1ad6f960133fe35551e0e0ff6
42 changes: 15 additions & 27 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,65 +634,53 @@ def test_with_str_update(self):
h.update("invalid update")


@hashlib_helper.requires_hashdigest('sha256')
class CopyTestCase(unittest.TestCase):

@hashlib_helper.requires_hashdigest('sha256')
def test_attributes_old(self):
# Testing if attributes are of same type.
h1 = hmac.HMAC.__new__(hmac.HMAC)
h1._init_old(b"key", b"msg", digestmod="sha256")
h2 = h1.copy()
self.assertEqual(type(h1._inner), type(h2._inner),
"Types of inner don't match.")
self.assertEqual(type(h1._outer), type(h2._outer),
"Types of outer don't match.")
self.assertEqual(type(h1._inner), type(h2._inner))
self.assertEqual(type(h1._outer), type(h2._outer))

@hashlib_helper.requires_hashdigest('sha256')
def test_realcopy_old(self):
# Testing if the copy method created a real copy.
h1 = hmac.HMAC.__new__(hmac.HMAC)
h1._init_old(b"key", b"msg", digestmod="sha256")
self.assertIsNone(h1._hmac)

h2 = h1.copy()
self.assertIsNone(h2._hmac)
# Using id() in case somebody has overridden __eq__/__ne__.
self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
self.assertTrue(id(h1._inner) != id(h2._inner),
"No real copy of the attribute 'inner'.")
self.assertTrue(id(h1._outer) != id(h2._outer),
"No real copy of the attribute 'outer'.")
self.assertIs(h1._hmac, None)
self.assertNotEqual(id(h1), id(h2))
self.assertNotEqual(id(h1._inner), id(h2._inner))
self.assertNotEqual(id(h1._outer), id(h2._outer))

@hashlib_helper.requires_hashlib()
@hashlib_helper.requires_hashdigest('sha256')
def test_realcopy_hmac(self):
h1 = hmac.HMAC.__new__(hmac.HMAC)
h1._init_hmac(b"key", b"msg", digestmod="sha256")
h2 = h1.copy()
self.assertTrue(id(h1._hmac) != id(h2._hmac))
self.assertNotEqual(id(h1._hmac), id(h2._hmac))

@hashlib_helper.requires_hashdigest('sha256')
def test_equality(self):
# Testing if the copy has the same digests.
h1 = hmac.HMAC(b"key", digestmod="sha256")
h1.update(b"some random text")
h2 = h1.copy()
self.assertEqual(h1.digest(), h2.digest(),
"Digest of copy doesn't match original digest.")
self.assertEqual(h1.hexdigest(), h2.hexdigest(),
"Hexdigest of copy doesn't match original hexdigest.")
self.assertEqual(h1.digest(), h2.digest())
self.assertEqual(h1.hexdigest(), h2.hexdigest())

@hashlib_helper.requires_hashdigest('sha256')
def test_equality_new(self):
# Testing if the copy has the same digests with hmac.new().
h1 = hmac.new(b"key", digestmod="sha256")
h1.update(b"some random text")
h2 = h1.copy()
self.assertTrue(
id(h1) != id(h2), "No real copy of the HMAC instance."
)
self.assertEqual(h1.digest(), h2.digest(),
"Digest of copy doesn't match original digest.")
self.assertEqual(h1.hexdigest(), h2.hexdigest(),
"Hexdigest of copy doesn't match original hexdigest.")
self.assertNotEqual(id(h1), id(h2))
self.assertEqual(h1.digest(), h2.digest())
self.assertEqual(h1.hexdigest(), h2.hexdigest())


class CompareDigestMixin:
Expand Down