summaryrefslogtreecommitdiff
path: root/lib/util/regress/strsig/strsig_test.c
blob: 30aaa2d5175f4b08e9dcd26cbf29275b4711c4cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2019-2020 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#include "sudo_compat.h"
#include "sudo_util.h"
#include "sudo_fatal.h"

sudo_dso_public int main(int argc, char *argv[]);

/*
 * Note: we do not test SIGUNUSED as it may not appear in sys_sigabbrev[]
 *       on Linux.  FreeBSD is missing SIGLWP (aka SIGTHR) in sys_signame[].
 */
static struct signal_data {
    int rval;
    int signo;
    const char *sigstr;
    const char *altstr;
} signal_data[] = {
#ifdef SIGHUP
    { 0, SIGHUP, "HUP", NULL },
#endif
#ifdef SIGINT
    { 0, SIGINT, "INT", NULL },
#endif
#ifdef SIGQUIT
    { 0, SIGQUIT, "QUIT", NULL },
#endif
#ifdef SIGILL
    { 0, SIGILL, "ILL", NULL },
#endif
#ifdef SIGTRAP
    { 0, SIGTRAP, "TRAP", NULL },
#endif
#ifdef SIGABRT
    { 0, SIGABRT, "ABRT", "IOT" },
#endif
#ifdef SIGIOT
    { 0, SIGIOT, "IOT", "ABRT" },
#endif
#ifdef SIGEMT
    { 0, SIGEMT, "EMT", NULL },
#endif
#ifdef SIGFPE
    { 0, SIGFPE, "FPE", NULL },
#endif
#ifdef SIGKILL
    { 0, SIGKILL, "KILL", NULL },
#endif
#ifdef SIGBUS
    { 0, SIGBUS, "BUS", NULL },
#endif
#ifdef SIGSEGV
    { 0, SIGSEGV, "SEGV", NULL },
#endif
#ifdef SIGSYS
    { 0, SIGSYS, "SYS", NULL },
#endif
#ifdef SIGPIPE
    { 0, SIGPIPE, "PIPE", NULL },
#endif
#ifdef SIGALRM
    { 0, SIGALRM, "ALRM", NULL },
#endif
#ifdef SIGTERM
    { 0, SIGTERM, "TERM", NULL },
#endif
#ifdef SIGSTKFLT
    { 0, SIGSTKFLT, "STKFLT", NULL },
#endif
#ifdef SIGIO
    { 0, SIGIO, "IO", "POLL"},
#endif
#ifdef SIGXCPU
    { 0, SIGXCPU, "XCPU", NULL },
#endif
#ifdef SIGXFSZ
    { 0, SIGXFSZ, "XFSZ", NULL },
#endif
#ifdef SIGVTALRM
    { 0, SIGVTALRM, "VTALRM", NULL },
#endif
#ifdef SIGPROF
    { 0, SIGPROF, "PROF", NULL },
#endif
#ifdef SIGWINCH
    { 0, SIGWINCH, "WINCH", NULL },
#endif
#ifdef SIGLOST
    { 0, SIGLOST, "LOST", NULL },
#endif
#ifdef SIGUSR1
    { 0, SIGUSR1, "USR1", NULL },
#endif
#ifdef SIGUSR2
    { 0, SIGUSR2, "USR2", NULL },
#endif
#ifdef SIGPWR
    { 0, SIGPWR, "PWR", NULL },
#endif
#ifdef SIGPOLL
    { 0, SIGPOLL, "POLL", "IO" },
#endif
#ifdef SIGSTOP
    { 0, SIGSTOP, "STOP", NULL },
#endif
#ifdef SIGTSTP
    { 0, SIGTSTP, "TSTP", NULL },
#endif
#ifdef SIGCONT
    { 0, SIGCONT, "CONT", NULL },
#endif
#ifdef SIGCHLD
    { 0, SIGCHLD, "CHLD", "CLD" },
#endif
#ifdef SIGCLD
    { 0, SIGCLD, "CLD", "CHLD" },
#endif
#ifdef SIGTTIN
    { 0, SIGTTIN, "TTIN", NULL },
#endif
#ifdef SIGTTOU
    { 0, SIGTTOU, "TTOU", NULL },
#endif
#ifdef SIGINFO
    { 0, SIGINFO, "INFO", NULL },
#endif
#ifdef SIGURG
    { 0, SIGURG, "URG", NULL },
#endif
#ifdef SIGWAITING
    { 0, SIGWAITING, "WAITING", NULL },
#endif
#if defined(SIGLWP) && !defined(__FreeBSD__)
    { 0, SIGLWP, "LWP", NULL },
#endif
#ifdef SIGFREEZE
    { 0, SIGFREEZE, "FREEZE", NULL },
#endif
#ifdef SIGTHAW
    { 0, SIGTHAW, "THAW", NULL },
#endif
#ifdef SIGCANCEL
    { 0, SIGCANCEL, "CANCEL", NULL },
#endif
#if defined(SIGRTMIN) && defined(SIGRTMAX)
    { 0, -1, "RTMIN",   NULL },
    { 0, -1, "RTMIN+1", NULL },
    { 0, -1, "RTMIN+2", NULL },
    { 0, -1, "RTMIN+3", NULL },
    { 0, -1, "RTMAX-3", NULL },
    { 0, -1, "RTMAX-2", NULL },
    { 0, -1, "RTMAX-1", NULL },
    { 0, -1, "RTMAX",   NULL },
#endif
    { -1, 1024, "QWERT", NULL },	/* invalid */
    { -1, 0, NULL, NULL }
};

