Debian Codegraph @

tests/by-util/test_install.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) helloworld nodir objdump n'source nconfined testdir
7
8 use rustix::process::{getegid, geteuid};
9 use std::env::current_exe;
10 use std::fs;
11 #[cfg(target_os = "linux")]
12 use std::os::unix::ffi::OsStringExt;
13 use std::os::unix::fs::{MetadataExt, PermissionsExt};
14 #[cfg(any(target_os = "linux", target_os = "android"))]
15 use std::thread::sleep;
16 use uucore::error::strip_errno;
17 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
18 use uucore::selinux::get_getfattr_output;
19 use uutests::at_and_ucmd;
20 use uutests::new_ucmd;
21 use uutests::util::{TestScenario, is_ci, run_ucmd_as_root};
22 use uutests::util_name;
23
24 #[test]
25 fn test_invalid_arg() {
26 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
27 }
28
29 #[test]
30 fn test_install_basic_try_reflink() {
31 let (at, mut ucmd) = at_and_ucmd!();
32 let dir = "target_dir";
33 let file1 = "source_file1";
34 let file2 = "source_file2";
35
36 at.touch(file1);
37 at.touch(file2);
38 at.mkdir(dir);
39 #[cfg(not(target_os = "linux"))]
40 ucmd.arg(file1).arg(file2).arg(dir).succeeds().no_stderr();
41 // mkfs.btrfs needs root. use strace instead.
42 #[cfg(target_os = "linux")]
43 if std::process::Command::new("strace")
44 .args(["-qqq", "-o", "strace.out", "-e", "trace=ioctl"])
45 .arg(uutests::util::get_tests_binary())
46 .args(["install", file1, file2, dir])
47 .current_dir(at.as_string())
48 .output()
49 .is_ok()
50 {
51 assert!(at.read("strace.out").contains("FICLONE"));
52 } else {
53 // missing strace
54 ucmd.arg(file1).arg(file2).arg(dir).succeeds().no_stderr();
55 }
56
57 assert!(at.file_exists(file1));
58 assert!(at.file_exists(file2));
59 assert!(at.file_exists(format!("{dir}/{file1}")));
60 assert!(at.file_exists(format!("{dir}/{file2}")));
61 }
62
63 #[test]
64 fn test_install_twice_dir() {
65 let dir = "dir";
66 let scene = TestScenario::new(util_name!());
67
68 scene.ucmd().arg("-d").arg(dir).succeeds();
69 scene.ucmd().arg("-d").arg(dir).succeeds();
70 let at = &scene.fixtures;
71
72 assert!(at.dir_exists(dir));
73 }
74
75 #[test]
76 fn test_install_failing_not_dir() {
77 let (at, mut ucmd) = at_and_ucmd!();
78 let file1 = "file1";
79 let file2 = "file2";
80 let file3 = "file3";
81
82 at.touch(file1);
83 at.touch(file2);
84 at.touch(file3);
85 ucmd.arg(file1)
86 .arg(file2)
87 .arg(file3)
88 .fails()
89 .stderr_contains("not a directory");
90 }
91
92 #[test]
93 fn test_install_ancestors_directories() {
94 let (at, mut ucmd) = at_and_ucmd!();
95 let ancestor1 = "ancestor1";
96 let ancestor2 = "ancestor1/ancestor2";
97 let target_dir = "ancestor1/ancestor2/target_dir";
98 let directories_arg = "-d";
99
100 ucmd.args(&[directories_arg, target_dir])
101 .succeeds()
102 .no_stderr();
103
104 assert!(at.dir_exists(ancestor1));
105 assert!(at.dir_exists(ancestor2));
106 assert!(at.dir_exists(target_dir));
107 }
108
109 #[test]
110 fn test_install_ancestors_mode_directories() {
111 let (at, mut ucmd) = at_and_ucmd!();
112 let ancestor1 = "ancestor1";
113 let ancestor2 = "ancestor1/ancestor2";
114 let target_dir = "ancestor1/ancestor2/target_dir";
115 let directories_arg = "-d";
116 let mode_arg = "--mode=200";
117 let probe = "probe";
118
119 at.mkdir(probe);
120 let default_perms = at.metadata(probe).permissions().mode();
121
122 ucmd.args(&[mode_arg, directories_arg, target_dir])
123 .succeeds()
124 .no_stderr();
125
126 assert!(at.dir_exists(ancestor1));
127 assert!(at.dir_exists(ancestor2));
128 assert!(at.dir_exists(target_dir));
129
130 assert_eq!(default_perms, at.metadata(ancestor1).permissions().mode());
131 assert_eq!(default_perms, at.metadata(ancestor2).permissions().mode());
132
133 // Expected mode only on the target_dir.
134 assert_eq!(0o40_200_u32, at.metadata(target_dir).permissions().mode());
135 }
136
137 #[test]
138 fn test_install_ancestors_mode_directories_with_file() {
139 let (at, mut ucmd) = at_and_ucmd!();
140 let ancestor1 = "ancestor1";
141 let ancestor2 = "ancestor1/ancestor2";
142 let target_file = "ancestor1/ancestor2/target_file";
143 let directories_arg = "-D";
144 let mode_arg = "--mode=200";
145 let file = "file";
146 let probe = "probe";
147
148 at.mkdir(probe);
149 let default_perms = at.metadata(probe).permissions().mode();
150
151 at.touch(file);
152
153 ucmd.args(&[mode_arg, directories_arg, file, target_file])
154 .succeeds()
155 .no_stderr();
156
157 assert!(at.dir_exists(ancestor1));
158 assert!(at.dir_exists(ancestor2));
159 assert!(at.file_exists(target_file));
160
161 assert_eq!(default_perms, at.metadata(ancestor1).permissions().mode());
162 assert_eq!(default_perms, at.metadata(ancestor2).permissions().mode());
163
164 // Expected mode only on the target_file.
165 assert_eq!(0o100_200_u32, at.metadata(target_file).permissions().mode());
166 }
167
168 #[test]
169 fn test_install_parent_directories() {
170 let (at, mut ucmd) = at_and_ucmd!();
171 let ancestor1 = "ancestor1";
172 let ancestor2 = "ancestor1/ancestor2";
173 let target_dir = "ancestor1/ancestor2/target_dir";
174 let directories_arg = "-d";
175
176 // Here one of the ancestors already exist and only the target_dir and
177 // its parent must be created.
178 at.mkdir(ancestor1);
179
180 ucmd.args(&[directories_arg, target_dir])
181 .succeeds()
182 .no_stderr();
183
184 assert!(at.dir_exists(ancestor2));
185 assert!(at.dir_exists(target_dir));
186 }
187
188 #[test]
189 fn test_install_several_directories() {
190 let (at, mut ucmd) = at_and_ucmd!();
191 let dir1 = "dir1";
192 let dir2 = "dir2";
193 let dir3 = "dir3";
194 let directories_arg = "-d";
195
196 ucmd.args(&[directories_arg, dir1, dir2, dir3])
197 .succeeds()
198 .no_stderr();
199
200 assert!(at.dir_exists(dir1));
201 assert!(at.dir_exists(dir2));
202 assert!(at.dir_exists(dir3));
203 }
204
205 #[test]
206 fn test_install_mode_numeric() {
207 let scene = TestScenario::new(util_name!());
208 let at = &scene.fixtures;
209 let dir = "dir1";
210 let dir2 = "dir2";
211
212 let file = "file";
213 let mode_arg = "--mode=333";
214
215 at.touch(file);
216 at.mkdir(dir);
217 scene
218 .ucmd()
219 .arg(file)
220 .arg(dir)
221 .arg(mode_arg)
222 .succeeds()
223 .no_stderr();
224
225 let dest_file = &format!("{dir}/{file}");
226 assert!(at.file_exists(file));
227 assert!(at.file_exists(dest_file));
228 let permissions = at.metadata(dest_file).permissions();
229 assert_eq!(0o100_333_u32, PermissionsExt::mode(&permissions));
230
231 let mode_arg = "-m 0333";
232 at.mkdir(dir2);
233
234 scene.ucmd().arg(mode_arg).arg(file).arg(dir2).succeeds();
235
236 let dest_file = &format!("{dir2}/{file}");
237 assert!(at.file_exists(file));
238 assert!(at.file_exists(dest_file));
239 let permissions = at.metadata(dest_file).permissions();
240 assert_eq!(0o100_333_u32, PermissionsExt::mode(&permissions));
241 }
242
243 #[test]
244 fn test_install_mode_symbolic() {
245 let (at, mut ucmd) = at_and_ucmd!();
246 let dir = "target_dir";
247 let file = "source_file";
248 let mode_arg = "--mode=o+wx";
249
250 at.touch(file);
251 at.mkdir(dir);
252 ucmd.arg(file).arg(dir).arg(mode_arg).succeeds().no_stderr();
253
254 let dest_file = &format!("{dir}/{file}");
255 assert!(at.file_exists(file));
256 assert!(at.file_exists(dest_file));
257 let permissions = at.metadata(dest_file).permissions();
258 assert_eq!(0o100_003_u32, PermissionsExt::mode(&permissions));
259 }
260
261 #[test]
262 fn test_install_mode_comma_separated() {
263 let (at, mut ucmd) = at_and_ucmd!();
264 let dir = "target_dir";
265 let file = "source_file";
266 // Test comma-separated mode like chmod: ug+rwX,o+rX
267 let mode_arg = "--mode=ug+rwX,o+rX";
268
269 at.touch(file);
270 at.mkdir(dir);
271 ucmd.arg(file).arg(dir).arg(mode_arg).succeeds().no_stderr();
272
273 let dest_file = &format!("{dir}/{file}");
274 assert!(at.file_exists(file));
275 assert!(at.file_exists(dest_file));
276 let permissions = at.metadata(dest_file).permissions();
277 // ug+rwX: For files, X only adds execute if file already has execute (it doesn't here, starting at 0)
278 // So this adds rw to user and group = 0o660
279 // o+rX: For files, X doesn't add execute, so this adds r to others = 0o004
280 // Total: 0o664 for file (0o100_664)
281 assert_eq!(0o100_664_u32, PermissionsExt::mode(&permissions));
282 }
283
284 #[test]
285 fn test_install_mode_comma_separated_directory() {
286 let scene = TestScenario::new(util_name!());
287 let at = &scene.fixtures;
288 let dir = "test_dir";
289 // Test comma-separated mode for directory creation: ug+rwX,o+rX
290 let mode_arg = "--mode=ug+rwX,o+rX";
291
292 scene
293 .ucmd()
294 .arg("-d")
295 .arg(dir)
296 .arg(mode_arg)
297 .succeeds()
298 .no_stderr();
299
300 assert!(at.dir_exists(dir));
301 let permissions = at.metadata(dir).permissions();
302 // ug+rwX sets user and group to rwx (0o770), o+rX sets others to r-x (0o005)
303 // Total: 0o775 for directory (0o040_775)
304 assert_eq!(0o040_775_u32, PermissionsExt::mode(&permissions));
305 }
306
307 #[test]
308 fn test_install_mode_symbolic_ignore_umask() {
309 let (at, mut ucmd) = at_and_ucmd!();
310 let dir = "target_dir";
311 let file = "source_file";
312 let mode_arg = "--mode=+w";
313
314 at.touch(file);
315 at.mkdir(dir);
316 ucmd.arg(file)
317 .arg(dir)
318 .arg(mode_arg)
319 .umask(0o022)
320 .succeeds()
321 .no_stderr();
322
323 let dest_file = &format!("{dir}/{file}");
324 assert!(at.file_exists(file));
325 assert!(at.file_exists(dest_file));
326 let permissions = at.metadata(dest_file).permissions();
327 assert_eq!(0o100_222_u32, PermissionsExt::mode(&permissions));
328 }
329
330 #[test]
331 fn test_install_mode_failing() {
332 let (at, mut ucmd) = at_and_ucmd!();
333 let dir = "target_dir";
334 let file = "source_file";
335 let mode_arg = "--mode=999";
336
337 at.touch(file);
338 at.mkdir(dir);
339 ucmd.arg(file)
340 .arg(dir)
341 .arg(mode_arg)
342 .fails()
343 .stderr_contains("Invalid mode string: invalid digit found in string");
344
345 let dest_file = &format!("{dir}/{file}");
346 assert!(at.file_exists(file));
347 assert!(!at.file_exists(dest_file));
348 }
349
350 #[test]
351 fn test_install_mode_directories() {
352 let (at, mut ucmd) = at_and_ucmd!();
353 let component = "component";
354 let directories_arg = "-d";
355 let mode_arg = "--mode=333";
356
357 ucmd.arg(directories_arg)
358 .arg(component)
359 .arg(mode_arg)
360 .succeeds()
361 .no_stderr();
362
363 assert!(at.dir_exists(component));
364 let permissions = at.metadata(component).permissions();
365 assert_eq!(0o040_333_u32, PermissionsExt::mode(&permissions));
366 }
367
368 #[test]
369 fn test_install_target_file() {
370 let (at, mut ucmd) = at_and_ucmd!();
371 let file1 = "source_file";
372 let file2 = "target_file";
373
374 at.touch(file1);
375 at.touch(file2);
376 ucmd.arg(file1).arg(file2).succeeds().no_stderr();
377
378 assert!(at.file_exists(file1));
379 assert!(at.file_exists(file2));
380 }
381
382 #[test]
383 fn test_install_missing_source_reports_cannot_stat_with_path() {
384 new_ucmd!()
385 .arg("missing_source")
386 .arg("target_file")
387 .fails_with_code(1)
388 .stderr_contains("cannot stat 'missing_source': No such file or directory");
389 }
390
391 #[test]
392 fn test_install_target_new_file() {
393 let (at, mut ucmd) = at_and_ucmd!();
394 let file = "file";
395 let dir = "target_dir";
396
397 at.touch(file);
398 at.mkdir(dir);
399 ucmd.arg(file)
400 .arg(format!("{dir}/{file}"))
401 .succeeds()
402 .no_stderr();
403
404 assert!(at.file_exists(file));
405 assert!(at.file_exists(format!("{dir}/{file}")));
406 }
407
408 #[test]
409 fn test_install_target_new_file_with_group() {
410 let (at, mut ucmd) = at_and_ucmd!();
411 let file = "file";
412 let dir = "target_dir";
413 let gid = getegid().as_raw();
414
415 at.touch(file);
416 at.mkdir(dir);
417 let result = ucmd
418 .arg(file)
419 .arg("--group")
420 .arg(gid.to_string())
421 .arg(format!("{dir}/{file}"))
422 .run();
423
424 if is_ci() && result.stderr_str().contains("no such group:") {
425 // In the CI, some server are failing to return the group.
426 // As seems to be a configuration issue, ignoring it
427 return;
428 }
429
430 result.success();
431 assert!(at.file_exists(file));
432 assert!(at.file_exists(format!("{dir}/{file}")));
433 }
434
435 #[test]
436 fn test_install_target_new_file_with_owner() {
437 let (at, mut ucmd) = at_and_ucmd!();
438 let file = "file";
439 let dir = "target_dir";
440 let uid = geteuid().as_raw();
441
442 at.touch(file);
443 at.mkdir(dir);
444 let result = ucmd
445 .arg(file)
446 .arg("--owner")
447 .arg(uid.to_string())
448 .arg(format!("{dir}/{file}"))
449 .run();
450
451 if is_ci() && result.stderr_str().contains("no such user:") {
452 // In the CI, some server are failing to return the user id.
453 // As seems to be a configuration issue, ignoring it
454 return;
455 }
456
457 result.success();
458 assert!(at.file_exists(file));
459 assert!(at.file_exists(format!("{dir}/{file}")));
460 }
461
462 #[test]
463 fn test_install_target_new_file_failing_nonexistent_parent() {
464 let (at, mut ucmd) = at_and_ucmd!();
465 let file1 = "source_file";
466 let file2 = "target_file";
467 let dir = "target_dir";
468
469 at.touch(file1);
470
471 ucmd.arg(file1)
472 .arg(format!("{dir}/{file2}"))
473 .fails()
474 .stderr_contains("No such file or directory");
475 }
476
477 #[test]
478 fn test_install_preserve_timestamps() {
479 let (at, mut ucmd) = at_and_ucmd!();
480 let file1 = "source_file";
481 let file2 = "target_file";
482 at.touch(file1);
483
484 ucmd.arg(file1).arg(file2).arg("-p").succeeds().no_stderr();
485
486 assert!(at.file_exists(file1));
487 assert!(at.file_exists(file2));
488
489 let file1_metadata = at.metadata(file1);
490 let file2_metadata = at.metadata(file2);
491
492 assert_eq!(
493 file1_metadata.accessed().ok(),
494 file2_metadata.accessed().ok()
495 );
496 assert_eq!(
497 file1_metadata.modified().ok(),
498 file2_metadata.modified().ok()
499 );
500 }
501
502 #[test]
503 #[cfg(not(target_os = "openbsd"))]
504 fn test_install_compare_preserve_timestamps() {
505 use std::time::Duration;
506
507 let (at, mut ucmd) = at_and_ucmd!();
508 let source = "source_file";
509 let dest = "dest_file";
510
511 // Same contents, but destination has a newer timestamp than the source.
512 at.write(source, "data");
513 at.write(dest, "data");
514 let old = std::time::SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
515 fs::OpenOptions::new()
516 .write(true)
517 .open(at.plus(source))
518 .unwrap()
519 .set_modified(old)
520 .unwrap();
521
522 // With --preserve-timestamps, the timestamp difference forces the copy so
523 // the destination ends up with the source's modification time.
524 ucmd.args(&["-C", "--preserve-timestamps", source, dest])
525 .succeeds()
526 .no_output();
527
528 assert_eq!(
529 at.metadata(source).modified().ok(),
530 at.metadata(dest).modified().ok()
531 );
532 }
533
534 // These two tests are failing but should work
535 #[test]
536 fn test_install_copy_file() {
537 let (at, mut ucmd) = at_and_ucmd!();
538 let file1 = "source_file";
539 let file2 = "target_file";
540
541 at.touch(file1);
542 ucmd.arg(file1).arg(file2).succeeds().no_stderr();
543
544 assert!(at.file_exists(file1));
545 assert!(at.file_exists(file2));
546 }
547
548 #[test]
549 #[cfg(any(target_os = "linux", target_os = "android"))]
550 fn test_install_target_file_dev_null() {
551 let (at, mut ucmd) = at_and_ucmd!();
552
553 let file1 = "/dev/null";
554 let file2 = "target_file";
555
556 ucmd.arg(file1).arg(file2).succeeds();
557
558 assert!(at.file_exists(file2));
559 }
560
561 #[test]
562 fn test_install_nested_paths_copy_file() {
563 let (at, mut ucmd) = at_and_ucmd!();
564 let file1 = "source_file";
565 let dir1 = "source_dir";
566 let dir2 = "target_dir";
567
568 at.mkdir(dir1);
569 at.mkdir(dir2);
570 at.touch(format!("{dir1}/{file1}"));
571
572 ucmd.arg(format!("{dir1}/{file1}"))
573 .arg(dir2)
574 .succeeds()
575 .no_stderr();
576 assert!(at.file_exists(format!("{dir2}/{file1}")));
577 }
578
579 #[test]
580 fn test_multiple_mode_arguments_override_not_error() {
581 let scene = TestScenario::new(util_name!());
582 let at = &scene.fixtures;
583 let dir = "source_dir";
584
585 let file = "source_file";
586 let gid = getegid().as_raw();
587 let uid = geteuid().as_raw();
588
589 at.touch(file);
590 at.mkdir(dir);
591
592 scene
593 .ucmd()
594 .args(&[
595 file,
596 &format!("{dir}/{file}"),
597 "--owner=invalid_owner",
598 "--owner",
599 &uid.to_string(),
600 ])
601 .succeeds()
602 .no_stderr();
603
604 scene
605 .ucmd()
606 .args(&[
607 file,
608 &format!("{dir}/{file}"),
609 "-o invalid_owner",
610 "-o",
611 &uid.to_string(),
612 ])
613 .succeeds()
614 .no_stderr();
615
616 scene
617 .ucmd()
618 .args(&[file, &format!("{dir}/{file}"), "--mode=999", "--mode=200"])
619 .succeeds()
620 .no_stderr();
621
622 scene
623 .ucmd()
624 .args(&[file, &format!("{dir}/{file}"), "-m 999", "-m 200"])
625 .succeeds()
626 .no_stderr();
627
628 scene
629 .ucmd()
630 .args(&[
631 file,
632 &format!("{dir}/{file}"),
633 "--group=invalid_group",
634 "--group",
635 &gid.to_string(),
636 ])
637 .succeeds()
638 .no_stderr();
639
640 scene
641 .ucmd()
642 .args(&[
643 file,
644 &format!("{dir}/{file}"),
645 "-g invalid_group",
646 "-g",
647 &gid.to_string(),
648 ])
649 .succeeds()
650 .no_stderr();
651 }
652
653 #[test]
654 fn test_install_failing_omitting_directory() {
655 let scene = TestScenario::new(util_name!());
656 let at = &scene.fixtures;
657 let file1 = "file1";
658 let dir1 = "dir1";
659 let no_dir2 = "no-dir2";
660 let dir3 = "dir3";
661
662 at.mkdir(dir1);
663 at.mkdir(dir3);
664 at.touch(file1);
665
666 // GNU install checks for existing target dir first before checking on source params
667 scene
668 .ucmd()
669 .arg(file1)
670 .arg(dir1)
671 .arg(no_dir2)
672 .fails()
673 .stderr_contains("is not a directory");
674
675 // file1 will be copied before install fails on dir1
676 scene
677 .ucmd()
678 .arg(file1)
679 .arg(dir1)
680 .arg(dir3)
681 .fails_with_code(1)
682 .stderr_contains("omitting directory");
683 assert!(at.file_exists(format!("{dir3}/{file1}")));
684
685 // install also fails, when only one source param is given
686 scene
687 .ucmd()
688 .arg(dir1)
689 .arg(dir3)
690 .fails_with_code(1)
691 .stderr_contains("omitting directory");
692 }
693
694 #[test]
695 fn test_install_failing_no_such_file() {
696 let (at, mut ucmd) = at_and_ucmd!();
697 let file1 = "source_file";
698 let file2 = "inexistent_file";
699 let dir1 = "target_dir";
700
701 at.mkdir(dir1);
702 at.touch(file1);
703
704 ucmd.arg(file1)
705 .arg(file2)
706 .arg(dir1)
707 .fails_with_code(1)
708 .stderr_contains("No such file or directory");
709 }
710
711 #[test]
712 #[cfg(not(target_os = "openbsd"))]
713 fn test_install_copy_then_compare_file() {
714 let scene = TestScenario::new(util_name!());
715 let at = &scene.fixtures;
716 let file1 = "test_install_copy_then_compare_file_a1";
717 let file2 = "test_install_copy_then_compare_file_a2";
718
719 at.touch(file1);
720 scene
721 .ucmd()
722 .arg("-C")
723 .arg(file1)
724 .arg(file2)
725 .succeeds()
726 .no_stderr();
727
728 let mut file2_meta = at.metadata(file2);
729 let before = file2_meta.modified().unwrap();
730
731 scene
732 .ucmd()
733 .arg("-C")
734 .arg(file1)
735 .arg(file2)
736 .succeeds()
737 .no_stderr();
738
739 file2_meta = at.metadata(file2);
740 let after = file2_meta.modified().unwrap();
741
742 assert_eq!(before, after);
743 }
744
745 #[test]
746 #[cfg(any(target_os = "linux", target_os = "android"))]
747 fn test_install_copy_then_compare_file_with_extra_mode() {
748 let scene = TestScenario::new(util_name!());
749 let at = &scene.fixtures;
750 // XXX: can't tests introspect on their own names?
751 let file1 = "test_install_copy_then_compare_file_with_extra_mode_a1";
752 let file2 = "test_install_copy_then_compare_file_with_extra_mode_a2";
753
754 at.touch(file1);
755 scene
756 .ucmd()
757 .arg("-C")
758 .arg(file1)
759 .arg(file2)
760 .succeeds()
761 .no_stderr();
762
763 let mut file2_meta = at.metadata(file2);
764 let before = file2_meta.modified().unwrap();
765 sleep(std::time::Duration::from_millis(100));
766
767 scene
768 .ucmd()
769 .arg("-C")
770 .arg(file1)
771 .arg(file2)
772 .arg("-m")
773 .arg("1644")
774 .succeeds()
775 .stderr_contains(
776 "the --compare (-C) option is ignored when you specify a mode with non-permission bits",
777 );
778
779 file2_meta = at.metadata(file2);
780 let after_install_sticky = file2_meta.modified().unwrap();
781
782 assert_ne!(before, after_install_sticky);
783
784 sleep(std::time::Duration::from_millis(100));
785
786 // dest file still 1644, so need_copy ought to return `true`
787 scene
788 .ucmd()
789 .arg("-C")
790 .arg(file1)
791 .arg(file2)
792 .succeeds()
793 .no_stderr();
794
795 file2_meta = at.metadata(file2);
796 let after_install_sticky_again = file2_meta.modified().unwrap();
797
798 assert_ne!(after_install_sticky, after_install_sticky_again);
799 }
800
801 const STRIP_TARGET_FILE: &str = "helloworld_installed";
802 const STRIP_PROGRAM: &str = "#!/bin/sh\n: > \"$1\"\n";
803
804 #[test]
805 fn test_install_and_strip() {
806 let scene = TestScenario::new(util_name!());
807 let at = &scene.fixtures;
808
809 // Write the strip script and sync to disk to avoid ETXTBSY race on some
810 // platforms (observed on ARM64 Linux CI runners).
811 let strip_path = at.plus("strip");
812 {
813 use std::io::Write;
814 let mut f = fs::File::create(&strip_path).unwrap();
815 f.write_all(STRIP_PROGRAM.as_bytes()).unwrap();
816 f.sync_all().unwrap();
817 }
818 at.set_mode("strip", 0o755);
819 at.write("source", "file contents");
820 let path = format!(
821 "{}:{}",
822 at.plus(".").display(),
823 std::env::var("PATH").unwrap_or_default()
824 );
825
826 scene
827 .ucmd()
828 .env("PATH", path)
829 .arg("-s")
830 .arg("source")
831 .arg(STRIP_TARGET_FILE)
832 .succeeds()
833 .no_stderr();
834
835 assert_eq!(at.metadata(STRIP_TARGET_FILE).len(), 0);
836 }
837
838 #[test]
839 fn test_install_no_strip_with_program() {
840 let source = current_exe().unwrap();
841 TestScenario::new(util_name!())
842 .ucmd()
843 .arg("--strip-program")
844 .arg("false")
845 .arg(source)
846 .arg(STRIP_TARGET_FILE)
847 .succeeds()
848 .stderr_only(
849 "install: WARNING: ignoring --strip-program option as -s option was not specified\n",
850 );
851 }
852
853 #[test]
854 fn test_install_and_strip_with_program() {
855 let scene = TestScenario::new(util_name!());
856 let at = &scene.fixtures;
857
858 // Write the strip script and sync to disk to avoid ETXTBSY race on some
859 // platforms (observed on ARM64 Linux CI runners).
860 let strip_path = at.plus("strip-program");
861 {
862 use std::io::Write;
863 let mut f = fs::File::create(&strip_path).unwrap();
864 f.write_all(STRIP_PROGRAM.as_bytes()).unwrap();
865 f.sync_all().unwrap();
866 }
867 at.set_mode("strip-program", 0o755);
868 at.write("source", "file contents");
869
870 scene
871 .ucmd()
872 .arg("-s")
873 .arg("--strip-program")
874 .arg("./strip-program")
875 .arg("source")
876 .arg(STRIP_TARGET_FILE)
877 .succeeds()
878 .no_stderr();
879
880 assert_eq!(at.metadata(STRIP_TARGET_FILE).len(), 0);
881 }
882
883 #[cfg(all(unix, feature = "chmod"))]
884 #[test]
885 fn test_install_and_strip_with_program_hyphen() {
886 let scene = TestScenario::new(util_name!());
887
888 let at = &scene.fixtures;
889 let content = r#"#!/bin/sh
890 printf -- '%s\n' "$1" | grep '^[^-]'
891 "#;
892 at.write("no-hyphen", content);
893 scene.ccmd("chmod").arg("+x").arg("no-hyphen").succeeds();
894
895 at.touch("src");
896 scene
897 .ucmd()
898 .arg("-s")
899 .arg("--strip-program")
900 .arg("./no-hyphen")
901 .arg("--")
902 .arg("src")
903 .arg("-dest")
904 .succeeds()
905 .no_stderr()
906 .stdout_is("./-dest\n");
907
908 scene
909 .ucmd()
910 .arg("-s")
911 .arg("--strip-program")
912 .arg("./no-hyphen")
913 .arg("--")
914 .arg("src")
915 .arg("./-dest")
916 .succeeds()
917 .no_stderr()
918 .stdout_is("./-dest\n");
919 }
920
921 #[cfg(all(unix, feature = "chmod"))]
922 #[test]
923 fn test_install_on_invalid_link_at_destination() {
924 let scene = TestScenario::new(util_name!());
925
926 let at = &scene.fixtures;
927 at.mkdir("src");
928 at.mkdir("dest");
929 let src_dir = at.plus("src");
930 let dst_dir = at.plus("dest");
931
932 at.touch("test.sh");
933 at.symlink_file(
934 "/opt/FakeDestination",
935 &dst_dir.join("test.sh").to_string_lossy(),
936 );
937 scene.ccmd("chmod").arg("+x").arg("test.sh").succeeds();
938 at.symlink_file("test.sh", &src_dir.join("test.sh").to_string_lossy());
939
940 scene
941 .ucmd()
942 .current_dir(&src_dir)
943 .arg(src_dir.join("test.sh"))
944 .arg(dst_dir.join("test.sh"))
945 .succeeds()
946 .no_output();
947 }
948
949 #[cfg(all(unix, feature = "chmod"))]
950 #[test]
951 fn test_install_on_invalid_link_at_destination_and_dev_null_at_source() {
952 let scene = TestScenario::new(util_name!());
953
954 let at = &scene.fixtures;
955 at.mkdir("src");
956 at.mkdir("dest");
957 let src_dir = at.plus("src");
958 let dst_dir = at.plus("dest");
959
960 at.touch("test.sh");
961 at.symlink_file(
962 "/opt/FakeDestination",
963 &dst_dir.join("test.sh").to_string_lossy(),
964 );
965 scene.ccmd("chmod").arg("+x").arg("test.sh").succeeds();
966 at.symlink_file("test.sh", &src_dir.join("test.sh").to_string_lossy());
967
968 scene
969 .ucmd()
970 .current_dir(&src_dir)
971 .arg("/dev/null")
972 .arg(dst_dir.join("test.sh"))
973 .succeeds()
974 .no_output();
975 }
976
977 #[test]
978 fn test_install_and_strip_with_invalid_program() {
979 let scene = TestScenario::new(util_name!());
980 let at = &scene.fixtures;
981 let source = current_exe().unwrap();
982
983 scene
984 .ucmd()
985 .arg("-s")
986 .arg("--strip-program")
987 .arg("/bin/date")
988 .arg(source)
989 .arg(STRIP_TARGET_FILE)
990 .fails()
991 .stderr_contains("strip program failed");
992 assert!(!at.file_exists(STRIP_TARGET_FILE));
993 }
994
995 #[test]
996 fn test_install_and_strip_with_signal_terminated_program() {
997 let scene = TestScenario::new(util_name!());
998 let at = &scene.fixtures;
999 at.write("src.sh", "kill -9 $$\n");
1000 scene
1001 .ucmd()
1002 .args(&[
1003 "-s",
1004 "--strip-program",
1005 "/bin/sh",
1006 "src.sh",
1007 STRIP_TARGET_FILE,
1008 ])
1009 .fails()
1010 .stderr_only("install: strip process terminated abnormally\n");
1011 assert!(!at.file_exists(STRIP_TARGET_FILE));
1012 }
1013
1014 #[test]
1015 fn test_install_and_strip_with_non_existent_program() {
1016 let scene = TestScenario::new(util_name!());
1017 let at = &scene.fixtures;
1018 let source = current_exe().unwrap();
1019
1020 scene
1021 .ucmd()
1022 .arg("-s")
1023 .arg("--strip-program")
1024 .arg("/usr/bin/non_existent_program")
1025 .arg(source)
1026 .arg(STRIP_TARGET_FILE)
1027 .fails()
1028 .stderr_only("install: strip program failed: No such file or directory\n");
1029 assert!(!at.file_exists(STRIP_TARGET_FILE));
1030 }
1031
1032 #[test]
1033 fn test_install_creating_leading_dirs() {
1034 let scene = TestScenario::new(util_name!());
1035 let at = &scene.fixtures;
1036
1037 let source = "create_leading_test_file";
1038 let target = "dir1/dir2/dir3/test_file";
1039
1040 at.touch(source);
1041
1042 scene
1043 .ucmd()
1044 .arg("-D")
1045 .arg(source)
1046 .arg(at.plus(target))
1047 .succeeds()
1048 .no_stderr();
1049
1050 assert!(at.file_exists(target));
1051 }
1052
1053 #[test]
1054 fn test_install_creating_leading_dirs_verbose() {
1055 let scene = TestScenario::new(util_name!());
1056 let at = &scene.fixtures;
1057
1058 let source = "create_leading_test_file";
1059 let target = "dir1/no-dir2/no-dir3/test_file";
1060
1061 at.touch(source);
1062 at.mkdir("dir1");
1063
1064 let creating_dir1 = regex::Regex::new("(?m)^install: creating directory.*dir1'$").unwrap();
1065 let creating_nodir23 =
1066 regex::Regex::new(r"(?m)^install: creating directory.*no-dir[23]'$").unwrap();
1067
1068 scene
1069 .ucmd()
1070 .arg("-Dv")
1071 .arg(source)
1072 .arg(at.plus(target))
1073 .succeeds()
1074 .stdout_matches(&creating_nodir23)
1075 .stdout_does_not_match(&creating_dir1)
1076 .no_stderr();
1077
1078 assert!(at.file_exists(target));
1079 }
1080
1081 #[test]
1082 fn test_install_creating_leading_dirs_with_single_source_and_target_dir() {
1083 let scene = TestScenario::new(util_name!());
1084 let at = &scene.fixtures;
1085
1086 let source1 = "source_file_1";
1087 let target_dir = "missing_target_dir/";
1088
1089 at.touch(source1);
1090
1091 // installing a single file into a missing directory will fail, when -D is used w/o -t parameter
1092 scene
1093 .ucmd()
1094 .arg("-D")
1095 .arg(source1)
1096 .arg(at.plus(target_dir))
1097 .fails()
1098 .stderr_contains("missing_target_dir/' is not a directory");
1099
1100 assert!(!at.dir_exists(target_dir));
1101
1102 scene
1103 .ucmd()
1104 .arg("-D")
1105 .arg(source1)
1106 .arg("-t")
1107 .arg(at.plus(target_dir))
1108 .succeeds()
1109 .no_stderr();
1110
1111 assert!(at.file_exists(format!("{target_dir}/{source1}")));
1112 }
1113
1114 #[test]
1115 fn test_install_creating_leading_dirs_with_multiple_sources_and_target_dir() {
1116 let scene = TestScenario::new(util_name!());
1117 let at = &scene.fixtures;
1118
1119 let source1 = "source_file_1";
1120 let source2 = "source_file_2";
1121 let target_dir = "missing_target_dir";
1122
1123 at.touch(source1);
1124 at.touch(source2);
1125
1126 // installing multiple files into a missing directory will fail, when -D is used w/o -t parameter
1127 scene
1128 .ucmd()
1129 .arg("-D")
1130 .arg(source1)
1131 .arg(source2)
1132 .arg(at.plus(target_dir))
1133 .fails()
1134 .stderr_contains("missing_target_dir' is not a directory");
1135
1136 assert!(!at.dir_exists(target_dir));
1137
1138 scene
1139 .ucmd()
1140 .arg("-D")
1141 .arg(source1)
1142 .arg(source2)
1143 .arg("-t")
1144 .arg(at.plus(target_dir))
1145 .succeeds()
1146 .no_stderr();
1147
1148 assert!(at.dir_exists(target_dir));
1149 }
1150
1151 #[test]
1152 fn test_install_creating_leading_dir_fails_on_long_name() {
1153 let scene = TestScenario::new(util_name!());
1154 let at = &scene.fixtures;
1155
1156 let source = "create_leading_test_file";
1157 let target = format!("{}/test_file", "d".repeat(libc::PATH_MAX as usize + 1));
1158
1159 at.touch(source);
1160
1161 scene
1162 .ucmd()
1163 .arg("-D")
1164 .arg(source)
1165 .arg(at.plus(target.as_str()))
1166 .fails()
1167 .stderr_contains("cannot create directory");
1168 }
1169
1170 #[test]
1171 fn test_install_dir() {
1172 let (at, mut ucmd) = at_and_ucmd!();
1173 let dir = "target_dir";
1174 let file1 = "source_file1";
1175 let file2 = "source_file2";
1176
1177 at.touch(file1);
1178 at.touch(file2);
1179 at.mkdir(dir);
1180 ucmd.arg(file1)
1181 .arg(file2)
1182 .arg(format!("--target-directory={dir}"))
1183 .succeeds()
1184 .no_stderr();
1185
1186 assert!(at.file_exists(file1));
1187 assert!(at.file_exists(file2));
1188 assert!(at.file_exists(format!("{dir}/{file1}")));
1189 assert!(at.file_exists(format!("{dir}/{file2}")));
1190 }
1191 //
1192 // test backup functionality
1193 #[test]
1194 fn test_install_backup_short_no_args_files() {
1195 let scene = TestScenario::new(util_name!());
1196 let at = &scene.fixtures;
1197
1198 let file_a = "test_install_simple_backup_file_a";
1199 let file_b = "test_install_simple_backup_file_b";
1200
1201 at.touch(file_a);
1202 at.touch(file_b);
1203 scene
1204 .ucmd()
1205 .arg("-b")
1206 .arg(file_a)
1207 .arg(file_b)
1208 .succeeds()
1209 .no_stderr();
1210
1211 assert!(at.file_exists(file_a));
1212 assert!(at.file_exists(file_b));
1213 assert!(at.file_exists(format!("{file_b}~")));
1214 }
1215
1216 #[test]
1217 fn test_install_backup_short_no_args_file_to_dir() {
1218 let scene = TestScenario::new(util_name!());
1219 let at = &scene.fixtures;
1220
1221 let file = "test_install_simple_backup_file_a";
1222 let dest_dir = "test_install_dest/";
1223 let expect = format!("{dest_dir}{file}");
1224
1225 at.touch(file);
1226 at.mkdir(dest_dir);
1227 at.touch(&expect);
1228 scene
1229 .ucmd()
1230 .arg("-b")
1231 .arg(file)
1232 .arg(dest_dir)
1233 .succeeds()
1234 .no_stderr();
1235
1236 assert!(at.file_exists(file));
1237 assert!(at.file_exists(&expect));
1238 assert!(at.file_exists(format!("{expect}~")));
1239 }
1240
1241 // Long --backup option is tested separately as it requires a slightly different
1242 // handling than '-b' does.
1243 #[test]
1244 fn test_install_backup_long_no_args_files() {
1245 let scene = TestScenario::new(util_name!());
1246 let at = &scene.fixtures;
1247
1248 let file_a = "test_install_simple_backup_file_a";
1249 let file_b = "test_install_simple_backup_file_b";
1250
1251 at.touch(file_a);
1252 at.touch(file_b);
1253 scene
1254 .ucmd()
1255 .arg("--backup")
1256 .arg(file_a)
1257 .arg(file_b)
1258 .succeeds()
1259 .no_stderr();
1260
1261 assert!(at.file_exists(file_a));
1262 assert!(at.file_exists(file_b));
1263 assert!(at.file_exists(format!("{file_b}~")));
1264 }
1265
1266 #[test]
1267 fn test_install_backup_long_no_args_file_to_dir() {
1268 let scene = TestScenario::new(util_name!());
1269 let at = &scene.fixtures;
1270
1271 let file = "test_install_simple_backup_file_a";
1272 let dest_dir = "test_install_dest/";
1273 let expect = format!("{dest_dir}{file}");
1274
1275 at.touch(file);
1276 at.mkdir(dest_dir);
1277 at.touch(&expect);
1278 scene
1279 .ucmd()
1280 .arg("--backup")
1281 .arg(file)
1282 .arg(dest_dir)
1283 .succeeds()
1284 .no_stderr();
1285
1286 assert!(at.file_exists(file));
1287 assert!(at.file_exists(&expect));
1288 assert!(at.file_exists(format!("{expect}~")));
1289 }
1290
1291 #[test]
1292 fn test_install_backup_short_custom_suffix() {
1293 let scene = TestScenario::new(util_name!());
1294 let at = &scene.fixtures;
1295
1296 let file_a = "test_install_backup_custom_suffix_file_a";
1297 let file_b = "test_install_backup_custom_suffix_file_b";
1298 let suffix = "super-suffix-of-the-century";
1299
1300 at.touch(file_a);
1301 at.touch(file_b);
1302 scene
1303 .ucmd()
1304 .arg("-b")
1305 .arg(format!("--suffix={suffix}"))
1306 .arg(file_a)
1307 .arg(file_b)
1308 .succeeds()
1309 .no_stderr();
1310
1311 assert!(at.file_exists(file_a));
1312 assert!(at.file_exists(file_b));
1313 assert!(at.file_exists(format!("{file_b}{suffix}")));
1314 }
1315
1316 #[test]
1317 fn test_install_suffix_without_backup_option() {
1318 let scene = TestScenario::new(util_name!());
1319 let at = &scene.fixtures;
1320
1321 let file_a = "test_install_backup_custom_suffix_file_a";
1322 let file_b = "test_install_backup_custom_suffix_file_b";
1323 let suffix = "super-suffix-of-the-century";
1324
1325 at.touch(file_a);
1326 at.touch(file_b);
1327 scene
1328 .ucmd()
1329 .arg(format!("--suffix={suffix}"))
1330 .arg(file_a)
1331 .arg(file_b)
1332 .succeeds()
1333 .no_stderr();
1334
1335 assert!(at.file_exists(file_a));
1336 assert!(at.file_exists(file_b));
1337 assert!(at.file_exists(format!("{file_b}{suffix}")));
1338 }
1339
1340 #[test]
1341 fn test_install_backup_short_custom_suffix_hyphen_value() {
1342 let scene = TestScenario::new(util_name!());
1343 let at = &scene.fixtures;
1344
1345 let file_a = "test_install_backup_custom_suffix_file_a";
1346 let file_b = "test_install_backup_custom_suffix_file_b";
1347 let suffix = "-v";
1348
1349 at.touch(file_a);
1350 at.touch(file_b);
1351 scene
1352 .ucmd()
1353 .arg("-b")
1354 .arg(format!("--suffix={suffix}"))
1355 .arg(file_a)
1356 .arg(file_b)
1357 .succeeds()
1358 .no_stderr();
1359
1360 assert!(at.file_exists(file_a));
1361 assert!(at.file_exists(file_b));
1362 assert!(at.file_exists(format!("{file_b}{suffix}")));
1363 }
1364
1365 #[test]
1366 fn test_install_backup_custom_suffix_via_env() {
1367 let scene = TestScenario::new(util_name!());
1368 let at = &scene.fixtures;
1369
1370 let file_a = "test_install_backup_custom_suffix_file_a";
1371 let file_b = "test_install_backup_custom_suffix_file_b";
1372 let suffix = "super-suffix-of-the-century";
1373
1374 at.touch(file_a);
1375 at.touch(file_b);
1376 scene
1377 .ucmd()
1378 .arg("-b")
1379 .env("SIMPLE_BACKUP_SUFFIX", suffix)
1380 .arg(file_a)
1381 .arg(file_b)
1382 .succeeds()
1383 .no_stderr();
1384
1385 assert!(at.file_exists(file_a));
1386 assert!(at.file_exists(file_b));
1387 assert!(at.file_exists(format!("{file_b}{suffix}")));
1388 }
1389
1390 #[test]
1391 fn test_install_backup_error_includes_cause() {
1392 let (at, mut ucmd) = at_and_ucmd!();
1393 at.touch("source");
1394 at.touch("target");
1395 at.mkdir("target.backup");
1396 let error = strip_errno(&fs::rename(at.plus("target"), at.plus("target.backup")).unwrap_err());
1397
1398 ucmd.env("SIMPLE_BACKUP_SUFFIX", ".backup")
1399 .args(&["--backup", "source", "target"])
1400 .fails()
1401 .stderr_is(format!("install: cannot backup 'target': {error}\n"));
1402 }
1403
1404 #[test]
1405 fn test_install_backup_numbered_with_t() {
1406 let scene = TestScenario::new(util_name!());
1407 let at = &scene.fixtures;
1408
1409 let file_a = "test_install_backup_numbering_file_a";
1410 let file_b = "test_install_backup_numbering_file_b";
1411
1412 at.touch(file_a);
1413 at.touch(file_b);
1414 scene
1415 .ucmd()
1416 .arg("--backup=t")
1417 .arg(file_a)
1418 .arg(file_b)
1419 .succeeds()
1420 .no_stderr();
1421
1422 assert!(at.file_exists(file_a));
1423 assert!(at.file_exists(file_b));
1424 assert!(at.file_exists(format!("{file_b}.~1~")));
1425 }
1426
1427 #[test]
1428 fn test_install_backup_numbered_with_numbered() {
1429 let scene = TestScenario::new(util_name!());
1430 let at = &scene.fixtures;
1431
1432 let file_a = "test_install_backup_numbering_file_a";
1433 let file_b = "test_install_backup_numbering_file_b";
1434
1435 at.touch(file_a);
1436 at.touch(file_b);
1437 scene
1438 .ucmd()
1439 .arg("--backup=numbered")
1440 .arg(file_a)
1441 .arg(file_b)
1442 .succeeds()
1443 .no_stderr();
1444
1445 assert!(at.file_exists(file_a));
1446 assert!(at.file_exists(file_b));
1447 assert!(at.file_exists(format!("{file_b}.~1~")));
1448 }
1449
1450 #[test]
1451 fn test_install_backup_existing() {
1452 let scene = TestScenario::new(util_name!());
1453 let at = &scene.fixtures;
1454
1455 let file_a = "test_install_backup_numbering_file_a";
1456 let file_b = "test_install_backup_numbering_file_b";
1457
1458 at.touch(file_a);
1459 at.touch(file_b);
1460 scene
1461 .ucmd()
1462 .arg("--backup=existing")
1463 .arg(file_a)
1464 .arg(file_b)
1465 .succeeds()
1466 .no_stderr();
1467
1468 assert!(at.file_exists(file_a));
1469 assert!(at.file_exists(file_b));
1470 assert!(at.file_exists(format!("{file_b}~")));
1471 }
1472
1473 #[test]
1474 fn test_install_backup_nil() {
1475 let scene = TestScenario::new(util_name!());
1476 let at = &scene.fixtures;
1477
1478 let file_a = "test_install_backup_numbering_file_a";
1479 let file_b = "test_install_backup_numbering_file_b";
1480
1481 at.touch(file_a);
1482 at.touch(file_b);
1483 scene
1484 .ucmd()
1485 .arg("--backup=nil")
1486 .arg(file_a)
1487 .arg(file_b)
1488 .succeeds()
1489 .no_stderr();
1490
1491 assert!(at.file_exists(file_a));
1492 assert!(at.file_exists(file_b));
1493 assert!(at.file_exists(format!("{file_b}~")));
1494 }
1495
1496 #[test]
1497 fn test_install_backup_numbered_if_existing_backup_existing() {
1498 let scene = TestScenario::new(util_name!());
1499 let at = &scene.fixtures;
1500
1501 let file_a = "test_install_backup_numbering_file_a";
1502 let file_b = "test_install_backup_numbering_file_b";
1503 let file_b_backup = "test_install_backup_numbering_file_b.~1~";
1504
1505 at.touch(file_a);
1506 at.touch(file_b);
1507 at.touch(file_b_backup);
1508 scene
1509 .ucmd()
1510 .arg("--backup=existing")
1511 .arg(file_a)
1512 .arg(file_b)
1513 .succeeds()
1514 .no_stderr();
1515
1516 assert!(at.file_exists(file_a));
1517 assert!(at.file_exists(file_b));
1518 assert!(at.file_exists(file_b_backup));
1519 assert!(at.file_exists(format!("{file_b}.~2~")));
1520 }
1521
1522 #[test]
1523 fn test_install_backup_numbered_if_existing_backup_nil() {
1524 let scene = TestScenario::new(util_name!());
1525 let at = &scene.fixtures;
1526
1527 let file_a = "test_install_backup_numbering_file_a";
1528 let file_b = "test_install_backup_numbering_file_b";
1529 let file_b_backup = "test_install_backup_numbering_file_b.~1~";
1530
1531 at.touch(file_a);
1532 at.touch(file_b);
1533 at.touch(file_b_backup);
1534 scene
1535 .ucmd()
1536 .arg("--backup=nil")
1537 .arg(file_a)
1538 .arg(file_b)
1539 .succeeds()
1540 .no_stderr();
1541
1542 assert!(at.file_exists(file_a));
1543 assert!(at.file_exists(file_b));
1544 assert!(at.file_exists(file_b_backup));
1545 assert!(at.file_exists(format!("{file_b}.~2~")));
1546 }
1547
1548 #[test]
1549 fn test_install_backup_simple() {
1550 let scene = TestScenario::new(util_name!());
1551 let at = &scene.fixtures;
1552
1553 let file_a = "test_install_backup_numbering_file_a";
1554 let file_b = "test_install_backup_numbering_file_b";
1555
1556 at.touch(file_a);
1557 at.touch(file_b);
1558 scene
1559 .ucmd()
1560 .arg("--backup=simple")
1561 .arg(file_a)
1562 .arg(file_b)
1563 .succeeds()
1564 .no_stderr();
1565
1566 assert!(at.file_exists(file_a));
1567 assert!(at.file_exists(file_b));
1568 assert!(at.file_exists(format!("{file_b}~")));
1569 }
1570
1571 #[test]
1572 fn test_install_backup_never() {
1573 let scene = TestScenario::new(util_name!());
1574 let at = &scene.fixtures;
1575
1576 let file_a = "test_install_backup_numbering_file_a";
1577 let file_b = "test_install_backup_numbering_file_b";
1578
1579 at.touch(file_a);
1580 at.touch(file_b);
1581 scene
1582 .ucmd()
1583 .arg("--backup=never")
1584 .arg(file_a)
1585 .arg(file_b)
1586 .succeeds()
1587 .no_stderr();
1588
1589 assert!(at.file_exists(file_a));
1590 assert!(at.file_exists(file_b));
1591 assert!(at.file_exists(format!("{file_b}~")));
1592 }
1593
1594 #[test]
1595 fn test_install_backup_none() {
1596 let scene = TestScenario::new(util_name!());
1597 let at = &scene.fixtures;
1598
1599 let file_a = "test_install_backup_numbering_file_a";
1600 let file_b = "test_install_backup_numbering_file_b";
1601
1602 at.touch(file_a);
1603 at.touch(file_b);
1604 scene
1605 .ucmd()
1606 .arg("--backup=none")
1607 .arg(file_a)
1608 .arg(file_b)
1609 .succeeds()
1610 .no_stderr();
1611
1612 assert!(at.file_exists(file_a));
1613 assert!(at.file_exists(file_b));
1614 assert!(!at.file_exists(format!("{file_b}~")));
1615 }
1616
1617 #[test]
1618 fn test_install_backup_off() {
1619 let scene = TestScenario::new(util_name!());
1620 let at = &scene.fixtures;
1621
1622 let file_a = "test_install_backup_numbering_file_a";
1623 let file_b = "test_install_backup_numbering_file_b";
1624
1625 at.touch(file_a);
1626 at.touch(file_b);
1627 scene
1628 .ucmd()
1629 .arg("--backup=off")
1630 .arg(file_a)
1631 .arg(file_b)
1632 .succeeds()
1633 .no_stderr();
1634
1635 assert!(at.file_exists(file_a));
1636 assert!(at.file_exists(file_b));
1637 assert!(!at.file_exists(format!("{file_b}~")));
1638 }
1639
1640 #[test]
1641 fn test_install_missing_arguments() {
1642 let scene = TestScenario::new(util_name!());
1643 let at = &scene.fixtures;
1644
1645 let no_target_dir = "no-target_dir";
1646
1647 scene
1648 .ucmd()
1649 .fails_with_code(1)
1650 .usage_error("missing file operand");
1651
1652 scene
1653 .ucmd()
1654 .arg("-D")
1655 .arg(format!("-t {no_target_dir}"))
1656 .fails()
1657 .usage_error("missing file operand");
1658 assert!(!at.dir_exists(no_target_dir));
1659 }
1660
1661 #[test]
1662 fn test_install_missing_destination() {
1663 let scene = TestScenario::new(util_name!());
1664 let at = &scene.fixtures;
1665
1666 let file_1 = "source_file1";
1667 let dir_1 = "source_dir1";
1668
1669 at.touch(file_1);
1670 at.mkdir(dir_1);
1671
1672 // will fail and also print some info on correct usage
1673 scene
1674 .ucmd()
1675 .arg(file_1)
1676 .fails()
1677 .usage_error(format!("missing destination file operand after '{file_1}'"));
1678
1679 // GNU's install will check for correct num of arguments and then fail
1680 // and it does not recognize, that the source is not a file but a directory.
1681 scene
1682 .ucmd()
1683 .arg(dir_1)
1684 .fails()
1685 .usage_error(format!("missing destination file operand after '{dir_1}'"));
1686 }
1687
1688 #[test]
1689 fn test_install_dir_dot() {
1690 // To match tests/install/d-slashdot.sh
1691 let scene = TestScenario::new(util_name!());
1692
1693 scene.ucmd().arg("-d").arg("dir1/.").succeeds();
1694 scene.ucmd().arg("-d").arg("dir2/..").succeeds();
1695 // Tests that we don't have dir3/. in the output
1696 // but only 'dir3'
1697 scene
1698 .ucmd()
1699 .arg("-d")
1700 .arg("dir3/.")
1701 .arg("-v")
1702 .succeeds()
1703 .stdout_contains("creating directory 'dir3'");
1704 scene
1705 .ucmd()
1706 .arg("-d")
1707 .arg("dir4/./cal")
1708 .arg("-v")
1709 .succeeds()
1710 .stdout_contains("creating directory 'dir4/./cal'");
1711 scene
1712 .ucmd()
1713 .arg("-d")
1714 .arg("dir5/./cali/.")
1715 .arg("-v")
1716 .succeeds()
1717 .stdout_contains("creating directory 'dir5/cali'");
1718 scene
1719 .ucmd()
1720 .arg("-d")
1721 .arg("dir6/./")
1722 .arg("-v")
1723 .succeeds()
1724 .stdout_contains("creating directory 'dir6'");
1725
1726 let at = &scene.fixtures;
1727
1728 assert!(at.dir_exists("dir1"));
1729 assert!(at.dir_exists("dir2"));
1730 assert!(at.dir_exists("dir3"));
1731 assert!(at.dir_exists("dir4/cal"));
1732 assert!(at.dir_exists("dir5/cali"));
1733 assert!(at.dir_exists("dir6"));
1734 }
1735
1736 #[test]
1737 fn test_install_dir_with_existing_file() {
1738 let scene = TestScenario::new(util_name!());
1739 let at = &scene.fixtures;
1740
1741 let newdir1 = "newdir1";
1742 let existing_file = "existing_file";
1743 let newdir2 = "newdir2";
1744 at.touch(existing_file);
1745
1746 scene
1747 .ucmd()
1748 .arg("-d")
1749 .arg(newdir1)
1750 .arg(existing_file)
1751 .arg(newdir2)
1752 .fails()
1753 .stderr_contains("cannot create directory 'existing_file': File exists");
1754
1755 assert!(at.dir_exists(newdir1));
1756 assert!(!at.dir_exists(existing_file));
1757 assert!(at.dir_exists(newdir2));
1758 }
1759
1760 #[test]
1761 fn test_install_dir_with_multiple_existing_files() {
1762 let scene = TestScenario::new(util_name!());
1763 let at = &scene.fixtures;
1764 let file1 = "file1";
1765 let file2 = "file2";
1766
1767 at.touch(file1);
1768 at.touch(file2);
1769
1770 scene
1771 .ucmd()
1772 .arg("-d")
1773 .arg(file1)
1774 .arg(file2)
1775 .fails()
1776 .stderr_contains("cannot create directory 'file1': File exists")
1777 .stderr_contains("cannot create directory 'file2': File exists");
1778
1779 assert!(at.file_exists(file1));
1780 assert!(at.file_exists(file2));
1781 }
1782
1783 #[test]
1784 fn test_install_dir_req_verbose() {
1785 let scene = TestScenario::new(util_name!());
1786 let at = &scene.fixtures;
1787
1788 let file_1 = "source_file1";
1789 at.touch(file_1);
1790 scene
1791 .ucmd()
1792 .arg("-Dv")
1793 .arg(file_1)
1794 .arg("sub3/a/b/c/file")
1795 .succeeds()
1796 .stdout_contains("install: creating directory 'sub3'\ninstall: creating directory 'sub3/a'\ninstall: creating directory 'sub3/a/b'\ninstall: creating directory 'sub3/a/b/c'\n'source_file1' -> 'sub3/a/b/c/file'");
1797
1798 scene
1799 .ucmd()
1800 .arg("-t")
1801 .arg("sub4/a")
1802 .arg("-Dv")
1803 .arg(file_1)
1804 .succeeds()
1805 .stdout_contains("install: creating directory 'sub4'\ninstall: creating directory 'sub4/a'\n'source_file1' -> 'sub4/a/source_file1'");
1806
1807 at.mkdir("sub5");
1808 scene
1809 .ucmd()
1810 .arg("-Dv")
1811 .arg(file_1)
1812 .arg("sub5/a/b/c/file")
1813 .succeeds()
1814 .stdout_contains("install: creating directory 'sub5/a'\ninstall: creating directory 'sub5/a/b'\ninstall: creating directory 'sub5/a/b/c'\n'source_file1' -> 'sub5/a/b/c/file'");
1815 }
1816
1817 #[test]
1818 fn test_install_chown_file_invalid() {
1819 let scene = TestScenario::new(util_name!());
1820 let at = &scene.fixtures;
1821
1822 let file_1 = "source_file1";
1823 at.touch(file_1);
1824
1825 scene
1826 .ucmd()
1827 .arg("-o")
1828 .arg("test_invalid_user")
1829 .arg(file_1)
1830 .arg("target_file1")
1831 .fails()
1832 .stderr_contains("install: invalid user: 'test_invalid_user'");
1833
1834 scene
1835 .ucmd()
1836 .arg("-g")
1837 .arg("test_invalid_group")
1838 .arg(file_1)
1839 .arg("target_file1")
1840 .fails()
1841 .stderr_contains("install: invalid group: 'test_invalid_group'");
1842
1843 scene
1844 .ucmd()
1845 .arg("-o")
1846 .arg("test_invalid_user")
1847 .arg("-g")
1848 .arg("test_invalid_group")
1849 .arg(file_1)
1850 .arg("target_file1")
1851 .fails()
1852 .stderr_contains("install: invalid user: 'test_invalid_user'");
1853
1854 scene
1855 .ucmd()
1856 .arg("-g")
1857 .arg("test_invalid_group")
1858 .arg("-o")
1859 .arg("test_invalid_user")
1860 .arg(file_1)
1861 .arg("target_file1")
1862 .fails()
1863 .stderr_contains("install: invalid user: 'test_invalid_user'");
1864 }
1865
1866 #[test]
1867 fn test_install_chown_directory_invalid() {
1868 let scene = TestScenario::new(util_name!());
1869
1870 scene
1871 .ucmd()
1872 .arg("-o")
1873 .arg("test_invalid_user")
1874 .arg("-d")
1875 .arg("dir1/dir2")
1876 .fails()
1877 .stderr_contains("install: invalid user: 'test_invalid_user'");
1878
1879 scene
1880 .ucmd()
1881 .arg("-g")
1882 .arg("test_invalid_group")
1883 .arg("-d")
1884 .arg("dir1/dir2")
1885 .fails()
1886 .stderr_contains("install: invalid group: 'test_invalid_group'");
1887
1888 scene
1889 .ucmd()
1890 .arg("-o")
1891 .arg("test_invalid_user")
1892 .arg("-g")
1893 .arg("test_invalid_group")
1894 .arg("-d")
1895 .arg("dir1/dir2")
1896 .fails()
1897 .stderr_contains("install: invalid user: 'test_invalid_user'");
1898
1899 scene
1900 .ucmd()
1901 .arg("-g")
1902 .arg("test_invalid_group")
1903 .arg("-o")
1904 .arg("test_invalid_user")
1905 .arg("-d")
1906 .arg("dir1/dir2")
1907 .fails()
1908 .stderr_contains("install: invalid user: 'test_invalid_user'");
1909 }
1910
1911 #[test]
1912 #[cfg(not(target_os = "openbsd"))]
1913 fn test_install_compare_option() {
1914 let scene = TestScenario::new(util_name!());
1915 let at = &scene.fixtures;
1916 let first = "a";
1917 let second = "b";
1918 at.touch(first);
1919 scene
1920 .ucmd()
1921 .args(&["-Cv", first, second])
1922 .succeeds()
1923 .stdout_contains(format!("'{first}' -> '{second}'"));
1924 scene
1925 .ucmd()
1926 .args(&["-Cv", first, second])
1927 .succeeds()
1928 .no_stdout();
1929 scene
1930 .ucmd()
1931 .args(&["-Cv", "-m0644", first, second])
1932 .succeeds()
1933 .stdout_contains(format!("removed '{second}'\n'{first}' -> '{second}'"));
1934 scene
1935 .ucmd()
1936 .args(&["-Cv", first, second])
1937 .succeeds()
1938 .stdout_contains(format!("removed '{second}'\n'{first}' -> '{second}'"));
1939 // -C and --preserve-timestamps are no longer mutually exclusive
1940 scene
1941 .ucmd()
1942 .args(&["-C", "--preserve-timestamps", first, second])
1943 .succeeds()
1944 .no_output();
1945 scene
1946 .ucmd()
1947 .args(&["-C", "--strip", "--strip-program=echo", first, second])
1948 .fails_with_code(1)
1949 .stderr_contains("Options --compare and --strip are mutually exclusive");
1950 }
1951
1952 #[test]
1953 #[cfg(not(target_os = "openbsd"))]
1954 fn test_install_compare_basic() {
1955 let scene = TestScenario::new(util_name!());
1956 let at = &scene.fixtures;
1957
1958 let source = "source_file";
1959 let dest = "dest_file";
1960
1961 at.write(source, "test content");
1962
1963 // First install should copy
1964 scene
1965 .ucmd()
1966 .args(&["-Cv", "-m644", source, dest])
1967 .succeeds()
1968 .stdout_contains(format!("'{source}' -> '{dest}'"));
1969
1970 // Second install with same mode should be no-op (compare works)
1971 scene
1972 .ucmd()
1973 .args(&["-Cv", "-m644", source, dest])
1974 .succeeds()
1975 .no_stdout();
1976
1977 // Test that compare works correctly when content actually differs
1978 let source2 = "source2";
1979 at.write(source2, "different content");
1980
1981 scene
1982 .ucmd()
1983 .args(&["-Cv", "-m644", source2, dest])
1984 .succeeds()
1985 .stdout_contains("removed")
1986 .stdout_contains(format!("'{source2}' -> '{dest}'"));
1987
1988 // Second install should be no-op since content is now identical
1989 scene
1990 .ucmd()
1991 .args(&["-Cv", "-m644", source2, dest])
1992 .succeeds()
1993 .no_stdout();
1994 }
1995
1996 #[test]
1997 #[cfg(not(any(target_os = "openbsd", target_os = "freebsd")))]
1998 fn test_install_compare_special_mode_bits() {
1999 let scene = TestScenario::new(util_name!());
2000 let at = &scene.fixtures;
2001
2002 let source = "source_file";
2003 let dest = "dest_file";
2004
2005 at.write(source, "test content");
2006
2007 // Special mode bits - setgid (tests the core bug fix)
2008 // When setgid bit is set, -C should be ignored (always copy)
2009 // This tests the bug where b.specified_mode.unwrap_or(0) was used instead of b.mode()
2010 scene
2011 .ucmd()
2012 .args(&["-Cv", "-m2755", source, dest])
2013 .succeeds()
2014 .stdout_contains(format!("'{source}' -> '{dest}'"));
2015
2016 // Second install with same setgid mode should ALSO copy (not skip)
2017 // because -C option should be ignored when special mode bits are present
2018 scene
2019 .ucmd()
2020 .args(&["-Cv", "-m2755", source, dest])
2021 .succeeds()
2022 .stdout_contains("removed")
2023 .stdout_contains(format!("'{source}' -> '{dest}'"));
2024
2025 // Special mode bits - setuid
2026 scene
2027 .ucmd()
2028 .args(&["-Cv", "-m4755", source, dest])
2029 .succeeds()
2030 .stdout_contains("removed")
2031 .stdout_contains(format!("'{source}' -> '{dest}'"));
2032
2033 // Second install with setuid should also copy
2034 scene
2035 .ucmd()
2036 .args(&["-Cv", "-m4755", source, dest])
2037 .succeeds()
2038 .stdout_contains("removed")
2039 .stdout_contains(format!("'{source}' -> '{dest}'"));
2040
2041 // Special mode bits - sticky bit
2042 scene
2043 .ucmd()
2044 .args(&["-Cv", "-m1755", source, dest])
2045 .succeeds()
2046 .stdout_contains("removed")
2047 .stdout_contains(format!("'{source}' -> '{dest}'"));
2048
2049 // Second install with sticky bit should also copy
2050 scene
2051 .ucmd()
2052 .args(&["-Cv", "-m1755", source, dest])
2053 .succeeds()
2054 .stdout_contains("removed")
2055 .stdout_contains(format!("'{source}' -> '{dest}'"));
2056
2057 // Back to normal mode - compare should work again
2058 scene
2059 .ucmd()
2060 .args(&["-Cv", "-m644", source, dest])
2061 .succeeds()
2062 .stdout_contains("removed")
2063 .stdout_contains(format!("'{source}' -> '{dest}'"));
2064
2065 // Second install with normal mode should be no-op
2066 scene
2067 .ucmd()
2068 .args(&["-Cv", "-m644", source, dest])
2069 .succeeds()
2070 .no_stdout();
2071 }
2072
2073 #[test]
2074 #[cfg(not(target_os = "openbsd"))]
2075 fn test_install_compare_group_ownership() {
2076 let scene = TestScenario::new(util_name!());
2077 let at = &scene.fixtures;
2078
2079 let source = "source_file";
2080 let dest = "dest_file";
2081
2082 at.write(source, "test content");
2083
2084 let user_group = std::process::Command::new("id")
2085 .arg("-nrg")
2086 .output()
2087 .map_or_else(
2088 |_| "users".to_string(),
2089 |output| String::from_utf8_lossy(&output.stdout).trim().to_string(),
2090 ); // fallback group name
2091
2092 // Install with explicit group
2093 scene
2094 .ucmd()
2095 .args(&["-Cv", "-m664", "-g", &user_group, source, dest])
2096 .succeeds()
2097 .stdout_contains(format!("'{source}' -> '{dest}'"));
2098
2099 // Install without group - this should detect that no copy is needed
2100 // because the file already has the correct group (user's group)
2101 scene
2102 .ucmd()
2103 .args(&["-Cv", "-m664", source, dest])
2104 .succeeds()
2105 .no_stdout(); // Should be no-op if group ownership logic is correct
2106 }
2107
2108 #[test]
2109 #[cfg(not(target_os = "openbsd"))]
2110 fn test_install_compare_symlink_handling() {
2111 let scene = TestScenario::new(util_name!());
2112 let at = &scene.fixtures;
2113
2114 let source = "source_file";
2115 let symlink_dest = "symlink_dest";
2116 let target_file = "target_file";
2117
2118 at.write(source, "test content");
2119 at.write(target_file, "test content"); // Same content to test that symlinks are always replaced
2120 at.symlink_file(target_file, symlink_dest);
2121
2122 // Create a symlink as destination pointing to a different file - should always be replaced
2123 scene
2124 .ucmd()
2125 .args(&["-Cv", "-m644", source, symlink_dest])
2126 .succeeds()
2127 .stdout_contains("removed")
2128 .stdout_contains(format!("'{source}' -> '{symlink_dest}'"));
2129
2130 // Even if content would be the same, symlink destination should be replaced
2131 // Now symlink_dest is a regular file, so compare should work normally
2132 scene
2133 .ucmd()
2134 .args(&["-Cv", "-m644", source, symlink_dest])
2135 .succeeds()
2136 .no_stdout(); // Now it's a regular file, so compare should work
2137 }
2138
2139 #[test]
2140 // Matches part of tests/install/basic-1
2141 fn test_t_exist_dir() {
2142 let scene = TestScenario::new(util_name!());
2143 let at = &scene.fixtures;
2144
2145 let source1 = "file";
2146 let target_dir = "sub4/";
2147 let target_file = "sub4/file_exists";
2148
2149 at.touch(source1);
2150 at.mkdir(target_dir);
2151 at.touch(target_file);
2152
2153 scene
2154 .ucmd()
2155 .arg("-t")
2156 .arg(target_file)
2157 .arg("-Dv")
2158 .arg(source1)
2159 .fails()
2160 .stderr_contains("failed to access 'sub4/file_exists': Not a directory");
2161 }
2162
2163 #[test]
2164 fn test_target_file_ends_with_slash() {
2165 let scene = TestScenario::new(util_name!());
2166 let at = &scene.fixtures;
2167
2168 let source = "source_file";
2169 let target_dir = "dir";
2170 let target_file = "dir/target_file";
2171 let target_file_slash = format!("{target_file}/");
2172
2173 at.touch(source);
2174 at.mkdir(target_dir);
2175 at.touch(target_file);
2176
2177 scene
2178 .ucmd()
2179 .arg("-t")
2180 .arg(target_file_slash)
2181 .arg("-D")
2182 .arg(source)
2183 .fails()
2184 .stderr_contains("failed to access 'dir/target_file/': Not a directory");
2185 }
2186
2187 #[test]
2188 fn test_install_root_combined() {
2189 let ts = TestScenario::new(util_name!());
2190 let at = &ts.fixtures;
2191 at.touch("a");
2192 at.touch("c");
2193
2194 let run_and_check = |args: &[&str], target: &str, expected_uid: u32, expected_gid: u32| {
2195 if let Ok(result) = run_ucmd_as_root(&ts, args) {
2196 result.success();
2197 assert!(at.file_exists(target));
2198
2199 let metadata = fs::metadata(at.plus(target)).unwrap();
2200 assert_eq!(metadata.uid(), expected_uid);
2201 assert_eq!(metadata.gid(), expected_gid);
2202 } else {
2203 print!("Test skipped; requires root user");
2204 }
2205 };
2206
2207 run_and_check(&["-Cv", "-o1", "-g1", "a", "b"], "b", 1, 1);
2208 run_and_check(&["-Cv", "-o2", "-g1", "a", "b"], "b", 2, 1);
2209 run_and_check(&["-Cv", "-o2", "-g2", "a", "b"], "b", 2, 2);
2210
2211 run_and_check(&["-Cv", "-o2", "c", "d"], "d", 2, 0);
2212 run_and_check(&["-Cv", "c", "d"], "d", 0, 0);
2213 run_and_check(&["-Cv", "c", "d"], "d", 0, 0);
2214 }
2215
2216 #[test]
2217 #[cfg(unix)]
2218 fn test_install_from_fifo() {
2219 use std::fs::OpenOptions;
2220 use std::io::Write;
2221 use std::thread;
2222
2223 let pipe_name = "pipe";
2224 let target_name = "target";
2225 let test_string = "Hello, world!\n";
2226
2227 let s = TestScenario::new(util_name!());
2228 s.fixtures.mkfifo(pipe_name);
2229 assert!(s.fixtures.is_fifo(pipe_name));
2230
2231 let proc = s.ucmd().arg(pipe_name).arg(target_name).run_no_wait();
2232
2233 let pipe_path = s.fixtures.plus(pipe_name);
2234 let thread = thread::spawn(move || {
2235 let mut pipe = OpenOptions::new()
2236 .write(true)
2237 .create(false)
2238 .open(pipe_path)
2239 .unwrap();
2240 pipe.write_all(test_string.as_bytes()).unwrap();
2241 });
2242
2243 proc.wait().unwrap();
2244 thread.join().unwrap();
2245
2246 assert!(s.fixtures.file_exists(target_name));
2247 assert_eq!(s.fixtures.read(target_name), test_string);
2248 }
2249
2250 #[test]
2251 #[cfg(unix)]
2252 fn test_install_from_stdin() {
2253 let (at, mut ucmd) = at_and_ucmd!();
2254 let target = "target";
2255 let test_string = "Hello, World!\n";
2256
2257 ucmd.arg("/dev/fd/0")
2258 .arg(target)
2259 .pipe_in(test_string)
2260 .succeeds();
2261
2262 assert!(at.file_exists(target));
2263 assert_eq!(at.read(target), test_string);
2264 }
2265
2266 #[test]
2267 fn test_install_failing_copy_file_to_target_contain_subdir_with_same_name() {
2268 let (at, mut ucmd) = at_and_ucmd!();
2269 let file = "file";
2270 let dir1 = "dir1";
2271
2272 at.touch(file);
2273 at.mkdir_all(&format!("{dir1}/{file}"));
2274 ucmd.arg(file)
2275 .arg(dir1)
2276 .fails()
2277 .stderr_contains("cannot overwrite directory");
2278 }
2279
2280 #[test]
2281 fn test_install_same_file() {
2282 let (at, mut ucmd) = at_and_ucmd!();
2283 let file = "file";
2284
2285 at.touch(file);
2286 ucmd.arg(file)
2287 .arg(".")
2288 .fails()
2289 .stderr_contains("'file' and './file' are the same file");
2290 }
2291
2292 #[test]
2293 fn test_install_symlink_same_file() {
2294 let (at, mut ucmd) = at_and_ucmd!();
2295 let file = "file";
2296 let target_dir = "target_dir";
2297 let target_link = "target_link";
2298
2299 at.mkdir(target_dir);
2300 at.touch(format!("{target_dir}/{file}"));
2301 at.symlink_file(target_dir, target_link);
2302 ucmd.arg(format!("{target_dir}/{file}"))
2303 .arg(target_link)
2304 .fails()
2305 .stderr_contains(format!(
2306 "'{target_dir}/{file}' and '{target_link}/{file}' are the same file"
2307 ));
2308 }
2309
2310 #[test]
2311 fn test_install_no_target_directory_failing_cannot_overwrite() {
2312 let scene = TestScenario::new(util_name!());
2313 let at = &scene.fixtures;
2314 let file = "file";
2315 let dir = "dir";
2316
2317 at.touch(file);
2318 at.mkdir(dir);
2319 scene
2320 .ucmd()
2321 .arg("-T")
2322 .arg(file)
2323 .arg(dir)
2324 .fails()
2325 .stderr_contains("cannot overwrite directory 'dir' with non-directory");
2326
2327 assert!(!at.dir_exists("dir/file"));
2328 }
2329
2330 #[test]
2331 fn test_install_no_target_directory_overwrite_file() {
2332 let scene = TestScenario::new(util_name!());
2333 let at = &scene.fixtures;
2334 let file = "file";
2335 let dest = "dest";
2336
2337 at.touch(file);
2338 scene.ucmd().arg("-T").arg(file).arg(dest).succeeds();
2339 scene.ucmd().arg("-T").arg(file).arg(dest).succeeds();
2340
2341 assert!(!at.dir_exists("dir/file"));
2342 }
2343
2344 #[test]
2345 fn test_install_no_target_directory_failing_omitting_directory() {
2346 let scene = TestScenario::new(util_name!());
2347 let at = &scene.fixtures;
2348 let dir1 = "dir1";
2349 let dir2 = "dir2";
2350
2351 at.mkdir(dir1);
2352 at.mkdir(dir2);
2353 scene
2354 .ucmd()
2355 .arg("-T")
2356 .arg(dir1)
2357 .arg(dir2)
2358 .fails()
2359 .stderr_contains("omitting directory 'dir1'");
2360 }
2361
2362 #[test]
2363 fn test_install_no_target_directory_creating_leading_dirs_with_single_source_and_target_dir() {
2364 let scene = TestScenario::new(util_name!());
2365 let at = &scene.fixtures;
2366
2367 let source1 = "file";
2368 let target_dir = "missing_target_dir/";
2369
2370 at.touch(source1);
2371
2372 // installing a single file into a missing directory will fail, when -D is used w/o -t parameter
2373 scene
2374 .ucmd()
2375 .arg("-TD")
2376 .arg(source1)
2377 .arg(at.plus(target_dir))
2378 .fails()
2379 .stderr_contains("missing_target_dir/' is not a directory");
2380
2381 assert!(!at.dir_exists(target_dir));
2382 }
2383
2384 #[test]
2385 fn test_install_no_target_directory_failing_combine_with_target_directory() {
2386 let scene = TestScenario::new(util_name!());
2387 let at = &scene.fixtures;
2388 let file = "file";
2389 let dir1 = "dir1";
2390
2391 at.touch(file);
2392 at.mkdir(dir1);
2393 scene
2394 .ucmd()
2395 .arg("-T")
2396 .arg(file)
2397 .arg("-t")
2398 .arg(dir1)
2399 .fails()
2400 .stderr_contains(
2401 "Options --target-directory and --no-target-directory are mutually exclusive",
2402 );
2403 }
2404
2405 #[test]
2406 fn test_install_no_target_directory_failing_usage_with_target_directory() {
2407 let scene = TestScenario::new(util_name!());
2408 let at = &scene.fixtures;
2409 let file = "file";
2410
2411 at.touch(file);
2412 scene
2413 .ucmd()
2414 .arg("-T")
2415 .arg(file)
2416 .arg("-t")
2417 .fails()
2418 .stderr_contains(
2419 "a value is required for '--target-directory <DIRECTORY>' but none was supplied",
2420 )
2421 .stderr_contains("For more information, try '--help'");
2422 }
2423
2424 #[test]
2425 fn test_install_no_target_multiple_sources_and_target_dir() {
2426 let scene = TestScenario::new(util_name!());
2427 let at = &scene.fixtures;
2428
2429 let file1 = "file1";
2430 let file2 = "file2";
2431 let dir1 = "dir1";
2432 let dir2 = "dir2";
2433
2434 at.touch(file1);
2435 at.touch(file2);
2436 at.mkdir(dir1);
2437 at.mkdir(dir2);
2438
2439 // installing multiple files into a missing directory will fail, when -D is used w/o -t parameter
2440 scene
2441 .ucmd()
2442 .arg("-T")
2443 .arg(file1)
2444 .arg(file2)
2445 .arg(dir1)
2446 .fails()
2447 .stderr_contains("extra operand 'dir1'")
2448 .stderr_contains("[OPTION]... [FILE]...");
2449
2450 scene
2451 .ucmd()
2452 .arg("-T")
2453 .arg(file1)
2454 .arg(file2)
2455 .arg(dir1)
2456 .arg(dir2)
2457 .fails()
2458 .stderr_contains("extra operand 'dir1'")
2459 .stderr_contains("[OPTION]... [FILE]...");
2460 }
2461
2462 #[test]
2463 fn test_install_no_target_basic() {
2464 let (at, mut ucmd) = at_and_ucmd!();
2465 let file = "file";
2466 let dir = "dir";
2467
2468 at.touch(file);
2469 at.mkdir(dir);
2470 ucmd.arg("-T")
2471 .arg(file)
2472 .arg(format!("{dir}/{file}"))
2473 .succeeds()
2474 .no_stderr();
2475
2476 assert!(at.file_exists(file));
2477 assert!(at.file_exists(format!("{dir}/{file}")));
2478 }
2479
2480 #[test]
2481 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
2482 fn test_selinux() {
2483 let scene = TestScenario::new(util_name!());
2484 let at = &scene.fixtures;
2485 let src = "orig";
2486 at.touch(src);
2487
2488 let dest = "orig.2";
2489
2490 let args = ["-Z", "--context=unconfined_u:object_r:user_tmp_t:s0"];
2491 for arg in args {
2492 let result = new_ucmd!()
2493 .arg(arg)
2494 .arg("-v")
2495 .arg(at.plus_as_string(src))
2496 .arg(at.plus_as_string(dest))
2497 .run();
2498
2499 // Skip test if SELinux is not enabled
2500 if result
2501 .stderr_str()
2502 .contains("SELinux is not enabled on this system")
2503 {
2504 println!("Skipping SELinux test: SELinux is not enabled");
2505 at.remove(&at.plus_as_string(dest));
2506 continue;
2507 }
2508
2509 result.success().stdout_contains("orig' -> '");
2510
2511 // Try to get SELinux context, skip test if getfattr is not available
2512 let context_value =
2513 std::panic::catch_unwind(|| get_getfattr_output(&at.plus_as_string(dest)));
2514
2515 let Ok(context_value) = context_value else {
2516 println!("Skipping SELinux test: getfattr not available or failed");
2517 at.remove(&at.plus_as_string(dest));
2518 continue;
2519 };
2520
2521 assert!(
2522 context_value.contains("unconfined_u"),
2523 "Expected 'unconfined_u' not found in getfattr output:\n{context_value}"
2524 );
2525 at.remove(&at.plus_as_string(dest));
2526 }
2527 }
2528
2529 #[test]
2530 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
2531 fn test_selinux_invalid_args() {
2532 let scene = TestScenario::new(util_name!());
2533 let at = &scene.fixtures;
2534 let src = "orig";
2535 at.touch(src);
2536 let dest = "orig.2";
2537
2538 let args = [
2539 "--context=a",
2540 "--context=unconfined_u:object_r:user_tmp_t:s0:a",
2541 "--context=nconfined_u:object_r:user_tmp_t:s0",
2542 ];
2543 for arg in args {
2544 let result = new_ucmd!()
2545 .arg(arg)
2546 .arg("-v")
2547 .arg(at.plus_as_string(src))
2548 .arg(at.plus_as_string(dest))
2549 .fails();
2550
2551 let stderr = result.stderr_str();
2552 assert!(
2553 stderr.contains("failed to set default file creation")
2554 || stderr.contains("SELinux is not enabled on this system"),
2555 "Expected stderr to contain either 'failed to set default file creation' or 'SELinux is not enabled on this system', but got: '{stderr}'"
2556 );
2557
2558 at.remove(&at.plus_as_string(dest));
2559 }
2560 }
2561
2562 #[test]
2563 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
2564 fn test_selinux_default_context() {
2565 let scene = TestScenario::new(util_name!());
2566 let at = &scene.fixtures;
2567 let src = "orig";
2568 at.touch(src);
2569 let dest = "orig.2";
2570
2571 let result = new_ucmd!()
2572 .arg("-Z")
2573 .arg("-v")
2574 .arg(at.plus_as_string(src))
2575 .arg(at.plus_as_string(dest))
2576 .run();
2577
2578 // Skip test if SELinux is not enabled
2579 if result
2580 .stderr_str()
2581 .contains("SELinux is not enabled on this system")
2582 {
2583 println!("Skipping SELinux default context test: SELinux is not enabled");
2584 return;
2585 }
2586
2587 result.success().stdout_contains("orig' -> '");
2588 assert!(at.file_exists(dest));
2589 }
2590
2591 #[test]
2592 #[cfg(not(any(target_os = "openbsd", target_os = "freebsd")))]
2593 fn test_install_compare_with_mode_bits() {
2594 let test_cases = [
2595 ("4755", "setuid bit", true),
2596 ("2755", "setgid bit", true),
2597 ("1755", "sticky bit", true),
2598 ("7755", "setuid + setgid + sticky bits", true),
2599 ("755", "permission-only mode", false),
2600 ];
2601
2602 for (mode, description, should_warn) in test_cases {
2603 let scene = TestScenario::new(util_name!());
2604 let at = &scene.fixtures;
2605 let source = format!("source_file_{mode}");
2606 let dest = format!("dest_file_{mode}");
2607
2608 at.write(&source, "test content");
2609
2610 let mode_arg = format!("--mode={mode}");
2611
2612 if should_warn {
2613 scene.ucmd().args(&["-C", &mode_arg, &source, &dest])
2614 .succeeds()
2615 .stderr_contains("the --compare (-C) option is ignored when you specify a mode with non-permission bits");
2616 } else {
2617 scene
2618 .ucmd()
2619 .args(&["-C", &mode_arg, &source, &dest])
2620 .succeeds()
2621 .no_stderr();
2622
2623 // Test second install should be no-op due to -C
2624 scene
2625 .ucmd()
2626 .args(&["-C", &mode_arg, &source, &dest])
2627 .succeeds()
2628 .no_stderr();
2629 }
2630
2631 assert!(
2632 at.file_exists(&dest),
2633 "Failed to create dest file for {description}"
2634 );
2635 }
2636 }
2637
2638 #[test]
2639 #[cfg(target_os = "linux")]
2640 fn test_install_non_utf8_paths() {
2641 let (at, mut ucmd) = at_and_ucmd!();
2642 let source_filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
2643 let dest_dir = "target_dir";
2644
2645 fs::write(at.plus(&source_filename), b"test content").unwrap();
2646 at.mkdir(dest_dir);
2647
2648 ucmd.arg(&source_filename).arg(dest_dir).succeeds();
2649
2650 // Test with trailing slash and directory creation (-D flag)
2651 let (at, mut ucmd) = at_and_ucmd!();
2652 let source_file = "source.txt";
2653 let mut target_path = std::ffi::OsString::from_vec(vec![0xFF, 0xFE, b'd', b'i', b'r']);
2654 target_path.push("/target.txt");
2655
2656 at.touch(source_file);
2657
2658 ucmd.arg("-D").arg(source_file).arg(&target_path).succeeds();
2659 }
2660
2661 /// A failed ownership change must not leave the setuid/setgid mode applied.
2662 #[test]
2663 fn test_install_failed_chown_does_not_leave_setuid() {
2664 // Only meaningful when the chown can actually fail.
2665 if geteuid().is_root() {
2666 return;
2667 }
2668
2669 let scene = TestScenario::new(util_name!());
2670 let at = &scene.fixtures;
2671 at.touch("src");
2672
2673 scene
2674 .ucmd()
2675 .args(&["-m", "4755", "-o", "root", "src", "dst"])
2676 .fails();
2677
2678 if at.file_exists("dst") {
2679 let mode = at.metadata("dst").permissions().mode() & 0o7777;
2680 assert_eq!(
2681 mode & 0o6000,
2682 0,
2683 "install left setuid/setgid set ({mode:o}) after the chown failed"
2684 );
2685 }
2686
2687 // The same for a directory created with -d.
2688 scene
2689 .ucmd()
2690 .args(&["-d", "-m", "4755", "-o", "root", "newdir"])
2691 .fails();
2692
2693 if at.dir_exists("newdir") {
2694 let mode = at.metadata("newdir").permissions().mode() & 0o7777;
2695 assert_eq!(
2696 mode & 0o6000,
2697 0,
2698 "install -d left setuid/setgid set ({mode:o}) after the chown failed"
2699 );
2700 }
2701 }
2702
2703 /// The mode must still be applied in full when no ownership change is requested.
2704 #[test]
2705 fn test_install_setuid_mode_applied_without_chown() {
2706 let scene = TestScenario::new(util_name!());
2707 let at = &scene.fixtures;
2708 at.touch("src");
2709
2710 scene.ucmd().args(&["-m", "4755", "src", "dst"]).succeeds();
2711 assert_eq!(at.metadata("dst").permissions().mode() & 0o7777, 0o4755);
2712
2713 scene
2714 .ucmd()
2715 .args(&["-d", "-m", "2755", "newdir"])
2716 .succeeds();
2717 assert_eq!(at.metadata("newdir").permissions().mode() & 0o7777, 0o2755);
2718 }
2719
2720 #[test]
2721 fn test_install_unprivileged_option_u_skips_chown() {
2722 // This test only makes sense when not running as root.
2723 if geteuid().is_root() {
2724 return;
2725 }
2726
2727 let scene = TestScenario::new(util_name!());
2728 let at = &scene.fixtures;
2729
2730 let src = "source_file";
2731 let dst_fail = "target_fail";
2732 let dst_ok = "target_ok";
2733 at.touch(src);
2734
2735 // Without -U, attempting to chown to root should fail for an unprivileged user.
2736 let res = scene.ucmd().args(&["--owner=root", src, dst_fail]).run();
2737
2738 res.failure();
2739
2740 // With -U, install should not require elevated privileges for owner/group changes,
2741 // meaning it should succeed and leave ownership as the current user.
2742 scene
2743 .ucmd()
2744 .args(&["-U", "--owner=root", src, dst_ok])
2745 .succeeds()
2746 .no_stderr();
2747
2748 assert!(at.file_exists(dst_ok));
2749 assert_eq!(at.metadata(dst_ok).uid(), geteuid().as_raw());
2750 }
2751
2752 #[test]
2753 fn test_install_normal_file_replaces_symlink() {
2754 let scene = TestScenario::new(util_name!());
2755 let at = &scene.fixtures;
2756
2757 at.write("source", "new content");
2758 at.write("sensitive", "important data");
2759
2760 // Create symlink at destination
2761 at.symlink_file("sensitive", "dest");
2762
2763 // Install should replace symlink with normal file (not follow it)
2764 scene.ucmd().arg("source").arg("dest").succeeds();
2765
2766 // Verify dest is now a normal file, not a symlink
2767 assert!(at.file_exists("dest"));
2768 assert_eq!(at.read("dest"), "new content");
2769
2770 // Verify sensitive file was NOT modified
2771 assert_eq!(at.read("sensitive"), "important data");
2772 }
2773
2774 #[test]
2775 #[cfg(unix)]
2776 fn test_install_d_symlink_in_path_is_followed() {
2777 // Test that pre-existing symlinks in the path are followed (GNU coreutils behavior).
2778 // install -D should traverse symlink components rather than replacing them.
2779 use std::os::unix::fs::symlink;
2780
2781 let scene = TestScenario::new(util_name!());
2782 let at = &scene.fixtures;
2783
2784 at.mkdir("target");
2785 at.write("source_file", "test content");
2786
2787 at.mkdir_all("testdir/a");
2788 symlink(at.plus("target"), at.plus("testdir/a/b")).unwrap();
2789
2790 // install -D should follow the symlink and write into the symlink target
2791 scene
2792 .ucmd()
2793 .arg("-D")
2794 .arg(at.plus("source_file"))
2795 .arg(at.plus("testdir/a/b/c/file"))
2796 .succeeds();
2797
2798 // File must be written through the symlink, i.e. inside the real target dir
2799 assert!(
2800 at.plus("target/c/file").exists(),
2801 "File should be written through the symlink into the real target directory"
2802 );
2803 assert_eq!(
2804 fs::read_to_string(at.plus("target/c/file")).unwrap(),
2805 "test content"
2806 );
2807
2808 // The symlink must not have been replaced with a real directory
2809 assert!(
2810 at.plus("testdir/a/b").is_symlink(),
2811 "Intermediate symlink should be preserved, not replaced with a real directory"
2812 );
2813 }
2814
2815 #[test]
2816 #[cfg(unix)]
2817 fn test_install_d_follows_symlink_prefix() {
2818 // Regression test for: install -D replaces symlink components instead of following them.
2819 // Reproduces the exact scenario from the bug report: a symlinked install prefix
2820 // (common in BOSH, Homebrew, Nix, stow) must be followed, not destroyed.
2821 use std::os::unix::fs::symlink;
2822
2823 let scene = TestScenario::new(util_name!());
2824 let at = &scene.fixtures;
2825
2826 // Simulate: ln -s /tmp/target /tmp/link
2827 at.mkdir("target");
2828 symlink(at.plus("target"), at.plus("link")).unwrap();
2829
2830 at.write("file.txt", "hello");
2831
2832 // install -D -m 644 file.txt link/subdir/file.txt
2833 scene
2834 .ucmd()
2835 .args(&["-D", "-m", "644"])
2836 .arg(at.plus("file.txt"))
2837 .arg(at.plus("link/subdir/file.txt"))
2838 .succeeds();
2839
2840 // GNU expected: /tmp/link remains a symlink, file written to /tmp/target/subdir/file.txt
2841 assert!(
2842 at.plus("link").is_symlink(),
2843 "The symlinked prefix must remain a symlink"
2844 );
2845 assert!(
2846 at.plus("target/subdir/file.txt").exists(),
2847 "File must be written into the real target directory via the symlink"
2848 );
2849 assert_eq!(
2850 fs::read_to_string(at.plus("target/subdir/file.txt")).unwrap(),
2851 "hello"
2852 );
2853 }
2854
2855 #[test]
2856 #[cfg(unix)]
2857 fn test_install_d_dangling_symlink_in_path_errors() {
2858 // A dangling symlink as a path component must not be silently replaced with a
2859 // real directory. GNU coreutils errors out in this case; we should too.
2860 use std::os::unix::fs::symlink;
2861
2862 let scene = TestScenario::new(util_name!());
2863 let at = &scene.fixtures;
2864
2865 // Create a symlink pointing to a nonexistent target (dangling)
2866 symlink(at.plus("nonexistent"), at.plus("dangling")).unwrap();
2867 assert!(at.plus("dangling").is_symlink());
2868
2869 at.write("file.txt", "hello");
2870
2871 // install -D file.txt dangling/subdir/file.txt should fail
2872 scene
2873 .ucmd()
2874 .args(&["-D", "-m", "644"])
2875 .arg(at.plus("file.txt"))
2876 .arg(at.plus("dangling/subdir/file.txt"))
2877 .fails();
2878
2879 // The dangling symlink must not have been replaced with a real directory
2880 assert!(
2881 at.plus("dangling").is_symlink(),
2882 "Dangling symlink must not be replaced with a real directory"
2883 );
2884 assert!(
2885 !at.plus("nonexistent").exists(),
2886 "The symlink target must not have been created"
2887 );
2888 }
2889
2890 #[test]
2891 #[cfg(target_os = "linux")]
2892 fn test_install_set_owner_nonexistent_uid_and_gid() {
2893 use std::collections::HashSet;
2894
2895 // Collect the ids already present on the system so we can pick unused ones.
2896 let mut used_uids: HashSet<u32> = HashSet::new();
2897 let mut used_gids: HashSet<u32> = HashSet::new();
2898 if let Ok(passwd) = fs::read_to_string("/etc/passwd") {
2899 for line in passwd.lines() {
2900 let fields: Vec<&str> = line.split(':').collect();
2901 if fields.len() < 4 {
2902 continue;
2903 }
2904 if let Ok(uid) = fields[2].parse::<u32>() {
2905 used_uids.insert(uid);
2906 }
2907 if let Ok(gid) = fields[3].parse::<u32>() {
2908 used_gids.insert(gid);
2909 }
2910 }
2911 }
2912
2913 // Pick high ids that are not associated with any user/group.
2914 let next_uid = (60_000..u32::MAX)
2915 .find(|id| !used_uids.contains(id))
2916 .expect("no unused uid found");
2917 let next_gid = (60_000..u32::MAX)
2918 .find(|id| !used_gids.contains(id))
2919 .expect("no unused gid found");
2920
2921 let ts = TestScenario::new(util_name!());
2922 let at = &ts.fixtures;
2923 at.touch("a");
2924
2925 if let Ok(result) = run_ucmd_as_root(
2926 &ts,
2927 &[
2928 format!("-o{next_uid}").as_str(),
2929 format!("-g{next_gid}").as_str(),
2930 "a",
2931 "b",
2932 ],
2933 ) {
2934 result.success();
2935 assert!(at.file_exists("b"));
2936
2937 let metadata = fs::metadata(at.plus("b")).unwrap();
2938 assert_eq!(metadata.uid(), next_uid);
2939 assert_eq!(metadata.gid(), next_gid);
2940 } else {
2941 println!("Test skipped; requires root user");
2942 }
2943 }
2944
2945 #[test]
2946 #[cfg(target_os = "linux")]
2947 fn test_install_proc_self_mem_as_dst() {
2948 let scene = TestScenario::new(util_name!());
2949 let src = "/dev/full";
2950 let dest = "/proc/self/mem";
2951
2952 scene
2953 .ucmd()
2954 .args(&["-g", "0"])
2955 .arg(src)
2956 .arg(dest)
2957 .fails()
2958 .stderr_contains("cannot remove");
2959 }
2960
2961 #[test]
2962 #[cfg(target_os = "linux")]
2963 fn test_install_dev_full_as_dst() {
2964 let scene = TestScenario::new(util_name!());
2965
2966 scene
2967 .ucmd()
2968 .arg("/dev/null")
2969 .arg("/dev/full")
2970 .fails()
2971 .stderr_contains("cannot remove '/dev/full'");
2972 }
2973
2974 #[test]
2975 fn test_install_backup_nil_same_file() {
2976 let scene = TestScenario::new(util_name!());
2977 let at = &scene.fixtures;
2978
2979 let file = "test_install_backup_numbering_file";
2980
2981 at.write(file, "content");
2982
2983 let methods = [
2984 "none", "off", "numbered", "t", "existing", "nil", "simple", "never",
2985 ];
2986
2987 for method in &methods {
2988 scene
2989 .ucmd()
2990 .args(&[
2991 format!("--backup={method}"),
2992 file.to_string(),
2993 file.to_string(),
2994 ])
2995 .fails()
2996 .stderr_contains("are the same file");
2997 assert_eq!(at.read(file), "content");
2998 }
2999 }
3000
3001 #[test]
3002 fn test_install_backup_refuses_when_source_is_the_backup() {
3003 // Installing `a~` over `a` renames `a` to `a~`, which would clobber the
3004 // source before it is read. GNU refuses; so must we.
3005 for mode in ["simple", "existing"] {
3006 let scene = TestScenario::new(util_name!());
3007 let at = &scene.fixtures;
3008 at.touch("a");
3009 at.write("a~", "source content");
3010
3011 scene
3012 .ucmd()
3013 .arg(format!("--backup={mode}"))
3014 .arg("a~")
3015 .arg("a")
3016 .fails()
3017 .stderr_is("install: backing up 'a' might destroy source; 'a~' not copied\n");
3018
3019 // The source must be intact.
3020 assert_eq!(at.read("a~"), "source content");
3021 }
3022 }
3023
3024 #[test]
3025 fn test_install_backup_numbered_allows_source_named_like_backup() {
3026 // A numbered backup never reuses an existing name, so it cannot destroy
3027 // the source and must not be refused.
3028 let scene = TestScenario::new(util_name!());
3029 let at = &scene.fixtures;
3030 at.touch("a");
3031 at.write("a~", "source content");
3032
3033 scene
3034 .ucmd()
3035 .arg("--backup=numbered")
3036 .arg("a~")
3037 .arg("a")
3038 .succeeds();
3039 assert_eq!(at.read("a~"), "source content");
3040 assert_eq!(at.read("a"), "source content");
3041 }
3042
3043 #[test]
3044 // Android denies hard links on the filesystem backing the test directory, so
3045 // the setup cannot be built there; see the mv analogue.
3046 #[cfg(not(target_os = "android"))]
3047 fn test_install_backup_allows_hardlink_under_another_name() {
3048 // `other` shares an inode with `a~` but its name is not `a` + suffix, so
3049 // the backup rename cannot clobber it. GNU allows this.
3050 let scene = TestScenario::new(util_name!());
3051 let at = &scene.fixtures;
3052 at.touch("a");
3053 at.write("a~", "source content");
3054 at.hard_link("a~", "other");
3055
3056 scene
3057 .ucmd()
3058 .arg("--backup=simple")
3059 .arg("other")
3060 .arg("a")
3061 .succeeds();
3062 assert_eq!(at.read("a"), "source content");
3063 }
3064
3065 #[test]
3066 fn test_install_backup_refuses_regardless_of_spelling() {
3067 // The guard compares files, not the strings naming them.
3068 let scene = TestScenario::new(util_name!());
3069 let at = &scene.fixtures;
3070 at.touch("a");
3071 at.write("a~", "source content");
3072
3073 scene
3074 .ucmd()
3075 .arg("--backup=simple")
3076 .arg("./a~")
3077 .arg("a")
3078 .fails()
3079 .stderr_contains("might destroy source");
3080 assert_eq!(at.read("a~"), "source content");
3081 }
3082
3083 #[test]
3084 fn test_install_backup_custom_suffix_refuses() {
3085 let scene = TestScenario::new(util_name!());
3086 let at = &scene.fixtures;
3087 at.touch("a");
3088 at.write("a.bak", "source content");
3089
3090 scene
3091 .ucmd()
3092 .args(&["-b", "--suffix=.bak", "a.bak", "a"])
3093 .fails()
3094 .stderr_contains("might destroy source");
3095 assert_eq!(at.read("a.bak"), "source content");
3096 }
3097
3098 // The mode is only parsed where a mode means something.
3099 #[cfg(unix)]
3100 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
3101 mod diagnostics {
3102 use super::*;
3103 #[test]
3104 fn test_snippet_points_at_the_bad_operator() {
3105 let (at, mut ucmd) = at_and_ucmd!();
3106 at.touch("source");
3107
3108 let result = ucmd
3109 .terminal_sim_stderr()
3110 .args(&["-m", "u+rw?x", "source", "dest"])
3111 .fails_with_code(1);
3112 let stderr = result.stderr_str();
3113
3114 assert!(stderr.contains("invalid operator"), "{stderr}");
3115 // The caret lands on `?`: three columns of `-m ` and four of mode.
3116 assert_eq!(result.caret_column(), Some(8), "{stderr}");
3117 }
3118
3119 #[test]
3120 fn test_plain_message_when_stderr_is_a_pipe() {
3121 let (at, mut ucmd) = at_and_ucmd!();
3122 at.touch("source");
3123
3124 // The test harness pipes stderr, so the report must not appear.
3125 let result = ucmd
3126 .args(&["-m", "u+rw?x", "source", "dest"])
3127 .fails_with_code(1);
3128 let stderr = result.stderr_str();
3129
3130 assert!(stderr.starts_with("install: "), "{stderr}");
3131 assert!(!stderr.contains(":1:"), "{stderr}");
3132 }
3133 }
3134
3135 #[test]
3136 #[cfg(unix)]
3137 fn test_install_d_parallel_mkdir_race() {
3138 // Regression test for issue #12355: concurrent `install -D` invocations
3139 // that share parent directories must all succeed instead of one losing
3140 // the stat/mkdir race and failing with `cannot create directory`.
3141 const PARALLEL: usize = 32;
3142 const ROUNDS: usize = 10;
3143
3144 let scene = TestScenario::new(util_name!());
3145 let at = &scene.fixtures;
3146 at.touch("s");
3147
3148 for round in 0..ROUNDS {
3149 let mut children = Vec::with_capacity(PARALLEL);
3150 for k in 0..PARALLEL {
3151 let mut cmd = scene.ucmd();
3152 cmd.arg("-D").arg("s").arg(format!("o{round}/q/f{k}"));
3153 children.push(cmd.run_no_wait());
3154 }
3155
3156 for child in children {
3157 child.wait().unwrap().success().no_stderr();
3158 }
3159
3160 for k in 0..PARALLEL {
3161 assert!(at.file_exists(format!("o{round}/q/f{k}")));
3162 }
3163 }
3164 }
3165
3166 #[test]
3167 #[cfg(target_os = "linux")]
3168 fn test_install_target_without_splice_support() {
3169 // Does eCryptfs not support splice on some kernel?
3170 use std::process::Command;
3171 if Command::new("strace")
3172 .args(["-e", "inject=splice:error=EINVAL:when=2", "true"])
3173 .output()
3174 .is_err()
3175 {
3176 return; // missing strace
3177 }
3178 let coreutils = uutests::util::get_tests_binary();
3179 Command::new("strace")
3180 .args([
3181 "-e",
3182 "inject=splice:error=EINVAL:when=2",
3183 coreutils,
3184 "install",
3185 coreutils,
3186 "target_file",
3187 ])
3188 .output()
3189 .unwrap();
3190 // properly copied with fallback from splice?
3191 assert!(uucore::fs::are_files_identical(coreutils, "target_file").unwrap());
3192 }