2
* Unit tests for string functions.
4
* Copyright (C) 2008 Sourcefire, Inc.
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License version 2 as
10
* published by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
#include "clamav-config.h"
33
#include "../libclamav/clamav.h"
34
#include "../libclamav/others.h"
35
#include "../libclamav/str.h"
36
#include "../libclamav/jsparse/textbuf.h"
39
START_TEST (test_unescape_simple)
41
char *str = cli_unescape("");
42
fail_unless(str && strlen(str) == 0, "cli_unescape empty string");
45
str = cli_unescape("1");
46
fail_unless(str && !strcmp(str,"1"), "cli_unescape one char");
49
str = cli_unescape("tesT");
50
fail_unless(str && !strcmp(str,"tesT"), "cli_unescape simple string");
55
START_TEST (test_unescape_hex)
57
char *str = cli_unescape("%5a");
58
fail_unless(str && !strcmp(str,"\x5a"), "cli_unescape hex");
61
str = cli_unescape("%b5%8");
62
fail_unless(str && !strcmp(str,"\xb5%8"), "cli_unescape truncated");
65
str = cli_unescape("%b5%");
66
fail_unless(str && !strcmp(str,"\xb5%"), "cli_unescape truncated/2");
69
str = cli_unescape("%00");
70
fail_unless(str && !strcmp(str,"\x1"), "cli_unescape %00");
75
START_TEST (test_unescape_unicode)
77
char *str = cli_unescape("%u05D0");
78
/* unicode is converted to utf-8 representation */
79
fail_unless(str && !strcmp(str,"\xd7\x90"), "cli_unescape unicode aleph");
82
str = cli_unescape("%u00a2%u007f%u0080%u07ff%u0800%ue000");
83
fail_unless(str && !strcmp(str,"\xc2\xa2\x7f\xc2\x80\xdf\xbf\xe0\xa0\x80\xee\x80\x80"),
84
"cli_unescape utf-8 test");
87
str = cli_unescape("%%u123%u12%u1%u%u1234");
88
fail_unless(str && !strcmp(str,"%%u123%u12%u1%u\xe1\x88\xb4"),
89
"cli_unescape unicode truncated");
95
static struct text_buffer buf;
97
static void buf_setup(void)
99
memset(&buf, 0, sizeof(buf));
102
static void buf_teardown(void)
106
memset(&buf, 0, sizeof(buf));
109
START_TEST (test_append_len)
111
fail_unless(textbuffer_append_len(&buf, "test",3) != -1, "tbuf append");
112
fail_unless(buf.data && !strncmp(buf.data,"tes",3), "textbuffer_append_len");
114
fail_unless(textbuffer_append_len(&buf, "test",CLI_MAX_ALLOCATION) == -1, "tbuf append");
115
fail_unless(buf.data && !strncmp(buf.data,"tes",3), "textbuffer_append_len");
119
START_TEST (test_append)
121
fail_unless(textbuffer_append(&buf, "test") != -1, "tbuf append");
122
fail_unless(textbuffer_putc(&buf, '\0') != -1, "tbuf putc");
123
fail_unless(buf.data && !strcmp(buf.data,"test"), "textbuffer_append");
127
START_TEST (test_putc)
129
fail_unless(textbuffer_putc(&buf, '\x5a') != -1, "tbuf putc");
130
fail_unless(buf.data && buf.data[0] == '\x5a', "textbuffer_putc");
134
START_TEST (test_normalize)
136
const char *str = "test\\0\\b\\t\\n\\v\\f\\r\\z\\x2a\\u1234test";
137
const char *expected ="test\x1\b\t\n\v\f\rz\x2a\xe1\x88\xb4test";
140
rc = cli_textbuffer_append_normalize(&buf, str, strlen(str));
141
fail_unless(rc != -1, "normalize");
143
fail_unless(textbuffer_putc(&buf, '\0') != -1, "putc \\0");
144
fail_unless(buf.data && !strcmp(buf.data, expected), "normalized text");
151
const char inp1[] = "a00026";
152
const char out1[] = "\xa0\x00\x26";
153
const char inp2[] = "ag0026";
155
r = cli_hex2str(inp1);
156
fail_unless(!!r, "cli_hex2str NULL");
157
fail_unless(!memcmp(r, out1, sizeof(out1)-1) ,
158
"cli_hex2str invalid output");
161
r = cli_hex2str(inp2);
162
fail_unless(!r, "cli_hex2str on invalid input");
166
Suite *test_str_suite(void)
168
Suite *s = suite_create("str");
169
TCase *tc_cli_unescape, *tc_tbuf, *tc_str;
171
tc_cli_unescape = tcase_create("cli_unescape");
172
suite_add_tcase (s, tc_cli_unescape);
173
tcase_add_test(tc_cli_unescape, test_unescape_simple);
174
tcase_add_test(tc_cli_unescape, test_unescape_unicode);
175
tcase_add_test(tc_cli_unescape, test_unescape_hex);
177
tc_tbuf = tcase_create("jsnorm textbuf functions");
178
suite_add_tcase (s, tc_tbuf);
179
tcase_add_checked_fixture (tc_tbuf, buf_setup, buf_teardown);
180
tcase_add_test(tc_tbuf, test_append_len);
181
tcase_add_test(tc_tbuf, test_append);
182
tcase_add_test(tc_tbuf, test_putc);
183
tcase_add_test(tc_tbuf, test_normalize);
185
tc_str = tcase_create("str functions");
186
suite_add_tcase (s, tc_str);
187
tcase_add_test(tc_str, hex2str);