Cotton code
84 lines · 3.4 KB · RustRaw
//! The generation verdict, exhaustively — it is a total function of two//! numbers, so every case it can take is written down here.use super::*;#[test]fn the_ordinary_refresh_rotates_to_the_next_generation() {    assert_eq!(judge(7, 7), Verdict::Rotate { next: 8 });}#[test]fn a_generation_ahead_of_this_macs_row_rotates_rather_than_refusing() {    // Another Mac advanced the chain and the op has not folded here yet. The    // token is MAC-verified, so only a Mac holding the workspace key minted it.    assert_eq!(judge(4, 9), Verdict::Rotate { next: 10 });}#[test]fn the_ceiling_is_a_dead_end_rather_than_a_place_the_chain_rests() {    // Standing still at the ceiling would leave ONE token that is a pure    // function of the grant, never rotates and can be replayed for ever, while    // every legitimate lower generation reads as reuse — replay detection off,    // permanently, and `MaxNonNull` makes it unrecoverable. Four billion    // refreshes of one consent does not happen; a row that says so is a row    // somebody wrote.    assert_eq!(judge(u32::MAX, u32::MAX), Verdict::Replay);    assert_eq!(judge(u32::MAX, u32::MAX - 1), Verdict::Replay);    assert_eq!(judge(3, u32::MAX), Verdict::Replay);    // And one below it still behaves like any other number.    assert_eq!(        judge(u32::MAX - 2, u32::MAX - 2),        Verdict::Rotate {            next: u32::MAX - 1        }    );}#[test]fn the_predecessor_presented_again_is_a_replay_by_the_numbers() {    // Whether it is honoured as a RETRY is `retry_window_open` plus the    // server's proof; the verdict itself never moves the chain backwards.    assert_eq!(judge(8, 7), Verdict::Replay);}/// RULE: Only the exact predecessor, inside sixty seconds of its rotation on either side, can be a retry — two generations behind, or a minute and a millisecond on, is reuse.#[test]fn the_retry_window_is_the_predecessor_inside_a_minute_of_its_rotation() {    let at = 1_000_000_000;    assert!(retry_window_open(8, 7, at, at));    assert!(retry_window_open(8, 7, at, at + RETRY_WINDOW_MS));    assert!(!retry_window_open(8, 7, at, at + RETRY_WINDOW_MS + 1));    // A stamp from a Mac whose clock runs ahead closes a minute past it.    assert!(retry_window_open(8, 7, at, at - RETRY_WINDOW_MS));    assert!(!retry_window_open(8, 7, at, at - RETRY_WINDOW_MS - 1));    // Not the predecessor: the successor was spent, or it is the chain's own.    assert!(!retry_window_open(8, 6, at, at));    assert!(!retry_window_open(8, 8, at, at));    assert!(!retry_window_open(8, 9, at, at));    // The ceiling is a row somebody wrote.    assert!(!retry_window_open(u32::MAX, u32::MAX - 1, at, at));}#[test]fn anything_older_than_the_predecessor_is_reuse() {    assert_eq!(judge(8, 6), Verdict::Replay);    assert_eq!(judge(8, 0), Verdict::Replay);}#[test]fn the_verdict_is_total_over_a_small_grid() {    // Every combination of a plausible row and a plausible presentation lands    // in exactly one arm, and a `Rotate` never goes backwards.    for current in 0u32..6 {        for presented in 0u32..6 {            match judge(current, presented) {                Verdict::Rotate { next } => {                    assert!(presented >= current, "rotate only from here or ahead");                    assert!(next > presented, "a rotation always moves");                }                Verdict::Replay => assert!(presented < current),            }        }    }}