xref: /openbsd-src/regress/bin/ksh/trap.t (revision 047a6e63f068a346477105816641ef1669873bc9)
1#	$OpenBSD: trap.t,v 1.6 2022/10/16 10:44:06 kn Exp $
2
3#
4# Check that I/O redirection failure triggers the ERR trap.
5# stderr patterns are minimal to match all of bash, ksh and ksh93.
6# Try writing the root directory to guarantee EISDIR.
7#
8
9name: failed-redirect-triggers-ERR-restricted
10description:
11	Check that restricted mode prevents valid redirections that may write.
12arguments: !-r!
13stdin:
14	trap 'echo ERR' ERR
15	true >/dev/null
16expected-stdout:
17	ERR
18expected-stderr-pattern:
19	/restricted/
20expected-exit: e != 0
21---
22
23
24name: failed-redirect-triggers-ERR-command
25description:
26	Redirect standard output for a single command.
27stdin:
28	trap 'echo ERR' ERR
29	true >/
30expected-stdout:
31	ERR
32expected-stderr-pattern:
33	/Is a directory/
34expected-exit: e != 0
35---
36
37
38name: failed-redirect-triggers-ERR-permanent
39description:
40	Permanently redirect standard output of the shell without execution.
41stdin:
42	trap 'echo ERR' ERR
43	exec >/
44expected-stdout:
45	ERR
46expected-stderr-pattern:
47	/Is a directory/
48expected-exit: e != 0
49---
50
51#
52# Check that the errexit option
53# a) does not interfere with running traps and
54# b) propagates a non-zero exit status from traps.
55# Check that traps are run in the same order in which they were triggered.
56#
57
58name: failed-ERR-runs-EXIT
59# XXX remove once bin/ksh/main.c r1.52 is backed out *AND* a new fix is in
60# XXX enable once bin/ksh/main.c r1.52 is backed out
61#expected-fail: yes
62description:
63	Check that EXIT runs under errexit even if ERR failed.
64arguments: !-e!
65stdin:
66	trap 'echo ERR ; false' ERR
67	trap 'echo EXIT' EXIT
68	false
69expected-stdout:
70	ERR
71	EXIT
72expected-exit: e != 0
73---
74
75
76name: errexit-aborts-EXIT
77# XXX remove once bin/ksh/main.c r1.52 is backed out
78expected-fail: yes
79description:
80	Check that errexit makes EXIT exit early.
81arguments: !-e!
82stdin:
83	trap 'echo ERR' ERR
84	trap 'false ; echo EXIT' EXIT
85expected-stdout:
86	ERR
87expected-exit: e != 0
88---
89
90
91name: EXIT-triggers-ERR
92# XXX remove once bin/ksh/main.c r1.52 is backed out
93expected-fail: yes
94description:
95	Check that ERR runs under errexit if EXIT failed.
96arguments: !-e!
97stdin:
98	trap 'echo ERR' ERR
99	trap 'echo EXIT ; false' EXIT
100	true
101expected-stdout:
102	EXIT
103	ERR
104expected-exit: e != 0
105---
106
107#
108# Check that the errexit option does not interfere with signal handler traps.
109#
110
111name: handled-signal-is-no-error
112description:
113	Check that gracefully handling a signal is not treated as error.
114arguments: !-e!
115stdin:
116	trap 'echo ERR' ERR
117	trap 'echo EXIT' EXIT
118	trap 'echo USR1' USR1
119	kill -USR1 $$
120expected-stdout:
121	USR1
122	EXIT
123expected-exit: e == 0
124---
125
126
127name: failed-INTR-runs-EXIT
128description:
129	Check that EXIT runs under errexit even if interrupt handling failed.
130	SIGINT, SIGQUIT, SIGTERM and SIGHUP are handled specially.
131	XXX Find/explain the difference if the busy loop runs directly, i.e. not
132	inside a subshell or process ($PROG -c "...").
133# XXX should always be passed like PROG
134arguments: !-e!
135env-setup: !ARGS=-e!
136stdin:
137	exec timeout --preserve-status -s INT -- 0.1s $PROG $ARGS -c '
138		trap "echo EXIT" EXIT
139		trap "echo INT ; false" INT
140		(while : ; do : ; done)
141	'
142expected-stdout:
143	INT
144	EXIT
145expected-exit: e != 0
146---
147