diff --git a/src/uu/id/src/id.rs b/src/uu/id/src/id.rs index a32d955a3..d0d6a64f7 100644 --- a/src/uu/id/src/id.rs +++ b/src/uu/id/src/id.rs @@ -77,8 +77,8 @@ pub fn uumain(args: Vec) -> i32 { "", "Display the process audit (not available on Linux)", ); - opts.optflag("G", "", "Display the different group IDs"); - opts.optflag("g", "", "Display the effective group ID as a number"); + opts.optflag("G", "groups", "Display the different group IDs"); + opts.optflag("g", "group", "Display the effective group ID as a number"); opts.optflag( "n", "", @@ -87,7 +87,7 @@ pub fn uumain(args: Vec) -> i32 { opts.optflag("P", "", "Display the id as a password file entry"); opts.optflag("p", "", "Make the output human-readable"); opts.optflag("r", "", "Display the real ID for the -g and -u options"); - opts.optflag("u", "", "Display the effective user ID as a number"); + opts.optflag("u", "user", "Display the effective user ID as a number"); let matches = opts.parse(args); @@ -298,7 +298,10 @@ fn id_print(possible_pw: Option, p_euid: bool, p_egid: bool) { .map(|p| (p.uid(), p.gid())) .unwrap_or((getuid(), getgid())); - let groups = Passwd::locate(uid).unwrap().belongs_to(); + let groups = match Passwd::locate(uid) { + Ok(p) => p.belongs_to(), + Err(e) => crash!(1, "Could not find uid {}: {}", uid, e), + }; print!("uid={}({})", uid, entries::uid2usr(uid).unwrap()); print!(" gid={}({})", gid, entries::gid2grp(gid).unwrap()); diff --git a/tests/test_id.rs b/tests/test_id.rs new file mode 100644 index 000000000..fe3706d30 --- /dev/null +++ b/tests/test_id.rs @@ -0,0 +1,141 @@ +use common::util::*; + +#[test] +fn test_id() { + let scene = TestScenario::new(util_name!()); + + let mut result = scene.ucmd().arg("-u").run(); + if result.stderr.contains("cannot find name for user ID") { + // In the CI, some server are failing to return whoami. + // As seems to be a configuration issue, ignoring it + return; + } + assert!(result.success); + + let uid = String::from(result.stdout.trim()); + result = scene.ucmd().run(); + if result.stderr.contains("cannot find name for user ID") { + // In the CI, some server are failing to return whoami. + // As seems to be a configuration issue, ignoring it + return; + } + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + if !result.stderr.contains("Could not find uid") { + // Verify that the id found by --user/-u exists in the list + assert!(result.stdout.contains(&uid)); + } +} + +#[test] +fn test_id_from_name() { + let mut scene = TestScenario::new("whoami"); + let result = scene.cmd("whoami").run(); + if result.stderr.contains("cannot find name for user ID") { + // In the CI, some server are failing to return whoami. + // As seems to be a configuration issue, ignoring it + return; + } + + let username = result.stdout.trim(); + + scene = TestScenario::new(util_name!()); + let result = scene.ucmd().arg(username).succeeds(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + let uid = String::from(result.stdout.trim()); + let result = scene.ucmd().succeeds(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + // Verify that the id found by --user/-u exists in the list + assert!(result.stdout.contains(&uid)); + // Verify that the username found by whoami exists in the list + assert!(result.stdout.contains(&username)); +} + +#[test] +fn test_id_name_from_id() { + let mut scene = TestScenario::new(util_name!()); + let result = scene.ucmd().arg("-u").run(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + let uid = String::from(result.stdout.trim()); + + scene = TestScenario::new(util_name!()); + let result = scene.ucmd().arg("-nu").arg(uid).run(); + if result.stderr.contains("No such user/group") { + // In the CI, some server are failing to return whoami. + // As seems to be a configuration issue, ignoring it + return; + } + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + + let username_id = String::from(result.stdout.trim()); + + scene = TestScenario::new("whoami"); + let result = scene.cmd("whoami").run(); + + let username_whoami = result.stdout.trim(); + + assert_eq!(username_id, username_whoami); +} + +#[test] +fn test_id_group() { + let scene = TestScenario::new(util_name!()); + + let mut result = scene.ucmd().arg("-g").succeeds(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + let s1 = String::from(result.stdout.trim()); + assert!(s1.parse::().is_ok()); + + result = scene.ucmd().arg("--group").succeeds(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + let s1 = String::from(result.stdout.trim()); + assert!(s1.parse::().is_ok()); +} + +#[test] +fn test_id_groups() { + let scene = TestScenario::new(util_name!()); + + let result = scene.ucmd().arg("-G").succeeds(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + let groups = result.stdout.trim().split_whitespace(); + for s in groups { + assert!(s.parse::().is_ok()); + } + + let result = scene.ucmd().arg("--groups").succeeds(); + println!("result.stdout = {}", result.stdout); + println!("result.stderr = {}", result.stderr); + assert!(result.success); + let groups = result.stdout.trim().split_whitespace(); + for s in groups { + assert!(s.parse::().is_ok()); + } +} + +#[test] +fn test_id_user() { + let scene = TestScenario::new(util_name!()); + + let mut result = scene.ucmd().arg("-u").succeeds(); + assert!(result.success); + let s1 = String::from(result.stdout.trim()); + assert!(s1.parse::().is_ok()); + result = scene.ucmd().arg("--user").succeeds(); + assert!(result.success); + let s1 = String::from(result.stdout.trim()); + assert!(s1.parse::().is_ok()); +} diff --git a/tests/tests.rs b/tests/tests.rs index be8ae7dd2..4db11f523 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -67,6 +67,7 @@ generic! { "fold", test_fold; "hashsum", test_hashsum; "head", test_head; + "id", test_id; "join", test_join; "link", test_link; "ln", test_ln;