230 lines
10 KiB
C++
230 lines
10 KiB
C++
#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;
|
|
}
|