1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#![allow(clippy::borrow_as_ptr)]
use crate::common::util::TestScenario;
use regex::Regex;
#[cfg(target_os = "linux")]
use std::fmt::Write;
// tests for basic tee functionality.
// inspired by:
// https://github.com/coreutils/coreutils/tests/misc/tee.sh
// spell-checker:ignore nopipe
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}
#[test]
fn test_short_help_is_long_help() {
// I can't believe that this test is necessary.
let help_short = new_ucmd!()
.arg("-h")
.succeeds()
.no_stderr()
.stdout_str()
.to_owned();
new_ucmd!()
.arg("--help")
.succeeds()
.no_stderr()
.stdout_is(help_short);
}
#[test]
fn test_tee_processing_multiple_operands() {
// POSIX says: "Processing of at least 13 file operands shall be supported."
let content = "tee_sample_content";
for n in [1, 2, 12, 13] {
let files = (1..=n).map(|x| x.to_string()).collect::<Vec<_>>();
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&files)
.pipe_in(content)
.succeeds()
.stdout_is(content);
for file in &files {
assert!(at.file_exists(file));
assert_eq!(at.read(file), content);
}
}
}
#[test]
fn test_tee_treat_minus_as_filename() {
// Ensure tee treats '-' as the name of a file, as mandated by POSIX.
let (at, mut ucmd) = at_and_ucmd!();
let content = "tee_sample_content";
let file = "-";
ucmd.arg("-").pipe_in(content).succeeds().stdout_is(content);
assert!(at.file_exists(file));
assert_eq!(at.read(file), content);
}
#[test]
fn test_tee_append() {
let (at, mut ucmd) = at_and_ucmd!();
let content = "tee_sample_content";
let file = "tee_out";
at.touch(file);
at.write(file, content);
assert_eq!(at.read(file), content);
ucmd.arg("-a")
.arg(file)
.pipe_in(content)
.succeeds()
.stdout_is(content);
assert!(at.file_exists(file));
assert_eq!(at.read(file), content.repeat(2));
}
#[test]
#[cfg(target_os = "linux")]
fn test_tee_no_more_writeable_1() {
// equals to 'tee /dev/full out2 <multi_read' call
let (at, mut ucmd) = at_and_ucmd!();
let content = (1..=10).fold(String::new(), |mut output, x| {
writeln!(output, "{x}").unwrap();
output
});
let file_out = "tee_file_out";
ucmd.arg("/dev/full")
.arg(file_out)
.pipe_in(&content[..])
.fails()
.stdout_contains(&content)
.stderr_contains("No space left on device");
assert_eq!(at.read(file_out), content);
}
#[test]
fn test_readonly() {
let (at, mut ucmd) = at_and_ucmd!();
let content_tee = "hello";
let content_file = "world";
let file_out = "tee_file_out";
let writable_file = "tee_file_out2";
at.write(file_out, content_file);
at.set_readonly(file_out);
ucmd.arg(file_out)
.arg(writable_file)
.pipe_in(content_tee)
.ignore_stdin_write_error()
.fails()
.stdout_is(content_tee)
// Windows says "Access is denied" for some reason.
.stderr_matches(&Regex::new("(Permission|Access is) denied").unwrap());
assert_eq!(at.read(file_out), content_file);
assert_eq!(at.read(writable_file), content_tee);
}
#[test]
#[cfg(target_os = "linux")]
fn test_tee_no_more_writeable_2() {
// should be equals to 'tee out1 out2 >/dev/full <multi_read' call
// but currently there is no way to redirect stdout to /dev/full
// so this test is disabled
let (_at, mut ucmd) = at_and_ucmd!();
let _content = (1..=10).fold(String::new(), |mut output, x| {
let _ = writeln!(output, "{x}");
output
});
let file_out_a = "tee_file_out_a";
let file_out_b = "tee_file_out_b";
let _result = ucmd
.arg(file_out_a)
.arg(file_out_b)
.pipe_in("/dev/full")
.succeeds(); // TODO: expected to succeed currently; change to fails() when required
// TODO: comment in after https://github.com/uutils/coreutils/issues/1805 is fixed
// assert_eq!(at.read(file_out_a), content);
// assert_eq!(at.read(file_out_b), content);
// assert!(result.stderr.contains("No space left on device"));
}
#[cfg(target_os = "linux")]
mod linux_only {
use crate::common::util::{AtPath, CmdResult, TestScenario, UCommand};
use std::fmt::Write;
use std::fs::File;
use std::process::Stdio;
use std::time::Duration;
fn make_broken_pipe() -> File {
use libc::c_int;
use std::os::unix::io::FromRawFd;
let mut fds: [c_int; 2] = [0, 0];
assert!(
(unsafe { libc::pipe(std::ptr::from_mut::<c_int>(&mut fds[0])) } == 0),
"Failed to create pipe"
);
// Drop the read end of the pipe
let _ = unsafe { File::from_raw_fd(fds[0]) };
// Make the write end of the pipe into a Rust File
unsafe { File::from_raw_fd(fds[1]) }
}
fn make_hanging_read() -> File {
use libc::c_int;
use std::os::unix::io::FromRawFd;
let mut fds: [c_int; 2] = [0, 0];
assert!(
(unsafe { libc::pipe(std::ptr::from_mut::<c_int>(&mut fds[0])) } == 0),
"Failed to create pipe"
);
// PURPOSELY leak the write end of the pipe, so the read end hangs.
// Return the read end of the pipe
unsafe { File::from_raw_fd(fds[0]) }
}
fn run_tee(proc: &mut UCommand) -> (String, CmdResult) {
let content = (1..=100_000).fold(String::new(), |mut output, x| {
let _ = writeln!(output, "{x}");
output
});
let result = proc
.ignore_stdin_write_error()
.set_stdin(Stdio::piped())
.run_no_wait()
.pipe_in_and_wait(content.as_bytes());
(content, result)
}
fn expect_success(result: &CmdResult) {
assert!(
result.succeeded(),
"Command was expected to succeed.\nstdout = {}\n stderr = {}",
std::str::from_utf8(result.stdout()).unwrap(),
std::str::from_utf8(result.stderr()).unwrap(),
);
assert!(
result.stderr_str().is_empty(),
"Unexpected data on stderr.\n stderr = {}",
std::str::from_utf8(result.stderr()).unwrap(),
);
}
fn expect_failure(result: &CmdResult, message: &str) {
assert!(
!result.succeeded(),
"Command was expected to fail.\nstdout = {}\n stderr = {}",
std::str::from_utf8(result.stdout()).unwrap(),
std::str::from_utf8(result.stderr()).unwrap(),
);
assert!(
result.stderr_str().contains(message),
"Expected to see error message fragment {} in stderr, but did not.\n stderr = {}",
message,
std::str::from_utf8(result.stderr()).unwrap(),
);
}
fn expect_silent_failure(result: &CmdResult) {
assert!(
!result.succeeded(),
"Command was expected to fail.\nstdout = {}\n stderr = {}",
std::str::from_utf8(result.stdout()).unwrap(),
std::str::from_utf8(result.stderr()).unwrap(),
);
assert!(
result.stderr_str().is_empty(),
"Unexpected data on stderr.\n stderr = {}",
std::str::from_utf8(result.stderr()).unwrap(),
);
}
fn expect_correct(name: &str, at: &AtPath, contents: &str) {
assert!(at.file_exists(name));
let compare = at.read(name);
assert_eq!(compare, contents);
}
fn expect_short(name: &str, at: &AtPath, contents: &str) {
assert!(at.file_exists(name));
let compare = at.read(name);
assert!(
compare.len() < contents.len(),
"Too many bytes ({}) written to {} (should be a short count from {})",
compare.len(),
name,
contents.len()
);
assert!(contents.starts_with(&compare),
"Expected truncated output to be a prefix of the correct output, but it isn't.\n Correct: {contents}\n Compare: {compare}");
}
#[test]
fn test_pipe_error_default() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd.arg(file_out_a).set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_silent_failure(&output);
expect_short(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_warn_nopipe_1() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("-p")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_warn_nopipe_2() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_warn_nopipe_3() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=warn-nopipe")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_warn_nopipe_3_shortcut() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=warn-")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_warn() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=warn")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "Broken pipe");
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_exit() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=exit")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "Broken pipe");
expect_short(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_exit_nopipe() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=exit-nopipe")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_error_exit_nopipe_shortcut() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=exit-nop")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_default() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd.arg(file_out_a).arg("/dev/full");
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_warn_nopipe_1() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("-p")
.arg(file_out_a)
.arg("/dev/full")
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_warn_nopipe_2() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error")
.arg(file_out_a)
.arg("/dev/full")
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_warn_nopipe_3() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=warn-nopipe")
.arg(file_out_a)
.arg("/dev/full")
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_warn() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=warn")
.arg(file_out_a)
.arg("/dev/full")
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_correct(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_exit() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=exit")
.arg(file_out_a)
.arg("/dev/full")
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_short(file_out_a, &at, content.as_str());
}
#[test]
fn test_space_error_exit_nopipe() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("--output-error=exit-nopipe")
.arg(file_out_a)
.arg("/dev/full")
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_failure(&output, "No space left");
expect_short(file_out_a, &at, content.as_str());
}
#[test]
fn test_pipe_mode_broken_pipe_only() {
new_ucmd!()
.timeout(Duration::from_secs(1))
.arg("-p")
.set_stdin(make_hanging_read())
.set_stdout(make_broken_pipe())
.succeeds();
}
#[test]
fn test_pipe_mode_broken_pipe_file() {
let (at, mut ucmd) = at_and_ucmd!();
let file_out_a = "tee_file_out_a";
let proc = ucmd
.arg("-p")
.arg(file_out_a)
.set_stdout(make_broken_pipe());
let (content, output) = run_tee(proc);
expect_success(&output);
expect_correct(file_out_a, &at, content.as_str());
}
}
|