clamav/unit_tests/check_str.c

286 lines
8.3 KiB
C
Raw Normal View History

/*
2020-01-03 15:44:07 -05:00
* Unit tests for string functions.
*
2020-01-03 15:44:07 -05:00
* Copyright (C) 2013-2020 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2008-2013 Sourcefire, Inc.
*
* Authors: Török Edvin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
2008-07-10 10:30:41 +00:00
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <check.h>
2014-02-08 00:31:12 -05:00
2008-07-10 10:30:41 +00:00
#include "../libclamav/clamav.h"
#include "../libclamav/others.h"
#include "../libclamav/str.h"
#include "../libclamav/mbox.h"
#include "../libclamav/message.h"
2008-07-10 10:30:41 +00:00
#include "../libclamav/jsparse/textbuf.h"
#include "checks.h"
2008-07-10 10:30:41 +00:00
START_TEST(test_unescape_simple)
2008-07-10 10:30:41 +00:00
{
char *str = cli_unescape("");
ck_assert_msg(str && strlen(str) == 0, "cli_unescape empty string");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("1");
ck_assert_msg(str && !strcmp(str, "1"), "cli_unescape one char");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("tesT");
ck_assert_msg(str && !strcmp(str, "tesT"), "cli_unescape simple string");
free(str);
2008-07-10 10:30:41 +00:00
}
END_TEST
START_TEST(test_unescape_hex)
2008-07-10 10:30:41 +00:00
{
char *str = cli_unescape("%5a");
ck_assert_msg(str && !strcmp(str, "\x5a"), "cli_unescape hex");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("%b5%8");
ck_assert_msg(str && !strcmp(str, "\xb5%8"), "cli_unescape truncated");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("%b5%");
ck_assert_msg(str && !strcmp(str, "\xb5%"), "cli_unescape truncated/2");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("%00");
ck_assert_msg(str && !strcmp(str, "\x1"), "cli_unescape %00");
free(str);
2008-07-10 10:30:41 +00:00
}
END_TEST
START_TEST(test_unescape_unicode)
2008-07-10 10:30:41 +00:00
{
char *str = cli_unescape("%u05D0");
/* unicode is converted to utf-8 representation */
ck_assert_msg(str && !strcmp(str, "\xd7\x90"), "cli_unescape unicode aleph");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("%u00a2%u007f%u0080%u07ff%u0800%ue000");
ck_assert_msg(str && !strcmp(str, "\xc2\xa2\x7f\xc2\x80\xdf\xbf\xe0\xa0\x80\xee\x80\x80"),
"cli_unescape utf-8 test");
free(str);
2008-07-10 10:30:41 +00:00
str = cli_unescape("%%u123%u12%u1%u%u1234");
ck_assert_msg(str && !strcmp(str, "%%u123%u12%u1%u\xe1\x88\xb4"),
"cli_unescape unicode truncated");
2008-07-10 10:30:41 +00:00
free(str);
2008-07-10 10:30:41 +00:00
}
END_TEST
static struct text_buffer buf;
static void buf_setup(void)
{
memset(&buf, 0, sizeof(buf));
2008-07-10 10:30:41 +00:00
}
static void buf_teardown(void)
{
if (buf.data)
free(buf.data);
memset(&buf, 0, sizeof(buf));
2008-07-10 10:30:41 +00:00
}
START_TEST(test_append_len)
2008-07-10 10:30:41 +00:00
{
ck_assert_msg(textbuffer_append_len(&buf, "test", 3) != -1, "tbuf append");
ck_assert_msg(buf.data && !strncmp(buf.data, "tes", 3), "textbuffer_append_len");
errmsg_expected();
ck_assert_msg(textbuffer_append_len(&buf, "test", CLI_MAX_ALLOCATION) == -1, "tbuf append");
ck_assert_msg(buf.data && !strncmp(buf.data, "tes", 3), "textbuffer_append_len");
2008-07-10 10:30:41 +00:00
}
END_TEST
START_TEST(test_append)
2008-07-10 10:30:41 +00:00
{
ck_assert_msg(textbuffer_append(&buf, "test") != -1, "tbuf append");
ck_assert_msg(textbuffer_putc(&buf, '\0') != -1, "tbuf putc");
ck_assert_msg(buf.data && !strcmp(buf.data, "test"), "textbuffer_append");
2008-07-10 10:30:41 +00:00
}
END_TEST
START_TEST(test_putc)
2008-07-10 10:30:41 +00:00
{
ck_assert_msg(textbuffer_putc(&buf, '\x5a') != -1, "tbuf putc");
ck_assert_msg(buf.data && buf.data[0] == '\x5a', "textbuffer_putc");
2008-07-10 10:30:41 +00:00
}
END_TEST
START_TEST(test_normalize)
2008-07-10 10:30:41 +00:00
{
const char *str = "test\\0\\b\\t\\n\\v\\f\\r\\z\\x2a\\u1234test";
const char *expected = "test\x1\b\t\n\v\f\rz\x2a\xe1\x88\xb4test";
int rc;
2008-07-10 10:30:41 +00:00
rc = cli_textbuffer_append_normalize(&buf, str, strlen(str));
ck_assert_msg(rc != -1, "normalize");
2008-07-10 10:30:41 +00:00
ck_assert_msg(textbuffer_putc(&buf, '\0') != -1, "putc \\0");
ck_assert_msg(buf.data && !strcmp(buf.data, expected), "normalized text");
2008-07-10 10:30:41 +00:00
}
END_TEST
START_TEST(hex2str)
{
char *r;
const char inp1[] = "a00026";
const char out1[] = "\xa0\x00\x26";
const char inp2[] = "ag0026";
r = cli_hex2str(inp1);
ck_assert_msg(!!r, "cli_hex2str NULL");
ck_assert_msg(!memcmp(r, out1, sizeof(out1) - 1),
"cli_hex2str invalid output");
free(r);
r = cli_hex2str(inp2);
ck_assert_msg(!r, "cli_hex2str on invalid input");
}
END_TEST
static struct base64lines {
const char *line;
const char *decoded;
unsigned int len;
} base64tests[] = {
{"", "", 0},
{"Zg==", "f", 1},
{"Zm8=", "fo", 2},
{"Zm9v", "foo", 3},
{"Zm9vYg==", "foob", 4},
{"Zm9vYmFy", "foobar", 6},
/* with missing padding */
{"Zg", "f", 1},
{"Zm8", "fo", 2},
{"Zm9vYg", "foob", 4}};
START_TEST(test_base64)
{
unsigned char *ret, *ret2;
unsigned len;
unsigned char buf[1024];
const struct base64lines *test = &base64tests[_i];
message *m = messageCreate();
ck_assert_msg(!!m, "Unable to create message");
ret = decodeLine(m, BASE64, test->line, buf, sizeof(buf));
ck_assert_msg(!!ret, "unable to decode line");
ret2 = base64Flush(m, ret);
if (!ret2)
ret2 = ret;
*ret2 = '\0';
len = ret2 - buf;
ck_assert_msg(len == test->len, "invalid base64 decoded length: %u expected %u (%s)\n",
len, test->len, buf);
ck_assert_msg(!memcmp(buf, test->decoded, test->len),
"invalid base64 decoded data: %s, expected:%s\n",
buf, test->decoded);
messageDestroy(m);
}
END_TEST
2011-06-17 22:20:31 +03:00
static struct {
const char *u16;
const char *u8;
2011-06-17 22:20:31 +03:00
} u16_tests[] = {
{"\x74\x00\x65\x00\x73\x00\x74\x00\x00\x00", "test"},
{"\xff\xfe\x00", ""},
{"\x80\x00\x00", "\xc2\x80"},
{"\xff\x07\x00", "\xdf\xbf"},
{"\x00\x08\x00", "\xe0\xa0\x80"},
{"\xff\x0f\x00", "\xe0\xbf\xbf"},
{"\x00\x10\x00", "\xe1\x80\x80"},
{"\xff\xcf\x00", "\xec\xbf\xbf"},
{"\x00\xd0\x00", "\xed\x80\x80"},
{"\xff\xd7\x00", "\xed\x9f\xbf"},
{"\x00\xe0\x00", "\xee\x80\x80"},
{"\xff\xff\x00", "\xef\xbf\xbf"},
{"\x00\xd8\x00\xdc\x00", "\xf0\x90\x80\x80"},
{"\xbf\xd8\xff\xdf\x00", "\xf0\xbf\xbf\xbf"},
{"\xc0\xd8\x00\xdc\x00", "\xf1\x80\x80\x80"},
{"\xbf\xdb\xff\xdf\x00", "\xf3\xbf\xbf\xbf"},
{"\xc0\xdb\x00\xdc\x00", "\xf4\x80\x80\x80"},
{"\xff\xdb\xff\xdf\x00", "\xf4\x8f\xbf\xbf"},
{"\x00\xdc\x00\xd8\x00", "\xef\xbf\xbd\xef\xbf\xbd"}};
2011-06-17 22:20:31 +03:00
static unsigned u16_len(const char *s)
{
unsigned i;
for (i = 0; s[i] || s[i + 1]; i += 2) {
}
2011-06-17 22:20:31 +03:00
return i;
}
START_TEST(test_u16_u8)
{
char *result = cli_utf16_to_utf8(u16_tests[_i].u16, u16_len(u16_tests[_i].u16), UTF16_LE);
ck_assert_msg(!!result, "cli_utf16_to_utf8 non-null");
ck_assert_msg(!strcmp(result, u16_tests[_i].u8), "utf16_to_8 %d failed, expected: %s, got %s", _i, u16_tests[_i].u8, result);
2011-06-17 22:20:31 +03:00
free(result);
}
END_TEST
2008-07-10 10:30:41 +00:00
Suite *test_str_suite(void)
{
Suite *s = suite_create("str");
TCase *tc_cli_unescape, *tc_tbuf, *tc_str, *tc_decodeline;
2008-07-10 10:30:41 +00:00
tc_cli_unescape = tcase_create("cli_unescape");
suite_add_tcase(s, tc_cli_unescape);
2008-07-10 10:30:41 +00:00
tcase_add_test(tc_cli_unescape, test_unescape_simple);
tcase_add_test(tc_cli_unescape, test_unescape_unicode);
tcase_add_test(tc_cli_unescape, test_unescape_hex);
tc_tbuf = tcase_create("jsnorm textbuf functions");
suite_add_tcase(s, tc_tbuf);
tcase_add_checked_fixture(tc_tbuf, buf_setup, buf_teardown);
2008-07-10 10:30:41 +00:00
tcase_add_test(tc_tbuf, test_append_len);
tcase_add_test(tc_tbuf, test_append);
tcase_add_test(tc_tbuf, test_putc);
tcase_add_test(tc_tbuf, test_normalize);
tc_str = tcase_create("str functions");
suite_add_tcase(s, tc_str);
tcase_add_test(tc_str, hex2str);
tcase_add_loop_test(tc_str, test_u16_u8, 0, sizeof(u16_tests) / sizeof(u16_tests[0]));
tc_decodeline = tcase_create("decodeline");
suite_add_tcase(s, tc_decodeline);
tcase_add_loop_test(tc_decodeline, test_base64, 0, sizeof(base64tests) / sizeof(base64tests[0]));
2008-07-10 10:30:41 +00:00
return s;
}