t_syntax.sh revision 1.6 1 1.6 kre # $NetBSD: t_syntax.sh,v 1.6 2017/07/26 17:50:20 kre Exp $
2 1.1 kre #
3 1.1 kre # Copyright (c) 2017 The NetBSD Foundation, Inc.
4 1.1 kre # All rights reserved.
5 1.1 kre #
6 1.1 kre # Redistribution and use in source and binary forms, with or without
7 1.1 kre # modification, are permitted provided that the following conditions
8 1.1 kre # are met:
9 1.1 kre # 1. Redistributions of source code must retain the above copyright
10 1.1 kre # notice, this list of conditions and the following disclaimer.
11 1.1 kre # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kre # notice, this list of conditions and the following disclaimer in the
13 1.1 kre # documentation and/or other materials provided with the distribution.
14 1.1 kre #
15 1.1 kre # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 kre # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 kre # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 kre # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 kre # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 kre # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 kre # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 kre # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 kre # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 kre # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kre # POSSIBILITY OF SUCH DAMAGE.
26 1.1 kre #
27 1.1 kre : ${TEST_SH:=/bin/sh}
28 1.1 kre
29 1.1 kre # This set of tests verifies various requirementgs relating to correct
30 1.1 kre # (and incorrect) syntax of shell input
31 1.1 kre #
32 1.1 kre # There is no intent in these tests to verify correct operation
33 1.1 kre # (though that sometimes cannot be separated from correct parsing.)
34 1.1 kre # That is (or should be) verified elsewhere.
35 1.1 kre #
36 1.1 kre # Also, some very basic syntax is tested in almost every test
37 1.1 kre # (they cannot work without basic parsing of elementary commands)
38 1.1 kre # so that is also not explicitly tested here.
39 1.1 kre #
40 1.1 kre # Similarly word expansion, field splitting, redirection, all have
41 1.1 kre # tests of their own (though we do test parsing of redirect ops).
42 1.1 kre #
43 1.1 kre # Note that in order to test the basic facilities, other shell operations
44 1.1 kre # are used - a test failure here does not necessarily mean that the
45 1.1 kre # operation intended to be tested is faulty, just that something is.
46 1.1 kre
47 1.1 kre atf_test_case a_basic_tokenisation
48 1.1 kre a_basic_tokenisation_head() {
49 1.1 kre atf_set "descr" "Test the shell correctly finds various tokens"
50 1.1 kre }
51 1.1 kre a_basic_tokenisation_body() {
52 1.1 kre atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
53 1.1 kre 'set -- a b c; echo $#'
54 1.1 kre atf_check -s exit:0 -o 'inline:2\n' -e empty ${TEST_SH} -c \
55 1.1 kre 'set -- a""b c; echo $#'
56 1.1 kre atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
57 1.1 kre 'set -- a"" b c; echo $#'
58 1.1 kre atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
59 1.1 kre 'set -- ""a b c\;; echo $#'
60 1.1 kre
61 1.1 kre atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
62 1.1 kre 'set -- set -- c; echo $#'
63 1.1 kre atf_check -s exit:0 -o 'inline:1\n' -e empty ${TEST_SH} -c \
64 1.1 kre 'set --;set -- c; echo $#'
65 1.1 kre atf_check -s exit:0 -o 'inline:1\n' -e empty ${TEST_SH} -c \
66 1.1 kre 'set --&set -- c; echo $#'
67 1.1 kre atf_check -s exit:0 -o 'inline:1\n' -e empty ${TEST_SH} -c \
68 1.1 kre 'set -- a b&&set -- c; echo $#'
69 1.1 kre atf_check -s exit:0 -o 'inline:2\n' -e empty ${TEST_SH} -c \
70 1.1 kre 'set -- a b||set -- c; echo $#'
71 1.1 kre }
72 1.1 kre
73 1.1 kre atf_test_case b_comments
74 1.1 kre b_comments_head() {
75 1.1 kre atf_set "descr" "Test the shell correctly handles comments"
76 1.1 kre }
77 1.1 kre b_comments_body() {
78 1.1 kre
79 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '#'
80 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '# exit 1'
81 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'true # exit 1'
82 1.1 kre atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c 'false # exit 0'
83 1.1 kre
84 1.1 kre atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
85 1.1 kre 'echo foo # bar'
86 1.1 kre atf_check -s exit:0 -o 'inline:foo # bar\n' -e empty ${TEST_SH} -c \
87 1.1 kre 'echo foo \# bar'
88 1.1 kre atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
89 1.1 kre 'echo foo; # echo bar'
90 1.1 kre atf_check -s exit:0 -o 'inline:foo#bar\n' -e empty ${TEST_SH} -c \
91 1.1 kre 'echo foo#bar'
92 1.1 kre atf_check -s exit:0 -o 'inline:foo# bar\n' -e empty ${TEST_SH} -c \
93 1.1 kre 'echo foo# bar'
94 1.1 kre atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
95 1.1 kre 'x=foo; echo ${x#bar}'
96 1.1 kre
97 1.1 kre atf_check -s exit:0 -o 'inline:#\n' -e empty ${TEST_SH} -c \
98 1.1 kre 'echo "#"'
99 1.1 kre atf_check -s exit:0 -o 'inline:#\n' -e empty ${TEST_SH} -c \
100 1.1 kre "echo '#'"
101 1.1 kre atf_check -s exit:0 -o 'inline:#\n' -e empty ${TEST_SH} -c \
102 1.1 kre 'echo \#'
103 1.1 kre atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
104 1.1 kre 'echo "#"#'
105 1.1 kre atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
106 1.1 kre "echo '#'#"
107 1.1 kre atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
108 1.1 kre 'echo \##'
109 1.1 kre atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
110 1.1 kre 'echo "#"# #"#"'
111 1.1 kre atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
112 1.1 kre "echo '#'# #'#'"
113 1.1 kre atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
114 1.1 kre 'echo \## #\#'
115 1.1 kre
116 1.1 kre cat <<-'DONE'|atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH}
117 1.1 kre # test comments do not provoke synax errors !\
118 1.1 kre echo foo # ( { " hello
119 1.1 kre while : # that's forever
120 1.1 kre do # the following command list
121 1.1 kre # starting with nothing ${unset?error}
122 1.1 kre break # done loop terminate $( echo bar; exit 1 )
123 1.1 kre done #####################################################
124 1.1 kre # "hello
125 1.1 kre exit 0
126 1.1 kre DONE
127 1.1 kre }
128 1.1 kre
129 1.1 kre atf_test_case c_line_wrapping
130 1.1 kre c_line_wrapping_head() {
131 1.1 kre atf_set "descr" "check aspects of command line wrapping"
132 1.1 kre }
133 1.1 kre c_line_wrapping_body() {
134 1.1 kre atf_require_prog ls
135 1.1 kre atf_require_prog printf
136 1.1 kre
137 1.1 kre cat <<- 'DONE' | atf_check -s exit:0 -o ignore -e empty ${TEST_SH} -e
138 1.1 kre l\
139 1.1 kre s
140 1.1 kre DONE
141 1.1 kre
142 1.1 kre cat <<- 'DONE' | atf_check -s exit:7 -o empty -e empty ${TEST_SH}
143 1.1 kre e\
144 1.1 kre x\
145 1.1 kre it \
146 1.1 kre 7
147 1.1 kre DONE
148 1.1 kre
149 1.1 kre # Have to do this twice as cannot say "any exit code but 0 or 7" ...
150 1.1 kre cat <<- 'DONE' | atf_check -s not-exit:0 -o empty -e not-empty \
151 1.1 kre ${TEST_SH}
152 1.1 kre e\
153 1.1 kre x\
154 1.1 kre it\
155 1.1 kre 7
156 1.1 kre DONE
157 1.1 kre cat <<- 'DONE' | atf_check -s not-exit:7 -o empty -e not-empty \
158 1.1 kre ${TEST_SH}
159 1.1 kre e\
160 1.1 kre x\
161 1.1 kre it\
162 1.1 kre 7
163 1.1 kre DONE
164 1.1 kre
165 1.1 kre cat <<- 'DONE' | atf_check -s exit:0 -o empty -e empty ${TEST_SH}
166 1.1 kre wh\
167 1.1 kre il\
168 1.1 kre e \
169 1.1 kre f\a\
170 1.1 kre \l\s\e
171 1.1 kre do
172 1.1 kre :\
173 1.1 kre ;
174 1.1 kre done
175 1.1 kre DONE
176 1.1 kre
177 1.1 kre cat <<- 'DONE' | atf_check -s exit:0 -o inline:'hellohellohellohello' \
178 1.1 kre -e empty ${TEST_SH}
179 1.1 kre V\
180 1.1 kre AR=hel\
181 1.1 kre lo
182 1.1 kre unset U V1
183 1.1 kre pri\
184 1.1 kre ntf '%s' ${\
185 1.1 kre VAR\
186 1.1 kre }
187 1.1 kre p\
188 1.1 kre r\
189 1.1 kre i\
190 1.1 kre n\
191 1.1 kre t\
192 1.1 kre f\
193 1.1 kre \
194 1.1 kre '%s' \
195 1.1 kre $\
196 1.1 kre {\
197 1.1 kre V\
198 1.1 kre A\
199 1.1 kre R}
200 1.1 kre printf '%s' ${U\
201 1.1 kre -\
202 1.1 kre "$\
203 1.1 kre {V\
204 1.1 kre 1:\
205 1.1 kre =$\
206 1.1 kre {V\
207 1.1 kre AR+\
208 1.1 kre ${V\
209 1.1 kre AR}\
210 1.1 kre }\
211 1.3 kre }"}
212 1.1 kre printf '%s' ${V\
213 1.1 kre 1?V1\
214 1.1 kre \
215 1.1 kre FAIL}
216 1.1 kre DONE
217 1.3 kre
218 1.1 kre cat <<- 'DONE' | atf_check -s exit:0 -o inline:'2\n' ${TEST_SH}
219 1.1 kre l\
220 1.1 kre s=7 bi\
221 1.1 kre n\
222 1.1 kre =\
223 1.1 kre 3
224 1.1 kre echo $(\
225 1.1 kre ( ls /bin )\
226 1.1 kre )
227 1.1 kre DONE
228 1.3 kre
229 1.3 kre # Inspired by src/etc/MAKEDEV.tmpl failure with (broken)
230 1.3 kre # sh LINENO code... avoid it happening again...
231 1.3 kre for VARS in 1:0:0:0 0:1:0:0 0:0:1:0 0:0:0:1 \
232 1.3 kre 1:0:0:1 1:0:1:0 1:1:0:0 0:1:1:0 \
233 1.3 kre 0:0:0:0 1:1:0:1 0:1:1:1 1:1:1:1
234 1.3 kre do
235 1.3 kre eval $(
236 1.3 kre IFS=:
237 1.3 kre set -- $VARS
238 1.3 kre test $(( $1 + $2 + $3 + $4 )) -eq 1 &&
239 1.3 kre R=OK || R=BAD
240 1.3 kre printf "R=%s;" $R
241 1.3 kre for v in a b c d
242 1.3 kre do
243 1.3 kre case $1 in
244 1.3 kre (0) printf "export %s=false;" $v;;
245 1.3 kre (1) printf "export %s=true;" $v;;
246 1.3 kre esac
247 1.3 kre shift
248 1.3 kre done
249 1.3 kre )
250 1.3 kre
251 1.3 kre cat <<- 'DONE' | atf_check -s exit:0 -o inline:"${R}" ${TEST_SH}
252 1.3 kre case $(( $($a && echo 1 || echo 0) + \
253 1.3 kre $($b && echo 1 || echo 0) + \
254 1.3 kre $($c && echo 1 || echo 0) + \
255 1.3 kre $($d && echo 1 || echo 0) ))
256 1.3 kre in
257 1.3 kre (1) printf OK ;;
258 1.3 kre (*) printf BAD ;;
259 1.3 kre esac
260 1.3 kre DONE
261 1.3 kre done
262 1.3 kre
263 1.3 kre # inspired by pkgsrc/pkgtools/cwrappers :: libnbcompat/configure
264 1.3 kre # failure with (broken) sh LINENO core .. avoid recurrence
265 1.3 kre # This test would have failed.
266 1.3 kre cat <<- 'DONE' | atf_check -s exit:0 -o inline:'/tmp\n' ${TEST_SH}
267 1.3 kre dn=/tmp/foo
268 1.3 kre
269 1.3 kre D=`dirname -- "${dn}" ||
270 1.3 kre expr X"${dn}" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
271 1.3 kre X"${dn}" : 'X\(//\)[^/]' \| \
272 1.3 kre X"${dn}" : 'X\(//\)$' \| \
273 1.3 kre X"${dn}" : 'X\(/\)' \| . 2>/dev/null ||
274 1.3 kre echo X"${dn}" |
275 1.3 kre sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
276 1.3 kre s//\1/
277 1.3 kre q
278 1.3 kre }
279 1.3 kre /^X\(\/\/\)[^/].*/{
280 1.3 kre s//\1/
281 1.3 kre q
282 1.3 kre }
283 1.3 kre /^X\(\/\/\)$/{
284 1.3 kre s//\1/
285 1.3 kre q
286 1.3 kre }
287 1.3 kre /^X\(\/\).*/{
288 1.3 kre s//\1/
289 1.3 kre q
290 1.3 kre }
291 1.3 kre s/.*/./; q'`
292 1.3 kre
293 1.3 kre echo "${D}"
294 1.3 kre DONE
295 1.3 kre return 0
296 1.1 kre }
297 1.1 kre
298 1.1 kre atf_test_case d_redirects
299 1.1 kre d_redirects_head() {
300 1.1 kre atf_set "descr" "Check parsing of redirect operators"
301 1.1 kre }
302 1.1 kre d_redirects_body() {
303 1.1 kre
304 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
305 1.1 kre '>/dev/null'
306 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
307 1.1 kre '</dev/null'
308 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
309 1.1 kre '>>/dev/null'
310 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
311 1.1 kre '<>/dev/null'
312 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
313 1.1 kre '</dev/null>/dev/null'
314 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
315 1.1 kre '>|/dev/null'
316 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
317 1.1 kre '>/dev/null>/dev/null>/dev/null'
318 1.1 kre
319 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
320 1.1 kre 'echo hello >/dev/null'
321 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
322 1.1 kre 'echo >/dev/null hello'
323 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
324 1.1 kre '>/dev/null echo hello'
325 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
326 1.1 kre 'echo hello >/dev/null world'
327 1.4 kre atf_check -s exit:0 -o 'inline:hello world\n' -e empty ${TEST_SH} -c \
328 1.1 kre 'echo hello </dev/null world'
329 1.1 kre
330 1.4 kre atf_check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
331 1.1 kre 'echo hello </dev/null'
332 1.4 kre atf_check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
333 1.1 kre 'echo hello 3</dev/null'
334 1.4 kre atf_check -s exit:0 -o 'inline:hello 3\n' -e empty ${TEST_SH} -c \
335 1.1 kre 'echo hello 3 </dev/null'
336 1.4 kre atf_check -s exit:0 -o 'inline:hello 3\n' -e empty ${TEST_SH} -c \
337 1.1 kre 'echo hello \3</dev/null'
338 1.4 kre atf_check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
339 1.1 kre 'echo hello</dev/null'
340 1.4 kre atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
341 1.1 kre 'hello=3; echo ${hello}</dev/null'
342 1.1 kre
343 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
344 1.1 kre '2>&1'
345 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
346 1.1 kre '2>& 1'
347 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
348 1.1 kre 'FD=1; 2>&"${FD}"'
349 1.4 kre atf_check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
350 1.1 kre 'FD=1; echo hello 2>&"${FD}" >&2'
351 1.1 kre
352 1.4 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
353 1.1 kre '2>&- 3<&- 4>&-'
354 1.1 kre
355 1.1 kre return 0
356 1.1 kre }
357 1.1 kre
358 1.1 kre atf_test_case f_variable_syntax
359 1.1 kre f_variable_syntax_head() {
360 1.1 kre atf_set "descr" "Check that var names of all legal forms work"
361 1.1 kre }
362 1.1 kre f_variable_syntax_body() {
363 1.1 kre # don't test _ as a variable, it can be "unusual"
364 1.1 kre for vname in a ab _a _9 a123 a_1_2_3 __ ___ ____ __1__ _0 \
365 1.1 kre A AA AAA AaBb _A_a A_a_ a1_ abc_123 ab_12_cd_ef_34_99 \
366 1.1 kre abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWXYZ_ \
367 1.5 kre A_VERY_LONG_VARIABLE_NAME_that_is_probably_longer_than_most_used_in_the_average_shell_script_already_about_100_chars_in_this_one_but_there_is_not_supposed_to_be_any_limit_on_the_length_at_all \
368 1.5 kre Then_Make_it_Even_Longer_by_Multiplying_it___A_VERY_LONG_VARIABLE_NAME_that_is_probably_longer_than_most_used_in_the_average_shell_script_already_about_100_chars_in_this_one_but_there_is_not_supposed_to_be_any_limit_on_the_length_at_all__A_VERY_LONG_VARIABLE_NAME_that_is_probably_longer_than_most_used_in_the_average_shell_script_already_about_100_chars_in_this_one_but_there_is_not_supposed_to_be_any_limit_on_the_length_at_all__A_VERY_LONG_VARIABLE_NAME_that_is_probably_longer_than_most_used_in_the_average_shell_script_already_about_100_chars_in_this_one_but_there_is_not_supposed_to_be_any_limit_on_the_length_at_all__A_VERY_LONG_VARIABLE_NAME_that_is_probably_longer_than_most_used_in_the_average_shell_script_already_about_100_chars_in_this_one_but_there_is_not_supposed_to_be_any_limit_on_the_length_at_all \
369 1.5 kre xyzzy __0123454321__ _0_1_2_3_4_5_6_7_8_9_ ABBA X_ Y__ Z___ \
370 1.1 kre _____________________________________________________________
371 1.1 kre do
372 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
373 1.1 kre "unset ${vname}"
374 1.1 kre atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -c \
375 1.1 kre "unset ${vname}; printf %s \${${vname}-OK}"
376 1.1 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
377 1.1 kre "${vname}=GOOD; printf %s \${${vname}-OK}"
378 1.1 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
379 1.1 kre "${vname}=GOOD; printf %s \$${vname}"
380 1.1 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
381 1.1 kre "unset ${vname};${vname}=GOOD;printf %s \${${vname}-OK}"
382 1.1 kre atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -c \
383 1.1 kre "${vname}=GOOD;unset ${vname};printf %s \${${vname}-OK}"
384 1.1 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
385 1.1 kre "${vname}=GOOD; unset ${vname}x; printf %s \$${vname}"
386 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
387 1.1 kre "unset ${vname}x; ${vname}=GOOD; printf %s \$${vname}x"
388 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
389 1.5 kre "${vname}=GOOD; ${vname}_=BAD; printf %s \$${vname}"
390 1.5 kre
391 1.5 kre case "${vname}" in
392 1.5 kre ?) continue;;
393 1.5 kre esac
394 1.5 kre
395 1.5 kre # The following tests do not work for 1 char var names.
396 1.5 kre # hence the check and "continue" above to skip the remaining
397 1.5 kre # tests for that case
398 1.5 kre
399 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
400 1.5 kre "${vname}=GOOD; unset ${vname%?}; printf %s \$${vname}"
401 1.5 kre
402 1.5 kre # (this next would work, but becomes just a duplicate of
403 1.5 kre # an earlier test, so is pointless for 1 ch names)
404 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
405 1.5 kre "${vname}=GOOD; unset ${vname}x ${vname%?}; printf %s \$${vname}"
406 1.5 kre
407 1.5 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
408 1.5 kre "unset ${vname%?};${vname}=GOOD; printf %s \$${vname%?}"
409 1.5 kre
410 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
411 1.5 kre "${vname}=GOOD; ${vname%?}=BAD; printf %s \$${vname}"
412 1.5 kre
413 1.5 kre # all the remaining tests require the 2nd char of the
414 1.5 kre # variable name to be a legal first character. That
415 1.5 kre # is, not a digit, so skip the rest if we have a digit
416 1.5 kre # second...
417 1.5 kre case "${vname}" in
418 1.5 kre ?[0-9]*) continue;;
419 1.5 kre esac
420 1.5 kre
421 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
422 1.5 kre "${vname}=GOOD; unset ${vname#?}; printf %s \$${vname}"
423 1.5 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
424 1.5 kre "unset ${vname#?};${vname}=GOOD; printf %s \$${vname#?}"
425 1.5 kre
426 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
427 1.5 kre "${vname}=GOOD; ${vname#?}=BAD; printf %s \$${vname}"
428 1.5 kre
429 1.5 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
430 1.5 kre "unset ${vname%?} ${vname#?} ${vname}x; ${vname}=GOOD;
431 1.5 kre printf %s \$${vname%?}\$${vname#?}\$${vname}x"
432 1.5 kre
433 1.5 kre atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
434 1.5 kre "${vname}=GOOD; ${vname%?}=BAD; ${vname}_=BAD;
435 1.5 kre ${vname#?}=BAD; printf %s \$${vname}"
436 1.1 kre done
437 1.1 kre
438 1.1 kre # don't test '.' in var names, some shells permit that (in ${} anyway)
439 1.1 kre # this test also cannot check for embedded - + ? = : % or #
440 1.1 kre for vname in ,x -p +h :def 0_1 'x*x' '()' '"' "'" 'a b c' '?!' ';'
441 1.1 kre do
442 1.1 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
443 1.1 kre "echo \${${vname}}"
444 1.1 kre done
445 1.1 kre
446 1.1 kre for vname in ,x -p +h :def 0_1 'x*x' '()' '"' "'" 'a b c' x,y,z '?!' \
447 1.1 kre ';' a-b a+b 'a?b' 'a:b' 'a%b' 'a#b' 0 1 99 @ '*' '!' '?'
448 1.1 kre do
449 1.1 kre # failure modes will differ, but they should all fail somehow
450 1.1 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
451 1.1 kre "${vname}="
452 1.1 kre done
453 1.1 kre
454 1.1 kre }
455 1.1 kre
456 1.1 kre atf_test_case g_var_assign
457 1.1 kre g_var_assign_head() {
458 1.1 kre atf_set "descr" "Check var assignments "
459 1.1 kre }
460 1.1 kre g_var_assign_body() {
461 1.1 kre atf_check -s exit:0 -e empty -o empty ${TEST_SH} -c \
462 1.1 kre 'a=b'
463 1.1 kre atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
464 1.1 kre '\a=b'
465 1.1 kre atf_check -s exit:0 -e empty -o empty ${TEST_SH} -c \
466 1.1 kre 'a=b c=d'
467 1.1 kre atf_check -s exit:0 -e empty -o 'inline:e=f\n' ${TEST_SH} -c \
468 1.1 kre 'a=b c=d echo e=f'
469 1.1 kre atf_check -s exit:0 -e empty -o 'inline:e=f\n' ${TEST_SH} -c \
470 1.1 kre 'a=b 2>/dev/null c=d </dev/null echo e=f'
471 1.1 kre
472 1.1 kre # We need to make sure that we do not accidentally
473 1.1 kre # find a command called 'a=b' ...
474 1.1 kre
475 1.1 kre for d in /foo /foo/bar /not-dir /no/such/directory '/!!!' \
476 1.1 kre '/-/--/#' '/"/""/"""' -
477 1.1 kre do
478 1.1 kre test -d "${d}" || break
479 1.1 kre done
480 1.1 kre test "${#d}" -gt 1 || atf-skip 'Wacky directories seem to exist!'
481 1.1 kre
482 1.1 kre atf_check -s exit:0 -e empty -o empty ${TEST_SH} -c \
483 1.1 kre "PATH='${d}';"'a=\b'
484 1.1 kre atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
485 1.1 kre "PATH='${d}';"'a\=b'
486 1.1 kre atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
487 1.1 kre "PATH='${d}';"'\a=b'
488 1.1 kre atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
489 1.1 kre "PATH='${d}';"'X=; c=d ${X} a=b'
490 1.1 kre }
491 1.1 kre
492 1.1 kre atf_test_case i_pipelines
493 1.1 kre i_pipelines_head() {
494 1.1 kre atf_set "descr" "Check pipelines"
495 1.1 kre }
496 1.1 kre i_pipelines_body() {
497 1.1 kre
498 1.1 kre cmd='printf "%s\n" foo'
499 1.1 kre for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
500 1.1 kre do
501 1.1 kre atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
502 1.1 kre "${cmd}"
503 1.1 kre cmd="${cmd} | cat"
504 1.1 kre done
505 1.1 kre
506 1.1 kre cmd='printf "%s\n" foo'
507 1.1 kre for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
508 1.1 kre do
509 1.1 kre atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
510 1.1 kre "${cmd}"
511 1.1 kre cmd="${cmd} |
512 1.1 kre cat"
513 1.1 kre done
514 1.1 kre
515 1.1 kre cmd='printf "%s\n" foo'
516 1.1 kre for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
517 1.1 kre do
518 1.1 kre atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
519 1.1 kre "${cmd}"
520 1.1 kre cmd="${cmd} |
521 1.1 kre
522 1.1 kre
523 1.1 kre
524 1.1 kre
525 1.1 kre cat"
526 1.1 kre done
527 1.1 kre
528 1.1 kre cmd='! printf "%s\n" foo'
529 1.1 kre for n in 1 2 3 4 5 6 7 8 9 10
530 1.1 kre do
531 1.1 kre atf_check -s exit:1 -o inline:'foo\n' -e empty ${TEST_SH} -c \
532 1.1 kre "${cmd}"
533 1.1 kre cmd="${cmd} | cat"
534 1.1 kre done
535 1.1 kre
536 1.1 kre cmd='exec 4>&2 3<&0; printf "%s\n" foo'
537 1.1 kre for n in 1 2 3
538 1.1 kre do
539 1.1 kre pfx=
540 1.1 kre for xtra in 'x=y' 'a=b' '6>&1' '5<&3'
541 1.1 kre do
542 1.1 kre atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
543 1.1 kre "${cmd} | ${xtra} cat"
544 1.1 kre
545 1.1 kre atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
546 1.1 kre "${cmd} | ${pfx} cat"
547 1.1 kre
548 1.1 kre pfx="${pfx} ${xtra}"
549 1.1 kre done
550 1.1 kre cmd="${cmd} | ${pfx} cat"
551 1.1 kre done
552 1.1 kre
553 1.1 kre # pipelines are not required to contain commands ...
554 1.1 kre # they don't do anything useful (at all) but the syntax is legal
555 1.1 kre base='4>&2'; cmd="${base}"
556 1.1 kre for pipe in 'a=b' '3<&0' '>>/dev/null' 'a= b= c=' '${x}' 'cat'
557 1.1 kre do
558 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
559 1.1 kre "${base} | ${pipe}"
560 1.1 kre
561 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
562 1.1 kre "${cmd} | ${pipe}"
563 1.1 kre
564 1.1 kre cmd="${cmd} | ${pipe}"
565 1.1 kre done
566 1.1 kre
567 1.1 kre # but the command cannot be empty, or a reserved word used improperly
568 1.1 kre base='printf "%s\n" foo'; cmd="${base}"
569 1.1 kre for pipe in '' do done then else fi esac
570 1.1 kre do
571 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
572 1.1 kre "${base} | ${pipe}"
573 1.1 kre
574 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
575 1.1 kre "${pipe} | ${base}"
576 1.1 kre
577 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
578 1.1 kre "${cmd} | ${pipe}"
579 1.1 kre
580 1.1 kre cmd="${cmd} | ${pipe}"
581 1.1 kre done
582 1.1 kre }
583 1.1 kre
584 1.1 kre atf_test_case j_and_or_lists
585 1.1 kre j_and_or_lists_head() {
586 1.1 kre atf_set "descr" "Check && and || command lists"
587 1.1 kre }
588 1.1 kre j_and_or_lists_body() {
589 1.1 kre and=true
590 1.1 kre or=false
591 1.1 kre and_or=false
592 1.1 kre for i in 1 2 3 4 5 6 7 8 9 10
593 1.1 kre do
594 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
595 1.1 kre "${and}"
596 1.1 kre
597 1.1 kre atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
598 1.1 kre "${or}"
599 1.1 kre
600 1.1 kre atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
601 1.1 kre "${and_or}"
602 1.1 kre
603 1.1 kre and="${and} && true"
604 1.1 kre or="${or} || false"
605 1.1 kre and_or="${and_or} || true && false"
606 1.1 kre done
607 1.1 kre
608 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
609 1.1 kre 'true &&'
610 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
611 1.1 kre '&& true'
612 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
613 1.1 kre '|| true'
614 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
615 1.1 kre 'true ||'
616 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
617 1.1 kre 'true || && false'
618 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
619 1.1 kre 'false || && true'
620 1.1 kre
621 1.1 kre cmd='printf "%s" foo | cat | cat>/dev/null'
622 1.1 kre line="${cmd}"
623 1.1 kre for i in 1 2 3 4
624 1.1 kre do
625 1.1 kre line="${line} && ! ${cmd} || ${cmd}"
626 1.1 kre
627 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
628 1.1 kre "${line}"
629 1.1 kre done
630 1.1 kre
631 1.1 kre }
632 1.1 kre
633 1.1 kre atf_test_case k_lists
634 1.1 kre k_lists_head() {
635 1.1 kre atf_set "descr" "Check ; command lists"
636 1.1 kre }
637 1.1 kre k_lists_body() {
638 1.1 kre line=
639 1.1 kre for N in 1 2 3 4
640 1.1 kre do
641 1.1 kre for cmd in \
642 1.1 kre true false : 'cat</dev/null>/dev/null' x=3 'exec 4>&-'
643 1.1 kre do
644 1.1 kre line="${line}${line:+;}${cmd}"
645 1.1 kre atf_check -s exit:0 -o 'inline:hello\nworld\n' \
646 1.1 kre -e empty ${TEST_SH} -c \
647 1.1 kre "echo hello; ${line}; echo world"
648 1.1 kre atf_check -s exit:0 -o 'inline:hello\nworld\n' \
649 1.1 kre -e empty ${TEST_SH} -c \
650 1.1 kre "echo hello; ${line}; echo world;"
651 1.1 kre done
652 1.1 kre done
653 1.1 kre
654 1.1 kre for cmd in ';' ';;' 'false ;; true' 'false; ;true' '; true'
655 1.1 kre do
656 1.1 kre atf_check -s not-exit:0 -o ignore -e not-empty \
657 1.1 kre ${TEST_SH} -c "${cmd}"
658 1.1 kre done
659 1.1 kre }
660 1.1 kre
661 1.1 kre atf_test_case l_async_lists
662 1.1 kre l_async_lists_head() {
663 1.1 kre atf_set "descr" "Check & command lists"
664 1.1 kre }
665 1.1 kre l_async_lists_body() {
666 1.1 kre line=
667 1.1 kre for N in 1 2 3 4
668 1.1 kre do
669 1.1 kre for cmd in \
670 1.1 kre true false : 'cat</dev/null>/dev/null' x=3 'exec 4>&-'
671 1.1 kre do
672 1.1 kre line="${line:+${line}&}${cmd}"
673 1.1 kre atf_check -s exit:0 -o 'inline:hello\nworld\n' \
674 1.1 kre -e empty ${TEST_SH} -c \
675 1.1 kre "echo hello; ${line}& echo world"
676 1.1 kre atf_check -s exit:0 -o 'inline:hello\nworld\n' \
677 1.1 kre -e empty ${TEST_SH} -c \
678 1.1 kre "echo hello; ${line}& echo world"
679 1.1 kre done
680 1.1 kre done
681 1.1 kre
682 1.1 kre for cmd in '&' ';&' '&;' '& true' 'false& &true'
683 1.1 kre do
684 1.1 kre atf_check -s not-exit:0 -o ignore -e not-empty \
685 1.1 kre ${TEST_SH} -c "${cmd}"
686 1.1 kre done
687 1.1 kre }
688 1.1 kre
689 1.1 kre atf_test_case m_compound_lists
690 1.1 kre m_compound_lists_head() {
691 1.1 kre atf_set "descr" "Check subshells () and { ;} command grouping"
692 1.1 kre }
693 1.1 kre m_compound_lists_body() {
694 1.1 kre # Note: (( is an unspecified (reserved) operator, don't use it...
695 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
696 1.1 kre '( true )'
697 1.1 kre atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
698 1.1 kre '( false )'
699 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
700 1.1 kre '( (:) )'
701 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
702 1.1 kre '( ( true ))'
703 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
704 1.1 kre '( ( ( ( ( true )))))'
705 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
706 1.1 kre '( ( ( ( (true);:));true))'
707 1.1 kre
708 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
709 1.1 kre '()'
710 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
711 1.1 kre '( )'
712 1.1 kre
713 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
714 1.1 kre '{ true; }'
715 1.1 kre atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
716 1.1 kre '{ false; }'
717 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
718 1.1 kre '{ { :; };}'
719 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
720 1.1 kre '{ { { { { :;};};};};}'
721 1.1 kre
722 1.1 kre atf_check -s exit:0 -o 'inline:}\n' -e empty ${TEST_SH} -c \
723 1.1 kre '{ echo } ; }'
724 1.1 kre atf_check -s exit:0 -o 'inline:{\n' -e empty ${TEST_SH} -c \
725 1.1 kre '{ echo { ; }'
726 1.1 kre }
727 1.1 kre
728 1.1 kre atf_test_case q_for_loop
729 1.1 kre q_for_loop_head() {
730 1.1 kre atf_set "descr" "Check for loop parsing"
731 1.1 kre }
732 1.1 kre q_for_loop_body() {
733 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
734 1.1 kre 'for x; do : ; done'
735 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
736 1.1 kre 'for x in ; do : ; done'
737 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
738 1.1 kre 'for x in a b c ; do : ; done'
739 1.1 kre
740 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
741 1.1 kre 'for x in in;do : ; done'
742 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
743 1.1 kre 'for x in for;do : ; done'
744 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
745 1.1 kre 'for x in do;do : ; done'
746 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
747 1.1 kre 'for for in in;do :;done'
748 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
749 1.1 kre 'for for in for;do :; done'
750 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
751 1.1 kre 'for for in do;do : ;done'
752 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
753 1.1 kre 'for in in in;do : ; done'
754 1.2 kre atf_check -s exit:0 -o 'inline:do\nin\ndo\n' -e empty ${TEST_SH} -c \
755 1.2 kre 'for in in in do in;do case $in in in)echo do;;do)echo in;;esac; done'
756 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
757 1.1 kre 'for in in for;do : ; done'
758 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
759 1.1 kre 'for in in do;do : ; done'
760 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
761 1.1 kre 'for do in in;do : ; done'
762 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
763 1.1 kre 'for do in for;do : ; done'
764 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
765 1.1 kre 'for do in do;do : ; done'
766 1.1 kre atf_check -s exit:0 -o 'inline:dodo\n' -e empty ${TEST_SH} -c \
767 1.1 kre 'for do in do;do echo ${do}do ; done'
768 1.1 kre }
769 1.1 kre
770 1.1 kre atf_test_case r_case
771 1.1 kre r_case_head() {
772 1.1 kre atf_set "descr" "Check case statement parsing"
773 1.1 kre }
774 1.1 kre r_case_body() {
775 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
776 1.1 kre 'case x in esac'
777 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
778 1.1 kre 'case x in x) esac'
779 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
780 1.1 kre 'case x in (x) esac'
781 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
782 1.1 kre 'case x in x) ;; esac'
783 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
784 1.1 kre 'case x in (x) ;; esac'
785 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
786 1.1 kre 'case x in x|y) ;; esac'
787 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
788 1.1 kre 'case x in (x|y) ;; esac'
789 1.1 kre
790 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
791 1.1 kre 'case x in x|esac) ;; esac'
792 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
793 1.1 kre 'case x in x|esac|y) ;; esac'
794 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
795 1.1 kre 'case x in (x|esac) ;; esac'
796 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
797 1.1 kre 'case x in (x|esac|y) ;; esac'
798 1.1 kre
799 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
800 1.1 kre 'case x in in) esac'
801 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
802 1.1 kre 'case x in in) ;; esac'
803 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
804 1.1 kre 'case x in x|in) ;; esac'
805 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
806 1.1 kre 'case x in x|in|y) ;; esac'
807 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
808 1.1 kre 'case x in (x|in) ;; esac'
809 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
810 1.1 kre 'case x in (in|x) ;; esac'
811 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
812 1.1 kre 'case x in (x|in|y) ;; esac'
813 1.1 kre
814 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
815 1.1 kre 'case case in case) esac'
816 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
817 1.1 kre 'case in in in) esac'
818 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
819 1.1 kre 'case esac in (in) esac'
820 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
821 1.1 kre 'case in in esac|cat'
822 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
823 1.1 kre 'case esac in esac|cat'
824 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
825 1.1 kre 'case in in esac|case x in u) echo foo;; esac'
826 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
827 1.1 kre 'case esac in esac|case x in u) echo foo;; esac'
828 1.1 kre atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
829 1.1 kre 'case in in esac|case x in x) echo foo;; esac'
830 1.1 kre
831 1.1 kre atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
832 1.1 kre 'case in in (esac|cat'
833 1.1 kre
834 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
835 1.1 kre 'case x in x );;esac'
836 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
837 1.1 kre 'case x in ( x );;esac'
838 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
839 1.1 kre 'case x in x | y );;esac'
840 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
841 1.1 kre 'case x in ( x | y );;esac'
842 1.1 kre
843 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
844 1.1 kre 'case x
845 1.1 kre in
846 1.1 kre ( x | y )
847 1.1 kre
848 1.1 kre ;;
849 1.1 kre
850 1.1 kre
851 1.1 kre esac
852 1.1 kre '
853 1.1 kre }
854 1.1 kre
855 1.1 kre atf_test_case s_if
856 1.1 kre s_if_head() {
857 1.1 kre atf_set "descr" "Check if statement parsing"
858 1.1 kre }
859 1.1 kre s_if_body() {
860 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
861 1.1 kre 'if :; then :; fi'
862 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
863 1.1 kre 'if :; then :; else :; fi'
864 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
865 1.1 kre 'if :; then :; elif :; then :; else :; fi'
866 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
867 1.1 kre 'if :; then :; elif :; then :; elif :; then :; else :; fi'
868 1.1 kre
869 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
870 1.1 kre 'if :; then : else :; fi'
871 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
872 1.1 kre 'if : then :; then :; fi'
873 1.1 kre
874 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
875 1.1 kre 'if if :;then :;fi then :;fi'
876 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
877 1.1 kre 'if if :;then if :;then :;fi fi;then :;fi'
878 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
879 1.1 kre 'if if :;then :;fi;then :;else if :;then :;fi fi'
880 1.1 kre
881 1.1 kre for a in true false; do
882 1.1 kre for b in true false; do
883 1.1 kre for c in true false; do
884 1.1 kre
885 1.1 kre $a && out=a || {
886 1.1 kre $b && out=b || {
887 1.1 kre $c && out=c || {
888 1.1 kre out=d; };};}
889 1.1 kre
890 1.1 kre atf_check -s exit:0 -e empty \
891 1.1 kre -o "inline:${out}"'\n' \
892 1.1 kre ${TEST_SH} -c \
893 1.1 kre "if $a; then echo a
894 1.1 kre elif $b; then echo b
895 1.1 kre elif $c; then echo c
896 1.1 kre else echo d
897 1.1 kre fi"
898 1.1 kre done
899 1.1 kre done
900 1.1 kre done
901 1.1 kre }
902 1.1 kre
903 1.1 kre atf_test_case t_loops
904 1.1 kre t_loops_head() {
905 1.1 kre atf_set "descr" "Check while/until loop parsing"
906 1.1 kre }
907 1.1 kre t_loops_body() {
908 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
909 1.1 kre 'while false; do :; done'
910 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
911 1.1 kre 'while false; do \done; done'
912 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
913 1.1 kre 'until :; do :; done'
914 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
915 1.1 kre 'until :; do \done; done'
916 1.1 kre
917 1.1 kre atf_check -s exit:0 -o 'inline:x\n1\n' -e empty ${TEST_SH} -c \
918 1.1 kre ':; while (exit) do echo x; false; done; echo $?'
919 1.1 kre atf_check -s exit:0 -o 'inline:x\n0\n' -e empty ${TEST_SH} -c \
920 1.1 kre 'false; until (exit) do echo x; done; echo $?'
921 1.1 kre }
922 1.1 kre
923 1.1 kre atf_test_case u_case_cont
924 1.1 kre u_case_cont_head() {
925 1.1 kre atf_set "descr" "Check case stmt parsing using ;& [optional]"
926 1.1 kre }
927 1.1 kre u_case_cont_body() {
928 1.1 kre
929 1.1 kre ${TEST_SH} -c 'case x in (x) false ;& (y) : ;; esac' 2>/dev/null ||
930 1.1 kre atf_skip ";& case list terminator unsupported in ${TEST_SH}"
931 1.1 kre
932 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
933 1.1 kre 'case x in x) ;& esac'
934 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
935 1.1 kre 'case x in (x) ;& esac'
936 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
937 1.1 kre 'case x in x|y) ;& esac'
938 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
939 1.1 kre 'case x in (x|y) ;& esac'
940 1.1 kre
941 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
942 1.1 kre 'case x in x );&esac'
943 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
944 1.1 kre 'case x in ( x );&esac'
945 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
946 1.1 kre 'case x in x | y );&esac'
947 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
948 1.1 kre 'case x in ( x | y );&esac'
949 1.1 kre
950 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
951 1.1 kre 'case x in x) ;& (y) esac'
952 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
953 1.1 kre 'case x in (x) ;& esac'
954 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
955 1.1 kre 'case x in x|y) ;& esac'
956 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
957 1.1 kre 'case x in (x|y) ;& esac'
958 1.1 kre }
959 1.1 kre
960 1.1 kre atf_test_case x_functions
961 1.1 kre x_functions_head() {
962 1.1 kre atf_set "descr" "Check function definition parsing"
963 1.1 kre }
964 1.1 kre x_functions_body() {
965 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
966 1.1 kre 'func() { :; }'
967 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
968 1.1 kre 'func() { :; }; func'
969 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
970 1.1 kre 'func()(:)'
971 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
972 1.1 kre 'func()if :; then false; else true; fi'
973 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
974 1.1 kre 'func()while false; do :;done'
975 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
976 1.1 kre 'func () for a in b c; do :; done'
977 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
978 1.1 kre 'func() case x in (y) esac'
979 1.1 kre
980 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
981 1.1 kre 'f1() { f2() { :; }; }; f1; f2'
982 1.1 kre
983 1.1 kre # f2 should be not found, but f1 clears $?
984 1.1 kre atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} -c \
985 1.1 kre 'f1() { f2() { :; }; }; f2; f1'
986 1.1 kre
987 1.1 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
988 1.1 kre 'f1() { eval "$1() { :; }"; }; f1 f2; f2'
989 1.1 kre }
990 1.1 kre
991 1.6 kre atf_test_case z_PR_48498
992 1.6 kre z_PR_48498_head() {
993 1.6 kre atf_set "descr" "Check for detecting the syntax error from PR bin/48498"
994 1.6 kre }
995 1.6 kre z_PR_48498_body() {
996 1.6 kre
997 1.6 kre # reserved words/operators that end things,
998 1.6 kre # were completely ignored after a ';' or '&'
999 1.6 kre # many of these tests lifted directly from the PR
1000 1.6 kre
1001 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1002 1.6 kre 'true; fi'
1003 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1004 1.6 kre 'false; fi'
1005 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1006 1.6 kre 'false; then echo wut'
1007 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1008 1.6 kre 'true; then echo wut'
1009 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1010 1.6 kre 'true; do echo wut'
1011 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1012 1.6 kre 'true; then'
1013 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1014 1.6 kre 'true; else'
1015 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1016 1.6 kre 'true; do'
1017 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1018 1.6 kre 'true; done'
1019 1.6 kre # {
1020 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1021 1.6 kre ': ; }'
1022 1.6 kre # (
1023 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1024 1.6 kre ':;)'
1025 1.6 kre
1026 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1027 1.6 kre 'true& fi'
1028 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1029 1.6 kre 'false& fi'
1030 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1031 1.6 kre 'false& then echo wut'
1032 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1033 1.6 kre 'true& then echo wut'
1034 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1035 1.6 kre 'true& do echo wut'
1036 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1037 1.6 kre 'true& then'
1038 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1039 1.6 kre 'true& else'
1040 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1041 1.6 kre 'true& do'
1042 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1043 1.6 kre 'true&done'
1044 1.6 kre # {
1045 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1046 1.6 kre ':&}'
1047 1.6 kre # (
1048 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1049 1.6 kre ':&)'
1050 1.6 kre }
1051 1.6 kre
1052 1.6 kre atf_test_case z_PR_52426
1053 1.6 kre z_PR_52426_head() {
1054 1.6 kre atf_set "descr" "Check for detecting the syntax error from PR bin/52426"
1055 1.6 kre }
1056 1.6 kre z_PR_52426_body() {
1057 1.6 kre # Absoluely anything was permitted as a pattern of a case
1058 1.6 kre # statement, any token (except 'esac') would serve
1059 1.6 kre # What follows are a few "pretty" examples that were accepted.
1060 1.6 kre # The first is the example from the PR
1061 1.6 kre
1062 1.6 kre # Note we use only ;; type case lists, ;& should do the same, but
1063 1.6 kre # only for shells that support it, we do not want the shell to
1064 1.6 kre # object to any of these just because it does not support ;&
1065 1.6 kre
1066 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1067 1.6 kre 'case x in <|() ;; esac'
1068 1.6 kre
1069 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1070 1.6 kre 'case x in ((|)) ;; esac'
1071 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1072 1.6 kre 'case x in _|() ;; esac'
1073 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1074 1.6 kre 'case x in ()|() ;; esac'
1075 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1076 1.6 kre 'case x in -|;) ;; esac'
1077 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1078 1.6 kre 'case x in (;|-) ;; esac'
1079 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1080 1.6 kre 'case x in ;;|;) ;; esac'
1081 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1082 1.6 kre 'case x in (|| | ||) ;; esac'
1083 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1084 1.6 kre 'case x in (<<|>>) ;; esac'
1085 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1086 1.6 kre 'case x in (&&|&) ;; (|||>&) ;; &) esac'
1087 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1088 1.6 kre 'case x in (>||<) ;; esac'
1089 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1090 1.6 kre 'case x in( || | || | || | || | || );; esac'
1091 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1092 1.6 kre 'case x in (||| ||| ||| ||| ||) ;; esac'
1093 1.6 kre atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
1094 1.6 kre 'case x in <> |
1095 1.6 kre ) ;; esac'
1096 1.6 kre
1097 1.6 kre # now check some similar looking cases that are supposed to work
1098 1.6 kre # That is, make sure the fix for the PR does not break valid cases.
1099 1.6 kre
1100 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1101 1.6 kre 'case fi in ({|}) ;; (!) ;; esac'
1102 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1103 1.6 kre 'case esac in ([|]);; (][);; !!!|!!!|!!!|!!!);; esac'
1104 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1105 1.6 kre 'case then in ({[]]}) ;; (^^);; (^|^);; ([!]);; (-);; esac'
1106 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1107 1.6 kre 'case while in while );;(if|then|elif|fi);;(do|done);; esac'
1108 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1109 1.6 kre 'case until in($);;($$);;($4.50);;(1/2);;0.3333);;esac'
1110 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1111 1.6 kre 'case return in !);; !$);; $!);; !#);; (@);; esac'
1112 1.6 kre atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
1113 1.6 kre 'case break in (/);; (\/);; (/\|/\));; (\\//);; esac'
1114 1.6 kre }
1115 1.6 kre
1116 1.1 kre atf_init_test_cases() {
1117 1.1 kre atf_add_test_case a_basic_tokenisation
1118 1.1 kre atf_add_test_case b_comments
1119 1.1 kre atf_add_test_case c_line_wrapping
1120 1.1 kre atf_add_test_case d_redirects
1121 1.1 kre atf_add_test_case f_variable_syntax
1122 1.1 kre atf_add_test_case g_var_assign
1123 1.1 kre atf_add_test_case i_pipelines
1124 1.1 kre atf_add_test_case j_and_or_lists
1125 1.1 kre atf_add_test_case k_lists
1126 1.1 kre atf_add_test_case l_async_lists
1127 1.1 kre atf_add_test_case m_compound_lists
1128 1.1 kre atf_add_test_case q_for_loop
1129 1.1 kre atf_add_test_case r_case
1130 1.1 kre atf_add_test_case s_if
1131 1.1 kre atf_add_test_case t_loops
1132 1.1 kre atf_add_test_case u_case_cont
1133 1.1 kre atf_add_test_case x_functions
1134 1.6 kre
1135 1.6 kre atf_add_test_case z_PR_48498
1136 1.6 kre atf_add_test_case z_PR_52426
1137 1.1 kre }
1138