Skip to content

Commit d2eb48a

Browse files
committed
refactor CopyTestCase
1 parent acda122 commit d2eb48a

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

Lib/test/test_hmac.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -634,65 +634,53 @@ def test_with_str_update(self):
634634
h.update("invalid update")
635635

636636

637+
@hashlib_helper.requires_hashdigest('sha256')
637638
class CopyTestCase(unittest.TestCase):
638639

639-
@hashlib_helper.requires_hashdigest('sha256')
640640
def test_attributes_old(self):
641641
# Testing if attributes are of same type.
642642
h1 = hmac.HMAC.__new__(hmac.HMAC)
643643
h1._init_old(b"key", b"msg", digestmod="sha256")
644644
h2 = h1.copy()
645-
self.assertEqual(type(h1._inner), type(h2._inner),
646-
"Types of inner don't match.")
647-
self.assertEqual(type(h1._outer), type(h2._outer),
648-
"Types of outer don't match.")
645+
self.assertEqual(type(h1._inner), type(h2._inner))
646+
self.assertEqual(type(h1._outer), type(h2._outer))
649647

650-
@hashlib_helper.requires_hashdigest('sha256')
651648
def test_realcopy_old(self):
652649
# Testing if the copy method created a real copy.
653650
h1 = hmac.HMAC.__new__(hmac.HMAC)
654651
h1._init_old(b"key", b"msg", digestmod="sha256")
652+
self.assertIsNone(h1._hmac)
653+
655654
h2 = h1.copy()
655+
self.assertIsNone(h2._hmac)
656656
# Using id() in case somebody has overridden __eq__/__ne__.
657-
self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
658-
self.assertTrue(id(h1._inner) != id(h2._inner),
659-
"No real copy of the attribute 'inner'.")
660-
self.assertTrue(id(h1._outer) != id(h2._outer),
661-
"No real copy of the attribute 'outer'.")
662-
self.assertIs(h1._hmac, None)
657+
self.assertNotEqual(id(h1), id(h2))
658+
self.assertNotEqual(id(h1._inner), id(h2._inner))
659+
self.assertNotEqual(id(h1._outer), id(h2._outer))
663660

664661
@hashlib_helper.requires_hashlib()
665-
@hashlib_helper.requires_hashdigest('sha256')
666662
def test_realcopy_hmac(self):
667663
h1 = hmac.HMAC.__new__(hmac.HMAC)
668664
h1._init_hmac(b"key", b"msg", digestmod="sha256")
669665
h2 = h1.copy()
670-
self.assertTrue(id(h1._hmac) != id(h2._hmac))
666+
self.assertNotEqual(id(h1._hmac), id(h2._hmac))
671667

672-
@hashlib_helper.requires_hashdigest('sha256')
673668
def test_equality(self):
674669
# Testing if the copy has the same digests.
675670
h1 = hmac.HMAC(b"key", digestmod="sha256")
676671
h1.update(b"some random text")
677672
h2 = h1.copy()
678-
self.assertEqual(h1.digest(), h2.digest(),
679-
"Digest of copy doesn't match original digest.")
680-
self.assertEqual(h1.hexdigest(), h2.hexdigest(),
681-
"Hexdigest of copy doesn't match original hexdigest.")
673+
self.assertEqual(h1.digest(), h2.digest())
674+
self.assertEqual(h1.hexdigest(), h2.hexdigest())
682675

683-
@hashlib_helper.requires_hashdigest('sha256')
684676
def test_equality_new(self):
685677
# Testing if the copy has the same digests with hmac.new().
686678
h1 = hmac.new(b"key", digestmod="sha256")
687679
h1.update(b"some random text")
688680
h2 = h1.copy()
689-
self.assertTrue(
690-
id(h1) != id(h2), "No real copy of the HMAC instance."
691-
)
692-
self.assertEqual(h1.digest(), h2.digest(),
693-
"Digest of copy doesn't match original digest.")
694-
self.assertEqual(h1.hexdigest(), h2.hexdigest(),
695-
"Hexdigest of copy doesn't match original hexdigest.")
681+
self.assertNotEqual(id(h1), id(h2))
682+
self.assertEqual(h1.digest(), h2.digest())
683+
self.assertEqual(h1.hexdigest(), h2.hexdigest())
696684

697685

698686
class CompareDigestMixin:

0 commit comments

Comments
 (0)