use std::time::Duration;
use super::Superseded;
const OLD: &str = "sk-zzq-the-key-that-was-replaced";
const LONG: Duration = Duration::from_mins(1);
fn holding(token: &str) -> Superseded {
let window = Superseded::new();
window.hold(token.to_owned(), LONG);
window
}
#[test]
fn a_fresh_window_is_closed_and_admits_nothing() {
let window = Superseded::new();
assert!(!window.is_open());
assert!(!window.admits(OLD.as_bytes()));
assert!(!window.admits(b""));
}
#[test]
fn a_held_key_admits_every_time_it_is_presented() {
let window = holding(OLD);
for attempt in 1..=5 {
assert!(
window.admits(OLD.as_bytes()),
"presentation {attempt} was refused; this is not a one-shot credential"
);
}
assert!(window.is_open(), "and the window is still open afterwards");
}
#[test]
fn every_near_miss_is_still_refused() {
let window = holding(OLD);
for wrong in [
"",
" ",
"sk-zzq-the-key-that-was-replace", "sk-zzq-the-key-that-was-replaced ", "sk-zzq-the-key-that-was-replacec", "SK-ZZQ-THE-KEY-THAT-WAS-REPLACED", "sk-zzq-the-replacement",
] {
assert!(
!window.admits(wrong.as_bytes()),
"{wrong:?} must not admit through the window"
);
}
}
#[test]
fn a_key_held_for_no_time_at_all_is_not_held_at_all() {
let window = Superseded::new();
window.hold(OLD.to_owned(), Duration::ZERO);
assert!(!window.admits(OLD.as_bytes()));
assert!(!window.is_open());
assert!(
window.lock().is_none(),
"a zero window must drop the key rather than park a dead secret"
);
}
#[test]
fn a_grace_the_clock_cannot_represent_holds_nothing_rather_than_panicking() {
let window = Superseded::new();
window.hold(OLD.to_owned(), Duration::MAX);
assert!(
!window.admits(OLD.as_bytes()),
"Duration::MAX must not become a permanent second credential"
);
assert!(window.lock().is_none());
}
#[test]
fn an_enormous_but_representable_grace_is_not_a_panic_either() {
for large in [
Duration::from_secs(u64::MAX / 2),
Duration::from_secs(1 << 40),
Duration::from_secs(1 << 50),
] {
let window = Superseded::new();
window.hold(OLD.to_owned(), large); let _ = window.admits(OLD.as_bytes());
}
}
#[test]
fn a_key_outlives_its_window_by_the_clock() {
let window = Superseded::new();
window.hold(OLD.to_owned(), Duration::from_millis(1));
std::thread::sleep(Duration::from_millis(20));
assert!(
!window.admits(OLD.as_bytes()),
"the window closed a clock tick ago and is still admitting"
);
}
#[test]
fn an_expired_key_is_dropped_rather_than_ignored() {
let window = Superseded::new();
window.hold(OLD.to_owned(), Duration::from_millis(1));
assert!(
window.lock().is_some(),
"the key occupies the slot while its window is open"
);
std::thread::sleep(Duration::from_millis(20));
assert!(!window.admits(OLD.as_bytes()));
assert!(
window.lock().is_none(),
"an expired key must be dropped by the check that refused it"
);
}
#[test]
fn asking_whether_a_window_is_open_also_drops_an_expired_key() {
let window = Superseded::new();
window.hold(OLD.to_owned(), Duration::from_millis(1));
std::thread::sleep(Duration::from_millis(20));
assert!(!window.is_open());
assert!(window.lock().is_none());
}
#[test]
fn a_second_hold_retires_the_first_key_rather_than_chaining() {
let window = holding(OLD);
window.hold("sk-zzq-the-first-replacement".to_owned(), LONG);
assert!(
!window.admits(OLD.as_bytes()),
"the older key must not survive a second rotation"
);
assert!(window.admits(b"sk-zzq-the-first-replacement"));
}
#[test]
fn releasing_closes_an_open_window_at_once() {
let window = holding(OLD);
assert!(window.is_open());
window.release();
assert!(!window.admits(OLD.as_bytes()));
assert!(!window.is_open());
assert!(window.lock().is_none(), "and the key is gone, not parked");
}
#[test]
fn releasing_nothing_is_allowed() {
let window = Superseded::new();
window.release();
window.release();
assert!(!window.is_open());
}