Cotton code
96 lines · 3.8 KB · RustRaw
//! The wall an erasure's unlink stands behind — against a real seeded file.//!//! Every key, path and id is synthetic//! ([nothing-real-ever-goes-in-cotton-s-public-tree]); no file is written and//! none is removed — the question is which path the rule ANSWERS.use std::path::Path;use super::*;use crate::test_support::{seeded_file, WORKSPACE};const THIS_PHONE: &str = "device:01J0000000000000000000000A";/// Another database this device holds — a project's.const ANOTHER_DATABASE: &str = "66666666-6666-4666-8666-666666666666";const MEDIA_DIR: &str = "/synthetic/client/media";fn photo(database: &str, letter: char) -> String {    format!("{database}/photos/{}.heic", letter.to_string().repeat(64))}fn stored(database: &str, name: &str) -> String {    format!("{MEDIA_DIR}/{database}/ab/{name}.heic")}fn filed(db: &Driver, key: &str, local_path: &str) {    db.run(        "INSERT INTO _media (key, workspace_id, sha256, content_type, size_bytes, local_path, \         state, attempts, created_at, updated_at) VALUES (?,?,?,?,?,?,'synced',0,?,?)",        &[            SqlValue::Text(key.to_string()),            SqlValue::Text(WORKSPACE.to_string()),            SqlValue::Text("f".repeat(64)),            SqlValue::Text("image/heic".to_string()),            SqlValue::Integer(9),            SqlValue::Text(local_path.to_string()),            SqlValue::Text("2026-09-16T09:00:00.000Z".to_string()),            SqlValue::Text("2026-09-16T09:00:00.000Z".to_string()),        ],    )    .expect("the ledger row");}/// RULE: An erasure in one database never unlinks a key or a file outside that database's own prefix — the prefix is the erasing file's identity, never the key's first segment.#[test]fn erasure_in_a_place_never_unlinks_a_key_outside_its_prefix() {    let erasing = FileId::for_tests(WORKSPACE);    let mine = photo(WORKSPACE, 'a');    let theirs = photo(ANOTHER_DATABASE, 'b');    // The object store's half: a key.    assert!(may_unlink_key(&erasing, &mine));    assert!(        !may_unlink_key(&erasing, &theirs),        "a key under another database's prefix is not this erasure's to take"    );    assert!(!may_unlink_key(&erasing, ""));    assert!(!may_unlink_key(&erasing, &format!("{WORKSPACE}/../{theirs}")));    // The device's half: a file.    let db = seeded_file(THIS_PHONE);    let media = Path::new(MEDIA_DIR);    filed(&db, &mine, &stored(WORKSPACE, "mine"));    assert_eq!(        may_unlink_local(&db, &erasing, media, &mine).expect("reads"),        Some(stored(WORKSPACE, "mine").into())    );    // Another database's key, even with a row in THIS ledger naming it.    filed(&db, &theirs, &stored(ANOTHER_DATABASE, "theirs"));    assert_eq!(may_unlink_local(&db, &erasing, media, &theirs).expect("reads"), None);    // This database's key whose row names a file under ANOTHER database's    // folder, or one that climbs out of its own.    let astray = photo(WORKSPACE, 'c');    filed(&db, &astray, &stored(ANOTHER_DATABASE, "astray"));    assert_eq!(may_unlink_local(&db, &erasing, media, &astray).expect("reads"), None);    let climbing = photo(WORKSPACE, 'd');    filed(        &db,        &climbing,        &format!("{MEDIA_DIR}/{WORKSPACE}/../{ANOTHER_DATABASE}/ab/climbing.heic"),    );    assert_eq!(may_unlink_local(&db, &erasing, media, &climbing).expect("reads"), None);    // Two keys, one content-addressed file: neither erasure takes it from the    // other.    let twin = photo(WORKSPACE, 'e');    filed(&db, &twin, &stored(WORKSPACE, "mine"));    assert_eq!(may_unlink_local(&db, &erasing, media, &mine).expect("reads"), None);    // A key with no row here has no file here.    assert_eq!(        may_unlink_local(&db, &erasing, media, &photo(WORKSPACE, 'f')).expect("reads"),        None    );}