eoka 0.15.0

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
use super::*;
use std::os::unix::fs::symlink;

fn temp_profile_dir(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "eoka-browser-test-{}-{}-{}",
        std::process::id(),
        name,
        fastrand::u64(..)
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

/// Our own cmdline never contains "--headless" (it's the `cargo test`
/// binary), so pointing a fake lock at `std::process::id()` gives us a
/// guaranteed-live pid with a known, non-headless argv — no real Chrome
/// needed to exercise the liveness/headless-detection branches.
fn lock_self(dir: &std::path::Path) {
    symlink(
        format!("some-host-{}", std::process::id()),
        dir.join("SingletonLock"),
    )
    .unwrap();
}

#[test]
fn sandbox_switches_match_every_chromium_prefix_and_case() {
    for arg in [
        "--no-sandbox",
        "-no-sandbox",
        "/no-sandbox",
        "--No-Sandbox",
        "--disable-gpu-sandbox=1",
        "--single-process",
        "--no-zygote",
        "--in-process-gpu",
    ] {
        assert!(disables_sandbox(arg), "{arg}");
    }
    for arg in [
        "--headless=new",
        "--sandbox-ipc",
        "chrome",
        "--no-sandboxing",
    ] {
        assert!(!disables_sandbox(arg), "{arg}");
    }
}

#[test]
fn no_lock_returns_none() {
    let dir = temp_profile_dir("no-lock");
    assert!(try_attach_existing(&dir, true, false).unwrap().is_none());
    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn stale_lock_is_cleared_and_returns_none() {
    let dir = temp_profile_dir("stale-lock");
    // A pid this large is never a real live process.
    symlink("some-host-2147483647", dir.join("SingletonLock")).unwrap();

    assert!(try_attach_existing(&dir, true, false).unwrap().is_none());
    assert!(
        !dir.join("SingletonLock").exists(),
        "stale lock should have been removed"
    );
    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn headless_mismatch_errors_before_touching_the_network() {
    let dir = temp_profile_dir("headless-mismatch");
    lock_self(&dir);
    // No DevToolsActivePort written — if the headless check didn't run
    // first, this would produce the "no debug port" error instead.
    let error = try_attach_existing(&dir, true, false)
        .unwrap_err()
        .to_string();
    assert!(
        error.contains("non-headless") && error.contains("requesting headless mode"),
        "expected a headless/headed mismatch error, got: {error}"
    );
    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn live_lock_without_debug_port_errors() {
    let dir = temp_profile_dir("no-port");
    lock_self(&dir);

    // Matches our own (non-headless) argv, so the headless check passes
    // and the missing-port error is what should surface.
    let error = try_attach_existing(&dir, false, false)
        .unwrap_err()
        .to_string();
    assert!(
        error.contains("debug port"),
        "expected a missing-debug-port error, got: {error}"
    );
    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn unresponsive_port_errors() {
    let dir = temp_profile_dir("bad-port");
    lock_self(&dir);
    // Port 0 is never a connectable listener.
    std::fs::write(dir.join("DevToolsActivePort"), "0\n/devtools/browser/x\n").unwrap();

    let error = try_attach_existing(&dir, false, false)
        .unwrap_err()
        .to_string();
    assert!(
        error.contains("isn't responding"),
        "expected an unresponsive-port error, got: {error}"
    );
    std::fs::remove_dir_all(&dir).unwrap();
}

// Privacy regression: profile dirs hold the cookie store and previously
// landed in /tmp with default umask (world-readable). They must be 0700.
#[cfg(unix)]
#[test]
fn profile_dirs_are_owner_only() {
    use std::os::unix::fs::PermissionsExt;

    let runtime = RuntimeDirectory::create(None).unwrap();
    let owned = BrowserProfile::create(&StealthConfig::default(), &runtime).unwrap();
    let mode = std::fs::metadata(&owned.path).unwrap().permissions().mode() & 0o777;
    assert_eq!(mode, 0o700, "spawned temp profile dir must be 0700");
    std::fs::remove_dir_all(&owned.path).unwrap();

    let custom = temp_profile_dir("user-supplied");
    std::fs::set_permissions(&custom, std::fs::Permissions::from_mode(0o750)).unwrap();
    let guard = BrowserProfile::create(
        &StealthConfig {
            user_data_dir: Some(custom.to_string_lossy().into_owned()),
            ..StealthConfig::default()
        },
        &runtime,
    )
    .unwrap();
    let mode = std::fs::metadata(&guard.path).unwrap().permissions().mode() & 0o777;
    assert_eq!(
        mode, 0o750,
        "user-supplied profile permissions must be preserved"
    );
    std::fs::remove_dir_all(&custom).unwrap();
}