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