#include <gtest/gtest.h>

#include "utils.h"
#include "packet_private.h"
#include "packet_parser.h"
#include "session_private.h"
#include "session_manager.h"
#include "test_packets.h"

struct session_manager_options opts = {
    // max session number
    .max_tcp_session_num = RX_BURST_MAX * 2,
    .max_udp_session_num = RX_BURST_MAX * 2,

    // 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_init_timeout = 1,
    .tcp_handshake_timeout = 2,
    .tcp_data_timeout = 3,
    .tcp_half_closed_timeout = 4,
    .tcp_time_wait_timeout = 5,
    .tcp_discard_timeout = 6,
    .tcp_unverified_rst_timeout = 7,

    // udp timeout
    .udp_data_timeout = 8,
    .udp_discard_timeout = 0,

    // 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,

    // TCP Reassembly
    .tcp_reassembly_enable = 1,
    .tcp_reassembly_max_timeout = 60000,
    .tcp_reassembly_max_segments = 16,
};

static uint64_t session_id_generator(uint64_t now_ms __attribute__((unused)))
{
    static uint64_t count = 0;
    return (++count);
}

static void packet_set_ip_src_addr(struct packet *pkt, uint32_t addr)
{
    const struct layer_private *ipv4_layer = packet_get_innermost_layer(pkt, LAYER_PROTO_IPV4);
    EXPECT_TRUE(ipv4_layer);
    struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
    hdr->ip_src.s_addr = htonl(addr);
}

#if 1
TEST(UDP_OVERLOAD, EVICT_OLD_SESS)
{
    struct packet pkt;
    struct session *sess = NULL;
    struct session_manager *mgr = NULL;
    struct session_manager_stat *stat = NULL;

    mgr = session_manager_new(&opts, 1);
    EXPECT_TRUE(mgr != NULL);
    session_manager_set_session_id_generator(mgr, session_id_generator);

    // C2S REQ Packet
    printf("\n=> Packet Parse: UDP C2S REQ packet\n");
    memset(&pkt, 0, sizeof(pkt));
    packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
    printf("<= Packet Parse: done\n\n");

    // new session
    for (uint32_t i = 0; i < opts.max_udp_session_num; i++)
    {
        packet_set_ip_src_addr(&pkt, i);
        EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1));
    }
    printf("=> Session Manager: after add %lu new sessions\n", opts.max_udp_session_num);
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->udp_sess_used == opts.max_udp_session_num);
    EXPECT_TRUE(stat->udp_sess_opening == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_sess_active == 0);
    EXPECT_TRUE(stat->udp_sess_closing == 0);
    EXPECT_TRUE(stat->udp_sess_closed == RX_BURST_MAX); // have evicted, have't free
    EXPECT_TRUE(stat->udp_sess_evicted == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_pkts_bypass_table_full == 0);
    EXPECT_TRUE(stat->udp_pkts_bypass_session_evicted == 0);

    // evicted session
    while (1)
    {
        sess = session_manager_get_evicted_session(mgr);
        if (sess)
        {
            session_manager_free_session(mgr, sess);
        }
        else
        {
            break;
        }
    }

    for (uint32_t i = 0; i < RX_BURST_MAX; i++)
    {
        packet_set_ip_src_addr(&pkt, i);
        EXPECT_TRUE(session_manager_lookup_session_by_packet(mgr, &pkt) == NULL);
        EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1) == NULL); // hit evicted session, can't renew session
    }
    printf("=> Session Manager: after readd %d evicted sessions\n", RX_BURST_MAX);
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->udp_sess_used == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_sess_opening == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_sess_active == 0);
    EXPECT_TRUE(stat->udp_sess_closing == 0);
    EXPECT_TRUE(stat->udp_sess_closed == 0);
    EXPECT_TRUE(stat->udp_sess_evicted == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_pkts_bypass_table_full == 0);
    EXPECT_TRUE(stat->udp_pkts_bypass_session_evicted == RX_BURST_MAX);

    // evicted session timeout
    packet_set_ip_src_addr(&pkt, 0);
    EXPECT_TRUE(session_manager_lookup_session_by_packet(mgr, &pkt) == NULL);
    EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1 + opts.evicted_session_filter_timeout));
    printf("=> Session Manager: after evicted session timeout\n");
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->udp_sess_used == RX_BURST_MAX + 1);
    EXPECT_TRUE(stat->udp_sess_opening == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_sess_active == 0);
    EXPECT_TRUE(stat->udp_sess_closing == 0);
    EXPECT_TRUE(stat->udp_sess_closed == 1); // have evicted, have't free
    EXPECT_TRUE(stat->udp_sess_evicted == RX_BURST_MAX + 1);
    EXPECT_TRUE(stat->udp_pkts_bypass_table_full == 0);
    EXPECT_TRUE(stat->udp_pkts_bypass_session_evicted == RX_BURST_MAX);

    session_manager_free(mgr);
}
#endif

