// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2020 YADRO

#include "host_console.hpp"

#include <sys/socket.h>
#include <sys/un.h>

#include <gtest/gtest.h>

static constexpr char socketPath[] = "\0obmc-console";
static constexpr std::string defaultSocketId = "default";

/**
 * @class HostConsoleTest
 * @brief Persistent file storage tests.
 */
class HostConsoleTest : public ::testing::Test
{
  protected:
    void startServer(const char* socketId)
    {
        // Start server
        serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);
        ASSERT_NE(serverSocket, -1);
        std::string path(socketPath, socketPath + sizeof(socketPath) - 1);
        path += '.';
        if (*socketId)
        {
            path += socketId;
        }
        else
        {
            path += defaultSocketId;
        }
        sockaddr_un sa;
        sa.sun_family = AF_UNIX;
        memcpy(&sa.sun_path, path.c_str(), path.length());
        const socklen_t len = sizeof(sa) - sizeof(sa.sun_path) + path.length();
        ASSERT_NE(
            bind(serverSocket, reinterpret_cast<const sockaddr*>(&sa), len),
            -1);
        ASSERT_NE(listen(serverSocket, 1), -1);
    }

    void TearDown() override
    {
        // Stop server
        if (serverSocket != -1)
        {
            close(serverSocket);
        }
    }

    int serverSocket = -1;
};

TEST_F(HostConsoleTest, SingleHost)
{
    const char* socketId = "";
    startServer(socketId);

    HostConsole con(socketId);
    con.connect();

    const int clientSocket = accept(serverSocket, nullptr, nullptr);
    EXPECT_NE(clientSocket, -1);
    close(clientSocket);
}

TEST_F(HostConsoleTest, MultiHost)
{
    const char* socketId = "host123";
    startServer(socketId);

    HostConsole con(socketId);
    con.connect();

    const int clientSocket = accept(serverSocket, nullptr, nullptr);
    EXPECT_NE(clientSocket, -1);

    const char* data = "test data";
    const size_t len = strlen(data);
    EXPECT_EQ(send(clientSocket, data, len, 0), len);

    char buf[64];
    memset(buf, 0, sizeof(buf));
    EXPECT_EQ(con.read(buf, sizeof(buf)), len);
    EXPECT_STREQ(buf, data);
    EXPECT_EQ(con.read(buf, sizeof(buf)), 0);

    close(clientSocket);
}
