use super::*;
use serde_json::json;
#[test]
fn redteam_set_is_nonempty_and_uniquely_named() {
let p = redteam_probes();
assert!(p.len() >= 7, "expected the full red-team set");
let mut names: Vec<&str> = p.iter().map(|x| x.name).collect();
names.sort_unstable();
names.dedup();
assert_eq!(names.len(), p.len(), "probe names must be unique");
assert!(p.iter().all(|x| !x.js.is_empty()));
}
#[test]
fn timing_surfaces_are_stochastic_others_deterministic() {
for p in redteam_probes() {
let want =
if p.name.contains("busy-loop timing") || p.name.contains("high-resolution timer") {
Determinism::Stochastic
} else {
Determinism::Deterministic
};
assert_eq!(p.determinism, want, "wrong determinism for {:?}", p.name);
}
}
#[test]
fn outcome_class_label_is_payload_independent() {
assert_eq!(ProbeOutcome::Pass.class_label(), "Pass");
assert_eq!(ProbeOutcome::Drift("a".into()).class_label(), "Drift");
assert_eq!(ProbeOutcome::Drift("b".into()).class_label(), "Drift");
assert_eq!(ProbeOutcome::Critical("x".into()).class_label(), "Critical");
assert_eq!(
ProbeOutcome::ProbeError("e".into()).class_label(),
"ProbeError"
);
}
#[test]
fn timing_cv_pass_drift_critical_boundaries() {
assert_eq!(classify_timing_cv(&json!(0.25)), ProbeOutcome::Pass);
assert_eq!(classify_timing_cv(&json!(0.10)), ProbeOutcome::Pass);
assert!(matches!(
classify_timing_cv(&json!(0.05)),
ProbeOutcome::Drift(_)
));
assert!(matches!(
classify_timing_cv(&json!(0.01)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_timing_cv(&json!(-1)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_timing_cv(&json!("x")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn missing_apis_thresholds() {
assert_eq!(classify_missing_apis(&json!(0)), ProbeOutcome::Pass);
assert!(matches!(
classify_missing_apis(&json!(2)),
ProbeOutcome::Drift(_)
));
assert!(matches!(
classify_missing_apis(&json!(5)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_missing_apis(&json!("x")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn automation_globals_empty_passes_nonempty_critical() {
assert_eq!(classify_automation_globals(&json!([])), ProbeOutcome::Pass);
match classify_automation_globals(&json!(["navigator.webdriver", "cdc_foo"])) {
ProbeOutcome::Critical(m) => {
assert!(m.contains("navigator.webdriver") && m.contains("cdc_foo"))
}
o => panic!("expected Critical, got {o:?}"),
}
assert!(matches!(
classify_automation_globals(&json!("nope")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn document_all_quirk() {
assert_eq!(
classify_document_all(&json!("undefined")),
ProbeOutcome::Pass
);
assert!(matches!(
classify_document_all(&json!("object")),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_document_all(&json!(1)),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn timer_resolution_outcomes() {
assert_eq!(classify_timer_resolution(&json!(1.0)), ProbeOutcome::Pass);
assert_eq!(classify_timer_resolution(&json!(0.05)), ProbeOutcome::Pass);
assert!(matches!(
classify_timer_resolution(&json!(-1)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_timer_resolution(&json!(0.0)),
ProbeOutcome::Drift(_)
));
assert!(matches!(
classify_timer_resolution(&json!("x")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn timer_resolution_js_does_work_between_reads_not_a_tight_loop() {
let js = super::TIMER_RESOLUTION_JS;
assert!(
js.contains("performance.now"),
"probe must read performance.now"
);
assert!(
js.contains("for(var j=0") && js.contains("while("),
"timer probe must spin with work between reads, not tight-read: {js}"
);
assert!(
!js.contains("deltas.push"),
"timer probe regressed to the tight-read delta-array form that false-drifts \
clamped (privacy-hardened) browsers"
);
}
#[test]
fn fp_api_coherence_firefox_leaking_blink_api_is_critical() {
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": true, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
match classify_fp_api_coherence(&v) {
ProbeOutcome::Critical(m) => assert!(m.contains("navigator.usb"), "{m}"),
o => panic!("expected Critical, got {o:?}"),
}
}
#[test]
fn fp_api_coherence_clean_firefox_passes() {
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": false, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
assert_eq!(classify_fp_api_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn fp_api_coherence_firefox_with_web_serial_passes() {
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": false, "hid": false, "serial": true, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
assert_eq!(classify_fp_api_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn fp_api_coherence_firefox_without_mediadevices_drifts() {
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": false, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": false
});
assert!(matches!(
classify_fp_api_coherence(&v),
ProbeOutcome::Drift(_)
));
}
#[test]
fn fp_api_coherence_chrome_with_blink_apis_passes() {
let v = json!({
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"usb": true, "hid": true, "serial": true, "bluetooth": true,
"getBattery": true, "mediaDevices": true
});
assert_eq!(classify_fp_api_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn fp_api_coherence_stripped_chrome_drifts() {
let v = json!({
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"usb": false, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
assert!(matches!(
classify_fp_api_coherence(&v),
ProbeOutcome::Drift(_)
));
}
#[test]
fn fp_api_coherence_missing_ua_is_probe_error() {
assert!(matches!(
classify_fp_api_coherence(&json!({"usb": false})),
ProbeOutcome::ProbeError(_)
));
}
const FF_UA: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0";
const CR_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
#[test]
fn error_coherence_clean_firefox_passes() {
let v = json!({
"ua": FF_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "gecko"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_firefox_with_capturestacktrace_alone_passes() {
let v = json!({
"ua": FF_UA, "captureStackTrace": true, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "gecko"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_firefox_with_v8_stacktracelimit_is_critical() {
let v = json!({
"ua": FF_UA, "captureStackTrace": true, "stackTraceLimit": true,
"prepareStackTrace": false, "stackShape": "gecko"
});
match classify_error_engine_coherence(&v) {
ProbeOutcome::Critical(m) => assert!(m.contains("Error.stackTraceLimit"), "{m}"),
o => panic!("expected Critical, got {o:?}"),
}
}
#[test]
fn error_coherence_firefox_with_v8_stack_shape_is_critical() {
let v = json!({
"ua": FF_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "v8"
});
assert!(matches!(
classify_error_engine_coherence(&v),
ProbeOutcome::Critical(_)
));
}
#[test]
fn error_coherence_real_chrome_passes() {
let v = json!({
"ua": CR_UA, "captureStackTrace": true, "stackTraceLimit": true,
"prepareStackTrace": true, "stackShape": "v8"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_chrome_without_capturestacktrace_drifts() {
let v = json!({
"ua": CR_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "v8"
});
assert!(matches!(
classify_error_engine_coherence(&v),
ProbeOutcome::Drift(_)
));
}
#[test]
fn error_coherence_firefox_no_stack_captured_passes() {
let v = json!({
"ua": FF_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "none"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_missing_ua_is_probe_error() {
assert!(matches!(
classify_error_engine_coherence(&json!({"captureStackTrace": false})),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn iframe_contentwindow_native_string_passes() {
assert_eq!(
classify_must_be_native_code(&json!(
"function get contentWindow() {\n [native code]\n}"
)),
ProbeOutcome::Pass
);
}
#[test]
fn iframe_contentwindow_wrapped_string_critical() {
match classify_must_be_native_code(&json!("function() { return {}; }")) {
ProbeOutcome::Critical(_) => {}
other => panic!("expected Critical for wrapped getter, got {other:?}"),
}
}
#[test]
fn permissions_query_native_string_passes() {
assert_eq!(
classify_must_be_native_code(&json!("function query() {\n [native code]\n}")),
ProbeOutcome::Pass
);
}
#[test]
fn redteam_probe_list_contains_new_native_code_probes() {
let names: std::collections::HashSet<&str> = redteam_probes().iter().map(|p| p.name).collect();
assert!(names.contains("redteam: HTMLIFrameElement.contentWindow getter is native code"));
assert!(names.contains("redteam: Permissions.prototype.query is native code"));
}