File: test_signal.c

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (391 lines) | stat: -rw-r--r-- 8,749 bytes parent folder | download | duplicates (6)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* libnih
 *
 * test_signal.c - test suite for nih/signal.c
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <nih/test.h>

#if HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
#endif /* HAVE_VALGRIND_VALGRIND_H */

#include <signal.h>

#include <nih/macros.h>
#include <nih/list.h>
#include <nih/signal.h>


static void
my_sig_handler (int signum) {
}

void
test_set_handler (void)
{
	struct sigaction act;
	int              ret, i;

	TEST_FUNCTION ("nih_signal_set_handler");

	/* Check that we can install a signal handler, and that the action
	 * for that signal points to our handler, has the right flags and
	 * an empty signal mask.
	 */
	TEST_FEATURE ("with valid signal");
	ret = nih_signal_set_handler (SIGUSR1, my_sig_handler);

	TEST_EQ (ret, 0);

	sigaction (SIGUSR1, NULL, &act);
	TEST_EQ_P (act.sa_handler, my_sig_handler);
	TEST_TRUE (act.sa_flags & SA_RESTART);
	TEST_FALSE (act.sa_flags & SA_RESETHAND);

	for (i = 1; i < 32; i++)
		TEST_FALSE (sigismember (&act.sa_mask, i));


#if HAVE_VALGRIND_VALGRIND_H
	/* This test fails when running under valgrind; because for no
	 * readily apparent reason, that lets us catch SIGKILL!
	 */
	if (! RUNNING_ON_VALGRIND) {
#endif
	/* Check that attempting to set a handler for SIGKILL results in
	 * -1 being returned.
	 */
	TEST_FEATURE ("with invalid signal");
	ret = nih_signal_set_handler (SIGKILL, my_sig_handler);

	TEST_LT (ret, 0);
#if HAVE_VALGRIND_VALGRIND_H
	}
#endif
}

void
test_set_default (void)
{
	struct sigaction act;
	int              ret, i;

	TEST_FUNCTION ("nih_signal_set_default");

	/* Check that we can reset a signal to the default handling, which
	 * should update the action properly.
	 */
	TEST_FEATURE ("with valid signal");
	ret = nih_signal_set_default (SIGUSR1);

	TEST_EQ (ret, 0);

	sigaction (SIGUSR1, NULL, &act);
	TEST_EQ_P (act.sa_handler, SIG_DFL);
	TEST_FALSE (act.sa_flags & SA_RESTART);
	TEST_FALSE (act.sa_flags & SA_NOCLDSTOP);

	for (i = 1; i < 32; i++)
		TEST_FALSE (sigismember (&act.sa_mask, i));


#if HAVE_VALGRIND_VALGRIND_H
	/* This test fails when running under valgrind; because for no
	 * readily apparent reason, that lets us catch SIGKILL!
	 */
	if (! RUNNING_ON_VALGRIND) {
#endif
	/* Check that attempting to set a handler for SIGKILL results in
	 * -1 being returned.
	 */
	TEST_FEATURE ("with invalid signal");
	ret = nih_signal_set_default (SIGKILL);

	TEST_LT (ret, 0);
#if HAVE_VALGRIND_VALGRIND_H
	}
#endif
}

void
test_set_ignore (void)
{
	struct sigaction act;
	int              ret, i;

	TEST_FUNCTION ("nih_signal_set_ignore");

	/* Check that we can set a signal to be ignored, which should update
	 * the action properly.
	 */
	TEST_FEATURE ("with valid signal");
	ret = nih_signal_set_ignore (SIGUSR1);

	TEST_EQ (ret, 0);

	sigaction (SIGUSR1, NULL, &act);
	TEST_EQ_P (act.sa_handler, SIG_IGN);
	TEST_FALSE (act.sa_flags & SA_RESTART);
	TEST_FALSE (act.sa_flags & SA_NOCLDSTOP);

	for (i = 1; i < 32; i++)
		TEST_FALSE (sigismember (&act.sa_mask, i));


#if HAVE_VALGRIND_VALGRIND_H
	/* This test fails when running under valgrind; because for no
	 * readily apparent reason, that lets us ignore SIGKILL!
	 */
	if (! RUNNING_ON_VALGRIND) {
#endif
	/* Check that attempting to set a handler for SIGKILL results in
	 * -1 being returned.
	 */
	TEST_FEATURE ("with invalid signal");
	ret = nih_signal_set_ignore (SIGKILL);

	TEST_LT (ret, 0);
#if HAVE_VALGRIND_VALGRIND_H
	}
#endif
}

void
test_reset (void)
{
	struct sigaction act;
	int              i;

	/* Check that we can reset all signals back to their defaults. */
	TEST_FUNCTION ("nih_signal_reset");
	nih_signal_set_ignore (SIGTERM);
	nih_signal_reset ();

	sigaction (SIGTERM, NULL, &act);
	TEST_EQ_P (act.sa_handler, SIG_DFL);
	TEST_FALSE (act.sa_flags & SA_RESTART);
	TEST_FALSE (act.sa_flags & SA_NOCLDSTOP);

	for (i = 1; i < 32; i++)
		TEST_FALSE (sigismember (&act.sa_mask, i));
}


