xref: /openbsd/regress/bin/ksh/obsd-regress.t (revision 5c6f3d6f)
1#	$OpenBSD: obsd-regress.t,v 1.13 2021/03/05 15:24:37 zhuk Exp $
2
3#
4# ksh regression tests from OpenBSD
5#
6
7name: eval-1
8description:
9	Tests for ${name#pat} vs. "${name#pat}" expansion
10stdin:
11	a=
12	for n in ${a#*=}; do echo ${n}; done
13	for n in "${a#*=}"; do echo ${n}; done
14expected-stdout:
15
16---
17
18name: eval-2
19description:
20	Tests for ${name##pat} vs. "${name##pat}" expansion
21stdin:
22	a=
23	for n in ${a##*=}; do echo ${n}; done
24	for n in "${a##*=}"; do echo ${n}; done
25expected-stdout:
26
27---
28
29name: eval-3
30description:
31	Tests for ${name%=pat} vs. "${name%=pat}" expansion
32stdin:
33	a=
34	for n in ${a%=*}; do echo ${n}; done
35	for n in "${a%=*}"; do echo ${n}; done
36expected-stdout:
37
38---
39
40name: eval-4
41description:
42	Tests for ${name%%=pat} vs. "${name%%=pat}" expansion
43stdin:
44	a=
45	for n in ${a%%=*}; do echo ${n}; done
46	for n in "${a%%=*}"; do echo ${n}; done
47expected-stdout:
48
49---
50
51name: eval-5
52description:
53	Tests for expansion including multiple read-only variables
54stdin:
55	set -- script .sh
56	echo ${1%$2}
57	set -- foobar barbaz baz
58	echo ${1%${2%$3}}
59	set -- aa bb cc
60	echo ${*:+$*}
61expected-stdout:
62	script
63	foo
64	aa bb cc
65---
66
67name: and-list-error-1
68description:
69	Test exit status of rightmost element in 2 element && list in -e mode
70stdin:
71	true && false
72	echo "should not print"
73arguments: !-e!
74expected-exit: e != 0
75---
76
77name: and-list-error-2
78description:
79	Test exit status of rightmost element in 3 element && list in -e mode
80stdin:
81	true && true && false
82	echo "should not print"
83arguments: !-e!
84expected-exit: e != 0
85---
86
87name: or-list-error-1
88description:
89	Test exit status of || list in -e mode
90stdin:
91	false || false
92	echo "should not print"
93arguments: !-e!
94expected-exit: e != 0
95---
96
97name: or-list-error-2
98description:
99	Test exit status of || list in -e mode
100stdin:
101	eval false || true
102	echo "should print"
103expected-stdout:
104	should print
105arguments: !-e!
106---
107
108name: var-functions
109description:
110	Calling
111
112		FOO=bar f
113
114	where f is a ksh style function, should not set FOO in the current env.
115	If f is a bourne style function, FOO should be set. Furthermore,
116	the function should receive a correct value of FOO. Additionally,
117	setting FOO in the function itself should not change the value in
118	global environment.
119
120	Inspired by PR 2450.
121stdin:
122	function k {
123		if [ x$FOO != xbar ]; then
124			echo 1
125			return 1
126		fi
127		x=$(env | grep FOO)
128		if [ "x$x" != "xFOO=bar" ]; then
129			echo 2
130			return 1;
131		fi
132		FOO=foo
133		return 0
134	}
135	b () {
136		if [ x$FOO != xbar ]; then
137			echo 3
138			return 1
139		fi
140		x=$(env | grep FOO)
141		if [ "x$x" != "xFOO=bar" ]; then
142			echo 4
143			return 1;
144		fi
145		FOO=foo
146		return 0
147	}
148	FOO=bar k
149	if [ $? != 0 ]; then
150		exit 1
151	fi
152	if [ x$FOO != x ]; then
153		exit 1
154	fi
155	FOO=bar b
156	if [ $? != 0 ]; then
157		exit 1
158	fi
159	if [ x$FOO != xbar ]; then
160		exit 1
161	fi
162	FOO=barbar
163	FOO=bar k
164	if [ $? != 0 ]; then
165		exit 1
166	fi
167	if [ x$FOO != xbarbar ]; then
168		exit 1
169	fi
170	FOO=bar b
171	if [ $? != 0 ]; then
172		exit 1
173	fi
174	if [ x$FOO != xbar ]; then
175		exit 1
176	fi
177---
178
179name: longline-crash
180description:
181	This used to cause a core dump
182stdin:
183	ulimit -c 0
184	deplibs="-lz -lpng /usr/local/lib/libjpeg.la -ltiff -lm -lX11 -lXext /usr/local/lib/libiconv.la -L/usr/local/lib -L/usr/ports/devel/gettext/w-gettext-0.10.40/gettext-0.10.40/intl/.libs /usr/local/lib/libintl.la /usr/local/lib/libglib.la /usr/local/lib/libgmodule.la -lintl -lm -lX11 -lXext -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgdk.la -lintl -lm -lX11 -lXext -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgtk.la -ltiff -ljpeg -lz -lpng -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgdk_pixbuf.la -lz -lpng /usr/local/lib/libiconv.la -L/usr/local/lib -L/usr/ports/devel/gettext/w-gettext-0.10.40/gettext-0.10.40/intl/.libs /usr/local/lib/libintl.la /usr/local/lib/libglib.la -lm -lm /usr/local/lib/libaudiofile.la -lm -lm -laudiofile -L/usr/local/lib /usr/local/lib/libesd.la -lm -lz -L/usr/local/lib /usr/local/lib/libgnomesupport.la -lm -lz -lm -lglib -L/usr/local/lib /usr/local/lib/libgnome.la -lX11 -lXext /usr/local/lib/libiconv.la -L/usr/local/lib -L/usr/ports/devel/gettext/w-gettext-0.10.40/gettext-0.10.40/intl/.libs /usr/local/lib/libintl.la /usr/local/lib/libgmodule.la -lintl -lm -lX11 -lXext -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgdk.la -lintl -lm -lX11 -lXext -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgtk.la -lICE -lSM -lz -lpng /usr/local/lib/libungif.la /usr/local/lib/libjpeg.la -ltiff -lm -lz -lpng /usr/local/lib/libungif.la -lz /usr/local/lib/libjpeg.la -ltiff -L/usr/local/lib -L/usr/X11R6/lib /usr/local/lib/libgdk_imlib.la -lm -L/usr/local/lib /usr/local/lib/libart_lgpl.la -lm -lz -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -lICE -lSM -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -L/usr/X11R6/lib -lm -lz -lpng -lungif -lz -ljpeg -ltiff -ljpeg -lgdk_imlib -lglib -lm -laudiofile -lm -laudiofile -lesd -L/usr/local/lib /usr/local/lib/libgnomeui.la -lz -lz /usr/local/lib/libxml.la -lz -lz -lz /usr/local/lib/libxml.la -lm -lX11 -lXext /usr/local/lib/libiconv.la -L/usr/ports/devel/gettext/w-gettext-0.10.40/gettext-0.10.40/intl/.libs /usr/local/lib/libintl.la /usr/local/lib/libglib.la /usr/local/lib/libgmodule.la -lintl -lglib -lgmodule /usr/local/lib/libgdk.la /usr/local/lib/libgtk.la -L/usr/X11R6/lib -L/usr/local/lib /usr/local/lib/libglade.la -lz -lz -lz /usr/local/lib/libxml.la /usr/local/lib/libglib.la -lm -lm /usr/local/lib/libaudiofile.la -lm -lm -laudiofile /usr/local/lib/libesd.la -lm -lz /usr/local/lib/libgnomesupport.la -lm -lz -lm -lglib /usr/local/lib/libgnome.la -lX11 -lXext /usr/local/lib/libiconv.la -L/usr/ports/devel/gettext/w-gettext-0.10.40/gettext-0.10.40/intl/.libs /usr/local/lib/libintl.la /usr/local/lib/libgmodule.la -lintl -lm -lX11 -lXext -lglib -lgmodule /usr/local/lib/libgdk.la -lintl -lm -lX11 -lXext -lglib -lgmodule /usr/local/lib/libgtk.la -lICE -lSM -lz -lpng /usr/local/lib/libungif.la /usr/local/lib/libjpeg.la -ltiff -lm -lz -lz /usr/local/lib/libgdk_imlib.la /usr/local/lib/libart_lgpl.la -lm -lz -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -lm -lz -lungif -lz -ljpeg -ljpeg -lgdk_imlib -lglib -lm -laudiofile -lm -laudiofile -lesd /usr/local/lib/libgnomeui.la -L/usr/X11R6/lib -L/usr/local/lib /usr/local/lib/libglade-gnome.la /usr/local/lib/libglib.la -lm -lm /usr/local/lib/libaudiofile.la -lm -lm -laudiofile -L/usr/local/lib /usr/local/lib/libesd.la -lm -lz -L/usr/local/lib /usr/local/lib/libgnomesupport.la -lm -lz -lm -lglib -L/usr/local/lib /usr/local/lib/libgnome.la -lX11 -lXext /usr/local/lib/libiconv.la -L/usr/local/lib -L/usr/ports/devel/gettext/w-gettext-0.10.40/gettext-0.10.40/intl/.libs /usr/local/lib/libintl.la /usr/local/lib/libgmodule.la -lintl -lm -lX11 -lXext -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgdk.la -lintl -lm -lX11 -lXext -L/usr/X11R6/lib -lglib -lgmodule -L/usr/local/lib /usr/local/lib/libgtk.la -lICE -lSM -lz -lpng /usr/local/lib/libungif.la /usr/local/lib/libjpeg.la -ltiff -lm -lz -lpng /usr/local/lib/libungif.la -lz /usr/local/lib/libjpeg.la -ltiff -L/usr/local/lib -L/usr/X11R6/lib /usr/local/lib/libgdk_imlib.la -lm -L/usr/local/lib /usr/local/lib/libart_lgpl.la -lm -lz -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -lICE -lSM -lm -lX11 -lXext -lintl -lglib -lgmodule -lgdk -lgtk -L/usr/X11R6/lib -lm -lz -lpng -lungif -lz -ljpeg -ltiff -ljpeg -lgdk_imlib -lglib -lm -laudiofile -lm -laudiofile -lesd -L/usr/local/lib /usr/local/lib/libgnomeui.la -L/usr/X11R6/lib -L/usr/local/lib"
185	specialdeplibs="-lgnomeui -lart_lgpl -lgdk_imlib -ltiff -ljpeg -lungif -lpng -lz -lSM -lICE -lgtk -lgdk -lgmodule -lintl -lXext -lX11 -lgnome -lgnomesupport -lesd -laudiofile -lm -lglib"
186	for deplib in $deplibs; do
187	    case $deplib in
188		-L*)
189		    new_libs="$deplib $new_libs"
190		    ;;
191		*)
192		    case " $specialdeplibs " in
193			*" $deplib "*)
194			    new_libs="$deplib $new_libs";;
195		    esac
196		    ;;
197	    esac
198	done
199---
200
201name: seterror-1
202description:
203	The -e flag should be ignored on unsuccessful commands before && inside an
204	if statement.
205stdin:
206	if true; then false && false; fi
207	true
208arguments: !-e!
209expected-exit: e == 0
210---
211
212name: seterror-2
213description:
214	The -e flag should be ignored on unsuccessful commands before && inside a
215	nested if statement.
216stdin:
217	if true; then if true; then false && false; fi; fi
218	true
219arguments: !-e!
220expected-exit: e == 0
221---
222
223name: seterror-3
224description:
225	The -e flag should be ignored when executing a compound list
226	followed by an elif statement.
227stdin:
228	if true; then :; elif true; then false && false; fi
229arguments: !-e!
230expected-exit: e == 0
231---
232
233name: seterror-4
234description:
235	Inside a for statement, the -e flag should be ignored on successful commands
236	before ||, or unsuccessful commands before &&.
237stdin:
238	for i in 1 2 3
239	do
240		false && false
241		true || false
242	done
243arguments: !-e!
244expected-exit: e == 0
245---
246
247name: seterror-5
248description:
249	The -e flag should be ignored when executing a pipeline
250	beginning with '!'
251stdin:
252	! true | false
253	true
254arguments: !-e!
255expected-exit: e == 0
256---
257
258name: seterror-6
259description:
260	When trapping ERR and EXIT, both traps should run in -e mode
261	when an error occurs.
262stdin:
263	trap 'echo EXIT' EXIT
264	trap 'echo ERR' ERR
265	set -e
266	false
267	echo DONE
268	exit 0
269arguments: !-e!
270expected-exit: e != 0
271expected-stdout:
272	ERR
273	EXIT
274---
275
276name: seterror-7
277description:
278	The -e flag within a command substitution should be honored
279stdin:
280	echo $( set -e; false; echo foo )
281arguments: !-e!
282expected-stdout:
283
284---
285
286name: seterror-8
287description:
288	The -e flag within an if statement should terminate && chains on
289	failure in rightmost command.
290stdin:
291	if true; then true && false; fi
292	true
293arguments: !-e!
294expected-exit: e != 0
295---
296
297name: seterror-9
298description:
299	The -e flag within a for statement should terminate && or || chains on
300	failure in rightmost command.
301stdin:
302	for f in 0; do true && false; done
303	true
304arguments: !-e!
305expected-exit: e != 0
306---
307
308name: seterror-10
309description:
310	The -e flag within a while statement should terminate && or || chains on
311	failure in rightmost command.
312stdin:
313	while true; do true && false; done
314	true
315arguments: !-e!
316expected-exit: e != 0
317---
318
319name: seterror-11
320description:
321	Putting it all together for -e mode, test an && chain behaving in different
322	ways in different iterations of a for loop.
323# file x is absent; xx is present
324file-setup: file 644 "xx"
325# file y is absent
326arguments: !-e!
327# first iteration of for loop errors at first branch of &&
328#   execution should continue in spite of -e mode
329# second iteration of for loop errors at second branch of &&
330#   -e mode should trigger exit
331stdin:
332	for f in x xx
333	do
334		test -f $f && test -f y  # final statement in loop
335	done
336	echo "should not print"
337expected-exit: e != 0
338---
339
340name: seterror-12
341description:
342	Putting it all together for -e mode, test an && chain behaving in different
343	ways in different iterations of a while loop.
344# file x is absent; xx is present
345file-setup: file 644 "x"
346# file y is absent
347arguments: !-e!
348# first iteration of for loop errors at first branch of &&
349#   execution should continue in spite of -e mode
350# second iteration of for loop errors at second branch of &&
351#   -e mode should trigger exit
352stdin:
353	x=''
354	y=''
355	while [ "$x" != xxx ]
356	do
357		x=x$x
358		y=y$y
359		test -f $x && test -f y  # final statement in loop
360	done
361	echo "should not print"
362expected-exit: e != 0
363---
364
365name: seterror-13
366description:
367	Putting it all together for -e mode, test an && chain behaving in different
368	ways in different iterations of a until loop.
369# file x is absent; xx is present
370file-setup: file 644 "x"
371# files y and yy are both absent
372arguments: !-e!
373# first iteration of for loop errors at first branch of &&
374#   execution should continue in spite of -e mode
375# second iteration of for loop errors at second branch of &&
376#   -e mode should trigger exit
377stdin:
378	x=''
379	y=''
380	until [ "$x" == xxx ]
381	do
382		x=x$x
383		y=y$y
384		test -f $x && test -f $y  # final statement in loop
385	done
386	echo "should not print"
387expected-exit: e != 0
388---
389
390name: input-comsub
391description:
392	A command substitution using input redirection should exit with
393	failure if the input file does not exist.
394stdin:
395	var=$(< non-existent)
396expected-exit: e != 0
397expected-stderr-pattern: /non-existent/
398
399---
400name: empty-for-list
401description:
402	A for list which expands to zero items should not execute the body.
403stdin:
404	set foo bar baz ; for out in ; do echo $out ; done
405
406---
407
408name: command-pvV-posix-priorities
409description:
410	For POSIX compatibility, command -v should find aliases and reserved
411	words, and command -p[vV] should find aliases, reserved words, and
412	builtins over external commands.
413stdin:
414	PATH=$(command -p getconf PATH) || PATH=/bin:/usr/bin
415	alias foo="bar baz"
416	bar() { :; }
417	for word in 'if' 'foo' 'bar' 'set' 'true' 'ls'; do
418		command -v "$word"
419		command -pv "$word"
420		command -V "$word"
421		command -pV "$word"
422	done
423expected-stdout-pattern:
424	/^if
425	if
426	if is a reserved word
427	if is a reserved word
428	alias foo='bar baz'
429	alias foo='bar baz'
430	foo is an alias for 'bar baz'
431	foo is an alias for 'bar baz'
432	bar
433	bar
434	bar is a function
435	bar is a function
436	set
437	set
438	set is a special shell builtin
439	set is a special shell builtin
440	true
441	true
442	true is a shell builtin
443	true is a shell builtin
444	.*\/ls.*
445	.*\/ls.*
446	ls is a tracked alias for .*\/ls.*
447	ls is .*\/ls.*$/
448---
449
450name: whence-preserve-tradition
451description:
452	POSIX 'command' and ksh88/pdksh-specific 'whence' are handled by the
453	same c_whence() function.  This regression test is to ensure that
454	the POSIX compatibility changes for 'command' (see previous test) do
455	not affect traditional 'whence' behaviour.
456stdin:
457	PATH=$(command -p getconf PATH) || PATH=/bin:/usr/bin
458	alias foo="bar baz"
459	bar() { :; }
460	for word in 'if' 'foo' 'bar' 'set' 'true' 'ls'; do
461		whence "$word"
462		whence -p "$word"
463		whence -v "$word"
464		whence -pv "$word"
465	done
466expected-stdout-pattern:
467	/^if
468	if is a reserved word
469	if not found
470	'bar baz'
471	foo is an alias for 'bar baz'
472	foo not found
473	bar
474	bar is a function
475	bar not found
476	set
477	set is a special shell builtin
478	set not found
479	true
480	.*\/true.*
481	true is a shell builtin
482	true is a tracked alias for .*\/true.*
483	.*\/ls.*
484	.*\/ls.*
485	ls is a tracked alias for .*\/ls.*
486	ls is a tracked alias for .*\/ls.*$/
487---
488
489name: shellopt-u-1
490description:
491	Check that "$@" and "$*" are exempt from 'set -u' (nounset)
492stdin:
493	set -u
494	: "$@$*$1"
495expected-exit: e == 1
496expected-stderr-pattern:
497	/: 1: parameter not set$/
498---
499
500name: pwd
501description:
502	PWD and OLDPWD must be exported
503stdin:
504	d=$(printenv PWD)
505	: ${d:?"PWD not exported"}
506	cd .
507	d=$(printenv OLDPWD)
508	: ${d:?"OLDPWD not exported"}
509---
510
511name: kill-SIGNAME
512description:
513	support kill -s SIGNAME syntax
514stdin:
515	kill -s SIGINFO $$
516---
517
518name: pipeline-pipefail-1
519description:
520	check pipeline return status
521stdin:
522	set -o pipefail
523	true | true
524---
525
526name: pipeline-pipefail-2
527description:
528	check pipeline return status
529stdin:
530	set -o pipefail
531	false | true
532expected-exit: e == 1
533---
534
535name: pipeline-pipefail-3
536description:
537	check pipeline return status
538stdin:
539	set -o pipefail
540	true | false
541expected-exit: e == 1
542---
543
544name: pipeline-pipefail-4
545description:
546	check pipeline return status
547stdin:
548	set -o pipefail
549	! false | true
550---
551
552name: pipeline-pipefail-errexit-1
553description:
554	check pipeline return status
555stdin:
556	set -e
557	false | true
558	echo "ok"
559expected-stdout: ok
560---
561
562name: pipeline-pipefail-errexit-2
563description:
564	check pipeline return status
565stdin:
566	set -e
567	set -o pipefail
568	false | true
569	echo "should not print"
570expected-exit: e == 1
571expected-stdout:
572---
573
574name: pipeline-pipefail-errexit-3
575description:
576	check pipeline return status
577stdin:
578	set -e
579	set -o pipefail
580	false | true || echo "ok"
581expected-stdout: ok
582---
583
584name: pipeline-pipefail-check-time-1
585description:
586	check pipeline return status
587stdin:
588	false | true &
589	p=$!
590	set -o pipefail
591	wait $p
592---
593
594name: pipeline-pipefail-check-time-2
595description:
596	check pipeline return status
597stdin:
598	set -o pipefail
599	false | true &
600	p=$!
601	set +o pipefail
602	wait $p
603expected-exit: e == 1
604---
605
606name: overwrite-ro-array
607description:
608	do not allow to override first element of a read-only array
609	via the non-array access.
610stdin:
611	arr[0]=foo
612	readonly arr
613	arr=bar
614expected-exit: e == 1
615expected-stderr-pattern:
616	/: arr: is read only$/
617---
618