diff --git a/configure.ac b/configure.ac index 28dedd06df..58208420b3 100644 --- a/configure.ac +++ b/configure.ac @@ -441,6 +441,7 @@ AM_CONDITIONAL([BUILD_FUZZTARGETS], [test "x$enable_fuzztargets" = "xyes"]) AC_PROG_CXX AS_IF([test "x$enable_fuzztargets" = "xyes"], [ + AC_DEFINE([FUZZ], [1], [Fuzz targets are enabled]) AC_DEFINE([AFLFUZZ_NO_RANDOM], [1], [Disable all use of random functions]) CFLAGS_ORIG=$CFLAGS CFLAGS="-Werror" @@ -478,9 +479,6 @@ return 0; # enable the running of unit tests AC_ARG_ENABLE(unittests, AS_HELP_STRING([--enable-unittests], [Enable compilation of the unit tests]),[enable_unittests=$enableval],[enable_unittests=no]) - AS_IF([test "x$enable_fuzztargets" = "xyes"], [ - export enable_unittests="yes" - ]) AS_IF([test "x$enable_unittests" = "xyes"], [ AC_DEFINE([UNITTESTS],[1],[Enable built-in unittests]) ]) diff --git a/src/tests/fuzz/fuzz_applayerprotodetectgetproto.c b/src/tests/fuzz/fuzz_applayerprotodetectgetproto.c index 27b271924e..4c6320879d 100644 --- a/src/tests/fuzz/fuzz_applayerprotodetectgetproto.c +++ b/src/tests/fuzz/fuzz_applayerprotodetectgetproto.c @@ -45,7 +45,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) alpd_tctx = AppLayerProtoDetectGetCtxThread(); } - f = UTHBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (data[2] << 8) | data[3], (data[4] << 8) | data[5]); + f = TestHelperBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (data[2] << 8) | data[3], (data[4] << 8) | data[5]); if (f == NULL) { return 0; } @@ -69,7 +69,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) } } } - UTHFreeFlow(f); + FlowFree(f); return 0; } diff --git a/src/tests/fuzz/fuzz_decodepcapfile.c b/src/tests/fuzz/fuzz_decodepcapfile.c index 28cf661f6e..3b77f2b425 100644 --- a/src/tests/fuzz/fuzz_decodepcapfile.c +++ b/src/tests/fuzz/fuzz_decodepcapfile.c @@ -83,7 +83,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) } //rewrite buffer to a file as libpcap does not have buffer inputs - if (UTHbufferToFile("/tmp/fuzz.pcap", data, size) < 0) { + if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) { return 0; } diff --git a/src/tests/fuzz/fuzz_sigpcap.c b/src/tests/fuzz/fuzz_sigpcap.c index bce14c3fbc..92b407f1f8 100644 --- a/src/tests/fuzz/fuzz_sigpcap.c +++ b/src/tests/fuzz/fuzz_sigpcap.c @@ -119,11 +119,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) } if (pos > 0 && pos < size) { // dump signatures to a file so as to reuse SigLoadSignatures - if (UTHbufferToFile(suricata.sig_file, data, pos-1) < 0) { + if (TestHelperBufferToFile(suricata.sig_file, data, pos-1) < 0) { return 0; } } else { - if (UTHbufferToFile(suricata.sig_file, data, pos) < 0) { + if (TestHelperBufferToFile(suricata.sig_file, data, pos) < 0) { return 0; } } @@ -139,7 +139,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) size -= pos; //rewrite buffer to a file as libpcap does not have buffer inputs - if (UTHbufferToFile("/tmp/fuzz.pcap", data, size) < 0) { + if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) { return 0; } diff --git a/src/util-unittest-helper.c b/src/util-unittest-helper.c index 0078df7e9f..1afccfdf4b 100644 --- a/src/util-unittest-helper.c +++ b/src/util-unittest-helper.c @@ -45,6 +45,80 @@ #include "util-unittest.h" #include "util-unittest-helper.h" +#if defined(UNITTESTS) || defined(FUZZ) +Flow *TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp) +{ + struct in_addr in; + + Flow *f = SCMalloc(sizeof(Flow)); + if (unlikely(f == NULL)) { + printf("FlowAlloc failed\n"); + ; + return NULL; + } + memset(f, 0x00, sizeof(Flow)); + + FLOW_INITIALIZE(f); + + if (family == AF_INET) { + f->flags |= FLOW_IPV4; + } else if (family == AF_INET6) { + f->flags |= FLOW_IPV6; + } + + if (src != NULL) { + if (family == AF_INET) { + if (inet_pton(AF_INET, src, &in) != 1) { + printf("invalid address %s\n", src); + SCFree(f); + return NULL; + } + f->src.addr_data32[0] = in.s_addr; + } else { + BUG_ON(1); + } + } + if (dst != NULL) { + if (family == AF_INET) { + if (inet_pton(AF_INET, dst, &in) != 1) { + printf("invalid address %s\n", dst); + SCFree(f); + return NULL; + } + f->dst.addr_data32[0] = in.s_addr; + } else { + BUG_ON(1); + } + } + + f->sp = sp; + f->dp = dp; + + return f; +} +/** \brief writes the contents of a buffer into a file */ +int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size) +{ + if (remove(name) != 0) { + if (errno != ENOENT) { + printf("failed remove, errno=%d\n", errno); + return -1; + } + } + FILE *fd = fopen(name, "wb"); + if (fd == NULL) { + printf("failed open, errno=%d\n", errno); + return -2; + } + if (fwrite (data, 1, size, fd) != size) { + fclose(fd); + return -3; + } + fclose(fd); + return 0; +} + +#endif #ifdef UNITTESTS /** @@ -445,53 +519,7 @@ void UTHAssignFlow(Packet *p, Flow *f) Flow *UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp) { - struct in_addr in; - - Flow *f = SCMalloc(sizeof(Flow)); - if (unlikely(f == NULL)) { - printf("FlowAlloc failed\n"); - ; - return NULL; - } - memset(f, 0x00, sizeof(Flow)); - - FLOW_INITIALIZE(f); - - if (family == AF_INET) { - f->flags |= FLOW_IPV4; - } else if (family == AF_INET6) { - f->flags |= FLOW_IPV6; - } - - if (src != NULL) { - if (family == AF_INET) { - if (inet_pton(AF_INET, src, &in) != 1) { - printf("invalid address %s\n", src); - SCFree(f); - return NULL; - } - f->src.addr_data32[0] = in.s_addr; - } else { - BUG_ON(1); - } - } - if (dst != NULL) { - if (family == AF_INET) { - if (inet_pton(AF_INET, dst, &in) != 1) { - printf("invalid address %s\n", dst); - SCFree(f); - return NULL; - } - f->dst.addr_data32[0] = in.s_addr; - } else { - BUG_ON(1); - } - } - - f->sp = sp; - f->dp = dp; - - return f; + return TestHelperBuildFlow(family, src, dst, sp, dp); } void UTHFreeFlow(Flow *flow) @@ -942,28 +970,6 @@ int UTHParseSignature(const char *str, bool expect) PASS; } -/** \brief writes the contents of a buffer into a file */ -int UTHbufferToFile(const char * name, const uint8_t *data, size_t size) { - FILE * fd; - if (remove(name) != 0) { - if (errno != ENOENT) { - printf("failed remove, errno=%d\n", errno); - return -1; - } - } - fd = fopen(name, "wb"); - if (fd == NULL) { - printf("failed open, errno=%d\n", errno); - return -2; - } - if (fwrite (data, 1, size, fd) != size) { - fclose(fd); - return -3; - } - fclose(fd); - return 0; -} - /* * unittests for the unittest helpers */ diff --git a/src/util-unittest-helper.h b/src/util-unittest-helper.h index 05e7963ffd..fcecec3056 100644 --- a/src/util-unittest-helper.h +++ b/src/util-unittest-helper.h @@ -24,6 +24,10 @@ #ifndef __UTIL_UNITTEST_HELPER__ #define __UTIL_UNITTEST_HELPER__ +#if defined(UNITTESTS) || defined(FUZZ) +Flow *TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp); +int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size); +#endif #ifdef UNITTESTS uint32_t UTHSetIPv4Address(const char *); @@ -63,7 +67,6 @@ Packet *UTHBuildPacketIPV6Real(uint8_t *, uint16_t , uint8_t ipproto, const char void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len); int UTHParseSignature(const char *str, bool expect); -int UTHbufferToFile(const char * name, const uint8_t *data, size_t size); #endif void UTHRegisterTests(void);