~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to .pc/0002-Sebastian-Andrzej-Siewior.patch/unit_tests/check_clamav.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#if HAVE_CONFIG_H
2
 
#include "clamav-config.h"
3
 
#endif
4
 
 
5
 
#include <stdio.h>
6
 
 
7
 
#include <stdlib.h>
8
 
#include <limits.h>
9
 
#include <fcntl.h>
10
 
#include <string.h>
11
 
#include <check.h>
12
 
#include <sys/types.h>
13
 
#include <dirent.h>
14
 
#include <sys/mman.h>
15
 
#include "../libclamav/clamav.h"
16
 
#include "../libclamav/others.h"
17
 
#include "../libclamav/matcher.h"
18
 
#include "../libclamav/version.h"
19
 
#include "../libclamav/dsig.h"
20
 
#include "../libclamav/sha256.h"
21
 
#include "checks.h"
22
 
 
23
 
/* extern void cl_free(struct cl_engine *engine); */
24
 
START_TEST (test_cl_free)
25
 
/*
26
 
    struct cl_engine *engine = NULL;
27
 
    cl_free(NULL);
28
 
*/
29
 
END_TEST
30
 
 
31
 
/* extern struct cl_engine *cl_dup(struct cl_engine *engine); */
32
 
START_TEST (test_cl_dup)
33
 
    /*
34
 
    struct cl_engine *engine;
35
 
    fail_unless(NULL == cl_dup(NULL), "cl_dup null pointer");
36
 
    */
37
 
END_TEST
38
 
 
39
 
/* extern int cl_build(struct cl_engine *engine); */
40
 
START_TEST (test_cl_build)
41
 
    /*
42
 
    struct cl_engine *engine;
43
 
    fail_unless(CL_ENULLARG == cl_build(NULL), "cl_build null pointer");
44
 
    engine = calloc(sizeof(struct cl_engine),1);
45
 
    fail_unless(engine, "cl_build calloc");
46
 
    fail_unless(CL_ENULLARG == cl_build(engine), "cl_build(engine) with null ->root");
47
 
    */
48
 
/*    engine->root = cli_calloc(CL_TARGET_TABLE_SIZE, sizeof(struct cli_matcher *)); */
49
 
END_TEST
50
 
 
51
 
/* extern void cl_debug(void); */
52
 
START_TEST (test_cl_debug)
53
 
{
54
 
    int old_status = cli_debug_flag;
55
 
    cli_debug_flag = 0;
56
 
    cl_debug();
57
 
    fail_unless(1 == cli_debug_flag, "cl_debug failed to set cli_debug_flag");
58
 
 
59
 
    cli_debug_flag = 1;
60
 
    cl_debug();
61
 
    fail_unless(1 == cli_debug_flag, "cl_debug failed when flag was already set");
62
 
    cli_debug_flag = old_status;
63
 
}
64
 
END_TEST
65
 
 
66
 
/* extern const char *cl_retdbdir(void); */
67
 
START_TEST (test_cl_retdbdir)
68
 
    fail_unless(!strcmp(DATADIR, cl_retdbdir()), "cl_retdbdir");
69
 
END_TEST
70
 
 
71
 
#ifndef REPO_VERSION
72
 
#define REPO_VERSION VERSION
73
 
#endif
74
 
 
75
 
/* extern const char *cl_retver(void); */
76
 
START_TEST (test_cl_retver)
77
 
{
78
 
    const char *ver = cl_retver();
79
 
    fail_unless(!strcmp(REPO_VERSION""VERSION_SUFFIX, ver),"cl_retver");
80
 
    fail_unless(strcspn(ver,"012345789") < strlen(ver),
81
 
                    "cl_retver must have a number");
82
 
}
83
 
END_TEST
84
 
 
85
 
/* extern void cl_cvdfree(struct cl_cvd *cvd); */
86
 
START_TEST (test_cl_cvdfree)
87
 
/*
88
 
    struct cl_cvd *cvd1, *cvd2;
89
 
 
90
 
    cvd1 = malloc(sizeof(struct cl_cvd));
91
 
    fail_unless(cvd1, "cvd malloc");
92
 
    cl_cvdfree(cvd1);
93
 
 
94
 
    cvd2 = malloc(sizeof(struct cl_cvd));
95
 
    cvd2->time = malloc(1);
96
 
    cvd2->md5 = malloc(1);
97
 
    cvd2->dsig= malloc(1);
98
 
    cvd2->builder = malloc(1);
99
 
    fail_unless(cvd2, "cvd malloc");
100
 
    fail_unless(cvd2->time, "cvd malloc");
101
 
    fail_unless(cvd2->md5, "cvd malloc");
102
 
    fail_unless(cvd2->dsig, "cvd malloc");
103
 
    fail_unless(cvd2->builder, "cvd malloc");
104
 
    cl_cvdfree(cvd2);
105
 
    cl_cvdfree(NULL);
106
 
*/
107
 
END_TEST
108
 
 
109
 
/* extern int cl_statfree(struct cl_stat *dbstat); */
110
 
START_TEST (test_cl_statfree)
111
 
