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#include <gtest/gtest.h> 16d9f0492fSopenharmony_ci#include <pthread.h> 17d9f0492fSopenharmony_ci#include <sys/eventfd.h> 18d9f0492fSopenharmony_ci 19d9f0492fSopenharmony_ci#include "begetctl.h" 20d9f0492fSopenharmony_ci#include "init.h" 21d9f0492fSopenharmony_ci#include "init_hashmap.h" 22d9f0492fSopenharmony_ci#include "init_param.h" 23d9f0492fSopenharmony_ci#include "init_utils.h" 24d9f0492fSopenharmony_ci#include "le_epoll.h" 25d9f0492fSopenharmony_ci#include "le_loop.h" 26d9f0492fSopenharmony_ci#include "le_socket.h" 27d9f0492fSopenharmony_ci#include "le_task.h" 28d9f0492fSopenharmony_ci#include "loop_event.h" 29d9f0492fSopenharmony_ci#include "param_manager.h" 30d9f0492fSopenharmony_ci#include "param_message.h" 31d9f0492fSopenharmony_ci#include "param_utils.h" 32d9f0492fSopenharmony_ci#include "trigger_manager.h" 33d9f0492fSopenharmony_ci 34d9f0492fSopenharmony_ciusing namespace testing::ext; 35d9f0492fSopenharmony_ciusing namespace std; 36d9f0492fSopenharmony_ci 37d9f0492fSopenharmony_ciusing HashTab = struct { 38d9f0492fSopenharmony_ci HashNodeCompare nodeCompare; 39d9f0492fSopenharmony_ci HashKeyCompare keyCompare; 40d9f0492fSopenharmony_ci HashNodeFunction nodeHash; 41d9f0492fSopenharmony_ci HashKeyFunction keyHash; 42d9f0492fSopenharmony_ci HashNodeOnFree nodeFree; 43d9f0492fSopenharmony_ci int maxBucket; 44d9f0492fSopenharmony_ci uint32_t tableId; 45d9f0492fSopenharmony_ci HashNode *buckets[0]; 46d9f0492fSopenharmony_ci}; 47d9f0492fSopenharmony_ci 48d9f0492fSopenharmony_cistatic LE_STATUS TestHandleTaskEvent(const LoopHandle loop, const TaskHandle task, uint32_t oper) 49d9f0492fSopenharmony_ci{ 50d9f0492fSopenharmony_ci return LE_SUCCESS; 51d9f0492fSopenharmony_ci} 52d9f0492fSopenharmony_ci 53d9f0492fSopenharmony_cistatic void OnReceiveRequest(const TaskHandle task, const uint8_t *buffer, uint32_t nread) 54d9f0492fSopenharmony_ci{ 55d9f0492fSopenharmony_ci UNUSED(task); 56d9f0492fSopenharmony_ci UNUSED(buffer); 57d9f0492fSopenharmony_ci UNUSED(nread); 58d9f0492fSopenharmony_ci} 59d9f0492fSopenharmony_ci 60d9f0492fSopenharmony_cistatic void ProcessAsyncEvent(const TaskHandle taskHandle, uint64_t eventId, const uint8_t *buffer, uint32_t buffLen) 61d9f0492fSopenharmony_ci{ 62d9f0492fSopenharmony_ci UNUSED(taskHandle); 63d9f0492fSopenharmony_ci UNUSED(eventId); 64d9f0492fSopenharmony_ci UNUSED(buffer); 65d9f0492fSopenharmony_ci UNUSED(buffLen); 66d9f0492fSopenharmony_ci} 67d9f0492fSopenharmony_ci 68d9f0492fSopenharmony_cistatic int IncomingConnect(LoopHandle loop, TaskHandle server) 69d9f0492fSopenharmony_ci{ 70d9f0492fSopenharmony_ci UNUSED(loop); 71d9f0492fSopenharmony_ci UNUSED(server); 72d9f0492fSopenharmony_ci return 0; 73d9f0492fSopenharmony_ci} 74d9f0492fSopenharmony_ci 75d9f0492fSopenharmony_cistatic void ProcessWatchEventTest(WatcherHandle taskHandle, int fd, uint32_t *events, const void *context) 76d9f0492fSopenharmony_ci{ 77d9f0492fSopenharmony_ci UNUSED(taskHandle); 78d9f0492fSopenharmony_ci UNUSED(fd); 79d9f0492fSopenharmony_ci UNUSED(events); 80d9f0492fSopenharmony_ci UNUSED(context); 81d9f0492fSopenharmony_ci} 82d9f0492fSopenharmony_ci 83d9f0492fSopenharmony_cinamespace init_ut { 84d9f0492fSopenharmony_ciclass LoopEventUnittest : public testing::Test { 85d9f0492fSopenharmony_cipublic: 86d9f0492fSopenharmony_ci LoopEventUnittest() {}; 87d9f0492fSopenharmony_ci virtual ~LoopEventUnittest() {}; 88d9f0492fSopenharmony_ci static void SetUpTestCase(void) {}; 89d9f0492fSopenharmony_ci static void TearDownTestCase(void) {}; 90d9f0492fSopenharmony_ci void SetUp() {}; 91d9f0492fSopenharmony_ci void TearDown() {}; 92d9f0492fSopenharmony_ci void TestBody(void) {}; 93d9f0492fSopenharmony_ci int CreateServerTask() 94d9f0492fSopenharmony_ci { 95d9f0492fSopenharmony_ci CheckTaskFlags(nullptr, EVENT_WRITE); 96d9f0492fSopenharmony_ci ParamStreamInfo info = {}; 97d9f0492fSopenharmony_ci info.server = const_cast<char *>(PIPE_NAME); 98d9f0492fSopenharmony_ci info.close = nullptr; 99d9f0492fSopenharmony_ci info.recvMessage = nullptr; 100d9f0492fSopenharmony_ci info.incomingConnect = OnIncomingConnect; 101d9f0492fSopenharmony_ci return ParamServerCreate(&serverTask_, &info); 102d9f0492fSopenharmony_ci } 103d9f0492fSopenharmony_ci 104d9f0492fSopenharmony_ci void StreamTaskTest () 105d9f0492fSopenharmony_ci { 106d9f0492fSopenharmony_ci LE_StreamInfo streamInfo = {}; 107d9f0492fSopenharmony_ci streamInfo.recvMessage = OnReceiveRequest; 108d9f0492fSopenharmony_ci streamInfo.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_CONNECT | TASK_TEST; 109d9f0492fSopenharmony_ci streamInfo.server = (char *)"/data/testpipea"; 110d9f0492fSopenharmony_ci TaskHandle clientTaskHandle = nullptr; 111d9f0492fSopenharmony_ci LE_AcceptStreamClient(LE_GetDefaultLoop(), (TaskHandle)serverTask_, &clientTaskHandle, &streamInfo); 112d9f0492fSopenharmony_ci if (clientTaskHandle == nullptr) { 113d9f0492fSopenharmony_ci return; 114d9f0492fSopenharmony_ci } 115d9f0492fSopenharmony_ci ((StreamConnectTask *)clientTaskHandle)->stream.base.handleEvent(LE_GetDefaultLoop(), 116d9f0492fSopenharmony_ci (TaskHandle)clientTaskHandle, 0); 117d9f0492fSopenharmony_ci ((StreamConnectTask *)clientTaskHandle)->stream.base.handleEvent(LE_GetDefaultLoop(), 118d9f0492fSopenharmony_ci (TaskHandle)clientTaskHandle, EVENT_READ); 119d9f0492fSopenharmony_ci 120d9f0492fSopenharmony_ci TaskHandle clientTaskHandlec = nullptr; 121d9f0492fSopenharmony_ci streamInfo.baseInfo.flags = TASK_STREAM | TASK_TCP | TASK_SERVER; 122d9f0492fSopenharmony_ci streamInfo.server = (char *)"0.0.0.0:10110"; 123d9f0492fSopenharmony_ci LE_CreateStreamClient(LE_GetDefaultLoop(), &clientTaskHandlec, &streamInfo); 124d9f0492fSopenharmony_ci if (clientTaskHandlec == nullptr) { 125d9f0492fSopenharmony_ci return; 126d9f0492fSopenharmony_ci } 127d9f0492fSopenharmony_ci TaskHandle clientTaskHandled = nullptr; 128d9f0492fSopenharmony_ci streamInfo.baseInfo.flags = TASK_STREAM | TASK_TCP | TASK_CONNECT; 129d9f0492fSopenharmony_ci streamInfo.server = (char *)"127.0.0.1:10111"; 130d9f0492fSopenharmony_ci LE_CreateStreamClient(LE_GetDefaultLoop(), &clientTaskHandled, &streamInfo); 131d9f0492fSopenharmony_ci if (clientTaskHandled == nullptr) { 132d9f0492fSopenharmony_ci return; 133d9f0492fSopenharmony_ci } 134d9f0492fSopenharmony_ci } 135d9f0492fSopenharmony_ci void LeTaskTest() 136d9f0492fSopenharmony_ci { 137d9f0492fSopenharmony_ci LE_StreamServerInfo info = {}; 138d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_SERVER | TASK_TEST; 139d9f0492fSopenharmony_ci info.server = (char *)"/data/testpipe"; 140d9f0492fSopenharmony_ci info.baseInfo.close = nullptr; 141d9f0492fSopenharmony_ci info.incommingConnect = IncomingConnect; 142d9f0492fSopenharmony_ci LE_CreateStreamServer(LE_GetDefaultLoop(), &serverTask_, &info); 143d9f0492fSopenharmony_ci if (serverTask_ == nullptr) { 144d9f0492fSopenharmony_ci return; 145d9f0492fSopenharmony_ci } 146d9f0492fSopenharmony_ci ((StreamServerTask *)serverTask_)->base.handleEvent(LE_GetDefaultLoop(), serverTask_, EVENT_READ); 147d9f0492fSopenharmony_ci 148d9f0492fSopenharmony_ci uint64_t eventId = 0; 149d9f0492fSopenharmony_ci ParamStreamInfo paramStreamInfo = {}; 150d9f0492fSopenharmony_ci paramStreamInfo.flags = PARAM_TEST_FLAGS; 151d9f0492fSopenharmony_ci paramStreamInfo.server = nullptr; 152d9f0492fSopenharmony_ci paramStreamInfo.close = nullptr; 153d9f0492fSopenharmony_ci paramStreamInfo.recvMessage = ProcessMessage; 154d9f0492fSopenharmony_ci paramStreamInfo.incomingConnect = nullptr; 155d9f0492fSopenharmony_ci ParamTaskPtr client = nullptr; 156d9f0492fSopenharmony_ci int ret = ParamStreamCreate(&client, serverTask_, ¶mStreamInfo, sizeof(ParamWatcher)); 157d9f0492fSopenharmony_ci PARAM_CHECK(ret == 0, return, "Failed to create client"); 158d9f0492fSopenharmony_ci 159d9f0492fSopenharmony_ci BufferHandle handle = LE_CreateBuffer(LE_GetDefaultLoop(), 1 + sizeof(eventId)); 160d9f0492fSopenharmony_ci LE_Buffer *buffer = (LE_Buffer *)handle; 161d9f0492fSopenharmony_ci AddBuffer((StreamTask *)client, buffer); 162d9f0492fSopenharmony_ci ((StreamConnectTask *)client)->stream.base.handleEvent(LE_GetDefaultLoop(), (TaskHandle)(client), EVENT_WRITE); 163d9f0492fSopenharmony_ci EXPECT_NE(LE_GetSendResult(handle), 0); 164d9f0492fSopenharmony_ci 165d9f0492fSopenharmony_ci ParamMessage *request = (ParamMessage *)CreateParamMessage(MSG_SET_PARAM, "name", sizeof(ParamMessage)); 166d9f0492fSopenharmony_ci ((StreamConnectTask *)client)->recvMessage(LE_GetDefaultLoop(), reinterpret_cast<uint8_t *>(request), 167d9f0492fSopenharmony_ci sizeof(ParamMessage)); 168d9f0492fSopenharmony_ci 169d9f0492fSopenharmony_ci LE_Buffer *next = nullptr; 170d9f0492fSopenharmony_ci EXPECT_EQ(GetNextBuffer((StreamTask *)client, next), nullptr); 171d9f0492fSopenharmony_ci ParamWatcher *watcher = (ParamWatcher *)ParamGetTaskUserData(client); 172d9f0492fSopenharmony_ci PARAM_CHECK(watcher != nullptr, return, "Failed to get watcher"); 173d9f0492fSopenharmony_ci OH_ListInit(&watcher->triggerHead); 174d9f0492fSopenharmony_ci LE_FreeBuffer(LE_GetDefaultLoop(), (TaskHandle)client, nullptr); 175d9f0492fSopenharmony_ci return; 176d9f0492fSopenharmony_ci } 177d9f0492fSopenharmony_ci void ProcessEventTest() 178d9f0492fSopenharmony_ci { 179d9f0492fSopenharmony_ci ProcessEvent((EventLoop *)LE_GetDefaultLoop(), 1, EVENT_READ); 180d9f0492fSopenharmony_ci LE_BaseInfo info = {TASK_EVENT, nullptr}; 181d9f0492fSopenharmony_ci int testfd = 65535; // 65535 is not exist fd 182d9f0492fSopenharmony_ci BaseTask *task = CreateTask(LE_GetDefaultLoop(), testfd, &info, sizeof(StreamClientTask)); 183d9f0492fSopenharmony_ci if (task != nullptr) { 184d9f0492fSopenharmony_ci task->handleEvent = TestHandleTaskEvent; 185d9f0492fSopenharmony_ci ProcessEvent((EventLoop *)LE_GetDefaultLoop(), testfd, EVENT_READ); 186d9f0492fSopenharmony_ci } 187d9f0492fSopenharmony_ci } 188d9f0492fSopenharmony_ci void ProcessasynEvent() 189d9f0492fSopenharmony_ci { 190d9f0492fSopenharmony_ci TaskHandle asynHandle = nullptr; 191d9f0492fSopenharmony_ci LE_CreateAsyncTask(LE_GetDefaultLoop(), &asynHandle, ProcessAsyncEvent); 192d9f0492fSopenharmony_ci if (asynHandle == nullptr) { 193d9f0492fSopenharmony_ci return; 194d9f0492fSopenharmony_ci } 195d9f0492fSopenharmony_ci ((AsyncEventTask *)asynHandle)->stream.base.handleEvent(LE_GetDefaultLoop(), asynHandle, EVENT_READ); 196d9f0492fSopenharmony_ci ((AsyncEventTask *)asynHandle)->stream.base.handleEvent(LE_GetDefaultLoop(), asynHandle, EVENT_WRITE); 197d9f0492fSopenharmony_ci LE_StopAsyncTask(LE_GetDefaultLoop(), asynHandle); 198d9f0492fSopenharmony_ci } 199d9f0492fSopenharmony_ci void ProcessWatcherTask() 200d9f0492fSopenharmony_ci { 201d9f0492fSopenharmony_ci WatcherHandle handle = nullptr; 202d9f0492fSopenharmony_ci LE_WatchInfo info = {}; 203d9f0492fSopenharmony_ci info.fd = -1; 204d9f0492fSopenharmony_ci info.flags = WATCHER_ONCE; 205d9f0492fSopenharmony_ci info.events = EVENT_READ; 206d9f0492fSopenharmony_ci info.processEvent = ProcessWatchEventTest; 207d9f0492fSopenharmony_ci LE_StartWatcher(LE_GetDefaultLoop(), &handle, &info, nullptr); 208d9f0492fSopenharmony_ci if (handle == nullptr) { 209d9f0492fSopenharmony_ci return; 210d9f0492fSopenharmony_ci } 211d9f0492fSopenharmony_ci ((WatcherTask *)handle)->base.handleEvent(LE_GetDefaultLoop(), (TaskHandle)handle, EVENT_READ); 212d9f0492fSopenharmony_ci ((WatcherTask *)handle)->base.handleEvent(LE_GetDefaultLoop(), (TaskHandle)handle, 0); 213d9f0492fSopenharmony_ci ((WatcherTask *)handle)->base.flags = WATCHER_ONCE; 214d9f0492fSopenharmony_ci ((WatcherTask *)handle)->base.handleEvent(LE_GetDefaultLoop(), (TaskHandle)handle, EVENT_READ); 215d9f0492fSopenharmony_ci LE_RemoveWatcher(LE_GetDefaultLoop(), handle); 216d9f0492fSopenharmony_ci } 217d9f0492fSopenharmony_ci void CreateSocketTest() 218d9f0492fSopenharmony_ci { 219d9f0492fSopenharmony_ci ParamTaskPtr serverTask = nullptr; 220d9f0492fSopenharmony_ci LE_StreamServerInfo info = {}; 221d9f0492fSopenharmony_ci info.baseInfo.flags = TASK_PIPE | TASK_CONNECT | TASK_TEST; 222d9f0492fSopenharmony_ci info.server = (char *)"/data/testpipe"; 223d9f0492fSopenharmony_ci info.baseInfo.close = nullptr; 224d9f0492fSopenharmony_ci info.incommingConnect = IncomingConnect; 225d9f0492fSopenharmony_ci info.socketId = 1111; // 1111 is test fd 226d9f0492fSopenharmony_ci LE_CreateStreamServer(LE_GetDefaultLoop(), &serverTask, &info); 227d9f0492fSopenharmony_ci EXPECT_NE(serverTask, nullptr); 228d9f0492fSopenharmony_ci if (serverTask == nullptr) { 229d9f0492fSopenharmony_ci return; 230d9f0492fSopenharmony_ci } 231d9f0492fSopenharmony_ci ((StreamServerTask *)serverTask)->base.taskId.fd = -1; 232d9f0492fSopenharmony_ci OnIncomingConnect(LE_GetDefaultLoop(), serverTask); 233d9f0492fSopenharmony_ci LE_GetSocketFd(serverTask); 234d9f0492fSopenharmony_ci AcceptSocket(-1, TASK_PIPE); 235d9f0492fSopenharmony_ci AcceptSocket(-1, TASK_TCP); 236d9f0492fSopenharmony_ci AcceptSocket(-1, TASK_TEST); 237d9f0492fSopenharmony_ci } 238d9f0492fSopenharmony_ci 239d9f0492fSopenharmony_ciprivate: 240d9f0492fSopenharmony_ci ParamTaskPtr serverTask_ = nullptr; 241d9f0492fSopenharmony_ci}; 242d9f0492fSopenharmony_ci 243d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_LeTaskTest_001, TestSize.Level1) 244d9f0492fSopenharmony_ci{ 245d9f0492fSopenharmony_ci LoopEventUnittest loopevtest = LoopEventUnittest(); 246d9f0492fSopenharmony_ci loopevtest.LeTaskTest(); 247d9f0492fSopenharmony_ci} 248d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestRunServer_001, TestSize.Level1) 249d9f0492fSopenharmony_ci{ 250d9f0492fSopenharmony_ci LoopEventUnittest loopevtest = LoopEventUnittest(); 251d9f0492fSopenharmony_ci loopevtest.ProcessEventTest(); 252d9f0492fSopenharmony_ci} 253d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestProcessasynEvent_001, TestSize.Level1) 254d9f0492fSopenharmony_ci{ 255d9f0492fSopenharmony_ci LoopEventUnittest loopevtest = LoopEventUnittest(); 256d9f0492fSopenharmony_ci loopevtest.ProcessasynEvent(); 257d9f0492fSopenharmony_ci} 258d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestCreateSocketTest_001, TestSize.Level1) 259d9f0492fSopenharmony_ci{ 260d9f0492fSopenharmony_ci LoopEventUnittest loopevtest = LoopEventUnittest(); 261d9f0492fSopenharmony_ci loopevtest.CreateSocketTest(); 262d9f0492fSopenharmony_ci} 263d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestProcessWatcherTask_001, TestSize.Level1) 264d9f0492fSopenharmony_ci{ 265d9f0492fSopenharmony_ci LoopEventUnittest loopevtest = LoopEventUnittest(); 266d9f0492fSopenharmony_ci loopevtest.ProcessWatcherTask(); 267d9f0492fSopenharmony_ci} 268d9f0492fSopenharmony_ci 269d9f0492fSopenharmony_cistatic LoopHandle g_loop = nullptr; 270d9f0492fSopenharmony_cistatic int g_timeCount = 0; 271d9f0492fSopenharmony_cistatic void Test_ProcessTimer(const TimerHandle taskHandle, void *context) 272d9f0492fSopenharmony_ci{ 273d9f0492fSopenharmony_ci g_timeCount++; 274d9f0492fSopenharmony_ci printf("Test_ProcessTimer %d\n", g_timeCount); 275d9f0492fSopenharmony_ci if (g_timeCount > 1) { 276d9f0492fSopenharmony_ci LE_StopLoop(g_loop); 277d9f0492fSopenharmony_ci } 278d9f0492fSopenharmony_ci} 279d9f0492fSopenharmony_ci 280d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestLoopAbnormal_001, TestSize.Level1) 281d9f0492fSopenharmony_ci{ 282d9f0492fSopenharmony_ci LE_StartWatcher(nullptr, nullptr, nullptr, nullptr); 283d9f0492fSopenharmony_ci LE_WatchInfo info = {}; 284d9f0492fSopenharmony_ci info.fd = -1; 285d9f0492fSopenharmony_ci info.flags = WATCHER_ONCE; 286d9f0492fSopenharmony_ci info.events = EVENT_READ; 287d9f0492fSopenharmony_ci info.processEvent = nullptr; 288d9f0492fSopenharmony_ci LE_StartWatcher(LE_GetDefaultLoop(), nullptr, &info, nullptr); 289d9f0492fSopenharmony_ci LE_StartWatcher(LE_GetDefaultLoop(), nullptr, nullptr, nullptr); 290d9f0492fSopenharmony_ci} 291d9f0492fSopenharmony_ci 292d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestLoopIdle_001, TestSize.Level1) 293d9f0492fSopenharmony_ci{ 294d9f0492fSopenharmony_ci int ret = LE_DelayProc(LE_GetDefaultLoop(), nullptr, nullptr); 295d9f0492fSopenharmony_ci ASSERT_NE(ret, 0); 296d9f0492fSopenharmony_ci LE_DelIdle(nullptr); 297d9f0492fSopenharmony_ci} 298d9f0492fSopenharmony_ci 299d9f0492fSopenharmony_ciHWTEST_F(LoopEventUnittest, Init_TestLEFreeBuffer_001, TestSize.Level1) 300d9f0492fSopenharmony_ci{ 301d9f0492fSopenharmony_ci uint64_t eventId = 0; 302d9f0492fSopenharmony_ci BufferHandle handle = LE_CreateBuffer(LE_GetDefaultLoop(), 1 + sizeof(eventId)); 303d9f0492fSopenharmony_ci ASSERT_NE(handle, nullptr); 304d9f0492fSopenharmony_ci LE_FreeBuffer(LE_GetDefaultLoop(), nullptr, handle); 305d9f0492fSopenharmony_ci} 306d9f0492fSopenharmony_ci} // namespace init_ut 307