atf-check_test.sh revision 240116
1240116Smarcel#
2240116Smarcel# Automated Testing Framework (atf)
3240116Smarcel#
4240116Smarcel# Copyright (c) 2008 The NetBSD Foundation, Inc.
5240116Smarcel# All rights reserved.
6240116Smarcel#
7240116Smarcel# Redistribution and use in source and binary forms, with or without
8240116Smarcel# modification, are permitted provided that the following conditions
9240116Smarcel# are met:
10240116Smarcel# 1. Redistributions of source code must retain the above copyright
11240116Smarcel#    notice, this list of conditions and the following disclaimer.
12240116Smarcel# 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel#    notice, this list of conditions and the following disclaimer in the
14240116Smarcel#    documentation and/or other materials provided with the distribution.
15240116Smarcel#
16240116Smarcel# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel#
29240116Smarcel
30240116Smarcel# The Atf_Check and Atf-Shell variables are set by atf-sh.
31240116Smarcel
32240116Smarcelh_pass()
33240116Smarcel{
34240116Smarcel    cmd="$1"; shift
35240116Smarcel
36240116Smarcel    echo "Running [atf-check $*] against [${cmd}]"
37240116Smarcel
38240116Smarcel    cat >script.sh <<EOF
39240116Smarcel#! ${Atf_Shell}
40240116Smarcel${cmd}
41240116SmarcelEOF
42240116Smarcel    chmod +x script.sh
43240116Smarcel
44240116Smarcel    if ! ${Atf_Check} "${@}" ./script.sh >tmp; then
45240116Smarcel        cat tmp
46240116Smarcel        atf_fail "atf-check failed"
47240116Smarcel    fi
48240116Smarcel}
49240116Smarcel
50240116Smarcelh_fail()
51240116Smarcel{
52240116Smarcel    cmd="$1"; shift
53240116Smarcel
54240116Smarcel    echo "Running [atf-check $*] against [${cmd}]"
55240116Smarcel
56240116Smarcel    cat >script.sh <<EOF
57240116Smarcel#! ${Atf_Shell}
58240116Smarcel${cmd}
59240116SmarcelEOF
60240116Smarcel    chmod +x script.sh
61240116Smarcel
62240116Smarcel    if ${Atf_Check} "${@}" ./script.sh 2>tmp; then
63240116Smarcel        cat tmp
64240116Smarcel        atf_fail "atf-check succeeded but should fail"
65240116Smarcel    fi
66240116Smarcel}
67240116Smarcel
68240116Smarcelatf_test_case sflag_eq_ne
69240116Smarcelsflag_eq_ne_head()
70240116Smarcel{
71240116Smarcel    atf_set "descr" "Tests for the -s option using the 'eq' and 'ne' qualifiers"
72240116Smarcel}
73240116Smarcelsflag_eq_ne_body()
74240116Smarcel{
75240116Smarcel    h_pass "true" -s eq:0
76240116Smarcel    h_pass "false" -s ne:0
77240116Smarcel    h_pass "exit 255" -s eq:255
78240116Smarcel    h_pass "exit 0" -s ne:255
79240116Smarcel
80240116Smarcel    h_fail "exit 256" -s eq:256
81240116Smarcel    h_fail "exit -1" -s eq:-1
82240116Smarcel    h_fail "true" -s ne:256
83240116Smarcel    h_fail "true" -s ne:-1
84240116Smarcel}
85240116Smarcel
86240116Smarcelatf_test_case sflag_exit
87240116Smarcelsflag_exit_head()
88240116Smarcel{
89240116Smarcel    atf_set "descr" "Tests for the -s option using the 'exit' qualifier"
90240116Smarcel}
91240116Smarcelsflag_exit_body()
92240116Smarcel{
93240116Smarcel    h_pass 'true' -s exit:0
94240116Smarcel    h_pass 'false' -s not-exit:0
95240116Smarcel    h_pass 'exit 255' -s exit:255
96240116Smarcel    h_pass 'exit 0' -s not-exit:255
97240116Smarcel
98240116Smarcel    h_fail 'exit 256' -s exit:256
99240116Smarcel    h_fail 'exit -1' -s exit:-1
100240116Smarcel    h_fail 'true' -s not-exit:256
101240116Smarcel    h_fail 'true' -s not-exit:-1
102240116Smarcel
103240116Smarcel    h_pass 'true' -s exit
104240116Smarcel    h_pass 'false' -s exit
105240116Smarcel    if ${Atf_Check} -s exit -x 'kill $$'; then
106240116Smarcel        atf_fail "Signal detected as clean exit"
107240116Smarcel    fi
108240116Smarcel}
109240116Smarcel
110240116Smarcelatf_test_case sflag_ignore
111240116Smarcelsflag_ignore_head()
112240116Smarcel{
113240116Smarcel    atf_set "descr" "Tests for the -s option using the 'ignore' qualifier"
114240116Smarcel}
115240116Smarcelsflag_ignore_body()
116240116Smarcel{
117240116Smarcel    h_pass 'true' -s ignore
118240116Smarcel    h_pass 'false' -s ignore
119240116Smarcel    if ${Atf_Check} -s ignored -x 'kill $$'; then
120240116Smarcel        atf_fail "Signal not ignored"
121240116Smarcel    fi
122240116Smarcel}
123240116Smarcel
124240116Smarcelatf_test_case sflag_signal
125240116Smarcelsflag_signal_head()
126240116Smarcel{
127240116Smarcel    atf_set "descr" "Tests for the -s option using the 'signal' qualifier"
128240116Smarcel}
129240116Smarcelsflag_signal_body()
130240116Smarcel{
131240116Smarcel    ${Atf_Check} -s signal:hup -x 'kill -1 $$' || atf_fail "Signal not detected"
132240116Smarcel    ${Atf_Check} -s signal:sighup -x 'kill -1 $$' || atf_fail "Signal not" \
133240116Smarcel        "detected"
134240116Smarcel    ${Atf_Check} -s signal:1 -x 'kill -1 $$' || atf_fail "Signal not detected"
135240116Smarcel    ${Atf_Check} -s signal -x 'kill -1 $$' || atf_fail "Signal not detected"
136240116Smarcel
137240116Smarcel    ${Atf_Check} -s not-signal:kill -x 'kill -9 $$' && \
138240116Smarcel        atf_fail "not-signal:kill matched kill -9"
139240116Smarcel    ${Atf_Check} -s not-signal:kill -x 'kill -1 $$' || \
140240116Smarcel        atf_fail "not-signal:kill did not match kill -1"
141240116Smarcel
142240116Smarcel    h_fail 'true' -s signal
143240116Smarcel    h_fail 'false' -s signal
144240116Smarcel}
145240116Smarcel
146240116Smarcelatf_test_case xflag
147240116Smarcelxflag_head()
148240116Smarcel{
149240116Smarcel    atf_set "descr" "Tests for the -x option"
150240116Smarcel}
151240116Smarcelxflag_body()
152240116Smarcel{
153240116Smarcel    ${Atf_Check} -s ne:0 -o ignore -e ignore "echo foo 2>&1" || \
154240116Smarcel        atf_fail "Shell command succeeded without -x"
155240116Smarcel
156240116Smarcel    ${Atf_Check} -e inline:"foo\n" -x "echo foo 1>&2" || \
157240116Smarcel        atf_fail "Cannot run command with -x"
158240116Smarcel
159240116Smarcel    ${Atf_Check} -o inline:"foo\n" -x echo foo || \
160240116Smarcel        atf_fail "Using -x does not respect all provided arguments"
161240116Smarcel}
162240116Smarcel
163240116Smarcelatf_test_case oflag_empty
164240116Smarceloflag_empty_head()
165240116Smarcel{
166240116Smarcel    atf_set "descr" "Tests for the -o option using the 'empty' argument"
167240116Smarcel}
168240116Smarceloflag_empty_body()
169240116Smarcel{
170240116Smarcel    h_pass "true" -o empty
171240116Smarcel    h_fail "echo foo" -o empty
172240116Smarcel}
173240116Smarcel
174240116Smarcelatf_test_case oflag_ignore
175240116Smarceloflag_ignore_head()
176240116Smarcel{
177240116Smarcel    atf_set "descr" "Tests for the -o option using the 'ignore' argument"
178240116Smarcel}
179240116Smarceloflag_ignore_body()
180240116Smarcel{
181240116Smarcel    h_pass "true" -o ignore
182240116Smarcel    h_pass "echo foo" -o ignore
183240116Smarcel}
184240116Smarcel
185240116Smarcelatf_test_case oflag_file
186240116Smarceloflag_file_head()
187240116Smarcel{
188240116Smarcel    atf_set "descr" "Tests for the -o option using the 'file:' argument"
189240116Smarcel}
190240116Smarceloflag_file_body()
191240116Smarcel{
192240116Smarcel    touch empty
193240116Smarcel    h_pass "true" -o file:empty
194240116Smarcel
195240116Smarcel    echo foo >text
196240116Smarcel    h_pass "echo foo" -o file:text
197240116Smarcel    h_fail "echo bar" -o file:text
198240116Smarcel
199240116Smarcel    dd if=/dev/urandom of=bin bs=1k count=10
200240116Smarcel    h_pass "cat bin" -o file:bin
201240116Smarcel}
202240116Smarcel
203240116Smarcelatf_test_case oflag_inline
204240116Smarceloflag_inline_head()
205240116Smarcel{
206240116Smarcel    atf_set "descr" "Tests for the -o option using the 'inline:' argument"
207240116Smarcel}
208240116Smarceloflag_inline_body()
209240116Smarcel{
210240116Smarcel    h_pass "true" -o inline:
211240116Smarcel    h_pass "echo foo bar" -o inline:"foo bar\n"
212240116Smarcel    h_pass "printf 'foo bar'" -o inline:"foo bar"
213240116Smarcel    h_pass "printf '\t\n\t\n'" -o inline:"\t\n\t\n"
214240116Smarcel    # XXX Ugly hack to workaround the lack of \e in FreeBSD.  Look for a
215240116Smarcel    # nicer solution...
216240116Smarcel    case $(uname) in
217240116Smarcel    Darwin|FreeBSD)
218240116Smarcel        h_pass "printf '\a\b\f\n\r\t\v'" -o inline:"\a\b\f\n\r\t\v"
219240116Smarcel        ;;
220240116Smarcel    *)
221240116Smarcel        h_pass "printf '\a\b\e\f\n\r\t\v'" -o inline:"\a\b\e\f\n\r\t\v"
222240116Smarcel        ;;
223240116Smarcel    esac
224240116Smarcel    h_pass "printf '\011\022\033\012'" -o inline:"\011\022\033\012"
225240116Smarcel
226240116Smarcel    h_fail "echo foo bar" -o inline:"foo bar"
227240116Smarcel    h_fail "echo -n foo bar" -o inline:"foo bar\n"
228240116Smarcel}
229240116Smarcel
230240116Smarcelatf_test_case oflag_match
231240116Smarceloflag_match_head()
232240116Smarcel{
233240116Smarcel    atf_set "descr" "Tests for the -o option using the 'match:' argument"
234240116Smarcel}
235240116Smarceloflag_match_body()
236240116Smarcel{
237240116Smarcel    h_pass "printf no-newline" -o "match:^no-newline"
238240116Smarcel    h_pass "echo line1; echo foo bar" -o "match:^foo"
239240116Smarcel    h_pass "echo foo bar" -o "match:o b"
240240116Smarcel    h_fail "echo foo bar" -o "match:baz"
241240116Smarcel    h_fail "echo foo bar" -o "match:^bar"
242240116Smarcel}
243240116Smarcel
244240116Smarcelatf_test_case oflag_save
245240116Smarceloflag_save_head()
246240116Smarcel{
247240116Smarcel    atf_set "descr" "Tests for the -o option using the 'save:' argument"
248240116Smarcel}
249240116Smarceloflag_save_body()
250240116Smarcel{
251240116Smarcel    h_pass "echo foo" -o save:out
252240116Smarcel    echo foo >exp
253240116Smarcel    cmp -s out exp || atf_fail "Saved output does not match expected results"
254240116Smarcel}
255240116Smarcel
256240116Smarcelatf_test_case oflag_multiple
257240116Smarceloflag_multiple_head()
258240116Smarcel{
259240116Smarcel    atf_set "descr" "Tests for multiple occurrences of the -o option"
260240116Smarcel}
261240116Smarceloflag_multiple_body()
262240116Smarcel{
263240116Smarcel    h_pass "echo foo bar" -o match:foo -o match:bar
264240116Smarcel    h_pass "echo foo; echo bar" -o match:foo -o match:bar
265240116Smarcel    h_fail "echo foo baz" -o match:bar -o match:foo
266240116Smarcel    h_fail "echo foo; echo baz" -o match:bar -o match:foo
267240116Smarcel}
268240116Smarcel
269240116Smarcelatf_test_case oflag_negated
270240116Smarceloflag_negated_head()
271240116Smarcel{
272240116Smarcel    atf_set "descr" "Tests for negated occurrences of the -o option"
273240116Smarcel}
274240116Smarceloflag_negated_body()
275240116Smarcel{
276240116Smarcel    h_fail "echo foo" -o empty
277240116Smarcel    h_pass "echo foo" -o not-empty
278240116Smarcel
279240116Smarcel    h_pass "echo foo bar" -o match:foo
280240116Smarcel    h_fail "echo foo bar" -o not-match:foo
281240116Smarcel}
282240116Smarcel
283240116Smarcelatf_test_case eflag_empty
284240116Smarceleflag_empty_head()
285240116Smarcel{
286240116Smarcel    atf_set "descr" "Tests for the -e option using the 'empty' argument"
287240116Smarcel}
288240116Smarceleflag_empty_body()
289240116Smarcel{
290240116Smarcel    h_pass "true 1>&2" -e empty
291240116Smarcel    h_fail "echo foo 1>&2" -e empty
292240116Smarcel}
293240116Smarcel
294240116Smarcelatf_test_case eflag_ignore
295240116Smarceleflag_ignore_head()
296240116Smarcel{
297240116Smarcel    atf_set "descr" "Tests for the -e option using the 'ignore' argument"
298240116Smarcel}
299240116Smarceleflag_ignore_body()
300240116Smarcel{
301240116Smarcel    h_pass "true 1>&2" -e ignore
302240116Smarcel    h_pass "echo foo 1>&2" -e ignore
303240116Smarcel}
304240116Smarcel
305240116Smarcelatf_test_case eflag_file
306240116Smarceleflag_file_head()
307240116Smarcel{
308240116Smarcel    atf_set "descr" "Tests for the -e option using the 'file:' argument"
309240116Smarcel}
310240116Smarceleflag_file_body()
311240116Smarcel{
312240116Smarcel    touch empty
313240116Smarcel    h_pass "true 1>&2" -e file:empty
314240116Smarcel
315240116Smarcel    echo foo >text
316240116Smarcel    h_pass "echo foo 1>&2" -e file:text
317240116Smarcel    h_fail "echo bar 1>&2" -e file:text
318240116Smarcel
319240116Smarcel    dd if=/dev/urandom of=bin bs=1k count=10
320240116Smarcel    h_pass "cat bin 1>&2" -e file:bin
321240116Smarcel}
322240116Smarcel
323240116Smarcelatf_test_case eflag_inline
324240116Smarceleflag_inline_head()
325240116Smarcel{
326240116Smarcel    atf_set "descr" "Tests for the -e option using the 'inline:' argument"
327240116Smarcel}
328240116Smarceleflag_inline_body()
329240116Smarcel{
330240116Smarcel    h_pass "true 1>&2" -e inline:
331240116Smarcel    h_pass "echo foo bar 1>&2" -e inline:"foo bar\n"
332240116Smarcel    h_pass "printf 'foo bar' 1>&2" -e inline:"foo bar"
333240116Smarcel    h_pass "printf '\t\n\t\n' 1>&2" -e inline:"\t\n\t\n"
334240116Smarcel    # XXX Ugly hack to workaround the lack of \e in FreeBSD.  Look for a
335240116Smarcel    # nicer solution...
336240116Smarcel    case $(uname) in
337240116Smarcel    Darwin|FreeBSD)
338240116Smarcel        h_pass "printf '\a\b\f\n\r\t\v' 1>&2" -e inline:"\a\b\f\n\r\t\v"
339240116Smarcel        ;;
340240116Smarcel    *)
341240116Smarcel        h_pass "printf '\a\b\e\f\n\r\t\v' 1>&2" -e inline:"\a\b\e\f\n\r\t\v"
342240116Smarcel        ;;
343240116Smarcel    esac
344240116Smarcel    h_pass "printf '\011\022\033\012' 1>&2" -e inline:"\011\022\033\012"
345240116Smarcel
346240116Smarcel    h_fail "echo foo bar 1>&2" -e inline:"foo bar"
347240116Smarcel    h_fail "echo -n foo bar 1>&2" -e inline:"foo bar\n"
348240116Smarcel}
349240116Smarcel
350240116Smarcelatf_test_case eflag_save
351240116Smarceleflag_save_head()
352240116Smarcel{
353240116Smarcel    atf_set "descr" "Tests for the -e option using the 'save:' argument"
354240116Smarcel}
355240116Smarceleflag_save_body()
356240116Smarcel{
357240116Smarcel    h_pass "echo foo 1>&2" -e save:out
358240116Smarcel    echo foo >exp
359240116Smarcel    cmp -s out exp || atf_fail "Saved output does not match expected results"
360240116Smarcel}
361240116Smarcel
362240116Smarcelatf_test_case eflag_match
363240116Smarceleflag_match_head()
364240116Smarcel{
365240116Smarcel    atf_set "descr" "Tests for the -e option using the 'match:' argument"
366240116Smarcel}
367240116Smarceleflag_match_body()
368240116Smarcel{
369240116Smarcel    h_pass "printf no-newline 1>&2" -e "match:^no-newline"
370240116Smarcel    h_pass "echo line1 1>&2; echo foo bar 1>&2" -e "match:^foo"
371240116Smarcel    h_pass "echo foo bar 1>&2" -e "match:o b"
372240116Smarcel    h_fail "echo foo bar 1>&2" -e "match:baz"
373240116Smarcel    h_fail "echo foo bar 1>&2" -e "match:^bar"
374240116Smarcel}
375240116Smarcel
376240116Smarcelatf_test_case eflag_multiple
377240116Smarceleflag_multiple_head()
378240116Smarcel{
379240116Smarcel    atf_set "descr" "Tests for multiple occurrences of the -e option"
380240116Smarcel}
381240116Smarceleflag_multiple_body()
382240116Smarcel{
383240116Smarcel    h_pass "echo foo bar 1>&2" -e match:foo -e match:bar
384240116Smarcel    h_pass "echo foo 1>&2; echo bar 1>&2" -e match:foo -e match:bar
385240116Smarcel    h_fail "echo foo baz 1>&2" -e match:bar -e match:foo
386240116Smarcel    h_fail "echo foo 1>&2; echo baz 1>&2" -e match:bar -e match:foo
387240116Smarcel}
388240116Smarcel
389240116Smarcelatf_test_case eflag_negated
390240116Smarceleflag_negated_head()
391240116Smarcel{
392240116Smarcel    atf_set "descr" "Tests for negated occurrences of the -e option"
393240116Smarcel}
394240116Smarceleflag_negated_body()
395240116Smarcel{
396240116Smarcel    h_fail "echo foo 1>&2" -e empty
397240116Smarcel    h_pass "echo foo 1>&2" -e not-empty
398240116Smarcel
399240116Smarcel    h_pass "echo foo bar 1>&2" -e match:foo
400240116Smarcel    h_fail "echo foo bar 1>&2" -e not-match:foo
401240116Smarcel}
402240116Smarcel
403240116Smarcelatf_test_case stdin
404240116Smarcelstdin_head()
405240116Smarcel{
406240116Smarcel    atf_set "descr" "Tests that stdin is preserved"
407240116Smarcel}
408240116Smarcelstdin_body()
409240116Smarcel{
410240116Smarcel    echo "hello" | ${Atf_Check} -o match:"hello" cat || \
411240116Smarcel        atf_fail "atf-check does not seem to respect stdin"
412240116Smarcel}
413240116Smarcel
414240116Smarcelatf_test_case invalid_umask
415240116Smarcelinvalid_umask_head()
416240116Smarcel{
417240116Smarcel    atf_set "descr" "Tests for a correct error condition if the umask is" \
418240116Smarcel            "too restrictive"
419240116Smarcel}
420240116Smarcelinvalid_umask_body()
421240116Smarcel{
422240116Smarcel    umask 0222
423240116Smarcel    ${Atf_Check} false 2>stderr && \
424240116Smarcel        atf_fail "atf-check returned 0 but it should have failed"
425240116Smarcel    cat stderr
426240116Smarcel    grep 'temporary.*current umask.*0222' stderr >/dev/null || \
427240116Smarcel        atf_fail "atf-check did not report an error related to the" \
428240116Smarcel                 "current umask"
429240116Smarcel}
430240116Smarcel
431240116Smarcelatf_init_test_cases()
432240116Smarcel{
433240116Smarcel    atf_add_test_case sflag_eq_ne
434240116Smarcel    atf_add_test_case sflag_exit
435240116Smarcel    atf_add_test_case sflag_ignore
436240116Smarcel    atf_add_test_case sflag_signal
437240116Smarcel
438240116Smarcel    atf_add_test_case xflag
439240116Smarcel
440240116Smarcel    atf_add_test_case oflag_empty
441240116Smarcel    atf_add_test_case oflag_ignore
442240116Smarcel    atf_add_test_case oflag_file
443240116Smarcel    atf_add_test_case oflag_inline
444240116Smarcel    atf_add_test_case oflag_match
445240116Smarcel    atf_add_test_case oflag_save
446240116Smarcel    atf_add_test_case oflag_multiple
447240116Smarcel    atf_add_test_case oflag_negated
448240116Smarcel
449240116Smarcel    atf_add_test_case eflag_empty
450240116Smarcel    atf_add_test_case eflag_ignore
451240116Smarcel    atf_add_test_case eflag_file
452240116Smarcel    atf_add_test_case eflag_inline
453240116Smarcel    atf_add_test_case eflag_match
454240116Smarcel    atf_add_test_case eflag_save
455240116Smarcel    atf_add_test_case eflag_multiple
456240116Smarcel    atf_add_test_case eflag_negated
457240116Smarcel
458240116Smarcel    atf_add_test_case stdin
459240116Smarcel
460240116Smarcel    atf_add_test_case invalid_umask
461240116Smarcel}
462240116Smarcel
463240116Smarcel# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
464