]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
convert regex_test
authorEvan Hunt <each@isc.org>
Wed, 24 Oct 2018 20:23:43 +0000 (13:23 -0700)
committerEvan Hunt <each@isc.org>
Thu, 15 Nov 2018 04:17:04 +0000 (20:17 -0800)
lib/isc/tests/Kyuafile
lib/isc/tests/Makefile.in
lib/isc/tests/regex_test.c

index 93feb2467740f82a9682d6170c8aae55c0cbb15b..e96fd6e833879d495c4cf2647bc30f1cafcbd7cb 100644 (file)
@@ -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'}
index b95d1be4cc0185dbe2acf84cda56a13e76d97113..77d2d0e645f64be1f901d8c2f583ac25c42dfc5a 100644 (file)
@@ -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} \
index b5e88b31f4efd266e11805e7b4b97b0d2c15a1ab..6cb799e4c9a37f7836ed6c585ec70fe46c794876 100644 (file)
 
 #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