Add --unittests-coverage option to list how many code modules have tests

pull/420/merge
Victor Julien 13 years ago
parent fc7879322e
commit 9faa4b740d

@ -729,6 +729,9 @@ void RegisterDCERPCUDPParsers(void) {
DCERPCUDPParse); DCERPCUDPParse);
AppLayerRegisterStateFuncs(ALPROTO_DCERPC_UDP, DCERPCUDPStateAlloc, AppLayerRegisterStateFuncs(ALPROTO_DCERPC_UDP, DCERPCUDPStateAlloc,
DCERPCUDPStateFree); DCERPCUDPStateFree);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_DCERPC_UDP, DCERPCUDPParserRegisterTests);
#endif
} }
/* UNITTESTS */ /* UNITTESTS */

@ -1878,6 +1878,9 @@ void RegisterDCERPCParsers(void) {
DCERPCParseResponse); DCERPCParseResponse);
AppLayerRegisterStateFuncs(ALPROTO_DCERPC, DCERPCStateAlloc, AppLayerRegisterStateFuncs(ALPROTO_DCERPC, DCERPCStateAlloc,
DCERPCStateFree); DCERPCStateFree);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_DCERPC, DCERPCParserRegisterTests);
#endif
} }
/* UNITTESTS */ /* UNITTESTS */

@ -279,6 +279,9 @@ void RegisterFTPParsers(void) {
FTP_FIELD_REQUEST_LINE, FTPParseRequestCommandLine, FTP_FIELD_REQUEST_LINE, FTPParseRequestCommandLine,
"ftp"); "ftp");
AppLayerRegisterStateFuncs(ALPROTO_FTP, FTPStateAlloc, FTPStateFree); AppLayerRegisterStateFuncs(ALPROTO_FTP, FTPStateAlloc, FTPStateFree);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_FTP, FTPParserRegisterTests);
#endif
} }
void FTPAtExitPrintStats(void) { void FTPAtExitPrintStats(void) {

@ -2425,6 +2425,9 @@ void RegisterHTPParsers(void)
HTPHandleRequestData); HTPHandleRequestData);
AppLayerRegisterProto(proto_name, ALPROTO_HTTP, STREAM_TOCLIENT, AppLayerRegisterProto(proto_name, ALPROTO_HTTP, STREAM_TOCLIENT,
HTPHandleResponseData); HTPHandleResponseData);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_HTTP, HTPParserRegisterTests);
#endif
SC_ATOMIC_INIT(htp_config_flags); SC_ATOMIC_INIT(htp_config_flags);
HTPConfigure(); HTPConfigure();

