2023-08-21 08:49:27 +00:00
|
|
|
// 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.
|
2022-03-17 23:03:26 +00:00
|
|
|
// spell-checker:ignore dont
|
2023-03-20 13:51:19 +00:00
|
|
|
use crate::common::util::TestScenario;
|
2021-02-23 09:21:01 +00:00
|
|
|
|
2022-09-10 16:38:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_arg() {
|
|
|
|
new_ucmd!().arg("--definitely-invalid").fails().code_is(125);
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:21:01 +00:00
|
|
|
// FIXME: this depends on the system having true and false in PATH
|
|
|
|
// the best solution is probably to generate some test binaries that we can call for any
|
|
|
|
// utility that requires executing another program (kill, for instance)
|
|
|
|
#[test]
|
2021-05-30 05:10:54 +00:00
|
|
|
fn test_subcommand_return_code() {
|
2021-03-13 22:30:32 +00:00
|
|
|
new_ucmd!().arg("1").arg("true").succeeds();
|
2021-02-23 09:21:01 +00:00
|
|
|
|
2023-01-01 14:33:02 +00:00
|
|
|
new_ucmd!().arg("1").arg("false").run().code_is(1);
|
2021-02-23 09:21:01 +00:00
|
|
|
}
|
2021-06-10 14:59:06 +00:00
|
|
|
|
2022-03-20 19:16:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_time_interval() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["xyz", "sleep", "0"])
|
|
|
|
.fails()
|
2022-03-26 14:22:23 +00:00
|
|
|
.code_is(125)
|
|
|
|
.usage_error("invalid time interval 'xyz'");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_kill_after() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-k", "xyz", "1", "sleep", "0"])
|
|
|
|
.fails()
|
|
|
|
.code_is(125)
|
2022-03-20 19:16:03 +00:00
|
|
|
.usage_error("invalid time interval 'xyz'");
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:59:06 +00:00
|
|
|
#[test]
|
|
|
|
fn test_command_with_args() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["1700", "echo", "-n", "abcd"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("abcd");
|
|
|
|
}
|
2021-06-10 16:34:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verbose() {
|
2022-04-02 08:47:37 +00:00
|
|
|
for verbose_flag in ["-v", "--verbose"] {
|
2021-06-10 16:34:05 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.args(&[verbose_flag, ".1", "sleep", "10"])
|
|
|
|
.fails()
|
2023-01-05 20:09:15 +00:00
|
|
|
.stderr_only("timeout: sending signal TERM to command 'sleep'\n");
|
2021-06-10 16:34:05 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.args(&[verbose_flag, "-s0", "-k.1", ".1", "sleep", "10"])
|
|
|
|
.fails()
|
2023-01-05 20:09:15 +00:00
|
|
|
.stderr_only("timeout: sending signal EXIT to command 'sleep'\ntimeout: sending signal KILL to command 'sleep'\n");
|
2021-06-10 16:34:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 16:51:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zero_timeout() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-v", "0", "sleep", ".1"])
|
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-v", "0", "-s0", "-k0", "sleep", ".1"])
|
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
}
|
2022-03-06 22:17:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_command_empty_args() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["", ""])
|
|
|
|
.fails()
|
|
|
|
.stderr_contains("timeout: empty string");
|
|
|
|
}
|
2022-03-12 03:40:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_preserve_status() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--preserve-status", ".1", "sleep", "10"])
|
|
|
|
.fails()
|
|
|
|
// 128 + SIGTERM = 128 + 15
|
|
|
|
.code_is(128 + 15)
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
}
|
2022-03-17 23:03:26 +00:00
|
|
|
|
2024-03-12 18:05:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_preserve_status_even_when_send_signal() {
|
|
|
|
// When sending CONT signal, process doesn't get killed or stopped.
|
|
|
|
// So, expected result is success and code 0.
|
2024-05-07 19:32:24 +00:00
|
|
|
for cont_spelling in ["CONT", "cOnT", "SIGcont"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-s", cont_spelling, "--preserve-status", ".1", "sleep", "2"])
|
|
|
|
.succeeds()
|
|
|
|
.code_is(0)
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
}
|
2024-03-12 18:05:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 23:03:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_dont_overflow() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["9223372036854775808d", "sleep", "0"])
|
|
|
|
.succeeds()
|
|
|
|
.code_is(0)
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-k", "9223372036854775808d", "10", "sleep", "0"])
|
|
|
|
.succeeds()
|
|
|
|
.code_is(0)
|
|
|
|
.no_stderr()
|
|
|
|
.no_stdout();
|
|
|
|
}
|
2022-03-20 19:35:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_negative_interval() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--", "-1", "sleep", "0"])
|
|
|
|
.fails()
|
|
|
|
.usage_error("invalid time interval '-1'");
|
|
|
|
}
|
2022-03-23 01:02:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_signal() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-s", "invalid", "1", "sleep", "0"])
|
|
|
|
.fails()
|
|
|
|
.usage_error("'invalid': invalid signal");
|
|
|
|
}
|
2022-03-26 14:18:30 +00:00
|
|
|
|
2023-02-22 20:24:08 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_multi_byte_characters() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["10€", "sleep", "0"])
|
|
|
|
.fails()
|
|
|
|
.usage_error("invalid time interval '10€'");
|
|
|
|
}
|
|
|
|
|
2022-03-26 14:18:30 +00:00
|
|
|
/// Test that the long form of the `--kill-after` argument is recognized.
|
|
|
|
#[test]
|
|
|
|
fn test_kill_after_long() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--kill-after=1", "1", "sleep", "0"])
|
|
|
|
.succeeds()
|
|
|
|
.no_stdout()
|
|
|
|
.no_stderr();
|
|
|
|
}
|
2023-02-01 07:39:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_kill_subprocess() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&[
|
|
|
|
// Make sure the CI can spawn the subprocess.
|
|
|
|
"10",
|
|
|
|
"sh",
|
|
|
|
"-c",
|
2024-03-12 18:08:51 +00:00
|
|
|
"trap 'echo inside_trap' TERM; sleep 30",
|
2023-02-01 07:39:07 +00:00
|
|
|
])
|
|
|
|
.fails()
|
|
|
|
.code_is(124)
|
2024-03-12 18:08:51 +00:00
|
|
|
.stdout_contains("inside_trap")
|
2023-02-01 07:39:07 +00:00
|
|
|
.stderr_contains("Terminated");
|
|
|
|
}
|