Changeset View
Changeset View
Standalone View
Standalone View
bin/sh/t_redircloexec.sh
Property | Old Value | New Value |
---|---|---|
File Mode | 100755 | 100644 |
# $NetBSD: t_redircloexec.sh,v 1.3 2016/05/15 15:44:43 kre Exp $ | # $NetBSD: t_redircloexec.sh,v 1.5 2017/05/27 13:11:50 kre Exp $ | ||||
# | # | ||||
# Copyright (c) 2016 The NetBSD Foundation, Inc. | # Copyright (c) 2016 The NetBSD Foundation, Inc. | ||||
# All rights reserved. | # All rights reserved. | ||||
# | # | ||||
# This code is derived from software contributed to The NetBSD Foundation | # This code is derived from software contributed to The NetBSD Foundation | ||||
# by Christos Zoulas. | # by Christos Zoulas. | ||||
# | # | ||||
# Redistribution and use in source and binary forms, with or without | # Redistribution and use in source and binary forms, with or without | ||||
Show All 24 Lines | mkhelper() { | ||||
name=$1 | name=$1 | ||||
fd=$2 | fd=$2 | ||||
shift 2 | shift 2 | ||||
echo "$@" > ./"${name}1" | echo "$@" > ./"${name}1" | ||||
echo "echo ${name}2" ">&${fd}" > ./"${name}2" | echo "echo ${name}2" ">&${fd}" > ./"${name}2" | ||||
} | } | ||||
runhelper() { | |||||
${TEST_SH} "./${1}1" | |||||
} | |||||
cleanhelper() { | cleanhelper() { | ||||
# not really needed, atf cleans up... | # not really needed, atf cleans up... | ||||
rm -f ./"${1}1" ./"${1}2" out | rm -f ./"${1}1" ./"${1}2" out | ||||
} | } | ||||
atf_test_case exec_redir_closed | atf_test_case exec_redir_closed | ||||
exec_redir_closed_head() { | exec_redir_closed_head() { | ||||
atf_set "descr" "Tests that redirections created by exec are closed on exec" | atf_set "descr" "Tests that redirections created by exec are closed on exec" | ||||
} | } | ||||
exec_redir_closed_body() { | exec_redir_closed_body() { | ||||
[ -n "${POSIXLY_CORRECT+set}" ] && atf_skip "tests non-posix behaviour" | |||||
mkhelper exec 6 \ | mkhelper exec 6 \ | ||||
"exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" | "exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" | ||||
atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} ./exec1 | atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} ./exec1 | ||||
atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -e ./exec1 | atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -e ./exec1 | ||||
mkhelper exec 9 \ | mkhelper exec 9 \ | ||||
"exec 9> out; echo exec1 >&9; ${TEST_SH} exec2" | "exec 9> out; echo exec1 >&9; ${TEST_SH} exec2" | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | subshell_redir_open_body() { | ||||
mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out" | mkhelper comp 5 "( ${TEST_SH} ./comp2; ${TEST_SH} ./comp2 ) 5>out" | ||||
atf_check -s exit:0 \ | atf_check -s exit:0 \ | ||||
-o empty \ | -o empty \ | ||||
-e empty \ | -e empty \ | ||||
${TEST_SH} ./comp1 | ${TEST_SH} ./comp1 | ||||
cleanhelper comp | cleanhelper comp | ||||
} | } | ||||
atf_test_case posix_exec_redir | |||||
posix_exec_redir_head() { | |||||
atf_set "descr" "Tests that redirections created by exec" \ | |||||
" in posix mode are not closed on exec" | |||||
} | |||||
posix_exec_redir_body() { | |||||
# This test mostly just expects the opposite results than | |||||
# exec_redir_closed ... | |||||
# First work out how to get shell into posix mode | |||||
POSIX= | |||||
# This should succeed only if "set -o posix" succeeds. | |||||
# If it fails, whether it fails and exits the shell, or | |||||
# just returns a "false" from set (exit != 0), with or | |||||
# without errs on stderr, should not matter | |||||
if ${TEST_SH} -c "set -o posix && exit 0 || exit 1" 2>/dev/null | |||||
then | |||||
# If we have this method, use it, as we can expect | |||||
# this really should mean the shell is in posix mode. | |||||
POSIX='set -o posix;' | |||||
else | |||||
# This one is just a guess, and there is no assurance | |||||
# that it will do anything at all. What's more, since | |||||
# we do not know what the shell being tested does | |||||
# differently in posix and non-posix modes, if it | |||||
# even has that concept, there's nothing we can test | |||||
# to find out. | |||||
# A shell that always operates in posix mode (at least | |||||
# with regard to redirects on exec and close-on-exec | |||||
# should pass this test, in any case. | |||||
POSIXLY_CORRECT=true ; export POSIXLY_CORRECT | |||||
fi | |||||
mkhelper exec 6 \ | |||||
"${POSIX} exec 6> out; echo exec1 >&6; ${TEST_SH} exec2; exec 6>&-" | |||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 | |||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} -e ./exec1 | |||||
mkhelper exec 9 \ | |||||
"${POSIX} exec 9> out; echo exec1 >&9; ${TEST_SH} exec2" | |||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} ./exec1 | |||||
mkhelper exec 8 \ | |||||
"${POSIX}" \ | |||||
"exec 8> out; printf OK; echo exec1 >&8;" \ | |||||
"printf OK; ${TEST_SH} exec2; printf GOOD" | |||||
atf_check -s exit:0 -o match:OKOKGOOD -e empty \ | |||||
${TEST_SH} -e ./exec1 | |||||
mkhelper exec 7 \ | |||||
"${POSIX}" \ | |||||
"exec 7> out; printf OK; echo exec1 >&7;" \ | |||||
"printf OK; ${TEST_SH} exec2 || printf ERR" | |||||
atf_check -s exit:0 -o match:OKOK -o not-match:ERR -e empty \ | |||||
${TEST_SH} ./exec1 | |||||
cleanhelper exec | |||||
} | |||||
atf_init_test_cases() { | atf_init_test_cases() { | ||||
atf_add_test_case exec_redir_closed | atf_add_test_case exec_redir_closed | ||||
atf_add_test_case exec_redir_open | atf_add_test_case exec_redir_open | ||||
atf_add_test_case loop_redir_open | atf_add_test_case loop_redir_open | ||||
atf_add_test_case compound_redir_open | atf_add_test_case compound_redir_open | ||||
atf_add_test_case simple_redir_open | atf_add_test_case simple_redir_open | ||||
atf_add_test_case subshell_redir_open | atf_add_test_case subshell_redir_open | ||||
atf_add_test_case posix_exec_redir | |||||
} | } |