#[path = "common/hermit_binary.rs"]
mod hermit_test;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::process::Output;
fn command_output(mut command: Command, label: &str) -> Output {
hermit_test::configure_guest_execution(&mut command);
let rendered = format!("{command:?}");
let output = hermit_test::hermit_output(&rendered, &mut command);
assert!(
output.status.success(),
"{label} failed: {rendered}\nstatus: {}\nstdout:\n{}\nstderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
output
}
#[test]
fn kernel_keyring_is_deterministically_unavailable() {
let repository = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("hermit-cli should be inside the repository");
let build_root = Path::new(env!("CARGO_TARGET_TMPDIR"))
.join(format!("kernel-keyring-{}", std::process::id()));
fs::create_dir_all(&build_root).expect("failed to create guest build directory");
let cases = [
("add_key", repository.join("tests/c/add_key_enosys.c")),
(
"request_key",
repository.join("tests/c/request_key_enosys.c"),
),
("keyctl", repository.join("tests/c/keyctl_enosys.c")),
];
for (syscall, source) in cases {
let guest = build_root.join(format!("{syscall}_enosys"));
let mut compile = Command::new("cc");
compile
.args(["-O2", "-std=c11", "-Wall", "-Wextra", "-Werror"])
.arg(source)
.arg("-o")
.arg(&guest);
command_output(compile, &format!("{syscall} guest compilation"));
let mut verify = Command::new("timeout");
verify
.args(["--kill-after", "5s", "90s"])
.arg(hermit_test::hermit_binary())
.args([
"--log=info",
"--backend=ptrace",
"run",
"--strict",
"--verify",
"--panic-on-unsupported-syscalls",
"--base-env=minimal",
"--",
])
.arg(&guest);
let output = command_output(verify, &format!("{syscall} strict verification"));
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stdout.contains("Determinism verified") || stderr.contains("Determinism verified"),
"{syscall} omitted Hermit's determinism marker\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
}
}
fn keyctl_enosys_flag(output: &Output, label: &str) -> String {
let stdout = String::from_utf8_lossy(&output.stdout);
stdout
.lines()
.find(|line| line.starts_with("keyctl_enosys="))
.unwrap_or_else(|| {
panic!(
"{label} did not report a keyctl_enosys flag\nstdout:\n{stdout}\nstderr:\n{}",
String::from_utf8_lossy(&output.stderr)
)
})
.to_string()
}
#[test]
fn kernel_keyring_passes_through_with_compatibility_opt_out() {
let repository = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("hermit-cli should be inside the repository");
let build_root = Path::new(env!("CARGO_TARGET_TMPDIR"))
.join(format!("kernel-keyring-passthrough-{}", std::process::id()));
fs::create_dir_all(&build_root).expect("failed to create guest build directory");
let source = repository.join("tests/c/keyctl_passthrough.c");
let guest = build_root.join("keyctl_passthrough");
let mut compile = Command::new("cc");
compile
.args(["-O2", "-std=c11", "-Wall", "-Wextra", "-Werror"])
.arg(&source)
.arg("-o")
.arg(&guest);
command_output(compile, "keyctl passthrough guest compilation");
let native = Command::new(&guest);
let native_output = command_output(native, "native keyctl passthrough");
let native_flag = keyctl_enosys_flag(&native_output, "native keyctl passthrough");
let mut hermit = Command::new("timeout");
hermit
.args(["--kill-after", "5s", "90s"])
.arg(hermit_test::hermit_binary())
.args([
"--log=off",
"--backend=ptrace",
"run",
"--base-env=minimal",
"--allow-unsupported-syscalls",
"--",
])
.arg(&guest);
let hermit_output = command_output(hermit, "explicit Hermit keyctl compatibility opt-out");
let hermit_flag = keyctl_enosys_flag(
&hermit_output,
"explicit Hermit keyctl compatibility opt-out",
);
assert_eq!(
hermit_flag, native_flag,
"Hermit's explicit compatibility opt-out produced a different keyctl result than the host: \
native reported `{native_flag}`, Hermit reported `{hermit_flag}` \
(a Hermit-forced ENOSYS here would break the rr keyctl compatibility test)"
);
}