Debian Codegraph @

tests/by-util/test_chown.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 (words) agroupthatdoesntexist auserthatdoesntexist cuuser groupname notexisting passgrp
7 #[cfg(all(unix, not(target_os = "openbsd")))]
8 use std::os::unix::fs::MetadataExt;
9 use uutests::util::{CmdResult, TestScenario, is_ci, run_ucmd_as_root};
10 use uutests::util_name;
11 use uutests::{at_and_ucmd, new_ucmd};
12 // Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
13 // If we are running inside the CI and "needle" is in "stderr" skipping this test is
14 // considered okay. If we are not inside the CI this calls assert!(result.success).
15 //
16 // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
17 //
18 // stderr: "whoami: cannot find name for user ID 1001"
19 // TODO: Maybe `adduser --uid 1001 username` can put things right?
20 //
21 // stderr: "id: cannot find name for group ID 116"
22 // stderr: "thread 'main' panicked at 'called `Result::unwrap()` on an `Err`
23 // value: Custom { kind: NotFound, error: "No such id: 1001" }',
24 // /project/src/uucore/src/lib/features/perms.rs:176:44"
25 //
26 fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
27 if !result.succeeded() {
28 println!("result.stdout = {}", result.stdout_str());
29 println!("result.stderr = {}", result.stderr_str());
30 if is_ci() && result.stderr_str().contains(needle) {
31 println!("test skipped:");
32 return true;
33 }
34 result.success();
35 }
36 false
37 }
38
39 #[cfg(any(target_os = "linux", target_os = "android", windows))]
40 const ROOT_GROUP: &str = "root";
41 #[cfg(not(any(target_os = "linux", target_os = "android", windows)))]
42 const ROOT_GROUP: &str = "wheel";
43
44 #[cfg(test)]
45 mod test_passgrp {
46 use chown::entries::{gid2grp, grp2gid, uid2usr, usr2uid};
47
48 #[test]
49 fn test_usr2uid() {
50 assert_eq!(0, usr2uid("root").unwrap());
51 assert!(usr2uid("88_888_888").is_err());
52 assert!(usr2uid("auserthatdoesntexist").is_err());
53 }
54
55 #[test]
56 fn test_grp2gid() {
57 assert_eq!(0, grp2gid(super::ROOT_GROUP).unwrap());
58 assert!(grp2gid("88_888_888").is_err());
59 assert!(grp2gid("agroupthatdoesntexist").is_err());
60 }
61
62 #[test]
63 fn test_uid2usr() {
64 assert_eq!("root", uid2usr(0).unwrap());
65 assert!(uid2usr(88_888_888).is_err());
66 }
67
68 #[test]
69 fn test_gid2grp() {
70 assert_eq!(super::ROOT_GROUP, gid2grp(0).unwrap());
71 assert!(gid2grp(88_888_888).is_err());
72 }
73 }
74
75 #[test]
76 fn test_invalid_option() {
77 new_ucmd!().arg("-w").arg("-q").arg("/").fails();
78 }
79
80 #[test]
81 fn test_invalid_arg() {
82 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
83 }
84
85 #[test]
86 fn test_chown_only_owner() {
87 // test chown username file.txt
88
89 let scene = TestScenario::new(util_name!());
90 let at = &scene.fixtures;
91
92 let result = scene.cmd("whoami").run();
93 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
94 return;
95 }
96 let user_name = String::from(result.stdout_str().trim());
97 assert!(!user_name.is_empty());
98
99 let file1 = "test_chown_file1";
100 at.touch(file1);
101
102 // since only superuser can change owner, we have to change from ourself to ourself
103 scene
104 .ucmd()
105 .arg(user_name)
106 .arg("--verbose")
107 .arg(file1)
108 .succeeds()
109 .stdout_contains("retained as")
110 .no_stderr();
111
112 // try to change to another existing user, e.g. 'root'
113 scene
114 .ucmd()
115 .arg("root")
116 .arg("--verbose")
117 .arg(file1)
118 .fails()
119 .stderr_contains("failed to change");
120 }
121
122 #[test]
123 fn test_chown_only_owner_colon() {
124 // test chown username: file.txt
125
126 let scene = TestScenario::new(util_name!());
127 let at = &scene.fixtures;
128
129 let result = scene.cmd("whoami").run();
130 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
131 return;
132 }
133 let user_name = String::from(result.stdout_str().trim());
134 assert!(!user_name.is_empty());
135
136 let file1 = "test_chown_file1";
137 at.touch(file1);
138
139 scene
140 .ucmd()
141 .arg(format!("{user_name}:"))
142 .arg("--verbose")
143 .arg(file1)
144 .succeeds()
145 .stdout_contains("retained as")
146 .no_stderr();
147
148 scene
149 .ucmd()
150 .arg(format!("{user_name}."))
151 .arg("--verbose")
152 .arg(file1)
153 .succeeds()
154 .stdout_contains("retained as")
155 .stderr_contains("warning: '.' should be ':'");
156
157 scene
158 .ucmd()
159 .arg("root:")
160 .arg("--verbose")
161 .arg(file1)
162 .fails()
163 .stderr_contains("failed to change");
164 }
165
166 #[test]
167 fn test_chown_dot_separator_warning() {
168 // test that using '.' as separator emits a warning
169
170 let scene = TestScenario::new(util_name!());
171 let at = &scene.fixtures;
172
173 let result = scene.cmd("whoami").run();
174 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
175 return;
176 }
177 let user_name = String::from(result.stdout_str().trim());
178 assert!(!user_name.is_empty());
179
180 let file1 = "test_chown_dot_warn";
181 at.touch(file1);
182
183 let result = scene.cmd("id").arg("-gn").run();
184 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
185 return;
186 }
187 let group_name = String::from(result.stdout_str().trim());
188 assert!(!group_name.is_empty());
189
190 // chown user. file should warn about '.' separator
191 scene
192 .ucmd()
193 .arg(format!("{user_name}."))
194 .arg(file1)
195 .succeeds()
196 .stderr_contains("warning: '.' should be ':'");
197
198 // chown user.group file should warn AND apply both owner and group
199 let result = scene
200 .ucmd()
201 .arg(format!("{user_name}.{group_name}"))
202 .arg("--verbose")
203 .arg(file1)
204 .run();
205 if skipping_test_is_okay(&result, "chown: invalid group:") {
206 return;
207 }
208 result.stderr_contains("warning: '.' should be ':'");
209 // "retained as" on Linux, "changed ownership" on BSDs (group inherited from parent dir)
210 assert!(
211 result.stdout_str().contains("retained as")
212 || result.stdout_str().contains("changed ownership"),
213 "expected verbose ownership output, got stdout={:?} stderr={:?}",
214 result.stdout_str(),
215 result.stderr_str()
216 );
217
218 // chown user: file should not warn
219 scene
220 .ucmd()
221 .arg(format!("{user_name}:"))
222 .arg(file1)
223 .succeeds()
224 .stderr_does_not_contain("warning");
225 }
226
227 #[test]
228 fn test_chown_only_colon() {
229 // test chown : file.txt
230
231 let scene = TestScenario::new(util_name!());
232 let at = &scene.fixtures;
233
234 let file1 = "test_chown_file1";
235 at.touch(file1);
236
237 // expected:
238 // $ chown -v : file.txt 2>out_err ; echo $? ; cat out_err
239 // ownership of 'file.txt' retained
240 // 0
241 let result = scene.ucmd().arg(":").arg("--verbose").arg(file1).run();
242 if skipping_test_is_okay(&result, "No such id") {
243 return;
244 }
245 result.stdout_contains("retained as");
246 result.no_stderr(); // GNU prints --verbose retained/changed lines to stdout
247
248 // test chown : file.txt
249 // expected:
250 // $ chown -v :: file.txt 2>out_err ; echo $? ; cat out_err
251 // 1
252 // chown: invalid group: '::'
253 scene
254 .ucmd()
255 .arg("::")
256 .arg("--verbose")
257 .arg(file1)
258 .fails()
259 .stderr_contains("invalid group: '::'");
260
261 scene
262 .ucmd()
263 .arg("..")
264 .arg("--verbose")
265 .arg(file1)
266 .fails()
267 .stderr_contains("invalid group: '..'");
268 }
269
270 #[test]
271 fn test_chown_failed_stdout() {
272 // test chown root file.txt
273
274 // TODO: implement once output "failed to change" to stdout is fixed
275 // expected:
276 // $ chown -v root file.txt 2>out_err ; echo $? ; cat out_err
277 // failed to change ownership of 'file.txt' from jhs to root
278 // 1
279 // chown: changing ownership of 'file.txt': Operation not permitted
280 }
281
282 #[test]
283 #[cfg(not(target_os = "openbsd"))]
284 fn test_chown_owner_group() {
285 // test chown username:group file.txt
286
287 let scene = TestScenario::new(util_name!());
288 let at = &scene.fixtures;
289
290 let result = scene.cmd("whoami").run();
291 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
292 return;
293 }
294
295 let user_name = String::from(result.stdout_str().trim());
296 assert!(!user_name.is_empty());
297
298 let file1 = "test_chown_file1";
299 at.touch(file1);
300
301 let result = scene.cmd("id").arg("-gn").run();
302 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
303 return;
304 }
305 let group_name = String::from(result.stdout_str().trim());
306 assert!(!group_name.is_empty());
307
308 let result = scene
309 .ucmd()
310 .arg(format!("{user_name}:{group_name}"))
311 .arg("--verbose")
312 .arg(file1)
313 .run();
314 if skipping_test_is_okay(&result, "chown: invalid group:") {
315 return;
316 }
317 result.stdout_contains("retained as");
318
319 scene
320 .ucmd()
321 .arg("root:root:root")
322 .arg("--verbose")
323 .arg(file1)
324 .fails()
325 .stderr_contains("invalid group");
326
327 scene
328 .ucmd()
329 .arg("root.root.root")
330 .arg("--verbose")
331 .arg(file1)
332 .fails()
333 .stderr_contains("invalid group");
334
335 scene
336 .ucmd()
337 .arg(format!("root:{ROOT_GROUP}"))
338 .arg("--verbose")
339 .arg(file1)
340 .fails()
341 .stderr_contains("failed to change");
342 }
343
344 #[test]
345 #[cfg(not(target_os = "openbsd"))]
346 fn test_chown_various_input() {
347 // test chown username:group file.txt
348
349 let scene = TestScenario::new(util_name!());
350 let at = &scene.fixtures;
351
352 let result = scene.cmd("whoami").run();
353 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
354 return;
355 }
356
357 let user_name = String::from(result.stdout_str().trim());
358 assert!(!user_name.is_empty());
359
360 let file1 = "test_chown_file1";
361 at.touch(file1);
362
363 let result = scene.cmd("id").arg("-gn").run();
364 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
365 return;
366 }
367 let group_name = String::from(result.stdout_str().trim());
368 assert!(!group_name.is_empty());
369
370 let result = scene
371 .ucmd()
372 .arg(format!("{user_name}:{group_name}"))
373 .arg("--verbose")
374 .arg(file1)
375 .run();
376 if skipping_test_is_okay(&result, "chown: invalid group:") {
377 return;
378 }
379 result.stdout_contains("retained as");
380
381 // check that username.groupname is understood
382 let result = scene
383 .ucmd()
384 .arg(format!("{user_name}.{group_name}"))
385 .arg("--verbose")
386 .arg(file1)
387 .run();
388 if skipping_test_is_okay(&result, "chown: invalid group:") {
389 return;
390 }
391 result.stdout_contains("retained as");
392
393 // Fails as user.name doesn't exist in the CI
394 // but it is valid
395 scene
396 .ucmd()
397 .arg(format!("{}:{}", "user.name", "groupname"))
398 .arg("--verbose")
399 .arg(file1)
400 .fails()
401 .stderr_contains("chown: invalid user: 'user.name:groupname'");
402 }
403
404 #[test]
405 #[cfg(any(windows, all(unix, not(target_os = "openbsd"))))]
406 fn test_chown_only_group() {
407 // test chown :group file.txt
408
409 let scene = TestScenario::new(util_name!());
410 let at = &scene.fixtures;
411
412 let result = scene.cmd("id").arg("-gn").run();
413 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
414 return;
415 }
416 let group_name = String::from(result.stdout_str().trim());
417 assert!(!group_name.is_empty());
418
419 let file1 = "test_chown_file1";
420 at.touch(file1);
421
422 let result = scene
423 .ucmd()
424 .arg(format!(":{group_name}"))
425 .arg("--verbose")
426 .arg(file1)
427 .run();
428 result.stdout_contains("retained as");
429 result.success();
430
431 // FreeBSD user on CI is part of wheel group
432 if group_name != ROOT_GROUP {
433 scene
434 .ucmd()
435 .arg(format!(":{ROOT_GROUP}"))
436 .arg("--verbose")
437 .arg(file1)
438 .fails()
439 .stderr_contains("failed to change");
440 }
441 }
442
443 #[test]
444 fn test_chown_only_user_id() {
445 // test chown 1111 file.txt
446
447 let scene = TestScenario::new(util_name!());
448 let at = &scene.fixtures;
449
450 let result = scene.cmd("id").arg("-u").run();
451 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
452 return;
453 }
454 let user_id = String::from(result.stdout_str().trim());
455 assert!(!user_id.is_empty());
456
457 let file1 = "test_chown_file1";
458 at.touch(file1);
459
460 let result = scene.ucmd().arg(user_id).arg("--verbose").arg(file1).run();
461 if skipping_test_is_okay(&result, "invalid user") {
462 // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
463 // stderr: "chown: invalid user: '1001'
464 return;
465 }
466 result.stdout_contains("retained as");
467
468 scene
469 .ucmd()
470 .arg("0")
471 .arg("--verbose")
472 .arg(file1)
473 .fails()
474 .stderr_contains("failed to change");
475 }
476
477 #[test]
478 fn test_chown_fail_id() {
479 // test chown 1111. file.txt
480
481 let scene = TestScenario::new(util_name!());
482 let at = &scene.fixtures;
483
484 let result = scene.cmd("id").arg("-u").run();
485 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
486 return;
487 }
488 let user_id = String::from(result.stdout_str().trim());
489 assert!(!user_id.is_empty());
490
491 let file1 = "test_chown_file1";
492 at.touch(file1);
493
494 scene
495 .ucmd()
496 .arg(format!("{user_id}:"))
497 .arg(file1)
498 .fails()
499 .stderr_contains("invalid spec");
500
501 scene
502 .ucmd()
503 .arg(format!("{user_id}."))
504 .arg(file1)
505 .fails()
506 .stderr_contains("invalid spec");
507 }
508
509 /// Test for setting the owner to a user ID for a user that does not exist.
510 ///
511 /// For example:
512 ///
513 /// $ touch f && chown 12345 f
514 ///
515 /// succeeds with exit status 0 and outputs nothing. The owner of the
516 /// file is set to 12345, even though no user with that ID exists.
517 ///
518 /// This test must be run as root, because only the root user can
519 /// transfer ownership of a file.
520 #[test]
521 fn test_chown_only_user_id_nonexistent_user() {
522 let ts = TestScenario::new(util_name!());
523 let at = &ts.fixtures;
524 at.touch("f");
525 if let Ok(result) = run_ucmd_as_root(&ts, &["12345", "f"]) {
526 result.success().no_output();
527 } else {
528 print!("Test skipped; requires root user");
529 }
530 }
531
532 #[test]
533 #[cfg(not(target_os = "openbsd"))]
534 fn test_chown_only_group_id() {
535 // test chown :1111 file.txt
536
537 let scene = TestScenario::new(util_name!());
538 let at = &scene.fixtures;
539
540 let result = scene.cmd("id").arg("-g").run();
541 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
542 return;
543 }
544 let group_id = String::from(result.stdout_str().trim());
545 assert!(!group_id.is_empty());
546
547 let file1 = "test_chown_file1";
548 at.touch(file1);
549
550 let result = scene
551 .ucmd()
552 .arg(format!(":{group_id}"))
553 .arg("--verbose")
554 .arg(file1)
555 .run();
556 if skipping_test_is_okay(&result, "chown: invalid group:") {
557 // With mac into the CI, we can get this answer
558 return;
559 }
560 result.stdout_contains("retained as");
561
562 // Apparently on CI "macos-latest, x86_64-apple-darwin, feat_os_unix"
563 // the process has the rights to change from runner:staff to runner:wheel
564 #[cfg(any(windows, all(unix, not(target_vendor = "apple"))))]
565 // FreeBSD user on CI is part of wheel group
566 if group_id != "0" {
567 scene
568 .ucmd()
569 .arg(":0")
570 .arg("--verbose")
571 .arg(file1)
572 .fails()
573 .stderr_contains("failed to change");
574 }
575 }
576
577 /// Test for setting the group to a group ID for a group that does not exist.
578 ///
579 /// For example:
580 ///
581 /// $ touch f && chown :12345 f
582 ///
583 /// succeeds with exit status 0 and outputs nothing. The group of the
584 /// file is set to 12345, even though no group with that ID exists.
585 ///
586 /// This test must be run as root, because only the root user can
587 /// transfer ownership of a file.
588 #[test]
589 fn test_chown_only_group_id_nonexistent_group() {
590 let ts = TestScenario::new(util_name!());
591 let at = &ts.fixtures;
592 at.touch("f");
593 if let Ok(result) = run_ucmd_as_root(&ts, &[":12345", "f"]) {
594 result.success().no_output();
595 } else {
596 print!("Test skipped; requires root user");
597 }
598 }
599
600 #[test]
601 #[cfg(not(target_os = "openbsd"))]
602 fn test_chown_owner_group_id() {
603 // test chown 1111:1111 file.txt
604
605 let scene = TestScenario::new(util_name!());
606 let at = &scene.fixtures;
607
608 let result = scene.cmd("id").arg("-u").run();
609 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
610 return;
611 }
612 let user_id = String::from(result.stdout_str().trim());
613 assert!(!user_id.is_empty());
614
615 let result = scene.cmd("id").arg("-g").run();
616 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
617 return;
618 }
619 let group_id = String::from(result.stdout_str().trim());
620 assert!(!group_id.is_empty());
621
622 let file1 = "test_chown_file1";
623 at.touch(file1);
624
625 let result = scene
626 .ucmd()
627 .arg(format!("{user_id}:{group_id}"))
628 .arg("--verbose")
629 .arg(file1)
630 .run();
631 if skipping_test_is_okay(&result, "invalid user") {
632 // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
633 // stderr: "chown: invalid user: '1001:116'
634 return;
635 }
636 result.stdout_contains("retained as");
637
638 let result = scene
639 .ucmd()
640 .arg(format!("{user_id}.{group_id}"))
641 .arg("--verbose")
642 .arg(file1)
643 .run();
644 if skipping_test_is_okay(&result, "invalid user") {
645 // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
646 // stderr: "chown: invalid user: '1001.116'
647 return;
648 }
649 result.stdout_contains("retained as");
650
651 scene
652 .ucmd()
653 .arg("0:0")
654 .arg("--verbose")
655 .arg(file1)
656 .fails()
657 .stderr_contains("failed to change");
658 }
659
660 #[test]
661 #[cfg(not(target_os = "openbsd"))]
662 fn test_chown_owner_group_mix() {
663 // test chown 1111:group file.txt
664
665 let scene = TestScenario::new(util_name!());
666 let at = &scene.fixtures;
667
668 let result = scene.cmd("id").arg("-u").run();
669 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
670 return;
671 }
672 let user_id = String::from(result.stdout_str().trim());
673 assert!(!user_id.is_empty());
674
675 let result = scene.cmd("id").arg("-gn").run();
676 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
677 return;
678 }
679 let group_name = String::from(result.stdout_str().trim());
680 assert!(!group_name.is_empty());
681
682 let file1 = "test_chown_file1";
683 at.touch(file1);
684
685 let result = scene
686 .ucmd()
687 .arg(format!("{user_id}:{group_name}"))
688 .arg("--verbose")
689 .arg(file1)
690 .run();
691 result.stdout_contains("retained as");
692
693 scene
694 .ucmd()
695 .arg(format!("0:{ROOT_GROUP}"))
696 .arg("--verbose")
697 .arg(file1)
698 .fails()
699 .stderr_contains("failed to change");
700 }
701
702 #[test]
703 fn test_chown_recursive() {
704 let scene = TestScenario::new(util_name!());
705 let at = &scene.fixtures;
706
707 let result = scene.cmd("whoami").run();
708 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
709 return;
710 }
711 let user_name = String::from(result.stdout_str().trim());
712 assert!(!user_name.is_empty());
713
714 at.mkdir_all("a/b/c");
715 at.mkdir("z");
716 at.touch(at.plus_as_string("a/a"));
717 at.touch(at.plus_as_string("a/b/b"));
718 at.touch(at.plus_as_string("a/b/c/c"));
719 at.touch(at.plus_as_string("z/y"));
720
721 scene
722 .ucmd()
723 .arg("-R")
724 .arg("--verbose")
725 .arg(user_name)
726 .arg("a")
727 .arg("z")
728 .succeeds()
729 .stdout_contains("ownership of 'a/a' retained as")
730 .stdout_contains("ownership of 'z/y' retained as");
731 }
732
733 #[test]
734 fn test_root_preserve() {
735 let scene = TestScenario::new(util_name!());
736
737 let result = scene.cmd("whoami").run();
738 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
739 return;
740 }
741 let user_name = String::from(result.stdout_str().trim());
742 assert!(!user_name.is_empty());
743
744 let result = scene
745 .ucmd()
746 .arg("--preserve-root")
747 .arg("-R")
748 .arg(user_name)
749 .arg("/")
750 .fails();
751 result.stderr_contains("chown: it is dangerous to operate recursively");
752 }
753
754 #[cfg(any(target_os = "linux", target_os = "android"))]
755 #[test]
756 fn test_big_p() {
757 if !rustix::process::geteuid().is_root() {
758 new_ucmd!()
759 .arg("-RP")
760 .arg("bin")
761 .arg("/proc/self/cwd")
762 .fails()
763 .stderr_contains(
764 // linux fails with "Operation not permitted (os error 1)"
765 // because of insufficient permissions,
766 // android fails with "Permission denied (os error 13)"
767 // because it can't resolve /proc (even though it can resolve /proc/self/)
768 "chown: changing ownership of '/proc/self/cwd': ",
769 );
770 }
771 }
772
773 #[test]
774 fn test_chown_file_notexisting() {
775 // test chown username not_existing
776
777 let scene = TestScenario::new(util_name!());
778
779 let result = scene.cmd("whoami").run();
780 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
781 return;
782 }
783 let user_name = String::from(result.stdout_str().trim());
784 assert!(!user_name.is_empty());
785
786 scene
787 .ucmd()
788 .arg(&user_name)
789 .arg("--verbose")
790 .arg("not_existing")
791 .fails()
792 .stdout_contains(format!(
793 "failed to change ownership of 'not_existing' to {user_name}"
794 ));
795 // TODO: uncomment once message changed from "cannot dereference" to "cannot access"
796 // result.stderr_contains("cannot access 'not_existing': No such file or directory");
797 }
798
799 #[test]
800 fn test_chown_no_change_to_user() {
801 let scene = TestScenario::new(util_name!());
802 let at = &scene.fixtures;
803
804 let result = scene.cmd("whoami").run();
805 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
806 return;
807 }
808 let user_name = String::from(result.stdout_str().trim());
809 assert!(!user_name.is_empty());
810
811 for (i, from) in ["42", ":42", "42:42"].iter().enumerate() {
812 let file = i.to_string();
813 at.touch(&file);
814 scene
815 .ucmd()
816 .arg("-v")
817 .arg(format!("--from={from}"))
818 .arg("43")
819 .arg(&file)
820 .succeeds()
821 .stdout_only(format!("ownership of '{file}' retained as {user_name}\n"));
822 }
823 }
824
825 #[test]
826 #[cfg(not(target_os = "openbsd"))]
827 fn test_chown_no_change_to_group() {
828 let scene = TestScenario::new(util_name!());
829 let at = &scene.fixtures;
830
831 let result = scene.cmd("whoami").run();
832 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
833 return;
834 }
835 let user_name = String::from(result.stdout_str().trim());
836 assert!(!user_name.is_empty());
837 let result = scene.cmd("id").arg("-ng").run();
838 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
839 return;
840 }
841 let group_name = String::from(result.stdout_str().trim());
842 assert!(!group_name.is_empty());
843
844 for (i, from) in ["42", ":42", "42:42"].iter().enumerate() {
845 let file = i.to_string();
846 at.touch(&file);
847 scene
848 .ucmd()
849 .arg("-v")
850 .arg(format!("--from={from}"))
851 .arg(":43")
852 .arg(&file)
853 .succeeds()
854 .stdout_only(format!("ownership of '{file}' retained as {group_name}\n"));
855 }
856 }
857
858 #[test]
859 #[cfg(not(target_os = "openbsd"))]
860 fn test_chown_no_change_to_user_group() {
861 let scene = TestScenario::new(util_name!());
862 let at = &scene.fixtures;
863
864 let result = scene.cmd("whoami").run();
865 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
866 return;
867 }
868 let user_name = String::from(result.stdout_str().trim());
869 assert!(!user_name.is_empty());
870 let result = scene.cmd("id").arg("-ng").run();
871 if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
872 return;
873 }
874 let group_name = String::from(result.stdout_str().trim());
875 assert!(!group_name.is_empty());
876
877 for (i, from) in ["42", ":42", "42:42"].iter().enumerate() {
878 let file = i.to_string();
879 at.touch(&file);
880 scene
881 .ucmd()
882 .arg("-v")
883 .arg(format!("--from={from}"))
884 .arg("43:43")
885 .arg(&file)
886 .succeeds()
887 .stdout_only(format!(
888 "ownership of '{file}' retained as {user_name}:{group_name}\n"
889 ));
890 }
891 }
892
893 #[test]
894 fn test_chown_reference_file() {
895 let (at, mut ucmd) = at_and_ucmd!();
896 at.touch("a");
897 at.touch("b");
898 ucmd.arg("--verbose")
899 .arg("--reference")
900 .arg("a")
901 .arg("b")
902 .succeeds()
903 .stdout_contains("ownership of 'b' retained as")
904 .no_stderr();
905 }
906
907 #[test]
908 #[cfg(all(unix, not(target_os = "openbsd")))]
909 fn test_chown_no_dereference_symlink_to_dir() {
910 let scene = TestScenario::new(util_name!());
911 let at = &scene.fixtures;
912
913 let result = scene.cmd("whoami").run();
914 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
915 return;
916 }
917 let user_name = String::from(result.stdout_str().trim());
918 assert!(!user_name.is_empty());
919
920 at.mkdir("dir");
921 at.symlink_dir("dir", "link_to_dir");
922
923 let link_meta_before = std::fs::symlink_metadata(at.plus("link_to_dir")).unwrap();
924 let link_ctime_before = (link_meta_before.ctime(), link_meta_before.ctime_nsec());
925
926 let dir_meta_before = std::fs::metadata(at.plus("dir")).unwrap();
927 let dir_ctime_before = (dir_meta_before.ctime(), dir_meta_before.ctime_nsec());
928
929 scene
930 .ucmd()
931 .arg("--no-dereference")
932 .arg(&user_name)
933 .arg("link_to_dir")
934 .succeeds();
935
936 let link_meta_after = std::fs::symlink_metadata(at.plus("link_to_dir")).unwrap();
937 let link_ctime_after = (link_meta_after.ctime(), link_meta_after.ctime_nsec());
938
939 let dir_meta_after = std::fs::metadata(at.plus("dir")).unwrap();
940 let dir_ctime_after = (dir_meta_after.ctime(), dir_meta_after.ctime_nsec());
941
942 assert_ne!(
943 link_ctime_before, link_ctime_after,
944 "link's ctime should have advanced"
945 );
946 assert_eq!(
947 dir_ctime_before, dir_ctime_after,
948 "dir's ctime should not have changed"
949 );
950 }
951
952 #[test]
953 fn test_chown_symlink_cycles() {
954 let scene = TestScenario::new(util_name!());
955 let at = &scene.fixtures;
956
957 let result = scene.cmd("whoami").run();
958 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
959 return;
960 }
961 let user_name = String::from(result.stdout_str().trim());
962 assert!(!user_name.is_empty());
963
964 at.mkdir_all("a/b/c");
965 at.symlink_dir("a", "a/b/c/d");
966
967 let result = scene.ucmd().arg("-vRL").arg(&user_name).arg("a").run();
968
969 if cfg!(target_vendor = "apple") || cfg!(target_os = "openbsd") || cfg!(target_os = "android") {
970 result
971 .stdout_contains(format!("ownership of 'a' retained as {user_name}"))
972 .stdout_contains(format!("ownership of 'a/b' retained as {user_name}"))
973 .stdout_contains(format!("ownership of 'a/b/c' retained as {user_name}"))
974 .stdout_does_not_contain(format!("ownership of 'a/b/c/d' retained as {user_name}"))
975 .stdout_does_not_contain(format!("ownership of 'a/b/c/d/b' retained as {user_name}"))
976 .stdout_does_not_contain(format!(
977 "ownership of 'a/b/c/d/b/c' retained as {user_name}"
978 ));
979 } else {
980 result
981 .success()
982 .stdout_contains(format!("ownership of 'a' retained as {user_name}"))
983 .stdout_contains(format!("ownership of 'a/b' retained as {user_name}"))
984 .stdout_contains(format!("ownership of 'a/b/c' retained as {user_name}"))
985 .stdout_contains(format!("ownership of 'a/b/c/d' retained as {user_name}"))
986 .stdout_does_not_contain(format!("ownership of 'a/b/c/d/b' retained as {user_name}"))
987 .stdout_does_not_contain(format!(
988 "ownership of 'a/b/c/d/b/c' retained as {user_name}"
989 ))
990 .stdout_does_not_contain(format!(
991 "ownership of 'a/b/c/d/b/c/d' retained as {user_name}"
992 ));
993 }
994 }
995
996 #[test]
997 fn test_chown_symlink_two_links_same_dir() {
998 // Two symlinks pointing at the same directory is NOT a cycle: neither link is
999 // an ancestor of the other, so the target's contents must be visited through
1000 // *both* links. This guards the backtracking in the linux safe-traversal
1001 // cycle detection against false positives.
1002 let scene = TestScenario::new(util_name!());
1003 let at = &scene.fixtures;
1004
1005 let result = scene.cmd("whoami").run();
1006 if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
1007 return;
1008 }
1009 let user_name = String::from(result.stdout_str().trim());
1010 assert!(!user_name.is_empty());
1011
1012 // cSpell:disable
1013 at.mkdir_all("base/realdir");
1014 at.touch("base/realdir/file");
1015 at.symlink_dir("base/realdir", "base/link1");
1016 at.symlink_dir("base/realdir", "base/link2");
1017
1018 let result = scene.ucmd().arg("-vRL").arg(&user_name).arg("base").run();
1019
1020 // Only linux uses the safe-traversal cycle detection that this test exercises;
1021 // other platforms fall back to walkdir with its own loop handling.
1022 if cfg!(target_os = "linux") {
1023 result
1024 .success()
1025 .stdout_contains(format!(
1026 "ownership of 'base/realdir/file' retained as {user_name}"
1027 ))
1028 .stdout_contains(format!(
1029 "ownership of 'base/link1/file' retained as {user_name}"
1030 ))
1031 .stdout_contains(format!(
1032 "ownership of 'base/link2/file' retained as {user_name}"
1033 ));
1034 }
1035 // cSpell:enable
1036 }
1037
1038 #[cfg(target_os = "linux")]
1039 #[test]
1040 fn verbose_missing_file_write_error_is_reported_not_panic() {
1041 use rustix::process::geteuid;
1042 use std::fs::OpenOptions;
1043
1044 let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap();
1045 new_ucmd!()
1046 .arg("--verbose")
1047 .arg(geteuid().to_string())
1048 .arg("does-not-exist")
1049 .set_stdout(dev_full)
1050 .fails_with_code(1)
1051 .stderr_contains("chown: write error: No space left on device");
1052 }