testing: new test macros, new testing documentation group.

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.
pull/1987/merge
Jason Ish 9 years ago committed by Victor Julien
parent 796dd5223b
commit 13cf2f829e

@ -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;

@ -24,6 +24,14 @@
* 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;
}
/**
* @}
*/

@ -24,6 +24,12 @@
* 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__ */
/**
* @}
*/

Loading…
Cancel
Save