From 73f54c4a900c95bb4e3551682439dacd05b90fa5 Mon Sep 17 00:00:00 2001 From: "B. Blechschmidt" Date: Thu, 30 Mar 2023 01:50:30 +0200 Subject: [PATCH] Adapt tests to use the setup functionality --- tests/proxy.rs | 89 +++++++------------------------------------------- 1 file changed, 11 insertions(+), 78 deletions(-) diff --git a/tests/proxy.rs b/tests/proxy.rs index 02f8319..4fe4dd6 100644 --- a/tests/proxy.rs +++ b/tests/proxy.rs @@ -3,25 +3,22 @@ mod tests { extern crate reqwest; use std::env; - use std::io::BufRead; - use std::net::SocketAddr; - use std::process::Command; use fork::Fork; use nix::sys::signal; use nix::unistd::Pid; use serial_test::serial; + use tun2proxy::setup::{get_default_cidrs, Setup}; use tun2proxy::{main_entry, Options, Proxy, ProxyType}; - static TUN_TEST_DEVICE: &str = "tun0"; - static ALL_ROUTES: [&str; 4] = ["0.0.0.0/1", "128.0.0.0/1", "::/1", "8000::/1"]; - #[derive(Clone, Debug)] struct Test { proxy: Proxy, } + static TUN_TEST_DEVICE: &str = "tun0"; + fn proxy_from_env(env_var: &str) -> Result { let url = env::var(env_var).map_err(|_| format!("{env_var} environment variable not found"))?; @@ -45,77 +42,6 @@ mod tests { #[ctor::ctor] fn init() { dotenvy::dotenv().ok(); - routes_setup(); - } - - #[cfg(test)] - #[ctor::dtor] - fn cleanup() { - Command::new("ip") - .args(["link", "del", TUN_TEST_DEVICE]) - .output() - .expect("failed to delete tun device"); - } - - fn routes_setup() { - let mut all_servers: Vec = Vec::new(); - - for test in tests() { - if test.is_err() { - continue; - } - all_servers.push(test.unwrap().proxy.addr); - } - - Command::new("ip") - .args(["tuntap", "add", "name", TUN_TEST_DEVICE, "mode", "tun"]) - .output() - .expect("failed to create tun device"); - - Command::new("ip") - .args(["link", "set", TUN_TEST_DEVICE, "up"]) - .output() - .expect("failed to bring up tun device"); - - let routes = Command::new("ip") - .args(["route", "show"]) - .output() - .expect("failed to get routing table"); - - // Equivalent of `ip route | grep '^default' | cut -d ' ' -f 2-` - let mut default_route_args = Vec::::new(); - for result in routes.stdout.lines() { - let line = result.unwrap(); - let split = line.split_whitespace(); - for (i, route_component) in split.enumerate() { - if i == 0 && route_component != "default" { - break; - } else if i == 0 { - continue; - } - default_route_args.push(String::from(route_component)); - } - if !default_route_args.is_empty() { - break; - } - } - - for server in all_servers { - let mut proxy_route = vec!["route".to_string(), "add".to_string()]; - proxy_route.push(server.ip().to_string()); - proxy_route.extend(default_route_args.clone()); - Command::new("ip") - .args(proxy_route) - .output() - .expect("failed to get routing table"); - } - - for route in ALL_ROUTES { - Command::new("ip") - .args(["route", "add", route, "dev", TUN_TEST_DEVICE]) - .output() - .expect("failed to add route"); - } } fn request_ip_host_http() { @@ -141,13 +67,20 @@ mod tests { match fork::fork() { Ok(Fork::Parent(child)) => { test_function(); - signal::kill(Pid::from_raw(child), signal::SIGKILL) + signal::kill(Pid::from_raw(child), signal::SIGINT) .expect("failed to kill child"); nix::sys::wait::waitpid(Pid::from_raw(child), None) .expect("failed to wait for child"); } Ok(Fork::Child) => { prctl::set_death_signal(signal::SIGKILL as isize).unwrap(); // 9 == SIGKILL + + let _setup = Setup::new( + TUN_TEST_DEVICE, + &test.proxy.addr.ip(), + get_default_cidrs(), + false, + ); let _ = main_entry( TUN_TEST_DEVICE, test.proxy,