@@ -973,37 +973,42 @@ def HMAC(self, key, msg=None):
973
973
return self .hmac .new (key , msg , digestmod = 'sha256' )
974
974
975
975
976
+ class CopyBaseTestCase :
977
+
978
+ def test_attributes (self ):
979
+ raise NotImplementedError
980
+
981
+ def test_realcopy (self ):
982
+ raise NotImplementedError
983
+
984
+
976
985
@hashlib_helper .requires_hashdigest ('sha256' )
977
- class CopyTestCase ( unittest .TestCase ):
986
+ class PythonCopyTestCase ( CopyBaseTestCase , unittest .TestCase ):
978
987
979
- def test_attributes_old (self ):
988
+ def test_attributes (self ):
980
989
# Testing if attributes are of same type.
981
990
h1 = hmac .HMAC .__new__ (hmac .HMAC )
982
991
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
+
983
996
h2 = h1 .copy ()
997
+ self .assertIsNotNone (h2 ._inner )
998
+ self .assertIsNotNone (h2 ._outer )
984
999
self .assertEqual (type (h1 ._inner ), type (h2 ._inner ))
985
1000
self .assertEqual (type (h1 ._outer ), type (h2 ._outer ))
986
1001
987
- def test_realcopy_old (self ):
1002
+ def test_realcopy (self ):
988
1003
# Testing if the copy method created a real copy.
989
1004
h1 = hmac .HMAC .__new__ (hmac .HMAC )
990
1005
h1 ._init_old (b"key" , b"msg" , digestmod = "sha256" )
991
- self .assertIsNone (h1 ._hmac )
992
-
993
1006
h2 = h1 .copy ()
994
- self .assertIsNone (h2 ._hmac )
995
1007
# Using id() in case somebody has overridden __eq__/__ne__.
996
1008
self .assertNotEqual (id (h1 ), id (h2 ))
997
1009
self .assertNotEqual (id (h1 ._inner ), id (h2 ._inner ))
998
1010
self .assertNotEqual (id (h1 ._outer ), id (h2 ._outer ))
999
1011
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
-
1007
1012
def test_equality (self ):
1008
1013
# Testing if the copy has the same digests.
1009
1014
h1 = hmac .HMAC (b"key" , digestmod = "sha256" )
@@ -1017,11 +1022,57 @@ def test_equality_new(self):
1017
1022
h1 = hmac .new (b"key" , digestmod = "sha256" )
1018
1023
h1 .update (b"some random text" )
1019
1024
h2 = h1 .copy ()
1025
+ # Using id() in case somebody has overridden __eq__/__ne__.
1020
1026
self .assertNotEqual (id (h1 ), id (h2 ))
1021
1027
self .assertEqual (h1 .digest (), h2 .digest ())
1022
1028
self .assertEqual (h1 .hexdigest (), h2 .hexdigest ())
1023
1029
1024
1030
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
+
1025
1076
class CompareDigestMixin :
1026
1077
1027
1078
@staticmethod
0 commit comments