tun2proxy/tests/proxy.rs

152 lines
4.6 KiB
Rust
Raw Normal View History

2023-08-31 14:31:02 +08:00
#[cfg(target_os = "linux")]
2023-03-20 23:52:38 +01:00
#[cfg(test)]
mod tests {
extern crate reqwest;
use fork::Fork;
use nix::sys::signal;
use nix::unistd::Pid;
use serial_test::serial;
use smoltcp::wire::IpCidr;
2023-11-06 20:03:40 +08:00
use std::env;
use tun2proxy::setup::{get_default_cidrs, Setup};
use tun2proxy::util::str_to_cidr;
2023-04-10 23:24:53 +02:00
use tun2proxy::{main_entry, NetworkInterface, Options, Proxy, ProxyType};
2023-03-20 23:52:38 +01:00
#[derive(Clone, Debug)]
2023-03-20 23:52:38 +01:00
struct Test {
proxy: Proxy,
2023-03-20 23:52:38 +01:00
}
static TUN_TEST_DEVICE: &str = "tun0";
fn proxy_from_env(env_var: &str) -> Result<Proxy, String> {
2023-08-08 23:45:16 +08:00
let url = env::var(env_var).map_err(|_| format!("{env_var} environment variable not found"))?;
Proxy::from_url(url.as_str()).map_err(|_| format!("{env_var} URL cannot be parsed"))
}
fn test_from_env(env_var: &str) -> Result<Test, String> {
let proxy = proxy_from_env(env_var)?;
Ok(Test { proxy })
}
2023-03-25 01:39:46 +01:00
fn tests() -> [Result<Test, String>; 3] {
[
test_from_env("SOCKS4_SERVER"),
test_from_env("SOCKS5_SERVER"),
test_from_env("HTTP_SERVER"),
]
}
2023-03-20 23:52:38 +01:00
#[cfg(test)]
#[ctor::ctor]
fn init() {
dotenvy::dotenv().ok();
2023-03-20 23:52:38 +01:00
}
2023-03-23 14:51:56 +01:00
fn request_ip_host_http() {
reqwest::blocking::get("http://1.1.1.1").expect("failed to issue HTTP request");
}
fn request_example_https() {
reqwest::blocking::get("https://example.org").expect("failed to issue HTTPs request");
}
fn run_test<F, T>(filter: F, test_function: T)
2023-03-20 23:52:38 +01:00
where
F: Fn(&Test) -> bool,
2023-03-23 14:51:56 +01:00
T: Fn(),
2023-03-20 23:52:38 +01:00
{
for potential_test in tests() {
match potential_test {
Ok(test) => {
2023-03-24 18:04:27 +01:00
if !filter(&test) {
continue;
}
let mut bypass_ips = Vec::<IpCidr>::new();
match env::var("BYPASS_IP") {
Err(_) => {
let prefix_len = if test.proxy.addr.ip().is_ipv6() { 128 } else { 32 };
bypass_ips.push(IpCidr::new(test.proxy.addr.ip().into(), prefix_len));
}
Ok(ip_str) => bypass_ips.push(str_to_cidr(&ip_str).expect("Invalid bypass IP")),
2023-04-03 00:39:13 +02:00
};
let mut setup = Setup::new(TUN_TEST_DEVICE, bypass_ips, get_default_cidrs());
setup.configure().unwrap();
match fork::fork() {
Ok(Fork::Parent(child)) => {
2023-03-23 14:51:56 +01:00
test_function();
2023-08-08 23:45:16 +08:00
signal::kill(Pid::from_raw(child), signal::SIGINT).expect("failed to kill child");
setup.restore().unwrap();
}
Ok(Fork::Child) => {
prctl::set_death_signal(signal::SIGINT as isize).unwrap();
2023-03-24 09:27:31 +08:00
let _ = main_entry(
2023-04-10 23:24:53 +02:00
&NetworkInterface::Named(TUN_TEST_DEVICE.into()),
&test.proxy,
2023-03-23 18:11:08 +01:00
Options::new().with_virtual_dns(),
);
std::process::exit(0);
}
Err(_) => panic!(),
}
2023-03-20 23:52:38 +01:00
}
Err(_) => {
continue;
2023-03-20 23:52:38 +01:00
}
}
}
}
2023-03-22 15:39:08 +01:00
fn require_var(var: &str) {
2023-03-25 02:01:17 +01:00
env::var(var).unwrap_or_else(|_| panic!("{} environment variable required", var));
2023-03-22 15:39:08 +01:00
}
2023-03-25 01:39:46 +01:00
#[serial]
#[test_log::test]
fn test_socks4() {
require_var("SOCKS4_SERVER");
2023-08-08 23:45:16 +08:00
run_test(|test| test.proxy.proxy_type == ProxyType::Socks4, request_ip_host_http)
2023-03-25 01:39:46 +01:00
}
#[serial]
2023-03-23 18:11:08 +01:00
#[test_log::test]
2023-03-20 23:52:38 +01:00
fn test_socks5() {
2023-03-22 15:39:08 +01:00
require_var("SOCKS5_SERVER");
2023-08-08 23:45:16 +08:00
run_test(|test| test.proxy.proxy_type == ProxyType::Socks5, request_ip_host_http)
2023-03-20 23:52:38 +01:00
}
#[serial]
2023-03-23 18:11:08 +01:00
#[test_log::test]
2023-03-20 23:52:38 +01:00
fn test_http() {
2023-03-22 15:39:08 +01:00
require_var("HTTP_SERVER");
2023-08-08 23:45:16 +08:00
run_test(|test| test.proxy.proxy_type == ProxyType::Http, request_ip_host_http)
2023-03-23 14:51:56 +01:00
}
2023-03-25 01:39:46 +01:00
#[serial]
#[test_log::test]
fn test_socks4_dns() {
require_var("SOCKS4_SERVER");
2023-08-08 23:45:16 +08:00
run_test(|test| test.proxy.proxy_type == ProxyType::Socks4, request_example_https)
2023-03-25 01:39:46 +01:00
}
2023-03-23 14:51:56 +01:00
#[serial]
2023-03-23 18:11:08 +01:00
#[test_log::test]
2023-03-23 14:51:56 +01:00
fn test_socks5_dns() {
require_var("SOCKS5_SERVER");
2023-08-08 23:45:16 +08:00
run_test(|test| test.proxy.proxy_type == ProxyType::Socks5, request_example_https)
2023-03-23 14:51:56 +01:00
}
#[serial]
2023-03-23 18:11:08 +01:00
#[test_log::test]
2023-03-23 14:51:56 +01:00
fn test_http_dns() {
require_var("HTTP_SERVER");
2023-08-08 23:45:16 +08:00
run_test(|test| test.proxy.proxy_type == ProxyType::Http, request_example_https)
2023-03-20 23:52:38 +01:00
}
}