summaryrefslogtreecommitdiff
path: root/test/monitor/gtest_packet_dump_unit.cpp
blob: 285294c0d34101e6f9216f3348eb8f01c0434c05 (plain)
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
222
223
224
225
226
227
228
229
#include <unistd.h>
#include <stddef.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <gtest/gtest.h>
#include <pcap/pcap.h>

#ifdef __cplusplus
extern "C"
{
#endif

#include "stellar/packet.h"
#include "monitor/monitor_private.h"
#include "sds/sds.h"
#include "monitor/monitor_packet_dump.h"
#ifdef __cplusplus
}
#endif

#define TEST_PKT_DUMP_FILE_NAME "__gtest_stm_pkt_dump.pcap"

/* mock */
int packet_manager_subscribe(UNUSED struct packet_manager *pkt_mgr, UNUSED enum packet_stage stage, UNUSED on_packet_stage_callback *cb, UNUSED void *args)
{
    assert(0);
    return 0;
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "greedy", "threads", "1,2,3", "datalinkip", "1.2.3.4", "datalinkport", "54321", "bpf", "tcp", "port", "80"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_EQ(NULL, reply);
    EXPECT_EQ(1, tmp_conn_parm.greedy_mode);
    EXPECT_EQ(0, thread_enable_flag[0]);
    EXPECT_EQ(1, thread_enable_flag[1]);
    EXPECT_EQ(1, thread_enable_flag[2]);
    EXPECT_EQ(1, thread_enable_flag[3]);
    EXPECT_EQ(0, thread_enable_flag[5]);
    EXPECT_EQ(0, thread_enable_flag[7]);
    EXPECT_EQ(0, thread_enable_flag[9]);
    EXPECT_EQ(0x04030201, tmp_conn_parm.peer_data_ip_net_order);
    EXPECT_EQ(htons(54321), tmp_conn_parm.peer_data_port_net_order);
    EXPECT_STREQ(cli_bpf_str, "tcp port 80");
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args_out_of_order)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "datalinkip", "1.2.3.4", "datalinkport", "54321", "bpf", "tcp", "port", "80", "greedy", "threads", "1,3,5,7,9"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_EQ(NULL, reply);
    EXPECT_EQ(1, tmp_conn_parm.greedy_mode);
    EXPECT_EQ(0, thread_enable_flag[0]);
    EXPECT_EQ(1, thread_enable_flag[1]);
    EXPECT_EQ(0, thread_enable_flag[2]);
    EXPECT_EQ(1, thread_enable_flag[3]);
    EXPECT_EQ(1, thread_enable_flag[5]);
    EXPECT_EQ(1, thread_enable_flag[7]);
    EXPECT_EQ(1, thread_enable_flag[9]);
    EXPECT_EQ(0, thread_enable_flag[10]);
    EXPECT_EQ(0x04030201, tmp_conn_parm.peer_data_ip_net_order);
    EXPECT_EQ(htons(54321), tmp_conn_parm.peer_data_port_net_order);
    EXPECT_STREQ(cli_bpf_str, "tcp port 80 ");
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args_error_ip)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "datalinkip", "1.2.3.256", "datalinkport", "54321", "bpf", "tcp", "port", "80", "greedy", "threads", "1,3,5,7,9"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str = NULL;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_TRUE(reply != NULL);
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
    monitor_reply_free(reply);
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args_error_port)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "datalinkip", "1.2.3.4", "datalinkport", "654321", "bpf", "tcp", "port", "80", "greedy", "threads", "1,3,5,7,9"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str = NULL;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_TRUE(reply != NULL);
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
    monitor_reply_free(reply);
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args_no_port)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "datalinkip", "1.2.3.4", "bpf", "tcp", "port", "80", "greedy", "threads", "1,3,5,7,9"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str = NULL;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_TRUE(reply != NULL);
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
    monitor_reply_free(reply);
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args_error_bpf)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "datalinkip", "1.2.3.4", "datalinkport", "654321", "bpf", "tcp", "port", "xxx", "greedy", "threads", "1,3,5,7,9"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str = NULL;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_TRUE(reply != NULL);
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
    monitor_reply_free(reply);
}

TEST(MONITOR_PKT_DUMP_UNIT, parse_args_error_threads)
{
    int thread_enable_flag[STELLAR_MAX_THREAD_NUM] = {};
    struct stm_pktdump_task tmp_conn_parm = {};
    const char *argv[] = {"./stellar_dump", "datalinkip", "1.2.3.4", "datalinkport", "54321", "bpf", "tcp", "port", "xxx", "greedy", "threads", "1,3,5,7,999"};
    int argc = sizeof(argv) / sizeof(char *);
    sds cli_bpf_str = NULL;
    struct monitor_reply *reply = stm_packet_dump_parse_args(argc, (char **)argv, &tmp_conn_parm, thread_enable_flag, STELLAR_MAX_THREAD_NUM, &cli_bpf_str);
    ASSERT_TRUE(reply != NULL);
    pcap_freecode(&tmp_conn_parm.bpf_filter);
    sdsfree(cli_bpf_str);
    monitor_reply_free(reply);
}

