#[cfg(any(target_os = "macos", target_os = "linux", windows))]
mod cases {
use std::io::{Read, Write};
use std::net::{Ipv4Addr, TcpListener};
use std::process::Command;
use std::thread;
use crabpot::{Config, Sandbox};
#[cfg(windows)]
const NULL: &str = "NUL";
#[cfg(not(windows))]
const NULL: &str = "/dev/null";
fn config(allowed: &[&str], denied: &[&str]) -> Config {
Config::from_json(
&serde_json::json!({
"network": {"allowedDomains": allowed, "deniedDomains": denied},
"filesystem": {"denyRead": [], "allowWrite": [], "denyWrite": []},
})
.to_string(),
)
.expect("the config parses")
}
fn origin() -> u16 {
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).expect("the origin binds");
let port = listener
.local_addr()
.expect("the origin has an address")
.port();
thread::spawn(move || {
for connection in listener.incoming() {
let Ok(mut client) = connection else { continue };
let mut head = Vec::new();
let mut buf = [0u8; 1024];
while !head.ends_with(b"\r\n\r\n") {
match client.read(&mut buf) {
Ok(read @ 1..) => head.extend_from_slice(&buf[..read]),
_ => break,
}
}
let _ = client.write_all(
b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
);
}
});
port
}
fn status(sandbox: &Sandbox, port: u16) -> String {
let mut curl = Command::new("curl");
curl.args(["-s", "-o", NULL, "-w", "%{http_code}", "--max-time", "20"])
.args(["--noproxy", "example.invalid"])
.arg(format!("http://127.0.0.1:{port}/"));
let output = sandbox
.command(&curl)
.expect("the command is wrapped")
.output()
.expect("the wrapped command runs");
String::from_utf8_lossy(&output.stdout).into_owned()
}
pub fn a_running_sandbox_follows_replaced_network_lists() {
let port = origin();
let sandbox = Sandbox::start(&config(&[], &[])).expect("the sandbox starts");
assert_eq!(status(&sandbox, port), "403", "nothing is allowed yet");
sandbox.replace_network_lists(&config(&["127.0.0.1"], &[]));
assert_eq!(status(&sandbox, port), "200", "the added domain");
sandbox.replace_network_lists(&config(&["127.0.0.1"], &["127.0.0.1"]));
assert_eq!(status(&sandbox, port), "403", "the denied domain wins");
sandbox.replace_network_lists(&config(&["127.0.0.1"], &[]));
assert_eq!(status(&sandbox, port), "200", "the denial is lifted");
}
pub fn a_sandbox_can_be_shared_between_threads() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Sandbox>();
let port = origin();
let sandbox = Sandbox::start(&config(&[], &[])).expect("the sandbox starts");
let open = config(&["127.0.0.1"], &[]);
let shut = config(&[], &[]);
thread::scope(|scope| {
for _ in 0..4 {
scope.spawn(|| {
for _ in 0..20 {
sandbox.replace_network_lists(&shut);
sandbox.replace_network_lists(&open);
}
});
}
});
assert_eq!(status(&sandbox, port), "200", "the last list is whole");
}
}
#[cfg(any(target_os = "macos", target_os = "linux", windows))]
fn main() {
crabpot::run_from_args();
#[cfg(target_os = "linux")]
if std::env::var_os("CI").is_none() {
eprintln!("skipped: set CI to run a Linux sandbox");
return;
}
let cases: [(&str, fn()); 2] = [
(
"a_running_sandbox_follows_replaced_network_lists",
cases::a_running_sandbox_follows_replaced_network_lists,
),
(
"a_sandbox_can_be_shared_between_threads",
cases::a_sandbox_can_be_shared_between_threads,
),
];
for (name, case) in cases {
case();
println!("test {name} ... ok");
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
fn main() {}