use super::*;
use crate::Tag;
use alloc::{vec, vec::Vec};
use der::{Decode, Encode};
use hex_literal::hex;
use std::collections::BTreeMap;
const DICT_TEST: &[u8] = include_bytes!("../../tests/data/small-dict.der");
const DICT_MORE_TYPES_TEST: &[u8] = include_bytes!("../../tests/data/dict-more-types.der");
const CONSTRAINT_TEST: &[u8] = include_bytes!("../../tests/data/constraint-dict.der");
#[test]
fn test_dict() {
let value = Value::from_der(DICT_TEST).unwrap();
let Value::Dict(dict) = value else {
panic!("wrong type");
};
let mut expected: BTreeMap<Tag, Value> = BTreeMap::new();
expected.insert(b"kcep".into(), 18446744005121377600u64.into());
expected.insert(b"kclf".into(), 55902208u64.into());
expected.insert(b"kclo".into(), 18446744005107531776u64.into());
expected.insert(b"kclz".into(), 15859712u64.into());
expected.insert(b"kcrf".into(), 0u64.into());
expected.insert(b"kcrz".into(), 52264960u64.into());
expected.insert(b"kcwf".into(), 52264960u64.into());
expected.insert(b"kcwz".into(), 3637248u64.into());
assert_eq!(dict, expected);
let mut encoded = Vec::new();
Value::Dict(expected.clone())
.encode_to_vec(&mut encoded)
.unwrap();
assert_eq!(encoded, DICT_TEST);
let dict = DictRef::<ValueRef<'_>>::from_der(DICT_TEST).unwrap();
let dict = dict.decode_owned().unwrap();
assert_eq!(dict, expected);
}
#[test]
fn test_dict_more_types() {
let value = Value::from_der(DICT_MORE_TYPES_TEST).unwrap();
let Value::Dict(dict) = value else {
panic!("wrong type");
};
let mut expected: BTreeMap<Tag, Value> = BTreeMap::new();
expected.insert(
b"BNCH".into(),
hex!("F4720CB9138FE8119B90AEE41869593CEA382521D803EDA0423A2294415C697F").into(),
);
expected.insert(b"BORD".into(), 10u64.into());
expected.insert(b"CEPO".into(), 1u64.into());
expected.insert(b"CHIP".into(), 33025u64.into());
expected.insert(b"CPRO".into(), true.into());
expected.insert(b"CSEC".into(), true.into());
expected.insert(b"ECID".into(), 8242489359073310u64.into());
expected.insert(b"SDOM".into(), 1u64.into());
expected.insert(
b"pcrp".into(),
hex!(
"0440465E12B073BAB7885BE45281833FA8F676BA71482C6C482383683408A86C
1DE77C19274C48248BF44537F64D2EFEFEEE0ACE1AC03736F5F6BF93433C2A14
9329869DE6237C98E29BA420573F9164BB0CB400C7F7ED5815D7EAF9788A0DF0
12"
)
.into(),
);
expected.insert(
b"snon".into(),
hex!("C86318AD55C5C7B728015696F9F92452D8DF456E").into(),
);
expected.insert(
b"srvn".into(),
hex!("622F165C7803BF7881A73E288E8243AD4253C086").into(),
);
assert_eq!(dict, expected);
let mut encoded = Vec::new();
Value::Dict(expected).encode_to_vec(&mut encoded).unwrap();
assert_eq!(encoded, DICT_MORE_TYPES_TEST);
}
#[test]
fn test_constraint_decode() {
let constraint = Constraint::from_der(CONSTRAINT_TEST).unwrap();
let mut expected_man_props = BTreeMap::new();
expected_man_props.insert(Tag::from_bytes(*b"BNCH"), Constraint::Any);
expected_man_props.insert(Tag::from_bytes(*b"BORD"), Constraint::Any);
expected_man_props.insert(Tag::from_bytes(*b"CEPO"), 1u64.into());
expected_man_props.insert(Tag::from_bytes(*b"CHIP"), 33025u64.into());
expected_man_props.insert(Tag::from_bytes(*b"CPRO"), Constraint::Any);
expected_man_props.insert(Tag::from_bytes(*b"CSEC"), Constraint::Any);
expected_man_props.insert(Tag::from_bytes(*b"ECID"), Constraint::Any);
expected_man_props.insert(Tag::from_bytes(*b"SDOM"), 1u64.into());
expected_man_props.insert(Tag::from_bytes(*b"snon"), Constraint::Any);
let mut expected_obj_props = BTreeMap::new();
expected_obj_props.insert(Tag::from_bytes(*b"DGST"), Constraint::Any);
expected_obj_props.insert(Tag::from_bytes(*b"EPRO"), Constraint::Any);
expected_obj_props.insert(Tag::from_bytes(*b"ESEC"), Constraint::Any);
let mut expected = BTreeMap::new();
expected.insert(
Tag::from_bytes(*b"MANP"),
Constraint::Dict(expected_man_props),
);
expected.insert(
Tag::from_bytes(*b"OBJP"),
Constraint::Dict(expected_obj_props),
);
let Constraint::Dict(dict) = constraint else {
panic!("expected the root to be a dictionary")
};
assert_eq!(dict, expected);
}
#[test]
fn test_constraint_match() {
assert!(Constraint::Any.match_value(&Value::Boolean(false)));
assert!(
Constraint::Array(vec![Constraint::Boolean(true), Constraint::Any,]).match_value(
&Value::Array(vec![Value::Boolean(true), Value::Integer(1234),])
)
);
let mut test_dict_constraint = BTreeMap::new();
test_dict_constraint.insert(Tag::from(b"FOOO"), Constraint::Any);
test_dict_constraint.insert(Tag::from(b"BAAR"), Constraint::Integer(1337));
test_dict_constraint.insert(Tag::from(b"BAAZ"), Constraint::Data(b"".to_vec()));
let test_dict_constraint = Constraint::Dict(test_dict_constraint);
let mut test_dict = BTreeMap::new();
test_dict.insert(Tag::from(b"FOOO"), Value::Boolean(false));
test_dict.insert(Tag::from(b"BAAR"), Value::Integer(1337));
test_dict.insert(Tag::from(b"BAAZ"), Value::Data(b"".to_vec()));
let test_dict = Value::Dict(test_dict);
assert!(test_dict_constraint.match_value(&test_dict));
}
#[test]
#[cfg(feature = "serde")]
fn test_value_serde() {
let value = Value::from_der(DICT_MORE_TYPES_TEST).unwrap();
let string = serde_json::to_string(&value).unwrap();
let new_value: Value = serde_json::from_str(&string).unwrap();
assert_eq!(value, new_value);
}