TEST(MONITOR_PKT_DUMP_UNIT, pcap_compile_and_filter_ipv4)
{
    /* This is a IPv4 tcp SYN packet, 192.168.40.139:48662 -> 107.155.25.121:80 */
    static const unsigned char packet_bytes[] = {
        0x48, 0x73, 0x97, 0x96, 0x38, 0x10, 0x00, 0x22,
        0x46, 0x2f, 0x35, 0xb4, 0x08, 0x00,
        0x45, 0x00, 0x00, 0x38, 0x0c, 0x1d, 0x40, 0x00, 0x40, 0x06,
        0xc0, 0x5b, 0xc0, 0xa8, 0x28, 0x8b, 0x6b, 0x9b,
        0x19, 0x79, 0xbe, 0x16, 0x00, 0x50, 0x7b, 0xf9,
        0x8b, 0x34, 0x00, 0x00, 0x00, 0x00, 0x90, 0x02,
        0x72, 0x10, 0x6e, 0x72, 0x00, 0x00, 0x02, 0x04,
        0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0x2d, 0xbb,
        0x87, 0x29, 0x00, 0x00, 0x00, 0x00};

    struct bpf_program bpf_bin;
    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_EN10MB, &bpf_bin, "tcp port 48662 and host 192.168.40.139 and host 107.155.25.121", 1, 0);
    int match_from_eth = bpf_filter(bpf_bin.bf_insns, packet_bytes, sizeof(packet_bytes), sizeof(packet_bytes));
    EXPECT_TRUE(match_from_eth != 0);
    pcap_freecode(&bpf_bin);

    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_EN10MB, &bpf_bin, "tcp port 12345 and tcp port 54321", 1, 0);
    int unmatch_from_eth = bpf_filter(bpf_bin.bf_insns, packet_bytes, sizeof(packet_bytes), sizeof(packet_bytes));
    EXPECT_TRUE(unmatch_from_eth == 0);
    pcap_freecode(&bpf_bin);

    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_RAW, &bpf_bin, "tcp port 48662 and host 192.168.40.139 and host 107.155.25.121", 1, 0);
    int match_from_ip = bpf_filter(bpf_bin.bf_insns, packet_bytes + 14, sizeof(packet_bytes) - 14, sizeof(packet_bytes) - 14);
    EXPECT_TRUE(match_from_ip != 0);
    pcap_freecode(&bpf_bin);

    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_RAW, &bpf_bin, "host 1.2.3.4 and host 5.6.7.8", 1, 0);
    int unmatch_from_ip = bpf_filter(bpf_bin.bf_insns, packet_bytes + 14, sizeof(packet_bytes) - 14, sizeof(packet_bytes) - 14);
    EXPECT_TRUE(unmatch_from_ip == 0);
    pcap_freecode(&bpf_bin);
}

TEST(MONITOR_PKT_DUMP_UNIT, pcap_compile_and_filter_ipv6)
{
    /* This is a IPv6 tcp SYN packet, 2001::192.168.40.134:37948 -> 2001::192.168.40.133:22 */
    static const unsigned char packet_bytes[] = {
        0x00, 0x22, 0x46, 0x36, 0x51, 0x38, 0x00, 0x22,
        0x46, 0x36, 0x51, 0x3c, 0x86, 0xdd, 0x60, 0x00,
        0x00, 0x00, 0x00, 0x28, 0x06, 0x40, 0x20, 0x01,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92,
        0x01, 0x68, 0x00, 0x40, 0x01, 0x34, 0x20, 0x01,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92,
        0x01, 0x68, 0x00, 0x40, 0x01, 0x33, 0x94, 0x3c,
        0x00, 0x16, 0x09, 0x68, 0x7d, 0x85, 0x00, 0x00,
        0x00, 0x00, 0xa0, 0x02, 0x71, 0xac, 0xff, 0xec,
        0x00, 0x00, 0x02, 0x04, 0x07, 0x94, 0x04, 0x02,
        0x08, 0x0a, 0x16, 0xa8, 0x59, 0xc2, 0x00, 0x00,
        0x00, 0x00, 0x01, 0x03, 0x03, 0x07};

    struct bpf_program bpf_bin;
    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_EN10MB, &bpf_bin, "tcp port 37948 and host 2001::192.168:40:134 and host 2001::192:168:40:133", 1, 0);
    int match_from_eth = bpf_filter(bpf_bin.bf_insns, packet_bytes, sizeof(packet_bytes), sizeof(packet_bytes));
    EXPECT_TRUE(match_from_eth != 0);
    pcap_freecode(&bpf_bin);

    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_EN10MB, &bpf_bin, "tcp port 12345 and tcp port 54321", 1, 0);
    int unmatch_from_eth = bpf_filter(bpf_bin.bf_insns, packet_bytes, sizeof(packet_bytes), sizeof(packet_bytes));
    EXPECT_TRUE(unmatch_from_eth == 0);
    pcap_freecode(&bpf_bin);

    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_RAW, &bpf_bin, "tcp port 37948 and host 2001::192.168:40:134 and host 2001::192:168:40:133", 1, 0);
    int match_from_ip = bpf_filter(bpf_bin.bf_insns, packet_bytes + 14, sizeof(packet_bytes) - 14, sizeof(packet_bytes) - 14);
    EXPECT_TRUE(match_from_ip != 0);
    pcap_freecode(&bpf_bin);

    pcap_compile_nopcap(STM_PACKET_DUMP_CAP_LEN, DLT_RAW, &bpf_bin, "host 1.2.3.4 and host 5.6.7.8", 1, 0);
    int unmatch_from_ip = bpf_filter(bpf_bin.bf_insns, packet_bytes + 14, sizeof(packet_bytes) - 14, sizeof(packet_bytes) - 14);
    EXPECT_TRUE(unmatch_from_ip == 0);
    pcap_freecode(&bpf_bin);
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}