1*640235e2SEnji Cooper# $NetBSD: t_exit.sh,v 1.6 2016/05/07 23:51:30 kre Exp $ 257718be8SEnji Cooper# 357718be8SEnji Cooper# Copyright (c) 2007 The NetBSD Foundation, Inc. 457718be8SEnji Cooper# All rights reserved. 557718be8SEnji Cooper# 657718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without 757718be8SEnji Cooper# modification, are permitted provided that the following conditions 857718be8SEnji Cooper# are met: 957718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright 1057718be8SEnji Cooper# notice, this list of conditions and the following disclaimer. 1157718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright 1257718be8SEnji Cooper# notice, this list of conditions and the following disclaimer in the 1357718be8SEnji Cooper# documentation and/or other materials provided with the distribution. 1457718be8SEnji Cooper# 1557718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1657718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1757718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1857718be8SEnji Cooper# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 1957718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2057718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2157718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2257718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2357718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2457718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2557718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE. 2657718be8SEnji Cooper# 27*640235e2SEnji Cooper# the implementation of "sh" to test 28*640235e2SEnji Cooper: ${TEST_SH:="/bin/sh"} 2957718be8SEnji Cooper 3057718be8SEnji Cooper 3157718be8SEnji Cooperatf_test_case background 3257718be8SEnji Cooperbackground_head() { 3357718be8SEnji Cooper atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \ 3457718be8SEnji Cooper "a command in the background (PR bin/46327)" 3557718be8SEnji Cooper} 3657718be8SEnji Cooperbackground_body() { 37*640235e2SEnji Cooper atf_check -o match:0 -e empty ${TEST_SH} -c 'true; true & echo $?' 38*640235e2SEnji Cooper # atf_expect_fail "PR bin/46327" (now fixed?) 39*640235e2SEnji Cooper atf_check -o match:0 -e empty ${TEST_SH} -c 'false; true & echo $?' 4057718be8SEnji Cooper} 4157718be8SEnji Cooper 4257718be8SEnji Cooperatf_test_case function 4357718be8SEnji Cooperfunction_head() { 4457718be8SEnji Cooper atf_set "descr" "Tests that \$? is correctly updated inside " \ 4557718be8SEnji Cooper "a function" 4657718be8SEnji Cooper} 4757718be8SEnji Cooperfunction_body() { 48*640235e2SEnji Cooper atf_check -s exit:0 -o match:STATUS=1-0 -e empty \ 49*640235e2SEnji Cooper ${TEST_SH} -c ' 50*640235e2SEnji Cooper crud() { 51*640235e2SEnji Cooper test yes = no 52*640235e2SEnji Cooper 53*640235e2SEnji Cooper cat <<-EOF 54*640235e2SEnji Cooper STATUS=$? 55*640235e2SEnji Cooper EOF 56*640235e2SEnji Cooper } 57*640235e2SEnji Cooper foo=$(crud) 58*640235e2SEnji Cooper echo "${foo}-$?" 59*640235e2SEnji Cooper ' 6057718be8SEnji Cooper} 6157718be8SEnji Cooper 6257718be8SEnji Cooperatf_test_case readout 6357718be8SEnji Cooperreadout_head() { 6457718be8SEnji Cooper atf_set "descr" "Tests that \$? is correctly updated in a " \ 6557718be8SEnji Cooper "compound expression" 6657718be8SEnji Cooper} 6757718be8SEnji Cooperreadout_body() { 68*640235e2SEnji Cooper atf_check -s exit:0 -o match:0 -e empty \ 69*640235e2SEnji Cooper ${TEST_SH} -c 'true && ! true | false; echo $?' 7057718be8SEnji Cooper} 7157718be8SEnji Cooper 7257718be8SEnji Cooperatf_test_case trap_subshell 7357718be8SEnji Coopertrap_subshell_head() { 7457718be8SEnji Cooper atf_set "descr" "Tests that the trap statement in a subshell " \ 7557718be8SEnji Cooper "works when the subshell exits" 7657718be8SEnji Cooper} 7757718be8SEnji Coopertrap_subshell_body() { 78*640235e2SEnji Cooper atf_check -s exit:0 -o inline:'exiting\n' -e empty \ 79*640235e2SEnji Cooper ${TEST_SH} -c '( trap "echo exiting" EXIT; /usr/bin/true )' 8057718be8SEnji Cooper} 8157718be8SEnji Cooper 8257718be8SEnji Cooperatf_test_case trap_zero__implicit_exit 83*640235e2SEnji Coopertrap_zero__implicit_exit_head() { 84*640235e2SEnji Cooper atf_set "descr" "Tests that the trap statement in a subshell in a " \ 85*640235e2SEnji Cooper "script works when the subshell simply runs out of commands" 86*640235e2SEnji Cooper} 8757718be8SEnji Coopertrap_zero__implicit_exit_body() { 88*640235e2SEnji Cooper # PR bin/6764: sh works but ksh does not 8957718be8SEnji Cooper echo '( trap "echo exiting" 0 )' >helper.sh 90*640235e2SEnji Cooper atf_check -s exit:0 -o match:exiting -e empty ${TEST_SH} helper.sh 91*640235e2SEnji Cooper # test ksh by setting TEST_SH to /bin/ksh and run the entire set... 92*640235e2SEnji Cooper # atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh 9357718be8SEnji Cooper} 9457718be8SEnji Cooper 9557718be8SEnji Cooperatf_test_case trap_zero__explicit_exit 96*640235e2SEnji Coopertrap_zero__explicit_exit_head() { 97*640235e2SEnji Cooper atf_set "descr" "Tests that the trap statement in a subshell in a " \ 98*640235e2SEnji Cooper "script works when the subshell executes an explicit exit" 99*640235e2SEnji Cooper} 10057718be8SEnji Coopertrap_zero__explicit_exit_body() { 101*640235e2SEnji Cooper echo '( trap "echo exiting" 0; exit; echo NO_NO_NO )' >helper.sh 102*640235e2SEnji Cooper atf_check -s exit:0 -o match:exiting -o not-match:NO_NO -e empty \ 103*640235e2SEnji Cooper ${TEST_SH} helper.sh 104*640235e2SEnji Cooper # test ksh by setting TEST_SH to /bin/ksh and run the entire set... 105*640235e2SEnji Cooper # atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh 10657718be8SEnji Cooper} 10757718be8SEnji Cooper 108*640235e2SEnji Cooperatf_test_case simple_exit 109*640235e2SEnji Coopersimple_exit_head() { 110*640235e2SEnji Cooper atf_set "descr" "Tests that various values for exit status work" 111*640235e2SEnji Cooper} 112*640235e2SEnji Cooper# Note: ATF will not allow tests of exit values > 255, even if they would work 113*640235e2SEnji Coopersimple_exit_body() { 114*640235e2SEnji Cooper for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255 115*640235e2SEnji Cooper do 116*640235e2SEnji Cooper atf_check -s exit:$N -o empty -e empty \ 117*640235e2SEnji Cooper ${TEST_SH} -c "exit $N; echo FOO; echo BAR >&2" 118*640235e2SEnji Cooper done 119*640235e2SEnji Cooper} 120*640235e2SEnji Cooper 121*640235e2SEnji Cooperatf_test_case subshell_exit 122*640235e2SEnji Coopersubshell_exit_head() { 123*640235e2SEnji Cooper atf_set "descr" "Tests that subshell exit status works and \$? gets it" 124*640235e2SEnji Cooper} 125*640235e2SEnji Cooper# Note: ATF will not allow tests of exit values > 255, even if they would work 126*640235e2SEnji Coopersubshell_exit_body() { 127*640235e2SEnji Cooper for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255 128*640235e2SEnji Cooper do 129*640235e2SEnji Cooper atf_check -s exit:0 -o empty -e empty \ 130*640235e2SEnji Cooper ${TEST_SH} -c "(exit $N); test \$? -eq $N" 131*640235e2SEnji Cooper done 132*640235e2SEnji Cooper} 133*640235e2SEnji Cooper 134*640235e2SEnji Cooperatf_test_case subshell_background 135*640235e2SEnji Coopersubshell_background_head() { 136*640235e2SEnji Cooper atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \ 137*640235e2SEnji Cooper "a subshell in the background" 138*640235e2SEnji Cooper} 139*640235e2SEnji Coopersubshell_background_body() { 140*640235e2SEnji Cooper atf_check -o match:0 -e empty \ 141*640235e2SEnji Cooper ${TEST_SH} -c 'true; (false || true) & echo $?' 142*640235e2SEnji Cooper # atf_expect_fail "PR bin/46327" (now fixed?) 143*640235e2SEnji Cooper atf_check -o match:0 -e empty \ 144*640235e2SEnji Cooper ${TEST_SH} -c 'false; (false || true) & echo $?' 14557718be8SEnji Cooper} 14657718be8SEnji Cooper 14757718be8SEnji Cooperatf_init_test_cases() { 14857718be8SEnji Cooper atf_add_test_case background 14957718be8SEnji Cooper atf_add_test_case function 15057718be8SEnji Cooper atf_add_test_case readout 15157718be8SEnji Cooper atf_add_test_case trap_subshell 15257718be8SEnji Cooper atf_add_test_case trap_zero__implicit_exit 15357718be8SEnji Cooper atf_add_test_case trap_zero__explicit_exit 154*640235e2SEnji Cooper atf_add_test_case simple_exit 155*640235e2SEnji Cooper atf_add_test_case subshell_exit 156*640235e2SEnji Cooper atf_add_test_case subshell_background 15757718be8SEnji Cooper} 158