]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
testing: new test macros, new testing documentation group.
authorJason Ish <ish@unx.ca>
Wed, 6 Apr 2016 22:50:08 +0000 (16:50 -0600)
committerVictor Julien <victor@inliniac.net>
Fri, 8 Apr 2016 10:28:09 +0000 (12:28 +0200)
Unit testing support macros for failing on expressions,
as well as passing tests on expressions.

If fatal unittests are enabled BUG_ON will be triggered for
an assertion providing the line number of the failure, otherwise
the test will simply fail.

Moved the fatal flag to a global var instead of a configuration
parameter for ease of access from a macro.

src/suricata.c
src/util-unittest.c
src/util-unittest.h

index f47aa5aa8f90b51050e259dbcbdb0b6ba9eb67b0..280181b13eb94084225b013f831bb471edd9b306 100644 (file)
@@ -1398,10 +1398,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
             }
             else if(strcmp((long_opts[option_index]).name, "fatal-unittests") == 0) {
 #ifdef UNITTESTS
-                if (ConfSetFinal("unittests.failure-fatal", "1") != 1) {
-                    fprintf(stderr, "ERROR: Failed to set unittests failure-fatal.\n");
-                    return TM_ECODE_FAILED;
-                }
+                unittests_fatal = 1;
 #else
                 fprintf(stderr, "ERROR: Unit tests not enabled. Make sure to pass --enable-unittests to configure when building.\n");
                 return TM_ECODE_FAILED;
index f4e9a454512e6ac15178ed83b9a0e2d633c05e89..0503a2431b0ab5818cf07ffb00108528e8646edf 100644 (file)
  * Unit test framework
  */
 
+/**
+ * \defgroup Testing Testing
+ *
+ * \brief Unit testing support functions.
+ *
+ * @{
+ */
+
 #include "suricata-common.h"
 #include "runmodes.h"
 #include "util-unittest.h"
@@ -38,6 +46,8 @@ static pcre_extra *parse_regex_study;
 
 static UtTest *ut_list;
 
+int unittests_fatal = 0;
+
 /**
  * \brief Allocate UtTest list member
  *
@@ -181,12 +191,6 @@ uint32_t UtRunTests(char *regex_arg)
     uint32_t good = 0, bad = 0, matchcnt = 0;
     int ret = 0, rcomp = 0;
     int ov[MAX_SUBSTRINGS];
-    int failure_fatal;
-
-    if (ConfGetBool("unittests.failure-fatal", &failure_fatal) != 1) {
-        SCLogDebug("ConfGetBool could not load the value.");
-        failure_fatal = 0;
-    }
 
     rcomp = UtRegex(regex_arg);
 
@@ -205,7 +209,7 @@ uint32_t UtRunTests(char *regex_arg)
                 ret = ut->TestFn();
                 printf("%s\n", ret ? "pass" : "FAILED");
                 if (!ret) {
-                    if (failure_fatal == 1) {
+                    if (unittests_fatal == 1) {
                         fprintf(stderr, "ERROR: unittest failed.\n");
                         exit(EXIT_FAILURE);
                     }
@@ -292,6 +296,7 @@ int UtSelftestFalse(void)
     if (0)return 0;
     else  return 1;
 }
+
 #endif /* UNITTESTS */
 
 /** \brief Run self tests
@@ -321,3 +326,7 @@ int UtRunSelftest (char *regex_arg)
 #endif /* UNITTESTS */
     return 0;
 }
+
+/**
+ * @}
+ */
index ccf8ff6f9cb53411596ee459bb6d031eef18425e..cca59b869b56d8c9a721bca6a12ed89f10b92cb1 100644 (file)
  * Unit test framework
  */
 
+/**
+ * \addtogroup Testing
+ *
+ * @{
+ */
+
 #ifndef __UTIL_UNITTEST_H__
 #define __UTIL_UNITTEST_H__
 
@@ -38,7 +44,6 @@ typedef struct UtTest_ {
 
 } UtTest;
 
-
 void UtRegisterTest(char *name, int(*TestFn)(void));
 uint32_t UtRunTests(char *regex_arg);
 void UtInitialize(void);
@@ -47,7 +52,64 @@ int UtRunSelftest (char *regex_arg);
 void UtListTests(char *regex_arg);
 void UtRunModeRegister(void);
 
+extern int unittests_fatal;
+
+/**
+ * \brief Fail a test if expression evaluates to false.
+ */
+#define FAIL_IF(expr) do {                             \
+        if (unittests_fatal) {                         \
+            BUG_ON(expr);                              \
+        } else if (expr) {                             \
+            return 0;                                  \
+        }                                              \
+    } while (0)
+
+/**
+ * \brief Fail a test if expression to true.
+ */
+#define FAIL_IF_NOT(expr) do { \
+        FAIL_IF(!(expr));      \
+    } while (0)
+
+/**
+ * \brief Fail a test if expression evaluates to NULL.
+ */
+#define FAIL_IF_NULL(expr) do {                 \
+        FAIL_IF(NULL == expr);                  \
+    } while (0)
+
+/**
+ * \brief Fail a test if expression evaluates to non-NULL.
+ */
+#define FAIL_IF_NOT_NULL(expr) do { \
+        FAIL_IF(NULL != expr);      \
+    } while (0)
+
+/**
+ * \brief Pass the test.
+ *
+ * Only to be used at the end of a function instead instead of "return 1."
+ */
+#define PASS do { \
+        return 1; \
+    } while (0)
+
 #endif
 
+/**
+ * \brief Pass the test if expression evaluates to true.
+ *
+ * Only to be used at the end of a function instead of returning the
+ * result of an expression.
+ */
+#define PASS_IF(expr) do { \
+        FAIL_IF(!(expr));  \
+        PASS;              \
+    } while (0)
+
 #endif /* __UTIL_UNITTEST_H__ */
 
+/**
+ * @}
+ */