Skip to content

Commit b3d5926

Browse files
committed
fix: fix a bug found by tests
1 parent 8cc4dc9 commit b3d5926

File tree

6 files changed

+123
-10
lines changed

6 files changed

+123
-10
lines changed

src/c2pa/c2pa.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,12 @@ def load_settings(settings: str, format: str = "json") -> None:
537537

538538

539539
def read_file(path: Union[str, Path],
540-
data_dir: Optional[Union[str, Path]] = None) -> str:
540+
data_dir: Union[str, Path]) -> str:
541541
"""Read a C2PA manifest from a file.
542542
543543
Args:
544544
path: Path to the file to read
545-
data_dir: Optional directory to write binary resources to
545+
data_dir: Directory to write binary resources to
546546
547547
Returns:
548548
The manifest as a JSON string
@@ -553,20 +553,19 @@ def read_file(path: Union[str, Path],
553553
container = _StringContainer()
554554

555555
container._path_str = str(path).encode('utf-8')
556-
container._data_dir_str = str(data_dir).encode(
557-
'utf-8') if data_dir else None
556+
container._data_dir_str = str(data_dir).encode('utf-8')
558557

559558
result = _lib.c2pa_read_file(container._path_str, container._data_dir_str)
560559
return _parse_operation_result_for_error(result)
561560

562561

563562
def read_ingredient_file(
564-
path: Union[str, Path], data_dir: Optional[Union[str, Path]] = None) -> str:
563+
path: Union[str, Path], data_dir: Union[str, Path]) -> str:
565564
"""Read a C2PA ingredient from a file.
566565
567566
Args:
568567
path: Path to the file to read
569-
data_dir: Optional directory to write binary resources to
568+
data_dir: Directory to write binary resources to
570569
571570
Returns:
572571
The ingredient as a JSON string
@@ -577,8 +576,7 @@ def read_ingredient_file(
577576
container = _StringContainer()
578577

579578
container._path_str = str(path).encode('utf-8')
580-
container._data_dir_str = str(data_dir).encode(
581-
'utf-8') if data_dir else None
579+
container._data_dir_str = str(data_dir).encode('utf-8')
582580

583581
result = _lib.c2pa_read_ingredient_file(
584582
container._path_str, container._data_dir_str)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"active_manifest": "contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e",
3+
"manifests": {
4+
"contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e": {
5+
"claim_generator": "make_test_images/0.26.0 c2pa-rs/0.26.0",
6+
"title": "C.jpg",
7+
"format": "image/jpeg",
8+
"instance_id": "xmp:iid:939a4c48-0dff-44ec-8f95-61f52b11618f",
9+
"thumbnail": {
10+
"format": "image/jpeg",
11+
"identifier": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.assertions/c2pa.thumbnail.claim.jpeg"
12+
},
13+
"ingredients": [],
14+
"assertions": [
15+
{
16+
"label": "stds.schema-org.CreativeWork",
17+
"data": {
18+
"@context": "http://schema.org/",
19+
"@type": "CreativeWork",
20+
"author": [
21+
{
22+
"@type": "Person",
23+
"name": "Adobe make_test"
24+
}
25+
]
26+
},
27+
"kind": "Json"
28+
},
29+
{
30+
"label": "c2pa.actions",
31+
"data": {
32+
"actions": [
33+
{
34+
"action": "c2pa.created",
35+
"softwareAgent": "Make Test Images 0.26.0",
36+
"parameters": {
37+
"name": "gradient"
38+
},
39+
"digitalSourceType": "http://cv.iptc.org/newscodes/digitalsourcetype/algorithmicMedia"
40+
}
41+
]
42+
}
43+
}
44+
],
45+
"signature_info": {
46+
"alg": "Ps256",
47+
"issuer": "C2PA Test Signing Cert",
48+
"cert_serial_number": "720724073027128164015125666832722375746636448153",
49+
"time": "2023-09-29T17:40:14+00:00"
50+
},
51+
"label": "contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e"
52+
}
53+
},
54+
"validation_results": {
55+
"activeManifest": {
56+
"success": [
57+
{
58+
"code": "claimSignature.insideValidity",
59+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.signature",
60+
"explanation": "claim signature valid"
61+
},
62+
{
63+
"code": "claimSignature.validated",
64+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.signature",
65+
"explanation": "claim signature valid"
66+
},
67+
{
68+
"code": "assertion.hashedURI.match",
69+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.assertions/c2pa.thumbnail.claim.jpeg",
70+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/c2pa.thumbnail.claim.jpeg"
71+
},
72+
{
73+
"code": "assertion.hashedURI.match",
74+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.assertions/stds.schema-org.CreativeWork",
75+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/stds.schema-org.CreativeWork"
76+
},
77+
{
78+
"code": "assertion.hashedURI.match",
79+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.assertions/c2pa.actions",
80+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/c2pa.actions"
81+
},
82+
{
83+
"code": "assertion.hashedURI.match",
84+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.assertions/c2pa.hash.data",
85+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/c2pa.hash.data"
86+
},
87+
{
88+
"code": "assertion.dataHash.match",
89+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e/c2pa.assertions/c2pa.hash.data",
90+
"explanation": "data hash valid"
91+
}
92+
],
93+
"informational": [],
94+
"failure": []
95+
}
96+
},
97+
"validation_state": "Valid"
98+
}
50 KB
Binary file not shown.
50 KB
Binary file not shown.
50 KB
Binary file not shown.

tests/test_unit_tests.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import ctypes
2020

2121
from c2pa import Builder, C2paError as Error, Reader, C2paSigningAlg as SigningAlg, C2paSignerInfo, Signer, sdk_version
22-
from c2pa.c2pa import Stream, read_ingredient_file
22+
from c2pa.c2pa import Stream, read_ingredient_file, read_file
2323

2424
PROJECT_PATH = os.getcwd()
2525

@@ -682,12 +682,29 @@ def test_read_ingredient_file(self):
682682

683683
ingredient_json_with_dir = read_ingredient_file(self.testPath, temp_data_dir)
684684

685-
# Parse the JSON and verify specific ingredient fields
685+
# Verify some fields
686686
ingredient_data = json.loads(ingredient_json_with_dir)
687687
self.assertEqual(ingredient_data["title"], "C.jpg")
688688
self.assertEqual(ingredient_data["format"], "image/jpeg")
689689
self.assertIn("thumbnail", ingredient_data)
690690

691+
def test_read_file(self):
692+
"""Test reading a C2PA ingredient from a file."""
693+
temp_data_dir = os.path.join(self.data_dir, "temp_data")
694+
os.makedirs(temp_data_dir, exist_ok=True)
695+
696+
# self.testPath has C2PA metadata to read
697+
file_json_with_dir = read_file(self.testPath, temp_data_dir)
698+
699+
# Parse the JSON and verify specific fields
700+
file_data = json.loads(file_json_with_dir)
701+
expected_manifest_id = "contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e"
702+
703+
# Verify some fields
704+
self.assertEqual(file_data["active_manifest"], expected_manifest_id)
705+
self.assertIn("manifests", file_data)
706+
self.assertIn(expected_manifest_id, file_data["manifests"])
707+
691708

692709
if __name__ == '__main__':
693710
unittest.main()

0 commit comments

Comments
 (0)