Home | History | Annotate | Line # | Download | only in sh
t_redircloexec.sh revision 1.1
      1  1.1  christos # $NetBSD: t_redircloexec.sh,v 1.1 2016/03/13 18:55:12 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.1  christos 	local name="$1"
     35  1.1  christos 	local fd="$2"
     36  1.1  christos 	shift 2
     37  1.1  christos 	(echo "#!${TEST_SH}"; echo "$@") > ./"$name"1
     38  1.1  christos 	(echo "#!${TEST_SH}"; echo "echo $name"2 ">&$fd") > ./"$name"2
     39  1.1  christos 	chmod +x ./"$name"1 ./"$name"2
     40  1.1  christos }
     41  1.1  christos 
     42  1.1  christos runhelper() {
     43  1.1  christos 	local name="$1"
     44  1.1  christos 	shift
     45  1.1  christos 
     46  1.1  christos 	"./$name"1
     47  1.1  christos }
     48  1.1  christos 
     49  1.1  christos cleanhelper() {
     50  1.1  christos 	local name="$1"
     51  1.1  christos 	shift
     52  1.1  christos 	rm -f ./"$name"1 ./"$name"2
     53  1.1  christos }
     54  1.1  christos 
     55  1.1  christos atf_test_case exec_redir_closed
     56  1.1  christos exec_redir_closed_head() {
     57  1.1  christos 	atf_set "descr" "Tests that redirections created by exec are closed on exec"
     58  1.1  christos }
     59  1.1  christos exec_redir_closed_body() {
     60  1.1  christos 	mkhelper exec 6 "exec 6> out; echo exec1 >&6; ./exec2; exec 6>&-"
     61  1.1  christos 	atf_check -s exit:0 \
     62  1.1  christos 		-o empty \
     63  1.1  christos 		-e match:"./exec2: 6: Bad file descriptor" \
     64  1.1  christos 		./exec1
     65  1.1  christos 	cleanhelper exec
     66  1.1  christos }
     67  1.1  christos 
     68  1.1  christos atf_test_case loop_redir_open
     69  1.1  christos loop_redir_open_head() {
     70  1.1  christos 	atf_set "descr" "Tests that redirections in loops don't close on exec"
     71  1.1  christos }
     72  1.1  christos loop_redir_open_body() {
     73  1.1  christos 	mkhelper for 3 "for x in x; do ./for2; done 3>out"
     74  1.1  christos 	atf_check -s exit:0 \
     75  1.1  christos 		-o empty \
     76  1.1  christos 		-e empty \
     77  1.1  christos 		./for1
     78  1.1  christos 	cleanhelper for
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos atf_test_case compound_redir_open
     82  1.1  christos compound_redir_open_head() {
     83  1.1  christos 	atf_set "descr" "Tests that redirections in compound statements don't close on exec"
     84  1.1  christos }
     85  1.1  christos compound_redir_open_body() {
     86  1.1  christos 	mkhelper comp 3 "{ ./comp2; } 3>out"
     87  1.1  christos 	atf_check -s exit:0 \
     88  1.1  christos 		-o empty \
     89  1.1  christos 		-e empty \
     90  1.1  christos 		./comp1
     91  1.1  christos 	cleanhelper comp
     92  1.1  christos }
     93  1.1  christos 
     94  1.1  christos atf_test_case simple_redir_open
     95  1.1  christos simple_redir_open_head() {
     96  1.1  christos 	atf_set "descr" "Tests that redirections in simple commands don't close on exec"
     97  1.1  christos }
     98  1.1  christos simple_redir_open_body() {
     99  1.1  christos 	mkhelper simp 6 "./simp2 6>out"
    100  1.1  christos 	atf_check -s exit:0 \
    101  1.1  christos 		-o empty \
    102  1.1  christos 		-e empty \
    103  1.1  christos 		./simp1
    104  1.1  christos 	cleanhelper simp
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos atf_init_test_cases() {
    108  1.1  christos 	atf_add_test_case exec_redir_closed
    109  1.1  christos 	atf_add_test_case loop_redir_open
    110  1.1  christos 	atf_add_test_case compound_redir_open
    111  1.1  christos 	atf_add_test_case simple_redir_open
    112  1.1  christos }
    113