1111# specific language governing permissions and limitations under
1212# each license.import unittest
1313
14- import unittest
15- from unittest .mock import mock_open , patch
16- import c2pa_api
17- from c2pa_api import c2pa
1814import os
1915import 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+
2022PROJECT_PATH = os .getcwd ()
2123
2224testPath = os .path .join (PROJECT_PATH , "tests" , "fixtures" , "C.jpg" )
2325
2426class TestC2paSdk (unittest .TestCase ):
2527
2628 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 ())
3130
3231
3332class TestReader (unittest .TestCase ):
3433
35- def test_normal_read (self ):
34+ def test_stream_read (self ):
3635 with open (testPath , "rb" ) as file :
37- reader = c2pa_api . Reader ("image/jpeg" ,file )
36+ reader = Reader ("image/jpeg" ,file )
3837 json = reader .json ()
3938 self .assertIn ("C.jpg" , json )
4039
41- def test_normal_read_and_parse (self ):
40+ def test_stream_read_and_parse (self ):
4241 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" )
4950
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 )
5355
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()
5956
6057class TestBuilder (unittest .TestCase ):
6158 # Define a manifest as a dictionary
@@ -84,26 +81,36 @@ class TestBuilder(unittest.TestCase):
8481 ]
8582 }
8683
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" )
8987
9088 # 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 ()
9390
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" )
9693
97- def test_normal_build (self ):
94+ def test_streams_build (self ):
9895 with open (testPath , "rb" ) as file :
99- builder = c2pa_api . Builder (TestBuilder .manifestDefinition )
96+ builder = Builder (TestBuilder .manifestDefinition )
10097 output = byte_array = io .BytesIO (bytearray ())
10198 builder .sign (TestBuilder .signer , "image/jpeg" , file , output )
10299 output .seek (0 )
103- reader = c2pa_api . Reader ("image/jpeg" , output )
100+ reader = Reader ("image/jpeg" , output )
104101 self .assertIn ("Python Test" , reader .json ())
105102
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 ())
107114
108115if __name__ == '__main__' :
109116 unittest .main ()
0 commit comments