Skip to content

Commit 4177afa

Browse files
committed
add regression test for HMAC repr()
1 parent 168d018 commit 4177afa

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Lib/test/test_hmac.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -728,37 +728,50 @@ def test_hmac_digest_digestmod_parameter(self):
728728

729729

730730
class SanityTestCaseMixin(CreatorMixin):
731+
731732
hmac_class = None
733+
digestname = None
732734

733-
@hashlib_helper.requires_hashdigest('sha256')
734735
def test_methods(self):
735-
h = self.hmac_new(b"my secret key", digestmod="sha256")
736-
self.check(h)
737-
738-
def check(self, h):
736+
h = self.hmac_new(b"my secret key", digestmod=self.digestname)
739737
self.assertIsInstance(h, self.hmac_class)
740738
self.assertIsNone(h.update(b"compute the hash of this text!"))
741739
self.assertIsInstance(h.digest(), bytes)
742740
self.assertIsInstance(h.hexdigest(), str)
743741
self.assertIsInstance(h.copy(), self.hmac_class)
744742

743+
def test_repr(self):
744+
raise NotImplementedError
745+
745746

747+
@hashlib_helper.requires_hashdigest('sha256')
746748
class PySanityTestCase(ThroughObjectMixin, PyModuleMixin, SanityTestCaseMixin,
747749
unittest.TestCase):
748750

749751
@classmethod
750752
def setUpClass(cls):
751753
super().setUpClass()
752754
cls.hmac_class = cls.hmac.HMAC
755+
cls.digestname = 'sha256'
756+
757+
def test_repr(self):
758+
h = self.hmac_new(b"my secret key", digestmod=self.digestname)
759+
self.assertStartsWith(repr(h), "<hmac.HMAC object at")
753760

754761

762+
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
755763
class OpenSSLSanityTestCase(ThroughOpenSSLAPIMixin, SanityTestCaseMixin,
756764
unittest.TestCase):
757765

758766
@classmethod
759767
def setUpClass(cls):
760768
super().setUpClass()
761769
cls.hmac_class = _hashlib.HMAC
770+
cls.digestname = 'sha256'
771+
772+
def test_repr(self):
773+
h = self.hmac_new(b"my secret key", digestmod=self.digestname)
774+
self.assertStartsWith(repr(h), f"<{self.digestname} HMAC object @")
762775

763776

764777
class UpdateTestCaseMixin:

0 commit comments

Comments
 (0)