usesuper::*;/// RULE (R6): the kinds sealed are a database's photos, videos, books, music and films, and nothing of another/// database or another kind (messages seal by their own table's rule; documents and meetings are read by the object).#[test]fn only_a_persons_pictures_books_songs_and_films_are_sealed_here() {// A kept chat picture too, since 22 Nov 2026 (owner's decision: never unencrypted on the server).for key in ["db-1/photos/a.jpg", "db-1/videos/a.mov", "db-1/books/a.epub", "db-1/music/a.mp3", "db-1/films/f-1/whole.mp4", "db-1/messages/t/a.jpg"] { assert!(is_person_file("db-1", key), "{key}"); }for key in ["db-2/photos/a.jpg", "db-2/messages/t/a.jpg", "db-1/documents/d/a.pdf", "db-1/sealed/0123", "db-1/photos/", "photo:abc"] { assert!(!is_person_file("db-1", key), "{key}"); }}fn arrival_stored(database: &str, key: &str, plain: &[u8]) -> Vec<u8> {let public = cotton_core::seal::arrival::arrival_public(&cotton_core::seal::DataKey::from_bytes([5; 32]), database).unwrap(); cotton_core::seal::arrival_file::seal_with(&FileKey::from_bytes([9; 32]), [1; 7], &[2; 32], &[3; 12], &public, key, plain).unwrap()}fn held_files(database: &str) -> PersonFiles {let keys = crate::seal::HeldDataKeys::new(); keys.hold(database, &cotton_core::seal::DataKey::from_bytes([5; 32]));PersonFiles::new(database, keys asArc<dynDataKeyStore>)}/// RULE (R6 with R2): a cover the object fetched and sealed at arrival comes/// down as the picture on a device holding the data key: its key is the one/// its head carries wrapped to this person, and its bytes the f1 file past it.#[test]fn a_file_sealed_at_arrival_opens_with_the_key_its_head_carries() {let dir = tempfile::tempdir().unwrap();let database = "00000000-0000-4000-8000-0000000000a1";let key = format!("{database}/photos/{}.jpg", "ab".repeat(32));let plain: Vec<u8> = (0..150_000).map(|i| (i % 251) as u8).collect();let raw = dir.path().join("cover.sealed"); std::fs::write(&raw, arrival_stored(database, &key, &plain)).unwrap(); assert!(looks_sealed(&raw).unwrap());let files = held_files(database);let fk = files.key_for_download(&key, &raw).unwrap();let dest = dir.path().join("cover.jpg"); open_download(&fk, &key, &raw, &dest).unwrap(); assert_eq!(std::fs::read(&dest).unwrap(), plain);}/// RULE: an arrival file whose head is moved onto another file, or opened with/// no data key held, never opens: refused as not opening, or as waiting.#[test]fn a_file_sealed_at_arrival_opens_for_no_other_file_and_waits_for_the_key() {let dir = tempfile::tempdir().unwrap();let database = "00000000-0000-4000-8000-0000000000a1";let key = format!("{database}/photos/{}.jpg", "ab".repeat(32));let raw = dir.path().join("cover.sealed"); std::fs::write(&raw, arrival_stored(database, &key, b"a synthetic cover")).unwrap();let other = format!("{database}/photos/{}.jpg", "cd".repeat(32));let refused = held_files(database).key_for_download(&other, &raw).err().expect("refused"); assert_eq!(refused.code.as_deref(), Some("seal_does_not_open"));let none = PersonFiles::new(database, crate::seal::HeldDataKeys::new() asArc<dynDataKeyStore>);let waiting = none.key_for_download(&key, &raw).err().expect("waits"); assert_eq!(waiting.status, 0, "no key yet is transient: {}", waiting.message);}/// RULE (owner's decision, 22 Nov 2026): a kept chat picture is sealed under the message table's own files root, which a window over messages hands the Worker; a photo under the person's files root, which no window carries. Each opens only under its own.#[test]fn a_kept_chat_picture_is_sealed_under_the_message_tables_root_and_a_photo_is_not() {let files = held_files("db-1");let dk = cotton_core::seal::DataKey::from_bytes([5; 32]);let chat = "db-1/messages/thread-1/a.png";let photo = "db-1/photos/a.png";let tfr = cotton_core::seal::keys::table_file_root(&dk, "db-1", "message").unwrap();let fr = cotton_core::seal::keys::file_root(&dk, "db-1").unwrap(); assert_eq!(files.file_key(chat).unwrap().text(), file::person_file_key(&tfr, chat).text()); assert_eq!(files.file_key(photo).unwrap().text(), file::person_file_key(&fr, photo).text()); assert_ne!(files.file_key(chat).unwrap().text(), file::person_file_key(&fr, chat).text(), "never the photos' root");}/// RULE (AVATAR SEAL 2 (2), 27 Nov 2026): a device that holds no data key yet still reads a face or chat picture held/// clear from before (its bytes the ones its name says); a clear file whose bytes are not its name's is refused; a/// sealed one waits, transiently, for the key.#[test]fn a_clear_file_from_before_opens_with_no_data_key_and_a_sealed_one_waits() {use sha2::{Digestas _, Sha256};let dir = tempfile::tempdir().unwrap();let database = "00000000-0000-4000-8000-0000000000a1";let none = PersonFiles::new(database, crate::seal::HeldDataKeys::new() asArc<dynDataKeyStore>);let face = b"an invented face from before".to_vec();let sha = hex::encode(Sha256::digest(&face));for key in [format!("{database}/people/p-1/{sha}.jpg"), format!("{database}/people/p-1/whatsapp/{sha}.jpg"), format!("{database}/messages/t-1/{sha}.jpg")] {let (raw, dest) = (dir.path().join("face.raw"), dir.path().join("face.jpg")); std::fs::write(&raw, &face).unwrap(); none.open_fetched(&key, &raw, &dest).expect("a clear file from before opens with no key"); assert_eq!(std::fs::read(&dest).unwrap(), face); assert!(!raw.exists()); std::fs::remove_file(&dest).unwrap(); }let raw = dir.path().join("other.raw"); std::fs::write(&raw, b"other bytes").unwrap();let refused = none.open_fetched(&format!("{database}/people/p-1/{sha}.jpg"), &raw, &dir.path().join("x.jpg")).err().expect("not its name's bytes"); assert_eq!(refused.code.as_deref(), Some("seal_does_not_open"));let key = format!("{database}/photos/{}.jpg", "ab".repeat(32));let raw = dir.path().join("cover.sealed"); std::fs::write(&raw, arrival_stored(database, &key, b"a synthetic cover")).unwrap();let waiting = none.open_fetched(&key, &raw, &dir.path().join("cover.jpg")).err().expect("waits"); assert_eq!(waiting.status, 0, "no key yet is transient: {}", waiting.message);}