Debian Codegraph @

tests/by-util/test_tac.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 axxbxx bxxaxx axxx axxxx xxaxx xxax xxxxa axyz zyax zyxa bbaaa aaabc bcdddd cddddaaabc xyzabc abcxyzabc nbbaaa EISDIR SIGBUS mmap
7 #[cfg(unix)]
8 use uutests::at_and_ucmd;
9 use uutests::new_ucmd;
10 use uutests::util::TestScenario;
11 use uutests::util_name;
12
13 #[test]
14 #[cfg(target_os = "linux")]
15 fn test_tac_non_utf8_paths() {
16 use std::os::unix::ffi::OsStringExt;
17 let (at, mut ucmd) = at_and_ucmd!();
18
19 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
20 std::fs::write(at.plus(&filename), b"line1\nline2\nline3\n").unwrap();
21
22 ucmd.arg(&filename)
23 .succeeds()
24 .stdout_is("line3\nline2\nline1\n");
25 }
26
27 #[test]
28 fn test_invalid_arg() {
29 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
30 }
31
32 #[test]
33 fn test_stdin_default() {
34 new_ucmd!()
35 .pipe_in("100\n200\n300\n400\n500")
36 .succeeds()
37 .stdout_is("500400\n300\n200\n100\n");
38 }
39
40 #[test]
41 fn test_stdin_non_newline_separator() {
42 new_ucmd!()
43 .args(&["-s", ":"])
44 .pipe_in("100:200:300:400:500")
45 .succeeds()
46 .stdout_is("500400:300:200:100:");
47 }
48
49 #[test]
50 fn test_stdin_non_newline_separator_before() {
51 new_ucmd!()
52 .args(&["-b", "-s", ":"])
53 .pipe_in("100:200:300:400:500")
54 .succeeds()
55 .stdout_is(":500:400:300:200100");
56 }
57
58 #[test]
59 fn test_single_default() {
60 new_ucmd!()
61 .arg("prime_per_line.txt")
62 .succeeds()
63 .stdout_is_fixture("prime_per_line.expected");
64 }
65
66 #[test]
67 fn test_single_non_newline_separator() {
68 new_ucmd!()
69 .args(&["-s", ":", "delimited_primes.txt"])
70 .succeeds()
71 .stdout_is_fixture("delimited_primes.expected");
72 }
73
74 #[test]
75 fn test_single_non_newline_separator_before() {
76 new_ucmd!()
77 .args(&["-b", "-s", ":", "delimited_primes.txt"])
78 .succeeds()
79 .stdout_is_fixture("delimited_primes_before.expected");
80 }
81
82 #[test]
83 fn test_invalid_input() {
84 let scene = TestScenario::new(util_name!());
85 let at = &scene.fixtures;
86
87 #[cfg(not(windows))]
88 let not_found_err = "failed to open 'b' for reading: No such file or directory";
89 #[cfg(windows)]
90 let not_found_err =
91 "failed to open 'b' for reading: The system cannot find the file specified.";
92
93 scene.ucmd().arg("b").fails().stderr_contains(not_found_err);
94
95 at.mkdir("a");
96 // On Unix, File::open succeeds on directories but read_to_end fails with EISDIR.
97 // On Windows, File::open on a directory fails with "Access is denied".
98 #[cfg(not(windows))]
99 let dir_err = "a: read error: Is a directory";
100 #[cfg(windows)]
101 let dir_err = "failed to open 'a' for reading: Access is denied";
102
103 scene.ucmd().arg("a").fails().stderr_contains(dir_err);
104 }
105
106 #[test]
107 fn test_no_line_separators() {
108 new_ucmd!().pipe_in("a").succeeds().stdout_is("a");
109 }
110
111 #[test]
112 fn test_before_trailing_separator_no_leading_separator() {
113 new_ucmd!()
114 .arg("-b")
115 .pipe_in("a\nb\n")
116 .succeeds()
117 .stdout_is("\n\nba");
118 }
119
120 #[test]
121 fn test_before_trailing_separator_and_leading_separator() {
122 new_ucmd!()
123 .arg("-b")
124 .pipe_in("\na\nb\n")
125 .succeeds()
126 .stdout_is("\n\nb\na");
127 }
128
129 #[test]
130 fn test_before_leading_separator_no_trailing_separator() {
131 new_ucmd!()
132 .arg("-b")
133 .pipe_in("\na\nb")
134 .succeeds()
135 .stdout_is("\nb\na");
136 }
137
138 #[test]
139 fn test_before_no_separator() {
140 new_ucmd!()
141 .arg("-b")
142 .pipe_in("ab")
143 .succeeds()
144 .stdout_is("ab");
145 }
146
147 #[test]
148 fn test_before_empty_file() {
149 new_ucmd!().arg("-b").pipe_in("").succeeds().no_output();
150 }
151
152 #[test]
153 fn test_multi_char_separator() {
154 new_ucmd!()
155 .args(&["-s", "xx"])
156 .pipe_in("axxbxx")
157 .succeeds()
158 .stdout_is("bxxaxx");
159 }
160
161 #[test]
162 // FIXME: See https://github.com/uutils/coreutils/issues/4204
163 #[cfg(not(windows))]
164 fn test_multi_char_separator_overlap() {
165 // The right-most pair of "x" characters in the input is treated as
166 // the only line separator. That is, "axxx" is interpreted as having
167 // one line comprising the string "ax" followed by the line
168 // separator "xx".
169 new_ucmd!()
170 .args(&["-s", "xx"])
171 .pipe_in("axxx")
172 .succeeds()
173 .stdout_is("axxx");
174
175 // Each non-overlapping pair of "x" characters in the input is
176 // treated as a line separator. That is, "axxxx" is interpreted as
177 // having two lines:
178 //
179 // * the second line is the empty string "" followed by the line
180 // separator "xx",
181 // * the first line is the string "a" followed by the line separator
182 // "xx".
183 //
184 // The lines are printed in reverse, resulting in "xx" followed by
185 // "axx".
186 new_ucmd!()
187 .args(&["-s", "xx"])
188 .pipe_in("axxxx")
189 .succeeds()
190 .stdout_is("xxaxx");
191 }
192
193 #[test]
194 fn test_multi_char_separator_overlap_before() {
195 // With the "-b" option, the line separator is assumed to be at the
196 // beginning of the line. In this case, That is, "axxx" is
197 // interpreted as having two lines:
198 //
199 // * the second line is the empty string "" preceded by the line
200 // separator "xx",
201 // * the first line is the string "ax" preceded by no line
202 // separator, since there are no more characters preceding it.
203 //
204 // The lines are printed in reverse, resulting in "xx" followed by
205 // "ax".
206 new_ucmd!()
207 .args(&["-b", "-s", "xx"])
208 .pipe_in("axxx")
209 .succeeds()
210 .stdout_is("xxax");
211
212 // With the "-b" option, the line separator is assumed to be at the
213 // beginning of the line. Each non-overlapping pair of "x"
214 // characters in the input is treated as a line separator. That is,
215 // "axxxx" is interpreted as having three lines:
216 //
217 // * the third line is the empty string "" preceded by the line
218 // separator "xx" (the last two "x" characters in the input
219 // string),
220 // * the second line is the empty string "" preceded by the line
221 // separator "xx" (the first two "x" characters in the input
222 // string),
223 // * the first line is the string "a" preceded by no line separator,
224 // since there are no more characters preceding it.
225 //
226 // The lines are printed in reverse, resulting in "xx" followed by
227 // "xx" followed by "a".
228 new_ucmd!()
229 .args(&["-b", "-s", "xx"])
230 .pipe_in("axxxx")
231 .succeeds()
232 .stdout_is("xxxxa");
233 }
234
235 #[test]
236 fn test_null_separator() {
237 new_ucmd!()
238 .args(&["-s", ""])
239 .pipe_in("a\0b\0")
240 .succeeds()
241 .stdout_is("b\0a\0");
242 }
243
244 #[test]
245 #[cfg(unix)]
246 fn test_non_utf8_separator() {
247 use std::os::unix::ffi::OsStringExt;
248 new_ucmd!()
249 .arg("-s")
250 .arg(std::ffi::OsString::from_vec(b"\xe9".to_vec()))
251 .pipe_in(b"1\xe92".to_vec())
252 .succeeds()
253 .no_stderr()
254 .stdout_is_bytes(b"21\xe9");
255 }
256
257 #[test]
258 #[cfg(unix)]
259 fn test_non_utf8_regex_separator() {
260 use std::os::unix::ffi::OsStringExt;
261
262 new_ucmd!()
263 .args(&["-r", "-s"])
264 .arg(std::ffi::OsString::from_vec(b"\xe9".to_vec()))
265 .pipe_in(b"a.b.\xe9c.d?".to_vec())
266 .succeeds()
267 .no_stderr()
268 .stdout_is_bytes(b"c.d?a.b.\xe9");
269
270 new_ucmd!()
271 .args(&["-r", "-s"])
272 .arg(std::ffi::OsString::from_vec(b"[.\xe9?]".to_vec()))
273 .pipe_in(b"a.b.\xe9c.d?".to_vec())
274 .succeeds()
275 .no_stderr()
276 .stdout_is_bytes(b"d?\xe9c.b.a.");
277
278 new_ucmd!()
279 .args(&["-r", "-s"])
280 .arg(std::ffi::OsString::from_vec(b"[.?]\xe9".to_vec()))
281 .pipe_in(b"a.b\xe9c.d?")
282 .succeeds()
283 .no_stderr()
284 .stdout_is_bytes(b"a.b\xe9c.d?");
285
286 new_ucmd!()
287 .args(&["-r", "-s"])
288 .arg(std::ffi::OsString::from_vec(b"[.?]\xe9".to_vec()))
289 .pipe_in(b"a.b[.?]\xe9c.d?")
290 .succeeds()
291 .no_stderr()
292 .stdout_is_bytes(b"a.b[.?]\xe9c.d?");
293
294 new_ucmd!()
295 .args(&["-r", "-s"])
296 .arg(std::ffi::OsString::from_vec(b"[.?]\xe9".to_vec()))
297 .pipe_in(b"a.\xe9b")
298 .succeeds()
299 .no_stderr()
300 .stdout_is_bytes(b"ba.\xe9");
301 }
302
303 #[test]
304 fn test_regex_bare_anchors() {
305 new_ucmd!()
306 .args(&["-r", "-s", "^"])
307 .pipe_in("a\nb\nc\n")
308 .succeeds()
309 .no_stderr()
310 .stdout_is_bytes(b"c\nb\na\n");
311
312 new_ucmd!()
313 .args(&["-r", "-s", "$"])
314 .pipe_in("a\nb\nc\n")
315 .succeeds()
316 .stdout_is_bytes(b"\n\nc\nba");
317
318 new_ucmd!()
319 .args(&["-r", "-s", "^$"])
320 .pipe_in("a\nb\nc\n")
321 .succeeds()
322 .stdout_is_bytes(b"a\nb\nc\n");
323 }
324
325 #[test]
326 fn test_regex() {
327 new_ucmd!()
328 .args(&["-r", "-s", "[xyz]+"])
329 .pipe_in("axyz")
330 .succeeds()
331 .no_stderr()
332 .stdout_is("zyax");
333
334 new_ucmd!()
335 .args(&["-r", "-s", ":+"])
336 .pipe_in("a:b::c:::d::::")
337 .succeeds()
338 .no_stderr()
339 .stdout_is(":::d:::c::b:a:");
340
341 new_ucmd!()
342 .args(&["-r", "-s", r"[\+]+[-]+[\+]+"])
343 // line 0 1 2
344 // |--||-----||--------|
345 .pipe_in("a+-+b++--++c+d-e+---+")
346 .succeeds()
347 .no_stderr()
348 // line 2 1 0
349 // |--------||-----||--|
350 .stdout_is("c+d-e+---+b++--++a+-+");
351 }
352
353 #[test]
354 fn test_regex_before() {
355 new_ucmd!()
356 .args(&["-b", "-r", "-s", "[xyz]+"])
357 .pipe_in("axyz")
358 .succeeds()
359 .no_stderr()
360 .stdout_is("zyxa");
361
362 new_ucmd!()
363 .args(&["-b", "-r", "-s", ":+"])
364 .pipe_in(":a::b:::c::::d")
365 .succeeds()
366 .stdout_is(":d::::c:::b::a");
367
368 // Because `tac` searches for matches of the regular expression from
369 // right to left, the second to last line is
370 //
371 // +--++b
372 //
373 // not
374 //
375 // ++--++b
376 //
377 new_ucmd!()
378 .args(&["-b", "-r", "-s", r"[\+]+[-]+[\+]+"])
379 // line 0 1 2
380 // |---||----||--------|
381 .pipe_in("+-+a++--++b+---+c+d-e")
382 .succeeds()
383 .no_stderr()
384 // line 2 1 0
385 // |--------||----||---|
386 .stdout_is("+---+c+d-e+--++b+-+a+");
387 }
388
389 #[cfg(target_os = "linux")]
390 #[test]
391 fn test_failed_write_is_reported() {
392 new_ucmd!()
393 .pipe_in("hello")
394 .set_stdout(std::fs::File::create("/dev/full").unwrap())
395 .fails()
396 .stderr_is("tac: failed to write to stdout: No space left on device\n");
397 }
398
399 #[cfg(unix)]
400 #[test]
401 fn test_stdin_bad_tmpdir_fallback() {
402 // When TMPDIR is invalid, tac falls back to reading stdin directly into memory
403 new_ucmd!()
404 .env("TMPDIR", "/nonexistent/dir")
405 .arg("-")
406 .pipe_in("a\nb\nc\n")
407 .succeeds()
408 .stdout_is("c\nb\na\n");
409 }
410
411 #[test]
412 fn test_regex_or_operator() {
413 new_ucmd!()
414 .args(&["-r", "-s", r"[^x]\|x"])
415 .pipe_in("abc")
416 .succeeds()
417 .stdout_is("cba");
418 }
419
420 #[test]
421 fn test_unescaped_middle_anchor() {
422 new_ucmd!()
423 .args(&["-r", "-s", r"1^2"])
424 .pipe_in("111^222")
425 .succeeds()
426 .stdout_is("22111^2");
427
428 new_ucmd!()
429 .args(&["-r", "-s", r"a$b"])
430 .pipe_in("aaa$bbb")
431 .succeeds()
432 .stdout_is("bbaaa$b");
433 }
434
435 #[test]
436 fn test_escaped_middle_anchor() {
437 new_ucmd!()
438 .args(&["-r", "-s", r"c\^b"])
439 .pipe_in("aaabc^bcdddd")
440 .succeeds()
441 .stdout_is("cddddaaabc^b");
442
443 new_ucmd!()
444 .args(&["-r", "-s", r"c\$b"])
445 .pipe_in("aaabc$bcdddd")
446 .succeeds()
447 .stdout_is("cddddaaabc$b");
448 }
449
450 #[test]
451 fn test_regular_start_anchor() {
452 new_ucmd!()
453 .args(&["-r", "-s", r"^abc"])
454 .pipe_in("xyzabc123abc")
455 .succeeds()
456 .stdout_is("xyzabc123abc");
457
458 new_ucmd!()
459 .args(&["-r", "-s", r"^b"])
460 .pipe_in("aaa\nbbb\nccc\n")
461 .succeeds()
462 .stdout_is("bb\nccc\naaa\nb");
463 }
464
465 #[test]
466 fn test_regular_end_anchor() {
467 new_ucmd!()
468 .args(&["-r", "-s", r"abc$"])
469 .pipe_in("123abcxyzabc")
470 .succeeds()
471 .stdout_is("123abcxyzabc");
472
473 new_ucmd!()
474 .args(&["-r", "-s", r"b$"])
475 .pipe_in("aaa\nbbb\nccc\n")
476 .succeeds()
477 .stdout_is("\nccc\nbbaaa\nb");
478 }
479
480 /// Regression test for <https://github.com/uutils/coreutils/issues/9748>.
481 ///
482 /// `tac` used to mmap regular files, so truncating a file mid-read raised
483 /// SIGBUS and killed the process. It now reads files into memory up front, so a
484 /// concurrent truncation can no longer crash it. The assertion only checks that
485 /// no signal killed `tac`, so it is stable regardless of how the race lands.
486 #[test]
487 #[cfg(unix)]
488 fn test_tac_file_truncated_during_read_does_not_crash() {
489 use std::fs::OpenOptions;
490 use std::time::Duration;
491
492 let (at, mut ucmd) = at_and_ucmd!();
493
494 // A large sparse file, so the read overlaps with the truncation below.
495 // `set_len` keeps it sparse, so creation stays cheap.
496 let name = "input";
497 at.make_file(name).set_len(64 * 1024 * 1024).unwrap();
498
499 let child = ucmd.arg(name).run_no_wait();
500
501 // Give tac a moment to start reading, then truncate the file out from
502 // under it.
503 std::thread::sleep(Duration::from_millis(2));
504 OpenOptions::new()
505 .write(true)
506 .open(at.plus(name))
507 .unwrap()
508 .set_len(0)
509 .unwrap();
510
511 let result = child.wait().unwrap();
512 assert!(
513 result.signal().is_none(),
514 "tac was killed by signal {:?} (SIGBUS regression, see #9748)",
515 result.signal()
516 );
517 }
518
519 /// Companion to the test above for the `tac < file` path. `tac` used to mmap the
520 /// raw stdin fd, exposing a redirected file to the same SIGBUS-on-truncation
521 /// race; it now copies stdin to an unlinked temp file before mapping.
522 #[test]
523 #[cfg(unix)]
524 fn test_tac_stdin_redirected_file_truncated_during_read_does_not_crash() {
525 use std::fs::{File, OpenOptions};
526 use std::time::Duration;
527
528 let (at, mut ucmd) = at_and_ucmd!();
529
530 // A large sparse file, so the read overlaps with the truncation below.
531 let name = "input";
532 at.make_file(name).set_len(64 * 1024 * 1024).unwrap();
533
534 // Redirect the regular file in as stdin (`tac < input`).
535 let child = ucmd
536 .set_stdin(File::open(at.plus(name)).unwrap())
537 .run_no_wait();
538
539 // Give tac a moment to start reading, then truncate the file out from
540 // under it.
541 std::thread::sleep(Duration::from_millis(2));
542 OpenOptions::new()
543 .write(true)
544 .open(at.plus(name))
545 .unwrap()
546 .set_len(0)
547 .unwrap();
548
549 let result = child.wait().unwrap();
550 assert!(
551 result.signal().is_none(),
552 "tac was killed by signal {:?} (SIGBUS regression, see #9748)",
553 result.signal()
554 );
555 }