use std::collections::BTreeSet;
use std::process::Command;
fn bin() -> &'static str {
env!("CARGO_BIN_EXE_keyhog")
}
fn findings(path: &str, backend: &str, no_gpu: bool) -> BTreeSet<(String, String)> {
let mut cmd = Command::new(bin());
cmd.args([
"scan",
path,
"--format",
"json",
"--show-secrets",
"--no-suppress-test-fixtures",
"--daemon=off",
"--backend",
backend,
]);
if no_gpu {
cmd.arg("--no-gpu");
}
let out = cmd.output().expect("keyhog binary runs");
let code = out.status.code();
assert!(
matches!(code, Some(0 | 1)),
"explicit {backend} scan failed with {:?}: {}",
code,
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
let json: serde_json::Value = serde_json::from_str(&stdout)
.unwrap_or_else(|e| panic!("{backend} output is JSON: {e}\n{stdout}"));
let findings: BTreeSet<_> = json
.as_array()
.expect("findings array")
.iter()
.map(|f| {
(
f["detector_id"].as_str().unwrap_or_default().to_string(),
f["credential_hash"]
.as_str()
.unwrap_or_default()
.to_string(),
)
})
.collect();
assert_eq!(
code,
Some(if findings.is_empty() { 0 } else { 1 }),
"explicit {backend} exit code must agree with its finding report"
);
findings
}
fn available_gpu_routes() -> Vec<String> {
let output = Command::new(bin())
.args(["backend", "--self-test", "--json"])
.output()
.expect("backend self-test runs");
let stdout = String::from_utf8_lossy(&output.stdout);
let report: serde_json::Value = serde_json::from_str(&stdout)
.unwrap_or_else(|error| panic!("backend self-test output is JSON: {error}\n{stdout}"));
if report["gpu_available"] == false {
assert_eq!(
report["ok"], true,
"absent GPU report must be an honest skip"
);
assert_eq!(report["status"], "skip");
return Vec::new();
}
assert!(
output.status.success() && report["ok"] == true && report["status"] == "pass",
"present GPU peer failed production self-test: {}",
String::from_utf8_lossy(&output.stderr)
);
let routes: Vec<String> = report["probes"]
.as_array()
.expect("self-test probes array")
.iter()
.filter(|probe| probe["name"] == "gpu_region_presence" && probe["status"] == "pass")
.filter_map(|probe| probe["backend_route"].as_str().map(str::to_owned))
.collect();
assert!(
!routes.is_empty(),
"GPU reported available without an acquired peer self-test: {report}"
);
routes
}
fn parity_fixture() -> String {
let mut s = String::new();
s.push_str("// padding to push real tokens far past any first-window gate\n");
for i in 0..400 {
s.push_str(&format!("const PAD_LINE_{i}_NOTHING_TO_SEE_HERE = {i};\n"));
}
s.push_str("PERF_ENGG_CSB_MACHINE_STALLED_BY_CSB_MEMORY = 0x000000bd,\n");
s.push_str("CSB_TOKEN = csb_abcdefghij0123456789klmnop\n");
s
}
#[test]
fn gpu_and_simd_return_identical_findings() {
let dir = std::env::temp_dir().join(format!("kh-parity-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("mk tmp dir");
let file = dir.join("parity_fixture.txt");
std::fs::write(&file, parity_fixture()).expect("write fixture");
let path = file.to_str().unwrap();
let simd = findings(path, "simd", true);
let routes = available_gpu_routes();
assert!(
!simd.is_empty(),
"fixture should yield at least one SIMD finding (sanity)"
);
if routes.is_empty() {
eprintln!("no physical GPU peer acquired; exact GPU parity was not executed");
}
for route in routes {
let gpu = findings(path, &route, false);
assert_eq!(
gpu, simd,
"{route} and SIMD finding sets diverge (gpu_parity).\n in SIMD not GPU: {:?}\n in GPU not SIMD: {:?}",
simd.difference(&gpu).collect::<Vec<_>>(),
gpu.difference(&simd).collect::<Vec<_>>(),
);
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn gpu_does_not_add_decoded_license_key_false_positive() {
let dir = std::env::temp_dir().join(format!("kh-gpu-fp-parity-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("mk tmp dir");
let file = dir.join("mirror-neg-0009383.yaml");
std::fs::write(
&file,
concat!(
"apiVersion: v1\n",
"kind: Secret\n",
"metadata:\n",
" name: token-secret\n",
"type: Opaque\n",
"data:\n",
" token: Slc1VUstVE1aSTItV0lDREMtVDAwN00tSUFWT1A=\n",
),
)
.expect("write fixture");
let path = file.to_str().unwrap();
let simd = findings(path, "simd", true);
let routes = available_gpu_routes();
assert!(
simd.is_empty(),
"fixture should remain clean on the SIMD coalesced path, got {simd:?}"
);
if routes.is_empty() {
eprintln!("no physical GPU peer acquired; decoded-negative parity was not executed");
}
for route in routes {
let gpu = findings(path, &route, false);
assert_eq!(
gpu,
simd,
"{route} added decoded false positives absent from SIMD.\n in GPU not SIMD: {:?}",
gpu.difference(&simd).collect::<Vec<_>>(),
);
}
let _ = std::fs::remove_dir_all(&dir);
}