diff --git a/tests/by-util/test_df.rs b/tests/by-util/test_df.rs index a7fe8f03a1d..e63f64dc14f 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -1212,16 +1212,13 @@ fn test_df_hides_binfmt_misc_by_default() { /// Run df inside a mount namespace where /proc is masked with tmpfs. /// Returns (success, stdout, stderr). -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", not(wasi_runner)))] fn run_df_with_masked_proc(args: &str) -> Option<(bool, String, String)> { use std::process::Command; - // Check if user namespaces are available - Command::new("unshare") - .args(["-rm", "true"]) - .status() - .ok() - .filter(std::process::ExitStatus::success)?; + if !uutests::util::unshare_mountable() { + return None; + } let df_path = TestScenario::new("df").bin_path.clone(); let output = Command::new("unshare") @@ -1242,7 +1239,7 @@ fn run_df_with_masked_proc(args: &str) -> Option<(bool, String, String)> { /// Test df fallback when /proc is masked - should work with path, fail without or with filters. #[test] -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", not(wasi_runner)))] fn test_df_masked_proc_fallback() { if let Some((ok, stdout, stderr)) = run_df_with_masked_proc(".") { assert!(ok, "df . should succeed: {stderr}"); diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index e828d29a2e2..5107d71e177 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -8,7 +8,12 @@ use rstest::rstest; use std::io::Write; -#[cfg(not(windows))] +#[cfg(not(any( + windows, + target_os = "freebsd", + target_os = "openbsd", + target_vendor = "apple" +)))] use std::path::Path; #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] use uucore::selinux::get_getfattr_output; @@ -2904,19 +2909,32 @@ fn test_mv_mixed_hardlinks_and_regular_files() { assert_ne!(r1_meta.ino(), r2_meta.ino()); } -#[cfg(not(windows))] -#[ignore = "requires access to a different filesystem"] +#[cfg(all(target_os = "linux", not(wasi_runner)))] #[test] fn test_special_file_different_filesystem() { - let (at, mut ucmd) = at_and_ucmd!(); + if !uutests::util::unshare_mountable() { + println!("TEST SKIPPED: no unprivileged mount namespace available"); + return; + } + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; at.mkfifo("f"); - // TODO Use `TestScenario::mount_temp_fs()` for this purpose and - // un-ignore this test. - std::fs::create_dir("/dev/shm/tmp").unwrap(); - ucmd.args(&["f", "/dev/shm/tmp"]).succeeds().no_output(); + at.mkdir("dest"); + let fifo = at.plus_as_string("f"); + let dest = at.plus_as_string("dest"); + let coreutils = uutests::util::get_tests_binary(); + ts.cmd("unshare") + .args(&[ + "-rm", + "sh", + "-c", + &format!( + "mount -t tmpfs tmpfs {dest} && {coreutils} mv {fifo} {dest} && test -p {dest}/f" + ), + ]) + .succeeds() + .no_output(); assert!(!at.file_exists("f")); - assert!(Path::new("/dev/shm/tmp/f").exists()); - std::fs::remove_dir_all("/dev/shm/tmp").unwrap(); } /// Test cross-device move with permission denied error diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index b282c07dd0c..86a4358ef60 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -1648,22 +1648,15 @@ fn test_preserve_root_symlink_removal_without_trailing_slash() { /// even with the guard gone: `-i` reads stdin at EOF and so answers "no" to the /// first "descend into directory?" prompt, before any unlink. **Never make this /// `-rf`.** The mount itself lives in a throwaway user + mount namespace. -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", not(wasi_runner)))] #[test] fn test_preserve_root_bind_mount_of_root() { use std::process::Command; - - let ts = TestScenario::new(util_name!()); - - // Unprivileged user namespaces are disabled in some kernels and sandboxes. - let can_unshare = Command::new("unshare") - .args(["--user", "--map-root-user", "--mount", "true"]) - .status() - .is_ok_and(|status| status.success()); - if !can_unshare { + if !uutests::util::unshare_mountable() { println!("TEST SKIPPED: no unprivileged mount namespace available"); return; } + let ts = TestScenario::new(util_name!()); // Mount point lives in this test's own temp dir, so parallel runs cannot // collide and it goes away with the scenario. diff --git a/tests/uutests/src/lib/util.rs b/tests/uutests/src/lib/util.rs index d73ae9c7364..a73d71d7ef2 100644 --- a/tests/uutests/src/lib/util.rs +++ b/tests/uutests/src/lib/util.rs @@ -3431,6 +3431,14 @@ pub fn run_ucmd_as_root_with_stdin_stdout( } } +#[cfg(all(target_os = "linux", not(wasi_runner)))] +pub fn unshare_mountable() -> bool { + Command::new("unshare") + .args(["-rm", "mount", "-t", "tmpfs", "tmpfs", "/etc"]) + .status() + .is_ok_and(|s| s.success()) +} + /// Sanity checks for test utils #[cfg(test)] mod tests {