Skip to content

Commit 32a14b8

Browse files
committed
fix: Test repro
1 parent f2a8951 commit 32a14b8

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

tests/test_unit_tests.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,71 @@ def test_sign_file(self):
752752
# Clean up the temporary directory
753753
shutil.rmtree(temp_dir)
754754

755+
def test_sign_file_callback_signer(self):
756+
"""Test signing a file using the sign_file method."""
757+
import tempfile
758+
import shutil
759+
760+
# Create a temporary directory for the test
761+
temp_dir = tempfile.mkdtemp()
762+
try:
763+
# Create a temporary output file path
764+
output_path = os.path.join(temp_dir, "signed_output.jpg")
765+
766+
# Use the sign_file method
767+
builder = Builder(self.manifestDefinition)
768+
769+
# Create a real ES256 signing callback
770+
def sign_callback(data: bytes) -> bytes:
771+
"""Real ES256 signing callback that creates actual signatures."""
772+
# Load the private key from the test fixtures
773+
with open(os.path.join(self.data_dir, "es256_private.key"), "rb") as key_file:
774+
private_key_data = key_file.read()
775+
776+
# Load the private key using cryptography
777+
private_key = serialization.load_pem_private_key(
778+
private_key_data,
779+
password=None,
780+
backend=default_backend()
781+
)
782+
783+
# Create the signature using ES256 (ECDSA with SHA-256)
784+
signature = private_key.sign(
785+
data,
786+
padding=None, # ECDSA doesn't use padding
787+
algorithm=hashes.SHA256()
788+
)
789+
790+
return signature
791+
792+
# Create signer with callback
793+
signer = create_signer(
794+
callback=sign_callback,
795+
alg=SigningAlg.ES256,
796+
certs=self.certs.decode('utf-8'),
797+
tsa_url="http://timestamp.digicert.com"
798+
)
799+
800+
result = builder.sign_file(
801+
source_path=self.testPath,
802+
dest_path=output_path,
803+
signer=signer
804+
)
805+
806+
# Verify the output file was created
807+
self.assertTrue(os.path.exists(output_path))
808+
809+
# Read the signed file and verify the manifest
810+
with open(output_path, "rb") as file:
811+
reader = Reader("image/jpeg", file)
812+
json_data = reader.json()
813+
self.assertIn("Python Test", json_data)
814+
self.assertNotIn("validation_status", json_data)
815+
816+
finally:
817+
# Clean up the temporary directory
818+
shutil.rmtree(temp_dir)
819+
755820

756821
class TestStream(unittest.TestCase):
757822
def setUp(self):
@@ -1027,13 +1092,13 @@ def test_create_signer_with_callback(self):
10271092
"""Test creating a signer with a callback function."""
10281093
def mock_sign_callback(data: bytes) -> bytes:
10291094
"""Mock signing callback that returns a fake signature."""
1030-
# Return a fake signature (64 bytes for Ed25519)
1095+
# Return a fake signature (64 bytes for ES256)
10311096
return b"fake_signature_" + b"0" * 50
10321097

1033-
# Test with Ed25519 algorithm
1098+
# Test with ES256 algorithm
10341099
signer = create_signer(
10351100
callback=mock_sign_callback,
1036-
alg=SigningAlg.ED25519,
1101+
alg=SigningAlg.ES256,
10371102
certs=self.certs
10381103
)
10391104

0 commit comments

Comments
 (0)