coreutils/tests/by-util/test_chgrp.rs

254 lines
6.8 KiB
Rust
Raw Normal View History

// spell-checker:ignore (words) nosuchgroup groupname
use crate::common::util::*;
use rust_users::*;
2016-08-21 09:05:05 +00:00
#[test]
fn test_invalid_option() {
2020-04-13 18:36:03 +00:00
new_ucmd!().arg("-w").arg("/").fails();
2016-08-21 09:05:05 +00:00
}
2021-05-29 12:32:35 +00:00
static DIR: &str = "/tmp";
2016-08-21 09:05:05 +00:00
// we should always get both arguments, regardless of whether --reference was used
#[test]
fn test_help() {
new_ucmd!()
.arg("--help")
.succeeds()
.stdout_contains("ARGS:\n <GROUP> \n <FILE>... ");
}
#[test]
fn test_help_ref() {
new_ucmd!()
.arg("--help")
.arg("--reference=ref_file")
.succeeds()
.stdout_contains("ARGS:\n <GROUP> \n <FILE>... ");
}
#[test]
fn test_ref_help() {
new_ucmd!()
.arg("--reference=ref_file")
.arg("--help")
.succeeds()
.stdout_contains("ARGS:\n <GROUP> \n <FILE>... ");
}
2016-08-21 09:05:05 +00:00
#[test]
fn test_invalid_group() {
new_ucmd!()
.arg("__nosuchgroup__")
2016-08-21 09:05:05 +00:00
.arg("/")
.fails()
.stderr_is("chgrp: invalid group: __nosuchgroup__");
2016-08-21 09:05:05 +00:00
}
#[test]
fn test_1() {
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() {
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() {
// It's weird that on OS X, `realpath /etc/..` returns '/private'
2020-04-13 18:36:03 +00:00
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";
2020-04-13 18:36:03 +00:00
for d in &[
"/",
"////tmp//../../../../",
"..//../../..//../..//../../../../../../../../",
".//../../../../../../..//../../../../../../../",
] {
let (at, mut ucmd) = at_and_ucmd!();
at.symlink_file(d, file);
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!();
at.symlink_file("///usr", file);
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!();
at.symlink_file("/", "/tmp/__root__");
ucmd.arg("--preserve-root")
2016-08-21 09:05:05 +00:00
.arg("-R")
.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");
2020-04-13 18:36:03 +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() {
// 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 || uucore::os::is_wsl_1()) {
new_ucmd!()
.arg("-v")
.arg("--reference=/etc/passwd")
.arg("/etc")
.fails()
.stderr_is("chgrp: changing group of '/etc': Operation not permitted (os error 1)\nfailed to change group of '/etc' from root to root");
}
2016-08-21 09:05:05 +00:00
}
#[test]
#[cfg(target_vendor = "apple")]
2016-08-21 09:05:05 +00:00
fn test_reference() {
new_ucmd!()
2016-08-21 09:05:05 +00:00
.arg("-v")
.arg("--reference=ref_file")
2016-08-21 09:05:05 +00:00
.arg("/etc")
.fails()
// group name can differ, so just check the first part of the message
.stderr_contains("chgrp: changing group of '/etc': Operation not permitted (os error 1)\nfailed to change group of '/etc' from ");
}
#[test]
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
fn test_reference_multi_no_equal() {
new_ucmd!()
.arg("-v")
.arg("--reference")
.arg("ref_file")
.arg("file1")
.arg("file2")
.succeeds()
.stderr_contains("chgrp: group of 'file1' retained as ")
.stderr_contains("\nchgrp: group of 'file2' retained as ");
}
#[test]
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
fn test_reference_last() {
new_ucmd!()
.arg("-v")
.arg("file1")
.arg("file2")
.arg("file3")
.arg("--reference")
.arg("ref_file")
.succeeds()
.stderr_contains("chgrp: group of 'file1' retained as ")
.stderr_contains("\nchgrp: group of 'file2' retained as ")
.stderr_contains("\nchgrp: group of 'file3' retained as ");
}
#[test]
fn test_missing_files() {
new_ucmd!()
.arg("-v")
.arg("groupname")
.fails()
.stderr_contains(
"error: The following required arguments were not provided:\n <FILE>...\n",
);
2016-08-21 09:05:05 +00:00
}
#[test]
#[cfg(target_os = "linux")]
fn test_big_p() {
if get_effective_gid() != 0 {
new_ucmd!()
.arg("-RP")
.arg("bin")
.arg("/proc/self/cwd")
.fails()
.stderr_contains(
2020-04-13 18:36:03 +00:00
"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() {
if get_effective_gid() != 0 {
2020-04-13 18:36:03 +00:00
assert!(
new_ucmd!()
.arg("-RH")
.arg("bin")
.arg("/proc/self/fd")
.fails()
.stderr_str()
2020-04-13 18:36:03 +00:00
.lines()
.fold(0, |acc, _| acc + 1)
> 1
);
}
2016-08-21 09:05:05 +00:00
}
2021-08-08 23:33:02 +00:00
#[test]
#[cfg(target_os = "linux")]
fn basic_succeeds() {
let (at, mut ucmd) = at_and_ucmd!();
let one_group = nix::unistd::getgroups().unwrap();
// if there are no groups we can't run this test.
if let Some(group) = one_group.first() {
at.touch("f1");
ucmd.arg(group.as_raw().to_string())
.arg("f1")
.succeeds()
.no_stdout()
.no_stderr();
}
}
2021-08-10 21:31:42 +00:00
#[test]
fn test_no_change() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("file");
ucmd.arg("").arg(at.plus("file")).succeeds();
}