test(store): damaged pieces are repaired, a drill never passes on an older backup, books ahead and diverged, a disk with no links, headers, sync stand-ins, a destination of 3000 backups

Each test fails without the change it covers: the reviewer's sequence (a
byte flipped in backup 2's segment, more backups, a drill, a restore), a
damaged stored file found by the rotating share, a drill that must fail
when only an older manifest is whole, ahead told from diverged with a new
set beside the old, the no-link path (and two writers racing on it), sealed
pieces in folders, header settings capped and authenticated, one key
derivation per setting, files not downloaded yet, vault scratch files, and
a destination of 3000 backups made cheaply (12 MB of manifests, 0.8 KB for
the newest, 32 ms to find and read it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 19:21:34 -07:00
1 parent 6a8dc6c88b
commit 9ac47a3f10
8 files changed
+1578 -51

No files matched your search

+10 -2
View File
@@ -382,6 +382,7 @@ impl SetIo for DirSet {
}
fn len(&self, rel: &str) -> std::io::Result<Option<u64>> {
self.ctx.traced("len", rel);
match std::fs::symlink_metadata(self.path(rel)) {
// A link is not a file of a backup: it could lead anywhere.
Ok(m) if m.is_file() && online_only(&m) => Err(carry(BackupError::NotDownloaded)),
@@ -403,6 +404,7 @@ impl SetIo for DirSet {
rel: &str,
f: &mut dyn FnMut(&mut dyn Read) -> std::io::Result<()>,
) -> std::io::Result<()> {
self.ctx.traced("read", rel);
let path = self.path(rel);
let meta = match std::fs::symlink_metadata(&path) {
Ok(m) => m,
@@ -443,7 +445,11 @@ impl SetIo for DirSet {
fill(&mut w)?;
w.flush()?;
}
file.sync_all()
if self.ctx.syncs() {
file.sync_all()
} else {
Ok(())
}
})();
if let Err(e) = written {
// Something of ours that failed on its own: its scratch file
@@ -474,7 +480,9 @@ impl SetIo for DirSet {
return Err(e);
}
};
sync_dir(&parent);
if self.ctx.syncs() {
sync_dir(&parent);
}
self.ctx.point("published", rel).map_err(carry)?;
Ok(put)
}
+35 -1
View File
@@ -67,12 +67,14 @@ mod io;
mod read;
mod seal;
mod single;
#[cfg(test)]
mod tests;
mod write;
pub use cut::Cut;
pub use read::list;
pub use read::{Candidate, Found, Verified, drill, find, prove_local, verify_found};
pub use seal::BackupKey;
pub use seal::{BackupKey, key_derivations};
pub use single::{FILE_EXTENSION, save_file};
pub use write::{
Destination, Options, Written, holds_only_sealed, set_name_for, write_destination,
@@ -223,8 +225,16 @@ pub struct Ctx {
/// refuse to replace.
no_links: bool,
no_noreplace: bool,
/// Tests: nothing is synced to disk (a destination of thousands of
/// backups is made quickly; a real one never is).
no_sync: bool,
/// Tests: told each read and length look at a file of the set.
trace: Option<Trace>,
}
/// A test's look at which files of a set are read or measured.
pub type Trace = Arc<dyn Fn(&str, &str) + Send + Sync>;
impl std::fmt::Debug for Ctx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Ctx")
@@ -267,6 +277,30 @@ impl Ctx {
self
}
/// Told, in tests, each `read` and `len` of a file of a set.
#[doc(hidden)]
pub fn with_trace(mut self, trace: Trace) -> Self {
self.trace = Some(trace);
self
}
pub(crate) fn traced(&self, kind: &str, rel: &str) {
if let Some(t) = &self.trace {
t(kind, rel);
}
}
/// Nothing is synced to disk (tests that make thousands of backups).
#[doc(hidden)]
pub fn with_no_sync(mut self) -> Self {
self.no_sync = true;
self
}
pub(crate) fn syncs(&self) -> bool {
!self.no_sync
}
pub(crate) fn refuses_links(&self) -> bool {
self.no_links
}
+21 -31
View File
@@ -510,28 +510,27 @@ impl Set {
))),
}
}
}
/// The index of stored files a manifest names.
pub(crate) fn index(&self, m: &Manifest) -> Result<Vec<IndexLine>, BackupError> {
let mut text = VecSink::default();
self.read_series(&m.files, Kind::Index, &mut text)?;
let mut out = Vec::new();
let mut seen = BTreeSet::new();
for line in text.bytes.split(|b| *b == b'\n').filter(|l| !l.is_empty()) {
let l: IndexLine = serde_json::from_slice(line).map_err(|_| {
BackupError::Damaged("the backup's index of stored files is damaged".into())
})?;
if !is_sha(&l.sha256) {
return Err(BackupError::Damaged(
"the backup's index of stored files names a file by no hash".into(),
));
}
if seen.insert(l.sha256.clone()) {
out.push(l);
}
/// The index of stored files in `text` (one `{"sha256","bytes"}` per
/// line), each file once.
pub(crate) fn parse_index(text: &[u8]) -> Result<Vec<IndexLine>, BackupError> {
let mut out = Vec::new();
let mut seen = BTreeSet::new();
for line in text.split(|b| *b == b'\n').filter(|l| !l.is_empty()) {
let l: IndexLine = serde_json::from_slice(line).map_err(|_| {
BackupError::Damaged("the backup's index of stored files is damaged".into())
})?;
if !is_sha(&l.sha256) {
return Err(BackupError::Damaged(
"the backup's index of stored files names a file by no hash".into(),
));
}
if seen.insert(l.sha256.clone()) {
out.push(l);
}
Ok(out)
}
Ok(out)
}
fn short(sha: &str) -> &str {
@@ -825,18 +824,9 @@ fn materialize(
let mut index = BTreeSet::new();
let mut lines = Vec::new();
if index_damage.is_empty() {
for line in text.bytes.split(|b| *b == b'\n').filter(|l| !l.is_empty()) {
let l: IndexLine = serde_json::from_slice(line).map_err(|_| {
BackupError::Damaged("the backup's index of stored files is damaged".into())
})?;
if !is_sha(&l.sha256) {
return Err(BackupError::Damaged(
"the backup's index of stored files names a file by no hash".into(),
));
}
if index.insert(l.sha256.clone()) {
lines.push(l);
}
for l in parse_index(&text.bytes)? {
index.insert(l.sha256.clone());
lines.push(l);
}
}
damaged.extend(index_damage);
+11
View File
@@ -102,12 +102,23 @@ fn header_aad(kdf: &Kdf) -> Vec<u8> {
.into_bytes()
}
/// How many keys this program has derived from a passphrase (tests: a
/// folder of sealed sets costs one derivation per distinct setting).
static DERIVATIONS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// How many keys have been derived by this process.
#[doc(hidden)]
pub fn key_derivations() -> usize {
DERIVATIONS.load(std::sync::atomic::Ordering::SeqCst)
}
fn stretch(passphrase: &str, kdf: &Kdf) -> Result<Zeroizing<[u8; 32]>, BackupError> {
if !kdf_in_bounds(kdf) {
return Err(BackupError::Damaged(
"its header asks for a key this program won't derive".into(),
));
}
DERIVATIONS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let salt = hex::decode(&kdf.salt).expect("checked");
let params = Params::new(kdf.m, kdf.t, kdf.p, Some(32))
.map_err(|_| BackupError::Damaged("its header's key settings are not valid".into()))?;
+279
View File
@@ -0,0 +1,279 @@
//! Tests of the parts of a backup that need what the crate keeps to itself:
//! how a failed link is told from a disk that makes none, what a sync
//! service's stand-ins look like, the share of stored files each backup
//! re-reads, and a destination with thousands of backups.
use super::Ctx;
use super::cut::Cut;
use super::form::*;
use super::io::{dataless_flags, is_stand_in_name, link_unsupported, placeholder_attributes};
use super::read::{Set, in_rotation};
use super::write::{kept_form, write_into_set};
use std::io::Write;
use std::time::Instant;
#[test]
fn only_an_error_that_means_no_links_is_taken_to_mean_it() {
use std::io::{Error, ErrorKind};
assert!(link_unsupported(&Error::from(ErrorKind::Unsupported)));
// The name being taken, a missing folder, a refused permission and a
// full disk are what they say: never a reason to try a rename, which
// replaces.
for kind in [
ErrorKind::AlreadyExists,
ErrorKind::NotFound,
ErrorKind::PermissionDenied,
ErrorKind::StorageFull,
ErrorKind::Other,
] {
assert!(!link_unsupported(&Error::from(kind)), "{kind:?}");
}
#[cfg(unix)]
{
for n in [libc::EPERM, libc::ENOSYS, libc::EOPNOTSUPP, libc::ENOTSUP] {
assert!(link_unsupported(&Error::from_raw_os_error(n)), "errno {n}");
}
for n in [
libc::EACCES,
libc::ENOENT,
libc::EEXIST,
libc::ENOSPC,
libc::EIO,
] {
assert!(!link_unsupported(&Error::from_raw_os_error(n)), "errno {n}");
}
}
}
#[test]
fn what_a_sync_service_leaves_for_a_file_it_has_not_downloaded_is_known() {
// iCloud, before macOS 14: `.name.icloud` in the folder.
assert!(is_stand_in_name(".000002.json.icloud"));
assert!(is_stand_in_name(".a.icloud"));
assert!(!is_stand_in_name("000002.json"));
assert!(!is_stand_in_name(".wb-tmp-1-abc"));
assert!(!is_stand_in_name(".icloud"));
// Windows: offline (0x1000), recall on open (0x40000), recall on data
// access (0x400000); a plain file (archive, 0x20) and a read-only one
// (0x1) are not.
for attrs in [0x1000, 0x40000, 0x400000, 0x20 | 0x400000] {
assert!(placeholder_attributes(attrs), "{attrs:#x}");
}
for attrs in [0x20, 0x1, 0x80, 0x100] {
assert!(!placeholder_attributes(attrs), "{attrs:#x}");
}
// macOS: SF_DATALESS.
assert!(dataless_flags(0x4000_0000));
assert!(!dataless_flags(0x0004_0000));
}
#[test]
fn a_piece_belongs_to_one_share_and_every_share_is_read_within_sixteen_backups() {
use super::read::ROTATION;
let mut x = 0x2545_f491_4f6c_dd1du64;
for _ in 0..2000 {
// A pseudo-random hash.
let mut sha = String::new();
while sha.len() < 64 {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
sha.push_str(&format!("{x:016x}"));
}
sha.truncate(64);
for start in [1u64, 2, 17, 255, 3000] {
let reads = (start..start + ROTATION)
.filter(|n| in_rotation(&sha, *n))
.count();
assert_eq!(reads, 1, "{sha} from backup {start}");
}
}
}
#[test]
fn generations_and_set_names_round_trip() {
assert_eq!(generation_path("files/ab/xyz", 0), "files/ab/xyz");
assert_eq!(generation_path("files/ab/xyz", 2), "files/ab/xyz.g2");
assert_eq!(split_generation("files/ab/xyz.g2"), ("files/ab/xyz", 2));
assert_eq!(split_generation("files/ab/xyz"), ("files/ab/xyz", 0));
assert_eq!(
split_generation("journal/000001-abcd.g"),
("journal/000001-abcd.g", 0)
);
assert_eq!(plain_set_name("con", 1), "wanderbooks-con");
assert_eq!(plain_set_name("acme", 3), "wanderbooks-acme.3");
assert_eq!(parse_plain_set_name("wanderbooks-acme"), Some(("acme", 1)));
assert_eq!(
parse_plain_set_name("wanderbooks-acme.3"),
Some(("acme", 3))
);
// No book id holds a dot, so a later set is never another book's.
assert_eq!(parse_plain_set_name("wanderbooks-acme.1"), None);
assert_eq!(parse_plain_set_name("acme"), None);
}
/// A `Set` over a fresh folder, nothing synced.
fn fresh_set(root: &std::path::Path) -> (Set, std::path::PathBuf) {
let at = root.join("disk/wanderbooks-acme");
(Set::dir(at.clone(), None, &Ctx::new().with_no_sync()), at)
}
/// What a person's backups look like after a while, made by the real
/// writer: every manifest names every segment once read, but is kept as
/// the one before it and what is new, and a chain is cut at 64.
#[test]
fn the_writers_manifests_chain_to_a_bounded_depth_and_are_proven_link_by_link() {
const N: u64 = 150;
let dir = tempfile::tempdir().unwrap();
let book = dir.path().join("book");
std::fs::create_dir(&book).unwrap();
let ctx = Ctx::new().with_no_sync();
let (set, at) = fresh_set(dir.path());
// A journal of made-up lines: the writer never reads one, it hashes the
// bytes and takes the head from the caller. Each backup adds a line.
let mut journal = std::fs::File::create(book.join(crate::JOURNAL_FILE)).unwrap();
for i in 1..=N {
let line = format!("{{\"seq\":{i},\"pad\":\"{}\"}}", "x".repeat(60));
writeln!(journal, "{line}").unwrap();
journal.flush().unwrap();
let cut = Cut::take("acme", &book, (i, sha_hex(line.as_bytes()))).unwrap();
let w = write_into_set(&set, &cut, &ctx, false).unwrap();
assert!(w.new && w.number == i);
}
let mut depths = Vec::new();
for n in 1..=N {
let m = set.manifest(n).unwrap();
depths.push(m.depth);
assert_eq!(m.journal.segments.len() as u64, n, "manifest {n}");
let raw = std::fs::read_to_string(at.join(manifest_path(n, false))).unwrap();
let stored: Manifest = serde_json::from_str(raw.lines().next().unwrap()).unwrap();
assert_eq!(stored.depth == 0, stored.base.is_none());
// A stored manifest that chains names only what is new.
if stored.base.is_some() {
assert_eq!(stored.journal.segments.len(), 1, "manifest {n}");
assert_eq!(stored.journal.keep as u64, n - 1);
}
}
// Chains of 64: a manifest that names everything at 1, 65, 129.
assert_eq!(depths[0], 0);
assert_eq!(depths[63], 63);
assert_eq!(depths[64], 0);
assert_eq!(depths[128], 0);
assert!(depths.iter().all(|d| *d < DELTA_CHAIN));
// Each link is proven by the hash its successor holds: a manifest in
// the chain that was changed makes the ones built on it unreadable, and
// the reader takes the newest that still reads, saying how many it
// passed.
let k = N - 5;
let bad = at.join(manifest_path(k, false));
let mut bytes = std::fs::read(&bad).unwrap();
let i = bytes.len() / 3;
bytes[i] = if bytes[i] == b'0' { b'1' } else { b'0' };
std::fs::write(&bad, bytes).unwrap();
assert!(matches!(
set.manifest(N),
Err(super::BackupError::Damaged(_))
));
let (m, passed) = set.newest_whole().unwrap().unwrap();
assert_eq!((m.number, passed as u64), (k - 1, N - k + 1));
assert_eq!(m.journal.segments.len() as u64, k - 1);
}
/// A destination of thousands of backups, made cheaply: the real forms
/// (`kept_form`, `encode_manifest`) written straight into a folder with a
/// one-byte file for each segment, no books and no syncing, then read as
/// a restore reads it. The time and size are printed (the numbers the
/// docs quote) and bounded.
#[test]
fn a_destination_of_thousands_of_backups_stays_small_and_quick_to_read() {
const N: u64 = 3000;
let dir = tempfile::tempdir().unwrap();
let (set, at) = fresh_set(dir.path());
std::fs::create_dir_all(at.join("journal")).unwrap();
std::fs::create_dir_all(at.join(MANIFESTS)).unwrap();
let made = Instant::now();
let mut full = Manifest {
format: FORMAT.into(),
version: VERSION,
book: "acme".into(),
number: 0,
written: "2026-11-21T00:00:00Z".into(),
head: Head {
seq: 0,
hash: String::new(),
},
journal: Series::default(),
access: Series::default(),
files: Series::default(),
file_count: 0,
file_bytes: 0,
vault: Vec::new(),
keys: Vec::new(),
base: None,
depth: 0,
sha: String::new(),
};
let mut prev: Option<Manifest> = None;
for i in 1..=N {
let name = format!("journal/{i:06}-{i:016x}");
std::fs::write(at.join(&name), b"x").unwrap();
full.journal.segments.push(Piece {
file: name,
stored: 1,
bytes: 1,
sha256: format!("{i:064x}"),
});
full.journal.bytes = i;
full.journal.sha256 = format!("{i:064x}");
full.number = i;
full.head = Head {
seq: i,
hash: format!("{i:064x}"),
};
let kept = kept_form(&full, prev.as_ref());
let bytes = encode_manifest(&kept);
std::fs::write(at.join(manifest_path(i, false)), &bytes).unwrap();
let mut view = full.clone();
view.depth = kept.depth;
view.sha = decode_manifest(&bytes).unwrap().sha;
prev = Some(view);
}
let made = made.elapsed();
let sizes: Vec<u64> = (1..=N)
.map(|n| {
std::fs::metadata(at.join(manifest_path(n, false)))
.unwrap()
.len()
})
.collect();
let total: u64 = sizes.iter().sum();
let largest = *sizes.iter().max().unwrap();
let newest = *sizes.last().unwrap();
// What it would have been if every manifest named everything: a
// journal segment is ~170 bytes of a manifest.
let everything: u64 = (1..=N).map(|n| 170 * n).sum();
// Read as a restore reads it: the newest found, and whole.
let began = Instant::now();
let (m, passed) = set.newest_whole().unwrap().unwrap();
let found_in = began.elapsed();
assert_eq!((m.number, passed), (N, 0));
assert_eq!(m.journal.segments.len() as u64, N);
let began = Instant::now();
let numbers = set.numbers().unwrap().len();
let listed_in = began.elapsed();
assert_eq!(numbers as u64, N);
println!(
"{N} backups: manifests {total} bytes in all (named in full each time: ~{everything}), largest {largest}, newest {newest}; made in {made:?}; newest found and whole in {found_in:?}; listed in {listed_in:?}"
);
assert!(newest < 6 * 1024, "the newest manifest is {newest} bytes");
assert!(total < everything / 20, "{total} of ~{everything}");
assert!(found_in.as_secs() < 10, "{found_in:?}");
// Any manifest in it reads whole (a sample: each read walks a chain).
for n in (1..=N).step_by(131).chain([N - 1]) {
let m = set.manifest(n).unwrap();
assert_eq!(m.journal.segments.len() as u64, n);
}
}
+20 -17
View File
@@ -519,9 +519,12 @@ fn plan(
cut: &Cut,
distrust: &BTreeSet<String>,
ctx: &Ctx,
) -> Result<(Manifest, Vec<Plan>), BackupError> {
) -> Result<(Manifest, Vec<Plan>, Vec<String>), BackupError> {
let keys = set.keys.as_ref();
let mut plans = Vec::new();
// Damaged pieces this plan comes upon without being asked to repair
// them (the segments of an index it can't read, which it begins again).
let mut noticed: Vec<String> = Vec::new();
if let Some(p) = prev {
if p.book != cut.book() {
return Err(BackupError::Elsewhere);
@@ -570,18 +573,15 @@ fn plan(
let mut restart = false;
let known: Vec<IndexLine> = match prev {
Some(p) => {
if p.files.segments.iter().any(|s| distrust.contains(&s.file)) {
let mut text = VecSinkText::default();
let damaged = set.read_series_lenient(&p.files, Kind::Index, &mut text)?;
let named_bad = p.files.segments.iter().any(|s| distrust.contains(&s.file));
if !damaged.is_empty() || named_bad {
restart = true;
noticed.extend(damaged.into_iter().map(|d| d.file));
Vec::new()
} else {
match set.index(p) {
Ok(k) => k,
Err(BackupError::Damaged(_)) => {
restart = true;
Vec::new()
}
Err(e) => return Err(e),
}
super::read::parse_index(&text.0)?
}
}
None => Vec::new(),
@@ -694,7 +694,7 @@ fn plan(
depth: 0,
sha: String::new(),
};
Ok((manifest, plans))
Ok((manifest, plans, noticed))
}
/// Text gathered as a series is read.
@@ -715,7 +715,7 @@ impl Sink for VecSinkText {
/// The manifest as it is kept: when `prev` (whole, as read) is a link in a
/// chain that is not yet [`DELTA_CHAIN`] long, built on it, naming only
/// what each series adds to its segments. Otherwise it names everything.
fn kept_form(full: &Manifest, prev: Option<&Manifest>) -> Manifest {
pub(crate) fn kept_form(full: &Manifest, prev: Option<&Manifest>) -> Manifest {
let mut m = full.clone();
let Some(p) = prev.filter(|p| p.depth + 1 < DELTA_CHAIN && !p.sha.is_empty()) else {
return m;
@@ -825,10 +825,11 @@ fn prove_and_repair(
cut: &Cut,
mut number: u64,
mut added: BTreeSet<String>,
noticed: Vec<String>,
ctx: &Ctx,
) -> Result<Written, BackupError> {
let mut distrust: BTreeSet<String> = BTreeSet::new();
let mut repaired: Vec<String> = Vec::new();
let mut repaired: Vec<String> = noticed;
for round in 0..=REPAIR_ROUNDS {
ctx.phase("proof");
ctx.check("proving the backup")?;
@@ -855,7 +856,7 @@ fn prove_and_repair(
// them (the stored files are found by their hash and generation).
ctx.phase("repair");
let prev = set.manifest(number)?;
let (mut manifest, plans) = plan(set, Some(&prev), cut, &distrust, ctx)?;
let (mut manifest, plans, _) = plan(set, Some(&prev), cut, &distrust, ctx)?;
added = BTreeSet::new();
for p in &plans {
ctx.check("backing up")?;
@@ -898,7 +899,7 @@ pub(crate) fn write_into_set(
let prev = set.newest_whole()?.map(|(m, _)| m);
let next = numbers.last().map_or(1, |n| n + 1);
ctx.phase("plan");
let (mut manifest, plans) = plan(set, prev.as_ref(), cut, &BTreeSet::new(), ctx)?;
let (mut manifest, plans, noticed) = plan(set, prev.as_ref(), cut, &BTreeSet::new(), ctx)?;
if let Some(p) = &prev
&& manifest.same_content(p)
{
@@ -921,13 +922,15 @@ pub(crate) fn write_into_set(
}
}
match put_manifest(set, &mut manifest, prev.as_ref(), next, ctx)? {
Some(number) if prove => return prove_and_repair(set, cut, number, added, ctx),
Some(number) if prove => {
return prove_and_repair(set, cut, number, added, noticed, ctx);
}
Some(number) => {
return Ok(Written {
number,
head: cut.head().clone(),
new: true,
repaired: Vec::new(),
repaired: noticed,
files_proven: 0,
});
}