suricata
detect-tcpmss.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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-parse.h"
22 #include "../detect-engine-prefilter-common.h"
23 
24 #include "../detect-tcpmss.h"
25 
26 #include "../util-unittest.h"
27 
28 /**
29  * \test setting up a valid tcpmss value.
30  */
31 
32 static int DetectTcpmssParseTest01 (void)
33 {
34  DetectTcpmssData *tcpmssd = DetectTcpmssParse("10");
35 
36  FAIL_IF_NULL(tcpmssd);
37  FAIL_IF_NOT(tcpmssd->arg1 == 10);
38  FAIL_IF_NOT(tcpmssd->mode == DETECT_TCPMSS_EQ);
39 
40  DetectTcpmssFree(tcpmssd);
41 
42  PASS;
43 }
44 
45 /**
46  * \test setting up a valid tcpmss value with "<" operator.
47  */
48 
49 static int DetectTcpmssParseTest02 (void)
50 {
51  DetectTcpmssData *tcpmssd = DetectTcpmssParse("<10");
52 
53  FAIL_IF_NULL(tcpmssd);
54  FAIL_IF_NOT(tcpmssd->arg1 == 10);
55  FAIL_IF_NOT(tcpmssd->mode == DETECT_TCPMSS_LT);
56 
57  DetectTcpmssFree(tcpmssd);
58 
59  PASS;
60 }
61 
62 /**
63  * \test setting up an valid tcpmss values with "-" operator.
64  */
65 
66 static int DetectTcpmssParseTest03 (void)
67 {
68  DetectTcpmssData *tcpmssd = DetectTcpmssParse("1-2");
69 
70  FAIL_IF_NULL(tcpmssd);
71  FAIL_IF_NOT(tcpmssd->arg1 == 1);
72  FAIL_IF_NOT(tcpmssd->mode == DETECT_TCPMSS_RA);
73 
74  DetectTcpmssFree(tcpmssd);
75 
76  PASS;
77 }
78 
79 /**
80  * \test setting up an valid tcpmss value with
81  * ">" operator and include spaces arround the given values.
82  */
83 
84 static int DetectTcpmssParseTest04 (void)
85 {
86  DetectTcpmssData *tcpmssd = DetectTcpmssParse(" > 10 ");
87 
88  FAIL_IF_NULL(tcpmssd);
89  FAIL_IF_NOT(tcpmssd->arg1 == 10);
90  FAIL_IF_NOT(tcpmssd->mode == DETECT_TCPMSS_GT);
91 
92  DetectTcpmssFree(tcpmssd);
93 
94  PASS;
95 }
96 
97 /**
98  * \test setting up an valid tcpmss values with
99  * "-" operator and include spaces arround the given values.
100  */
101 
102 static int DetectTcpmssParseTest05 (void)
103 {
104  DetectTcpmssData *tcpmssd = DetectTcpmssParse(" 1 - 2 ");
105 
106  FAIL_IF_NULL(tcpmssd);
107  FAIL_IF_NOT(tcpmssd->arg1 == 1);
108  FAIL_IF_NOT(tcpmssd->arg2 == 2);
109  FAIL_IF_NOT(tcpmssd->mode == DETECT_TCPMSS_RA);
110 
111  DetectTcpmssFree(tcpmssd);
112 
113  PASS;
114 }
115 
116 /**
117  * \test setting up an valid tcpmss values with
118  * invalid "=" operator and include spaces arround the given values.
119  */
120 
121 static int DetectTcpmssParseTest06 (void)
122 {
123  DetectTcpmssData *tcpmssd = DetectTcpmssParse(" 1 = 2 ");
124  FAIL_IF_NOT_NULL(tcpmssd);
125  PASS;
126 }
127 
128 /**
129  * \test setting up valid tcpmss values with
130  * invalid "<>" operator and include spaces arround the given values.
131  */
132 
133 static int DetectTcpmssParseTest07 (void)
134 {
135  DetectTcpmssData *tcpmssd = DetectTcpmssParse(" 1<>2 ");
136  FAIL_IF_NOT_NULL(tcpmssd);
137  PASS;
138 }
139 
140 /**
141  * \brief this function registers unit tests for DetectTcpmss
142  */
144 {
145  UtRegisterTest("DetectTcpmssParseTest01", DetectTcpmssParseTest01);
146  UtRegisterTest("DetectTcpmssParseTest02", DetectTcpmssParseTest02);
147  UtRegisterTest("DetectTcpmssParseTest03", DetectTcpmssParseTest03);
148  UtRegisterTest("DetectTcpmssParseTest04", DetectTcpmssParseTest04);
149  UtRegisterTest("DetectTcpmssParseTest05", DetectTcpmssParseTest05);
150  UtRegisterTest("DetectTcpmssParseTest06", DetectTcpmssParseTest06);
151  UtRegisterTest("DetectTcpmssParseTest07", DetectTcpmssParseTest07);
152 }
#define DETECT_TCPMSS_LT
Definition: detect-tcpmss.h:27
#define DETECT_TCPMSS_GT
Definition: detect-tcpmss.h:29
#define PASS
Pass the test.
#define DETECT_TCPMSS_RA
Definition: detect-tcpmss.h:30
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void DetectTcpmssFree(void *)
this function will free memory associated with DetectTcpmssData
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
void DetectTcpmssRegisterTests(void)
this function registers unit tests for DetectTcpmss
#define DETECT_TCPMSS_EQ
Definition: detect-tcpmss.h:28
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82