Browse code

unit tests for uniq

git-svn: trunk@4074

aCaB authored on 2008/08/04 09:01:14
Showing 8 changed files
... ...
@@ -1,3 +1,7 @@
1
+Mon Aug  4 01:46:54 CEST 2008 (acab)
2
+------------------------------------
3
+  * unit_tests: add uniq checks
4
+
1 5
 Mon Aug  4 00:20:28 CEST 2008 (acab)
2 6
 ------------------------------------
3 7
   * test: add clam.ole.doc
... ...
@@ -103,6 +103,10 @@ CLAMAV_PRIVATE {
103 103
     load_regex_matcher;
104 104
     html_tag_arg_free;
105 105
     disasmbuf;
106
+    uniq_init;
107
+    uniq_free;
108
+    uniq_add;
109
+    uniq_get;
106 110
     cli_hex2str;
107 111
   local:
108 112
     *;
... ...
@@ -16,7 +16,7 @@ else
16 16
 check_PROGRAMS = $(programs)
17 17
 check_SCRIPTS = $(scripts)
18 18
 endif
19
-check_clamav_SOURCES = check_clamav.c check_jsnorm.c check_str.c check_regex.c checks.h $(top_builddir)/libclamav/clamav.h check_disasm.c
19
+check_clamav_SOURCES = check_clamav.c check_jsnorm.c check_str.c check_regex.c checks.h $(top_builddir)/libclamav/clamav.h check_disasm.c check_uniq.c
20 20
 check_clamav_CFLAGS = @CHECK_CFLAGS@ -DSRCDIR=\"$(abs_srcdir)\"
21 21
 check_clamav_LDADD = $(top_builddir)/libclamav/libclamav.la @THREAD_LIBS@ @CHECK_LIBS@
22 22
 
... ...
@@ -57,7 +57,8 @@ am_check_clamav_OBJECTS = check_clamav-check_clamav.$(OBJEXT) \
57 57
 	check_clamav-check_jsnorm.$(OBJEXT) \
58 58
 	check_clamav-check_str.$(OBJEXT) \
59 59
 	check_clamav-check_regex.$(OBJEXT) \
60
-	check_clamav-check_disasm.$(OBJEXT)
60
+	check_clamav-check_disasm.$(OBJEXT) \
61
+	check_clamav-check_uniq.$(OBJEXT)
61 62
 check_clamav_OBJECTS = $(am_check_clamav_OBJECTS)
62 63
 check_clamav_DEPENDENCIES = $(top_builddir)/libclamav/libclamav.la
63 64
 check_clamav_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
... ...
@@ -223,7 +224,7 @@ programs = check_clamav
223 223
 scripts = check_clamd.sh check_freshclam.sh check_sigtool.sh check_clamscan.sh valgrind_tests.sh
224 224
 @ENABLE_UT_INSTALL_TRUE@dist_bin_SCRIPTS = $(scripts)
225 225
 @ENABLE_UT_INSTALL_FALSE@check_SCRIPTS = $(scripts)
226
-check_clamav_SOURCES = check_clamav.c check_jsnorm.c check_str.c check_regex.c checks.h $(top_builddir)/libclamav/clamav.h check_disasm.c
226
+check_clamav_SOURCES = check_clamav.c check_jsnorm.c check_str.c check_regex.c checks.h $(top_builddir)/libclamav/clamav.h check_disasm.c check_uniq.c
227 227
 check_clamav_CFLAGS = @CHECK_CFLAGS@ -DSRCDIR=\"$(abs_srcdir)\"
228 228
 check_clamav_LDADD = $(top_builddir)/libclamav/libclamav.la @THREAD_LIBS@ @CHECK_LIBS@
229 229
 CLEANFILES = lcov.out *.gcno *.gcda *.log /tmp/clamd-test.log $(FILES) test-stderr.log clamscan.log valgrind.log clamdscan.log
... ...
@@ -367,6 +368,7 @@ distclean-compile:
367 367
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check_clamav-check_jsnorm.Po@am__quote@
368 368
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check_clamav-check_regex.Po@am__quote@
369 369
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check_clamav-check_str.Po@am__quote@
370
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check_clamav-check_uniq.Po@am__quote@
370 371
 
371 372
 .c.o:
372 373
 @am__fastdepCC_TRUE@	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
... ...
@@ -459,6 +461,20 @@ check_clamav-check_disasm.obj: check_disasm.c
459 459
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
460 460
 @am__fastdepCC_FALSE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_clamav_CFLAGS) $(CFLAGS) -c -o check_clamav-check_disasm.obj `if test -f 'check_disasm.c'; then $(CYGPATH_W) 'check_disasm.c'; else $(CYGPATH_W) '$(srcdir)/check_disasm.c'; fi`
