137 lines · 9.1 KB · RustRaw
use super::super::fixtures_tests::*;use super::super::row::{open_row_with_table_key, seal_row_with};use super::*;use crate::vault::wrap::public_of;fn tables() -> Vec<(String, u32)> { vec![("note".into(), 1), ("message".into(), 3)]}fn grant() -> Grant { build_grant_with(&EPHEMERAL, &nonce(3), &dk(), DATABASE, "req-1", "tok-1", &tables(), 15, &public_of(&WORKER_SECRET), &person()).unwrap()}#[test]fn the_statement_is_the_plans_bytes() { let statement = unlock_statement(DATABASE, "req-1", "tok-1", &["note".into(), "message".into(), "note".into()], 15, "w1:abc").unwrap(); let want = format!("cotton-unlock-v1\n{DATABASE}\nreq-1\ntok-1\nmessage,note\n15\n{}", super::super::sha256_hex(b"w1:abc")); assert_eq!(String::from_utf8(statement).unwrap(), want);}#[test]fn a_grant_verifies_opens_on_the_worker_and_opens_every_version_of_a_row() { let g = grant(); assert_eq!(g.tables, ["message", "note"]); assert!(verify_grant(&person().public_key(), DATABASE, "req-1", "tok-1", &g)); let bundle = open_grant(&WORKER_SECRET, "req-1", "tok-1", &g.tables, &g.wrapped).unwrap(); assert_eq!(bundle.keys["message"].keys().copied().collect::<Vec<_>>(), [1, 2, 3]); for v in 1..=3 { let row = seal_row_with(&nonce(1), &nonce(2), &dek(), &dk(), DATABASE, "message", ROW_ID, v, &message_spec(), &message_row()).unwrap(); let opened = open_row_with_table_key(&bundle.keys["message"][&v], DATABASE, "message", ROW_ID, &row).unwrap(); assert_eq!(opened["preview"], "Zanzibar ferry tickets"); } // a row sealed after the window (v4) does not open with anything granted let later = seal_row_with(&nonce(1), &nonce(2), &dek(), &dk(), DATABASE, "message", ROW_ID, 4, &message_spec(), &message_row()).unwrap(); assert!(bundle.keys["message"].values().all(|tk| open_row_with_table_key(tk, DATABASE, "message", ROW_ID, &later).is_err())); assert!(!format!("{bundle:?}").contains(&super::super::b64(bundle.keys["note"][&1].as_slice())));}#[test]fn a_forged_replayed_or_retargeted_grant_is_refused() { let g = grant(); let pk = person().public_key(); assert!(!verify_grant(&pk, DATABASE, "req-2", "tok-1", &g)); assert!(!verify_grant(&pk, DATABASE, "req-1", "tok-2", &g)); assert!(!verify_grant(&pk, OTHER_DATABASE, "req-1", "tok-1", &g)); let other = crate::keys::person::PersonKey::from_seed(zeroize::Zeroizing::new([8; 32])); assert!(!verify_grant(&other.public_key(), DATABASE, "req-1", "tok-1", &g)); let changes: [fn(&mut Grant); 3] = [|g| g.minutes = 14, |g| { g.tables.pop(); }, |g| g.wrapped.push('A')]; for change in changes { let mut forged = g.clone(); change(&mut forged); assert!(!verify_grant(&pk, DATABASE, "req-1", "tok-1", &forged)); } assert_eq!(open_grant(&WORKER_SECRET, "req-2", "tok-1", &g.tables, &g.wrapped).unwrap_err(), SealError::DoesNotOpen); assert_eq!(open_grant(&WORKER_SECRET, "req-1", "tok-1", &["note".into()], &g.wrapped).unwrap_err(), SealError::DoesNotOpen); assert_eq!(open_grant(&[10; 32], "req-1", "tok-1", &g.tables, &g.wrapped).unwrap_err(), SealError::DoesNotOpen);}#[test]fn minutes_tables_and_versions_outside_the_format_are_refused() { let wp = public_of(&WORKER_SECRET); for (minutes, tables) in [(5, tables()), (16, tables()), (30, tables()), (1441, tables()), (15, vec![]), (15, vec![("a,b".into(), 1)]), (15, vec![("note".into(), 0)]), (15, vec![("note".into(), 1), ("note".into(), 2)])] { let err = build_grant_with(&EPHEMERAL, &nonce(3), &dk(), DATABASE, "req-1", "tok-1", &tables, minutes, &wp, &person()).unwrap_err(); assert_eq!(err.code(), "seal_bad_input", "{minutes} {tables:?}"); }}/// RULE (ruling R4, 15 Nov 2026): every window the person may choose builds and verifies, a standing one (0) included; any other length is refused at the statement and at the build.#[test]fn each_duration_builds_and_verifies_and_no_other_length_does() { assert_eq!(DURATIONS, [15, 60, 1440, STANDING]); let wp = public_of(&WORKER_SECRET); for minutes in DURATIONS { let g = build_grant_with(&EPHEMERAL, &nonce(3), &dk(), DATABASE, "req-1", "tok-1", &tables(), minutes, &wp, &person()).unwrap(); assert_eq!(g.minutes, minutes); assert!(verify_grant(&person().public_key(), DATABASE, "req-1", "tok-1", &g), "{minutes}"); let statement = String::from_utf8(unlock_statement(DATABASE, "req-1", "tok-1", &g.tables, minutes, &g.wrapped).unwrap()).unwrap(); assert_eq!(statement.lines().nth(5), Some(minutes.to_string().as_str()), "minutes printed as the integer"); let mut other = g.clone(); other.minutes = if minutes == STANDING { 15 } else { STANDING }; assert!(!verify_grant(&person().public_key(), DATABASE, "req-1", "tok-1", &other), "a grant signed for {minutes} does not verify as {}", other.minutes); } for minutes in [1, 5, 14, 16, 30, 59, 61, 1439, 1441, u32::MAX] { assert_eq!(unlock_statement(DATABASE, "req-1", "tok-1", &["note".into()], minutes, "w1:abc").unwrap_err().code(), "seal_bad_input", "{minutes}"); }}/// RULE (owner's decision, 22 Nov 2026): a window over messages carries the message table's own files root, so the Worker opens a kept chat picture inside it; that root is no TK, no BK and not the person's files root (which opens photos and no window carries), and a grant naming no message carries none: its bundle is byte for byte what it was.#[test]fn a_window_over_messages_carries_the_files_root_of_messages_alone() { use super::super::keys::{file_root, table_file_root, table_of_file_kind}; let g = grant(); let bundle = open_grant(&WORKER_SECRET, "req-1", "tok-1", &g.tables, &g.wrapped).unwrap(); let root = table_file_root(&dk(), DATABASE, "message").unwrap(); assert_eq!(bundle.files.keys().collect::<Vec<_>>(), ["message"], "only message's files root"); assert_eq!(bundle.files["message"].as_slice(), root.as_slice()); assert_ne!(root.as_slice(), file_root(&dk(), DATABASE).unwrap().as_slice(), "never the person's files root"); assert!(bundle.keys["message"].values().all(|tk| tk.as_slice() != root.as_slice()) && bundle.blind["message"].as_slice() != root.as_slice()); assert!(!format!("{bundle:?}").contains(&super::super::b64(root.as_slice())), "Debug names the table, never the key"); // The Worker's key for one kept picture opens what the phone sealed under it. let key = format!("{DATABASE}/messages/thread-1/{}.png", "ab".repeat(32)); let fk = super::super::file::person_file_key(&bundle.files["message"], &key); let mut sealed = Vec::new(); super::super::file::seal_file(&super::super::file::person_file_key(&root, &key), &key, &mut b"an invented picture".as_slice(), &mut sealed).unwrap(); let mut plain = Vec::new(); super::super::file::open_file(&fk, &key, &mut sealed.as_slice(), &mut plain).unwrap(); assert_eq!(plain, b"an invented picture"); assert_eq!(table_of_file_kind("messages"), Some("message")); assert_eq!(table_of_file_kind("photos"), None, "a photo's root is never handed"); let notes = bundle_json(&super::bundle(&dk(), DATABASE, "req-1", "tok-1", &[("note".into(), 1)]).unwrap()); assert!(!notes.contains("\"files\""), "a grant without message is as it was: {}", notes.as_str()); let back = parse_bundle(bundle_json(&super::bundle(&dk(), DATABASE, "req-1", "tok-1", &tables()).unwrap()).as_bytes()).unwrap(); assert_eq!(back.files["message"].as_slice(), root.as_slice(), "read back whole");}/// RULE (AVATAR SEAL, MEDIA SEAL FINDINGS (1), 27 Nov 2026): a face is sealed under the person table's files root, so a/// window over person carries that root, and opens a face the phone sealed; a window over neither carries none.#[test]fn a_window_over_person_carries_the_files_root_of_person_and_opens_a_face() { use super::super::keys::{table_file_root, table_of_file_kind, table_opens_files}; let tables: Vec<(String, u32)> = vec![("person".into(), 1), ("note".into(), 1)]; let bundle = parse_bundle(bundle_json(&super::bundle(&dk(), DATABASE, "req-1", "tok-1", &tables).unwrap()).as_bytes()).unwrap(); let root = table_file_root(&dk(), DATABASE, "person").unwrap(); assert_eq!(bundle.files.keys().collect::<Vec<_>>(), ["person"], "only person's files root"); assert_eq!(bundle.files["person"].as_slice(), root.as_slice()); assert_ne!(root.as_slice(), table_file_root(&dk(), DATABASE, "message").unwrap().as_slice(), "never message's"); for key in [format!("{DATABASE}/people/p-1/{}.jpg", "cd".repeat(32)), format!("{DATABASE}/people/p-1/whatsapp/{}.jpg", "ef".repeat(32))] { let mut sealed = Vec::new(); super::super::file::seal_file(&super::super::file::person_file_key(&root, &key), &key, &mut b"an invented face".as_slice(), &mut sealed).unwrap(); let mut plain = Vec::new(); super::super::file::open_file(&super::super::file::person_file_key(&bundle.files["person"], &key), &key, &mut sealed.as_slice(), &mut plain).unwrap(); assert_eq!(plain, b"an invented face"); } assert_eq!(table_of_file_kind("people"), Some("person")); assert!(table_opens_files("person") && table_opens_files("message") && !table_opens_files("note") && !table_opens_files("photo"));}