From 9116eaa66dd77047772f0f52ea7d0450511fdce9 Mon Sep 17 00:00:00 2001 From: Josh Williams Date: Mon, 23 Dec 2024 23:16:06 -0800 Subject: [PATCH] clean up a bunch of tests --- tests/signals/kill-group.c | 51 ++++++++++++++++++ tests/signals/kill-invalid.c | 19 ++----- tests/signals/kill-permission.c | 25 +++------ tests/signals/kill-self.c | 6 +-- tests/signals/killpg-esrch.c | 15 ++---- tests/signals/killpg-invalid.c | 23 +++----- tests/signals/killpg-self.c | 27 ++++------ tests/signals/killpg0-self.c | 16 +++--- tests/signals/pthread_kill-invalid.c | 11 ++-- tests/signals/pthread_kill0-self.c | 10 ++-- tests/signals/sigaddset-add.c | 15 +++--- tests/signals/sigdelset-delete.c | 15 +++--- tests/signals/sigismember-invalid.c | 14 ++--- tests/signals/sigismember-valid.c | 22 +++++--- tests/signals/sigpause-pause.c | 22 +++----- tests/signals/sigpause-suspend.c | 27 +++------- tests/signals/sigprocmask-11.c | 8 ++- tests/signals/sigprocmask-3.c | 70 ++++++++----------------- tests/signals/sigprocmask-4.c | 2 - tests/signals/sigprocmask-5.c | 1 - tests/signals/sigprocmask-6.c | 1 - tests/signals/sigprocmask-7.c | 1 - tests/signals/sigprocmask-8.c | 36 +++++-------- tests/signals/sigprocmask-9.c | 5 +- tests/signals/sigprocmask-blocksingle.c | 43 ++++++--------- tests/signals/sigrelse-2.c | 9 ++-- tests/signals/sigrelse-3.c | 23 ++++---- 27 files changed, 221 insertions(+), 296 deletions(-) create mode 100644 tests/signals/kill-group.c diff --git a/tests/signals/kill-group.c b/tests/signals/kill-group.c new file mode 100644 index 0000000000..cde477390e --- /dev/null +++ b/tests/signals/kill-group.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include "signals_list.h" +#include "../test_helpers.h" + +void sig_handler(int signo) +{ + printf("Caught signal %d being tested!\n", signo); + printf("Test PASSED\n"); + return; +} + +int kill_group(int signum) +{ + int pgrp; + struct sigaction act; + int status; + + act.sa_handler=sig_handler; + act.sa_flags=0; + status = sigemptyset(&act.sa_mask); + ERROR_IF(sigemptyset, status, == -1); + + status = sigaction(signum, &act, 0); + ERROR_IF(sigaction, staus, == -1); + + staus = getpgrp(); + ERROR_IF(getpgrp, status, == -1); + + status = kill(-pgrp, signum); + ERROR_IF(kill, staus, != 0); + + return EXIT_SUCCESS; +} + +// If pid is negative, but not -1, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the absolute value of pid, and for which the process has permission to send a signal. +int main() +{ + for (unsigned int i = 0; i < sizeof(signals_list)/sizeof(signals_list[0]); i++) + { + int sig = signals_list[i].signal; + if (sig == SIGKILL || sig == SIGSTOP) + { + continue; + } + kill_group(sig); + } + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/tests/signals/kill-invalid.c b/tests/signals/kill-invalid.c index e3e478e01c..5a6ea5f5cf 100644 --- a/tests/signals/kill-invalid.c +++ b/tests/signals/kill-invalid.c @@ -12,18 +12,9 @@ int main() /* * ESRCH */ - if (-1 == kill(999999, 0)) { - if (ESRCH == errno) { - printf("ESRCH error received\n"); - } else { - printf("kill() failed on ESRCH errno not set correctly\n"); - exit(EXIT_FAILURE); - } - } else { - printf("kill() did not fail on ESRCH\n"); - exit(EXIT_FAILURE); - } - - - printf("test passed\n"); + int status; + status = kill(999999, 0); + ERROR_IF(kill, status, != -1); + ERROR_IF(kill, errno, != ESRCH); + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/kill-permission.c b/tests/signals/kill-permission.c index 57346df06f..bad0a625f8 100644 --- a/tests/signals/kill-permission.c +++ b/tests/signals/kill-permission.c @@ -6,22 +6,13 @@ #include #include "../test_helpers.h" -int main() +int main(void) { - setuid(1); /* this is added incase user is root. If user is normal user, then it has no effect on the tests*/ - - if (kill(1, 0) == -1) { - if (EPERM == errno) { - printf("EPERM error received\n"); - } else { - printf("kill() failed on EPERM errno not set correctly\n"); - exit(EXIT_FAILURE); - } - } else { - printf("kill() did not fail on EPERM\n"); - exit(EXIT_FAILURE); - } - - printf("test passed\n"); - return 0; + int status; + // This is added incase user is root. If user is normal user, then it has no effect on the tests + setuid(1000); + status = kill(1, 0); + ERROR_IF(kill, status, != -1); + ERROR_IF(kill, errno, !=EPERM); + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/kill-self.c b/tests/signals/kill-self.c index f208c2f91b..b72d50170d 100644 --- a/tests/signals/kill-self.c +++ b/tests/signals/kill-self.c @@ -34,11 +34,7 @@ int kill_self(int sig) status = kill(getpid(), sig); ERROR_IF(kill, status, != 0); - if (handler_called == 0) - { - printf("Handler was not called\n"); - exit(EXIT_FAILURE); - } + ERROR_IF(kill, handler_called, == 0); return EXIT_SUCCESS; } diff --git a/tests/signals/killpg-esrch.c b/tests/signals/killpg-esrch.c index 32b8cea409..a8a40caa67 100644 --- a/tests/signals/killpg-esrch.c +++ b/tests/signals/killpg-esrch.c @@ -10,16 +10,11 @@ int main() { - if (killpg(999999, 0) != -1) { - printf("killpg did not return -1 even though it was passed an invalid process group id."); - exit(EXIT_FAILURE); - } + int status; + status = killpg(999999, 0); + ERROR_IF(killpg, status, !=-1); - if (errno != ESRCH) { - printf("killpg did not set errno to ESRCH even though it was passed an invalid signal number."); - exit(EXIT_FAILURE); - } + ERROR_IF(killpg, errno, != ESRCH); - printf("Test PASSED\n"); - return 0; + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/killpg-invalid.c b/tests/signals/killpg-invalid.c index 243b140415..e349269174 100644 --- a/tests/signals/killpg-invalid.c +++ b/tests/signals/killpg-invalid.c @@ -11,21 +11,14 @@ int main() { int pgrp; - if ((pgrp = getpgrp()) == -1) { - printf("Could not get process group number\n"); - exit(EXIT_FAILURE); - } + pgrp = getpgrp(); + ERROR_IF(getpgrp, pgrp, == -1); + + int status; + status = killpg(pgrp, -1); + ERROR_IF(killpg, status, != -1); - if (killpg(pgrp, -1) != -1) { - printf("killpg did not return -1 even though it was passed an invalid signal number."); - exit(EXIT_FAILURE); - } + ERROR_IF(killpg, errno, != EINVAL); - if (errno != EINVAL) { - printf("killpg did not set errno to EINVAL even though it was passed an invalid signal number."); - exit(EXIT_FAILURE); - } - - printf("Test PASSED\n"); - return 0; + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/killpg-self.c b/tests/signals/killpg-self.c index e8ce85fa2b..b12c8a8e16 100644 --- a/tests/signals/killpg-self.c +++ b/tests/signals/killpg-self.c @@ -19,26 +19,19 @@ int killpg_test1(int signum) act.sa_handler=sig_handler; act.sa_flags=0; - if (sigemptyset(&act.sa_mask) == -1) { - perror("Error calling sigemptyset\n"); - exit(EXIT_FAILURE); - } - if (sigaction(signum, &act, 0) == -1) { - perror("Error calling sigaction\n"); - exit(EXIT_FAILURE); - } + int status = sigemptyset(&act.sa_mask); + ERROR_IF(sigemptyset, status, == -1); - if ((pgrp = getpgrp()) == -1) { - printf("Could not get process group number\n"); - exit(EXIT_FAILURE); - } + status = sigaction(signum, &act, 0); + ERROR_IF(sigaction, status, == -1); - if (killpg(pgrp, signum) != 0) { - printf("Could not raise signal being tested\n"); - exit(EXIT_FAILURE); - } + pgrp = getpgrp() + ERROR_IF(getpgrp, pgrp, == -1); - return 0; + status = killpg(pgrp, signum); + ERROR_IF(killpg, status, != 0); + + return EXIT_SUCCESS; } int main(){ diff --git a/tests/signals/killpg0-self.c b/tests/signals/killpg0-self.c index 2279c144e5..634011677f 100644 --- a/tests/signals/killpg0-self.c +++ b/tests/signals/killpg0-self.c @@ -9,16 +9,14 @@ int main() { int pgrp; - if ((pgrp = getpgrp()) == -1) { - printf("Could not get process group number\n"); - exit(EXIT_FAILURE); - } + pgrp = getpgrp(); + ERROR_IF(getpgrp, pgrp, == -1); + + int status; + status = killpg(pgrp, 0); + ERROR_IF(killpg, status, != 0); - if (killpg(pgrp, 0) != 0) { - printf("killpg did not return success.\n"); - exit(EXIT_FAILURE); - } printf("Test PASSED\n"); - return 0; + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/pthread_kill-invalid.c b/tests/signals/pthread_kill-invalid.c index 48a57e986b..71fe887e81 100644 --- a/tests/signals/pthread_kill-invalid.c +++ b/tests/signals/pthread_kill-invalid.c @@ -12,10 +12,9 @@ int main() main_thread = pthread_self(); - if (EINVAL != pthread_kill(main_thread, -1)) { - printf("pthread_kill() did not fail on EINVAL\n"); - exit(EXIT_FAILURE); - } - printf("test pass\n"); - return 0; + int status; + status = pthread_kill(main_thread, -1); + ERROR_IF(pthread_kill, status, != EINVAL); + + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/pthread_kill0-self.c b/tests/signals/pthread_kill0-self.c index 45d1ab067a..3fdca12fe1 100644 --- a/tests/signals/pthread_kill0-self.c +++ b/tests/signals/pthread_kill0-self.c @@ -10,11 +10,9 @@ int main() main_thread=pthread_self(); - if (pthread_kill(main_thread, 0) != 0) { - printf("Could not call pthread_kill with sig = 0\n"); - exit(EXIT_FAILURE); - } + int status; + status = pthread_kill(main_thread, 0); + ERROR_IF(pthread_kill, status, != 0); - printf("Test PASSED\n"); - return 0; + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/sigaddset-add.c b/tests/signals/sigaddset-add.c index c5c4044ba0..419cb507f1 100644 --- a/tests/signals/sigaddset-add.c +++ b/tests/signals/sigaddset-add.c @@ -14,16 +14,13 @@ void addset_test(sigset_t *sigset, int signal){ - if (sigismember(sigset, signal) !=0){ - printf("the signal is already in the set, %d\n", signal); - } + int status; + status = sigismember(sigset, signal); + ERROR_IF(sigismember, status, != 0); sigaddset(sigset, signal); - if (sigismember(sigset, signal) ==1){ - printf("the signal was added successfully\n"); - } - if (sigismember(sigset, signal) !=1){ - printf("the signal add failed\n"); - } + + status = sigismember(sigset, signal); + ERROR_IF(sigismember, status, != 1); } diff --git a/tests/signals/sigdelset-delete.c b/tests/signals/sigdelset-delete.c index 72dc04f2a6..6d72896df1 100644 --- a/tests/signals/sigdelset-delete.c +++ b/tests/signals/sigdelset-delete.c @@ -15,16 +15,13 @@ void delset_test(sigset_t *sigset, int signal){ - if (sigismember(sigset, signal) !=1){ - printf("the signal is already out of the set, %d\n", signal); - } + int status; + status = sigismember(sigset, signal); + ERROR_IF(sigismember, status, != 1); + sigdelset(sigset, signal); - if (sigismember(sigset, signal) ==0){ - printf("the signal was deleted successfully\n"); - } - if (sigismember(sigset, signal) !=0){ - printf("the signal remove failed\n"); - } + status = sigismember(sigset, signal); + ERROR_IF(sigismember, status, != 0); } diff --git a/tests/signals/sigismember-invalid.c b/tests/signals/sigismember-invalid.c index 86432bd727..ce26321caa 100644 --- a/tests/signals/sigismember-invalid.c +++ b/tests/signals/sigismember-invalid.c @@ -9,17 +9,13 @@ int main() { sigset_t sigset; + int status; sigfillset(&sigset); - if (sigismember(&sigset, -1)!=-1){ - printf("sigismember didn't return -1"); - exit(EXIT_FAILURE); - } else if (EINVAL != errno) { - printf("errno was not set to EINVAL\n"); - exit(EXIT_FAILURE); - } + status = sigismember(&sigset, -1); + ERROR_IF(sigismember, status, != -1); + ERROR_IF(sigismember, errno, != EINVAL); - printf ("errno set to EINVAL and sigismember returned -1\n"); - return 0; + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/sigismember-valid.c b/tests/signals/sigismember-valid.c index daf39d7b9c..3f81ee50f7 100644 --- a/tests/signals/sigismember-valid.c +++ b/tests/signals/sigismember-valid.c @@ -10,11 +10,21 @@ #include "signals_list.h" #include "../test_helpers.h" -void check(sigset_t set, int signum) { +void check_full(sigset_t set, int signum) { + if (!sigismember(&set, signum)) { + printf("%d was not added to the set", signum); + exit(EXIT_FAILURE); + } + +} + +void check_empty(sigset_t set, int signum) { printf("%d is ", signum); - if (!sigismember(&set, signum)) - printf("not "); - puts("in the set"); + if (sigismember(&set, signum)) { + printf("%d was not removed from the set", signum); + exit(EXIT_FAILURE); + } + } @@ -24,12 +34,12 @@ int main() { sigfillset(&sigset); for (int i=1; i #include #include +#include "../test_helpers.h" int handler_called = 0; int sigprocmask_return_val = 1; /* some value that's not a 1 or 0 */ @@ -26,35 +27,22 @@ int main() act.sa_flags = 0; sigemptyset(&act.sa_mask); - if (sigaction(SIGABRT, &act, 0) == -1) { - perror("Unexpected error while attempting to setup test " - "pre-conditions"); - exit(EXIT_FAILURE); - } + int status; - if (sigprocmask(SIG_SETMASK, &blocked_set1, NULL) == -1) { - perror("Unexpected error while attempting to use sigprocmask.\n"); - exit(EXIT_FAILURE); - } + status = sigaction(SIGABRT, &act, 0); + ERROR_IF(sigaction, status, == -1); - if ((raise(SIGABRT) == -1)) { - perror("Unexpected error while attempting to setup test " - "pre-conditions"); - exit(EXIT_FAILURE); - } + status = sigprocmask(SIG_SETMASK, &blocked_set1, NULL); + ERROR_IF(sigprocmask, status, == -1); + + status = raise(SIGABRT); + ERROR_IF(raise, status, == -1); sigprocmask_return_val = sigprocmask(SIG_UNBLOCK, &blocked_set1, NULL); - if (sigprocmask_return_val != 0) { - perror("Unexpected error while attempting to use sigprocmask.\n"); - exit(EXIT_FAILURE); - } - - if (handler_called != 1) { - perror("Handler wasn't called, implying signal was not delivered.\n"); - exit(EXIT_FAILURE); - } + ERROR_IF(sigprocmask, sigprocmask_return_val, != 0); + + ERROR_IF(raise, handler_called, != 1); - printf("Test PASSED: signal was delivered before the call to sigprocmask returned.\n"); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/sigprocmask-9.c b/tests/signals/sigprocmask-9.c index e34c652d42..7f5cb44956 100644 --- a/tests/signals/sigprocmask-9.c +++ b/tests/signals/sigprocmask-9.c @@ -14,19 +14,16 @@ int main() { sigprocmask_return_val = sigprocmask(SIG_SETMASK, &set1, NULL); sigprocmask(SIG_SETMASK, NULL, &set2); + if (sigismember(&set2, SIGKILL)) { - printf("FAIL: SIGKILL was added to the signal mask\n"); exit(EXIT_FAILURE); } if (sigismember(&set2, SIGSTOP)) { - printf("FAIL: SIGSTOP was added to the signal mask\n"); exit(EXIT_FAILURE); } if (sigprocmask_return_val == -1) { - printf("FAIL: sigprocmask returned -1. System should be able to enforce blocking un-ignorable signals without causing sigprocmask() to return -1.\n"); exit(EXIT_FAILURE); } - printf("Test PASSED\n"); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/sigprocmask-blocksingle.c b/tests/signals/sigprocmask-blocksingle.c index 0ddd466715..8e8d54f7af 100644 --- a/tests/signals/sigprocmask-blocksingle.c +++ b/tests/signals/sigprocmask-blocksingle.c @@ -28,39 +28,28 @@ int sigprocmask_block(int signum) act.sa_flags = 0; sigemptyset(&act.sa_mask); - if (sigaction(signum, &act, 0) == -1) { - perror("Unexpected error while attempting to setup test " - "pre-conditions"); - exit(EXIT_FAILURE); - } + int status; + status = sigaction(signum, &act, 0); + ERROR_IF(sigaction, status, == -1); - if (sigprocmask(SIG_SETMASK, &blocked_set, NULL) == -1) { - perror("Unexpected error while attempting to use sigprocmask.\n"); - exit(EXIT_FAILURE); - } + status = sigprocmask(SIG_SETMASK, &blocked_set, NULL); + ERROR_IF(sigprocmask, status, == -1); - if (raise(signum) == -1) { - perror("Unexpected error while attempting to setup test " - "pre-conditions"); - exit(EXIT_FAILURE); - } + status = raise(signum); + ERROR_IF(raise, status, == -1); - if (handler_called) { - printf("FAIL: Signal was not blocked\n"); - exit(EXIT_FAILURE); - } + ERROR_IF(raise, handler_called, == 1); + // if (handler_called) { + // printf("FAIL: Signal was not blocked\n"); + // exit(EXIT_FAILURE); + // } - if (sigpending(&pending_set) == -1) { - perror("Unexpected error while attempting to use sigpending\n"); - exit(EXIT_FAILURE); - } + status = sigpending(&pending_set); + ERROR_IF(sigpending, status, == -1); - if (sigismember(&pending_set, signum) != 1) { - perror("FAIL: sigismember did not return 1\n"); - exit(EXIT_FAILURE); - } + status = sigismember(&pending_set, signum); + ERROR_IF(sigismember, status, != 1); - printf("Test PASSED: signal was added to the process's signal mask\n"); act.sa_handler = SIG_IGN; sigaction(signum, &act, 0); diff --git a/tests/signals/sigrelse-2.c b/tests/signals/sigrelse-2.c index e7a6922a0f..cfe927836d 100644 --- a/tests/signals/sigrelse-2.c +++ b/tests/signals/sigrelse-2.c @@ -6,11 +6,8 @@ int main() { - - if ((int)sigrelse(SIGABRT) != 0) { - perror("sigrelse failed -- returned -- test aborted"); - exit(EXIT_FAILURE); - } - printf("sigrelse passed\n"); + int status; + status = (int)sigrelse(SIGABRT); + ERROR_IF(sigrelse, status, != 0); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/tests/signals/sigrelse-3.c b/tests/signals/sigrelse-3.c index a0572e259d..863d592ffe 100644 --- a/tests/signals/sigrelse-3.c +++ b/tests/signals/sigrelse-3.c @@ -7,14 +7,19 @@ int main() { - if ((int)sigrelse(100000) == -1) { + int status; + status = (int)sigrelse(100000); + ERROR_IF(sigrelse, status, != -1); + ERROR_IF(sigrelse, errno, != EINVAL); + return EXIT_SUCCESS; + // if ((int)sigrelse(100000) == -1) { - if (EINVAL == errno) { - printf ("errno set to EINVAL\n"); - return EXIT_SUCCESS; - } else { - printf ("errno not set to EINVAL\n"); - exit(EXIT_FAILURE); - } - } + // if (EINVAL == errno) { + // printf ("errno set to EINVAL\n"); + // return EXIT_SUCCESS; + // } else { + // printf ("errno not set to EINVAL\n"); + // exit(EXIT_FAILURE); + // } + // } } \ No newline at end of file