Home | History | Annotate | Line # | Download | only in sh
t_expand.sh revision 1.6
      1 # $NetBSD: t_expand.sh,v 1.6 2016/03/08 14:26:54 christos Exp $
      2 #
      3 # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 # the implementation of "sh" to test
     28 : ${TEST_SH:="/bin/sh"}
     29 
     30 #
     31 # This file tests the functions in expand.c.
     32 #
     33 
     34 delim_argv() {
     35 	str=
     36 	while [ $# -gt 0 ]; do
     37 		if [ -z "${str}" ]; then
     38 			str=">$1<"
     39 		else
     40 			str="${str} >$1<"
     41 		fi
     42 		shift
     43 	done
     44 	echo ${str}
     45 }
     46 
     47 atf_test_case dollar_at
     48 dollar_at_head() {
     49 	atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
     50 	                "of the \$@ variable had been broken.  Check for" \
     51 			"this behavior."
     52 }
     53 dollar_at_body() {
     54 	# This one should work everywhere.
     55 	atf_check -s exit:0 -o inline:' EOL\n' -e empty \
     56 		${TEST_SH} -c 'echo "" "" | '" sed 's,\$,EOL,'"
     57 
     58 	# This code triggered the bug.
     59 	atf_check -s exit:0 -o inline:' EOL\n' -e empty \
     60 		${TEST_SH} -c 'set -- "" ""; echo "$@" | '" sed 's,\$,EOL,'"
     61 
     62 	atf_check -s exit:0 -o inline:'0\n' -e empty ${TEST_SH} -c \
     63 		'set -- -; shift; n_arg() { echo $#; }; n_arg "$@"'
     64 }
     65 
     66 atf_test_case dollar_at_with_text
     67 dollar_at_with_text_head() {
     68 	atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
     69 	                "within the quotes.  PR bin/33956."
     70 }
     71 dollar_at_with_text_body() {
     72 
     73 	cat <<'EOF' > h-f1
     74 
     75 delim_argv() {
     76 	str=
     77 	while [ $# -gt 0 ]; do
     78 		if [ -z "${str}" ]; then
     79 			str=">$1<"
     80 		else
     81 			str="${str} >$1<"
     82 		fi
     83 		shift
     84 	done
     85 	echo "${str}"
     86 }
     87 
     88 EOF
     89 	cat <<'EOF' > h-f2
     90 
     91 delim_argv() {
     92 	str=
     93 	while [ $# -gt 0 ]; do
     94 
     95 		str="${str}${str:+ }>$1<"
     96 		shift
     97 
     98 	done
     99 	echo "${str}"
    100 }
    101 
    102 EOF
    103 
    104 	chmod +x h-f1 h-f2
    105 
    106 	for f in 1 2
    107 	do
    108 		atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
    109 			". ./h-f${f}; "'set -- ; delim_argv "$@"'
    110 		atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
    111 			${TEST_SH} -c \
    112 			". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
    113 		atf_check -s exit:0 -o inline:'>foo  bar<\n' -e empty \
    114 			${TEST_SH} -c \
    115 			". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
    116 
    117 		atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
    118 			${TEST_SH} -c \
    119 			". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
    120 		atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
    121 			${TEST_SH} -c \
    122 			". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
    123 		atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
    124 			${TEST_SH} -c \
    125 			". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
    126 	done
    127 }
    128 
    129 atf_test_case strip
    130 strip_head() {
    131 	atf_set "descr" "Checks that the %% operator works and strips" \
    132 	                "the contents of a variable from the given point" \
    133 			"to the end"
    134 }
    135 strip_body() {
    136 	line='#define bindir "/usr/bin" /* comment */'
    137 	stripped='#define bindir "/usr/bin" '
    138 
    139 	# atf_expect_fail "PR bin/43469" -- now fixed
    140 	for exp in 				\
    141 		'${line%%/\**}'			\
    142 		'${line%%"/*"*}'		\
    143 		'${line%%'"'"'/*'"'"'*}'	\
    144 		'"${line%%/\**}"'		\
    145 		'"${line%%"/*"*}"'		\
    146 		'"${line%%'"'"'/*'"'"'*}"'	\
    147 		'${line%/\**}'			\
    148 		'${line%"/*"*}'			\
    149 		'${line%'"'"'/*'"'"'*}'		\
    150 		'"${line%/\**}"'		\
    151 		'"${line%"/*"*}"'		\
    152 		'"${line%'"'"'/*'"'"'*}"'
    153 	do
    154 		atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
    155 			"line='${line}'; echo :${exp}:"
    156 	done
    157 }
    158 
    159 atf_test_case varpattern_backslashes
    160 varpattern_backslashes_head() {
    161 	atf_set "descr" "Tests that protecting wildcards with backslashes" \
    162 	                "works in variable patterns."
    163 }
    164 varpattern_backslashes_body() {
    165 	line='/foo/bar/*/baz'
    166 	stripped='/foo/bar/'
    167 	atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
    168 		'line="/foo/bar/*/baz"; echo ${line%%\**}'
    169 }
    170 
    171 atf_test_case arithmetic
    172 arithmetic_head() {
    173 	atf_set "descr" "POSIX requires shell arithmetic to use signed" \
    174 	                "long or a wider type.  We use intmax_t, so at" \
    175 			"least 64 bits should be available.  Make sure" \
    176 			"this is true."
    177 }
    178 arithmetic_body() {
    179 
    180 	atf_check -o inline:'3' -e empty ${TEST_SH} -c \
    181 		'printf %s $((1 + 2))'
    182 	atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
    183 		'printf %s $((0x7fffffff))'
    184 	atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
    185 		'printf %s $(((1 << 63) - 1))'
    186 }
    187 
    188 atf_test_case iteration_on_null_parameter
    189 iteration_on_null_parameter_head() {
    190 	atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
    191 	                "the error \"sh: @: parameter not set\" is incorrect." \
    192 	                "PR bin/48202."
    193 }
    194 iteration_on_null_parameter_body() {
    195 	atf_check -o empty -e empty ${TEST_SH} -c \
    196 		'N=; set -- ${N};   for X; do echo "[$X]"; done'
    197 }
    198 
    199 atf_test_case iteration_on_quoted_null_parameter
    200 iteration_on_quoted_null_parameter_head() {
    201 	atf_set "descr" \
    202 		'Check iteration of "$@" in for loop when set to null;'
    203 }
    204 iteration_on_quoted_null_parameter_body() {
    205 	atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
    206 		'N=; set -- "${N}"; for X; do echo "[$X]"; done'
    207 }
    208 
    209 atf_test_case iteration_on_null_or_null_parameter
    210 iteration_on_null_or_null_parameter_head() {
    211 	atf_set "descr" \
    212 		'Check expansion of null parameter as default for another null'
    213 }
    214 iteration_on_null_or_null_parameter_body() {
    215 	atf_check -o empty -e empty ${TEST_SH} -c \
    216 		'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
    217 }
    218 
    219 atf_test_case iteration_on_null_or_missing_parameter
    220 iteration_on_null_or_missing_parameter_head() {
    221 	atf_set "descr" \
    222 	    'Check expansion of missing parameter as default for another null'
    223 }
    224 iteration_on_null_or_missing_parameter_body() {
    225 	# atf_expect_fail 'PR bin/50834'
    226 	atf_check -o empty -e empty ${TEST_SH} -c \
    227 		'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
    228 }
    229 
    230 atf_init_test_cases() {
    231 	atf_add_test_case dollar_at
    232 	atf_add_test_case dollar_at_with_text
    233 	atf_add_test_case strip
    234 	atf_add_test_case varpattern_backslashes
    235 	atf_add_test_case arithmetic
    236 	atf_add_test_case iteration_on_null_parameter
    237 	atf_add_test_case iteration_on_quoted_null_parameter
    238 	atf_add_test_case iteration_on_null_or_null_parameter
    239 	atf_add_test_case iteration_on_null_or_missing_parameter
    240 }
    241