/*
112
 
    struct cl_stat *stat;
113
 
    fail_unless(CL_ENULLARG == cl_statfree(NULL), "cl_statfree(NULL)");
114
 
    
115
 
    stat = malloc(sizeof(struct cl_stat));
116
 
    fail_unless(NULL != stat, "malloc");
117
 
    fail_unless(CL_SUCCESS == cl_statfree(stat), "cl_statfree(empty_struct)");
118
 
    
119
 
    stat = malloc(sizeof(struct cl_stat));
120
 
    fail_unless(NULL != stat, "malloc");
121
 
    stat->stattab = strdup("test");
122
 
    fail_unless(NULL != stat->stattab, "strdup");
123
 
    fail_unless(CL_SUCCESS == cl_statfree(stat), "cl_statfree(stat with stattab)");
124
 
 
125
 
    stat = malloc(sizeof(struct cl_stat));
126
 
    fail_unless(NULL != stat, "malloc");
127
 
    stat->stattab = NULL;
128
 
    fail_unless(CL_SUCCESS == cl_statfree(stat), "cl_statfree(stat with stattab) set to NULL");
129
 
*/
130
 
END_TEST
131
 
 
132
 
/* extern unsigned int cl_retflevel(void); */
133
 
START_TEST (test_cl_retflevel)
134
 
END_TEST    
135
 
 
136
 
/* extern struct cl_cvd *cl_cvdhead(const char *file); */
137
 
START_TEST (test_cl_cvdhead)
138
 
/*
139
 
    fail_unless(NULL == cl_cvdhead(NULL), "cl_cvdhead(null)");
140
 
    fail_unless(NULL == cl_cvdhead("input/cl_cvdhead/1.txt"), "cl_cvdhead(515 byte file, all nulls)");
141
 
*/
142
 
    /* the data read from the file is passed to cl_cvdparse, test cases for that are separate */
143
 
END_TEST
144
 
 
145
 
/* extern struct cl_cvd *cl_cvdparse(const char *head); */
146
 
START_TEST (test_cl_cvdparse)
147
 
END_TEST
148
 
 
149
 
static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size);
150
 
static struct cl_engine *g_engine;
151
 
 
152
 
#ifdef CHECK_HAVE_LOOPS
153
 
/* int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options) */
154
 
START_TEST (test_cl_scandesc)
155
 
{
156
 
    const char *virname = NULL;
157
 
    char file[256];
158
 
    unsigned long size;
159
 
    unsigned long int scanned = 0;
160
 
    int ret;
161
 
 
162
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
163
 
    cli_dbgmsg("scanning (scandesc) %s\n", file);
164
 
    ret = cl_scandesc(fd, &virname, &scanned, g_engine, CL_SCAN_STDOPT);
165
 
    cli_dbgmsg("scan end (scandesc) %s\n", file);
166
 
 
167
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scandesc failed for %s: %s", file, cl_strerror(ret));
168
 
    fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
169
 
    close(fd);
170
 
}
171
 
END_TEST
172
 
 
173
 
START_TEST (test_cl_scandesc_allscan)
174
 
