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