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