1 1.5 kre # $NetBSD: t_redircloexec.sh,v 1.5 2017/05/27 13:11:50 kre Exp $ 2 1.1 christos # 3 1.1 christos # Copyright (c) 2016 The NetBSD Foundation, Inc. 4 1.1 christos # All rights reserved. 5 1.1 christos # 6 1.1 christos # This code is derived from software contributed to The NetBSD Foundation 7 1.1 christos # by Christos Zoulas. 8 1.1 christos # 9 1.1 christos # Redistribution and use in source and binary forms, with or without 10 1.1 christos # modification, are permitted provided that the following conditions 11 1.1 christos # are met: 12 1.1 christos # 1. Redistributions of source code must retain the above copyright 13 1.1 christos # notice, this list of conditions and the following disclaimer. 14 1.1 christos # 2. Redistributions in binary form must reproduce the above copyright 15 1.1 christos # notice, this list of conditions and the following disclaimer in the 16 1.1 christos # documentation and/or other materials provided with the distribution. 17 1.1 christos # 18 1.1 christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 christos # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 christos # POSSIBILITY OF SUCH DAMAGE. 29 1.1 christos # 30 1.1 christos # the implementation of "sh" to test 31 1.1 christos : ${TEST_SH:="/bin/sh"} 32 1.1 christos 33 1.1 christos mkhelper() { 34 1.2 christos name=$1 35 1.2 christos fd=$2 36 1.1 christos shift 2 37 1.2 christos 38 1.2 christos echo "$@" > ./"${name}1" 39 1.2 christos echo "echo ${name}2" ">&${fd}" > ./"${name}2" 40 1.1 christos } 41 1.1 christos 42 1.1 christos cleanhelper() { 43 1.2 christos # not really needed, atf cleans up... 44 1.2 christos rm -f ./"${1}1" ./"${1}2" out 45 1.1 christos } 46 1.1 christos 47 1.1 christos atf_test_case exec_redir_closed 48 1.1 christos exec_redir_closed_head() { 49 1.1 christos atf_set "descr" "Tests that redirections created by exec are closed on exec" 50 1.1 christos } 51 1.1 christos exec_redir_closed_body() { 52 1.2 christos 53 1.5 kre [ -n "${POSIXLY_CORRECT+set}" ] && atf_skip "tests non-posix behaviour" 54 1.5 kre 55 1.2 christos mkhelper exec 6 \ 56 1.2 christos "exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" 57 1.2 christos 58 1.2 christos atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} ./exec1 59 1.2 christos atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -e ./exec1 60 1.2 christos 61 1.2 christos mkhelper exec 9 \ 62 1.2 christos "exec 9> out; echo exec1 >&9; ${TEST_SH} exec2" 63 1.2 christos 64 1.2 christos atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} ./exec1 65 1.2 christos 66 1.2 christos mkhelper exec 8 \ 67 1.2 christos "exec 8> out; printf OK; echo exec1 >&8;" \ 68 1.2 christos "printf OK; ${TEST_SH} exec2; printf ERR" 69 1.2 christos 70 1.2 christos atf_check -s not-exit:0 -o match:OKOK -o not-match:ERR -e not-empty \ 71 1.2 christos ${TEST_SH} -e ./exec1 72 1.2 christos 73 1.2 christos mkhelper exec 7 \ 74 1.2 christos "exec 7> out; printf OK; echo exec1 >&7;" \ 75 1.2 christos "printf OK; ${TEST_SH} exec2 || printf ERR" 76 1.2 christos 77 1.2 christos atf_check -s exit:0 -o match:OKOKERR -e not-empty \ 78 1.2 christos ${TEST_SH} ./exec1 79 1.2 christos 80 1.1 christos cleanhelper exec 81 1.1 christos } 82 1.1 christos 83 1.3 kre atf_test_case exec_redir_open 84 1.3 kre exec_redir_open_head() { 85 1.3 kre atf_set "descr" "Tests that redirections created by exec can remain open" 86 1.3 kre } 87 1.3 kre exec_redir_open_body() { 88 1.3 kre 89 1.3 kre mkhelper exec 6 \ 90 1.3 kre "exec 6> out 6>&6; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" 91 1.3 kre 92 1.3 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 93 1.3 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1 94 1.3 kre 95 1.3 kre mkhelper exec 9 \ 96 1.3 kre "exec 9> out ; echo exec1 >&9; ${TEST_SH} exec2 9>&9" 97 1.3 kre 98 1.3 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 99 1.3 kre 100 1.3 kre mkhelper exec 8 \ 101 1.3 kre "exec 8> out; printf OK; exec 8>&8; echo exec1 >&8;" \ 102 1.3 kre "printf OK; ${TEST_SH} exec2; printf OK" 103 1.3 kre 104 1.3 kre atf_check -s exit:0 -o match:OKOKOK -e empty \ 105 1.3 kre ${TEST_SH} -e ./exec1 106 1.3 kre 107 1.3 kre mkhelper exec 7 \ 108 1.3 kre "exec 7> out; printf OK; echo exec1 >&7;" \ 109 1.3 kre "printf OK; ${TEST_SH} 7>&7 exec2; printf OK" 110 1.3 kre 111 1.3 kre atf_check -s exit:0 -o match:OKOKOK -e empty \ 112 1.3 kre ${TEST_SH} -e ./exec1 113 1.3 kre 114 1.3 kre cleanhelper exec 115 1.3 kre } 116 1.3 kre 117 1.1 christos atf_test_case loop_redir_open 118 1.1 christos loop_redir_open_head() { 119 1.1 christos atf_set "descr" "Tests that redirections in loops don't close on exec" 120 1.1 christos } 121 1.1 christos loop_redir_open_body() { 122 1.2 christos mkhelper for 3 "for x in x; do ${TEST_SH} ./for2; done 3>out" 123 1.1 christos atf_check -s exit:0 \ 124 1.1 christos -o empty \ 125 1.1 christos -e empty \ 126 1.2 christos ${TEST_SH} ./for1 127 1.1 christos cleanhelper for 128 1.1 christos } 129 1.1 christos 130 1.1 christos atf_test_case compound_redir_open 131 1.1 christos compound_redir_open_head() { 132 1.1 christos atf_set "descr" "Tests that redirections in compound statements don't close on exec" 133 1.1 christos } 134 1.1 christos compound_redir_open_body() { 135 1.2 christos mkhelper comp 3 "{ ${TEST_SH} ./comp2; } 3>out" 136 1.1 christos atf_check -s exit:0 \ 137 1.1 christos -o empty \ 138 1.1 christos -e empty \ 139 1.2 christos ${TEST_SH} ./comp1 140 1.1 christos cleanhelper comp 141 1.1 christos } 142 1.1 christos 143 1.1 christos atf_test_case simple_redir_open 144 1.1 christos simple_redir_open_head() { 145 1.1 christos atf_set "descr" "Tests that redirections in simple commands don't close on exec" 146 1.1 christos } 147 1.1 christos simple_redir_open_body() { 148 1.2 christos mkhelper simp 4 "${TEST_SH} ./simp2 4>out" 149 1.1 christos atf_check -s exit:0 \ 150 1.1 christos -o empty \ 151 1.1 christos -e empty \ 152 1.2 christos ${TEST_SH} ./simp1 153 1.1 christos cleanhelper simp 154 1.1 christos } 155 1.1 christos 156 1.2 christos atf_test_case subshell_redir_open 157 1.2 christos subshell_redir_open_head() { 158 1.2 christos atf_set "descr" "Tests that redirections on subshells don't close on exec" 159 1.2 christos } 160 1.2 christos subshell_redir_open_body() { 161 1.2 christos mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out" 162 1.2 christos atf_check -s exit:0 \ 163 1.2 christos -o empty \ 164 1.2 christos -e empty \ 165 1.2 christos ${TEST_SH} ./comp1 166 1.2 christos cleanhelper comp 167 1.2 christos } 168 1.2 christos 169 1.4 kre atf_test_case posix_exec_redir 170 1.4 kre posix_exec_redir_head() { 171 1.4 kre atf_set "descr" "Tests that redirections created by exec" \ 172 1.4 kre " in posix mode are not closed on exec" 173 1.4 kre } 174 1.4 kre posix_exec_redir_body() { 175 1.4 kre 176 1.4 kre # This test mostly just expects the opposite results than 177 1.4 kre # exec_redir_closed ... 178 1.4 kre 179 1.4 kre # First work out how to get shell into posix mode 180 1.4 kre POSIX= 181 1.4 kre 182 1.4 kre # This should succeed only if "set -o posix" succeeds. 183 1.4 kre # If it fails, whether it fails and exits the shell, or 184 1.4 kre # just returns a "false" from set (exit != 0), with or 185 1.4 kre # without errs on stderr, should not matter 186 1.4 kre 187 1.4 kre if ${TEST_SH} -c "set -o posix && exit 0 || exit 1" 2>/dev/null 188 1.4 kre then 189 1.4 kre # If we have this method, use it, as we can expect 190 1.4 kre # this really should mean the shell is in posix mode. 191 1.4 kre 192 1.4 kre POSIX='set -o posix;' 193 1.4 kre 194 1.4 kre else 195 1.4 kre # This one is just a guess, and there is no assurance 196 1.4 kre # that it will do anything at all. What's more, since 197 1.4 kre # we do not know what the shell being tested does 198 1.4 kre # differently in posix and non-posix modes, if it 199 1.4 kre # even has that concept, there's nothing we can test 200 1.4 kre # to find out. 201 1.4 kre 202 1.4 kre # A shell that always operates in posix mode (at least 203 1.4 kre # with regard to redirects on exec and close-on-exec 204 1.4 kre # should pass this test, in any case. 205 1.4 kre 206 1.4 kre POSIXLY_CORRECT=true ; export POSIXLY_CORRECT 207 1.4 kre 208 1.4 kre fi 209 1.4 kre 210 1.4 kre mkhelper exec 6 \ 211 1.4 kre "${POSIX} exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" 212 1.4 kre 213 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 214 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1 215 1.4 kre 216 1.4 kre mkhelper exec 9 \ 217 1.4 kre "${POSIX} exec 9> out; echo exec1 >&9; ${TEST_SH} exec2" 218 1.4 kre 219 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 220 1.4 kre 221 1.4 kre mkhelper exec 8 \ 222 1.4 kre "${POSIX}" \ 223 1.4 kre "exec 8> out; printf OK; echo exec1 >&8;" \ 224 1.4 kre "printf OK; ${TEST_SH} exec2; printf GOOD" 225 1.4 kre 226 1.4 kre atf_check -s exit:0 -o match:OKOKGOOD -e empty \ 227 1.4 kre ${TEST_SH} -e ./exec1 228 1.4 kre 229 1.4 kre mkhelper exec 7 \ 230 1.4 kre "${POSIX}" \ 231 1.4 kre "exec 7> out; printf OK; echo exec1 >&7;" \ 232 1.4 kre "printf OK; ${TEST_SH} exec2 || printf ERR" 233 1.4 kre 234 1.4 kre atf_check -s exit:0 -o match:OKOK -o not-match:ERR -e empty \ 235 1.4 kre ${TEST_SH} ./exec1 236 1.4 kre 237 1.4 kre cleanhelper exec 238 1.4 kre } 239 1.4 kre 240 1.1 christos atf_init_test_cases() { 241 1.1 christos atf_add_test_case exec_redir_closed 242 1.3 kre atf_add_test_case exec_redir_open 243 1.1 christos atf_add_test_case loop_redir_open 244 1.1 christos atf_add_test_case compound_redir_open 245 1.1 christos atf_add_test_case simple_redir_open 246 1.2 christos atf_add_test_case subshell_redir_open 247 1.4 kre atf_add_test_case posix_exec_redir 248 1.1 christos } 249