static int handler_called = 0;
static void *last_data;
static NihSignal *last_signal;

static void
my_handler (void *data, NihSignal *signal)
{
	handler_called++;
	last_data = data;
	last_signal = signal;
}

void
test_add_handler (void)
{
	NihSignal *signal;

	/* Check that we can add a signal handling callback function, and
	 * that the structure returned is properly populated and placed in
	 * the callbacks list.
	 */
	TEST_FUNCTION ("nih_signal_add_handler");
	nih_signal_poll ();
	TEST_ALLOC_FAIL {
		signal = nih_signal_add_handler (NULL, SIGUSR1,
						 my_handler, &signal);

		if (test_alloc_failed) {
			TEST_EQ_P (signal, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (signal, sizeof (NihSignal));
		TEST_LIST_NOT_EMPTY (&signal->entry);
		TEST_EQ (signal->signum, SIGUSR1);
		TEST_EQ_P (signal->handler, my_handler);
		TEST_EQ_P (signal->data, &signal);

		nih_free (signal);
	}
}

void
test_poll (void)
{
	NihSignal *signal1, *signal2;

	TEST_FUNCTION ("nih_signal_poll");
	signal1 = nih_signal_add_handler (NULL, SIGUSR1, my_handler, &signal1);
	signal2 = nih_signal_add_handler (NULL, SIGUSR2, my_handler, &signal2);

	/* Check that we can poll for a signal being caught, which should
	 * result in only the callback for that signal being run.
	 */
	TEST_FEATURE ("with one signal");
	handler_called = 0;
	last_data = NULL;
	last_signal = NULL;

	nih_signal_handler (SIGUSR1);
	nih_signal_poll ();

	TEST_EQ (handler_called, 1);
	TEST_EQ_P (last_signal, signal1);
	TEST_EQ_P (last_data, &signal1);


	/* Check that we can poll for only the other signal. */
	TEST_FEATURE ("with different signal");
	handler_called = 0;
	last_data = NULL;
	last_signal = NULL;

	nih_signal_handler (SIGUSR2);
	nih_signal_poll ();

	TEST_EQ (handler_called, 1);
	TEST_EQ_P (last_signal, signal2);
	TEST_EQ_P (last_data, &signal2);


	/* Check that we can poll for both signals. */
	TEST_FEATURE ("with multiple signals");
	handler_called = 0;

	nih_signal_handler (SIGUSR1);
	nih_signal_handler (SIGUSR2);
	nih_signal_poll ();

	TEST_EQ (handler_called, 2);


	/* Check what happens if a signal we have no callbacks for is
	 * caught.  This should run neither callback.
	 */
	TEST_FEATURE ("with unknown signal");
	handler_called = 0;

	nih_signal_handler (SIGINT);
	nih_signal_poll ();

	TEST_EQ (handler_called, 0);


	nih_free (signal1);
	nih_free (signal2);
}


void
test_to_name (void)
{
	const char *name;

	TEST_FUNCTION ("nih_signal_to_name");

	/* Check that we can obtain the name of a common signal. */
	TEST_FEATURE ("with SIGTERM");
	name = nih_signal_to_name (SIGTERM);

	TEST_EQ_STR (name, "TERM");


	/* Check that we get CHLD for SIGCHLD */
	TEST_FEATURE ("with SIGCHLD");
	name = nih_signal_to_name (SIGCHLD);

	TEST_EQ_STR (name, "CHLD");


	/* Check that we get IO for SIGIO */
	TEST_FEATURE ("with SIGIO");
	name = nih_signal_to_name (SIGIO);

	TEST_EQ_STR (name, "IO");


	/* Check that we get NULL for an unknown signal */
	TEST_FEATURE ("with unknown signal");
	name = nih_signal_to_name (32);

	TEST_EQ_P (name, NULL);
}

void
test_from_name (void)
{
	int signum;

	TEST_FUNCTION ("nih_signal_from_name");

	/* Check that we can convert a common signal into its number. */
	TEST_FEATURE ("with SIGTERM");
	signum = nih_signal_from_name ("SIGTERM");

	TEST_EQ (signum, SIGTERM);


	/* Check that we can omit the SIG from the front. */
	TEST_FEATURE ("with TERM");
	signum = nih_signal_from_name ("TERM");

	TEST_EQ (signum, SIGTERM);


	/* Check that we get SIGCHLD for SIGCHLD */
	TEST_FEATURE ("with SIGCHLD");
	signum = nih_signal_from_name ("SIGCHLD");

	TEST_EQ (signum, SIGCHLD);


	/* Check that we get SIGIO for SIGIO */
	TEST_FEATURE ("with SIGIO");
	signum = nih_signal_from_name ("SIGIO");

	TEST_EQ (signum, SIGIO);


	/* Check that we get a negative number for an unknown signal */
	TEST_FEATURE ("with unknown signal");
	signum = nih_signal_from_name ("SIGSNARF");

	TEST_LT (signum, 0);
}


int
main (int   argc,
      char *argv[])
{
	test_set_handler ();
	test_set_default ();
	test_set_ignore ();
	test_reset ();
	test_add_handler ();
	test_poll ();
	test_to_name ();
	test_from_name ();

	return 0;
}