1 # $NetBSD: t_redircloexec.sh,v 1.3 2016/05/15 15:44:43 kre Exp $
3 # Copyright (c) 2016 The NetBSD Foundation, Inc.
6 # This code is derived from software contributed to The NetBSD Foundation
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
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.
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.
30 # the implementation of "sh" to test
31 : ${TEST_SH:="/bin/sh"}
38 echo "$@" > ./"${name}1"
39 echo "echo ${name}2" ">&${fd}" > ./"${name}2"
47 # not really needed, atf cleans up...
48 rm -f ./"${1}1" ./"${1}2" out
51 atf_test_case exec_redir_closed
52 exec_redir_closed_head() {
53 atf_set "descr" "Tests that redirections created by exec are closed on exec"
55 exec_redir_closed_body() {
58 "exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
60 atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} ./exec1
61 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -e ./exec1
64 "exec 9> out; echo exec1 >&9; ${TEST_SH} exec2"
66 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} ./exec1
69 "exec 8> out; printf OK; echo exec1 >&8;" \
70 "printf OK; ${TEST_SH} exec2; printf ERR"
72 atf_check -s not-exit:0 -o match:OKOK -o not-match:ERR -e not-empty \
76 "exec 7> out; printf OK; echo exec1 >&7;" \
77 "printf OK; ${TEST_SH} exec2 || printf ERR"
79 atf_check -s exit:0 -o match:OKOKERR -e not-empty \
85 atf_test_case exec_redir_open
86 exec_redir_open_head() {
87 atf_set "descr" "Tests that redirections created by exec can remain open"
89 exec_redir_open_body() {
92 "exec 6> out 6>&6; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
94 atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
95 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1
98 "exec 9> out ; echo exec1 >&9; ${TEST_SH} exec2 9>&9"
100 atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
103 "exec 8> out; printf OK; exec 8>&8; echo exec1 >&8;" \
104 "printf OK; ${TEST_SH} exec2; printf OK"
106 atf_check -s exit:0 -o match:OKOKOK -e empty \
107 ${TEST_SH} -e ./exec1
110 "exec 7> out; printf OK; echo exec1 >&7;" \
111 "printf OK; ${TEST_SH} 7>&7 exec2; printf OK"
113 atf_check -s exit:0 -o match:OKOKOK -e empty \
114 ${TEST_SH} -e ./exec1
119 atf_test_case loop_redir_open
120 loop_redir_open_head() {
121 atf_set "descr" "Tests that redirections in loops don't close on exec"
123 loop_redir_open_body() {
124 mkhelper for 3 "for x in x; do ${TEST_SH} ./for2; done 3>out"
125 atf_check -s exit:0 \
132 atf_test_case compound_redir_open
133 compound_redir_open_head() {
134 atf_set "descr" "Tests that redirections in compound statements don't close on exec"
136 compound_redir_open_body() {
137 mkhelper comp 3 "{ ${TEST_SH} ./comp2; } 3>out"
138 atf_check -s exit:0 \
145 atf_test_case simple_redir_open
146 simple_redir_open_head() {
147 atf_set "descr" "Tests that redirections in simple commands don't close on exec"
149 simple_redir_open_body() {
150 mkhelper simp 4 "${TEST_SH} ./simp2 4>out"
151 atf_check -s exit:0 \
158 atf_test_case subshell_redir_open
159 subshell_redir_open_head() {
160 atf_set "descr" "Tests that redirections on subshells don't close on exec"
162 subshell_redir_open_body() {
163 mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out"
164 atf_check -s exit:0 \
171 atf_init_test_cases() {
172 atf_add_test_case exec_redir_closed
173 atf_add_test_case exec_redir_open
174 atf_add_test_case loop_redir_open
175 atf_add_test_case compound_redir_open
176 atf_add_test_case simple_redir_open
177 atf_add_test_case subshell_redir_open