Debian Codegraph @

tests/by-util/test_id.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore (ToDO) coreutil euid rgid
7
8 use std::process::{Command, Stdio};
9 use uutests::new_ucmd;
10 use uutests::unwrap_or_return;
11 use uutests::util::{TestScenario, check_coreutil_version, expected_result, is_ci, whoami};
12 use uutests::util_name;
13
14 #[cfg(all(feature = "chmod", feature = "chown"))]
15 use tempfile::TempPath;
16
17 const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 8.31
18
19 #[test]
20 fn test_invalid_arg() {
21 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
22 }
23
24 #[test]
25 fn test_id_ignore() {
26 new_ucmd!().arg("-a").succeeds();
27 }
28
29 #[test]
30 #[allow(unused_mut)]
31 fn test_id_no_specified_user() {
32 let ts = TestScenario::new(util_name!());
33 let result = ts.ucmd().run();
34 let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
35 let mut exp_stdout = exp_result.stdout_str().to_string();
36
37 #[cfg(not(feature = "selinux"))]
38 {
39 // NOTE: strip 'context' part from exp_stdout if selinux not enabled:
40 // example:
41 // uid=1001(runner) gid=121(docker) groups=121(docker),4(adm),101(systemd-journal) \
42 // context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
43 if let Some(context_offset) = exp_result.stdout_str().find(" context=") {
44 exp_stdout.replace_range(context_offset..exp_stdout.len() - 1, "");
45 }
46 }
47
48 result
49 .stdout_is(exp_stdout)
50 .stderr_is(exp_result.stderr_str())
51 .code_is(exp_result.code());
52 }
53
54 #[test]
55 fn test_id_single_user() {
56 let test_users = [&whoami()[..]];
57
58 let ts = TestScenario::new(util_name!());
59 let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
60 ts.ucmd()
61 .args(&test_users)
62 .run()
63 .stdout_is(exp_result.stdout_str())
64 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
65 .code_is(exp_result.code());
66
67 // u/g/G z/n
68 for opt in ["--user", "--group", "--groups"] {
69 let mut args = vec![opt];
70 args.extend_from_slice(&test_users);
71 exp_result = unwrap_or_return!(expected_result(&ts, &args));
72 ts.ucmd()
73 .args(&args)
74 .run()
75 .stdout_is(exp_result.stdout_str())
76 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
77 .code_is(exp_result.code());
78 args.push("--zero");
79 exp_result = unwrap_or_return!(expected_result(&ts, &args));
80 ts.ucmd()
81 .args(&args)
82 .run()
83 .stdout_is(exp_result.stdout_str())
84 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
85 .code_is(exp_result.code());
86 args.push("--name");
87 exp_result = unwrap_or_return!(expected_result(&ts, &args));
88 ts.ucmd()
89 .args(&args)
90 .run()
91 .stdout_is(exp_result.stdout_str())
92 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
93 .code_is(exp_result.code());
94 args.pop();
95 exp_result = unwrap_or_return!(expected_result(&ts, &args));
96 ts.ucmd()
97 .args(&args)
98 .run()
99 .stdout_is(exp_result.stdout_str())
100 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
101 .code_is(exp_result.code());
102 }
103 }
104
105 #[test]
106 fn test_id_single_user_non_existing() {
107 let args = &["hopefully_non_existing_username"];
108 let ts = TestScenario::new(util_name!());
109 let result = ts.ucmd().args(args).run();
110 let exp_result = unwrap_or_return!(expected_result(&ts, args));
111
112 // It is unknown why on macOS (and possibly others?) `id` adds "Invalid argument".
113 // coreutils 8.32: $ LC_ALL=C id foobar
114 // macOS: stderr: "id: 'foobar': no such user: Invalid argument"
115 // linux: stderr: "id: 'foobar': no such user"
116 result
117 .stdout_is(exp_result.stdout_str())
118 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
119 .code_is(exp_result.code());
120 }
121
122 #[test]
123 fn test_id_name() {
124 let ts = TestScenario::new(util_name!());
125 for opt in ["--user", "--group", "--groups"] {
126 let args = [opt, "--name"];
127 let result = ts.ucmd().args(&args).run();
128 let exp_result = unwrap_or_return!(expected_result(&ts, &args));
129 result
130 .stdout_is(exp_result.stdout_str())
131 .stderr_is(exp_result.stderr_str())
132 .code_is(exp_result.code());
133
134 if opt == "--user" {
135 assert_eq!(result.stdout_str().trim_end(), whoami());
136 }
137 }
138 }
139
140 #[test]
141 fn test_id_real() {
142 let ts = TestScenario::new(util_name!());
143 for opt in ["--user", "--group", "--groups"] {
144 let args = [opt, "--real"];
145 let result = ts.ucmd().args(&args).run();
146 let exp_result = unwrap_or_return!(expected_result(&ts, &args));
147 result
148 .stdout_is(exp_result.stdout_str())
149 .stderr_is(exp_result.stderr_str())
150 .code_is(exp_result.code());
151 }
152 }
153
154 #[test]
155 fn test_id_groups_ordering() {
156 // `groups=` is headed by the effective gid and `-G` by the real gid, so the
157 // two forms list the same set in a different order. Only observable when the
158 // real and effective gids differ, which needs a setgid wrapper, but the set
159 // and the `-r`-independence of `-G` hold either way.
160 let ts = TestScenario::new(util_name!());
161
162 let groups = ts.ucmd().arg("-G").succeeds().stdout_move_str();
163 let mut from_flag: Vec<&str> = groups.split_whitespace().collect();
164 assert!(!from_flag.is_empty());
165
166 // `-G` heads with the real gid
167 let rgid = ts.ucmd().args(&["-g", "-r"]).succeeds().stdout_move_str();
168 assert_eq!(from_flag[0], rgid.trim_end());
169
170 let default = ts.ucmd().succeeds().stdout_move_str();
171 let field = default.split(" groups=").nth(1).unwrap();
172 let mut from_default: Vec<&str> = field
173 .split(&[' ', '\n'][..])
174 .next()
175 .unwrap()
176 .split(',')
177 .map(|g| g.split('(').next().unwrap())
178 .collect();
179
180 from_flag.sort_unstable();
181 from_default.sort_unstable();
182 assert_eq!(from_flag, from_default);
183
184 // `-r` does not affect `-G`, matching GNU
185 ts.ucmd().args(&["-G", "-r"]).succeeds().stdout_is(&groups);
186 }
187
188 #[test]
189 #[cfg(not(any(target_os = "linux", target_os = "android")))]
190 fn test_id_pretty_print() {
191 // `-p` is BSD only and not supported on GNU's `id`
192 let username = whoami();
193 let result = new_ucmd!().arg("-p").run();
194 result.success().stdout_contains(username);
195 }
196
197 #[test]
198 #[cfg(not(any(target_os = "linux", target_os = "android")))]
199 fn test_id_password_style() {
200 // `-P` is BSD only and not supported on GNU's `id`
201 let username = whoami();
202 let result = new_ucmd!().arg("-P").arg(&username).succeeds();
203 assert!(result.stdout_str().starts_with(&username));
204 }
205
206 #[test]
207 fn test_id_multiple_users() {
208 unwrap_or_return!(check_coreutil_version(
209 util_name!(),
210 VERSION_MIN_MULTIPLE_USERS
211 ));
212
213 // Typical users commonly found on Unix systems.
214 let test_users = ["root", "man", "postfix", "sshd", &whoami()];
215
216 let ts = TestScenario::new(util_name!());
217 let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
218 ts.ucmd()
219 .args(&test_users)
220 .run()
221 .stdout_is(exp_result.stdout_str())
222 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
223 .code_is(exp_result.code());
224
225 // u/g/G z/n
226 for opt in ["--user", "--group", "--groups"] {
227 let mut args = vec![opt];
228 args.extend_from_slice(&test_users);
229 exp_result = unwrap_or_return!(expected_result(&ts, &args));
230 ts.ucmd()
231 .args(&args)
232 .run()
233 .stdout_is(exp_result.stdout_str())
234 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
235 .code_is(exp_result.code());
236 args.push("--zero");
237 exp_result = unwrap_or_return!(expected_result(&ts, &args));
238 ts.ucmd()
239 .args(&args)
240 .run()
241 .stdout_is(exp_result.stdout_str())
242 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
243 .code_is(exp_result.code());
244 args.push("--name");
245 exp_result = unwrap_or_return!(expected_result(&ts, &args));
246 ts.ucmd()
247 .args(&args)
248 .run()
249 .stdout_is(exp_result.stdout_str())
250 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
251 .code_is(exp_result.code());
252 args.pop();
253 exp_result = unwrap_or_return!(expected_result(&ts, &args));
254 ts.ucmd()
255 .args(&args)
256 .run()
257 .stdout_is(exp_result.stdout_str())
258 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
259 .code_is(exp_result.code());
260 }
261 }
262
263 #[test]
264 fn test_id_multiple_users_non_existing() {
265 unwrap_or_return!(check_coreutil_version(
266 util_name!(),
267 VERSION_MIN_MULTIPLE_USERS
268 ));
269
270 let test_users = [
271 "root",
272 "hopefully_non_existing_username1",
273 &whoami(),
274 "man",
275 "hopefully_non_existing_username2",
276 "hopefully_non_existing_username3",
277 "postfix",
278 "sshd",
279 "hopefully_non_existing_username4",
280 &whoami(),
281 ];
282
283 let ts = TestScenario::new(util_name!());
284 let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
285 ts.ucmd()
286 .args(&test_users)
287 .run()
288 .stdout_is(exp_result.stdout_str())
289 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
290 .code_is(exp_result.code());
291
292 // u/g/G z/n
293 for opt in ["--user", "--group", "--groups"] {
294 let mut args = vec![opt];
295 args.extend_from_slice(&test_users);
296 exp_result = unwrap_or_return!(expected_result(&ts, &args));
297 ts.ucmd()
298 .args(&args)
299 .run()
300 .stdout_is(exp_result.stdout_str())
301 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
302 .code_is(exp_result.code());
303 args.push("--zero");
304 exp_result = unwrap_or_return!(expected_result(&ts, &args));
305 ts.ucmd()
306 .args(&args)
307 .run()
308 .stdout_is(exp_result.stdout_str())
309 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
310 .code_is(exp_result.code());
311 args.push("--name");
312 exp_result = unwrap_or_return!(expected_result(&ts, &args));
313 ts.ucmd()
314 .args(&args)
315 .run()
316 .stdout_is(exp_result.stdout_str())
317 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
318 .code_is(exp_result.code());
319 args.pop();
320 exp_result = unwrap_or_return!(expected_result(&ts, &args));
321 ts.ucmd()
322 .args(&args)
323 .run()
324 .stdout_is(exp_result.stdout_str())
325 .stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
326 .code_is(exp_result.code());
327 }
328 }
329
330 #[test]
331 fn test_id_name_or_real_with_default_format() {
332 for flag in ["-n", "--name", "-r", "--real"] {
333 new_ucmd!()
334 .arg(flag)
335 .fails()
336 .stderr_only("id: printing only names or real IDs requires -u, -g, or -G\n");
337 }
338 }
339
340 #[test]
341 fn test_id_default_format() {
342 let ts = TestScenario::new(util_name!());
343 for opt1 in ["--name", "--real"] {
344 for opt2 in ["--user", "--group", "--groups"] {
345 // u/g/G n/r
346 let args = [opt2, opt1];
347 let result = ts.ucmd().args(&args).run();
348 let exp_result = unwrap_or_return!(expected_result(&ts, &args));
349 result
350 .stdout_is(exp_result.stdout_str())
351 .stderr_is(exp_result.stderr_str())
352 .code_is(exp_result.code());
353 }
354 }
355 for opt2 in ["--user", "--group", "--groups"] {
356 // u/g/G
357 let args = [opt2];
358 ts.ucmd()
359 .args(&args)
360 .succeeds()
361 .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
362 let args = [opt2, opt2];
363 ts.ucmd()
364 .args(&args)
365 .succeeds()
366 .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
367 }
368 }
369
370 #[test]
371 fn test_id_zero_with_default_format() {
372 for z_flag in ["-z", "--zero"] {
373 new_ucmd!()
374 .arg(z_flag)
375 .fails()
376 .stderr_only("id: option --zero not permitted in default format\n");
377 }
378 }
379
380 #[test]
381 fn test_id_zero_with_name_or_real() {
382 for z_flag in ["-z", "--zero"] {
383 for flag in ["-n", "--name", "-r", "--real"] {
384 new_ucmd!()
385 .args(&[z_flag, flag])
386 .fails()
387 .stderr_only("id: printing only names or real IDs requires -u, -g, or -G\n");
388 }
389 }
390 }
391
392 #[test]
393 fn test_id_zero() {
394 let ts = TestScenario::new(util_name!());
395 for z_flag in ["-z", "--zero"] {
396 for opt1 in ["--name", "--real"] {
397 for opt2 in ["--user", "--group", "--groups"] {
398 // u/g/G n/r z
399 let args = [opt2, z_flag, opt1];
400 let result = ts.ucmd().args(&args).run();
401 let exp_result = unwrap_or_return!(expected_result(&ts, &args));
402 result
403 .stdout_is(exp_result.stdout_str())
404 .stderr_is(exp_result.stderr_str())
405 .code_is(exp_result.code());
406 }
407 }
408 for opt2 in ["--user", "--group", "--groups"] {
409 // u/g/G z
410 let args = [opt2, z_flag];
411 ts.ucmd()
412 .args(&args)
413 .succeeds()
414 .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
415 }
416 }
417 }
418
419 #[test]
420 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
421 fn test_id_context() {
422 if !uucore::selinux::is_selinux_enabled() {
423 println!("test skipped: Kernel has no support for SElinux context");
424 return;
425 }
426 let ts = TestScenario::new(util_name!());
427 for c_flag in ["-Z", "--context"] {
428 ts.ucmd()
429 .args(&[c_flag])
430 .succeeds()
431 .stdout_only(unwrap_or_return!(expected_result(&ts, &[c_flag])).stdout_str());
432 for z_flag in ["-z", "--zero"] {
433 let args = [c_flag, z_flag];
434 ts.ucmd()
435 .args(&args)
436 .succeeds()
437 .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
438 for opt1 in ["--name", "--real"] {
439 // id: cannot print only names or real IDs in default format
440 let args = [opt1, c_flag];
441 ts.ucmd()
442 .args(&args)
443 .succeeds()
444 .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
445 let args = [opt1, c_flag, z_flag];
446 ts.ucmd()
447 .args(&args)
448 .succeeds()
449 .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
450 for opt2 in ["--user", "--group", "--groups"] {
451 // u/g/G n/r z Z
452 // for now, we print clap's standard response for "conflicts_with" instead of:
453 // id: cannot print "only" of more than one choice
454 let args = [opt2, c_flag, opt1];
455 let _result = ts.ucmd().args(&args).fails();
456 // let exp_result = unwrap_or_return!(expected_result(&args));
457 // result
458 // .stdout_is(exp_result.stdout_str())
459 // .stderr_is(exp_result.stderr_str())
460 // .code_is(exp_result.code());
461 }
462 }
463 for opt2 in ["--user", "--group", "--groups"] {
464 // u/g/G z Z
465 // for now, we print clap's standard response for "conflicts_with" instead of:
466 // id: cannot print "only" of more than one choice
467 let args = [opt2, c_flag];
468 let _result = ts.ucmd().args(&args).fails();
469 // let exp_result = unwrap_or_return!(expected_result(&args));
470 // result
471 // .stdout_is(exp_result.stdout_str())
472 // .stderr_is(exp_result.stderr_str())
473 // .code_is(exp_result.code());
474 }
475 }
476 }
477 }
478
479 #[test]
480 fn test_id_no_specified_user_posixly() {
481 // Test id output without security context
482
483 let ts = TestScenario::new(util_name!());
484 let result = ts.ucmd().env("POSIXLY_CORRECT", "1").run();
485 assert!(!result.stdout_str().contains("context="));
486 if !is_ci() {
487 result.success();
488 }
489
490 #[cfg(all(any(target_os = "linux", target_os = "android"), feature = "selinux"))]
491 {
492 if uucore::selinux::is_selinux_enabled() {
493 let result = ts.ucmd().succeeds();
494 assert!(result.stdout_str().contains("context="));
495 } else {
496 println!("test skipped: Kernel has no support for SElinux context");
497 }
498 }
499 }
500
501 #[test]
502 #[cfg(not(target_os = "android"))]
503 fn test_id_pretty_print_password_record() {
504 // `-p` is BSD only and not supported on GNU's `id`.
505 // `-P` is our own extension, and not supported by either GNU nor BSD.
506 // These must conflict, because they both set the output format.
507 new_ucmd!()
508 .arg("-p")
509 .arg("-P")
510 .fails()
511 .stderr_contains("the argument '-p' cannot be used with '-P'");
512 }
513
514 #[test]
515 #[cfg(all(feature = "chmod", feature = "chown"))]
516 fn test_id_pretty_print_suid_binary() {
517 if let Some(suid_coreutils_path) = create_root_owned_suid_coreutils_binary() {
518 let result = TestScenario::new(util_name!())
519 .cmd(suid_coreutils_path.to_str().unwrap())
520 .args(&[util_name!(), "-p"])
521 .succeeds();
522
523 // The `euid` line should be present only if the real UID does not belong to `root`
524 if rustix::process::getuid().is_root() {
525 result.stdout_does_not_contain("euid\t");
526 } else {
527 result.stdout_contains_line("euid\troot");
528 }
529
530 // The `rgid` line should be present only if the real GID does not belong to `root`
531 if rustix::process::getgid().is_root() {
532 result.stdout_does_not_contain("rgid\t");
533 } else {
534 result.stdout_contains("rgid\t");
535 }
536 } else {
537 print!("Test skipped; requires root user");
538 }
539 }
540
541 /// Create SUID temp file owned by `root:root` with the contents of the `coreutils` binary
542 #[cfg(all(feature = "chmod", feature = "chown"))]
543 fn create_root_owned_suid_coreutils_binary() -> Option<TempPath> {
544 use std::fs::read;
545 use std::io::Write;
546 use tempfile::NamedTempFile;
547 use uutests::util::{get_tests_binary, run_ucmd_as_root};
548
549 let mut temp_file = NamedTempFile::new().unwrap();
550 let coreutils_binary = read(get_tests_binary()).unwrap();
551 temp_file.write_all(&coreutils_binary).unwrap();
552 let temp_path = temp_file.into_temp_path();
553 let temp_path_str = temp_path.to_str().unwrap();
554
555 run_ucmd_as_root(&TestScenario::new("chown"), &["root:root", temp_path_str]).ok()?;
556 run_ucmd_as_root(&TestScenario::new("chmod"), &["+xs", temp_path_str]).ok()?;
557
558 Some(temp_path)
559 }
560
561 /// This test requires user with username 200 on system
562 #[test]
563 #[cfg(unix)]
564 fn test_id_digital_username() {
565 match Command::new("id")
566 .arg("200")
567 .stdout(Stdio::null())
568 .stderr(Stdio::null())
569 .status()
570 {
571 Ok(ret) if ret.success() => {}
572 Ok(_) => {
573 println!("Test skipped; requires user with username 200 on system");
574 return;
575 }
576 Err(e) => {
577 println!("failed to run id command: {e}");
578 return;
579 }
580 }
581
582 new_ucmd!().arg("200").succeeds();
583 }