]> git.ipfire.org Git - pakfire.git/blob - tests/testsuite.h
testsuite: Safety check if value is suddenly NULL
[pakfire.git] / tests / testsuite.h
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2017 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #ifndef PAKFIRE_TESTSUITE_H
22 #define PAKFIRE_TESTSUITE_H
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <pakfire/pakfire.h>
30
31 #define MAX_TESTS 128
32
33 #define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/"
34
35 struct test {
36 const char* name;
37 int (*func)(const struct test* t);
38 struct pakfire* pakfire;
39 };
40
41 struct testsuite {
42 struct test tests[MAX_TESTS];
43 size_t num;
44 };
45
46 extern struct testsuite ts;
47
48 int __testsuite_add_test(const char* name, int (*func)(const struct test* t));
49 int testsuite_run();
50
51 #define _LOG(prefix, fmt, ...) fprintf(stderr, "TESTS: " prefix fmt, ## __VA_ARGS__);
52 #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__);
53 #define LOG_WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__);
54 #define LOG_ERROR(fmt, ...) _LOG("ERROR: ", fmt, ## __VA_ARGS__);
55
56 #define testsuite_add_test(func) __testsuite_add_test(#func, func)
57
58 #define ASSERT(expr) \
59 do { \
60 if ((!(expr))) { \
61 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
62 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
63 goto FAIL; \
64 } \
65 } while (0)
66
67 #define ASSERT_NULL(expr) \
68 do { \
69 if (!((expr) == NULL)) { \
70 LOG_ERROR("Failed assertion: " #expr " == NULL %s:%d %s\n", \
71 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
72 goto FAIL; \
73 } \
74 } while (0)
75
76 #define ASSERT_SUCCESS(expr) \
77 do { \
78 if ((expr)) { \
79 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
80 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
81 goto FAIL; \
82 } \
83 } while (0)
84
85 #define ASSERT_FAILURE(expr) \
86 do { \
87 if (!(expr)) { \
88 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
89 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
90 goto FAIL; \
91 } \
92 } while (0)
93
94 #define ASSERT_TRUE(expr) \
95 do { \
96 if (!(expr)) { \
97 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return true in %s:%d: %m\n", \
98 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
99 goto FAIL; \
100 } \
101 } while (0)
102
103 #define ASSERT_FALSE(expr) \
104 do { \
105 if ((expr)) { \
106 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return false in %s:%d: %m\n", \
107 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
108 goto FAIL; \
109 } \
110 } while (0)
111
112 #define ASSERT_ERRNO(expr, e) \
113 do { \
114 if (!(expr)) { \
115 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
116 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
117 goto FAIL; \
118 } \
119 if (errno != e) { \
120 LOG_ERROR("Failed assertion: " #expr " failed with (%d - %s) " \
121 "but was expected to fail with (%d - %s) in %s:%d\n", \
122 errno, strerror(errno), e, strerror(e), __FILE__, __LINE__); \
123 goto FAIL; \
124 } \
125 } while (0)
126
127 #define ASSERT_STRING_EQUALS(value, string) \
128 do { \
129 if (!value) { \
130 LOG_ERROR("Failed assertion: " #value " is NULL, expected " #string " at %s:%d\n", \
131 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
132 goto FAIL; \
133 } \
134 if (strcmp(string, value) != 0) { \
135 LOG_ERROR("Failed assertion: " #value " (%s) != " #string " %s:%d %s\n", \
136 value, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
137 goto FAIL; \
138 } \
139 } while (0)
140
141 #define ASSERT_STRING_NOT_EQUALS(value1, value2) \
142 do { \
143 if (strcmp(value1, value2) == 0) { \
144 LOG_ERROR("Failed assertion: " #value1 " (%s) != " #value2 " (%s) %s:%d %s\n", \
145 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
146 goto FAIL; \
147 } \
148 } while (0)
149
150 #define ASSERT_STRING_STARTSWITH(string, start) \
151 do { \
152 if (strncmp(string, start, strlen(start)) != 0) { \
153 LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \
154 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
155 goto FAIL; \
156 } \
157 } while (0)
158
159 // Helper functions
160 FILE* test_mktemp(char** path);
161 char* test_mkdtemp();
162
163 #endif /* PAKFIRE_TESTSUITE_H */