summaryrefslogtreecommitdiff
path: root/include/test/test.h
blob: 306e737ab4589da010c0483933973004069e2777 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * Test macros
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#ifndef OUROBOROS_LIB_TEST_H
#define OUROBOROS_LIB_TEST_H

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/resource.h>

#define TEST_RC_SUCCESS  0
#define TEST_RC_SKIP     1
#define TEST_RC_FAIL    -1

#define TEST_START(...)                                                       \
        do {                                                                  \
                printf("%s", __func__);                                       \
                if (sizeof(#__VA_ARGS__) > 1)                                 \
                        printf(" " __VA_ARGS__);                              \
                printf(" started.\n");                                        \
                fflush(stdout);                                               \
        } while (0)

#define TEST_SUCCESS(...)                                                     \
        do {                                                                  \
                printf("\x1b[32m%s", __func__);                               \
                if (sizeof(#__VA_ARGS__) > 1)                                 \
                        printf(" " __VA_ARGS__);                              \
                printf(" succeeded.\x1b[0m\n");                               \
                fflush(stdout);                                               \
        } while (0)

#define TEST_SKIPPED()                                                        \
        do {                                                                  \
                printf("\x1b[33m%s skipped.\x1b[0m\n", __func__);             \
                fflush(stdout);                                               \
        } while (0)

#define TEST_FAIL(...)                                                        \
        do {                                                                  \
                printf("\x1b[31m%s", __func__);                               \
                if (sizeof(#__VA_ARGS__) > 1)                                 \
                        printf(" " __VA_ARGS__);                              \
                printf(" failed.\x1b[0m\n");                                  \
                fflush(stdout);                                               \
        } while (0)

#define TEST_END(result)                                                      \
        do { if (result == 0) TEST_SUCCESS(); else TEST_FAIL(); } while (0)

static int __attribute__((unused)) test_assert_fail(int(* testfunc)(void))
{
        pid_t pid;
        int   wstatus;

        pid = fork();
        if (pid == -1) {
                printf("Failed to fork: %s.\n", strerror(errno));
                return TEST_RC_FAIL;
        }

        if (pid == 0) {
#ifdef DISABLE_TESTS_CORE_DUMPS
                struct rlimit rl = { .rlim_cur = 0, .rlim_max = 0 };
                setrlimit(RLIMIT_CORE, &rl);
#endif
                return testfunc(); /* should abort */
        }

        waitpid(pid, &wstatus, 0);
#ifdef CONFIG_OUROBOROS_DEBUG
        if (WIFSIGNALED(wstatus) && (wstatus == 134 || wstatus == 6))
                return TEST_RC_SUCCESS;

        printf("Process did not abort, status: %d.\n", wstatus);
#else
        if (WIFEXITED(wstatus) && wstatus == 0)
                return TEST_RC_SUCCESS;

        printf("Process did not exit, status: %d.\n", wstatus);
#endif

        return TEST_RC_FAIL;
}

#endif /* OUROBOROS_LIB_TEST_H */