clamav/unit_tests/check_str.c

288 lines
8.2 KiB
C
Raw Normal View History

/*
2020-01-03 15:44:07 -05:00
* Unit tests for string functions.
*
2023-02-07 19:35:18 -08:00
* Copyright (C) 2013-2023 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
Add CMake build tooling This patch adds experimental-quality CMake build tooling. The libmspack build required a modification to use "" instead of <> for header #includes. This will hopefully be included in the libmspack upstream project when adding CMake build tooling to libmspack. Removed use of libltdl when using CMake. Flex & Bison are now required to build. If -DMAINTAINER_MODE, then GPERF is also required, though it currently doesn't actually do anything. TODO! I found that the autotools build system was generating the lexer output but not actually compiling it, instead using previously generated (and manually renamed) lexer c source. As a consequence, changes to the .l and .y files weren't making it into the build. To resolve this, I removed generated flex/bison files and fixed the tooling to use the freshly generated files. Flex and bison are now required build tools. On Windows, this adds a dependency on the winflexbison package, which can be obtained using Chocolatey or may be manually installed. CMake tooling only has partial support for building with external LLVM library, and no support for the internal LLVM (to be removed in the future). I.e. The CMake build currently only supports the bytecode interpreter. Many files used include paths relative to the top source directory or relative to the current project, rather than relative to each build target. Modern CMake support requires including internal dependency headers the same way you would external dependency headers (albeit with "" instead of <>). This meant correcting all header includes to be relative to the build targets and not relative to the workspace. For example, ... ```c include "../libclamav/clamav.h" include "clamd/clamd_others.h" ``` ... becomes: ```c // libclamav include "clamav.h" // clamd include "clamd_others.h" ``` Fixes header name conflicts by renaming a few of the files. Converted the "shared" code into a static library, which depends on libclamav. The ironically named "shared" static library provides features common to the ClamAV apps which are not required in libclamav itself and are not intended for use by downstream projects. This change was required for correct modern CMake practices but was also required to use the automake "subdir-objects" option. This eliminates warnings when running autoreconf which, in the next version of autoconf & automake are likely to break the build. libclamav used to build in multiple stages where an earlier stage is a static library containing utils required by the "shared" code. Linking clamdscan and clamdtop with this libclamav utils static lib allowed these two apps to function without libclamav. While this is nice in theory, the practical gains are minimal and it complicates the build system. As such, the autotools and CMake tooling was simplified for improved maintainability and this feature was thrown out. clamdtop and clamdscan now require libclamav to function. Removed the nopthreads version of the autotools libclamav_internal_utils static library and added pthread linking to a couple apps that may have issues building on some platforms without it, with the intention of removing needless complexity from the source. Kept the regular version of libclamav_internal_utils.la though it is no longer used anywhere but in libclamav. Added an experimental doxygen build option which attempts to build clamav.h and libfreshclam doxygen html docs. The CMake build tooling also may build the example program(s), which isn't a feature in the Autotools build system. Changed C standard to C90+ due to inline linking issues with socket.h when linking libfreshclam.so on Linux. Generate common.rc for win32. Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef from misc.c. Add CMake files to the automake dist, so users can try the new CMake tooling w/out having to build from a git clone. clamonacc changes: - Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other similar macros. - Added a new clamav-clamonacc.service systemd unit file, based on the work of ChadDevOps & Aaron Brighton. - Added missing clamonacc man page. Updates to clamdscan man page, add missing options. Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use libclamav). Rename Windows mspack.dll to libmspack.dll so all ClamAV-built libraries have the lib-prefix with Visual Studio as with CMake.
2020-08-13 00:25:34 -07:00
// libclamav
#include "clamav.h"
#include "others.h"
#include "str.h"
#include "entconv.h"
#include "mbox.h"
#include "message.h"
#include "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"),
2020-07-24 08:32:47 -07:00
"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"),
2020-07-24 08:32:47 -07:00
"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),
2020-07-24 08:32:47 -07:00
"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",
2020-07-24 08:32:47 -07:00
len, test->len, buf);
ck_assert_msg(!memcmp(buf, test->decoded, test->len),
2020-07-24 08:32:47 -07:00
"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), E_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;
}