#if 1
TEST(UDP_OVERLOAD, EVICT_NEW_SESS)
{
    struct packet pkt;
    struct session_manager *mgr = NULL;
    struct session_manager_stat *stat = NULL;
    struct session_manager_options _opts;
    memcpy(&_opts, &opts, sizeof(struct session_manager_options));
    _opts.udp_overload_evict_old_sess = 0;

    mgr = session_manager_new(&_opts, 1);
    EXPECT_TRUE(mgr != NULL);
    session_manager_set_session_id_generator(mgr, session_id_generator);

    // C2S REQ Packet
    printf("\n=> Packet Parse: UDP C2S REQ packet\n");
    memset(&pkt, 0, sizeof(pkt));
    packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
    printf("<= Packet Parse: done\n\n");

    // new session
    for (uint32_t i = 0; i < opts.max_udp_session_num; i++)
    {
        packet_set_ip_src_addr(&pkt, i);
        EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1));
    }
    printf("=> Session Manager: after add %lu new sessions\n", opts.max_udp_session_num);
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->udp_sess_used == opts.max_udp_session_num);
    EXPECT_TRUE(stat->udp_sess_opening == opts.max_udp_session_num);
    EXPECT_TRUE(stat->udp_sess_active == 0);
    EXPECT_TRUE(stat->udp_sess_closing == 0);
    EXPECT_TRUE(stat->udp_sess_closed == 0);
    EXPECT_TRUE(stat->udp_sess_evicted == 0);
    EXPECT_TRUE(stat->udp_pkts_bypass_table_full == 0);
    EXPECT_TRUE(stat->udp_pkts_bypass_session_evicted == 0);

    // evicted session
    EXPECT_TRUE(session_manager_get_evicted_session(mgr) == NULL);

    // table full, evict new session
    for (uint32_t i = 0; i < RX_BURST_MAX; i++)
    {
        packet_set_ip_src_addr(&pkt, opts.max_udp_session_num + i);
        EXPECT_TRUE(session_manager_lookup_session_by_packet(mgr, &pkt) == NULL);
        EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1) == NULL);
    }
    printf("=> Session Manager: after readd %d evicted session\n", RX_BURST_MAX);
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->udp_sess_used == opts.max_udp_session_num);
    EXPECT_TRUE(stat->udp_sess_opening == opts.max_udp_session_num);
    EXPECT_TRUE(stat->udp_sess_active == 0);
    EXPECT_TRUE(stat->udp_sess_closing == 0);
    EXPECT_TRUE(stat->udp_sess_closed == 0);
    EXPECT_TRUE(stat->udp_sess_evicted == 0);
    EXPECT_TRUE(stat->udp_pkts_bypass_table_full == RX_BURST_MAX);
    EXPECT_TRUE(stat->udp_pkts_bypass_session_evicted == 0);

    session_manager_free(mgr);
}
#endif

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