Created
May 10, 2012 05:58
-
-
Save rchatsiri/2651305 to your computer and use it in GitHub Desktop.
Check test scanbuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if HAVE_CONFIG_H | |
#include "clamav-config.h" | |
#endif | |
#include "../src/cli_load.h" | |
#include <string.h> | |
#include "check_common.h" | |
//#include "../tests/check_common.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <limits.h> | |
#include <string.h> | |
#include <check.h> | |
#include "checks.h" | |
//#include "libcompat.h" | |
#include "clamav.h" | |
#include "matcher-ac.h" | |
#include "matcher-bm.h" | |
#include "readdb.h" | |
#include "matcher.h" | |
#include "default.h" | |
#include "others.h" | |
#include "mbox.h" | |
#include "message.h" | |
#include "htmlnorm.h" | |
#include "phishcheck.h" | |
#include "regex_suffix.h" | |
#include "regex_list.h" | |
#include "phish_domaincheck_db.h" | |
#include "phish_whitelist.h" | |
static const struct ac_testdata_s { | |
const char *data; | |
const char *hexsig; | |
const char *virname; | |
} ac_testdata[] = { | |
{ "daaaaaaaddbbbbbcce", "64[4-4]61616161{2}6262[3-6]65", "Test_1" }, | |
{ "ebbbbbbbbeecccccddf", "6262(6162|6364|6265|6465){2}6363", "Test_2" }, | |
{ "aaaabbbbcccccdddddeeee", "616161*63636363*6565", "Test_3" }, | |
{ "oprstuwxy","6f??727374????7879", "Test_4" }, | |
{ "abdcabcddabccadbbdbacb", "6463{2-3}64646162(63|64|65)6361*6462????6261{-1}6362", "Test_5" }, | |
{ "abcdefghijkabcdefghijk", "62????65666768*696a6b6162{2-3}656667[1-3]6b", "Test_6" }, | |
{ "abcadbabcadbabcacb", "6?6164?26?62{3}?26162?361", "Test_7" }, | |
/* testcase for filter bug: it was checking only first 32 chars, and last maxpatlen */ | |
{ "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1dddddddddddddddddddd5\1\1\1\1\1\1\1\1\1\1\1\1\1","6464646464646464646464646464646464646464(35|36)","Test_8"}, | |
{ NULL, NULL, NULL} | |
}; | |
/* define for cli_ctx */ | |
static cli_ctx ctx; | |
static fmap_t *themap = NULL; | |
static const char *virname = NULL; | |
/*----------------------------------------------------------fork processes-----------------------------------------------------*/ | |
static SRunner *fork_cli_load; | |
static SRunner *fork_dummy_cli_load; | |
void fork_setup(void) | |
{ | |
fork_cli_load = srunner_create(test_cli_load_suite()); | |
fork_dummy_cli_load = srunner_create(test_cli_load_suite()); | |
srunner_set_fork_status(fork_cli_load, CK_NOFORK); | |
srunner_run_all(fork_cli_load, CK_VERBOSE); | |
} | |
void fork_teardown(void) | |
{ | |
srunner_free(fork_cli_load); | |
} | |
START_TEST(test_default_fork) | |
{ | |
fail_unless(srunner_fork_status(fork_dummy_cli_load) == CK_FORK, "Default fork status not set correctly"); | |
} | |
END_TEST | |
Suite *make_test_fork(void) | |
{ | |
Suite *s; | |
TCase *test_case; | |
s = suite_create("Core fork test"); | |
test_case = tcase_create("Core"); | |
suite_add_tcase(s, test_case); | |
tcase_add_test(test_case,test_default_fork); | |
return s; | |
} | |
/*----------------------------------------------------------test processes-----------------------------------------------------*/ | |
static void setup(void) | |
{ | |
struct cli_matcher *root; | |
virname = NULL; | |
themap = NULL; | |
ctx.virname = &virname; | |
ctx.fmap = &themap; | |
ctx.engine = cl_engine_new(); | |
fail_unless(!!ctx.engine, "cl_engine_new() fail"); | |
root = (struct cli_matcher*)mpool_calloc(ctx.engine->mempool,1,sizeof(struct cli_matcher)); | |
fail_unless( root != NULL, "root != NULL"); | |
/*Function for test check : fail ("Currency not set correctly on creation"); */ | |
root->mempool = ctx.engine->mempool; | |
ctx.engine->root[0] = root; | |
} | |
static void teardown(void) | |
{ | |
cl_engine_free((struct cl_engine*)ctx.engine); | |
} | |
START_TEST(test_cli_load) | |
{ | |
struct cli_ac_data mdata; | |
struct cli_matcher *root; | |
unsigned int i; | |
int ret; | |
root = ctx.engine->root[0]; | |
root->ac_only = 1; | |
#ifdef USE_MPOOL | |
root->mempool = mpool_create(); | |
#endif | |
ret = cli_ac_init(root, CLI_DEFAULT_AC_MINDEPTH, CLI_DEFAULT_AC_MAXDEPTH, 1); | |
fail_unless(ret == CL_SUCCESS, "cli_ac_init() failed"); | |
for(i = 0; ac_testdata[i].data; i++) { | |
ret = cli_parse_add(root, ac_testdata[i].virname, ac_testdata[i].hexsig, 0, 0, "*", 0, NULL, 0); | |
fail_unless(ret == CL_SUCCESS, "cli_parse_add() failed"); | |
} | |
ret = cli_ac_buildtrie(root); | |
fail_unless(ret == CL_SUCCESS, "cli_ac_buildtrie() failed"); | |
ret = cli_ac_initdata(&mdata, root->ac_partsigs, 0, 0, CLI_DEFAULT_AC_TRACKLEN); | |
fail_unless(ret == CL_SUCCESS, "cli_ac_initdata() failed"); | |
for(i = 0; ac_testdata[i].data; i++) { | |
ret = cli_ac_scanbuff((const unsigned char*)ac_testdata[i].data, strlen(ac_testdata[i].data), &virname, NULL, NULL, root, &mdata, 0, 0, NULL, AC_SCAN_VIR, NULL); | |
fail_unless_fmt(ret == CL_VIRUS, "cli_ac_scanbuff() failed for %s ", ac_testdata[i].virname); | |
fail_unless_fmt(!strncmp(virname, ac_testdata[i].virname, strlen(ac_testdata[i].virname)), "Dataset %u matched with %s", i, virname); | |
ret = cli_scanbuff((const unsigned char*)ac_testdata[i].data, strlen(ac_testdata[i].data), 0, &ctx, 0, NULL); | |
fail_unless_fmt(ret == CL_VIRUS, "cli_scanbuff() failed for %s", ac_testdata[i].virname); | |
fail_unless_fmt(!strncmp(virname, ac_testdata[i].virname, strlen(ac_testdata[i].virname)), "Dataset %u matched with %s", i, virname); | |
} | |
cli_ac_freedata(&mdata); | |
} | |
END_TEST | |
static Suite * test_cli_load_suite(void) | |
{ | |
Suite * s = suite_create("matchers"); | |
/* core test */ | |
TCase *tc_matchers = tcase_create("matchers"); | |
suite_add_tcase(s,tc_matchers); | |
tcase_add_checked_fixture(tc_matchers,setup,teardown); | |
tcase_add_test(tc_matchers,test_cli_load); | |
return s; | |
} | |
int main(void) | |
{ | |
/* #ifdef _POSIX_VERSION | |
fork_setup(); | |
#endif /* _POSIX_VERSION */ | |
int nf; | |
Suite * s; | |
SRunner *sr; | |
// s = test_cli_load_suite(); | |
s = make_test_fork(); | |
sr = srunner_create(s); | |
srunner_add_suite(sr, make_test_fork()); | |
srunner_run_all(sr,CK_NORMAL); | |
fork_teardown(); | |
nf = srunner_ntests_failed(sr); | |
srunner_free(sr); | |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment