t_varval.sh revision 1.1 1 1.1 christos # $NetBSD: t_varval.sh,v 1.1 2016/03/16 15:49:19 christos 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.1 christos ${TEST_SH}
86 1.1 christos
87 1.1 christos oneline "VAR=" 65 '; echo "${#VAR}:${VAR}"' |
88 1.1 christos atf_check -s exit:0 -o inline:'13:prefixAsuffix\n' -e empty \
89 1.1 christos ${TEST_SH}
90 1.1 christos
91 1.1 christos oneline "VAR=" 1 '; echo "${#VAR}:${VAR}"' |
92 1.1 christos atf_check -s exit:0 -o inline:'13:prefixsuffix\n' -e empty \
93 1.1 christos ${TEST_SH}
94 1.1 christos
95 1.1 christos oneline "VAR=" 10 '; echo "${#VAR}:${VAR}"' |
96 1.1 christos atf_check -s exit:0 -o inline:'13:prefix\nsuffix\n' -e empty \
97 1.1 christos ${TEST_SH}
98 1.1 christos
99 1.1 christos rm -f prefix* 2>/dev/null || :
100 1.1 christos oneline "echo hello >" 45 "" |
101 1.1 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH}
102 1.1 christos test -f "prefix-suffix" ||
103 1.1 christos atf_fail "failed to create prefix-suffix (45)"
104 1.1 christos test -s "prefix-suffix" ||
105 1.1 christos atf_fail "no data in prefix-suffix (45)"
106 1.1 christos test "$(cat prefix-suffix)" = "hello" ||
107 1.1 christos atf_fail "incorrect data in prefix-suffix (45)"
108 1.1 christos
109 1.1 christos return 0
110 1.1 christos }
111 1.1 christos
112 1.1 christos atf_test_case assignment
113 1.1 christos assignment_head() {
114 1.1 christos atf_set "descr" "Check that all chars can be assigned to vars"
115 1.1 christos }
116 1.1 christos assignment_body() {
117 1.1 christos atf_require_prog grep
118 1.1 christos atf_require_prog rm
119 1.1 christos
120 1.1 christos rm -f results || :
121 1.1 christos mkdata "VAR=" -- '; echo ${#VAR}' |
122 1.1 christos atf_check -s exit:0 -o save:results -e empty ${TEST_SH}
123 1.1 christos test -z $( grep -v "^13$" results ) ||
124 1.1 christos atf_fail "Incorrect lengths: $(grep -nv '^13$' results)"
125 1.1 christos
126 1.1 christos return 0
127 1.1 christos }
128 1.1 christos
129 1.1 christos atf_test_case cmdline
130 1.1 christos cmdline_head() {
131 1.1 christos atf_set "descr" "Check vars containing all chars can be used"
132 1.1 christos }
133 1.1 christos cmdline_body() {
134 1.1 christos atf_require_prog rm
135 1.1 christos atf_require_prog wc
136 1.1 christos
137 1.1 christos rm -f results || :
138 1.1 christos mkdata "VAR=" -- '; echo "${VAR}"' |
139 1.1 christos atf_check -s exit:0 -o save:results -e empty ${TEST_SH}
140 1.1 christos
141 1.1 christos # 256 because one output line contains a \n ...
142 1.1 christos test $( wc -l < results ) -eq 256 ||
143 1.1 christos atf_fail "incorrect line count in results"
144 1.1 christos test $(wc -c < results) -eq $(( 255 * 14 )) ||
145 1.1 christos atf_fail "incorrect character count in results"
146 1.1 christos
147 1.1 christos return 0
148 1.1 christos }
149 1.1 christos
150 1.1 christos atf_test_case redirect
151 1.1 christos redirect_head() {
152 1.1 christos atf_set "descr" "Check vars containing all chars can be used"
153 1.1 christos }
154 1.1 christos redirect_body() {
155 1.1 christos atf_require_prog ls
156 1.1 christos atf_require_prog wc
157 1.1 christos atf_require_prog rm
158 1.1 christos atf_require_prog mkdir
159 1.1 christos atf_require_prog rmdir
160 1.1 christos
161 1.1 christos nl='
162 1.1 christos '
163 1.1 christos
164 1.1 christos rm -f prefix* suffix || :
165 1.1 christos
166 1.1 christos mkdir prefix # one of the files will be prefix/suffix
167 1.1 christos mkdata "VAR=" -- '; echo "${VAR}" > "${VAR}"' |
168 1.1 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH}
169 1.1 christos
170 1.1 christos test -f "prefix/suffix" ||
171 1.1 christos atf_fail "Failed to create file in subdirectory"
172 1.1 christos test $( wc -l < "prefix/suffix" ) -eq 1 ||
173 1.1 christos atf_fail "Not exactly one line in prefix/suffix file"
174 1.1 christos
175 1.1 christos atf_check -s exit:0 -o empty -e empty rm "prefix/suffix"
176 1.1 christos atf_check -s exit:0 -o empty -e empty rmdir "prefix"
177 1.1 christos
178 1.1 christos test -f "prefix${nl}suffix" ||
179 1.1 christos atf_fail "Failed to create file with newline in its name"
180 1.1 christos test $( wc -l < "prefix${nl}suffix" ) -eq 2 ||
181 1.1 christos atf_fail "NewLine file did not contain embedded newline"
182 1.1 christos
183 1.1 christos atf_check -s exit:0 -o empty -e empty rm "prefix${nl}suffix"
184 1.1 christos
185 1.1 christos # Now there should be 253 files left...
186 1.1 christos test $( ls | wc -l ) -eq 253 ||
187 1.1 christos atf_fail \
188 1.1 christos "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))"
189 1.1 christos
190 1.1 christos # and each of them should have a name that is 13 chars long (+ \n)
191 1.1 christos test $( ls | wc -c ) -eq $(( 253 * 14 )) ||
192 1.1 christos atf_fail "File names do not appear to be as expected"
193 1.1 christos
194 1.1 christos return 0
195 1.1 christos }
196 1.1 christos
197 1.1 christos atf_test_case read
198 1.1 christos read_head() {
199 1.1 christos atf_set "descr" "Check vars containing all chars can be used"
200 1.1 christos }
201 1.1 christos read_body() {
202 1.1 christos atf_require_prog ls
203 1.1 christos atf_require_prog wc
204 1.1 christos atf_require_prog rm
205 1.1 christos atf_require_prog mkdir
206 1.1 christos atf_require_prog rmdir
207 1.1 christos
208 1.1 christos nl='
209 1.1 christos '
210 1.1 christos
211 1.1 christos rm -f prefix* suffix || :
212 1.1 christos
213 1.1 christos mkdir prefix # one of the files will be prefix/suffix
214 1.1 christos mkdata -q |
215 1.1 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '
216 1.1 christos while read -r VAR
217 1.1 christos do
218 1.1 christos # skip the mess made by embedded newline
219 1.1 christos case "${VAR}" in
220 1.1 christos (prefix | suffix) continue;;
221 1.1 christos esac
222 1.1 christos echo "${VAR}" > "${VAR}"
223 1.1 christos done'
224 1.1 christos
225 1.1 christos test -f "prefix/suffix" ||
226 1.1 christos atf_fail "Failed to create file in subdirectory"
227 1.1 christos test $( wc -l < "prefix/suffix" ) -eq 1 ||
228 1.1 christos atf_fail "Not exactly one line in prefix/suffix file"
229 1.1 christos
230 1.1 christos atf_check -s exit:0 -o empty -e empty rm "prefix/suffix"
231 1.1 christos atf_check -s exit:0 -o empty -e empty rmdir "prefix"
232 1.1 christos
233 1.1 christos # Now there should be 253 files left...
234 1.1 christos test $( ls | wc -l ) -eq 253 ||
235 1.1 christos atf_fail \
236 1.1 christos "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))"
237 1.1 christos
238 1.1 christos # and each of them should have a name that is 13 chars long (+ \n)
239 1.1 christos test $( ls | wc -c ) -eq $(( 253 * 14 )) ||
240 1.1 christos atf_fail "File names do not appear to be as expected"
241 1.1 christos
242 1.1 christos return 0
243 1.1 christos }
244 1.1 christos
245 1.1 christos atf_init_test_cases() {
246 1.1 christos atf_add_test_case aaa
247 1.1 christos atf_add_test_case assignment
248 1.1 christos atf_add_test_case cmdline
249 1.1 christos atf_add_test_case redirect
250 1.1 christos atf_add_test_case read
251 1.1 christos }
252