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

uuid_assertion.rs (3291B)


      1 // Copyright 2022 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 use crate::{
     15     assertion::{Assertion, AssertionBase, AssertionData, AssertionDecodeError},
     16     error::{Error, Result},
     17 };
     18 
     19 /// Helper class to create User assertion
     20 #[derive(Debug, Default)]
     21 pub struct Uuid {
     22     label: String,
     23     uuid: String,
     24     data: Vec<u8>,
     25 }
     26 
     27 impl Uuid {
     28     /// Create new Identity instance
     29     pub fn new(label: &str, uuid: String, data: Vec<u8>) -> Uuid {
     30         Uuid {
     31             label: label.to_owned(),
     32             uuid,
     33             data,
     34         }
     35     }
     36 }
     37 
     38 impl AssertionBase for Uuid {
     39     /// returns the label for this instance
     40     fn label(&self) -> &str {
     41         &self.label
     42     }
     43 
     44     // Build UUID assertion containing user defined data
     45     // Uuid must be a hex string representing a uuid
     46     fn to_assertion(&self) -> Result<Assertion> {
     47         // validate that the string is 16 hex bytes
     48         match hex::decode(&self.uuid) {
     49             Ok(v) if v.len() == 16 => (),
     50             _ => return Err(Error::BadParam("uuid must be 32 hex digits".to_string())),
     51         }
     52 
     53         let data = AssertionData::Uuid(self.uuid.to_owned(), self.data.to_owned());
     54         Ok(Assertion::new(&self.label, None, data).set_content_type("application/octet-stream"))
     55     }
     56 
     57     fn from_assertion(assertion: &Assertion) -> Result<Self> {
     58         match assertion.decode_data() {
     59             AssertionData::Uuid(s, data) => {
     60                 Ok(Uuid::new(&assertion.label(), s.clone(), data.clone()))
     61             }
     62             ad => Err(AssertionDecodeError::from_assertion_unexpected_data_type(
     63                 assertion, ad, "uuid",
     64             )
     65             .into()),
     66         }
     67     }
     68 }
     69 
     70 #[cfg(test)]
     71 pub mod tests {
     72     #![allow(clippy::expect_used)]
     73     #![allow(clippy::unwrap_used)]
     74 
     75     use super::*;
     76     const LABEL: &str = "uuid_test_assertion";
     77     const UUID: &str = "ABCDABCDABCDABCDABCDABCDABCDABCD";
     78     const INVALID_UUID: &str = "I am bad";
     79     const DATA: [u8; 16] = [
     80         0x0d, 0x0e, 0x0a, 0x0d, 0x0b, 0x0e, 0x0e, 0x0f, 0x0a, 0x0d, 0x0b, 0x0e, 0x0a, 0x0d, 0x0b,
     81         0x0e,
     82     ];
     83 
     84     #[test]
     85     fn assertion_uuid() {
     86         let original = Uuid::new(LABEL, UUID.to_string(), DATA.to_vec());
     87         let assertion = original.to_assertion().expect("build_assertion");
     88         assert_eq!(assertion.mime_type(), "application/octet-stream");
     89         assert_eq!(assertion.label(), LABEL);
     90         let result = Uuid::from_assertion(&assertion).expect("from_assertion");
     91         assert_eq!(original.data, result.data);
     92     }
     93 
     94     #[test]
     95     fn assertion_bad_uuid() {
     96         let original = Uuid::new(LABEL, INVALID_UUID.to_string(), DATA.to_vec());
     97         original
     98             .to_assertion()
     99             .expect_err("Assertion encoding error expected");
    100     }
    101 }