xref: /freebsd/bin/timeout/tests/timeout_test.sh (revision fd4fafa8732203a3e20f5404a65de83d37687e3a)
1
2atf_test_case nominal
3nominal_head()
4{
5	atf_set "descr" "Basic tests on timeout(1) utility"
6}
7
8nominal_body()
9{
10	atf_check \
11		-o empty \
12		-e empty \
13		-s exit:0 \
14		timeout 5 true
15}
16
17atf_test_case time_unit
18time_unit_head()
19{
20	atf_set "descr" "Test parsing the default time unit"
21}
22
23time_unit_body()
24{
25	atf_check \
26		-o empty \
27		-e empty \
28		-s exit:0 \
29		timeout 1d true
30
31	atf_check \
32		-o empty \
33		-e empty \
34		-s exit:0 \
35		timeout 1h true
36
37	atf_check \
38		-o empty \
39		-e empty \
40		-s exit:0 \
41		timeout 1m true
42
43	atf_check \
44		-o empty \
45		-e empty \
46		-s exit:0 \
47		timeout 1s true
48}
49
50atf_test_case no_timeout
51no_timeout_head()
52{
53	atf_set "descr" "Test disabled timeout"
54}
55
56no_timeout_body()
57{
58	atf_check \
59		-o empty \
60		-e empty \
61		-s exit:0 \
62		timeout 0 true
63}
64
65atf_test_case exit_numbers
66exit_numbers_head()
67{
68	atf_set "descr" "Test exit numbers"
69}
70
71exit_numbers_body()
72{
73	atf_check \
74		-o empty \
75		-e empty \
76		-s exit:2 \
77		-x timeout 5 sh -c \'exit 2\'
78
79	atf_check \
80		-o empty \
81		-e empty \
82		-s exit:124 \
83		timeout .1 sleep 1
84
85	# With preserve status exit should be 128 + TERM aka 143
86	atf_check \
87		-o empty \
88		-e empty \
89		-s exit:143 \
90		timeout --preserve-status .1 sleep 10
91
92	atf_check \
93		-o empty \
94		-e empty \
95		-s exit:124 \
96		timeout -s1 -k1 .1 sleep 10
97
98	atf_check \
99		-o empty \
100		-e empty \
101		-s exit:0 \
102		-x sh -c 'trap "" CHLD; exec timeout 10 true'
103}
104
105atf_test_case with_a_child
106with_a_child_head()
107{
108	atf_set "descr" "When starting with a child (coreutils bug#9098)"
109}
110
111with_a_child_body()
112{
113	out=$(sleep .1 & exec timeout .5 sh -c 'sleep 2; echo foo')
114	status=$?
115	test "$out" = "" && test $status = 124 || atf_fail
116
117}
118
119atf_test_case invalid_timeout
120invalid_timeout_head()
121{
122	atf_set "descr" "Invalid timeout"
123}
124
125invalid_timeout_body()
126{
127	atf_check \
128		-o empty \
129		-e inline:"timeout: invalid duration\n" \
130		-s exit:125 \
131		timeout invalid sleep 0
132
133	atf_check \
134		-o empty \
135		-e inline:"timeout: invalid duration\n" \
136		-s exit:125 \
137		timeout --kill-after=invalid 1 sleep 0
138
139	atf_check \
140		-o empty \
141		-e inline:"timeout: invalid duration\n" \
142		-s exit:125 \
143		timeout 42D sleep 0
144
145	atf_check \
146		-o empty \
147		-e inline:"timeout: invalid duration\n" \
148		-s exit:125 \
149		timeout 999999999999999999999999999999999999999999999999999999999999d sleep 0
150
151	atf_check \
152		-o empty \
153		-e inline:"timeout: invalid duration\n" \
154		-s exit:125 \
155		timeout 2.34e+5d sleep 0
156}
157
158atf_test_case invalid_signal
159invalid_signal_head()
160{
161	atf_set "descr" "Invalid signal"
162}
163
164invalid_signal_body()
165{
166	atf_check \
167		-o empty \
168		-e inline:"timeout: invalid signal\n" \
169		-s exit:125 \
170		timeout --signal=invalid 1 sleep 0
171}
172
173atf_test_case invalid_command
174invalid_command_head()
175{
176	atf_set "descr" "Invalid command"
177}
178
179invalid_command_body()
180{
181	atf_check \
182		-o empty \
183		-e inline:"timeout: exec(.): Permission denied\n" \
184		-s exit:126 \
185		timeout 10 .
186}
187
188atf_test_case no_such_command
189no_such_command_head()
190{
191	atf_set "descr" "No such command"
192}
193
194no_such_command_body()
195{
196	atf_check \
197		-o empty \
198		-e inline:"timeout: exec(enoexists): No such file or directory\n" \
199		-s exit:127 \
200		timeout 10 enoexists
201}
202
203atf_init_test_cases()
204{
205	atf_add_test_case nominal
206	atf_add_test_case time_unit
207	atf_add_test_case no_timeout
208	atf_add_test_case exit_numbers
209	atf_add_test_case with_a_child
210	atf_add_test_case invalid_timeout
211	atf_add_test_case invalid_signal
212	atf_add_test_case invalid_command
213	atf_add_test_case no_such_command
214}
215