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