interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / provenance / tests / common / mod.rs

69 lines · 2581 bytes Raw Blame History
1//! Helpers shared by the verdict test files. Mutations happen on copies under
2//! CARGO_TARGET_TMPDIR; committed fixtures are never rewritten.
3#![allow(dead_code)]
4
5use interplane_provenance::gojson::{sha256_hex, waldo_sha256};
6use serde_json::{json, Value};
7use std::collections::BTreeSet;
8use std::path::{Path, PathBuf};
9use std::sync::Mutex;
10
11pub fn copy_dir(from: &Path, to: &Path) {
12 std::fs::create_dir_all(to).unwrap();
13 for e in std::fs::read_dir(from).unwrap() {
14 let e = e.unwrap();
15 let dst = to.join(e.file_name());
16 if e.file_type().unwrap().is_dir() {
17 copy_dir(&e.path(), &dst);
18 } else {
19 std::fs::copy(e.path(), dst).unwrap();
20 }
21 }
22}
23
24/// A fresh copy of `fixture` under its own scratch directory. Tests in one binary run on parallel
25/// threads, so two tests sharing a name would delete and refill each other's copy mid-check
26/// (interplane main run 37932804024: a stray `missing_record`). A name may be claimed once per
27/// test binary; a second claim panics, so a clash fails every run instead of some runs.
28pub fn scratch(fixture: &str, name: &str) -> PathBuf {
29 static CLAIMED: Mutex<BTreeSet<String>> = Mutex::new(BTreeSet::new());
30 assert!(
31 CLAIMED.lock().unwrap().insert(name.to_string()),
32 "scratch name {name} is already used by another test in this binary"
33 );
34 let d = Path::new(env!("CARGO_TARGET_TMPDIR"))
35 .join("verdicts")
36 .join(name);
37 let _ = std::fs::remove_dir_all(&d);
38 copy_dir(&Path::new(env!("CARGO_MANIFEST_DIR")).join(fixture), &d);
39 d
40}
41
42pub fn companion(d: &Path) -> Value {
43 serde_json::from_slice(&std::fs::read(d.join("COMPANION.json")).unwrap()).unwrap()
44}
45
46pub fn write_companion(d: &Path, v: &Value) {
47 std::fs::write(
48 d.join("COMPANION.json"),
49 serde_json::to_vec_pretty(v).unwrap(),
50 )
51 .unwrap();
52}
53
54pub fn path_of(d: &Path, rec: &str) -> PathBuf {
55 d.join(companion(d)["records"][rec]["path"].as_str().unwrap())
56}
57
58/// What an attacker (or a careless tool) does after changing a record: update its entry in the
59/// manifest so the file-level digest matches. The chain must still refuse.
60pub fn restamp(d: &Path, rec: &str) {
61 let b = std::fs::read(path_of(d, rec)).unwrap();
62 let mut c = companion(d);
63 c["records"][rec]["sha256"] = json!(sha256_hex(&b));
64 c["records"][rec]["bytes"] = json!(b.len());
65 if c["records"][rec].get("waldo_sha256").is_some() {
66 c["records"][rec]["waldo_sha256"] = json!(waldo_sha256(&b));
67 }
68 write_companion(d, &c);
69}