1 1.6 kre # $NetBSD: t_exit.sh,v 1.6 2016/05/07 23:51:30 kre Exp $ 2 1.1 jruoho # 3 1.1 jruoho # Copyright (c) 2007 The NetBSD Foundation, Inc. 4 1.1 jruoho # All rights reserved. 5 1.1 jruoho # 6 1.1 jruoho # Redistribution and use in source and binary forms, with or without 7 1.1 jruoho # modification, are permitted provided that the following conditions 8 1.1 jruoho # are met: 9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright 10 1.1 jruoho # notice, this list of conditions and the following disclaimer. 11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright 12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the 13 1.1 jruoho # documentation and/or other materials provided with the distribution. 14 1.1 jruoho # 15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE. 26 1.1 jruoho # 27 1.4 christos # the implementation of "sh" to test 28 1.4 christos : ${TEST_SH:="/bin/sh"} 29 1.1 jruoho 30 1.1 jruoho 31 1.3 jruoho atf_test_case background 32 1.3 jruoho background_head() { 33 1.3 jruoho atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \ 34 1.4 christos "a command in the background (PR bin/46327)" 35 1.3 jruoho } 36 1.3 jruoho background_body() { 37 1.5 christos atf_check -o match:0 -e empty ${TEST_SH} -c 'true; true & echo $?' 38 1.4 christos # atf_expect_fail "PR bin/46327" (now fixed?) 39 1.5 christos atf_check -o match:0 -e empty ${TEST_SH} -c 'false; true & echo $?' 40 1.3 jruoho } 41 1.3 jruoho 42 1.1 jruoho atf_test_case function 43 1.1 jruoho function_head() { 44 1.4 christos atf_set "descr" "Tests that \$? is correctly updated inside " \ 45 1.4 christos "a function" 46 1.1 jruoho } 47 1.1 jruoho function_body() { 48 1.4 christos atf_check -s exit:0 -o match:STATUS=1-0 -e empty \ 49 1.5 christos ${TEST_SH} -c ' 50 1.4 christos crud() { 51 1.4 christos test yes = no 52 1.4 christos 53 1.4 christos cat <<-EOF 54 1.4 christos STATUS=$? 55 1.4 christos EOF 56 1.4 christos } 57 1.4 christos foo=$(crud) 58 1.4 christos echo "${foo}-$?" 59 1.4 christos ' 60 1.1 jruoho } 61 1.1 jruoho 62 1.1 jruoho atf_test_case readout 63 1.1 jruoho readout_head() { 64 1.4 christos atf_set "descr" "Tests that \$? is correctly updated in a " \ 65 1.4 christos "compound expression" 66 1.1 jruoho } 67 1.1 jruoho readout_body() { 68 1.4 christos atf_check -s exit:0 -o match:0 -e empty \ 69 1.5 christos ${TEST_SH} -c 'true && ! true | false; echo $?' 70 1.1 jruoho } 71 1.1 jruoho 72 1.1 jruoho atf_test_case trap_subshell 73 1.1 jruoho trap_subshell_head() { 74 1.4 christos atf_set "descr" "Tests that the trap statement in a subshell " \ 75 1.4 christos "works when the subshell exits" 76 1.1 jruoho } 77 1.1 jruoho trap_subshell_body() { 78 1.4 christos atf_check -s exit:0 -o inline:'exiting\n' -e empty \ 79 1.4 christos ${TEST_SH} -c '( trap "echo exiting" EXIT; /usr/bin/true )' 80 1.1 jruoho } 81 1.1 jruoho 82 1.1 jruoho atf_test_case trap_zero__implicit_exit 83 1.4 christos trap_zero__implicit_exit_head() { 84 1.4 christos atf_set "descr" "Tests that the trap statement in a subshell in a " \ 85 1.4 christos "script works when the subshell simply runs out of commands" 86 1.4 christos } 87 1.1 jruoho trap_zero__implicit_exit_body() { 88 1.4 christos # PR bin/6764: sh works but ksh does not 89 1.1 jruoho echo '( trap "echo exiting" 0 )' >helper.sh 90 1.5 christos atf_check -s exit:0 -o match:exiting -e empty ${TEST_SH} helper.sh 91 1.4 christos # test ksh by setting TEST_SH to /bin/ksh and run the entire set... 92 1.4 christos # atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh 93 1.1 jruoho } 94 1.1 jruoho 95 1.1 jruoho atf_test_case trap_zero__explicit_exit 96 1.4 christos trap_zero__explicit_exit_head() { 97 1.4 christos atf_set "descr" "Tests that the trap statement in a subshell in a " \ 98 1.4 christos "script works when the subshell executes an explicit exit" 99 1.4 christos } 100 1.1 jruoho trap_zero__explicit_exit_body() { 101 1.4 christos echo '( trap "echo exiting" 0; exit; echo NO_NO_NO )' >helper.sh 102 1.4 christos atf_check -s exit:0 -o match:exiting -o not-match:NO_NO -e empty \ 103 1.5 christos ${TEST_SH} helper.sh 104 1.4 christos # test ksh by setting TEST_SH to /bin/ksh and run the entire set... 105 1.4 christos # atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh 106 1.1 jruoho } 107 1.1 jruoho 108 1.4 christos atf_test_case simple_exit 109 1.4 christos simple_exit_head() { 110 1.4 christos atf_set "descr" "Tests that various values for exit status work" 111 1.4 christos } 112 1.4 christos # Note: ATF will not allow tests of exit values > 255, even if they would work 113 1.4 christos simple_exit_body() { 114 1.4 christos for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255 115 1.4 christos do 116 1.4 christos atf_check -s exit:$N -o empty -e empty \ 117 1.5 christos ${TEST_SH} -c "exit $N; echo FOO; echo BAR >&2" 118 1.4 christos done 119 1.4 christos } 120 1.4 christos 121 1.4 christos atf_test_case subshell_exit 122 1.4 christos subshell_exit_head() { 123 1.4 christos atf_set "descr" "Tests that subshell exit status works and \$? gets it" 124 1.4 christos } 125 1.4 christos # Note: ATF will not allow tests of exit values > 255, even if they would work 126 1.4 christos subshell_exit_body() { 127 1.4 christos for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255 128 1.4 christos do 129 1.4 christos atf_check -s exit:0 -o empty -e empty \ 130 1.5 christos ${TEST_SH} -c "(exit $N); test \$? -eq $N" 131 1.4 christos done 132 1.4 christos } 133 1.4 christos 134 1.4 christos atf_test_case subshell_background 135 1.4 christos subshell_background_head() { 136 1.4 christos atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \ 137 1.4 christos "a subshell in the background" 138 1.4 christos } 139 1.4 christos subshell_background_body() { 140 1.4 christos atf_check -o match:0 -e empty \ 141 1.5 christos ${TEST_SH} -c 'true; (false || true) & echo $?' 142 1.4 christos # atf_expect_fail "PR bin/46327" (now fixed?) 143 1.4 christos atf_check -o match:0 -e empty \ 144 1.5 christos ${TEST_SH} -c 'false; (false || true) & echo $?' 145 1.1 jruoho } 146 1.1 jruoho 147 1.1 jruoho atf_init_test_cases() { 148 1.3 jruoho atf_add_test_case background 149 1.1 jruoho atf_add_test_case function 150 1.1 jruoho atf_add_test_case readout 151 1.1 jruoho atf_add_test_case trap_subshell 152 1.1 jruoho atf_add_test_case trap_zero__implicit_exit 153 1.1 jruoho atf_add_test_case trap_zero__explicit_exit 154 1.4 christos atf_add_test_case simple_exit 155 1.4 christos atf_add_test_case subshell_exit 156 1.4 christos atf_add_test_case subshell_background 157 1.1 jruoho } 158