summaryrefslogtreecommitdiffstats
path: root/test/main.c
blob: 0e14d4404007a6343b3c32dfaf8e58c9979a41a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "ts.h"

#include "main_generated.c"

int main(void)
{
    struct sigaction sa = {0};
    sa.sa_handler = SIG_IGN;
    sigaction(SIGALRM, &sa, NULL);

    size_t passed = 0;
    size_t failed = 0;
    size_t crashed = 0;
    size_t skipped = 0;

    for (size_t i = 0; i < NUM_TESTS; i++)
    {
        pid_t pid = fork();
        if (pid < 0) {
            fprintf(stderr, "  SKIP %s (fork failed: %s)\n", tests[i].name, strerror(errno));
            skipped++;
            continue;
        }
        if (pid == 0) {
            bool ok = tests[i].func();
            _exit(ok ? 0 : 1);
        }
        int status = 0;
        alarm(30);
        pid_t result = waitpid(pid, &status, 0);
        alarm(0);
        if (result < 0) {
            if (errno == EINTR) {
                kill(pid, SIGKILL);
                if (waitpid(pid, &status, 0) < 0) {
                    fprintf(stderr, "  CRASH %s (waitpid after SIGKILL failed: %s)\n", tests[i].name, strerror(errno));
                }
                fprintf(stderr, "  CRASH %s (timeout)\n", tests[i].name);
                crashed++;
            } else {
                fprintf(stderr, "  CRASH %s (waitpid failed: %s)\n", tests[i].name, strerror(errno));
                crashed++;
            }
            continue;
        }
        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
            printf("  PASS %s\n", tests[i].name);
            passed++;
        } else if (WIFEXITED(status)) {
            fprintf(stderr, "  FAIL %s\n", tests[i].name);
            failed++;
        } else if (WIFSIGNALED(status)) {
            fprintf(stderr, "  CRASH %s (signal %d)\n", tests[i].name, WTERMSIG(status));
            crashed++;
        } else {
            fprintf(stderr, "  CRASH %s\n", tests[i].name);
            crashed++;
        }
    }

    printf("\n--- %zu tests: %zu passed, %zu failed, %zu crashed, %zu skipped ---\n",
            NUM_TESTS, passed, failed, crashed, skipped);

    return (failed > 0 || crashed > 0) ? 1 : 0;
}