Files

131 lines
4.7 KiB
Rust

//! Mandatory test 5 (save safety): a write killed mid-way leaves the original intact and the .bak valid.
//! The test binary re-runs itself as the child (PICTOR_SAVE_CHILD set), which stops inside a save and is killed.
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};
const ORIGINAL: &[u8] = b"%PDF-1.7 original content that must survive";
const OLD_BAK: &[u8] = b"%PDF-1.7 the previous backup";
fn new_content() -> Vec<u8> {
let mut v = b"%PDF-1.7 new content ".to_vec();
v.resize(4 << 20, b'x');
v
}
/// The child half: started by `run_child`, never run directly (it waits forever inside the save).
#[test]
fn child() {
let Ok(dir) = std::env::var("PICTOR_SAVE_CHILD") else { return };
let stage = std::env::var("PICTOR_SAVE_STAGE").unwrap();
let dir = PathBuf::from(dir);
let target = dir.join("doc.pdf");
let ready = dir.join("ready");
let stop = || {
std::fs::write(&ready, b"1").unwrap();
loop {
std::thread::sleep(Duration::from_secs(60));
}
};
let content = new_content();
let _ = pictor_save::save_overwrite_hooked(
&target,
|f| {
use std::io::Write;
f.write_all(&content[..content.len() / 2])?;
f.flush()?;
if stage == "content" {
stop();
}
f.write_all(&content[content.len() / 2..])
},
|| {
if stage == "bak" {
stop();
}
},
);
}
fn run_child(dir: &Path, stage: &str) {
let exe = std::env::current_exe().unwrap();
let mut child = Command::new(exe)
.args(["--exact", "child", "--nocapture", "--test-threads=1"])
.env("PICTOR_SAVE_CHILD", dir)
.env("PICTOR_SAVE_STAGE", stage)
.spawn()
.unwrap();
let started = Instant::now();
while !dir.join("ready").exists() {
assert!(started.elapsed() < Duration::from_secs(30), "child never reached the stage");
std::thread::sleep(Duration::from_millis(20));
}
child.kill().unwrap();
child.wait().unwrap();
}
fn setup(with_old_bak: bool) -> tempfile::TempDir {
let dir = tempfile::tempdir_in(env!("CARGO_TARGET_TMPDIR")).unwrap();
std::fs::write(dir.path().join("doc.pdf"), ORIGINAL).unwrap();
if with_old_bak {
std::fs::write(dir.path().join("doc.pdf.bak"), OLD_BAK).unwrap();
}
dir
}
#[test]
fn killed_while_writing_new_content_keeps_original_and_bak() {
let dir = setup(false);
run_child(dir.path(), "content");
assert_eq!(std::fs::read(dir.path().join("doc.pdf")).unwrap(), ORIGINAL, "original changed");
assert_eq!(std::fs::read(dir.path().join("doc.pdf.bak")).unwrap(), ORIGINAL, ".bak is not the original");
}
#[test]
fn killed_while_writing_bak_keeps_original_and_old_bak() {
let dir = setup(true);
run_child(dir.path(), "bak");
assert_eq!(std::fs::read(dir.path().join("doc.pdf")).unwrap(), ORIGINAL, "original changed");
let bak = std::fs::read(dir.path().join("doc.pdf.bak")).unwrap();
assert!(bak == OLD_BAK || bak == ORIGINAL, ".bak is neither the old backup nor the original");
}
#[test]
fn a_finished_overwrite_replaces_the_file_and_keeps_a_bak() {
let dir = setup(true);
let target = dir.path().join("doc.pdf");
let content = new_content();
let bak = pictor_save::save_overwrite(&target, &content).unwrap();
assert_eq!(std::fs::read(&target).unwrap(), content);
assert_eq!(bak, dir.path().join("doc.pdf.bak"));
assert_eq!(std::fs::read(&bak).unwrap(), ORIGINAL);
let leftovers: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.map(|e| e.unwrap().file_name().into_string().unwrap())
.filter(|n| n != "doc.pdf" && n != "doc.pdf.bak")
.collect();
assert!(leftovers.is_empty(), "temporary files left behind: {leftovers:?}");
}
#[test]
fn save_new_never_overwrites_and_names_the_copy() {
let dir = setup(false);
let target = dir.path().join("doc.pdf");
let first = pictor_save::edited_path(&target);
assert_eq!(first, dir.path().join("doc-edited.pdf"));
pictor_save::save_new(&first, b"one").unwrap();
assert!(pictor_save::save_new(&first, b"two").is_err(), "save_new clobbered an existing file");
assert_eq!(std::fs::read(&first).unwrap(), b"one");
assert_eq!(pictor_save::edited_path(&target), dir.path().join("doc-edited-2.pdf"));
assert_eq!(std::fs::read(&target).unwrap(), ORIGINAL);
}
#[test]
fn overwrite_of_a_missing_file_makes_no_bak() {
let dir = tempfile::tempdir_in(env!("CARGO_TARGET_TMPDIR")).unwrap();
let target = dir.path().join("fresh.png");
let bak = pictor_save::save_overwrite(&target, b"png").unwrap();
assert_eq!(std::fs::read(&target).unwrap(), b"png");
assert!(!bak.exists());
}