]> git.ipfire.org Git - pakfire.git/blob - tests/testsuite.h
mount: No longer create /dev/console in the jail
[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/ctx.h>
30 #include <pakfire/httpclient.h>
31 #include <pakfire/pakfire.h>
32
33 #define MAX_TESTS 128
34
35 #define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/"
36
37 enum test_flags {
38 TEST_WANTS_PAKFIRE = (1 << 0),
39 TEST_WANTS_HTTPCLIENT = (1 << 1),
40 };
41
42 struct test {
43 const char* name;
44 int (*func)(const struct test* t);
45 int flags;
46
47 // Pakfire Context
48 struct pakfire_ctx* ctx;
49
50 // Pakfire
51 struct pakfire* pakfire;
52
53 // HTTP Client
54 struct pakfire_httpclient* httpclient;
55 };
56
57 struct testsuite {
58 struct test tests[MAX_TESTS];
59 size_t num;
60 };
61
62 extern struct testsuite ts;
63
64 int __testsuite_add_test(const char* name, int (*func)(const struct test* t), int flags);
65 int testsuite_run(int argc, const char* argv[]);
66
67 #define _LOG(prefix, fmt, ...) fprintf(stderr, "TESTS: " prefix fmt, ## __VA_ARGS__);
68 #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__);
69 #define LOG_WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__);
70 #define LOG_ERROR(fmt, ...) _LOG("ERROR: ", fmt, ## __VA_ARGS__);
71
72 #define testsuite_add_test(func, flags) __testsuite_add_test(#func, func, flags)
73
74 static inline size_t testsuite_array_length(char* array[]) {
75 size_t length = 0;
76
77 for (char** line = array; *line; line++)
78 length++;
79
80 return length;
81 }
82
83 static inline int testsuite_compare_string_arrays(char* array1[], char* array2[]) {
84 const size_t length1 = testsuite_array_length(array1);
85 const size_t length2 = testsuite_array_length(array2);
86
87 // Check if length matches
88 if (length1 != length2) {
89 LOG_ERROR("Arrays differ in length (%zu != %zu)\n", length1, length2);
90 goto ERROR;
91 }
92
93 char** line1 = array1;
94 char** line2 = array2;
95 size_t num = 0;
96
97 // Check if all lines match
98 for (; *line1 && *line2; line1++, line2++, num++) {
99 if (strcmp(*line1, *line2) != 0) {
100 LOG_ERROR("Line %zu does not match\n", num);
101 goto ERROR;
102 }
103 }
104
105 // Success
106 return 0;
107
108 ERROR:
109 // Dump both arrays
110 for (line1 = array1, line2 = array2, num = 0; *line1 && *line2; line1++, line2++, num++) {
111 printf("Line %zu:\n");
112 printf(" 1: %s", *line1);
113 printf(" 2: %s", *line2);
114 }
115
116 return 1;
117 }
118
119 #define ASSERT(expr) \
120 do { \
121 if ((!(expr))) { \
122 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
123 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
124 goto FAIL; \
125 } \
126 } while (0)
127
128 #define ASSERT_NULL(expr) \
129 do { \
130 if (!((expr) == NULL)) { \
131 LOG_ERROR("Failed assertion: " #expr " == NULL %s:%d %s\n", \
132 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
133 goto FAIL; \
134 } \
135 } while (0)
136
137 #define ASSERT_SUCCESS(expr) \
138 do { \
139 if ((expr)) { \
140 LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \
141 #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
142 goto FAIL; \
143 } \
144 } while (0)
145
146 #define ASSERT_FAILURE(expr) \
147 do { \
148 if (!(expr)) { \
149 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
150 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
151 goto FAIL; \
152 } \
153 } while (0)
154
155 #define ASSERT_TRUE(expr) \
156 do { \
157 if (!(expr)) { \
158 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return true in %s:%d: %m\n", \
159 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
160 goto FAIL; \
161 } \
162 } while (0)
163
164 #define ASSERT_FALSE(expr) \
165 do { \
166 if ((expr)) { \
167 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return false in %s:%d: %m\n", \
168 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
169 goto FAIL; \
170 } \
171 } while (0)
172
173 #define ASSERT_ERRNO(expr, e) \
174 do { \
175 if (!(expr)) { \
176 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
177 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
178 goto FAIL; \
179 } \
180 if (errno != e) { \
181 LOG_ERROR("Failed assertion: " #expr " failed with (%d - %s) " \
182 "but was expected to fail with (%d - %s) in %s:%d\n", \
183 errno, strerror(errno), e, strerror(e), __FILE__, __LINE__); \
184 goto FAIL; \
185 } \
186 } while (0)
187
188 #define ASSERT_EQUALS(value1, value2) \
189 do { \
190 if (value1 != value2) { \
191 LOG_ERROR("Failed assertion: " #value1 " (%lld) != " #value2 " (%lld) %s:%d %s\n", \
192 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
193 goto FAIL; \
194 } \
195 } while (0)
196
197 #define ASSERT_STRING_EQUALS(value, string) \
198 do { \
199 if (!value) { \
200 LOG_ERROR("Failed assertion: %s is NULL, expected %s at %s:%d %s\n", \
201 #value, #string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
202 goto FAIL; \
203 } \
204 if (strcmp(string, value) != 0) { \
205 LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \
206 value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
207 goto FAIL; \
208 } \
209 } while (0)
210
211 #define ASSERT_STRING_ARRAY_EQUALS(array1, array2) \
212 do { \
213 if (testsuite_compare_string_arrays(array1, array2)) { \
214 LOG_ERROR("Failed assertion: " #array1 " != " #array2 " %s:%d %s\n", \
215 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
216 goto FAIL; \
217 } \
218 } while(0)
219
220 #define ASSERT_STRING_NOT_EQUALS(value1, value2) \
221 do { \
222 if (strcmp(value1, value2) == 0) { \
223 LOG_ERROR("Failed assertion: " #value1 " (%s) != " #value2 " (%s) %s:%d %s\n", \
224 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
225 goto FAIL; \
226 } \
227 } while (0)
228
229 #define ASSERT_STRING_STARTSWITH(string, start) \
230 do { \
231 if (strncmp(string, start, strlen(start)) != 0) { \
232 LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \
233 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
234 goto FAIL; \
235 } \
236 } while (0)
237
238 #define ASSERT_COMPARE(value1, value2, length) \
239 do { \
240 if (!value1) { \
241 LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
242 #value1, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
243 goto FAIL; \
244 } \
245 if (!value2) { \
246 LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
247 #value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
248 goto FAIL; \
249 } \
250 if (!length) { \
251 LOG_ERROR("Failed assertion: %s is zero at %s:%d %s\n", \
252 #length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
253 goto FAIL; \
254 } \
255 if (memcmp(value1, value2, length) != 0) { \
256 LOG_ERROR("Failed assertion: %s != %s (length = %zu) at %s:%d %s\n", \
257 #value1, #value2, length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
258 goto FAIL; \
259 } \
260 } while (0)
261
262 // Helper functions
263 FILE* test_mktemp(char** path);
264 char* test_mkdtemp(void);
265
266 #endif /* PAKFIRE_TESTSUITE_H */