2023-08-21 08:49:27 +00:00
|
|
|
// This file is part of the uutils coreutils package.
|
|
|
|
//
|
|
|
|
// For the full copyright and license information, please view the LICENSE
|
|
|
|
// file that was distributed with this source code.
|
2023-03-20 13:51:19 +00:00
|
|
|
use crate::common::util::TestScenario;
|
2021-10-11 20:23:52 +00:00
|
|
|
#[cfg(not(windows))]
|
2024-05-19 19:56:41 +00:00
|
|
|
use libc::mode_t;
|
2022-03-25 15:04:15 +00:00
|
|
|
#[cfg(not(windows))]
|
2023-04-10 06:31:31 +00:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2022-09-10 16:38:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_arg() {
|
|
|
|
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
|
|
|
|
}
|
|
|
|
|
2022-02-19 05:46:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_mkdir() {
|
2023-06-07 08:42:01 +00:00
|
|
|
new_ucmd!().arg("test_dir").succeeds();
|
2022-02-19 05:46:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 16:49:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_verbose() {
|
2023-06-07 08:42:01 +00:00
|
|
|
let expected = "mkdir: created directory 'test_dir'\n";
|
2020-11-08 16:49:41 +00:00
|
|
|
new_ucmd!()
|
2023-06-07 08:42:01 +00:00
|
|
|
.arg("test_dir")
|
2020-11-08 16:49:41 +00:00
|
|
|
.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!());
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir";
|
|
|
|
|
|
|
|
scene.ucmd().arg(test_dir).succeeds();
|
|
|
|
scene.ucmd().arg(test_dir).fails();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_mode() {
|
2023-06-07 08:42:01 +00:00
|
|
|
new_ucmd!().arg("-m").arg("755").arg("test_dir").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!());
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "parent_dir/child_dir";
|
|
|
|
|
|
|
|
scene.ucmd().arg("-p").arg(test_dir).succeeds();
|
|
|
|
scene.ucmd().arg("-p").arg(test_dir).succeeds();
|
|
|
|
scene.ucmd().arg("--parent").arg(test_dir).succeeds();
|
|
|
|
scene.ucmd().arg("--parents").arg(test_dir).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_no_parent() {
|
2023-06-07 08:42:01 +00:00
|
|
|
new_ucmd!().arg("parent_dir/child_dir").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!());
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir";
|
|
|
|
|
|
|
|
scene.ucmd().arg(test_dir).succeeds();
|
|
|
|
scene.ucmd().arg("-p").arg(test_dir).succeeds();
|
2017-04-01 12:38:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 02:12:38 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_parent_mode() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
2023-05-22 02:31:04 +00:00
|
|
|
let default_umask: mode_t = 0o160;
|
2023-05-19 02:12:38 +00:00
|
|
|
|
2024-05-19 19:56:41 +00:00
|
|
|
ucmd.arg("-p")
|
|
|
|
.arg("a/b")
|
|
|
|
.umask(default_umask)
|
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
2023-05-19 02:12:38 +00:00
|
|
|
|
|
|
|
assert!(at.dir_exists("a"));
|
|
|
|
// parents created by -p have permissions set to "=rwx,u+wx"
|
|
|
|
assert_eq!(
|
2023-05-19 02:23:08 +00:00
|
|
|
at.metadata("a").permissions().mode() as mode_t,
|
2023-05-19 02:12:38 +00:00
|
|
|
((!default_umask & 0o777) | 0o300) + 0o40000
|
|
|
|
);
|
|
|
|
assert!(at.dir_exists("a/b"));
|
|
|
|
// sub directory's permission is determined only by the umask
|
|
|
|
assert_eq!(
|
2023-05-19 02:23:08 +00:00
|
|
|
at.metadata("a/b").permissions().mode() as mode_t,
|
2023-05-19 02:12:38 +00:00
|
|
|
(!default_umask & 0o777) + 0o40000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[test]
|
|
|
|
fn test_mkdir_parent_mode_check_existing_parent() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
at.mkdir("a");
|
2024-05-19 19:56:41 +00:00
|
|
|
let parent_a_perms = at.metadata("a").permissions().mode();
|
2023-05-19 02:12:38 +00:00
|
|
|
|
2023-05-22 02:31:04 +00:00
|
|
|
let default_umask: mode_t = 0o160;
|
|
|
|
|
2023-05-19 02:12:38 +00:00
|
|
|
ucmd.arg("-p")
|
|
|
|
.arg("a/b/c")
|
2024-05-19 19:56:41 +00:00
|
|
|
.umask(default_umask)
|
2023-05-19 02:12:38 +00:00
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
|
|
|
|
assert!(at.dir_exists("a"));
|
|
|
|
// parent dirs that already exist do not get their permissions modified
|
2024-05-19 19:56:41 +00:00
|
|
|
assert_eq!(at.metadata("a").permissions().mode(), parent_a_perms);
|
2023-05-19 02:12:38 +00:00
|
|
|
assert!(at.dir_exists("a/b"));
|
|
|
|
assert_eq!(
|
2023-05-19 02:23:08 +00:00
|
|
|
at.metadata("a/b").permissions().mode() as mode_t,
|
2023-05-19 02:12:38 +00:00
|
|
|
((!default_umask & 0o777) | 0o300) + 0o40000
|
|
|
|
);
|
|
|
|
assert!(at.dir_exists("a/b/c"));
|
|
|
|
assert_eq!(
|
2023-05-19 02:23:08 +00:00
|
|
|
at.metadata("a/b/c").permissions().mode() as mode_t,
|
2023-05-19 02:12:38 +00:00
|
|
|
(!default_umask & 0o777) + 0o40000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-01 12:38:56 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_dup_file() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_file = "test_file.txt";
|
|
|
|
|
|
|
|
scene.fixtures.touch(test_file);
|
|
|
|
|
|
|
|
scene.ucmd().arg(test_file).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.
|
2023-06-07 08:42:01 +00:00
|
|
|
scene.ucmd().arg("-p").arg(test_file).fails();
|
2017-04-01 12:38:56 +00:00
|
|
|
}
|
2021-10-11 20:23:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_symbolic_mode() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir";
|
2021-10-11 20:23:52 +00:00
|
|
|
|
2023-06-07 08:42:01 +00:00
|
|
|
ucmd.arg("-m").arg("a=rwx").arg(test_dir).succeeds();
|
|
|
|
let perms = at.metadata(test_dir).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!();
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir";
|
2021-10-11 20:23:52 +00:00
|
|
|
|
2023-05-23 22:45:04 +00:00
|
|
|
let default_umask = 0o022;
|
|
|
|
|
2024-05-19 19:56:41 +00:00
|
|
|
ucmd.arg("-m")
|
|
|
|
.arg("-w")
|
|
|
|
.arg(test_dir)
|
|
|
|
.umask(default_umask)
|
|
|
|
.succeeds();
|
2023-06-07 08:42:01 +00:00
|
|
|
let perms = at.metadata(test_dir).permissions().mode();
|
2023-05-19 02:12:38 +00:00
|
|
|
assert_eq!(perms, 0o40577);
|
2021-10-11 20:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_multi_symbolic() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir";
|
2021-10-11 20:23:52 +00:00
|
|
|
|
2023-06-07 08:42:01 +00:00
|
|
|
ucmd.arg("-m").arg("u=rwx,g=rx,o=").arg(test_dir).succeeds();
|
|
|
|
let perms = at.metadata(test_dir).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() {
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir/test_dir_a/test_dir_b";
|
|
|
|
|
2022-03-06 20:36:08 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("-v")
|
2023-06-07 08:42:01 +00:00
|
|
|
.arg(test_dir)
|
2022-03-06 20:36:08 +00:00
|
|
|
.succeeds()
|
2023-06-07 08:42:01 +00:00
|
|
|
.stdout_contains("created directory 'test_dir'")
|
|
|
|
.stdout_contains("created directory 'test_dir/test_dir_a'")
|
|
|
|
.stdout_contains("created directory 'test_dir/test_dir_a/test_dir_b'");
|
|
|
|
new_ucmd!().arg("-v").arg(test_dir).fails().no_stdout();
|
|
|
|
|
|
|
|
let test_dir = "test_dir/../test_dir_a/../test_dir_b";
|
|
|
|
|
2022-03-06 20:36:08 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("-v")
|
2023-06-07 08:42:01 +00:00
|
|
|
.arg(test_dir)
|
2022-03-06 20:36:08 +00:00
|
|
|
.succeeds()
|
2023-06-07 08:42:01 +00:00
|
|
|
.stdout_contains("created directory 'test_dir'")
|
|
|
|
.stdout_contains("created directory 'test_dir/../test_dir_a'")
|
|
|
|
.stdout_contains("created directory 'test_dir/../test_dir_a/../test_dir_b'");
|
2022-03-06 20:36:08 +00:00
|
|
|
}
|
2022-03-15 11:03:47 +00:00
|
|
|
|
2022-03-22 22:08:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mkdir_trailing_dot() {
|
2023-06-07 08:42:01 +00:00
|
|
|
new_ucmd!().arg("-p").arg("-v").arg("test_dir").succeeds();
|
2022-03-22 22:08:47 +00:00
|
|
|
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("-v")
|
2023-06-07 08:42:01 +00:00
|
|
|
.arg("test_dir_a/.")
|
2022-03-22 22:08:47 +00:00
|
|
|
.succeeds()
|
2023-06-07 08:42:01 +00:00
|
|
|
.stdout_contains("created directory 'test_dir_a'");
|
2022-03-22 22:08:47 +00:00
|
|
|
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("-v")
|
2023-06-07 08:42:01 +00:00
|
|
|
.arg("test_dir_b/..")
|
2022-03-22 22:08:47 +00:00
|
|
|
.succeeds()
|
2023-06-07 08:42:01 +00:00
|
|
|
.stdout_contains("created directory 'test_dir_b'");
|
2023-06-07 08:44:11 +00:00
|
|
|
|
|
|
|
let scene = TestScenario::new("ls");
|
|
|
|
let result = scene.ucmd().arg("-al").run();
|
2022-03-22 22:08:47 +00:00
|
|
|
println!("ls dest {}", result.stdout_str());
|
|
|
|
}
|
2022-03-30 03:30:27 +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) {
|
2023-06-07 08:42:01 +00:00
|
|
|
let test_dir = "test_dir";
|
2022-02-19 05:37:56 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
2024-05-19 19:56:41 +00:00
|
|
|
ucmd.arg(test_dir).umask(umask_set).succeeds();
|
2023-06-07 08:42:01 +00:00
|
|
|
let perms = at.metadata(test_dir).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
|
2022-02-19 05:37:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 06:50:52 +00:00
|
|
|
for i in 0o0..0o027 {
|
2022-03-25 15:04:15 +00:00
|
|
|
// tests all permission combinations
|
2022-03-31 06:50:52 +00:00
|
|
|
test_single_case(i as mode_t);
|
2022-02-19 05:37:56 +00:00
|
|
|
}
|
2022-03-31 06:50:52 +00:00
|
|
|
}
|