Skip to content

Commit

Permalink
Merge pull request ossec#188 from cgzones/os_regex_unittests
Browse files Browse the repository at this point in the history
os_regex unit tests
  • Loading branch information
ddpbsd committed Apr 23, 2014
2 parents e3ca5db + f49bcca commit 584176c
Showing 1 changed file with 278 additions and 11 deletions.
289 changes: 278 additions & 11 deletions src/tests/test_os_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ START_TEST(test_success_match1)
};

for(i=0; tests[i][0] != NULL ; i++) {
ck_assert_msg(OS_Match2(tests[i][0],tests[i][1]),
"%s should have OS_Match2 true with %s: Ref: %s",
ck_assert_msg(OS_Match2(tests[i][0],tests[i][1]),
"%s should have OS_Match2 true with %s: Ref: %s",
tests[i][0], tests[i][1], tests[i][1]);
}
}
Expand Down Expand Up @@ -90,10 +90,38 @@ START_TEST(test_success_regex1)
{

int i;
/*
* Please note that all strings are \ escaped
/*
* Please note that all strings are \ escaped
*/
char *tests[][3] = {
{"abc", "abcd", ""},
{"abcd", "abcd", ""},
{"a", "a", ""},
{"a", "aa", ""},
{"^a", "ab", ""},
{"test", "testa", ""},
{"test", "testest", ""},
{"lalaila", "lalalalaila", ""},
{"abc|cde", "cde", ""},
{"^aa|ee|ii|oo|uu", "dfgdsii", ""},
{"Abc", "abc", ""},
{"ZBE", "zbe", ""},
{"ABC", "ABc", ""},
{"^A", "a", ""},
{"a|E", "abcdef", ""},
{"daniel", "daniel", ""},
{"DANIeL", "daNIel", ""},
{"^abc ", "abc ", ""},
{"ddd|eee|fff|ggg|ggg|hhh|iii", "iii", ""},
{"kwo|fe|fw|wfW|edW|dwDF|WdW|dw|d|^la", "la", ""},
{"^a", "a", ""},
{"^ab$", "ab", ""},
{"c$", "c", ""},
{"c$", "lalalalac", ""},
{"^bin$|^shell$", "bin", ""},
{"^bin$|^shell$", "shell", ""},
{"^bin$|^shell$|^ftp$", "shell", ""},
{"^bin$|^shell$|^ftp$", "ftp", ""},
{"\\s+123", " 123", ""},
{"\\s*123", "123", ""},
{"\\s123", " 123", ""},
Expand Down Expand Up @@ -125,8 +153,8 @@ START_TEST(test_success_regex1)
};

for(i=0; tests[i][0] != NULL ; i++) {
ck_assert_msg(OS_Regex(tests[i][0],tests[i][1]),
"%s should have OS_Regex true with %s: Ref: %s",
ck_assert_msg(OS_Regex(tests[i][0],tests[i][1]),
"%s should have OS_Regex true with %s: Ref: %s",
tests[i][0], tests[i][1], tests[i][2]);
}
}
Expand All @@ -136,10 +164,23 @@ START_TEST(test_fail_regex1)
{

int i;
/*
* Please note that all strings are \ escaped
/*
* Please note that all strings are \ escaped
*/
char *tests[][3] = {
{"abc", "abb", ""},
{"^ab", " ab", ""},
{"test", "tes", ""},
{"abcd", "abc", ""},
{"abbb", "abb", ""},
{"abbbbbbbb", "abbbbbbb", ""},
{"a|b|c| ", "def", ""},
{"lala$", "lalalalalal", ""},
{"^ab$", "abc", ""},
{"zzzz$", "zzzzzzzzzzzz ", ""},
{"^bin$|^shell$", "bina", ""},
{"^bin$|^shell$", "shella", ""},
{"^bin$|^shell$", "ashell", ""},
{"\\w+\\s+\\w+\\d+\\s$", "a aa11 ", ""},
{"^\\s+\\s l", " lala", ""},
{"test123test\\d+", "test123test", ""},
Expand All @@ -148,32 +189,258 @@ START_TEST(test_fail_regex1)
{"test123(\\d)", "test123a", ""},
{"\\(test)", "test", ""},
{"(\\w+)(\\d+)", "1 1", ""},
{NULL,NULL,NULL},
{NULL,NULL,NULL},
};

for(i=0; tests[i][0] != NULL ; i++) {
ck_assert_msg(!OS_Regex(tests[i][0],tests[i][1]),
"%s should have OS_Regex false with %s: Ref: %s",
ck_assert_msg(!OS_Regex(tests[i][0],tests[i][1]),
"%s should have OS_Regex false with %s: Ref: %s",
tests[i][0], tests[i][1], tests[i][2]);
}
}
END_TEST

START_TEST(test_success_wordmatch)
{
int i;

/*
* Please note that all strings are \ escaped
*/
char *tests[][2] = {
{ "test", "this is a test" },
{ "test", "thistestiswithoutspaces" },
{ "test|not", "test" },
{ "test|not", "not" },
{ "^test", "test on start" },
{NULL,NULL},
};

for(i=0; tests[i][0] != NULL ; i++) {
ck_assert_msg(OS_WordMatch(tests[i][0],tests[i][1]),
"%s should match positive with %s by OS_WordMatch",
tests[i][0], tests[i][1]);
}

}
END_TEST

START_TEST(test_fail_wordmatch)
{
int i;

/*
* Please note that all strings are \ escaped
*/
char *tests[][2] = {
{ "-test", "this is a test" },
{ "", "test" },
{ "test|not", "negative" },
{ "test", "" },
{ "^test", "starttest" },
{NULL,NULL},
};

for(i=0; tests[i][0] != NULL ; i++) {
ck_assert_msg(!OS_WordMatch(tests[i][0],tests[i][1]),
"%s should not match positive with %s by OS_WordMatch",
tests[i][0], tests[i][1]);
}

}
END_TEST

START_TEST(test_success_strisnum)
{
int i;

/*
* Please note that all strings are \ escaped
*/
char *tests[] = {
"1",
"0123",
NULL,
};

for(i=0; tests[i] != NULL ; i++) {
ck_assert_msg(OS_StrIsNum(tests[i]),
"%s should match positive by OS_StrIsNum",
tests[i]);
}

}
END_TEST

START_TEST(test_fail_strisnum)
{
int i;

/*
* Please note that all strings are \ escaped
*/
char *tests[] = {
"test",
"1234e",
NULL,
};

for(i=0; tests[i] != NULL ; i++) {
ck_assert_msg(!OS_StrIsNum(tests[i]),
"%s should not match positive by OS_StrIsNum",
tests[i]);
}

}
END_TEST

START_TEST(test_strhowclosedmatch)
{
int i;

/*
* Please note that all strings are \ escaped
*/
char *tests[][3] = {
{ "test", "test1234", "4" },
{ "test1234", "test", "4" },
{ "test", "test", "4" },
{ "test", "", "0" },
{ "", "test", "0" },
{NULL,NULL,NULL},
};

for(i=0; tests[i][0] != NULL ; i++) {
ck_assert_int_eq(OS_StrHowClosedMatch(tests[i][0],tests[i][1])
, atoi(tests[i][2]));
}

}
END_TEST

START_TEST(test_strbreak)
{
int i;

/*
* Please note that all strings are \ escaped
*/
char *tests[][15] = {
{ "X", "testX1234", "4", "test", "1234", NULL},
{ "X", "XtestX1234X", "4", "", "test", "1234", "", NULL},
{ "Y", "testX1234", "4", "testX1234", NULL},
{ "X", "testXX1234", "4", "test", "", "1234", NULL},
{ "X", "testX1234", "1", "testX1234", NULL},
{ "X", "testX1234X5678", "2", "test", "1234X5678", NULL},
{ "X", "testX1234", "0", NULL},
{NULL},
};

for(i=0; tests[i][0] != NULL; i++) {
char **result = OS_StrBreak(tests[i][0][0], tests[i][1], atoi(tests[i][2]));

int j = 3;
if(tests[i][j] == NULL)
{
ck_assert_ptr_eq(result, NULL);
continue;
}

int k;
for(k = 0; tests[i][j] != NULL; j++, k++)
{
ck_assert_ptr_ne(result[k], NULL);
ck_assert_str_eq(result[k], tests[i][j]);
}
ck_assert_ptr_eq(result[k], NULL);

k=0;
while(result[k])
free(result[k++]);
free(result);
}

}
END_TEST

START_TEST(test_regexextraction)
{

int i;
/*
* Please note that all strings are \ escaped
*/
char *tests[][15] = {
{ "123(\\w+\\s+)abc", "123sdf abc", "sdf ", NULL},
{ "123(\\w+\\s+)abc", "abc123sdf abc", "sdf ", NULL},
{ "123 (\\d+.\\d.\\d.\\d\\d*\\d*)", "123 45.6.5.567", "45.6.5.567", NULL},
{ "from (\\S*\\d+.\\d+.\\d+.\\d\\d*\\d*)", "sshd[21576]: Illegal user web14 from ::ffff:212.227.60.55", "::ffff:212.227.60.55", NULL},
{ "^sshd[\\d+]: Accepted \\S+ for (\\S+) from (\\S+) port ", "sshd[21405]: Accepted password for root from 192.1.1.1 port 6023", "root", "192.1.1.1", NULL},
{ ": \\((\\S+)@(\\S+)\\) [", "pure-ftpd: (?@enigma.lab.ossec.net) [INFO] New connection from enigma.lab.ossec.net", "?", "enigma.lab.ossec.net", NULL},
{NULL,NULL,NULL}
};

for(i=0; tests[i][0] != NULL; i++) {
OSRegex reg;
ck_assert_int_eq(OSRegex_Compile(tests[i][0], &reg, OS_RETURN_SUBSTRING), 1);
ck_assert_ptr_ne(OSRegex_Execute(tests[i][1], &reg), NULL);



char **result = reg.sub_strings;

int j;
int k;
for(j = 2, k = 0; tests[i][j] != NULL; j++, k++)
{
ck_assert_ptr_ne(result[k], NULL);
ck_assert_str_eq(result[k], tests[i][j]);
}
ck_assert_ptr_eq(result[k], NULL);

OSRegex_FreePattern(&reg);
}
}
END_TEST

Suite *test_suite(void)
{
Suite *s = suite_create("os_regex");

/* Core test case */
TCase *tc_match = tcase_create("Match");
TCase *tc_regex = tcase_create("Regex");
TCase *tc_wordmatch = tcase_create("WordMatch");
TCase *tc_strisnum = tcase_create("StrIsNum");
TCase *tc_strhowclosedmatch = tcase_create("StrHowClosedMatch");
TCase *tc_strbreak = tcase_create("StrBreak");
TCase *tc_regexextraction = tcase_create("RegexExtraction");

tcase_add_test(tc_match, test_success_match1);
tcase_add_test(tc_match, test_fail_match1);

tcase_add_test(tc_regex, test_success_regex1);
tcase_add_test(tc_regex, test_fail_regex1);

tcase_add_test(tc_wordmatch, test_success_wordmatch);
tcase_add_test(tc_wordmatch, test_fail_wordmatch);

tcase_add_test(tc_strisnum, test_success_strisnum);
tcase_add_test(tc_strisnum, test_fail_strisnum);

tcase_add_test(tc_strhowclosedmatch, test_strhowclosedmatch);

tcase_add_test(tc_strbreak, test_strbreak);

//tcase_add_test(tc_regexextraction, test_regexextraction);

suite_add_tcase(s, tc_match);
suite_add_tcase(s, tc_regex);
suite_add_tcase(s, tc_wordmatch);
suite_add_tcase(s, tc_strisnum);
suite_add_tcase(s, tc_strhowclosedmatch);
suite_add_tcase(s, tc_strbreak);
suite_add_tcase(s, tc_regexextraction);

return (s);
}
Expand Down

0 comments on commit 584176c

Please sign in to comment.