@@ -1025,6 +1025,57 @@ def test_builder_sign_file_callback_signer_from_callback(self):
10251025 finally :
10261026 shutil .rmtree (temp_dir )
10271027
1028+ def test_builder_sign_file_callback_signer_from_callback_V2 (self ):
1029+ """Test signing a file using the sign_file method with Signer.from_callback."""
1030+
1031+ temp_dir = tempfile .mkdtemp ()
1032+ try :
1033+
1034+ output_path = os .path .join (temp_dir , "signed_output_from_callback.jpg" )
1035+
1036+ # Will use the sign_file method
1037+ builder = Builder (self .manifestDefinitionV2 )
1038+
1039+ # Create signer with callback using Signer.from_callback
1040+ signer = Signer .from_callback (
1041+ callback = self .callback_signer_es256 ,
1042+ alg = SigningAlg .ES256 ,
1043+ certs = self .certs .decode ('utf-8' ),
1044+ tsa_url = "http://timestamp.digicert.com"
1045+ )
1046+
1047+ manifest_bytes = builder .sign_file (
1048+ source_path = self .testPath ,
1049+ dest_path = output_path ,
1050+ signer = signer
1051+ )
1052+
1053+ # Verify the output file was created
1054+ self .assertTrue (os .path .exists (output_path ))
1055+
1056+ # Verify results
1057+ self .assertIsInstance (manifest_bytes , bytes )
1058+ self .assertGreater (len (manifest_bytes ), 0 )
1059+
1060+ # Read the signed file and verify the manifest
1061+ with open (output_path , "rb" ) as file , Reader ("image/jpeg" , file ) as reader :
1062+ json_data = reader .json ()
1063+ self .assertIn ("Python Test" , json_data )
1064+ self .assertNotIn ("validation_status" , json_data )
1065+
1066+ # Parse the JSON and verify the signature algorithm
1067+ manifest_data = json .loads (json_data )
1068+ active_manifest_id = manifest_data ["active_manifest" ]
1069+ active_manifest = manifest_data ["manifests" ][active_manifest_id ]
1070+
1071+ # Verify the signature_info contains the correct algorithm
1072+ self .assertIn ("signature_info" , active_manifest )
1073+ signature_info = active_manifest ["signature_info" ]
1074+ self .assertEqual (signature_info ["alg" ], self .callback_signer_alg )
1075+
1076+ finally :
1077+ shutil .rmtree (temp_dir )
1078+
10281079 def test_sign_file_using_callback_signer_overloads (self ):
10291080 """Test signing a file using the sign_file function with a Signer object."""
10301081 # Create a temporary directory for the test
@@ -1247,7 +1298,6 @@ def test_signing_manifest_v2(self):
12471298 self .assertIn ("Python Test Image V2" , json_data )
12481299 self .assertNotIn ("validation_status" , json_data )
12491300
1250- # Clean up
12511301 output .close ()
12521302
12531303class TestStream (unittest .TestCase ):
0 commit comments