use super::*;
use crate::{iana, util::expect_err, CborSerializable, HeaderBuilder};
use alloc::vec;
#[test]
fn test_context_encode() {
let tests = vec![
(
CoseKdfContext::default(),
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.build(),
concat!(
"84", "01", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.party_u_info(PartyInfoBuilder::new().identity(vec![]).build())
.build(),
concat!(
"84", "01", "83", "40f6f6", "83", "f6f6f6", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.party_u_info(
PartyInfoBuilder::new()
.identity(vec![3, 6])
.nonce(Nonce::Integer(7))
.build(),
)
.build(),
concat!(
"84", "01", "83", "420306", "07f6", "83", "f6f6f6", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.party_u_info(
PartyInfoBuilder::new()
.identity(vec![3, 6])
.nonce(Nonce::Integer(-2))
.build(),
)
.build(),
concat!(
"84", "01", "83", "420306", "21f6", "83", "f6f6f6", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.party_v_info(
PartyInfoBuilder::new()
.identity(vec![3, 6])
.nonce(Nonce::Bytes(vec![7, 3]))
.build(),
)
.build(),
concat!(
"84", "01", "83", "f6f6f6", "83", "420306", "420703", "f6", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.party_v_info(
PartyInfoBuilder::new()
.identity(vec![3, 6])
.other(vec![7, 3])
.build(),
)
.build(),
concat!(
"84", "01", "83", "f6f6f6", "83", "420306", "f6", "420703", "82", "0040", ),
),
(
CoseKdfContextBuilder::new()
.supp_pub_info(
SuppPubInfoBuilder::new()
.key_data_length(10)
.protected(
HeaderBuilder::new()
.algorithm(iana::Algorithm::A128GCM)
.build(),
)
.build(),
)
.build(),
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0a43", "a10101" ),
),
(
CoseKdfContextBuilder::new()
.supp_pub_info(
SuppPubInfoBuilder::new()
.key_data_length(10)
.other(vec![1, 3, 5])
.build(),
)
.build(),
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "83", "0a40", "43010305", ),
),
(
CoseKdfContextBuilder::new()
.add_supp_priv_info(vec![1, 2, 3])
.build(),
concat!(
"85", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", "43", "010203", ),
),
(
CoseKdfContextBuilder::new()
.add_supp_priv_info(vec![1, 2, 3])
.add_supp_priv_info(vec![2, 3])
.add_supp_priv_info(vec![3])
.build(),
concat!(
"87", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", "43", "010203", "42", "0203", "41", "03", ),
),
];
for (i, (key, key_data)) in tests.iter().enumerate() {
let got = key.clone().to_vec().unwrap();
assert_eq!(*key_data, hex::encode(&got), "case {i}");
let mut got = CoseKdfContext::from_slice(&got).unwrap();
got.supp_pub_info.protected.original_data = None;
assert_eq!(*key, got);
}
}
#[test]
fn test_context_decode_fail() {
let tests = vec![
(
concat!(
"a2", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", ),
"expected array",
),
(
concat!(
"83", "00", "83", "f6f6f6", "83", "f6f6f6", ),
"expected array with at least 4 items",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", ),
"decode CBOR failure: Io(EndOfFile",
),
(
concat!(
"84", "08", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", ),
"expected value in IANA or private use range",
),
(
concat!(
"84", "40", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", ),
"expected int/tstr",
),
(
concat!(
"84", "00", "a1", "f6f6", "83", "f6f6f6", "82", "0040", ),
"expected array",
),
(
concat!(
"84", "00", "84", "f6f6f6f6", "83", "f6f6f6", "82", "0040", ),
"expected array with 3 items",
),
(
concat!(
"84", "00", "83", "f660f6", "83", "f6f6f6", "82", "0040", ),
"expected bstr / int / nil",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f660", "82", "0040", ),
"expected bstr / nil",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "60f6f6", "82", "0040", ),
"expected bstr / nil",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "a1", "0040", ),
"expected array",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "81", "00", ),
"expected array with 2 or 3 items",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "4040", ),
"expected int",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0060", ),
"expected bstr",
),
(
concat!(
"84", "00", "83", "f6f6f6", "83", "f6f6f6", "83", "004060", ),
"expected bstr",
),
(
concat!(
"85", "00", "83", "f6f6f6", "83", "f6f6f6", "82", "0040", "60", ),
"expected bstr",
),
(
concat!(
"84", "01", "83", "401b8000000000000000f6",
"83", "f6f6f6",
"82", "0040",
),
"out of range integer value",
),
];
for (context_data, err_msg) in tests.iter() {
let data = hex::decode(context_data).unwrap();
let result = CoseKdfContext::from_slice(&data);
expect_err(result, err_msg);
}
}