xref: /openbsd/regress/bin/ksh/trap.t (revision 047a6e63)
1*047a6e63Skn#	$OpenBSD: trap.t,v 1.6 2022/10/16 10:44:06 kn Exp $
2f8c806b8Skn
3f8c806b8Skn#
4f8c806b8Skn# Check that I/O redirection failure triggers the ERR trap.
5f8c806b8Skn# stderr patterns are minimal to match all of bash, ksh and ksh93.
6f8c806b8Skn# Try writing the root directory to guarantee EISDIR.
7f8c806b8Skn#
8f8c806b8Skn
9f8c806b8Sknname: failed-redirect-triggers-ERR-restricted
10f8c806b8Skndescription:
11f8c806b8Skn	Check that restricted mode prevents valid redirections that may write.
12f8c806b8Sknarguments: !-r!
13f8c806b8Sknstdin:
14f8c806b8Skn	trap 'echo ERR' ERR
15f8c806b8Skn	true >/dev/null
16f8c806b8Sknexpected-stdout:
17f8c806b8Skn	ERR
18f8c806b8Sknexpected-stderr-pattern:
19f8c806b8Skn	/restricted/
20f8c806b8Sknexpected-exit: e != 0
21f8c806b8Skn---
22f8c806b8Skn
23f8c806b8Skn
24f8c806b8Sknname: failed-redirect-triggers-ERR-command
25f8c806b8Skndescription:
26f8c806b8Skn	Redirect standard output for a single command.
27f8c806b8Sknstdin:
28f8c806b8Skn	trap 'echo ERR' ERR
29f8c806b8Skn	true >/
30f8c806b8Sknexpected-stdout:
31f8c806b8Skn	ERR
32f8c806b8Sknexpected-stderr-pattern:
33f8c806b8Skn	/Is a directory/
34f8c806b8Sknexpected-exit: e != 0
35f8c806b8Skn---
36f8c806b8Skn
37f8c806b8Skn
38f8c806b8Sknname: failed-redirect-triggers-ERR-permanent
39f8c806b8Skndescription:
40f8c806b8Skn	Permanently redirect standard output of the shell without execution.
41f8c806b8Sknstdin:
42f8c806b8Skn	trap 'echo ERR' ERR
43f8c806b8Skn	exec >/
44f8c806b8Sknexpected-stdout:
45f8c806b8Skn	ERR
46f8c806b8Sknexpected-stderr-pattern:
47f8c806b8Skn	/Is a directory/
48f8c806b8Sknexpected-exit: e != 0
49f8c806b8Skn---
5011cd34e8Skn
5111cd34e8Skn#
5295f9d6ebSkn# Check that the errexit option
5395f9d6ebSkn# a) does not interfere with running traps and
5495f9d6ebSkn# b) propagates a non-zero exit status from traps.
5595f9d6ebSkn# Check that traps are run in the same order in which they were triggered.
5611cd34e8Skn#
5711cd34e8Skn
58157b2ee2Sknname: failed-ERR-runs-EXIT
59ede0d159Skn# XXX remove once bin/ksh/main.c r1.52 is backed out *AND* a new fix is in
60ede0d159Skn# XXX enable once bin/ksh/main.c r1.52 is backed out
61ede0d159Skn#expected-fail: yes
6211cd34e8Skndescription:
6395f9d6ebSkn	Check that EXIT runs under errexit even if ERR failed.
6411cd34e8Sknarguments: !-e!
6511cd34e8Sknstdin:
6611cd34e8Skn	trap 'echo ERR ; false' ERR
6711cd34e8Skn	trap 'echo EXIT' EXIT
6811cd34e8Skn	false
6911cd34e8Sknexpected-stdout:
7011cd34e8Skn	ERR
7111cd34e8Skn	EXIT
7211cd34e8Sknexpected-exit: e != 0
7311cd34e8Skn---
7411cd34e8Skn
7511cd34e8Skn
7695f9d6ebSknname: errexit-aborts-EXIT
7795f9d6ebSkn# XXX remove once bin/ksh/main.c r1.52 is backed out
7895f9d6ebSknexpected-fail: yes
7995f9d6ebSkndescription:
8095f9d6ebSkn	Check that errexit makes EXIT exit early.
8195f9d6ebSknarguments: !-e!
8295f9d6ebSknstdin:
8395f9d6ebSkn	trap 'echo ERR' ERR
8495f9d6ebSkn	trap 'false ; echo EXIT' EXIT
8595f9d6ebSknexpected-stdout:
8695f9d6ebSkn	ERR
8795f9d6ebSknexpected-exit: e != 0
8895f9d6ebSkn---
8995f9d6ebSkn
9095f9d6ebSkn
9195f9d6ebSknname: EXIT-triggers-ERR
92ede0d159Skn# XXX remove once bin/ksh/main.c r1.52 is backed out
9395f9d6ebSknexpected-fail: yes
9495f9d6ebSkndescription:
9595f9d6ebSkn	Check that ERR runs under errexit if EXIT failed.
9695f9d6ebSknarguments: !-e!
9795f9d6ebSknstdin:
9895f9d6ebSkn	trap 'echo ERR' ERR
9995f9d6ebSkn	trap 'echo EXIT ; false' EXIT
10095f9d6ebSkn	true
10195f9d6ebSknexpected-stdout:
10295f9d6ebSkn	EXIT
10395f9d6ebSkn	ERR
10495f9d6ebSknexpected-exit: e != 0
10595f9d6ebSkn---
106157b2ee2Skn
107157b2ee2Skn#
108157b2ee2Skn# Check that the errexit option does not interfere with signal handler traps.
109157b2ee2Skn#
110157b2ee2Skn
111157b2ee2Sknname: handled-signal-is-no-error
112157b2ee2Skndescription:
113157b2ee2Skn	Check that gracefully handling a signal is not treated as error.
114157b2ee2Sknarguments: !-e!
115157b2ee2Sknstdin:
116157b2ee2Skn	trap 'echo ERR' ERR
117157b2ee2Skn	trap 'echo EXIT' EXIT
118157b2ee2Skn	trap 'echo USR1' USR1
119157b2ee2Skn	kill -USR1 $$
120157b2ee2Sknexpected-stdout:
121157b2ee2Skn	USR1
122157b2ee2Skn	EXIT
123157b2ee2Sknexpected-exit: e == 0
124157b2ee2Skn---
125*047a6e63Skn
126*047a6e63Skn
127*047a6e63Sknname: failed-INTR-runs-EXIT
128*047a6e63Skndescription:
129*047a6e63Skn	Check that EXIT runs under errexit even if interrupt handling failed.
130*047a6e63Skn	SIGINT, SIGQUIT, SIGTERM and SIGHUP are handled specially.
131*047a6e63Skn	XXX Find/explain the difference if the busy loop runs directly, i.e. not
132*047a6e63Skn	inside a subshell or process ($PROG -c "...").
133*047a6e63Skn# XXX should always be passed like PROG
134*047a6e63Sknarguments: !-e!
135*047a6e63Sknenv-setup: !ARGS=-e!
136*047a6e63Sknstdin:
137*047a6e63Skn	exec timeout --preserve-status -s INT -- 0.1s $PROG $ARGS -c '
138*047a6e63Skn		trap "echo EXIT" EXIT
139*047a6e63Skn		trap "echo INT ; false" INT
140*047a6e63Skn		(while : ; do : ; done)
141*047a6e63Skn	'
142*047a6e63Sknexpected-stdout:
143*047a6e63Skn	INT
144*047a6e63Skn	EXIT
145*047a6e63Sknexpected-exit: e != 0
146*047a6e63Skn---
147