summaryrefslogtreecommitdiffstats
path: root/usr.bin/timeout/tests/timeout_test.sh
blob: ffddfcafb4bb0f5e97c487bb17b0200461dd47c6 (plain)
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
# $FreeBSD$

atf_test_case nominal
nominal_head()
{
	atf_set "descr" "Basic tests on timeout(1) utility"
}

nominal_body()
{
	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		timeout 5 true
}

atf_test_case time_unit
time_unit_head()
{
	atf_set "descr" "Test parsing the default time unit"
}

time_unit_body()
{
	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		timeout 1d true

	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		timeout 1h true

	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		timeout 1m true

	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		timeout 1s true
}

atf_test_case no_timeout
no_timeout_head()
{
	atf_set "descr" "Test disabled timeout"
}

no_timeout_body()
{
	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		timeout 0 true
}

atf_test_case exit_numbers
exit_numbers_head()
{
	atf_set "descr" "Test exit numbers"
}

exit_numbers_body()
{
	atf_check \
		-o empty \
		-e empty \
		-s exit:2 \
		-x timeout 5 sh -c \'exit 2\'

	atf_check \
		-o empty \
		-e empty \
		-s exit:124 \
		timeout .1 sleep 1

	# With preserv status exit should be 128 + TERM aka 143
	atf_check \
		-o empty \
		-e empty \
		-s exit:143 \
		timeout --preserve-status .1 sleep 10

	atf_check \
		-o empty \
		-e empty \
		-s exit:124 \
		timeout -s1 -k1 .1 sleep 10

	atf_check \
		-o empty \
		-e empty \
		-s exit:0 \
		-x sh -c 'trap "" CHLD; exec timeout 10 true'
}

atf_test_case with_a_child
with_a_child_head()
{
	atf_set "descr" "When starting with a child (coreutils bug#9098)"
}

with_a_child_body()
{
	out=$(sleep .1 & exec timeout .5 sh -c 'sleep 2; echo foo')
	status=$?
	test "$out" = "" && test $status = 124 || atf_fail

}

atf_test_case invalid_timeout
invalid_timeout_head()
{
	atf_set "descr" "Invalid timeout"
}

invalid_timeout_body()
{
	atf_check \
		-o empty \
		-e inline:"timeout: invalid duration\n" \
		-s exit:125 \
		timeout invalid sleep 0

	atf_check \
		-o empty \
		-e inline:"timeout: invalid duration\n" \
		-s exit:125 \
		timeout --kill-after=invalid 1 sleep 0

	atf_check \
		-o empty \
		-e inline:"timeout: invalid duration\n" \
		-s exit:125 \
		timeout 42D sleep 0

	atf_check \
		-o empty \
		-e inline:"timeout: invalid duration\n" \
		-s exit:125 \
		timeout 999999999999999999999999999999999999999999999999999999999999d sleep 0

	atf_check \
		-o empty \
		-e inline:"timeout: invalid duration\n" \
		-s exit:125 \
		timeout 2.34e+5d sleep 0
}

atf_test_case invalid_signal
invalid_signal_head()
{
	atf_set "descr" "Invalid signal"
}

invalid_signal_body()
{
	atf_check \
		-o empty \
		-e inline:"timeout: invalid signal\n" \
		-s exit:125 \
		timeout --signal=invalid 1 sleep 0
}

atf_test_case invalid_command
invalid_command_head()
{
	atf_set "descr" "Invalid command"
}

invalid_command_body()
{
	atf_check \
		-o empty \
		-e inline:"timeout: exec(.): Permission denied\n" \
		-s exit:126 \
		timeout 10 .
}

atf_test_case no_such_command
no_such_command_head()
{
	atf_set "descr" "No such command"
}

no_such_command_body()
{
	atf_check \
		-o empty \
		-e inline:"timeout: exec(enoexists): No such file or directory\n" \
		-s exit:127 \
		timeout 10 enoexists
}

atf_init_test_cases()
{
	atf_add_test_case nominal
	atf_add_test_case time_unit
	atf_add_test_case no_timeout
	atf_add_test_case exit_numbers
	atf_add_test_case with_a_child
	atf_add_test_case invalid_timeout
	atf_add_test_case invalid_signal
	atf_add_test_case invalid_command
	atf_add_test_case no_such_command
}
OpenPOWER on IntegriCloud