use std::time::Instant;
use openmoat_audit::{EventId, Store};
use openmoat_core::{Action, CompiledPolicy, Policy};
use openmoat_hosts::Host;
use super::super::{CacheWrite, Pass, Progress, SessionTaint, catch_up, fold, of_session};
use super::{CWDS, ctx, far, read, record};
const BEFORE: &str = "version: 1\ndefaults: { \"*\": allow }\nreview:\n - { id: secrets-paths, \
fs.read: [\"~/.ssh/**\"] }\n";
const AFTER: &str = "version: 1\ndefaults: { \"*\": allow }\nreview:\n - { id: secrets-paths, \
fs.read: [\"~/.gnupg/**\"] }\n";
fn compiled<'p>(policy: &'p Policy, cwd: &str) -> CompiledPolicy<'p> {
CompiledPolicy::compile(policy, &ctx(cwd)).unwrap()
}
fn call(
store: &Store,
policy: &Policy,
cwd: &str,
deadline: Instant,
) -> anyhow::Result<(SessionTaint, usize)> {
let compiled = compiled(policy, cwd);
let mut save = |write: &CacheWrite| write.write(store);
let mut progress = Progress {
deadline,
save: &mut save,
every: super::super::CHECKPOINT_EVERY,
folded: 0,
};
let (taint, write) = of_session(store, Host::ClaudeCode, "s1", &compiled, cwd, &mut progress)?;
if let Some(write) = write {
write.write(store);
}
Ok((taint, progress.folded))
}
fn reference(store: &Store, policy: &Policy, cwd: &str) -> SessionTaint {
let events = store.session_after("s1", EventId(0)).unwrap();
let mut out = SessionTaint::default();
fold(
&mut out,
&events,
Host::ClaudeCode,
&compiled(policy, cwd),
cwd,
&mut Pass::default(),
)
.unwrap();
out
}
fn session(events: usize) -> Store {
let store = Store::open_in_memory().unwrap();
record(&store, CWDS[0], &read("/home/u/.ssh/id_rsa".to_owned()));
for i in 1..events {
record(&store, CWDS[0], &read(format!("/p/a/f{i}.rs")));
}
store
}
#[test]
fn a_policy_change_reads_on_from_the_carried_fold_until_the_new_one_catches_up() {
const EVENTS: usize = 2_000;
let (before, after) = (
Policy::parse(BEFORE).unwrap(),
Policy::parse(AFTER).unwrap(),
);
let store = session(EVENTS);
let (old, folded) = call(&store, &before, CWDS[0], far()).unwrap();
assert_eq!(folded, EVENTS);
assert_ne!(
old,
reference(&store, &after, CWDS[0]),
"the change must change the fold"
);
record(&store, CWDS[0], &read("/p/a/next.rs".to_owned()));
let budget = crate::commands::guard::taint_budget(Host::ClaudeCode);
let started = Instant::now();
let (carried, folded) = call(&store, &after, CWDS[0], started + budget).unwrap();
assert_eq!(folded, 1, "the first call under the new policy read on");
assert_eq!(carried.taint, old.taint);
record(&store, CWDS[1], &read("/p/b/next.rs".to_owned()));
let (_, folded) = call(&store, &after, CWDS[1], far()).unwrap();
assert_eq!(folded, 1);
catch_up(
&store,
Host::ClaudeCode,
"s1",
&compiled(&after, CWDS[0]),
CWDS[0],
far(),
);
record(&store, CWDS[0], &read("/p/a/last.rs".to_owned()));
let (exact, folded) = call(&store, &after, CWDS[0], far()).unwrap();
assert_eq!(folded, 1);
assert_eq!(exact, reference(&store, &after, CWDS[0]));
}
#[test]
fn an_unfinished_catch_up_leaves_the_carried_fold_in_charge() {
const EVENTS: usize = 1_000;
let (before, after) = (
Policy::parse(BEFORE).unwrap(),
Policy::parse(AFTER).unwrap(),
);
let store = session(EVENTS);
let (old, _) = call(&store, &before, CWDS[0], far()).unwrap();
let after_here = compiled(&after, CWDS[0]);
catch_up(
&store,
Host::ClaudeCode,
"s1",
&after_here,
CWDS[0],
Instant::now(),
);
let (carried, folded) = call(&store, &after, CWDS[0], far()).unwrap();
assert_eq!(folded, 0);
assert_eq!(carried, old);
catch_up(&store, Host::ClaudeCode, "s1", &after_here, CWDS[0], far());
let (exact, folded) = call(&store, &after, CWDS[0], far()).unwrap();
assert_eq!(folded, 0);
assert_eq!(exact, reference(&store, &after, CWDS[0]));
}
#[test]
fn a_fold_holding_a_call_without_a_directory_is_not_carried() {
let (before, after) = (
Policy::parse(BEFORE).unwrap(),
Policy::parse(AFTER).unwrap(),
);
let store = session(10);
super::record_in(
&store,
None,
&Action::FsRead {
path: "rel.rs".to_owned(),
},
);
let (old, _) = call(&store, &before, CWDS[0], far()).unwrap();
assert!(old.cwdless);
let (taint, folded) = call(&store, &after, CWDS[0], far()).unwrap();
assert_eq!(folded, 11);
assert_eq!(taint, reference(&store, &after, CWDS[0]));
}