#![cfg(test)]
use super::*;
#[test]
fn a_replayed_delegation_is_byte_identical_which_is_why_sequence_exists() {
let key = root();
let leaf_zero = sign_delegation(&key, &claim_at(ORIGIN, 1)).unwrap();
let mut superseding = claim_at(ORIGIN, 2);
superseding.delegated_public_key = attacker().public_key_bytes();
let leaf_n = sign_delegation(&key, &superseding).unwrap();
let replayed = leaf_zero.clone();
for artifact in [&leaf_zero, &leaf_n, &replayed] {
verify_delegation(artifact, &key.public_key_bytes(), DOMAIN, ORIGIN).unwrap();
}
let reissued = sign_delegation(&key, &claim_at(ORIGIN, 1)).unwrap();
assert_eq!(
replayed, reissued,
"two issuances of one claim must be byte-identical — if this ever \
stops holding, Ed25519 determinism or the encoder has changed and the \
reasoning behind `sequence` needs revisiting"
);
let replayed_seq = verify_delegation(&replayed, &key.public_key_bytes(), DOMAIN, ORIGIN)
.unwrap()
.claim
.sequence;
let current_seq = verify_delegation(&leaf_n, &key.public_key_bytes(), DOMAIN, ORIGIN)
.unwrap()
.claim
.sequence;
assert!(
replayed_seq < current_seq,
"the replayed delegation must be recognisably superseded"
);
let mut forged = claim_at(ORIGIN, 3);
forged.delegated_public_key = operational().public_key_bytes();
let forged_bytes = sign_delegation(&attacker(), &forged).unwrap();
assert!(
verify_delegation(&forged_bytes, &key.public_key_bytes(), DOMAIN, ORIGIN).is_err(),
"re-sequencing requires the root key"
);
}
#[test]
fn sequence_is_signature_covered_and_survives_the_round_trip() {
let key = root();
let mut checked = 0;
for sequence in [
0u64,
1,
23,
24,
255,
256,
65_535,
65_536,
1u64 << 63,
encoding::MAX_SEQUENCE - 1,
] {
let cose = sign_delegation(&key, &claim_at(ORIGIN, sequence)).unwrap();
let verified = verify_delegation(&cose, &key.public_key_bytes(), DOMAIN, ORIGIN).unwrap();
assert_eq!(verified.claim.sequence, sequence);
let other = sign_delegation(&key, &claim_at(ORIGIN, sequence + 1)).unwrap();
assert_ne!(
cose, other,
"sequence {sequence} did not reach the signed bytes"
);
checked += 1;
}
assert_eq!(
checked, 10,
"every head-width boundary must have been tried"
);
}
#[test]
fn a_sequence_with_no_successor_cannot_be_issued_or_verified() {
let key = root();
let root_key = key.public_key_bytes();
let mut at_max = claim();
at_max.sequence = u64::MAX;
assert!(matches!(
sign_delegation(&key, &at_max),
Err(TrustError::DelegationEncoding { .. })
));
let signature = key.sign(&delegation_preimage(&root_key, &at_max));
assert!(matches!(
assemble_delegation(&root_key, &at_max, &signature),
Err(TrustError::DelegationEncoding { .. })
));
let mutant = signed_envelope(
&protected_for(&key),
&encoding::payload_bytes(&at_max),
&key,
);
assert!(matches!(
verify_delegation(&mutant, &root_key, DOMAIN, ORIGIN),
Err(TrustError::DelegationVerification)
));
let mut at_limit = claim();
at_limit.sequence = encoding::MAX_SEQUENCE;
let ok = sign_delegation(&key, &at_limit).unwrap();
assert_eq!(
verify_delegation(&ok, &root_key, DOMAIN, ORIGIN)
.unwrap()
.claim
.sequence,
encoding::MAX_SEQUENCE
);
}
#[test]
fn not_before_is_never_an_ordering_key() {
let key = root();
let mut earlier = claim();
earlier.not_before_unix_ms = 1;
let mut same = claim();
same.not_before_unix_ms = 1;
let mut zero = claim();
zero.not_before_unix_ms = 0;
let mut far_future = claim();
far_future.not_before_unix_ms = u64::MAX;
let mut past_i64_max = claim();
past_i64_max.not_before_unix_ms = 1u64 << 63;
let mut accepted = 0;
for candidate in [earlier, same, zero, far_future, past_i64_max] {
let cose = sign_delegation(&key, &candidate).unwrap();
let verified = verify_delegation(&cose, &key.public_key_bytes(), DOMAIN, ORIGIN).unwrap();
assert_eq!(
verified.claim.not_before_unix_ms,
candidate.not_before_unix_ms
);
accepted += 1;
}
assert_eq!(accepted, 5, "every timestamp must have been exercised");
}