#ifndef HAVE_SIG2STR
static int
test_sig2str(int *ntests)
{
    struct signal_data *d;
    int rval, errors = 0;
    char sigstr[SIG2STR_MAX];

    for (d = signal_data; d->signo != 0; d++) {
	(*ntests)++;
	rval = sudo_sig2str(d->signo, sigstr);
	if (rval != d->rval) {
	    sudo_warnx_nodebug("FAIL: sig2str(SIG%s): %d != %d",
		d->sigstr, rval, d->rval);
	    errors++;
	    continue;
	}
	if (rval != 0)
	    continue;
	if (strcmp(sigstr, d->sigstr) != 0 &&
	    (d->altstr != NULL && strcmp(sigstr, d->altstr) != 0)) {
	    sudo_warnx_nodebug("FAIL: signal %d: %s != %s", d->signo,
		sigstr, d->sigstr);
	    errors++;
	    continue;
	}
    }

    return errors;
}
#else
static int
test_sig2str(int *ntests)
{
    return 0;
}
#endif /* HAVE_SIG2STR */

#ifndef HAVE_STR2SIG
static int
test_str2sig(int *ntests)
{
    struct signal_data *d;
    int rval, errors = 0;
    int signo;

    for (d = signal_data; d->sigstr != NULL; d++) {
	(*ntests)++;
	rval = sudo_str2sig(d->sigstr, &signo);
	if (rval != d->rval) {
	    sudo_warnx_nodebug("FAIL: str2sig(SIG%s): %d != %d",
		d->sigstr, rval, d->rval);
	    errors++;
	    continue;
	}
	if (rval != 0)
	    continue;
	if (signo != d->signo) {
	    sudo_warnx_nodebug("FAIL: signal SIG%s: %d != %d", d->sigstr,
		signo, d->signo);
	    errors++;
	    continue;
	}
    }

    return errors;
}
#else
static int
test_str2sig(int *ntests)
{
    return 0;
}
#endif /* HAVE_STR2SIG */

#if defined(SIGRTMIN) && defined(SIGRTMAX)
static
void init_sigrt(void)
{
    int i;

    /* Initialize real-time signal values. */
    for (i = 0; signal_data[i].signo != -1; i++)
	continue;
    signal_data[i++].signo = SIGRTMIN;
    signal_data[i++].signo = SIGRTMIN + 1;
    signal_data[i++].signo = SIGRTMIN + 2;
    signal_data[i++].signo = SIGRTMIN + 3;
    signal_data[i++].signo = SIGRTMAX - 3;
    signal_data[i++].signo = SIGRTMAX - 2;
    signal_data[i++].signo = SIGRTMAX - 1;
    signal_data[i++].signo = SIGRTMAX;

}
#else
static
void init_sigrt(void)
{
    /* No real-time signals. */
    return;
}
#endif

/*
 * Simple tests for sig2str() and str2sig().
 */
int
main(int argc, char *argv[])
{
    int ch, errors = 0, ntests = 0;

    initprogname(argc > 0 ? argv[0] : "strsig_test");

    while ((ch = getopt(argc, argv, "v")) != -1) {
	switch (ch) {
	case 'v':
	    /* ignore */
	    break;
	default:
	    fprintf(stderr, "usage: %s [-v]\n", getprogname());
	    return EXIT_FAILURE;
	}
    }
    argc -= optind;
    argv += optind;

    init_sigrt();
    errors += test_sig2str(&ntests);
    errors += test_str2sig(&ntests);

    if (ntests != 0) {
	printf("%s: %d tests run, %d errors, %d%% success rate\n",
	    getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
    }

    return errors;
}