Refactor the session manager using session transformation 2D array & Update test case
This commit is contained in:
@@ -1,60 +1,74 @@
|
||||
#include "test_utils.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "session.h"
|
||||
#include "session_manager.h"
|
||||
|
||||
#include "ipv4_utils.h"
|
||||
#include "test_packets.h"
|
||||
|
||||
struct session_manager_options opts = {
|
||||
// max session number
|
||||
.max_tcp_session_num = 256,
|
||||
.max_udp_session_num = 256,
|
||||
|
||||
// session overload
|
||||
.tcp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session
|
||||
.udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session
|
||||
|
||||
// tcp timeout
|
||||
.tcp_timeout_init = 1,
|
||||
.tcp_timeout_handshake = 2,
|
||||
.tcp_timeout_data = 3,
|
||||
.tcp_timeout_half_closed = 4,
|
||||
.tcp_timeout_time_wait = 5,
|
||||
.tcp_timeout_discard = 6,
|
||||
|
||||
// udp timeout
|
||||
.udp_timeout_data = 7,
|
||||
|
||||
// duplicate packet filter
|
||||
.duplicated_packet_filter_enable = 1,
|
||||
.duplicated_packet_filter_capacity = 1000,
|
||||
.duplicated_packet_filter_timeout = 10,
|
||||
.duplicated_packet_filter_error_rate = 0.0001,
|
||||
|
||||
// evicted session filter
|
||||
.evicted_session_filter_enable = 1,
|
||||
.evicted_session_filter_capacity = 1000,
|
||||
.evicted_session_filter_timeout = 10,
|
||||
.evicted_session_filter_error_rate = 0.0001,
|
||||
};
|
||||
|
||||
#if 1
|
||||
TEST(TIMEOUT, TCP_TIMEOUT_INIT1)
|
||||
TEST(TIMEOUT, TCP_TIMEOUT_INIT)
|
||||
{
|
||||
struct packet pkt;
|
||||
struct session *sess = NULL;
|
||||
struct session_manager *mgr = NULL;
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = session_manager_new(&opts);
|
||||
mgr = session_manager_new(&opts, 1);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
|
||||
// C2S SYN Packet
|
||||
printf("=> packet parse: TCP C2S SYN packet\n");
|
||||
printf("\n=> Packet Parse: TCP C2S SYN packet\n");
|
||||
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
|
||||
printf("<= packet parse\n\n");
|
||||
sess = session_manager_update_session(mgr, &pkt);
|
||||
printf("<= Packet Parse: done\n\n");
|
||||
|
||||
// lookup session
|
||||
EXPECT_TRUE(session_manager_lookup_session(mgr, &pkt) == NULL);
|
||||
// new session
|
||||
sess = session_manager_new_session(mgr, &pkt, 1);
|
||||
EXPECT_TRUE(sess);
|
||||
|
||||
__session_manager_check_counter(mgr, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
|
||||
__session_manager_check_tcp_timeout_init(mgr, &opts);
|
||||
__session_manager_check_tcp_timeout_time_wait(mgr, &opts);
|
||||
|
||||
__session_manager_check_counter(mgr, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
|
||||
session_manager_free(mgr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
TEST(TIMEOUT, TCP_TIMEOUT_INIT2)
|
||||
{
|
||||
struct packet pkt;
|
||||
struct session *sess = NULL;
|
||||
struct session_manager *mgr = NULL;
|
||||
|
||||
timestamp_update();
|
||||
|
||||
mgr = session_manager_new(&opts);
|
||||
EXPECT_TRUE(mgr != NULL);
|
||||
|
||||
// S2C SYNACK Packet
|
||||
printf("=> packet parse: TCP S2C SYNACK packet\n");
|
||||
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
|
||||
printf("<= packet parse\n\n");
|
||||
sess = session_manager_update_session(mgr, &pkt);
|
||||
// expire session
|
||||
EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_timeout_init) == NULL); // opening -> closing
|
||||
sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_timeout_init + opts.tcp_timeout_time_wait); // closing -> closed
|
||||
EXPECT_TRUE(sess);
|
||||
|
||||
__session_manager_check_counter(mgr, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
|
||||
__session_manager_check_tcp_timeout_init(mgr, &opts);
|
||||
__session_manager_check_tcp_timeout_time_wait(mgr, &opts);
|
||||
|
||||
__session_manager_check_counter(mgr, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
EXPECT_TRUE(session_get_state(sess) == SESSION_STATE_CLOSED);
|
||||
EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT);
|
||||
session_dump(sess);
|
||||
// free session
|
||||
session_manager_free_session(mgr, sess);
|
||||
|
||||
session_manager_free(mgr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user