t_syntax.sh revision 1.3 1 # $NetBSD: t_syntax.sh,v 1.3 2017/06/09 23:49:58 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 core .. 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_redirects
299 d_redirects_head() {
300 atf_set "descr" "Check parsing of redirect operators"
301 }
302 d_redirects_body() {
303
304 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
305 '>/dev/null'
306 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
307 '</dev/null'
308 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
309 '>>/dev/null'
310 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
311 '<>/dev/null'
312 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
313 '</dev/null>/dev/null'
314 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
315 '>|/dev/null'
316 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
317 '>/dev/null>/dev/null>/dev/null'
318
319 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
320 'echo hello >/dev/null'
321 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
322 'echo >/dev/null hello'
323 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
324 '>/dev/null echo hello'
325 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
326 'echo hello >/dev/null world'
327 atf-check -s exit:0 -o 'inline:hello world\n' -e empty ${TEST_SH} -c \
328 'echo hello </dev/null world'
329
330 atf-check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
331 'echo hello </dev/null'
332 atf-check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
333 'echo hello 3</dev/null'
334 atf-check -s exit:0 -o 'inline:hello 3\n' -e empty ${TEST_SH} -c \
335 'echo hello 3 </dev/null'
336 atf-check -s exit:0 -o 'inline:hello 3\n' -e empty ${TEST_SH} -c \
337 'echo hello \3</dev/null'
338 atf-check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
339 'echo hello</dev/null'
340 atf-check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
341 'hello=3; echo ${hello}</dev/null'
342
343 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
344 '2>&1'
345 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
346 '2>& 1'
347 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
348 'FD=1; 2>&"${FD}"'
349 atf-check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
350 'FD=1; echo hello 2>&"${FD}" >&2'
351
352 atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
353 '2>&- 3<&- 4>&-'
354
355 return 0
356 }
357
358 atf_test_case f_variable_syntax
359 f_variable_syntax_head() {
360 atf_set "descr" "Check that var names of all legal forms work"
361 }
362 f_variable_syntax_body() {
363 # don't test _ as a variable, it can be "unusual"
364 for vname in a ab _a _9 a123 a_1_2_3 __ ___ ____ __1__ _0 \
365 A AA AAA AaBb _A_a A_a_ a1_ abc_123 ab_12_cd_ef_34_99 \
366 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWXYZ_ \
367 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 xyzzy \
368 _____________________________________________________________
369 do
370 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
371 "unset ${vname}"
372 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -c \
373 "unset ${vname}; printf %s \${${vname}-OK}"
374 atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
375 "${vname}=GOOD; printf %s \${${vname}-OK}"
376 atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
377 "${vname}=GOOD; printf %s \$${vname}"
378 atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
379 "unset ${vname};${vname}=GOOD;printf %s \${${vname}-OK}"
380 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -c \
381 "${vname}=GOOD;unset ${vname};printf %s \${${vname}-OK}"
382 atf_check -s exit:0 -o match:GOOD -e empty ${TEST_SH} -c \
383 "${vname}=GOOD; unset ${vname}x; printf %s \$${vname}"
384 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
385 "unset ${vname}x; ${vname}=GOOD; printf %s \$${vname}x"
386 done
387
388 # don't test '.' in var names, some shells permit that (in ${} anyway)
389 # this test also cannot check for embedded - + ? = : % or #
390 for vname in ,x -p +h :def 0_1 'x*x' '()' '"' "'" 'a b c' '?!' ';'
391 do
392 atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
393 "echo \${${vname}}"
394 done
395
396 for vname in ,x -p +h :def 0_1 'x*x' '()' '"' "'" 'a b c' x,y,z '?!' \
397 ';' a-b a+b 'a?b' 'a:b' 'a%b' 'a#b' 0 1 99 @ '*' '!' '?'
398 do
399 # failure modes will differ, but they should all fail somehow
400 atf_check -s not-exit:0 -o ignore -e not-empty ${TEST_SH} -c \
401 "${vname}="
402 done
403
404 }
405
406 atf_test_case g_var_assign
407 g_var_assign_head() {
408 atf_set "descr" "Check var assignments "
409 }
410 g_var_assign_body() {
411 atf_check -s exit:0 -e empty -o empty ${TEST_SH} -c \
412 'a=b'
413 atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
414 '\a=b'
415 atf_check -s exit:0 -e empty -o empty ${TEST_SH} -c \
416 'a=b c=d'
417 atf_check -s exit:0 -e empty -o 'inline:e=f\n' ${TEST_SH} -c \
418 'a=b c=d echo e=f'
419 atf_check -s exit:0 -e empty -o 'inline:e=f\n' ${TEST_SH} -c \
420 'a=b 2>/dev/null c=d </dev/null echo e=f'
421
422 # We need to make sure that we do not accidentally
423 # find a command called 'a=b' ...
424
425 for d in /foo /foo/bar /not-dir /no/such/directory '/!!!' \
426 '/-/--/#' '/"/""/"""' -
427 do
428 test -d "${d}" || break
429 done
430 test "${#d}" -gt 1 || atf-skip 'Wacky directories seem to exist!'
431
432 atf_check -s exit:0 -e empty -o empty ${TEST_SH} -c \
433 "PATH='${d}';"'a=\b'
434 atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
435 "PATH='${d}';"'a\=b'
436 atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
437 "PATH='${d}';"'\a=b'
438 atf_check -s not-exit:0 -e not-empty -o empty ${TEST_SH} -c \
439 "PATH='${d}';"'X=; c=d ${X} a=b'
440 }
441
442 atf_test_case i_pipelines
443 i_pipelines_head() {
444 atf_set "descr" "Check pipelines"
445 }
446 i_pipelines_body() {
447
448 cmd='printf "%s\n" foo'
449 for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
450 do
451 atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
452 "${cmd}"
453 cmd="${cmd} | cat"
454 done
455
456 cmd='printf "%s\n" foo'
457 for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
458 do
459 atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
460 "${cmd}"
461 cmd="${cmd} |
462 cat"
463 done
464
465 cmd='printf "%s\n" foo'
466 for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
467 do
468 atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
469 "${cmd}"
470 cmd="${cmd} |
471
472
473
474
475 cat"
476 done
477
478 cmd='! printf "%s\n" foo'
479 for n in 1 2 3 4 5 6 7 8 9 10
480 do
481 atf_check -s exit:1 -o inline:'foo\n' -e empty ${TEST_SH} -c \
482 "${cmd}"
483 cmd="${cmd} | cat"
484 done
485
486 cmd='exec 4>&2 3<&0; printf "%s\n" foo'
487 for n in 1 2 3
488 do
489 pfx=
490 for xtra in 'x=y' 'a=b' '6>&1' '5<&3'
491 do
492 atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
493 "${cmd} | ${xtra} cat"
494
495 atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH} -c \
496 "${cmd} | ${pfx} cat"
497
498 pfx="${pfx} ${xtra}"
499 done
500 cmd="${cmd} | ${pfx} cat"
501 done
502
503 # pipelines are not required to contain commands ...
504 # they don't do anything useful (at all) but the syntax is legal
505 base='4>&2'; cmd="${base}"
506 for pipe in 'a=b' '3<&0' '>>/dev/null' 'a= b= c=' '${x}' 'cat'
507 do
508 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
509 "${base} | ${pipe}"
510
511 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
512 "${cmd} | ${pipe}"
513
514 cmd="${cmd} | ${pipe}"
515 done
516
517 # but the command cannot be empty, or a reserved word used improperly
518 base='printf "%s\n" foo'; cmd="${base}"
519 for pipe in '' do done then else fi esac
520 do
521 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
522 "${base} | ${pipe}"
523
524 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
525 "${pipe} | ${base}"
526
527 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
528 "${cmd} | ${pipe}"
529
530 cmd="${cmd} | ${pipe}"
531 done
532 }
533
534 atf_test_case j_and_or_lists
535 j_and_or_lists_head() {
536 atf_set "descr" "Check && and || command lists"
537 }
538 j_and_or_lists_body() {
539 and=true
540 or=false
541 and_or=false
542 for i in 1 2 3 4 5 6 7 8 9 10
543 do
544 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
545 "${and}"
546
547 atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
548 "${or}"
549
550 atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
551 "${and_or}"
552
553 and="${and} && true"
554 or="${or} || false"
555 and_or="${and_or} || true && false"
556 done
557
558 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
559 'true &&'
560 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
561 '&& true'
562 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
563 '|| true'
564 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
565 'true ||'
566 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
567 'true || && false'
568 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
569 'false || && true'
570
571 cmd='printf "%s" foo | cat | cat>/dev/null'
572 line="${cmd}"
573 for i in 1 2 3 4
574 do
575 line="${line} && ! ${cmd} || ${cmd}"
576
577 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
578 "${line}"
579 done
580
581 }
582
583 atf_test_case k_lists
584 k_lists_head() {
585 atf_set "descr" "Check ; command lists"
586 }
587 k_lists_body() {
588 line=
589 for N in 1 2 3 4
590 do
591 for cmd in \
592 true false : 'cat</dev/null>/dev/null' x=3 'exec 4>&-'
593 do
594 line="${line}${line:+;}${cmd}"
595 atf_check -s exit:0 -o 'inline:hello\nworld\n' \
596 -e empty ${TEST_SH} -c \
597 "echo hello; ${line}; echo world"
598 atf_check -s exit:0 -o 'inline:hello\nworld\n' \
599 -e empty ${TEST_SH} -c \
600 "echo hello; ${line}; echo world;"
601 done
602 done
603
604 for cmd in ';' ';;' 'false ;; true' 'false; ;true' '; true'
605 do
606 atf_check -s not-exit:0 -o ignore -e not-empty \
607 ${TEST_SH} -c "${cmd}"
608 done
609 }
610
611 atf_test_case l_async_lists
612 l_async_lists_head() {
613 atf_set "descr" "Check & command lists"
614 }
615 l_async_lists_body() {
616 line=
617 for N in 1 2 3 4
618 do
619 for cmd in \
620 true false : 'cat</dev/null>/dev/null' x=3 'exec 4>&-'
621 do
622 line="${line:+${line}&}${cmd}"
623 atf_check -s exit:0 -o 'inline:hello\nworld\n' \
624 -e empty ${TEST_SH} -c \
625 "echo hello; ${line}& echo world"
626 atf_check -s exit:0 -o 'inline:hello\nworld\n' \
627 -e empty ${TEST_SH} -c \
628 "echo hello; ${line}& echo world"
629 done
630 done
631
632 for cmd in '&' ';&' '&;' '& true' 'false& &true'
633 do
634 atf_check -s not-exit:0 -o ignore -e not-empty \
635 ${TEST_SH} -c "${cmd}"
636 done
637 }
638
639 atf_test_case m_compound_lists
640 m_compound_lists_head() {
641 atf_set "descr" "Check subshells () and { ;} command grouping"
642 }
643 m_compound_lists_body() {
644 # Note: (( is an unspecified (reserved) operator, don't use it...
645 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
646 '( true )'
647 atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
648 '( false )'
649 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
650 '( (:) )'
651 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
652 '( ( true ))'
653 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
654 '( ( ( ( ( true )))))'
655 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
656 '( ( ( ( (true);:));true))'
657
658 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
659 '()'
660 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
661 '( )'
662
663 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
664 '{ true; }'
665 atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c \
666 '{ false; }'
667 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
668 '{ { :; };}'
669 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
670 '{ { { { { :;};};};};}'
671
672 atf_check -s exit:0 -o 'inline:}\n' -e empty ${TEST_SH} -c \
673 '{ echo } ; }'
674 atf_check -s exit:0 -o 'inline:{\n' -e empty ${TEST_SH} -c \
675 '{ echo { ; }'
676 }
677
678 atf_test_case q_for_loop
679 q_for_loop_head() {
680 atf_set "descr" "Check for loop parsing"
681 }
682 q_for_loop_body() {
683 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
684 'for x; do : ; done'
685 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
686 'for x in ; do : ; done'
687 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
688 'for x in a b c ; do : ; done'
689
690 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
691 'for x in in;do : ; done'
692 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
693 'for x in for;do : ; done'
694 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
695 'for x in do;do : ; done'
696 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
697 'for for in in;do :;done'
698 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
699 'for for in for;do :; done'
700 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
701 'for for in do;do : ;done'
702 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
703 'for in in in;do : ; done'
704 atf_check -s exit:0 -o 'inline:do\nin\ndo\n' -e empty ${TEST_SH} -c \
705 'for in in in do in;do case $in in in)echo do;;do)echo in;;esac; done'
706 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
707 'for in in for;do : ; done'
708 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
709 'for in in do;do : ; done'
710 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
711 'for do in in;do : ; done'
712 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
713 'for do in for;do : ; done'
714 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
715 'for do in do;do : ; done'
716 atf_check -s exit:0 -o 'inline:dodo\n' -e empty ${TEST_SH} -c \
717 'for do in do;do echo ${do}do ; done'
718 }
719
720 atf_test_case r_case
721 r_case_head() {
722 atf_set "descr" "Check case statement parsing"
723 }
724 r_case_body() {
725 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
726 'case x in esac'
727 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
728 'case x in x) esac'
729 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
730 'case x in (x) esac'
731 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
732 'case x in x) ;; esac'
733 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
734 'case x in (x) ;; esac'
735 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
736 'case x in x|y) ;; esac'
737 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
738 'case x in (x|y) ;; esac'
739
740 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
741 'case x in x|esac) ;; esac'
742 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
743 'case x in x|esac|y) ;; esac'
744 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
745 'case x in (x|esac) ;; esac'
746 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
747 'case x in (x|esac|y) ;; esac'
748
749 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
750 'case x in in) esac'
751 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
752 'case x in in) ;; esac'
753 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
754 'case x in x|in) ;; esac'
755 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
756 'case x in x|in|y) ;; esac'
757 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
758 'case x in (x|in) ;; esac'
759 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
760 'case x in (in|x) ;; esac'
761 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
762 'case x in (x|in|y) ;; esac'
763
764 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
765 'case case in case) esac'
766 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
767 'case in in in) esac'
768 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
769 'case esac in (in) esac'
770 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
771 'case in in esac|cat'
772 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
773 'case esac in esac|cat'
774 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
775 'case in in esac|case x in u) echo foo;; esac'
776 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
777 'case esac in esac|case x in u) echo foo;; esac'
778 atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
779 'case in in esac|case x in x) echo foo;; esac'
780
781 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
782 'case in in (esac|cat'
783
784 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
785 'case x in x );;esac'
786 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
787 'case x in ( x );;esac'
788 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
789 'case x in x | y );;esac'
790 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
791 'case x in ( x | y );;esac'
792
793 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
794 'case x
795 in
796 ( x | y )
797
798 ;;
799
800
801 esac
802 '
803 }
804
805 atf_test_case s_if
806 s_if_head() {
807 atf_set "descr" "Check if statement parsing"
808 }
809 s_if_body() {
810 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
811 'if :; then :; fi'
812 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
813 'if :; then :; else :; fi'
814 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
815 'if :; then :; elif :; then :; else :; fi'
816 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
817 'if :; then :; elif :; then :; elif :; then :; else :; fi'
818
819 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
820 'if :; then : else :; fi'
821 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
822 'if : then :; then :; fi'
823
824 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
825 'if if :;then :;fi then :;fi'
826 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
827 'if if :;then if :;then :;fi fi;then :;fi'
828 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
829 'if if :;then :;fi;then :;else if :;then :;fi fi'
830
831 for a in true false; do
832 for b in true false; do
833 for c in true false; do
834
835 $a && out=a || {
836 $b && out=b || {
837 $c && out=c || {
838 out=d; };};}
839
840 atf_check -s exit:0 -e empty \
841 -o "inline:${out}"'\n' \
842 ${TEST_SH} -c \
843 "if $a; then echo a
844 elif $b; then echo b
845 elif $c; then echo c
846 else echo d
847 fi"
848 done
849 done
850 done
851 }
852
853 atf_test_case t_loops
854 t_loops_head() {
855 atf_set "descr" "Check while/until loop parsing"
856 }
857 t_loops_body() {
858 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
859 'while false; do :; done'
860 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
861 'while false; do \done; done'
862 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
863 'until :; do :; done'
864 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
865 'until :; do \done; done'
866
867 atf_check -s exit:0 -o 'inline:x\n1\n' -e empty ${TEST_SH} -c \
868 ':; while (exit) do echo x; false; done; echo $?'
869 atf_check -s exit:0 -o 'inline:x\n0\n' -e empty ${TEST_SH} -c \
870 'false; until (exit) do echo x; done; echo $?'
871 }
872
873 atf_test_case u_case_cont
874 u_case_cont_head() {
875 atf_set "descr" "Check case stmt parsing using ;& [optional]"
876 }
877 u_case_cont_body() {
878
879 ${TEST_SH} -c 'case x in (x) false ;& (y) : ;; esac' 2>/dev/null ||
880 atf_skip ";& case list terminator unsupported in ${TEST_SH}"
881
882 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
883 'case x in x) ;& esac'
884 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
885 'case x in (x) ;& esac'
886 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
887 'case x in x|y) ;& esac'
888 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
889 'case x in (x|y) ;& esac'
890
891 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
892 'case x in x );&esac'
893 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
894 'case x in ( x );&esac'
895 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
896 'case x in x | y );&esac'
897 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
898 'case x in ( x | y );&esac'
899
900 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
901 'case x in x) ;& (y) esac'
902 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
903 'case x in (x) ;& esac'
904 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
905 'case x in x|y) ;& esac'
906 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
907 'case x in (x|y) ;& esac'
908 }
909
910 atf_test_case x_functions
911 x_functions_head() {
912 atf_set "descr" "Check function definition parsing"
913 }
914 x_functions_body() {
915 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
916 'func() { :; }'
917 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
918 'func() { :; }; func'
919 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
920 'func()(:)'
921 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
922 'func()if :; then false; else true; fi'
923 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
924 'func()while false; do :;done'
925 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
926 'func () for a in b c; do :; done'
927 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
928 'func() case x in (y) esac'
929
930 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
931 'f1() { f2() { :; }; }; f1; f2'
932
933 # f2 should be not found, but f1 clears $?
934 atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} -c \
935 'f1() { f2() { :; }; }; f2; f1'
936
937 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
938 'f1() { eval "$1() { :; }"; }; f1 f2; f2'
939 }
940
941 atf_init_test_cases() {
942 atf_add_test_case a_basic_tokenisation
943 atf_add_test_case b_comments
944 atf_add_test_case c_line_wrapping
945 atf_add_test_case d_redirects
946 atf_add_test_case f_variable_syntax
947 atf_add_test_case g_var_assign
948 atf_add_test_case i_pipelines
949 atf_add_test_case j_and_or_lists
950 atf_add_test_case k_lists
951 atf_add_test_case l_async_lists
952 atf_add_test_case m_compound_lists
953 atf_add_test_case q_for_loop
954 atf_add_test_case r_case
955 atf_add_test_case s_if
956 atf_add_test_case t_loops
957 atf_add_test_case u_case_cont
958 atf_add_test_case x_functions
959 }
960