From 65e9d86e48e3fd182882ceb54b9b8b1ff475369d Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 22:47:30 +0000 Subject: store: a test per attack row marked unit Each row of `docs/store.md`, Attacks, marked unit has its test, `store_attack_`: where it is judged (`access::holding`, `access::token`), where the store held in memory meets it (`federation::store::memory`), and where a node reads its store from its document (`federation::held`). Tests that checked a row already take its name; a check reads the doc's rows and fails on a unit row without its test, but for rows 21 and 22, which the remakes bring. --- src/federation/held.rs | 220 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 209 insertions(+), 11 deletions(-) (limited to 'src/federation/held.rs') diff --git a/src/federation/held.rs b/src/federation/held.rs index f48778d..3c8bafb 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -790,10 +790,10 @@ async fn put_again(state: &crate::state::AppState) { #[cfg(test)] mod tests { use super::*; - use crate::document::typed::{TimestampFormat, Val}; - use crate::document::{Document, ScalarValue}; + use crate::document::Document; use crate::federation::event::Checkpoint; use crate::federation::harness::{self, Party, Spec}; + use crate::federation::store::memory::Refusal; use crate::federation::version::Version; fn root_of(spec: &Spec) -> Vec { @@ -806,15 +806,7 @@ mod tests { } fn put(doc: &mut Document, event: &[u8]) { - crate::document::testing::change(doc, |doc| { - store::write_val_path( - doc, - &events::event(&Id::of(event)), - &Val::Scalar(ScalarValue::Bytes(event.to_vec())), - store::Writing::sticky(TimestampFormat::default()), - ) - .unwrap(); - }); + events::put(doc, &[event]); } fn read(holding: &Holding, doc: &Document, epoch_seq: u32) { @@ -1034,4 +1026,210 @@ mod tests { std::fs::write(&path, b"{\"refused\": [\"not an ID\"]}").unwrap(); assert!(Holding::open(Some(path), files, None, None).is_err()); } + + // The store's attack rows (`docs/store.md`, Attacks) read from a document. + + /// tv's federation, its root, and a document started from it. + fn tv_alone() -> (Party, Vec, Document) { + let tv = Party::new("tv", 1); + let root = root_of(&Spec::first(&[&tv], &[])); + let doc = events::started(Federation::NAME, &root).unwrap(); + (tv, root, doc) + } + + /// An event whose dependencies never come: taken by no node; it stays in the document. + #[test] + fn store_attack_3() { + let (tv, _, mut doc) = tv_alone(); + let on = tv.write(&[Id::of(b"never")], "home/tv/x", harness::str("1")); + put(&mut doc, &on); + let holding = Holding::default(); + read(&holding, &doc, 0); + assert_eq!( + holding.with(|store| store.is_waiting(&Id::of(&on))), + Some(true) + ); + assert!(events::ids(&doc).contains(&Id::of(&on))); + } + + /// The same event put by two changes at once, or again later: one event. + #[test] + fn store_attack_5() { + let (tv, root, mut doc) = tv_alone(); + let write = tv.write(&[Id::of(&root)], "home/tv/x", harness::str("1")); + let mut other = doc.fork(); + put(&mut doc, &write); + put(&mut other, &write); + doc.merge(&mut other).unwrap(); + assert_eq!(events::ids(&doc).len(), 2); + let holding = Holding::default(); + read(&holding, &doc, 0); + read(&holding, &doc.fork(), 1); + assert_eq!( + holding.with(|store| store.taken().to_vec()), + Some(vec![Id::of(&write)]) + ); + } + + /// An event the fold refuses, beside others in one change: the store takes the others, + /// and not that one, nor what is built on it. + #[test] + fn store_attack_6() { + let (tv, root, mut doc) = tv_alone(); + let good = tv.write(&[Id::of(&root)], "home/tv/x", harness::str("1")); + let bad = tv.write(&[Id::of(&root)], "elsewhere/x", harness::str("1")); + let on_bad = tv.write(&[Id::of(&bad)], "home/tv/y", harness::str("1")); + events::put(&mut doc, &[&good, &bad, &on_bad]); + let holding = Holding::default(); + read(&holding, &doc, 0); + assert_eq!( + holding.with(|store| store.taken().to_vec()), + Some(vec![Id::of(&good)]) + ); + assert!(matches!( + holding.with(|store| store.refusal(&Id::of(&on_bad)).cloned()), + Some(Some(Refusal::BuiltOn(_))) + )); + } + + /// Bytes that are none of the fold's events, or a root other than the store's: refused. + #[test] + fn store_attack_8() { + let (_, _, mut doc) = tv_alone(); + let junk = b"{\"not\":\"an event\"}".to_vec(); + let other = root_of(&Spec::first(&[&Party::new("lass", 2)], &[])); + put(&mut doc, &junk); + put(&mut doc, &other); + let holding = Holding::default(); + read(&holding, &doc, 0); + for bytes in [&junk, &other] { + assert_eq!( + holding.with(|store| store.refusal(&Id::of(bytes)).cloned()), + Some(Some(Refusal::None)) + ); + } + assert_eq!(holding.with(|store| store.taken().len()), Some(0)); + } + + /// A node whose operator had it drop an event is sent it again, in another change, + /// before or after a restart: not taken, with everything built on it. + #[test] + fn store_attack_20() { + let (tv, root, mut doc) = tv_alone(); + let write = tv.write(&[Id::of(&root)], "home/tv/x", harness::str("1")); + let on = tv.write(&[Id::of(&write)], "home/tv/x", harness::str("2")); + put(&mut doc, &write); + let dir = crate::files::scratch_dir("held-attack-20"); + let path = dir.join("store.json"); + let files = Arc::new(Files::default()); + let holding = Holding::open(Some(path.clone()), Arc::clone(&files), None, None).unwrap(); + read(&holding, &doc, 0); + holding.drop_taken(&[Id::of(&write)]).unwrap().unwrap(); + // Another document, the event in another change. + let mut again = events::started(Federation::NAME, &root).unwrap(); + events::put(&mut again, &[&write, &on]); + read(&holding, &again, 1); + assert_eq!(holding.with(|store| store.taken().len()), Some(0)); + let restarted = Holding::open(Some(path), files, None, None).unwrap(); + read(&restarted, &again, 0); + assert_eq!(restarted.with(|store| store.taken().len()), Some(0)); + } + + /// An event not above the heads of the checkpoint a node moved to: not taken; it stays + /// in the document. + #[test] + fn store_attack_23() { + let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); + let makefu = Party::new("makefu", 3); + let first = Spec::first(&[&tv, &lass, &makefu], &[]); + let root = root_of(&first); + let on = first.hash(); + let head = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1")); + let mut next = first.after(on); + next.checkpoint = Some(vec![Id::of(&head)]); + let proposal = tv.propose(&[Id::of(&head)], &next.json()); + let votes = [ + tv.vote(&[Id::of(&proposal)], on, next.hash(), true), + lass.vote(&[Id::of(&proposal)], on, next.hash(), true), + ]; + let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], next.hash()); + let mut doc = events::started(Federation::NAME, &root).unwrap(); + for event in [&head, &proposal] + .into_iter() + .chain(&votes) + .chain([&written]) + { + put(&mut doc, event); + } + let holding = Holding::default(); + read(&holding, &doc, 0); + assert_ne!( + holding.with(|store| Id::of(store.checkpoint())), + Some(Id::of(&root)) + ); + let beside = makefu.write(&[Id::of(&root)], "home/makefu/b", harness::str("1")); + put(&mut doc, &beside); + read(&holding, &doc, 0); + assert_eq!( + holding.with(|store| store.refusal(&Id::of(&beside)).cloned()), + Some(Some(Refusal::NotAbove)) + ); + assert!(events::ids(&doc).contains(&Id::of(&beside))); + } + + /// Rows the next stages test. + const LATER: &[u32] = &[21, 22]; + + #[test] + fn every_row_marked_unit_has_its_test() { + let doc = include_str!("../../docs/store.md"); + let attacks = doc.split("\n## Attacks").nth(1).expect("its attacks"); + let attacks = attacks.split("\n## ").next().unwrap_or(attacks); + let sources = [ + include_str!("held.rs"), + include_str!("store/memory.rs"), + include_str!("../access/holding.rs"), + include_str!("../access/token.rs"), + ]; + let rows: BTreeMap = attacks + .lines() + .filter_map(|line| { + let cells: Vec<&str> = line.split('|').map(str::trim).collect(); + let n = cells.get(1)?.parse().ok()?; + Some((n, cells.get(4)?.to_string())) + }) + .collect(); + let tested: Vec = sources + .iter() + .flat_map(|source| source.lines()) + .filter_map(|line| { + line.trim() + .strip_prefix("fn store_attack_")? + .split('(') + .next()? + .parse() + .ok() + }) + .collect(); + let missing: Vec<&u32> = rows + .iter() + .filter(|(n, kind)| { + kind.starts_with("unit") && !tested.contains(n) && !LATER.contains(n) + }) + .map(|(n, _)| n) + .collect(); + assert!(missing.is_empty(), "rows without their test: {missing:?}"); + for n in &tested { + assert!(rows.contains_key(n), "a test of row {n}, which isn't there"); + assert_eq!( + tested.iter().filter(|m| *m == n).count(), + 1, + "row {n} tested twice" + ); + } + for n in LATER { + assert!(!tested.contains(n), "row {n} has its test now"); + assert!(rows[n].starts_with("unit"), "row {n} isn't marked unit"); + } + } } -- cgit v1.3.1