From c43319c3379f1754be63fa5c9586383287b1981c Mon Sep 17 00:00:00 2001 From: Breno Silva Date: Tue, 29 Sep 2009 23:52:47 -0300 Subject: [PATCH] Regular expression for UnitTests Signed-off-by: Brian Rectanus --- src/app-layer-detect-proto.c | 6 +- src/app-layer-http.c | 5 +- src/app-layer-tls.c | 5 +- src/decode-ethernet.c | 4 + src/decode-gre.c | 4 + src/decode-icmpv4.c | 6 +- src/decode-icmpv6.c | 5 + src/decode-ipv4.c | 6 +- src/decode-ppp.c | 4 + src/decode-pppoe.c | 4 + src/decode-tcp.c | 4 + src/decode-udp.c | 4 + src/detect-content.c | 4 - src/detect-decode-event.c | 4 + src/detect-dsize.c | 4 + src/detect-engine-address-ipv6.c | 5 +- src/detect-engine-address.c | 5 + src/detect-engine-port.c | 4 + src/detect-engine-proto.c | 4 + src/detect-flow.c | 4 + src/detect-ipopts.c | 4 + src/detect-parse.c | 4 + src/detect-stream_size.c | 6 +- src/eidps.c | 31 +++++-- src/flow-bit.c | 4 + src/stream-tcp-reassemble.c | 6 +- src/util-bloomfilter.c | 4 + src/util-hashlist.c | 4 + src/util-mpm-b2g.c | 4 + src/util-mpm-b3g.c | 5 +- src/util-mpm-wumanber.c | 5 +- src/util-pool.c | 4 + src/util-unittest.c | 152 ++++++++++++++++++++++++++++--- src/util-unittest.h | 10 +- 34 files changed, 283 insertions(+), 51 deletions(-) diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index 77063df8f4..796d759fe0 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -663,8 +663,10 @@ int AlpDetectTest07(void) { AlpProtoDestroy(&ctx); return r; } +#endif /* UNITTESTS */ void AlpDetectRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("AlpDetectTest01", AlpDetectTest01, 1); UtRegisterTest("AlpDetectTest02", AlpDetectTest02, 1); UtRegisterTest("AlpDetectTest03", AlpDetectTest03, 1); @@ -672,7 +674,5 @@ void AlpDetectRegisterTests(void) { UtRegisterTest("AlpDetectTest05", AlpDetectTest05, 1); UtRegisterTest("AlpDetectTest06", AlpDetectTest06, 1); UtRegisterTest("AlpDetectTest07", AlpDetectTest07, 1); -} - #endif /* UNITTESTS */ - +} diff --git a/src/app-layer-http.c b/src/app-layer-http.c index fc774ad897..f3c5d2ac65 100644 --- a/src/app-layer-http.c +++ b/src/app-layer-http.c @@ -960,8 +960,10 @@ int HTTPParserTest10(void) { end: return result; } +#endif /* UNITTESTS */ void HTTPParserRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("HTTPParserTest01", HTTPParserTest01, 1); UtRegisterTest("HTTPParserTest02", HTTPParserTest02, 1); UtRegisterTest("HTTPParserTest03", HTTPParserTest03, 1); @@ -972,7 +974,6 @@ void HTTPParserRegisterTests(void) { UtRegisterTest("HTTPParserTest08", HTTPParserTest08, 1); UtRegisterTest("HTTPParserTest09", HTTPParserTest09, 1); UtRegisterTest("HTTPParserTest10", HTTPParserTest10, 1); -} - #endif /* UNITTESTS */ +} diff --git a/src/app-layer-tls.c b/src/app-layer-tls.c index f2975370f6..7395ba3267 100644 --- a/src/app-layer-tls.c +++ b/src/app-layer-tls.c @@ -359,12 +359,13 @@ static int TLSParserTest04(void) { end: return result; } +#endif /* UNITTESTS */ void TLSParserRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("TLSParserTest01", TLSParserTest01, 1); UtRegisterTest("TLSParserTest02", TLSParserTest02, 1); UtRegisterTest("TLSParserTest03", TLSParserTest03, 1); UtRegisterTest("TLSParserTest04", TLSParserTest04, 1); -} - #endif /* UNITTESTS */ +} diff --git a/src/decode-ethernet.c b/src/decode-ethernet.c index 2b0bd68dff..04381503f6 100644 --- a/src/decode-ethernet.c +++ b/src/decode-ethernet.c @@ -40,6 +40,7 @@ void DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *p return; } +#ifdef UNITTESTS /** DecodeEthernettest01 * \brief Valid Ethernet packet * \retval 0 Expected test value @@ -77,6 +78,7 @@ static int DecodeEthernetTest01 (void) { return 0; } +#endif /* UNITTESTS */ /** @@ -84,5 +86,7 @@ static int DecodeEthernetTest01 (void) { * \todo More Ethernet tests */ void DecodeEthernetRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodeEthernetTest01", DecodeEthernetTest01, 0); +#endif /* UNITTESTS */ } diff --git a/src/decode-gre.c b/src/decode-gre.c index b2f063e067..82b68c76b9 100644 --- a/src/decode-gre.c +++ b/src/decode-gre.c @@ -215,6 +215,7 @@ void DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u } +#ifdef UNITTESTS /** * \test DecodeGRETest01 is a test for small gre packet */ @@ -314,13 +315,16 @@ static int DecodeGREtest03 (void) { return 1; } +#endif /* UNITTESTS */ /** * \brief this function registers unit tests for GRE decoder */ void DecodeGRERegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodeGREtest01", DecodeGREtest01, 1); UtRegisterTest("DecodeGREtest02", DecodeGREtest02, 1); UtRegisterTest("DecodeGREtest03", DecodeGREtest03, 1); +#endif /* UNITTESTS */ } diff --git a/src/decode-icmpv4.c b/src/decode-icmpv4.c index df316ab39e..ac66d472ea 100644 --- a/src/decode-icmpv4.c +++ b/src/decode-icmpv4.c @@ -429,12 +429,14 @@ static int DecodeICMPV4test08(void) { return 0; } +#endif /* UNITTESTS */ /** * \brief Registers ICMPV4 unit test * \todo More ICMPv4 tests */ void DecodeICMPV4RegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodeICMPV4ttest01", DecodeICMPV4test01, 1); UtRegisterTest("DecodeICMPV4ttest02", DecodeICMPV4test02, 1); UtRegisterTest("DecodeICMPV4ttest03", DecodeICMPV4test03, 1); @@ -445,7 +447,5 @@ void DecodeICMPV4RegisterTests(void) { ICMPV4CalculateInvalidChecksumtest06, 0); UtRegisterTest("DecodeICMPV4InvalidType", ICMPV4InvalidType07, 1); UtRegisterTest("DecodeICMPV4ttest08", DecodeICMPV4test08, 1); -} - #endif /* UNITTESTS */ - +} diff --git a/src/decode-icmpv6.c b/src/decode-icmpv6.c index a3bdf652cb..d1095d83a3 100644 --- a/src/decode-icmpv6.c +++ b/src/decode-icmpv6.c @@ -94,6 +94,8 @@ void DecodeICMPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt return; } +#ifdef UNITTESTS + static int ICMPV6CalculateValidChecksumtest01(void) { uint16_t csum = 0; @@ -147,6 +149,7 @@ static int ICMPV6CalculateInvalidChecksumtest02(void) { return (csum == ICMPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8), (uint16_t *)(raw_ipv6 + 54), 68)); } +#endif /* UNITTESTS */ /** * \brief Registers ICMPV6 unit tests @@ -154,8 +157,10 @@ static int ICMPV6CalculateInvalidChecksumtest02(void) { */ void DecodeICMPV6RegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("ICMPV6CalculateValidChecksumtest01", ICMPV6CalculateValidChecksumtest01, 1); UtRegisterTest("ICMPV6CalculateInValidChecksumtest02", ICMPV6CalculateInvalidChecksumtest02, 0); +#endif /* UNITTESTS */ } diff --git a/src/decode-ipv4.c b/src/decode-ipv4.c index a8712f9873..d9f3494c4f 100644 --- a/src/decode-ipv4.c +++ b/src/decode-ipv4.c @@ -1425,8 +1425,10 @@ static int IPV4CalculateInvalidChecksumtest02(void) return (csum == IPV4CalculateChecksum((uint16_t *)raw_ipv4, sizeof(raw_ipv4))); } +#endif /* UNITTESTS */ void DecodeIPV4RegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodeIPV4OptionsNONETest01", DecodeIPV4OptionsNONETest01, 1); UtRegisterTest("DecodeIPV4OptionsEOLTest01", DecodeIPV4OptionsEOLTest01, 1); UtRegisterTest("DecodeIPV4OptionsNOPTest01", DecodeIPV4OptionsNOPTest01, 1); @@ -1459,7 +1461,5 @@ void DecodeIPV4RegisterTests(void) { IPV4CalculateValidChecksumtest01, 1); UtRegisterTest("IPV4CalculateInvalidChecksumtest02", IPV4CalculateInvalidChecksumtest02, 0); - -} - #endif /* UNITTESTS */ +} diff --git a/src/decode-ppp.c b/src/decode-ppp.c index 09abfb1507..9de47e24eb 100644 --- a/src/decode-ppp.c +++ b/src/decode-ppp.c @@ -101,6 +101,7 @@ void DecodePPP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u } /* TESTS BELOW */ +#ifdef UNITTESTS /* DecodePPPtest01 * Decode malformed ip layer PPP packet @@ -244,11 +245,14 @@ static int DecodePPPtest04 (void) { return 1; } +#endif /* UNITTESTS */ void DecodePPPRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodePPPtest01", DecodePPPtest01, 1); UtRegisterTest("DecodePPPtest02", DecodePPPtest02, 1); UtRegisterTest("DecodePPPtest03", DecodePPPtest03, 1); UtRegisterTest("DecodePPPtest04", DecodePPPtest04, 1); +#endif /* UNITTESTS */ } diff --git a/src/decode-pppoe.c b/src/decode-pppoe.c index 8dd7781301..76cd3e2f7a 100644 --- a/src/decode-pppoe.c +++ b/src/decode-pppoe.c @@ -185,6 +185,7 @@ void DecodePPPOESession(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_ } } +#ifdef UNITTESTS /** DecodePPPOEtest01 * \brief Decode malformed PPPOE packet (too short) * \retval 1 Expected test value @@ -336,6 +337,7 @@ static int DecodePPPOEtest05 (void) { return 0; } +#endif /* UNITTESTS */ @@ -344,10 +346,12 @@ static int DecodePPPOEtest05 (void) { * \todo More PPPOE tests */ void DecodePPPOERegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodePPPOEtest01", DecodePPPOEtest01, 1); UtRegisterTest("DecodePPPOEtest02", DecodePPPOEtest02, 0); UtRegisterTest("DecodePPPOEtest03", DecodePPPOEtest03, 0); UtRegisterTest("DecodePPPOEtest04", DecodePPPOEtest04, 1); UtRegisterTest("DecodePPPOEtest05", DecodePPPOEtest05, 1); +#endif /* UNITTESTS */ } diff --git a/src/decode-tcp.c b/src/decode-tcp.c index 18713cd889..accdd62882 100644 --- a/src/decode-tcp.c +++ b/src/decode-tcp.c @@ -281,6 +281,7 @@ void DecodeTCP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u return; } +#ifdef UNITTESTS static int TCPCalculateValidChecksumtest01(void) { uint16_t csum = 0; @@ -495,9 +496,11 @@ static int TCPGetWscaleTest03(void) end: return retval; } +#endif /* UNITTESTS */ void DecodeTCPRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("TCPCalculateValidChecksumtest01", TCPCalculateValidChecksumtest01, 1); UtRegisterTest("TCPCalculateInvalidChecksumtest02", @@ -509,4 +512,5 @@ void DecodeTCPRegisterTests(void) UtRegisterTest("TCPGetWscaleTest01", TCPGetWscaleTest01, 1); UtRegisterTest("TCPGetWscaleTest02", TCPGetWscaleTest02, 1); UtRegisterTest("TCPGetWscaleTest03", TCPGetWscaleTest03, 1); +#endif /* UNITTESTS */ } diff --git a/src/decode-udp.c b/src/decode-udp.c index 10de36a1ec..9b9fded523 100644 --- a/src/decode-udp.c +++ b/src/decode-udp.c @@ -177,6 +177,7 @@ void DecodeUDP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u return; } +#ifdef UNITTESTS static int UDPV4CalculateValidChecksumtest01(void) { uint16_t csum = 0; @@ -274,9 +275,11 @@ static int UDPV6CalculateInvalidChecksumtest04(void) return (csum == UDPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8), (uint16_t *)(raw_ipv6 + 54), 20)); } +#endif /* UNITTESTS */ void DecodeUDPV4RegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("UDPV4CalculateValidChecksumtest01", UDPV4CalculateValidChecksumtest01, 1); UtRegisterTest("UDPV4CalculateInvalidChecksumtest02", @@ -285,4 +288,5 @@ void DecodeUDPV4RegisterTests(void) UDPV6CalculateValidChecksumtest03, 1); UtRegisterTest("UDPV6CalculateInvalidChecksumtest04", UDPV6CalculateInvalidChecksumtest04, 0); +#endif /* UNITTESTS */ } diff --git a/src/detect-content.c b/src/detect-content.c index c96e29fc3a..a54d797246 100644 --- a/src/detect-content.c +++ b/src/detect-content.c @@ -634,8 +634,6 @@ int DetectContentParseTest08 (void) { } return result; } - - #endif /* UNITTESTS */ /** @@ -651,7 +649,5 @@ void DetectContentRegisterTests(void) { UtRegisterTest("DetectContentParseTest06", DetectContentParseTest06, 1); UtRegisterTest("DetectContentParseTest07", DetectContentParseTest07, 1); UtRegisterTest("DetectContentParseTest08", DetectContentParseTest08, 1); - #endif /* UNITTESTS */ } - diff --git a/src/detect-decode-event.c b/src/detect-decode-event.c index b18b09cf12..682519d1fd 100644 --- a/src/detect-decode-event.c +++ b/src/detect-decode-event.c @@ -183,6 +183,7 @@ void DetectDecodeEventFree(DetectDecodeEventData *de) { /* * ONLY TESTS BELOW THIS COMMENT */ +#ifdef UNITTESTS /** * \test DecodeEventTestParse01 is a test for a valid decode-event value @@ -294,15 +295,18 @@ error: if (sm) free(sm); return 0; } +#endif /* UNITTESTS */ /** * \brief this function registers unit tests for DecodeEvent */ void DecodeEventRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DecodeEventTestParse01", DecodeEventTestParse01, 1); UtRegisterTest("DecodeEventTestParse02", DecodeEventTestParse02, 1); UtRegisterTest("DecodeEventTestParse03", DecodeEventTestParse03, 1); UtRegisterTest("DecodeEventTestParse04", DecodeEventTestParse04, 0); UtRegisterTest("DecodeEventTestParse05", DecodeEventTestParse05, 0); UtRegisterTest("DecodeEventTestParse06", DecodeEventTestParse06, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-dsize.c b/src/detect-dsize.c index ea0f4b5bb9..0c99619935 100644 --- a/src/detect-dsize.c +++ b/src/detect-dsize.c @@ -203,6 +203,7 @@ void DetectDsizeFree(DetectDsizeData *dd) { * ONLY TESTS BELOW THIS COMMENT */ +#ifdef UNITTESTS int DsizeTestParse01 (void) { DetectDsizeData *dd = NULL; dd = DetectDsizeParse("1"); @@ -393,8 +394,10 @@ int DsizeTestParse16 (void) { return 1; } +#endif /* UNITTESTS */ void DsizeRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DsizeTestParse01", DsizeTestParse01, 1); UtRegisterTest("DsizeTestParse02", DsizeTestParse02, 1); UtRegisterTest("DsizeTestParse03", DsizeTestParse03, 1); @@ -411,5 +414,6 @@ void DsizeRegisterTests(void) { UtRegisterTest("DsizeTestParse14", DsizeTestParse14, 1); UtRegisterTest("DsizeTestParse15", DsizeTestParse15, 1); UtRegisterTest("DsizeTestParse16", DsizeTestParse16, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-engine-address-ipv6.c b/src/detect-engine-address-ipv6.c index 928fd4c1db..ac6b56ba55 100644 --- a/src/detect-engine-address-ipv6.c +++ b/src/detect-engine-address-ipv6.c @@ -768,7 +768,7 @@ int DetectAddressGroupJoinIPv6(DetectEngineCtx *de_ctx, DetectAddressGroup *targ /* TESTS */ - +#ifdef UNITTESTS int AddressTestIPv6Gt01 (void) { int result = 0; @@ -1093,8 +1093,10 @@ int AddressTestIPv6SubOne02 (void) { return result; } +#endif /* UNITTESTS */ void DetectAddressIPv6Tests(void) { +#ifdef UNITTESTS UtRegisterTest("AddressTestIPv6Gt01", AddressTestIPv6Gt01, 1); UtRegisterTest("AddressTestIPv6Gt02", AddressTestIPv6Gt02, 1); UtRegisterTest("AddressTestIPv6Gt03", AddressTestIPv6Gt03, 1); @@ -1124,6 +1126,7 @@ void DetectAddressIPv6Tests(void) { UtRegisterTest("AddressTestIPv6SubOne01", AddressTestIPv6SubOne01, 1); UtRegisterTest("AddressTestIPv6SubOne02", AddressTestIPv6SubOne02, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-engine-address.c b/src/detect-engine-address.c index 16ce382dd0..7a0dd04c72 100644 --- a/src/detect-engine-address.c +++ b/src/detect-engine-address.c @@ -1311,6 +1311,8 @@ DetectAddressLookupGroup(DetectAddressGroupsHead *gh, Address *a) { /* TESTS */ + +#ifdef UNITTESTS int AddressTestParse01 (void) { DetectAddressData *dd = NULL; dd = DetectAddressParse("1.2.3.4"); @@ -3701,8 +3703,10 @@ error: DetectAddressDataFree(c); return 0; } +#endif /* UNITTESTS */ void DetectAddressTests(void) { +#ifdef UNITTESTS DetectAddressIPv6Tests(); UtRegisterTest("AddressTestParse01", AddressTestParse01, 1); @@ -3822,6 +3826,7 @@ void DetectAddressTests(void) { UtRegisterTest("AddressTestCutIPv408", AddressTestCutIPv408, 1); UtRegisterTest("AddressTestCutIPv409", AddressTestCutIPv409, 1); UtRegisterTest("AddressTestCutIPv410", AddressTestCutIPv410, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-engine-port.c b/src/detect-engine-port.c index 684748207a..9959b18ea6 100644 --- a/src/detect-engine-port.c +++ b/src/detect-engine-port.c @@ -1253,6 +1253,7 @@ DetectPort *DetectPortSpHashLookup(DetectEngineCtx *de_ctx, DetectPort *p) { /* TESTS */ +#ifdef UNITTESTS int PortTestParse01 (void) { DetectPort *dd = NULL; @@ -1438,9 +1439,11 @@ int PortTestParse09 (void) { end: return result; } +#endif /* UNITTESTS */ void DetectPortTests(void) { +#ifdef UNITTESTS UtRegisterTest("PortTestParse01", PortTestParse01, 1); UtRegisterTest("PortTestParse02", PortTestParse02, 1); UtRegisterTest("PortTestParse03", PortTestParse03, 1); @@ -1450,5 +1453,6 @@ void DetectPortTests(void) { UtRegisterTest("PortTestParse07", PortTestParse07, 1); UtRegisterTest("PortTestParse08", PortTestParse08, 1); UtRegisterTest("PortTestParse09", PortTestParse09, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-engine-proto.c b/src/detect-engine-proto.c index 10456aa06f..35cd453b08 100644 --- a/src/detect-engine-proto.c +++ b/src/detect-engine-proto.c @@ -75,6 +75,7 @@ int DetectProtoSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char * /* TESTS */ +#ifdef UNITTESTS static int ProtoTestParse01 (void) { DetectProto dp; memset(&dp,0,sizeof(DetectProto)); @@ -110,11 +111,14 @@ static int ProtoTestParse03 (void) { return 0; } +#endif /* UNITTESTS */ void DetectProtoTests(void) { +#ifdef UNITTESTS UtRegisterTest("ProtoTestParse01", ProtoTestParse01, 1); UtRegisterTest("ProtoTestParse02", ProtoTestParse02, 1); UtRegisterTest("ProtoTestParse03", ProtoTestParse03, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-flow.c b/src/detect-flow.c index e418a0370f..f76a80aa66 100644 --- a/src/detect-flow.c +++ b/src/detect-flow.c @@ -294,6 +294,7 @@ void DetectFlowFree(void *ptr) { free(fd); } +#ifdef UNITTESTS /** * \test DetectFlowTestParse01 is a test to make sure that we return "something" * when given valid flow opt @@ -667,11 +668,13 @@ int DetectFlowTestParse21 (void) { return result; } +#endif /* UNITTESTS */ /** * \brief this function registers unit tests for DetectFlow */ void DetectFlowRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("DetectFlowTestParse01", DetectFlowTestParse01, 1); UtRegisterTest("DetectFlowTestParse02", DetectFlowTestParse02, 1); UtRegisterTest("DetectFlowTestParse03", DetectFlowTestParse03, 1); @@ -693,4 +696,5 @@ void DetectFlowRegisterTests(void) { UtRegisterTest("DetectFlowTestParse19", DetectFlowTestParse19, 1); UtRegisterTest("DetectFlowTestParse20", DetectFlowTestParse20, 1); UtRegisterTest("DetectFlowTestParse21", DetectFlowTestParse21, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-ipopts.c b/src/detect-ipopts.c index 18dff076a6..c0d5cdfbb7 100644 --- a/src/detect-ipopts.c +++ b/src/detect-ipopts.c @@ -202,6 +202,7 @@ void DetectIpOptsFree(void *de_ptr) { * ONLY TESTS BELOW THIS COMMENT */ +#ifdef UNITTESTS /** * \test IpOptsTestParse01 is a test for a valid ipopts value * @@ -327,13 +328,16 @@ error: if (sm) free(sm); return 0; } +#endif /* UNITTESTS */ /** * \brief this function registers unit tests for IpOpts */ void IpOptsRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01, 1); UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02, 0); UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03, 1); UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04, 0); +#endif /* UNITTESTS */ } diff --git a/src/detect-parse.c b/src/detect-parse.c index 1899007782..e9cfe116b5 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -521,6 +521,7 @@ error: * TESTS */ +#ifdef UNITTESTS int SigParseTest01 (void) { int result = 1; Signature *sig = NULL; @@ -791,8 +792,10 @@ end: DetectEngineCtxFree(de_ctx); return result; } +#endif /* UNITTESTS */ void SigParseRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("SigParseTest01", SigParseTest01, 1); UtRegisterTest("SigParseTest02", SigParseTest02, 1); UtRegisterTest("SigParseTest03", SigParseTest03, 1); @@ -804,5 +807,6 @@ void SigParseRegisterTests(void) { UtRegisterTest("SigParseTestNegation05", SigParseTestNegation05, 1); UtRegisterTest("SigParseTestNegation06", SigParseTestNegation06, 1); UtRegisterTest("SigParseTestNegation07", SigParseTestNegation07, 1); +#endif /* UNITTESTS */ } diff --git a/src/detect-stream_size.c b/src/detect-stream_size.c index a0e55ef957..dfd522629c 100644 --- a/src/detect-stream_size.c +++ b/src/detect-stream_size.c @@ -329,6 +329,7 @@ void DetectStreamSizeFree(void *ptr) { free(sd); } +#ifdef UNITTESTS /** * \test DetectStreamSizeParseTest01 is a test to make sure that we parse the * user options correctly, when given valid stream_size options. @@ -479,16 +480,17 @@ static int DetectStreamSizeParseTest04 (void) { return result; } +#endif /* UNITTESTS */ /** * \brief this function registers unit tests for DetectStreamSize */ - void DetectStreamSizeRegisterTests(void) { - +#ifdef UNITTESTS UtRegisterTest("DetectStreamSizeParseTest01", DetectStreamSizeParseTest01, 1); UtRegisterTest("DetectStreamSizeParseTest02", DetectStreamSizeParseTest02, 1); UtRegisterTest("DetectStreamSizeParseTest03", DetectStreamSizeParseTest03, 1); UtRegisterTest("DetectStreamSizeParseTest04", DetectStreamSizeParseTest04, 1); +#endif /* UNITTESTS */ } diff --git a/src/eidps.c b/src/eidps.c index 29882a7d63..019d2fd23e 100644 --- a/src/eidps.c +++ b/src/eidps.c @@ -229,14 +229,15 @@ void EngineKill(void) { void usage(const char *progname) { printf("USAGE: %s\n\n", progname); - printf("\t-c : path to configuration file\n"); - printf("\t-i : run in pcap live mode\n"); - printf("\t-r : run in pcap file/offline mode\n"); - printf("\t-q : run in inline nfqueue mode\n"); - printf("\t-s : path to signature file (optional)\n"); - printf("\t-l : default log directory\n"); + printf("\t-c : path to configuration file\n"); + printf("\t-i : run in pcap live mode\n"); + printf("\t-r : run in pcap file/offline mode\n"); + printf("\t-q : run in inline nfqueue mode\n"); + printf("\t-s : path to signature file (optional)\n"); + printf("\t-l : default log directory\n"); #ifdef UNITTESTS - printf("\t-u : run the unittests and exit\n"); + printf("\t-u : run the unittests and exit\n"); + printf("\t-U, --unittest-filter=REGEX : filter unittests with a regex\n"); #endif /* UNITTESTS */ printf("\n"); } @@ -251,6 +252,7 @@ int main(int argc, char **argv) char *sig_file = NULL; int nfq_id = 0; char *conf_filename = NULL; + char *regex_arg = NULL; int dump_config = 0; /* initialize the logging subsys */ @@ -263,13 +265,14 @@ int main(int argc, char **argv) {"dump-config", 0, &dump_config, 1}, {"pfring-int", required_argument, 0, 0}, {"pfring-clusterid", required_argument, 0, 0}, + {"unittest-filter", required_argument, 0, 'U'}, {NULL, 0, NULL, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; - char short_opts[] = "c:hi:l:q:r:us:"; + char short_opts[] = "c:hi:l:q:r:us:U:"; while ((opt = getopt_long(argc, argv, short_opts, long_opts, &option_index)) != -1) { switch (opt) { @@ -325,6 +328,14 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); #endif /* UNITTESTS */ break; + case 'U': +#ifdef UNITTESTS + regex_arg = optarg; + + if(strlen(regex_arg) == 0) + regex_arg = NULL; +#endif + break; default: usage(argv[0]); exit(EXIT_FAILURE); @@ -399,7 +410,7 @@ int main(int argc, char **argv) #ifdef UNITTESTS if (mode == MODE_UNITTEST) { /* test and initialize the unittesting subsystem */ - UtRunSelftest(); /* inits and cleans up again */ + UtRunSelftest(regex_arg); /* inits and cleans up again */ UtInitialize(); TmModuleRegisterTests(); SigTableRegisterTests(); @@ -429,7 +440,7 @@ int main(int argc, char **argv) SCSigRegisterSignatureOrderingTests(); SCLogRegisterTests(); SCRadixRegisterTests(); - uint32_t failed = UtRunTests(); + uint32_t failed = UtRunTests(regex_arg); UtCleanup(); if (failed) exit(EXIT_FAILURE); else exit(EXIT_SUCCESS); diff --git a/src/flow-bit.c b/src/flow-bit.c index 9b29b05921..7a9b812dc5 100644 --- a/src/flow-bit.c +++ b/src/flow-bit.c @@ -161,6 +161,7 @@ void FlowBitFree(FlowBit *fb) { /* TESTS */ +#ifdef UNITTESTS static int FlowBitTest01 (void) { int ret = 0; @@ -410,8 +411,10 @@ end: GenericVarFree(f.flowvar); return ret; } +#endif /* UNITTESTS */ void FlowBitRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("FlowBitTest01", FlowBitTest01, 1); UtRegisterTest("FlowBitTest02", FlowBitTest02, 1); UtRegisterTest("FlowBitTest03", FlowBitTest03, 1); @@ -423,5 +426,6 @@ void FlowBitRegisterTests(void) { UtRegisterTest("FlowBitTest09", FlowBitTest09, 1); UtRegisterTest("FlowBitTest10", FlowBitTest10, 1); UtRegisterTest("FlowBitTest11", FlowBitTest11, 1); +#endif /* UNITTESTS */ } diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index 2dcb250b70..3eeef90203 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -2989,12 +2989,14 @@ end: StreamTcpReassembleFreeThreadCtx(ra_ctx); return ret; } +#endif /* UNITTESTS */ /** \brief The Function Register the Unit tests to test the reassembly engine * for various OS policies. */ void StreamTcpReassembleRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("StreamTcpReassembleTest01 -- BSD OS Before Reassembly Test", StreamTcpReassembleTest01, 1); UtRegisterTest("StreamTcpReassembleTest02 -- BSD OS At Same Reassembly Test", StreamTcpReassembleTest02, 1); UtRegisterTest("StreamTcpReassembleTest03 -- BSD OS After Reassembly Test", StreamTcpReassembleTest03, 1); @@ -3025,7 +3027,5 @@ void StreamTcpReassembleRegisterTests(void) { UtRegisterTest("StreamTcpReassembleTest28 -- Gap at Start IDS missed packet Reassembly Test", StreamTcpReassembleTest28, 1); UtRegisterTest("StreamTcpReassembleTest29 -- Gap at Middle IDS missed packet Reassembly Test", StreamTcpReassembleTest29, 1); UtRegisterTest("StreamTcpReassembleTest30 -- Gap at End IDS missed packet Reassembly Test", StreamTcpReassembleTest30, 1); -} - #endif /* UNITTESTS */ - +} diff --git a/src/util-bloomfilter.c b/src/util-bloomfilter.c index 91d4910734..7e83a9066a 100644 --- a/src/util-bloomfilter.c +++ b/src/util-bloomfilter.c @@ -127,6 +127,7 @@ static uint32_t BloomHash(void *data, uint16_t datalen, uint8_t iter, uint32_t h * ONLY TESTS BELOW THIS COMMENT */ +#ifdef UNITTESTS static int BloomFilterTestInit01 (void) { BloomFilter *bf = BloomFilterInit(1024, 4, BloomHash); if (bf == NULL) @@ -248,8 +249,10 @@ end: if (bf != NULL) BloomFilterFree(bf); return result; } +#endif /* UNITTESTS */ void BloomFilterRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("BloomFilterTestInit01", BloomFilterTestInit01, 1); UtRegisterTest("BloomFilterTestInit02", BloomFilterTestInit02, 1); UtRegisterTest("BloomFilterTestInit03", BloomFilterTestInit03, 1); @@ -261,5 +264,6 @@ void BloomFilterRegisterTests(void) { UtRegisterTest("BloomFilterTestFull01", BloomFilterTestFull01, 1); UtRegisterTest("BloomFilterTestFull02", BloomFilterTestFull02, 1); +#endif /* UNITTESTS */ } diff --git a/src/util-hashlist.c b/src/util-hashlist.c index 32de6e708a..446133f724 100644 --- a/src/util-hashlist.c +++ b/src/util-hashlist.c @@ -232,6 +232,7 @@ HashListTableBucket *HashListTableGetListHead(HashListTable *ht) { * ONLY TESTS BELOW THIS COMMENT */ +#ifdef UNITTESTS static int HashListTableTestInit01 (void) { HashListTable *ht = HashListTableInit(1024, HashListTableGenericHash, NULL, NULL); if (ht == NULL) @@ -421,8 +422,10 @@ end: if (ht != NULL) HashListTableFree(ht); return result; } +#endif /* UNITTESTS */ void HashListTableRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("HashListTableTestInit01", HashListTableTestInit01, 1); UtRegisterTest("HashListTableTestInit02", HashListTableTestInit02, 1); UtRegisterTest("HashListTableTestInit03", HashListTableTestInit03, 1); @@ -435,5 +438,6 @@ void HashListTableRegisterTests(void) { UtRegisterTest("HashListTableTestFull01", HashListTableTestFull01, 1); UtRegisterTest("HashListTableTestFull02", HashListTableTestFull02, 1); +#endif /* UNITTESTS */ } diff --git a/src/util-mpm-b2g.c b/src/util-mpm-b2g.c index ecd96c3866..ab245179cf 100644 --- a/src/util-mpm-b2g.c +++ b/src/util-mpm-b2g.c @@ -1740,6 +1740,7 @@ uint32_t B2gSearch1(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, PatternMatche * TESTS */ +#ifdef UNITTESTS static int B2gTestInit01 (void) { int result = 0; MpmCtx mpm_ctx; @@ -2491,8 +2492,10 @@ static int B2gTestSearch12 (void) { B2gDestroyCtx(&mpm_ctx); return result; } +#endif /* UNITTESTS */ void B2gRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("B2gTestInit01", B2gTestInit01, 1); /* UtRegisterTest("B2gTestS0Init01", B2gTestS0Init01, 1); @@ -2526,6 +2529,7 @@ void B2gRegisterTests(void) { UtRegisterTest("B2gTestSearch10", B2gTestSearch10, 1); UtRegisterTest("B2gTestSearch11", B2gTestSearch11, 1); UtRegisterTest("B2gTestSearch12", B2gTestSearch12, 1); +#endif /* UNITTESTS */ } #if 0 diff --git a/src/util-mpm-b3g.c b/src/util-mpm-b3g.c index 8857659327..ae3e14a437 100644 --- a/src/util-mpm-b3g.c +++ b/src/util-mpm-b3g.c @@ -1961,6 +1961,7 @@ uint32_t B3gSearch1(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, PatternMatche * TESTS */ +#ifdef UNITTESTS static int B3gTestInit01 (void) { int result = 0; MpmCtx mpm_ctx; @@ -2714,8 +2715,10 @@ static int B3gTestSearch12 (void) { B3gDestroyCtx(&mpm_ctx); return result; } +#endif /* UNITTESTS */ void B3gRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("B3gTestInit01", B3gTestInit01, 1); /* UtRegisterTest("B3gTestS0Init01", B3gTestS0Init01, 1); @@ -2750,7 +2753,7 @@ void B3gRegisterTests(void) { UtRegisterTest("B3gTestSearch10", B3gTestSearch10, 1); UtRegisterTest("B3gTestSearch11", B3gTestSearch11, 1); UtRegisterTest("B3gTestSearch12", B3gTestSearch12, 1); - +#endif /* UNITTESTS */ } #if 0 diff --git a/src/util-mpm-wumanber.c b/src/util-mpm-wumanber.c index f3d53ac911..e3ff0d6013 100644 --- a/src/util-mpm-wumanber.c +++ b/src/util-mpm-wumanber.c @@ -2360,7 +2360,7 @@ void WmThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) { * ONLY TESTS BELOW THIS COMMENT */ - +#ifdef UNITTESTS int WmTestInitCtx01 (void) { int result = 0; MpmCtx mpm_ctx; @@ -3852,8 +3852,10 @@ static int WmTestSearch22Hash16 (void) { WmDestroyCtx(&mpm_ctx); return result; } +#endif /* UNITTESTS */ void WmRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("WmTestInitCtx01", WmTestInitCtx01, 1); UtRegisterTest("WmTestInitCtx02", WmTestInitCtx02, 1); UtRegisterTest("WmTestInitCtx03", WmTestInitCtx03, 1); @@ -3929,5 +3931,6 @@ void WmRegisterTests(void) { UtRegisterTest("WmTestSearch22Hash14", WmTestSearch22Hash14, 1); UtRegisterTest("WmTestSearch22Hash15", WmTestSearch22Hash15, 1); UtRegisterTest("WmTestSearch22Hash16", WmTestSearch22Hash16, 1); +#endif /* UNITTESTS */ } diff --git a/src/util-pool.c b/src/util-pool.c index 4b8758ccd6..e3a1e8ea20 100644 --- a/src/util-pool.c +++ b/src/util-pool.c @@ -170,6 +170,7 @@ void PoolTestFree(void *ptr) { free(ptr); } +#ifdef UNITTESTS static int PoolTestInit01 (void) { Pool *p = PoolInit(10,5,PoolTestAlloc,NULL,PoolTestFree); if (p == NULL) @@ -403,13 +404,16 @@ end: PoolFree(p); return retval; } +#endif /* UNITTESTS */ void PoolRegisterTests(void) { +#ifdef UNITTESTS UtRegisterTest("PoolTestInit01", PoolTestInit01, 1); UtRegisterTest("PoolTestInit02", PoolTestInit02, 1); UtRegisterTest("PoolTestInit03", PoolTestInit03, 1); UtRegisterTest("PoolTestInit04", PoolTestInit04, 1); UtRegisterTest("PoolTestInit05", PoolTestInit05, 1); UtRegisterTest("PoolTestInit06", PoolTestInit06, 1); +#endif /* UNITTESTS */ } diff --git a/src/util-unittest.c b/src/util-unittest.c index 70633f4e3e..b760f8a616 100644 --- a/src/util-unittest.c +++ b/src/util-unittest.c @@ -1,8 +1,24 @@ +/** Copyright (c) 2009 Open Information Security Foundation + * + * \author Victor Julien + * \author Breno Silva + */ + + #include "eidps-common.h" #include "util-unittest.h" +static pcre *parse_regex; +static pcre_extra *parse_regex_study; + static UtTest *ut_list; +/** + * \brief Allocate UtTest list member + * + * \retval ut Pointer to UtTest + */ + static UtTest *UtAllocTest(void) { UtTest *ut = malloc(sizeof(UtTest)); if (ut == NULL) { @@ -15,6 +31,15 @@ static UtTest *UtAllocTest(void) { return ut; } +/** + * \brief Append test in UtTest list + * + * \param list Pointer to the start of the IP packet + * \param test Pointer to unit test + * + * \retval 0 Function always returns zero + */ + static int UtAppendTest(UtTest **list, UtTest *test) { if (*list == NULL) { *list = test; @@ -30,6 +55,15 @@ static int UtAppendTest(UtTest **list, UtTest *test) { return 0; } +/** + * \brief Register unit test + * + * \param name Unit test name + * \param TestFn Unit test function + * \param evalue Unit test function return value + * + */ + void UtRegisterTest(char *name, int(*TestFn)(void), int evalue) { UtTest *ut = UtAllocTest(); if (ut == NULL) @@ -44,27 +78,87 @@ void UtRegisterTest(char *name, int(*TestFn)(void), int evalue) { UtAppendTest(&ut_list, ut); } +/** + * \brief Compile a regex to run a specific unit test + * + * \param regex_arg The regular expression + * + * \retval 1 Regex compiled + * \retval -1 Regex error + */ + +int UtRegex (char *regex_arg) { + const char *eb; + int eo; + int opts = 0; + + if(regex_arg == NULL) + return -1; + + parse_regex = pcre_compile(regex_arg, opts, &eb, &eo, NULL); + if(parse_regex == NULL) + { + printf("pcre compile of \"%s\" failed at offset %" PRId32 ": %s\n", regex_arg, eo, eb); + goto error; + } + + parse_regex_study = pcre_study(parse_regex, 0, &eb); + if(eb != NULL) + { + printf("pcre study failed: %s\n", eb); + goto error; + } + + return 1; + +error: + return -1; +} + /** \brief Run all registered unittests. + * + * \param regex_arg The regular expression * * \retval 0 all successful * \retval result number of tests that failed */ -uint32_t UtRunTests(void) { + +uint32_t UtRunTests(char *regex_arg) { UtTest *ut; uint32_t good = 0, bad = 0; +#define MAX_SUBSTRINGS 30 + int ret = 0, rcomp = 0; + int ov[MAX_SUBSTRINGS]; + + rcomp = UtRegex(regex_arg); for (ut = ut_list; ut != NULL; ut = ut->next) { - printf("Test %-60s : ", ut->name); - fflush(stdout); /* flush so in case of a segv we see the testname */ - int ret = ut->TestFn(); - printf("%s\n", (ret == ut->evalue) ? "pass" : "FAILED"); - if (ret != ut->evalue) { - bad++; - } else { - good++; + if(rcomp == 1) { + ret = pcre_exec(parse_regex, parse_regex_study, ut->name, strlen(ut->name), 0, 0, ov, MAX_SUBSTRINGS); + if( ret >= 1 ) { + printf("Test %-60s : ", ut->name); + fflush(stdout); /* flush so in case of a segv we see the testname */ + ret = ut->TestFn(); + printf("%s\n", (ret == ut->evalue) ? "pass" : "FAILED"); + if (ret != ut->evalue) { + bad++; + } else { + good++; + } + } + } + else { + printf("Test %-60s : ", ut->name); + fflush(stdout); /* flush so in case of a segv we see the testname */ + ret = ut->TestFn(); + printf("%s\n", (ret == ut->evalue) ? "pass" : "FAILED"); + if (ret != ut->evalue) { + bad++; + } else { + good++; + } } } - printf("==== TEST RESULTS ====\n"); printf("PASSED: %" PRIu32 "\n", good); printf("FAILED: %" PRIu32 "\n", bad); @@ -72,10 +166,18 @@ uint32_t UtRunTests(void) { return bad; } +/** + * \brief Initialize unit test list + */ + void UtInitialize(void) { ut_list = NULL; } +/** + * \brief Cleanup unit test list + */ + void UtCleanup(void) { UtTest *tmp = ut_list, *otmp; @@ -89,16 +191,40 @@ void UtCleanup(void) { ut_list = NULL; } +#ifdef UNITTESTS + +/** \brief True test + * + * \retval 1 True + * \retval 0 False + */ + int UtSelftestTrue(void) { if (1)return 1; else return 0; } + +/** \brief False test + * + * \retval 1 False + * \retval 0 True + */ + int UtSelftestFalse(void) { if (0)return 1; else return 0; } +#endif /* UNITTESTS */ -int UtRunSelftest (void) { +/** \brief Run self tests + * + * \param regex_arg The regular expression + * + * \retval 0 all successful + */ + +int UtRunSelftest (char *regex_arg) { +#ifdef UNITTESTS printf("* Running Unittesting subsystem selftests...\n"); UtInitialize(); @@ -106,7 +232,7 @@ int UtRunSelftest (void) { UtRegisterTest("true", UtSelftestTrue, 1); UtRegisterTest("false", UtSelftestFalse, 0); - int ret = UtRunTests(); + int ret = UtRunTests(regex_arg); if (ret == 0) printf("* Done running Unittesting subsystem selftests...\n"); @@ -114,6 +240,6 @@ int UtRunSelftest (void) { printf("* ERROR running Unittesting subsystem selftests failed...\n"); UtCleanup(); +#endif /* UNITTESTS */ return 0; } - diff --git a/src/util-unittest.h b/src/util-unittest.h index f91e00dff9..0a2f3a10a5 100644 --- a/src/util-unittest.h +++ b/src/util-unittest.h @@ -1,4 +1,8 @@ -/* Copyright (c) 2008 Victor Julien */ +/** Copyright (c) 2009 Open Information Security Foundation + * + * \author Victor Julien + * \author Breno Silva + */ #ifndef __UTIL_UNITTEST_H__ #define __UTIL_UNITTEST_H__ @@ -15,10 +19,10 @@ typedef struct UtTest_ { void UtRegisterTest(char *name, int(*TestFn)(void), int evalue); -uint32_t UtRunTests(void); +uint32_t UtRunTests(char *regex_arg); void UtInitialize(void); void UtCleanup(void); -int UtRunSelftest (void); +int UtRunSelftest (char *regex_arg); #endif /* __UTIL_UNITTEST_H__ */