-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
221 lines (185 loc) · 5.67 KB
/
test.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <cstdlib>
extern "C" {
#include <ndpi/ndpi_typedefs.h>
#include <pcap.h>
#include <ndff.h>
}
#ifndef PCAP_ERRBUF_SIZE
#define PCAP_ERRBUF_SIZE 256
#endif
namespace {
class PcapFile
{
public:
PcapFile(const char *filename)
{
char errbuf[PCAP_ERRBUF_SIZE];
m_handle = pcap_open_offline(filename, errbuf);
}
const u_char *next(struct pcap_pkthdr &header)
{
return pcap_next(m_handle, &header);
}
~PcapFile()
{
pcap_close(m_handle);
}
private:
pcap_t *m_handle;
};
TEST(NdffTest, DetectVlanID) {
const u_char *packet;
struct pcap_pkthdr header;
u_int16_t type, vlan_id, offset;
char *errmsg = NULL;
PcapFile pcap("./http_over_vlan.pcap");
while (packet = pcap.next(header))
{
offset = ndff_detect_type(&header, DLT_EN10MB, 0, packet, &type, &vlan_id, &errmsg);
EXPECT_EQ(ETH_PROTO_IPv4, type);
EXPECT_EQ(18, offset);
}
}
TEST(NdffTest, CorrectlyHandleIPv4Header) {
const u_char *packet;
struct pcap_pkthdr header;
u_int16_t type, vlan_id, offset;
char *errmsg = NULL;
struct ndpi_iphdr *ipv4;
struct ndpi_ipv6hdr *ipv6;
u_int8_t proto;
union {
u_int32_t u32;
u_int8_t u8[4];
} ipaddr;
PcapFile pcap("./google_ssl.pcap");
packet = pcap.next(header);
offset = ndff_detect_type(&header, DLT_EN10MB, 0, packet, &type, &vlan_id, &errmsg);
offset = ndff_set_iphdr(&header, type, packet, offset, &ipv4, &ipv6, &proto);
EXPECT_EQ(IPPROTO_TCP, proto);
EXPECT_FALSE(ipv4 == NULL);
EXPECT_EQ(NULL, ipv6);
ipaddr.u32 = ipv4->saddr;
EXPECT_EQ(172,ipaddr.u8[0]);
EXPECT_EQ(31,ipaddr.u8[1]);
EXPECT_EQ(3,ipaddr.u8[2]);
EXPECT_EQ(224,ipaddr.u8[3]);
ipaddr.u32 = ipv4->daddr;
EXPECT_EQ(216,ipaddr.u8[0]);
EXPECT_EQ(58,ipaddr.u8[1]);
EXPECT_EQ(212,ipaddr.u8[2]);
EXPECT_EQ(100,ipaddr.u8[3]);
}
TEST(NdffTest, CorrectlySetTCPHeader) {
const u_char *packet;
struct pcap_pkthdr header;
u_int16_t type, vlan_id, offset;
char *errmsg = NULL;
struct ndpi_iphdr *ipv4;
struct ndpi_ipv6hdr *ipv6;
u_int8_t proto;
union {
u_int32_t u32;
u_int8_t u8[4];
} ipaddr;
struct ndpi_tcphdr *tcph;
struct ndpi_udphdr *udph;
u_int16_t payload_len;
u_int8_t *l4_payload;
PcapFile pcap("./google_ssl.pcap");
packet = pcap.next(header);
offset = ndff_detect_type(&header, DLT_EN10MB, 0, packet, &type, &vlan_id, &errmsg);
offset = ndff_set_iphdr(&header, type, packet, offset, &ipv4, &ipv6, &proto);
offset = ndff_set_l4hdr(&header, packet, offset, ipv4, ipv6, proto, &tcph, &udph, &l4_payload, &payload_len);
EXPECT_EQ(42835, ntohs(tcph->source));
EXPECT_EQ(443, ntohs(tcph->dest));
}
TEST(NdffTest, CorrectlySetUDPHeader) {
const u_char *packet;
struct pcap_pkthdr header;
u_int16_t type, vlan_id, offset;
char *errmsg = NULL;
struct ndpi_iphdr *ipv4;
struct ndpi_ipv6hdr *ipv6;
u_int8_t proto;
union {
u_int32_t u32;
u_int8_t u8[4];
} ipaddr;
struct ndpi_tcphdr *tcph;
struct ndpi_udphdr *udph;
u_int16_t payload_len;
u_int8_t *l4_payload;
PcapFile pcap("./quic.pcap");
packet = pcap.next(header);
offset = ndff_detect_type(&header, DLT_EN10MB, 0, packet, &type, &vlan_id, &errmsg);
offset = ndff_set_iphdr(&header, type, packet, offset, &ipv4, &ipv6, &proto);
offset = ndff_set_l4hdr(&header, packet, offset, ipv4, ipv6, proto, &tcph, &udph, &l4_payload, &payload_len);
EXPECT_EQ(57833, ntohs(udph->source));
EXPECT_EQ(443, ntohs(udph->dest));
}
TEST(NdffTest, DetectType) {
const u_char *packet;
struct pcap_pkthdr header;
u_int16_t type, vlan_id, offset;
char *errmsg = NULL;
PcapFile pcap("./google_ssl.pcap");
while (packet = pcap.next(header))
{
offset = ndff_detect_type(&header, DLT_EN10MB, 0, packet, &type, &vlan_id, &errmsg);
EXPECT_EQ(ETH_PROTO_IPv4, type);
EXPECT_EQ(14, offset);
}
}
TEST(NdffTest, GetFlowTest) {
const u_char *packet;
struct pcap_pkthdr header;
u_int16_t type, vlan_id, offset;
char *errmsg = NULL;
struct ndpi_iphdr *ipv4;
struct ndpi_ipv6hdr *ipv6;
u_int8_t proto;
union {
u_int32_t u32;
u_int8_t u8[4];
} ipaddr;
struct ndpi_tcphdr *tcph;
struct ndpi_udphdr *udph;
u_int16_t payload_len;
u_int8_t *l4_payload;
void **trees;
trees = (void**) calloc(1, sizeof(void *));
struct ndpi_id_struct *src, *dst;
struct ndff_flow *flow;
src = dst = NULL;
PcapFile pcap("./google_ssl.pcap");
// 172.31.3.224
u_int32_t src_ip = (224 << 24) | (3<< 16) | (31 << 8) | 172;
// 216.58.212.100
u_int32_t dst_ip = (100 << 24) | (212<< 16) | (58 << 8) | 216;
int cnt = 0;
while (packet = pcap.next(header))
{
offset = ndff_detect_type(&header, DLT_EN10MB, 0, packet, &type, &vlan_id, &errmsg);
offset = ndff_set_iphdr(&header, type, packet, offset, &ipv4, &ipv6, &proto);
offset = ndff_set_l4hdr(&header, packet, offset, ipv4, ipv6, proto, &tcph, &udph, &l4_payload, &payload_len);
flow = ndff_get_flow_info(trees,1,vlan_id, header.caplen, &src, &dst, ipv4, ipv6, tcph, udph);
EXPECT_EQ(0, flow->flow_id);
if (cnt++ == 4) break;
}
EXPECT_EQ(2, flow->in_packets);
EXPECT_EQ(3, flow->out_packets);
EXPECT_EQ(120, flow->in_bytes);
EXPECT_EQ(292, flow->out_bytes);
EXPECT_EQ(src_ip, flow->src_ip);
EXPECT_EQ(dst_ip, flow->dst_ip);
}
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}