1313498Sngie# $NetBSD: t_varquote.sh,v 1.5 2016/03/27 14:50:01 christos Exp $ 2272343Sngie# 3272343Sngie# Copyright (c) 2007 The NetBSD Foundation, Inc. 4272343Sngie# All rights reserved. 5272343Sngie# 6272343Sngie# Redistribution and use in source and binary forms, with or without 7272343Sngie# modification, are permitted provided that the following conditions 8272343Sngie# are met: 9272343Sngie# 1. Redistributions of source code must retain the above copyright 10272343Sngie# notice, this list of conditions and the following disclaimer. 11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright 12272343Sngie# notice, this list of conditions and the following disclaimer in the 13272343Sngie# documentation and/or other materials provided with the distribution. 14272343Sngie# 15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18272343Sngie# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25272343Sngie# POSSIBILITY OF SUCH DAMAGE. 26272343Sngie# 27313498Sngie# the implementation of "sh" to test 28313498Sngie: ${TEST_SH:="/bin/sh"} 29272343Sngie 30272343Sngie# Variable quoting test. 31272343Sngie 32272343Sngiecheck() { 33272343Sngie if [ "$1" != "$2" ] 34272343Sngie then 35272343Sngie atf_fail "expected [$2], found [$1]" 1>&2 36272343Sngie fi 37272343Sngie} 38272343Sngie 39272343Sngieatf_test_case all 40272343Sngieall_head() { 41272343Sngie atf_set "descr" "Basic checks for variable quoting" 42272343Sngie} 43272343Sngieall_body() { 44272343Sngie 45313498Sngie cat <<-'EOF' > script.sh 46313498Sngie T=0 47313498Sngie check() { 48313498Sngie T=$((${T} + 1)) 49272343Sngie 50313498Sngie if [ "$1" != "$2" ] 51313498Sngie then 52313498Sngie printf '%s\n' "T${T}: expected [$2], found [$1]" 53313498Sngie exit 1 54313498Sngie fi 55313498Sngie } 56272343Sngie 57313498Sngie #1 58313498Sngie foo='${a:-foo}' 59313498Sngie check "$foo" '${a:-foo}' 60313498Sngie #2 61313498Sngie foo="${a:-foo}" 62313498Sngie check "$foo" "foo" 63313498Sngie #3 64313498Sngie foo=${a:-"'{}'"} 65313498Sngie check "$foo" "'{}'" 66313498Sngie #4 67313498Sngie foo=${a:-${b:-"'{}'"}} 68313498Sngie check "$foo" "'{}'" 69313498Sngie #5 70313498Sngie # ${ } The ' are inside ".." so are literal (not quotes). 71313498Sngie foo="${a-'}'}" 72313498Sngie check "$foo" "''}" 73313498Sngie #6 74313498Sngie # The rules for quoting in ${var-word} expressions are somewhat 75313498Sngie # weird, in the following there is not one quoted string being 76313498Sngie # assigned to foo (with internally quoted sub-strings), rather 77313498Sngie # it is a mixed quoted/unquoted string, with parts that are 78313498Sngie # quoted, separated by 2 unquoted sections... 79313498Sngie # qqqqqqqqqq uuuuuuuuuu qq uuuu qqqq 80313498Sngie foo="${a:-${b:-"${c:-${d:-"x}"}}y}"}}z}" 81313498Sngie # " z*" 82313498Sngie # ${a:- } 83313498Sngie # ${b:- } 84313498Sngie # " y*" 85313498Sngie # ${c:- } 86313498Sngie # ${d:- } 87313498Sngie # "x*" 88313498Sngie check "$foo" "x}y}z}" 89313498Sngie #7 90313498Sngie # And believe it or not, this is the one that gives 91313498Sngie # most problems, with 3 different observed outputs... 92313498Sngie # qqqqq qq q is one interpretation 93313498Sngie # qqqqq QQQQ q is another (most common) 94313498Sngie # (the third is syntax error...) 95313498Sngie foo="${a:-"'{}'"}" 96313498Sngie check "$foo" "'{}'" 97272343Sngie 98313498Sngie EOF 99272343Sngie 100313498Sngie OUT=$( ${TEST_SH} script.sh 2>&1 ) 101313498Sngie if [ $? -ne 0 ] 102313498Sngie then 103313498Sngie atf_fail "${OUT}" 104313498Sngie elif [ -n "${OUT}" ] 105313498Sngie then 106313498Sngie atf_fail "script.sh unexpectedly said: ${OUT}" 107313498Sngie fi 108272343Sngie} 109272343Sngie 110272343Sngieatf_test_case nested_quotes_multiword 111272343Sngienested_quotes_multiword_head() { 112272343Sngie atf_set "descr" "Tests that having nested quoting in a multi-word" \ 113272343Sngie "string works (PR bin/43597)" 114272343Sngie} 115272343Sngienested_quotes_multiword_body() { 116272343Sngie atf_check -s eq:0 -o match:"first-word second-word" -e empty \ 117313498Sngie ${TEST_SH} -c 'echo "${foo:="first-word"} second-word"' 118272343Sngie} 119272343Sngie 120313498Sngieatf_test_case default_assignment_with_arith 121313498Sngiedefault_assignment_with_arith_head() { 122313498Sngie atf_set "descr" "Tests default variable assignment with arithmetic" \ 123313498Sngie "string works (PR bin/50827)" 124313498Sngie} 125313498Sngiedefault_assignment_with_arith_body() { 126313498Sngie atf_check -s eq:0 -o empty -e empty ${TEST_SH} -c ': "${x=$((1))}"' 127313498Sngie atf_check -s eq:0 -o match:1 -e empty ${TEST_SH} -c 'echo "${x=$((1))}"' 128313498Sngie} 129313498Sngie 130272343Sngieatf_init_test_cases() { 131272343Sngie atf_add_test_case all 132272343Sngie atf_add_test_case nested_quotes_multiword 133313498Sngie atf_add_test_case default_assignment_with_arith 134272343Sngie} 135