@ -45,6 +45,7 @@
#include "app-layer-protos.h" #include "app-layer-protos.h"
#include "app-layer-parser.h" #include "app-layer-parser.h"
#include "app-layer-smb.h" #include "app-layer-smb.h"
#include "app-layer-smb2.h"
#include "app-layer-dcerpc.h" #include "app-layer-dcerpc.h"
#include "app-layer-dcerpc-udp.h" #include "app-layer-dcerpc-udp.h"
#include "app-layer-htp.h" #include "app-layer-htp.h"
@ -727,6 +728,12 @@ int AppLayerRegisterProto(char *name, uint8_t proto, uint8_t flags,
return 0; return 0;
} }
#ifdef UNITTESTS
void AppLayerRegisterUnittests(uint16_t proto, void (*RegisterUnittests)(void)) {
al_proto_table[proto].RegisterUnittests = RegisterUnittests;
}
#endif
void AppLayerRegisterStateFuncs(uint16_t proto, void *(*StateAlloc)(void), void AppLayerRegisterStateFuncs(uint16_t proto, void *(*StateAlloc)(void),
void (*StateFree)(void *)) void (*StateFree)(void *))
{ {
@ -1337,6 +1344,8 @@ void RegisterAppLayerParsers(void)
RegisterHTPParsers(); RegisterHTPParsers();
RegisterSSLParsers(); RegisterSSLParsers();
RegisterSMBParsers(); RegisterSMBParsers();
/** \todo bug 719 */
//RegisterSMB2Parsers();
RegisterDCERPCParsers(); RegisterDCERPCParsers();
RegisterDCERPCUDPParsers(); RegisterDCERPCUDPParsers();
RegisterFTPParsers(); RegisterFTPParsers();
@ -5869,6 +5878,25 @@ static int AppLayerProbingParserTest15(void)
void AppLayerParserRegisterTests(void) void AppLayerParserRegisterTests(void)
{ {
#ifdef UNITTESTS #ifdef UNITTESTS
int i;
for (i = 0; i < ALPROTO_MAX; i++) {
AppLayerProto *p = &al_proto_table[i];
if (p->name == NULL)
continue;
g_ut_modules++;
if (p->RegisterUnittests != NULL) {
p->RegisterUnittests();
g_ut_covered++;
} else {
if (coverage_unittests)
SCLogWarning(SC_WARN_NO_UNITTESTS, "app layer module %s has no "
"unittests", p->name);
}
}
UtRegisterTest("AppLayerParserTest01", AppLayerParserTest01, 1); UtRegisterTest("AppLayerParserTest01", AppLayerParserTest01, 1);
UtRegisterTest("AppLayerParserTest02", AppLayerParserTest02, 1); UtRegisterTest("AppLayerParserTest02", AppLayerParserTest02, 1);
UtRegisterTest("AppLayerProbingParserTest01", AppLayerProbingParserTest01, 1); UtRegisterTest("AppLayerProbingParserTest01", AppLayerProbingParserTest01, 1);

@ -65,6 +65,10 @@ typedef struct AppLayerProto_ {
uint64_t (*StateGetTxCnt)(void *alstate); uint64_t (*StateGetTxCnt)(void *alstate);
void *(*StateGetTx)(void *alstate, uint64_t tx_id); void *(*StateGetTx)(void *alstate, uint64_t tx_id);
int (*StateGetAlstateProgressCompletionStatus)(uint8_t direction); int (*StateGetAlstateProgressCompletionStatus)(uint8_t direction);
#ifdef UNITTESTS
void (*RegisterUnittests)(void);
#endif
} AppLayerProto; } AppLayerProto;
/** flags for the result elmts */ /** flags for the result elmts */
@ -249,6 +253,9 @@ void AppLayerRegisterProbingParser(struct AlpProtoDetectCtx_ *, uint16_t, uint16
uint16_t, uint16_t, uint8_t, uint8_t, uint16_t, uint16_t, uint8_t, uint8_t,
uint8_t, uint8_t,
uint16_t (*ProbingParser)(uint8_t *, uint32_t)); uint16_t (*ProbingParser)(uint8_t *, uint32_t));
#ifdef UNITTESTS
void AppLayerRegisterUnittests(uint16_t proto, void (*RegisterUnittests)(void));
#endif
void AppLayerRegisterStateFuncs(uint16_t proto, void *(*StateAlloc)(void), void AppLayerRegisterStateFuncs(uint16_t proto, void *(*StateAlloc)(void),
void (*StateFree)(void *)); void (*StateFree)(void *));
void AppLayerRegisterLocalStorageFunc(uint16_t proto, void AppLayerRegisterLocalStorageFunc(uint16_t proto,

@ -1419,7 +1419,9 @@ void RegisterSMBParsers(void) {
STREAM_TOSERVER, STREAM_TOSERVER,
APP_LAYER_PROBING_PARSER_PRIORITY_HIGH, 1, APP_LAYER_PROBING_PARSER_PRIORITY_HIGH, 1,
SMBProbingParser); SMBProbingParser);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_SMB, SMBParserRegisterTests);
#endif
return; return;
} }

