summaryrefslogtreecommitdiffstats
path: root/src/tests/detect-engine-alert.c
blob: 86bc8d4a28585a796807845e76263eb48a7f08dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
}