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

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