Path: blob/main/contrib/atf/atf-sh/atf-check_test.sh
39482 views
# Copyright (c) 2008 The NetBSD Foundation, Inc.1# All rights reserved.2#3# Redistribution and use in source and binary forms, with or without4# modification, are permitted provided that the following conditions5# are met:6# 1. Redistributions of source code must retain the above copyright7# notice, this list of conditions and the following disclaimer.8# 2. Redistributions in binary form must reproduce the above copyright9# notice, this list of conditions and the following disclaimer in the10# documentation and/or other materials provided with the distribution.11#12# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425# The Atf_Check and Atf-Shell variables are set by atf-sh.2627h_pass()28{29cmd="$1"; shift3031echo "Running [atf-check $*] against [${cmd}]"3233cat >script.sh <<EOF34#! ${Atf_Shell}35${cmd}36EOF37chmod +x script.sh3839if ! ${Atf_Check} "${@}" ./script.sh >tmp; then40cat tmp41atf_fail "atf-check failed"42fi43}4445h_fail()46{47cmd="$1"; shift4849echo "Running [atf-check $*] against [${cmd}]"5051cat >script.sh <<EOF52#! ${Atf_Shell}53${cmd}54EOF55chmod +x script.sh5657if ${Atf_Check} "${@}" ./script.sh 2>tmp; then58cat tmp59atf_fail "atf-check succeeded but should fail"60fi61}6263atf_test_case sflag_eq_ne64sflag_eq_ne_head()65{66atf_set "descr" "Tests for the -s option using the 'eq' and 'ne' qualifiers"67}68sflag_eq_ne_body()69{70h_pass "true" -s eq:071h_pass "false" -s ne:072h_pass "exit 255" -s eq:25573h_pass "exit 0" -s ne:2557475h_fail "exit 256" -s eq:25676h_fail "exit -1" -s eq:-177h_fail "true" -s ne:25678h_fail "true" -s ne:-179}8081atf_test_case sflag_exit82sflag_exit_head()83{84atf_set "descr" "Tests for the -s option using the 'exit' qualifier"85}86sflag_exit_body()87{88h_pass 'true' -s exit:089h_pass 'false' -s not-exit:090h_pass 'exit 255' -s exit:25591h_pass 'exit 0' -s not-exit:2559293h_fail 'exit 256' -s exit:25694h_fail 'exit -1' -s exit:-195h_fail 'true' -s not-exit:25696h_fail 'true' -s not-exit:-19798h_pass 'true' -s exit99h_pass 'false' -s exit100if ${Atf_Check} -s exit -x 'kill $$'; then101atf_fail "Signal detected as clean exit"102fi103}104105atf_test_case sflag_ignore106sflag_ignore_head()107{108atf_set "descr" "Tests for the -s option using the 'ignore' qualifier"109}110sflag_ignore_body()111{112h_pass 'true' -s ignore113h_pass 'false' -s ignore114if ${Atf_Check} -s ignored -x 'kill $$'; then115atf_fail "Signal not ignored"116fi117}118119atf_test_case sflag_signal120sflag_signal_head()121{122atf_set "descr" "Tests for the -s option using the 'signal' qualifier"123}124sflag_signal_body()125{126${Atf_Check} -s signal:hup -x 'kill -1 $$' || atf_fail "Signal not detected"127${Atf_Check} -s signal:sighup -x 'kill -1 $$' || atf_fail "Signal not" \128"detected"129${Atf_Check} -s signal:1 -x 'kill -1 $$' || atf_fail "Signal not detected"130${Atf_Check} -s signal -x 'kill -1 $$' || atf_fail "Signal not detected"131132${Atf_Check} -s not-signal:kill -x 'kill -9 $$' && \133atf_fail "not-signal:kill matched kill -9"134${Atf_Check} -s not-signal:kill -x 'kill -1 $$' || \135atf_fail "not-signal:kill did not match kill -1"136137h_fail 'true' -s signal138h_fail 'false' -s signal139}140141atf_test_case xflag142xflag_head()143{144atf_set "descr" "Tests for the -x option"145}146xflag_body()147{148${Atf_Check} -s ne:0 -o ignore -e ignore "echo foo 2>&1" || \149atf_fail "Shell command succeeded without -x"150151${Atf_Check} -e inline:"foo\n" -x "echo foo 1>&2" || \152atf_fail "Cannot run command with -x"153154${Atf_Check} -o inline:"foo\n" -x echo foo || \155atf_fail "Using -x does not respect all provided arguments"156}157158atf_test_case oflag_empty159oflag_empty_head()160{161atf_set "descr" "Tests for the -o option using the 'empty' argument"162}163oflag_empty_body()164{165h_pass "true" -o empty166h_fail "echo foo" -o empty167}168169atf_test_case oflag_ignore170oflag_ignore_head()171{172atf_set "descr" "Tests for the -o option using the 'ignore' argument"173}174oflag_ignore_body()175{176h_pass "true" -o ignore177h_pass "echo foo" -o ignore178}179180atf_test_case oflag_file181oflag_file_head()182{183atf_set "descr" "Tests for the -o option using the 'file:' argument"184}185oflag_file_body()186{187touch empty188h_pass "true" -o file:empty189190echo foo >text191h_pass "echo foo" -o file:text192h_fail "echo bar" -o file:text193194dd if=/dev/urandom of=bin bs=1k count=10195h_pass "cat bin" -o file:bin196}197198atf_test_case oflag_inline199oflag_inline_head()200{201atf_set "descr" "Tests for the -o option using the 'inline:' argument"202}203oflag_inline_body()204{205h_pass "true" -o inline:206h_pass "echo foo bar" -o inline:"foo bar\n"207h_pass "printf 'foo bar'" -o inline:"foo bar"208h_pass "printf '\t\n\t\n'" -o inline:"\t\n\t\n"209h_pass "printf '\a\b\033\f\n\r\t\v'" -o inline:"\a\b\e\f\n\r\t\v"210h_pass "printf '\011\022\033\012'" -o inline:"\011\022\033\012"211212h_fail "echo foo bar" -o inline:"foo bar"213h_fail "echo -n foo bar" -o inline:"foo bar\n"214}215216atf_test_case oflag_match217oflag_match_head()218{219atf_set "descr" "Tests for the -o option using the 'match:' argument"220}221oflag_match_body()222{223h_pass "printf no-newline" -o "match:^no-newline"224h_pass "echo line1; echo foo bar" -o "match:^foo"225h_pass "echo foo bar" -o "match:o b"226h_fail "echo foo bar" -o "match:baz"227h_fail "echo foo bar" -o "match:^bar"228}229230atf_test_case oflag_save231oflag_save_head()232{233atf_set "descr" "Tests for the -o option using the 'save:' argument"234}235oflag_save_body()236{237h_pass "echo foo" -o save:out238echo foo >exp239cmp -s out exp || atf_fail "Saved output does not match expected results"240}241242atf_test_case oflag_multiple243oflag_multiple_head()244{245atf_set "descr" "Tests for multiple occurrences of the -o option"246}247oflag_multiple_body()248{249h_pass "echo foo bar" -o match:foo -o match:bar250h_pass "echo foo; echo bar" -o match:foo -o match:bar251h_fail "echo foo baz" -o match:bar -o match:foo252h_fail "echo foo; echo baz" -o match:bar -o match:foo253}254255atf_test_case oflag_negated256oflag_negated_head()257{258atf_set "descr" "Tests for negated occurrences of the -o option"259}260oflag_negated_body()261{262h_fail "echo foo" -o empty263h_pass "echo foo" -o not-empty264265h_pass "echo foo bar" -o match:foo266h_fail "echo foo bar" -o not-match:foo267}268269atf_test_case eflag_empty270eflag_empty_head()271{272atf_set "descr" "Tests for the -e option using the 'empty' argument"273}274eflag_empty_body()275{276h_pass "true 1>&2" -e empty277h_fail "echo foo 1>&2" -e empty278}279280atf_test_case eflag_ignore281eflag_ignore_head()282{283atf_set "descr" "Tests for the -e option using the 'ignore' argument"284}285eflag_ignore_body()286{287h_pass "true 1>&2" -e ignore288h_pass "echo foo 1>&2" -e ignore289}290291atf_test_case eflag_file292eflag_file_head()293{294atf_set "descr" "Tests for the -e option using the 'file:' argument"295}296eflag_file_body()297{298touch empty299h_pass "true 1>&2" -e file:empty300301echo foo >text302h_pass "echo foo 1>&2" -e file:text303h_fail "echo bar 1>&2" -e file:text304305dd if=/dev/urandom of=bin bs=1k count=10306h_pass "cat bin 1>&2" -e file:bin307}308309atf_test_case eflag_inline310eflag_inline_head()311{312atf_set "descr" "Tests for the -e option using the 'inline:' argument"313}314eflag_inline_body()315{316h_pass "true 1>&2" -e inline:317h_pass "echo foo bar 1>&2" -e inline:"foo bar\n"318h_pass "printf 'foo bar' 1>&2" -e inline:"foo bar"319h_pass "printf '\t\n\t\n' 1>&2" -e inline:"\t\n\t\n"320h_pass "printf '\a\b\033\f\n\r\t\v' 1>&2" -e inline:"\a\b\e\f\n\r\t\v"321h_pass "printf '\011\022\033\012' 1>&2" -e inline:"\011\022\033\012"322323h_fail "echo foo bar 1>&2" -e inline:"foo bar"324h_fail "echo -n foo bar 1>&2" -e inline:"foo bar\n"325}326327atf_test_case eflag_save328eflag_save_head()329{330atf_set "descr" "Tests for the -e option using the 'save:' argument"331}332eflag_save_body()333{334h_pass "echo foo 1>&2" -e save:out335echo foo >exp336cmp -s out exp || atf_fail "Saved output does not match expected results"337}338339atf_test_case eflag_match340eflag_match_head()341{342atf_set "descr" "Tests for the -e option using the 'match:' argument"343}344eflag_match_body()345{346h_pass "printf no-newline 1>&2" -e "match:^no-newline"347h_pass "echo line1 1>&2; echo foo bar 1>&2" -e "match:^foo"348h_pass "echo foo bar 1>&2" -e "match:o b"349h_fail "echo foo bar 1>&2" -e "match:baz"350h_fail "echo foo bar 1>&2" -e "match:^bar"351}352353atf_test_case eflag_multiple354eflag_multiple_head()355{356atf_set "descr" "Tests for multiple occurrences of the -e option"357}358eflag_multiple_body()359{360h_pass "echo foo bar 1>&2" -e match:foo -e match:bar361h_pass "echo foo 1>&2; echo bar 1>&2" -e match:foo -e match:bar362h_fail "echo foo baz 1>&2" -e match:bar -e match:foo363h_fail "echo foo 1>&2; echo baz 1>&2" -e match:bar -e match:foo364}365366atf_test_case eflag_negated367eflag_negated_head()368{369atf_set "descr" "Tests for negated occurrences of the -e option"370}371eflag_negated_body()372{373h_fail "echo foo 1>&2" -e empty374h_pass "echo foo 1>&2" -e not-empty375376h_pass "echo foo bar 1>&2" -e match:foo377h_fail "echo foo bar 1>&2" -e not-match:foo378}379380atf_test_case stdin381stdin_head()382{383atf_set "descr" "Tests that stdin is preserved"384}385stdin_body()386{387echo "hello" | ${Atf_Check} -o match:"hello" cat || \388atf_fail "atf-check does not seem to respect stdin"389}390391atf_test_case unusual_umask392unusual_umask_head()393{394atf_set "descr" "Tests that atf-check doesn't care about unusual umasks"395}396unusual_umask_body()397{398for mask in 022 027 0222 0177 0777 ; do399umask $mask400${Atf_Check} true || \401atf_fail "atf-check failed with umask $mask"402done403umask 022404}405406atf_init_test_cases()407{408atf_add_test_case sflag_eq_ne409atf_add_test_case sflag_exit410atf_add_test_case sflag_ignore411atf_add_test_case sflag_signal412413atf_add_test_case xflag414415atf_add_test_case oflag_empty416atf_add_test_case oflag_ignore417atf_add_test_case oflag_file418atf_add_test_case oflag_inline419atf_add_test_case oflag_match420atf_add_test_case oflag_save421atf_add_test_case oflag_multiple422atf_add_test_case oflag_negated423424atf_add_test_case eflag_empty425atf_add_test_case eflag_ignore426atf_add_test_case eflag_file427atf_add_test_case eflag_inline428atf_add_test_case eflag_match429atf_add_test_case eflag_save430atf_add_test_case eflag_multiple431atf_add_test_case eflag_negated432433atf_add_test_case stdin434435atf_add_test_case unusual_umask436}437438# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4439440441