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