Browse code

fix unit tests when mpool is activated

git-svn: trunk@4323

Török Edvin authored on 2008/11/04 04:26:57
Showing 12 changed files
... ...
@@ -1,3 +1,12 @@
1
+Sat Nov  1 19:52:25 EET 2008 (edwin)
2
+------------------------------------
3
+ * libclamav/libclamav.map, libclamav/matcher-ac.c,
4
+ libclamav/matcher-bm.c, libclamav/regex_list.c,
5
+ unit_tests/check_clamav.c, unit_tests/check_htmlnorm.c,
6
+ unit_tests/check_jsnorm.c, unit_tests/check_matchers.c,
7
+ unit_tests/check_regex.c, unit_tests/checks.h: fix unit tests when
8
+ mpool is activated
9
+
1 10
 Sat Nov  1 03:51:58 CET 2008 (acab)
2 11
 -----------------------------------
3 12
  * mempool: New configure option --enable-mempool (requires mmap)
4 13
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+monitor more than one remote clamd
1
+write a manpage
2
+consider writing a GUI using Tk 8.5 (version 8.5 has much nicer widgets than 8.4)
... ...
@@ -118,6 +118,9 @@ CLAMAV_PRIVATE {
118 118
     cli_bm_scanbuff;
119 119
     cli_bm_free;
120 120
     html_screnc_decode;
121
+    mp_create;
122
+    mp_destroy;
123
+    mp_free;
121 124
   local:
122 125
     *;
123 126
 };
... ...
@@ -26,6 +26,13 @@
26 26
 #include <string.h>
27 27
 #include <stdlib.h>
28 28
 #include <ctype.h>
29
+
30
+/* TODO: this should be in a common header */
31
+#ifndef CL_DEBUG
32
+#define NDEBUG
33
+#endif
34
+
35
+#include <assert.h>
29 36
 #ifdef	HAVE_UNISTD_H
30 37
 #include <unistd.h>
31 38
 #endif
... ...
@@ -300,6 +307,9 @@ int cli_ac_buildtrie(struct cli_matcher *root)
300 300
 
301 301
 int cli_ac_init(struct cli_matcher *root, uint8_t mindepth, uint8_t maxdepth)
