xref: /freebsd/contrib/netbsd-tests/bin/sh/t_redircloexec.sh (revision 0a36787e4c1fa0cf77dcf83be0867178476e372b)
1# $NetBSD: t_redircloexec.sh,v 1.3 2016/05/15 15:44:43 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
33mkhelper() {
34	name=$1
35	fd=$2
36	shift 2
37
38	echo "$@" > ./"${name}1"
39	echo "echo ${name}2" ">&${fd}" > ./"${name}2"
40}
41
42runhelper() {
43	${TEST_SH} "./${1}1"
44}
45
46cleanhelper() {
47	# not really needed, atf cleans up...
48	rm -f ./"${1}1" ./"${1}2" out
49}
50
51atf_test_case exec_redir_closed
52exec_redir_closed_head() {
53	atf_set "descr" "Tests that redirections created by exec are closed on exec"
54}
55exec_redir_closed_body() {
56
57	mkhelper exec 6 \
58		"exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
59
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
62
63	mkhelper exec 9 \
64		"exec 9> out; echo exec1 >&9; ${TEST_SH} exec2"
65
66	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} ./exec1
67
68	mkhelper exec 8 \
69		"exec 8> out; printf OK; echo exec1 >&8;" \
70		"printf OK; ${TEST_SH} exec2; printf ERR"
71
72	atf_check -s not-exit:0 -o match:OKOK -o not-match:ERR -e not-empty \
73		${TEST_SH} -e ./exec1
74
75	mkhelper exec 7 \
76		"exec 7> out; printf OK; echo exec1 >&7;" \
77		"printf OK; ${TEST_SH} exec2 || printf ERR"
78
79	atf_check -s exit:0 -o match:OKOKERR -e not-empty \
80		${TEST_SH} ./exec1
81
82	cleanhelper exec
83}
84
85atf_test_case exec_redir_open
86exec_redir_open_head() {
87	atf_set "descr" "Tests that redirections created by exec can remain open"
88}
89exec_redir_open_body() {
90
91	mkhelper exec 6 \
92		"exec 6> out 6>&6; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-"
93
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
96
97	mkhelper exec 9 \
98		"exec 9> out ; echo exec1 >&9; ${TEST_SH} exec2 9>&9"
99
100	atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1
101
102	mkhelper exec 8 \
103		"exec 8> out; printf OK; exec 8>&8; echo exec1 >&8;" \
104		"printf OK; ${TEST_SH} exec2; printf OK"
105
106	atf_check -s exit:0 -o match:OKOKOK -e empty \
107		${TEST_SH} -e ./exec1
108
109	mkhelper exec 7 \
110		"exec 7> out; printf OK; echo exec1 >&7;" \
111		"printf OK; ${TEST_SH} 7>&7 exec2; printf OK"
112
113	atf_check -s exit:0 -o match:OKOKOK -e empty \
114		${TEST_SH} -e ./exec1
115
116	cleanhelper exec
117}
118
119atf_test_case loop_redir_open
120loop_redir_open_head() {
121	atf_set "descr" "Tests that redirections in loops don't close on exec"
122}
123loop_redir_open_body() {
124	mkhelper for 3 "for x in x; do ${TEST_SH} ./for2; done 3>out"
125	atf_check -s exit:0 \
126		-o empty \
127		-e empty \
128		${TEST_SH} ./for1
129	cleanhelper for
130}
131
132atf_test_case compound_redir_open
133compound_redir_open_head() {
134	atf_set "descr" "Tests that redirections in compound statements don't close on exec"
135}
136compound_redir_open_body() {
137	mkhelper comp 3 "{ ${TEST_SH} ./comp2; } 3>out"
138	atf_check -s exit:0 \
139		-o empty \
140		-e empty \
141		${TEST_SH} ./comp1
142	cleanhelper comp
143}
144
145atf_test_case simple_redir_open
146simple_redir_open_head() {
147	atf_set "descr" "Tests that redirections in simple commands don't close on exec"
148}
149simple_redir_open_body() {
150	mkhelper simp 4 "${TEST_SH} ./simp2 4>out"
151	atf_check -s exit:0 \
152		-o empty \
153		-e empty \
154		${TEST_SH} ./simp1
155	cleanhelper simp
156}
157
158atf_test_case subshell_redir_open
159subshell_redir_open_head() {
160	atf_set "descr" "Tests that redirections on subshells don't close on exec"
161}
162subshell_redir_open_body() {
163	mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out"
164	atf_check -s exit:0 \
165		-o empty \
166		-e empty \
167		${TEST_SH} ./comp1
168	cleanhelper comp
169}
170
171atf_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
178}
179