11
11
# specific language governing permissions and limitations under
12
12
# each license.import unittest
13
13
14
- import unittest
15
- from unittest .mock import mock_open , patch
16
- import c2pa_api
17
- from c2pa_api import c2pa
18
14
import os
19
15
import io
16
+ import json
17
+ import unittest
18
+ from unittest .mock import mock_open , patch
19
+
20
+ from c2pa_api import Builder , Error , Reader , SigningAlg , create_signer , sdk_version , sign_ps256
21
+
20
22
PROJECT_PATH = os .getcwd ()
21
23
22
24
testPath = os .path .join (PROJECT_PATH , "tests" , "fixtures" , "C.jpg" )
23
25
24
26
class TestC2paSdk (unittest .TestCase ):
25
27
26
28
def test_version (self ):
27
- self .assertIn ("0.4.0" ,c2pa_api .c2pa .sdk_version ())
28
-
29
- #def test_supported_extensions(self):
30
- # self.assertIn("jpeg",c2pa_api.c2pa.supported_extensions())
29
+ self .assertIn ("0.4.0" , sdk_version ())
31
30
32
31
33
32
class TestReader (unittest .TestCase ):
34
33
35
- def test_normal_read (self ):
34
+ def test_stream_read (self ):
36
35
with open (testPath , "rb" ) as file :
37
- reader = c2pa_api . Reader ("image/jpeg" ,file )
36
+ reader = Reader ("image/jpeg" ,file )
38
37
json = reader .json ()
39
38
self .assertIn ("C.jpg" , json )
40
39
41
- def test_normal_read_and_parse (self ):
40
+ def test_stream_read_and_parse (self ):
42
41
with open (testPath , "rb" ) as file :
43
- reader = c2pa_api .Reader ("image/jpeg" ,file )
44
- json = reader .json ()
45
- self .assertIn ("C.jpg" , json )
46
- #manifest_store = c2pa_api.ManifestStore.from_json(json)
47
- #title= manifest_store.manifests[manifest_store.activeManifest].title
48
- #self.assertEqual(title, "C.jpg")
42
+ reader = Reader ("image/jpeg" ,file )
43
+ manifest_store = json .loads (reader .json ())
44
+ title = manifest = manifest_store ["manifests" ][manifest_store ["active_manifest" ]]["title" ]
45
+ self .assertEqual (title , "C.jpg" )
46
+
47
+ def test_json_decode_err (self ):
48
+ with self .assertRaises (Error .Io ):
49
+ manifest_store = Reader ("image/jpeg" ,"foo" )
49
50
50
- #def test_json_decode_err(self):
51
- # with self.assertRaises(c2pa_api.json.decoder.JSONDecodeError):
52
- # manifest_store = c2pa_api.ManifestStore.from_json("foo")
51
+ def test_reader_bad_format (self ):
52
+ with self .assertRaises (Error .NotSupported ):
53
+ with open (testPath , "rb" ) as file :
54
+ reader = Reader ("badFormat" ,file )
53
55
54
- #def test_reader_bad_format(self):
55
- # with self.assertRaises(c2pa_api.c2pa.StreamError.Other):
56
- # with open(testPath, "rb") as file:
57
- # manifestStore = c2pa_api.ManifestStoreReader("badFormat",file)
58
- # json = manifestStore.read()
59
56
60
57
class TestBuilder (unittest .TestCase ):
61
58
# Define a manifest as a dictionary
@@ -84,26 +81,36 @@ class TestBuilder(unittest.TestCase):
84
81
]
85
82
}
86
83
87
- def sign_ps256 (data : bytes ) -> bytes :
88
- return c2pa_api .sign_ps256_shell (data , "tests/fixtures/ps256.pem" )
84
+ # Define a function that signs data with PS256 using a private key
85
+ def sign (data : bytes ) -> bytes :
86
+ return sign_ps256 (data , "tests/fixtures/ps256.pem" )
89
87
90
88
# load the public keys from a pem file
91
- pemFile = os .path .join (PROJECT_PATH ,"tests" ,"fixtures" ,"ps256.pub" )
92
- certs = open (pemFile ,"rb" ).read ()
89
+ certs = open ("tests/fixtures/ps256.pub" ,"rb" ).read ()
93
90
94
- # Create a local signer from a certificate pem file
95
- signer = c2pa_api . create_signer (sign_ps256 , c2pa . SigningAlg .PS256 , certs , "http://timestamp.digicert.com" )
91
+ # Create a local Ps256 signer with certs and a timestamp server
92
+ signer = create_signer (sign , SigningAlg .PS256 , certs , "http://timestamp.digicert.com" )
96
93
97
- def test_normal_build (self ):
94
+ def test_streams_build (self ):
98
95
with open (testPath , "rb" ) as file :
99
- builder = c2pa_api . Builder (TestBuilder .manifestDefinition )
96
+ builder = Builder (TestBuilder .manifestDefinition )
100
97
output = byte_array = io .BytesIO (bytearray ())
101
98
builder .sign (TestBuilder .signer , "image/jpeg" , file , output )
102
99
output .seek (0 )
103
- reader = c2pa_api . Reader ("image/jpeg" , output )
100
+ reader = Reader ("image/jpeg" , output )
104
101
self .assertIn ("Python Test" , reader .json ())
105
102
106
-
103
+ def test_streams_build (self ):
104
+ with open (testPath , "rb" ) as file :
105
+ builder = Builder (TestBuilder .manifestDefinition )
106
+ archive = byte_array = io .BytesIO (bytearray ())
107
+ builder .to_archive (archive )
108
+ builder = Builder .from_archive (archive )
109
+ output = byte_array = io .BytesIO (bytearray ())
110
+ builder .sign (TestBuilder .signer , "image/jpeg" , file , output )
111
+ output .seek (0 )
112
+ reader = Reader ("image/jpeg" , output )
113
+ self .assertIn ("Python Test" , reader .json ())
107
114
108
115
if __name__ == '__main__' :
109
116
unittest .main ()
0 commit comments