use std::process::Command;
fn sandlock_bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_sandlock"))
}
#[test]
fn test_learn_then_run() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "cat", "/etc/hostname"])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(),
"learn failed: stderr={}", String::from_utf8_lossy(&learn.stderr));
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "cat", "/etc/hostname"])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(),
"run failed: stderr={}", String::from_utf8_lossy(&run.stderr));
assert!(!String::from_utf8_lossy(&run.stdout).trim().is_empty(),
"expected output from cat /etc/hostname");
}
#[test]
fn test_learn_then_run_write() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let write_dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
let write_path = write_dir.path().join("run-write-test.txt");
let write_path_str = write_path.to_str().unwrap();
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "sh", "-c", &format!("echo hello > {write_path_str}")])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(),
"learn failed: {}", String::from_utf8_lossy(&learn.stderr));
assert!(!write_path.exists(), "COW isolation failed during learn");
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "sh", "-c", &format!("echo hello > {write_path_str}")])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(), "run failed: {}", String::from_utf8_lossy(&run.stderr));
assert_eq!(std::fs::read_to_string(&write_path).unwrap_or_default().trim(), "hello",
"file not written during run");
let _ = std::fs::remove_file(&write_path);
}
#[test]
fn test_learn_then_run_write_and_read() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let write_dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
let file = write_dir.path().join("rw-test.txt");
let file_str = file.to_str().unwrap();
let script = format!("echo hello > {file_str} && cat {file_str}");
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "sh", "-c", &script])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(),
"learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "sh", "-c", &script])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(), "run failed: {}", String::from_utf8_lossy(&run.stderr));
assert_eq!(String::from_utf8_lossy(&run.stdout).trim(), "hello",
"expected workload output");
let _ = std::fs::remove_file(&file);
}
#[test]
fn test_learn_then_run_network() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
std::thread::spawn(move || { let _ = listener.accept(); let _ = listener.accept(); });
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let script = format!("import socket; s=socket.socket(); s.connect(('127.0.0.1',{port})); s.close()");
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "python3", "-c", &script])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(), "learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "python3", "-c", &script])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(), "run failed: {}", String::from_utf8_lossy(&run.stderr));
}
#[test]
fn test_learn_then_run_bind() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let script = concat!(
"import socket\n",
"s = socket.socket()\n",
"s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n",
"s.bind(('127.0.0.1', 19878))\n",
"s.close()\n",
);
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "python3", "-c", script])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(), "learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "python3", "-c", script])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(), "run failed: {}", String::from_utf8_lossy(&run.stderr));
}
#[test]
fn test_learn_then_run_unix_bind() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
let sock = dir.path().join("rndtrip.sock");
let sock_str = sock.to_str().unwrap();
let script = format!(
"import socket; s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM); \
s.bind('{sock_str}'); s.close()"
);
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "python3", "-c", &script])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(),
"learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let _ = std::fs::remove_file(&sock);
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "python3", "-c", &script])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(),
"run failed (unix bind parent not in writes?): {}",
String::from_utf8_lossy(&run.stderr));
}
#[test]
fn test_learn_then_run_collapse() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
let dir_path = dir.path().canonicalize().expect("canonicalize tempdir");
let observed: Vec<String> = (1..=4)
.map(|i| {
let p = dir_path.join(format!("observed{i}.txt"));
std::fs::write(&p, "collapse me\n").expect("write observed file");
p.to_str().unwrap().to_owned()
})
.collect();
let extra = dir_path.join("unobserved.txt");
std::fs::write(&extra, "covered by directory grant\n").expect("write unobserved file");
let mut learn_cmd = sandlock_bin();
learn_cmd.args(["learn", "--collapse=4", "-o", &profile_path, "--", "cat"]);
learn_cmd.args(&observed);
let learn = learn_cmd.output().expect("failed to run sandlock learn");
assert!(learn.status.success(), "learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "cat", extra.to_str().unwrap()])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(),
"run failed with collapsed profile: {}", String::from_utf8_lossy(&run.stderr));
assert_eq!(String::from_utf8_lossy(&run.stdout).trim(), "covered by directory grant",
"unexpected output from unobserved file");
}
#[test]
fn test_learn_then_run_memory_limit() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let script = "import time; x = bytearray(20 * 1024 * 1024); time.sleep(0.05)";
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "python3", "-c", script])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(), "learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let profile_toml = std::fs::read_to_string(&profile_path).expect("read profile");
let mem_line = profile_toml.lines().find(|l| l.starts_with("memory")).unwrap();
let mib: u64 = mem_line.trim_end_matches('"').split('"').nth(1)
.and_then(|s| s.trim_end_matches('M').parse().ok()).unwrap_or(0);
assert!(mib >= 25, "expected memory >= 25M (20M + 25% headroom), got {mib}M");
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "python3", "-c", script])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(), "run failed: {}", String::from_utf8_lossy(&run.stderr));
}
#[test]
fn test_learn_then_run_process_limit() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "sh", "-c", "cat /etc/hostname & wait"])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(), "learn failed: {}", String::from_utf8_lossy(&learn.stderr));
let profile_toml = std::fs::read_to_string(&profile_path).expect("read profile");
let proc_line = profile_toml.lines().find(|l| l.starts_with("processes")).unwrap();
let procs: u32 = proc_line.split('=').nth(1)
.and_then(|s| s.trim().parse().ok()).unwrap_or(0);
assert!(procs >= 4, "expected processes >= 4 (2 observed * 2), got {procs}");
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "sh", "-c", "cat /etc/hostname & wait"])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(), "run failed: {}", String::from_utf8_lossy(&run.stderr));
}
#[test]
fn test_learn_then_run_mknod() {
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
let fifo = dir.path().join("rndtrip.fifo");
let fifo_str = fifo.to_str().unwrap();
let script = format!("import os; os.mkfifo('{fifo_str}')");
let learn = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "python3", "-c", &script])
.output()
.expect("failed to run sandlock learn");
assert!(learn.status.success(),
"learn failed: {}", String::from_utf8_lossy(&learn.stderr));
assert!(!fifo.exists(), "COW isolation failed during learn");
let run = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--", "python3", "-c", &script])
.output()
.expect("failed to run sandlock run");
assert!(run.status.success(),
"run failed (mknod not in profile writes?): {}", String::from_utf8_lossy(&run.stderr));
assert!(fifo.exists(), "FIFO not created during run");
let _ = std::fs::remove_file(&fifo);
}
#[test]
fn test_learn_then_run_merge() {
let dir_a = tempfile::TempDir::new_in("/var/tmp").expect("tempdir a");
let dir_b = tempfile::TempDir::new_in("/var/tmp").expect("tempdir b");
let file_a = dir_a.path().join("input.txt");
let file_b = dir_b.path().join("input.txt");
let secret = dir_a.path().join("secret.txt");
std::fs::write(&file_a, "data-a").expect("write file_a");
std::fs::write(&file_b, "data-b").expect("write file_b");
std::fs::write(&secret, "sensitive").expect("write secret");
let profile = tempfile::NamedTempFile::new().expect("tempfile");
let profile_path = profile.path().to_str().unwrap().to_owned();
let learn1 = sandlock_bin()
.args(["learn", "-o", &profile_path, "--", "cat", file_a.to_str().unwrap()])
.output()
.expect("failed to run sandlock learn");
assert!(learn1.status.success(), "learn1 failed: {}", String::from_utf8_lossy(&learn1.stderr));
let existing = std::fs::read_to_string(&profile_path).expect("read profile");
let deny_line = format!("deny = [\"{}\"]", secret.display());
let hardened = existing.replace("[filesystem]", &format!("[filesystem]\n{deny_line}"));
std::fs::write(&profile_path, &hardened).expect("write hardened profile");
let learn2 = sandlock_bin()
.args(["learn", "--merge", &profile_path, "--", "cat", file_b.to_str().unwrap()])
.output()
.expect("failed to run sandlock learn --merge");
assert!(learn2.status.success(), "learn2 failed: {}", String::from_utf8_lossy(&learn2.stderr));
let run_ok = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--",
"cat", file_a.to_str().unwrap(), file_b.to_str().unwrap()])
.output()
.expect("failed to run sandlock run");
assert!(run_ok.status.success(),
"run with merged profile failed: {}", String::from_utf8_lossy(&run_ok.stderr));
let run_deny = sandlock_bin()
.args(["run", "--profile-file", &profile_path, "--",
"cat", secret.to_str().unwrap()])
.output()
.expect("failed to run sandlock run (deny check)");
assert!(!run_deny.status.success(),
"secret should be blocked by deny rule after merge");
}