Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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}");
Expand Down
38 changes: 28 additions & 10 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Comment thread
oech3 marked this conversation as resolved.
Resolved
}
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
Expand Down
13 changes: 3 additions & 10 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/uutests/src/lib/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Comment thread
oech3 marked this conversation as resolved.
Resolved
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 {
Expand Down
Loading