Cotton code
127 lines · 4.3 KB · RustRaw
//! A revocation written through the one writer reaches every reader of a//! device's standing, through each reader's own public function.//!//! The writer (`cotton_store::revocation::revoke`, reached through//! `cotton_tools::devices::DeviceWrite::revoke`) writes the `cotton_revocations`//! fact and the `revoked_at` mirror in one act. Most readers still read the//! mirror; this file holds that each of them refuses the device all the same.//! It edits none of their files.//!//! The relay roster's reader (`cotton-ffi` `shell/roster_sync`) has no public//! function; its half is `fact_revocation_removes_the_device_from_the_relay_roster`//! beside it.//!//! Every key is invented.use std::sync::Arc;use chrono::{DateTime, Utc};use cotton_client::seams::{FixedClock, SeededRandom};use cotton_client::start::{start_local, StartLocal};use cotton_client::Layout;use cotton_core::device::pairing::DevicePlatform;use cotton_store::driver::Driver;use cotton_tools::devices::{DeviceWrite, NewDeviceRow};use rusqlite::types::Value as SqlValue;const THIS_PHONE: &str = "cGhvbmUta2V5LWZvci10ZXN0cy1vbmx5LTAwMDAwMDAwMA";const THE_MAC: &str = "bWFjLWtleS1mb3ItdGVzdHMtb25seS0wMDAwMDAwMDAwMA";const A_REVOKED_MAC: &str = "b3RoZXItbWFjLWtleS1mb3ItdGVzdHMtb25seS0wMDAwMDA";fn at() -> DateTime<Utc> {    DateTime::parse_from_rfc3339("2026-10-04T09:00:00.000Z")        .expect("a literal instant")        .with_timezone(&Utc)}/// What each reader answers for the Mac, in this file, now.#[derive(Debug, PartialEq, Eq)]struct Answers {    can_host: bool,}fn answers(db: &Driver) -> Answers {    let can_host = db        .all(            "SELECT platform, sampled_at, host_version, revoked_at FROM cotton_devices \             WHERE device_id = ?",            &[SqlValue::Text(A_REVOKED_MAC.to_string())],        )        .expect("the rows read")        .iter()        .any(|row| {            cotton_core::rules::machines::can_host(                row.text("platform"),                row.text("sampled_at"),                row.text("host_version"),                row.text("revoked_at"),            )        });    Answers { can_host }}/// RULE: A device revoked through the one revocation writer is refused by every revoked_at reader through its public function: can_host.#[test]fn every_revoked_at_reader_sees_a_fact_revocation() {    let root = tempfile::tempdir().expect("a temp dir");    let layout = Layout::new(root.path().join("CottonDB"));    let started = start_local(StartLocal {        layout: &layout,        clock: &FixedClock::new(at()),        random: &SeededRandom::new(3),        name: None,        device_key_id: Some(THIS_PHONE),        device_name: "This phone",        platform: DevicePlatform::Ios,        app_version: None,        receipt_signer: cotton_store::signed::SignerSlot::default(),    })    .expect("a start");    let db = Driver::open(&started.replica).expect("the replica");    let write = DeviceWrite {        db: &db,        workspace_id: &started.workspace_id,        owner_id: &started.owner_id,        now: at(),        random: Arc::new(cotton_tools::random::SystemRandom),    };    // Two Macs, one of them revoked below.    for device_id in [THE_MAC, A_REVOKED_MAC] {        write            .insert(&NewDeviceRow {                device_id: device_id.to_string(),                workspace_id: started.workspace_id.clone(),                name: "A Mac".to_string(),                platform: Some(DevicePlatform::Mac),                app_version: None,                approved_at: "2026-10-04T09:00:00.000Z".to_string(),                approved_by: THIS_PHONE.to_string(),                relay_identity_key: None,            })            .expect("the device row");    }    let live = Answers { can_host: true };    assert_eq!(        answers(&db),        live,        "every reader admits the Mac while it is live"    );    assert!(write        .revoke(A_REVOKED_MAC, THIS_PHONE, "revoked")        .expect("the revocation"));    let facts = db        .all(            "SELECT reason FROM cotton_revocations WHERE subject_table = 'cotton_devices'",            &[],        )        .expect("the facts read");    assert_eq!(facts.len(), 1, "the writer wrote the fact");    assert_eq!(        answers(&db),        Answers { can_host: false },        "every reader refuses the revoked Mac"    );}