summaryrefslogtreecommitdiff
path: root/include/ouroboros/test.h
blob: 096e145ce27e9c5aba0cdcd2943f3aee63c4f468 (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
/*
 * 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>

#define TEST_START()                                                          \
        do {                                                                  \
                printf("%s started.\n", __func__);                            \
                fflush(stdout);                                               \
        } while (0)
#define TEST_SUCCESS()                                                        \
        do {                                                                  \
                printf("%s succeeded.\n", __func__);                          \
                fflush(stdout);                                               \
        } while (0)

#define TEST_FAIL()                                                           \
        do {                                                                  \
                printf("%s failed.\n", __func__);                             \
                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 -1;
        }

        if (pid == 0)
                return testfunc(); /* should abort */

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

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

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

        return -1;
}

#endif /* OUROBOROS_LIB_TEST_H */