]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
fuzz: remove UNITTEST dependency
authorVictor Julien <victor@inliniac.net>
Tue, 7 Apr 2020 10:41:12 +0000 (12:41 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 8 Apr 2020 20:45:48 +0000 (22:45 +0200)
Expose UTH flow builder to new 'FUZZ' define as well. Move UTHbufferToFile
as well and rename it to a more generic 'TestHelperBufferToFile'.

This way UNITTESTS can be disabled. This leads to smaller code size
and more realistic testing as in some parts of the code things
behave slightly differently when UNITTESTS are enabled.

configure.ac
src/tests/fuzz/fuzz_applayerprotodetectgetproto.c
src/tests/fuzz/fuzz_decodepcapfile.c
src/tests/fuzz/fuzz_sigpcap.c
src/util-unittest-helper.c
src/util-unittest-helper.h

index 28dedd06df8f5e8238cda6661789533f48176858..58208420b3472b72f0bdd15a6668632ce348ea18 100644 (file)
     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])
     ])
index 27b271924e5cd88e621279b7255ea08a29e0ae30..4c6320879deed218e3a02e2c4f6fb629aef99d7b 100644 (file)
@@ -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;
 }
index 28cf661f6e64f9ba7e219f9a3232605cfd27e669..3b77f2b4254eefb018d65201aca7a24138c780c8 100644 (file)
@@ -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;
     }
 
index bce14c3fbcd256f25bb112ca543b94dcf5a80096..92b407f1f80c7937e1e89373e75238f204e16348 100644 (file)
@@ -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;
     }
 
index 0078df7e9f1f541e31d70335f41ee7176daea43c..1afccfdf4b6e5a157916e0d34c70a894e11c09c9 100644 (file)
 #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
  */
index 05e7963ffd5cb72329d863f4d7a8692789c912d8..fcecec3056dbc3d596894d8845a2860491e91f24 100644 (file)
 #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);