| 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 dont SIGBREAK |
| 7 |
|
| 8 |
use rstest::rstest; |
| 9 |
use std::time::Duration; |
| 10 |
use uucore::display::Quotable; |
| 11 |
use uutests::util::TestScenario; |
| 12 |
use uutests::{new_ucmd, util_name}; |
| 13 |
|
| 14 |
/// A scenario plus the path to the multicall test binary, used to run child |
| 15 |
/// commands (`sleep`, `true`, ...) portably: it exists on every test platform, |
| 16 |
/// unlike `sh`/`sleep` from `PATH`. |
| 17 |
fn scenario_with_bin() -> (TestScenario, String) { |
| 18 |
let ts = TestScenario::new(util_name!()); |
| 19 |
let bin = ts.bin_path.to_string_lossy().into_owned(); |
| 20 |
(ts, bin) |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn test_invalid_arg() { |
| 25 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(125); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_subcommand_return_code() { |
| 30 |
let (ts, bin) = scenario_with_bin(); |
| 31 |
ts.ucmd().args(&["1", &bin, "true"]).succeeds(); |
| 32 |
|
| 33 |
ts.ucmd().args(&["1", &bin, "false"]).fails_with_code(1); |
| 34 |
} |
| 35 |
|
| 36 |
#[rstest] |
| 37 |
#[case::alphabetic("xyz")] |
| 38 |
#[case::single_quote("'1")] |
| 39 |
fn test_invalid_time_interval(#[case] input: &str) { |
| 40 |
new_ucmd!() |
| 41 |
.args(&[input, "sleep", "0"]) |
| 42 |
.fails_with_code(125) |
| 43 |
.usage_error(format!("invalid time interval {}", input.quote())); |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn test_invalid_kill_after() { |
| 48 |
new_ucmd!() |
| 49 |
.args(&["-k", "xyz", "1", "sleep", "0"]) |
| 50 |
.fails_with_code(125) |
| 51 |
.usage_error("invalid time interval 'xyz'"); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn test_command_with_args() { |
| 56 |
let (ts, bin) = scenario_with_bin(); |
| 57 |
ts.ucmd() |
| 58 |
.args(&["1700", &bin, "echo", "-n", "abcd"]) |
| 59 |
.succeeds() |
| 60 |
.stdout_only("abcd"); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn test_verbose() { |
| 65 |
let (ts, bin) = scenario_with_bin(); |
| 66 |
for verbose_flag in ["-v", "--verbose"] { |
| 67 |
ts.ucmd() |
| 68 |
.args(&[verbose_flag, ".1", &bin, "sleep", "1"]) |
| 69 |
.fails() |
| 70 |
.no_stdout() |
| 71 |
.stderr_contains("timeout: sending signal TERM to command"); |
| 72 |
ts.ucmd() |
| 73 |
.args(&[verbose_flag, "-s0", "-k.1", ".1", &bin, "sleep", "1"]) |
| 74 |
.fails() |
| 75 |
.no_stdout() |
| 76 |
.stderr_contains("timeout: sending signal 0 to command") |
| 77 |
.stderr_contains("timeout: sending signal KILL to command"); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_zero_timeout() { |
| 83 |
let (ts, bin) = scenario_with_bin(); |
| 84 |
ts.ucmd() |
| 85 |
.args(&["-v", "0", &bin, "sleep", ".1"]) |
| 86 |
.succeeds() |
| 87 |
.no_output(); |
| 88 |
ts.ucmd() |
| 89 |
.args(&["-v", "0", "-s0", "-k0", &bin, "sleep", ".1"]) |
| 90 |
.succeeds() |
| 91 |
.no_output(); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn test_command_empty_args() { |
| 96 |
new_ucmd!() |
| 97 |
.args(&["", ""]) |
| 98 |
.fails() |
| 99 |
.stderr_contains("timeout: invalid time interval ''"); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_foreground() { |
| 104 |
let (ts, bin) = scenario_with_bin(); |
| 105 |
for arg in ["-f", "--foreground"] { |
| 106 |
ts.ucmd() |
| 107 |
.args(&[arg, ".1", &bin, "sleep", "10"]) |
| 108 |
.fails_with_code(124) |
| 109 |
.no_output(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn test_preserve_status() { |
| 115 |
let (ts, bin) = scenario_with_bin(); |
| 116 |
for arg in ["-p", "--preserve-status"] { |
| 117 |
ts.ucmd() |
| 118 |
.args(&[arg, ".1", &bin, "sleep", "10"]) |
| 119 |
// 128 + SIGTERM = 128 + 15 |
| 120 |
.fails_with_code(128 + 15) |
| 121 |
.no_output(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn test_kill_after_preserves_timeout_exit_without_preserve_status() { |
| 127 |
let (ts, bin) = scenario_with_bin(); |
| 128 |
ts.ucmd() |
| 129 |
.args(&["-k", "1", "1", &bin, "sleep", "10"]) |
| 130 |
.fails_with_code(124) |
| 131 |
.no_output(); |
| 132 |
} |
| 133 |
#[test] |
| 134 |
fn test_preserve_status_even_when_send_signal() { |
| 135 |
let (ts, bin) = scenario_with_bin(); |
| 136 |
// When sending CONT signal, process doesn't get killed or stopped. |
| 137 |
// So, expected result is success and code 0. |
| 138 |
for cont_spelling in ["CONT", "cOnT", "SIGcont"] { |
| 139 |
ts.ucmd() |
| 140 |
.args(&[ |
| 141 |
"-s", |
| 142 |
cont_spelling, |
| 143 |
"--preserve-status", |
| 144 |
".1", |
| 145 |
&bin, |
| 146 |
"sleep", |
| 147 |
"1", |
| 148 |
]) |
| 149 |
.succeeds() |
| 150 |
.no_output(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn test_dont_overflow() { |
| 156 |
let (ts, bin) = scenario_with_bin(); |
| 157 |
ts.ucmd() |
| 158 |
.args(&["9223372036854775808d", &bin, "sleep", "0"]) |
| 159 |
.succeeds() |
| 160 |
.no_output(); |
| 161 |
ts.ucmd() |
| 162 |
.args(&["-k", "9223372036854775808d", "10", &bin, "sleep", "0"]) |
| 163 |
.succeeds() |
| 164 |
.no_output(); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn test_dont_underflow() { |
| 169 |
let (ts, bin) = scenario_with_bin(); |
| 170 |
ts.ucmd() |
| 171 |
.args(&[".0000000001", &bin, "sleep", "1"]) |
| 172 |
.fails_with_code(124) |
| 173 |
.no_output(); |
| 174 |
ts.ucmd() |
| 175 |
.args(&["1e-100", &bin, "sleep", "1"]) |
| 176 |
.fails_with_code(124) |
| 177 |
.no_output(); |
| 178 |
// Unlike GNU coreutils, we underflow to 1ns for very short timeouts. |
| 179 |
// https://debbugs.gnu.org/cgi/bugreport.cgi?bug=77535 |
| 180 |
ts.ucmd() |
| 181 |
.args(&["1e-18172487393827593258", &bin, "sleep", "1"]) |
| 182 |
.fails_with_code(124) |
| 183 |
.no_output(); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn test_negative_interval() { |
| 188 |
new_ucmd!() |
| 189 |
.args(&["--", "-1", "sleep", "0"]) |
| 190 |
.fails() |
| 191 |
.usage_error("invalid time interval '-1'"); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn test_invalid_signal() { |
| 196 |
new_ucmd!() |
| 197 |
.args(&["-s", "invalid", "1", "sleep", "0"]) |
| 198 |
.fails() |
| 199 |
.usage_error("'invalid': invalid signal"); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn test_invalid_multi_byte_characters() { |
| 204 |
new_ucmd!() |
| 205 |
.args(&["10€", "sleep", "0"]) |
| 206 |
.fails() |
| 207 |
.usage_error("invalid time interval '10€'"); |
| 208 |
} |
| 209 |
|
| 210 |
/// Test that the long form of the `--kill-after` argument is recognized. |
| 211 |
#[test] |
| 212 |
fn test_kill_after_long() { |
| 213 |
let (ts, bin) = scenario_with_bin(); |
| 214 |
ts.ucmd() |
| 215 |
.args(&["--kill-after=1", "1", &bin, "sleep", "0"]) |
| 216 |
.succeeds() |
| 217 |
.no_output(); |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
#[cfg(unix)] |
| 222 |
fn test_kill_subprocess() { |
| 223 |
new_ucmd!() |
| 224 |
.args(&[ |
| 225 |
// Make sure the CI can spawn the subprocess. |
| 226 |
"1", |
| 227 |
"sh", |
| 228 |
"-c", |
| 229 |
"trap 'echo inside_trap' TERM; sleep 5", |
| 230 |
]) |
| 231 |
.fails_with_code(124) |
| 232 |
.stdout_contains("inside_trap"); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_hex_timeout_ending_with_d() { |
| 237 |
let (ts, bin) = scenario_with_bin(); |
| 238 |
ts.ucmd() |
| 239 |
.args(&["0x0.1d", &bin, "sleep", "10"]) |
| 240 |
.timeout(Duration::from_secs(1)) |
| 241 |
.fails_with_code(124) |
| 242 |
.no_output(); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
#[cfg_attr( |
| 247 |
all(target_vendor = "apple", target_arch = "x86_64"), |
| 248 |
ignore = "wontfix: intermittent failure in obsolete macOS x86_64 (macOS bug)." |
| 249 |
)] |
| 250 |
#[cfg(unix)] |
| 251 |
fn test_terminate_child_on_receiving_terminate() { |
| 252 |
let mut timeout_cmd = new_ucmd!() |
| 253 |
.args(&[ |
| 254 |
"10", |
| 255 |
"sh", |
| 256 |
"-c", |
| 257 |
"trap 'echo child received TERM' TERM; sleep 5", |
| 258 |
]) |
| 259 |
.run_no_wait(); |
| 260 |
timeout_cmd.delay(100); |
| 261 |
timeout_cmd.kill_with_custom_signal(nix::sys::signal::Signal::SIGTERM); |
| 262 |
timeout_cmd |
| 263 |
.make_assertion() |
| 264 |
.is_not_alive() |
| 265 |
.with_current_output() |
| 266 |
.code_is(143) |
| 267 |
.stdout_contains("child received TERM"); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn test_command_not_found() { |
| 272 |
// Test exit code 127 when command doesn't exist |
| 273 |
new_ucmd!() |
| 274 |
.args(&["1", "/this/command/definitely/does/not/exist"]) |
| 275 |
.fails_with_code(127); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn test_command_cannot_invoke() { |
| 280 |
// Test exit code 126 when command exists but cannot be invoked |
| 281 |
// Try to execute a directory (should give permission denied or similar) |
| 282 |
new_ucmd!().args(&["1", "/"]).fails_with_code(126); |
| 283 |
} |
| 284 |
|
| 285 |
#[test] |
| 286 |
#[cfg(unix)] |
| 287 |
fn test_sigchld_ignored_by_parent() { |
| 288 |
let ts = TestScenario::new(util_name!()); |
| 289 |
let bin_path = ts.bin_path.to_string_lossy(); |
| 290 |
ts.ucmd() |
| 291 |
.args(&[ |
| 292 |
"10", |
| 293 |
"sh", |
| 294 |
"-c", |
| 295 |
&format!("trap '' CHLD; exec {bin_path} timeout 1 true"), |
| 296 |
]) |
| 297 |
.succeeds(); |
| 298 |
} |
| 299 |
|
| 300 |
#[test] |
| 301 |
#[cfg(unix)] |
| 302 |
fn test_with_background_child() { |
| 303 |
new_ucmd!() |
| 304 |
.args(&[".5", "sh", "-c", "sleep .1 & sleep 2"]) |
| 305 |
.fails_with_code(124) |
| 306 |
.no_stdout(); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
#[cfg(unix)] |
| 311 |
fn test_forward_sigint_to_child() { |
| 312 |
let mut cmd = new_ucmd!() |
| 313 |
.args(&[ |
| 314 |
"10", |
| 315 |
"sh", |
| 316 |
"-c", |
| 317 |
"trap 'echo got_int; exit 42' INT; sleep 5", |
| 318 |
]) |
| 319 |
.run_no_wait(); |
| 320 |
#[cfg(target_vendor = "apple")] |
| 321 |
cmd.delay(1000); |
| 322 |
#[cfg(not(target_vendor = "apple"))] |
| 323 |
cmd.delay(100); |
| 324 |
cmd.kill_with_custom_signal(nix::sys::signal::Signal::SIGINT); |
| 325 |
cmd.make_assertion() |
| 326 |
.is_not_alive() |
| 327 |
.with_current_output() |
| 328 |
.stdout_contains("got_int"); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn test_foreground_signal0_kill_after() { |
| 333 |
let (ts, bin) = scenario_with_bin(); |
| 334 |
ts.ucmd() |
| 335 |
.args(&["--foreground", "-s0", "-k.1", ".1", &bin, "sleep", "10"]) |
| 336 |
.fails_with_code(137); |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 341 |
fn test_realtime_signal_names() { |
| 342 |
// timeout should accept RTMIN and RTMAX as valid signal names |
| 343 |
new_ucmd!() |
| 344 |
.args(&["-v", "-s", "RTMAX", ".1", "sleep", "1"]) |
| 345 |
.fails() |
| 346 |
.stderr_contains("sending signal RTMAX to command"); |
| 347 |
new_ucmd!() |
| 348 |
.args(&["-v", "-s", "RTMIN", ".1", "sleep", "1"]) |
| 349 |
.fails() |
| 350 |
.stderr_contains("sending signal RTMIN to command"); |
| 351 |
new_ucmd!() |
| 352 |
.args(&["-v", "-s", "SIGRTMAX", ".1", "sleep", "1"]) |
| 353 |
.fails() |
| 354 |
.stderr_contains("sending signal RTMAX to command"); |
| 355 |
} |
| 356 |
|
| 357 |
/// The whole process tree must be timed out in non-foreground mode, not just |
| 358 |
/// the direct child: a grandchild sequencer (an inner `cmd.exe`) would |
| 359 |
/// otherwise survive the timeout and create the marker file. |
| 360 |
#[test] |
| 361 |
#[cfg(windows)] |
| 362 |
fn test_windows_kills_process_tree() { |
| 363 |
let (ts, bin) = scenario_with_bin(); |
| 364 |
let at = &ts.fixtures; |
| 365 |
at.write( |
| 366 |
"tree_grandchild.bat", |
| 367 |
&format!("@echo off\r\n\"{bin}\" sleep 1\r\n\"{bin}\" touch tree_marker\r\n"), |
| 368 |
); |
| 369 |
// Outer cmd is timeout's direct child; inner cmd (running the batch file) |
| 370 |
// is the grandchild doing the work. |
| 371 |
ts.ucmd() |
| 372 |
.args(&[".3", "cmd", "/c", "cmd", "/c", "tree_grandchild.bat"]) |
| 373 |
.fails_with_code(124); |
| 374 |
// Give a surviving grandchild ample time to reach the `touch`. |
| 375 |
std::thread::sleep(Duration::from_millis(2500)); |
| 376 |
assert!( |
| 377 |
!at.file_exists("tree_marker"), |
| 378 |
"grandchild survived the process-tree kill" |
| 379 |
); |
| 380 |
} |
| 381 |
|
| 382 |
/// `-s INT` is delivered as a CTRL_BREAK to the child's group (or termination |
| 383 |
/// without a console); either way the child dies and timeout reports 124. |
| 384 |
#[test] |
| 385 |
#[cfg(windows)] |
| 386 |
fn test_windows_int_signal_kills_child() { |
| 387 |
let (ts, bin) = scenario_with_bin(); |
| 388 |
ts.ucmd() |
| 389 |
.args(&["-v", "-s", "INT", ".1", &bin, "sleep", "10"]) |
| 390 |
.fails_with_code(124) |
| 391 |
.stderr_contains("sending signal INT to command"); |
| 392 |
} |
| 393 |
|
| 394 |
#[test] |
| 395 |
#[cfg(windows)] |
| 396 |
fn test_windows_kill_after() { |
| 397 |
let (ts, bin) = scenario_with_bin(); |
| 398 |
ts.ucmd() |
| 399 |
.args(&["-s0", "-k", ".2", ".2", &bin, "sleep", "10"]) |
| 400 |
.fails_with_code(137); |
| 401 |
} |
| 402 |
|
| 403 |
/// Terminations use exit code 128 + N, so `--preserve-status` reports the |
| 404 |
/// same codes as on unix. |
| 405 |
#[test] |
| 406 |
#[cfg(windows)] |
| 407 |
fn test_windows_preserve_status_signal_codes() { |
| 408 |
let (ts, bin) = scenario_with_bin(); |
| 409 |
ts.ucmd() |
| 410 |
.args(&["-p", "-s", "KILL", ".1", &bin, "sleep", "10"]) |
| 411 |
.fails_with_code(128 + 9); |
| 412 |
ts.ucmd() |
| 413 |
.args(&["-p", "-s", "HUP", ".1", &bin, "sleep", "10"]) |
| 414 |
.fails_with_code(128 + 1); |
| 415 |
} |
| 416 |
|
| 417 |
/// POSIX signal names/numbers are accepted; names unix would reject (the |
| 418 |
/// CRT-only SIGBREAK) are rejected here too. |
| 419 |
#[test] |
| 420 |
#[cfg(windows)] |
| 421 |
fn test_windows_signal_name_parsing() { |
| 422 |
let (ts, bin) = scenario_with_bin(); |
| 423 |
ts.ucmd() |
| 424 |
.args(&["-s", "SIGBREAK", "1", &bin, "true"]) |
| 425 |
.fails_with_code(125) |
| 426 |
.usage_error("'SIGBREAK': invalid signal"); |
| 427 |
ts.ucmd() |
| 428 |
.args(&["-s", "USR1", "1", &bin, "true"]) |
| 429 |
.succeeds(); |
| 430 |
ts.ucmd().args(&["-s", "9", "1", &bin, "true"]).succeeds(); |
| 431 |
} |
| 432 |
|
| 433 |
/// A file that exists but is not executable yields 126, like unix. |
| 434 |
#[test] |
| 435 |
#[cfg(windows)] |
| 436 |
fn test_windows_command_cannot_invoke() { |
| 437 |
let ts = TestScenario::new(util_name!()); |
| 438 |
let at = &ts.fixtures; |
| 439 |
at.touch("not_executable.txt"); |
| 440 |
// The explicit path is needed: a bare filename is searched in PATH (not |
| 441 |
// the current directory) and would fail with 127 instead. |
| 442 |
ts.ucmd() |
| 443 |
.args(&["1", ".\\not_executable.txt"]) |
| 444 |
.fails_with_code(126); |
| 445 |
} |