1 1.6 rillig # $NetBSD: t_varquote.sh,v 1.6 2024/04/28 07:27:40 rillig Exp $ 2 1.1 jruoho # 3 1.1 jruoho # Copyright (c) 2007 The NetBSD Foundation, Inc. 4 1.1 jruoho # All rights reserved. 5 1.1 jruoho # 6 1.1 jruoho # Redistribution and use in source and binary forms, with or without 7 1.1 jruoho # modification, are permitted provided that the following conditions 8 1.1 jruoho # are met: 9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright 10 1.1 jruoho # notice, this list of conditions and the following disclaimer. 11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright 12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the 13 1.1 jruoho # documentation and/or other materials provided with the distribution. 14 1.1 jruoho # 15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE. 26 1.1 jruoho # 27 1.4 christos # the implementation of "sh" to test 28 1.4 christos : ${TEST_SH:="/bin/sh"} 29 1.1 jruoho 30 1.1 jruoho # Variable quoting test. 31 1.1 jruoho 32 1.1 jruoho check() { 33 1.1 jruoho if [ "$1" != "$2" ] 34 1.1 jruoho then 35 1.1 jruoho atf_fail "expected [$2], found [$1]" 1>&2 36 1.1 jruoho fi 37 1.1 jruoho } 38 1.1 jruoho 39 1.1 jruoho atf_test_case all 40 1.1 jruoho all_head() { 41 1.1 jruoho atf_set "descr" "Basic checks for variable quoting" 42 1.1 jruoho } 43 1.1 jruoho all_body() { 44 1.1 jruoho 45 1.4 christos cat <<-'EOF' > script.sh 46 1.4 christos T=0 47 1.4 christos check() { 48 1.4 christos T=$((${T} + 1)) 49 1.4 christos 50 1.4 christos if [ "$1" != "$2" ] 51 1.4 christos then 52 1.4 christos printf '%s\n' "T${T}: expected [$2], found [$1]" 53 1.4 christos exit 1 54 1.4 christos fi 55 1.4 christos } 56 1.4 christos 57 1.4 christos #1 58 1.4 christos foo='${a:-foo}' 59 1.4 christos check "$foo" '${a:-foo}' 60 1.4 christos #2 61 1.4 christos foo="${a:-foo}" 62 1.4 christos check "$foo" "foo" 63 1.4 christos #3 64 1.4 christos foo=${a:-"'{}'"} 65 1.4 christos check "$foo" "'{}'" 66 1.4 christos #4 67 1.4 christos foo=${a:-${b:-"'{}'"}} 68 1.4 christos check "$foo" "'{}'" 69 1.4 christos #5 70 1.4 christos # ${ } The ' are inside ".." so are literal (not quotes). 71 1.4 christos foo="${a-'}'}" 72 1.4 christos check "$foo" "''}" 73 1.4 christos #6 74 1.4 christos # The rules for quoting in ${var-word} expressions are somewhat 75 1.4 christos # weird, in the following there is not one quoted string being 76 1.4 christos # assigned to foo (with internally quoted sub-strings), rather 77 1.4 christos # it is a mixed quoted/unquoted string, with parts that are 78 1.4 christos # quoted, separated by 2 unquoted sections... 79 1.4 christos # qqqqqqqqqq uuuuuuuuuu qq uuuu qqqq 80 1.4 christos foo="${a:-${b:-"${c:-${d:-"x}"}}y}"}}z}" 81 1.4 christos # " z*" 82 1.4 christos # ${a:- } 83 1.4 christos # ${b:- } 84 1.4 christos # " y*" 85 1.4 christos # ${c:- } 86 1.4 christos # ${d:- } 87 1.4 christos # "x*" 88 1.4 christos check "$foo" "x}y}z}" 89 1.4 christos #7 90 1.4 christos # And believe it or not, this is the one that gives 91 1.4 christos # most problems, with 3 different observed outputs... 92 1.4 christos # qqqqq qq q is one interpretation 93 1.4 christos # qqqqq QQQQ q is another (most common) 94 1.4 christos # (the third is syntax error...) 95 1.4 christos foo="${a:-"'{}'"}" 96 1.4 christos check "$foo" "'{}'" 97 1.1 jruoho 98 1.4 christos EOF 99 1.1 jruoho 100 1.4 christos OUT=$( ${TEST_SH} script.sh 2>&1 ) 101 1.4 christos if [ $? -ne 0 ] 102 1.4 christos then 103 1.4 christos atf_fail "${OUT}" 104 1.4 christos elif [ -n "${OUT}" ] 105 1.4 christos then 106 1.4 christos atf_fail "script.sh unexpectedly said: ${OUT}" 107 1.4 christos fi 108 1.1 jruoho } 109 1.1 jruoho 110 1.1 jruoho atf_test_case nested_quotes_multiword 111 1.1 jruoho nested_quotes_multiword_head() { 112 1.1 jruoho atf_set "descr" "Tests that having nested quoting in a multi-word" \ 113 1.2 christos "string works (PR bin/43597)" 114 1.1 jruoho } 115 1.1 jruoho nested_quotes_multiword_body() { 116 1.6 rillig atf_check -s exit:0 -o match:"first-word second-word" -e empty \ 117 1.4 christos ${TEST_SH} -c 'echo "${foo:="first-word"} second-word"' 118 1.1 jruoho } 119 1.1 jruoho 120 1.3 christos atf_test_case default_assignment_with_arith 121 1.3 christos default_assignment_with_arith_head() { 122 1.3 christos atf_set "descr" "Tests default variable assignment with arithmetic" \ 123 1.3 christos "string works (PR bin/50827)" 124 1.3 christos } 125 1.3 christos default_assignment_with_arith_body() { 126 1.6 rillig atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c ': "${x=$((1))}"' 127 1.6 rillig atf_check -s exit:0 -o match:1 -e empty ${TEST_SH} -c 'echo "${x=$((1))}"' 128 1.3 christos } 129 1.3 christos 130 1.1 jruoho atf_init_test_cases() { 131 1.1 jruoho atf_add_test_case all 132 1.1 jruoho atf_add_test_case nested_quotes_multiword 133 1.3 christos atf_add_test_case default_assignment_with_arith 134 1.1 jruoho } 135