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",
"--no-daemon",
]);
cmd.env("KEYHOG_BACKEND", backend);
if no_gpu {
cmd.env("KEYHOG_NO_GPU", "1");
} else {
cmd.env_remove("KEYHOG_NO_GPU");
}
let out = cmd.output().expect("keyhog binary runs");
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}"));
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()
}
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 gpu = findings(path, "gpu", false);
let _ = std::fs::remove_dir_all(&dir);
assert!(
!simd.is_empty(),
"fixture should yield at least one SIMD finding (sanity)"
);
assert_eq!(
gpu, simd,
"GPU 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<_>>(),
);
}
#[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 gpu = findings(path, "gpu", false);
let _ = std::fs::remove_dir_all(&dir);
assert!(
simd.is_empty(),
"fixture should remain clean on the SIMD coalesced path, got {simd:?}"
);
assert_eq!(
gpu,
simd,
"GPU added decoded false positives absent from SIMD.\n in GPU not SIMD: {:?}",
gpu.difference(&simd).collect::<Vec<_>>(),
);
}