guise 0.1.2

Canonical browser fingerprint bundles, human-behavior timing, and TLS profile re-exports for the Santh fleet
Documentation
use super::classify::*;
use super::*;

#[test]
fn catalogue_has_at_least_probe_count_floor_entries() {
    let list = probes();
    assert!(
        list.len() >= PROBE_COUNT_FLOOR,
        "probe catalogue has {} entries; floor is {}",
        list.len(),
        PROBE_COUNT_FLOOR
    );
}

#[test]
fn probe_names_are_unique() {
    let list = probes();
    let mut seen = std::collections::HashSet::new();
    for p in &list {
        assert!(seen.insert(p.name), "duplicate probe name: {}", p.name);
    }
}

#[test]
fn probe_severities_distributed() {
    let list = probes();
    let high = list.iter().filter(|p| p.severity == Severity::High).count();
    let medium = list
        .iter()
        .filter(|p| p.severity == Severity::Medium)
        .count();
    let low = list.iter().filter(|p| p.severity == Severity::Low).count();
    assert!(
        high >= 4,
        "expected at least 4 High-severity probes; got {high}"
    );
    assert!(
        medium >= 5,
        "expected at least 5 Medium-severity probes; got {medium}"
    );
    assert!(
        low >= 20,
        "expected at least 20 Low-severity probes; got {low}"
    );
}

#[test]
fn classify_webdriver_ok_pass_on_false_critical_on_true() {
    // A real browser reports navigator.webdriver === false (present, false).
    assert_eq!(
        classify_webdriver_ok(&serde_json::json!(false)),
        ProbeOutcome::Pass
    );
    // null (hidden) is leniently accepted...
    assert_eq!(
        classify_webdriver_ok(&serde_json::Value::Null),
        ProbeOutcome::Pass
    );
    // ...but true is the automation leak.
    match classify_webdriver_ok(&serde_json::json!(true)) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical for webdriver===true, got {other:?}"),
    }
}

#[test]
fn classify_must_be_undefined_pass_on_null() {
    assert_eq!(
        classify_must_be_undefined(&serde_json::Value::Null),
        ProbeOutcome::Pass
    );
}

#[test]
fn classify_must_be_undefined_critical_on_value() {
    match classify_must_be_undefined(&serde_json::json!(true)) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical, got {other:?}"),
    }
}

#[test]
fn classify_must_be_true_pass_on_true() {
    assert_eq!(
        classify_must_be_true(&serde_json::json!(true)),
        ProbeOutcome::Pass
    );
}

#[test]
fn classify_must_be_true_critical_on_false() {
    match classify_must_be_true(&serde_json::json!(false)) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical, got {other:?}"),
    }
}

#[test]
fn classify_must_be_chromium_ua_pass_on_real_chrome() {
    assert_eq!(
        classify_must_be_chromium_ua(&serde_json::json!(
            "Mozilla/5.0 (X11; Linux x86_64) Chrome/134.0.0.0 Safari/537.36"
        )),
        ProbeOutcome::Pass
    );
}

#[test]
fn classify_must_be_chromium_ua_passes_chromium_vendor_profiles() {
    for ua in [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
         (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
        "Mozilla/5.0 (Linux; Android 14; SM-S928B) AppleWebKit/537.36 \
         (KHTML, like Gecko) SamsungBrowser/26.0 Chrome/126.0.0.0 Mobile Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
         (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/116.0.0.0",
    ] {
        assert_eq!(
            classify_must_be_chromium_ua(&serde_json::json!(ua)),
            ProbeOutcome::Pass,
            "{ua}"
        );
    }
}

#[test]
fn classify_must_be_chromium_ua_critical_on_headless() {
    match classify_must_be_chromium_ua(&serde_json::json!("HeadlessChrome/134")) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical, got {other:?}"),
    }
}

#[test]
fn classify_must_be_chromium_ua_drifts_on_non_chromium_browser() {
    for ua in [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 \
         (KHTML, like Gecko) Version/17.5 Safari/605.1.15",
    ] {
        match classify_must_be_chromium_ua(&serde_json::json!(ua)) {
            ProbeOutcome::Drift(message) => assert!(
                message.contains("non-Chromium"),
                "unexpected drift message: {message}"
            ),
            other => panic!("expected Drift, got {other:?}"),
        }
    }
}

// ─── Family-aware catalogue (Lane B) ─────────────────────────────────────
// Two-pair: the Chromium catalogue measures against Chrome truth; the Firefox
// catalogue drops the Chromium-only surfaces (which a real Firefox legitimately
// lacks) and substitutes the matching Firefox truths.

#[test]
fn chromium_catalogue_keeps_chrome_only_surfaces() {
    let names: std::collections::HashSet<&str> =
        probes_for(UserAgentBrowser::Chrome).iter().map(|p| p.name).collect();
    // Chrome truths are present...
    assert!(names.contains("window.chrome.runtime exists"));
    assert!(names.contains("navigator.vendor non-empty"));
    assert!(names.contains("navigator.usb exists"));
    assert!(names.contains("navigator.productSub equals '20030107'"));
    // ...and the Firefox-specific truths are NOT (Chrome would fail them).
    assert!(!names.contains("window.chrome absent (Firefox)"));
    assert!(!names.contains("navigator.vendor is empty (Firefox)"));
    // `probes()` is the back-compatible Chromium default.
    assert_eq!(probes().len(), probes_for(UserAgentBrowser::Chrome).len());
}

#[test]
fn firefox_catalogue_drops_chrome_only_and_adds_firefox_truths() {
    let names: std::collections::HashSet<&str> =
        probes_for(UserAgentBrowser::Firefox).iter().map(|p| p.name).collect();
    // EVERY Chromium-only surface is dropped — none may survive for Firefox.
    for chrome_only in super::catalogue::CHROMIUM_ONLY_PROBES {
        assert!(
            !names.contains(chrome_only),
            "Firefox catalogue must not probe Chromium-only surface {chrome_only:?}"
        );
    }
    // The Firefox truths replace them.
    assert!(names.contains("window.chrome absent (Firefox)"));
    assert!(names.contains("navigator.vendor is empty (Firefox)"));
    assert!(names.contains("navigator.userAgent is Gecko/Firefox"));
    assert!(names.contains("navigator.usb absent (Firefox)"));
    assert!(names.contains("navigator.oscpu is a string (Firefox-only surface)"));
}

#[test]
fn firefox_catalogue_probe_names_unique() {
    let list = probes_for(UserAgentBrowser::Firefox);
    let mut seen = std::collections::HashSet::new();
    for p in &list {
        assert!(seen.insert(p.name), "duplicate probe name in firefox catalogue: {}", p.name);
    }
}

#[test]
fn classify_empty_string_pass_on_empty_critical_on_value() {
    assert_eq!(
        classify_must_be_empty_string(&serde_json::json!("")),
        ProbeOutcome::Pass
    );
    match classify_must_be_empty_string(&serde_json::json!("Google Inc.")) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical for non-empty vendor, got {other:?}"),
    }
}

