1 # $NetBSD: t_option.sh,v 1.3 2016/03/08 14:19:28 christos Exp $
3 # Copyright (c) 2016 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
31 # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
41 # t is needed, as inside $()` $- appears to lose
42 # the 'e' option if it happened to already be
43 # set. Must check if that is what should
44 # happen, but that is a different issue.
46 test -z "${opt}" && continue
48 # if we are playing with more that one option at a
49 # time, the code below requires that we start with no
50 # options set, or it will mis-diagnose the situation
52 test "${#opt}" -gt 1 &&
53 CLEAR='xx="$-" && xx=$(echo "$xx" | tr -d cs) && test -n "$xx" && set +"$xx";'
55 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
58 echo "ERROR: Unable to $1 option $2" >&2
64 x=$(echo "$t" | tr -d "$1")
65 test "$t" = "$x" && x set "$1"
71 x=$(echo "$t" | tr -d "$1")
72 test "$t" != "$x" && x clear "$1"
77 # if we do not do this, -x tracing splatters stderr
78 # for some shells, -v does as well (is that correct?)
80 (*[xv]*) exec 2>/dev/null;;
84 x=$(echo "$o" | tr -d "$opt")
86 if [ "$o" = "$x" ]; then # option was off
97 test_optional_on_off()
103 test "${opt}" = n && continue
104 ${TEST_SH} -c "set -${opt}" 2>/dev/null &&
105 OPTS="${OPTS} ${opt}" || RET=1
108 test -n "${OPTS}" && test_option_on_off ${OPTS}
115 atf_set "descr" "Tests that 'set -a' turns on all var export " \
116 "and that it behaves as defined by the standard"
120 atf_require_prog grep
124 # without -a, new variables should not be exported (so grep "fails")
125 atf_check -s exit:1 -o empty -e empty ${TEST_SH} -ce \
126 'unset VAR; set +a; VAR=value; env | grep "^VAR="'
128 # with -a, they should be
129 atf_check -s exit:0 -o match:VAR=value -e empty ${TEST_SH} -ce \
130 'unset VAR; set -a; VAR=value; env | grep "^VAR="'
135 atf_set "descr" "Tests that 'set -C' turns on no clobber mode " \
136 "and that it behaves as defined by the standard"
143 # Check that the environment to use for the tests is sane ...
144 # we assume current dir is a new tempory directory & is empty
146 test -z "$(ls)" || atf_skip "Test execution directory not clean"
147 test -c "/dev/null" || atf_skip "Problem with /dev/null"
149 echo Dummy_Content > Junk_File
150 echo Precious_Content > Important_File
152 # Check that we can redirect onto file when -C is not set
153 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
155 D=$(ls -l Junk_File) || exit 1
157 echo "Overwrite it now" > Junk_File
158 A=$(ls -l Junk_File) || exit 1
159 test "${A}" != "${D}"
162 # Check that we cannot redirect onto file when -C is set
163 atf_check -s exit:0 -o empty -e not-empty ${TEST_SH} -c \
165 D=$(ls -l Important_File) || exit 1
167 echo "Fail to Overwrite it now" > Important_File
168 A=$(ls -l Important_File) || exit 1
172 # Check that we can append to file, even when -C is set
173 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
175 D=$(ls -l Junk_File) || exit 1
177 echo "Append to it now" >> Junk_File
178 A=$(ls -l Junk_File) || exit 1
179 test "${A}" != "${D}"
182 # Check that we abort on attempt to redirect onto file when -Ce is set
183 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
186 echo "Fail to Overwrite it now" > Important_File
187 echo "Should not reach this point"
190 # Last check that we can override -C for when we really need to
191 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
193 D=$(ls -l Junk_File) || exit 1
195 echo "Change the poor bugger again" >| Junk_File
196 A=$(ls -l Junk_File) || exit 1
197 test "${A}" != "${D}"
203 atf_set "descr" "Tests that 'set -e' turns on error detection " \
204 "and that a simple case behaves as defined by the standard"
209 # Check that -e does nothing if no commands fail
210 atf_check -s exit:0 -o match:I_am_OK -e empty \
212 'false; printf "%s" I_am; set -e; true; printf "%s\n" _OK'
214 # and that it (silently, but with exit status) aborts if cmd fails
215 atf_check -s not-exit:0 -o match:I_am -o not-match:Broken -e empty \
217 'false; printf "%s" I_am; set -e; false; printf "%s\n" _Broken'
219 # same, except -e this time is on from the beginning
220 atf_check -s not-exit:0 -o match:I_am -o not-match:Broken -e empty \
221 ${TEST_SH} -ec 'printf "%s" I_am; false; printf "%s\n" _Broken'
223 # More checking of -e in other places, there is lots to deal with.
228 atf_set "descr" "Tests that 'set -f' turns off pathname expansion " \
229 "and that it behaves as defined by the standard"
236 # Check that the environment to use for the tests is sane ...
237 # we assume current dir is a new tempory directory & is empty
239 test -z "$(ls)" || atf_skip "Test execution directory not clean"
241 # we will assume that atf will clean up this junk directory
242 # when we are done. But for testing pathname expansion
245 for f in a b c d e f aa ab ac ad ae aaa aab aac aad aba abc bbb ccc
250 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -ec \
251 'X=$(echo b*); Y=$(echo b*); test "${X}" != "a*";
252 test "${X}" = "${Y}"'
254 # now test expansion is different when -f is set
255 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -ec \
256 'X=$(echo b*); Y=$(set -f; echo b*); test "${X}" != "${Y}"'
261 atf_set "descr" "Tests that 'set -n' supresses command execution " \
262 "and that it behaves as defined by the standard"
265 # pointless to test this, if it turns on, it stays on...
266 # test_option_on_off n
267 # so just allow the tests below to verify it can be turned on
269 # nothing should be executed, hence no output...
270 atf_check -s exit:0 -o empty -e empty \
271 ${TEST_SH} -enc 'echo ABANDON HOPE; echo ALL YE; echo ...'
273 # this is true even when the "commands" do not exist
274 atf_check -s exit:0 -o empty -e empty \
275 ${TEST_SH} -enc 'ERR; FAIL; ABANDON HOPE'
277 # but if there is a syntax error, it should be detected (w or w/o -e)
278 atf_check -s not-exit:0 -o empty -e not-empty \
279 ${TEST_SH} -enc 'echo JUMP; for frogs swim; echo in puddles'
280 atf_check -s not-exit:0 -o empty -e not-empty \
281 ${TEST_SH} -nc 'echo ABANDON HOPE; echo "ALL YE; echo ...'
282 atf_check -s not-exit:0 -o empty -e not-empty \
283 ${TEST_SH} -enc 'echo ABANDON HOPE;; echo ALL YE; echo ...'
284 atf_check -s not-exit:0 -o empty -e not-empty \
285 ${TEST_SH} -nc 'do YOU ABANDON HOPE; for all eternity?'
287 # now test enabling -n in the middle of a script
288 # note that once turned on, it cannot be turned off again.
290 # omit more complex cases, as those can send some shells
291 # into infinite loops, and believe it or not, that might be OK!
293 atf_check -s exit:0 -o match:first -o not-match:second -e empty \
294 ${TEST_SH} -c 'echo first; set -n; echo second'
295 atf_check -s exit:0 -o match:first -o not-match:third -e empty \
296 ${TEST_SH} -c 'echo first; set -n; echo second; set +n; echo third'
297 atf_check -s exit:0 -o inline:'a\nb\n' -e empty \
298 ${TEST_SH} -c 'for x in a b c d
301 a);; b);; c) set -n;; d);;
306 # This last one is a bit more complex to explain, so I will not try
308 # First, we need to know what signal number is used for SIGUSR1 on
309 # the local (testing) system (signal number is $(( $XIT - 128 )) )
311 # this will take slightly over 1 second elapsed time (the sleep 1)
312 # The "10" for the first sleep just needs to be something big enough
313 # that the rest of the commands have time to complete, even on
314 # very slow testing systems. 10 should be enough. Otherwise irrelevant
316 # The shell will usually blather to stderr about the sleep 10 being
317 # killed, but it affects nothing, so just allow it to cry.
319 (sleep 10 & sleep 1; kill -USR1 $!; wait $!)
322 # The exit value should be an integer > 128 and < 256 (often 158)
323 # If it is not just skip the test
325 # If we do run the test, it should take (slightly over) either 1 or 2
326 # seconds to complete, depending upon the shell being tested.
329 ( 129 | 1[3-9][0-9] | 2[0-4][0-9] | 25[0-5] )
331 # The script below should exit with the same code - no output
333 # Or that is the result that seems best explanable.
334 # "set -n" in uses like this is not exactly well defined...
336 # This script comes from a member of the austin group
337 # (they author changes to the posix shell spec - and more.)
338 # The author is also an (occasional?) NetBSD user.
339 atf_check -s exit:${XIT} -o empty -e empty ${TEST_SH} -c '
341 { sleep 1; kill -USR1 $$; sleep 1; } &
343 wait && echo t || echo f
353 atf_set "descr" "Tests that 'set -u' turns on unset var detection " \
354 "and that it behaves as defined by the standard"
359 # first make sure it is OK to unset an unset variable
360 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -ce \
361 'unset _UNSET_VARIABLE_; echo OK'
363 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -cue \
364 'unset _UNSET_VARIABLE_; echo OK'
366 # and that without -u accessing an unset variable is harmless
367 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -ce \
368 'unset X; echo ${X}; echo OK'
369 # and that the unset variable test expansion works properly
370 atf_check -s exit:0 -o match:OKOK -e empty ${TEST_SH} -ce \
371 'unset X; printf "%s" ${X-OK}; echo OK'
373 # Next test that with -u set, the shell aborts on access to unset var
374 # do not use -e, want to make sure it is -u that causes abort
375 atf_check -s not-exit:0 -o not-match:ERR -e not-empty ${TEST_SH} -c \
376 'unset X; set -u; echo ${X}; echo ERR'
377 # quoting should make no difference...
378 atf_check -s not-exit:0 -o not-match:ERR -e not-empty ${TEST_SH} -c \
379 'unset X; set -u; echo "${X}"; echo ERR'
381 # Now a bunch of accesses to unset vars, with -u, in ways that are OK
382 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -ce \
383 'unset X; set -u; echo ${X-GOOD}; echo OK'
384 atf_check -s exit:0 -o match:OK -e empty ${TEST_SH} -ce \
385 'unset X; set -u; echo ${X-OK}'
386 atf_check -s exit:0 -o not-match:ERR -o match:OK -e empty \
387 ${TEST_SH} -ce 'unset X; set -u; echo ${X+ERR}; echo OK'
389 # and some more ways that are not OK
390 atf_check -s not-exit:0 -o not-match:ERR -e not-empty ${TEST_SH} -c \
391 'unset X; set -u; echo ${X#foo}; echo ERR'
392 atf_check -s not-exit:0 -o not-match:ERR -e not-empty ${TEST_SH} -c \
393 'unset X; set -u; echo ${X%%bar}; echo ERR'
395 # lastly, just while we are checking unset vars, test aborts w/o -u
396 atf_check -s not-exit:0 -o not-match:ERR -e not-empty ${TEST_SH} -c \
397 'unset X; echo ${X?}; echo ERR'
398 atf_check -s not-exit:0 -o not-match:ERR -e match:X_NOT_SET \
399 ${TEST_SH} -c 'unset X; echo ${X?X_NOT_SET}; echo ERR'
404 atf_set "descr" "Tests that 'set -v' turns on input read echoing " \
405 "and that it behaves as defined by the standard"
410 # check that -v does nothing if no later input line is read
411 atf_check -s exit:0 \
412 -o match:OKOK -o not-match:echo -o not-match:printf \
414 ${TEST_SH} -ec 'printf "%s" OK; set -v; echo OK; exit 0'
416 # but that it does when there are multiple lines
423 atf_check -s exit:0 \
424 -o match:OKOK -o not-match:echo -o not-match:printf \
425 -e match:printf -e match:OK -e match:echo \
426 -e not-match:set ${TEST_SH}
428 # and that it can be disabled again
436 atf_check -s exit:0 \
437 -o match:OKOK -o not-match:echo -o not-match:printf \
438 -e match:printf -e match:OK -e not-match:echo \
441 # and lastly, that shell keywords do get output when "read"
450 atf_check -s exit:0 \
451 -o match:111222333 -o not-match:printf \
452 -o not-match:for -o not-match:do -o not-match:done \
453 -e match:printf -e match:111 -e not-match:111222 \
454 -e match:for -e match:do -e match:done \
460 atf_set "descr" "Tests that 'set -x' turns on command exec logging " \
461 "and that it behaves as defined by the standard"
466 # check that cmd output appears after -x is enabled
467 atf_check -s exit:0 \
468 -o match:OKOK -o not-match:echo -o not-match:printf \
469 -e not-match:printf -e match:OK -e match:echo \
470 ${TEST_SH} -ec 'printf "%s" OK; set -x; echo OK; exit 0'
472 # and that it stops again afer -x is disabled
473 atf_check -s exit:0 \
474 -o match:OKOK -o not-match:echo -o not-match:printf \
475 -e match:printf -e match:OK -e not-match:echo \
476 ${TEST_SH} -ec 'set -x; printf "%s" OK; set +x; echo OK; exit 0'
478 # also check that PS4 is output correctly
479 atf_check -s exit:0 \
480 -o match:OK -o not-match:echo \
481 -e match:OK -e match:Run:echo \
482 ${TEST_SH} -ec 'PS4=Run:; set -x; echo OK; exit 0'
486 # This one seems controversial... I suspect it is NetBSD's sh
487 # that is wrong to not output "for" "while" "if" ... etc
489 # and lastly, that shell keywords do not get output when "executed"
490 atf_check -s exit:0 \
491 -o match:111222333 -o not-match:printf \
493 -e match:printf -e match:111 -e not-match:111222 \
494 -e not-match:for -e not-match:do -e not-match:done \
496 'set -x; for i in 111 222 333; do printf "%s" $i; done; echo; exit 0'
501 test -n "$1" || { echo >&2 "Internal error"; exit 1; }
503 cat > "$1" << 'END_OF_FUNCTIONS'
511 expr "$2" : "\(.*$1\)" >/dev/null
521 instr e "$-" && instr u "$-" && return 0
530 instr e "$-" && instr u "$-" && return 0
539 instr '[eu]' "$-" || printf %s "OK"
542 instr e "$-" && instr u "$-" && printf %s "OK"
549 atf_test_case restore_local_opts
550 restore_local_opts_head() {
551 atf_set "descr" "Tests that 'local -' saves and restores options. " \
552 "Note that "local" is a local shell addition"
554 restore_local_opts_body() {
556 atf_require_prog expr
559 opt_test_setup "${FN}" || atf_skip "Cannot setup test environment"
561 ${TEST_SH} -ec ". './${FN}'; local_opt_check" 2>/dev/null ||
562 atf_skip "sh extension 'local -' not supported by ${TEST_SH}"
564 atf_check -s exit:0 -o match:OKOK -o not-match:ERR -e empty \
565 ${TEST_SH} -ec ". './${FN}'; local_test"
568 atf_test_case vi_emacs_VE_toggle
569 vi_emacs_VE_toggle_head() {
570 atf_set "descr" "Tests enabling vi disables emacs (and v.v - but why?)"\
571 " Note that -V and -E are local shell additions"
573 vi_emacs_VE_toggle_body() {
575 test_optional_on_off V E ||
576 atf_skip "One or both V & E opts unsupported by ${TEST_SH}"
578 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '
587 echo >&2 "Option set or toggle failure:" \
588 " on=$1 off=$2 set=$-"
591 set -V; q V E || x V E
592 set -E; q E V || x E V
593 set -V; q V E || x V E
594 set +EV; q "" "[VE]" || x "" VE
599 atf_test_case xx_bogus
601 atf_set "descr" "Tests that attempting to set a nonsense option fails."
604 # Biggest problem here is picking a "nonsense option" that is
605 # not implemented by any shell, anywhere. Hopefully this will do.
607 # 'set' is a special builtin, so a conforming shell should exit
608 # on an arg error, and the ERR should not be printed.
609 atf_check -s not-exit:0 -o empty -e not-empty \
610 ${TEST_SH} -c 'set -% ; echo ERR'
613 atf_test_case Option_switching
614 Option_switching_head() {
615 atf_set "descr" "options can be enabled and disabled"
617 Option_switching_body() {
619 # Cannot test -m, setting it causes test shell to fail...
620 # (test shell gets SIGKILL!) Wonder why ... something related to atf
621 # That is, it works if just run as "sh -c 'echo $-; set -m; echo $-'"
623 # Don't bother testing toggling -n, once on, it stays on...
624 # (and because the test fn refuses to allow us to try)
626 # Cannot test -o or -c here, or the extension -s
627 # they can only be used, not switched
629 # these are the posix options, that all shells should implement
630 test_option_on_off a b C e f h u v x # m
632 # and these are extensions that might not exist (non-fatal to test)
633 # -i and -s (and -c) are posix options, but are not required to
634 # be accessable via the "set" command, just the command line.
635 # We allow for -i to work with set, as that makes some sense,
637 test_optional_on_off E i I p q V || true
639 # Also test (some) option combinations ...
640 # only testing posix options here, because it is easier...
641 test_option_on_off aeu vx Ca aCefux
644 atf_init_test_cases() {
645 # tests are run in order sort of names produces, so choose names wisely
647 # this one tests turning on/off all the mandatory. and extra flags
648 atf_add_test_case Option_switching
649 # and this tests the NetBSD "local -" functionality in functions.
650 atf_add_test_case restore_local_opts
652 # no tests for -m (no idea how to do that one)
653 # -I (no easy way to generate the EOF it ignores)
654 # -i (not sure how to test that one at the minute)
655 # -p (because we aren't going to run tests setuid)
656 # -V/-E (too much effort, and a real test would be huge)
657 # -c (because almost all the other tests test it anyway)
658 # -q (because, for now, I am lazy)
659 # -s (coming soon, hopefully)
660 # -o (really +o: again, hopefully soon)
661 # -o longname (again, just laziness, don't wait...)
662 # -h/-b (because NetBSD doesn't implement them)
663 atf_add_test_case set_a
664 atf_add_test_case set_C
665 atf_add_test_case set_e
666 atf_add_test_case set_f
667 atf_add_test_case set_n
668 atf_add_test_case set_u
669 atf_add_test_case set_v
670 atf_add_test_case set_x
672 atf_add_test_case vi_emacs_VE_toggle
673 atf_add_test_case xx_bogus