]>
Commit | Line | Data |
---|---|---|
7b463f34 MT |
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 | ||
3930aa9f MT |
21 | #ifndef PAKFIRE_TESTSUITE_H |
22 | #define PAKFIRE_TESTSUITE_H | |
23 | ||
c81287d2 | 24 | #include <errno.h> |
7b463f34 MT |
25 | #include <stdlib.h> |
26 | #include <stdio.h> | |
10ad6f16 | 27 | #include <string.h> |
7b463f34 | 28 | |
f907e1a2 MT |
29 | #include <pakfire/pakfire.h> |
30 | ||
1413be07 MT |
31 | #define MAX_TESTS 128 |
32 | ||
384d009a | 33 | #define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/" |
4250f96b | 34 | |
35bb0e8f | 35 | struct test { |
7b463f34 | 36 | const char* name; |
35bb0e8f | 37 | int (*func)(const struct test* t); |
b107a018 | 38 | struct pakfire* pakfire; |
35bb0e8f | 39 | }; |
7b463f34 | 40 | |
35bb0e8f MT |
41 | struct testsuite { |
42 | struct test tests[MAX_TESTS]; | |
1413be07 | 43 | size_t num; |
f0303ce8 | 44 | }; |
7b463f34 | 45 | |
35bb0e8f | 46 | extern struct testsuite ts; |
1413be07 | 47 | |
35bb0e8f | 48 | int __testsuite_add_test(const char* name, int (*func)(const struct test* t)); |
b0ead88b | 49 | int testsuite_run(int argc, const char* argv[]); |
7b463f34 | 50 | |
72c9450f | 51 | #define _LOG(prefix, fmt, ...) fprintf(stderr, "TESTS: " prefix fmt, ## __VA_ARGS__); |
7b463f34 MT |
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 | ||
1413be07 | 56 | #define testsuite_add_test(func) __testsuite_add_test(#func, func) |
a0aba665 | 57 | |
9edef5ee MT |
58 | static inline size_t testsuite_array_length(char* array[]) { |
59 | size_t length = 0; | |
60 | ||
61 | for (char** line = array; *line; line++) | |
62 | length++; | |
63 | ||
64 | return length; | |
65 | } | |
66 | ||
67 | static inline int testsuite_compare_string_arrays(char* array1[], char* array2[]) { | |
68 | const size_t length1 = testsuite_array_length(array1); | |
69 | const size_t length2 = testsuite_array_length(array2); | |
70 | ||
71 | // Check if length matches | |
72 | if (length1 != length2) { | |
73 | LOG_ERROR("Arrays differ in length (%zu != %zu)\n", length1, length2); | |
74 | goto ERROR; | |
75 | } | |
76 | ||
77 | char** line1 = array1; | |
78 | char** line2 = array2; | |
79 | size_t num = 0; | |
80 | ||
81 | // Check if all lines match | |
82 | for (; *line1 && *line2; line1++, line2++, num++) { | |
83 | if (strcmp(*line1, *line2) != 0) { | |
84 | LOG_ERROR("Line %zu does not match\n", num); | |
85 | goto ERROR; | |
86 | } | |
87 | } | |
88 | ||
89 | // Success | |
90 | return 0; | |
91 | ||
92 | ERROR: | |
93 | // Dump both arrays | |
94 | for (line1 = array1, line2 = array2, num = 0; *line1 && *line2; line1++, line2++, num++) { | |
95 | printf("Line %zu:\n"); | |
96 | printf(" 1: %s", *line1); | |
97 | printf(" 2: %s", *line2); | |
98 | } | |
99 | ||
100 | return 1; | |
101 | } | |
102 | ||
89ed926c | 103 | #define ASSERT(expr) \ |
7b463f34 MT |
104 | do { \ |
105 | if ((!(expr))) { \ | |
07ba6e56 | 106 | LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \ |
7b463f34 | 107 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ |
e2e52986 | 108 | goto FAIL; \ |
7b463f34 MT |
109 | } \ |
110 | } while (0) | |
3930aa9f | 111 | |
563e9600 MT |
112 | #define ASSERT_NULL(expr) \ |
113 | do { \ | |
114 | if (!((expr) == NULL)) { \ | |
115 | LOG_ERROR("Failed assertion: " #expr " == NULL %s:%d %s\n", \ | |
116 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 117 | goto FAIL; \ |
563e9600 MT |
118 | } \ |
119 | } while (0) | |
120 | ||
9dd11ed0 MT |
121 | #define ASSERT_SUCCESS(expr) \ |
122 | do { \ | |
123 | if ((expr)) { \ | |
5e09732d MT |
124 | LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \ |
125 | #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 126 | goto FAIL; \ |
9dd11ed0 MT |
127 | } \ |
128 | } while (0) | |
129 | ||
49f17efc MT |
130 | #define ASSERT_FAILURE(expr) \ |
131 | do { \ | |
d1936038 | 132 | if (!(expr)) { \ |
49f17efc MT |
133 | LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \ |
134 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 135 | goto FAIL; \ |
49f17efc MT |
136 | } \ |
137 | } while (0) | |
138 | ||
fa5c92a2 MT |
139 | #define ASSERT_TRUE(expr) \ |
140 | do { \ | |
141 | if (!(expr)) { \ | |
142 | LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return true in %s:%d: %m\n", \ | |
143 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
144 | goto FAIL; \ | |
145 | } \ | |
146 | } while (0) | |
147 | ||
148 | #define ASSERT_FALSE(expr) \ | |
149 | do { \ | |
150 | if ((expr)) { \ | |
151 | LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return false in %s:%d: %m\n", \ | |
152 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
153 | goto FAIL; \ | |
154 | } \ | |
155 | } while (0) | |
156 | ||
563e9600 MT |
157 | #define ASSERT_ERRNO(expr, e) \ |
158 | do { \ | |
159 | if (!(expr)) { \ | |
160 | LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \ | |
161 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 162 | goto FAIL; \ |
563e9600 MT |
163 | } \ |
164 | if (errno != e) { \ | |
165 | LOG_ERROR("Failed assertion: " #expr " failed with (%d - %s) " \ | |
166 | "but was expected to fail with (%d - %s) in %s:%d\n", \ | |
167 | errno, strerror(errno), e, strerror(e), __FILE__, __LINE__); \ | |
e2e52986 | 168 | goto FAIL; \ |
563e9600 MT |
169 | } \ |
170 | } while (0) | |
171 | ||
984f134f MT |
172 | #define ASSERT_EQUALS(value1, value2) \ |
173 | do { \ | |
174 | if (value1 != value2) { \ | |
175 | LOG_ERROR("Failed assertion: " #value1 " (%lld) != " #value2 " (%lld) %s:%d %s\n", \ | |
176 | value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
177 | goto FAIL; \ | |
178 | } \ | |
179 | } while (0) | |
180 | ||
05035906 | 181 | #define ASSERT_STRING_EQUALS(value, string) \ |
8d687eaa | 182 | do { \ |
5bcbc1cf | 183 | if (!value) { \ |
cc8ec57f MT |
184 | LOG_ERROR("Failed assertion: %s is NULL, expected %s at %s:%d %s\n", \ |
185 | #value, #string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
5bcbc1cf MT |
186 | goto FAIL; \ |
187 | } \ | |
8d687eaa | 188 | if (strcmp(string, value) != 0) { \ |
cc8ec57f MT |
189 | LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \ |
190 | value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 191 | goto FAIL; \ |
8d687eaa MT |
192 | } \ |
193 | } while (0) | |
194 | ||
9edef5ee MT |
195 | #define ASSERT_STRING_ARRAY_EQUALS(array1, array2) \ |
196 | do { \ | |
197 | if (testsuite_compare_string_arrays(array1, array2)) { \ | |
198 | LOG_ERROR("Failed assertion: " #array1 " != " #array2 " %s:%d %s\n", \ | |
199 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
200 | goto FAIL; \ | |
201 | } \ | |
202 | } while(0) | |
203 | ||
9e1e7985 MT |
204 | #define ASSERT_STRING_NOT_EQUALS(value1, value2) \ |
205 | do { \ | |
206 | if (strcmp(value1, value2) == 0) { \ | |
207 | LOG_ERROR("Failed assertion: " #value1 " (%s) != " #value2 " (%s) %s:%d %s\n", \ | |
208 | value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 209 | goto FAIL; \ |
9e1e7985 MT |
210 | } \ |
211 | } while (0) | |
212 | ||
6dbda957 MT |
213 | #define ASSERT_STRING_STARTSWITH(string, start) \ |
214 | do { \ | |
215 | if (strncmp(string, start, strlen(start)) != 0) { \ | |
216 | LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \ | |
217 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
e2e52986 | 218 | goto FAIL; \ |
6dbda957 MT |
219 | } \ |
220 | } while (0) | |
221 | ||
5e09732d MT |
222 | #define ASSERT_COMPARE(value1, value2, length) \ |
223 | do { \ | |
224 | if (!value1) { \ | |
225 | LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \ | |
226 | #value1, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
227 | goto FAIL; \ | |
228 | } \ | |
229 | if (!value2) { \ | |
230 | LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \ | |
231 | #value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
232 | goto FAIL; \ | |
233 | } \ | |
234 | if (!length) { \ | |
235 | LOG_ERROR("Failed assertion: %s is zero at %s:%d %s\n", \ | |
236 | #length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
237 | goto FAIL; \ | |
238 | } \ | |
239 | if (memcmp(value1, value2, length) != 0) { \ | |
240 | LOG_ERROR("Failed assertion: %s != %s (length = %zu) at %s:%d %s\n", \ | |
241 | #value1, #value2, length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ | |
242 | goto FAIL; \ | |
243 | } \ | |
244 | } while (0) | |
245 | ||
436677a3 | 246 | // Helper functions |
79a0a5cd | 247 | FILE* test_mktemp(char** path); |
22d67bfc | 248 | char* test_mkdtemp(void); |
436677a3 | 249 | |
3930aa9f | 250 | #endif /* PAKFIRE_TESTSUITE_H */ |