{
175
 
    const char *virname = NULL;
176
 
    const char ** virpp = &virname;
177
 
    char file[256];
178
 
    unsigned long size;
179
 
    unsigned long int scanned = 0;
180
 
    int ret;
181
 
 
182
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
183
 
    cli_dbgmsg("scanning (scandesc) %s\n", file);
184
 
    ret = cl_scandesc(fd, virpp, &scanned, g_engine, CL_SCAN_ALLMATCHES+CL_SCAN_STDOPT);
185
 
    virpp = (const char **)*virpp; /* allscan api hack */
186
 
    cli_dbgmsg("scan end (scandesc) %s\n", file);
187
 
 
188
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scandesc_allscan failed for %s: %s", file, cl_strerror(ret));
189
 
    fail_unless_fmt(*virpp && !strcmp(*virpp, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", *virpp);
190
 
    free((void *)virpp);
191
 
    close(fd);
192
 
}
193
 
END_TEST
194
 
 
195
 
//* int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options) */
196
 
START_TEST (test_cl_scanfile)
197
 
{
198
 
    const char *virname = NULL;
199
 
    char file[256];
200
 
    unsigned long size;
201
 
    unsigned long int scanned = 0;
202
 
    int ret;
203
 
 
204
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
205
 
    close(fd);
206
 
 
207
 
    cli_dbgmsg("scanning (scanfile) %s\n", file);
208
 
    ret = cl_scanfile(file, &virname, &scanned, g_engine, CL_SCAN_STDOPT);
209
 
    cli_dbgmsg("scan end (scanfile) %s\n", file);
210
 
 
211
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile failed for %s: %s", file, cl_strerror(ret));
212
 
    fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
213
 
}
214
 
END_TEST
215
 
 
216
 
START_TEST (test_cl_scanfile_allscan)
217
 
{
218
 
    const char *virname = NULL;
219
 
    const char **virpp = &virname;
220
 
    char file[256];
221
 
    unsigned long size;
222
 
    unsigned long int scanned = 0;
223
 
    int ret;
224
 
 
225
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
226
 
    close(fd);
227
 
 
228
 
    cli_dbgmsg("scanning (scanfile_allscan) %s\n", file);
229
 
    ret = cl_scanfile(file, virpp, &scanned, g_engine, CL_SCAN_ALLMATCHES+CL_SCAN_STDOPT);
230
 
    virpp = (const char **)*virpp; /* allscan api hack */
231
 
    cli_dbgmsg("scan end (scanfile_allscan) %s\n", file);
232
 
 
233
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_allscan failed for %s: %s", file, cl_strerror(ret));
234
 
    fail_unless_fmt(*virpp && !strcmp(*virpp, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", *virpp);
235
 
    free((void *)virpp);
236
 
}
237
 
END_TEST
238
 
 
239
 
START_TEST (test_cl_scanfile_callback)
240
 
{
241
 
    const char *virname = NULL;
242
 
    char file[256];
243
 
    unsigned long size;
244
 
    unsigned long int scanned = 0;
245
 
    int ret;
246
 
 
247
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
248
 
    close(fd);
249
 
 
250
 
    cli_dbgmsg("scanning (scanfile_cb) %s\n", file);
251
 
    /* TODO: test callbacks */
252
 
    ret = cl_scanfile_callback(file, &virname, &scanned, g_engine, CL_SCAN_STDOPT, NULL);
253
 
    cli_dbgmsg("scan end (scanfile_cb) %s\n", file);
254
 
 
255
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_cb failed for %s: %s", file, cl_strerror(ret));
256
 
    fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
257
 
}
258
 
END_TEST
259
 
 
260
 
START_TEST (test_cl_scanfile_callback_allscan)
261
 
{
262
 
    const char *virname = NULL;
263
 
    const char **virpp = &virname;
264
 
    char file[256];
265
 
    unsigned long size;
266
 
    unsigned long int scanned = 0;
267
 
    int ret;
268
 
 
269
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
270
 
    close(fd);
271
 
 
272
 
    cli_dbgmsg("scanning (scanfile_cb_allscan) %s\n", file);
273
 
    /* TODO: test callbacks */
274
 
    ret = cl_scanfile_callback(file, virpp, &scanned, g_engine, CL_SCAN_ALLMATCHES+CL_SCAN_STDOPT, NULL);
275
 
    virpp = (const char **)*virpp; /* allscan api hack */
276
 
    cli_dbgmsg("scan end (scanfile_cb_allscan) %s\n", file);
277
 
 
278
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_cb_allscan failed for %s: %s", file, cl_strerror(ret));
279
 
    fail_unless_fmt(*virpp && !strcmp(*virpp, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", *virpp);
280
 
    free((void *)virpp);
281
 
}
282
 
END_TEST
283
 
 
284
 
START_TEST (test_cl_scandesc_callback)
285
 
{
286
 
    const char *virname = NULL;
287
 
    char file[256];
288
 
    unsigned long size;
289
 
    unsigned long int scanned = 0;
290
 
    int ret;
291
 
 
292
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
293
 
 
294
 
    cli_dbgmsg("scanning (scandesc_cb) %s\n", file);
295
 
    /* TODO: test callbacks */
296
 
    ret = cl_scandesc_callback(fd, &virname, &scanned, g_engine, CL_SCAN_STDOPT, NULL);
297
 
    cli_dbgmsg("scan end (scandesc_cb) %s\n", file);
298
 
 
299
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile failed for %s: %s", file, cl_strerror(ret));
300
 
    fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
301
 
    close(fd);
302
 
}
303
 
END_TEST
304
 
 
305
 
START_TEST (test_cl_scandesc_callback_allscan)
306
 
{
307
 
    const char *virname = NULL;
308
 
    const char **virpp = &virname;
309
 
    char file[256];
310
 
    unsigned long size;
311
 
    unsigned long int scanned = 0;
312
 
    int ret;
313
 
 
314
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
315
 
 
316
 
    cli_dbgmsg("scanning (scandesc_cb_allscan) %s\n", file);
317
 
    /* TODO: test callbacks */
318
 
    ret = cl_scandesc_callback(fd, virpp, &scanned, g_engine, CL_SCAN_ALLMATCHES+CL_SCAN_STDOPT, NULL);
319
 
    virpp = (const char **)*virpp; /* allscan api hack */
320
 
    cli_dbgmsg("scan end (scandesc_cb_allscan) %s\n", file);
321
 
 
322
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_allscan failed for %s: %s", file, cl_strerror(ret));
323
 
    fail_unless_fmt(*virpp && !strcmp(*virpp, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", *virpp);
324
 
    free((void *)virpp);
325
 
    close(fd);
326
 
}
327
 
END_TEST
328
 
 
329
 
#endif
330
 
 
331
 
/* int cl_load(const char *path, struct cl_engine **engine, unsigned int *signo, unsigned int options) */
332
 
START_TEST (test_cl_load)
333
 
END_TEST
334
 
 
335
 
/* int cl_cvdverify(const char *file) */
336
 
START_TEST (test_cl_cvdverify)
337
 
END_TEST
338
 
 
339
 
/* int cl_statinidir(const char *dirname, struct cl_stat *dbstat) */
340
 
START_TEST (test_cl_statinidir)
341
 
END_TEST
342
 
 
343
 
/* int cl_statchkdir(const struct cl_stat *dbstat) */
344
 
START_TEST (test_cl_statchkdir)
345
 
END_TEST
346
 
 
347
 
/* void cl_settempdir(const char *dir, short leavetemps) */
348
 
START_TEST (test_cl_settempdir)
349
 
END_TEST
350
 
 
351
 
/* const char *cl_strerror(int clerror) */
352
 
START_TEST (test_cl_strerror)
353
 
END_TEST
354
 
 
355
 
static char **testfiles = NULL;
356
 
static unsigned testfiles_n = 0;
357
 
 
358
 
#if HAVE_BZLIB_H
359
 
static const int expected_testfiles = 48;
360
 
#else
361
 
static const int expected_testfiles = 46;
362
 
#endif
363
 
 
364
 
static void init_testfiles(void)
365
 
{
366
 
    struct dirent *dirent;
367
 
    unsigned i = 0;
368
 
 
369
 
    DIR *d = opendir(OBJDIR"/../test");
370
 
    fail_unless(!!d, "opendir");
371
 
    if (!d)
372
 
        return;
373
 
    testfiles = NULL;
374
 
    testfiles_n = 0;
375
 
    while ((dirent = readdir(d))) {
376
 
        if (strncmp(dirent->d_name, "clam", 4))
377
 
            continue;
378
 
        i++;
379
 
        testfiles = cli_realloc(testfiles, i*sizeof(*testfiles));
380
 
        fail_unless(!!testfiles, "cli_realloc");
381
 
        testfiles[i-1] = strdup(dirent->d_name);
382
 
    }
383
 
    testfiles_n = i;
384
 
    fail_unless_fmt(testfiles_n == expected_testfiles, "testfiles: %d != %d", testfiles_n, expected_testfiles);
385
 
 
386
 
    closedir(d);
387
 
}
388
 
 
389
 
static void free_testfiles(void)
390
 
{
391
 
    unsigned i;
392
 
    for (i=0;i<testfiles_n;i++) {
393
 
        free(testfiles[i]);
394
 
    }
395
 
    free(testfiles);
396
 
    testfiles = NULL;
397
 
    testfiles_n = 0;
398
 
}
399
 
 
400
 
static int inited = 0;
401
 
 
402
 
static void engine_setup(void)
403
 
{
404
 
    unsigned int sigs = 0;
405
 
    const char *hdb = OBJDIR"/clamav.hdb";
406
 
 
407
 
    init_testfiles();
408
 
    if (!inited)
409
 
        fail_unless(cl_init(CL_INIT_DEFAULT) == 0, "cl_init");
410
 
    inited = 1;
411
 
    g_engine = cl_engine_new();
412
 
    fail_unless(!!g_engine, "engine");
413
 
    fail_unless_fmt(cl_load(hdb, g_engine, &sigs, CL_DB_STDOPT) == 0, "cl_load %s", hdb);
414
 
    fail_unless(sigs == 1, "sigs");
415
 
    fail_unless(cl_engine_compile(g_engine) == 0, "cl_engine_compile");
416
 
}
417
 
 
418
 
static void engine_teardown(void)
419
 
{
420
 
    free_testfiles();
421
 
    cl_engine_free(g_engine);
422
 
}
423
 
 
424
 
static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size)
425
 
{
426
 
    int fd;
427
 
    STATBUF st;
428
 
 
429
 
    fail_unless(i < testfiles_n);
430
 
    snprintf(file, fsize, OBJDIR"/../test/%s", testfiles[i]);
431
 
 
432
 
    fd = open(file, O_RDONLY);
433
 
    fail_unless(fd > 0, "open");
434
 
    fail_unless(FSTAT(fd, &st) == 0, "fstat");
435
 
    *size = st.st_size;
436
 
    return fd;
437
 
}
438
 
#ifdef CHECK_HAVE_LOOPS
439
 
 
440
 
static off_t pread_cb(void *handle, void *buf, size_t count, off_t offset)
441
 
{
442
 
    return pread(*((int*)handle), buf, count, offset);
443
 
}
444
 
 
445
 
START_TEST (test_cl_scanmap_callback_handle)
446
 
{
447
 
    const char *virname = NULL;
448
 
    unsigned long int scanned = 0;
449
 
    cl_fmap_t *map;
450
 
    int ret;
451
 
    char file[256];
452
 
    unsigned long size;
453
 
 
454
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
455
 
    /* intentionally use different way than scanners.c for testing */
456
 
    map = cl_fmap_open_handle(&fd, 0, size, pread_cb, 1);
457
 
    fail_unless(!!map, "cl_fmap_open_handle");
458
 
 
459
 
    cli_dbgmsg("scanning (handle) %s\n", file);
460
 
    ret = cl_scanmap_callback(map, &virname, &scanned, g_engine, CL_SCAN_STDOPT, NULL);
461
 
    cli_dbgmsg("scan end (handle) %s\n", file);
462
 
 
463
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
464
 
    fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
465
 
    close(fd);
466
 
}
467
 
END_TEST
468
 
 
469
 
START_TEST (test_cl_scanmap_callback_handle_allscan)
470
 
{
471
 
    const char *virname = NULL;
472
 
    const char ** virpp = &virname;
473
 
    unsigned long int scanned = 0;
474
 
    cl_fmap_t *map;
475
 
    int ret;
476
 
    char file[256];
477
 
    unsigned long size;
478
 
 
479
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
480
 
    /* intentionally use different way than scanners.c for testing */
481
 
    map = cl_fmap_open_handle(&fd, 0, size, pread_cb, 1);
482
 
    fail_unless(!!map, "cl_fmap_open_handle");
483
 
 
484
 
    cli_dbgmsg("scanning (handle) allscan %s\n", file);
485
 
    ret = cl_scanmap_callback(map, virpp, &scanned, g_engine, CL_SCAN_ALLMATCHES+CL_SCAN_STDOPT, NULL);
486
 
    virpp = (const char **)*virpp; /* allscan api hack */
487
 
    cli_dbgmsg("scan end (handle) allscan %s\n", file);
488
 
 
489
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback_allscan failed for %s: %s", file, cl_strerror(ret));
490
 
    fail_unless_fmt(*virpp && !strcmp(*virpp, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", *virpp);
491
 
    free((void *)virpp);
492
 
    close(fd);
493
 
}
494
 
END_TEST
495
 
 
496
 
START_TEST (test_cl_scanmap_callback_mem)
497
 
{
498
 
    const char *virname = NULL;
499
 
    unsigned long int scanned = 0;
500
 
    cl_fmap_t *map;
501
 
    int ret;
502
 
    void *mem;
503
 
    unsigned long size;
504
 
    char file[256];
505
 
 
506
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
507
 
 
508
 
    mem = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
509
 
    fail_unless(mem != MAP_FAILED, "mmap");
510
 
 
511
 
    /* intentionally use different way than scanners.c for testing */
512
 
    map = cl_fmap_open_memory(mem, size);
513
 
    fail_unless(!!map, "cl_fmap_open_mem");
514
 
 
515
 
    cli_dbgmsg("scanning (mem) %s\n", file);
516
 
    ret = cl_scanmap_callback(map, &virname, &scanned, g_engine, CL_SCAN_STDOPT, NULL);
517
 
    cli_dbgmsg("scan end (mem) %s\n", file);
518
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
519
 
    fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s for %s", virname, file);
520
 
    close(fd);
521
 
    cl_fmap_close(map);
522
 
 
523
 
    munmap(mem, size);
524
 
}
525
 
END_TEST
526
 
 
527
 
START_TEST (test_cl_scanmap_callback_mem_allscan)
528
 
{
529
 
    const char *virname = NULL;
530
 
    const char **virpp = &virname;
531
 
    unsigned long int scanned = 0;
532
 
    cl_fmap_t *map;
533
 
    int ret;
534
 
    void *mem;
535
 
    unsigned long size;
536
 
    char file[256];
537
 
 
538
 
    int fd = get_test_file(_i, file, sizeof(file), &size);
539
 
 
540
 
    mem = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
541
 
    fail_unless(mem != MAP_FAILED, "mmap");
542
 
 
543
 
    /* intentionally use different way than scanners.c for testing */
544
 
    map = cl_fmap_open_memory(mem, size);
545
 
    fail_unless(!!map, "cl_fmap_open_mem");
546
 
 
547
 
    cli_dbgmsg("scanning (mem) allscan %s\n", file);
548
 
    ret = cl_scanmap_callback(map, virpp, &scanned, g_engine, CL_SCAN_ALLMATCHES+CL_SCAN_STDOPT, NULL);
549
 
    virpp = (const char **)*virpp; /* allscan api hack */
550
 
    cli_dbgmsg("scan end (mem) allscan %s\n", file);
551
 
    fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
552
 
    fail_unless_fmt(*virpp && !strcmp(*virpp, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s for %s", *virpp, file);
553
 
    close(fd);
554
 
    cl_fmap_close(map);
555
 
    free((void *)virpp);
556
 
    munmap(mem, size);
557
 
}
558
 
END_TEST
559
 
#endif
560
 
 
561
 
static Suite *test_cl_suite(void)
562
 
{
563
 
    Suite *s = suite_create("cl_api");
564
 
    TCase *tc_cl = tcase_create("cl_dup");
565
 
    TCase *tc_cl_scan = tcase_create("cl_scan");
566
 
    suite_add_tcase (s, tc_cl);
567
 
    tcase_add_test(tc_cl, test_cl_free);
568
 
    tcase_add_test(tc_cl, test_cl_dup);
569
 
    tcase_add_test(tc_cl, test_cl_build);
570
 
    tcase_add_test(tc_cl, test_cl_debug);
571
 
    tcase_add_test(tc_cl, test_cl_retdbdir);
572
 
    tcase_add_test(tc_cl, test_cl_retver);
573
 
    tcase_add_test(tc_cl, test_cl_cvdfree);
574
 
    tcase_add_test(tc_cl, test_cl_statfree);
575
 
    tcase_add_test(tc_cl, test_cl_retflevel);
576
 
    tcase_add_test(tc_cl, test_cl_cvdhead);
577
 
    tcase_add_test(tc_cl, test_cl_cvdparse);
578
 
    tcase_add_test(tc_cl, test_cl_load);
579
 
    tcase_add_test(tc_cl, test_cl_cvdverify);
580
 
    tcase_add_test(tc_cl, test_cl_statinidir);
581
 
    tcase_add_test(tc_cl, test_cl_statchkdir);
582
 
    tcase_add_test(tc_cl, test_cl_settempdir);
583
 
    tcase_add_test(tc_cl, test_cl_strerror);
584
 
 
585
 
    suite_add_tcase(s, tc_cl_scan);
586
 
    tcase_add_checked_fixture (tc_cl_scan, engine_setup, engine_teardown);
587
 
#ifdef CHECK_HAVE_LOOPS
588
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scandesc, 0, expected_testfiles);
589
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scandesc_allscan, 0, expected_testfiles);
590
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanfile, 0, expected_testfiles);
591
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanfile_allscan, 0, expected_testfiles);
592
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scandesc_callback, 0, expected_testfiles);
593
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scandesc_callback_allscan, 0, expected_testfiles);
594
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanfile_callback, 0, expected_testfiles);
595
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanfile_callback_allscan, 0, expected_testfiles);
596
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanmap_callback_handle, 0, expected_testfiles);
597
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanmap_callback_handle_allscan, 0, expected_testfiles);
598
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanmap_callback_mem, 0, expected_testfiles);
599
 
    tcase_add_loop_test(tc_cl_scan, test_cl_scanmap_callback_mem_allscan, 0, expected_testfiles);
600
 
#endif
601
 
    return s;
602
 
}
603
 
 
604
 
