xref: /netbsd-src/tests/bin/sh/t_exit.sh (revision 44b399896032322aeee8074a1d46378872d32bdf)
1*44b39989Skre# $NetBSD: t_exit.sh,v 1.6 2016/05/07 23:51:30 kre Exp $
228604916Sjruoho#
328604916Sjruoho# Copyright (c) 2007 The NetBSD Foundation, Inc.
428604916Sjruoho# All rights reserved.
528604916Sjruoho#
628604916Sjruoho# Redistribution and use in source and binary forms, with or without
728604916Sjruoho# modification, are permitted provided that the following conditions
828604916Sjruoho# are met:
928604916Sjruoho# 1. Redistributions of source code must retain the above copyright
1028604916Sjruoho#    notice, this list of conditions and the following disclaimer.
1128604916Sjruoho# 2. Redistributions in binary form must reproduce the above copyright
1228604916Sjruoho#    notice, this list of conditions and the following disclaimer in the
1328604916Sjruoho#    documentation and/or other materials provided with the distribution.
1428604916Sjruoho#
1528604916Sjruoho# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1628604916Sjruoho# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1728604916Sjruoho# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1828604916Sjruoho# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1928604916Sjruoho# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2028604916Sjruoho# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2128604916Sjruoho# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2228604916Sjruoho# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2328604916Sjruoho# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2428604916Sjruoho# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2528604916Sjruoho# POSSIBILITY OF SUCH DAMAGE.
2628604916Sjruoho#
27b189a005Schristos# the implementation of "sh" to test
28b189a005Schristos: ${TEST_SH:="/bin/sh"}
2928604916Sjruoho
3028604916Sjruoho
31144c5468Sjruohoatf_test_case background
32144c5468Sjruohobackground_head() {
33144c5468Sjruoho	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
34144c5468Sjruoho			"a command in the background (PR bin/46327)"
35144c5468Sjruoho}
36144c5468Sjruohobackground_body() {
3706f9bef6Schristos	atf_check -o match:0 -e empty ${TEST_SH} -c 'true; true & echo $?'
38b189a005Schristos	# atf_expect_fail "PR bin/46327" (now fixed?)
3906f9bef6Schristos	atf_check -o match:0 -e empty ${TEST_SH} -c 'false; true & echo $?'
40144c5468Sjruoho}
41144c5468Sjruoho
4228604916Sjruohoatf_test_case function
4328604916Sjruohofunction_head() {
4428604916Sjruoho	atf_set "descr" "Tests that \$? is correctly updated inside " \
4528604916Sjruoho			"a function"
4628604916Sjruoho}
4728604916Sjruohofunction_body() {
48b189a005Schristos	atf_check -s exit:0 -o match:STATUS=1-0 -e empty \
4906f9bef6Schristos		${TEST_SH} -c '
50b189a005Schristos			crud() {
51b189a005Schristos				test yes = no
52b189a005Schristos
53b189a005Schristos				cat <<-EOF
54b189a005Schristos				STATUS=$?
55b189a005Schristos				EOF
56b189a005Schristos			}
57b189a005Schristos			foo=$(crud)
58b189a005Schristos			echo "${foo}-$?"
59b189a005Schristos		'
6028604916Sjruoho}
6128604916Sjruoho
6228604916Sjruohoatf_test_case readout
6328604916Sjruohoreadout_head() {
6428604916Sjruoho	atf_set "descr" "Tests that \$? is correctly updated in a " \
6528604916Sjruoho			"compound expression"
6628604916Sjruoho}
6728604916Sjruohoreadout_body() {
68b189a005Schristos	atf_check -s exit:0 -o match:0 -e empty \
6906f9bef6Schristos		${TEST_SH} -c 'true && ! true | false; echo $?'
7028604916Sjruoho}
7128604916Sjruoho
7228604916Sjruohoatf_test_case trap_subshell
7328604916Sjruohotrap_subshell_head() {
7428604916Sjruoho	atf_set "descr" "Tests that the trap statement in a subshell " \
7528604916Sjruoho			"works when the subshell exits"
7628604916Sjruoho}
7728604916Sjruohotrap_subshell_body() {
78b189a005Schristos	atf_check -s exit:0 -o inline:'exiting\n' -e empty \
79b189a005Schristos	    ${TEST_SH} -c '( trap "echo exiting" EXIT; /usr/bin/true )'
8028604916Sjruoho}
8128604916Sjruoho
8228604916Sjruohoatf_test_case trap_zero__implicit_exit
83b189a005Schristostrap_zero__implicit_exit_head() {
84b189a005Schristos	atf_set "descr" "Tests that the trap statement in a subshell in a " \
85b189a005Schristos		"script works when the subshell simply runs out of commands"
86b189a005Schristos}
8728604916Sjruohotrap_zero__implicit_exit_body() {
88b189a005Schristos	# PR bin/6764: sh works but ksh does not
8928604916Sjruoho	echo '( trap "echo exiting" 0 )' >helper.sh
9006f9bef6Schristos	atf_check -s exit:0 -o match:exiting -e empty ${TEST_SH} helper.sh
91b189a005Schristos	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
92b189a005Schristos	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
9328604916Sjruoho}
9428604916Sjruoho
9528604916Sjruohoatf_test_case trap_zero__explicit_exit
96b189a005Schristostrap_zero__explicit_exit_head() {
97b189a005Schristos	atf_set "descr" "Tests that the trap statement in a subshell in a " \
98b189a005Schristos		"script works when the subshell executes an explicit exit"
99b189a005Schristos}
10028604916Sjruohotrap_zero__explicit_exit_body() {
101b189a005Schristos	echo '( trap "echo exiting" 0; exit; echo NO_NO_NO )' >helper.sh
102b189a005Schristos	atf_check -s exit:0 -o match:exiting -o not-match:NO_NO -e empty \
10306f9bef6Schristos		${TEST_SH} helper.sh
104b189a005Schristos	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
105b189a005Schristos	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
10628604916Sjruoho}
10728604916Sjruoho
108b189a005Schristosatf_test_case simple_exit
109b189a005Schristossimple_exit_head() {
110b189a005Schristos	atf_set "descr" "Tests that various values for exit status work"
111b189a005Schristos}
112b189a005Schristos# Note: ATF will not allow tests of exit values > 255, even if they would work
113b189a005Schristossimple_exit_body() {
114b189a005Schristos	for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
115b189a005Schristos	do
116b189a005Schristos		atf_check -s exit:$N -o empty -e empty \
11706f9bef6Schristos			${TEST_SH} -c "exit $N; echo FOO; echo BAR >&2"
118b189a005Schristos	done
119b189a005Schristos}
120b189a005Schristos
121b189a005Schristosatf_test_case subshell_exit
122b189a005Schristossubshell_exit_head() {
123b189a005Schristos	atf_set "descr" "Tests that subshell exit status works and \$? gets it"
124b189a005Schristos}
125b189a005Schristos# Note: ATF will not allow tests of exit values > 255, even if they would work
126b189a005Schristossubshell_exit_body() {
127b189a005Schristos	for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
128b189a005Schristos	do
129b189a005Schristos		atf_check -s exit:0 -o empty -e empty \
13006f9bef6Schristos			${TEST_SH} -c "(exit $N); test \$? -eq $N"
131b189a005Schristos	done
132b189a005Schristos}
133b189a005Schristos
134b189a005Schristosatf_test_case subshell_background
135b189a005Schristossubshell_background_head() {
136b189a005Schristos	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
137b189a005Schristos			"a subshell in the background"
138b189a005Schristos}
139b189a005Schristossubshell_background_body() {
140b189a005Schristos	atf_check -o match:0 -e empty \
14106f9bef6Schristos		${TEST_SH} -c 'true; (false || true) & echo $?'
142b189a005Schristos	# atf_expect_fail "PR bin/46327" (now fixed?)
143b189a005Schristos	atf_check -o match:0 -e empty \
14406f9bef6Schristos		${TEST_SH} -c 'false; (false || true) & echo $?'
14528604916Sjruoho}
14628604916Sjruoho
14728604916Sjruohoatf_init_test_cases() {
148144c5468Sjruoho	atf_add_test_case background
14928604916Sjruoho	atf_add_test_case function
15028604916Sjruoho	atf_add_test_case readout
15128604916Sjruoho	atf_add_test_case trap_subshell
15228604916Sjruoho	atf_add_test_case trap_zero__implicit_exit
15328604916Sjruoho	atf_add_test_case trap_zero__explicit_exit
154b189a005Schristos	atf_add_test_case simple_exit
155b189a005Schristos	atf_add_test_case subshell_exit
156b189a005Schristos	atf_add_test_case subshell_background
15728604916Sjruoho}
158