use super::{check_path_traversable_by as check, TraversalError};
use crate::test_child::RestoreMode;
use std::os::unix::fs::PermissionsExt as _;
fn chmod(path: &std::path::Path, mode: u32) {
std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap();
}
fn scratch_dir() -> tempfile::TempDir {
let dir = tempfile::Builder::new()
.tempdir_in("/tmp")
.expect("tempdir directly under /tmp");
chmod(dir.path(), 0o755);
dir
}
fn assert_denied_with_eacces(r: Result<(), TraversalError>) {
match r {
Err(TraversalError::Denied { errno, .. }) => assert_eq!(errno, libc::EACCES),
other => panic!("expected Denied(EACCES), got {other:?}"),
}
}
#[skuld::test]
fn a_reachable_directory_is_ok() {
let dir = scratch_dir();
let r = check(dir.path());
assert!(r.is_ok(), "{r:?}");
}
#[skuld::test]
fn search_only_for_owner_and_other_is_ok() {
let dir = scratch_dir();
let _restore = RestoreMode::new(dir.path(), 0o755);
chmod(dir.path(), 0o101);
let r = check(dir.path());
assert!(r.is_ok(), "{r:?}");
}
#[skuld::test]
fn world_searchable_only_is_ok() {
let dir = scratch_dir();
let _restore = RestoreMode::new(dir.path(), 0o755);
chmod(dir.path(), 0o701);
let r = check(dir.path());
assert!(r.is_ok(), "{r:?}");
}
#[skuld::test]
fn read_only_with_no_execute_bit_is_denied() {
let dir = scratch_dir();
let _restore = RestoreMode::new(dir.path(), 0o755);
chmod(dir.path(), 0o404);
assert_denied_with_eacces(check(dir.path()));
}
#[skuld::test]
fn a_symlinked_ancestor_is_denied_by_the_kernel_not_modelled() {
let base = scratch_dir();
let real = base.path().join("real");
std::fs::create_dir(&real).unwrap();
std::fs::create_dir(real.join("inner")).unwrap();
let link = base.path().join("link");
std::os::unix::fs::symlink(&real, &link).unwrap();
let target = link.join("inner");
let r = check(&target);
assert!(r.is_ok(), "positive control: {r:?}");
let _restore = RestoreMode::new(&real, 0o755);
chmod(&real, 0o000);
assert_denied_with_eacces(check(&target));
}