1303980Sngie# $NetBSD: t_redircloexec.sh,v 1.3 2016/05/15 15:44:43 kre Exp $ 2303980Sngie# 3303980Sngie# Copyright (c) 2016 The NetBSD Foundation, Inc. 4303980Sngie# All rights reserved. 5303980Sngie# 6303980Sngie# This code is derived from software contributed to The NetBSD Foundation 7303980Sngie# by Christos Zoulas. 8303980Sngie# 9303980Sngie# Redistribution and use in source and binary forms, with or without 10303980Sngie# modification, are permitted provided that the following conditions 11303980Sngie# are met: 12303980Sngie# 1. Redistributions of source code must retain the above copyright 13303980Sngie# notice, this list of conditions and the following disclaimer. 14303980Sngie# 2. Redistributions in binary form must reproduce the above copyright 15303980Sngie# notice, this list of conditions and the following disclaimer in the 16303980Sngie# documentation and/or other materials provided with the distribution. 17303980Sngie# 18303980Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19303980Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20303980Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21303980Sngie# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22303980Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23303980Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24303980Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25303980Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26303980Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27303980Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28303980Sngie# POSSIBILITY OF SUCH DAMAGE. 29303980Sngie# 30303980Sngie# the implementation of "sh" to test 31303980Sngie: ${TEST_SH:="/bin/sh"} 32303980Sngie 33303980Sngiemkhelper() { 34303980Sngie name=$1 35303980Sngie fd=$2 36303980Sngie shift 2 37303980Sngie 38303980Sngie echo "$@" > ./"${name}1" 39303980Sngie echo "echo ${name}2" ">&${fd}" > ./"${name}2" 40303980Sngie} 41303980Sngie 42303980Sngierunhelper() { 43303980Sngie ${TEST_SH} "./${1}1" 44303980Sngie} 45303980Sngie 46303980Sngiecleanhelper() { 47303980Sngie # not really needed, atf cleans up... 48303980Sngie rm -f ./"${1}1" ./"${1}2" out 49303980Sngie} 50303980Sngie 51303980Sngieatf_test_case exec_redir_closed 52303980Sngieexec_redir_closed_head() { 53303980Sngie atf_set "descr" "Tests that redirections created by exec are closed on exec" 54303980Sngie} 55303980Sngieexec_redir_closed_body() { 56303980Sngie 57303980Sngie mkhelper exec 6 \ 58303980Sngie "exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" 59303980Sngie 60303980Sngie atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} ./exec1 61303980Sngie atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -e ./exec1 62303980Sngie 63303980Sngie mkhelper exec 9 \ 64303980Sngie "exec 9> out; echo exec1 >&9; ${TEST_SH} exec2" 65303980Sngie 66303980Sngie atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} ./exec1 67303980Sngie 68303980Sngie mkhelper exec 8 \ 69303980Sngie "exec 8> out; printf OK; echo exec1 >&8;" \ 70303980Sngie "printf OK; ${TEST_SH} exec2; printf ERR" 71303980Sngie 72303980Sngie atf_check -s not-exit:0 -o match:OKOK -o not-match:ERR -e not-empty \ 73303980Sngie ${TEST_SH} -e ./exec1 74303980Sngie 75303980Sngie mkhelper exec 7 \ 76303980Sngie "exec 7> out; printf OK; echo exec1 >&7;" \ 77303980Sngie "printf OK; ${TEST_SH} exec2 || printf ERR" 78303980Sngie 79303980Sngie atf_check -s exit:0 -o match:OKOKERR -e not-empty \ 80303980Sngie ${TEST_SH} ./exec1 81303980Sngie 82303980Sngie cleanhelper exec 83303980Sngie} 84303980Sngie 85303980Sngieatf_test_case exec_redir_open 86303980Sngieexec_redir_open_head() { 87303980Sngie atf_set "descr" "Tests that redirections created by exec can remain open" 88303980Sngie} 89303980Sngieexec_redir_open_body() { 90303980Sngie 91303980Sngie mkhelper exec 6 \ 92303980Sngie "exec 6> out 6>&6; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" 93303980Sngie 94303980Sngie atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 95303980Sngie atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1 96303980Sngie 97303980Sngie mkhelper exec 9 \ 98303980Sngie "exec 9> out ; echo exec1 >&9; ${TEST_SH} exec2 9>&9" 99303980Sngie 100303980Sngie atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 101303980Sngie 102303980Sngie mkhelper exec 8 \ 103303980Sngie "exec 8> out; printf OK; exec 8>&8; echo exec1 >&8;" \ 104303980Sngie "printf OK; ${TEST_SH} exec2; printf OK" 105303980Sngie 106303980Sngie atf_check -s exit:0 -o match:OKOKOK -e empty \ 107303980Sngie ${TEST_SH} -e ./exec1 108303980Sngie 109303980Sngie mkhelper exec 7 \ 110303980Sngie "exec 7> out; printf OK; echo exec1 >&7;" \ 111303980Sngie "printf OK; ${TEST_SH} 7>&7 exec2; printf OK" 112303980Sngie 113303980Sngie atf_check -s exit:0 -o match:OKOKOK -e empty \ 114303980Sngie ${TEST_SH} -e ./exec1 115303980Sngie 116303980Sngie cleanhelper exec 117303980Sngie} 118303980Sngie 119303980Sngieatf_test_case loop_redir_open 120303980Sngieloop_redir_open_head() { 121303980Sngie atf_set "descr" "Tests that redirections in loops don't close on exec" 122303980Sngie} 123303980Sngieloop_redir_open_body() { 124303980Sngie mkhelper for 3 "for x in x; do ${TEST_SH} ./for2; done 3>out" 125303980Sngie atf_check -s exit:0 \ 126303980Sngie -o empty \ 127303980Sngie -e empty \ 128303980Sngie ${TEST_SH} ./for1 129303980Sngie cleanhelper for 130303980Sngie} 131303980Sngie 132303980Sngieatf_test_case compound_redir_open 133303980Sngiecompound_redir_open_head() { 134303980Sngie atf_set "descr" "Tests that redirections in compound statements don't close on exec" 135303980Sngie} 136303980Sngiecompound_redir_open_body() { 137303980Sngie mkhelper comp 3 "{ ${TEST_SH} ./comp2; } 3>out" 138303980Sngie atf_check -s exit:0 \ 139303980Sngie -o empty \ 140303980Sngie -e empty \ 141303980Sngie ${TEST_SH} ./comp1 142303980Sngie cleanhelper comp 143303980Sngie} 144303980Sngie 145303980Sngieatf_test_case simple_redir_open 146303980Sngiesimple_redir_open_head() { 147303980Sngie atf_set "descr" "Tests that redirections in simple commands don't close on exec" 148303980Sngie} 149303980Sngiesimple_redir_open_body() { 150303980Sngie mkhelper simp 4 "${TEST_SH} ./simp2 4>out" 151303980Sngie atf_check -s exit:0 \ 152303980Sngie -o empty \ 153303980Sngie -e empty \ 154303980Sngie ${TEST_SH} ./simp1 155303980Sngie cleanhelper simp 156303980Sngie} 157303980Sngie 158303980Sngieatf_test_case subshell_redir_open 159303980Sngiesubshell_redir_open_head() { 160303980Sngie atf_set "descr" "Tests that redirections on subshells don't close on exec" 161303980Sngie} 162303980Sngiesubshell_redir_open_body() { 163303980Sngie mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out" 164303980Sngie atf_check -s exit:0 \ 165303980Sngie -o empty \ 166303980Sngie -e empty \ 167303980Sngie ${TEST_SH} ./comp1 168303980Sngie cleanhelper comp 169303980Sngie} 170303980Sngie 171303980Sngieatf_init_test_cases() { 172303980Sngie atf_add_test_case exec_redir_closed 173303980Sngie atf_add_test_case exec_redir_open 174303980Sngie atf_add_test_case loop_redir_open 175303980Sngie atf_add_test_case compound_redir_open 176303980Sngie atf_add_test_case simple_redir_open 177303980Sngie atf_add_test_case subshell_redir_open 178303980Sngie} 179