@ -521,6 +521,9 @@ void RegisterSMB2Parsers(void) {
AppLayerRegisterProto("smb", ALPROTO_SMB2, STREAM_TOSERVER, SMB2Parse); AppLayerRegisterProto("smb", ALPROTO_SMB2, STREAM_TOSERVER, SMB2Parse);
AppLayerRegisterProto("smb", ALPROTO_SMB2, STREAM_TOCLIENT, SMB2Parse); AppLayerRegisterProto("smb", ALPROTO_SMB2, STREAM_TOCLIENT, SMB2Parse);
AppLayerRegisterStateFuncs(ALPROTO_SMB2, SMB2StateAlloc, SMB2StateFree); AppLayerRegisterStateFuncs(ALPROTO_SMB2, SMB2StateAlloc, SMB2StateFree);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_SMB2, SMB2ParserRegisterTests);
#endif
} }
/* UNITTESTS */ /* UNITTESTS */
@ -585,7 +588,6 @@ end:
} }
void SMB2ParserRegisterTests(void) { void SMB2ParserRegisterTests(void) {
printf("SMB2ParserRegisterTests\n");
UtRegisterTest("SMB2ParserTest01", SMB2ParserTest01, 1); UtRegisterTest("SMB2ParserTest01", SMB2ParserTest01, 1);
} }
#endif #endif

@ -865,6 +865,9 @@ void RegisterSMTPParsers(void)
SMTPSetMpmState(); SMTPSetMpmState();
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_SMTP, SMTPParserRegisterTests);
#endif
return; return;
} }

@ -747,7 +747,9 @@ void RegisterSSHParsers(void)
SSHParseClientRecord); SSHParseClientRecord);
AppLayerRegisterStateFuncs(ALPROTO_SSH, SSHStateAlloc, SSHStateFree); AppLayerRegisterStateFuncs(ALPROTO_SSH, SSHStateAlloc, SSHStateFree);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_SSH, SSHParserRegisterTests);
#endif
} }
/* UNITTESTS */ /* UNITTESTS */

@ -989,6 +989,9 @@ void RegisterSSLParsers(void)
STREAM_TOSERVER, STREAM_TOSERVER,
APP_LAYER_PROBING_PARSER_PRIORITY_HIGH, 1, APP_LAYER_PROBING_PARSER_PRIORITY_HIGH, 1,
SSLProbingParser); SSLProbingParser);
#ifdef UNITTESTS
AppLayerRegisterUnittests(ALPROTO_TLS, SSLParserRegisterTests);
#endif
/* Get the value of no reassembly option from the config file */ /* Get the value of no reassembly option from the config file */
if (ConfGetBool("tls.no-reassemble", &ssl_config.no_reassemble) != 1) if (ConfGetBool("tls.no-reassemble", &ssl_config.no_reassemble) != 1)

@ -4903,22 +4903,23 @@ void SigTableSetup(void) {
DetectLuajitRegister(); DetectLuajitRegister();
DetectIPRepRegister(); DetectIPRepRegister();
DetectDnsQueryRegister(); DetectDnsQueryRegister();
uint8_t i = 0;
for (i = 0; i < DETECT_TBLSIZE; i++) {
if (sigmatch_table[i].RegisterTests == NULL) {
SCLogDebug("detection plugin %s has no unittest "
"registration function.", sigmatch_table[i].name);
}
}
} }
void SigTableRegisterTests(void) { void SigTableRegisterTests(void) {
/* register the tests */ /* register the tests */
uint8_t i = 0; int i = 0;
for (i = 0; i < DETECT_TBLSIZE; i++) { for (i = 0; i < DETECT_TBLSIZE; i++) {
g_ut_modules++;
if (sigmatch_table[i].RegisterTests != NULL) { if (sigmatch_table[i].RegisterTests != NULL) {
sigmatch_table[i].RegisterTests(); sigmatch_table[i].RegisterTests();
g_ut_covered++;
} else {
SCLogDebug("detection plugin %s has no unittest "
"registration function.", sigmatch_table[i].name);
if (coverage_unittests)
SCLogWarning(SC_WARN_NO_UNITTESTS, "detection plugin %s has no unittest "
"registration function.", sigmatch_table[i].name);
} }
} }
} }

