mod common;
use common::{HttpjailCommand, test_https_allow, test_https_blocking};
use serial_test::serial;
use std::process::{Command, Stdio};
use std::str::FromStr;
use std::thread;
use std::time::Duration;
#[test]
fn test_weak_mode_blocks_https_correctly() {
test_https_blocking(false);
}
#[test]
fn test_weak_mode_allows_https_with_allow_rule() {
test_https_allow(false);
}
#[test]
fn test_weak_mode_blocks_http_correctly() {
let result = HttpjailCommand::new()
.weak()
.js("false")
.verbose(2)
.command(vec!["curl", "--max-time", "3", "http://ifconfig.me"])
.execute();
match result {
Ok((exit_code, stdout, stderr)) => {
println!("Exit code: {}", exit_code);
println!("Stdout: {}", stdout);
println!("Stderr: {}", stderr);
assert!(
stdout.contains("Request blocked by httpjail") || exit_code != 0,
"Expected request to be blocked, but got normal response"
);
assert!(std::net::Ipv4Addr::from_str(stdout.trim()).is_err());
}
Err(e) => {
panic!("Failed to execute httpjail: {}", e);
}
}
}
#[cfg(not(target_os = "macos"))]
#[test]
fn test_weak_mode_timeout_works() {
let result = HttpjailCommand::new()
.weak()
.js("true")
.verbose(2)
.timeout(2) .command(vec!["bash", "-c", "sleep 3"]) .execute();
match result {
Ok((exit_code, _stdout, _stderr)) => {
assert_eq!(
exit_code, 124,
"Expected timeout exit code 124, got: {}",
exit_code
);
}
Err(e) => {
panic!("Failed to execute httpjail: {}", e);
}
}
}
#[test]
fn test_weak_mode_allows_localhost() {
let result = HttpjailCommand::new()
.weak()
.js("r.host === 'localhost' || r.host === '127.0.0.1'")
.verbose(1)
.command(vec!["curl", "--max-time", "3", "http://localhost:80"])
.execute();
match result {
Ok((exit_code, _stdout, stderr)) => {
println!("Exit code: {}", exit_code);
println!("Stderr: {}", stderr);
assert!(
exit_code == 7 || exit_code == 28 || exit_code == 52,
"Expected connection refused (7), timeout (28), or empty reply (52), got: {}",
exit_code
);
assert!(
!stderr.contains("403") && !stderr.contains("Forbidden"),
"Request should not be forbidden by proxy"
);
}
Err(e) => {
panic!("Failed to execute httpjail: {}", e);
}
}
}
#[test]
fn test_weak_mode_appends_no_proxy() {
let result = HttpjailCommand::new()
.weak()
.js("true")
.env("NO_PROXY", "example.com")
.verbose(2)
.command(vec!["env"])
.execute();
match result {
Ok((exit_code, stdout, _stderr)) => {
assert_eq!(exit_code, 0, "env command should succeed");
let mut found_upper = false;
let mut found_lower = false;
for line in stdout.lines() {
if let Some((key, value)) = line.split_once('=') {
if key == "NO_PROXY" {
found_upper = true;
assert!(
value.contains("example.com")
&& value.contains("localhost")
&& value.contains("127.0.0.1")
&& value.contains("::1"),
"NO_PROXY missing expected entries: {}",
value
);
} else if key == "no_proxy" {
found_lower = true;
assert!(
value.contains("example.com")
&& value.contains("localhost")
&& value.contains("127.0.0.1")
&& value.contains("::1"),
"no_proxy missing expected entries: {}",
value
);
}
}
}
assert!(found_upper, "NO_PROXY variable not found");
assert!(found_lower, "no_proxy variable not found");
}
Err(e) => panic!("Failed to execute httpjail: {}", e),
}
}
async fn start_server(http_port: u16, https_port: u16) -> Result<std::process::Child, String> {
let httpjail_path: &str = env!("CARGO_BIN_EXE_httpjail");
let mut cmd = Command::new(httpjail_path);
cmd.arg("--server")
.arg("--js")
.arg("true")
.arg("-vv")
.env("HTTPJAIL_HTTP_BIND", format!("127.0.0.1:{}", http_port))
.env("HTTPJAIL_HTTPS_BIND", format!("127.0.0.1:{}", https_port))
.env("HTTPJAIL_SKIP_KEYCHAIN_INSTALL", "1") .stdout(Stdio::null())
.stderr(Stdio::null());
let child = cmd
.spawn()
.map_err(|e| format!("Failed to start server: {}", e))?;
if !common::wait_for_server(http_port, Duration::from_secs(5)).await {
return Err(format!("Server failed to start on port {}", http_port));
}
Ok(child)
}
fn test_curl_through_proxy(http_port: u16, _https_port: u16) -> Result<String, String> {
if !verify_bind_address(http_port, "127.0.0.1") {
return Err(format!("Proxy port {} is not listening", http_port));
}
let output = Command::new("curl")
.arg("-x")
.arg(format!("http://127.0.0.1:{}", http_port))
.arg("--max-time")
.arg("10") .arg("-s")
.arg("-S") .arg("-w")
.arg("\nHTTP_CODE:%{http_code}")
.arg("http://example.com/")
.output()
.map_err(|e| format!("Failed to run curl: {}", e))?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
if !output.status.success() {
eprintln!("Curl failed - stdout: {}", stdout);
eprintln!("Curl failed - stderr: {}", stderr);
return Err(format!(
"Curl failed with status: {}, stderr: {}",
output.status, stderr
));
}
if stdout.contains("HTTP_CODE:200") || stdout.contains("Example Domain") {
Ok(stdout.to_string())
} else if stdout.contains("HTTP_CODE:403") {
Ok("Blocked by proxy".to_string())
} else {
Err(format!("Unexpected response: {}", stdout))
}
}
fn verify_bind_address(port: u16, expected_ip: &str) -> bool {
std::net::TcpStream::connect(format!("{}:{}", expected_ip, port)).is_ok()
}
#[tokio::test]
async fn test_server_mode() {
let http_port = 19876;
let https_port = 19877;
let mut server = start_server(http_port, https_port)
.await
.expect("Failed to start server");
match test_curl_through_proxy(http_port, https_port) {
Ok(_response) => {
}
Err(e) => panic!("Curl test failed: {}", e),
}
assert!(
verify_bind_address(http_port, "127.0.0.1"),
"Server should bind to localhost"
);
let _ = server.kill();
let _ = server.wait();
}
async fn start_server_with_bind(http_bind: &str, https_bind: &str) -> (std::process::Child, u16) {
let httpjail_path: &str = env!("CARGO_BIN_EXE_httpjail");
let mut child = Command::new(httpjail_path)
.arg("--server")
.arg("--js")
.arg("true")
.env("HTTPJAIL_HTTP_BIND", http_bind)
.env("HTTPJAIL_HTTPS_BIND", https_bind)
.env("HTTPJAIL_SKIP_KEYCHAIN_INSTALL", "1")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("Failed to spawn server");
let expected_port = if let Ok(port) = http_bind.parse::<u16>() {
port
} else if let Some(port_str) = http_bind.strip_prefix(':') {
port_str.parse::<u16>().unwrap_or(8080)
} else if let Ok(addr) = http_bind.parse::<std::net::SocketAddr>() {
addr.port()
} else {
8080 };
if !common::wait_for_server(expected_port, Duration::from_secs(3)).await {
child.kill().ok();
panic!("Server failed to bind to port {}", expected_port);
}
(child, expected_port)
}
#[tokio::test]
#[serial]
async fn test_server_bind_defaults() {
let (mut server, port) = start_server_with_bind("", "").await;
assert_eq!(port, 8080, "Server should default to port 8080");
server.kill().ok();
}
#[tokio::test]
#[serial]
async fn test_server_bind_port_only() {
let (mut server, port) = start_server_with_bind("19882", "19883").await;
assert_eq!(
port, 19882,
"Server should bind to specified port on all interfaces"
);
server.kill().ok();
}
#[tokio::test]
#[serial]
async fn test_server_bind_colon_prefix_port() {
let (mut server, port) = start_server_with_bind(":19892", ":19893").await;
assert_eq!(
port, 19892,
"Server should bind to specified port on all interfaces with :port format"
);
server.kill().ok();
}
#[tokio::test]
#[serial]
async fn test_server_bind_all_interfaces() {
let (mut server, port) = start_server_with_bind("0.0.0.0:19884", "0.0.0.0:19885").await;
assert_eq!(
port, 19884,
"Server should bind to specified port on 0.0.0.0"
);
server.kill().ok();
}
#[tokio::test]
#[serial]
async fn test_server_bind_ip_without_port() {
let (mut server, port) = start_server_with_bind("127.0.0.1", "127.0.0.1").await;
assert_eq!(
port, 8080,
"Server should use default port 8080 when only IP specified"
);
server.kill().ok();
}
#[test]
#[serial]
fn test_server_bind_explicit_port_zero() {
let httpjail_path: &str = env!("CARGO_BIN_EXE_httpjail");
let mut child = Command::new(httpjail_path)
.arg("--server")
.arg("--js")
.arg("true")
.env("HTTPJAIL_HTTP_BIND", "0") .env("HTTPJAIL_HTTPS_BIND", "0")
.env("HTTPJAIL_SKIP_KEYCHAIN_INSTALL", "1")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("Failed to spawn server");
thread::sleep(Duration::from_millis(500));
let not_on_default = std::net::TcpStream::connect("127.0.0.1:8080").is_err();
assert!(
not_on_default,
"Server should not bind to default port 8080 when explicit :0 provided"
);
assert!(
child.try_wait().unwrap().is_none(),
"Server should be running"
);
child.kill().ok();
}
#[test]
fn test_host_header_security() {
use std::process::Command;
let direct_result = Command::new("curl")
.args([
"-s",
"-H",
"Host: evil.com",
"--max-time",
"5",
"https://httpbingo.org/headers",
])
.output()
.expect("Failed to execute curl directly");
let direct_stdout = String::from_utf8_lossy(&direct_result.stdout);
assert!(
direct_stdout.contains("\"Host\"") && direct_stdout.contains("evil.com"),
"Direct curl should send mismatched Host header (got: {})",
direct_stdout
);
let httpjail_result = HttpjailCommand::new()
.weak()
.js("true") .command(vec![
"curl",
"-s",
"-H",
"Host: evil.com",
"--max-time",
"5",
"https://httpbingo.org/headers",
])
.execute();
assert!(httpjail_result.is_ok(), "Httpjail request should complete");
let (exit_code, stdout, _) = httpjail_result.unwrap();
assert_eq!(exit_code, 0, "Httpjail request should succeed");
assert!(
stdout.contains("\"Host\"") && stdout.contains("httpbingo.org"),
"Httpjail should correct Host header to httpbingo.org (got: {})",
stdout
);
assert!(
!stdout.contains("evil.com"),
"Httpjail should not forward mismatched Host header evil.com (got: {})",
stdout
);
}