Debian Codegraph @

tests/by-util/test_yes.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 use std::ffi::OsStr;
7
8 use uutests::new_ucmd;
9
10 const NO_ARGS: &[&str] = &[];
11
12 /// Run `yes`, capture some of the output, then check exit status.
13 fn run(args: &[impl AsRef<OsStr>], expected: &[u8]) {
14 let result = new_ucmd!().args(args).run_stdout_starts_with(expected);
15
16 // On Unix systems (not WASI), yes should be terminated by SIGPIPE when the pipe closes.
17 // On WASI and Windows, there are no signals, so just check the process succeeded.
18 #[cfg(all(unix, not(wasi_runner)))]
19 result.signal_name_is("PIPE");
20
21 #[cfg(any(not(unix), wasi_runner))]
22 result.success();
23 }
24
25 #[test]
26 fn test_invalid_arg() {
27 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
28 }
29
30 #[test]
31 fn test_version() {
32 new_ucmd!().arg("--version").succeeds();
33 }
34
35 #[test]
36 #[cfg_attr(wasi_runner, ignore)]
37 fn test_simple() {
38 run(NO_ARGS, b"y\ny\ny\ny\n");
39 }
40
41 #[test]
42 #[cfg_attr(wasi_runner, ignore)]
43 fn test_args() {
44 run(&["a", "bar", "c"], b"a bar c\na bar c\na ba");
45 }
46
47 #[test]
48 #[cfg_attr(wasi_runner, ignore)]
49 fn test_long_output() {
50 run(NO_ARGS, "y\n".repeat(512 * 1024).as_bytes());
51 }
52
53 /// Test with an output that seems likely to get mangled in case of incomplete writes.
54 #[test]
55 #[cfg_attr(wasi_runner, ignore)]
56 fn test_long_odd_output() {
57 run(&["abcdef"], "abcdef\n".repeat(1024 * 1024).as_bytes());
58 }
59
60 /// Test with an input that doesn't fit in the standard buffer.
61 #[test]
62 #[cfg_attr(wasi_runner, ignore)]
63 fn test_long_input() {
64 #[cfg(not(windows))]
65 const TIMES: usize = 14000;
66 // On Windows the command line is limited to 8191 bytes.
67 // This is not actually enough to fill the buffer, but it's still nice to
68 // try something long.
69 #[cfg(windows)]
70 const TIMES: usize = 500;
71 let arg = "abcdef".repeat(TIMES) + "\n";
72 let expected_out = arg.repeat(5);
73 run(&[&arg[..arg.len() - 1]], expected_out.as_bytes());
74 }
75
76 /// A joined line larger than the internal broker pipe's capacity (1 MiB)
77 /// can cause a deadlock
78 #[test]
79 #[cfg(any(target_os = "linux", target_os = "android"))]
80 #[cfg_attr(wasi_runner, ignore)]
81 fn test_long_line_exceeds_pipe_capacity() {
82 // A single argv string is capped at ~128 KiB by the kernel, so use many
83 // args joined by spaces to build a >1 MiB line instead of one huge arg.
84 let word = "a".repeat(60_000);
85 let args: Vec<&str> = std::iter::repeat_n(word.as_str(), 20).collect();
86 let line = args.join(" ");
87 run(&args, format!("{line}\n{line}\n").as_bytes());
88 }
89
90 #[test]
91 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
92 #[cfg_attr(wasi_runner, ignore)]
93 fn test_piped_to_dev_full() {
94 use std::fs::OpenOptions;
95
96 for append in [true, false] {
97 {
98 let dev_full = OpenOptions::new()
99 .write(true)
100 .append(append)
101 .open("/dev/full")
102 .unwrap();
103
104 new_ucmd!()
105 .set_stdout(dev_full)
106 .fails()
107 .stderr_contains("No space left on device");
108 }
109 }
110 }
111
112 #[test]
113 #[cfg(unix)]
114 // WASI runners (wasmtime) require UTF-8 arguments, so skip this test when testing WASI binaries
115 #[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")]
116 fn test_non_utf8() {
117 use std::os::unix::ffi::OsStrExt;
118
119 run(
120 &[
121 OsStr::from_bytes(b"\xbf\xff\xee"),
122 OsStr::from_bytes(b"bar"),
123 ],
124 &b"\xbf\xff\xee bar\n".repeat(5000),
125 );
126 }