#include <gtest/gtest.h>

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

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(TCP_OVERLOAD, EVICT_OLD_SESS)
{
    struct packet pkt;
    struct session_manager *mgr = NULL;
    struct session_manager_stat *stat = NULL;
    struct session_manager_config _cfg;
    memcpy(&_cfg, &cfg, sizeof(struct session_manager_config));
    _cfg.tcp_session_max = RX_BURST_MAX * 2;
    _cfg.udp_session_max = RX_BURST_MAX * 2;

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

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

    // new session
    for (uint32_t i = 0; i < _cfg.tcp_session_max; 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", _cfg.tcp_session_max);
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->tcp_sess_used == _cfg.tcp_session_max);
    EXPECT_TRUE(stat->tcp_sess_opening == RX_BURST_MAX);
    EXPECT_TRUE(stat->tcp_sess_active == 0);
    EXPECT_TRUE(stat->tcp_sess_closing == 0);
    EXPECT_TRUE(stat->tcp_sess_closed == RX_BURST_MAX); // have evicted, have't free
    EXPECT_TRUE(stat->tcp_sess_evicted == RX_BURST_MAX);
    EXPECT_TRUE(stat->tcp_pkts_bypass_table_full == 0);
    EXPECT_TRUE(stat->tcp_pkts_bypass_session_not_found == 0);

    session_manager_free(mgr);
}
#endif

#if 1
TEST(TCP_OVERLOAD, EVICT_NEW_SESS)
{
    struct packet pkt;
    struct session_manager *mgr = NULL;
    struct session_manager_stat *stat = NULL;
    struct session_manager_config _cfg;
    memcpy(&_cfg, &cfg, sizeof(struct session_manager_config));
    _cfg.tcp_session_max = RX_BURST_MAX * 2;
    _cfg.udp_session_max = RX_BURST_MAX * 2;
    _cfg.evict_old_on_tcp_table_limit = 0;

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

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

    // new session
    for (uint32_t i = 0; i < _cfg.tcp_session_max; 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", _cfg.tcp_session_max);
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->tcp_sess_used == _cfg.tcp_session_max);
    EXPECT_TRUE(stat->tcp_sess_opening == _cfg.tcp_session_max);
    EXPECT_TRUE(stat->tcp_sess_active == 0);
    EXPECT_TRUE(stat->tcp_sess_closing == 0);
    EXPECT_TRUE(stat->tcp_sess_closed == 0);
    EXPECT_TRUE(stat->tcp_sess_evicted == 0);
    EXPECT_TRUE(stat->tcp_pkts_bypass_table_full == 0);
    EXPECT_TRUE(stat->tcp_pkts_bypass_session_not_found == 0);

    // table full, evict new session
    for (uint32_t i = 0; i < RX_BURST_MAX; i++)
    {
        packet_set_ip_src_addr(&pkt, _cfg.tcp_session_max + 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 evicte new session\n");
    stat = session_manager_stat(mgr);
    EXPECT_TRUE(stat);
    EXPECT_TRUE(stat->tcp_sess_used == _cfg.tcp_session_max);
    EXPECT_TRUE(stat->tcp_sess_opening == _cfg.tcp_session_max);
    EXPECT_TRUE(stat->tcp_sess_active == 0);
    EXPECT_TRUE(stat->tcp_sess_closing == 0);
    EXPECT_TRUE(stat->tcp_sess_closed == 0);
    EXPECT_TRUE(stat->tcp_sess_evicted == 0);
    EXPECT_TRUE(stat->tcp_pkts_bypass_table_full == RX_BURST_MAX);
    EXPECT_TRUE(stat->tcp_pkts_bypass_session_not_found == 0);

    session_manager_free(mgr);
}
#endif

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