add IPv4 & IPv6 frag reassemble test case
This commit is contained in:
@@ -1,17 +1,4 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "timestamp.h"
|
||||
#include "tcp_utils.h"
|
||||
#include "ipv4_utils.h"
|
||||
#include "packet_helpers.h"
|
||||
#include "ip_reassemble.h"
|
||||
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1000,
|
||||
.bucket_entries = 16,
|
||||
.bucket_num = 8,
|
||||
};
|
||||
#include "gtest_utils.h"
|
||||
|
||||
/*
|
||||
* Frame 4: 60 bytes on wire (480 bits), 60 bytes captured (480 bits)
|
||||
@@ -204,30 +191,55 @@ unsigned char expect[] = {
|
||||
0xf4, 0xa5, 0x27, 0x0f, 0x91, 0x07, 0x24, 0x8b, 0x03, 0xd5, 0x18, 0xe7, 0x50, 0x18, 0xff, 0x00, 0x30, 0x16, 0x00, 0x00, 0x61, 0x62, 0x63, 0x0a // data from frag2
|
||||
};
|
||||
|
||||
TEST(IPV4_REASSEMBLE, PADDING)
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, PADDING_ORDER)
|
||||
{
|
||||
struct packet pkt;
|
||||
struct packet *new_pkt;
|
||||
const struct layer_record *layer;
|
||||
struct ip_reassemble_manager *mgr;
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1,
|
||||
.bucket_entries = 16,
|
||||
.bucket_num = 8,
|
||||
};
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = ip_reassemble_manager_create(&config);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
0, 0, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag1
|
||||
packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
1, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag2
|
||||
packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
2, 1, 1, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// check packet
|
||||
packet_print(new_pkt);
|
||||
// packet_print(new_pkt);
|
||||
EXPECT_TRUE(new_pkt->data_len == 14 /* ETH */ + 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(new_pkt->data_len == sizeof(expect));
|
||||
EXPECT_TRUE(memcmp(new_pkt->data_ptr, expect, new_pkt->data_len) == 0);
|
||||
@@ -272,26 +284,446 @@ TEST(IPV4_REASSEMBLE, PADDING)
|
||||
|
||||
ip_reassemble_manager_destory(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, PADDING_UNORDER)
|
||||
{
|
||||
struct packet pkt;
|
||||
struct packet *new_pkt;
|
||||
const struct layer_record *layer;
|
||||
struct ip_reassemble_manager *mgr;
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1,
|
||||
.bucket_entries = 16,
|
||||
.bucket_num = 8,
|
||||
};
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = ip_reassemble_manager_create(&config);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
0, 0, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag2
|
||||
packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
1, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag1
|
||||
packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
2, 1, 1, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// check packet
|
||||
// packet_print(new_pkt);
|
||||
EXPECT_TRUE(new_pkt->data_len == 14 /* ETH */ + 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(new_pkt->data_len == sizeof(expect));
|
||||
EXPECT_TRUE(memcmp(new_pkt->data_ptr, expect, new_pkt->data_len) == 0);
|
||||
|
||||
// check IPv4
|
||||
layer = packet_get_innermost_layer(new_pkt, LAYER_TYPE_IPV4);
|
||||
EXPECT_TRUE(layer);
|
||||
struct ip *hdr = (struct ip *)layer->hdr_ptr;
|
||||
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
|
||||
EXPECT_TRUE(ipv4_hdr_get_hdr_len(hdr) == 20 /* IPv4 */);
|
||||
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_total_len(hdr) == 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 0xffff);
|
||||
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 0x0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
|
||||
EXPECT_TRUE(ipv4_hdr_get_proto(hdr) == 6);
|
||||
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x6d73); // NOTE this is correct checksum
|
||||
EXPECT_TRUE(ipv4_hdr_get_src_addr(hdr) == 0xc0a82467);
|
||||
EXPECT_TRUE(ipv4_hdr_get_dst_addr(hdr) == 0xc0a82889);
|
||||
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_opt_data(hdr) == NULL);
|
||||
|
||||
// check TCP
|
||||
layer = packet_get_innermost_layer(new_pkt, LAYER_TYPE_TCP);
|
||||
EXPECT_TRUE(layer);
|
||||
struct tcphdr *tcp_hdr = (struct tcphdr *)layer->hdr_ptr;
|
||||
EXPECT_TRUE(tcp_hdr_get_src_port(tcp_hdr) == 62629);
|
||||
EXPECT_TRUE(tcp_hdr_get_dst_port(tcp_hdr) == 9999);
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp_hdr) == 2433164423);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp_hdr) == 64297191);
|
||||
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp_hdr) == 20);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp_hdr) == 0x018);
|
||||
EXPECT_TRUE(tcp_hdr_get_window(tcp_hdr) == 65280);
|
||||
EXPECT_TRUE(tcp_hdr_get_checksum(tcp_hdr) == 0x5e92);
|
||||
EXPECT_TRUE(tcp_hdr_get_urg_ptr(tcp_hdr) == 0);
|
||||
EXPECT_TRUE(tcp_hdr_get_opt_len(tcp_hdr) == 0);
|
||||
EXPECT_TRUE(tcp_hdr_get_opt_data(tcp_hdr) == NULL);
|
||||
|
||||
// free packet
|
||||
packet_free(new_pkt);
|
||||
|
||||
ip_reassemble_manager_destory(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, EXPIRE)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
struct packet pkt;
|
||||
struct packet *new_pkt;
|
||||
struct ip_reassemble_manager *mgr;
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1,
|
||||
.bucket_entries = 16,
|
||||
.bucket_num = 8,
|
||||
};
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = ip_reassemble_manager_create(&config);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
0, 0, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag1
|
||||
packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
1, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// sleep 1s
|
||||
sleep(1);
|
||||
timestamp_update();
|
||||
|
||||
// frag2
|
||||
packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
2, 2, 1, 1, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// free packet
|
||||
packet_free(new_pkt);
|
||||
|
||||
ip_reassemble_manager_destory(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, DUP_FIRST_FRAG)
|
||||
{
|
||||
struct packet pkt;
|
||||
struct packet *new_pkt;
|
||||
const struct layer_record *layer;
|
||||
struct ip_reassemble_manager *mgr;
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1,
|
||||
.bucket_entries = 16,
|
||||
.bucket_num = 8,
|
||||
};
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = ip_reassemble_manager_create(&config);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
0, 0, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag1
|
||||
packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
1, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag1
|
||||
packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
2, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 1, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag2
|
||||
packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
3, 1, 1, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 1, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// check packet
|
||||
// packet_print(new_pkt);
|
||||
EXPECT_TRUE(new_pkt->data_len == 14 /* ETH */ + 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(new_pkt->data_len == sizeof(expect));
|
||||
EXPECT_TRUE(memcmp(new_pkt->data_ptr, expect, new_pkt->data_len) == 0);
|
||||
|
||||
// check IPv4
|
||||
layer = packet_get_innermost_layer(new_pkt, LAYER_TYPE_IPV4);
|
||||
EXPECT_TRUE(layer);
|
||||
struct ip *hdr = (struct ip *)layer->hdr_ptr;
|
||||
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
|
||||
EXPECT_TRUE(ipv4_hdr_get_hdr_len(hdr) == 20 /* IPv4 */);
|
||||
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_total_len(hdr) == 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 0xffff);
|
||||
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 0x0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
|
||||
EXPECT_TRUE(ipv4_hdr_get_proto(hdr) == 6);
|
||||
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x6d73); // NOTE this is correct checksum
|
||||
EXPECT_TRUE(ipv4_hdr_get_src_addr(hdr) == 0xc0a82467);
|
||||
EXPECT_TRUE(ipv4_hdr_get_dst_addr(hdr) == 0xc0a82889);
|
||||
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_opt_data(hdr) == NULL);
|
||||
|
||||
// check TCP
|
||||
layer = packet_get_innermost_layer(new_pkt, LAYER_TYPE_TCP);
|
||||
EXPECT_TRUE(layer);
|
||||
struct tcphdr *tcp_hdr = (struct tcphdr *)layer->hdr_ptr;
|
||||
EXPECT_TRUE(tcp_hdr_get_src_port(tcp_hdr) == 62629);
|
||||
EXPECT_TRUE(tcp_hdr_get_dst_port(tcp_hdr) == 9999);
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp_hdr) == 2433164423);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp_hdr) == 64297191);
|
||||
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp_hdr) == 20);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp_hdr) == 0x018);
|
||||
EXPECT_TRUE(tcp_hdr_get_window(tcp_hdr) == 65280);
|
||||
EXPECT_TRUE(tcp_hdr_get_checksum(tcp_hdr) == 0x5e92);
|
||||
EXPECT_TRUE(tcp_hdr_get_urg_ptr(tcp_hdr) == 0);
|
||||
EXPECT_TRUE(tcp_hdr_get_opt_len(tcp_hdr) == 0);
|
||||
EXPECT_TRUE(tcp_hdr_get_opt_data(tcp_hdr) == NULL);
|
||||
|
||||
// free packet
|
||||
packet_free(new_pkt);
|
||||
|
||||
ip_reassemble_manager_destory(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, DUP_LAST_FRAG)
|
||||
{
|
||||
struct packet pkt;
|
||||
struct packet *new_pkt;
|
||||
const struct layer_record *layer;
|
||||
struct ip_reassemble_manager *mgr;
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1,
|
||||
.bucket_entries = 16,
|
||||
.bucket_num = 8,
|
||||
};
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = ip_reassemble_manager_create(&config);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
0, 0, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag2
|
||||
packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
1, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag2
|
||||
packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
2, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 1, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// frag1
|
||||
packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
3, 1, 1, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 1, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// check packet
|
||||
// packet_print(new_pkt);
|
||||
EXPECT_TRUE(new_pkt->data_len == 14 /* ETH */ + 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(new_pkt->data_len == sizeof(expect));
|
||||
EXPECT_TRUE(memcmp(new_pkt->data_ptr, expect, new_pkt->data_len) == 0);
|
||||
|
||||
// check IPv4
|
||||
layer = packet_get_innermost_layer(new_pkt, LAYER_TYPE_IPV4);
|
||||
EXPECT_TRUE(layer);
|
||||
struct ip *hdr = (struct ip *)layer->hdr_ptr;
|
||||
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
|
||||
EXPECT_TRUE(ipv4_hdr_get_hdr_len(hdr) == 20 /* IPv4 */);
|
||||
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_total_len(hdr) == 20 /* IPv4 */ + 20 /* TCP */ + 28 /* DATA */);
|
||||
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 0xffff);
|
||||
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 0x0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
|
||||
EXPECT_TRUE(ipv4_hdr_get_proto(hdr) == 6);
|
||||
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x6d73); // NOTE this is correct checksum
|
||||
EXPECT_TRUE(ipv4_hdr_get_src_addr(hdr) == 0xc0a82467);
|
||||
EXPECT_TRUE(ipv4_hdr_get_dst_addr(hdr) == 0xc0a82889);
|
||||
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
|
||||
EXPECT_TRUE(ipv4_hdr_get_opt_data(hdr) == NULL);
|
||||
|
||||
// check TCP
|
||||
layer = packet_get_innermost_layer(new_pkt, LAYER_TYPE_TCP);
|
||||
EXPECT_TRUE(layer);
|
||||
struct tcphdr *tcp_hdr = (struct tcphdr *)layer->hdr_ptr;
|
||||
EXPECT_TRUE(tcp_hdr_get_src_port(tcp_hdr) == 62629);
|
||||
EXPECT_TRUE(tcp_hdr_get_dst_port(tcp_hdr) == 9999);
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp_hdr) == 2433164423);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp_hdr) == 64297191);
|
||||
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp_hdr) == 20);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp_hdr) == 0x018);
|
||||
EXPECT_TRUE(tcp_hdr_get_window(tcp_hdr) == 65280);
|
||||
EXPECT_TRUE(tcp_hdr_get_checksum(tcp_hdr) == 0x5e92);
|
||||
EXPECT_TRUE(tcp_hdr_get_urg_ptr(tcp_hdr) == 0);
|
||||
EXPECT_TRUE(tcp_hdr_get_opt_len(tcp_hdr) == 0);
|
||||
EXPECT_TRUE(tcp_hdr_get_opt_data(tcp_hdr) == NULL);
|
||||
|
||||
// free packet
|
||||
packet_free(new_pkt);
|
||||
|
||||
ip_reassemble_manager_destory(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, FULL)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
struct packet pkt;
|
||||
struct packet *new_pkt;
|
||||
struct ip_reassemble_manager *mgr;
|
||||
struct ip_reassemble_config config = {
|
||||
.enable = true,
|
||||
.timeout = 1,
|
||||
.bucket_entries = 1,
|
||||
.bucket_num = 1,
|
||||
};
|
||||
|
||||
TEST(IPV4_REASSEMBLE, HOLE)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
timestamp_update();
|
||||
|
||||
TEST(IPV4_REASSEMBLE, NESTED)
|
||||
{
|
||||
// TODO
|
||||
mgr = ip_reassemble_manager_create(&config);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
0, 0, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
char dup_frag[sizeof(frag1)] = {0};
|
||||
memcpy(dup_frag, frag1, sizeof(frag1));
|
||||
packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag));
|
||||
|
||||
// flow1
|
||||
packet_set_ipv4_src_addr(&pkt, 1);
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
1, 1, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// flow2
|
||||
packet_set_ipv4_src_addr(&pkt, 2);
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
2, 2, 0, 0, // ip4: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// flow3
|
||||
packet_set_ipv4_src_addr(&pkt, 3);
|
||||
new_pkt = ip_reassemble_packet(mgr, &pkt);
|
||||
EXPECT_TRUE(new_pkt == NULL);
|
||||
// ip_reassemble_manager_print_stat(mgr);
|
||||
check_stat(ip_reassemble_manager_get_stat(mgr),
|
||||
3, 2, 0, 0, // ip4: find, add, del, timeout
|
||||
1, 0, 0, 0, 0, 0, // ip4: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
0, 0, 0, 0, // ip6: find, add, del, timeout
|
||||
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
|
||||
|
||||
// free packet
|
||||
packet_free(new_pkt);
|
||||
|
||||
ip_reassemble_manager_destory(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(IPV4_REASSEMBLE, OVERLAP)
|
||||
{
|
||||
// TEST ON IPv6
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user