use std::io;
use std::path::Path;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
mod other;
#[cfg(windows)]
mod windows;
#[cfg(target_os = "linux")]
use linux as platform;
#[cfg(target_os = "macos")]
use macos as platform;
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
use other as platform;
#[cfg(windows)]
use windows as platform;
pub use platform::Probe;
pub fn in_run_sandbox(lock: Option<&Path>) -> bool {
let error = match lock.map(platform::open_lock) {
Some(Ok(_)) => return false,
Some(Err(error)) => error,
None => io::Error::from(io::ErrorKind::NotFound),
};
refused_by_sandbox(&error, Probe::system())
}
pub fn refused_by_sandbox(error: &io::Error, probe: Probe) -> bool {
platform::refused_by_sandbox(error, probe)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_readable_lock_is_never_relayed() {
let dir = tempfile::tempdir().unwrap();
let lock = dir.path().join("policy.lock");
std::fs::write(&lock, "{}").unwrap();
assert!(!in_run_sandbox(Some(&lock)));
}
#[test]
fn a_missing_or_mode_locked_lock_outside_a_sandbox_is_not_relayed() {
let dir = tempfile::tempdir().unwrap();
let missing = std::fs::File::open(dir.path().join("none/policy.lock")).unwrap_err();
let outside = platform::unconfined();
assert!(!refused_by_sandbox(&missing, outside));
let by_mode = io::Error::from_raw_os_error(13);
assert!(!refused_by_sandbox(&by_mode, outside));
}
#[cfg(any(target_os = "macos", target_os = "linux", windows))]
#[test]
fn the_run_sandbox_refusal_is_relayed() {
let (error, probe) = platform::run_sandbox();
assert!(refused_by_sandbox(&error, probe));
}
}