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
|
/*
* 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>
#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)
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 */
|