mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
id: add more tests to be consistent with GNU testsuite tests (tests/id/zero.sh)
This commit is contained in:
parent
9af9343745
commit
17c6f4c13a
1 changed files with 207 additions and 32 deletions
|
@ -13,6 +13,8 @@ use crate::common::util::*;
|
||||||
//
|
//
|
||||||
|
|
||||||
fn whoami() -> String {
|
fn whoami() -> String {
|
||||||
|
// Use environment variable to get current user instead of invoking `whoami`
|
||||||
|
// and fall back to user "nobody" on error.
|
||||||
std::env::var("USER").unwrap_or_else(|e| {
|
std::env::var("USER").unwrap_or_else(|e| {
|
||||||
println!("warning: {}, using \"nobody\" instead", e);
|
println!("warning: {}, using \"nobody\" instead", e);
|
||||||
"nobody".to_string()
|
"nobody".to_string()
|
||||||
|
@ -21,12 +23,12 @@ fn whoami() -> String {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_no_argument() {
|
fn test_id_no_specified_user() {
|
||||||
let result = new_ucmd!().run();
|
let result = new_ucmd!().run();
|
||||||
let expected_result = expected_result(&[]);
|
let expected_result = expected_result(&[]);
|
||||||
let mut exp_stdout = expected_result.stdout_str().to_string();
|
let mut exp_stdout = expected_result.stdout_str().to_string();
|
||||||
|
|
||||||
// uu_stid does not support selinux context. Remove 'context' part from exp_stdout:
|
// uu_id does not support selinux context. Remove 'context' part from exp_stdout:
|
||||||
let context_offset = expected_result
|
let context_offset = expected_result
|
||||||
.stdout_str()
|
.stdout_str()
|
||||||
.find(" context")
|
.find(" context")
|
||||||
|
@ -42,18 +44,63 @@ fn test_id_no_argument() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_single_user() {
|
fn test_id_single_user() {
|
||||||
let args = &[&whoami()[..]];
|
let test_users = [&whoami()[..]];
|
||||||
let result = new_ucmd!().args(args).run();
|
|
||||||
let expected_result = expected_result(args);
|
let scene = TestScenario::new(util_name!());
|
||||||
result
|
let mut exp_result = expected_result(&test_users);
|
||||||
.stdout_is(expected_result.stdout_str())
|
scene
|
||||||
.stderr_is(expected_result.stderr_str())
|
.ucmd()
|
||||||
.code_is(expected_result.code());
|
.args(&test_users)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
|
||||||
|
// u/g/G z/n
|
||||||
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
|
let mut args = vec![opt];
|
||||||
|
args.extend_from_slice(&test_users);
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.push("--zero");
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.push("--name");
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.pop();
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_single_invalid_user() {
|
fn test_id_single_user_non_existing() {
|
||||||
let args = &["hopefully_non_existing_username"];
|
let args = &["hopefully_non_existing_username"];
|
||||||
let result = new_ucmd!().args(args).run();
|
let result = new_ucmd!().args(args).run();
|
||||||
let expected_result = expected_result(args);
|
let expected_result = expected_result(args);
|
||||||
|
@ -127,43 +174,164 @@ fn test_id_password_style() {
|
||||||
assert!(result.stdout_str().starts_with(&username));
|
assert!(result.stdout_str().starts_with(&username));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg(unix)]
|
|
||||||
fn test_id_default_format() {
|
|
||||||
// TODO: These are the same tests like in test_id_zero but without --zero flag.
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_multiple_users() {
|
fn test_id_multiple_users() {
|
||||||
// Same typical users that GNU testsuite is using.
|
// Same typical users that GNU testsuite is using.
|
||||||
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||||
|
|
||||||
let result = new_ucmd!().args(&test_users).run();
|
let scene = TestScenario::new(util_name!());
|
||||||
let expected_result = expected_result(&test_users);
|
let mut exp_result = expected_result(&test_users);
|
||||||
result
|
scene
|
||||||
.stdout_is(expected_result.stdout_str())
|
.ucmd()
|
||||||
.stderr_is(expected_result.stderr_str());
|
.args(&test_users)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
|
||||||
|
// u/g/G z/n
|
||||||
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
|
let mut args = vec![opt];
|
||||||
|
args.extend_from_slice(&test_users);
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.push("--zero");
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.push("--name");
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.pop();
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_multiple_invalid_users() {
|
fn test_id_multiple_users_non_existing() {
|
||||||
let test_users = [
|
let test_users = [
|
||||||
"root",
|
"root",
|
||||||
"hopefully_non_existing_username1",
|
"hopefully_non_existing_username1",
|
||||||
|
&whoami(),
|
||||||
"man",
|
"man",
|
||||||
|
"hopefully_non_existing_username2",
|
||||||
|
"hopefully_non_existing_username3",
|
||||||
"postfix",
|
"postfix",
|
||||||
"sshd",
|
"sshd",
|
||||||
"hopefully_non_existing_username2",
|
"hopefully_non_existing_username4",
|
||||||
&whoami(),
|
&whoami(),
|
||||||
];
|
];
|
||||||
|
|
||||||
let result = new_ucmd!().args(&test_users).run();
|
let scene = TestScenario::new(util_name!());
|
||||||
let expected_result = expected_result(&test_users);
|
let mut exp_result = expected_result(&test_users);
|
||||||
result
|
scene
|
||||||
.stdout_is(expected_result.stdout_str())
|
.ucmd()
|
||||||
.stderr_is(expected_result.stderr_str());
|
.args(&test_users)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
|
||||||
|
// u/g/G z/n
|
||||||
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
|
let mut args = vec![opt];
|
||||||
|
args.extend_from_slice(&test_users);
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.push("--zero");
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.push("--name");
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
args.pop();
|
||||||
|
exp_result = expected_result(&args);
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.run()
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn test_id_default_format() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
for &opt1 in &["--name", "--real"] {
|
||||||
|
// id: cannot print only names or real IDs in default format
|
||||||
|
let args = [opt1];
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.fails()
|
||||||
|
.stderr_only(expected_result(&args).stderr_str());
|
||||||
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
|
// u/g/G n/r
|
||||||
|
let args = [opt2, opt1];
|
||||||
|
let result = scene.ucmd().args(&args).run();
|
||||||
|
let exp_result = expected_result(&args);
|
||||||
|
result
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
|
// u/g/G
|
||||||
|
let args = [opt2];
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&args)
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only(expected_result(&args).stdout_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -171,6 +339,12 @@ fn test_id_multiple_invalid_users() {
|
||||||
fn test_id_zero() {
|
fn test_id_zero() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let scene = TestScenario::new(util_name!());
|
||||||
for z_flag in &["-z", "--zero"] {
|
for z_flag in &["-z", "--zero"] {
|
||||||
|
// id: option --zero not permitted in default format
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&[z_flag])
|
||||||
|
.fails()
|
||||||
|
.stderr_only(expected_result(&[z_flag]).stderr_str());
|
||||||
for &opt1 in &["--name", "--real"] {
|
for &opt1 in &["--name", "--real"] {
|
||||||
// id: cannot print only names or real IDs in default format
|
// id: cannot print only names or real IDs in default format
|
||||||
let args = [opt1, z_flag];
|
let args = [opt1, z_flag];
|
||||||
|
@ -183,14 +357,15 @@ fn test_id_zero() {
|
||||||
// u/g/G n/r z
|
// u/g/G n/r z
|
||||||
let args = [opt2, z_flag, opt1];
|
let args = [opt2, z_flag, opt1];
|
||||||
let result = scene.ucmd().args(&args).run();
|
let result = scene.ucmd().args(&args).run();
|
||||||
let expected_result = expected_result(&args);
|
let exp_result = expected_result(&args);
|
||||||
result
|
result
|
||||||
.stdout_is(expected_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(expected_result.stderr_str());
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// u/g/G z
|
|
||||||
for &opt2 in &["--user", "--group", "--groups"] {
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
|
// u/g/G z
|
||||||
let args = [opt2, z_flag];
|
let args = [opt2, z_flag];
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue