添加gtest测试用例.
This commit is contained in:
@@ -1,2 +1,9 @@
|
||||
all: test_jump_layer gtest_jump_layer
|
||||
|
||||
CFLAGS = -g -Wall -fPIC -std=c++11 -DSAPP_V4=1 -D_XOPEN_SOURCE
|
||||
|
||||
test_jump_layer:test_jump_layer.c
|
||||
gcc -g -o $@ test_jump_layer.c -D_GNU_SOURCE -L/opt/MESA/lib -lMESA_jump_layer -I ../inc -I/opt/MESA/include/MESA -l pcap
|
||||
|
||||
gtest_jump_layer:gtest_jump_layer.cpp
|
||||
g++ -g -o $@ $^ $(CFLAGS) -D_GNU_SOURCE -L/opt/MESA/lib -lMESA_jump_layer -I ../inc -I/opt/MESA/include/MESA -lpcap -lgtest -lpthread
|
||||
|
||||
1185
test/gtest_jump_layer.cpp
Normal file
1185
test/gtest_jump_layer.cpp
Normal file
File diff suppressed because it is too large
Load Diff
BIN
test/sample_pcap/tcp_simple.pcap
Normal file
BIN
test/sample_pcap/tcp_simple.pcap
Normal file
Binary file not shown.
@@ -11,6 +11,9 @@ static char *g_input_pcap_name;
|
||||
static char *g_input_bpf_string;
|
||||
static struct bpf_program g_bpf_filter;
|
||||
|
||||
static int g_raw_input_layer_type = (int )ADDR_TYPE_MAC;
|
||||
static int g_expect_layer_type = (int )ADDR_TYPE_IPV4;
|
||||
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
printf("Usage:\n");
|
||||
@@ -59,39 +62,109 @@ static int pcap_init(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _pcap_pkt_handle(u_char *user, const struct pcap_pkthdr *hdr, const u_char *data)
|
||||
static void _jump_from_udp(const void *uhdr)
|
||||
{
|
||||
const void *next_hdr;
|
||||
|
||||
next_hdr = MESA_jump_layer(uhdr, ADDR_TYPE_UDP, ADDR_TYPE_GPRS_TUNNEL);
|
||||
if(next_hdr){
|
||||
printf("bingo! jump to gtp from udp succ!\n");
|
||||
}
|
||||
|
||||
next_hdr = MESA_jump_layer(uhdr, ADDR_TYPE_UDP, ADDR_TYPE_L2TP);
|
||||
if(next_hdr){
|
||||
printf("bingo! jump to l2tp from udp succ!\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void _jump_from_ipv4(const void *ip4_hdr)
|
||||
{
|
||||
const void *next_hdr;
|
||||
|
||||
next_hdr = MESA_jump_layer(ip4_hdr, ADDR_TYPE_IPV4, ADDR_TYPE_TCP);
|
||||
if(next_hdr){
|
||||
printf("bingo! jump to tcp from ipv4 succ!\n");
|
||||
}
|
||||
|
||||
next_hdr = MESA_jump_layer(ip4_hdr, ADDR_TYPE_IPV4, ADDR_TYPE_UDP);
|
||||
if(next_hdr){
|
||||
printf("bingo! jump to udp from ipv4 succ!\n");
|
||||
_jump_from_udp(next_hdr);
|
||||
}
|
||||
}
|
||||
|
||||
static void _jump_from_ipv6(const void *ip4_hdr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void _jump_from_ethernet(const void *ehdr)
|
||||
{
|
||||
const void *next_hdr;
|
||||
|
||||
next_hdr = MESA_jump_layer(ehdr, ADDR_TYPE_MAC, ADDR_TYPE_IPV4);
|
||||
if(next_hdr){
|
||||
printf("bingo! jump to ipv4 from ethernet succ!\n");
|
||||
_jump_from_ipv4(next_hdr);
|
||||
}
|
||||
|
||||
next_hdr = MESA_jump_layer(ehdr, ADDR_TYPE_MAC, ADDR_TYPE_IPV6);
|
||||
if(next_hdr){
|
||||
printf("bingo! jump to ipv6 from ethernet succ!\n");
|
||||
_jump_from_ipv6(next_hdr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void _jump_greedy(const struct pcap_pkthdr *hdr, const u_char *data)
|
||||
{
|
||||
const void *ip4_hdr, *ip6_h;
|
||||
|
||||
char print_buf[128];
|
||||
int offset_to_eth;
|
||||
static int pkt_index = 0;
|
||||
|
||||
ip4_hdr = MESA_jump_layer_greedy(data, ADDR_TYPE_MAC, ADDR_TYPE_IPV4);
|
||||
ip6_h = MESA_jump_layer_greedy(data, ADDR_TYPE_MAC, ADDR_TYPE_IPV6);
|
||||
|
||||
|
||||
printf("-----------------------------packet index:%d------------------------------------------\n", pkt_index++);
|
||||
if(ip4_hdr){
|
||||
offset_to_eth = (u_char *)ip4_hdr-data;
|
||||
if(g_input_bpf_string
|
||||
&& (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip4_hdr, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){
|
||||
goto done;
|
||||
printf("-----------------------------packet index:%d------------------------------------------\n", pkt_index++);
|
||||
if(ip4_hdr){
|
||||
offset_to_eth = (u_char *)ip4_hdr-data;
|
||||
if(g_input_bpf_string
|
||||
&& (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip4_hdr, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){
|
||||
goto done;
|
||||
}
|
||||
printf("Innermost layer ipv4 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv4_ntop((struct ip *)ip4_hdr, print_buf, sizeof(print_buf)));
|
||||
}
|
||||
printf("Innermost layer ipv4 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv4_ntop((struct ip *)ip4_hdr, print_buf, sizeof(print_buf)));
|
||||
}
|
||||
|
||||
if(ip6_h){
|
||||
offset_to_eth = (u_char *)ip6_h-data;
|
||||
if(g_input_bpf_string
|
||||
&& (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip6_h, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){
|
||||
goto done;
|
||||
|
||||
if(ip6_h){
|
||||
offset_to_eth = (u_char *)ip6_h-data;
|
||||
if(g_input_bpf_string
|
||||
&& (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip6_h, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){
|
||||
goto done;
|
||||
}
|
||||
|
||||
printf("Innermost layer ipv6 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv6_ntop((struct ip6_hdr *)ip6_h, print_buf, sizeof(print_buf)));
|
||||
}
|
||||
|
||||
done:
|
||||
|
||||
printf("--------------------------------------------------------------------------------------\n\n");
|
||||
|
||||
printf("Innermost layer ipv6 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv6_ntop((struct ip6_hdr *)ip6_h, print_buf, sizeof(print_buf)));
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
static void _pcap_pkt_handle(u_char *user, const struct pcap_pkthdr *hdr, const u_char *data)
|
||||
{
|
||||
|
||||
_jump_from_ethernet(data);
|
||||
|
||||
_jump_greedy(hdr, data);
|
||||
|
||||
printf("--------------------------------------------------------------------------------------\n\n");
|
||||
}
|
||||
|
||||
static void pcap_run(void)
|
||||
|
||||
Reference in New Issue
Block a user