302 302
 {
303
+#ifdef USE_MPOOL
304
+    assert(root->mempool && "mempool must be initialized");
305
+#endif
303 306
 
304 307
     root->ac_root = (struct cli_ac_node *) mp_calloc(root->mempool, 1, sizeof(struct cli_ac_node));
305 308
     if(!root->ac_root) {
... ...
@@ -22,8 +22,11 @@
22 22
 #include "clamav-config.h"
23 23
 #endif
24 24
 
25
+#ifndef CL_DEBUG
26
+#define NDEBUG
27
+#endif
25 28
 #include <stdio.h>
26
-
29
+#include <assert.h>
27 30
 #include "clamav.h"
28 31
 #include "memory.h"
29 32
 #include "others.h"
... ...
@@ -98,7 +101,9 @@ int cli_bm_addpatt(struct cli_matcher *root, struct cli_bm_patt *pattern)
98 98
 int cli_bm_init(struct cli_matcher *root)
99 99
 {
100 100
 	uint16_t i, size = HASH(255, 255, 255) + 1;
101
-
101
+#ifdef USE_MPOOL
102
+    assert (root->mempool && "mempool must be initialized");
103
+#endif
102 104
     if(!(root->bm_shift = (uint8_t *) mp_calloc(root->mempool, size, sizeof(uint8_t))))
103 105
 	return CL_EMEM;
104 106
 
... ...
@@ -360,6 +360,7 @@ int init_regex_list(struct regex_matcher* matcher)
360 360
 #ifdef USE_MPOOL
361 361
 	matcher->mempool = mp;
362 362
 	matcher->suffixes.mempool = mp;
363
+	assert(mp && "mempool must be initialized");
363 364
 #endif
364 365
 	if((rc = cli_ac_init(&matcher->suffixes, 2, 32))) {
365 366
 		return rc;
... ...
@@ -375,6 +375,35 @@ void diff_files(int fd, int ref_fd)
375 375
 	free(ref);
376 376
 }
377 377
 
378
+#ifdef USE_MEMPOOL
379
+static mp_t *pool;
380
+#else
381
+static void *pool;
382
+#endif
383
+struct cli_dconf *dconf;
384
+
385
+void dconf_setup(void)
386
+{
387
+	pool = NULL;
388
+	dconf = NULL;
389
+#ifdef USE_MEMPOOL
390
+	pool = mp_create();
391
+	fail_unless(!!pool, "unable to create pool");
392
+#endif
393
+	dconf = cli_mp_dconf_init(pool);
394
+	fail_unless(!!dconf, "failed to init dconf");
395
+}
396
+
397
+void dconf_teardown(void)
398
+{
399
+	mp_free(pool, dconf);
400
+#ifdef USE_MEMPOOL
401
+	if (pool)
402
+		mp_destroy(pool);
403
+#endif
404
+}
405
+
406
+
378 407
 int main(int argc, char **argv)
379 408
 {
380 409
     int nf;
... ...
@@ -28,19 +28,17 @@
28 28
 #include "../libclamav/others.h"
29 29
 
30 30
 static char *dir;
31
-static struct cli_dconf *dconf;
32 31
 
33 32
 static void htmlnorm_setup(void)
34 33
 {
35
-	dconf = cli_dconf_init();
34
+	dconf_setup();
36 35
 	dir = cli_gentemp(NULL);
37
-	fail_unless(!!dconf, "failed to init dconf");
38 36
 	fail_unless(!!dir, "cli_gentemp failed");
39 37
 }
40 38
 
41 39
 static void htmlnorm_teardown(void)
42 40
 {
43
-	free(dconf);
41
+	dconf_teardown();
44 42
 	fail_unless(cli_rmdirs(dir) == 0, "rmdirs failed");
45 43
 	free(dir);
46 44
 	dir = NULL;
... ...
@@ -180,16 +180,13 @@ START_TEST (js_begin_end)
180 180
 {
181 181
 	char buf[16384] = "</script>";
182 182
 	size_t p;
183
-	struct cli_dconf *dconf = cli_dconf_init();
184 183
 
185
-	fail_unless(!!dconf, "failed to init dconf");
186 184
 	for(p=strlen(buf); p < 8191; p++) {
187 185
 		buf[p++] = 'a';
188 186
 		buf[p] = ' ';
189 187
 	}
190 188
 	strncpy(buf + 8192, " stuff stuff <script language='javascript'> function () {}", 8192);
191 189
 	fail_unless(html_normalise_mem((unsigned char*)buf, sizeof(buf), NULL, NULL, dconf) == 1, "normalise");
192
-	free(dconf);
193 190
 }
194 191
 END_TEST
195 192
 
... ...
@@ -198,12 +195,10 @@ START_TEST (multiple_scripts)
198 198
 	char buf[] = "</script> stuff"\
199 199
 			    "<script language='Javascript'> function foo() {} </script>"\
200 200
 			    "<script language='Javascript'> function bar() {} </script>";
201
-	struct cli_dconf *dconf = cli_dconf_init();
202 201
 
203 202
 	fail_unless(!!dconf, "failed to init dconf");
204 203
 	fail_unless(html_normalise_mem((unsigned char*)buf, sizeof(buf), NULL, NULL, dconf) == 1, "normalise");
205 204
 	/* TODO: test that both had been normalized */
206
-	free(dconf);
207 205
 }
208 206
 END_TEST
209 207
 
... ...
@@ -427,7 +422,6 @@ END_TEST
427 427
 START_TEST (screnc_infloop)
428 428
 {
429 429
 	char buf[24700] = "<%@ language='jscript.encode'>";
430
-	struct cli_dconf *dconf = cli_dconf_init();
431 430
 	size_t p;
432 431
 
433 432
 	fail_unless(!!dconf, "failed to init dconf");
... ...
@@ -439,7 +433,6 @@ START_TEST (screnc_infloop)
439 439
 	}
440 440
 	strncpy(buf+24626,"#@~^ ", 10);
441 441
 	fail_unless(html_normalise_mem((unsigned char*)buf, sizeof(buf), NULL, NULL, dconf) == 1, "normalise");
442
-	free(dconf);
443 442
 }
444 443
 END_TEST
445 444
 
... ...
@@ -497,14 +490,12 @@ Suite *test_jsnorm_suite(void)
497 497
 #endif
498 498
     tcase_add_test(tc_jsnorm_tokenizer, js_buffer);
499 499
 
500
-    tc_jsnorm_bugs = tcase_create("jsnorm bugs");
500
+    tc_jsnorm_bugs = tcase_create("bugs");
501 501
     suite_add_tcase (s, tc_jsnorm_bugs);
502
+    tcase_add_checked_fixture(tc_jsnorm_bugs, dconf_setup, dconf_teardown);
502 503
     tcase_add_test(tc_jsnorm_bugs, js_begin_end);
503 504
     tcase_add_test(tc_jsnorm_bugs, multiple_scripts);
504
-
505
-    tc_screnc_infloop = tcase_create("screnc infloop bug");
506
-    suite_add_tcase (s, tc_screnc_infloop);
507
-    tcase_add_test(tc_screnc_infloop, screnc_infloop);
505
+    tcase_add_test(tc_jsnorm_bugs, screnc_infloop);
508 506
 
509 507
     return s;
510 508
 }
... ...
@@ -61,6 +61,9 @@ START_TEST (test_ac_scanbuff) {
61 61
     fail_unless(root != NULL, "root == NULL");
62 62
     root->ac_only = 1;
63 63
 
64
+#ifdef USE_MEMPOOL
65
+    root->mempool = mp_create();
66
+#endif
64 67
     ret = cli_ac_init(root, AC_DEFAULT_MIN_DEPTH, AC_DEFAULT_MAX_DEPTH);
65 68
     fail_unless(ret == CL_SUCCESS, "cli_ac_init() failed");
66 69
 
... ...
@@ -83,6 +86,9 @@ START_TEST (test_ac_scanbuff) {
83 83
 
84 84
     cli_ac_freedata(&mdata);
85 85
     cli_ac_free(root);
86
+#ifdef USE_MEMPOOL
87
+    mp_destroy(root->mempool);
88
+#endif
86 89
     free(root);
87 90
 }
88 91
 END_TEST
... ...
@@ -96,6 +102,9 @@ START_TEST (test_bm_scanbuff) {
96 96
     root = (struct cli_matcher *) cli_calloc(1, sizeof(struct cli_matcher));
97 97
     fail_unless(root != NULL, "root == NULL");
98 98
 
99
+#ifdef USE_MEMPOOL
100
+    root->mempool = mp_create();
101
+#endif
99 102
     ret = cli_bm_init(root);
100 103
     fail_unless(ret == CL_SUCCESS, "cli_bm_init() failed");
101 104
 
... ...
@@ -110,6 +119,9 @@ START_TEST (test_bm_scanbuff) {
110 110
     fail_unless(ret == CL_VIRUS, "cli_bm_scanbuff() failed");
111 111
     fail_unless(!strncmp(virname, "Sig2", 4), "Incorrect signature matched in cli_bm_scanbuff()\n");
112 112
     cli_bm_free(root);
113
+#ifdef USE_MEMPOOL
114
+    mp_destroy(root->mempool);
115
+#endif
113 116
     free(root);
114 117
 }
115 118
 END_TEST
... ...
@@ -153,13 +153,20 @@ static struct regex_matcher matcher;
153 153
 
154 154
 static void rsetup(void)
155 155
 {
156
-	int rc = init_regex_list(&matcher);
156
+	int rc;
157
+#ifdef USE_MEMPOOL
158
+	matcher.mempool = mp_create();
159
+#endif
160
+	rc = init_regex_list(&matcher);
157 161
 	fail_unless(rc == 0, "init_regex_list");
158 162
 }
159 163
 
160 164
 static void rteardown(void)
161 165
 {
162 166
 	regex_list_done(&matcher);
167
+#ifdef USE_MEMPOOL
168
+	mp_destroy(matcher.mempool);
169
+#endif
163 170
 }
164 171
 
165 172
 static const struct rtest {
... ...
@@ -25,4 +25,8 @@ int open_testfile(const char *name);
25 25
 void diff_files(int fd, int reffd);
26 26
 void diff_file_mem(int fd, const char *ref, size_t len);
27 27
 
28
+extern struct cli_dconf *dconf;
29
+void dconf_setup(void);
30
+void dconf_teardown(void);
31
+
28 32
 #endif