Skip to content

Commit

Permalink
tests: add wait_signal test in process-api-tests.c
Browse files Browse the repository at this point in the history
Test for reported signal in status by mm_wait_process() in case of the
process is killed.

Change-Id: I565d5bd4df2ade370077f5027dab885f1d2ba54f
  • Loading branch information
nbourdau committed Jan 7, 2023
1 parent b3b8a65 commit 0e7fcaa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/child-proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@
#endif

#include <mmsysio.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#endif

#if defined(_MSC_VER)
# define raise_sigill __ud2
#else
# define raise_sigill __builtin_trap
#endif


static
void raise_sigfpe(void)
{
#ifdef _WIN32
RaiseException(EXCEPTION_FLT_DIVIDE_BY_ZERO, 0, 0, NULL);
#else
raise(SIGFPE);
#endif
}


static
int check_open_files(int argc, char* argv[])
Expand All @@ -33,6 +55,23 @@ int check_open_files(int argc, char* argv[])
}


static
int check_signal(int signum)
{
union {int* iptr; intptr_t v;} val = {.v = 0};

switch (signum) {
case SIGABRT: abort();
case SIGSEGV: printf("%i", *val.iptr); break; // Must raise segfault
case SIGFPE: raise_sigfpe(); break;
case SIGILL: raise_sigill(); break;
default: raise(signum);
}

return EXIT_FAILURE;
}


int main(int argc, char* argv[])
{
if (argc < 2) {
Expand All @@ -46,6 +85,9 @@ int main(int argc, char* argv[])
if (strcmp(argv[1], "check-exit") == 0)
return atoi(argv[2]);

if (strcmp(argv[1], "check-signal") == 0)
return check_signal(atoi(argv[2]));

fprintf(stderr, "child-proc: invalid argument: %s\n", argv[1]);
return EXIT_FAILURE;
}
33 changes: 33 additions & 0 deletions tests/process-api-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <check.h>

#include <signal.h>
#include <stdio.h>
#include <strings.h>

Expand Down Expand Up @@ -520,6 +521,37 @@ START_TEST(wait_exit)
END_TEST


static const int wait_signal_cases[] = {
SIGSEGV,
SIGILL,
SIGFPE,
#if !defined(_WIN32)
SIGTERM,
SIGABRT,
SIGINT,
#endif
};

START_TEST(wait_signal)
{
int status;
mm_pid_t pid;
char arg[32];
int signum = wait_signal_cases[_i];
char* cmd[] = {CHILDPROC_BINPATH, "check-signal", arg, NULL};

sprintf(arg, "%i", signum);

ck_assert(mm_spawn(&pid, cmd[0], 0, NULL, 0, cmd, NULL) == 0);
ck_assert(mm_wait_process(pid, &status) == 0);

// Check process is exited and exit code is the one expected
ck_assert(status & MM_WSTATUS_SIGNALED);
ck_assert_int_eq(status & MM_WSTATUS_CODEMASK, signum);
}
END_TEST


/**************************************************************************
* *
* process test suite setup *
Expand All @@ -542,6 +574,7 @@ TCase* create_process_tcase(void)
tcase_add_loop_test(tc, spawn_invalid_args, 0, MM_NELEM(inval_cases));
tcase_add_test(tc, wait_twice);
tcase_add_loop_test(tc, wait_exit, 0, MM_NELEM(exit_code_cases));
tcase_add_loop_test(tc, wait_signal, 0, MM_NELEM(wait_signal_cases));

#ifndef _WIN32
tcase_add_loop_test(tc, spawn_error_limits, 0, NUM_ERRLIMITS_CASES);
Expand Down

0 comments on commit 0e7fcaa

Please sign in to comment.