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