1 # $NetBSD: t_varval.sh,v 1.1 2016/03/16 15:49:19 christos Exp $
3 # Copyright (c) 2016 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
30 # Test all kinds of weird values in various ways to use shell $... expansions
37 v=$( printf '\\%3.3o' $(( $2 & 0xFF )) )
40 printf "%sprefix${v}suffix%s" "$q" "$q"
41 elif [ $# -ne 4 ]; then
42 printf %s prefix\"\'\"suffix
44 printf %s prefix\'suffix
55 -q) quote=no; shift; continue;;
58 pfx="${pfx}${pfx:+ }${1}"
65 sfx="${sfx}${sfx:+ }${1}"
69 i=1 # '\0' is not expected to work, anywhere...
72 oneline "${pfx}" "$i" "${sfx}" $quote
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".
83 oneline "echo " 9 '' |
84 atf_check -s exit:0 -o inline:'prefix\tsuffix\n' -e empty \
87 oneline "VAR=" 65 '; echo "${#VAR}:${VAR}"' |
88 atf_check -s exit:0 -o inline:'13:prefixAsuffix\n' -e empty \
91 oneline "VAR=" 1 '; echo "${#VAR}:${VAR}"' |
92 atf_check -s exit:0 -o inline:'13:prefix
\ 1suffix\n' -e empty \
95 oneline "VAR=" 10 '; echo "${#VAR}:${VAR}"' |
96 atf_check -s exit:0 -o inline:'13:prefix\nsuffix\n' -e empty \
99 rm -f prefix* 2>/dev/null || :
100 oneline "echo hello >" 45 "" |
101 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
102 test -f "prefix-suffix" ||
103 atf_fail "failed to create prefix-suffix (45)"
104 test -s "prefix-suffix" ||
105 atf_fail "no data in prefix-suffix (45)"
106 test "$(cat prefix-suffix)" = "hello" ||
107 atf_fail "incorrect data in prefix-suffix (45)"
112 atf_test_case assignment
114 atf_set "descr" "Check that all chars can be assigned to vars"
117 atf_require_prog grep
121 mkdata "VAR=" -- '; echo ${#VAR}' |
122 atf_check -s exit:0 -o save:results -e empty ${TEST_SH}
123 test -z $( grep -v "^13$" results ) ||
124 atf_fail "Incorrect lengths: $(grep -nv '^13$' results)"
129 atf_test_case cmdline
131 atf_set "descr" "Check vars containing all chars can be used"
138 mkdata "VAR=" -- '; echo "${VAR}"' |
139 atf_check -s exit:0 -o save:results -e empty ${TEST_SH}
141 # 256 because one output line contains a \n ...
142 test $( wc -l < results ) -eq 256 ||
143 atf_fail "incorrect line count in results"
144 test $(wc -c < results) -eq $(( 255 * 14 )) ||
145 atf_fail "incorrect character count in results"
150 atf_test_case redirect
152 atf_set "descr" "Check vars containing all chars can be used"
158 atf_require_prog mkdir
159 atf_require_prog rmdir
164 rm -f prefix* suffix || :
166 mkdir prefix # one of the files will be prefix/suffix
167 mkdata "VAR=" -- '; echo "${VAR}" > "${VAR}"' |
168 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
170 test -f "prefix/suffix" ||
171 atf_fail "Failed to create file in subdirectory"
172 test $( wc -l < "prefix/suffix" ) -eq 1 ||
173 atf_fail "Not exactly one line in prefix/suffix file"
175 atf_check -s exit:0 -o empty -e empty rm "prefix/suffix"
176 atf_check -s exit:0 -o empty -e empty rmdir "prefix"
178 test -f "prefix${nl}suffix" ||
179 atf_fail "Failed to create file with newline in its name"
180 test $( wc -l < "prefix${nl}suffix" ) -eq 2 ||
181 atf_fail "NewLine file did not contain embedded newline"
183 atf_check -s exit:0 -o empty -e empty rm "prefix${nl}suffix"
185 # Now there should be 253 files left...
186 test $( ls | wc -l ) -eq 253 ||
188 "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))"
190 # and each of them should have a name that is 13 chars long (+ \n)
191 test $( ls | wc -c ) -eq $(( 253 * 14 )) ||
192 atf_fail "File names do not appear to be as expected"
199 atf_set "descr" "Check vars containing all chars can be used"
205 atf_require_prog mkdir
206 atf_require_prog rmdir
211 rm -f prefix* suffix || :
213 mkdir prefix # one of the files will be prefix/suffix
215 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '
218 # skip the mess made by embedded newline
220 (prefix | suffix) continue;;
222 echo "${VAR}" > "${VAR}"
225 test -f "prefix/suffix" ||
226 atf_fail "Failed to create file in subdirectory"
227 test $( wc -l < "prefix/suffix" ) -eq 1 ||
228 atf_fail "Not exactly one line in prefix/suffix file"
230 atf_check -s exit:0 -o empty -e empty rm "prefix/suffix"
231 atf_check -s exit:0 -o empty -e empty rmdir "prefix"
233 # Now there should be 253 files left...
234 test $( ls | wc -l ) -eq 253 ||
236 "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))"
238 # and each of them should have a name that is 13 chars long (+ \n)
239 test $( ls | wc -c ) -eq $(( 253 * 14 )) ||
240 atf_fail "File names do not appear to be as expected"
245 atf_init_test_cases() {
246 atf_add_test_case aaa
247 atf_add_test_case assignment
248 atf_add_test_case cmdline
249 atf_add_test_case redirect
250 atf_add_test_case read