diff --git a/crates/aien-closure/README.md b/crates/aien-closure/README.md index d462bd5d..136451b4 100644 --- a/crates/aien-closure/README.md +++ b/crates/aien-closure/README.md @@ -40,13 +40,13 @@ the first finding (sorted by the order below, then component). | Exit | Code | Raised when | | --- | --- | --- | -| 10 | UNVERIFIED_DEPENDENCY | a dependency has no manifest, a manifest is malformed, a declared dep is not a real edge, the chain does not verify as PASS, the receipt tier is below the profile, or no manifest exists | +| 10 | UNVERIFIED_DEPENDENCY | a dependency has no manifest, a manifest is malformed, a declared dep is not a real edge, `closure.lock` pins a dependency the component does not have, the chain does not verify as PASS, the receipt tier is below the profile, or no manifest exists | | 11 | MISSING_RECEIPT | a receipt id (component or any chain member) is not in the store | | 12 | RECEIPT_HASH_MISMATCH | a receipt file does not hash to its id, or is not a valid receipt | -| 13 | DEPENDENCY_NOT_PINNED | a dep has no receipt pin, the pin differs from the dep's receipt, the receipt does not depend on the pinned receipt, or `closure.lock` is missing or differs | +| 13 | DEPENDENCY_NOT_PINNED | a dep has no receipt pin, the pin differs from the dep's receipt, the receipt does not depend on the pinned receipt, or `closure.lock` is missing or has a missing or wrong line | | 14 | DEPENDENCY_CYCLE | the internal graph or the receipt graph has a cycle | | 15 | STALE_RECEIPT | current source digest differs from the manifest, or the receipt does not record the manifest's source digest | -| 16 | UNDECLARED_IMPORT | the graph has an edge the manifest does not declare | +| 16 | UNDECLARED_IMPORT | the graph has an edge the manifest does not declare (only this direction; an extra declaration is UNVERIFIED_DEPENDENCY) | | 17 | TAINTED_ARTIFACT | a receipt in the chain was recorded from a dirty tree or is TEST_ONLY_TRUST | | 18 | UNKNOWN_VERIFIER_PROFILE | the manifest names a profile this verifier does not know | @@ -104,3 +104,27 @@ checks one PASS case and at least one failing case per code, each asserting the exact finding and the binary's exit code. The committed seeded fixture (with store) is what `.github/workflows/verify-closure.yml` runs against. To rewrite it: `cargo test -p aien-closure -- --ignored regenerate_committed_fixture`. + +## One meaning per declaration code + +The meanings follow `aien-protocols` `specs/verified-crumb/SPEC.md` section 6.2 +(contract 0.2.0), implemented in `src/declare.rs`: + +| Situation | Code | +| --- | --- | +| an edge (or import) with no receipt pin, or a pin that is not the dependency's receipt | DEPENDENCY_NOT_PINNED | +| a `closure.lock` line for a dependency the component does not have | UNVERIFIED_DEPENDENCY | +| a declared dep that is not an actual edge | UNVERIFIED_DEPENDENCY | +| an actual edge the manifest does not declare | UNDECLARED_IMPORT | +| a VC's stored source imports a program not in `dependencies[]` (SPEC step 8) | UNDECLARED_IMPORT | +| a `dependencies[]` entry the VC's stored source does not import (SPEC step 8) | UNVERIFIED_DEPENDENCY | + +This crate reads no VCs, so the last two rows are covered by a unit test of the +shared set comparison (`declare::compare`), not by the binary. +`tests/declaration_codes.rs` has one test per row plus a negative twin that +fails if the row's code is swapped for a neighbouring code. + +## Receipt cross-check fixtures + +`tests/fixtures/receipts/` holds receipts minted with aien-proof for cases the +Omega C reader's three fixtures do not cover. See the README there. diff --git a/crates/aien-closure/src/declare.rs b/crates/aien-closure/src/declare.rs new file mode 100644 index 00000000..d733bf9f --- /dev/null +++ b/crates/aien-closure/src/declare.rs @@ -0,0 +1,149 @@ +//! Declaration checks: what a unit says it depends on against what it uses. +//! +//! One meaning per code, from `aien-protocols` `specs/verified-crumb/SPEC.md` +//! section 6.2: +//! +//! | Situation | Code | +//! |---|---| +//! | actual import or edge not declared in the dependency list | `UNDECLARED_IMPORT` | +//! | declared dependency that is not an actual import or edge | `UNVERIFIED_DEPENDENCY` | +//! | lock line for something the program never imports | `UNVERIFIED_DEPENDENCY` | +//! | import, edge or manifest entry without a (correct) lock pin | `DEPENDENCY_NOT_PINNED` | +//! +//! The first two rows are one set comparison, [`compare`]. The same function +//! serves SPEC step 8 (a VC's stored source imports against its +//! `dependencies[]`), which this crate does not run today because it reads no +//! VCs. The lock row is [`extra_needs`]. + +use crate::error::Code; +use std::collections::BTreeSet; + +/// One disagreement between the actual set and the declared set. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Disagreement { + pub code: Code, + pub name: String, +} + +/// Compare what a program actually uses with what its dependency list +/// declares. Actual but undeclared is `UNDECLARED_IMPORT` (the program does +/// more than it says). Declared but not actual is `UNVERIFIED_DEPENDENCY` +/// (nothing shows the declaration is an edge). Sorted by code then name. +pub fn compare(actual: &BTreeSet, declared: &BTreeSet) -> Vec { + let mut out: Vec = actual + .difference(declared) + .map(|n| Disagreement { + code: Code::UndeclaredImport, + name: n.clone(), + }) + .collect(); + out.extend(declared.difference(actual).map(|n| Disagreement { + code: Code::UnverifiedDependency, + name: n.clone(), + })); + out +} + +fn needs_pair(line: &str) -> Option<(&str, &str)> { + let mut it = line.split(' '); + match (it.next(), it.next(), it.next()) { + (Some("needs"), Some(c), Some(d)) => Some((c, d)), + _ => None, + } +} + +/// `needs ` lines of the lock on disk whose +/// (component, dep) pair is not in the rebuilt lock: pins for something the +/// component does not depend on. A line with a known pair but another id is +/// not extra; it is a wrong pin and stays `DEPENDENCY_NOT_PINNED`. +pub fn extra_needs(have: &str, want: &str) -> Vec<(String, String)> { + let wanted: BTreeSet<(&str, &str)> = want.lines().filter_map(needs_pair).collect(); + let mut out: Vec<(String, String)> = have + .lines() + .filter_map(needs_pair) + .filter(|p| !wanted.contains(p)) + .map(|(c, d)| (c.to_string(), d.to_string())) + .collect(); + out.sort(); + out.dedup(); + out +} + +/// The lock text with every extra `needs` line removed, so what remains can +/// be compared with the rebuilt lock for the pin rows. +pub fn without_extra_needs(have: &str, want: &str) -> String { + let wanted: BTreeSet<(&str, &str)> = want.lines().filter_map(needs_pair).collect(); + let mut s = String::new(); + for line in have.lines() { + if needs_pair(line).is_some_and(|p| !wanted.contains(&p)) { + continue; + } + s.push_str(line); + s.push('\n'); + } + s +} + +#[cfg(test)] +mod tests { + use super::*; + + fn set(items: &[&str]) -> BTreeSet { + items.iter().map(|s| s.to_string()).collect() + } + + fn codes(d: &[Disagreement]) -> Vec<(Code, &str)> { + d.iter().map(|x| (x.code, x.name.as_str())).collect() + } + + #[test] + fn agreement_has_no_findings() { + assert!(compare(&set(&["a", "b"]), &set(&["a", "b"])).is_empty()); + assert!(compare(&set(&[]), &set(&[])).is_empty()); + } + + #[test] + fn row_actual_not_declared_is_undeclared_import() { + let d = compare(&set(&["a", "b"]), &set(&["a"])); + assert_eq!(codes(&d), vec![(Code::UndeclaredImport, "b")]); + // Negative: it is not the extra-declaration code. + assert!(d.iter().all(|x| x.code != Code::UnverifiedDependency)); + } + + #[test] + fn row_declared_not_actual_is_unverified_dependency() { + let d = compare(&set(&["a"]), &set(&["a", "z"])); + assert_eq!(codes(&d), vec![(Code::UnverifiedDependency, "z")]); + assert!(d.iter().all(|x| x.code != Code::UndeclaredImport)); + } + + #[test] + fn step8_stored_source_imports_against_dependencies() { + // SPEC step 8: the VC's stored source imports are `actual`, the + // `dependencies[]` ids are `declared`. Both directions at once. + let d = compare(&set(&["id1", "id2"]), &set(&["id2", "id3"])); + assert_eq!( + codes(&d), + vec![ + (Code::UndeclaredImport, "id1"), + (Code::UnverifiedDependency, "id3") + ] + ); + } + + #[test] + fn extra_needs_finds_only_unknown_pairs() { + let want = "closure-lock v1\ncomponent a r s\nneeds a b ID1\n"; + let same = want.to_string(); + assert!(extra_needs(&same, want).is_empty()); + // Known pair, other id: a wrong pin, not an extra one. + let wrong = "closure-lock v1\ncomponent a r s\nneeds a b OTHER\n"; + assert!(extra_needs(wrong, want).is_empty()); + // Unknown pair: extra. + let extra = format!("{want}needs a z ID9\n"); + assert_eq!(extra_needs(&extra, want), vec![("a".into(), "z".into())]); + assert_eq!(without_extra_needs(&extra, want), want); + // Missing line is not extra. + assert!(extra_needs("closure-lock v1\n", want).is_empty()); + } +} diff --git a/crates/aien-closure/src/lib.rs b/crates/aien-closure/src/lib.rs index 65a1b340..16626423 100644 --- a/crates/aien-closure/src/lib.rs +++ b/crates/aien-closure/src/lib.rs @@ -4,6 +4,7 @@ //! internal edge is declared in a `closure.toml` manifest and backed by a //! verified aien-proof receipt. Fails closed: any finding is a non-zero exit. +pub mod declare; pub mod digest; pub mod error; pub mod graph; diff --git a/crates/aien-closure/src/verify.rs b/crates/aien-closure/src/verify.rs index c0ad12c5..6b8d175d 100644 --- a/crates/aien-closure/src/verify.rs +++ b/crates/aien-closure/src/verify.rs @@ -1,5 +1,6 @@ //! The verification pipeline behind `verify-closure`. +use crate::declare; use crate::digest::source_digest; use crate::error::{Code, Finding}; use crate::graph::{cargo_packages, closure_of, find_cycle, graph_of, Package}; @@ -140,14 +141,26 @@ fn check_component( ); } - // Edges: every actual edge declared and pinned to the dep's own receipt. + // Declarations (SPEC 6.2): an actual edge the manifest does not declare is + // UNDECLARED_IMPORT; a declared dep that is not an actual edge is + // UNVERIFIED_DEPENDENCY. + let actual: BTreeSet = pkg.deps.iter().cloned().collect(); + let declared: BTreeSet = m.deps.keys().cloned().collect(); + for d in declare::compare(&actual, &declared) { + let dep = &d.name; + let detail = match d.code { + Code::UndeclaredImport => { + format!("edge to {dep} is not declared in {}", manifest::FILE_NAME) + } + _ => format!("declared dep {dep} is not an edge of the graph"), + }; + add(d.code, detail); + } + + // Edges: every declared actual edge pinned to the dep's own receipt. for dep in &pkg.deps { - match m.deps.get(dep) { - None => add( - Code::UndeclaredImport, - format!("edge to {dep} is not declared in {}", manifest::FILE_NAME), - ), - Some(pin) => match manifests.get(dep) { + if let Some(pin) = m.deps.get(dep) { + match manifests.get(dep) { None => add( Code::UnverifiedDependency, format!("dependency {dep} has no {}", manifest::FILE_NAME), @@ -166,15 +179,7 @@ fn check_component( ), Some(_) => {} }, - }, - } - } - for dep in m.deps.keys() { - if !pkg.deps.contains(dep) { - add( - Code::UnverifiedDependency, - format!("declared dep {dep} is not an edge of the graph"), - ); + } } } @@ -358,11 +363,25 @@ pub fn verify(opts: &Options) -> Result { } match fs::read_to_string(&path) { Ok(have) if have == want => {} - Ok(_) => findings.push(Finding::new( - Code::DependencyNotPinned, - "-", - format!("{LOCK_FILE} does not match the rebuilt closure"), - )), + Ok(have) => { + // A pin for something the component does not depend on is + // UNVERIFIED_DEPENDENCY (SPEC 6.2). Everything else that + // differs (missing line, wrong id) is DEPENDENCY_NOT_PINNED. + for (comp, dep) in declare::extra_needs(&have, &want) { + findings.push(Finding::new( + Code::UnverifiedDependency, + &comp, + format!("{LOCK_FILE} pins {dep} for {comp}, which does not depend on it"), + )); + } + if declare::without_extra_needs(&have, &want) != want { + findings.push(Finding::new( + Code::DependencyNotPinned, + "-", + format!("{LOCK_FILE} does not match the rebuilt closure"), + )); + } + } Err(_) => findings.push(Finding::new( Code::DependencyNotPinned, "-", diff --git a/crates/aien-closure/tests/closure.rs b/crates/aien-closure/tests/closure.rs index 8b9a7f8c..6c44bbe5 100644 --- a/crates/aien-closure/tests/closure.rs +++ b/crates/aien-closure/tests/closure.rs @@ -1,59 +1,11 @@ mod common; use aien_closure::error::ALL; -use aien_closure::{verify, Code, Options}; +use aien_closure::Code; use common::*; use std::collections::BTreeSet; -use std::path::Path; use std::process::Command; -fn run(root: &Path, store: &Path) -> aien_closure::Outcome { - verify(&Options { - root: root.to_path_buf(), - store: store.to_path_buf(), - write_lock: false, - }) - .unwrap() -} - -fn codes(root: &Path, store: &Path) -> BTreeSet { - let out = run(root, store); - for f in &out.findings { - eprintln!("{}", f.line()); - } - out.findings.iter().map(|f| f.code).collect() -} - -/// Run the real binary and return (exit code, stdout). -fn bin(root: &Path, store: &Path) -> (i32, String) { - let o = Command::new(env!("CARGO_BIN_EXE_verify-closure")) - .args([ - "--root", - root.to_str().unwrap(), - "--store", - store.to_str().unwrap(), - ]) - .output() - .unwrap(); - ( - o.status.code().unwrap(), - String::from_utf8_lossy(&o.stdout).into_owned(), - ) -} - -/// The case must produce exactly `want`, and the binary must exit with the -/// matching code and print one FINDING line per finding. -fn expect_only(root: &Path, store: &Path, want: Code) { - assert_eq!(codes(root, store), BTreeSet::from([want]), "wrong findings"); - let (exit, stdout) = bin(root, store); - assert_eq!(exit, want.exit_code(), "wrong exit code:\n{stdout}"); - assert!( - stdout.contains(&format!("FINDING {} ", want.as_str())), - "{stdout}" - ); - assert!(stdout.contains("SUMMARY status=FAIL"), "{stdout}"); -} - #[test] fn pass_case() { let (root, store, ids) = seeded(&Knobs::default()); diff --git a/crates/aien-closure/tests/common/mod.rs b/crates/aien-closure/tests/common/mod.rs index fd056500..f0e13ba9 100644 --- a/crates/aien-closure/tests/common/mod.rs +++ b/crates/aien-closure/tests/common/mod.rs @@ -6,12 +6,14 @@ use aien_closure::digest::source_digest; use aien_closure::graph::{cargo_packages, graph_of}; use aien_closure::manifest::{self, Manifest}; use aien_closure::verify::{render_lock, LOCK_FILE}; +use aien_closure::{verify, Code, Options}; use aien_proof::evidence::{ receipt_id, store_receipt, Assertion, Mutation, Receipt, Tier, Verdict, SCHEMA, SCHEMA_VERSION, }; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::fs; use std::path::{Path, PathBuf}; +use std::process::Command; use std::sync::atomic::{AtomicUsize, Ordering}; static N: AtomicUsize = AtomicUsize::new(0); @@ -162,3 +164,50 @@ pub fn write(root: &Path, rel: &str, text: &str) { pub fn read(root: &Path, rel: &str) -> String { fs::read_to_string(root.join(rel)).unwrap() } + +pub fn run(root: &Path, store: &Path) -> aien_closure::Outcome { + verify(&Options { + root: root.to_path_buf(), + store: store.to_path_buf(), + write_lock: false, + }) + .unwrap() +} + +pub fn codes(root: &Path, store: &Path) -> BTreeSet { + let out = run(root, store); + for f in &out.findings { + eprintln!("{}", f.line()); + } + out.findings.iter().map(|f| f.code).collect() +} + +/// Run the real binary and return (exit code, stdout). +pub fn bin(root: &Path, store: &Path) -> (i32, String) { + let o = Command::new(env!("CARGO_BIN_EXE_verify-closure")) + .args([ + "--root", + root.to_str().unwrap(), + "--store", + store.to_str().unwrap(), + ]) + .output() + .unwrap(); + ( + o.status.code().unwrap(), + String::from_utf8_lossy(&o.stdout).into_owned(), + ) +} + +/// The case must produce exactly `want`, and the binary must exit with the +/// matching code and print one FINDING line per finding. +pub fn expect_only(root: &Path, store: &Path, want: Code) { + assert_eq!(codes(root, store), BTreeSet::from([want]), "wrong findings"); + let (exit, stdout) = bin(root, store); + assert_eq!(exit, want.exit_code(), "wrong exit code:\n{stdout}"); + assert!( + stdout.contains(&format!("FINDING {} ", want.as_str())), + "{stdout}" + ); + assert!(stdout.contains("SUMMARY status=FAIL"), "{stdout}"); +} diff --git a/crates/aien-closure/tests/declaration_codes.rs b/crates/aien-closure/tests/declaration_codes.rs new file mode 100644 index 00000000..85d4e988 --- /dev/null +++ b/crates/aien-closure/tests/declaration_codes.rs @@ -0,0 +1,253 @@ +//! SPEC section 6.2 of aien-protocols `specs/verified-crumb/SPEC.md`: one +//! meaning per declaration code. One test per row of the table, each with a +//! negative twin that fails if the row's code is swapped for a neighbouring +//! code (a mutant that returns the wrong code is killed by the twin). +//! +//! Rows 5a and 5b (a VC's stored source imports against `dependencies[]`) are +//! covered by the unit test `step8_stored_source_imports_against_dependencies` +//! in `src/declare.rs`: this crate reads no VCs, the same set comparison is +//! what step 8 needs. + +mod common; + +use aien_closure::Code; +use aien_proof::evidence::{receipt_id, store_receipt, Receipt}; +use common::*; +use std::collections::BTreeSet; +use std::path::Path; + +fn only(root: &Path, store: &Path, want: Code) { + expect_only(root, store, want); +} + +/// Change a manifest line in place. +fn edit_manifest(root: &Path, krate: &str, from: &str, to: &str) { + let rel = format!("crates/{krate}/closure.toml"); + let text = read(root, &rel); + assert!(text.contains(from), "{from:?} not in {text}"); + write(root, &rel, &text.replace(from, to)); +} + +/// Re-qualify `krate` with an extra receipt dependency on `dep`, and declare +/// it in the manifest, so the only thing wrong is that `dep` is not an edge. +fn declare_non_edge( + root: &Path, + store: &Path, + ids: &std::collections::BTreeMap, + krate: &str, + dep: &str, +) { + let p = store.join("receipts").join(format!("{}.json", ids[krate])); + let mut r: Receipt = serde_json::from_str(&std::fs::read_to_string(p).unwrap()).unwrap(); + r.dependencies.push(ids[dep].clone()); + r.id = receipt_id(&r).unwrap(); + store_receipt(store, &r).unwrap(); + edit_manifest(root, krate, &ids[krate], &r.id); + let rel = format!("crates/{krate}/closure.toml"); + let text = read(root, &rel); + write(root, &rel, &format!("{}dep {dep} {}\n", text, ids[dep])); +} + +fn found(root: &Path, store: &Path) -> BTreeSet { + codes(root, store) +} + +// Row 1: an import with no pin, or a pin that is not the dependency's own receipt. + +#[test] +fn row1_edge_without_receipt_pin_is_not_pinned() { + let (root, store, ids) = seeded(&Knobs::default()); + edit_manifest( + &root, + "alpha", + &format!("dep beta {}", ids["beta"]), + "dep beta", + ); + only(&root, &store, Code::DependencyNotPinned); +} + +#[test] +fn row1_not_undeclared_and_not_unverified_twin() { + // Same situation, negative twin: a missing pin is neither an undeclared + // edge (the edge IS declared) nor an extra declaration. + let (root, store, ids) = seeded(&Knobs::default()); + edit_manifest( + &root, + "alpha", + &format!("dep beta {}", ids["beta"]), + "dep beta", + ); + let got = found(&root, &store); + assert!(!got.contains(&Code::UndeclaredImport), "{got:?}"); + assert!(!got.contains(&Code::UnverifiedDependency), "{got:?}"); +} + +#[test] +fn row1_pin_that_is_not_the_dependencys_receipt_is_not_pinned() { + let (root, store, ids) = seeded(&Knobs::default()); + edit_manifest( + &root, + "alpha", + &format!("dep beta {}", ids["beta"]), + &format!("dep beta {}", ids["gamma"]), + ); + only(&root, &store, Code::DependencyNotPinned); +} + +// Row 2: a lock line for something the component does not depend on. + +#[test] +fn row2_lock_line_with_no_edge_is_unverified_dependency() { + let (root, store, ids) = seeded(&Knobs::default()); + let lock = read(&root, "closure.lock"); + // gamma has no dependencies, so `gamma needs alpha` is a pin with no edge. + write( + &root, + "closure.lock", + &format!("{lock}needs gamma alpha {}\n", ids["alpha"]), + ); + only(&root, &store, Code::UnverifiedDependency); +} + +#[test] +fn row2_not_undeclared_and_not_not_pinned_twin() { + let (root, store, ids) = seeded(&Knobs::default()); + let lock = read(&root, "closure.lock"); + write( + &root, + "closure.lock", + &format!("{lock}needs gamma alpha {}\n", ids["alpha"]), + ); + let got = found(&root, &store); + assert!(!got.contains(&Code::UndeclaredImport), "{got:?}"); + assert!(!got.contains(&Code::DependencyNotPinned), "{got:?}"); +} + +#[test] +fn row2_wrong_id_and_missing_line_stay_not_pinned() { + // The mirror cases are pin problems, not extra pins: a known pair with + // another id, and a missing line. + let (root, store, ids) = seeded(&Knobs::default()); + let lock = read(&root, "closure.lock"); + write( + &root, + "closure.lock", + &lock.replace( + &format!("needs alpha gamma {}", ids["gamma"]), + &format!("needs alpha gamma {}", ids["alpha"]), + ), + ); + only(&root, &store, Code::DependencyNotPinned); + let dropped: String = lock + .lines() + .filter(|l| !l.starts_with("needs alpha gamma")) + .map(|l| format!("{l}\n")) + .collect(); + write(&root, "closure.lock", &dropped); + only(&root, &store, Code::DependencyNotPinned); +} + +#[test] +fn row2_extra_pin_and_missing_pin_together_report_both() { + let (root, store, ids) = seeded(&Knobs::default()); + let lock = read(&root, "closure.lock"); + let mut edited: String = lock + .lines() + .filter(|l| !l.starts_with("needs alpha gamma")) + .map(|l| format!("{l}\n")) + .collect(); + edited.push_str(&format!("needs gamma alpha {}\n", ids["alpha"])); + write(&root, "closure.lock", &edited); + assert_eq!( + found(&root, &store), + BTreeSet::from([Code::UnverifiedDependency, Code::DependencyNotPinned]) + ); +} + +// Row 3: a declared dependency that is not an actual edge. + +#[test] +fn row3_declared_dep_that_is_not_an_edge_is_unverified_dependency() { + // alpha depends on beta only; it now also declares gamma (a transitive + // dependency, not an edge of alpha). + let (root, store, ids) = seeded(&Knobs::default()); + declare_non_edge(&root, &store, &ids, "alpha", "gamma"); + only(&root, &store, Code::UnverifiedDependency); +} + +#[test] +fn row3_not_undeclared_twin() { + let (root, store, ids) = seeded(&Knobs::default()); + declare_non_edge(&root, &store, &ids, "alpha", "gamma"); + let got = found(&root, &store); + assert!(!got.contains(&Code::UndeclaredImport), "{got:?}"); + assert!(!got.contains(&Code::DependencyNotPinned), "{got:?}"); +} + +#[test] +fn row3_declared_dep_without_pin_that_is_not_an_edge_is_still_unverified() { + let (root, store, _) = seeded(&Knobs::default()); + let text = read(&root, "crates/alpha/closure.toml"); + write( + &root, + "crates/alpha/closure.toml", + &format!("{text}dep gamma\n"), + ); + only(&root, &store, Code::UnverifiedDependency); +} + +// Row 4: an actual edge the manifest does not declare. + +fn undeclared_edge() -> (std::path::PathBuf, std::path::PathBuf) { + let (root, store, _) = seeded(&Knobs::default()); + let m = read(&root, "crates/alpha/Cargo.toml"); + write( + &root, + "crates/alpha/Cargo.toml", + &m.replace( + "[dependencies]", + "[dependencies]\ngamma = { path = \"../gamma\" }", + ), + ); + // Re-seed against the new graph, then drop the declaration again, so the + // undeclared edge is the only thing wrong. + seed(&root, &store, &Knobs::default()); + let man = read(&root, "crates/alpha/closure.toml"); + let kept: String = man + .lines() + .filter(|l| !l.starts_with("dep gamma")) + .map(|l| format!("{l}\n")) + .collect(); + write(&root, "crates/alpha/closure.toml", &kept); + (root, store) +} + +#[test] +fn row4_actual_edge_not_declared_is_undeclared_import() { + let (root, store) = undeclared_edge(); + only(&root, &store, Code::UndeclaredImport); +} + +#[test] +fn row4_not_unverified_and_not_not_pinned_twin() { + let (root, store) = undeclared_edge(); + let got = found(&root, &store); + assert!(!got.contains(&Code::UnverifiedDependency), "{got:?}"); + assert!(!got.contains(&Code::DependencyNotPinned), "{got:?}"); +} + +// Rows 1 to 4 are four different codes, so a mutant that merges two of them +// is caught by the exit codes. + +#[test] +fn rows_1_to_4_map_to_four_distinct_exit_codes() { + let exits: BTreeSet = [ + Code::DependencyNotPinned, + Code::UnverifiedDependency, + Code::UndeclaredImport, + ] + .iter() + .map(|c| c.exit_code()) + .collect(); + assert_eq!(exits, BTreeSet::from([13, 10, 16])); +} diff --git a/crates/aien-closure/tests/fixtures/receipts/09eab03c5834b48772cac29fbd9f1b37db21d9388ba20f8c81bdddb2de1c2c65.json b/crates/aien-closure/tests/fixtures/receipts/09eab03c5834b48772cac29fbd9f1b37db21d9388ba20f8c81bdddb2de1c2c65.json new file mode 100644 index 00000000..cec5b296 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/09eab03c5834b48772cac29fbd9f1b37db21d9388ba20f8c81bdddb2de1c2c65.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "09eab03c5834b48772cac29fbd9f1b37db21d9388ba20f8c81bdddb2de1c2c65", + "kind": "host-v1", + "tier": "HOST_TEST", + "result": "FAIL", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck fail", + "machine": "host", + "env_class": "HOST_TEST", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:7cb0a49cf4a36b090242fbfe84688ea855fe51275d1889e8e2a2eff08f3fb21c" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "fail", + "pass": false, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "NONE", + "observed_mutation": "NONE", + "authority": "", + "output_digest": "0559ab41385d8b4ef9695bac46dd7fe33e014ba2fc7c3488e5d7505bb6413aae", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/25bf973765f7c1b0b1ff447474d81d954883c3706a180fb4effe9953a4a01b80.json b/crates/aien-closure/tests/fixtures/receipts/25bf973765f7c1b0b1ff447474d81d954883c3706a180fb4effe9953a4a01b80.json new file mode 100644 index 00000000..d8dedb5a --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/25bf973765f7c1b0b1ff447474d81d954883c3706a180fb4effe9953a4a01b80.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "25bf973765f7c1b0b1ff447474d81d954883c3706a180fb4effe9953a4a01b80", + "kind": "host-v1", + "tier": "TEST_ONLY_TRUST", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck test-only", + "machine": "host", + "env_class": "TEST_ONLY_TRUST", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:dae3c0a9778c86e5f47e6843d41d6d8fbc986d6e823369b9453d4be7aa0b2698" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "NONE", + "observed_mutation": "NONE", + "authority": "", + "output_digest": "78beaa33e8172c93cf8dcc9e6ab8e54303ecd157ca70fcdd23fb2b31a255bbe6", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/81433a807b6ec84166822aba84c4b5e8a74820f3f40f83ce93d3d669e66e9671.json b/crates/aien-closure/tests/fixtures/receipts/81433a807b6ec84166822aba84c4b5e8a74820f3f40f83ce93d3d669e66e9671.json new file mode 100644 index 00000000..f2fdad6b --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/81433a807b6ec84166822aba84c4b5e8a74820f3f40f83ce93d3d669e66e9671.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "81433a807b6ec84166822aba84c4b5e8a74820f3f40f83ce93d3d669e66e9671", + "kind": "host-v1", + "tier": "MACHINE1_READ_ONLY", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck hw-no-lease", + "machine": "machine-1", + "env_class": "MACHINE1_READ_ONLY", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:dfdd6903b3a22c65b5ce54235e86400aaaf172db849f7057a1e48bd641a50c95" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "NONE", + "observed_mutation": "NONE", + "authority": "", + "output_digest": "d7e9f8e7ae88a42fc2894c8a19697b4f3a281a66288ca1200857452f2a2c4fcf", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/9643a90de5a0b5d4ca1e9f143942b527ae78e71666dbc3d669b9fe16713c254e.json b/crates/aien-closure/tests/fixtures/receipts/9643a90de5a0b5d4ca1e9f143942b527ae78e71666dbc3d669b9fe16713c254e.json new file mode 100644 index 00000000..29dd9fca --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/9643a90de5a0b5d4ca1e9f143942b527ae78e71666dbc3d669b9fe16713c254e.json @@ -0,0 +1,70 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "9643a90de5a0b5d4ca1e9f143942b527ae78e71666dbc3d669b9fe16713c254e", + "kind": "host-v1", + "tier": "HOST_TEST", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck host-lists", + "machine": "host", + "env_class": "HOST_TEST", + "input_artifacts": [ + "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "blake3:0000000000000000000000000000000000000000000000000000000000000000", + "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + ], + "output_artifacts": [ + "sha512:99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", + "blake3:1111111111111111111111111111111111111111111111111111111111111111" + ], + "assertions": [ + { + "id": "zeta", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + }, + { + "id": "alpha", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + }, + { + "id": "mid", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [ + "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "2222222222222222222222222222222222222222222222222222222222222222", + "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + ], + "declared_mutation": "NONE", + "observed_mutation": "NONE", + "authority": "", + "output_digest": "f43ce9a0ad6704fcd4f21a42119ee4608f1ce0cde08e870ae2ba041bedc09e70", + "external_refs": [ + "ref-b", + "ref-a", + "ref-b" + ], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/README.md b/crates/aien-closure/tests/fixtures/receipts/README.md new file mode 100644 index 00000000..492596dd --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/README.md @@ -0,0 +1,68 @@ +# Receipt cross-check fixtures + +Receipts minted with aien-proof (`aien_proof::evidence`) for the cases the Omega +C reader's three fixtures (`omega/tests/resolve/receipts`) do not cover. Those +three are all `kind` `component-qualification`, tier `HOST_TEST`, mutation +`NONE`, no lease, no ledger. These cover what they leave out: + +- a non-NONE declared and observed mutation, including observed below declared +- a lease binding and a ledger binding +- `kind` equal to a verifier profile (`host-v1`, `qemu-v1`, `production-v1`), + which is what SPEC 5.1 rule 5 needs (`receipt.kind` equals `verifier_profile`) +- tiers above HOST_TEST: QEMU, MACHINE1_ATTENDED, PRODUCTION (with authority) +- lists in non-canonical order with duplicates and mixed digest spellings (the + id is over the sorted unique form, so a reader that does not sort gets + another id) +- four receipts a profile-aware reader must refuse + +Each file is `.json`, the same layout as an aien-proof store +(`/receipts/.json`). `expected.txt` has one line per file: + +``` + name=... kind=... tier=... result=... mutation=/ lease=yes|no ledger=yes|no verify= with_store= accept=ACCEPT|REFUSE +``` + +## What a reader must do + +`accept=ACCEPT` means: the id recomputes from the canonical bytes, the +single-receipt consistency rules pass (`verify=PASS`), and the receipt tier +satisfies the minimum tier of the profile named by `kind` (host-v1 needs +HOST_TEST, qemu-v1 needs QEMU, production-v1 needs PRODUCTION, compared with +`aien_proof::chain::tier_satisfies`). `accept=REFUSE` is the opposite: + +| Fixture name | Why refused | +|---|---| +| host-v1-result-fail | result FAIL (`verify=FAIL`) | +| host-v1-hardware-without-lease | hardware tier PASS with no lease (`verify=INCOMPLETE`) | +| host-v1-test-only-trust | TEST_ONLY_TRUST satisfies nothing but itself | +| production-v1-on-qemu-tier | production-v1 needs a physical tier, QEMU is not | + +## Known limit: ledger and lease bindings + +Receipts with `lease=yes` or `ledger=yes` have `with_store=ERROR`: aien-proof +`verify_with_store` also checks the referenced ledger event and lease hold +against a proof store, and these fixtures point at events that exist in no +store. The Omega C reader does not check those bindings (stated limit in +`omega_receipt.h`), so for it the expected result is ACCEPT. A reader that does +check them must refuse these files without a backing store. The id covers the +binding fields either way: changing the lease or ledger fields without +re-minting breaks the id (`changing_any_bound_field_breaks_the_id`). + +## How Omega imports them + +Copy this directory next to `tests/resolve/receipts`, run the C reader on every +`.json`, and compare with `expected.txt`: the id check and the consistency +rules must give `verify`, and the profile tier rule must give `accept`. Do not +edit the JSON by hand: the file name is the id and the test below pins the +bytes. + +## Regeneration and tests + +`cargo test -p aien-closure --offline --locked --test receipt_crosscheck` +checks that the committed bytes equal what the test mints (deterministic: +fixed timestamps, commits and digests), that every case is decided as the table +says, and that changing any of 17 bound fields (kind, mutation, lease, ledger, +authority, assertions, lists, ...) in a fixture breaks the id. + +To rewrite the files after a deliberate change: +`cargo test -p aien-closure --offline --locked --test receipt_crosscheck -- --ignored regenerate_receipt_fixtures`. diff --git a/crates/aien-closure/tests/fixtures/receipts/b33e41e9c66c3c93d7b466abfa292cc1f41099c98f446294050ec36cc8910489.json b/crates/aien-closure/tests/fixtures/receipts/b33e41e9c66c3c93d7b466abfa292cc1f41099c98f446294050ec36cc8910489.json new file mode 100644 index 00000000..0a34e8c8 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/b33e41e9c66c3c93d7b466abfa292cc1f41099c98f446294050ec36cc8910489.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "b33e41e9c66c3c93d7b466abfa292cc1f41099c98f446294050ec36cc8910489", + "kind": "qemu-v1", + "tier": "QEMU", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck qemu", + "machine": "qemu-virt-aarch64", + "env_class": "QEMU", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:48a78cc45e3feec5bf8e8f8a4930bf93a523b4a62b25936c32f1f49ca6ae41ba" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "BOUNDED_TEST_REGION_WRITE", + "observed_mutation": "BOUNDED_TEST_REGION_WRITE", + "authority": "", + "output_digest": "79f44f93900cf0eefee1ef0de1c8bf6088aa06be2b1e7fe44a7d38dcdd93decb", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/b5f772e852cd1ed737fdfc7dead8742d84b30b6379c88956d7b1551fa3dc0888.json b/crates/aien-closure/tests/fixtures/receipts/b5f772e852cd1ed737fdfc7dead8742d84b30b6379c88956d7b1551fa3dc0888.json new file mode 100644 index 00000000..ab49a100 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/b5f772e852cd1ed737fdfc7dead8742d84b30b6379c88956d7b1551fa3dc0888.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "b5f772e852cd1ed737fdfc7dead8742d84b30b6379c88956d7b1551fa3dc0888", + "kind": "production-v1", + "tier": "QEMU", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck prod-on-qemu", + "machine": "host", + "env_class": "QEMU", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:7a6b1c159028ae1267983bfa5a3afd04e5515f91702733165b7cf0c36ff384b4" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "NONE", + "observed_mutation": "NONE", + "authority": "", + "output_digest": "1373d72477d824150bf72ca690c79f8c01fa4b1b8e14ea67b2af5d0c99239453", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/d75c983992894bf4ed6f772406f2cb290de39e399b638e427db06834f908d3ea.json b/crates/aien-closure/tests/fixtures/receipts/d75c983992894bf4ed6f772406f2cb290de39e399b638e427db06834f908d3ea.json new file mode 100644 index 00000000..2f047014 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/d75c983992894bf4ed6f772406f2cb290de39e399b638e427db06834f908d3ea.json @@ -0,0 +1,47 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "d75c983992894bf4ed6f772406f2cb290de39e399b638e427db06834f908d3ea", + "kind": "host-v1", + "tier": "MACHINE1_ATTENDED", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck hw-lease", + "machine": "machine-1", + "env_class": "MACHINE1_ATTENDED", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:7a946c562b68abbdb8f3219565d06817d3551266dbef7f6a128cab005b79776e" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "BOOT_CONFIGURATION_CHANGE", + "observed_mutation": "ONE_TIME_BOOT_SELECTION", + "authority": "", + "output_digest": "027ae3aeb8103e24ba3868525efd30373df672081b73855184494a45b84b62bc", + "external_refs": [], + "ledger": { + "index": 42, + "hash": "8b87956012aabad059c560c96bc1e934b478ec8cfaaf272a5d2860ce68a9d13f" + }, + "lease": { + "hold_id": "598857a8d0f99d7dc59be8a26a164269f10f6a0928c613f32dfcbb61205df807", + "resource": "machine-1" + }, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/dd465c3d45775c1b266bd3c8367c798551bf91f16d88bf276224cd3ea5468503.json b/crates/aien-closure/tests/fixtures/receipts/dd465c3d45775c1b266bd3c8367c798551bf91f16d88bf276224cd3ea5468503.json new file mode 100644 index 00000000..146b7e27 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/dd465c3d45775c1b266bd3c8367c798551bf91f16d88bf276224cd3ea5468503.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "dd465c3d45775c1b266bd3c8367c798551bf91f16d88bf276224cd3ea5468503", + "kind": "host-v1", + "tier": "HOST_TEST", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck host-declared-above", + "machine": "host", + "env_class": "HOST_TEST", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:17fda073457cd584b696c37122fa668147a41a36f15eb749b7104ed5656477d1" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "BOUNDED_TEST_REGION_WRITE", + "observed_mutation": "ONE_TIME_BOOT_SELECTION", + "authority": "", + "output_digest": "e0bbf21c85acf4265816b7f4b9899dec8bbd732033666ca1830257fdf0532006", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/e3e3a03ddac21fb0398246310bd6a9026f67206189eda0950eb0a6014b860025.json b/crates/aien-closure/tests/fixtures/receipts/e3e3a03ddac21fb0398246310bd6a9026f67206189eda0950eb0a6014b860025.json new file mode 100644 index 00000000..8df00989 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/e3e3a03ddac21fb0398246310bd6a9026f67206189eda0950eb0a6014b860025.json @@ -0,0 +1,44 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "e3e3a03ddac21fb0398246310bd6a9026f67206189eda0950eb0a6014b860025", + "kind": "production-v1", + "tier": "PRODUCTION", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck production", + "machine": "machine-1", + "env_class": "PRODUCTION", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:19e9ee75552ea78815d9b66c6d0ad0779e637ca2a0c032e49f89645a685c81e1" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "TRUST_ROOT_CHANGE", + "observed_mutation": "TRUST_ROOT_CHANGE", + "authority": "owner-key:fixture", + "output_digest": "68d238cfb0aa2cc89d17f84aa251ea47a5cb1cf4ebe7effe4743c0d390a9e78c", + "external_refs": [], + "ledger": null, + "lease": { + "hold_id": "376b7ecc0b520004de505ca442bdeec64d192e8676eedadf68f52636b9af190f", + "resource": "machine-1" + }, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/expected.txt b/crates/aien-closure/tests/fixtures/receipts/expected.txt new file mode 100644 index 00000000..0f9d3ecf --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/expected.txt @@ -0,0 +1,15 @@ +# receipt cross-check expectations, one line per fixture (file .json) +# accept = verify passes AND tier satisfies the profile named by kind +# verify = aien-proof verify_bytes status; with_store = verify_with_store against a store +# holding no ledger and no hold (the C reader does not check ledger or lease bindings) +09eab03c5834b48772cac29fbd9f1b37db21d9388ba20f8c81bdddb2de1c2c65 name=host-v1-result-fail kind=host-v1 tier=HOST_TEST result=FAIL mutation=NONE/NONE lease=no ledger=no verify=FAIL with_store=FAIL accept=REFUSE +25bf973765f7c1b0b1ff447474d81d954883c3706a180fb4effe9953a4a01b80 name=host-v1-test-only-trust kind=host-v1 tier=TEST_ONLY_TRUST result=PASS mutation=NONE/NONE lease=no ledger=no verify=PASS with_store=PASS accept=REFUSE +81433a807b6ec84166822aba84c4b5e8a74820f3f40f83ce93d3d669e66e9671 name=host-v1-hardware-without-lease kind=host-v1 tier=MACHINE1_READ_ONLY result=PASS mutation=NONE/NONE lease=no ledger=no verify=INCOMPLETE with_store=INCOMPLETE accept=REFUSE +9643a90de5a0b5d4ca1e9f143942b527ae78e71666dbc3d669b9fe16713c254e name=host-v1-unsorted-duplicate-lists kind=host-v1 tier=HOST_TEST result=PASS mutation=NONE/NONE lease=no ledger=no verify=PASS with_store=PASS accept=ACCEPT +b33e41e9c66c3c93d7b466abfa292cc1f41099c98f446294050ec36cc8910489 name=qemu-v1-qemu-tier-mutation kind=qemu-v1 tier=QEMU result=PASS mutation=BOUNDED_TEST_REGION_WRITE/BOUNDED_TEST_REGION_WRITE lease=no ledger=no verify=PASS with_store=PASS accept=ACCEPT +b5f772e852cd1ed737fdfc7dead8742d84b30b6379c88956d7b1551fa3dc0888 name=production-v1-on-qemu-tier kind=production-v1 tier=QEMU result=PASS mutation=NONE/NONE lease=no ledger=no verify=PASS with_store=PASS accept=REFUSE +d75c983992894bf4ed6f772406f2cb290de39e399b638e427db06834f908d3ea name=host-v1-hardware-tier-lease-ledger kind=host-v1 tier=MACHINE1_ATTENDED result=PASS mutation=BOOT_CONFIGURATION_CHANGE/ONE_TIME_BOOT_SELECTION lease=yes ledger=yes verify=PASS with_store=ERROR accept=ACCEPT +dd465c3d45775c1b266bd3c8367c798551bf91f16d88bf276224cd3ea5468503 name=host-v1-declared-above-observed kind=host-v1 tier=HOST_TEST result=PASS mutation=BOUNDED_TEST_REGION_WRITE/ONE_TIME_BOOT_SELECTION lease=no ledger=no verify=PASS with_store=PASS accept=ACCEPT +e3e3a03ddac21fb0398246310bd6a9026f67206189eda0950eb0a6014b860025 name=production-v1-authority-lease kind=production-v1 tier=PRODUCTION result=PASS mutation=TRUST_ROOT_CHANGE/TRUST_ROOT_CHANGE lease=yes ledger=no verify=PASS with_store=ERROR accept=ACCEPT +f3fbb365b0aa81d4f090b4c954fb2d88b5a94891a24c2e4d4be1da2066955145 name=host-v1-ledger-binding kind=host-v1 tier=HOST_TEST result=PASS mutation=NONE/NONE lease=no ledger=yes verify=PASS with_store=ERROR accept=ACCEPT +fa2b52f9bc8aacc03b7908cde2dec6775b7721e40518dfc83f6e0dd622ea0814 name=host-v1-volatile-mutation kind=host-v1 tier=HOST_TEST result=PASS mutation=VOLATILE_ONLY/VOLATILE_ONLY lease=no ledger=no verify=PASS with_store=PASS accept=ACCEPT diff --git a/crates/aien-closure/tests/fixtures/receipts/f3fbb365b0aa81d4f090b4c954fb2d88b5a94891a24c2e4d4be1da2066955145.json b/crates/aien-closure/tests/fixtures/receipts/f3fbb365b0aa81d4f090b4c954fb2d88b5a94891a24c2e4d4be1da2066955145.json new file mode 100644 index 00000000..b2d9e017 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/f3fbb365b0aa81d4f090b4c954fb2d88b5a94891a24c2e4d4be1da2066955145.json @@ -0,0 +1,44 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "f3fbb365b0aa81d4f090b4c954fb2d88b5a94891a24c2e4d4be1da2066955145", + "kind": "host-v1", + "tier": "HOST_TEST", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck host-ledger", + "machine": "host", + "env_class": "HOST_TEST", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:0be8387e25574a5fe0622f1e956e975678dd0b4c583882308d96f5574c5af95d" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "NONE", + "observed_mutation": "NONE", + "authority": "", + "output_digest": "913fcdb198bc10d7c86910cac2e90e6b7de84d5c6aa580e29a089feb8754afab", + "external_refs": [], + "ledger": { + "index": 42, + "hash": "5f6e9c16a9605d439462fdd78794f4a8cead4976f18d6d26c16c1671f98d712f" + }, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/fixtures/receipts/fa2b52f9bc8aacc03b7908cde2dec6775b7721e40518dfc83f6e0dd622ea0814.json b/crates/aien-closure/tests/fixtures/receipts/fa2b52f9bc8aacc03b7908cde2dec6775b7721e40518dfc83f6e0dd622ea0814.json new file mode 100644 index 00000000..a9693479 --- /dev/null +++ b/crates/aien-closure/tests/fixtures/receipts/fa2b52f9bc8aacc03b7908cde2dec6775b7721e40518dfc83f6e0dd622ea0814.json @@ -0,0 +1,41 @@ +{ + "schema": "EvidenceReceiptV1", + "version": 1, + "id": "fa2b52f9bc8aacc03b7908cde2dec6775b7721e40518dfc83f6e0dd622ea0814", + "kind": "host-v1", + "tier": "HOST_TEST", + "result": "PASS", + "timestamp": 1786000100, + "repo": "https://github.com/aien-dev/aien-sovereign-core", + "commit": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "dirty": false, + "toolchain": "rustc fixture", + "procedure": "receipt-crosscheck host-volatile", + "machine": "host", + "env_class": "HOST_TEST", + "input_artifacts": [ + "sha256:1111111111111111111111111111111111111111111111111111111111111111", + "sha256:bf45d4b9d99f0e48b2c25e8653ddc5410397c989f8c1ecd5b4626ced528c4b57" + ], + "output_artifacts": [], + "assertions": [ + { + "id": "crosscheck_pass", + "expected": "pass", + "observed": "pass", + "pass": true, + "source": "cross-check", + "note": "" + } + ], + "dependencies": [], + "declared_mutation": "VOLATILE_ONLY", + "observed_mutation": "VOLATILE_ONLY", + "authority": "", + "output_digest": "1f3f9acb72290d67c35f94503fc96dd91b04985d3f64d6193f76e2a18189ecc3", + "external_refs": [], + "ledger": null, + "lease": null, + "required_features": [], + "reserved": "" +} diff --git a/crates/aien-closure/tests/receipt_crosscheck.rs b/crates/aien-closure/tests/receipt_crosscheck.rs new file mode 100644 index 00000000..bc60ffae --- /dev/null +++ b/crates/aien-closure/tests/receipt_crosscheck.rs @@ -0,0 +1,464 @@ +//! Rust side of the receipt cross-check for the Omega C reader (VC1 carry +//! item 5). Mints receipts with aien-proof for the cases the C reader's three +//! fixtures (`omega/tests/resolve/receipts`, all `component-qualification`, +//! tier HOST_TEST, mutation NONE, no lease, no ledger) do not cover, and +//! commits them under `tests/fixtures/receipts/` with `expected.txt`. +//! +//! The committed bytes must equal what this test mints (deterministic), every +//! fixture must verify the way `expected.txt` says, and a change to any bound +//! field must break the id. To rewrite the fixtures: +//! `cargo test -p aien-closure --test receipt_crosscheck -- --ignored regenerate_receipt_fixtures`. + +mod common; + +use aien_closure::verify::profile_min_tier; +use aien_proof::chain::tier_satisfies; +use aien_proof::evidence::{ + receipt_id, store_receipt, verify_bytes, verify_with_store, Assertion, LeaseRef, LedgerRef, + Mutation, Receipt, Tier, Verdict, SCHEMA, SCHEMA_VERSION, +}; +use common::temp_dir; +use std::fs; +use std::path::{Path, PathBuf}; + +fn fixture_dir() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/receipts") +} + +fn b3(s: &str) -> String { + blake3::hash(s.as_bytes()).to_hex().to_string() +} + +fn assertion(id: &str, pass: bool) -> Assertion { + Assertion { + id: id.to_string(), + expected: "pass".to_string(), + observed: if pass { "pass" } else { "fail" }.to_string(), + pass, + source: "cross-check".to_string(), + note: String::new(), + } +} + +/// An unsealed receipt of the given verifier-profile kind and tier. `name` makes +/// every fixture's inputs and output distinct. +fn base(name: &str, kind: &str, tier: Tier) -> Receipt { + let r = Receipt { + schema: SCHEMA.to_string(), + version: SCHEMA_VERSION, + id: String::new(), + kind: kind.to_string(), + tier, + result: Verdict::Pass, + timestamp: 1_786_000_100, + repo: "https://github.com/aien-dev/aien-sovereign-core".to_string(), + commit: "b".repeat(40), + dirty: false, + toolchain: "rustc fixture".to_string(), + procedure: format!("receipt-crosscheck {name}"), + machine: "host".to_string(), + env_class: tier.as_str().to_string(), + input_artifacts: vec![ + format!("sha256:{}", "1".repeat(64)), + format!("sha256:{}", b3(&format!("{name}-source"))), + ], + output_artifacts: vec![], + assertions: vec![assertion("crosscheck_pass", true)], + dependencies: vec![], + declared_mutation: Mutation::None, + observed_mutation: Mutation::None, + authority: String::new(), + output_digest: b3(&format!("{name}-output")), + external_refs: vec![], + ledger: None, + lease: None, + required_features: vec![], + reserved: String::new(), + }; + r +} + +fn seal(mut r: Receipt) -> Receipt { + r.id = receipt_id(&r).unwrap(); + r +} + +fn lease(name: &str) -> LeaseRef { + LeaseRef { + hold_id: b3(&format!("{name}-hold")), + resource: "machine-1".to_string(), + } +} + +fn ledger(name: &str) -> LedgerRef { + LedgerRef { + index: 42, + hash: b3(&format!("{name}-ledger-event")), + } +} + +struct Case { + name: &'static str, + receipt: Receipt, + /// What a profile-aware reader must do: accept only if the identity and + /// consistency check pass and the tier satisfies the kind's profile. + accept: bool, +} + +fn cases() -> Vec { + let mut v = Vec::new(); + let mut add = |name: &'static str, receipt: Receipt, accept: bool| { + v.push(Case { + name, + receipt, + accept, + }); + }; + + // Accepted: non-NONE mutation on the lowest tier. + let mut r = base("host-volatile", "host-v1", Tier::HostTest); + r.declared_mutation = Mutation::VolatileOnly; + r.observed_mutation = Mutation::VolatileOnly; + add("host-v1-volatile-mutation", seal(r), true); + + // Accepted: observed strictly below declared, both non-NONE. + let mut r = base("host-declared-above", "host-v1", Tier::HostTest); + r.declared_mutation = Mutation::BoundedTestRegionWrite; + r.observed_mutation = Mutation::OneTimeBootSelection; + add("host-v1-declared-above-observed", seal(r), true); + + // Accepted: ledger binding (the event is not in any store here). + let mut r = base("host-ledger", "host-v1", Tier::HostTest); + r.ledger = Some(ledger("host-ledger")); + add("host-v1-ledger-binding", seal(r), true); + + // Accepted: lists in non-canonical order with duplicates, mixed digest + // spellings, several assertions. The id is over the sorted unique form. + let mut r = base("host-lists", "host-v1", Tier::HostTest); + r.input_artifacts = vec![ + format!("sha256:{}", "f".repeat(64)), + format!("blake3:{}", "0".repeat(64)), + format!("sha256:{}", "f".repeat(64)), + "a".repeat(64), + ]; + r.output_artifacts = vec![ + format!("sha512:{}", "9".repeat(128)), + format!("blake3:{}", "1".repeat(64)), + ]; + r.dependencies = vec!["e".repeat(64), "2".repeat(64), "e".repeat(64)]; + r.assertions = vec![ + assertion("zeta", true), + assertion("alpha", true), + assertion("mid", true), + ]; + r.external_refs = vec![ + "ref-b".to_string(), + "ref-a".to_string(), + "ref-b".to_string(), + ]; + add("host-v1-unsorted-duplicate-lists", seal(r), true); + + // Accepted: the QEMU profile at the QEMU tier. + let mut r = base("qemu", "qemu-v1", Tier::Qemu); + r.machine = "qemu-virt-aarch64".to_string(); + r.declared_mutation = Mutation::BoundedTestRegionWrite; + r.observed_mutation = Mutation::BoundedTestRegionWrite; + add("qemu-v1-qemu-tier-mutation", seal(r), true); + + // Accepted: a hardware-tier receipt satisfies the lower host-v1 profile; + // it carries a lease and a ledger binding and a boot-level mutation. + let mut r = base("hw-lease", "host-v1", Tier::Machine1Attended); + r.machine = "machine-1".to_string(); + r.declared_mutation = Mutation::BootConfigurationChange; + r.observed_mutation = Mutation::OneTimeBootSelection; + r.lease = Some(lease("hw-lease")); + r.ledger = Some(ledger("hw-lease")); + add("host-v1-hardware-tier-lease-ledger", seal(r), true); + + // Accepted: PRODUCTION with an authority reference, lease, trust-root + // mutation (the maximum severity pair that is not destructive). + let mut r = base("production", "production-v1", Tier::Production); + r.machine = "machine-1".to_string(); + r.authority = "owner-key:fixture".to_string(); + r.declared_mutation = Mutation::TrustRootChange; + r.observed_mutation = Mutation::TrustRootChange; + r.lease = Some(lease("production")); + add("production-v1-authority-lease", seal(r), true); + + // Refused: hardware tier PASS with no lease (aien-proof reports + // INCOMPLETE). + let mut r = base("hw-no-lease", "host-v1", Tier::Machine1ReadOnly); + r.machine = "machine-1".to_string(); + add("host-v1-hardware-without-lease", seal(r), false); + + // Refused: a recorded FAIL with a failed assertion. + let mut r = base("fail", "host-v1", Tier::HostTest); + r.result = Verdict::Fail; + r.assertions = vec![assertion("crosscheck_pass", false)]; + add("host-v1-result-fail", seal(r), false); + + // Refused: TEST_ONLY_TRUST satisfies nothing but itself. + let r = base("test-only", "host-v1", Tier::TestOnlyTrust); + add("host-v1-test-only-trust", seal(r), false); + + // Refused: the production profile needs a physical tier; QEMU is not. + let r = base("prod-on-qemu", "production-v1", Tier::Qemu); + add("production-v1-on-qemu-tier", seal(r), false); + + v +} + +fn mutation_text(r: &Receipt) -> String { + format!( + "{}/{}", + r.declared_mutation.as_str(), + r.observed_mutation.as_str() + ) +} + +fn yes(b: bool) -> &'static str { + if b { + "yes" + } else { + "no" + } +} + +/// What aien-proof says: single-receipt verification (identity and +/// consistency), then with a store that has no ledger and no hold. +fn rust_verdict(r: &Receipt) -> (String, String) { + let text = serde_json::to_string_pretty(r).unwrap(); + let alone = match verify_bytes(text.as_bytes()) { + Ok((_, rep)) => rep.status.as_str().to_string(), + Err(e) => format!("ERROR({e})"), + }; + let empty = temp_dir("crosscheck-empty-store"); + let with_store = match verify_with_store(text.as_bytes(), &empty) { + Ok((_, rep)) => rep.status.as_str().to_string(), + Err(_) => "ERROR".to_string(), + }; + (alone, with_store) +} + +fn expected_text(cases: &[Case]) -> String { + let mut s = String::from( + "# receipt cross-check expectations, one line per fixture (file .json)\n\ + # accept = verify passes AND tier satisfies the profile named by kind\n\ + # verify = aien-proof verify_bytes status; with_store = verify_with_store against a store\n\ + # holding no ledger and no hold (the C reader does not check ledger or lease bindings)\n", + ); + let mut sorted: Vec<&Case> = cases.iter().collect(); + sorted.sort_by(|a, b| a.receipt.id.cmp(&b.receipt.id)); + for c in sorted { + let r = &c.receipt; + let (verify, with_store) = rust_verdict(r); + s.push_str(&format!( + "{} name={} kind={} tier={} result={} mutation={} lease={} ledger={} verify={} with_store={} accept={}\n", + r.id, + c.name, + r.kind, + r.tier.as_str(), + r.result.as_str(), + mutation_text(r), + yes(r.lease.is_some()), + yes(r.ledger.is_some()), + verify, + with_store, + if c.accept { "ACCEPT" } else { "REFUSE" }, + )); + } + s +} + +/// The reader decision the table claims, recomputed from aien-proof. +fn decided_accept(r: &Receipt) -> bool { + let (verify, _) = rust_verdict(r); + let need = profile_min_tier(&r.kind).expect("fixture kinds are verifier profiles"); + verify == "PASS" && tier_satisfies(need, r.tier) +} + +fn minted_bytes(r: &Receipt) -> String { + let store = temp_dir("crosscheck-mint"); + let id = store_receipt(&store, r).unwrap(); + fs::read_to_string(store.join("receipts").join(format!("{id}.json"))).unwrap() +} + +#[test] +#[ignore] +fn regenerate_receipt_fixtures() { + let dir = fixture_dir(); + let cs = cases(); + fs::create_dir_all(&dir).unwrap(); + for e in fs::read_dir(&dir).unwrap() { + let p = e.unwrap().path(); + if p.extension().is_some_and(|x| x == "json") { + fs::remove_file(p).unwrap(); + } + } + for c in &cs { + fs::write( + dir.join(format!("{}.json", c.receipt.id)), + minted_bytes(&c.receipt), + ) + .unwrap(); + } + fs::write(dir.join("expected.txt"), expected_text(&cs)).unwrap(); +} + +#[test] +fn every_case_is_decided_as_the_table_says() { + for c in cases() { + assert_eq!( + decided_accept(&c.receipt), + c.accept, + "{}: aien-proof decides otherwise", + c.name + ); + } +} + +#[test] +fn cases_cover_what_the_c_fixtures_do_not() { + let cs = cases(); + let accepted: Vec<&Case> = cs.iter().filter(|c| c.accept).collect(); + let has = |f: &dyn Fn(&Receipt) -> bool| accepted.iter().any(|c| f(&c.receipt)); + assert!(has(&|r| r.declared_mutation != Mutation::None)); + assert!(has(&|r| r.observed_mutation != Mutation::None)); + assert!(has(&|r| r.lease.is_some())); + assert!(has(&|r| r.ledger.is_some())); + assert!(has(&|r| r.kind == "host-v1")); + assert!(has(&|r| r.kind == "qemu-v1")); + assert!(has(&|r| r.kind == "production-v1")); + assert!(has(&|r| !r.authority.is_empty())); + assert!(cs.iter().any(|c| !c.accept)); + // None of them is the kind the C fixtures use. + assert!(cs + .iter() + .all(|c| c.receipt.kind != "component-qualification")); + // Ids are distinct. + let ids: std::collections::BTreeSet<&str> = cs.iter().map(|c| c.receipt.id.as_str()).collect(); + assert_eq!(ids.len(), cs.len()); +} + +#[test] +fn committed_fixtures_are_exactly_what_is_minted() { + let dir = fixture_dir(); + let cs = cases(); + let mut on_disk: Vec = fs::read_dir(&dir) + .unwrap() + .map(|e| e.unwrap().file_name().to_string_lossy().into_owned()) + .filter(|n| n.ends_with(".json")) + .collect(); + on_disk.sort(); + let mut want: Vec = cs + .iter() + .map(|c| format!("{}.json", c.receipt.id)) + .collect(); + want.sort(); + assert_eq!(on_disk, want, "fixture set differs; regenerate"); + for c in &cs { + let path = dir.join(format!("{}.json", c.receipt.id)); + assert_eq!( + fs::read_to_string(&path).unwrap(), + minted_bytes(&c.receipt), + "{} bytes differ", + c.name + ); + } + assert_eq!( + fs::read_to_string(dir.join("expected.txt")).unwrap(), + expected_text(&cs), + "expected.txt differs; regenerate" + ); +} + +#[test] +fn committed_files_verify_by_their_own_file_name() { + // Read the files the way the C reader will: by name, then check that the + // name is the canonical identity of the bytes inside. + let dir = fixture_dir(); + for c in cases() { + let id = &c.receipt.id; + let text = fs::read(dir.join(format!("{id}.json"))).unwrap(); + let (r, _) = verify_bytes(&text).unwrap(); + assert_eq!(&r.id, id); + assert_eq!(&receipt_id(&r).unwrap(), id); + } +} + +#[test] +fn store_bound_cases_are_refused_without_a_store_and_the_rest_are_not() { + // The C reader ignores ledger and lease bindings (stated limit), so the + // fixtures record what aien-proof does with them when no store backs them. + for c in cases() { + let (verify, with_store) = rust_verdict(&c.receipt); + let bound = c.receipt.lease.is_some() || c.receipt.ledger.is_some(); + if bound && verify == "PASS" { + assert_eq!(with_store, "ERROR", "{}", c.name); + } else if !bound { + assert_eq!(with_store, verify, "{}", c.name); + } + } +} + +fn set(v: &mut serde_json::Value, ptr: &str, to: serde_json::Value) -> bool { + match v.pointer_mut(ptr) { + Some(slot) => { + *slot = to; + true + } + None => false, + } +} + +#[test] +fn changing_any_bound_field_breaks_the_id() { + // Mutant-style negatives: every field the C reader must hash. Each edit + // keeps the old id, so a reader that skips the field would still accept. + let dir = fixture_dir(); + let tampers: Vec<(&str, serde_json::Value)> = vec![ + ("/kind", "host-v2".into()), + ("/timestamp", 7.into()), + ("/machine", "other".into()), + ("/procedure", "other".into()), + ("/commit", "c".repeat(40).into()), + ("/dirty", true.into()), + ("/output_digest", "d".repeat(64).into()), + ("/authority", "someone-else".into()), + ("/declared_mutation", "DESTRUCTIVE_STORAGE".into()), + ("/observed_mutation", "VOLATILE_ONLY".into()), + ("/lease/resource", "machine-2".into()), + ("/lease/hold_id", "0".repeat(64).into()), + ("/ledger/index", 43.into()), + ("/ledger/hash", "0".repeat(64).into()), + ("/assertions/0/observed", "changed".into()), + ( + "/input_artifacts/0", + format!("sha256:{}", "7".repeat(64)).into(), + ), + ("/dependencies/0", "9".repeat(64).into()), + ]; + let mut applied = 0usize; + for c in cases() { + let text = fs::read_to_string(dir.join(format!("{}.json", c.receipt.id))).unwrap(); + let original: serde_json::Value = serde_json::from_str(&text).unwrap(); + for (ptr, to) in &tampers { + if original.pointer(ptr) == Some(to) { + continue; + } + let mut v = original.clone(); + if !set(&mut v, ptr, to.clone()) { + continue; + } + applied += 1; + let tampered = serde_json::to_string(&v).unwrap(); + let got = verify_bytes(tampered.as_bytes()); + assert!( + !matches!(&got, Ok((_, rep)) if rep.status == Verdict::Pass), + "{}: changing {ptr} was not detected", + c.name + ); + } + } + assert!(applied > 60, "only {applied} tampers applied"); +}