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