use std::io::Cursor; use super::*; fn key(n: u8) -> BackupKey { BackupKey::mint_with(&[n; 14]) } fn manifest() -> Manifest { Manifest { app: "story".into(), made_at: "2026-11-25T10:00:00Z".into(), made_by: "test".into(), device: "Android phone".into(), parts: vec![], counts: [("scripts".to_string(), 3), ("scenes".to_string(), 41)].into_iter().collect(), } } /// Invented bytes that do not deflate to nothing: a pseudo-random run, so a file spans several chunks. fn bytes(len: usize, seed: u32) -> Vec { let mut x = seed.wrapping_mul(2654435761).max(1); (0..len) .map(|_| { x ^= x << 13; x ^= x >> 17; x ^= x << 5; x as u8 }) .collect() } fn made(k: &BackupKey, parts: &[(&str, Vec)]) -> (Made, Vec) { let mut out = Vec::new(); let mut readers: Vec>> = parts.iter().map(|(_, b)| Cursor::new(b.clone())).collect(); let mut sources: Vec> = parts .iter() .zip(readers.iter_mut()) .map(|((name, b), r)| Source { name: name.to_string(), bytes: b.len() as u64, read: r }) .collect(); let made = make_with(&mut out, k, &manifest(), &mut sources, &[4; 16]).expect("made"); assert_eq!(made.bytes, out.len() as u64); (made, out) } fn open_it(file: &[u8], unlock: Unlock<'_>) -> Result<(Opened, Vec<(String, Vec)>), Refusal> { let mut got: Vec<(String, Vec)> = Vec::new(); let opened = open(&mut Cursor::new(file), "story", unlock, &mut |part, chunk| { match got.last_mut() { Some((name, b)) if *name == part.name => b.extend_from_slice(chunk), _ => got.push((part.name.clone(), chunk.to_vec())), } Ok(()) })?; Ok((opened, got)) } #[test] fn a_file_made_opens_whole_with_its_key() { let data = bytes(CHUNK * 2 + 777, 1); let picture = bytes(5000, 2); let (made, file) = made(&key(1), &[("data.sqlite", data.clone()), ("files/a.jpg", picture.clone())]); assert_eq!(made.outer.app, "story"); assert_eq!(made.outer.key, key(1).check()); let (opened, parts) = open_it(&file, Unlock::Key(&key(1))).expect("opens"); assert_eq!(parts, vec![("data.sqlite".to_string(), data), ("files/a.jpg".to_string(), picture)]); assert_eq!(opened.manifest.counts.get("scenes"), Some(&41)); assert_eq!(opened.manifest.parts.len(), 2); } #[test] fn an_empty_app_makes_a_file_that_opens() { let (_, file) = made(&key(1), &[("data.sqlite", vec![])]); let (_, parts) = open_it(&file, Unlock::Key(&key(1))).expect("opens"); assert!(parts.is_empty(), "no bytes, no chunks handed over"); } #[test] fn text_is_deflated_so_a_database_takes_less_room() { let text = "INSERT INTO note VALUES ('a walk by the sea');\n".repeat(20_000).into_bytes(); let (made, _) = made(&key(1), &[("data.sqlite", text.clone())]); assert!(made.bytes < text.len() as u64 / 10, "{} of {}", made.bytes, text.len()); } #[test] fn nothing_a_person_keeps_stands_in_the_open() { let (made, file) = made(&key(1), &[("data.sqlite", b"a secret line about Asha".repeat(10))]); let text = String::from_utf8_lossy(&file); for word in ["scenes", "scripts", "data.sqlite", "Asha", "Android"] { assert!(!text.contains(word), "{word} must be sealed"); } let (outer, _) = read_outer(&mut Cursor::new(&file)).expect("header"); assert_eq!(outer, made.outer); } #[test] fn the_code_opens_a_file_on_a_phone_without_the_key() { let (_, file) = made(&key(1), &[("data.sqlite", bytes(100, 3))]); let code = key(1).code(); let (opened, _) = open_it(&file, Unlock::Code(&code.to_lowercase())).expect("the typed code opens it"); assert_eq!(opened.key, key(1), "the phone keeps the key, so its next backups need no code"); open_it(&file, Unlock::Both(&key(9), &code)).expect("a key store holding another code falls back to the typed one"); } #[test] fn another_code_or_a_mistyped_one_is_refused_in_words() { let (_, file) = made(&key(1), &[("data.sqlite", bytes(100, 3))]); assert_eq!(open_it(&file, Unlock::Key(&key(2))).unwrap_err(), Refusal::OtherKey); assert_eq!(open_it(&file, Unlock::Code(&key(2).code())).unwrap_err(), Refusal::OtherKey); assert!(matches!(open_it(&file, Unlock::Code("ABCD")).unwrap_err(), Refusal::CodeTyped(_))); assert!(matches!(open_it(&file, Unlock::Both(&key(2), "ABCD")).unwrap_err(), Refusal::CodeTyped(_))); } #[test] fn another_apps_file_or_no_backup_at_all_is_refused() { let (_, file) = made(&key(1), &[("data.sqlite", bytes(10, 1))]); let err = open(&mut Cursor::new(&file), "notes", Unlock::Key(&key(1)), &mut |_, _| Ok(())).unwrap_err(); assert_eq!(err, Refusal::OtherApp("story".into())); let err = open(&mut Cursor::new(b"SQLite format 3\0...."), "story", Unlock::Key(&key(1)), &mut |_, _| Ok(())).unwrap_err(); assert_eq!(err, Refusal::NotABackup); assert_eq!(read_outer(&mut Cursor::new(b"")).unwrap_err(), Refusal::NotABackup); } #[test] fn a_newer_format_asks_for_an_update() { let (_, file) = made(&key(1), &[("data.sqlite", bytes(10, 1))]); let text = String::from_utf8_lossy(&file[..200]).to_string(); let newer = text.replacen("\"format\":1", "\"format\":2", 1); let mut changed = newer.into_bytes(); changed.extend_from_slice(&file[200..]); assert_eq!(read_outer(&mut Cursor::new(&changed)).unwrap_err(), Refusal::Newer); } #[test] fn every_flipped_byte_is_caught_and_nothing_is_kept() { let (_, file) = made(&key(1), &[("data.sqlite", bytes(CHUNK + 300, 7))]); let header_end = file.iter().skip(MAGIC.len()).position(|b| *b == b'\n').unwrap() + MAGIC.len() + 1; // Each place: inside the open header's id, a chunk length, inside the first chunk, inside the last chunk. for at in [MAGIC.len() + 40, header_end + 1, header_end + 100, file.len() - 3] { let mut broken = file.clone(); broken[at] ^= 0x20; let err = open_it(&broken, Unlock::Key(&key(1))).unwrap_err(); assert!(matches!(err, Refusal::Damaged | Refusal::Cut | Refusal::OtherKey), "byte {at}: {err:?}"); } } #[test] fn a_file_cut_short_or_with_bytes_added_is_refused() { let (_, file) = made(&key(1), &[("data.sqlite", bytes(CHUNK + 300, 7))]); for keep in [file.len() - 1, file.len() - 20, file.len() / 2, MAGIC.len() + 10] { let err = open_it(&file[..keep], Unlock::Key(&key(1))).unwrap_err(); assert!(matches!(err, Refusal::Cut | Refusal::Damaged), "{keep}: {err:?}"); } let mut longer = file.clone(); longer.extend_from_slice(b"more"); assert_eq!(open_it(&longer, Unlock::Key(&key(1))).unwrap_err(), Refusal::Damaged); } #[test] fn chunks_cannot_be_swapped_between_two_files_of_one_key() { let (_, a) = made(&key(1), &[("data.sqlite", bytes(500, 1))]); let mut out = Vec::new(); let data = bytes(500, 2); let mut reader = Cursor::new(data.clone()); let mut sources = [Source { name: "data.sqlite".into(), bytes: 500, read: &mut reader }]; make_with(&mut out, &key(1), &manifest(), &mut sources, &[9; 16]).expect("made"); let a_end = a.iter().skip(MAGIC.len()).position(|b| *b == b'\n').unwrap() + MAGIC.len() + 1; let b_end = out.iter().skip(MAGIC.len()).position(|b| *b == b'\n').unwrap() + MAGIC.len() + 1; let mut mixed = a[..a_end].to_vec(); mixed.extend_from_slice(&out[b_end..]); assert_eq!(open_it(&mixed, Unlock::Key(&key(1))).unwrap_err(), Refusal::Damaged); } #[test] fn a_part_named_outside_its_folder_is_never_made() { let mut out = Vec::new(); let mut reader = Cursor::new(vec![1u8]); let mut sources = [Source { name: "../x".into(), bytes: 1, read: &mut reader }]; assert!(matches!(make(&mut out, &key(1), &manifest(), &mut sources), Err(Refusal::Io(_)))); for bad in ["", "/etc/x", "a//b", "a/../b", "a\\b"] { assert!(!plain_name(bad), "{bad}"); } assert!(plain_name("files/ab.jpg")); }