#include <gtest/gtest.h>

#include "session_transition.h"

TEST(SESSION_TRANSITION, RUN)
{
    session_transition_init();

    EXPECT_TRUE(session_transition_run(SESSION_STATE_INIT, NONE) == SESSION_STATE_INIT);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_INIT, TCP_SYN) == SESSION_STATE_OPENING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_INIT, TCP_SYN_ACK) == SESSION_STATE_OPENING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_INIT, UDP_DATA) == SESSION_STATE_OPENING);

    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, NONE) == SESSION_STATE_OPENING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, TCP_DATA) == SESSION_STATE_ACTIVE);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, UDP_DATA) == SESSION_STATE_ACTIVE);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, TCP_FIN) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, TCP_RST) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, TIMEOUT) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, USER_CLOSE) == SESSION_STATE_DISCARD);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, LRU_EVICT) == SESSION_STATE_CLOSED);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_OPENING, PORT_REUSE_EVICT) == SESSION_STATE_CLOSED);

    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, NONE) == SESSION_STATE_ACTIVE);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, TCP_FIN) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, TCP_RST) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, TIMEOUT) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, USER_CLOSE) == SESSION_STATE_DISCARD);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, LRU_EVICT) == SESSION_STATE_CLOSED);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_ACTIVE, PORT_REUSE_EVICT) == SESSION_STATE_CLOSED);

    EXPECT_TRUE(session_transition_run(SESSION_STATE_CLOSING, NONE) == SESSION_STATE_CLOSING);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_CLOSING, USER_CLOSE) == SESSION_STATE_DISCARD);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_CLOSING, LRU_EVICT) == SESSION_STATE_CLOSED);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_CLOSING, PORT_REUSE_EVICT) == SESSION_STATE_CLOSED);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_CLOSING, TIMEOUT) == SESSION_STATE_CLOSED);

    EXPECT_TRUE(session_transition_run(SESSION_STATE_DISCARD, NONE) == SESSION_STATE_DISCARD);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_DISCARD, LRU_EVICT) == SESSION_STATE_CLOSED);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_DISCARD, PORT_REUSE_EVICT) == SESSION_STATE_CLOSED);
    EXPECT_TRUE(session_transition_run(SESSION_STATE_DISCARD, TIMEOUT) == SESSION_STATE_CLOSED);
}

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