#![cfg(test)]
use super::*;
#[test]
fn round_trip_is_identity_on_the_value() {
let mut checked = 0;
for delegation in [sample(), seat_sample()] {
let parsed = Delegation::from_cose_bytes(&delegation.to_cose_bytes()).unwrap();
assert_eq!(parsed, delegation);
checked += 1;
}
assert_eq!(checked, 2, "both valid pairs must have round-tripped");
}
#[test]
fn round_trip_is_identity_on_the_bytes() {
let bytes = sample().to_cose_bytes();
let reencoded = Delegation::from_cose_bytes(&bytes).unwrap().to_cose_bytes();
assert_eq!(reencoded, bytes);
}
#[test]
fn round_trip_holds_across_the_interesting_shapes() {
let cases: Vec<(String, u64, u64)> = vec![
("a".to_string(), 0, 0),
("b".repeat(23), 23, 23),
("example.test".to_string(), 24, 24),
("d".repeat(255), 65_535, 255),
("e".repeat(256), 1_700_000_000_000, 300),
("f".repeat(3000), u64::MAX, MAX_SEQUENCE),
("g".repeat(10_000), 1, 1),
("h".repeat(70_000), 2, 2),
];
let mut checked = 0;
for (origin, not_before_unix_ms, sequence) in cases {
let delegation = Delegation {
root_public_key: ROOT_KEY,
claim: claim_for(&origin, not_before_unix_ms, sequence),
signature: SIGNATURE,
};
let bytes = delegation.to_cose_bytes();
assert_eq!(Delegation::from_cose_bytes(&bytes).unwrap(), delegation);
checked += 1;
}
assert_eq!(checked, 8, "every shape must have been exercised");
}
#[test]
fn to_cose_bytes_stays_infallible_which_is_why_the_round_trip_claim_is_qualified() {
let empty_subject = Delegation {
root_public_key: ROOT_KEY,
claim: claim_for("", 0, 0),
signature: SIGNATURE,
};
assert!(Delegation::from_cose_bytes(&empty_subject.to_cose_bytes()).is_err());
let ok = sample();
assert_eq!(
Delegation::from_cose_bytes(&ok.to_cose_bytes()).unwrap(),
ok
);
}