summaryrefslogtreecommitdiffstats
path: root/src/tests/detect-ssl-state.c
blob: 6be8ea8d89b93c25aef154801d85c28837c8fccf (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (C) 2007-2019 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.
 */

/**
 * \file
 *
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
 *
 */

#include "detect-engine-build.h"

static int DetectSslStateTest01(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("client_hello");
    FAIL_IF_NULL(ssd);
    FAIL_IF_NOT(ssd->flags == DETECT_SSL_STATE_CLIENT_HELLO);
    SCFree(ssd);
    PASS;
}

static int DetectSslStateTest02(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("server_hello , client_hello");
    FAIL_IF_NULL(ssd);
    FAIL_IF_NOT(ssd->flags == (DETECT_SSL_STATE_SERVER_HELLO |
            DETECT_SSL_STATE_CLIENT_HELLO));
    SCFree(ssd);
    PASS;
}

static int DetectSslStateTest03(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("server_hello , client_keyx , "
                                                  "client_hello");
    FAIL_IF_NULL(ssd);
    FAIL_IF_NOT(ssd->flags == (DETECT_SSL_STATE_SERVER_HELLO |
                       DETECT_SSL_STATE_CLIENT_KEYX |
                       DETECT_SSL_STATE_CLIENT_HELLO));
    SCFree(ssd);
    PASS;
}

static int DetectSslStateTest04(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("server_hello , client_keyx , "
                                                  "client_hello , server_keyx , "
                                                  "unknown");
    FAIL_IF_NULL(ssd);
    FAIL_IF_NOT(ssd->flags == (DETECT_SSL_STATE_SERVER_HELLO |
                       DETECT_SSL_STATE_CLIENT_KEYX |
                       DETECT_SSL_STATE_CLIENT_HELLO |
                       DETECT_SSL_STATE_SERVER_KEYX |
                       DETECT_SSL_STATE_UNKNOWN));
    SCFree(ssd);
    PASS;
}

static int DetectSslStateTest05(void)
{
    DetectSslStateData *ssd = DetectSslStateParse(", server_hello , client_keyx , "
                                                  "client_hello , server_keyx , "
                                                  "unknown");

    FAIL_IF_NOT_NULL(ssd);
    PASS;
}

static int DetectSslStateTest06(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("server_hello , client_keyx , "
                                                  "client_hello , server_keyx , "
                                                  "unknown , ");
    FAIL_IF_NOT_NULL(ssd);
    PASS;
}

/**
 * \brief Test that the "|" character still works as a separate for
 * compatibility with older Suricata rules.
 */
static int DetectSslStateTest08(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("server_hello|client_hello");
    FAIL_IF_NULL(ssd);
    FAIL_IF_NOT(ssd->flags == (DETECT_SSL_STATE_SERVER_HELLO |
            DETECT_SSL_STATE_CLIENT_HELLO));
    SCFree(ssd);
    PASS;
}

/**
 * \test Test parsing of negated states.
 */
static int DetectSslStateTestParseNegate(void)
{
    DetectSslStateData *ssd = DetectSslStateParse("!client_hello");
    FAIL_IF_NULL(ssd);
    uint32_t expected = DETECT_SSL_STATE_CLIENT_HELLO;
    FAIL_IF(ssd->flags != expected || ssd->mask != expected);
    SCFree(ssd);

    ssd = DetectSslStateParse("!client_hello,!server_hello");
    FAIL_IF_NULL(ssd);
    expected = DETECT_SSL_STATE_CLIENT_HELLO | DETECT_SSL_STATE_SERVER_HELLO;
    FAIL_IF(ssd->flags != expected || ssd->mask != expected);
    SCFree(ssd);

    PASS;
}

static void DetectSslStateRegisterTests(void)
{
    UtRegisterTest("DetectSslStateTest01", DetectSslStateTest01);
    UtRegisterTest("DetectSslStateTest02", DetectSslStateTest02);
    UtRegisterTest("DetectSslStateTest03", DetectSslStateTest03);
    UtRegisterTest("DetectSslStateTest04", DetectSslStateTest04);
    UtRegisterTest("DetectSslStateTest05", DetectSslStateTest05);
    UtRegisterTest("DetectSslStateTest06", DetectSslStateTest06);
    UtRegisterTest("DetectSslStateTest08", DetectSslStateTest08);
    UtRegisterTest("DetectSslStateTestParseNegate",
        DetectSslStateTestParseNegate);
}