Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
| 3 | |
| 4 | #include "host_console.hpp" |
| 5 | |
| 6 | #include <sys/socket.h> |
| 7 | #include <sys/un.h> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
Chau Ly | 47734e2 | 2023-07-19 08:03:18 +0000 | [diff] [blame] | 11 | static constexpr char socketPath[] = "\0obmc-console"; |
| 12 | static constexpr std::string defaultSocketId = "default"; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * @class HostConsoleTest |
| 16 | * @brief Persistent file storage tests. |
| 17 | */ |
| 18 | class HostConsoleTest : public ::testing::Test |
| 19 | { |
| 20 | protected: |
| 21 | void startServer(const char* socketId) |
| 22 | { |
| 23 | // Start server |
| 24 | serverSocket = socket(AF_UNIX, SOCK_STREAM, 0); |
| 25 | ASSERT_NE(serverSocket, -1); |
| 26 | std::string path(socketPath, socketPath + sizeof(socketPath) - 1); |
Chau Ly | 47734e2 | 2023-07-19 08:03:18 +0000 | [diff] [blame] | 27 | path += '.'; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 28 | if (*socketId) |
| 29 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 30 | path += socketId; |
| 31 | } |
Chau Ly | 47734e2 | 2023-07-19 08:03:18 +0000 | [diff] [blame] | 32 | else |
| 33 | { |
| 34 | path += defaultSocketId; |
| 35 | } |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 36 | sockaddr_un sa; |
| 37 | sa.sun_family = AF_UNIX; |
| 38 | memcpy(&sa.sun_path, path.c_str(), path.length()); |
| 39 | const socklen_t len = sizeof(sa) - sizeof(sa.sun_path) + path.length(); |
Patrick Williams | 87c333e | 2024-08-16 15:20:11 -0400 | [diff] [blame^] | 40 | ASSERT_NE(bind(serverSocket, reinterpret_cast<const sockaddr*>(&sa), |
| 41 | len), |
| 42 | -1); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 43 | ASSERT_NE(listen(serverSocket, 1), -1); |
| 44 | } |
| 45 | |
| 46 | void TearDown() override |
| 47 | { |
| 48 | // Stop server |
| 49 | if (serverSocket != -1) |
| 50 | { |
| 51 | close(serverSocket); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | int serverSocket = -1; |
| 56 | }; |
| 57 | |
| 58 | TEST_F(HostConsoleTest, SingleHost) |
| 59 | { |
| 60 | const char* socketId = ""; |
| 61 | startServer(socketId); |
| 62 | |
| 63 | HostConsole con(socketId); |
| 64 | con.connect(); |
| 65 | |
| 66 | const int clientSocket = accept(serverSocket, nullptr, nullptr); |
| 67 | EXPECT_NE(clientSocket, -1); |
| 68 | close(clientSocket); |
| 69 | } |
| 70 | |
| 71 | TEST_F(HostConsoleTest, MultiHost) |
| 72 | { |
| 73 | const char* socketId = "host123"; |
| 74 | startServer(socketId); |
| 75 | |
| 76 | HostConsole con(socketId); |
| 77 | con.connect(); |
| 78 | |
| 79 | const int clientSocket = accept(serverSocket, nullptr, nullptr); |
| 80 | EXPECT_NE(clientSocket, -1); |
| 81 | |
| 82 | const char* data = "test data"; |
| 83 | const size_t len = strlen(data); |
| 84 | EXPECT_EQ(send(clientSocket, data, len, 0), len); |
| 85 | |
| 86 | char buf[64]; |
| 87 | memset(buf, 0, sizeof(buf)); |
| 88 | EXPECT_EQ(con.read(buf, sizeof(buf)), len); |
| 89 | EXPECT_STREQ(buf, data); |
| 90 | EXPECT_EQ(con.read(buf, sizeof(buf)), 0); |
| 91 | |
| 92 | close(clientSocket); |
| 93 | } |