suricata
detect-engine-alert.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "../suricata-common.h"
19 
20 #include "../detect.h"
21 #include "../detect-engine.h"
22 #include "../detect-engine-alert.h"
23 #include "../detect-parse.h"
24 
25 #include "../util-unittest.h"
26 #include "../util-unittest-helper.h"
27 
28 /**
29  * \brief Tests that the reject action is correctly set in Packet->action
30  */
31 static int TestDetectAlertPacketApplySignatureActions01(void)
32 {
33 #ifdef HAVE_LIBNET11
34  uint8_t payload[] = "Hi all!";
35  uint16_t length = sizeof(payload) - 1;
37  (uint8_t *)payload, length, IPPROTO_TCP, "192.168.1.5", "192.168.1.1", 41424, 80);
38  FAIL_IF_NULL(p);
39 
40  const char sig[] = "reject tcp any any -> any 80 (content:\"Hi all\"; sid:1; rev:1;)";
41  FAIL_IF(UTHPacketMatchSig(p, sig) == 0);
42  FAIL_IF_NOT(PacketTestAction(p, ACTION_REJECT_ANY));
43 
44  UTHFreePackets(&p, 1);
45 #endif /* HAVE_LIBNET11 */
46  PASS;
47 }
48 
49 /**
50  * \brief Tests that the packet has the drop action correctly updated in Packet->action
51  */
52 static int TestDetectAlertPacketApplySignatureActions02(void)
53 {
54  uint8_t payload[] = "Hi all!";
55  uint16_t length = sizeof(payload) - 1;
57  (uint8_t *)payload, length, IPPROTO_TCP, "192.168.1.5", "192.168.1.1", 41424, 80);
58  FAIL_IF_NULL(p);
59 
60  const char sig[] = "drop tcp any any -> any any (msg:\"sig 1\"; content:\"Hi all\"; sid:1;)";
61  FAIL_IF(UTHPacketMatchSig(p, sig) == 0);
62  FAIL_IF_NOT(PacketTestAction(p, ACTION_DROP));
63 
64  UTHFreePackets(&p, 1);
65  PASS;
66 }
67 
68 /**
69  * \brief Registers Detect Engine Alert unit tests
70  */
72 {
73  UtRegisterTest("TestDetectAlertPacketApplySignatureActions01",
74  TestDetectAlertPacketApplySignatureActions01);
75  UtRegisterTest("TestDetectAlertPacketApplySignatureActions02",
76  TestDetectAlertPacketApplySignatureActions02);
77 }
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
ACTION_REJECT_ANY
#define ACTION_REJECT_ANY
Definition: action-globals.h:37
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:804
UTHBuildPacketReal
Packet * UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst, uint16_t sport, uint16_t dport)
UTHBuildPacketReal is a function that create tcp/udp packets for unittests specifying ip and port sou...
Definition: util-unittest-helper.c:260
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
Packet_
Definition: decode.h:476
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
DetectEngineAlertRegisterTests
void DetectEngineAlertRegisterTests(void)
Registers Detect Engine Alert unit tests.
Definition: detect-engine-alert.c:71
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:450