c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

mod.rs (3706B)


      1 // Copyright 2024 Adobe. All rights reserved.
      2 // This file is licensed to you under the Apache License,
      3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
      4 // or the MIT license (http://opensource.org/licenses/MIT),
      5 // at your option.
      6 
      7 // Unless required by applicable law or agreed to in writing,
      8 // this software is distributed on an "AS IS" BASIS, WITHOUT
      9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
     10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
     11 // specific language governing permissions and limitations under
     12 // each license.
     13 
     14 mod compare_readers;
     15 mod test_signer;
     16 
     17 use std::{
     18     fs,
     19     io::{Read, Seek},
     20     path::{Path, PathBuf},
     21 };
     22 
     23 use c2pa::{format_from_path, Reader, Result};
     24 pub use compare_readers::compare_readers;
     25 #[allow(unused)]
     26 pub use test_signer::test_signer;
     27 
     28 #[allow(unused_macros)]
     29 macro_rules! assert_err {
     30     ($expression:expr, $($pattern:tt)+) => {
     31         match $expression {
     32             $($pattern)+ => (),
     33             ref e => panic!("expected `{}` but got `{:?}`", stringify!($($pattern)+), e),
     34         }
     35     }
     36 }
     37 #[allow(unused_imports)]
     38 pub(super) use assert_err;
     39 
     40 pub fn fixtures_path<P: AsRef<Path>>(file_name: P) -> std::path::PathBuf {
     41     PathBuf::from("tests/fixtures").join(file_name)
     42 }
     43 
     44 pub fn known_good_path<P: AsRef<Path>>(file_name: P) -> std::path::PathBuf {
     45     PathBuf::from("tests/known_good").join(file_name)
     46 }
     47 
     48 /// get a file from path without requiring file_io feature enabled in the c2pa crate
     49 pub fn reader_from_file<P: AsRef<Path>>(path: P) -> Result<Reader> {
     50     let format = format_from_path(&path).ok_or(c2pa::Error::UnsupportedType)?;
     51     Reader::from_stream(&format, &mut fs::File::open(&path)?)
     52 }
     53 
     54 #[allow(unused)]
     55 pub fn write_known_good<P: AsRef<Path>>(file_name: P) -> Result<()> {
     56     let reader = reader_from_file(fixtures_path(&file_name))?;
     57     let mut path = known_good_path(file_name);
     58     path.set_extension("json");
     59     fs::write(path, reader.json()).map_err(Into::into)
     60 }
     61 
     62 pub fn read_known_good<P: AsRef<Path>>(file_name: P) -> std::io::Result<String> {
     63     let mut path = known_good_path(file_name);
     64     path.set_extension("json");
     65     fs::read_to_string(path)
     66 }
     67 
     68 #[allow(unused)]
     69 pub fn compare_to_known_good<P: AsRef<Path>>(reader: &Reader, file_name: P) -> Result<()> {
     70     let known = read_known_good(&file_name)?;
     71     let reader1 = Reader::from_json(&known)?;
     72     //et reader2 = reader_from_file(fixtures_path(file_name))?;
     73     let result = compare_readers(&reader1, reader)?;
     74     assert!(result.is_empty(), "{}", result.join("\n"));
     75     Ok(())
     76 }
     77 
     78 #[allow(unused)]
     79 pub fn compare_stream_to_known_good<P: AsRef<Path>, S: Read + Seek + Send>(
     80     stream: &mut S,
     81     format: &str,
     82     known_file: P,
     83 ) -> Result<()> {
     84     let known = read_known_good(&known_file)?;
     85     let reader1 = Reader::from_json(&known)?;
     86     let reader2 = Reader::from_stream(format, stream)?;
     87     let result = compare_readers(&reader1, &reader2)?;
     88     assert!(result.is_empty(), "{}", result.join("\n"));
     89     Ok(())
     90 }
     91 
     92 #[allow(unused)]
     93 pub fn fixture_stream(name: &str) -> Result<(String, fs::File)> {
     94     let path = fixtures_path(name);
     95     let format = format_from_path(&path).ok_or(c2pa::Error::UnsupportedType)?;
     96     let stream = fs::File::open(&path)?;
     97     Ok((format, stream))
     98 }
     99 
    100 #[allow(unused)]
    101 pub fn check_validation_status(reader: &Reader, code: &str) {
    102     if let Some(validation_statuses) = reader.validation_status() {
    103         assert!(
    104             validation_statuses
    105                 .iter()
    106                 .any(|status| status.code() == code),
    107             "Expected to find {code} in validation status"
    108         );
    109     } else {
    110         panic!("Expected to find validation status");
    111     }
    112 }