static uint8_t le_data[4] = {0x67,0x45,0x23,0x01};
605
 
static int32_t le_expected[4] = { 0x01234567, 0x67012345, 0x45670123, 0x23456701};
606
 
uint8_t *data = NULL;
607
 
uint8_t *data2 = NULL;
608
 
#define DATA_REP 100
609
 
 
610
 
static void data_setup(void)
611
 
{
612
 
        uint8_t *p;
613
 
        size_t i;
614
 
 
615
 
        data = malloc(sizeof(le_data)*DATA_REP);
616
 
        data2 = malloc(sizeof(le_data)*DATA_REP);
617
 
        fail_unless(!!data, "unable to allocate memory for fixture");
618
 
        fail_unless(!!data2, "unable to allocate memory for fixture");
619
 
        p = data;
620
 
        /* make multiple copies of le_data, we need to run readint tests in a loop, so we need
621
 
         * to give it some data to run it on */
622
 
        for(i=0; i<DATA_REP;i++) {
623
 
                memcpy(p, le_data, sizeof(le_data));
624
 
                p += sizeof(le_data);
625
 
        }
626
 
        memset(data2, 0, DATA_REP*sizeof(le_data));
627
 
}
628
 
 
629
 
static void data_teardown(void)
630
 
{
631
 
        free(data);
632
 
        free(data2);
633
 
}
634
 
 
635
 
