}
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;
* Unit test framework
*/
+/**
+ * \defgroup Testing Testing
+ *
+ * \brief Unit testing support functions.
+ *
+ * @{
+ */
+
#include "suricata-common.h"
#include "runmodes.h"
#include "util-unittest.h"
static UtTest *ut_list;
+int unittests_fatal = 0;
+
/**
* \brief Allocate UtTest list member
*
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);
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);
}
if (0)return 0;
else return 1;
}
+
#endif /* UNITTESTS */
/** \brief Run self tests
#endif /* UNITTESTS */
return 0;
}
+
+/**
+ * @}
+ */
* Unit test framework
*/
+/**
+ * \addtogroup Testing
+ *
+ * @{
+ */
+
#ifndef __UTIL_UNITTEST_H__
#define __UTIL_UNITTEST_H__
} UtTest;
-
void UtRegisterTest(char *name, int(*TestFn)(void));
uint32_t UtRunTests(char *regex_arg);
void UtInitialize(void);
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__ */
+/**
+ * @}
+ */