2
#include "clamav-config.h"
12
#include <sys/types.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"
23
/* extern void cl_free(struct cl_engine *engine); */
24
START_TEST (test_cl_free)
26
struct cl_engine *engine = NULL;
31
/* extern struct cl_engine *cl_dup(struct cl_engine *engine); */
32
START_TEST (test_cl_dup)
34
struct cl_engine *engine;
35
fail_unless(NULL == cl_dup(NULL), "cl_dup null pointer");
39
/* extern int cl_build(struct cl_engine *engine); */
40
START_TEST (test_cl_build)
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");
48
/* engine->root = cli_calloc(CL_TARGET_TABLE_SIZE, sizeof(struct cli_matcher *)); */
51
/* extern void cl_debug(void); */
52
START_TEST (test_cl_debug)
54
int old_status = cli_debug_flag;
57
fail_unless(1 == cli_debug_flag, "cl_debug failed to set cli_debug_flag");
61
fail_unless(1 == cli_debug_flag, "cl_debug failed when flag was already set");
62
cli_debug_flag = old_status;
66
/* extern const char *cl_retdbdir(void); */
67
START_TEST (test_cl_retdbdir)
68
fail_unless(!strcmp(DATADIR, cl_retdbdir()), "cl_retdbdir");
72
#define REPO_VERSION VERSION
75
/* extern const char *cl_retver(void); */
76
START_TEST (test_cl_retver)
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");
85
/* extern void cl_cvdfree(struct cl_cvd *cvd); */
86
START_TEST (test_cl_cvdfree)
88
struct cl_cvd *cvd1, *cvd2;
90
cvd1 = malloc(sizeof(struct cl_cvd));
91
fail_unless(cvd1, "cvd malloc");
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");
109
/* extern int cl_statfree(struct cl_stat *dbstat); */
110
START_TEST (test_cl_statfree)
112
struct cl_stat *stat;
113
fail_unless(CL_ENULLARG == cl_statfree(NULL), "cl_statfree(NULL)");
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)");
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)");
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");
132
/* extern unsigned int cl_retflevel(void); */
133
START_TEST (test_cl_retflevel)
136
/* extern struct cl_cvd *cl_cvdhead(const char *file); */
137
START_TEST (test_cl_cvdhead)
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)");
142
/* the data read from the file is passed to cl_cvdparse, test cases for that are separate */
145
/* extern struct cl_cvd *cl_cvdparse(const char *head); */
146
START_TEST (test_cl_cvdparse)
149
static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size);
150
static struct cl_engine *g_engine;
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)
156
const char *virname = NULL;
159
unsigned long int scanned = 0;
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);
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);
173
START_TEST (test_cl_scandesc_allscan)
175
const char *virname = NULL;
176
const char ** virpp = &virname;
179
unsigned long int scanned = 0;
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);
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);
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)
198
const char *virname = NULL;
201
unsigned long int scanned = 0;
204
int fd = get_test_file(_i, file, sizeof(file), &size);
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);
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);
216
START_TEST (test_cl_scanfile_allscan)
218
const char *virname = NULL;
219
const char **virpp = &virname;
222
unsigned long int scanned = 0;
225
int fd = get_test_file(_i, file, sizeof(file), &size);
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);
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);
239
START_TEST (test_cl_scanfile_callback)
241
const char *virname = NULL;
244
unsigned long int scanned = 0;
247
int fd = get_test_file(_i, file, sizeof(file), &size);
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);
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);
260
START_TEST (test_cl_scanfile_callback_allscan)
262
const char *virname = NULL;
263
const char **virpp = &virname;
266
unsigned long int scanned = 0;
269
int fd = get_test_file(_i, file, sizeof(file), &size);
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);
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);
284
START_TEST (test_cl_scandesc_callback)
286
const char *virname = NULL;
289
unsigned long int scanned = 0;
292
int fd = get_test_file(_i, file, sizeof(file), &size);
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);
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);
305
START_TEST (test_cl_scandesc_callback_allscan)
307
const char *virname = NULL;
308
const char **virpp = &virname;
311
unsigned long int scanned = 0;
314
int fd = get_test_file(_i, file, sizeof(file), &size);
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);
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);
331
/* int cl_load(const char *path, struct cl_engine **engine, unsigned int *signo, unsigned int options) */
332
START_TEST (test_cl_load)
335
/* int cl_cvdverify(const char *file) */
336
START_TEST (test_cl_cvdverify)
339
/* int cl_statinidir(const char *dirname, struct cl_stat *dbstat) */
340
START_TEST (test_cl_statinidir)
343
/* int cl_statchkdir(const struct cl_stat *dbstat) */
344
START_TEST (test_cl_statchkdir)
347
/* void cl_settempdir(const char *dir, short leavetemps) */
348
START_TEST (test_cl_settempdir)
351
/* const char *cl_strerror(int clerror) */
352
START_TEST (test_cl_strerror)
355
static char **testfiles = NULL;
356
static unsigned testfiles_n = 0;
359
static const int expected_testfiles = 48;
361
static const int expected_testfiles = 46;
364
static void init_testfiles(void)
366
struct dirent *dirent;
369
DIR *d = opendir(OBJDIR"/../test");
370
fail_unless(!!d, "opendir");
375
while ((dirent = readdir(d))) {
376
if (strncmp(dirent->d_name, "clam", 4))
379
testfiles = cli_realloc(testfiles, i*sizeof(*testfiles));
380
fail_unless(!!testfiles, "cli_realloc");
381
testfiles[i-1] = strdup(dirent->d_name);
384
fail_unless_fmt(testfiles_n == expected_testfiles, "testfiles: %d != %d", testfiles_n, expected_testfiles);
389
static void free_testfiles(void)
392
for (i=0;i<testfiles_n;i++) {
400
static int inited = 0;
402
static void engine_setup(void)
404
unsigned int sigs = 0;
405
const char *hdb = OBJDIR"/clamav.hdb";
409
fail_unless(cl_init(CL_INIT_DEFAULT) == 0, "cl_init");
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");
418
static void engine_teardown(void)
421
cl_engine_free(g_engine);
424
static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size)
429
fail_unless(i < testfiles_n);
430
snprintf(file, fsize, OBJDIR"/../test/%s", testfiles[i]);
432
fd = open(file, O_RDONLY);
433
fail_unless(fd > 0, "open");
434
fail_unless(FSTAT(fd, &st) == 0, "fstat");
438
#ifdef CHECK_HAVE_LOOPS
440
static off_t pread_cb(void *handle, void *buf, size_t count, off_t offset)
442
return pread(*((int*)handle), buf, count, offset);
445
START_TEST (test_cl_scanmap_callback_handle)
447
const char *virname = NULL;
448
unsigned long int scanned = 0;
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");
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);
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);
469
START_TEST (test_cl_scanmap_callback_handle_allscan)
471
const char *virname = NULL;
472
const char ** virpp = &virname;
473
unsigned long int scanned = 0;
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");
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);
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);
496
START_TEST (test_cl_scanmap_callback_mem)
498
const char *virname = NULL;
499
unsigned long int scanned = 0;
506
int fd = get_test_file(_i, file, sizeof(file), &size);
508
mem = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
509
fail_unless(mem != MAP_FAILED, "mmap");
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");
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);
527
START_TEST (test_cl_scanmap_callback_mem_allscan)
529
const char *virname = NULL;
530
const char **virpp = &virname;
531
unsigned long int scanned = 0;
538
int fd = get_test_file(_i, file, sizeof(file), &size);
540
mem = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
541
fail_unless(mem != MAP_FAILED, "mmap");
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");
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);
561
static Suite *test_cl_suite(void)
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);
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);
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;
610
static void data_setup(void)
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");
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);
626
memset(data2, 0, DATA_REP*sizeof(le_data));
629
static void data_teardown(void)
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)
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");
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");
654
/* test reading with different alignments, _i is parameter from tcase_add_loop_test */
655
START_TEST (test_cli_readint32)
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");
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");
671
/* test writing with different alignments, _i is parameter from tcase_add_loop_test */
672
START_TEST (test_cli_writeint32)
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);
679
for(j=_i;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
680
fail_unless(cli_readint32(&data2[j]) == 0x12345678, "write/read mismatch");
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);
686
for(j=0;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
687
fail_unless(cli_readint32(&data2[j]) == 0x12345678, "write/read mismatch");
692
static struct dsig_test {
697
{"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
699
{"96b7feb3b2a863846438809fe481906f", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MXYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
701
{"ae307614434715274c60854c931a26de", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MXYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
703
{"96b7feb3b2a863846438809fe481906f", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
705
{"ae307614434715274060854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
707
{"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaatinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
709
{"96b7feb3b2a863846438809fe481906f", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MYYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
711
{"ge307614434715274c60854c931a26dee","60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
713
{"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGee",
715
{"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+",
719
static const size_t dsig_tests_cnt = sizeof(dsig_tests)/sizeof(dsig_tests[0]);
721
START_TEST (test_cli_dsig)
723
fail_unless(cli_versig(dsig_tests[_i].md5, dsig_tests[_i].dsig) == dsig_tests[_i].result,
724
"digital signature verification test failed");
728
static uint8_t tv1[3] = {
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
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 }
754
START_TEST (test_sha256)
757
uint8_t hsha256[SHA256_HASH_SIZE];
761
memset (buf, 0x61, sizeof (buf));
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");
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");
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");
781
static Suite *test_cli_suite(void)
783
Suite *s = suite_create("cli");
784
TCase *tc_cli_others = tcase_create("byteorder_macros");
785
TCase *tc_cli_dsig = tcase_create("digital signatures");
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);
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);
799
#endif /* CHECK_HAVE_LOOPS */
801
void errmsg_expected(void)
803
fputs("cli_errmsg() expected here\n", stderr);
806
int open_testfile(const char *name)
809
const char * srcdir = getenv("srcdir");
813
/* when run from automake srcdir is set, but if run manually then not */
817
str = cli_malloc(strlen(name)+strlen(srcdir)+2);
818
fail_unless(!!str, "cli_malloc");
819
sprintf(str, "%s/%s", srcdir, name);
821
fd = open(str, O_RDONLY);
822
fail_unless_fmt(fd >= 0, "open() failed: %s", str);
827
void diff_file_mem(int fd, const char *ref, size_t len)
830
size_t p, reflen = len;
831
char *buf = cli_malloc(len);
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);
846
fail_unless_fmt(c1 == c2, "file contents mismatch at byte: %lu, was: %c, expected: %c", p, c2, c1);
848
p = lseek(fd, 0, SEEK_END);
849
fail_unless_fmt(p == reflen, "trailing garbage, file size: %ld, expected: %ld", p, reflen);
853
void diff_files(int fd, int ref_fd)
857
off_t siz = lseek(ref_fd, 0, SEEK_END);
858
fail_unless_fmt(siz != -1, "lseek failed");
860
ref = cli_malloc(siz);
861
fail_unless_fmt(!!ref, "unable to malloc buffer: %d", siz);
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);
867
diff_file_mem(fd, ref, siz);
872
static mpool_t *pool;
876
struct cli_dconf *dconf;
878
void dconf_setup(void)
883
pool = mpool_create();
884
fail_unless(!!pool, "unable to create pool");
886
dconf = cli_mpool_dconf_init(pool);
887
fail_unless(!!dconf, "failed to init dconf");
890
void dconf_teardown(void)
892
mpool_free(pool, dconf);
899
static void check_version_compatible()
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",
916
check_micro_version);
927
check_version_compatible();
929
sr = srunner_create(s);
930
#ifdef CHECK_HAVE_LOOPS
931
srunner_add_suite(sr, test_cli_suite());
933
printf("*** Warning ***: your check version is too old,\nseveral important tests will not execute\n");
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());
945
srunner_set_log(sr, "test.log");
946
if(freopen("test-stderr.log","w+",stderr) == NULL) {
947
fputs("Unable to redirect stderr!\n",stderr);
951
srunner_run_all(sr, CK_NORMAL);
952
nf = srunner_ntests_failed(sr);
955
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;