2020-05-25 17:05:26 +00:00
|
|
|
use crate::common::util::*;
|
2021-10-11 20:23:52 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2022-03-25 15:04:15 +00:00
|
|
|
#[cfg(not(windows))]
|
2022-02-19 05:37:56 +00:00
|
|
|
extern crate libc;
|
2022-03-25 15:04:15 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
use self::libc::{mode_t, umask};
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
static TEST_DIR1: &str = "mkdir_test1";
|
|
|
|
static TEST_DIR2: &str = "mkdir_test2";
|
|
|
|
static TEST_DIR3: &str = "mkdir_test3";
|
|
|
|
static TEST_DIR4: &str = "mkdir_test4/mkdir_test4_1";
|
|
|
|
static TEST_DIR5: &str = "mkdir_test5/mkdir_test5_1";
|
|
|
|
static TEST_DIR6: &str = "mkdir_test6";
|
|
|
|
static TEST_FILE7: &str = "mkdir_test7";
|
2022-03-06 20:36:08 +00:00
|
|
|
static TEST_DIR8: &str = "mkdir_test8/mkdir_test8_1/mkdir_test8_2";
|
|
|
|
static TEST_DIR9: &str = "mkdir_test9/../mkdir_test9_1/../mkdir_test9_2";
|
2022-03-15 11:03:47 +00:00
|
|
|
static TEST_DIR10: &str = "mkdir_test10";
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2022-02-19 05:46:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_mkdir() {
|
2022-03-25 15:04:15 +00:00
|
|
|
new_ucmd!().arg(TEST_DIR1).succeeds();
|
2022-02-19 05:46:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 16:49:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_verbose() {
|
|
|
|
let expected = "mkdir: created directory 'mkdir_test1'\n";
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(TEST_DIR1)
|
|
|
|
.arg("-v")
|
|
|
|
.run()
|
|
|
|
.stdout_is(expected);
|
|
|
|
}
|
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_dup_dir() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2016-08-13 21:59:21 +00:00
|
|
|
scene.ucmd().arg(TEST_DIR2).succeeds();
|
2017-04-01 12:38:56 +00:00
|
|
|
scene.ucmd().arg(TEST_DIR2).fails();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_mode() {
|
2020-04-13 18:36:03 +00:00
|
|
|
new_ucmd!().arg("-m").arg("755").arg(TEST_DIR3).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_parent() {
|
2016-12-23 13:27:32 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
scene.ucmd().arg("-p").arg(TEST_DIR4).succeeds();
|
|
|
|
scene.ucmd().arg("-p").arg(TEST_DIR4).succeeds();
|
2020-12-18 13:41:59 +00:00
|
|
|
scene.ucmd().arg("--parent").arg(TEST_DIR4).succeeds();
|
|
|
|
scene.ucmd().arg("--parents").arg(TEST_DIR4).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_no_parent() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!().arg(TEST_DIR5).fails();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
2017-04-01 12:38:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_dup_dir_parent() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
scene.ucmd().arg(TEST_DIR6).succeeds();
|
|
|
|
scene.ucmd().arg("-p").arg(TEST_DIR6).succeeds();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_dup_file() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
scene.fixtures.touch(TEST_FILE7);
|
|
|
|
scene.ucmd().arg(TEST_FILE7).fails();
|
2018-05-01 11:42:11 +00:00
|
|
|
|
2017-04-01 12:38:56 +00:00
|
|
|
// mkdir should fail for a file even if -p is specified.
|
|
|
|
scene.ucmd().arg("-p").arg(TEST_FILE7).fails();
|
|
|
|
}
|
2021-10-11 20:23:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_symbolic_mode() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
ucmd.arg("-m").arg("a=rwx").arg(TEST_DIR1).succeeds();
|
|
|
|
let perms = at.metadata(TEST_DIR1).permissions().mode();
|
2022-01-30 12:55:03 +00:00
|
|
|
assert_eq!(perms, 0o40777);
|
2021-10-11 20:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_symbolic_alteration() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
ucmd.arg("-m").arg("-w").arg(TEST_DIR1).succeeds();
|
|
|
|
let perms = at.metadata(TEST_DIR1).permissions().mode();
|
2022-01-30 12:55:03 +00:00
|
|
|
assert_eq!(perms, 0o40555);
|
2021-10-11 20:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_multi_symbolic() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
ucmd.arg("-m")
|
|
|
|
.arg("u=rwx,g=rx,o=")
|
|
|
|
.arg(TEST_DIR1)
|
|
|
|
.succeeds();
|
|
|
|
let perms = at.metadata(TEST_DIR1).permissions().mode();
|
2022-01-30 12:55:03 +00:00
|
|
|
assert_eq!(perms, 0o40750);
|
2021-10-11 20:23:52 +00:00
|
|
|
}
|
2022-02-19 05:37:56 +00:00
|
|
|
|
2022-03-06 20:36:08 +00:00
|
|
|
#[test]
|
|
|
|
fn test_recursive_reporting() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("-v")
|
|
|
|
.arg(TEST_DIR8)
|
|
|
|
.succeeds()
|
|
|
|
.stdout_contains("created directory 'mkdir_test8'")
|
|
|
|
.stdout_contains("created directory 'mkdir_test8/mkdir_test8_1'")
|
|
|
|
.stdout_contains("created directory 'mkdir_test8/mkdir_test8_1/mkdir_test8_2'");
|
|
|
|
new_ucmd!().arg("-v").arg(TEST_DIR8).fails().no_stdout();
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("-v")
|
|
|
|
.arg(TEST_DIR9)
|
|
|
|
.succeeds()
|
|
|
|
.stdout_contains("created directory 'mkdir_test9'")
|
|
|
|
.stdout_contains("created directory 'mkdir_test9/../mkdir_test9_1'")
|
|
|
|
.stdout_contains("created directory 'mkdir_test9/../mkdir_test9_1/../mkdir_test9_2'");
|
|
|
|
}
|
2022-03-15 11:03:47 +00:00
|
|
|
|
2022-02-19 05:37:56 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_umask_compliance() {
|
2022-02-24 07:26:46 +00:00
|
|
|
fn test_single_case(umask_set: mode_t) {
|
2022-02-19 05:37:56 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
let original_umask = unsafe { umask(umask_set) };
|
|
|
|
|
2022-03-15 11:03:47 +00:00
|
|
|
ucmd.arg(TEST_DIR10).succeeds();
|
|
|
|
let perms = at.metadata(TEST_DIR10).permissions().mode() as mode_t;
|
2022-02-19 05:37:56 +00:00
|
|
|
|
2022-03-25 15:04:15 +00:00
|
|
|
assert_eq!(perms, (!umask_set & 0o0777) + 0o40000); // before compare, add the set GUID, UID bits
|
|
|
|
unsafe {
|
|
|
|
umask(original_umask);
|
|
|
|
} // set umask back to original
|
2022-02-19 05:37:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 15:04:15 +00:00
|
|
|
for i in 0o0..0o777 {
|
|
|
|
// tests all permission combinations
|
2022-02-19 05:37:56 +00:00
|
|
|
test_single_case(i);
|
|
|
|
}
|
|
|
|
}
|