Cotton code
140 lines · 4.8 KB · RustRaw
//! The shell's judge clock: the boot session's source, and judge time read//! off the continuous clock alone.use std::sync::atomic::{AtomicI64, Ordering};use super::*;struct FakeContinuous {    ms: AtomicI64,    boot: String,}impl Continuous for FakeContinuous {    fn now_ms(&self) -> Result<i64, String> {        Ok(self.ms.load(Ordering::SeqCst))    }    fn boot(&self) -> Result<String, String> {        Ok(self.boot.clone())    }}struct Unreadable;impl Continuous for Unreadable {    fn now_ms(&self) -> Result<i64, String> {        Ok(1_000)    }    fn boot(&self) -> Result<String, String> {        Err("no boot session".to_string())    }}const RELAY_ISO: &str = "2026-10-03T10:00:00.000Z";const RELAY_MS: i64 = 1_791_021_600_000;/// RULE: The boot session is kern.bootsessionuuid (Linux: the kernel's boot_id; iOS: an id the app mints and keeps), never kern.boottime — a boot time moves when the wall clock is set, and judge time must not.#[test]fn boot_session_is_bootsessionuuid_and_survives_a_wall_clock_set() {    #[cfg(target_os = "linux")]    {        let source = std::fs::read_to_string(LINUX_BOOT_ID).expect("the kernel's boot_id");        assert_eq!(boot_session().expect("a boot session"), source.trim());    }    #[cfg(target_os = "ios")]    keep_boot_session_in(&std::env::temp_dir());    #[cfg(all(target_vendor = "apple", not(target_os = "ios")))]    {        let out = std::process::Command::new("/usr/sbin/sysctl")            .args(["-n", "kern.bootsessionuuid"])            .output()            .expect("sysctl runs");        let source = String::from_utf8(out.stdout).expect("utf-8");        assert_eq!(boot_session().expect("a boot session"), source.trim());    }    assert_eq!(boot_session(), boot_session());    // Judge time reads the continuous clock, not the wall: setting the wall    // clock an hour back after the reading moves nothing.    let continuous = Arc::new(FakeContinuous {        ms: AtomicI64::new(10_000),        boot: "boot-a".to_string(),    });    let judge = JudgeClock::new(continuous.clone());    assert_eq!(        judge.offer_server_time(RELAY_ISO, RELAY_MS),        Ok(Offered::Taken)    );    continuous.ms.store(70_000, Ordering::SeqCst);    assert_eq!(judge.now(), Some(RELAY_MS + 60_000));    assert_eq!(judge.state(RELAY_MS + 60_000), ClockState::Checked);    assert_eq!(        judge.state(RELAY_MS + 60_000 - 3_600_000),        ClockState::Corrected {            off_by_ms: -3_600_000        }    );    assert_eq!(judge.now(), Some(RELAY_MS + 60_000));}#[test]fn the_kept_boot_session_is_refused_without_a_folder_then_minted_once_for_the_process() {    // One test, in order: the folder is named once per process.    assert!(kept_boot_session()        .expect_err("no folder named yet")        .contains("keep_boot_session_in"));    let folder = tempfile::tempdir().expect("a folder");    keep_boot_session_in(folder.path());    let id = kept_boot_session().expect("a minted boot session");    let kept = std::fs::read_to_string(folder.path().join(minted_boot::FILE)).expect("kept");    assert_eq!(kept.lines().next(), Some(id.as_str()));    assert_eq!(kept_boot_session(), Ok(id));}#[test]fn an_unreadable_boot_session_is_no_judge_time() {    let judge = JudgeClock::new(Arc::new(Unreadable));    assert!(judge.offer_server_time(RELAY_ISO, RELAY_MS).is_err());    assert_eq!(judge.now(), None);    assert_eq!(judge.state(RELAY_MS), ClockState::Unchecked);}#[test]fn an_unreadable_server_time_is_refused() {    let judge = JudgeClock::new(Arc::new(FakeContinuous {        ms: AtomicI64::new(0),        boot: "boot-a".to_string(),    }));    assert!(judge.offer_server_time("not a time", RELAY_MS).is_err());    assert_eq!(judge.now(), None);}#[test]fn the_running_machine_has_a_continuous_clock() {    let first = SystemContinuous.now_ms().expect("a continuous clock");    let second = SystemContinuous.now_ms().expect("a continuous clock");    assert!(second >= first);}#[test]fn a_mint_clock_carries_judge_time_and_the_boot_it_counts_in() {    let continuous = Arc::new(FakeContinuous {        ms: AtomicI64::new(10_000),        boot: "boot-a".to_string(),    });    let judge = JudgeClock::new(continuous.clone());    let unchecked = judge.mint_clock(RELAY_MS).expect("readable");    assert_eq!(unchecked.judge_ms(), None);    assert_eq!(unchecked.boot.as_deref(), Some("boot-a"));    assert_eq!(unchecked.cont, 10_000);    judge        .offer_server_time(RELAY_ISO, RELAY_MS)        .expect("a reading");    continuous.ms.store(12_000, Ordering::SeqCst);    let clock = judge.mint_clock(RELAY_MS + 99).expect("readable");    assert_eq!(clock.judge_ms(), Some(RELAY_MS + 2_000));    assert_eq!(clock.wall, RELAY_MS + 99);    assert!(JudgeClock::new(Arc::new(Unreadable)).mint_clock(RELAY_MS).is_err());}