2016-08-21 09:05:05 +00:00
|
|
|
use common::util::*;
|
2018-03-02 07:08:09 +00:00
|
|
|
use rust_users::*;
|
2016-08-21 09:05:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_option() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-08-21 09:05:05 +00:00
|
|
|
.arg("-w")
|
|
|
|
.arg("/")
|
|
|
|
.fails();
|
|
|
|
}
|
|
|
|
|
|
|
|
static DIR: &'static str = "/tmp";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_group() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2018-03-02 07:08:09 +00:00
|
|
|
.arg("__nosuchgroup__")
|
2016-08-21 09:05:05 +00:00
|
|
|
.arg("/")
|
|
|
|
.fails()
|
2018-03-02 07:08:09 +00:00
|
|
|
.stderr_is("chgrp: invalid group: __nosuchgroup__");
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1() {
|
2018-03-02 07:08:09 +00:00
|
|
|
if get_effective_gid() != 0 {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("bin")
|
|
|
|
.arg(DIR)
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: changing group of '/tmp': Operation not permitted (os error 1)");
|
|
|
|
}
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fail_silently() {
|
2018-03-02 07:08:09 +00:00
|
|
|
if get_effective_gid() != 0 {
|
|
|
|
for opt in &["-f", "--silent", "--quiet"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(opt)
|
|
|
|
.arg("bin")
|
|
|
|
.arg(DIR)
|
|
|
|
.run()
|
|
|
|
.fails_silently();
|
|
|
|
}
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_preserve_root() {
|
2016-08-28 11:55:41 +00:00
|
|
|
// It's weird that on OS X, `realpath /etc/..` returns '/private'
|
|
|
|
for d in &["/", "/////tmp///../../../../",
|
|
|
|
"../../../../../../../../../../../../../../",
|
|
|
|
"./../../../../../../../../../../../../../../"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("--preserve-root")
|
|
|
|
.arg("-R")
|
|
|
|
.arg("bin").arg(d)
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_preserve_root_symlink() {
|
|
|
|
let file = "test_chgrp_symlink2root";
|
|
|
|
for d in &["/", "////tmp//../../../../",
|
|
|
|
"..//../../..//../..//../../../../../../../../",
|
|
|
|
".//../../../../../../..//../../../../../../../"] {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2018-11-13 04:55:25 +00:00
|
|
|
at.symlink_file(d, file);
|
2016-08-28 11:55:41 +00:00
|
|
|
ucmd.arg("--preserve-root")
|
|
|
|
.arg("-HR")
|
|
|
|
.arg("bin").arg(file)
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
|
|
|
|
}
|
|
|
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2018-11-13 04:55:25 +00:00
|
|
|
at.symlink_file("///usr", file);
|
2016-08-28 11:55:41 +00:00
|
|
|
ucmd.arg("--preserve-root")
|
|
|
|
.arg("-HR")
|
|
|
|
.arg("bin").arg(format!(".//{}/..//..//../../", file))
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
|
|
|
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2018-11-13 04:55:25 +00:00
|
|
|
at.symlink_file("/", "/tmp/__root__");
|
2016-08-28 11:55:41 +00:00
|
|
|
ucmd.arg("--preserve-root")
|
2016-08-21 09:05:05 +00:00
|
|
|
.arg("-R")
|
2016-08-28 11:55:41 +00:00
|
|
|
.arg("bin").arg("/tmp/__root__/.")
|
2016-08-21 09:05:05 +00:00
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
|
2016-08-28 11:55:41 +00:00
|
|
|
|
|
|
|
use ::std::fs;
|
|
|
|
fs::remove_file("/tmp/__root__").unwrap();
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn test_reference() {
|
2019-12-25 06:42:11 +00:00
|
|
|
// skip for root or MS-WSL
|
|
|
|
// * MS-WSL is bugged (as of 2019-12-25), allowing non-root accounts su-level privileges for `chgrp`
|
|
|
|
// * for MS-WSL, succeeds and stdout == 'group of /etc retained as root'
|
|
|
|
if !(get_effective_gid() == 0 || is_wsl()) {
|
2018-03-02 07:08:09 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg("-v")
|
|
|
|
.arg("--reference=/etc/passwd")
|
|
|
|
.arg("/etc")
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: changing group of '/etc': Operation not permitted (os error 1)\n")
|
|
|
|
.stdout_is("failed to change group of /etc from root to root\n");
|
|
|
|
}
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn test_reference() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-08-21 09:05:05 +00:00
|
|
|
.arg("-v")
|
|
|
|
.arg("--reference=/etc/passwd")
|
|
|
|
.arg("/etc")
|
|
|
|
.succeeds();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn test_big_p() {
|
2018-03-02 07:08:09 +00:00
|
|
|
if get_effective_gid() != 0 {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-RP")
|
|
|
|
.arg("bin")
|
|
|
|
.arg("/proc/self/cwd")
|
|
|
|
.fails()
|
|
|
|
.stderr_is("chgrp: changing group of '/proc/self/cwd': Operation not permitted (os error 1)\n");
|
|
|
|
}
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn test_big_h() {
|
2018-03-02 07:08:09 +00:00
|
|
|
if get_effective_gid() != 0 {
|
|
|
|
assert!(new_ucmd!()
|
|
|
|
.arg("-RH")
|
|
|
|
.arg("bin")
|
|
|
|
.arg("/proc/self/fd")
|
|
|
|
.fails()
|
|
|
|
.stderr
|
|
|
|
.lines()
|
|
|
|
.fold(0, |acc, _| acc + 1) > 1);
|
|
|
|
}
|
2016-08-21 09:05:05 +00:00
|
|
|
}
|