1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci 16d9f0492fSopenharmony_ci#include <gtest/gtest.h> 17d9f0492fSopenharmony_ci#include <thread> 18d9f0492fSopenharmony_ci#include <sys/eventfd.h> 19d9f0492fSopenharmony_ci#include <cstdarg> 20d9f0492fSopenharmony_ci 21d9f0492fSopenharmony_ci#include "begetctl.h" 22d9f0492fSopenharmony_ci#include "cJSON.h" 23d9f0492fSopenharmony_ci#include "init.h" 24d9f0492fSopenharmony_ci#include "init_hashmap.h" 25d9f0492fSopenharmony_ci#include "init_param.h" 26d9f0492fSopenharmony_ci#include "init_utils.h" 27d9f0492fSopenharmony_ci#include "le_epoll.h" 28d9f0492fSopenharmony_ci#include "le_loop.h" 29d9f0492fSopenharmony_ci#include "le_socket.h" 30d9f0492fSopenharmony_ci#include "le_task.h" 31d9f0492fSopenharmony_ci#include "loop_event.h" 32d9f0492fSopenharmony_ci#include "param_manager.h" 33d9f0492fSopenharmony_ci#include "param_message.h" 34d9f0492fSopenharmony_ci#include "param_utils.h" 35d9f0492fSopenharmony_ci#include "trigger_manager.h" 36d9f0492fSopenharmony_ci 37d9f0492fSopenharmony_ciusing namespace testing::ext; 38d9f0492fSopenharmony_ciusing namespace std; 39d9f0492fSopenharmony_ci 40d9f0492fSopenharmony_cinamespace init_ut { 41d9f0492fSopenharmony_ciconst std::string TCP_SERVER = "127.0.0.1:7777"; 42d9f0492fSopenharmony_ciconst std::string PIPE_SERVER = STARTUP_INIT_UT_PATH "/dev/unix/socket/testsocket"; 43d9f0492fSopenharmony_ciconst std::string WATCHER_FILE = STARTUP_INIT_UT_PATH "/test_watcher_file"; 44d9f0492fSopenharmony_ciconst std::string FORMAT_STR = "{ \"cmd\":%d, \"message\":\"%s\" }"; 45d9f0492fSopenharmony_ci 46d9f0492fSopenharmony_cistatic LoopHandle g_loopClient_ = nullptr; 47d9f0492fSopenharmony_cistatic LoopHandle g_loopServer_ = nullptr; 48d9f0492fSopenharmony_cistatic int g_maxCount = 0; 49d9f0492fSopenharmony_cistatic int g_timeCount = 0; 50d9f0492fSopenharmony_cistatic int g_cmd = 2; 51d9f0492fSopenharmony_cistatic void DecodeMessage(const char *buffer, size_t nread, uint32_t &cmd) 52d9f0492fSopenharmony_ci{ 53d9f0492fSopenharmony_ci cJSON *root = cJSON_ParseWithLength(buffer, nread); 54d9f0492fSopenharmony_ci if (root == nullptr) { 55d9f0492fSopenharmony_ci EXPECT_NE(root, nullptr); 56d9f0492fSopenharmony_ci printf("Invalid message %s \n", buffer); 57d9f0492fSopenharmony_ci return; 58d9f0492fSopenharmony_ci } 59d9f0492fSopenharmony_ci printf("Message: %s \n", cJSON_GetStringValue(cJSON_GetObjectItem(root, "message"))); 60d9f0492fSopenharmony_ci cmd = cJSON_GetNumberValue(cJSON_GetObjectItem(root, "cmd")); 61d9f0492fSopenharmony_ci printf("cmd: %d \n", cmd); 62d9f0492fSopenharmony_ci cJSON_Delete(root); 63d9f0492fSopenharmony_ci return; 64d9f0492fSopenharmony_ci} 65d9f0492fSopenharmony_ci 66d9f0492fSopenharmony_cistatic void SendMessage(const LoopHandle loopHandle, const TaskHandle taskHandle, const char *message, ...) 67d9f0492fSopenharmony_ci{ 68d9f0492fSopenharmony_ci uint32_t bufferSize = 1024; // 1024 buffer size 69d9f0492fSopenharmony_ci BufferHandle handle = LE_CreateBuffer(loopHandle, bufferSize); 70d9f0492fSopenharmony_ci char *buffer = (char *)LE_GetBufferInfo(handle, nullptr, &bufferSize); 71d9f0492fSopenharmony_ci 72d9f0492fSopenharmony_ci va_list vargs; 73d9f0492fSopenharmony_ci va_start(vargs, message); 74d9f0492fSopenharmony_ci if (vsnprintf_s(buffer, bufferSize, bufferSize - 1, message, vargs) == -1) { 75d9f0492fSopenharmony_ci LE_FreeBuffer(loopHandle, taskHandle, handle); 76d9f0492fSopenharmony_ci va_end(vargs); 77d9f0492fSopenharmony_ci EXPECT_EQ(1, 0); 78d9f0492fSopenharmony_ci return; 79d9f0492fSopenharmony_ci } 80d9f0492fSopenharmony_ci va_end(vargs); 81d9f0492fSopenharmony_ci int ret = LE_Send(loopHandle, taskHandle, handle, bufferSize); 82d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 83d9f0492fSopenharmony_ci} 84d9f0492fSopenharmony_ci 85d9f0492fSopenharmony_cistatic void TestOnClose(const TaskHandle taskHandle) 86d9f0492fSopenharmony_ci{ 87d9f0492fSopenharmony_ci} 88d9f0492fSopenharmony_ci 89d9f0492fSopenharmony_cistatic LE_STATUS TestHandleTaskEvent(const LoopHandle loop, const TaskHandle task, uint32_t oper) 90d9f0492fSopenharmony_ci{ 91d9f0492fSopenharmony_ci return LE_SUCCESS; 92d9f0492fSopenharmony_ci} 93d9f0492fSopenharmony_ci 94d9f0492fSopenharmony_cistatic void TestOnReceiveRequest(const TaskHandle task, const uint8_t *buffer, uint32_t nread) 95d9f0492fSopenharmony_ci{ 96d9f0492fSopenharmony_ci EXPECT_NE(buffer, nullptr); 97d9f0492fSopenharmony_ci if (buffer == nullptr) { 98d9f0492fSopenharmony_ci return; 99d9f0492fSopenharmony_ci } 100d9f0492fSopenharmony_ci printf("Server receive message %s \n", reinterpret_cast<const char *>(buffer)); 101d9f0492fSopenharmony_ci uint32_t cmd = 0; 102d9f0492fSopenharmony_ci DecodeMessage(reinterpret_cast<const char *>(buffer), nread, cmd); 103d9f0492fSopenharmony_ci SendMessage(g_loopServer_, task, reinterpret_cast<const char *>(buffer)); 104d9f0492fSopenharmony_ci} 105d9f0492fSopenharmony_ci 106d9f0492fSopenharmony_cistatic void TestClientOnReceiveRequest(const TaskHandle task, const uint8_t *buffer, uint32_t nread) 107d9f0492fSopenharmony_ci{ 108d9f0492fSopenharmony_ci printf("Client receive message %s \n", reinterpret_cast<const char *>(buffer)); 109d9f0492fSopenharmony_ci EXPECT_NE(buffer, nullptr); 110d9f0492fSopenharmony_ci if (buffer == nullptr) { 111d9f0492fSopenharmony_ci return; 112d9f0492fSopenharmony_ci } 113d9f0492fSopenharmony_ci uint32_t cmd = 0; 114d9f0492fSopenharmony_ci DecodeMessage(reinterpret_cast<const char *>(buffer), nread, cmd); 115d9f0492fSopenharmony_ci if (cmd == 5 || cmd == 2) { // 2 5 close server 116d9f0492fSopenharmony_ci LE_StopLoop(g_loopClient_); 117d9f0492fSopenharmony_ci } 118d9f0492fSopenharmony_ci} 119d9f0492fSopenharmony_ci 120d9f0492fSopenharmony_cistatic void ProcessAsyncEvent(const TaskHandle taskHandle, uint64_t eventId, const uint8_t *buffer, uint32_t buffLen) 121d9f0492fSopenharmony_ci{ 122d9f0492fSopenharmony_ci UNUSED(taskHandle); 123d9f0492fSopenharmony_ci UNUSED(eventId); 124d9f0492fSopenharmony_ci UNUSED(buffer); 125d9f0492fSopenharmony_ci UNUSED(buffLen); 126d9f0492fSopenharmony_ci} 127d9f0492fSopenharmony_ci 128d9f0492fSopenharmony_cistatic void TestSendMessageComplete(const TaskHandle taskHandle, BufferHandle handle) 129d9f0492fSopenharmony_ci{ 130d9f0492fSopenharmony_ci printf("SendMessage result %d \n", LE_GetSendResult(handle)); 131d9f0492fSopenharmony_ci uint32_t bufferSize = 1024; // 1024 buffer size 132d9f0492fSopenharmony_ci char *buffer = (char *)LE_GetBufferInfo(handle, nullptr, &bufferSize); 133d9f0492fSopenharmony_ci uint32_t cmd = 0; 134d9f0492fSopenharmony_ci DecodeMessage(reinterpret_cast<const char *>(buffer), bufferSize, cmd); 135d9f0492fSopenharmony_ci if (cmd == 5) { // 5 close server 136d9f0492fSopenharmony_ci LE_StopLoop(g_loopServer_); 137d9f0492fSopenharmony_ci } 138d9f0492fSopenharmony_ci} 139d9f0492fSopenharmony_ci 140d9f0492fSopenharmony_cistatic int TestTcpIncomingConnect(LoopHandle loop, TaskHandle server) 141d9f0492fSopenharmony_ci{ 142d9f0492fSopenharmony_ci PARAM_CHECK(server != nullptr, return -1, "Error server"); 143d9f0492fSopenharmony_ci printf("Tcp connect incoming \n"); 144d9f0492fSopenharmony_ci TaskHandle stream; 145d9f0492fSopenharmony_ci LE_StreamInfo info = {}; 146d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_STREAM | TASK_TCP | TASK_CONNECT; 147d9f0492fSopenharmony_ci info.baseInfo.close = TestOnClose; 148d9f0492fSopenharmony_ci info.baseInfo.userDataSize = 0; 149d9f0492fSopenharmony_ci info.disConnectComplete = nullptr; 150d9f0492fSopenharmony_ci info.sendMessageComplete = TestSendMessageComplete; 151d9f0492fSopenharmony_ci info.recvMessage = TestOnReceiveRequest; 152d9f0492fSopenharmony_ci LE_STATUS ret = LE_AcceptStreamClient(loop, server, &stream, &info); 153d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 154d9f0492fSopenharmony_ci return 0; 155d9f0492fSopenharmony_ci} 156d9f0492fSopenharmony_ci 157d9f0492fSopenharmony_cistatic int TestPipIncomingConnect(LoopHandle loop, TaskHandle server) 158d9f0492fSopenharmony_ci{ 159d9f0492fSopenharmony_ci PARAM_CHECK(server != nullptr, return -1, "Error server"); 160d9f0492fSopenharmony_ci printf("Pipe connect incoming \n"); 161d9f0492fSopenharmony_ci TaskHandle stream; 162d9f0492fSopenharmony_ci LE_StreamInfo info = {}; 163d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_CONNECT; 164d9f0492fSopenharmony_ci info.baseInfo.close = TestOnClose; 165d9f0492fSopenharmony_ci info.baseInfo.userDataSize = 0; 166d9f0492fSopenharmony_ci info.disConnectComplete = nullptr; 167d9f0492fSopenharmony_ci info.sendMessageComplete = TestSendMessageComplete; 168d9f0492fSopenharmony_ci info.recvMessage = TestOnReceiveRequest; 169d9f0492fSopenharmony_ci LE_STATUS ret = LE_AcceptStreamClient(loop, server, &stream, &info); 170d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 171d9f0492fSopenharmony_ci return 0; 172d9f0492fSopenharmony_ci} 173d9f0492fSopenharmony_ci 174d9f0492fSopenharmony_cistatic void TestConnectComplete(const TaskHandle client) 175d9f0492fSopenharmony_ci{ 176d9f0492fSopenharmony_ci printf("Connect complete \n"); 177d9f0492fSopenharmony_ci} 178d9f0492fSopenharmony_ci 179d9f0492fSopenharmony_cistatic void TestDisConnectComplete(const TaskHandle client) 180d9f0492fSopenharmony_ci{ 181d9f0492fSopenharmony_ci printf("DisConnect complete \n"); 182d9f0492fSopenharmony_ci LE_StopLoop(g_loopClient_); 183d9f0492fSopenharmony_ci} 184d9f0492fSopenharmony_ci 185d9f0492fSopenharmony_cistatic void TestProcessTimer(const TimerHandle taskHandle, void *context) 186d9f0492fSopenharmony_ci{ 187d9f0492fSopenharmony_ci g_timeCount++; 188d9f0492fSopenharmony_ci printf("ProcessTimer %d\n", g_timeCount); 189d9f0492fSopenharmony_ci if (g_maxCount == 2) { // 2 stop 190d9f0492fSopenharmony_ci if (g_timeCount >= g_maxCount) { 191d9f0492fSopenharmony_ci LE_StopLoop(g_loopClient_); 192d9f0492fSopenharmony_ci } 193d9f0492fSopenharmony_ci } 194d9f0492fSopenharmony_ci if (g_maxCount == 3) { // 3 stop timer 195d9f0492fSopenharmony_ci if (g_timeCount >= g_maxCount) { 196d9f0492fSopenharmony_ci LE_StopTimer(g_loopClient_, taskHandle); 197d9f0492fSopenharmony_ci LE_StopLoop(g_loopClient_); 198d9f0492fSopenharmony_ci } 199d9f0492fSopenharmony_ci } 200d9f0492fSopenharmony_ci if (g_maxCount == 10) { // 10 write watcher file 201d9f0492fSopenharmony_ci FILE *tmpFile = fopen(WATCHER_FILE.c_str(), "wr"); 202d9f0492fSopenharmony_ci if (tmpFile != nullptr) { 203d9f0492fSopenharmony_ci fprintf(tmpFile, "%s", "test watcher file 22222222222"); 204d9f0492fSopenharmony_ci (void)fflush(tmpFile); 205d9f0492fSopenharmony_ci fclose(tmpFile); 206d9f0492fSopenharmony_ci } 207d9f0492fSopenharmony_ci LE_StopTimer(g_loopClient_, taskHandle); 208d9f0492fSopenharmony_ci LE_StopLoop(g_loopClient_); 209d9f0492fSopenharmony_ci } 210d9f0492fSopenharmony_ci} 211d9f0492fSopenharmony_ci 212d9f0492fSopenharmony_cistatic void ProcessWatchEventTest(WatcherHandle taskHandle, int fd, uint32_t *events, const void *context) 213d9f0492fSopenharmony_ci{ 214d9f0492fSopenharmony_ci UNUSED(taskHandle); 215d9f0492fSopenharmony_ci UNUSED(fd); 216d9f0492fSopenharmony_ci UNUSED(events); 217d9f0492fSopenharmony_ci UNUSED(context); 218d9f0492fSopenharmony_ci printf("Process watcher event \n"); 219d9f0492fSopenharmony_ci LE_StopLoop(g_loopClient_); 220d9f0492fSopenharmony_ci} 221d9f0492fSopenharmony_ci 222d9f0492fSopenharmony_ciclass LoopServerUnitTest : public testing::Test { 223d9f0492fSopenharmony_cipublic: 224d9f0492fSopenharmony_ci LoopServerUnitTest() {}; 225d9f0492fSopenharmony_ci virtual ~LoopServerUnitTest() {}; 226d9f0492fSopenharmony_ci static void SetUpTestCase(void) {}; 227d9f0492fSopenharmony_ci static void TearDownTestCase(void) {}; 228d9f0492fSopenharmony_ci void SetUp() {}; 229d9f0492fSopenharmony_ci void TearDown() {}; 230d9f0492fSopenharmony_ci void TestBody(void) {}; 231d9f0492fSopenharmony_ci 232d9f0492fSopenharmony_ci // for thread to create tcp\pipe server 233d9f0492fSopenharmony_ci void RunServer(void) 234d9f0492fSopenharmony_ci { 235d9f0492fSopenharmony_ci TaskHandle tcpServer = nullptr; 236d9f0492fSopenharmony_ci TaskHandle pipeServer = nullptr; 237d9f0492fSopenharmony_ci LE_STATUS ret = LE_CreateLoop(&g_loopServer_); 238d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 239d9f0492fSopenharmony_ci // create server for tcp 240d9f0492fSopenharmony_ci LE_StreamServerInfo info = {}; 241d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_STREAM | TASK_TCP | TASK_SERVER; 242d9f0492fSopenharmony_ci info.socketId = -1; 243d9f0492fSopenharmony_ci info.server = const_cast<char *>(TCP_SERVER.c_str()); 244d9f0492fSopenharmony_ci info.baseInfo.close = TestOnClose; 245d9f0492fSopenharmony_ci info.incommingConnect = TestTcpIncomingConnect; 246d9f0492fSopenharmony_ci ret = LE_CreateStreamServer(g_loopServer_, &tcpServer, &info); 247d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 248d9f0492fSopenharmony_ci 249d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_SERVER; 250d9f0492fSopenharmony_ci info.socketId = -1; 251d9f0492fSopenharmony_ci info.server = const_cast<char *>(PIPE_SERVER.c_str()); 252d9f0492fSopenharmony_ci info.baseInfo.close = TestOnClose; 253d9f0492fSopenharmony_ci info.incommingConnect = TestPipIncomingConnect; 254d9f0492fSopenharmony_ci ret = LE_CreateStreamServer(g_loopServer_, &pipeServer, &info); 255d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 256d9f0492fSopenharmony_ci 257d9f0492fSopenharmony_ci printf("Run server pipeServer_ \n"); 258d9f0492fSopenharmony_ci // run loop for server 259d9f0492fSopenharmony_ci LE_RunLoop(g_loopServer_); 260d9f0492fSopenharmony_ci 261d9f0492fSopenharmony_ci printf("Run server pipeServer_ \n"); 262d9f0492fSopenharmony_ci LE_CloseStreamTask(g_loopServer_, pipeServer); 263d9f0492fSopenharmony_ci pipeServer = nullptr; 264d9f0492fSopenharmony_ci LE_CloseStreamTask(g_loopServer_, tcpServer); 265d9f0492fSopenharmony_ci tcpServer = nullptr; 266d9f0492fSopenharmony_ci LE_CloseLoop(g_loopServer_); 267d9f0492fSopenharmony_ci g_loopServer_ = nullptr; 268d9f0492fSopenharmony_ci } 269d9f0492fSopenharmony_ci 270d9f0492fSopenharmony_ci void StartServer() 271d9f0492fSopenharmony_ci { 272d9f0492fSopenharmony_ci std::thread(&LoopServerUnitTest::RunServer, this).detach(); 273d9f0492fSopenharmony_ci sleep(1); 274d9f0492fSopenharmony_ci } 275d9f0492fSopenharmony_ci 276d9f0492fSopenharmony_ci TaskHandle CreateConnect(const char *tcpServer, uint32_t flags) 277d9f0492fSopenharmony_ci { 278d9f0492fSopenharmony_ci if (g_loopClient_ == nullptr) { 279d9f0492fSopenharmony_ci LE_STATUS ret = LE_CreateLoop(&g_loopClient_); 280d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 281d9f0492fSopenharmony_ci } 282d9f0492fSopenharmony_ci 283d9f0492fSopenharmony_ci TaskHandle task = nullptr; 284d9f0492fSopenharmony_ci LE_StreamInfo info = {}; 285d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_STREAM | flags | TASK_CONNECT; 286d9f0492fSopenharmony_ci info.server = const_cast<char *>(tcpServer); 287d9f0492fSopenharmony_ci info.baseInfo.userDataSize = 0; 288d9f0492fSopenharmony_ci info.baseInfo.close = TestOnClose; 289d9f0492fSopenharmony_ci info.disConnectComplete = TestDisConnectComplete; 290d9f0492fSopenharmony_ci info.connectComplete = TestConnectComplete; 291d9f0492fSopenharmony_ci info.sendMessageComplete = nullptr; 292d9f0492fSopenharmony_ci info.recvMessage = TestClientOnReceiveRequest; 293d9f0492fSopenharmony_ci LE_STATUS status = LE_CreateStreamClient(g_loopClient_, &task, &info); 294d9f0492fSopenharmony_ci EXPECT_EQ(status, 0); 295d9f0492fSopenharmony_ci return task; 296d9f0492fSopenharmony_ci } 297d9f0492fSopenharmony_ci 298d9f0492fSopenharmony_ci WatcherHandle CreateWatcherTask(int fd, const char *fileName) 299d9f0492fSopenharmony_ci { 300d9f0492fSopenharmony_ci if (g_loopClient_ == nullptr) { 301d9f0492fSopenharmony_ci LE_STATUS ret = LE_CreateLoop(&g_loopClient_); 302d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 303d9f0492fSopenharmony_ci } 304d9f0492fSopenharmony_ci WatcherHandle handle = nullptr; 305d9f0492fSopenharmony_ci LE_WatchInfo info = {}; 306d9f0492fSopenharmony_ci info.fd = fd; 307d9f0492fSopenharmony_ci info.flags = WATCHER_ONCE; 308d9f0492fSopenharmony_ci info.events = EVENT_READ | EVENT_WRITE; 309d9f0492fSopenharmony_ci info.processEvent = ProcessWatchEventTest; 310d9f0492fSopenharmony_ci LE_STATUS status = LE_StartWatcher(g_loopClient_, &handle, &info, nullptr); 311d9f0492fSopenharmony_ci EXPECT_EQ(status, 0); 312d9f0492fSopenharmony_ci return handle; 313d9f0492fSopenharmony_ci } 314d9f0492fSopenharmony_ci 315d9f0492fSopenharmony_ci TimerHandle CreateTimerTask(int repeat) 316d9f0492fSopenharmony_ci { 317d9f0492fSopenharmony_ci if (g_loopClient_ == nullptr) { 318d9f0492fSopenharmony_ci LE_STATUS ret = LE_CreateLoop(&g_loopClient_); 319d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 320d9f0492fSopenharmony_ci } 321d9f0492fSopenharmony_ci TimerHandle timer = nullptr; 322d9f0492fSopenharmony_ci int ret = LE_CreateTimer(g_loopClient_, &timer, TestProcessTimer, nullptr); 323d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 324d9f0492fSopenharmony_ci ret = LE_StartTimer(g_loopClient_, timer, 500, repeat); // 500 ms 325d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 326d9f0492fSopenharmony_ci return timer; 327d9f0492fSopenharmony_ci } 328d9f0492fSopenharmony_ciprivate: 329d9f0492fSopenharmony_ci std::thread *serverThread_ = nullptr; 330d9f0492fSopenharmony_ci}; 331d9f0492fSopenharmony_ci 332d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestRunServer_001, TestSize.Level1) 333d9f0492fSopenharmony_ci{ 334d9f0492fSopenharmony_ci LoopServerUnitTest test; 335d9f0492fSopenharmony_ci test.StartServer(); 336d9f0492fSopenharmony_ci} 337d9f0492fSopenharmony_ci 338d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestPipConnect_001, TestSize.Level1) 339d9f0492fSopenharmony_ci{ 340d9f0492fSopenharmony_ci g_cmd = 2; // 2 only close client 341d9f0492fSopenharmony_ci LoopServerUnitTest test; 342d9f0492fSopenharmony_ci TaskHandle pipe = test.CreateConnect(PIPE_SERVER.c_str(), TASK_PIPE); 343d9f0492fSopenharmony_ci EXPECT_NE(pipe, nullptr); 344d9f0492fSopenharmony_ci SendMessage(g_loopClient_, pipe, FORMAT_STR.c_str(), g_cmd, "connect success"); 345d9f0492fSopenharmony_ci LE_RunLoop(g_loopClient_); 346d9f0492fSopenharmony_ci LE_CloseStreamTask(g_loopClient_, pipe); 347d9f0492fSopenharmony_ci LE_CloseLoop(g_loopClient_); 348d9f0492fSopenharmony_ci g_loopClient_ = nullptr; 349d9f0492fSopenharmony_ci} 350d9f0492fSopenharmony_ci 351d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestTcpConnect_001, TestSize.Level1) 352d9f0492fSopenharmony_ci{ 353d9f0492fSopenharmony_ci g_cmd = 2; // 2 only close client 354d9f0492fSopenharmony_ci LoopServerUnitTest test; 355d9f0492fSopenharmony_ci TaskHandle tcp = test.CreateConnect(TCP_SERVER.c_str(), TASK_TCP); 356d9f0492fSopenharmony_ci EXPECT_NE(tcp, nullptr); 357d9f0492fSopenharmony_ci SendMessage(g_loopClient_, tcp, FORMAT_STR.c_str(), g_cmd, "connect success"); 358d9f0492fSopenharmony_ci LE_RunLoop(g_loopClient_); 359d9f0492fSopenharmony_ci LE_CloseStreamTask(g_loopClient_, tcp); 360d9f0492fSopenharmony_ci LE_CloseLoop(g_loopClient_); 361d9f0492fSopenharmony_ci g_loopClient_ = nullptr; 362d9f0492fSopenharmony_ci} 363d9f0492fSopenharmony_ci 364d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestTimer_001, TestSize.Level1) 365d9f0492fSopenharmony_ci{ 366d9f0492fSopenharmony_ci LoopServerUnitTest test; 367d9f0492fSopenharmony_ci g_maxCount = 2; // 2 stop 368d9f0492fSopenharmony_ci TimerHandle timer = test.CreateTimerTask(2); 369d9f0492fSopenharmony_ci EXPECT_NE(timer, nullptr); 370d9f0492fSopenharmony_ci LE_RunLoop(g_loopClient_); 371d9f0492fSopenharmony_ci LE_CloseLoop(g_loopClient_); 372d9f0492fSopenharmony_ci g_loopClient_ = nullptr; 373d9f0492fSopenharmony_ci} 374d9f0492fSopenharmony_ci 375d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestTimer_002, TestSize.Level1) 376d9f0492fSopenharmony_ci{ 377d9f0492fSopenharmony_ci LoopServerUnitTest test; 378d9f0492fSopenharmony_ci g_maxCount = 3; // 3 stop timer 379d9f0492fSopenharmony_ci TimerHandle timer = test.CreateTimerTask(3); 380d9f0492fSopenharmony_ci EXPECT_NE(timer, nullptr); 381d9f0492fSopenharmony_ci LE_RunLoop(g_loopClient_); 382d9f0492fSopenharmony_ci LE_CloseLoop(g_loopClient_); 383d9f0492fSopenharmony_ci g_loopClient_ = nullptr; 384d9f0492fSopenharmony_ci} 385d9f0492fSopenharmony_ci 386d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestWatcher_001, TestSize.Level1) 387d9f0492fSopenharmony_ci{ 388d9f0492fSopenharmony_ci int fd = open(WATCHER_FILE.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 389d9f0492fSopenharmony_ci if (fd >= 0) { 390d9f0492fSopenharmony_ci write(fd, WATCHER_FILE.c_str(), WATCHER_FILE.size()); 391d9f0492fSopenharmony_ci } 392d9f0492fSopenharmony_ci EXPECT_GE(fd, 0); 393d9f0492fSopenharmony_ci printf("Watcher fd %d \n", fd); 394d9f0492fSopenharmony_ci LoopServerUnitTest test; 395d9f0492fSopenharmony_ci WatcherHandle watcher = test.CreateWatcherTask(3, WATCHER_FILE.c_str()); 396d9f0492fSopenharmony_ci EXPECT_NE(watcher, nullptr); 397d9f0492fSopenharmony_ci g_maxCount = 10; // 10 write watcher file 398d9f0492fSopenharmony_ci TimerHandle timer = test.CreateTimerTask(1); 399d9f0492fSopenharmony_ci EXPECT_NE(timer, nullptr); 400d9f0492fSopenharmony_ci 401d9f0492fSopenharmony_ci LE_RunLoop(g_loopClient_); 402d9f0492fSopenharmony_ci LE_RemoveWatcher(g_loopClient_, watcher); 403d9f0492fSopenharmony_ci close(fd); 404d9f0492fSopenharmony_ci LE_CloseLoop(g_loopClient_); 405d9f0492fSopenharmony_ci g_loopClient_ = nullptr; 406d9f0492fSopenharmony_ci} 407d9f0492fSopenharmony_ci 408d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestStopServer_001, TestSize.Level1) 409d9f0492fSopenharmony_ci{ 410d9f0492fSopenharmony_ci g_cmd = 5; // 5 close server 411d9f0492fSopenharmony_ci LoopServerUnitTest test; 412d9f0492fSopenharmony_ci TaskHandle pip = test.CreateConnect(PIPE_SERVER.c_str(), TASK_PIPE); 413d9f0492fSopenharmony_ci EXPECT_NE(pip, nullptr); 414d9f0492fSopenharmony_ci SendMessage(g_loopClient_, pip, FORMAT_STR.c_str(), g_cmd, "connect success"); 415d9f0492fSopenharmony_ci LE_RunLoop(g_loopClient_); 416d9f0492fSopenharmony_ci LE_CloseStreamTask(g_loopClient_, pip); 417d9f0492fSopenharmony_ci LE_CloseLoop(g_loopClient_); 418d9f0492fSopenharmony_ci g_loopClient_ = nullptr; 419d9f0492fSopenharmony_ci} 420d9f0492fSopenharmony_ci 421d9f0492fSopenharmony_ciHWTEST_F(LoopServerUnitTest, Init_TestServerTimeout_001, TestSize.Level1) 422d9f0492fSopenharmony_ci{ 423d9f0492fSopenharmony_ci int flag = TASK_STREAM | TASK_PIPE | TASK_SERVER | TASK_TEST; 424d9f0492fSopenharmony_ci int serverSock = CreateSocket(flag, "/data/test1pipe"); 425d9f0492fSopenharmony_ci EXPECT_NE(serverSock, -1); 426d9f0492fSopenharmony_ci int ret = AcceptSocket(serverSock, flag); 427d9f0492fSopenharmony_ci EXPECT_EQ(ret, -1); 428d9f0492fSopenharmony_ci} 429d9f0492fSopenharmony_ci} // namespace init_ut 430