#ifdef CHECK_HAVE_LOOPS
636
 
/* test reading with different alignments, _i is parameter from tcase_add_loop_test */
637
 
START_TEST (test_cli_readint16)
638
 
{
639
 
    size_t j;
640
 
    int16_t value;
641
 
    /* read 2 bytes apart, start is not always aligned*/
642
 
    for(j=_i;j <= DATA_REP*sizeof(le_data)-2;j += 2) {
643
 
        value = le_expected[j&3];
644
 
        fail_unless(cli_readint16(&data[j]) == value, "(1) data read must be little endian");
645
 
    }
646
 
    /* read 2 bytes apart, always aligned*/
647
 
    for(j=0;j <= DATA_REP*sizeof(le_data)-2;j += 2) {
648
 
        value = le_expected[j&3];
649
 
        fail_unless(cli_readint16(&data[j]) == value, "(2) data read must be little endian");
650
 
    }
651
 
}
652
 
END_TEST
653
 
 
654
 
/* test reading with different alignments, _i is parameter from tcase_add_loop_test */
655
 
START_TEST (test_cli_readint32)
656
 
{
657
 
    size_t j;
658
 
    int32_t value = le_expected[_i&3];
659
 
    /* read 4 bytes apart, start is not always aligned*/
660
 
    for(j=_i;j < DATA_REP*sizeof(le_data)-4;j += 4) {
661
 
        fail_unless(cli_readint32(&data[j]) == value, "(1) data read must be little endian");
662
 
    }
663
 
    value = le_expected[0];
664
 
    /* read 4 bytes apart, always aligned*/
665
 
    for(j=0;j < DATA_REP*sizeof(le_data)-4;j += 4) {
666
 
        fail_unless(cli_readint32(&data[j]) == value, "(2) data read must be little endian");
667
 
    }
668
 
}
669
 
