use crate::process::{ProcessAllowlist, ProcessRunner};
use crate::workspace::WorkspaceRoot;
use std::path::Path;
pub async fn run_all<R: ProcessRunner>(runner: &R, allowlist: &ProcessAllowlist) {
rejects_an_unlisted_program(runner).await;
runs_an_allowlisted_program(runner, allowlist).await;
}
const NEVER_ALLOWED: &str = "__origin_contract_never_allowed__";
async fn rejects_an_unlisted_program<R: ProcessRunner>(runner: &R) {
let workspace_root =
WorkspaceRoot::new(Path::new("/").to_path_buf()).expect("absolute path is valid");
let result = runner
.run(NEVER_ALLOWED, &[], workspace_root.as_path())
.await;
match result {
Err(err) => {
assert_eq!(
err.kind(),
origin_domain::ErrorKind::Permission,
"a program not in the allowlist must be rejected with Permission, \
not a different error kind"
);
}
Ok(_) => panic!(
"an unlisted program `{NEVER_ALLOWED}` must be rejected before it \
reaches the operating system"
),
}
}
async fn runs_an_allowlisted_program<R: ProcessRunner>(runner: &R, allowlist: &ProcessAllowlist) {
let allowed = allowlist
.entries()
.first()
.expect("the allowlist must contain at least one program for the contract test");
let workspace_root =
WorkspaceRoot::new(Path::new("/").to_path_buf()).expect("absolute path is valid");
let result = runner
.run(allowed, &["--version".to_owned()], workspace_root.as_path())
.await;
match result {
Ok(output) => {
let _ = output;
}
Err(err) => {
assert_ne!(
err.kind(),
origin_domain::ErrorKind::Permission,
"an allowlisted program `{allowed}` must not be rejected with Permission"
);
}
}
}