/[ascend]/trunk/base/generic/utilities/test/test_ascSignal.c
ViewVC logotype

Contents of /trunk/base/generic/utilities/test/test_ascSignal.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1003 - (show annotations) (download) (as text)
Sun Dec 31 02:35:27 2006 UTC (15 years, 6 months ago) by johnpye
File MIME type: text/x-csrc
File size: 23355 byte(s)
Some progress on fixing test_ascSignal.c
1 /*
2 * Unit test functions for ASCEND: utilities/ascSignal.c
3 *
4 * Copyright (C) 2005 Jerry St.Clair
5 *
6 * This file is part of the Ascend Environment.
7 *
8 * The Ascend Environment is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Ascend Environment is distributed in hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
21 * COPYING.
22 */
23
24 #include <stdio.h>
25 #include <utilities/ascConfig.h>
26 #ifdef __WIN32__
27 #include <io.h>
28 #endif
29 #include <utilities/ascMalloc.h>
30 #include <utilities/ascSignal.h>
31 #include <utilities/ascPanic.h>
32 #include "CUnit/CUnit.h"
33 #include "test_ascSignal.h"
34 #include "printutil.h"
35
36 static JMP_BUF my_jmp_buf1;
37
38 #define MEMUSED(N) CU_TEST(ascmeminuse()==N)
39
40 static int f_handler1_called;
41 static int f_handler1_sigval;
42 /*
43 * Signal handler for unit tests.
44 * Resets the signal handlers and sets f_handler1_called to
45 * TRUE and f_handler1_sigval to the signal type code (-1 if
46 * an unsupported sigval). Then longjmp's using
47 * my_jmp_buf1 and the sigval.
48 */
49 void my_handler1(int sigval)
50 {
51 f_handler1_called = TRUE;
52 Asc_SignalRecover(FALSE);
53 switch (sigval)
54 {
55 case SIGFPE:
56 f_handler1_sigval = SIGFPE;
57 FPRESET;
58 break;
59 case SIGINT:
60 f_handler1_sigval = SIGINT;
61 break;
62 case SIGSEGV:
63 f_handler1_sigval = SIGSEGV;
64 break;
65 default:
66 f_handler1_sigval = -1;
67 break;
68 }
69 LONGJMP(my_jmp_buf1, sigval);
70 }
71
72 void wrong_handler(int sigval){
73 ASC_PANIC("Don't use this one");
74 }
75
76 static JMP_BUF my_jmp_buf2;
77
78 static int f_handler2_called;
79 static int f_handler2_sigval;
80 /*
81 * Signal handler for unit tests.
82 * Resets the signal handlers and sets f_handler1_called to
83 * TRUE and f_handler1_sigval to the signal type code (-1 if
84 * an unsupported sigval). Then longjmp's using
85 * my_jmp_buf1 and the sigval.
86 */
87 void my_handler2(int sigval)
88 {
89 f_handler2_called = TRUE;
90 Asc_SignalRecover(FALSE);
91 switch (sigval)
92 {
93 case SIGFPE:
94 f_handler2_sigval = SIGFPE;
95 FPRESET;
96 break;
97 case SIGINT:
98 f_handler2_sigval = SIGINT;
99 break;
100 case SIGSEGV:
101 f_handler2_sigval = SIGSEGV;
102 break;
103 default:
104 f_handler2_sigval = -1;
105 break;
106 }
107 LONGJMP(my_jmp_buf2, sigval);
108 }
109
110 static int old_active = 0;
111 static SigHandlerFn *old_fpe_handler = NULL;
112 static SigHandlerFn *old_int_handler = NULL;
113 static SigHandlerFn *old_seg_handler = NULL;
114
115 static void store_current_signals(void){
116 CU_TEST(old_active==0);
117 old_active = 1;
118 old_fpe_handler = SIGNAL(SIGFPE, my_handler1); /* save any pre-existing handlers */
119 old_int_handler = SIGNAL(SIGINT, my_handler1);
120 old_seg_handler = SIGNAL(SIGSEGV, my_handler1);
121 }
122
123 #define CHECK_SIGNALS_MATCH_STACKS(FPEFN,INTFN,SEGFN) \
124 { SigHandlerFn *oldfpe, *oldint, *oldseg; \
125 oldfpe = signal(SIGFPE,SIG_IGN);\
126 CU_TEST(oldfpe == FPEFN);\
127 CU_TEST(Asc_SignalStackTop(SIGFPE)==FPEFN);\
128 oldfpe = signal(SIGFPE,oldfpe);\
129 \
130 oldint = signal(SIGINT,SIG_IGN);\
131 CU_TEST(oldint == INTFN);\
132 CU_TEST(Asc_SignalStackTop(SIGINT)==INTFN);\
133 oldint = signal(SIGINT,oldint);\
134 \
135 oldseg = signal(SIGSEGV,SIG_IGN);\
136 CU_TEST(oldseg == SEGFN);\
137 CU_TEST(Asc_SignalStackTop(SIGSEGV)==SEGFN);\
138 oldseg = signal(SIGSEGV,oldseg);\
139 }
140
141 static void restore_previous_signals(void){
142 CU_TEST(old_active==1);
143 if (NULL != old_fpe_handler) /* restore any pre-existing handlers */
144 SIGNAL(SIGFPE, old_fpe_handler);
145 if (NULL != old_int_handler)
146 SIGNAL(SIGINT, old_int_handler);
147 if (NULL != old_seg_handler)
148 SIGNAL(SIGSEGV, old_seg_handler);
149 old_active = 0;
150 }
151
152 /*----------------------------------------*/
153
154 static void test_ascsignal_basic(void){
155
156 SigHandlerFn *old_handler;
157 volatile int signal1_caught;
158
159 #ifdef NO_SIGNAL_TRAPS
160 /* no point in testing if the functionality is disabled */
161 CU_FAIL("Signal handler manager not enabled.");
162 return;
163 #else
164
165 CONSOLE_DEBUG("SIGFPE = %d",SIGFPE);
166 CONSOLE_DEBUG("SIGINT = %d",SIGINT);
167 CONSOLE_DEBUG("SIGSEGV= %d",SIGSEGV);
168
169 store_current_signals();
170
171 SIGNAL(SIGFPE, my_handler1); /* install some pre-existing handlers */
172 SIGNAL(SIGINT, SIG_DFL);
173 SIGNAL(SIGSEGV, my_handler2);
174
175 /* Asc_SignalInit(), Asc_SignalDestroy() - not much to test */
176
177 CU_TEST(0 == Asc_SignalInit()); /* initialize the signal manager */
178
179 CHECK_SIGNALS_MATCH_STACKS(my_handler1, NULL, my_handler2);
180
181 /* mix up the current handlers */
182 SIGNAL(SIGFPE, my_handler2);
183 SIGNAL(SIGINT, SIG_IGN);
184 SIGNAL(SIGSEGV, my_handler1);
185
186 Asc_SignalRecover(TRUE);
187
188 /* check that recovery worked; check that repeated calls don't damage it */
189 CHECK_SIGNALS_MATCH_STACKS(my_handler1, NULL, my_handler2);
190 CHECK_SIGNALS_MATCH_STACKS(my_handler1, NULL, my_handler2);
191 CHECK_SIGNALS_MATCH_STACKS(my_handler1, NULL, my_handler2);
192
193 restore_previous_signals();
194 Asc_SignalDestroy();
195 MEMUSED(0);
196 }
197
198 /*----------------------------------------*/
199
200 static void test_ascsignal_pushpopint(void){
201 SigHandlerFn *old_handler;
202 volatile int signal1_caught;
203
204 store_current_signals();
205
206 SIGNAL(SIGINT, SIG_IGN);
207 CU_TEST(0==Asc_SignalInit());
208
209 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
210
211 CU_TEST(0==Asc_SignalHandlerPush(SIGINT, my_handler1)); /* ok - supported signal, ok func */
212 CU_TEST(Asc_SignalStackLength(SIGINT)==2);
213
214 CONSOLE_DEBUG("Stack should have my_handler1...");
215 Asc_SignalPrintStack(SIGINT);
216
217 old_handler = SIGNAL(SIGINT, SIG_DFL);
218 CU_TEST(old_handler == my_handler1);
219
220 CU_TEST(0 != Asc_SignalHandlerPush(SIGILL, Asc_SignalTrap)); /* unsupported signal type */
221 CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2)); /* unsupported signal type */
222 Asc_SignalRecover(TRUE);
223 CU_TEST(Asc_SignalStackLength(SIGINT)==2);
224
225 old_handler = SIGNAL(SIGINT, SIG_DFL);
226 CU_TEST(old_handler == my_handler1);
227
228 Asc_SignalRecover(TRUE);
229 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
230 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
231
232 old_handler = SIGNAL(SIGINT, SIG_DFL);
233 CU_TEST(old_handler == SIG_IGN);
234
235 Asc_SignalRecover(TRUE);
236 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
237
238 old_handler = SIGNAL(SIGINT, SIG_DFL);
239 CU_TEST(SIG_IGN == old_handler);
240
241 Asc_SignalRecover(TRUE);
242
243 old_handler = SIGNAL(SIGINT, SIG_DFL);
244 CU_TEST(SIG_IGN == old_handler);
245 Asc_SignalRecover(TRUE);
246
247 CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1)); /* unsupported signal type */
248 CU_TEST(0 != Asc_SignalHandlerPop(SIGABRT, Asc_SignalTrap)); /* unsupported signal type */
249
250 old_handler = SIGNAL(SIGINT, SIG_DFL);
251 CU_TEST(SIG_IGN == old_handler);
252
253 restore_previous_signals();
254 Asc_SignalDestroy();
255 MEMUSED(0);
256 }
257
258 /*----------------------------------------*/
259
260 static void test_ascsignal_pushpop(void){
261 SigHandlerFn *old_handler;
262 volatile int signal1_caught;
263
264 CONSOLE_DEBUG("my_handler1 = %p",my_handler1);
265 CONSOLE_DEBUG("my_handler2 = %p",my_handler2);
266 CONSOLE_DEBUG("Asc_SignalTrap = %p",Asc_SignalTrap);
267
268 store_current_signals();
269
270 /* set the initial handlers */
271 SIGNAL(SIGFPE, my_handler1);
272 SIGNAL(SIGINT, SIG_IGN);
273 SIGNAL(SIGSEGV, my_handler2);
274
275 /* initialise the stack */
276 CU_TEST(0 == Asc_SignalInit());
277
278 CHECK_SIGNALS_MATCH_STACKS(my_handler1, SIG_IGN, my_handler2);
279
280 /* check that initial handlers were pushed onto the stack */
281 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
282 CU_TEST(Asc_SignalStackLength(SIGFPE)==1);
283 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
284
285 /* push an additional handler onto SIGFPE stack */
286 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
287
288 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, SIG_IGN, my_handler2);
289
290 CONSOLE_DEBUG("SIGFPE stack:");
291 Asc_SignalPrintStack(SIGFPE);
292
293 /* push an additional handler onto SIGINT stack */
294 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));
295
296 /* check the stacks */
297 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, my_handler1, my_handler2);
298 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1); /* my_handler2 */
299 CU_TEST(Asc_SignalStackLength(SIGINT)==2); /* SIG_IGN, my_handler1 */
300 CU_TEST(Asc_SignalStackLength(SIGFPE)==2); /* my_handler1, Asc_SignalTrap */
301
302 CONSOLE_DEBUG("SIGFPE stack:");
303 Asc_SignalPrintStack(SIGFPE);
304
305 /* attempt to push a NULL handler should have no effect */
306 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, NULL));
307 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, my_handler1, my_handler2);
308 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
309 CU_TEST(Asc_SignalStackLength(SIGINT)==2);
310 CU_TEST(Asc_SignalStackLength(SIGFPE)==2);
311 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, my_handler1, my_handler2);
312
313 CONSOLE_DEBUG("SIGFPE stack:");
314 Asc_SignalPrintStack(SIGFPE);
315 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, my_handler1, my_handler2);
316
317 /* attempt to push handler for unsupported signal types should have no effect, should return error */
318 CU_TEST(0 != Asc_SignalHandlerPush(SIGILL, Asc_SignalTrap));
319 CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2));
320 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, my_handler1, my_handler2);
321 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
322 CU_TEST(Asc_SignalStackLength(SIGINT)==2);
323 CU_TEST(Asc_SignalStackLength(SIGFPE)==2);
324 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, my_handler1, my_handler2);
325
326 /* pop off the SIGINT stack */
327 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
328
329 CONSOLE_DEBUG("SIGFPE stack:");
330 Asc_SignalPrintStack(SIGFPE);
331
332 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, SIG_IGN, my_handler2);
333 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
334 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
335 CU_TEST(Asc_SignalStackLength(SIGFPE)==2);
336
337 /* attempt to pop off an incorrect handler */
338 CONSOLE_DEBUG("Popping incorrect handler");
339 CU_TEST(0 != Asc_SignalHandlerPop(SIGFPE, wrong_handler));
340
341 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, SIG_IGN, my_handler2);
342 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
343 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
344 CU_TEST(Asc_SignalStackLength(SIGFPE)==2);
345
346 /* mess with the SIGFPE handler, then attempt to recover from stacks */
347 old_handler = SIGNAL(SIGFPE, SIG_DFL);
348 Asc_SignalRecover(TRUE);
349
350 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, SIG_IGN, my_handler2);
351 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
352 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
353 CU_TEST(Asc_SignalStackLength(SIGFPE)==2);
354
355 /* try a couple of unsupported signal types */
356 CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1));
357 CU_TEST(0 != Asc_SignalHandlerPop(SIGABRT, Asc_SignalTrap));
358
359 CHECK_SIGNALS_MATCH_STACKS(Asc_SignalTrap, SIG_IGN, my_handler2);
360 CU_TEST(Asc_SignalStackLength(SIGSEGV)==1);
361 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
362 CU_TEST(Asc_SignalStackLength(SIGFPE)==2);
363
364 restore_previous_signals();
365 Asc_SignalDestroy();
366 MEMUSED(0);
367 }
368
369 /*----------------------------------------*/
370
371 static void test_ascsignal_trap(void){
372 SigHandlerFn *old_handler;
373 volatile int signal1_caught;
374
375 store_current_signals();
376
377 SIGNAL(SIGFPE,my_handler1);
378 SIGNAL(SIGINT,NULL);
379 SIGNAL(SIGSEGV,my_handler2);
380
381 CU_TEST(0 == Asc_SignalInit());
382
383 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
384 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
385 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap));
386
387 signal1_caught = FALSE;
388 CONSOLE_DEBUG("Raise and catch a SIGFPE...");
389 if (0 == SETJMP(g_fpe_env)){
390 CONSOLE_DEBUG("Raising SIGFPE");
391 raise(SIGFPE);
392 CONSOLE_DEBUG("SHOULDN'T BE HERE");
393 CU_FAIL("Shouldn't be here");
394 }else{
395 signal1_caught = TRUE;
396 CONSOLE_DEBUG("Caught SIGFPE");
397 }
398 CU_TEST(TRUE == signal1_caught);
399
400 signal1_caught = FALSE;
401 /* raise & catch a SIGINT */
402 if (0 == SETJMP(g_int_env)){
403 raise(SIGINT);
404 }else{
405 signal1_caught = TRUE;
406 CONSOLE_DEBUG("Caught SIGINT");
407 }
408 CU_TEST(TRUE == signal1_caught);
409
410 signal1_caught = FALSE;
411 /* raise & catch a SIGSEGV */
412 if (0 == SETJMP(g_seg_env)) {
413 raise(SIGSEGV);
414 }else{
415 signal1_caught = TRUE;
416 CONSOLE_DEBUG("Caught SIGSEGV");
417 }
418 CU_TEST(TRUE == signal1_caught);
419
420
421 Asc_SignalRecover(TRUE);
422
423 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
424 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
425 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
426
427 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
428 CU_TEST(my_handler1 == old_handler);
429 old_handler = SIGNAL(SIGINT, SIG_DFL);
430 CU_TEST(NULL == old_handler);
431 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
432 CU_TEST(my_handler2 == old_handler);
433 Asc_SignalRecover(TRUE);
434
435 restore_previous_signals();
436 Asc_SignalDestroy();
437 MEMUSED(0);
438 }
439
440 /*----------------------------------------*/
441
442 static void test_ascsignal_nestingfpe(void){
443 volatile int signal1_caught;
444 volatile int signal2_caught;
445 volatile int signal3_caught;
446
447 store_current_signals();
448 CU_TEST(0 == Asc_SignalInit());
449
450 /* test typical use with nesting of handlers */
451
452 f_handler1_called = FALSE; /* initialize flags for detecting flow */
453 f_handler1_sigval = 0;
454 f_handler2_called = FALSE;
455 f_handler2_sigval = 0;
456 signal1_caught = FALSE;
457 signal2_caught = FALSE;
458 signal3_caught = FALSE;
459
460 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler1));
461 if (0 == SETJMP(my_jmp_buf1)) {
462 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler2));
463 if (0 == SETJMP(my_jmp_buf2)) {
464 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
465 if(0 == SETJMP(g_fpe_env)) {
466 raise(SIGFPE);
467 CONSOLE_DEBUG("...");
468 CU_FAIL("Can't be here! No signal was raised!");
469 }else{
470 CONSOLE_DEBUG("...");
471 CU_TEST(f_handler1_called == FALSE);
472 CU_TEST(f_handler1_sigval == 0);
473 CU_TEST(f_handler2_called == FALSE);
474 CU_TEST(f_handler2_sigval == 0);
475 signal3_caught = TRUE;
476 }
477 CONSOLE_DEBUG("...");
478 CU_TEST(FALSE == signal1_caught);
479 CU_TEST(FALSE == signal2_caught);
480 CU_TEST(TRUE == signal3_caught);
481 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
482 f_handler1_called = FALSE;
483 f_handler1_sigval = 0;
484 f_handler2_called = FALSE;
485 f_handler2_sigval = 0;
486 signal1_caught = FALSE;
487 signal2_caught = FALSE;
488 signal3_caught = FALSE;
489 raise(SIGFPE);
490 }else{
491 CONSOLE_DEBUG("...");
492 CU_TEST(f_handler1_called == FALSE);
493 CU_TEST(f_handler1_sigval == 0);
494 CU_TEST(f_handler2_called == TRUE);
495 CU_TEST(f_handler2_sigval == SIGFPE);
496 signal2_caught = TRUE;
497 }
498 CU_TEST(FALSE == signal1_caught);
499 CU_TEST(TRUE == signal2_caught);
500 CU_TEST(FALSE == signal3_caught);
501 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler2));
502 f_handler1_called = FALSE;
503 f_handler1_sigval = 0;
504 f_handler2_called = FALSE;
505 f_handler2_sigval = 0;
506 signal1_caught = FALSE;
507 signal2_caught = FALSE;
508 signal3_caught = FALSE;
509 raise(SIGFPE);
510 }else{
511 CONSOLE_DEBUG("...");
512 CU_TEST(f_handler1_called == TRUE);
513 CU_TEST(f_handler1_sigval == SIGFPE);
514 CU_TEST(f_handler2_called == FALSE);
515 CU_TEST(f_handler2_sigval == 0);
516 signal1_caught = TRUE;
517 }
518 CU_TEST(TRUE == signal1_caught);
519 CU_TEST(FALSE == signal2_caught);
520 CU_TEST(FALSE == signal3_caught);
521 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));
522
523 CONSOLE_DEBUG("...");
524
525 restore_previous_signals();
526 Asc_SignalDestroy();
527 MEMUSED(0);
528 }
529
530 /*----------------------------------------*/
531
532 static void test_ascsignal_nestingint(void){
533 volatile int signal1_caught;
534 volatile int signal2_caught;
535 volatile int signal3_caught;
536 SigHandlerFn *old_handler;
537
538 store_current_signals();
539 CU_TEST(0 == Asc_SignalInit());
540
541 f_handler1_called = FALSE; /* initialize flags for detecting flow */
542 f_handler1_sigval = 0;
543 f_handler2_called = FALSE;
544 f_handler2_sigval = 0;
545 signal1_caught = FALSE;
546 signal2_caught = FALSE;
547 signal3_caught = FALSE;
548
549 CONSOLE_DEBUG("my_handler1 = %p",my_handler1);
550 CONSOLE_DEBUG("my_handler2 = %p",my_handler2);
551 CONSOLE_DEBUG("Asc_SignalTrap = %p",Asc_SignalTrap);
552
553 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler2)); /* test for SIGINT */
554 if (0 == SETJMP(my_jmp_buf2)) {
555 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
556 if (0 == SETJMP(g_int_env)) {
557 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));
558 if (0 == SETJMP(my_jmp_buf1)) {
559 CONSOLE_DEBUG("Raising to my_handler1");
560 raise(SIGINT);
561 CONSOLE_DEBUG("SHOULDN'T BE HERE!");
562 CU_FAIL("Shouldn't be here!");
563 }else{
564 CONSOLE_DEBUG("Caught from my_handler1");
565 CU_TEST(f_handler1_called == TRUE);
566 CU_TEST(f_handler1_sigval == SIGINT);
567 CU_TEST(f_handler2_called == FALSE);
568 CU_TEST(f_handler2_sigval == 0);
569 signal3_caught = TRUE;
570 }
571 CU_TEST(FALSE == signal1_caught);
572 CU_TEST(FALSE == signal2_caught);
573 CU_TEST(TRUE == signal3_caught);
574 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
575
576 /* check that 'Asc_SignalTrap' is now installed for SIGINT */
577 old_handler = SIGNAL(SIGINT,SIG_DFL);
578 CU_TEST(old_handler == Asc_SignalTrap);
579 old_handler = SIGNAL(SIGINT,Asc_SignalTrap);
580 CU_TEST(old_handler == SIG_DFL);
581
582 f_handler1_called = FALSE;
583 f_handler1_sigval = 0;
584 f_handler2_called = FALSE;
585 f_handler2_sigval = 0;
586 signal1_caught = FALSE;
587 signal2_caught = FALSE;
588 signal3_caught = FALSE;
589 CONSOLE_DEBUG("Raising to Asc_SignalTrap");
590 raise(SIGINT);
591 CONSOLE_DEBUG("SHOULDN'T BE HERE!");
592 CU_FAIL("Shouldn't be here!");
593 }else{
594 CONSOLE_DEBUG("Caught from Asc_SignalTrap");
595 CU_TEST(f_handler1_called == FALSE);
596 CU_TEST(f_handler1_sigval == 0);
597 CU_TEST(f_handler2_called == FALSE);
598 CU_TEST(f_handler2_sigval == 0);
599 signal2_caught = TRUE;
600 }
601 CU_TEST(FALSE == signal1_caught);
602 CU_TEST(TRUE == signal2_caught);
603 CU_TEST(FALSE == signal3_caught);
604 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
605 f_handler1_called = FALSE;
606 f_handler1_sigval = 0;
607 f_handler2_called = FALSE;
608 f_handler2_sigval = 0;
609 signal1_caught = FALSE;
610 signal2_caught = FALSE;
611 signal3_caught = FALSE;
612 CONSOLE_DEBUG("Raising to my_handler2");
613 raise(SIGINT);
614 CONSOLE_DEBUG("SHOULDN'T BE HERE!");
615 CU_FAIL("Shouldn't be here!");
616 }else{
617 CONSOLE_DEBUG("Caught from my_handler2");
618 CU_TEST(f_handler1_called == FALSE);
619 CU_TEST(f_handler1_sigval == 0);
620 CU_TEST(f_handler2_called == TRUE);
621 CU_TEST(f_handler2_sigval == SIGINT);
622 signal1_caught = TRUE;
623 }
624 CU_TEST(TRUE == signal1_caught);
625 CU_TEST(FALSE == signal2_caught);
626 CU_TEST(FALSE == signal3_caught);
627 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));
628
629 restore_previous_signals();
630 Asc_SignalDestroy();
631 MEMUSED(0);
632 }
633
634 /*----------------------------------------*/
635
636 static void test_ascsignal_nestingsegv(void){
637
638 SigHandlerFn *old_handler;
639 volatile int signal1_caught;
640 volatile int signal2_caught;
641 volatile int signal3_caught;
642
643 store_current_signals();
644 CU_TEST(0 == Asc_SignalInit());
645
646 f_handler1_called = FALSE; /* initialize flags for detecting flow */
647 f_handler1_sigval = 0;
648 f_handler2_called = FALSE;
649 f_handler2_sigval = 0;
650 signal1_caught = FALSE;
651 signal2_caught = FALSE;
652 signal3_caught = FALSE;
653
654 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap)); /* test for SIGSEGV */
655 if (0 == SETJMP(g_seg_env)) {
656 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler2));
657 if (0 == SETJMP(my_jmp_buf2)) {
658 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler1));
659 if (0 == SETJMP(my_jmp_buf1)) {
660 raise(SIGSEGV);
661 }
662 else {
663 CU_TEST(f_handler1_called == TRUE);
664 CU_TEST(f_handler1_sigval == SIGSEGV);
665 CU_TEST(f_handler2_called == FALSE);
666 CU_TEST(f_handler2_sigval == 0);
667 signal3_caught = TRUE;
668 }
669 CU_TEST(FALSE == signal1_caught);
670 CU_TEST(FALSE == signal2_caught);
671 CU_TEST(TRUE == signal3_caught);
672 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler1));
673 f_handler1_called = FALSE;
674 f_handler1_sigval = 0;
675 f_handler2_called = FALSE;
676 f_handler2_sigval = 0;
677 signal1_caught = FALSE;
678 signal2_caught = FALSE;
679 signal3_caught = FALSE;
680 raise(SIGSEGV);
681 }
682 else {
683 CU_TEST(f_handler1_called == FALSE);
684 CU_TEST(f_handler1_sigval == 0);
685 CU_TEST(f_handler2_called == TRUE);
686 CU_TEST(f_handler2_sigval == SIGSEGV);
687 signal2_caught = TRUE;
688 }
689 CU_TEST(FALSE == signal1_caught);
690 CU_TEST(TRUE == signal2_caught);
691 CU_TEST(FALSE == signal3_caught);
692 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler2));
693 f_handler1_called = FALSE;
694 f_handler1_sigval = 0;
695 f_handler2_called = FALSE;
696 f_handler2_sigval = 0;
697 signal1_caught = FALSE;
698 signal2_caught = FALSE;
699 signal3_caught = FALSE;
700 raise(SIGSEGV);
701 }
702 else {
703 CU_TEST(f_handler1_called == FALSE);
704 CU_TEST(f_handler1_sigval == 0);
705 CU_TEST(f_handler2_called == FALSE);
706 CU_TEST(f_handler2_sigval == 0);
707 signal1_caught = TRUE;
708 }
709 CU_TEST(TRUE == signal1_caught);
710 CU_TEST(FALSE == signal2_caught);
711 CU_TEST(FALSE == signal3_caught);
712 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
713
714 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
715 CU_TEST(my_handler1 == old_handler);
716 old_handler = SIGNAL(SIGINT, SIG_DFL);
717 CU_TEST(NULL == old_handler);
718 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
719 CU_TEST(my_handler2 == old_handler);
720 Asc_SignalRecover(TRUE);
721
722 Asc_SignalDestroy();
723
724 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* original handlers should still be in place */
725 CU_TEST(my_handler1 == old_handler);
726 old_handler = SIGNAL(SIGINT, SIG_DFL);
727 CU_TEST(NULL == old_handler);
728 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
729 CU_TEST(my_handler2 == old_handler);
730 Asc_SignalRecover(TRUE);
731
732 #endif /* NO_SIGNAL_TRAPS */
733
734 restore_previous_signals();
735 Asc_SignalDestroy();
736 MEMUSED(0);
737 }
738
739 /*===========================================================================*/
740 /* Registration information */
741
742 #define T(N) {#N,test_ascsignal_##N}
743 static CU_TestInfo ascSignal_test_list[] = {
744 T(basic)
745 , T(pushpopint)
746 , T(pushpop)
747 , T(trap)
748 , T(nestingfpe)
749 , T(nestingint)
750 , T(nestingsegv)
751 , CU_TEST_INFO_NULL
752 };
753 #undef T
754
755 static CU_SuiteInfo suites[] = {
756 {"test_utilities_ascSignal", NULL, NULL, ascSignal_test_list},
757 CU_SUITE_INFO_NULL
758 };
759
760 /*-------------------------------------------------------------------*/
761 CU_ErrorCode test_register_utilities_ascSignal(void)
762 {
763 return CU_register_suites(suites);
764 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22