1 /*
2 * Copyright (c) 2024 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
16 #define private public
17 #include "epoll_manager_test.h"
18
19 #include <sys/timerfd.h>
20 #include <unistd.h>
21
22 #include "devicestatus_common.h"
23 #include "fi_log.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "EpollManagerTest"
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 using namespace testing::ext;
32 namespace {
33 constexpr int32_t MAX_N_EVENTS { 64 };
34 constexpr int32_t TIMEOUT { 30 };
35 constexpr int32_t BLOCK_EPOLL { -1 };
36 constexpr int32_t UNBLOCK_EPOLL { 0 };
37 constexpr int32_t TIME_WAIT_FOR_OP_MS { 1001 };
38 constexpr int32_t DISPATCH_TIMES { 5 };
39 constexpr int32_t EXPIRE_TIME { 2 };
40 } // namespace
41
SetUpTestCase()42 void EpollManagerTest::SetUpTestCase() {}
43
TearDownTestCase()44 void EpollManagerTest::TearDownTestCase() {}
45
MonitorEvent()46 MonitorEvent::MonitorEvent()
47 {
48 inotifyFd_ = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
49 if (inotifyFd_ < 0) {
50 FI_HILOGE("timerfd_create failed");
51 }
52 }
53
~MonitorEvent()54 MonitorEvent::~MonitorEvent()
55 {
56 if (inotifyFd_ < 0) {
57 FI_HILOGE("Invalid timerFd_");
58 return;
59 }
60 if (close(inotifyFd_) < 0) {
61 FI_HILOGE("Close timer fd failed, error:%{public}s, timerFd_:%{public}d", strerror(errno), inotifyFd_);
62 }
63 inotifyFd_ = -1;
64 }
65
Dispatch(const struct epoll_event & ev)66 void MonitorEvent::Dispatch(const struct epoll_event &ev)
67 {
68 CALL_DEBUG_ENTER;
69 if (inotifyFd_ < 0) {
70 FI_HILOGE("inotifyFd_ is invalid.");
71 return;
72 }
73 if ((ev.events & EPOLLIN) == EPOLLIN) {
74 int32_t buf[EXPIRE_TIME] = { 0 };
75 int32_t ret = read(inotifyFd_, buf, sizeof(buf));
76 if (ret < 0) {
77 FI_HILOGE("epoll callback read error");
78 } else {
79 FI_HILOGI("Epoll input, buf[0]: %{public}d, buf[1]: %{public}d", buf[0], buf[1]);
80 }
81 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
82 FI_HILOGE("Epoll hangup, errno:%{public}s", strerror(errno));
83 }
84 }
85
SetTimer()86 int32_t MonitorEvent::SetTimer()
87 {
88 if (inotifyFd_ < 0) {
89 FI_HILOGE("TimerManager is not initialized");
90 return RET_ERR;
91 }
92 struct itimerspec tspec {};
93 tspec.it_interval = {1, 0}; // period timeout value = 1s
94 tspec.it_value = {1, 0}; // initial timeout value = 1s0ns
95 if (timerfd_settime(inotifyFd_, 0, &tspec, NULL) != 0) {
96 FI_HILOGE("Timer: the inotifyFd_ is error");
97 return RET_ERR;
98 }
99 return RET_OK;
100 }
101
SetUp()102 void EpollManagerTest::SetUp() {}
103
TearDown()104 void EpollManagerTest::TearDown() {}
105
106 /**
107 * @tc.name: EpollManagerTest_Open001
108 * @tc.desc: Test Open, open and close once
109 * @tc.type: FUNC
110 */
111 HWTEST_F(EpollManagerTest, EpollManagerTest_Open001, TestSize.Level1)
112 {
113 CALL_TEST_DEBUG;
114 EpollManager epollMgr;
115 ASSERT_TRUE(epollMgr.Open());
116
117 auto monitor = std::make_shared<MonitorEvent>();
118 ASSERT_TRUE(epollMgr.Add(monitor));
119 }
120
121 /**
122 * @tc.name: EpollManagerTest_Open002
123 * @tc.desc: Test Open, open and close twice
124 * @tc.type: FUNC
125 */
126 HWTEST_F(EpollManagerTest, EpollManagerTest_Open002, TestSize.Level1)
127 {
128 CALL_TEST_DEBUG;
129 EpollManager epollMgr;
130 ASSERT_TRUE(epollMgr.Open());
131 epollMgr.Close();
132
133 EXPECT_TRUE(epollMgr.Open());
134 epollMgr.Close();
135 }
136
137 /**
138 * @tc.name: EpollManagerTest_Close001
139 * @tc.desc: Test Close
140 * @tc.type: FUNC
141 */
142 HWTEST_F(EpollManagerTest, EpollManagerTest_Close001, TestSize.Level1)
143 {
144 CALL_TEST_DEBUG;
145 EpollManager epollMgr;
146 epollMgr.Close();
147 EXPECT_TRUE(epollMgr.Open());
148 epollMgr.Close();
149 }
150
151 /**
152 * @tc.name: EpollManagerTest_GetFd001
153 * @tc.desc: Test GetFd
154 * @tc.type: FUNC
155 */
156 HWTEST_F(EpollManagerTest, EpollManagerTest_GetFd001, TestSize.Level1)
157 {
158 CALL_TEST_DEBUG;
159 EpollManager epollMgr;
160 EXPECT_LT(epollMgr.GetFd(), 0);
161 EXPECT_TRUE(epollMgr.Open());
162 EXPECT_GE(epollMgr.GetFd(), 0);
163 }
164
165 /**
166 * @tc.name: EpollManagerTest_Add001
167 * @tc.desc: Test Add, Add duplicate events
168 * @tc.type: FUNC
169 */
170 HWTEST_F(EpollManagerTest, EpollManagerTest_Add001, TestSize.Level1)
171 {
172 CALL_TEST_DEBUG;
173 EpollManager epollMgr;
174 ASSERT_TRUE(epollMgr.Open());
175
176 auto monitor = std::make_shared<MonitorEvent>();
177 ASSERT_TRUE(epollMgr.Add(monitor));
178 EXPECT_TRUE(epollMgr.Add(monitor));
179 }
180
181 /**
182 * @tc.name: EpollManagerTest_Add002
183 * @tc.desc: Test Add, Add the event with fd of -1
184 * @tc.type: FUNC
185 */
186 HWTEST_F(EpollManagerTest, EpollManagerTest_Add002, TestSize.Level1)
187 {
188 CALL_TEST_DEBUG;
189 EpollManager epollMgr;
190 ASSERT_TRUE(epollMgr.Open());
191
192 auto monitor = std::make_shared<MonitorEvent>();
193 auto fd = monitor->GetFd();
194 monitor->inotifyFd_ = -1;
195 EXPECT_FALSE(epollMgr.Add(monitor));
196 monitor->inotifyFd_ = fd;
197 }
198
199 /**
200 * @tc.name: EpollManagerTest_Remove001
201 * @tc.desc: Test Remove, remove existing events
202 * @tc.type: FUNC
203 */
204 HWTEST_F(EpollManagerTest, EpollManagerTest_Remove001, TestSize.Level1)
205 {
206 CALL_TEST_DEBUG;
207 EpollManager epollMgr;
208 ASSERT_TRUE(epollMgr.Open());
209
210 auto monitor = std::make_shared<MonitorEvent>();
211 ASSERT_TRUE(epollMgr.Add(monitor));
212 epollMgr.Remove(monitor);
213 }
214
215 /**
216 * @tc.name: EpollManagerTest_Remove002
217 * @tc.desc: Test Remove, remove events that have not been added
218 * @tc.type: FUNC
219 */
220 HWTEST_F(EpollManagerTest, EpollManagerTest_Remove002, TestSize.Level1)
221 {
222 CALL_TEST_DEBUG;
223 EpollManager epollMgr;
224 ASSERT_TRUE(epollMgr.Open());
225
226 auto monitor = std::make_shared<MonitorEvent>();
227 epollMgr.Remove(monitor);
228 }
229
230 /**
231 * @tc.name: EpollManagerTest_Remove003
232 * @tc.desc: Test Remove, remove the event with fd of -1
233 * @tc.type: FUNC
234 */
235 HWTEST_F(EpollManagerTest, EpollManagerTest_Remove003, TestSize.Level1)
236 {
237 CALL_TEST_DEBUG;
238 EpollManager epollMgr;
239 ASSERT_TRUE(epollMgr.Open());
240
241 auto monitor = std::make_shared<MonitorEvent>();
242 monitor->inotifyFd_ = -1;
243 epollMgr.Remove(monitor);
244 }
245
246 /**
247 * @tc.name: EpollManagerTest_Update001
248 * @tc.desc: Test Update
249 * @tc.type: FUNC
250 */
251 HWTEST_F(EpollManagerTest, EpollManagerTest_Update001, TestSize.Level1)
252 {
253 CALL_TEST_DEBUG;
254 EpollManager epollMgr;
255 ASSERT_TRUE(epollMgr.Open());
256
257 auto monitor = std::make_shared<MonitorEvent>();
258 ASSERT_TRUE(epollMgr.Add(monitor));
259 EXPECT_TRUE(epollMgr.Update(monitor));
260 }
261
262 /**
263 * @tc.name: EpollManagerTest_Update002
264 * @tc.desc: Test Update, modify a non-existent event
265 * @tc.type: FUNC
266 */
267 HWTEST_F(EpollManagerTest, EpollManagerTest_Update002, TestSize.Level1)
268 {
269 CALL_TEST_DEBUG;
270 EpollManager epollMgr;
271 ASSERT_TRUE(epollMgr.Open());
272
273 auto monitor = std::make_shared<MonitorEvent>();
274 EXPECT_FALSE(epollMgr.Update(monitor));
275 }
276
277 /**
278 * @tc.name: EpollManagerTest_WaitTimeout001
279 * @tc.desc: Test WaitTimeout
280 * @tc.type: FUNC
281 */
282 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout001, TestSize.Level1)
283 {
284 CALL_TEST_DEBUG;
285 EpollManager epollMgr;
286 ASSERT_TRUE(epollMgr.Open());
287
288 auto monitor = std::make_shared<MonitorEvent>();
289 ASSERT_TRUE(epollMgr.Add(monitor));
290
291 struct epoll_event evs[MAX_N_EVENTS] {};
292 EXPECT_GE(epollMgr.WaitTimeout(evs, MAX_N_EVENTS, TIMEOUT), 0);
293 }
294
295 /**
296 * @tc.name: EpollManagerTest_WaitTimeout002
297 * @tc.desc: Test WaitTimeout, no event to wait
298 * @tc.type: FUNC
299 */
300 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout002, TestSize.Level1)
301 {
302 CALL_TEST_DEBUG;
303 EpollManager epollMgr;
304 ASSERT_TRUE(epollMgr.Open());
305
306 struct epoll_event evs[MAX_N_EVENTS];
307 EXPECT_GE(epollMgr.WaitTimeout(evs, MAX_N_EVENTS, TIMEOUT), 0);
308 }
309
310 /**
311 * @tc.name: EpollManagerTest_WaitTimeout003
312 * @tc.desc: Test WaitTimeout, use of block mode
313 * @tc.type: FUNC
314 */
315 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout003, TestSize.Level1)
316 {
317 CALL_TEST_DEBUG;
318 EpollManager epollMgr;
319 ASSERT_TRUE(epollMgr.Open());
320
321 auto monitor = std::make_shared<MonitorEvent>();
322 EXPECT_EQ(monitor->SetTimer(), RET_OK);
323 ASSERT_TRUE(epollMgr.Add(monitor));
324
325 struct epoll_event evs[MAX_N_EVENTS];
326 EXPECT_GE(epollMgr.WaitTimeout(evs, MAX_N_EVENTS, BLOCK_EPOLL), 0);
327 }
328
329 /**
330 * @tc.name: EpollManagerTest_WaitTimeout004
331 * @tc.desc: Test WaitTimeout, use of non blocking mode
332 * @tc.type: FUNC
333 */
334 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout004, TestSize.Level1)
335 {
336 CALL_TEST_DEBUG;
337 EpollManager epollMgr;
338 ASSERT_TRUE(epollMgr.Open());
339
340 auto monitor = std::make_shared<MonitorEvent>();
341 ASSERT_TRUE(epollMgr.Add(monitor));
342
343 struct epoll_event evs[MAX_N_EVENTS];
344 EXPECT_GE(epollMgr.WaitTimeout(evs, MAX_N_EVENTS, UNBLOCK_EPOLL), 0);
345 }
346
347 /**
348 * @tc.name: EpollManagerTest_WaitTimeout005
349 * @tc.desc: Test WaitTimeout, Abnormal scenarios with small memory and multiple events
350 * @tc.type: FUNC
351 */
352 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout005, TestSize.Level1)
353 {
354 CALL_TEST_DEBUG;
355 EpollManager epollMgr;
356 ASSERT_TRUE(epollMgr.Open());
357
358 auto monitor = std::make_shared<MonitorEvent>();
359 ASSERT_TRUE(epollMgr.Add(monitor));
360
361 struct epoll_event evs[MAX_N_EVENTS];
362 EXPECT_GE(epollMgr.WaitTimeout(evs, MAX_N_EVENTS, TIMEOUT), 0);
363 }
364
365 /**
366 * @tc.name: EpollManagerTest_WaitTimeout006
367 * @tc.desc: Test WaitTimeout, Abnormal scenarios with big memory and zero events
368 * @tc.type: FUNC
369 */
370 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout006, TestSize.Level1)
371 {
372 CALL_TEST_DEBUG;
373 EpollManager epollMgr;
374 ASSERT_TRUE(epollMgr.Open());
375
376 auto monitor = std::make_shared<MonitorEvent>();
377 ASSERT_TRUE(epollMgr.Add(monitor));
378
379 struct epoll_event evs[MAX_N_EVENTS];
380 EXPECT_LT(epollMgr.WaitTimeout(evs, 0, TIMEOUT), 0);
381 }
382
383 /**
384 * @tc.name: EpollManagerTest_Wait001
385 * @tc.desc: Test Wait
386 * @tc.type: FUNC
387 */
388 HWTEST_F(EpollManagerTest, EpollManagerTest_Wait001, TestSize.Level1)
389 {
390 CALL_TEST_DEBUG;
391 EpollManager epollMgr;
392 ASSERT_TRUE(epollMgr.Open());
393
394 auto monitor = std::make_shared<MonitorEvent>();
395 ASSERT_EQ(monitor->SetTimer(), RET_OK);
396 ASSERT_TRUE(epollMgr.Add(monitor));
397
398 struct epoll_event evs[MAX_N_EVENTS];
399 EXPECT_GE(epollMgr.Wait(evs, MAX_N_EVENTS), 0);
400 }
401
402 /**
403 * @tc.name: EpollManagerTest_Dispatch001
404 * @tc.desc: Test Dispatch, dispatch when there is event input
405 * @tc.type: FUNC
406 */
407 HWTEST_F(EpollManagerTest, EpollManagerTest_Dispatch001, TestSize.Level1)
408 {
409 CALL_TEST_DEBUG;
410 EpollManager epollMgr;
411 ASSERT_TRUE(epollMgr.Open());
412
413 auto monitor = std::make_shared<MonitorEvent>();
414 ASSERT_TRUE(epollMgr.Add(monitor));
415
416 struct epoll_event ev {};
417 ev.events = EPOLLIN;
418 ev.data.ptr = &epollMgr;
419
420 epollMgr.Dispatch(ev);
421 }
422
423 /**
424 * @tc.name: EpollManagerTest_Dispatch002
425 * @tc.desc: Test Dispatch, dispatch when there are errors
426 * @tc.type: FUNC
427 */
428 HWTEST_F(EpollManagerTest, EpollManagerTest_Dispatch002, TestSize.Level1)
429 {
430 CALL_TEST_DEBUG;
431 EpollManager epollMgr;
432 ASSERT_TRUE(epollMgr.Open());
433
434 auto monitor = std::make_shared<MonitorEvent>();
435 ASSERT_TRUE(epollMgr.Add(monitor));
436
437 struct epoll_event ev {};
438 ev.events = EPOLLERR;
439 ev.data.ptr = &epollMgr;
440
441 epollMgr.Dispatch(ev);
442 }
443
444 /**
445 * @tc.name: EpollManagerTest_Dispatch003
446 * @tc.desc: Test Dispatch, dispatch when there is event input
447 * @tc.type: FUNC
448 */
449 HWTEST_F(EpollManagerTest, EpollManagerTest_Dispatch003, TestSize.Level1)
450 {
451 CALL_TEST_DEBUG;
452 EpollManager epollMgr;
453 ASSERT_TRUE(epollMgr.Open());
454
455 auto monitor = std::make_shared<MonitorEvent>();
456 EXPECT_EQ(monitor->SetTimer(), RET_OK);
457 ASSERT_TRUE(epollMgr.Add(monitor));
458
459 struct epoll_event ev {};
460 ev.events = EPOLLIN;
461 ev.data.ptr = monitor.get();
462 ev.data.fd = monitor->GetFd();
463
464 for (int32_t i = 0; i < DISPATCH_TIMES; ++i) {
465 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
466 epollMgr.Dispatch(ev);
467 }
468 }
469 } // namespace DeviceStatus
470 } // namespace Msdp
471 } // namespace OHOS