clamav/unit_tests/check_bytecode.c

173 lines
4.6 KiB
C
Raw Normal View History

2009-07-13 19:45:05 +03:00
/*
* Unit tests for bytecode functions.
*
* Copyright (C) 2009 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.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <check.h>
#include "../libclamav/clamav.h"
#include "../libclamav/others.h"
#include "../libclamav/bytecode.h"
#include "checks.h"
2010-02-15 14:37:09 +02:00
#include "../libclamav/dconf.h"
2010-03-22 17:16:07 +02:00
#include "../libclamav/bytecode_priv.h"
2009-07-13 19:45:05 +03:00
static void runtest(const char *file, uint64_t expected, int fail, int nojit)
2009-07-13 19:45:05 +03:00
{
int rc;
int fd = open_testfile(file);
FILE *f;
struct cli_bc bc;
struct cli_bc_ctx *ctx;
struct cli_all_bc bcs;
2009-07-13 19:45:05 +03:00
uint64_t v;
fail_unless(fd >= 0, "retmagic open failed");
f = fdopen(fd, "r");
fail_unless(!!f, "retmagic fdopen failed");
cl_debug();
if (!nojit) {
2010-02-15 14:37:09 +02:00
rc = cli_bytecode_init(&bcs, BYTECODE_ENGINE_MASK);
fail_unless(rc == CL_SUCCESS, "cli_bytecode_init failed");
} else {
bcs.engine = NULL;
}
bcs.all_bcs = &bc;
bcs.count = 1;
rc = cli_bytecode_load(&bc, f, NULL, 1);
2009-07-13 19:45:05 +03:00
fail_unless(rc == CL_SUCCESS, "cli_bytecode_load failed");
fclose(f);
2010-02-15 14:37:09 +02:00
rc = cli_bytecode_prepare(&bcs, BYTECODE_ENGINE_MASK);
fail_unless(rc == CL_SUCCESS, "cli_bytecode_prepare failed");
if (have_clamjit && !nojit && nojit != -1) {
fail_unless(bc.state == bc_jit, "preparing for JIT failed");
}
2009-07-13 19:45:05 +03:00
ctx = cli_bytecode_context_alloc();
2010-03-22 17:16:07 +02:00
/* small timeout, these bytecodes are fast! */
ctx->bytecode_timeout = 100000;
2009-07-13 19:45:05 +03:00
fail_unless(!!ctx, "cli_bytecode_context_alloc failed");
cli_bytecode_context_setfuncid(ctx, &bc, 0);
2009-08-27 20:41:29 +03:00
rc = cli_bytecode_run(&bcs, &bc, ctx);
2009-10-02 12:27:52 +03:00
fail_unless_fmt(rc == fail, "cli_bytecode_run failed, expected: %u, have: %u\n",
fail, rc);
2009-07-13 19:45:05 +03:00
if (rc == CL_SUCCESS) {
v = cli_bytecode_context_getresult_int(ctx);
fail_unless_fmt(v == expected, "Invalid return value from bytecode run, expected: %llx, have: %llx\n",
expected, v);
}
2009-07-13 19:45:05 +03:00
cli_bytecode_context_destroy(ctx);
cli_bytecode_destroy(&bc);
cli_bytecode_done(&bcs);
2009-07-13 19:45:05 +03:00
}
START_TEST (test_retmagic)
{
cl_init(CL_INIT_DEFAULT);
runtest("input/retmagic.cbc", 0x1234f00d, CL_SUCCESS, 0);
runtest("input/retmagic.cbc", 0x1234f00d, CL_SUCCESS, 1);
2009-07-13 19:45:05 +03:00
}
END_TEST
START_TEST (test_arith)
{
cl_init(CL_INIT_DEFAULT);
runtest("input/arith.cbc", 0xd5555555, CL_SUCCESS, 0);
runtest("input/arith.cbc", 0xd5555555, CL_SUCCESS, 1);
2009-07-13 19:45:05 +03:00
}
END_TEST
2009-08-20 16:23:43 +03:00
START_TEST (test_apicalls)
{
cl_init(CL_INIT_DEFAULT);
2009-09-02 15:37:35 +03:00
runtest("input/apicalls.cbc", 0xf00d, CL_SUCCESS, 0);
runtest("input/apicalls.cbc", 0xf00d, CL_SUCCESS, 1);
2009-08-20 16:23:43 +03:00
}
END_TEST
2009-09-02 18:53:29 +03:00
START_TEST (test_apicalls2)
{
cl_init(CL_INIT_DEFAULT);
2009-12-11 21:41:57 +02:00
if (have_clamjit)/*FIXME: should work with both */
2009-09-02 18:53:29 +03:00
runtest("input/apicalls2.cbc", 0xf00d, CL_SUCCESS, 0);
2010-01-27 12:07:08 +02:00
/* runtest("input/apicalls2.cbc", 0xf00d, CL_SUCCESS, 1); */
2009-09-02 18:53:29 +03:00
}
END_TEST
START_TEST (test_div0)
{
cl_init(CL_INIT_DEFAULT);
/* must not crash on div#0 but catch it */
runtest("input/div0.cbc", 0, CL_EBYTECODE, 0);
runtest("input/div0.cbc", 0, CL_EBYTECODE, 1);
}
END_TEST
2009-08-20 16:23:43 +03:00
START_TEST (test_lsig)
{
cl_init(CL_INIT_DEFAULT);
2010-03-22 12:49:16 +02:00
#if 0
FIXME: match_counts should be initialized in clambc mode
2009-12-11 21:41:57 +02:00
if (have_clamjit)/* FIXME: should work with both */
2009-10-02 12:27:52 +03:00
runtest("input/lsig.cbc", 0, 0, 0);
2009-12-11 21:41:57 +02:00
//runtest("input/lsig.cbc", 0, CL_EBYTECODE, 1);
2010-03-22 12:49:16 +02:00
#endif
}
END_TEST
2010-03-22 17:16:07 +02:00
START_TEST (test_inf)
{
cl_init(CL_INIT_DEFAULT);
if (have_clamjit)
runtest("input/inf.cbc", 0, CL_ETIMEOUT, 0);
}
END_TEST
2009-07-13 19:45:05 +03:00
Suite *test_bytecode_suite(void)
{
Suite *s = suite_create("bytecode");
TCase *tc_cli_arith = tcase_create("arithmetic");
suite_add_tcase(s, tc_cli_arith);
2009-10-02 13:36:02 +03:00
tcase_add_test(tc_cli_arith, test_retmagic);
2009-07-13 19:45:05 +03:00
tcase_add_test(tc_cli_arith, test_arith);
2009-08-20 16:23:43 +03:00
tcase_add_test(tc_cli_arith, test_apicalls);
2009-09-02 18:53:29 +03:00
tcase_add_test(tc_cli_arith, test_apicalls2);
2009-10-02 13:36:02 +03:00
tcase_add_test(tc_cli_arith, test_div0);
2009-10-02 12:27:52 +03:00
tcase_add_test(tc_cli_arith, test_lsig);
2010-03-22 17:16:07 +02:00
tcase_add_test(tc_cli_arith, test_inf);
2009-07-13 19:45:05 +03:00
return s;
}