#[cfg(windows)]
use std::path::{Path, PathBuf};
#[cfg(windows)]
use std::process::{Command, Stdio};
#[cfg(windows)]
use std::time::Duration;
#[cfg(windows)]
fn system_ping() -> PathBuf {
std::env::var("WINDIR")
.map(|windows_dir| Path::new(&windows_dir).join("System32").join("ping.exe"))
.unwrap_or_else(|_| PathBuf::from(r"C:\Windows\System32\ping.exe"))
}
#[cfg(windows)]
fn wow64_ping() -> PathBuf {
std::env::var("WINDIR")
.map(|windows_dir| Path::new(&windows_dir).join("SysWOW64").join("ping.exe"))
.unwrap_or_else(|_| PathBuf::from(r"C:\Windows\SysWOW64\ping.exe"))
}
#[cfg(windows)]
fn spawn_holder_via_cmd(image: &Path) -> std::process::Child {
Command::new("cmd")
.arg("/c")
.arg(image.as_os_str())
.args(["-n", "90", "127.0.0.1"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("spawn the holder process")
}
#[cfg(windows)]
fn wait_for_instances(
exe: &Path,
deadline: Duration,
) -> Vec<shun::targets::instance::RunningInstance> {
let started = std::time::Instant::now();
loop {
match shun::targets::instance::running_instances(exe) {
Ok(instances) if !instances.is_empty() => return instances,
_ => {}
}
if started.elapsed() >= deadline {
return Vec::new();
}
std::thread::sleep(Duration::from_millis(100));
}
}
#[cfg(windows)]
#[test]
#[ignore = "the windows-latest runner kills this test binary at startup \
(exit 1, no failure report) — it passes on dev hosts; re-enable once \
the runner interaction is understood"]
fn a_same_named_process_at_another_path_is_not_matched() {
use shun::targets::instance::{running_instances, stop_instances};
let wow_ping = wow64_ping();
if !wow_ping.is_file() {
eprintln!("SysWOW64\\ping.exe absent on this host — probe skipped");
return;
}
let mut holder = spawn_holder_via_cmd(&wow_ping);
let wow_instances = wait_for_instances(&wow_ping, Duration::from_secs(5));
assert!(
!wow_instances.is_empty(),
"the WOW64 holder is detected under its own path: {wow_instances:?}"
);
let wow_pids: std::collections::HashSet<u32> = wow_instances.iter().map(|i| i.pid).collect();
assert!(
!running_instances(&system_ping())
.expect("the sibling detection session opens")
.iter()
.any(|i| wow_pids.contains(&i.pid)),
"the WOW64 holder must not be matched against the System32 path"
);
let survivors = stop_instances(&wow_ping).expect("the shutdown session opens");
assert!(
survivors.is_empty(),
"the WOW64 holder stops: {survivors:?}"
);
let _ = holder.wait();
assert!(
running_instances(&wow_ping)
.expect("the post-shutdown session opens")
.is_empty(),
"the WOW64 holder's path is clear afterwards"
);
}