Cotton code
123 lines · 5.4 KB · RustRaw
use super::*;const DATABASE: &str = "11111111-1111-4111-8111-111111111111";const BYTES: &[u8] = br#"{"actor":"device-job:mint-author","author":"device:01A","fields":{},"kind":"retract","row":"r1","schema":54,"stamp":"1790000000000:0:device:01A","table":"cotton_todos"}"#;fn hex_of(raw: &[u8]) -> String {    hex::encode(raw)}// RULE: A receipt's signature covers its canonical bytes and the database id, so the same bytes verify in no other database.#[test]fn receipt_signature_covers_bytes_and_database_id() {    // Frozen: the string, and the signature of the key whose secret is 32 × 0x07.    let message = signed_string(DATABASE, BYTES);    assert_eq!(        hex_of(&message),        FROZEN_STRING,        "lp(context) ‖ lp(database id) ‖ lp(SHA-256(bytes)) changed"    );    let signer = KeySigner::of_seed(7);    let sig = signer.sign(&message).expect("signs");    assert_eq!(hex_of(&sig), FROZEN_SIG, "Ed25519 is deterministic: the frozen signature moved");    let key = signer.device_key();    assert_eq!(key, FROZEN_KEY);    assert_eq!(verify(&key, DATABASE, BYTES, Some(&sig)), Ok(()));    // Another database id: the same bytes, the same signature, no.    assert_eq!(        verify(&key, "22222222-2222-4222-8222-222222222222", BYTES, Some(&sig)),        Err(Unverified::Mismatch)    );    // Other bytes.    let mut other = BYTES.to_vec();    other[20] ^= 1;    assert_eq!(verify(&key, DATABASE, &other, Some(&sig)), Err(Unverified::Mismatch));    // Another device's key.    assert_eq!(        verify(&KeySigner::of_seed(8).device_key(), DATABASE, BYTES, Some(&sig)),        Err(Unverified::Mismatch)    );    // No signature, and an author whose device is no key at all.    assert_eq!(verify(&key, DATABASE, BYTES, None), Err(Unverified::Unsigned));    assert_eq!(        verify("device:01JOWNERMAC0000000000000000", DATABASE, BYTES, Some(&sig)),        Err(Unverified::Unsigned)    );    // A signature of the wrong length is not one.    assert_eq!(verify(&key, DATABASE, BYTES, Some(&sig[..63])), Err(Unverified::Mismatch));}#[test]fn the_two_refusals_say_what_the_serving_mac_says() {    assert_eq!(        Unverified::Unsigned.reason(),        "this write is not signed by the device that made it"    );    assert_eq!(        Unverified::Mismatch.reason(),        "this write's signature does not match the device that made it"    );}const FROZEN_STRING: &str = "00000011636f74746f6e2d726563656970742d76310000002431313131313131312d313131312d343131312d383131312d31313131313131313131313100000020771793e6e8df5e42cfd3ca1afc9122cb62fc7492836ad2e703663afa42541137";const FROZEN_SIG: &str = "22b1acf802da45c1d1faf23a8aeedf1af5059a3ae37ab0837979e3e2b24a7dd9f10e881f8caadb9be5ca6f7fc1e9fcae1e36d4021c743f8765ba0da24d68880b";const FROZEN_KEY: &str = "6kpsY-KcUgq-9VB7Ey7F-ZVHdq6-vnuSQh7qaRRG0iw";// ── touches_a_loss: an unsent change a move would reshape ──────────────────fn feedback_at(schema: u32, author: &str, table: &str, fields: &[(&str, serde_json::Value)]) -> crate::canonical::Receipt {    crate::canonical::Receipt {        table: table.to_string(),        row_id: "row-1".to_string(),        kind: crate::canonical::Kind::Create,        stamp: format!("1791636664120:0000:{author}"),        author: author.to_string(),        actor: "owner".to_string(),        schema,        fields: fields.iter().map(|(name, value)| ((*name).to_string(), value.clone())).collect(),        restamps: None,        erase: None,        undo: None,    }}const AUTHOR: &str = "device:01M4JXCW0C2BYT38WJE5A7D4R7";fn receipt_at(    schema: u32,    table: &str,    row: &str,    kind: crate::canonical::Kind,    fields: &[(&str, serde_json::Value)],) -> crate::canonical::Receipt {    crate::canonical::Receipt {        row_id: row.to_string(),        kind,        ..feedback_at(schema, AUTHOR, table, fields)    }}/// RULE: An unsent change stamped below the oldest file this build moves (141), or to a table a move since its stamp dropped on purpose (`cotton_widgets`, schema 144), touches a loss and holds its copy where it is; a change at the floor itself does not — nothing below the floor can say what its fields mean now, and the move declared the dropped table's rows lost (29 Oct 2026).#[test]fn a_change_below_the_oldest_moved_file_or_to_a_dropped_table_touches_a_loss() {    let below = crate::shape_move::LEDGER_BASE - 1;    for (schema, table, fields) in [        (below, "cotton_notes", vec![("title", serde_json::json!("A synthetic note"))]),        (138, "cotton_money_transactions", vec![("status", serde_json::json!("posted"))]),        (89, "cotton_people", vec![("phones", serde_json::json!(r#"["+91 90000 10001"]"#))]),        (104, "cotton_stores", vec![("name", serde_json::json!("A synthetic store"))]),        (143, "cotton_widgets", vec![("kind", serde_json::json!("shows"))]),    ] {        let receipt = receipt_at(schema, table, "row-1", crate::canonical::Kind::Change, &fields);        let op = crate::wire::from_receipt(format!("op-{table}"), &receipt).expect("an op");        assert!(touches_a_loss(&op), "v{schema} {table}: touches a loss");    }    let at_floor = crate::wire::from_receipt(        "op-floor".to_string(),        &receipt_at(crate::shape_move::LEDGER_BASE, "cotton_notes", "row-1", crate::canonical::Kind::Change, &[("title", serde_json::json!("A synthetic note"))]),    )    .expect("an op");    assert!(!touches_a_loss(&at_floor));}