use super::*;
const ALL_TEST_PROFILES: &[StealthProfile] = &[
StealthProfile::ChromeWindowsStable,
StealthProfile::ChromeWindowsLegacy96,
StealthProfile::ChromeMacStable,
StealthProfile::EdgeWindowsStable,
StealthProfile::Ie11Windows,
StealthProfile::FirefoxLinux,
StealthProfile::FirefoxWindows,
StealthProfile::ChromeAndroid,
StealthProfile::SafariIphone,
StealthProfile::SafariIpad,
StealthProfile::SafariMacStable,
StealthProfile::ChromeLinux,
StealthProfile::BraveWindows,
StealthProfile::OperaWindows,
StealthProfile::SamsungInternetAndroid,
];
fn js_brackets_balanced(src: &str) -> Result<(), String> {
#[derive(PartialEq)]
enum S {
Normal,
Line,
Block,
Str(char),
}
let b = src.as_bytes();
let mut st = S::Normal;
let mut depth: i64 = 0;
let mut i = 0;
let mut escape = false;
while i < b.len() {
let c = b[i] as char;
let next = if i + 1 < b.len() { b[i + 1] as char } else { '\0' };
match st {
S::Normal => match c {
'/' if next == '/' => {
st = S::Line;
i += 2;
continue;
}
'/' if next == '*' => {
st = S::Block;
i += 2;
continue;
}
'\'' | '"' | '`' => st = S::Str(c),
'(' | '[' | '{' => depth += 1,
')' | ']' | '}' => {
depth -= 1;
if depth < 0 {
return Err(format!("unbalanced '{c}' at byte {i} (depth < 0)"));
}
}
_ => {}
},
S::Line => {
if c == '\n' {
st = S::Normal;
}
}
S::Block => {
if c == '*' && next == '/' {
st = S::Normal;
i += 2;
continue;
}
}
S::Str(delim) => {
if escape {
escape = false;
} else if c == '\\' {
escape = true;
} else if c == delim {
st = S::Normal;
}
}
}
i += 1;
}
if depth != 0 {
return Err(format!("unbalanced brackets: final depth {depth}"));
}
if st != S::Normal {
return Err("unterminated string or comment".into());
}
Ok(())
}
#[test]
fn profile_js_is_syntactically_balanced_for_all_profiles() {
for p in ALL_TEST_PROFILES {
let overrides = profile_to_overrides(p);
let js = profile_js(&overrides);
js_brackets_balanced(&js)
.unwrap_or_else(|e| panic!("profile_js for {p:?} is malformed: {e}"));
}
}
#[test]
fn js_brackets_balanced_catches_premature_comment_close() {
let bad = "(() => { /* screen.* (width/height/avail*/colorDepth/pixelDepth) NOT */ })()";
assert!(js_brackets_balanced(bad).is_err(), "must catch premature */");
let good =
"(() => { /* screen.* (width/height/availWidth/colorDepth/pixelDepth) NOT */ })()";
assert!(js_brackets_balanced(good).is_ok(), "clean comment must pass");
}
#[test]
fn every_chromium_profile_has_brands() {
for p in [
StealthProfile::ChromeWindowsStable,
StealthProfile::ChromeWindowsLegacy96,
StealthProfile::ChromeMacStable,
StealthProfile::EdgeWindowsStable,
StealthProfile::ChromeAndroid,
StealthProfile::ChromeLinux,
StealthProfile::BraveWindows,
StealthProfile::OperaWindows,
StealthProfile::SamsungInternetAndroid,
] {
let ov = profile_to_overrides(&p);
assert!(
!ov.brands.is_empty(),
"{p:?} should declare userAgentData brands"
);
}
}
#[test]
fn chromium_client_hints_derive_full_versions_from_profile_user_agents() {
let chrome = profile_client_hints(&StealthProfile::ChromeWindowsStable)
.expect("Chrome should expose User-Agent Client Hints");
assert!(chrome
.full_version_list
.iter()
.any(|entry| { entry.brand == "Google Chrome" && entry.version == "131.0.0.0" }));
assert!(chrome
.full_version_list
.iter()
.any(|entry| entry.brand == "Not?A_Brand" && entry.version == "99.0.0.0"));
assert_eq!(chrome.ua_full_version, "131.0.0.0");
assert_eq!(chrome.platform, "Windows");
assert_eq!(chrome.platform_version, "15.0.0");
assert_eq!(chrome.architecture, "x86");
assert_eq!(chrome.bitness, "64");
let opera = profile_client_hints(&StealthProfile::OperaWindows)
.expect("Opera should expose Chromium Client Hints");
assert!(opera
.full_version_list
.iter()
.any(|entry| entry.brand == "Opera" && entry.version == "116.0.0.0"));
assert!(opera
.full_version_list
.iter()
.any(|entry| entry.brand == "Chromium" && entry.version == "131.0.0.0"));
assert_eq!(opera.ua_full_version, "116.0.0.0");
let samsung = profile_client_hints(&StealthProfile::SamsungInternetAndroid)
.expect("Samsung Internet should expose Chromium Client Hints");
assert!(samsung
.full_version_list
.iter()
.any(|entry| { entry.brand == "Samsung Internet" && entry.version == "26.0.0.0" }));
assert!(samsung
.full_version_list
.iter()
.any(|entry| entry.brand == "Chromium" && entry.version == "126.0.0.0"));
assert_eq!(samsung.platform, "Android");
assert_eq!(samsung.platform_version, "14.0.0");
assert_eq!(samsung.architecture, "arm");
assert_eq!(samsung.bitness, "");
assert_eq!(samsung.model, "SM-S928B");
}
#[test]
fn non_chromium_profiles_have_no_client_hints() {
for profile in [
StealthProfile::FirefoxLinux,
StealthProfile::FirefoxWindows,
StealthProfile::SafariIphone,
StealthProfile::SafariIpad,
StealthProfile::SafariMacStable,
StealthProfile::Ie11Windows,
] {
assert!(
profile_client_hints(&profile).is_none(),
"{profile:?} must not expose User-Agent Client Hints"
);
let overrides = profile_to_overrides(&profile);
assert_eq!(client_hint_brands_json(&overrides), "[]");
assert_eq!(client_hint_full_version_list_json(&overrides), "[]");
}
}
#[test]
fn hardware_variants_are_profile_coherent() {
for profile in ALL_TEST_PROFILES {
let variants = profile_hardware_variants(*profile);
assert!(!variants.is_empty(), "{profile:?} has no hardware variants");
for hardware in variants {
assert!(hardware.screen_width > 0, "{profile:?} width is zero");
assert!(hardware.screen_height > 0, "{profile:?} height is zero");
assert!(
matches!(hardware.color_depth, 24 | 30),
"{profile:?} has uncommon color depth {}",
hardware.color_depth
);
assert!(
hardware.hardware_concurrency >= 4,
"{profile:?} reports too few logical cores"
);
assert!(hardware.device_memory >= 4, "{profile:?} memory too low");
assert_eq!(
hardware.webgl_vendor.is_empty(),
hardware.webgl_renderer.is_empty(),
"{profile:?} half-spoofed WebGL adapter (vendor/renderer emptiness disagree)"
);
let facts = profile_facts(*profile);
if facts.mobile {
assert!(
hardware.screen_width <= 1024,
"{profile:?} mobile profile has desktop width"
);
} else {
assert!(
hardware.screen_width >= 1024,
"{profile:?} desktop profile has mobile width"
);
}
}
}
}
#[test]
fn default_hardware_matches_materialized_overrides() {
for profile in ALL_TEST_PROFILES {
let hardware = profile_hardware(*profile);
let overrides = profile_to_overrides(profile);
assert_eq!(overrides.screen_width, hardware.screen_width);
assert_eq!(overrides.screen_height, hardware.screen_height);
assert_eq!(overrides.color_depth, hardware.color_depth);
assert_eq!(
overrides.hardware_concurrency,
u32::from(hardware.hardware_concurrency)
);
assert_eq!(overrides.device_memory, u32::from(hardware.device_memory));
assert_eq!(overrides.webgl_vendor, hardware.webgl_vendor);
assert_eq!(overrides.webgl_renderer, hardware.webgl_renderer);
}
}
#[test]
fn indexed_hardware_overrides_apply_selected_variant() {
let profile = StealthProfile::ChromeWindowsStable;
let hardware = profile_hardware_at(profile, 2);
let overrides = profile_to_overrides_at(&profile, 2);
assert_eq!(overrides.screen_width, hardware.screen_width);
assert_eq!(overrides.screen_height, hardware.screen_height);
assert_eq!(overrides.webgl_vendor, "Google Inc. (AMD)");
assert!(overrides.webgl_renderer.contains("Radeon RX 6700 XT"));
}
#[test]
fn safari_and_firefox_have_no_brands() {
for p in [
StealthProfile::FirefoxLinux,
StealthProfile::FirefoxWindows,
StealthProfile::SafariIphone,
StealthProfile::SafariIpad,
StealthProfile::SafariMacStable,
] {
let ov = profile_to_overrides(&p);
assert!(
ov.brands.is_empty(),
"{p:?} does not expose userAgentData / Client Hints"
);
}
}
#[test]
fn ua_matches_platform() {
let cases = [
(StealthProfile::ChromeWindowsStable, "Windows", "Win32"),
(StealthProfile::ChromeWindowsLegacy96, "Windows", "Win32"),
(StealthProfile::ChromeMacStable, "Mac OS X", "MacIntel"),
(StealthProfile::EdgeWindowsStable, "Windows", "Win32"),
(StealthProfile::Ie11Windows, "Trident/7.0", "Win32"),
(StealthProfile::FirefoxLinux, "Linux", "Linux x86_64"),
(StealthProfile::FirefoxWindows, "Windows", "Win32"),
(StealthProfile::ChromeAndroid, "Android", "Linux armv8l"),
(StealthProfile::ChromeLinux, "Linux", "Linux x86_64"),
(StealthProfile::SafariIphone, "iPhone", "iPhone"),
(StealthProfile::SafariIpad, "iPad", "iPad"),
(StealthProfile::SafariMacStable, "Mac OS X", "MacIntel"),
(StealthProfile::BraveWindows, "Windows", "Win32"),
(StealthProfile::OperaWindows, "Windows", "Win32"),
(
StealthProfile::SamsungInternetAndroid,
"Android",
"Linux armv8l",
),
];
for (p, ua_substr, platform) in cases {
let ov = profile_to_overrides(&p);
assert!(
ov.user_agent.contains(ua_substr),
"{p:?} UA missing {ua_substr}"
);
assert_eq!(ov.platform, platform, "{p:?} platform mismatch");
}
}
#[test]
fn chrome_windows_user_agent_const_matches_profile() {
let ov = profile_to_overrides(&StealthProfile::ChromeWindowsStable);
assert_eq!(ov.user_agent, CHROME_WINDOWS_STABLE_USER_AGENT);
}
#[test]
fn profile_user_agent_matches_materialized_overrides() {
for profile in ALL_TEST_PROFILES {
assert_eq!(
profile_to_overrides(profile).user_agent,
profile_user_agent(*profile),
"{profile:?} duplicated UA drifted from profile_user_agent"
);
}
}
#[test]
fn materialized_overrides_share_base_identity_facts() {
for profile in ALL_TEST_PROFILES {
let facts = profile_facts(*profile);
let overrides = profile_to_overrides(profile);
assert_eq!(overrides.user_agent, facts.user_agent);
assert_eq!(overrides.platform, facts.platform);
assert_eq!(overrides.mobile, facts.mobile);
assert_eq!(overrides.screen_width, facts.screen_width);
assert_eq!(overrides.screen_height, facts.screen_height);
assert_eq!(
overrides.languages,
facts
.languages
.iter()
.map(|language| (*language).to_string())
.collect::<Vec<_>>()
);
}
}
#[test]
fn navigation_headers_share_base_identity_facts() {
let facts = profile_facts(StealthProfile::FirefoxLinux);
assert_eq!(
profile_navigation_headers(StealthProfile::FirefoxLinux),
[
NavigationHeader {
name: USER_AGENT_HEADER,
value: facts.user_agent,
},
NavigationHeader {
name: ACCEPT_HEADER,
value: facts.accept,
},
NavigationHeader {
name: ACCEPT_LANGUAGE_HEADER,
value: facts.accept_language,
},
]
);
}
#[test]
fn browser_headers_share_base_identity_facts_with_compression() {
let facts = profile_facts(StealthProfile::FirefoxLinux);
assert_eq!(
profile_browser_headers(StealthProfile::FirefoxLinux),
[
NavigationHeader {
name: USER_AGENT_HEADER,
value: facts.user_agent,
},
NavigationHeader {
name: ACCEPT_HEADER,
value: facts.accept,
},
NavigationHeader {
name: ACCEPT_LANGUAGE_HEADER,
value: facts.accept_language,
},
NavigationHeader {
name: ACCEPT_ENCODING_HEADER,
value: facts.accept_encoding,
},
]
);
}
#[test]
fn profile_js_emits_uadata_only_when_brands_present() {
let chrome = profile_js(&profile_to_overrides(&StealthProfile::ChromeWindowsStable));
assert!(chrome.contains("userAgentData"));
assert!(chrome.contains("brands.length > 0"));
}
#[test]
fn profile_js_high_entropy_client_hints_derive_from_profile() {
let chrome = profile_js(&profile_to_overrides(&StealthProfile::ChromeWindowsStable));
assert!(chrome.contains("fullVersionList"));
assert!(chrome.contains(r#""version":"131.0.0.0""#));
assert!(chrome.contains("uaFullVersion: \"131.0.0.0\""));
assert!(!chrome.contains("uaFullVersion: '130.0.0.0'"));
let linux = profile_js(&profile_to_overrides(&StealthProfile::ChromeLinux));
assert!(linux.contains("platformVersion: \"\""));
assert!(linux.contains("platform: \"Linux\""));
}
#[test]
fn profile_js_overrides_webgl_unmasked_params() {
let js = profile_js(&profile_to_overrides(&StealthProfile::ChromeMacStable));
assert!(
js.contains("0x1F00"),
"WebGL VENDOR constant must be referenced"
);
assert!(
js.contains("0x1F01"),
"WebGL RENDERER constant must be referenced"
);
assert!(
js.contains("0x9245"),
"WebGL UNMASKED_VENDOR constant must be referenced"
);
assert!(
js.contains("0x9246"),
"WebGL UNMASKED_RENDERER constant must be referenced"
);
assert!(
js.contains("Apple M1 Pro"),
"Mac profile must pin an Apple GPU renderer"
);
}
#[test]
fn profile_js_pins_window_dimensions_and_touch_capability() {
let desktop = profile_js(&profile_to_overrides(&StealthProfile::ChromeWindowsStable));
assert!(desktop.contains("Object.defineProperty(window, 'outerWidth'"));
assert!(desktop.contains("Object.defineProperty(window, 'innerHeight'"));
assert!(desktop.contains("Object.defineProperty(Navigator.prototype, 'maxTouchPoints'"));
assert!(desktop.contains("get: __seal(() => 1920, 'get outerWidth')"));
assert!(desktop.contains("get: __seal(() => 1080 + 88, 'get outerHeight')"));
let mobile = profile_js(&profile_to_overrides(&StealthProfile::ChromeAndroid));
assert!(mobile.contains("Object.defineProperty(Navigator.prototype, 'maxTouchPoints'"));
assert!(mobile.contains("get: __seal(() => 5, 'get maxTouchPoints')"));
assert!(mobile.contains("+ 0"));
}
#[test]
fn android_profile_is_mobile_with_narrow_screen() {
let ov = profile_to_overrides(&StealthProfile::ChromeAndroid);
assert!(ov.mobile);
assert!(
ov.screen_width < 500,
"mobile profile should have a phone-sized screen"
);
}
#[test]
fn languages_default_to_english() {
for p in ALL_TEST_PROFILES {
let ov = profile_to_overrides(p);
assert_eq!(ov.languages.first().map(|s| s.as_str()), Some("en-US"));
}
}
#[test]
fn safari_profiles_have_apple_webgl_renderer() {
for p in [
StealthProfile::SafariIphone,
StealthProfile::SafariIpad,
StealthProfile::SafariMacStable,
] {
let ov = profile_to_overrides(&p);
assert!(
ov.webgl_vendor.contains("Apple"),
"{p:?} must claim an Apple GPU vendor"
);
}
}
#[test]
fn brave_profile_lists_brave_brand_first() {
let ov = profile_to_overrides(&StealthProfile::BraveWindows);
let first = ov.brands.first().map(|(b, _)| b.as_str());
assert_eq!(first, Some("Brave"));
}
#[test]
fn samsung_internet_profile_is_mobile() {
let ov = profile_to_overrides(&StealthProfile::SamsungInternetAndroid);
assert!(ov.mobile);
assert!(ov.user_agent.contains("SamsungBrowser"));
}
#[test]
fn opera_profile_lists_opera_brand() {
let ov = profile_to_overrides(&StealthProfile::OperaWindows);
let names: Vec<&str> = ov.brands.iter().map(|(b, _)| b.as_str()).collect();
assert!(names.contains(&"Opera"));
assert!(ov.user_agent.contains("OPR/"));
}
#[test]
fn safari_profiles_have_no_brave_or_opera_marker() {
for p in [
StealthProfile::SafariIphone,
StealthProfile::SafariIpad,
StealthProfile::SafariMacStable,
] {
let ov = profile_to_overrides(&p);
assert!(!ov.user_agent.contains("OPR/"));
assert!(!ov.user_agent.contains("Brave"));
assert!(ov.user_agent.contains("Safari/"));
}
}
#[test]
fn edge_profile_lists_microsoft_brand_distinct_from_chrome() {
let ov = profile_to_overrides(&StealthProfile::EdgeWindowsStable);
let brand_names: Vec<&str> = ov.brands.iter().map(|(b, _)| b.as_str()).collect();
assert!(brand_names.contains(&"Microsoft Edge"));
assert!(
!brand_names.contains(&"Google Chrome"),
"Edge profile must not list Google Chrome - that mismatch is itself a tell"
);
}