From dfc42bb10b1c09a76f3435383842291a93e05d43 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 20:29:24 -0800 Subject: [PATCH] Tests: checked saves keep the original, compressed files capped on both paths Checked saves (used under folder roots and for script commands) had no test for a fill that fails or is canceled, for the copy of the old file going to the system's temporary folder when the folder refuses it, or for the path that writes in the system's temporary folder when the folder refuses the temporary file. Five tests cover them: - a failed fill and a canceled fill return their error, leave the file as it was and remove the temporary file - a refused rename in a folder that then takes no new file keeps the old copy in the system's temporary folder and removes it afterwards - a folder that takes no temporary file saves through the system's temporary folder, and a failed fill there leaves nothing behind - a compressed file with data after its JSON is refused one byte past its JSON cap when streamed, and one byte past its whole-file cap when unpacked whole The permission tests skip when running as root. --- crates/format/src/atomic.rs | 150 ++++++++++++++++++++++++++++++++++++ crates/format/src/unpack.rs | 30 ++++++++ 2 files changed, 180 insertions(+) diff --git a/crates/format/src/atomic.rs b/crates/format/src/atomic.rs index dd0cd4cc6c..c28e6d8091 100644 --- a/crates/format/src/atomic.rs +++ b/crates/format/src/atomic.rs @@ -1413,6 +1413,156 @@ mod tests { let _ = std::fs::remove_dir_all(d); } + /// A checked write whose fill fails puts nothing in place: the error is returned, the file + /// keeps its old content, and the temporary file is removed. + #[cfg(unix)] + #[test] + fn a_checked_write_keeps_the_original_when_its_fill_fails() { + let (d, root, _) = checked_folders("checked-fill-fails"); + let folder = root.join("inside"); + std::fs::write(folder.join("doc.txt"), b"original").unwrap(); + let r = write_atomic_checked( + &folder.join("doc.txt"), + |f| { + f.write_all(b"half a fi")?; + Err(io::Error::other("disk full")) + }, + under(&root), + ); + assert_eq!(r.map_err(|e| e.to_string()), Err("disk full".to_string())); + assert_eq!(std::fs::read(folder.join("doc.txt")).unwrap(), b"original"); + assert!(leftovers(&folder, "doc.txt").is_empty(), "{:?}", leftovers(&folder, "doc.txt")); + let _ = std::fs::remove_dir_all(d); + } + + /// A checked write canceled while it fills puts nothing in place: the error is the + /// cancellation, the file keeps its old content, and the temporary file is removed. + #[cfg(unix)] + #[test] + fn a_checked_write_canceled_while_it_fills_keeps_the_original() { + use vectorcraft_doc::cancel::{CancelToken, Cancelled, scope}; + let (d, root, _) = checked_folders("checked-cancel"); + let folder = root.join("inside"); + std::fs::write(folder.join("doc.txt"), b"original").unwrap(); + let token = CancelToken::new(); + let r = scope(&token, None, || { + write_atomic_checked( + &folder.join("doc.txt"), + |f| { + f.write_all(b"new")?; + token.cancel(); + Ok(()) + }, + under(&root), + ) + }); + assert!(r.as_ref().is_err_and(|e| e.get_ref().is_some_and(|e| e.is::())), "{r:?}"); + assert_eq!(std::fs::read(folder.join("doc.txt")).unwrap(), b"original"); + assert!(leftovers(&folder, "doc.txt").is_empty(), "{:?}", leftovers(&folder, "doc.txt")); + let _ = std::fs::remove_dir_all(d); + } + + /// Makes `folder` refuse new files (mode 0o555). `false`, with the folder writable again, when + /// a new file can still be created there (root ignores the mode). + #[cfg(any(target_os = "linux", target_vendor = "apple"))] + fn refuse_new_files(folder: &Path) -> bool { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(folder, std::fs::Permissions::from_mode(0o555)).unwrap(); + let probe = folder.join("probe"); + if std::fs::write(&probe, b"").is_ok() { + let _ = std::fs::remove_file(&probe); + std::fs::set_permissions(folder, std::fs::Permissions::from_mode(0o755)).unwrap(); + return false; + } + true + } + + /// Files in the system's temporary folder whose names start with the temporary names a write + /// to a file named `name` uses. + #[cfg(any(target_os = "linux", target_vendor = "apple"))] + fn system_temp_files(name: &str) -> Vec { + let prefix = format!(".{name}.{}-", std::process::id()); + std::fs::read_dir(std::env::temp_dir()) + .unwrap() + .filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().to_string())) + .filter(|n| n.starts_with(&prefix)) + .collect() + } + + /// A checked write over a file whose folder refuses the rename and then any new file: the copy + /// of the file as it was goes to the system's temporary folder, the new content is copied into + /// the file, and that copy is removed afterwards. (Skipped for root.) + #[cfg(any(target_os = "linux", target_vendor = "apple"))] + #[test] + fn a_checked_write_keeps_the_old_copy_outside_a_folder_that_takes_no_new_file() { + use std::os::unix::fs::PermissionsExt; + let name = "checked-old-elsewhere.txt"; + let (d, root, _) = checked_folders("checked-old-elsewhere"); + let folder = root.join("inside"); + std::fs::write(folder.join(name), b"original, and longer").unwrap(); + let refused = std::cell::Cell::new(true); + let r = write_atomic_checked( + &folder.join(name), + |f| { + f.write_all(b"new")?; + RENAME_REFUSED.with(|c| c.set(true)); + refused.set(refuse_new_files(&folder)); + Ok(()) + }, + under(&root), + ); + RENAME_REFUSED.with(|c| c.set(false)); + std::fs::set_permissions(&folder, std::fs::Permissions::from_mode(0o755)).unwrap(); + if !refused.get() { + let _ = std::fs::remove_dir_all(d); + eprintln!("skipped: the folder takes new files whatever its mode (running as root)"); + return; + } + r.unwrap(); + assert_eq!(std::fs::read(folder.join(name)).unwrap(), b"new"); + assert!(!leftovers(&folder, name).iter().any(|n| n.ends_with(".old")), "{:?}", leftovers(&folder, name)); + assert!(system_temp_files(name).is_empty(), "{:?}", system_temp_files(name)); + let _ = std::fs::remove_dir_all(d); + } + + /// A folder that refuses the temporary file of a checked write but lets the file itself be + /// written: the content is written in the system's temporary folder and copied in, and nothing + /// is left behind. A failed fill there leaves the file as it was. (Skipped for root.) + #[cfg(any(target_os = "linux", target_vendor = "apple"))] + #[test] + fn a_checked_write_saves_through_the_system_folder_when_its_folder_takes_no_temporary_file() { + use std::os::unix::fs::PermissionsExt; + let name = "checked-no-temp.txt"; + let (d, root, _) = checked_folders("checked-no-temp"); + let folder = root.join("inside"); + std::fs::write(folder.join(name), b"original, and longer").unwrap(); + if !refuse_new_files(&folder) { + let _ = std::fs::remove_dir_all(d); + eprintln!("skipped: the folder takes new files whatever its mode (running as root)"); + return; + } + let saved = write_atomic_checked(&folder.join(name), |f| f.write_all(b"new"), under(&root)); + let saved_content = std::fs::read(folder.join(name)).unwrap(); + let saved_temp = system_temp_files(name); + let failed = write_atomic_checked( + &folder.join(name), + |f| { + f.write_all(b"half a fi")?; + Err(io::Error::other("disk full")) + }, + under(&root), + ); + std::fs::set_permissions(&folder, std::fs::Permissions::from_mode(0o755)).unwrap(); + saved.unwrap(); + assert_eq!(saved_content, b"new"); + assert!(saved_temp.is_empty(), "{saved_temp:?}"); + assert_eq!(failed.map_err(|e| e.to_string()), Err("disk full".to_string())); + assert_eq!(std::fs::read(folder.join(name)).unwrap(), b"new"); + assert!(leftovers(&folder, name).is_empty(), "{:?}", leftovers(&folder, name)); + assert!(system_temp_files(name).is_empty(), "{:?}", system_temp_files(name)); + let _ = std::fs::remove_dir_all(d); + } + /// The other files in `d` besides `keep`. fn leftovers(d: &Path, keep: &str) -> Vec { std::fs::read_dir(d).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().to_string()).filter(|n| n != keep).collect() diff --git a/crates/format/src/unpack.rs b/crates/format/src/unpack.rs index ec9623118c..e636aadf74 100644 --- a/crates/format/src/unpack.rs +++ b/crates/format/src/unpack.rs @@ -374,6 +374,36 @@ mod tests { assert!(matches!(Unpacked::within(&gz, json.len() as u64 - 1), Err(FormatError::TooBig))); } + /// A compressed file with data after its JSON is refused past its budget on both paths: the + /// JSON before the marker where the writer puts it, and the whole file when the marker is + /// elsewhere and the file is unpacked whole. + #[test] + fn a_compressed_file_with_data_is_capped_on_both_paths() { + let with_data = |json: &[u8]| { + let mut file = json.to_vec(); + file.extend(crate::BLOB_MAGIC); + file.extend([1u8; 64]); + file + }; + let mut json = br#"{"pad":""#.to_vec(); + json.extend(std::iter::repeat_n(b'x', 4096)); + json.extend(br#""}"#); + let streamed = gzip(&with_data(&json)); + let max_json = json.len() as u64; + let opened = Unpacked::within(&streamed, max_json).unwrap(); + assert!(matches!(opened.tail, Tail::Stream { .. }), "read as a stream"); + assert_eq!(opened.json(), json.as_slice()); + assert!(matches!(Unpacked::within(&streamed, max_json - 1), Err(FormatError::TooBig))); + // No `}` before the marker: unpacked whole, within the whole-file cap and not one byte past it. + let plain = with_data(b"1"); + let whole = gzip(&plain); + let max_whole = plain.len() as u64; + let opened = Unpacked::within_capped(&whole, 64, max_whole, MAX_BLOBS).unwrap(); + assert!(matches!(opened.tail, Tail::Whole(_)), "unpacked whole"); + assert_eq!(opened.json(), b"1"); + assert!(matches!(Unpacked::within_capped(&whole, 64, max_whole - 1, MAX_BLOBS), Err(FormatError::TooBig))); + } + #[test] fn entries_sharing_bytes_obey_the_limits_in_memory_too() { // Each entry within MAX_BLOB, together past MAX_BLOBS: many references to the same bytes.