t_redircloexec.sh revision 1.2 1 # $NetBSD: t_redircloexec.sh,v 1.2 2016/03/16 21:13:51 christos 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 runhelper() {
43 ${TEST_SH} "./${1}1"
44 }
45
46 cleanhelper() {
47 # not really needed, atf cleans up...
48 rm -f ./"${1}1" ./"${1}2" out
49 }
50
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"
54 }
55 exec_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
85 atf_test_case loop_redir_open
86 loop_redir_open_head() {
87 atf_set "descr" "Tests that redirections in loops don't close on exec"
88 }
89 loop_redir_open_body() {
90 mkhelper for 3 "for x in x; do ${TEST_SH} ./for2; done 3>out"
91 atf_check -s exit:0 \
92 -o empty \
93 -e empty \
94 ${TEST_SH} ./for1
95 cleanhelper for
96 }
97
98 atf_test_case compound_redir_open
99 compound_redir_open_head() {
100 atf_set "descr" "Tests that redirections in compound statements don't close on exec"
101 }
102 compound_redir_open_body() {
103 mkhelper comp 3 "{ ${TEST_SH} ./comp2; } 3>out"
104 atf_check -s exit:0 \
105 -o empty \
106 -e empty \
107 ${TEST_SH} ./comp1
108 cleanhelper comp
109 }
110
111 atf_test_case simple_redir_open
112 simple_redir_open_head() {
113 atf_set "descr" "Tests that redirections in simple commands don't close on exec"
114 }
115 simple_redir_open_body() {
116 mkhelper simp 4 "${TEST_SH} ./simp2 4>out"
117 atf_check -s exit:0 \
118 -o empty \
119 -e empty \
120 ${TEST_SH} ./simp1
121 cleanhelper simp
122 }
123
124 atf_test_case subshell_redir_open
125 subshell_redir_open_head() {
126 atf_set "descr" "Tests that redirections on subshells don't close on exec"
127 }
128 subshell_redir_open_body() {
129 mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out"
130 atf_check -s exit:0 \
131 -o empty \
132 -e empty \
133 ${TEST_SH} ./comp1
134 cleanhelper comp
135 }
136
137 atf_init_test_cases() {
138 atf_add_test_case exec_redir_closed
139 atf_add_test_case loop_redir_open
140 atf_add_test_case compound_redir_open
141 atf_add_test_case simple_redir_open
142 atf_add_test_case subshell_redir_open
143 }
144