Commit 5334c3a6 authored by Evan Hunt's avatar Evan Hunt
Browse files

convert regex_test

(cherry picked from commit bd009b34)
(cherry picked from commit 6182c943)
parent 4506ec30
......@@ -20,7 +20,7 @@ atf_test_program{name='print_test'}
tap_test_program{name='queue_test'}
tap_test_program{name='radix_test'}
tap_test_program{name='random_test'}
atf_test_program{name='regex_test'}
tap_test_program{name='regex_test'}
tap_test_program{name='result_test'}
tap_test_program{name='safe_test'}
tap_test_program{name='sockaddr_test'}
......
......@@ -145,8 +145,9 @@ random_test@EXEEXT@: random_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
regex_test@EXEEXT@: regex_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
regex_test.@O@ ${ISCLIBS} ${LIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
${LDFLAGS} -o $@ regex_test.@O@ \
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
result_test@EXEEXT@: result_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
......
......@@ -11,31 +11,41 @@
#include <config.h>
#include <atf-c.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define UNIT_TESTING
#include <cmocka.h>
#ifdef HAVE_REGEX_H
#include <regex.h>
#endif
#include <isc/commandline.h>
#include <isc/regex.h>
#include <isc/print.h>
#include <isc/util.h>
ATF_TC(regex_validate);
ATF_TC_HEAD(regex_validate, tc) {
atf_tc_set_md_var(tc, "descr", "check isc_regex_validate()");
}
ATF_TC_BODY(regex_validate, tc) {
/* Set to true for verbose output */
static bool verbose = false;
/* test isc_regex_validate() */
static void
regex_validate(void **state) {
/*
* test regex were generated using http://code.google.com/p/regfuzz/
* modified to use only printable characters
*/
struct {
const char * expression;
const char *expression;
int expect;
int exception; /* regcomp accepts but is disallowed. */
} tests[] = {
......@@ -1070,7 +1080,7 @@ ATF_TC_BODY(regex_validate, tc) {
unsigned int i;
int r;
UNUSED(tc);
UNUSED(state);
#ifdef HAVE_REGEX_H
/*
......@@ -1083,19 +1093,29 @@ ATF_TC_BODY(regex_validate, tc) {
r = regcomp(&preg, tests[i].expression, REG_EXTENDED);
if (((r != 0 && tests[i].expect != -1) ||
(r == 0 && tests[i].expect == -1)) && !tests[i].exception)
fprintf(stderr, "regcomp(%s) -> %s expected %s\n",
tests[i].expression, r != 0 ? "bad" : "good",
tests[i].expect == -1 ? "bad" : "good");
else if (r == 0 &&
preg.re_nsub != (unsigned int)tests[i].expect &&
!tests[i].exception) {
fprintf(stderr, "%s preg.re_nsub %lu expected %d\n",
tests[i].expression,
(unsigned long)preg.re_nsub, tests[i].expect);
tests[i].expect = preg.re_nsub;
{
if (verbose) {
print_error("regcomp(%s) -> %s expected %s\n",
tests[i].expression,
r != 0 ? "bad" : "good",
tests[i].expect == -1
? "bad" : "good");
}
} else if (r == 0 &&
preg.re_nsub != (unsigned int)tests[i].expect &&
!tests[i].exception)
{
if (verbose) {
print_error("%s preg.re_nsub %lu expected %d\n",
tests[i].expression,
(unsigned long)preg.re_nsub,
tests[i].expect);
}
tests[i].expect = preg.re_nsub;
}
if (r == 0)
if (r == 0) {
regfree(&preg);
}
}
#endif
......@@ -1104,18 +1124,42 @@ ATF_TC_BODY(regex_validate, tc) {
*/
for (i = 0; i < sizeof(tests)/sizeof(*tests); i++) {
r = isc_regex_validate(tests[i].expression);
if (r != tests[i].expect)
fprintf(stderr, "%s -> %d expected %d\n",
tests[i].expression, r, tests[i].expect);
ATF_CHECK_EQ(r, tests[i].expect);
if (r != tests[i].expect) {
print_error("# %s -> %d expected %d\n",
tests[i].expression, r, tests[i].expect);
}
assert_int_equal(r, tests[i].expect);
}
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, regex_validate);
return (atf_no_error());
int
main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(regex_validate),
};
int c;
while ((c = isc_commandline_parse(argc, argv, "v")) != -1) {
switch (c) {
case 'v':
verbose = true;
break;
default:
break;
}
}
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (0);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment