1 1.2 kre # $NetBSD: t_varval.sh,v 1.2 2021/11/22 05:07:15 kre Exp $ 2 1.1 christos # 3 1.1 christos # Copyright (c) 2016 The NetBSD Foundation, Inc. 4 1.1 christos # All rights reserved. 5 1.1 christos # 6 1.1 christos # Redistribution and use in source and binary forms, with or without 7 1.1 christos # modification, are permitted provided that the following conditions 8 1.1 christos # are met: 9 1.1 christos # 1. Redistributions of source code must retain the above copyright 10 1.1 christos # notice, this list of conditions and the following disclaimer. 11 1.1 christos # 2. Redistributions in binary form must reproduce the above copyright 12 1.1 christos # notice, this list of conditions and the following disclaimer in the 13 1.1 christos # documentation and/or other materials provided with the distribution. 14 1.1 christos # 15 1.1 christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 1.1 christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 1.1 christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 1.1 christos # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 1.1 christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 1.1 christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 1.1 christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 1.1 christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 1.1 christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 1.1 christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 1.1 christos # POSSIBILITY OF SUCH DAMAGE. 26 1.1 christos # 27 1.1 christos # the implementation of "sh" to test 28 1.1 christos : ${TEST_SH:="/bin/sh"} 29 1.1 christos 30 1.1 christos # Test all kinds of weird values in various ways to use shell $... expansions 31 1.1 christos 32 1.1 christos oneline() 33 1.1 christos { 34 1.1 christos q="'" 35 1.1 christos test $# -eq 4 && q="" 36 1.1 christos 37 1.1 christos v=$( printf '\\%3.3o' $(( $2 & 0xFF )) ) 38 1.1 christos printf "%s" "$1" 39 1.1 christos if [ $2 != 39 ]; then 40 1.1 christos printf "%sprefix${v}suffix%s" "$q" "$q" 41 1.1 christos elif [ $# -ne 4 ]; then 42 1.1 christos printf %s prefix\"\'\"suffix 43 1.1 christos else 44 1.1 christos printf %s prefix\'suffix 45 1.1 christos fi 46 1.1 christos printf "%s\n" "$3" 47 1.1 christos } 48 1.1 christos 49 1.1 christos mkdata() { 50 1.1 christos quote= pfx= 51 1.1 christos while [ $# -gt 0 ] 52 1.1 christos do 53 1.1 christos case "$1" in 54 1.1 christos --) shift; break;; 55 1.1 christos -q) quote=no; shift; continue;; 56 1.1 christos esac 57 1.1 christos 58 1.1 christos pfx="${pfx}${pfx:+ }${1}" 59 1.1 christos shift 60 1.1 christos done 61 1.1 christos 62 1.1 christos sfx= 63 1.1 christos while [ $# -gt 0 ] 64 1.1 christos do 65 1.1 christos sfx="${sfx}${sfx:+ }${1}" 66 1.1 christos shift 67 1.1 christos done 68 1.1 christos 69 1.1 christos i=1 # '\0' is not expected to work, anywhere... 70 1.1 christos while [ $i -lt 256 ] 71 1.1 christos do 72 1.1 christos oneline "${pfx}" "$i" "${sfx}" $quote 73 1.1 christos i=$(( $i + 1 )) 74 1.1 christos done 75 1.1 christos } 76 1.1 christos 77 1.1 christos atf_test_case aaa 78 1.1 christos aaa_head() { 79 1.1 christos atf_set "descr" "Check that this test has a hope of working. " \ 80 1.1 christos "Just give up on these tests if the aaa test fails". 81 1.1 christos } 82 1.1 christos aaa_body() { 83 1.1 christos oneline "echo " 9 '' | 84 1.1 christos atf_check -s exit:0 -o inline:'prefix\tsuffix\n' -e empty \ 85 1.2 kre ${TEST_SH} || 86 1.2 kre atf_fail 'echo 9 -> tab' 87 1.1 christos 88 1.1 christos oneline "VAR=" 65 '; echo "${#VAR}:${VAR}"' | 89 1.1 christos atf_check -s exit:0 -o inline:'13:prefixAsuffix\n' -e empty \ 90 1.2 kre ${TEST_SH} || 91 1.2 kre atf_fail '65 -> A' 92 1.1 christos 93 1.1 christos oneline "VAR=" 1 '; echo "${#VAR}:${VAR}"' | 94 1.1 christos atf_check -s exit:0 -o inline:'13:prefixsuffix\n' -e empty \ 95 1.2 kre ${TEST_SH} || 96 1.2 kre atf_fail '1 -> ^A' 97 1.1 christos 98 1.1 christos oneline "VAR=" 10 '; echo "${#VAR}:${VAR}"' | 99 1.1 christos atf_check -s exit:0 -o inline:'13:prefix\nsuffix\n' -e empty \ 100 1.2 kre ${TEST_SH} || 101 1.2 kre atf_fail '10 -> \n' 102 1.1 christos 103 1.1 christos rm -f prefix* 2>/dev/null || : 104 1.1 christos oneline "echo hello >" 45 "" | 105 1.2 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} || 106 1.2 kre atf_fail 'redir into 45 -> E' 107 1.1 christos test -f "prefix-suffix" || 108 1.1 christos atf_fail "failed to create prefix-suffix (45)" 109 1.1 christos test -s "prefix-suffix" || 110 1.1 christos atf_fail "no data in prefix-suffix (45)" 111 1.1 christos test "$(cat prefix-suffix)" = "hello" || 112 1.1 christos atf_fail "incorrect data in prefix-suffix (45)" 113 1.1 christos 114 1.1 christos return 0 115 1.1 christos } 116 1.1 christos 117 1.1 christos atf_test_case assignment 118 1.1 christos assignment_head() { 119 1.1 christos atf_set "descr" "Check that all chars can be assigned to vars" 120 1.1 christos } 121 1.1 christos assignment_body() { 122 1.1 christos atf_require_prog grep 123 1.1 christos atf_require_prog rm 124 1.1 christos 125 1.1 christos rm -f results || : 126 1.1 christos mkdata "VAR=" -- '; echo ${#VAR}' | 127 1.2 kre atf_check -s exit:0 -o save:results -e empty ${TEST_SH} || 128 1.2 kre atf_fail 'making results' 129 1.1 christos test -z $( grep -v "^13$" results ) || 130 1.1 christos atf_fail "Incorrect lengths: $(grep -nv '^13$' results)" 131 1.1 christos 132 1.1 christos return 0 133 1.1 christos } 134 1.1 christos 135 1.1 christos atf_test_case cmdline 136 1.1 christos cmdline_head() { 137 1.1 christos atf_set "descr" "Check vars containing all chars can be used" 138 1.1 christos } 139 1.1 christos cmdline_body() { 140 1.1 christos atf_require_prog rm 141 1.1 christos atf_require_prog wc 142 1.1 christos 143 1.1 christos rm -f results || : 144 1.1 christos mkdata "VAR=" -- '; echo "${VAR}"' | 145 1.2 kre atf_check -s exit:0 -o save:results -e empty ${TEST_SH} || 146 1.2 kre atf_fail 'making results' 147 1.1 christos 148 1.1 christos # 256 because one output line contains a \n ... 149 1.1 christos test $( wc -l < results ) -eq 256 || 150 1.1 christos atf_fail "incorrect line count in results" 151 1.1 christos test $(wc -c < results) -eq $(( 255 * 14 )) || 152 1.1 christos atf_fail "incorrect character count in results" 153 1.1 christos 154 1.1 christos return 0 155 1.1 christos } 156 1.1 christos 157 1.1 christos atf_test_case redirect 158 1.1 christos redirect_head() { 159 1.1 christos atf_set "descr" "Check vars containing all chars can be used" 160 1.1 christos } 161 1.1 christos redirect_body() { 162 1.1 christos atf_require_prog ls 163 1.1 christos atf_require_prog wc 164 1.1 christos atf_require_prog rm 165 1.1 christos atf_require_prog mkdir 166 1.1 christos atf_require_prog rmdir 167 1.1 christos 168 1.1 christos nl=' 169 1.1 christos ' 170 1.1 christos 171 1.1 christos rm -f prefix* suffix || : 172 1.1 christos 173 1.1 christos mkdir prefix # one of the files will be prefix/suffix 174 1.1 christos mkdata "VAR=" -- '; echo "${VAR}" > "${VAR}"' | 175 1.2 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} || 176 1.2 kre atf_fail "$VAR -> ./$VAR" 177 1.1 christos 178 1.1 christos test -f "prefix/suffix" || 179 1.1 christos atf_fail "Failed to create file in subdirectory" 180 1.1 christos test $( wc -l < "prefix/suffix" ) -eq 1 || 181 1.1 christos atf_fail "Not exactly one line in prefix/suffix file" 182 1.1 christos 183 1.1 christos atf_check -s exit:0 -o empty -e empty rm "prefix/suffix" 184 1.1 christos atf_check -s exit:0 -o empty -e empty rmdir "prefix" 185 1.1 christos 186 1.1 christos test -f "prefix${nl}suffix" || 187 1.1 christos atf_fail "Failed to create file with newline in its name" 188 1.1 christos test $( wc -l < "prefix${nl}suffix" ) -eq 2 || 189 1.1 christos atf_fail "NewLine file did not contain embedded newline" 190 1.1 christos 191 1.1 christos atf_check -s exit:0 -o empty -e empty rm "prefix${nl}suffix" 192 1.1 christos 193 1.1 christos # Now there should be 253 files left... 194 1.1 christos test $( ls | wc -l ) -eq 253 || 195 1.1 christos atf_fail \ 196 1.1 christos "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))" 197 1.1 christos 198 1.1 christos # and each of them should have a name that is 13 chars long (+ \n) 199 1.1 christos test $( ls | wc -c ) -eq $(( 253 * 14 )) || 200 1.1 christos atf_fail "File names do not appear to be as expected" 201 1.1 christos 202 1.1 christos return 0 203 1.1 christos } 204 1.1 christos 205 1.1 christos atf_test_case read 206 1.1 christos read_head() { 207 1.1 christos atf_set "descr" "Check vars containing all chars can be used" 208 1.1 christos } 209 1.1 christos read_body() { 210 1.1 christos atf_require_prog ls 211 1.1 christos atf_require_prog wc 212 1.1 christos atf_require_prog rm 213 1.1 christos atf_require_prog mkdir 214 1.1 christos atf_require_prog rmdir 215 1.1 christos 216 1.1 christos nl=' 217 1.1 christos ' 218 1.1 christos 219 1.1 christos rm -f prefix* suffix || : 220 1.1 christos 221 1.1 christos mkdir prefix # one of the files will be prefix/suffix 222 1.1 christos mkdata -q | 223 1.1 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c ' 224 1.1 christos while read -r VAR 225 1.1 christos do 226 1.1 christos # skip the mess made by embedded newline 227 1.1 christos case "${VAR}" in 228 1.1 christos (prefix | suffix) continue;; 229 1.1 christos esac 230 1.1 christos echo "${VAR}" > "${VAR}" 231 1.2 kre done' || 232 1.2 kre atf_fail 'mkdata' 233 1.1 christos 234 1.1 christos test -f "prefix/suffix" || 235 1.1 christos atf_fail "Failed to create file in subdirectory" 236 1.1 christos test $( wc -l < "prefix/suffix" ) -eq 1 || 237 1.1 christos atf_fail "Not exactly one line in prefix/suffix file" 238 1.1 christos 239 1.1 christos atf_check -s exit:0 -o empty -e empty rm "prefix/suffix" 240 1.1 christos atf_check -s exit:0 -o empty -e empty rmdir "prefix" 241 1.1 christos 242 1.1 christos # Now there should be 253 files left... 243 1.1 christos test $( ls | wc -l ) -eq 253 || 244 1.1 christos atf_fail \ 245 1.1 christos "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))" 246 1.1 christos 247 1.1 christos # and each of them should have a name that is 13 chars long (+ \n) 248 1.1 christos test $( ls | wc -c ) -eq $(( 253 * 14 )) || 249 1.1 christos atf_fail "File names do not appear to be as expected" 250 1.1 christos 251 1.1 christos return 0 252 1.1 christos } 253 1.1 christos 254 1.1 christos atf_init_test_cases() { 255 1.1 christos atf_add_test_case aaa 256 1.1 christos atf_add_test_case assignment 257 1.1 christos atf_add_test_case cmdline 258 1.1 christos atf_add_test_case redirect 259 1.1 christos atf_add_test_case read 260 1.1 christos } 261