use super::*;
use crate::{cbor::value::Value, iana, iana::WithPrivateRange, util::expect_err, CborSerializable};
use alloc::{borrow::ToOwned, vec};
#[test]
fn test_cwt_encode() {
let tests = [
(
ClaimsSet {
issuer: Some("abc".to_owned()),
..Default::default()
},
concat!(
"a1", "01", "63", "616263" ),
),
(ClaimsSetBuilder::new().build(), "a0"),
(
ClaimsSetBuilder::new()
.issuer("aaa".to_owned())
.subject("bb".to_owned())
.audience("c".to_owned())
.expiration_time(Timestamp::WholeSeconds(0x100))
.not_before(Timestamp::WholeSeconds(0x200))
.issued_at(Timestamp::WholeSeconds(0x10))
.cwt_id(vec![1, 2, 3, 4])
.private_claim(-70_000, Value::Integer(0.into()))
.build(),
concat!(
"a8", "01",
"63",
"616161", "02",
"62",
"6262", "03",
"61",
"63", "04",
"19",
"0100", "05",
"19",
"0200", "06",
"10", "07",
"44",
"01020304", "3a0001116f",
"00" ),
),
(
ClaimsSetBuilder::new()
.claim(
iana::CwtClaimName::Cnf,
Value::Map(vec![(Value::Integer(0.into()), Value::Integer(0.into()))]),
)
.build(),
concat!(
"a1", "08", "a1", "00", "00"
),
),
(
ClaimsSetBuilder::new()
.text_claim("aa".to_owned(), Value::Integer(0.into()))
.build(),
concat!(
"a1", "62", "6161", "00",
),
),
(
ClaimsSetBuilder::new()
.expiration_time(Timestamp::FractionalSeconds(1.5))
.build(),
concat!(
"a1", "04", "f9", "3e00",
),
),
];
for (i, (claims, claims_data)) in tests.iter().enumerate() {
let got = claims.clone().to_vec().unwrap();
assert_eq!(*claims_data, hex::encode(&got), "case {i}");
let got = ClaimsSet::from_slice(&got).unwrap();
assert_eq!(*claims, got);
}
}
#[test]
fn test_cwt_decode_fail() {
let tests = vec![
(
concat!(
"81", "01",
),
"expected map",
),
(
concat!(
"a1", "01", "08", ),
"expected tstr",
),
(
concat!(
"a1", "02", "08", ),
"expected tstr",
),
(
concat!(
"a1", "03", "08", ),
"expected tstr",
),
(
concat!(
"a1", "04", "40", ),
"expected int/float",
),
(
concat!(
"a1", "05", "40", ),
"expected int/float",
),
(
concat!(
"a1", "06", "40", ),
"expected int/float",
),
(
concat!(
"a1", "07", "01", ),
"expected bstr",
),
(
concat!(
"a1", "07", "40", "06", "01", ),
"extraneous data",
),
(
concat!(
"a2", "07", "40", "07", "40", ),
"duplicate map key",
),
];
for (claims_data, err_msg) in tests.iter() {
let data = hex::decode(claims_data).unwrap();
let result = ClaimsSet::from_slice(&data);
expect_err(result, err_msg);
}
}
#[test]
fn test_cwt_is_private() {
assert!(!iana::CwtClaimName::is_private(1));
assert!(iana::CwtClaimName::is_private(-500_000));
}
#[test]
#[should_panic]
fn test_cwt_claims_builder_core_param_panic() {
let _claims = ClaimsSetBuilder::new()
.claim(iana::CwtClaimName::Iss, Value::Null)
.build();
}
#[test]
#[should_panic]
fn test_cwt_claims_builder_non_private_panic() {
let _claims = ClaimsSetBuilder::new()
.private_claim(100, Value::Null)
.build();
}
#[test]
fn test_cwt_dup_claim() {
let claims = ClaimsSetBuilder::new()
.claim(iana::CwtClaimName::AceProfile, Value::Integer(1.into()))
.claim(iana::CwtClaimName::AceProfile, Value::Integer(2.into()))
.build();
let data = claims.to_vec().unwrap();
let result = ClaimsSet::from_slice(&data);
expect_err(result, "duplicate map key");
}