t_varquote.sh revision 313498
1# $NetBSD: t_varquote.sh,v 1.5 2016/03/27 14:50:01 christos Exp $
2#
3# Copyright (c) 2007 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# Variable quoting test.
31
32check() {
33	if [ "$1" != "$2" ]
34	then
35		atf_fail "expected [$2], found [$1]" 1>&2
36	fi
37}
38
39atf_test_case all
40all_head() {
41	atf_set "descr" "Basic checks for variable quoting"
42}
43all_body() {
44
45	cat <<-'EOF' > script.sh
46		T=0
47		check() {
48			T=$((${T} + 1))
49
50			if [ "$1" != "$2" ]
51			then
52				printf '%s\n' "T${T}: expected [$2], found [$1]"
53				exit 1
54			fi
55		}
56
57								#1
58		foo='${a:-foo}'
59		check "$foo" '${a:-foo}'
60								#2
61		foo="${a:-foo}"
62		check "$foo" "foo"
63								#3
64		foo=${a:-"'{}'"}	
65		check "$foo" "'{}'"
66								#4
67		foo=${a:-${b:-"'{}'"}}
68		check "$foo" "'{}'"
69								#5
70		#    ${   }   The ' are inside ".." so are literal (not quotes).
71		foo="${a-'}'}"
72		check "$foo" "''}"
73								#6
74		# The rules for quoting in ${var-word} expressions are somewhat
75		# weird, in the following there is not one quoted string being
76		# assigned to foo (with internally quoted sub-strings), rather
77		# it is a mixed quoted/unquoted string, with parts that are
78		# quoted, separated by 2 unquoted sections...
79		#    qqqqqqqqqq uuuuuuuuuu qq uuuu qqqq
80		foo="${a:-${b:-"${c:-${d:-"x}"}}y}"}}z}"
81		#   "                                z*"
82		#    ${a:-                          }
83		#         ${b:-                    }
84		#              "                y*"
85		#               ${c:-          }
86		#                    ${d:-    }
87		#                         "x*"
88		check "$foo" "x}y}z}"
89								#7
90		# And believe it or not, this is the one that gives
91		# most problems, with 3 different observed outputs...
92		#    qqqqq  qq  q		is one interpretation
93		#    qqqqq QQQQ q		is another (most common)
94		#			(the third is syntax error...)
95		foo="${a:-"'{}'"}"
96		check "$foo" "'{}'"
97
98	EOF
99
100	OUT=$( ${TEST_SH} script.sh 2>&1 )
101	if  [ $? -ne 0 ]
102	then
103		atf_fail "${OUT}"
104	elif [ -n "${OUT}" ]
105	then
106		atf_fail "script.sh unexpectedly said: ${OUT}"
107	fi
108}
109
110atf_test_case nested_quotes_multiword
111nested_quotes_multiword_head() {
112	atf_set "descr" "Tests that having nested quoting in a multi-word" \
113	    "string works (PR bin/43597)"
114}
115nested_quotes_multiword_body() {
116	atf_check -s eq:0 -o match:"first-word second-word" -e empty \
117	    ${TEST_SH} -c 'echo "${foo:="first-word"} second-word"'
118}
119
120atf_test_case default_assignment_with_arith
121default_assignment_with_arith_head() {
122	atf_set "descr" "Tests default variable assignment with arithmetic" \
123	    "string works (PR bin/50827)"
124}
125default_assignment_with_arith_body() {
126	atf_check -s eq:0 -o empty -e empty ${TEST_SH} -c ': "${x=$((1))}"'
127	atf_check -s eq:0 -o match:1 -e empty ${TEST_SH} -c 'echo "${x=$((1))}"'
128}
129
130atf_init_test_cases() {
131	atf_add_test_case all
132	atf_add_test_case nested_quotes_multiword
133	atf_add_test_case default_assignment_with_arith
134}
135