Home | History | Annotate | Download | only in raise
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  *  General API test of the raise() function.
      9  *  Test calling raise with the following values:
     10  *  - MINIMUM valid (in this case SIGABRT signal)
     11  *  - MAXIMUM valid (in this case SIGXFSZ signal)
     12  *  Item with POSIX default action as T - SIGALRM
     13  *  Item with POSIX default action as A - (done - SIGABRT)
     14  *  Item with POSIX default action as I - SIGCHLD
     15  *  Item with POSIX default action as S - SIGTSTP
     16  *  Item with POSIX default action as C - SIGCONT
     17  *
     18  *  Error conditions:
     19  *  Boundary values for int
     20  *  MIN INT = INT32_MIN
     21  *  MAX INT = INT32_MAX
     22  *  MIN INT - 1 = 2147483647 (this is what gcc will set to)
     23  *  MAX INT + 1 = -2147483647 (this is what gcc will set to)
     24  *  unassigned value = -1073743192 (ex. of what gcc will set to)
     25  *  unassigned value = 1073743192 (ex. of what gcc will set to)
     26  *
     27  *  Steps:
     28  *  1) Set up signal handlers for all valid signals
     29  *  2) Call all valid signals and verify they are received.
     30  *  3) Call all invalid signals and verify raise() returns 0 and sets
     31  *     errno to EINVAL.
     32  *
     33  *  Note:  This test case maps to multiple assertions: 1, 2 (implicitly),
     34  *  5, 6, 7, so it was given a large number as a file name.
     35  */
     36 
     37 #include <signal.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <stdint.h>
     41 #include <errno.h>
     42 #include "posixtest.h"
     43 
     44 #define NUMVALIDTESTS 6
     45 #define NUMINVALIDTESTS 6
     46 static int valid_tests[NUMVALIDTESTS] = {
     47 	SIGABRT, SIGXFSZ, SIGALRM, SIGCHLD, SIGTSTP, SIGCONT
     48 };
     49 
     50 static int invalid_tests[NUMINVALIDTESTS] = {
     51 	INT32_MIN, INT32_MAX, 2147483647, -2147483647, -1073743192, 1073743192
     52 };
     53 
     54 void handler(int signo)
     55 {
     56 	printf("Caught signal being tested!\n");
     57 }
     58 
     59 int main(void)
     60 {
     61 	int i;
     62 	int failure = 0;
     63 	struct sigaction act;
     64 
     65 	act.sa_handler = handler;
     66 	act.sa_flags = 0;
     67 	if (sigemptyset(&act.sa_mask) == -1) {
     68 		perror("Error calling sigemptyset\n");
     69 		return PTS_UNRESOLVED;
     70 	}
     71 
     72 	for (i = 0; i < NUMVALIDTESTS; i++) {
     73 		if (sigaction(valid_tests[i], &act, 0) == -1) {
     74 			perror("Error calling sigaction\n");
     75 			return PTS_UNRESOLVED;
     76 		}
     77 	}
     78 
     79 	for (i = 0; i < NUMVALIDTESTS; i++) {
     80 		if (raise(valid_tests[i]) != 0) {
     81 			printf("Could not raise signal being tested\n");
     82 			failure = 1;
     83 		}
     84 	}
     85 
     86 	for (i = 0; i < NUMINVALIDTESTS; i++) {
     87 		if (raise(invalid_tests[i]) == 0) {
     88 			printf("Received success raising an invalid signal\n");
     89 			failure = 1;
     90 		} else {
     91 			if (EINVAL == errno) {
     92 				printf("errno correctly set\n");
     93 			} else {
     94 				printf("errno not correctly set\n");
     95 				failure = 1;
     96 			}
     97 		}
     98 	}
     99 
    100 	if (failure) {
    101 		printf("At least one test FAILED -- see above\n");
    102 		return PTS_FAIL;
    103 	} else {
    104 		printf("All tests PASSED\n");
    105 		return PTS_PASS;
    106 	}
    107 }
    108