Skip to content

Commit bc48eac

Browse files
committed
split hmac.copy() test by implementation
1 parent 7f61b1b commit bc48eac

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

Lib/test/test_hmac.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -907,37 +907,42 @@ def HMAC(self, key, msg=None):
907907
return hmac.new(key, msg, digestmod='sha256')
908908

909909

910+
class CopyBaseTestCase:
911+
912+
def test_attributes(self):
913+
raise NotImplementedError
914+
915+
def test_realcopy(self):
916+
raise NotImplementedError
917+
918+
910919
@hashlib_helper.requires_hashdigest('sha256')
911-
class CopyTestCase(unittest.TestCase):
920+
class PythonCopyTestCase(CopyBaseTestCase, unittest.TestCase):
912921

913-
def test_attributes_old(self):
922+
def test_attributes(self):
914923
# Testing if attributes are of same type.
915924
h1 = hmac.HMAC.__new__(hmac.HMAC)
916925
h1._init_old(b"key", b"msg", digestmod="sha256")
926+
self.assertIsNone(h1._hmac)
927+
self.assertIsNotNone(h1._inner)
928+
self.assertIsNotNone(h1._outer)
929+
917930
h2 = h1.copy()
931+
self.assertIsNotNone(h2._inner)
932+
self.assertIsNotNone(h2._outer)
918933
self.assertEqual(type(h1._inner), type(h2._inner))
919934
self.assertEqual(type(h1._outer), type(h2._outer))
920935

921-
def test_realcopy_old(self):
936+
def test_realcopy(self):
922937
# Testing if the copy method created a real copy.
923938
h1 = hmac.HMAC.__new__(hmac.HMAC)
924939
h1._init_old(b"key", b"msg", digestmod="sha256")
925-
self.assertIsNone(h1._hmac)
926-
927940
h2 = h1.copy()
928-
self.assertIsNone(h2._hmac)
929941
# Using id() in case somebody has overridden __eq__/__ne__.
930942
self.assertNotEqual(id(h1), id(h2))
931943
self.assertNotEqual(id(h1._inner), id(h2._inner))
932944
self.assertNotEqual(id(h1._outer), id(h2._outer))
933945

934-
@hashlib_helper.requires_hashlib()
935-
def test_realcopy_hmac(self):
936-
h1 = hmac.HMAC.__new__(hmac.HMAC)
937-
h1._init_hmac(b"key", b"msg", digestmod="sha256")
938-
h2 = h1.copy()
939-
self.assertNotEqual(id(h1._hmac), id(h2._hmac))
940-
941946
def test_equality(self):
942947
# Testing if the copy has the same digests.
943948
h1 = hmac.HMAC(b"key", digestmod="sha256")
@@ -951,11 +956,48 @@ def test_equality_new(self):
951956
h1 = hmac.new(b"key", digestmod="sha256")
952957
h1.update(b"some random text")
953958
h2 = h1.copy()
959+
# Using id() in case somebody has overridden __eq__/__ne__.
954960
self.assertNotEqual(id(h1), id(h2))
955961
self.assertEqual(h1.digest(), h2.digest())
956962
self.assertEqual(h1.hexdigest(), h2.hexdigest())
957963

958964

965+
class ExtensionCopyTestCase(CopyBaseTestCase):
966+
967+
def init(self, h):
968+
"""Call the dedicate init() method to test."""
969+
raise NotImplementedError
970+
971+
def test_attributes(self):
972+
# Testing if attributes are of same type.
973+
h1 = hmac.HMAC.__new__(hmac.HMAC)
974+
975+
self.init(h1)
976+
self.assertIsNotNone(h1._hmac)
977+
self.assertNotHasAttr(h1, '_inner')
978+
self.assertNotHasAttr(h1, '_outer')
979+
980+
h2 = h1.copy()
981+
self.assertIsNotNone(h2._hmac)
982+
self.assertNotHasAttr(h2, '_inner')
983+
self.assertNotHasAttr(h2, '_outer')
984+
985+
def test_realcopy(self):
986+
h1 = hmac.HMAC.__new__(hmac.HMAC)
987+
self.init(h1)
988+
h2 = h1.copy()
989+
# Using id() in case somebody has overridden __eq__/__ne__.
990+
self.assertNotEqual(id(h1._hmac), id(h2._hmac))
991+
992+
993+
@hashlib_helper.requires_hashlib()
994+
@hashlib_helper.requires_hashdigest('sha256', openssl=True)
995+
class OpenSSLCopyTestCase(ExtensionCopyTestCase, unittest.TestCase):
996+
997+
def init(self, h):
998+
h._init_openssl_hmac(b"key", b"msg", digestmod="sha256")
999+
1000+
9591001
class CompareDigestMixin:
9601002

9611003
@staticmethod

0 commit comments

Comments
 (0)