Merge pull request #4896 from shinhs0506/mkdir-test

Mkdir: explicit umask and sequential tests
This commit is contained in:
Daniel Hofstetter 2023-05-25 07:15:48 +02:00 committed by GitHub
commit 79df3937c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,16 @@
use crate::common::util::TestScenario; use crate::common::util::TestScenario;
#[cfg(not(windows))] #[cfg(not(windows))]
use libc::{mode_t, umask}; use libc::{mode_t, umask};
use once_cell::sync::Lazy;
#[cfg(not(windows))] #[cfg(not(windows))]
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::sync::Mutex;
// tests in `test_mkdir.rs` cannot run in parallel since some tests alter the umask. This may cause
// other tests to run under a wrong set of permissions
//
// when writing a test case, acquire this mutex before proceeding with the main logic of the test
static TEST_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static TEST_DIR1: &str = "mkdir_test1"; static TEST_DIR1: &str = "mkdir_test1";
static TEST_DIR2: &str = "mkdir_test2"; static TEST_DIR2: &str = "mkdir_test2";
@ -20,16 +28,19 @@ static TEST_DIR12: &str = "mkdir_test12";
#[test] #[test]
fn test_invalid_arg() { fn test_invalid_arg() {
let _guard = TEST_MUTEX.lock();
new_ucmd!().arg("--definitely-invalid").fails().code_is(1); new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
} }
#[test] #[test]
fn test_mkdir_mkdir() { fn test_mkdir_mkdir() {
let _guard = TEST_MUTEX.lock();
new_ucmd!().arg(TEST_DIR1).succeeds(); new_ucmd!().arg(TEST_DIR1).succeeds();
} }
#[test] #[test]
fn test_mkdir_verbose() { fn test_mkdir_verbose() {
let _guard = TEST_MUTEX.lock();
let expected = "mkdir: created directory 'mkdir_test1'\n"; let expected = "mkdir: created directory 'mkdir_test1'\n";
new_ucmd!() new_ucmd!()
.arg(TEST_DIR1) .arg(TEST_DIR1)
@ -40,6 +51,7 @@ fn test_mkdir_verbose() {
#[test] #[test]
fn test_mkdir_dup_dir() { fn test_mkdir_dup_dir() {
let _guard = TEST_MUTEX.lock();
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
scene.ucmd().arg(TEST_DIR2).succeeds(); scene.ucmd().arg(TEST_DIR2).succeeds();
scene.ucmd().arg(TEST_DIR2).fails(); scene.ucmd().arg(TEST_DIR2).fails();
@ -47,11 +59,13 @@ fn test_mkdir_dup_dir() {
#[test] #[test]
fn test_mkdir_mode() { fn test_mkdir_mode() {
let _guard = TEST_MUTEX.lock();
new_ucmd!().arg("-m").arg("755").arg(TEST_DIR3).succeeds(); new_ucmd!().arg("-m").arg("755").arg(TEST_DIR3).succeeds();
} }
#[test] #[test]
fn test_mkdir_parent() { fn test_mkdir_parent() {
let _guard = TEST_MUTEX.lock();
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
scene.ucmd().arg("-p").arg(TEST_DIR4).succeeds(); scene.ucmd().arg("-p").arg(TEST_DIR4).succeeds();
scene.ucmd().arg("-p").arg(TEST_DIR4).succeeds(); scene.ucmd().arg("-p").arg(TEST_DIR4).succeeds();
@ -61,11 +75,13 @@ fn test_mkdir_parent() {
#[test] #[test]
fn test_mkdir_no_parent() { fn test_mkdir_no_parent() {
let _guard = TEST_MUTEX.lock();
new_ucmd!().arg(TEST_DIR5).fails(); new_ucmd!().arg(TEST_DIR5).fails();
} }
#[test] #[test]
fn test_mkdir_dup_dir_parent() { fn test_mkdir_dup_dir_parent() {
let _guard = TEST_MUTEX.lock();
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
scene.ucmd().arg(TEST_DIR6).succeeds(); scene.ucmd().arg(TEST_DIR6).succeeds();
scene.ucmd().arg("-p").arg(TEST_DIR6).succeeds(); scene.ucmd().arg("-p").arg(TEST_DIR6).succeeds();
@ -74,6 +90,7 @@ fn test_mkdir_dup_dir_parent() {
#[cfg(not(windows))] #[cfg(not(windows))]
#[test] #[test]
fn test_mkdir_parent_mode() { fn test_mkdir_parent_mode() {
let _guard = TEST_MUTEX.lock();
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let default_umask: mode_t = 0o160; let default_umask: mode_t = 0o160;
@ -102,6 +119,7 @@ fn test_mkdir_parent_mode() {
#[cfg(not(windows))] #[cfg(not(windows))]
#[test] #[test]
fn test_mkdir_parent_mode_check_existing_parent() { fn test_mkdir_parent_mode_check_existing_parent() {
let _guard = TEST_MUTEX.lock();
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a"); at.mkdir("a");
@ -139,6 +157,7 @@ fn test_mkdir_parent_mode_check_existing_parent() {
#[test] #[test]
fn test_mkdir_dup_file() { fn test_mkdir_dup_file() {
let _guard = TEST_MUTEX.lock();
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
scene.fixtures.touch(TEST_FILE7); scene.fixtures.touch(TEST_FILE7);
scene.ucmd().arg(TEST_FILE7).fails(); scene.ucmd().arg(TEST_FILE7).fails();
@ -150,6 +169,7 @@ fn test_mkdir_dup_file() {
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn test_symbolic_mode() { fn test_symbolic_mode() {
let _guard = TEST_MUTEX.lock();
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("-m").arg("a=rwx").arg(TEST_DIR1).succeeds(); ucmd.arg("-m").arg("a=rwx").arg(TEST_DIR1).succeeds();
@ -160,16 +180,23 @@ fn test_symbolic_mode() {
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn test_symbolic_alteration() { fn test_symbolic_alteration() {
let _guard = TEST_MUTEX.lock();
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let default_umask = 0o022;
let original_umask = unsafe { umask(default_umask) };
ucmd.arg("-m").arg("-w").arg(TEST_DIR1).succeeds(); ucmd.arg("-m").arg("-w").arg(TEST_DIR1).succeeds();
let perms = at.metadata(TEST_DIR1).permissions().mode(); let perms = at.metadata(TEST_DIR1).permissions().mode();
assert_eq!(perms, 0o40577); assert_eq!(perms, 0o40577);
unsafe { umask(original_umask) };
} }
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn test_multi_symbolic() { fn test_multi_symbolic() {
let _guard = TEST_MUTEX.lock();
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("-m") ucmd.arg("-m")
@ -182,6 +209,7 @@ fn test_multi_symbolic() {
#[test] #[test]
fn test_recursive_reporting() { fn test_recursive_reporting() {
let _guard = TEST_MUTEX.lock();
new_ucmd!() new_ucmd!()
.arg("-p") .arg("-p")
.arg("-v") .arg("-v")
@ -203,6 +231,7 @@ fn test_recursive_reporting() {
#[test] #[test]
fn test_mkdir_trailing_dot() { fn test_mkdir_trailing_dot() {
let _guard = TEST_MUTEX.lock();
let scene2 = TestScenario::new("ls"); let scene2 = TestScenario::new("ls");
new_ucmd!() new_ucmd!()
.arg("-p") .arg("-p")
@ -231,6 +260,7 @@ fn test_mkdir_trailing_dot() {
#[cfg(not(windows))] #[cfg(not(windows))]
fn test_umask_compliance() { fn test_umask_compliance() {
fn test_single_case(umask_set: mode_t) { fn test_single_case(umask_set: mode_t) {
let _guard = TEST_MUTEX.lock();
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let original_umask = unsafe { umask(umask_set) }; let original_umask = unsafe { umask(umask_set) };