use std::path::PathBuf;
use hyperlight_host::sandbox::SandboxConfiguration;
use hyperlight_host::{GuestBinary, Sandbox, SandboxBuilder, UninitializedSandbox};
use hyperlight_testing::{c_simple_guest_as_pathbuf, simple_guest_as_pathbuf};
fn rust_guest_path() -> PathBuf {
simple_guest_as_pathbuf()
}
fn c_guest_path() -> PathBuf {
c_simple_guest_as_pathbuf()
}
pub fn build_rust_sandbox<C>(configure: C) -> Sandbox
where
C: FnOnce(SandboxBuilder) -> SandboxBuilder,
{
configure(SandboxBuilder::from_file(rust_guest_path()))
.build()
.unwrap()
}
pub fn new_rust_sandbox() -> Sandbox {
build_rust_sandbox(|builder| builder)
}
pub fn with_rust_sandbox<F>(f: F)
where
F: FnOnce(Sandbox),
{
f(new_rust_sandbox());
}
pub fn with_rust_sandbox_from<C, F>(configure: C, f: F)
where
C: FnOnce(SandboxBuilder) -> SandboxBuilder,
F: FnOnce(Sandbox),
{
f(build_rust_sandbox(configure));
}
pub fn with_rust_uninit_sandbox<F>(f: F)
where
F: FnOnce(UninitializedSandbox),
{
with_rust_uninit_sandbox_cfg(SandboxConfiguration::default(), f);
}
pub fn with_rust_uninit_sandbox_cfg<F>(cfg: SandboxConfiguration, f: F)
where
F: FnOnce(UninitializedSandbox),
{
let sandbox =
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), Some(cfg)).unwrap();
f(sandbox);
}
pub fn build_c_sandbox<C>(configure: C) -> Sandbox
where
C: FnOnce(SandboxBuilder) -> SandboxBuilder,
{
configure(SandboxBuilder::from_file(c_guest_path()))
.build()
.unwrap()
}
pub fn with_c_sandbox<F>(f: F)
where
F: FnOnce(Sandbox),
{
f(build_c_sandbox(|builder| builder));
}
pub fn with_c_sandbox_from<C, F>(configure: C, f: F)
where
C: FnOnce(SandboxBuilder) -> SandboxBuilder,
F: FnOnce(Sandbox),
{
f(build_c_sandbox(configure));
}
pub fn with_all_guests<F>(f: F)
where
F: Fn(PathBuf),
{
for path in [rust_guest_path(), c_guest_path()] {
f(path);
}
}
pub fn with_all_sandboxes<F>(f: F)
where
F: Fn(Sandbox),
{
with_all_guests(|path| {
f(SandboxBuilder::from_file(path).build().unwrap());
});
}