/
tests.c
554 lines (483 loc) · 13.1 KB
/
tests.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <string.h>
#include <tests.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef USE_NFTW
#include <ftw.h>
#include <stdlib.h>
#define NOPENFD 20
#endif
#include <regex.h>
#include <kdbinternal.h>
#ifdef __cplusplus
extern "C" {
#endif
int nbError;
int nbTest;
char file[KDB_MAX_PATH_LENGTH];
char srcdir[KDB_MAX_PATH_LENGTH + 1];
char bindir[KDB_MAX_PATH_LENGTH + 1];
char * tmpfilename;
char * tempHome;
int tempHomeLen;
char * tempHomeConf;
/**Does some useful startup.
*/
int init (int argc, char ** argv)
{
char * tmpvar;
int fd;
if (argc > 1)
{
strncpy (srcdir, argv[1], sizeof (srcdir) - 1);
}
else
{
strncpy (srcdir, BUILTIN_DATA_FOLDER, sizeof (srcdir) - 1);
}
tmpvar = getenv ("TMPDIR");
if (!tmpvar)
{
tmpvar = "/tmp";
}
// check tempvar for trailing slash /
if (strlen (tmpvar) > 2)
{
if (tmpvar[strlen (tmpvar) - 1] == '/')
{
tmpvar[strlen (tmpvar) - 1] = '\0';
}
}
tempHomeLen = strlen (tmpvar) + 1 + 13 + 6 + 1;
tempHome = elektraMalloc (tempHomeLen);
tempHomeConf = elektraMalloc (tempHomeLen + strlen (KDB_DB_USER) + 2);
succeed_if (tempHome != 0, "elektraMalloc failed");
snprintf (tempHome, tempHomeLen, "%s/elektra-test.XXXXXX", tmpvar);
snprintf (tempHomeConf, tempHomeLen, "%s/elektra-test.XXXXXX/" KDB_DB_USER, tmpvar);
succeed_if (mkdtemp (tempHome) != 0, "mkdtemp failed");
setenv ("HOME", tempHome, 1);
atexit (clean_temp_home);
int tmpfilenameLen = tempHomeLen + 1 + 12 + 6 + 1;
tmpfilename = elektraMalloc (tmpfilenameLen);
succeed_if (tmpfilenameLen != 0, "elektraMalloc failed");
snprintf (tmpfilename, tmpfilenameLen, "%s/elektra-tmp.XXXXXX", tempHome);
fd = mkstemp (tmpfilename);
succeed_if (fd != -1, "mkstemp failed");
close (fd);
const char * binpath = getenv ("KDB_TEST_BIN_DIR");
if (binpath != NULL)
{
strncpy (bindir, binpath, sizeof (bindir) - 1);
}
else
{
strncpy (bindir, BUILTIN_EXEC_FOLDER, sizeof (bindir) - 1);
}
return 0;
}
/**Create a root key for a backend.
*
* @return an allocated root key */
Key * create_root_key (const char * backendName)
{
Key * root = keyNew ("user:/tests", KEY_END);
/*Make mountpoint beneath root, and do all tests here*/
keyAddBaseName (root, backendName);
keySetString (root, backendName);
keySetString (root, backendName);
return root;
}
/**Create a configuration keyset for a backend.
*
* @return an allocated configuration keyset for a backend*/
KeySet * create_conf (const char * filename)
{
return ksNew (2, keyNew ("system:/path", KEY_VALUE, filename, KEY_END), KS_END);
}
int compare_line_files_fun (const char * filename, const char * genfilename, int (*cmpFun) (const char *, const char *, size_t n))
{
FILE *forg, *fgen;
char bufferorg[BUFFER_LENGTH + 1];
char buffergen[BUFFER_LENGTH + 1];
char * org = 0;
char * gen = 0;
int line = 0;
int remainingBufferLength = BUFFER_LENGTH;
forg = fopen (filename, "r");
fgen = fopen (genfilename, "r");
strncpy (bufferorg, "could not open file, orig: ", BUFFER_LENGTH);
remainingBufferLength -= strlen ("could not open file, orig: ");
strncat (bufferorg, filename, remainingBufferLength);
remainingBufferLength -= strlen (filename);
strncat (bufferorg, " gen: ", remainingBufferLength);
remainingBufferLength -= strlen (" gen: ");
strncat (bufferorg, genfilename, remainingBufferLength);
exit_if_fail (forg && fgen, bufferorg);
while ((org = fgets (bufferorg, BUFFER_LENGTH, forg)) && (gen = fgets (buffergen, BUFFER_LENGTH, fgen)))
{
line++;
if ((*cmpFun) (bufferorg, buffergen, BUFFER_LENGTH))
{
printf ("Compare <%s>, with <%s>\n", bufferorg, buffergen);
printf ("in file %s, line %d.\n", filename, line);
succeed_if (0, "comparing lines failed");
goto error;
}
}
if (org || fgets (buffergen, BUFFER_LENGTH, fgen))
{
printf ("The files do not have same number of lines (%d): %s.\n", line, filename);
succeed_if (0, "comparing files failed");
goto error;
}
fclose (forg);
fclose (fgen);
return 1;
error:
fclose (forg);
fclose (fgen);
return 0;
}
/**
* @brief Compare two files line by line
*
* @param filename first file
* @param genfilename file to compare with
*
* @retval 0 on errors (succeed_if already executed)
* @retval 1 on success
*/
int compare_line_files (const char * filename, const char * genfilename)
{
return compare_line_files_fun (filename, genfilename, &strncmp);
}
/**
* @brief Compare regex in pattern to str
*
* @param pattern char * representing a regex pattern
* @param str char * we compare the pattern to
*
* @retval 1 if pattern is invalid or does not match str
* @retval 0 on success
*/
int regexcmp (const char * pattern, const char * str, size_t n ELEKTRA_UNUSED)
{
int status;
regex_t re;
if (regcomp (&re, pattern, REG_EXTENDED | REG_NOSUB) != 0)
{
return (1);
}
status = regexec (&re, str, (size_t) 0, NULL, 0);
regfree (&re);
return status;
}
/**
* @brief Compare two files line by line where the original file is made up of
* regex
*
* @param filename first file, containing regex patterns
* @param genfilename file to compare with
*
* @retval 0 on errors (succeed_if already executed)
* @retval 1 on success
*/
int compare_regex_to_line_files (const char * filename, const char * genfilename)
{
return compare_line_files_fun (filename, genfilename, ®excmp);
}
/**Compare two files line by line.
*
* Fails when there are any differences.
*
* The original file is passed as parameter.
* It will be compared with the file -gen.
*
* file.xml -> file-gen.xml (xml comparator)
* file.txt -> file-gen.txt (line comparator)
* file.c -> file-gen.c (c comparator)
*
*/
int compare_files (const char * filename)
{
char genfilename[KDB_MAX_PATH_LENGTH];
char * dot = strrchr (filename, '.');
exit_if_fail (dot != 0, "could not find extension in file");
strncpy (genfilename, filename, dot - filename);
/* does not terminate string, but strcat need it, so: */
genfilename[dot - filename] = 0;
strcat (genfilename, "-gen");
if (!strcmp (dot, ".xml"))
{
strcat (genfilename, ".xml");
}
else if (!strcmp (dot, ".txt"))
{
strcat (genfilename, ".txt");
}
else if (!strcmp (dot, ".c"))
{
strcat (genfilename, ".c");
}
return compare_line_files (filename, genfilename);
}
/* return file name in srcdir.
* No bound checking on file size, may overflow. */
char * srcdir_file (const char * fileName)
{
strcpy (file, srcdir);
strcat (file, "/");
strcat (file, fileName);
return file;
}
/* return file name in srcdir.
* No bound checking on file size, may overflow. */
char * bindir_file (const char * fileName)
{
strcpy (file, bindir);
strcat (file, "/");
strcat (file, fileName);
return file;
}
const char * elektraFilename (void)
{
return tmpfilename;
}
void elektraUnlink (const char * filename)
{
unlink (filename);
}
void clear_sync (KeySet * ks)
{
Key * k;
ksRewind (ks);
while ((k = ksNext (ks)) != 0)
keyClearSync (k);
}
void output_meta (Key * k)
{
const Key * meta;
keyRewindMeta (k);
while ((meta = keyNextMeta (k)) != 0)
{
printf (", %s: %s", keyName (meta), (const char *) keyValue (meta));
}
printf ("\n");
}
void output_key (Key * k)
{
// output_meta will print endline
printf ("%p key: %s, string: %s", (void *) k, keyName (k), keyString (k));
output_meta (k);
}
void output_keyset (KeySet * ks)
{
Key * k;
ksRewind (ks);
while ((k = ksNext (ks)) != 0)
{
output_key (k);
}
}
void output_plugin (Plugin * plugin)
{
if (!plugin) return;
printf ("Name: %s [%zu]\n", plugin->name, plugin->refcounter);
output_keyset (plugin->config);
}
void output_backend (Backend * backend)
{
if (!backend) return;
printf ("us: %zd, ss: %zd\n", backend->usersize, backend->systemsize);
output_key (backend->mountpoint);
}
void output_trie (Trie * trie)
{
int i;
for (i = 0; i < KDB_MAX_UCHAR; ++i)
{
if (trie->value[i])
{
printf ("output_trie: %p, mp: %s %s [%d]\n", (void *) trie->value[i], keyName (trie->value[i]->mountpoint),
keyString (trie->value[i]->mountpoint), i);
}
if (trie->children[i]) output_trie (trie->children[i]);
}
if (trie->empty_value)
{
printf ("empty_value: %p, mp: %s %s\n", (void *) trie->empty_value, keyName (trie->empty_value->mountpoint),
keyString (trie->empty_value->mountpoint));
}
}
void output_split (Split * split)
{
printf ("Split - size: %zu, alloc: %zu\n", split->size, split->alloc);
for (size_t i = 0; i < split->size; ++i)
{
if (split->handles[i])
{
printf ("split #%zu size: %zd, handle: %p, sync: %d, parent: %s (%s), spec: %zd, dir: %zd, user: %zd, system: "
"%zd\n",
i, ksGetSize (split->keysets[i]), (void *) split->handles[i], split->syncbits[i],
keyName (split->parents[i]), keyString (split->parents[i]), split->handles[i]->specsize,
split->handles[i]->dirsize, split->handles[i]->usersize, split->handles[i]->systemsize);
}
else
{
printf ("split #%zu, size: %zd, default split, sync: %d\n", i, ksGetSize (split->keysets[i]), split->syncbits[i]);
}
}
}
void generate_split (Split * split)
{
printf ("succeed_if (split->size == %zu, \"size of split not correct\");\n", split->size);
for (size_t i = 0; i < split->size; ++i)
{
printf ("succeed_if (split->syncbits[%zu]== %d, \"size of split not correct\");\n", i, split->syncbits[i]);
printf ("succeed_if (ksGetSize(split->keysets[%zu]) == %zd, \"wrong size\");\n", i, ksGetSize (split->keysets[i]));
}
}
/**
* @brief Output warnings if present
*
* To check for warnings use:
* succeed_if(output_warnings(parentKey), "warning(s) found");
*
* @param warningKey the key to retrieve metadata from
*
* @see check_for_errors_and_warnings if you want errors to have a test case failed without output
*
* @retval 1 if no warnings (can be used within succeed_if)
*/
int output_warnings (Key * warningKey)
{
//! [warnings]
Key * cutpoint = keyNew ("meta:/warnings", KEY_END);
KeySet * warnings = ksCut (keyMeta (warningKey), cutpoint);
if (!warningKey || ksGetSize (warnings) == 0)
{
ksDel (warnings);
keyDel (cutpoint);
return 1;
}
printf ("There are %zu warnings\n", ksGetSize (warnings));
elektraCursor i = 1;
while (i < ksGetSize (warnings))
{
++i;
Key * cur = ksAtCursor (warnings, i);
while (!keyIsDirectlyBelow (cutpoint, cur))
{
printf ("%s: %s\n", keyName (cur) + keyGetNameSize (cutpoint), keyString (cur));
++i;
cur = ksAtCursor (warnings, i);
}
printf ("\n");
}
ksDel (warnings);
keyDel (cutpoint);
//! [warnings]
return 0;
}
/**
* @brief Output the error if present
*
* To check for error use:
* succeed_if(output_error(parentKey), "error found");
*
* @param errorKey keys to retrieve errors from
*
* @retval 1 if no error (can be used within succeed_if)
*/
int output_error (Key * errorKey)
{
//! [error]
const Key * metaError = keyGetMeta (errorKey, "error");
if (!metaError) return 1; /* There is no current error */
printf ("number: %s\n", keyString (keyGetMeta (errorKey, "error/number")));
printf ("description: : %s\n", keyString (keyGetMeta (errorKey, "error/description")));
printf ("module: : %s\n", keyString (keyGetMeta (errorKey, "error/module")));
printf ("at: %s:%s\n", keyString (keyGetMeta (errorKey, "error/file")), keyString (keyGetMeta (errorKey, "error/line")));
printf ("reason: : %s\n", keyString (keyGetMeta (errorKey, "error/reason")));
printf ("mountpoint: : %s\n", keyString (keyGetMeta (errorKey, "error/mountpoint")));
printf ("configfile: : %s\n", keyString (keyGetMeta (errorKey, "error/configfile")));
//! [error]
return 0;
}
#ifdef USE_NFTW
static int rm_all (const char * fpath, const struct stat * sb ELEKTRA_UNUSED, int tflag, struct FTW * ftwbuf ELEKTRA_UNUSED)
{
if (tflag == FTW_F)
{
unlink (fpath);
}
else if (tflag == FTW_D || tflag == FTW_DP)
{
rmdir (fpath);
}
else
{
// not a file or dir we can delete
printf ("unexpected flag: %d\n", tflag);
return 1;
}
return 0;
}
#endif
void clean_temp_home (void)
{
if (tmpfilename)
{
elektraUnlink (tmpfilename);
elektraFree (tmpfilename);
tmpfilename = NULL;
}
if (tempHomeConf)
{
rmdir (tempHomeConf);
elektraFree (tempHomeConf);
tempHomeConf = NULL;
}
if (tempHome)
{
#ifdef USE_NFTW
int nftw_flags = FTW_DEPTH | FTW_PHYS;
succeed_if (nftw (tempHome, rm_all, NOPENFD, nftw_flags) == 0, "Could not delete TMPHOME via nftw");
#else
size_t fileToCleanLen = tempHomeLen + 30;
char * fileToClean = elektraMalloc (fileToCleanLen);
snprintf (fileToClean, fileToCleanLen, "%s/.gnupg/random_seed", tempHome);
unlink (fileToClean);
snprintf (fileToClean, fileToCleanLen, "%s/.gnupg/trustdb.gpg", tempHome);
unlink (fileToClean);
snprintf (fileToClean, fileToCleanLen, "%s/.gnupg/pubring.kbx~", tempHome);
unlink (fileToClean);
snprintf (fileToClean, fileToCleanLen, "%s/.gnupg/pubring.kbx", tempHome);
unlink (fileToClean);
snprintf (fileToClean, fileToCleanLen, "%s/.gnupg", tempHome);
rmdir (fileToClean);
elektraFree (fileToClean);
succeed_if (rmdir (tempHome) == 0, "Could not delete TMPHOME manually");
#endif
elektraFree (tempHome);
tempHome = NULL;
tempHomeLen = 0;
}
}
#ifdef __cplusplus
} // end extern "C"
#endif