1# $NetBSD: t_varval.sh,v 1.2 2021/11/22 05:07:15 kre Exp $ 2# 3# Copyright (c) 2016 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# Test all kinds of weird values in various ways to use shell $... expansions 31 32oneline() 33{ 34 q="'" 35 test $# -eq 4 && q="" 36 37 v=$( printf '\\%3.3o' $(( $2 & 0xFF )) ) 38 printf "%s" "$1" 39 if [ $2 != 39 ]; then 40 printf "%sprefix${v}suffix%s" "$q" "$q" 41 elif [ $# -ne 4 ]; then 42 printf %s prefix\"\'\"suffix 43 else 44 printf %s prefix\'suffix 45 fi 46 printf "%s\n" "$3" 47} 48 49mkdata() { 50 quote= pfx= 51 while [ $# -gt 0 ] 52 do 53 case "$1" in 54 --) shift; break;; 55 -q) quote=no; shift; continue;; 56 esac 57 58 pfx="${pfx}${pfx:+ }${1}" 59 shift 60 done 61 62 sfx= 63 while [ $# -gt 0 ] 64 do 65 sfx="${sfx}${sfx:+ }${1}" 66 shift 67 done 68 69 i=1 # '\0' is not expected to work, anywhere... 70 while [ $i -lt 256 ] 71 do 72 oneline "${pfx}" "$i" "${sfx}" $quote 73 i=$(( $i + 1 )) 74 done 75} 76 77atf_test_case aaa 78aaa_head() { 79 atf_set "descr" "Check that this test has a hope of working. " \ 80 "Just give up on these tests if the aaa test fails". 81} 82aaa_body() { 83 oneline "echo " 9 '' | 84 atf_check -s exit:0 -o inline:'prefix\tsuffix\n' -e empty \ 85 ${TEST_SH} || 86 atf_fail 'echo 9 -> tab' 87 88 oneline "VAR=" 65 '; echo "${#VAR}:${VAR}"' | 89 atf_check -s exit:0 -o inline:'13:prefixAsuffix\n' -e empty \ 90 ${TEST_SH} || 91 atf_fail '65 -> A' 92 93 oneline "VAR=" 1 '; echo "${#VAR}:${VAR}"' | 94 atf_check -s exit:0 -o inline:'13:prefixsuffix\n' -e empty \ 95 ${TEST_SH} || 96 atf_fail '1 -> ^A' 97 98 oneline "VAR=" 10 '; echo "${#VAR}:${VAR}"' | 99 atf_check -s exit:0 -o inline:'13:prefix\nsuffix\n' -e empty \ 100 ${TEST_SH} || 101 atf_fail '10 -> \n' 102 103 rm -f prefix* 2>/dev/null || : 104 oneline "echo hello >" 45 "" | 105 atf_check -s exit:0 -o empty -e empty ${TEST_SH} || 106 atf_fail 'redir into 45 -> E' 107 test -f "prefix-suffix" || 108 atf_fail "failed to create prefix-suffix (45)" 109 test -s "prefix-suffix" || 110 atf_fail "no data in prefix-suffix (45)" 111 test "$(cat prefix-suffix)" = "hello" || 112 atf_fail "incorrect data in prefix-suffix (45)" 113 114 return 0 115} 116 117atf_test_case assignment 118assignment_head() { 119 atf_set "descr" "Check that all chars can be assigned to vars" 120} 121assignment_body() { 122 atf_require_prog grep 123 atf_require_prog rm 124 125 rm -f results || : 126 mkdata "VAR=" -- '; echo ${#VAR}' | 127 atf_check -s exit:0 -o save:results -e empty ${TEST_SH} || 128 atf_fail 'making results' 129 test -z $( grep -v "^13$" results ) || 130 atf_fail "Incorrect lengths: $(grep -nv '^13$' results)" 131 132 return 0 133} 134 135atf_test_case cmdline 136cmdline_head() { 137 atf_set "descr" "Check vars containing all chars can be used" 138} 139cmdline_body() { 140 atf_require_prog rm 141 atf_require_prog wc 142 143 rm -f results || : 144 mkdata "VAR=" -- '; echo "${VAR}"' | 145 atf_check -s exit:0 -o save:results -e empty ${TEST_SH} || 146 atf_fail 'making results' 147 148 # 256 because one output line contains a \n ... 149 test $( wc -l < results ) -eq 256 || 150 atf_fail "incorrect line count in results" 151 test $(wc -c < results) -eq $(( 255 * 14 )) || 152 atf_fail "incorrect character count in results" 153 154 return 0 155} 156 157atf_test_case redirect 158redirect_head() { 159 atf_set "descr" "Check vars containing all chars can be used" 160} 161redirect_body() { 162 atf_require_prog ls 163 atf_require_prog wc 164 atf_require_prog rm 165 atf_require_prog mkdir 166 atf_require_prog rmdir 167 168 nl=' 169' 170 171 rm -f prefix* suffix || : 172 173 mkdir prefix # one of the files will be prefix/suffix 174 mkdata "VAR=" -- '; echo "${VAR}" > "${VAR}"' | 175 atf_check -s exit:0 -o empty -e empty ${TEST_SH} || 176 atf_fail "$VAR -> ./$VAR" 177 178 test -f "prefix/suffix" || 179 atf_fail "Failed to create file in subdirectory" 180 test $( wc -l < "prefix/suffix" ) -eq 1 || 181 atf_fail "Not exactly one line in prefix/suffix file" 182 183 atf_check -s exit:0 -o empty -e empty rm "prefix/suffix" 184 atf_check -s exit:0 -o empty -e empty rmdir "prefix" 185 186 test -f "prefix${nl}suffix" || 187 atf_fail "Failed to create file with newline in its name" 188 test $( wc -l < "prefix${nl}suffix" ) -eq 2 || 189 atf_fail "NewLine file did not contain embedded newline" 190 191 atf_check -s exit:0 -o empty -e empty rm "prefix${nl}suffix" 192 193 # Now there should be 253 files left... 194 test $( ls | wc -l ) -eq 253 || 195 atf_fail \ 196 "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))" 197 198 # and each of them should have a name that is 13 chars long (+ \n) 199 test $( ls | wc -c ) -eq $(( 253 * 14 )) || 200 atf_fail "File names do not appear to be as expected" 201 202 return 0 203} 204 205atf_test_case read 206read_head() { 207 atf_set "descr" "Check vars containing all chars can be used" 208} 209read_body() { 210 atf_require_prog ls 211 atf_require_prog wc 212 atf_require_prog rm 213 atf_require_prog mkdir 214 atf_require_prog rmdir 215 216 nl=' 217' 218 219 rm -f prefix* suffix || : 220 221 mkdir prefix # one of the files will be prefix/suffix 222 mkdata -q | 223 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c ' 224 while read -r VAR 225 do 226 # skip the mess made by embedded newline 227 case "${VAR}" in 228 (prefix | suffix) continue;; 229 esac 230 echo "${VAR}" > "${VAR}" 231 done' || 232 atf_fail 'mkdata' 233 234 test -f "prefix/suffix" || 235 atf_fail "Failed to create file in subdirectory" 236 test $( wc -l < "prefix/suffix" ) -eq 1 || 237 atf_fail "Not exactly one line in prefix/suffix file" 238 239 atf_check -s exit:0 -o empty -e empty rm "prefix/suffix" 240 atf_check -s exit:0 -o empty -e empty rmdir "prefix" 241 242 # Now there should be 253 files left... 243 test $( ls | wc -l ) -eq 253 || 244 atf_fail \ 245 "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))" 246 247 # and each of them should have a name that is 13 chars long (+ \n) 248 test $( ls | wc -c ) -eq $(( 253 * 14 )) || 249 atf_fail "File names do not appear to be as expected" 250 251 return 0 252} 253 254atf_init_test_cases() { 255 atf_add_test_case aaa 256 atf_add_test_case assignment 257 atf_add_test_case cmdline 258 atf_add_test_case redirect 259 atf_add_test_case read 260} 261