user_cbor.rs (3789B)
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 serde::{Deserialize, Serialize}; 15 16 use crate::{ 17 assertion::{Assertion, AssertionBase, AssertionData, AssertionDecodeError}, 18 error::{Error, Result}, 19 }; 20 21 /// Helper class to create Cbor User assertion 22 #[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq)] 23 pub struct UserCbor { 24 label: String, 25 cbor_data: Vec<u8>, 26 } 27 28 impl UserCbor { 29 /// Create new UserCbor instance 30 pub fn new(label: &str, data: Vec<u8>) -> UserCbor { 31 UserCbor { 32 label: label.to_owned(), 33 cbor_data: data, 34 } 35 } 36 } 37 38 impl AssertionBase for UserCbor { 39 /// returns the label for this instance 40 fn label(&self) -> &str { 41 &self.label 42 } 43 44 fn to_assertion(&self) -> Result<Assertion> { 45 // validate cbor 46 let _value: serde_cbor::Value = 47 serde_cbor::from_slice(&self.cbor_data).map_err(|_err| Error::AssertionEncoding)?; 48 let data = AssertionData::Cbor(self.cbor_data.clone()); 49 Ok(Assertion::new(&self.label, None, data)) 50 } 51 52 fn from_assertion(assertion: &Assertion) -> Result<Self> { 53 match assertion.decode_data() { 54 AssertionData::Cbor(data) => { 55 // validate cbor 56 let _value: serde_cbor::Value = serde_cbor::from_slice(data).map_err(|e| { 57 Error::AssertionDecoding(AssertionDecodeError::from_assertion_and_cbor_err( 58 assertion, e, 59 )) 60 })?; 61 62 Ok(Self::new(&assertion.label(), data.clone())) 63 } 64 ad => Err(AssertionDecodeError::from_assertion_unexpected_data_type( 65 assertion, ad, "cbor", 66 ) 67 .into()), 68 } 69 } 70 } 71 72 #[cfg(test)] 73 pub mod tests { 74 #![allow(clippy::expect_used)] 75 #![allow(clippy::unwrap_used)] 76 77 use super::*; 78 const LABEL: &str = "user_test_assertion"; 79 const DATA: &str = r#"{ "l1":"some data", "l2":"some other data" }"#; 80 81 #[test] 82 fn assertion_user_cbor() { 83 let json: serde_json::Value = serde_json::from_str(DATA).unwrap(); 84 let data = serde_cbor::to_vec(&json).unwrap(); 85 let original = UserCbor::new(LABEL, data); 86 let assertion = original.to_assertion().expect("build_assertion"); 87 assert_eq!(assertion.mime_type(), "application/cbor"); 88 assert_eq!(assertion.label(), LABEL); 89 let result = UserCbor::from_assertion(&assertion).expect("from_assertion"); 90 assert_eq!(original.cbor_data, result.cbor_data); 91 } 92 93 #[test] 94 fn assertion_user_cbor_invalid_to() { 95 let invalid_cbor = vec![0x0d, 0x0e, 0x0a, 0x0d, 0x0b, 0x0e, 0x0e, 0x0f]; 96 let original = UserCbor::new(LABEL, invalid_cbor); 97 original 98 .to_assertion() 99 .expect_err("Assertion encoding error expected"); 100 } 101 102 #[test] 103 fn assertion_user_cbor_invalid_from() { 104 let invalid_cbor = vec![0x0d, 0x0e, 0x0a, 0x0d, 0x0b, 0x0e, 0x0e, 0x0f]; 105 let data = AssertionData::Cbor(invalid_cbor); 106 let assertion = Assertion::new(LABEL, None, data); 107 let _result = 108 UserCbor::from_assertion(&assertion).expect_err("Assertion decoding error expected"); 109 } 110 }