use super::*;
#[test]
fn the_shim_reads_the_workspace_s_own_policy_at_its_live_tip() {
let w = World::new();
let env = crate::xdg::Env::from_pairs([
("HOME", w.dir.path().join("home").display().to_string()),
(
"XDG_STATE_HOME",
w.dir.path().join("state").display().to_string(),
),
]);
let verdict = |w: &World, command: &str| {
let mut out: Vec<u8> = Vec::new();
assert_eq!(
run(
&mut request("bash", json!({ "command": command })).as_bytes(),
&mut out,
&env,
&w.workspace(),
),
0
);
String::from_utf8(out).unwrap()
};
assert!(verdict(&w, "curl x").contains("pass"));
assert!(verdict(&w, "python go.py").contains("pass"));
w.policy(concat!(
"table:\n",
" open-world: hold\n",
"rules:\n",
" python: destructive\n",
"secrets:\n",
" - .kube\n",
));
assert!(verdict(&w, "curl x").contains("hold"));
assert!(verdict(&w, "python go.py").contains("refuse"));
assert!(verdict(&w, "cat .kube/config").contains("refuse"));
}
#[test]
fn the_reason_carries_a_bounded_input_summary() {
let w = World::new();
let parked = Consult {
policy: Policy::parse("table:\n open-world: hold\n"),
..w.consult()
};
let short = adjudicate(
&parked,
&Request::parse(&request("bash", json!({"command": "curl x"}))).unwrap(),
);
let Verdict::Hold(reason) = short else {
panic!("the override holds open-world");
};
assert!(reason.contains("curl x"), "{reason}");
assert!(reason.contains("open-world"), "{reason}");
let long = adjudicate(
&parked,
&Request::parse(&request(
"bash",
json!({"command": "curl ".to_owned() + &"x".repeat(500) }),
))
.unwrap(),
);
let Verdict::Hold(reason) = long else {
panic!("the override holds open-world");
};
assert!(reason.contains('…'), "a runaway input is cut: {reason}");
assert!(reason.len() < 400, "and stays readable: {}", reason.len());
}