@ -313,5 +313,8 @@ typedef enum PacketProfileDetectId_ {
size_t strlcat(char *, const char *src, size_t siz); size_t strlcat(char *, const char *src, size_t siz);
size_t strlcpy(char *dst, const char *src, size_t siz); size_t strlcpy(char *dst, const char *src, size_t siz);
extern int coverage_unittests;
extern int g_ut_modules;
extern int g_ut_covered;
#endif /* __SURICATA_COMMON_H__ */ #endif /* __SURICATA_COMMON_H__ */

@ -514,6 +514,7 @@ void usage(const char *progname)
printf("\t-U, --unittest-filter=REGEX : filter unittests with a regex\n"); printf("\t-U, --unittest-filter=REGEX : filter unittests with a regex\n");
printf("\t--list-unittests : list unit tests\n"); printf("\t--list-unittests : list unit tests\n");
printf("\t--fatal-unittests : enable fatal failure on unittest error\n"); printf("\t--fatal-unittests : enable fatal failure on unittest error\n");
printf("\t--unittests-coverage : display unittest coverage report\n");
#endif /* UNITTESTS */ #endif /* UNITTESTS */
printf("\t--list-app-layer-protos : list supported app layer protocols\n"); printf("\t--list-app-layer-protos : list supported app layer protocols\n");
printf("\t--list-keywords[=all|csv|<kword>] : list keywords implemented by the engine\n"); printf("\t--list-keywords[=all|csv|<kword>] : list keywords implemented by the engine\n");
@ -703,6 +704,10 @@ void SCPrintBuildInfo(void) {
#include "build-info.h" #include "build-info.h"
} }
int coverage_unittests;
int g_ut_modules;
int g_ut_covered;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int opt; int opt;
@ -789,6 +794,12 @@ int main(int argc, char **argv)
/* Initialize the configuration module. */ /* Initialize the configuration module. */
ConfInit(); ConfInit();
#ifdef UNITTESTS
coverage_unittests = 0;
g_ut_modules = 0;
g_ut_covered = 0;
#endif
struct option long_opts[] = { struct option long_opts[] = {
{"dump-config", 0, &dump_config, 1}, {"dump-config", 0, &dump_config, 1},
{"pfring", optional_argument, 0, 0}, {"pfring", optional_argument, 0, 0},
@ -817,6 +828,7 @@ int main(int argc, char **argv)
{"pidfile", required_argument, 0, 0}, {"pidfile", required_argument, 0, 0},
{"init-errors-fatal", 0, 0, 0}, {"init-errors-fatal", 0, 0, 0},
{"fatal-unittests", 0, 0, 0}, {"fatal-unittests", 0, 0, 0},
{"unittests-coverage", 0, &coverage_unittests, 1},
{"user", required_argument, 0, 0}, {"user", required_argument, 0, 0},
{"group", required_argument, 0, 0}, {"group", required_argument, 0, 0},
{"erf-in", required_argument, 0, 0}, {"erf-in", required_argument, 0, 0},
@ -1615,7 +1627,7 @@ int main(int argc, char **argv)
SCLogInfo("Memory used at startup: %"PRIdMAX, (intmax_t)global_mem); SCLogInfo("Memory used at startup: %"PRIdMAX, (intmax_t)global_mem);
#endif #endif
/* test and initialize the unittesting subsystem */ /* test and initialize the unittesting subsystem */
if(regex_arg == NULL){ if(regex_arg == NULL && !coverage_unittests){
regex_arg = ".*"; regex_arg = ".*";
UtRunSelftest(regex_arg); /* inits and cleans up again */ UtRunSelftest(regex_arg); /* inits and cleans up again */
} }
@ -1638,15 +1650,9 @@ int main(int argc, char **argv)
FlowBitRegisterTests(); FlowBitRegisterTests();
FlowAlertSidRegisterTests(); FlowAlertSidRegisterTests();
SCPerfRegisterTests(); SCPerfRegisterTests();
DecodePPPRegisterTests(); DecodePPPRegisterTests();
DecodeVLANRegisterTests(); DecodeVLANRegisterTests();
HTPParserRegisterTests();
SSLParserRegisterTests();
SSHParserRegisterTests();
SMBParserRegisterTests();
DCERPCParserRegisterTests();
DCERPCUDPParserRegisterTests();
FTPParserRegisterTests();
DecodeRawRegisterTests(); DecodeRawRegisterTests();
DecodePPPOERegisterTests(); DecodePPPOERegisterTests();
DecodeICMPV4RegisterTests(); DecodeICMPV4RegisterTests();
@ -1657,6 +1663,7 @@ int main(int argc, char **argv)
DecodeUDPV4RegisterTests(); DecodeUDPV4RegisterTests();
DecodeGRERegisterTests(); DecodeGRERegisterTests();
DecodeAsn1RegisterTests(); DecodeAsn1RegisterTests();
AlpDetectRegisterTests(); AlpDetectRegisterTests();
ConfRegisterTests(); ConfRegisterTests();
ConfYamlRegisterTests(); ConfYamlRegisterTests();
@ -1701,7 +1708,6 @@ int main(int argc, char **argv)
DetectEngineHttpHRHRegisterTests(); DetectEngineHttpHRHRegisterTests();
DetectEngineRegisterTests(); DetectEngineRegisterTests();
SCLogRegisterTests(); SCLogRegisterTests();
SMTPParserRegisterTests();
MagicRegisterTests(); MagicRegisterTests();
UtilMiscRegisterTests(); UtilMiscRegisterTests();
DetectAddressTests(); DetectAddressTests();
@ -1713,8 +1719,11 @@ int main(int argc, char **argv)
#endif #endif
if (list_unittests) { if (list_unittests) {
UtListTests(regex_arg); UtListTests(regex_arg);
} } else if (coverage_unittests) {
else { /* nothing */
SCLogInfo("%d out of %d code modules have unittests (%.0f%%)",
g_ut_covered, g_ut_modules, (float)((float)g_ut_covered/(float)g_ut_modules)*100);
} else {
uint32_t failed = UtRunTests(regex_arg); uint32_t failed = UtRunTests(regex_arg);
UtCleanup(); UtCleanup();
if (failed) { if (failed) {

@ -202,11 +202,16 @@ void TmModuleRegisterTests(void) {
if (t->name == NULL) if (t->name == NULL)
continue; continue;
g_ut_modules++;
if (t->RegisterTests == NULL) { if (t->RegisterTests == NULL) {
SCLogDebug("threading module %s has no unittest " if (coverage_unittests)
SCLogWarning(SC_WARN_NO_UNITTESTS, "threading module %s has no unittest "
"registration function.", t->name); "registration function.", t->name);
} else { } else {
t->RegisterTests(); t->RegisterTests();
g_ut_covered++;
} }
} }
#endif /* UNITTESTS */ #endif /* UNITTESTS */

@ -274,6 +274,7 @@ const char * SCErrorToString(SCError err)
CASE_CODE (SC_ERR_CUDA_BUFFER_ERROR); CASE_CODE (SC_ERR_CUDA_BUFFER_ERROR);
CASE_CODE (SC_ERR_DNS_LOG_GENERIC); CASE_CODE (SC_ERR_DNS_LOG_GENERIC);
CASE_CODE (SC_WARN_OPTION_OBSOLETE); CASE_CODE (SC_WARN_OPTION_OBSOLETE);
CASE_CODE (SC_WARN_NO_UNITTESTS);
} }
return "UNKNOWN_ERROR"; return "UNKNOWN_ERROR";

@ -263,6 +263,7 @@ typedef enum {
SC_ERR_CUDA_BUFFER_ERROR, SC_ERR_CUDA_BUFFER_ERROR,
SC_ERR_DNS_LOG_GENERIC, SC_ERR_DNS_LOG_GENERIC,
SC_WARN_OPTION_OBSOLETE, SC_WARN_OPTION_OBSOLETE,
SC_WARN_NO_UNITTESTS,
} SCError; } SCError;
const char *SCErrorToString(SCError); const char *SCErrorToString(SCError);

@ -663,10 +663,15 @@ void MpmRegisterTests(void) {
if (i == MPM_NOTSET) if (i == MPM_NOTSET)
continue; continue;
g_ut_modules++;
if (mpm_table[i].RegisterUnittests != NULL) { if (mpm_table[i].RegisterUnittests != NULL) {
g_ut_covered++;
mpm_table[i].RegisterUnittests(); mpm_table[i].RegisterUnittests();
} else { } else {
printf("Warning: mpm %s has no unittest registration function...", mpm_table[i].name); if (coverage_unittests)
SCLogWarning(SC_WARN_NO_UNITTESTS, "mpm module %s has no "
"unittest registration function.", mpm_table[i].name);
} }
} }

Loading…
Cancel
Save