#[test]
fn classify_firefox_ua_pass_on_gecko_critical_on_chrome() {
    assert_eq!(
        classify_must_be_firefox_ua(&serde_json::json!(
            "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0"
        )),
        ProbeOutcome::Pass
    );
    match classify_must_be_firefox_ua(&serde_json::json!(
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
    )) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical for a Chrome UA, got {other:?}"),
    }
}

#[test]
fn classify_must_not_contain_swiftshader_critical_on_match() {
    match classify_must_not_contain_swiftshader(&serde_json::json!("Google Inc. (SwiftShader)")) {
        ProbeOutcome::Critical(_) => {}
        other => panic!("expected Critical, got {other:?}"),
    }
}

#[test]
fn drift_report_is_green_when_passed_meets_floor() {
    let report = DriftReport {
        probed: 100,
        passed: 95,
        drift: 5,
        critical: 0,
        probe_errors: 0,
        per_probe: vec![],
    };
    assert!(report.is_green());
}

#[test]
fn drift_report_is_not_green_with_critical() {
    let report = DriftReport {
        probed: 100,
        passed: 99,
        drift: 0,
        critical: 1,
        probe_errors: 0,
        per_probe: vec![],
    };
    assert!(!report.is_green());
}

#[test]
fn drift_report_is_not_green_when_passed_under_floor() {
    let report = DriftReport {
        probed: 100,
        passed: 80,
        drift: 20,
        critical: 0,
        probe_errors: 0,
        per_probe: vec![],
    };
    assert!(!report.is_green());
}

#[test]
fn render_report_includes_summary() {
    let report = DriftReport {
        probed: 5,
        passed: 5,
        drift: 0,
        critical: 0,
        probe_errors: 0,
        per_probe: vec![ProbeReport {
            name: "test_probe".to_string(),
            severity: "Low".to_string(),
            outcome: ProbeOutcome::Pass,
        }],
    };
    let rendered = render_report(&report);
    assert!(rendered.contains("STEALTH PROBE"));
    assert!(rendered.contains("test_probe"));
}

proptest::proptest! {
    #![proptest_config(proptest::test_runner::Config {
        cases: 10_000, .. proptest::test_runner::Config::default()
    })]

    #[test]
    fn prop_classify_in_range_consistent(
        n in 0u64..1_000_000,
        min in 0u64..1000,
        max in 1000u64..10_000,
    ) {
        let outcome = classify_in_range(&serde_json::json!(n), min, max);
        if (min..=max).contains(&n) {
            assert_eq!(outcome, ProbeOutcome::Pass);
        } else {
            match outcome {
                ProbeOutcome::Drift(_) => {},
                other => panic!("expected Drift, got {other:?}"),
            }
        }
    }

    #[test]
    fn prop_render_report_does_not_panic(probed in 0usize..1000, passed in 0usize..1000) {
        let report = DriftReport {
            probed,
            passed: passed.min(probed),
            drift: 0,
            critical: 0,
            probe_errors: 0,
            per_probe: vec![],
        };
        let _ = render_report(&report);
    }
}