blob: e3e23a46321820a84772180874be5eee99d6ef7a [file] [log] [blame]
// 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);
}