aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation')
-rw-r--r--src/federation/held.rs220
-rw-r--r--src/federation/store/memory.rs54
2 files changed, 253 insertions, 21 deletions
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<u8> {
@@ -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<u8>, 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<u32, String> = 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<u32> = 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");
+ }
+ }
}
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs
index b8035d7..bc0c672 100644
--- a/src/federation/store/memory.rs
+++ b/src/federation/store/memory.rs
@@ -805,6 +805,47 @@ mod tests {
assert_eq!(anew.own(), (own, drops));
}
+ /// Heads for a checkpoint before the store's, or one in another's history: no move
+ /// (`docs/store.md`, Attacks, 25).
+ #[test]
+ fn store_attack_25() {
+ let (mut store, cut, _, first) = cut_at_a_lock();
+ let checkpoint = store.checkpoint().to_vec();
+ store.offer(testfold::cut(&[cut], 2, &[first]));
+ assert_eq!(store.checkpoint(), checkpoint.as_slice());
+ let mut store = self::store();
+ let root = store.id();
+ let one = taken(store.offer(testfold::put(&[root], "a", "1")));
+ let two = taken(store.offer(testfold::put(&[one], "a", "2")));
+ let checkpoint = store.checkpoint().to_vec();
+ store.offer(testfold::cut(&[two], 1, &[one, two]));
+ assert_eq!(store.checkpoint(), checkpoint.as_slice());
+ }
+
+ /// A head brought again after a move: taken as held, and not read anew; so is what
+ /// came before it (`docs/store.md`, Attacks, 26).
+ #[test]
+ fn store_attack_26() {
+ let (mut store, _, lock, first) = cut_at_a_lock();
+ let root = store.id();
+ let second = Id::of(&testfold::put(&[first], "a", "2"));
+ let state = store.state().clone();
+ assert_eq!(store.offer(testfold::lock(&[second], "b")), Offered::Held);
+ assert_eq!(Id::of(&testfold::lock(&[second], "b")), lock);
+ assert_eq!(store.offer(testfold::put(&[root], "a", "1")), Offered::Held);
+ assert!(store.state() == &state);
+ }
+
+ /// An operator has its node drop an event its checkpoint keeps, or one of its heads:
+ /// refused (`docs/store.md`, Attacks, 27).
+ #[test]
+ fn store_attack_27() {
+ let (mut store, _, lock, first) = cut_at_a_lock();
+ let second = Id::of(&testfold::put(&[first], "a", "2"));
+ assert!(store.drop_taken(&[second]).is_err());
+ assert!(store.drop_taken(&[lock]).is_err());
+ }
+
/// Heads leave out what is neither among them, in their history, nor above them.
#[test]
fn heads_leave_out_what_is_beside_them() {
@@ -869,15 +910,6 @@ mod tests {
store.offer(testfold::put(&[root], "c", "1")),
Offered::Refused(Refusal::NotAbove)
);
- // A head brought again is held; so is what came before it.
- assert_eq!(
- store.offer(testfold::lock(
- &[Id::of(&testfold::put(&[first], "a", "2"))],
- "b"
- )),
- Offered::Held
- );
- assert_eq!(store.offer(testfold::put(&[root], "a", "1")), Offered::Held);
}
/// The checkpoint a store builds at heads it holds is the one it moves to there.
@@ -932,8 +964,10 @@ mod tests {
);
}
+ /// A fork's events brought to the store it came from, or the other way: never taken
+ /// (`docs/store.md`, Attacks, 9).
#[test]
- fn a_fork_and_the_store_it_left_take_nothing_of_each_other() {
+ fn store_attack_9() {
let mut store = store();
let root = store.id();
let here = taken(store.offer(testfold::put(&[root], "a", "1")));