use std::path::{Path, PathBuf};
const SPAWN_ENTRY_POINT: [&str; 2] = ["support", "mod.rs"];
#[test]
fn no_test_spawns_the_tcode_binary_unguarded() {
let tests_dir = crate_tests_dir();
let offenders = unguarded_spawn_sites(&tests_dir, spawn_needle());
assert!(
offenders.is_empty(),
"these test sites spawn the `tcode` binary without the ambient-daemon \
guard, so the child registers its fixture directory in the operator's \
LIVE trusty-search daemon and the daemon writes `.trusty-search/` back \
into the sandbox (#3036, #3195). Call `support::tcode_command()` \
instead — it sets `TRUSTY_TEST_HARNESS=1` on the child:\n{}",
offenders.join("\n")
);
}
#[test]
fn only_the_exact_support_mod_path_is_exempt() {
let needle = spawn_needle();
let unguarded = format!(" let out = Command::new(env!(\"{needle}\"))");
let root = tempfile::tempdir().expect("fixture root");
for dir in ["support", "cli_support", "support/nested"] {
std::fs::create_dir_all(root.path().join(dir)).expect("mkdir fixture");
std::fs::write(root.path().join(dir).join("mod.rs"), &unguarded).expect("write fixture");
}
std::fs::write(
root.path().join("guarded_e2e.rs"),
" let out = support::tcode_command()",
)
.expect("write guarded fixture");
let offenders = unguarded_spawn_sites(root.path(), needle);
let root_prefix = format!("{}/", root.path().display());
let mut flagged: Vec<String> = offenders
.iter()
.map(|o| {
o.trim_start_matches(&root_prefix)
.split(':')
.next()
.unwrap_or(o)
.to_string()
})
.collect();
flagged.sort();
assert_eq!(
flagged,
vec!["cli_support/mod.rs", "support/nested/mod.rs"],
"only `<tests>/support/mod.rs` may be exempt; a directory that merely \
ends in `support`, and a `support/` nested deeper, must both be \
caught. Got: {offenders:?}"
);
}
#[test]
fn the_spawn_guard_actually_scans_the_test_sources() {
let tests_dir = crate_tests_dir();
let sources = rust_sources(&tests_dir);
assert!(
sources.len() > 5,
"expected this crate's `tests/` tree; found {} sources under {}",
sources.len(),
tests_dir.display()
);
assert!(
sources
.iter()
.any(|(p, _)| is_spawn_entry_point(p, &tests_dir)),
"the one allowed spawn entry point `tests/{}` was not found — the \
exemption in `no_test_spawns_the_tcode_binary_unguarded` would be \
checking a path that no longer exists",
SPAWN_ENTRY_POINT.join("/")
);
}
fn spawn_needle() -> &'static str {
concat!("CARGO_BIN_EXE", "_tcode")
}
fn crate_tests_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests")
}
fn unguarded_spawn_sites(tests_dir: &Path, needle: &str) -> Vec<String> {
let mut offenders = Vec::new();
for (path, source) in rust_sources(tests_dir) {
if is_spawn_entry_point(&path, tests_dir) {
continue;
}
for (lineno, line) in source.lines().enumerate() {
if line.contains(needle) && !line.trim_start().starts_with("//") {
offenders.push(format!(
"{}:{}: {}",
path.display(),
lineno + 1,
line.trim()
));
}
}
}
offenders
}
fn is_spawn_entry_point(path: &Path, tests_dir: &Path) -> bool {
path.strip_prefix(tests_dir).is_ok_and(|relative| {
relative
.components()
.map(|c| c.as_os_str())
.eq(SPAWN_ENTRY_POINT.iter().map(std::ffi::OsStr::new))
})
}
fn rust_sources(dir: &Path) -> Vec<(PathBuf, String)> {
let mut found = Vec::new();
let Ok(entries) = std::fs::read_dir(dir) else {
return found;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
found.extend(rust_sources(&path));
} else if path.extension().is_some_and(|e| e == "rs")
&& let Ok(source) = std::fs::read_to_string(&path)
{
found.push((path, source));
}
}
found
}