atf-check_test.sh revision 275988
1# Copyright (c) 2008 The NetBSD Foundation, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26# The Atf_Check and Atf-Shell variables are set by atf-sh.
27
28h_pass()
29{
30    cmd="$1"; shift
31
32    echo "Running [atf-check $*] against [${cmd}]"
33
34    cat >script.sh <<EOF
35#! ${Atf_Shell}
36${cmd}
37EOF
38    chmod +x script.sh
39
40    if ! ${Atf_Check} "${@}" ./script.sh >tmp; then
41        cat tmp
42        atf_fail "atf-check failed"
43    fi
44}
45
46h_fail()
47{
48    cmd="$1"; shift
49
50    echo "Running [atf-check $*] against [${cmd}]"
51
52    cat >script.sh <<EOF
53#! ${Atf_Shell}
54${cmd}
55EOF
56    chmod +x script.sh
57
58    if ${Atf_Check} "${@}" ./script.sh 2>tmp; then
59        cat tmp
60        atf_fail "atf-check succeeded but should fail"
61    fi
62}
63
64atf_test_case sflag_eq_ne
65sflag_eq_ne_head()
66{
67    atf_set "descr" "Tests for the -s option using the 'eq' and 'ne' qualifiers"
68}
69sflag_eq_ne_body()
70{
71    h_pass "true" -s eq:0
72    h_pass "false" -s ne:0
73    h_pass "exit 255" -s eq:255
74    h_pass "exit 0" -s ne:255
75
76    h_fail "exit 256" -s eq:256
77    h_fail "exit -1" -s eq:-1
78    h_fail "true" -s ne:256
79    h_fail "true" -s ne:-1
80}
81
82atf_test_case sflag_exit
83sflag_exit_head()
84{
85    atf_set "descr" "Tests for the -s option using the 'exit' qualifier"
86}
87sflag_exit_body()
88{
89    h_pass 'true' -s exit:0
90    h_pass 'false' -s not-exit:0
91    h_pass 'exit 255' -s exit:255
92    h_pass 'exit 0' -s not-exit:255
93
94    h_fail 'exit 256' -s exit:256
95    h_fail 'exit -1' -s exit:-1
96    h_fail 'true' -s not-exit:256
97    h_fail 'true' -s not-exit:-1
98
99    h_pass 'true' -s exit
100    h_pass 'false' -s exit
101    if ${Atf_Check} -s exit -x 'kill $$'; then
102        atf_fail "Signal detected as clean exit"
103    fi
104}
105
106atf_test_case sflag_ignore
107sflag_ignore_head()
108{
109    atf_set "descr" "Tests for the -s option using the 'ignore' qualifier"
110}
111sflag_ignore_body()
112{
113    h_pass 'true' -s ignore
114    h_pass 'false' -s ignore
115    if ${Atf_Check} -s ignored -x 'kill $$'; then
116        atf_fail "Signal not ignored"
117    fi
118}
119
120atf_test_case sflag_signal
121sflag_signal_head()
122{
123    atf_set "descr" "Tests for the -s option using the 'signal' qualifier"
124}
125sflag_signal_body()
126{
127    ${Atf_Check} -s signal:hup -x 'kill -1 $$' || atf_fail "Signal not detected"
128    ${Atf_Check} -s signal:sighup -x 'kill -1 $$' || atf_fail "Signal not" \
129        "detected"
130    ${Atf_Check} -s signal:1 -x 'kill -1 $$' || atf_fail "Signal not detected"
131    ${Atf_Check} -s signal -x 'kill -1 $$' || atf_fail "Signal not detected"
132
133    ${Atf_Check} -s not-signal:kill -x 'kill -9 $$' && \
134        atf_fail "not-signal:kill matched kill -9"
135    ${Atf_Check} -s not-signal:kill -x 'kill -1 $$' || \
136        atf_fail "not-signal:kill did not match kill -1"
137
138    h_fail 'true' -s signal
139    h_fail 'false' -s signal
140}
141
142atf_test_case xflag
143xflag_head()
144{
145    atf_set "descr" "Tests for the -x option"
146}
147xflag_body()
148{
149    ${Atf_Check} -s ne:0 -o ignore -e ignore "echo foo 2>&1" || \
150        atf_fail "Shell command succeeded without -x"
151
152    ${Atf_Check} -e inline:"foo\n" -x "echo foo 1>&2" || \
153        atf_fail "Cannot run command with -x"
154
155    ${Atf_Check} -o inline:"foo\n" -x echo foo || \
156        atf_fail "Using -x does not respect all provided arguments"
157}
158
159atf_test_case oflag_empty
160oflag_empty_head()
161{
162    atf_set "descr" "Tests for the -o option using the 'empty' argument"
163}
164oflag_empty_body()
165{
166    h_pass "true" -o empty
167    h_fail "echo foo" -o empty
168}
169
170atf_test_case oflag_ignore
171oflag_ignore_head()
172{
173    atf_set "descr" "Tests for the -o option using the 'ignore' argument"
174}
175oflag_ignore_body()
176{
177    h_pass "true" -o ignore
178    h_pass "echo foo" -o ignore
179}
180
181atf_test_case oflag_file
182oflag_file_head()
183{
184    atf_set "descr" "Tests for the -o option using the 'file:' argument"
185}
186oflag_file_body()
187{
188    touch empty
189    h_pass "true" -o file:empty
190
191    echo foo >text
192    h_pass "echo foo" -o file:text
193    h_fail "echo bar" -o file:text
194
195    dd if=/dev/urandom of=bin bs=1k count=10
196    h_pass "cat bin" -o file:bin
197}
198
199atf_test_case oflag_inline
200oflag_inline_head()
201{
202    atf_set "descr" "Tests for the -o option using the 'inline:' argument"
203}
204oflag_inline_body()
205{
206    h_pass "true" -o inline:
207    h_pass "echo foo bar" -o inline:"foo bar\n"
208    h_pass "printf 'foo bar'" -o inline:"foo bar"
209    h_pass "printf '\t\n\t\n'" -o inline:"\t\n\t\n"
210    h_pass "printf '\a\b\033\f\n\r\t\v'" -o inline:"\a\b\e\f\n\r\t\v"
211    h_pass "printf '\011\022\033\012'" -o inline:"\011\022\033\012"
212
213    h_fail "echo foo bar" -o inline:"foo bar"
214    h_fail "echo -n foo bar" -o inline:"foo bar\n"
215}
216
217atf_test_case oflag_match
218oflag_match_head()
219{
220    atf_set "descr" "Tests for the -o option using the 'match:' argument"
221}
222oflag_match_body()
223{
224    h_pass "printf no-newline" -o "match:^no-newline"
225    h_pass "echo line1; echo foo bar" -o "match:^foo"
226    h_pass "echo foo bar" -o "match:o b"
227    h_fail "echo foo bar" -o "match:baz"
228    h_fail "echo foo bar" -o "match:^bar"
229}
230
231atf_test_case oflag_save
232oflag_save_head()
233{
234    atf_set "descr" "Tests for the -o option using the 'save:' argument"
235}
236oflag_save_body()
237{
238    h_pass "echo foo" -o save:out
239    echo foo >exp
240    cmp -s out exp || atf_fail "Saved output does not match expected results"
241}
242
243atf_test_case oflag_multiple
244oflag_multiple_head()
245{
246    atf_set "descr" "Tests for multiple occurrences of the -o option"
247}
248oflag_multiple_body()
249{
250    h_pass "echo foo bar" -o match:foo -o match:bar
251    h_pass "echo foo; echo bar" -o match:foo -o match:bar
252    h_fail "echo foo baz" -o match:bar -o match:foo
253    h_fail "echo foo; echo baz" -o match:bar -o match:foo
254}
255
256atf_test_case oflag_negated
257oflag_negated_head()
258{
259    atf_set "descr" "Tests for negated occurrences of the -o option"
260}
261oflag_negated_body()
262{
263    h_fail "echo foo" -o empty
264    h_pass "echo foo" -o not-empty
265
266    h_pass "echo foo bar" -o match:foo
267    h_fail "echo foo bar" -o not-match:foo
268}
269
270atf_test_case eflag_empty
271eflag_empty_head()
272{
273    atf_set "descr" "Tests for the -e option using the 'empty' argument"
274}
275eflag_empty_body()
276{
277    h_pass "true 1>&2" -e empty
278    h_fail "echo foo 1>&2" -e empty
279}
280
281atf_test_case eflag_ignore
282eflag_ignore_head()
283{
284    atf_set "descr" "Tests for the -e option using the 'ignore' argument"
285}
286eflag_ignore_body()
287{
288    h_pass "true 1>&2" -e ignore
289    h_pass "echo foo 1>&2" -e ignore
290}
291
292atf_test_case eflag_file
293eflag_file_head()
294{
295    atf_set "descr" "Tests for the -e option using the 'file:' argument"
296}
297eflag_file_body()
298{
299    touch empty
300    h_pass "true 1>&2" -e file:empty
301
302    echo foo >text
303    h_pass "echo foo 1>&2" -e file:text
304    h_fail "echo bar 1>&2" -e file:text
305
306    dd if=/dev/urandom of=bin bs=1k count=10
307    h_pass "cat bin 1>&2" -e file:bin
308}
309
310atf_test_case eflag_inline
311eflag_inline_head()
312{
313    atf_set "descr" "Tests for the -e option using the 'inline:' argument"
314}
315eflag_inline_body()
316{
317    h_pass "true 1>&2" -e inline:
318    h_pass "echo foo bar 1>&2" -e inline:"foo bar\n"
319    h_pass "printf 'foo bar' 1>&2" -e inline:"foo bar"
320    h_pass "printf '\t\n\t\n' 1>&2" -e inline:"\t\n\t\n"
321    h_pass "printf '\a\b\033\f\n\r\t\v' 1>&2" -e inline:"\a\b\e\f\n\r\t\v"
322    h_pass "printf '\011\022\033\012' 1>&2" -e inline:"\011\022\033\012"
323
324    h_fail "echo foo bar 1>&2" -e inline:"foo bar"
325    h_fail "echo -n foo bar 1>&2" -e inline:"foo bar\n"
326}
327
328atf_test_case eflag_save
329eflag_save_head()
330{
331    atf_set "descr" "Tests for the -e option using the 'save:' argument"
332}
333eflag_save_body()
334{
335    h_pass "echo foo 1>&2" -e save:out
336    echo foo >exp
337    cmp -s out exp || atf_fail "Saved output does not match expected results"
338}
339
340atf_test_case eflag_match
341eflag_match_head()
342{
343    atf_set "descr" "Tests for the -e option using the 'match:' argument"
344}
345eflag_match_body()
346{
347    h_pass "printf no-newline 1>&2" -e "match:^no-newline"
348    h_pass "echo line1 1>&2; echo foo bar 1>&2" -e "match:^foo"
349    h_pass "echo foo bar 1>&2" -e "match:o b"
350    h_fail "echo foo bar 1>&2" -e "match:baz"
351    h_fail "echo foo bar 1>&2" -e "match:^bar"
352}
353
354atf_test_case eflag_multiple
355eflag_multiple_head()
356{
357    atf_set "descr" "Tests for multiple occurrences of the -e option"
358}
359eflag_multiple_body()
360{
361    h_pass "echo foo bar 1>&2" -e match:foo -e match:bar
362    h_pass "echo foo 1>&2; echo bar 1>&2" -e match:foo -e match:bar
363    h_fail "echo foo baz 1>&2" -e match:bar -e match:foo
364    h_fail "echo foo 1>&2; echo baz 1>&2" -e match:bar -e match:foo
365}
366
367atf_test_case eflag_negated
368eflag_negated_head()
369{
370    atf_set "descr" "Tests for negated occurrences of the -e option"
371}
372eflag_negated_body()
373{
374    h_fail "echo foo 1>&2" -e empty
375    h_pass "echo foo 1>&2" -e not-empty
376
377    h_pass "echo foo bar 1>&2" -e match:foo
378    h_fail "echo foo bar 1>&2" -e not-match:foo
379}
380
381atf_test_case stdin
382stdin_head()
383{
384    atf_set "descr" "Tests that stdin is preserved"
385}
386stdin_body()
387{
388    echo "hello" | ${Atf_Check} -o match:"hello" cat || \
389        atf_fail "atf-check does not seem to respect stdin"
390}
391
392atf_test_case invalid_umask
393invalid_umask_head()
394{
395    atf_set "descr" "Tests for a correct error condition if the umask is" \
396            "too restrictive"
397}
398invalid_umask_body()
399{
400    umask 0222
401    ${Atf_Check} false 2>stderr && \
402        atf_fail "atf-check returned 0 but it should have failed"
403    cat stderr
404    grep 'temporary.*current umask.*0222' stderr >/dev/null || \
405        atf_fail "atf-check did not report an error related to the" \
406                 "current umask"
407}
408
409atf_init_test_cases()
410{
411    atf_add_test_case sflag_eq_ne
412    atf_add_test_case sflag_exit
413    atf_add_test_case sflag_ignore
414    atf_add_test_case sflag_signal
415
416    atf_add_test_case xflag
417
418    atf_add_test_case oflag_empty
419    atf_add_test_case oflag_ignore
420    atf_add_test_case oflag_file
421    atf_add_test_case oflag_inline
422    atf_add_test_case oflag_match
423    atf_add_test_case oflag_save
424    atf_add_test_case oflag_multiple
425    atf_add_test_case oflag_negated
426
427    atf_add_test_case eflag_empty
428    atf_add_test_case eflag_ignore
429    atf_add_test_case eflag_file
430    atf_add_test_case eflag_inline
431    atf_add_test_case eflag_match
432    atf_add_test_case eflag_save
433    atf_add_test_case eflag_multiple
434    atf_add_test_case eflag_negated
435
436    atf_add_test_case stdin
437
438    atf_add_test_case invalid_umask
439}
440
441# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
442