suricata
reputation.c
Go to the documentation of this file.
1 /* Copyright (C) 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 /**
19  *
20  * \author Giuseppe Longo <giuseppe@glongo.it>
21  *
22  */
23 
24 #include "conf-yaml-loader.h"
25 #include "detect-engine.h"
26 #include "stream-tcp-private.h"
27 #include "stream-tcp-reassemble.h"
28 #include "stream-tcp.h"
29 #include "util-unittest-helper.h"
30 
31 #define TEST_INIT \
32  DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
33  FAIL_IF(de_ctx == NULL); \
34  \
35  Address a; \
36  uint8_t cat = 0, value = 0;
37 
38 #define TEST_INIT_WITH_PACKET_IPV6(src, dst) \
39  uint8_t *buf = (uint8_t *)"Hi all!"; \
40  uint16_t buflen = strlen((char *)buf); \
41  Packet *p = UTHBuildPacketIPV6SrcDst((uint8_t *)buf, buflen, IPPROTO_TCP, (src), (dst)); \
42  FAIL_IF(p == NULL); \
43  TEST_INIT
44 
45 #define TEST_INIT_WITH_PACKET(ip) \
46  uint8_t *buf = (uint8_t *)"Hi all!"; \
47  uint16_t buflen = strlen((char *)buf); \
48  Packet *p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP); \
49  FAIL_IF(p == NULL); \
50  p->src.addr_data32[0] = UTHSetIPv4Address(ip); \
51  TEST_INIT
52 
53 #define TEST_CLEANUP \
54  DetectEngineCtxFree(de_ctx);
55 
56 #define TEST_CLEANUP_WITH_PACKET \
57  UTHFreePacket(p); \
58  TEST_CLEANUP
59 
60 static int SRepTest01(void)
61 {
62  TEST_INIT;
63 
64  char ipstr[16];
65  char str[] = "1.2.3.4,1,2";
66  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 0);
67  PrintInet(AF_INET, (const void *)&a.address, ipstr, sizeof(ipstr));
68  FAIL_IF(strcmp(ipstr, "1.2.3.4") != 0);
69  FAIL_IF(cat != 1);
70  FAIL_IF(value != 2);
71 
73  PASS;
74 }
75 
76 static int SRepTest02(void)
77 {
78  TEST_INIT;
79 
80  char str[] = "1.1.1.1,";
81  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0);
82 
84  PASS;
85 }
86 
87 static int SRepTest03(void)
88 {
89  char str[] = "1,Shortname,Long Name";
90  uint8_t cat = 0;
91  char shortname[SREP_SHORTNAME_LEN];
92 
93  FAIL_IF(SRepCatSplitLine(str, &cat, shortname, sizeof(shortname)) != 0);
94  FAIL_IF(strcmp(shortname, "Shortname") != 0);
95  FAIL_IF(cat != 1);
96 
97  PASS;
98 }
99 
100 static int SRepTest04(void)
101 {
102  TEST_INIT;
103 
104  char str[] = "10.0.0.0/16,1,2";
105  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
106 
107  TEST_CLEANUP;
108  PASS;
109 }
110 
111 static int SRepTest05(void)
112 {
113  TEST_INIT_WITH_PACKET("10.0.0.1");
114 
115  char str[] = "10.0.0.0/16,1,20";
116  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
117 
118  cat = 1;
119  FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 20);
120 
122  PASS;
123 }
124 
125 static int SRepTest06(void)
126 {
127  TEST_INIT_WITH_PACKET("192.168.0.1");
128 
129  char str1[] = "0.0.0.0/0,1,10\n";
130  char str2[] = "192.168.0.0/16,2,127";
131 
132  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str1, &a, &cat, &value) != 1);
133  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str2, &a, &cat, &value) != 1);
134 
135  cat = 1;
136  FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 10);
137 
139  PASS;
140 }
141 
142 static int SRepTest07(void) {
143  TEST_INIT;
144 
145  char str[] = "2000:0000:0000:0000:0000:0000:0000:0001,";
146  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0);
147 
148  TEST_CLEANUP;
149  PASS;
150 }
151 
152 static int SRepTest08(void)
153 {
154  TEST_INIT_WITH_PACKET_IPV6("2000:0000:0000:0000:0000:0000:0000:0001", "FFFF::1");
155 
156  char str1[] = "0.0.0.0/0,1,10\n";
157  char str2[] = "192.168.0.0/16,2,127\n";
158  char str3[] = "2000::/3,1,10\n";
159  char str4[] = "FFFF::/127,2,127\n";
160  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str1, &a, &cat, &value) != 1);
161  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str2, &a, &cat, &value) != 1);
162  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str3, &a, &cat, &value) != 1);
163  FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str4, &a, &cat, &value) != 1);
164 
165  cat = 1;
166  FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 10);
167 
169  PASS;
170 }
171 
172 /** Register the following unittests for the Reputation module */
174 {
175  UtRegisterTest("SRepTest01", SRepTest01);
176  UtRegisterTest("SRepTest02", SRepTest02);
177  UtRegisterTest("SRepTest03", SRepTest03);
178  UtRegisterTest("SRepTest04", SRepTest04);
179  UtRegisterTest("SRepTest05", SRepTest05);
180  UtRegisterTest("SRepTest06", SRepTest06);
181  UtRegisterTest("SRepTest07", SRepTest07);
182  UtRegisterTest("SRepTest08", SRepTest08);
183 }
SREP_SHORTNAME_LEN
#define SREP_SHORTNAME_LEN
Definition: reputation.c:326
detect-engine.h
stream-tcp.h
TEST_CLEANUP
#define TEST_CLEANUP
Definition: reputation.c:53
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
stream-tcp-reassemble.h
TEST_INIT_WITH_PACKET_IPV6
#define TEST_INIT_WITH_PACKET_IPV6(src, dst)
Definition: reputation.c:38
util-unittest-helper.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineCtx_::srepCIDR_ctx
SRepCIDRTree * srepCIDR_ctx
Definition: detect.h:948
PrintInet
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:231
TEST_INIT
#define TEST_INIT
Definition: reputation.c:31
TEST_INIT_WITH_PACKET
#define TEST_INIT_WITH_PACKET(ip)
Definition: reputation.c:45
conf-yaml-loader.h
stream-tcp-private.h
SRepCIDRGetIPRepSrc
int8_t SRepCIDRGetIPRepSrc(SRepCIDRTree *cidr_ctx, Packet *p, uint8_t cat, uint32_t version)
Definition: reputation.c:135
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
TEST_CLEANUP_WITH_PACKET
#define TEST_CLEANUP_WITH_PACKET
Definition: reputation.c:56
str
#define str(s)
Definition: suricata-common.h:308
SCReputationRegisterTests
void SCReputationRegisterTests(void)
Definition: reputation.c:173