@@ -973,37 +973,42 @@ def HMAC(self, key, msg=None):
973973 return self .hmac .new (key , msg , digestmod = 'sha256' )
974974
975975
976+ class CopyBaseTestCase :
977+
978+ def test_attributes (self ):
979+ raise NotImplementedError
980+
981+ def test_realcopy (self ):
982+ raise NotImplementedError
983+
984+
976985@hashlib_helper .requires_hashdigest ('sha256' )
977- class CopyTestCase ( unittest .TestCase ):
986+ class PythonCopyTestCase ( CopyBaseTestCase , unittest .TestCase ):
978987
979- def test_attributes_old (self ):
988+ def test_attributes (self ):
980989 # Testing if attributes are of same type.
981990 h1 = hmac .HMAC .__new__ (hmac .HMAC )
982991 h1 ._init_old (b"key" , b"msg" , digestmod = "sha256" )
992+ self .assertIsNone (h1 ._hmac )
993+ self .assertIsNotNone (h1 ._inner )
994+ self .assertIsNotNone (h1 ._outer )
995+
983996 h2 = h1 .copy ()
997+ self .assertIsNotNone (h2 ._inner )
998+ self .assertIsNotNone (h2 ._outer )
984999 self .assertEqual (type (h1 ._inner ), type (h2 ._inner ))
9851000 self .assertEqual (type (h1 ._outer ), type (h2 ._outer ))
9861001
987- def test_realcopy_old (self ):
1002+ def test_realcopy (self ):
9881003 # Testing if the copy method created a real copy.
9891004 h1 = hmac .HMAC .__new__ (hmac .HMAC )
9901005 h1 ._init_old (b"key" , b"msg" , digestmod = "sha256" )
991- self .assertIsNone (h1 ._hmac )
992-
9931006 h2 = h1 .copy ()
994- self .assertIsNone (h2 ._hmac )
9951007 # Using id() in case somebody has overridden __eq__/__ne__.
9961008 self .assertNotEqual (id (h1 ), id (h2 ))
9971009 self .assertNotEqual (id (h1 ._inner ), id (h2 ._inner ))
9981010 self .assertNotEqual (id (h1 ._outer ), id (h2 ._outer ))
9991011
1000- @hashlib_helper .requires_hashlib ()
1001- def test_realcopy_hmac (self ):
1002- h1 = hmac .HMAC .__new__ (hmac .HMAC )
1003- h1 ._init_hmac (b"key" , b"msg" , digestmod = "sha256" )
1004- h2 = h1 .copy ()
1005- self .assertNotEqual (id (h1 ._hmac ), id (h2 ._hmac ))
1006-
10071012 def test_equality (self ):
10081013 # Testing if the copy has the same digests.
10091014 h1 = hmac .HMAC (b"key" , digestmod = "sha256" )
@@ -1017,11 +1022,57 @@ def test_equality_new(self):
10171022 h1 = hmac .new (b"key" , digestmod = "sha256" )
10181023 h1 .update (b"some random text" )
10191024 h2 = h1 .copy ()
1025+ # Using id() in case somebody has overridden __eq__/__ne__.
10201026 self .assertNotEqual (id (h1 ), id (h2 ))
10211027 self .assertEqual (h1 .digest (), h2 .digest ())
10221028 self .assertEqual (h1 .hexdigest (), h2 .hexdigest ())
10231029
10241030
1031+ class ExtensionCopyTestCase (CopyBaseTestCase ):
1032+
1033+ def init (self , h ):
1034+ """Call the dedicate init() method to test."""
1035+ raise NotImplementedError
1036+
1037+ def test_attributes (self ):
1038+ # Testing if attributes are of same type.
1039+ h1 = hmac .HMAC .__new__ (hmac .HMAC )
1040+
1041+ self .init (h1 )
1042+ self .assertIsNotNone (h1 ._hmac )
1043+ self .assertNotHasAttr (h1 , '_inner' )
1044+ self .assertNotHasAttr (h1 , '_outer' )
1045+
1046+ h2 = h1 .copy ()
1047+ self .assertIsNotNone (h2 ._hmac )
1048+ self .assertNotHasAttr (h2 , '_inner' )
1049+ self .assertNotHasAttr (h2 , '_outer' )
1050+
1051+ def test_realcopy (self ):
1052+ h1 = hmac .HMAC .__new__ (hmac .HMAC )
1053+ self .init (h1 )
1054+ h2 = h1 .copy ()
1055+ # Using id() in case somebody has overridden __eq__/__ne__.
1056+ self .assertNotEqual (id (h1 ._hmac ), id (h2 ._hmac ))
1057+
1058+
1059+ @hashlib_helper .requires_hashlib ()
1060+ @hashlib_helper .requires_hashdigest ('sha256' , openssl = True )
1061+ class OpenSSLCopyTestCase (ExtensionCopyTestCase , unittest .TestCase ):
1062+
1063+ def init (self , h ):
1064+ h ._init_openssl_hmac (b"key" , b"msg" , digestmod = "sha256" )
1065+
1066+
1067+ @hashlib_helper .requires_builtin_hmac ()
1068+ class BuiltinCopyTestCase (ExtensionCopyTestCase , unittest .TestCase ):
1069+
1070+ def init (self , h ):
1071+ # Even if Python does not build '_sha2', the HACL* sources
1072+ # are still built, making it possible to use SHA-2 hashes.
1073+ h ._init_builtin_hmac (b"key" , b"msg" , digestmod = "sha256" )
1074+
1075+
10251076class CompareDigestMixin :
10261077
10271078 @staticmethod
0 commit comments