]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/alert: add unittests to check packet action
authorJuliana Fajardini <jufajardini@oisf.net>
Mon, 1 Aug 2022 23:04:22 +0000 (20:04 -0300)
committerVictor Julien <vjulien@oisf.net>
Thu, 25 Aug 2022 10:38:08 +0000 (12:38 +0200)
Add unittests to check that packet flags are correctly updated after
detection finds drop or reject rules that match.

Related to
Bug #5458

src/Makefile.am
src/detect-engine-alert.c
src/detect-engine-alert.h
src/runmode-unittests.c
src/tests/detect-engine-alert.c [new file with mode: 0644]

index 30762104f46d06fc5af2ad242ed4fb2e94938d43..ba9187061dc431be77da0574767768b223dad091 100755 (executable)
@@ -1219,6 +1219,7 @@ EXTRA_DIST = \
        tests/detect-ttl.c \
        tests/source-pcap.c \
        tests/app-layer-htp-file.c \
+       tests/detect-engine-alert.c \
        tests/detect-engine-content-inspection.c \
        tests/detect-icmpv4hdr.c \
        tests/detect-parse.c \
index 834b4835881a69846edba25ca27bead7e49db1f2..1738b0fe5ee3bf162f4ed1d6d0ad255c65047d28 100644 (file)
@@ -417,6 +417,8 @@ void PacketAlertFinalize(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx
             p->flags |= PKT_FIRST_ALERTS;
         }
     }
-
 }
 
+#ifdef UNITTESTS
+#include "tests/detect-engine-alert.c"
+#endif
index bf54d90c91b13f7582033be961b310bcd34d4995..ee940c58699a1d78788bb06590e2f5444f74a386 100644 (file)
@@ -36,5 +36,6 @@ void PacketAlertFinalize(DetectEngineCtx *, DetectEngineThreadCtx *, Packet *);
 int PacketAlertCheck(Packet *, uint32_t);
 void PacketAlertTagInit(void);
 PacketAlert *PacketAlertGetTag(void);
+void DetectEngineAlertRegisterTests(void);
 
 #endif /* __DETECT_ENGINE_ALERT_H__ */
index 817ef180f597e04a48db9c52c9e9fb0fbe424df9..42058329e78b57ee9dffb0180e6d796be9d21093 100644 (file)
@@ -199,6 +199,7 @@ static void RegisterUnittests(void)
     DetectAddressTests();
     DetectProtoTests();
     DetectPortTests();
+    DetectEngineAlertRegisterTests();
     SCAtomicRegisterTests();
     MemrchrRegisterTests();
     AppLayerUnittestsRegister();
diff --git a/src/tests/detect-engine-alert.c b/src/tests/detect-engine-alert.c
new file mode 100644 (file)
index 0000000..86bc8d4
--- /dev/null
@@ -0,0 +1,77 @@
+/* Copyright (C) 2022 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "../suricata-common.h"
+
+#include "../detect.h"
+#include "../detect-engine.h"
+#include "../detect-engine-alert.h"
+#include "../detect-parse.h"
+
+#include "../util-unittest.h"
+#include "../util-unittest-helper.h"
+
+/**
+ * \brief Tests that the reject action is correctly set in Packet->action
+ */
+static int TestDetectAlertPacketApplySignatureActions01(void)
+{
+#ifdef HAVE_LIBNET11
+    uint8_t payload[] = "Hi all!";
+    uint16_t length = sizeof(payload) - 1;
+    Packet *p = UTHBuildPacketReal(
+            (uint8_t *)payload, length, IPPROTO_TCP, "192.168.1.5", "192.168.1.1", 41424, 80);
+    FAIL_IF_NULL(p);
+
+    const char sig[] = "reject tcp any any -> any 80 (content:\"Hi all\"; sid:1; rev:1;)";
+    FAIL_IF(UTHPacketMatchSig(p, sig) == 0);
+    FAIL_IF_NOT(PacketTestAction(p, ACTION_REJECT_ANY));
+
+    UTHFreePackets(&p, 1);
+#endif /* HAVE_LIBNET11 */
+    PASS;
+}
+
+/**
+ * \brief Tests that the packet has the drop action correctly updated in Packet->action
+ */
+static int TestDetectAlertPacketApplySignatureActions02(void)
+{
+    uint8_t payload[] = "Hi all!";
+    uint16_t length = sizeof(payload) - 1;
+    Packet *p = UTHBuildPacketReal(
+            (uint8_t *)payload, length, IPPROTO_TCP, "192.168.1.5", "192.168.1.1", 41424, 80);
+    FAIL_IF_NULL(p);
+
+    const char sig[] = "drop tcp any any -> any any (msg:\"sig 1\"; content:\"Hi all\"; sid:1;)";
+    FAIL_IF(UTHPacketMatchSig(p, sig) == 0);
+    FAIL_IF_NOT(PacketTestAction(p, ACTION_DROP));
+
+    UTHFreePackets(&p, 1);
+    PASS;
+}
+
+/**
+ * \brief Registers Detect Engine Alert unit tests
+ */
+void DetectEngineAlertRegisterTests(void)
+{
+    UtRegisterTest("TestDetectAlertPacketApplySignatureActions01",
+            TestDetectAlertPacketApplySignatureActions01);
+    UtRegisterTest("TestDetectAlertPacketApplySignatureActions02",
+            TestDetectAlertPacketApplySignatureActions02);
+}