]> git.ipfire.org Git - people/ms/pakfire.git/blame - tests/testsuite.h
xfer: Receive any error messages from cURL
[people/ms/pakfire.git] / tests / testsuite.h
CommitLineData
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
ab11cb4c 29#include <pakfire/ctx.h>
a2ab26e0 30#include <pakfire/httpclient.h>
f907e1a2
MT
31#include <pakfire/pakfire.h>
32
1413be07
MT
33#define MAX_TESTS 128
34
384d009a 35#define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/"
4250f96b 36
f96ed3ac 37enum test_flags {
a2ab26e0
MT
38 TEST_WANTS_PAKFIRE = (1 << 0),
39 TEST_WANTS_HTTPCLIENT = (1 << 1),
f96ed3ac
MT
40};
41
35bb0e8f 42struct test {
7b463f34 43 const char* name;
35bb0e8f 44 int (*func)(const struct test* t);
c296431f 45 int flags;
ab11cb4c
MT
46
47 // Pakfire Context
48 struct pakfire_ctx* ctx;
49
50 // Pakfire
b107a018 51 struct pakfire* pakfire;
a2ab26e0
MT
52
53 // HTTP Client
54 struct pakfire_httpclient* httpclient;
35bb0e8f 55};
7b463f34 56
35bb0e8f
MT
57struct testsuite {
58 struct test tests[MAX_TESTS];
1413be07 59 size_t num;
f0303ce8 60};
7b463f34 61
35bb0e8f 62extern struct testsuite ts;
1413be07 63
c296431f 64int __testsuite_add_test(const char* name, int (*func)(const struct test* t), int flags);
b0ead88b 65int testsuite_run(int argc, const char* argv[]);
7b463f34 66
72c9450f 67#define _LOG(prefix, fmt, ...) fprintf(stderr, "TESTS: " prefix fmt, ## __VA_ARGS__);
7b463f34
MT
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
c296431f 72#define testsuite_add_test(func, flags) __testsuite_add_test(#func, func, flags)
a0aba665 73
9edef5ee
MT
74static 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
83static 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
108ERROR:
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
89ed926c 119#define ASSERT(expr) \
7b463f34
MT
120 do { \
121 if ((!(expr))) { \
07ba6e56 122 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
7b463f34 123 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
e2e52986 124 goto FAIL; \
7b463f34
MT
125 } \
126 } while (0)
3930aa9f 127
563e9600
MT
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__); \
e2e52986 133 goto FAIL; \
563e9600
MT
134 } \
135 } while (0)
136
9dd11ed0
MT
137#define ASSERT_SUCCESS(expr) \
138 do { \
139 if ((expr)) { \
5e09732d
MT
140 LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \
141 #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
e2e52986 142 goto FAIL; \
9dd11ed0
MT
143 } \
144 } while (0)
145
49f17efc
MT
146#define ASSERT_FAILURE(expr) \
147 do { \
d1936038 148 if (!(expr)) { \
49f17efc
MT
149 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
150 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
e2e52986 151 goto FAIL; \
49f17efc
MT
152 } \
153 } while (0)
154
fa5c92a2
MT
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
563e9600
MT
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__); \
e2e52986 178 goto FAIL; \
563e9600
MT
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__); \
e2e52986 184 goto FAIL; \
563e9600
MT
185 } \
186 } while (0)
187
984f134f
MT
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
05035906 197#define ASSERT_STRING_EQUALS(value, string) \
8d687eaa 198 do { \
5bcbc1cf 199 if (!value) { \
cc8ec57f
MT
200 LOG_ERROR("Failed assertion: %s is NULL, expected %s at %s:%d %s\n", \
201 #value, #string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
5bcbc1cf
MT
202 goto FAIL; \
203 } \
8d687eaa 204 if (strcmp(string, value) != 0) { \
cc8ec57f
MT
205 LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \
206 value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
e2e52986 207 goto FAIL; \
8d687eaa
MT
208 } \
209 } while (0)
210
9edef5ee
MT
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
9e1e7985
MT
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__); \
e2e52986 225 goto FAIL; \
9e1e7985
MT
226 } \
227 } while (0)
228
6dbda957
MT
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__); \
e2e52986 234 goto FAIL; \
6dbda957
MT
235 } \
236 } while (0)
237
5e09732d
MT
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
436677a3 262// Helper functions
79a0a5cd 263FILE* test_mktemp(char** path);
22d67bfc 264char* test_mkdtemp(void);
436677a3 265
3930aa9f 266#endif /* PAKFIRE_TESTSUITE_H */