t_redircloexec.sh revision 1.4 1 1.4 kre # $NetBSD: t_redircloexec.sh,v 1.4 2017/05/14 17:28:46 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.2 christos mkhelper exec 6 \
54 1.2 christos "exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
55 1.2 christos
56 1.2 christos atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} ./exec1
57 1.2 christos atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -e ./exec1
58 1.2 christos
59 1.2 christos mkhelper exec 9 \
60 1.2 christos "exec 9> out; echo exec1 >&9; ${TEST_SH} exec2"
61 1.2 christos
62 1.2 christos atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} ./exec1
63 1.2 christos
64 1.2 christos mkhelper exec 8 \
65 1.2 christos "exec 8> out; printf OK; echo exec1 >&8;" \
66 1.2 christos "printf OK; ${TEST_SH} exec2; printf ERR"
67 1.2 christos
68 1.2 christos atf_check -s not-exit:0 -o match:OKOK -o not-match:ERR -e not-empty \
69 1.2 christos ${TEST_SH} -e ./exec1
70 1.2 christos
71 1.2 christos mkhelper exec 7 \
72 1.2 christos "exec 7> out; printf OK; echo exec1 >&7;" \
73 1.2 christos "printf OK; ${TEST_SH} exec2 || printf ERR"
74 1.2 christos
75 1.2 christos atf_check -s exit:0 -o match:OKOKERR -e not-empty \
76 1.2 christos ${TEST_SH} ./exec1
77 1.2 christos
78 1.1 christos cleanhelper exec
79 1.1 christos }
80 1.1 christos
81 1.3 kre atf_test_case exec_redir_open
82 1.3 kre exec_redir_open_head() {
83 1.3 kre atf_set "descr" "Tests that redirections created by exec can remain open"
84 1.3 kre }
85 1.3 kre exec_redir_open_body() {
86 1.3 kre
87 1.3 kre mkhelper exec 6 \
88 1.3 kre "exec 6> out 6>&6; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
89 1.3 kre
90 1.3 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
91 1.3 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1
92 1.3 kre
93 1.3 kre mkhelper exec 9 \
94 1.3 kre "exec 9> out ; echo exec1 >&9; ${TEST_SH} exec2 9>&9"
95 1.3 kre
96 1.3 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
97 1.3 kre
98 1.3 kre mkhelper exec 8 \
99 1.3 kre "exec 8> out; printf OK; exec 8>&8; echo exec1 >&8;" \
100 1.3 kre "printf OK; ${TEST_SH} exec2; printf OK"
101 1.3 kre
102 1.3 kre atf_check -s exit:0 -o match:OKOKOK -e empty \
103 1.3 kre ${TEST_SH} -e ./exec1
104 1.3 kre
105 1.3 kre mkhelper exec 7 \
106 1.3 kre "exec 7> out; printf OK; echo exec1 >&7;" \
107 1.3 kre "printf OK; ${TEST_SH} 7>&7 exec2; printf OK"
108 1.3 kre
109 1.3 kre atf_check -s exit:0 -o match:OKOKOK -e empty \
110 1.3 kre ${TEST_SH} -e ./exec1
111 1.3 kre
112 1.3 kre cleanhelper exec
113 1.3 kre }
114 1.3 kre
115 1.1 christos atf_test_case loop_redir_open
116 1.1 christos loop_redir_open_head() {
117 1.1 christos atf_set "descr" "Tests that redirections in loops don't close on exec"
118 1.1 christos }
119 1.1 christos loop_redir_open_body() {
120 1.2 christos mkhelper for 3 "for x in x; do ${TEST_SH} ./for2; done 3>out"
121 1.1 christos atf_check -s exit:0 \
122 1.1 christos -o empty \
123 1.1 christos -e empty \
124 1.2 christos ${TEST_SH} ./for1
125 1.1 christos cleanhelper for
126 1.1 christos }
127 1.1 christos
128 1.1 christos atf_test_case compound_redir_open
129 1.1 christos compound_redir_open_head() {
130 1.1 christos atf_set "descr" "Tests that redirections in compound statements don't close on exec"
131 1.1 christos }
132 1.1 christos compound_redir_open_body() {
133 1.2 christos mkhelper comp 3 "{ ${TEST_SH} ./comp2; } 3>out"
134 1.1 christos atf_check -s exit:0 \
135 1.1 christos -o empty \
136 1.1 christos -e empty \
137 1.2 christos ${TEST_SH} ./comp1
138 1.1 christos cleanhelper comp
139 1.1 christos }
140 1.1 christos
141 1.1 christos atf_test_case simple_redir_open
142 1.1 christos simple_redir_open_head() {
143 1.1 christos atf_set "descr" "Tests that redirections in simple commands don't close on exec"
144 1.1 christos }
145 1.1 christos simple_redir_open_body() {
146 1.2 christos mkhelper simp 4 "${TEST_SH} ./simp2 4>out"
147 1.1 christos atf_check -s exit:0 \
148 1.1 christos -o empty \
149 1.1 christos -e empty \
150 1.2 christos ${TEST_SH} ./simp1
151 1.1 christos cleanhelper simp
152 1.1 christos }
153 1.1 christos
154 1.2 christos atf_test_case subshell_redir_open
155 1.2 christos subshell_redir_open_head() {
156 1.2 christos atf_set "descr" "Tests that redirections on subshells don't close on exec"
157 1.2 christos }
158 1.2 christos subshell_redir_open_body() {
159 1.2 christos mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out"
160 1.2 christos atf_check -s exit:0 \
161 1.2 christos -o empty \
162 1.2 christos -e empty \
163 1.2 christos ${TEST_SH} ./comp1
164 1.2 christos cleanhelper comp
165 1.2 christos }
166 1.2 christos
167 1.4 kre atf_test_case posix_exec_redir
168 1.4 kre posix_exec_redir_head() {
169 1.4 kre atf_set "descr" "Tests that redirections created by exec" \
170 1.4 kre " in posix mode are not closed on exec"
171 1.4 kre }
172 1.4 kre posix_exec_redir_body() {
173 1.4 kre
174 1.4 kre # This test mostly just expects the opposite results than
175 1.4 kre # exec_redir_closed ...
176 1.4 kre
177 1.4 kre # First work out how to get shell into posix mode
178 1.4 kre POSIX=
179 1.4 kre
180 1.4 kre # This should succeed only if "set -o posix" succeeds.
181 1.4 kre # If it fails, whether it fails and exits the shell, or
182 1.4 kre # just returns a "false" from set (exit != 0), with or
183 1.4 kre # without errs on stderr, should not matter
184 1.4 kre
185 1.4 kre if ${TEST_SH} -c "set -o posix && exit 0 || exit 1" 2>/dev/null
186 1.4 kre then
187 1.4 kre # If we have this method, use it, as we can expect
188 1.4 kre # this really should mean the shell is in posix mode.
189 1.4 kre
190 1.4 kre POSIX='set -o posix;'
191 1.4 kre
192 1.4 kre else
193 1.4 kre # This one is just a guess, and there is no assurance
194 1.4 kre # that it will do anything at all. What's more, since
195 1.4 kre # we do not know what the shell being tested does
196 1.4 kre # differently in posix and non-posix modes, if it
197 1.4 kre # even has that concept, there's nothing we can test
198 1.4 kre # to find out.
199 1.4 kre
200 1.4 kre # A shell that always operates in posix mode (at least
201 1.4 kre # with regard to redirects on exec and close-on-exec
202 1.4 kre # should pass this test, in any case.
203 1.4 kre
204 1.4 kre POSIXLY_CORRECT=true ; export POSIXLY_CORRECT
205 1.4 kre
206 1.4 kre fi
207 1.4 kre
208 1.4 kre mkhelper exec 6 \
209 1.4 kre "${POSIX} exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
210 1.4 kre
211 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
212 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1
213 1.4 kre
214 1.4 kre mkhelper exec 9 \
215 1.4 kre "${POSIX} exec 9> out; echo exec1 >&9; ${TEST_SH} exec2"
216 1.4 kre
217 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
218 1.4 kre
219 1.4 kre mkhelper exec 8 \
220 1.4 kre "${POSIX}" \
221 1.4 kre "exec 8> out; printf OK; echo exec1 >&8;" \
222 1.4 kre "printf OK; ${TEST_SH} exec2; printf GOOD"
223 1.4 kre
224 1.4 kre atf_check -s exit:0 -o match:OKOKGOOD -e empty \
225 1.4 kre ${TEST_SH} -e ./exec1
226 1.4 kre
227 1.4 kre mkhelper exec 7 \
228 1.4 kre "${POSIX}" \
229 1.4 kre "exec 7> out; printf OK; echo exec1 >&7;" \
230 1.4 kre "printf OK; ${TEST_SH} exec2 || printf ERR"
231 1.4 kre
232 1.4 kre atf_check -s exit:0 -o match:OKOK -o not-match:ERR -e empty \
233 1.4 kre ${TEST_SH} ./exec1
234 1.4 kre
235 1.4 kre cleanhelper exec
236 1.4 kre }
237 1.4 kre
238 1.1 christos atf_init_test_cases() {
239 1.1 christos atf_add_test_case exec_redir_closed
240 1.3 kre atf_add_test_case exec_redir_open
241 1.1 christos atf_add_test_case loop_redir_open
242 1.1 christos atf_add_test_case compound_redir_open
243 1.1 christos atf_add_test_case simple_redir_open
244 1.2 christos atf_add_test_case subshell_redir_open
245 1.4 kre atf_add_test_case posix_exec_redir
246 1.1 christos }
247