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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/* Copyright (C) 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.
*/
/**
*
* \author Giuseppe Longo <giuseppe@glongo.it>
*
*/
#include "conf-yaml-loader.h"
#include "detect-engine.h"
#include "stream-tcp-private.h"
#include "stream-tcp-reassemble.h"
#include "stream-tcp.h"
#include "util-unittest-helper.h"
#define TEST_INIT \
DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
FAIL_IF(de_ctx == NULL); \
SRepInit(de_ctx); \
\
Address a; \
uint8_t cat = 0, value = 0;
#define TEST_INIT_WITH_PACKET_IPV6(src, dst) \
uint8_t *buf = (uint8_t *)"Hi all!"; \
uint16_t buflen = strlen((char *)buf); \
Packet *p = UTHBuildPacketIPV6SrcDst((uint8_t *)buf, buflen, IPPROTO_TCP, (src), (dst)); \
FAIL_IF(p == NULL); \
TEST_INIT
#define TEST_INIT_WITH_PACKET(ip) \
uint8_t *buf = (uint8_t *)"Hi all!"; \
uint16_t buflen = strlen((char *)buf); \
Packet *p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP); \
FAIL_IF(p == NULL); \
p->src.addr_data32[0] = UTHSetIPv4Address(ip); \
TEST_INIT
#define TEST_CLEANUP \
DetectEngineCtxFree(de_ctx);
#define TEST_CLEANUP_WITH_PACKET \
UTHFreePacket(p); \
TEST_CLEANUP
static int SRepTest01(void)
{
TEST_INIT;
char ipstr[16];
char str[] = "1.2.3.4,1,2";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 0);
PrintInet(AF_INET, (const void *)&a.address, ipstr, sizeof(ipstr));
FAIL_IF(strcmp(ipstr, "1.2.3.4") != 0);
FAIL_IF(cat != 1);
FAIL_IF(value != 2);
TEST_CLEANUP;
PASS;
}
static int SRepTest02(void)
{
TEST_INIT;
char str[] = "1.1.1.1,";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0);
TEST_CLEANUP;
PASS;
}
static int SRepTest03(void)
{
char str[] = "1,Shortname,Long Name";
uint8_t cat = 0;
char shortname[SREP_SHORTNAME_LEN];
FAIL_IF(SRepCatSplitLine(str, &cat, shortname, sizeof(shortname)) != 0);
FAIL_IF(strcmp(shortname, "Shortname") != 0);
FAIL_IF(cat != 1);
PASS;
}
static int SRepTest04(void)
{
TEST_INIT;
char str[] = "10.0.0.0/16,1,2";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
TEST_CLEANUP;
PASS;
}
static int SRepTest05(void)
{
TEST_INIT_WITH_PACKET("10.0.0.1");
char str[] = "10.0.0.0/16,1,20";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
cat = 1;
FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 20);
TEST_CLEANUP_WITH_PACKET;
PASS;
}
static int SRepTest06(void)
{
TEST_INIT_WITH_PACKET("192.168.0.1");
char str1[] = "0.0.0.0/0,1,10\n";
char str2[] = "192.168.0.0/16,2,127";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str1, &a, &cat, &value) != 1);
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str2, &a, &cat, &value) != 1);
cat = 1;
FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 10);
TEST_CLEANUP_WITH_PACKET;
PASS;
}
static int SRepTest07(void) {
TEST_INIT;
char str[] = "2000:0000:0000:0000:0000:0000:0000:0001,";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0);
TEST_CLEANUP;
PASS;
}
static int SRepTest08(void)
{
TEST_INIT_WITH_PACKET_IPV6("2000:0000:0000:0000:0000:0000:0000:0001", "FFFF::1");
char str1[] = "0.0.0.0/0,1,10\n";
char str2[] = "192.168.0.0/16,2,127\n";
char str3[] = "2000::/3,1,10\n";
char str4[] = "FFFF::/127,2,127\n";
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str1, &a, &cat, &value) != 1);
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str2, &a, &cat, &value) != 1);
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str3, &a, &cat, &value) != 1);
FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str4, &a, &cat, &value) != 1);
cat = 1;
FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 10);
TEST_CLEANUP_WITH_PACKET;
PASS;
}
/** Register the following unittests for the Reputation module */
void SCReputationRegisterTests(void)
{
UtRegisterTest("SRepTest01", SRepTest01);
UtRegisterTest("SRepTest02", SRepTest02);
UtRegisterTest("SRepTest03", SRepTest03);
UtRegisterTest("SRepTest04", SRepTest04);
UtRegisterTest("SRepTest05", SRepTest05);
UtRegisterTest("SRepTest06", SRepTest06);
UtRegisterTest("SRepTest07", SRepTest07);
UtRegisterTest("SRepTest08", SRepTest08);
}
|