//! What the federation's tests build with (`docs/federation-tests.md`, The harness): //! parties with keys made from seeds, signing events as the design says, and versions. use ed25519_dalek::SigningKey; use super::event; use super::jcs::Json; use super::store::Id; use crate::access::sshsig::PublicKey; /// A party of the tests: a name and a key, made from a seed. #[derive(Clone)] pub(crate) struct Party { pub(crate) name: String, pub(crate) key: SigningKey, } /// A key made from a seed, spelled as the federation spells one. pub(crate) fn key_line(seed: u8) -> String { PublicKey( SigningKey::from_bytes(&[seed; 32]) .verifying_key() .to_bytes(), ) .line() } /// A node ID made from a seed. pub(crate) fn node_id(seed: u8) -> String { crate::identity::node_id_of( &SigningKey::from_bytes(&[seed.wrapping_add(128); 32]) .verifying_key() .to_bytes(), ) } pub(crate) fn str(text: &str) -> Json { Json::Str(text.to_owned()) } /// The JSON of IDs, as an event or a version names them. pub(crate) fn ids(ids: &[Id]) -> Json { let mut ids = ids.to_vec(); ids.sort(); ids.dedup(); Json::Array(ids.iter().map(|id| Json::Str(id.to_string())).collect()) } pub(crate) fn object(members: Vec<(&str, Json)>) -> Json { Json::object( members .into_iter() .map(|(name, value)| (name.to_owned(), value)) .collect(), ) .expect("names once each") } impl Party { pub(crate) fn new(name: &str, seed: u8) -> Party { Party { name: name.to_owned(), key: SigningKey::from_bytes(&[seed; 32]), } } /// Its key, spelled as the federation spells one. pub(crate) fn key_line(&self) -> String { PublicKey(self.key.verifying_key().to_bytes()).line() } /// An event it signs, on `deps`, doing `does` (a field's name, and what it holds), with /// `key` its key, signed with `signer`: the two differ only where a test wants them to. pub(crate) fn signed_as( &self, signer: &SigningKey, key: &str, deps: &[Id], does: (&str, Json), ) -> Vec { event::signed(signer, &self.name, key, deps, does) } pub(crate) fn sign(&self, deps: &[Id], does: (&str, Json)) -> Vec { self.signed_as(&self.key, &self.key_line(), deps, does) } pub(crate) fn write(&self, deps: &[Id], path: &str, value: Json) -> Vec { self.sign(deps, event::write(path, value)) } pub(crate) fn keys(&self, deps: &[Id], keys: &[String]) -> Vec { self.sign(deps, event::keys(keys)) } pub(crate) fn nodes(&self, deps: &[Id], nodes: &[String]) -> Vec { self.sign(deps, event::nodes(nodes)) } pub(crate) fn propose(&self, deps: &[Id], version: &Json) -> Vec { self.sign(deps, event::proposal(version)) } pub(crate) fn vote(&self, deps: &[Id], on: Id, proposal: Id, yes: bool) -> Vec { self.sign(deps, event::vote(on, proposal, yes)) } } /// A version event, which nobody signs. pub(crate) fn version_event(deps: &[Id], version: Id) -> Vec { event::version_event(deps, version) } /// What a version gives a party, as a test spells it out. #[derive(Clone)] pub(crate) struct SpecParty { pub(crate) name: String, pub(crate) keys_since: u64, pub(crate) keys: Vec, pub(crate) nodes_since: u64, pub(crate) nodes: Vec, } /// A version, as a test spells it out. #[derive(Clone)] pub(crate) struct Spec { pub(crate) number: u64, pub(crate) changes: Option, pub(crate) parties: Vec, /// Each rule: its path and its writer. pub(crate) rules: Vec<(String, Json)>, pub(crate) checkpoint: Option>, } impl Spec { /// A first version naming these parties, each with its key and no nodes but the /// first's, with a namespace each (`home/{party}/**`) and the rules given after. pub(crate) fn first(parties: &[&Party], rules: &[(&str, Json)]) -> Spec { Spec { number: 0, changes: None, parties: parties .iter() .enumerate() .map(|(i, party)| { let nodes = if i == 0 { vec![node_id(0)] } else { Vec::new() }; SpecParty { name: party.name.clone(), keys_since: 0, keys: vec![party.key_line()], nodes_since: 0, nodes, } }) .collect(), rules: std::iter::once(("home/{party}/**".to_owned(), str("{party}"))) .chain( rules .iter() .map(|(path, writer)| ((*path).to_owned(), writer.clone())), ) .collect(), checkpoint: None, } } /// The version after `before`, which this spells: one past its number, changing its /// hash, nothing named anew. pub(crate) fn after(&self, before: Id) -> Spec { Spec { number: self.number + 1, changes: Some(before), checkpoint: None, ..self.clone() } } pub(crate) fn json(&self) -> Json { let mut members = vec![ ("federation", Json::Int(1)), ("number", Json::Int(self.number as i64)), ( "changes", self.changes .map_or(Json::Null, |hash| Json::Str(hash.to_string())), ), ( "keys", object( self.parties .iter() .map(|party| { ( party.name.as_str(), object(vec![ ("since", Json::Int(party.keys_since as i64)), ( "keys", Json::Array( party.keys.iter().map(|key| str(key)).collect(), ), ), ]), ) }) .collect(), ), ), ( "nodes", object( self.parties .iter() .map(|party| { ( party.name.as_str(), object(vec![ ("since", Json::Int(party.nodes_since as i64)), ( "nodes", Json::Array(party.nodes.iter().map(|id| str(id)).collect()), ), ]), ) }) .collect(), ), ), ( "rules", Json::Array( self.rules .iter() .map(|(path, writer)| { object(vec![("path", str(path)), ("writer", writer.clone())]) }) .collect(), ), ), ]; if let Some(heads) = &self.checkpoint { members.push(("checkpoint", ids(heads))); } object(members) } /// The version's hash. pub(crate) fn hash(&self) -> Id { Id::of(&self.json().bytes()) } /// The root of a federation whose first version this is. pub(crate) fn root(&self) -> Vec { event::Checkpoint { at: Vec::new(), keeps: Vec::new(), version: super::version::Version::read(&self.json()).expect("a version"), } .bytes() } } /// Checks the attack table of `doc` (its section Attacks): every row has its test, named /// `{prefix}{row}`, among the sources of its kind, `unit` rows among `unit` and `sim` /// rows among `sim`, but for the rows `later`, whose tests come with later stages; a row /// marked `—` has none; and no test names a row that isn't there, nor two tests one row. pub(crate) fn every_row_has_its_test( doc: &str, prefix: &str, unit: &[&str], sim: &[&str], later: &[u32], ) { let attacks = doc.split("\n## Attacks").nth(1).expect("an attack table"); let attacks = attacks.split("\n## ").next().unwrap_or(attacks); let rows: std::collections::BTreeMap = attacks .lines() .filter_map(|line| { let cells: Vec<&str> = line.split('|').map(str::trim).collect(); Some((cells.get(1)?.parse().ok()?, cells.get(4)?.to_string())) }) .collect(); let tests = |sources: &[&str]| -> Vec { sources .iter() .flat_map(|source| source.lines()) .filter_map(|line| { let line = line.trim(); line.strip_prefix("async ") .unwrap_or(line) .strip_prefix("fn ")? .strip_prefix(prefix)? .split('(') .next()? .parse() .ok() }) .collect() }; let (unit, sim) = (tests(unit), tests(sim)); let all: Vec = unit.iter().chain(&sim).copied().collect(); for n in &all { assert!(rows.contains_key(n), "a test of row {n}, which isn't there"); assert_eq!( all.iter().filter(|m| *m == n).count(), 1, "row {n} tested twice" ); } let mut missing = Vec::new(); for (n, kind) in &rows { let none = kind == "—"; let tested = if kind.starts_with("unit") { unit.contains(n) } else if kind == "sim" { sim.contains(n) } else { false }; if later.contains(n) { assert!(!all.contains(n), "row {n} has its test now"); assert!(!none, "row {n} asks for no test"); } else if none { assert!(!all.contains(n), "row {n} asks for no test, and has one"); } else if !tested { missing.push(*n); } } assert!( missing.is_empty(), "rows without their test of their kind: {missing:?}" ); }