]> git.ipfire.org Git - people/stevee/pakfire.git/blob - tests/testsuite.h
FHS: Perform world writable check only for regular files
[people/stevee/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(int argc, const char* argv[]);
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 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
103 #define ASSERT(expr) \
104 do { \
105 if ((!(expr))) { \
106 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
107 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
108 goto FAIL; \
109 } \
110 } while (0)
111
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__); \
117 goto FAIL; \
118 } \
119 } while (0)
120
121 #define ASSERT_SUCCESS(expr) \
122 do { \
123 if ((expr)) { \
124 LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \
125 #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
126 goto FAIL; \
127 } \
128 } while (0)
129
130 #define ASSERT_FAILURE(expr) \
131 do { \
132 if (!(expr)) { \
133 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
134 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
135 goto FAIL; \
136 } \
137 } while (0)
138
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
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__); \
162 goto FAIL; \
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__); \
168 goto FAIL; \
169 } \
170 } while (0)
171
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
181 #define ASSERT_STRING_EQUALS(value, string) \
182 do { \
183 if (!value) { \
184 LOG_ERROR("Failed assertion: %s is NULL, expected %s at %s:%d %s\n", \
185 #value, #string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
186 goto FAIL; \
187 } \
188 if (strcmp(string, value) != 0) { \
189 LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \
190 value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
191 goto FAIL; \
192 } \
193 } while (0)
194
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
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__); \
209 goto FAIL; \
210 } \
211 } while (0)
212
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__); \
218 goto FAIL; \
219 } \
220 } while (0)
221
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
246 // Helper functions
247 FILE* test_mktemp(char** path);
248 char* test_mkdtemp(void);
249
250 #endif /* PAKFIRE_TESTSUITE_H */