use super::proc_magic_symlink::get_proc_self_root;
use crate::VirtualRoot;
#[test]
fn virtualroot_proc_root_maintains_isolation() {
let proc_self_root = get_proc_self_root();
if let Ok(vroot) = VirtualRoot::<()>::try_new(&proc_self_root) {
let vroot_path_str = vroot.interop_path().to_string_lossy().to_string();
assert!(
vroot_path_str.starts_with("/proc/self/root"),
"VirtualRoot lost namespace context: {vroot_path_str}"
);
}
}
#[test]
fn virtualroot_virtual_join_clamps_to_namespace() {
let proc_self_root = get_proc_self_root();
if let Ok(vroot) = VirtualRoot::<()>::try_new(&proc_self_root) {
match vroot.virtual_join("../../../etc/passwd") {
Ok(vpath) => {
let system_path = vpath.as_unvirtual().strictpath_to_string_lossy();
assert!(
system_path.starts_with("/proc/self/root"),
"VirtualPath escaped namespace: {system_path}"
);
let virtual_display = vpath.virtualpath_display().to_string();
assert!(
virtual_display.starts_with('/'),
"Virtual display must be rooted: {virtual_display}"
);
}
Err(e) => {
eprintln!("Virtual join error (acceptable): {e:?}");
}
}
}
}
#[test]
fn virtualroot_display_is_isolated() {
let proc_self_root = get_proc_self_root();
if let Ok(vroot) = VirtualRoot::<()>::try_new(&proc_self_root) {
if let Ok(vpath) = vroot.virtual_join("etc/passwd") {
let display = vpath.virtualpath_display().to_string();
assert!(display.starts_with('/'));
assert!(
!display.contains("/proc/self/root"),
"Virtual display leaked namespace path: {display}"
);
}
}
}
#[test]
fn virtualroot_absolute_input_clamped_to_namespace() {
let proc_self_root = get_proc_self_root();
if let Ok(vroot) = VirtualRoot::<()>::try_new(&proc_self_root) {
match vroot.virtual_join("/etc/shadow") {
Ok(vpath) => {
let system_path = vpath.as_unvirtual().strictpath_to_string_lossy();
assert!(
system_path.starts_with("/proc/self/root"),
"Absolute input escaped namespace: {system_path}"
);
}
Err(e) => {
eprintln!("Absolute input clamping error (acceptable): {e:?}");
}
}
}
}