Skip to content

Commit 0e7fcaa

Browse files
committed
tests: add wait_signal test in process-api-tests.c
Test for reported signal in status by mm_wait_process() in case of the process is killed. Change-Id: I565d5bd4df2ade370077f5027dab885f1d2ba54f
1 parent b3b8a65 commit 0e7fcaa

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

tests/child-proc.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,32 @@
66
#endif
77

88
#include <mmsysio.h>
9+
#include <signal.h>
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <string.h>
1213

14+
#ifdef _WIN32
15+
#include <windows.h>
16+
#endif
17+
18+
#if defined(_MSC_VER)
19+
# define raise_sigill __ud2
20+
#else
21+
# define raise_sigill __builtin_trap
22+
#endif
23+
24+
25+
static
26+
void raise_sigfpe(void)
27+
{
28+
#ifdef _WIN32
29+
RaiseException(EXCEPTION_FLT_DIVIDE_BY_ZERO, 0, 0, NULL);
30+
#else
31+
raise(SIGFPE);
32+
#endif
33+
}
34+
1335

1436
static
1537
int check_open_files(int argc, char* argv[])
@@ -33,6 +55,23 @@ int check_open_files(int argc, char* argv[])
3355
}
3456

3557

58+
static
59+
int check_signal(int signum)
60+
{
61+
union {int* iptr; intptr_t v;} val = {.v = 0};
62+
63+
switch (signum) {
64+
case SIGABRT: abort();
65+
case SIGSEGV: printf("%i", *val.iptr); break; // Must raise segfault
66+
case SIGFPE: raise_sigfpe(); break;
67+
case SIGILL: raise_sigill(); break;
68+
default: raise(signum);
69+
}
70+
71+
return EXIT_FAILURE;
72+
}
73+
74+
3675
int main(int argc, char* argv[])
3776
{
3877
if (argc < 2) {
@@ -46,6 +85,9 @@ int main(int argc, char* argv[])
4685
if (strcmp(argv[1], "check-exit") == 0)
4786
return atoi(argv[2]);
4887

88+
if (strcmp(argv[1], "check-signal") == 0)
89+
return check_signal(atoi(argv[2]));
90+
4991
fprintf(stderr, "child-proc: invalid argument: %s\n", argv[1]);
5092
return EXIT_FAILURE;
5193
}

tests/process-api-tests.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <check.h>
99

10+
#include <signal.h>
1011
#include <stdio.h>
1112
#include <strings.h>
1213

@@ -520,6 +521,37 @@ START_TEST(wait_exit)
520521
END_TEST
521522

522523

524+
static const int wait_signal_cases[] = {
525+
SIGSEGV,
526+
SIGILL,
527+
SIGFPE,
528+
#if !defined(_WIN32)
529+
SIGTERM,
530+
SIGABRT,
531+
SIGINT,
532+
#endif
533+
};
534+
535+
START_TEST(wait_signal)
536+
{
537+
int status;
538+
mm_pid_t pid;
539+
char arg[32];
540+
int signum = wait_signal_cases[_i];
541+
char* cmd[] = {CHILDPROC_BINPATH, "check-signal", arg, NULL};
542+
543+
sprintf(arg, "%i", signum);
544+
545+
ck_assert(mm_spawn(&pid, cmd[0], 0, NULL, 0, cmd, NULL) == 0);
546+
ck_assert(mm_wait_process(pid, &status) == 0);
547+
548+
// Check process is exited and exit code is the one expected
549+
ck_assert(status & MM_WSTATUS_SIGNALED);
550+
ck_assert_int_eq(status & MM_WSTATUS_CODEMASK, signum);
551+
}
552+
END_TEST
553+
554+
523555
/**************************************************************************
524556
* *
525557
* process test suite setup *
@@ -542,6 +574,7 @@ TCase* create_process_tcase(void)
542574
tcase_add_loop_test(tc, spawn_invalid_args, 0, MM_NELEM(inval_cases));
543575
tcase_add_test(tc, wait_twice);
544576
tcase_add_loop_test(tc, wait_exit, 0, MM_NELEM(exit_code_cases));
577+
tcase_add_loop_test(tc, wait_signal, 0, MM_NELEM(wait_signal_cases));
545578

546579
#ifndef _WIN32
547580
tcase_add_loop_test(tc, spawn_error_limits, 0, NUM_ERRLIMITS_CASES);

0 commit comments

Comments
 (0)