461 461
 
462
+check_clamav-check_uniq.o: check_uniq.c
463
+@am__fastdepCC_TRUE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_clamav_CFLAGS) $(CFLAGS) -MT check_clamav-check_uniq.o -MD -MP -MF $(DEPDIR)/check_clamav-check_uniq.Tpo -c -o check_clamav-check_uniq.o `test -f 'check_uniq.c' || echo '$(srcdir)/'`check_uniq.c
464
+@am__fastdepCC_TRUE@	mv -f $(DEPDIR)/check_clamav-check_uniq.Tpo $(DEPDIR)/check_clamav-check_uniq.Po
465
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='check_uniq.c' object='check_clamav-check_uniq.o' libtool=no @AMDEPBACKSLASH@
466
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
467
+@am__fastdepCC_FALSE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_clamav_CFLAGS) $(CFLAGS) -c -o check_clamav-check_uniq.o `test -f 'check_uniq.c' || echo '$(srcdir)/'`check_uniq.c
468
+
469
+check_clamav-check_uniq.obj: check_uniq.c
470
+@am__fastdepCC_TRUE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_clamav_CFLAGS) $(CFLAGS) -MT check_clamav-check_uniq.obj -MD -MP -MF $(DEPDIR)/check_clamav-check_uniq.Tpo -c -o check_clamav-check_uniq.obj `if test -f 'check_uniq.c'; then $(CYGPATH_W) 'check_uniq.c'; else $(CYGPATH_W) '$(srcdir)/check_uniq.c'; fi`
471
+@am__fastdepCC_TRUE@	mv -f $(DEPDIR)/check_clamav-check_uniq.Tpo $(DEPDIR)/check_clamav-check_uniq.Po
472
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='check_uniq.c' object='check_clamav-check_uniq.obj' libtool=no @AMDEPBACKSLASH@
473
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
474
+@am__fastdepCC_FALSE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_clamav_CFLAGS) $(CFLAGS) -c -o check_clamav-check_uniq.obj `if test -f 'check_uniq.c'; then $(CYGPATH_W) 'check_uniq.c'; else $(CYGPATH_W) '$(srcdir)/check_uniq.c'; fi`
475
+
462 476
 mostlyclean-libtool:
463 477
 	-rm -f *.lo
464 478
 
... ...
@@ -342,6 +342,7 @@ int main(int argc, char **argv)
342 342
     srunner_add_suite(sr, test_str_suite());
343 343
     srunner_add_suite(sr, test_regex_suite());
344 344
     srunner_add_suite(sr, test_disasm_suite());
345
+    srunner_add_suite(sr, test_uniq_suite());
345 346
 
346 347
     srunner_set_log(sr, "test.log");
