Cotton code
92 lines · 3.4 KB · RustRaw
//! The minted boot session: kept across reads of one boot, new after a reboot.use super::*;const HOUR: i64 = 3_600_000;/// The wall clock at the first read, 2026-10-04T00:00:00Z.const WALL: i64 = 1_791_072_000_000;fn ids() -> impl FnMut() -> String {    let mut next = 0;    move || {        next += 1;        format!("boot-{next}")    }}/// RULE: On iOS the boot session is an id the app mints and keeps beside its continuous and wall readings, never kern.bootsessionuuid (the sandbox refuses it) or kern.boottime: a continuous clock that went back, or a boot instant that moved, mints a new id.#[test]fn a_kept_boot_session_lasts_one_boot_and_a_reboot_mints_another() {    let folder = tempfile::tempdir().expect("a folder");    let file = folder.path().join(FILE);    let mut mint = ids();    // The first read mints and keeps; later reads in the boot keep the id.    assert_eq!(session(&file, HOUR, WALL, &mut mint), Ok("boot-1".to_string()));    assert_eq!(session(&file, 2 * HOUR, WALL + HOUR, &mut mint), Ok("boot-1".to_string()));    // The wall clock drifting a few seconds against the continuous one is    // still this boot.    assert_eq!(        session(&file, 3 * HOUR, WALL + 2 * HOUR + 4_000, &mut mint),        Ok("boot-1".to_string())    );    // The continuous clock is LOWER than the kept reading: the phone rebooted.    assert_eq!(        session(&file, 60_000, WALL + 2 * HOUR + 120_000, &mut mint),        Ok("boot-2".to_string())    );    // The new readings were kept, so the new boot keeps its id.    assert_eq!(        session(&file, 120_000, WALL + 2 * HOUR + 180_000, &mut mint),        Ok("boot-2".to_string())    );    // A reboot the continuous clock alone cannot see: the phone was up 2    // minutes at the kept read, rebooted, and is now up an hour. The boot    // instant moved, so it is another boot.    assert_eq!(        session(&file, HOUR, WALL + 5 * HOUR, &mut mint),        Ok("boot-3".to_string())    );}#[test]fn a_wall_clock_set_between_reads_mints_a_new_id_rather_than_trust_the_old_one() {    let folder = tempfile::tempdir().expect("a folder");    let file = folder.path().join(FILE);    let mut mint = ids();    assert_eq!(session(&file, HOUR, WALL, &mut mint), Ok("boot-1".to_string()));    // A minute later on the continuous clock, a day back on the wall: this    // is indistinguishable from a reboot with a longer uptime, so it is new.    assert_eq!(        session(&file, HOUR + 60_000, WALL - 24 * HOUR, &mut mint),        Ok("boot-2".to_string())    );}#[test]fn a_file_that_names_no_boot_is_replaced_and_one_that_cannot_be_kept_is_an_error() {    let folder = tempfile::tempdir().expect("a folder");    let file = folder.path().join(FILE);    std::fs::write(&file, "half a fi").expect("written");    let mut mint = ids();    assert_eq!(session(&file, HOUR, WALL, &mut mint), Ok("boot-1".to_string()));    assert_eq!(        std::fs::read_to_string(&file).expect("kept"),        format!("boot-1\n{HOUR}\n{WALL}\n")    );    let nowhere = folder.path().join("no-such-folder").join(FILE);    assert!(session(&nowhere, HOUR, WALL, &mut mint)        .expect_err("nothing to keep it in")        .contains("cannot be kept"));}#[test]fn fresh_ids_are_32_hex_characters_and_differ() {    let (first, second) = (fresh_id(), fresh_id());    assert_eq!(first.len(), 32);    assert!(first.chars().all(|c| c.is_ascii_hexdigit()));    assert_ne!(first, second);}