forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.h
433 lines (392 loc) · 37.5 KB
/
tests.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* @file
*
* @brief Some common functions in use for testing framework.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef TESTS_H
#define TESTS_H
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <kdb.h>
#include <kdbhelper.h>
#include <kdbmacros.h>
#define BUFFER_LENGTH 4096
#define ELEKTRA_TEST_ROOT "/tests/ckdb/"
#ifdef __cplusplus
extern "C" {
#endif
extern int nbError;
extern int nbTest;
extern uid_t nbUid;
extern gid_t nbGid;
extern char * tempHome;
extern int tempHomeLen;
extern char * tmpfilename;
int init (int argc, char ** argv);
#ifndef __cplusplus
#define print_result(name) \
{ \
printf ("\n" name " Results: %d Test%s done — %d error%s.\n", nbTest, nbTest == 1 ? "" : "s", nbError, \
nbError == 1 ? "" : "s"); \
}
#define warn_if_fail(expression, message) \
{ \
nbTest++; \
if (!(expression)) \
{ \
printf ("%s:%d: warn in %s: %s\n", __FILE__, __LINE__, __func__, message); \
} \
}
#define yield_error(message) \
{ \
nbError++; \
printf ("%s:%d: error in %s: %s\n", __FILE__, __LINE__, __func__, message); \
}
#define yield_error_fmt(message_fmt, ...) \
{ \
nbError++; \
printf ("%s:%d: error in %s: " message_fmt "\n", __FILE__, __LINE__, __func__, __VA_ARGS__); \
}
#define succeed_if(expression, message) \
{ \
nbTest++; \
if (!(expression)) \
{ \
yield_error (message); \
} \
}
#define succeed_if_fmt(expression, message_fmt, ...) \
{ \
nbTest++; \
if (!(expression)) \
{ \
yield_error_fmt (message_fmt, __VA_ARGS__); \
} \
}
#define exit_if_fail(expression, message) \
{ \
nbTest++; \
if (!(expression)) \
{ \
printf ("%s:%d: fatal in %s: %s\n", __FILE__, __LINE__, __func__, message); \
exit (1); \
} \
}
#define compare_key_name(pk1, pk2) \
{ \
Key * nmmk1 = pk1; \
Key * nmmk2 = pk2; \
nbTest++; \
if (strcmp (keyName (nmmk1), keyName (nmmk2)) != 0) \
{ \
char errorMsg[BUFFER_LENGTH]; \
\
strcpy (errorMsg, "key name \""); \
strcat (errorMsg, keyName (nmmk1)); \
strcat (errorMsg, "\" is not equal to \""); \
strcat (errorMsg, keyName (nmmk2)); \
strcat (errorMsg, "\""); \
\
yield_error (errorMsg); \
} \
}
#define compare_key_value(pk1, pk2) \
{ \
Key * vmmk1 = pk1; \
Key * vmmk2 = pk2; \
nbTest++; \
int vmmk1binary = keyIsBinary (vmmk1); \
int vmmk2binary = keyIsBinary (vmmk2); \
if (vmmk1binary && vmmk2binary) \
{ \
compare_key_binary (vmmk1, vmmk2); \
} \
else if (!vmmk1binary && !vmmk2binary) \
{ \
compare_key_string (vmmk1, vmmk2); \
} \
else \
{ \
char errorMsg[BUFFER_LENGTH]; \
\
strcpy (errorMsg, "key \""); \
strcat (errorMsg, keyName (vmmk1binary ? vmmk1 : vmmk2)); \
strcat (errorMsg, "\" is binary, but key \""); \
strcat (errorMsg, keyName (vmmk1binary ? vmmk2 : vmmk1)); \
strcat (errorMsg, "\" is not"); \
\
yield_error (errorMsg); \
} \
}
#define compare_key_string(pk1, pk2) \
{ \
Key * smmk1 = pk1; \
Key * smmk2 = pk2; \
nbTest++; \
if (strcmp (keyString (smmk1), keyString (smmk2)) != 0) \
{ \
char errorMsg[BUFFER_LENGTH]; \
\
strcpy (errorMsg, "key value \""); \
strcat (errorMsg, keyString (smmk1)); \
strcat (errorMsg, "\" is not equal to \""); \
strcat (errorMsg, keyString (smmk2)); \
strcat (errorMsg, "\""); \
\
yield_error (errorMsg); \
} \
}
#define compare_key_binary(pk1, pk2) \
{ \
Key * bmmk1 = pk1; \
Key * bmmk2 = pk2; \
nbTest++; \
size_t bmmk1size = keyGetValueSize (pk1); \
size_t bmmk2size = keyGetValueSize (pk2); \
if (bmmk1size != bmmk2size) \
{ \
char errorMsg[BUFFER_LENGTH]; \
\
strcpy (errorMsg, "key value sizes of \""); \
strcat (errorMsg, keyName (bmmk1)); \
strcat (errorMsg, "\" and \""); \
strcat (errorMsg, keyName (bmmk2)); \
strcat (errorMsg, "\" don't match"); \
\
yield_error (errorMsg); \
} \
else if (bmmk1size != 0) \
{ \
void * bmmk1buf = elektraMalloc (bmmk1size); \
void * bmmk2buf = elektraMalloc (bmmk2size); \
\
keyGetBinary (bmmk1, bmmk1buf, bmmk1size); \
keyGetBinary (bmmk2, bmmk2buf, bmmk2size); \
\
if (memcmp (bmmk1buf, bmmk2buf, bmmk1size) != 0) \
{ \
char errorMsg[BUFFER_LENGTH]; \
\
strcpy (errorMsg, "binary values of key \""); \
strcat (errorMsg, keyName (bmmk1)); \
strcat (errorMsg, "\" and \""); \
strcat (errorMsg, keyName (bmmk2)); \
strcat (errorMsg, "\" don't match"); \
\
yield_error (errorMsg); \
} \
elektraFree (bmmk1buf); \
elektraFree (bmmk2buf); \
} \
}
// GCC diagnostic not allowed inside functions
// does not work with 4.4
// 4.6 and 4.7 as in debian work with it, that are:
// 4.6.3
// 4.7.2
//(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52116)
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 2))) || \
(__GNUC__ == 4 && (__GNUC_MINOR__ > 7 || (__GNUC_MINOR__ == 7 && __GNUC_PATCHLEVEL__ > 1))))
#define ELEKTRA_PRAGMA(x) _Pragma (ELEKTRA_PRAGMA_STR (x))
#elif defined(__clang_major__) && (__clang_major__ > 3 || (__clang_major__ == 3 && (__clang_minor__ >= 3)))
// might also be supported in earlier versions of clang, but no documentation was found
#define ELEKTRA_PRAGMA(x) _Pragma (ELEKTRA_PRAGMA_STR (x))
#else
#define ELEKTRA_PRAGMA(x)
#endif
#define ELEKTRA_PRAGMA_STR(x) #x
#define ELEKTRA_DIAG_STORE ELEKTRA_PRAGMA (GCC diagnostic push)
#define ELEKTRA_DIAG_OFF_STR(x) ELEKTRA_PRAGMA (GCC diagnostic ignored x)
#define ELEKTRA_DIAG_OFF(x) ELEKTRA_DIAG_OFF_STR (ELEKTRA_PRAGMA_STR (x))
#define ELEKTRA_DIAG_RESTORE ELEKTRA_PRAGMA (GCC diagnostic pop)
#define succeed_if_same_string(ps1, ps2) \
{ \
nbTest++; \
const char * s1 = ps1; \
const char * s2 = ps2; \
ELEKTRA_DIAG_STORE \
ELEKTRA_DIAG_OFF (-Waddress) \
if (!s1) \
{ \
yield_error ("left hand side is null pointer") \
} \
else if (!s2) \
{ \
yield_error ("right hand side is null pointer") \
} \
else \
{ \
succeed_if_fmt (strcmp (s1, s2) == 0, "string \"%s\" is not equal to \"%s\"\n\tcompared: %s and %s", s1, s2, #ps1, \
#ps2) \
} \
ELEKTRA_DIAG_RESTORE \
}
// only works with types convertible to int
#define succeed_if_same_int(s1, s2) \
{ \
nbTest++; \
int s1 = ps1; \
int s2 = ps2; \
if (s1 != s2) \
{ \
char errorMsg[BUFFER_LENGTH]; \
\
snprintf (errorMsg, BUFFER_LENGTH, "int %d is not equal to %d", s1, s2); \
\
yield_error (errorMsg); \
} \
}
/**
* Checks if two keys are equal.
*
* Warning: messes up internal cursor
*
*/
#define compare_key(pk1, pk2) \
do \
{ \
nbTest++; \
Key * mmk1 = (Key *) pk1; \
Key * mmk2 = (Key *) pk2; \
if (mmk1 == mmk2) \
{ \
break; /* same pointer */ \
} \
else if (mmk1 == NULL) \
{ \
yield_error ("first key is null, but second is not") \
} \
else if (mmk2 == NULL) \
{ \
yield_error ("second key is null, but first is not") \
} \
else if (mmk1 != mmk2) \
{ \
compare_key_name (mmk1, mmk2); \
\
compare_key_value (mmk1, mmk2); \
\
const Key * meta; \
keyRewindMeta (mmk1); \
keyRewindMeta (mmk2); \
while ((meta = keyNextMeta (mmk1)) != 0) \
{ \
const Key * const metaCmp = keyNextMeta (mmk2); \
if (metaCmp == 0) \
{ \
nbError++; \
printf ("%s:%d: error in %s: Compare key \"%s\" with \"%s\" failed, did not find corresponding " \
"metakey %s (k1 > k2)\n", \
__FILE__, __LINE__, __func__, ELEKTRA_QUOTE (mmk1), ELEKTRA_QUOTE (mmk2), keyName (meta)); \
break; \
} \
if (strcmp (keyName (meta), keyName (metaCmp)) != 0) \
{ \
nbError++; \
printf ("%s:%d: error in %s: Name of meta key \"%s\" ≠ \"%s\"\n", __FILE__, __LINE__, __func__, \
keyName (meta), keyName (metaCmp)); \
break; \
} \
if (strcmp (keyString (meta), keyString (metaCmp)) != 0) \
{ \
nbError++; \
printf ("%s:%d: error in %s: Comparison of the keys with name \"%s\" failed. The value of the " \
"metakey \"%s\" is not equal: \"%s\" ≠ \"%s\"\n", \
__FILE__, __LINE__, __func__, keyName (mmk1), keyName (meta), keyString (meta), \
keyString (metaCmp)); \
break; \
} \
} \
\
const Key * const metaCmp = keyNextMeta (mmk2); \
if (metaCmp != 0) \
{ \
nbError++; \
printf ("%s:%d: error in %s: Compare key \"%s\" with \"%s\" failed, too many metakeys found (k1 < k2)\n", \
__FILE__, __LINE__, __func__, ELEKTRA_QUOTE (mmk1), ELEKTRA_QUOTE (mmk2)); \
} \
} \
} while (0)
/**Compare two keysets.
*
* Compare if two keysets contain the same keys.
* @retval 0 on success
* */
#define compare_keyset(pks1, pks2) \
{ \
nbTest++; \
KeySet * mmks1 = (KeySet *) pks1; \
KeySet * mmks2 = (KeySet *) pks2; \
int bothEmpty = ksGetSize (mmks1) == 0 && ksGetSize (mmks1) == ksGetSize (mmks2); \
if (mmks1 != mmks2 && !bothEmpty) \
{ \
Key * cmmk1 = 0; \
Key * cmmk2 = 0; \
\
if (ksGetSize (mmks1) == 0) yield_error ("real size of " ELEKTRA_QUOTE (mmks1) " was 0"); \
if (ksGetSize (mmks2) == 0) yield_error ("real size of " ELEKTRA_QUOTE (mmks2) " was 0"); \
\
if (ksGetSize (mmks1) != ksGetSize (mmks2)) \
{ \
nbError++; \
printf ("%s:%d: error in %s: Compare keyset failed, size of keysets are not equal with size(%s): %d, " \
"size(%s): %d\n", \
__FILE__, __LINE__, __func__, ELEKTRA_QUOTE (mmks1), (int) ksGetSize (mmks1), \
ELEKTRA_QUOTE (mmks2), (int) ksGetSize (mmks2)); \
printf ("mmks1:\n"); \
output_keyset (mmks1); \
printf ("mmks2:\n"); \
output_keyset (mmks2); \
} \
else \
{ \
\
ksRewind (mmks1); \
ksRewind (mmks2); \
\
while ((cmmk1 = ksNext (mmks1)) != 0) \
{ \
cmmk2 = ksNext (mmks2); \
if (!cmmk2) \
{ \
yield_error ("Compare keyset " ELEKTRA_QUOTE (mmks1) " with " ELEKTRA_QUOTE ( \
mmks2) " failed, did not find corresponding key") break; \
} \
\
compare_key (cmmk1, cmmk2); \
} \
} \
} \
}
#endif // __cplusplus
int compare_files (const char * filename);
int compare_line_files (const char * filename, const char * genfilename);
int compare_regex_to_line_files (const char * filename, const char * genfilename);
char * srcdir_file (const char * fileName);
char * bindir_file (const char * fileName);
const char * elektraFilename (void);
void elektraUnlink (const char * filename);
Key * create_root_key (const char * backendName);
KeySet * create_conf (const char * filename);
void output_meta (Key * k);
void output_key (Key * ks);
void output_keyset (KeySet * ks);
int output_warnings (Key * errorKey);
int output_error (Key * errorKey);
void clean_temp_home (void);
#ifdef __cplusplus
} // end extern "C"
#endif
#endif