git-svn: trunk@3931
Török Edvin authored on 2008/07/10 19:30:411 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,145 @@ |
0 |
+#if HAVE_CONFIG_H |
|
1 |
+#include "clamav-config.h" |
|
2 |
+#endif |
|
3 |
+ |
|
4 |
+#include <stdio.h> |
|
5 |
+#ifdef HAVE_CHECK |
|
6 |
+ |
|
7 |
+#include <stdlib.h> |
|
8 |
+#include <limits.h> |
|
9 |
+#include <string.h> |
|
10 |
+#include <check.h> |
|
11 |
+#include "../libclamav/clamav.h" |
|
12 |
+#include "../libclamav/others.h" |
|
13 |
+#include "../libclamav/str.h" |
|
14 |
+#include "../libclamav/jsparse/textbuf.h" |
|
15 |
+ |
|
16 |
+START_TEST (test_unescape_simple) |
|
17 |
+{ |
|
18 |
+ char *str = cli_unescape(""); |
|
19 |
+ fail_unless(str && strlen(str) == 0, "cli_unescape empty string"); |
|
20 |
+ free(str); |
|
21 |
+ |
|
22 |
+ str = cli_unescape("1"); |
|
23 |
+ fail_unless(str && !strcmp(str,"1"), "cli_unescape one char"); |
|
24 |
+ free(str); |
|
25 |
+ |
|
26 |
+ str = cli_unescape("tesT"); |
|
27 |
+ fail_unless(str && !strcmp(str,"tesT"), "cli_unescape simple string"); |
|
28 |
+ free(str); |
|
29 |
+} |
|
30 |
+END_TEST |
|
31 |
+ |
|
32 |
+START_TEST (test_unescape_hex) |
|
33 |
+{ |
|
34 |
+ char *str = cli_unescape("%5a"); |
|
35 |
+ fail_unless(str && !strcmp(str,"\x5a"), "cli_unescape hex"); |
|
36 |
+ free(str); |
|
37 |
+ |
|
38 |
+ str = cli_unescape("%b5%8"); |
|
39 |
+ fail_unless(str && !strcmp(str,"\xb5%8"), "cli_unescape truncated"); |
|
40 |
+ free(str); |
|
41 |
+ |
|
42 |
+ str = cli_unescape("%b5%"); |
|
43 |
+ fail_unless(str && !strcmp(str,"\xb5%"), "cli_unescape truncated/2"); |
|
44 |
+ free(str); |
|
45 |
+ |
|
46 |
+ str = cli_unescape("%00"); |
|
47 |
+ fail_unless(str && !strcmp(str,"\x1"), "cli_unescape %00"); |
|
48 |
+ free(str); |
|
49 |
+} |
|
50 |
+END_TEST |
|
51 |
+ |
|
52 |
+START_TEST (test_unescape_unicode) |
|
53 |
+{ |
|
54 |
+ char *str = cli_unescape("%u05D0"); |
|
55 |
+ /* unicode is converted to utf-8 representation */ |
|
56 |
+ fail_unless(str && !strcmp(str,"\xd7\x90"), "cli_unescape unicode aleph"); |
|
57 |
+ free(str); |
|
58 |
+ |
|
59 |
+ str = cli_unescape("%u00a2%u007f%u0080%u07ff%u0800%ue000"); |
|
60 |
+ fail_unless(str && !strcmp(str,"\xc2\xa2\x7f\xc2\x80\xdf\xbf\xe0\xa0\x80\xee\x80\x80"), |
|
61 |
+ "cli_unescape utf-8 test"); |
|
62 |
+ free(str); |
|
63 |
+ |
|
64 |
+ str = cli_unescape("%%u123%u12%u1%u%u1234"); |
|
65 |
+ fail_unless(str && !strcmp(str,"%%u123%u12%u1%u\xe1\x88\xb4"), |
|
66 |
+ "cli_unescape unicode truncated"); |
|
67 |
+ |
|
68 |
+ free(str); |
|
69 |
+} |
|
70 |
+END_TEST |
|
71 |
+ |
|
72 |
+static struct text_buffer buf; |
|
73 |
+ |
|
74 |
+static void buf_setup(void) |
|
75 |
+{ |
|
76 |
+ memset(&buf, 0, sizeof(buf)); |
|
77 |
+} |
|
78 |
+ |
|
79 |
+static void buf_teardown(void) |
|
80 |
+{ |
|
81 |
+ if(buf.data) |
|
82 |
+ free(buf.data); |
|
83 |
+ memset(&buf, 0, sizeof(buf)); |
|
84 |
+} |
|
85 |
+ |
|
86 |
+START_TEST (test_append_len) |
|
87 |
+{ |
|
88 |
+ fail_unless(textbuffer_append_len(&buf, "test",3) != -1, "tbuf append"); |
|
89 |
+ fail_unless(buf.data && !strncmp(buf.data,"tes",3), "textbuffer_append_len"); |
|
90 |
+} |
|
91 |
+END_TEST |
|
92 |
+ |
|
93 |
+START_TEST (test_append) |
|
94 |
+{ |
|
95 |
+ fail_unless(textbuffer_append(&buf, "test") != -1, "tbuf append"); |
|
96 |
+ fail_unless(textbuffer_putc(&buf, '\0') != -1, "tbuf putc"); |
|
97 |
+ fail_unless(buf.data && !strcmp(buf.data,"test"), "textbuffer_append"); |
|
98 |
+} |
|
99 |
+END_TEST |
|
100 |
+ |
|
101 |
+START_TEST (test_putc) |
|
102 |
+{ |
|
103 |
+ fail_unless(textbuffer_putc(&buf, '\x5a') != -1, "tbuf putc"); |
|
104 |
+ fail_unless(buf.data && buf.data[0] == '\x5a', "textbuffer_putc"); |
|
105 |
+} |
|
106 |
+END_TEST |
|
107 |
+ |
|
108 |
+START_TEST (test_normalize) |
|
109 |
+{ |
|
110 |
+ const char *str = "test\\0\\b\\t\\n\\v\\f\\r\\z\\x2a\\u1234test"; |
|
111 |
+ const char *expected ="test\x1\b\t\n\v\f\rz\x2a\xe1\x88\xb4test"; |
|
112 |
+ int rc; |
|
113 |
+ |
|
114 |
+ rc = cli_textbuffer_append_normalize(&buf, str, strlen(str)); |
|
115 |
+ fail_unless(rc != -1, "normalize"); |
|
116 |
+ |
|
117 |
+ fail_unless(textbuffer_putc(&buf, '\0') != -1, "putc \\0"); |
|
118 |
+ fail_unless(buf.data && !strcmp(buf.data, expected), "normalized text"); |
|
119 |
+} |
|
120 |
+END_TEST |
|
121 |
+ |
|
122 |
+Suite *test_str_suite(void); |
|
123 |
+Suite *test_str_suite(void) |
|
124 |
+{ |
|
125 |
+ Suite *s = suite_create("str"); |
|
126 |
+ TCase *tc_cli_unescape, *tc_tbuf; |
|
127 |
+ |
|
128 |
+ tc_cli_unescape = tcase_create("cli_unescape"); |
|
129 |
+ suite_add_tcase (s, tc_cli_unescape); |
|
130 |
+ tcase_add_test(tc_cli_unescape, test_unescape_simple); |
|
131 |
+ tcase_add_test(tc_cli_unescape, test_unescape_unicode); |
|
132 |
+ tcase_add_test(tc_cli_unescape, test_unescape_hex); |
|
133 |
+ |
|
134 |
+ tc_tbuf = tcase_create("jsnorm textbuf functions"); |
|
135 |
+ suite_add_tcase (s, tc_tbuf); |
|
136 |
+ tcase_add_checked_fixture (tc_tbuf, buf_setup, buf_teardown); |
|
137 |
+ tcase_add_test(tc_tbuf, test_append_len); |
|
138 |
+ tcase_add_test(tc_tbuf, test_append); |
|
139 |
+ tcase_add_test(tc_tbuf, test_putc); |
|
140 |
+ tcase_add_test(tc_tbuf, test_normalize); |
|
141 |
+ |
|
142 |
+ return s; |
|
143 |
+} |
|
144 |
+#endif |