347 348
     if(freopen("test-stderr.log","w+",stderr) == NULL) {
... ...
@@ -3,7 +3,7 @@
3 3
  *
4 4
  *  Copyright (C) 2008 Sourcefire, Inc.
5 5
  *
6
- *  Authors: Török Edvin
6
+ *  Authors: aCaB <acab@clamav.net>
7 7
  *
8 8
  *  This program is free software; you can redistribute it and/or modify
9 9
  *  it under the terms of the GNU General Public License version 2 as
10 10
new file mode 100644
... ...
@@ -0,0 +1,111 @@
0
+/*
1
+ *  Unit tests for JS normalizer.
2
+ *
3
+ *  Copyright (C) 2008 Sourcefire, Inc.
4
+ *
5
+ *  Authors: aCaB <acab@clamav.net>
6
+ *
7
+ *  This program is free software; you can redistribute it and/or modify
8
+ *  it under the terms of the GNU General Public License version 2 as
9
+ *  published by the Free Software Foundation.
10
+ *
11
+ *  This program is distributed in the hope that it will be useful,
12
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ *  GNU General Public License for more details.
15
+ *
16
+ *  You should have received a copy of the GNU General Public License
17
+ *  along with this program; if not, write to the Free Software
18
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ *  MA 02110-1301, USA.
20
+ */
21
+#if HAVE_CONFIG_H
22
+#include "clamav-config.h"
23
+#endif
24
+
25
+#ifdef HAVE_CHECK
26
+
27
+#include <check.h>
28
+#include <stdlib.h>
29
+#include <string.h>
30
+
31
+#include "../libclamav/uniq.h"
32
+#include "checks.h"
33
+
34
+START_TEST (test_uniq_initfail) {
35
+  struct uniq *U;
36
+  U = uniq_init(0);
37
+  fail_unless(U==NULL, "uniq_init(0)!=NULL");
38
+}
39
+END_TEST
40
+
41
+START_TEST (test_uniq_known) {
42
+  char *hash;
43
+  uint32_t u;
44
+  struct {
45
+    const char *key;
46
+    const uint32_t key_len;
47
+    const char *expected;
48
+  } tests[] = {
49
+    { NULL, 0, "d41d8cd98f00b204e9800998ecf8427e" }, 
50
+    { "_vba_project", 12, "ae4f6474bee50ccdf1a6b853ba8ad32a" },
51
+    { "powerpoint document", 19, "87320d137f01f7b183eb533a1de6c62a" },
52
+    { "worddocument", 12, "126ea3fd0ff7f18c9c5eec0c07398c49" },
53
+    { "_1_ole10native", 14, "e74f5f7bbf0b77708bc591157d708d3d" },
54
+    { NULL, 0, NULL }
55
+  };
56
+  int i;
57
+
58
+  struct uniq *U = uniq_init(5);
59
+  fail_unless(U!=0, "uniq_init");
60
+
61
+  for(i=0; tests[i].expected; i++) {
62
+    u = uniq_add(U, tests[i].key, tests[i].key_len, &hash);
63
+    fail_unless(u==0 && strcmp(hash, tests[i].expected)==0, "uniq_add(%s) = %u - expected %s, got %s", tests[i].key, u, tests[i].expected, hash);
64
+  }
65
+
66
+  for(i=0; tests[i].expected; i++) {
67
+    u = uniq_get(U, tests[i].key, tests[i].key_len, &hash);
68
+    fail_unless(u==1 && strcmp(hash, tests[i].expected)==0, "uniq_get(%s) = %u - expected %s, got %s", tests[i].key, u, tests[i].expected, hash);
69
+  }
70
+
71
+  uniq_free(U);
72
+}
73
+END_TEST
74
+
75
+
76
+START_TEST (test_uniq_colls) {
77
+  uint32_t u;
78
+  const char *tests[] = { "_vba_project", "powerpoint document", "worddocument", "_1_ole10native" };
79
+  int i, j;
80
+
81
+  struct uniq *U = uniq_init(10);
82
+  fail_unless(U!=0, "uniq_init");
83
+
84
+  for(j=4; j>0; j--)
85
+    for (i=0; i<j; i++)
86
+      u = uniq_add(U, tests[i], strlen(tests[i]), NULL);
87
+  
88
+  for (i=0; i<4; i++) {
89
+    u = uniq_add(U, tests[i], strlen(tests[i]), NULL);
90
+    fail_unless(u+i==4, "uniq_get(%s) = %u - expected %u", tests[i], u, 4-i);
91
+  }
92
+
93
+  uniq_free(U);
94
+}
95
+END_TEST
96
+
97
+Suite *test_uniq_suite(void)
98
+{
99
+    Suite *s = suite_create("unique");
100
+    TCase *tc_uniq;
101
+    tc_uniq = tcase_create("unique");
102
+    suite_add_tcase (s, tc_uniq);
103
+    tcase_add_test(tc_uniq, test_uniq_initfail);
104
+    tcase_add_test(tc_uniq, test_uniq_known);
105
+    tcase_add_test(tc_uniq, test_uniq_colls);
106
+    return s;
107
+}
108
+
109
+
110
+#endif
... ...
@@ -4,6 +4,7 @@ Suite *test_jsnorm_suite(void);
4 4
 Suite *test_str_suite(void);
5 5
 Suite *test_regex_suite(void);
6 6
 Suite *test_disasm_suite(void);
7
+Suite *test_uniq_suite(void);
7 8
 void errmsg_expected(void);
8 9
 int open_testfile(const char *name);
9 10
 #endif