~ubuntu-branches/ubuntu/raring/clamav/raring

« back to all changes in this revision

Viewing changes to unit_tests/check_str.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Gran
  • Date: 2008-09-05 17:25:34 UTC
  • mfrom: (0.35.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080905172534-yi3f8fkye1o7u1r3
* New upstream version (closes: #497662, #497773)
  - lots of new options for clamd.conf
  - fixes CVEs CVE-2008-3912, CVE-2008-3913, CVE-2008-3914, and
    CVE-2008-1389
* No longer supports --unzip option, so typo is gone (closes: #496276)
* Translations:
  - sv (thanks Martin Bagge <brother@bsnet.se>) (closes: #491760)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Unit tests for string functions. 
 
3
 *
 
4
 *  Copyright (C) 2008 Sourcefire, Inc.
 
5
 *
 
6
 *  Authors: Török Edvin
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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,
 
20
 *  MA 02110-1301, USA.
 
21
 */
 
22
#if HAVE_CONFIG_H
 
23
#include "clamav-config.h"
 
24
#endif
 
25
 
 
26
#include <stdio.h>
 
27
#ifdef HAVE_CHECK
 
28
 
 
29
#include <stdlib.h>
 
30
#include <limits.h>
 
31
#include <string.h>
 
32
#include <check.h>
 
33
#include "../libclamav/clamav.h"
 
34
#include "../libclamav/others.h"
 
35
#include "../libclamav/str.h"
 
36
#include "../libclamav/jsparse/textbuf.h"
 
37
#include "checks.h"
 
38
 
 
39
START_TEST (test_unescape_simple)
 
40
{
 
41
        char *str = cli_unescape("");
 
42
        fail_unless(str && strlen(str) == 0, "cli_unescape empty string");
 
43
        free(str);
 
44
 
 
45
        str = cli_unescape("1");
 
46
        fail_unless(str && !strcmp(str,"1"), "cli_unescape one char");
 
47
        free(str);
 
48
 
 
49
        str = cli_unescape("tesT");
 
50
        fail_unless(str && !strcmp(str,"tesT"), "cli_unescape simple string");
 
51
        free(str);
 
52
}
 
53
END_TEST
 
54
 
 
55
START_TEST (test_unescape_hex)
 
56
{
 
57
        char *str = cli_unescape("%5a");
 
58
        fail_unless(str && !strcmp(str,"\x5a"), "cli_unescape hex");
 
59
        free(str);
 
60
 
 
61
        str = cli_unescape("%b5%8");
 
62
        fail_unless(str && !strcmp(str,"\xb5%8"), "cli_unescape truncated");
 
63
        free(str);
 
64
 
 
65
        str = cli_unescape("%b5%");
 
66
        fail_unless(str && !strcmp(str,"\xb5%"), "cli_unescape truncated/2");
 
67
        free(str);
 
68
 
 
69
        str = cli_unescape("%00");
 
70
        fail_unless(str && !strcmp(str,"\x1"), "cli_unescape %00");
 
71
        free(str);
 
72
}
 
73
END_TEST
 
74
 
 
75
START_TEST (test_unescape_unicode)
 
76
{
 
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");
 
80
        free(str);
 
81
 
 
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");
 
85
        free(str);
 
86
 
 
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");
 
90
 
 
91
        free(str);
 
92
}
 
93
END_TEST
 
94
 
 
95
static struct text_buffer buf;
 
96
 
 
97
static void buf_setup(void)
 
98
{
 
99
        memset(&buf, 0, sizeof(buf));
 
100
}
 
101
 
 
102
static void buf_teardown(void)
 
103
{
 
104
        if(buf.data)
 
105
                free(buf.data);
 
106
        memset(&buf, 0, sizeof(buf));
 
107
}
 
108
 
 
109
START_TEST (test_append_len)
 
110
{
 
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");
 
113
        errmsg_expected();
 
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");
 
116
}
 
117
END_TEST
 
118
 
 
119
START_TEST (test_append)
 
120
{
 
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");
 
124
}
 
125
END_TEST
 
126
 
 
127
START_TEST (test_putc)
 
128
{
 
129
        fail_unless(textbuffer_putc(&buf, '\x5a') != -1, "tbuf putc");
 
130
        fail_unless(buf.data && buf.data[0] == '\x5a', "textbuffer_putc");
 
131
}
 
132
END_TEST
 
133
 
 
134
START_TEST (test_normalize)
 
135
{
 
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";
 
138
        int rc;
 
139
 
 
140
        rc = cli_textbuffer_append_normalize(&buf, str, strlen(str));
 
141
        fail_unless(rc != -1, "normalize");
 
142
 
 
143
        fail_unless(textbuffer_putc(&buf, '\0') != -1, "putc \\0");
 
144
        fail_unless(buf.data && !strcmp(buf.data, expected), "normalized text");
 
145
}
 
146
END_TEST
 
147
 
 
148
START_TEST (hex2str)
 
149
{
 
150
        char *r;
 
151
        const char inp1[] = "a00026";
 
152
        const char out1[] = "\xa0\x00\x26";
 
153
        const char inp2[] = "ag0026";
 
154
 
 
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");
 
159
        free(r);
 
160
 
 
161
        r = cli_hex2str(inp2);
 
162
        fail_unless(!r, "cli_hex2str on invalid input");
 
163
}
 
164
END_TEST
 
165
 
 
166
Suite *test_str_suite(void)
 
167
{
 
168
    Suite *s = suite_create("str");
 
169
    TCase *tc_cli_unescape, *tc_tbuf, *tc_str;
 
170
 
 
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);
 
176
 
 
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);
 
184
 
 
185
    tc_str = tcase_create("str functions");
 
186
    suite_add_tcase (s, tc_str);
 
187
    tcase_add_test(tc_str, hex2str);
 
188
 
 
189
    return s;
 
190
}
 
191
#endif