END_TEST
670
 
 
671
 
/* test writing with different alignments, _i is parameter from tcase_add_loop_test */
672
 
START_TEST (test_cli_writeint32)
673
 
{
674
 
    size_t j;
675
 
    /* write 4 bytes apart, start is not always aligned*/
676
 
    for(j=_i;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
677
 
        cli_writeint32(&data2[j], 0x12345678);
678
 
    }
679
 
    for(j=_i;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
680
 
        fail_unless(cli_readint32(&data2[j]) == 0x12345678, "write/read mismatch");
681
 
    }
682
 
    /* write 4 bytes apart, always aligned*/
683
 
    for(j=0;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
684
 
        cli_writeint32(&data2[j], 0x12345678);
685
 
    }
686
 
    for(j=0;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
687
 
        fail_unless(cli_readint32(&data2[j]) == 0x12345678, "write/read mismatch");
688
 
    }
689
 
}
690
 
END_TEST
691
 
 
692
 
static struct dsig_test {
693
 
    const char *md5;
694
 
    const char *dsig;
695
 
    int result;
696
 
} dsig_tests [] = {
697
 
    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe", 
698
 
        CL_SUCCESS},
699
 
    {"96b7feb3b2a863846438809fe481906f", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MXYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
700
 
        CL_SUCCESS},
701
 
    {"ae307614434715274c60854c931a26de", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MXYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
702
 
        CL_EVERIFY},
703
 
    {"96b7feb3b2a863846438809fe481906f", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
704
 
        CL_EVERIFY},
705
 
    {"ae307614434715274060854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
706
 
        CL_EVERIFY},
707
 
    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaatinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
708
 
        CL_EVERIFY},
709
 
    {"96b7feb3b2a863846438809fe481906f", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MYYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
710
 
        CL_EVERIFY},
