From: Evan Hunt Date: Wed, 24 Oct 2018 20:23:43 +0000 (-0700) Subject: convert regex_test X-Git-Tag: v9.13.4~21^2~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd009b34a6802ef4dde47436ce1a52a4ba0d8afb;p=thirdparty%2Fbind9.git convert regex_test --- diff --git a/lib/isc/tests/Kyuafile b/lib/isc/tests/Kyuafile index 93feb246774..e96fd6e8338 100644 --- a/lib/isc/tests/Kyuafile +++ b/lib/isc/tests/Kyuafile @@ -18,7 +18,7 @@ tap_test_program{name='parse_test'} tap_test_program{name='pool_test'} tap_test_program{name='queue_test'} tap_test_program{name='radix_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'} diff --git a/lib/isc/tests/Makefile.in b/lib/isc/tests/Makefile.in index b95d1be4cc0..77d2d0e645f 100644 --- a/lib/isc/tests/Makefile.in +++ b/lib/isc/tests/Makefile.in @@ -146,8 +146,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} \ diff --git a/lib/isc/tests/regex_test.c b/lib/isc/tests/regex_test.c index b5e88b31f4e..6cb799e4c9a 100644 --- a/lib/isc/tests/regex_test.c +++ b/lib/isc/tests/regex_test.c @@ -11,31 +11,41 @@ #include -#include +#if HAVE_CMOCKA + +#include +#include +#include #include #include #include #include + +#define UNIT_TESTING +#include + #ifdef HAVE_REGEX_H #include #endif +#include #include #include #include -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 + +int +main(void) { + printf("1..0 # Skipped: cmocka not available\n"); + return (0); } +#endif