2020-05-25 17:05:26 +00:00
|
|
|
use crate::common::util::*;
|
2021-08-16 19:56:09 +00:00
|
|
|
use std::fs::{metadata, set_permissions, OpenOptions, Permissions};
|
2016-05-22 07:46:54 +00:00
|
|
|
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
2016-12-19 10:02:58 +00:00
|
|
|
use std::sync::Mutex;
|
2016-01-04 15:58:49 +00:00
|
|
|
|
2016-06-19 14:52:56 +00:00
|
|
|
extern crate libc;
|
2021-03-28 18:56:37 +00:00
|
|
|
use self::chmod::strip_minus_from_mode;
|
|
|
|
extern crate chmod;
|
2016-06-19 14:52:56 +00:00
|
|
|
use self::libc::umask;
|
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
static TEST_FILE: &str = "file";
|
|
|
|
static REFERENCE_FILE: &str = "reference";
|
2016-05-22 07:46:54 +00:00
|
|
|
static REFERENCE_PERMS: u32 = 0o247;
|
2016-12-19 10:02:58 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref UMASK_MUTEX: Mutex<()> = Mutex::new(());
|
|
|
|
}
|
2016-01-04 15:58:49 +00:00
|
|
|
|
|
|
|
struct TestCase {
|
|
|
|
args: Vec<&'static str>,
|
2016-05-22 07:46:54 +00:00
|
|
|
before: u32,
|
2020-04-13 18:36:03 +00:00
|
|
|
after: u32,
|
2016-01-04 15:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
fn make_file(file: &str, mode: u32) {
|
2020-04-13 18:36:03 +00:00
|
|
|
OpenOptions::new()
|
|
|
|
.mode(mode)
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.open(file)
|
|
|
|
.unwrap();
|
2016-05-22 07:46:54 +00:00
|
|
|
let mut perms = metadata(file).unwrap().permissions();
|
2016-01-04 15:58:49 +00:00
|
|
|
perms.set_mode(mode);
|
2016-05-22 07:46:54 +00:00
|
|
|
set_permissions(file, perms).unwrap();
|
2016-01-04 15:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 18:35:03 +00:00
|
|
|
fn run_single_test(test: &TestCase, at: AtPath, mut ucmd: UCommand) {
|
2021-05-30 05:10:54 +00:00
|
|
|
make_file(&at.plus_as_string(TEST_FILE), test.before);
|
2020-04-13 18:36:03 +00:00
|
|
|
let perms = at.metadata(TEST_FILE).permissions().mode();
|
|
|
|
if perms != test.before {
|
2021-03-31 11:30:06 +00:00
|
|
|
panic!(
|
2020-04-13 18:36:03 +00:00
|
|
|
"{}: expected: {:o} got: {:o}",
|
|
|
|
"setting permissions on test files before actual test run failed", test.after, perms
|
2021-03-31 11:30:06 +00:00
|
|
|
);
|
2020-04-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for arg in &test.args {
|
|
|
|
ucmd.arg(arg);
|
|
|
|
}
|
|
|
|
let r = ucmd.run();
|
2021-04-22 20:37:44 +00:00
|
|
|
if !r.succeeded() {
|
2021-04-05 20:03:43 +00:00
|
|
|
println!("{}", r.stderr_str());
|
2021-03-31 11:30:06 +00:00
|
|
|
panic!("{:?}: failed", ucmd.raw);
|
2020-04-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let perms = at.metadata(TEST_FILE).permissions().mode();
|
|
|
|
if perms != test.after {
|
2021-03-31 11:30:06 +00:00
|
|
|
panic!(
|
2020-04-13 18:36:03 +00:00
|
|
|
"{:?}: expected: {:o} got: {:o}",
|
|
|
|
ucmd.raw, test.after, perms
|
2021-03-31 11:30:06 +00:00
|
|
|
);
|
2020-04-13 18:36:03 +00:00
|
|
|
}
|
2016-03-11 18:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run_tests(tests: Vec<TestCase>) {
|
|
|
|
for test in tests {
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, ucmd) = at_and_ucmd!();
|
2016-03-11 18:35:03 +00:00
|
|
|
run_single_test(&test, at, ucmd);
|
2016-01-04 15:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2016-01-04 15:58:49 +00:00
|
|
|
fn test_chmod_octal() {
|
2020-04-13 18:36:03 +00:00
|
|
|
let tests = vec![
|
|
|
|
TestCase {
|
|
|
|
args: vec!["0700", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100700,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["0070", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100070,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["0007", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100007,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-0700", TEST_FILE],
|
|
|
|
before: 0o100700,
|
|
|
|
after: 0o100000,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-0070", TEST_FILE],
|
|
|
|
before: 0o100060,
|
|
|
|
after: 0o100000,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-0007", TEST_FILE],
|
|
|
|
before: 0o100001,
|
|
|
|
after: 0o100000,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["+0100", TEST_FILE],
|
|
|
|
before: 0o100600,
|
|
|
|
after: 0o100700,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["+0020", TEST_FILE],
|
|
|
|
before: 0o100050,
|
|
|
|
after: 0o100070,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["+0004", TEST_FILE],
|
|
|
|
before: 0o100003,
|
|
|
|
after: 0o100007,
|
|
|
|
},
|
|
|
|
];
|
2016-01-04 15:58:49 +00:00
|
|
|
run_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable-next-line
|
2016-01-04 15:58:49 +00:00
|
|
|
fn test_chmod_ugoa() {
|
2016-12-19 10:02:58 +00:00
|
|
|
let _guard = UMASK_MUTEX.lock();
|
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
let last = unsafe { umask(0) };
|
|
|
|
let tests = vec![
|
|
|
|
TestCase {
|
|
|
|
args: vec!["u=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100700,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["g=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100070,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["o=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100007,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["a=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100777,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-r", TEST_FILE],
|
|
|
|
before: 0o100777,
|
|
|
|
after: 0o100333,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-w", TEST_FILE],
|
|
|
|
before: 0o100777,
|
|
|
|
after: 0o100555,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-x", TEST_FILE],
|
|
|
|
before: 0o100777,
|
|
|
|
after: 0o100666,
|
|
|
|
},
|
|
|
|
];
|
2016-01-04 15:58:49 +00:00
|
|
|
run_tests(tests);
|
2016-06-19 14:52:56 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
umask(0o022);
|
|
|
|
}
|
2020-04-13 18:36:03 +00:00
|
|
|
let tests = vec![
|
|
|
|
TestCase {
|
|
|
|
args: vec!["u=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100700,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["g=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100070,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["o=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100007,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["a=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100777,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["+rw", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100644,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["=rwx", TEST_FILE],
|
|
|
|
before: 0o100000,
|
|
|
|
after: 0o100755,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["-x", TEST_FILE],
|
|
|
|
before: 0o100777,
|
|
|
|
after: 0o100666,
|
|
|
|
},
|
|
|
|
];
|
2016-06-19 14:52:56 +00:00
|
|
|
run_tests(tests);
|
2021-08-16 22:20:44 +00:00
|
|
|
|
|
|
|
// check that we print an error if umask prevents us from removing a permission
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
at.touch("file");
|
|
|
|
set_permissions(at.plus("file"), Permissions::from_mode(0o777)).unwrap();
|
|
|
|
ucmd.args(&["-w", "file"])
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
// spell-checker:disable-next-line
|
|
|
|
.stderr_is("chmod: file: new permissions are r-xrwxrwx, not r-xr-xr-x");
|
|
|
|
assert_eq!(
|
|
|
|
metadata(at.plus("file")).unwrap().permissions().mode(),
|
|
|
|
0o100577
|
|
|
|
);
|
|
|
|
|
2016-06-19 14:52:56 +00:00
|
|
|
unsafe {
|
|
|
|
umask(last);
|
|
|
|
}
|
2016-01-04 15:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2016-01-04 15:58:49 +00:00
|
|
|
fn test_chmod_ugo_copy() {
|
2020-04-13 18:36:03 +00:00
|
|
|
let tests = vec![
|
|
|
|
TestCase {
|
|
|
|
args: vec!["u=g", TEST_FILE],
|
|
|
|
before: 0o100070,
|
|
|
|
after: 0o100770,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["g=o", TEST_FILE],
|
|
|
|
before: 0o100005,
|
|
|
|
after: 0o100055,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["o=u", TEST_FILE],
|
|
|
|
before: 0o100200,
|
|
|
|
after: 0o100202,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["u-g", TEST_FILE],
|
|
|
|
before: 0o100710,
|
|
|
|
after: 0o100610,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["u+g", TEST_FILE],
|
|
|
|
before: 0o100250,
|
|
|
|
after: 0o100750,
|
|
|
|
},
|
|
|
|
];
|
2016-01-04 15:58:49 +00:00
|
|
|
run_tests(tests);
|
|
|
|
}
|
2016-03-10 19:34:49 +00:00
|
|
|
|
2016-03-15 13:20:11 +00:00
|
|
|
#[test]
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2016-03-15 13:20:11 +00:00
|
|
|
fn test_chmod_many_options() {
|
2016-12-19 10:02:58 +00:00
|
|
|
let _guard = UMASK_MUTEX.lock();
|
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
let original_umask = unsafe { umask(0) };
|
|
|
|
let tests = vec![TestCase {
|
|
|
|
args: vec!["-r,a+w", TEST_FILE],
|
|
|
|
before: 0o100444,
|
|
|
|
after: 0o100222,
|
|
|
|
}];
|
2016-03-15 13:20:11 +00:00
|
|
|
run_tests(tests);
|
2016-12-18 12:35:30 +00:00
|
|
|
unsafe {
|
|
|
|
umask(original_umask);
|
|
|
|
}
|
2016-03-15 13:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 19:34:49 +00:00
|
|
|
#[test]
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2016-03-10 19:34:49 +00:00
|
|
|
fn test_chmod_reference_file() {
|
2020-04-13 18:36:03 +00:00
|
|
|
let tests = vec![
|
|
|
|
TestCase {
|
|
|
|
args: vec!["--reference", REFERENCE_FILE, TEST_FILE],
|
|
|
|
before: 0o100070,
|
|
|
|
after: 0o100247,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
args: vec!["a-w", "--reference", REFERENCE_FILE, TEST_FILE],
|
|
|
|
before: 0o100070,
|
|
|
|
after: 0o100247,
|
|
|
|
},
|
|
|
|
];
|
2016-08-23 11:52:43 +00:00
|
|
|
let (at, ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
make_file(&at.plus_as_string(REFERENCE_FILE), REFERENCE_PERMS);
|
2016-03-11 18:35:03 +00:00
|
|
|
run_single_test(&tests[0], at, ucmd);
|
2016-03-10 19:34:49 +00:00
|
|
|
}
|
2020-12-12 09:31:12 +00:00
|
|
|
|
2021-05-26 13:34:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_permission_denied() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
|
|
|
|
|
|
|
at.mkdir("d/");
|
|
|
|
at.mkdir("d/no-x");
|
|
|
|
at.mkdir("d/no-x/y");
|
|
|
|
|
|
|
|
scene.ucmd().arg("u=rw").arg("d/no-x").succeeds();
|
|
|
|
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-R")
|
|
|
|
.arg("o=r")
|
|
|
|
.arg("d")
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chmod: 'd/no-x/y': Permission denied");
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:12 +00:00
|
|
|
#[test]
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2020-12-12 09:31:12 +00:00
|
|
|
fn test_chmod_recursive() {
|
|
|
|
let _guard = UMASK_MUTEX.lock();
|
|
|
|
|
|
|
|
let original_umask = unsafe { umask(0) };
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
at.mkdir("a");
|
|
|
|
at.mkdir("a/b");
|
|
|
|
at.mkdir("a/b/c");
|
|
|
|
at.mkdir("z");
|
2021-05-30 05:10:54 +00:00
|
|
|
make_file(&at.plus_as_string("a/a"), 0o100444);
|
|
|
|
make_file(&at.plus_as_string("a/b/b"), 0o100444);
|
|
|
|
make_file(&at.plus_as_string("a/b/c/c"), 0o100444);
|
|
|
|
make_file(&at.plus_as_string("z/y"), 0o100444);
|
2020-12-12 09:31:12 +00:00
|
|
|
|
2021-04-05 20:03:43 +00:00
|
|
|
ucmd.arg("-R")
|
2020-12-12 09:31:12 +00:00
|
|
|
.arg("--verbose")
|
|
|
|
.arg("-r,a+w")
|
|
|
|
.arg("a")
|
|
|
|
.arg("z")
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
2021-08-15 22:18:45 +00:00
|
|
|
.stdout_contains(&"to 0333 (-wx-wx-wx)")
|
|
|
|
.stdout_contains(&"to 0222 (-w--w--w-)");
|
2020-12-12 09:31:12 +00:00
|
|
|
|
|
|
|
assert_eq!(at.metadata("z/y").permissions().mode(), 0o100222);
|
|
|
|
assert_eq!(at.metadata("a/a").permissions().mode(), 0o100222);
|
|
|
|
assert_eq!(at.metadata("a/b/b").permissions().mode(), 0o100222);
|
|
|
|
assert_eq!(at.metadata("a/b/c/c").permissions().mode(), 0o100222);
|
|
|
|
println!("mode {:o}", at.metadata("a").permissions().mode());
|
|
|
|
assert_eq!(at.metadata("a").permissions().mode(), 0o40333);
|
|
|
|
assert_eq!(at.metadata("z").permissions().mode(), 0o40333);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
umask(original_umask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chmod_non_existing_file() {
|
2021-04-05 20:03:43 +00:00
|
|
|
new_ucmd!()
|
2020-12-12 09:31:12 +00:00
|
|
|
.arg("-R")
|
|
|
|
.arg("-r,a+w")
|
2021-05-30 05:10:54 +00:00
|
|
|
.arg("does-not-exist")
|
2021-04-05 20:03:43 +00:00
|
|
|
.fails()
|
2021-05-30 05:10:54 +00:00
|
|
|
.stderr_contains(&"cannot access 'does-not-exist': No such file or directory");
|
2020-12-12 09:31:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 15:02:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_chmod_non_existing_file_silent() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-R")
|
|
|
|
.arg("--quiet")
|
|
|
|
.arg("-r,a+w")
|
|
|
|
.arg("does-not-exist")
|
|
|
|
.fails()
|
|
|
|
.no_stderr()
|
|
|
|
.code_is(1);
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_chmod_preserve_root() {
|
2021-04-05 20:03:43 +00:00
|
|
|
new_ucmd!()
|
2020-12-12 09:31:12 +00:00
|
|
|
.arg("-R")
|
|
|
|
.arg("--preserve-root")
|
|
|
|
.arg("755")
|
|
|
|
.arg("/")
|
2021-04-05 20:03:43 +00:00
|
|
|
.fails()
|
2021-05-25 23:45:53 +00:00
|
|
|
.stderr_contains(&"chmod: it is dangerous to operate recursively on '/'");
|
2020-12-12 09:31:12 +00:00
|
|
|
}
|
2021-01-18 22:09:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chmod_symlink_non_existing_file() {
|
2021-03-29 20:07:09 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
2021-01-18 22:09:00 +00:00
|
|
|
|
2021-03-29 20:07:09 +00:00
|
|
|
let non_existing = "test_chmod_symlink_non_existing_file";
|
|
|
|
let test_symlink = "test_chmod_symlink_non_existing_file_symlink";
|
|
|
|
let expected_stdout = &format!(
|
|
|
|
"failed to change mode of '{}' from 0000 (---------) to 0000 (---------)",
|
|
|
|
test_symlink
|
|
|
|
);
|
|
|
|
let expected_stderr = &format!("cannot operate on dangling symlink '{}'", test_symlink);
|
|
|
|
|
|
|
|
at.symlink_file(non_existing, test_symlink);
|
|
|
|
|
|
|
|
// this cannot succeed since the symbolic link dangles
|
2021-04-17 09:22:49 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2021-04-05 20:03:43 +00:00
|
|
|
.arg("755")
|
|
|
|
.arg("-v")
|
|
|
|
.arg(test_symlink)
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stdout_contains(expected_stdout)
|
|
|
|
.stderr_contains(expected_stderr);
|
2021-03-29 20:07:09 +00:00
|
|
|
|
|
|
|
// this should be the same than with just '-v' but without stderr
|
2021-04-17 09:22:49 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2021-01-18 22:09:00 +00:00
|
|
|
.arg("755")
|
|
|
|
.arg("-v")
|
2021-03-29 20:07:09 +00:00
|
|
|
.arg("-f")
|
|
|
|
.arg(test_symlink)
|
2021-04-05 20:03:43 +00:00
|
|
|
.run()
|
|
|
|
.code_is(1)
|
|
|
|
.no_stderr()
|
|
|
|
.stdout_contains(expected_stdout);
|
2021-01-18 22:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-29 20:07:09 +00:00
|
|
|
fn test_chmod_symlink_non_existing_file_recursive() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
2021-01-18 22:09:00 +00:00
|
|
|
|
2021-03-29 20:07:09 +00:00
|
|
|
let non_existing = "test_chmod_symlink_non_existing_file_recursive";
|
|
|
|
let test_symlink = "test_chmod_symlink_non_existing_file_recursive_symlink";
|
|
|
|
let test_directory = "test_chmod_symlink_non_existing_file_directory";
|
|
|
|
|
|
|
|
at.mkdir(test_directory);
|
|
|
|
at.symlink_file(
|
|
|
|
non_existing,
|
|
|
|
&format!("{}/{}", test_directory, test_symlink),
|
|
|
|
);
|
|
|
|
|
|
|
|
// this should succeed
|
2021-04-17 09:22:49 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2021-03-29 20:07:09 +00:00
|
|
|
.arg("-R")
|
|
|
|
.arg("755")
|
|
|
|
.arg(test_directory)
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
2021-03-29 20:07:09 +00:00
|
|
|
|
|
|
|
let expected_stdout = &format!(
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable-next-line
|
2021-03-29 20:07:09 +00:00
|
|
|
"mode of '{}' retained as 0755 (rwxr-xr-x)\nneither symbolic link '{}/{}' nor referent has been changed",
|
|
|
|
test_directory, test_directory, test_symlink
|
|
|
|
);
|
|
|
|
|
|
|
|
// '-v': this should succeed without stderr
|
2021-04-17 09:22:49 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2021-03-29 20:07:09 +00:00
|
|
|
.arg("-R")
|
|
|
|
.arg("-v")
|
|
|
|
.arg("755")
|
|
|
|
.arg(test_directory)
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_contains(expected_stdout)
|
|
|
|
.no_stderr();
|
2021-03-29 20:07:09 +00:00
|
|
|
|
|
|
|
// '-vf': this should be the same than with just '-v'
|
2021-04-17 09:22:49 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2021-03-29 20:07:09 +00:00
|
|
|
.arg("-R")
|
|
|
|
.arg("-v")
|
|
|
|
.arg("-f")
|
|
|
|
.arg("755")
|
|
|
|
.arg(test_directory)
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_contains(expected_stdout)
|
|
|
|
.no_stderr();
|
2021-01-18 22:09:00 +00:00
|
|
|
}
|
2021-03-28 18:56:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chmod_strip_minus_from_mode() {
|
|
|
|
let tests = vec![
|
|
|
|
// ( before, after )
|
|
|
|
("chmod -v -xw -R FILE", "chmod -v xw -R FILE"),
|
|
|
|
("chmod g=rwx FILE -c", "chmod g=rwx FILE -c"),
|
|
|
|
(
|
|
|
|
"chmod -c -R -w,o+w FILE --preserve-root",
|
|
|
|
"chmod -c -R w,o+w FILE --preserve-root",
|
|
|
|
),
|
|
|
|
("chmod -c -R +w FILE ", "chmod -c -R +w FILE "),
|
|
|
|
("chmod a=r,=xX FILE", "chmod a=r,=xX FILE"),
|
|
|
|
(
|
2021-05-30 05:10:54 +00:00
|
|
|
"chmod -v --reference REF_FILE -R FILE",
|
|
|
|
"chmod -v --reference REF_FILE -R FILE",
|
2021-03-28 18:56:37 +00:00
|
|
|
),
|
|
|
|
("chmod -Rvc -w-x FILE", "chmod -Rvc w-x FILE"),
|
|
|
|
("chmod 755 -v FILE", "chmod 755 -v FILE"),
|
|
|
|
("chmod -v +0004 FILE -R", "chmod -v +0004 FILE -R"),
|
|
|
|
("chmod -v -0007 FILE -R", "chmod -v 0007 FILE -R"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for test in tests {
|
2021-05-29 12:32:35 +00:00
|
|
|
let mut args: Vec<String> = test.0.split(' ').map(|v| v.to_string()).collect();
|
2021-03-28 18:56:37 +00:00
|
|
|
let _mode_had_minus_prefix = strip_minus_from_mode(&mut args);
|
|
|
|
assert_eq!(test.1, args.join(" "));
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 19:56:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chmod_keep_setgid() {
|
|
|
|
for &(from, arg, to) in &[
|
|
|
|
(0o7777, "777", 0o46777),
|
|
|
|
(0o7777, "=777", 0o40777),
|
|
|
|
(0o7777, "0777", 0o46777),
|
|
|
|
(0o7777, "=0777", 0o40777),
|
|
|
|
(0o7777, "00777", 0o40777),
|
|
|
|
(0o2444, "a+wx", 0o42777),
|
|
|
|
(0o2444, "a=wx", 0o42333),
|
|
|
|
(0o1444, "g+s", 0o43444),
|
|
|
|
(0o4444, "u-s", 0o40444),
|
|
|
|
(0o7444, "a-s", 0o41444),
|
|
|
|
] {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
at.mkdir("dir");
|
|
|
|
set_permissions(at.plus("dir"), Permissions::from_mode(from)).unwrap();
|
|
|
|
let r = ucmd.arg(arg).arg("dir").succeeds();
|
|
|
|
println!("{}", r.stderr_str());
|
|
|
|
assert_eq!(at.metadata("dir").permissions().mode(), to);
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 13:27:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_operands() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("777")
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_is("chmod: missing operand");
|
|
|
|
}
|
2021-08-17 13:35:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mode_after_dash_dash() {
|
|
|
|
let (at, ucmd) = at_and_ucmd!();
|
|
|
|
run_single_test(
|
|
|
|
&TestCase {
|
|
|
|
args: vec!["--", "-r", TEST_FILE],
|
|
|
|
before: 0o100777,
|
|
|
|
after: 0o100333,
|
|
|
|
},
|
|
|
|
at,
|
|
|
|
ucmd,
|
|
|
|
);
|
|
|
|
}
|