711
 
    {"ge307614434715274c60854c931a26dee","60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
712
 
        CL_EVERIFY},
713
 
    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGee", 
714
 
        CL_EVERIFY},
715
 
    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+", 
716
 
        CL_EVERIFY}
717
 
};
718
 
 
719
 
static const size_t dsig_tests_cnt = sizeof(dsig_tests)/sizeof(dsig_tests[0]);
720
 
 
721
 
START_TEST (test_cli_dsig)
722
 
{
723
 
    fail_unless(cli_versig(dsig_tests[_i].md5, dsig_tests[_i].dsig) == dsig_tests[_i].result,
724
 
                "digital signature verification test failed");
725
 
}
726
 
END_TEST
727
 
 
728
 
static uint8_t tv1[3] = {
729
 
  0x61, 0x62, 0x63
730
 
};
731
 
 
732
 
static uint8_t tv2[56] = {
733
 
  0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65,
734
 
  0x63, 0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67,
735
 
  0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
736
 
  0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b,
737
 
  0x69, 0x6a, 0x6b, 0x6c, 0x6a, 0x6b, 0x6c, 0x6d,
738
 
  0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
739
 
  0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
740
 
};
741
 
 
742
 
static uint8_t res256[3][SHA256_HASH_SIZE] = {
743
 
  { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
744
 
    0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
745
 
    0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad },
746
 
  { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93,
747
 
    0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
748
 
    0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 },
749
 
  { 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2,
750
 
    0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
751
 
    0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 }
752
 
};
753
 
 
754
 
START_TEST (test_sha256)
755
 
{
756
 
    SHA256_CTX sha256;
757
 
    uint8_t hsha256[SHA256_HASH_SIZE];
758
 
    uint8_t buf[1000];
759
 
    int i;
760
 
 
761
 
    memset (buf, 0x61, sizeof (buf));
762
 
 
763
 
    sha256_init (&sha256);
764
 
    sha256_update (&sha256, tv1, sizeof (tv1));
765
 
    sha256_final (&sha256, hsha256);
766
 
    fail_unless(!memcmp (hsha256, res256[0], sizeof (hsha256)), "sha256 test vector #1 failed");
767
 
 
768
 
    sha256_init (&sha256);
769
 
    sha256_update (&sha256, tv2, sizeof (tv2));
770
 
    sha256_final (&sha256, hsha256);
771
 
    fail_unless(!memcmp (hsha256, res256[1], sizeof (hsha256)), "sha256 test vector #2 failed");
772
 
 
773
 
    sha256_init (&sha256);
774
 
    for (i = 0; i < 1000; i++)
775
 
        sha256_update (&sha256, buf, sizeof (buf));
776
 
    sha256_final (&sha256, hsha256);
777
 
    fail_unless(!memcmp (hsha256, res256[2], sizeof (hsha256)), "sha256 test vector #3 failed");
778
 
}
779
 
END_TEST
780
 
 
781
 
static Suite *test_cli_suite(void)
782
 
{
783
 
    Suite *s = suite_create("cli");
784
 
    TCase *tc_cli_others = tcase_create("byteorder_macros");
785
 
    TCase *tc_cli_dsig = tcase_create("digital signatures");
786
 
 
787
 
    suite_add_tcase (s, tc_cli_others);
788
 
    tcase_add_checked_fixture (tc_cli_others, data_setup, data_teardown);
789
 
    tcase_add_loop_test(tc_cli_others, test_cli_readint32, 0, 16);
790
 
    tcase_add_loop_test(tc_cli_others, test_cli_readint16, 0, 16);
791
 
    tcase_add_loop_test(tc_cli_others, test_cli_writeint32, 0, 16);
792
 
 
793
 
    suite_add_tcase (s, tc_cli_dsig);
794
 
    tcase_add_loop_test(tc_cli_dsig, test_cli_dsig, 0, dsig_tests_cnt);
795
 
    tcase_add_test(tc_cli_dsig, test_sha256);
796
 
 
797
 
    return s;
798
 
}
799
 
#endif /* CHECK_HAVE_LOOPS */
800
 
 
801
 
void errmsg_expected(void)
802
 
{
803
 
        fputs("cli_errmsg() expected here\n", stderr);
804
 
}
805
 
 
806
 
int open_testfile(const char *name)
807
 
{
808
 
        int fd;
809
 
        const char * srcdir = getenv("srcdir");
810
 
        char *str;
811
 
 
812
 
        if(!srcdir) {
813
 
                /* when run from automake srcdir is set, but if run manually then not */
814
 
                srcdir = SRCDIR;
815
 
        }
816
 
 
817
 
        str = cli_malloc(strlen(name)+strlen(srcdir)+2);
818
 
        fail_unless(!!str, "cli_malloc");
819
 
        sprintf(str, "%s/%s", srcdir, name);
820
 
 
821
 
        fd = open(str, O_RDONLY);
822
 
        fail_unless_fmt(fd >= 0, "open() failed: %s", str);
823
 
        free(str);
824
 
        return fd;
825
 
}
826
 
 
827
 
void diff_file_mem(int fd, const char *ref, size_t len)
828
 
{
829
 
        char c1,c2;
830
 
        size_t p, reflen = len;
831
 
        char *buf = cli_malloc(len);
832
 
 
833
 
        fail_unless_fmt(!!buf, "unable to malloc buffer: %d", len);
834
 
        p = read(fd, buf, len);
835
 
        fail_unless_fmt(p == len,  "file is smaller: %lu, expected: %lu", p, len);
836
 
        p = 0;
837
 
        while(len > 0) {
838
 
                c1 = ref[p];
839
 
                c2 = buf[p];
840
 
                if(c1 != c2)
841
 
                        break;
842
 
                p++;
843
 
                len--;
844
 
        }
845
 
        if (len > 0)
846
 
                fail_unless_fmt(c1 == c2, "file contents mismatch at byte: %lu, was: %c, expected: %c", p, c2, c1);
847
 
        free(buf);
848
 
        p = lseek(fd, 0, SEEK_END);
849
 
        fail_unless_fmt(p == reflen, "trailing garbage, file size: %ld, expected: %ld", p, reflen);
850
 
        close(fd);
851
 
}
852
 
 
853
 
void diff_files(int fd, int ref_fd)
854
 
{
855
 
        char *ref;
856
 
        ssize_t nread;
857
 
        off_t siz = lseek(ref_fd, 0, SEEK_END);
858
 
        fail_unless_fmt(siz != -1, "lseek failed");
859
 
 
860
 
        ref = cli_malloc(siz);
861
 
        fail_unless_fmt(!!ref, "unable to malloc buffer: %d", siz);
862
 
 
863
 
        fail_unless_fmt(lseek(ref_fd, 0, SEEK_SET) == 0,"lseek failed");
864
 
        nread = read(ref_fd, ref, siz);
865
 
        fail_unless_fmt(nread == siz, "short read, expected: %ld, was: %ld", siz, nread);
866
 
        close(ref_fd);
867
 
        diff_file_mem(fd, ref, siz);
868
 
        free(ref);
869
 
}
870
 
 
871
 
#ifdef USE_MPOOL
872
 
static mpool_t *pool;
873
 
#else
874
 
static void *pool;
875
 
#endif
876
 
struct cli_dconf *dconf;
877
 
 
878
 
void dconf_setup(void)
879
 
{
880
 
        pool = NULL;
881
 
        dconf = NULL;
882
 
#ifdef USE_MPOOL
883
 
        pool = mpool_create();
884
 
        fail_unless(!!pool, "unable to create pool");
885
 
#endif
886
 
        dconf = cli_mpool_dconf_init(pool);
887
 
        fail_unless(!!dconf, "failed to init dconf");
888
 
}
889
 
 
890
 
void dconf_teardown(void)
891
 
{
892
 
        mpool_free(pool, dconf);
893
 
#ifdef USE_MPOOL
894
 
        if (pool)
895
 
                mpool_destroy(pool);
896
 
#endif
897
 
}
898
 
 
899
 
static void check_version_compatible()
900
 
{
901
 
    /* check 0.9.8 is not ABI compatible with 0.9.6,
902
 
     * if by accident you compile with check 0.9.6 header
903
 
     * and link with 0.9.8 then check will hang/crash. */
904
 
    if ((check_major_version != CHECK_MAJOR_VERSION) ||
905
 
        (check_minor_version != CHECK_MINOR_VERSION) ||
906
 
        (check_micro_version != CHECK_MICRO_VERSION)) {
907
 
        fprintf(stderr, "ERROR: check version mismatch!\n"
908
 
                "\tVersion from header: %u.%u.%u\n"
909
 
                "\tVersion from library: %u.%u.%u\n"
910
 
                "\tMake sure check.h and -lcheck are same version!\n",
911
 
                CHECK_MAJOR_VERSION,
912
 
                CHECK_MINOR_VERSION,
913
 
                CHECK_MICRO_VERSION,
914
 
                check_major_version,
915
 
                check_minor_version,
916
 
                check_micro_version);
917
 
        exit(EXIT_FAILURE);
918
 
    }
919
 
}
920
 
 
921
 
int main(void)
922
 
{
923
 
    int nf;
924
 
    Suite *s;
925
 
    SRunner *sr;
926
 
 
927
 
    check_version_compatible();
928
 
    s = test_cl_suite();
929
 
    sr = srunner_create(s);
930
 
#ifdef CHECK_HAVE_LOOPS
931
 
    srunner_add_suite(sr, test_cli_suite());
932
 
#else
933
 
    printf("*** Warning ***: your check version is too old,\nseveral important tests will not execute\n");
934
 
#endif
935
 
    srunner_add_suite(sr, test_jsnorm_suite());
936
 
    srunner_add_suite(sr, test_str_suite());
937
 
    srunner_add_suite(sr, test_regex_suite());
938
 
    srunner_add_suite(sr, test_disasm_suite());
939
 
    srunner_add_suite(sr, test_uniq_suite());
940
 
    srunner_add_suite(sr, test_matchers_suite());
941
 
    srunner_add_suite(sr, test_htmlnorm_suite());
942
 
    srunner_add_suite(sr, test_bytecode_suite());
943
 
 
944
 
 
945
 
    srunner_set_log(sr, "test.log");
946
 
    if(freopen("test-stderr.log","w+",stderr) == NULL) {
947
 
            fputs("Unable to redirect stderr!\n",stderr);
948
 
    }
949
 
    cl_debug();
950
 
 
951
 
    srunner_run_all(sr, CK_NORMAL);
952
 
    nf = srunner_ntests_failed(sr);
953
 
    srunner_free(sr);
954
 
 
955
 
    return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
956
 
}