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