use super::*;
#[test]
fn transport_coherence_bundles_all_three_layers_coherently() {
let tc = transport_coherence_for_profile(StealthProfile::FirefoxWindows)
.expect("FirefoxWindows has a full transport profile");
assert_eq!(tc.network.os, UserAgentPlatform::Windows);
assert_eq!(tc.network.initial_ttl, 128);
assert_eq!(tc.h2.family, "firefox");
assert_eq!(tc.header_order.family, "firefox");
let lin = transport_coherence_for_profile(StealthProfile::FirefoxLinux)
.expect("FirefoxLinux has a full transport profile");
assert_eq!(lin.network.initial_ttl, 64);
assert_eq!(lin.h2.family, "firefox");
assert_eq!(lin.h2, tc.h2);
assert_ne!(lin.network, tc.network);
}
#[test]
fn transport_coherence_network_os_matches_every_profiles_claimed_os() {
for profile in crate::fingerprint::ALL_PROFILES {
if let Some(tc) = transport_coherence_for_profile(*profile) {
assert_eq!(
tc.network.os,
crate::fingerprint::profile_platform(*profile),
"{profile:?}: transport network OS disagrees with claimed platform"
);
}
}
}
#[test]
fn transport_coherence_absent_for_personas_without_http_profile() {
assert!(transport_coherence_for_profile(StealthProfile::Ie11Windows).is_none());
assert!(pair_for_profile(StealthProfile::Ie11Windows).is_none());
}
#[test]
fn transport_coherence_exposes_the_foxio_windows_ja4t_end_to_end() {
let win = transport_coherence_for_profile(StealthProfile::FirefoxWindows)
.expect("FirefoxWindows has a full transport profile");
assert_eq!(win.ja4t().unwrap(), "64240_2-1-3-1-1-4_1460_8");
let lin = transport_coherence_for_profile(StealthProfile::FirefoxLinux)
.expect("FirefoxLinux has a full transport profile");
assert_eq!(lin.ja4t().unwrap(), "*_2-4-8-1-3_1460_7");
assert_ne!(win.ja4t().unwrap(), lin.ja4t().unwrap());
}
#[test]
fn host_platform_agrees_with_compiled_target_os() {
let expected = match std::env::consts::OS {
"linux" => UserAgentPlatform::Linux,
"macos" => UserAgentPlatform::MacOs,
"windows" => UserAgentPlatform::Windows,
"android" => UserAgentPlatform::Android,
"ios" => UserAgentPlatform::Ios,
_ => UserAgentPlatform::Unknown,
};
assert_eq!(host_platform(), expected);
match host_platform() {
UserAgentPlatform::Unknown => assert_eq!(host_initial_ttl(), None),
_ => assert!(host_initial_ttl().is_some()),
}
}
#[test]
fn persona_host_network_coherence_is_host_relative_not_hardcoded() {
let host = host_platform();
let same_os_persona = match host {
UserAgentPlatform::Linux => Some(StealthProfile::FirefoxLinux),
UserAgentPlatform::Windows => Some(StealthProfile::FirefoxWindows),
UserAgentPlatform::MacOs => Some(StealthProfile::SafariMacStable),
UserAgentPlatform::Android => Some(StealthProfile::ChromeAndroid),
UserAgentPlatform::Ios => Some(StealthProfile::SafariIphone),
UserAgentPlatform::Unknown => None,
};
let cross_band_persona = match host {
UserAgentPlatform::Windows => Some(StealthProfile::FirefoxLinux),
UserAgentPlatform::Linux
| UserAgentPlatform::MacOs
| UserAgentPlatform::Android
| UserAgentPlatform::Ios => Some(StealthProfile::FirefoxWindows),
UserAgentPlatform::Unknown => None,
};
match same_os_persona {
Some(profile) => assert!(
persona_host_network_coherence(profile).is_coherent(),
"{host:?} host: a same-OS persona must be transport-coherent"
),
None => assert_eq!(
persona_host_network_coherence(StealthProfile::FirefoxLinux),
NetworkOsCoherence::Unknown,
"unmodeled host OS must yield Unknown, never a false verdict"
),
}
if let Some(profile) = cross_band_persona {
let verdict = persona_host_network_coherence(profile);
assert!(
!verdict.is_coherent(),
"{host:?} host: a cross-band persona's L2 stack must be flagged ({verdict:?})"
);
}
}
#[test]
fn every_persona_transport_layer_agrees_on_one_identity() {
for profile in crate::fingerprint::ALL_PROFILES {
assert_eq!(
persona_transport_coherence(*profile),
Ok(()),
"{profile:?}: transport layers disagree on one identity"
);
}
}
#[test]
fn every_persona_passes_full_stack_coherence() {
for profile in crate::fingerprint::ALL_PROFILES {
persona_full_stack_coherence(*profile)
.unwrap_or_else(|e| panic!("{profile:?} is full-stack incoherent: {e}"));
}
}
#[test]
fn full_stack_gate_surfaces_a_browser_half_failure() {
let mut ov = crate::fingerprint::profile_to_overrides(&StealthProfile::FirefoxLinux);
ov.platform = "Win32".into();
match full_stack_coherence_of(StealthProfile::FirefoxLinux, &ov) {
Err(PersonaIncoherence::Browser(msg)) => assert!(
msg.contains("Linux-based but platform is"),
"expected the Linux platform mismatch surfaced through the unified gate, got: {msg}"
),
other => panic!("expected a Browser-half incoherence, got {other:?}"),
}
}
#[test]
fn full_stack_gate_passes_when_both_halves_agree() {
let ov = crate::fingerprint::profile_to_overrides(&StealthProfile::FirefoxLinux);
assert_eq!(
full_stack_coherence_of(StealthProfile::FirefoxLinux, &ov),
Ok(())
);
}
#[test]
fn self_probe_coherent_on_own_wire_and_flags_cross_os_for_every_persona() {
for profile in crate::fingerprint::ALL_PROFILES {
let stack = crate::fingerprint::profile_os_network_stack(*profile);
let own_akamai = pair_for_profile(*profile).map(|(_, h2)| h2.akamai_fingerprint());
let own = WireCapture {
observed_ttl: Some(stack.initial_ttl),
akamai_fingerprint: own_akamai.clone(),
observed_ja4t: None,
};
assert_eq!(
persona_wire_self_probe(*profile, &own),
WireSelfProbe::Coherent,
"{profile:?}: own expected wire must be coherent"
);
let wrong_initial = if stack.initial_ttl == 64 { 128 } else { 64 };
let cross_os = WireCapture {
observed_ttl: Some(wrong_initial),
akamai_fingerprint: own_akamai,
observed_ja4t: None,
};
match persona_wire_self_probe(*profile, &cross_os) {
WireSelfProbe::Incoherent(mismatches) => assert!(
mismatches.iter().any(|m| matches!(
m,
WireLayerMismatch::Ttl { observed_initial_ttl, .. }
if *observed_initial_ttl == wrong_initial
)),
"{profile:?}: a cross-OS egress TTL was not flagged"
),
other => panic!("{profile:?}: expected a TTL mismatch, got {other:?}"),
}
}
}
#[test]
fn self_probe_flags_foreign_engine_akamai_for_every_modeled_persona() {
for profile in crate::fingerprint::ALL_PROFILES {
let Some((_, own_h2)) = pair_for_profile(*profile) else {
continue;
};
let foreign = [CHROME_H2, FIREFOX_H2, SAFARI_H2]
.into_iter()
.find(|h2| h2.family != own_h2.family)
.expect("three distinct H2 families ship");
let stack = crate::fingerprint::profile_os_network_stack(*profile);
let capture = WireCapture {
observed_ttl: Some(stack.initial_ttl),
akamai_fingerprint: Some(foreign.akamai_fingerprint()),
observed_ja4t: None,
};
match persona_wire_self_probe(*profile, &capture) {
WireSelfProbe::Incoherent(mismatches) => assert!(
mismatches
.iter()
.any(|m| matches!(m, WireLayerMismatch::Akamai { .. })),
"{profile:?}: a foreign-engine Akamai was not flagged"
),
other => panic!("{profile:?}: expected an Akamai mismatch, got {other:?}"),
}
}
}
#[test]
fn transport_family_folds_chromium_brands_and_rejects_ie() {
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Chrome),
Some("chrome")
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Edge),
Some("chrome")
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Opera),
Some("chrome")
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::SamsungInternet),
Some("chrome")
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Firefox),
Some("firefox")
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Safari),
Some("safari")
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::InternetExplorer),
None
);
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Unknown),
None
);
}
#[cfg(feature = "http")]
#[test]
fn every_personas_tls_family_agrees_with_its_http_family() {
use crate::fingerprint::tls_profiles::{
default_impersonate_profile_for_stealth_profile, impersonate_profile_family,
};
for profile in crate::fingerprint::ALL_PROFILES {
let Some((_, h2)) = pair_for_profile(*profile) else {
continue;
};
let tls = default_impersonate_profile_for_stealth_profile(*profile);
if let Some(tls_family) = impersonate_profile_family(tls) {
assert_eq!(
tls_family, h2.family,
"{profile:?}: TLS family {tls_family} != HTTP family {}",
h2.family
);
}
}
}
#[cfg(feature = "http")]
#[test]
fn impersonate_profile_family_classifies_browsers_and_rejects_okhttp() {
use crate::fingerprint::tls_profiles::impersonate_profile_family;
use scanclient::tls_impersonate::ImpersonateProfile;
assert_eq!(
impersonate_profile_family(ImpersonateProfile::Firefox133),
Some("firefox")
);
assert_eq!(
impersonate_profile_family(ImpersonateProfile::Chrome131),
Some("chrome")
);
assert_eq!(
impersonate_profile_family(ImpersonateProfile::Edge131),
Some("chrome")
);
assert_eq!(
impersonate_profile_family(ImpersonateProfile::Safari18),
Some("safari")
);
assert_eq!(
impersonate_profile_family(ImpersonateProfile::SafariIpad18),
Some("safari")
);
assert_eq!(
impersonate_profile_family(ImpersonateProfile::OkHttp5),
None
);
}
#[test]
fn ie11_persona_is_coherent_with_no_http_layers_to_cross_check() {
assert_eq!(
persona_transport_coherence(StealthProfile::Ie11Windows),
Ok(())
);
assert!(pair_for_profile(StealthProfile::Ie11Windows).is_none());
}
#[test]
fn coherence_gate_third_check_is_meaningful_for_firefox() {
let (_, h2) = pair_for_profile(StealthProfile::FirefoxWindows).unwrap();
assert_eq!(h2.family, "firefox");
assert_eq!(
transport_family_for_browser(UserAgentBrowser::Firefox),
Some(h2.family)
);
let (_, chrome_h2) = pair_for_profile(StealthProfile::ChromeWindowsStable).unwrap();
assert_eq!(chrome_h2.family, "chrome");
assert_ne!(chrome_h2.family, h2.family);
}
#[test]
fn configured_host_initial_ttl_reads_a_sane_value_on_linux() {
match configured_host_initial_ttl() {
Some(ttl) => assert!(ttl > 0, "configured TTL must be a positive byte, got {ttl}"),
None => assert!(
cfg!(not(target_os = "linux"))
|| std::fs::metadata("/proc/sys/net/ipv4/ip_default_ttl").is_err(),
"Linux with a readable ip_default_ttl must yield Some, not None"
),
}
}
#[test]
fn configured_ttl_composes_with_the_coherence_predicate() {
use crate::fingerprint::{infer_initial_ttl, os_network_coherence};
if let Some(ttl) = configured_host_initial_ttl() {
let inferred = infer_initial_ttl(ttl);
let band_persona = match inferred {
128 => StealthProfile::FirefoxWindows,
_ => StealthProfile::FirefoxLinux,
};
let verdict = os_network_coherence(band_persona, ttl);
if inferred == 64 || inferred == 128 {
assert!(
verdict.is_coherent(),
"configured TTL {ttl} (band {inferred}) must be coherent with its band persona"
);
}
}
}