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