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