use std::process::Command;
#[cfg(unix)]
fn observed_token(extra_args: &[&str]) -> String {
let out = std::env::temp_dir().join(format!(
"tak-subject-env-{}-{}",
std::process::id(),
extra_args.join("-").replace(['-', '/'], "_")
));
let _ = std::fs::remove_file(&out);
let script = format!(
"printf '%s' \"${{GITHUB_TOKEN-ABSENT}}\" > {}",
out.display()
);
let mut cmd = Command::new(env!("CARGO_BIN_EXE_tak"));
cmd.args(["run", "--runs", "1", "--warmup", "0"])
.args(extra_args)
.args(["--", "sh", "-c", &script])
.env("GITHUB_TOKEN", "sentinel-must-not-leak")
.env("GH_TOKEN", "sentinel-must-not-leak");
let status = cmd.status().expect("failed to run tak");
assert!(status.success(), "tak exited with {status}");
let got = std::fs::read_to_string(&out).expect("subject wrote nothing");
std::fs::remove_file(&out).ok();
got
}
#[cfg(unix)]
#[test]
fn the_timing_path_does_not_leak_the_token() {
assert_eq!(observed_token(&["--no-counters"]), "ABSENT");
}
#[cfg(unix)]
#[test]
fn the_cachegrind_path_does_not_leak_the_token() {
if !tak_cli::measure::valgrind_available() {
eprintln!("skipping: valgrind not installed");
return;
}
assert_eq!(observed_token(&[]), "ABSENT");
}
#[cfg(unix)]
#[test]
fn allowing_the_token_lets_it_through() {
assert_eq!(
observed_token(&["--no-counters", "--env-allow", "GITHUB_TOKEN"]),
"sentinel-must-not-leak"
);
}
#[cfg(unix)]
#[test]
fn denying_another_variable_replaces_the_default_list() {
assert_eq!(
observed_token(&["--no-counters", "--env-deny", "SOMETHING_ELSE"]),
"sentinel-must-not-leak"
);
}
#[cfg(unix)]
#[test]
fn unrelated_environment_still_reaches_the_subject() {
let out = std::env::temp_dir().join(format!("tak-subject-env-keep-{}", std::process::id()));
let _ = std::fs::remove_file(&out);
let script = format!(
"printf '%s' \"${{TAK_TEST_KEEP-ABSENT}}\" > {}",
out.display()
);
let status = Command::new(env!("CARGO_BIN_EXE_tak"))
.args(["run", "--runs", "1", "--warmup", "0", "--no-counters"])
.args(["--", "sh", "-c", &script])
.env("GITHUB_TOKEN", "sentinel-must-not-leak")
.env("TAK_TEST_KEEP", "kept")
.status()
.expect("failed to run tak");
assert!(status.success(), "tak exited with {status}");
let got = std::fs::read_to_string(&out).expect("subject wrote nothing");
std::fs::remove_file(&out).ok();
assert_eq!(got, "kept");
}