1 /*
2 * Copyright (c) 2025 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 #include <string>
17 #include <vector>
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "eventfd_mock.h"
23 #include "filemgmt_libhilog.h"
24 #include "fs_file_watcher.h"
25 #include "inotify_mock.h"
26 #include "mock_watcher_callback.h"
27 #include "poll_mock.h"
28 #include "securec.h"
29 #include "unistd_mock.h"
30
31 namespace OHOS {
32 namespace FileManagement {
33 namespace ModuleFileIO {
34 namespace Test {
35
36 class FsFileWatcherMockTest : public testing::Test {
37 public:
38 static void SetUpTestCase(void);
39 static void TearDownTestCase(void);
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase(void)44 void FsFileWatcherMockTest::SetUpTestCase(void)
45 {
46 GTEST_LOG_(INFO) << "SetUpTestCase";
47 EventfdMock::EnableMock();
48 InotifyMock::EnableMock();
49 PollMock::EnableMock();
50 UnistdMock::EnableMock();
51 }
52
TearDownTestCase(void)53 void FsFileWatcherMockTest::TearDownTestCase(void)
54 {
55 EventfdMock::DisableMock();
56 InotifyMock::DisableMock();
57 PollMock::DisableMock();
58 UnistdMock::DisableMock();
59 GTEST_LOG_(INFO) << "TearDownTestCase";
60 }
61
SetUp(void)62 void FsFileWatcherMockTest::SetUp(void)
63 {
64 GTEST_LOG_(INFO) << "SetUp";
65 errno = 0; // Reset errno
66 }
67
TearDown(void)68 void FsFileWatcherMockTest::TearDown(void)
69 {
70 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
71 watcher.taskRunning_ = false;
72 watcher.run_ = false;
73 watcher.reading_ = false;
74 watcher.closed_ = false;
75 watcher.notifyFd_ = -1;
76 watcher.eventFd_ = -1;
77 watcher.dataCache_.ClearCache();
78 GTEST_LOG_(INFO) << "TearDown";
79 }
80
81 inline const int32_t EXPECTED_WD = 100;
82 inline const int32_t UNEXPECTED_WD = 200;
83
84 /**
85 * @tc.name: FsFileWatcherMockTest_GetNotifyId_001
86 * @tc.desc: Test function of FsFileWatcher::GetNotifyId interface for FAILED.
87 * @tc.size: SMALL
88 * @tc.type: FUNC
89 * @tc.level Level 0
90 */
91 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyId_001, testing::ext::TestSize.Level0)
92 {
93 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyId_001";
94 // Prepare test condition
95 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
96 int32_t expected = -1;
97 // Do testing
98 int32_t result = watcher.GetNotifyId();
99 // Verify results
100 EXPECT_EQ(result, expected);
101 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyId_001";
102 }
103
104 /**
105 * @tc.name: FsFileWatcherMockTest_InitNotify_001
106 * @tc.desc: Test function of FsFileWatcher::InitNotify interface for SUCCESS.
107 * @tc.size: MEDIUM
108 * @tc.type: FUNC
109 * @tc.level Level 0
110 */
111 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_001, testing::ext::TestSize.Level0)
112 {
113 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_001";
114 int32_t eventFd = 2;
115 // Prepare test condition
116 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
117 // Set mock behaviors
118 auto eventfdMock = EventfdMock::GetMock();
119 auto inotifyMock = InotifyMock::GetMock();
120 EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1));
121 EXPECT_CALL(*eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(eventFd));
122 // Do testing
123 bool result = watcher.InitNotify();
124 // Verify results
125 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
126 testing::Mock::VerifyAndClearExpectations(eventfdMock.get());
127 EXPECT_TRUE(result);
128 EXPECT_EQ(watcher.notifyFd_, 1);
129 EXPECT_EQ(watcher.eventFd_, eventFd);
130 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_001";
131 }
132
133 /**
134 * @tc.name: FsFileWatcherMockTest_InitNotify_002
135 * @tc.desc: Test function of FsFileWatcher::InitNotify interface for FAILURE when inotify_init fails.
136 * @tc.size: MEDIUM
137 * @tc.type: FUNC
138 * @tc.level Level 1
139 */
140 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_002, testing::ext::TestSize.Level1)
141 {
142 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_002";
143 // Prepare test condition
144 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
145 // Set mock behaviors
146 auto inotifyMock = InotifyMock::GetMock();
147 EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1));
148 // Do testing
149 bool result = watcher.InitNotify();
150 // Verify results
151 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
152 EXPECT_FALSE(result);
153 EXPECT_EQ(watcher.notifyFd_, -1);
154 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_002";
155 }
156
157 /**
158 * @tc.name: FsFileWatcherMockTest_InitNotify_003
159 * @tc.desc: Test function of FsFileWatcher::InitNotify interface for FAILURE when eventfd fails.
160 * @tc.size: MEDIUM
161 * @tc.type: FUNC
162 * @tc.level Level 1
163 */
164 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_003, testing::ext::TestSize.Level1)
165 {
166 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_003";
167 // Prepare test condition
168 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
169 // Set mock behaviors
170 auto inotifyMock = InotifyMock::GetMock();
171 auto eventfdMock = EventfdMock::GetMock();
172 EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1));
173 EXPECT_CALL(*eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1));
174 // Do testing
175 bool result = watcher.InitNotify();
176 // Verify results
177 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
178 testing::Mock::VerifyAndClearExpectations(eventfdMock.get());
179 EXPECT_FALSE(result);
180 EXPECT_EQ(watcher.notifyFd_, 1);
181 EXPECT_EQ(watcher.eventFd_, -1);
182 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_003";
183 }
184
185 /**
186 * @tc.name: FsFileWatcherMockTest_StartNotify_001
187 * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is not watched.
188 * @tc.size: MEDIUM
189 * @tc.type: FUNC
190 * @tc.level Level 0
191 */
192 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing::ext::TestSize.Level0)
193 {
194 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_001";
195 // Prepare test parameters
196 auto info = std::make_shared<WatcherInfo>(nullptr);
197 info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_001";
198 info->events = IN_CREATE | IN_DELETE;
199 // Prepare test condition
200 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
201 watcher.notifyFd_ = 1;
202 // Set mock behaviors
203 auto inotifyMock = InotifyMock::GetMock();
204 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
205 .Times(1)
206 .WillOnce(testing::Return(EXPECTED_WD));
207 // Do testing
208 int32_t result = watcher.StartNotify(info);
209 // Verify results
210 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
211 EXPECT_EQ(result, ERRNO_NOERR);
212 EXPECT_EQ(info->wd, EXPECTED_WD);
213 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_001";
214 }
215
216 /**
217 * @tc.name: FsFileWatcherMockTest_StartNotify_002
218 * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is already watched with same
219 * events.
220 * @tc.size: MEDIUM
221 * @tc.type: FUNC
222 * @tc.level Level 0
223 */
224 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_002, testing::ext::TestSize.Level0)
225 {
226 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_002";
227 // Prepare test parameters
228 auto info = std::make_shared<WatcherInfo>(nullptr);
229 info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_002";
230 info->events = IN_CREATE | IN_DELETE;
231 info->wd = EXPECTED_WD;
232 // Prepare test condition
233 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
234 watcher.notifyFd_ = 1;
235 watcher.dataCache_.AddWatcherInfo(info);
236 // Set mock behaviors
237 auto inotifyMock = InotifyMock::GetMock();
238 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).Times(0);
239 // Do testing
240 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
241 int32_t result = watcher.StartNotify(info);
242 // Verify results
243 EXPECT_EQ(result, ERRNO_NOERR);
244 EXPECT_EQ(info->wd, EXPECTED_WD);
245 }
246
247 /**
248 * @tc.name: FsFileWatcherMockTest_StartNotify_003
249 * @tc.desc: Test function of FsFileWatcher::StartNotify interface for FAILURE when info is nullptr.
250 * @tc.size: SMALL
251 * @tc.type: FUNC
252 * @tc.level Level 1
253 */
254 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_003, testing::ext::TestSize.Level1)
255 {
256 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_003";
257 // Prepare test condition
258 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
259 // Do testing with nullptr parameter
260 int32_t result = watcher.StartNotify(nullptr);
261 // Verify results
262 EXPECT_EQ(result, EINVAL);
263 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_003";
264 }
265
266 /**
267 * @tc.name: FsFileWatcherMockTest_StartNotify_004
268 * @tc.desc: Test function of FsFileWatcher::StartNotify interface for FAILURE when notifyFd_ is invalid.
269 * @tc.size: SMALL
270 * @tc.type: FUNC
271 * @tc.level Level 1
272 */
273 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_004, testing::ext::TestSize.Level1)
274 {
275 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_004";
276 // Prepare test condition
277 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
278 watcher.notifyFd_ = -1;
279 // Build test parameters
280 auto info = std::make_shared<WatcherInfo>(nullptr);
281 info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_004";
282 info->events = IN_CREATE;
283 // Do testing
284 int32_t result = watcher.StartNotify(info);
285 // Verify results
286 EXPECT_EQ(result, EIO);
287 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_004";
288 }
289
290 /**
291 * @tc.name: FsFileWatcherMockTest_StartNotify_005
292 * @tc.desc: Test function of FsFileWatcher::StartNotify interface for FAILURE when inotify_add_watch fails.
293 * @tc.size: MEDIUM
294 * @tc.type: FUNC
295 * @tc.level Level 1
296 */
297 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_005, testing::ext::TestSize.Level1)
298 {
299 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_005";
300 // Prepare test condition
301 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
302 watcher.notifyFd_ = 1;
303 // Build test parameters
304 auto info = std::make_shared<WatcherInfo>(nullptr);
305 info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_005";
306 info->events = IN_DELETE;
307 // Set mock behaviors for inotify_add_watch failure
308 auto inotifyMock = InotifyMock::GetMock();
309 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
310 .Times(1)
311 .WillOnce(testing::SetErrnoAndReturn(EIO, -1));
312 // Do testing
313 int32_t result = watcher.StartNotify(info);
314 // Verify results
315 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
316 EXPECT_EQ(result, EIO);
317 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_005";
318 }
319
320 /**
321 * @tc.name: FsFileWatcherMockTest_StartNotify_006
322 * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is already watched.
323 * @tc.size: MEDIUM
324 * @tc.type: FUNC
325 * @tc.level Level 0
326 */
327 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_006, testing::ext::TestSize.Level0)
328 {
329 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_006";
330 // Prepare test parameters
331 auto info = std::make_shared<WatcherInfo>(nullptr);
332 info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_006";
333 info->events = IN_CREATE;
334 // Prepare test condition
335 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
336 watcher.notifyFd_ = 1;
337 auto cachedInfo = std::make_shared<WatcherInfo>(nullptr);
338 cachedInfo->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_006";
339 cachedInfo->events = IN_DELETE;
340 cachedInfo->wd = EXPECTED_WD;
341 watcher.dataCache_.AddWatcherInfo(cachedInfo);
342 // Set mock behaviors
343 auto inotifyMock = InotifyMock::GetMock();
344 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
345 .Times(1)
346 .WillOnce(testing::Return(EXPECTED_WD));
347 // Do testing
348 int32_t result = watcher.StartNotify(info);
349 // Verify results
350 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
351 EXPECT_EQ(result, ERRNO_NOERR);
352 EXPECT_EQ(info->wd, EXPECTED_WD);
353 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_006";
354 }
355
356 /**
357 * @tc.name: FsFileWatcherMockTest_StopNotify_001
358 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS.
359 * @tc.size: MEDIUM
360 * @tc.type: FUNC
361 * @tc.level Level 0
362 */
363 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_001, testing::ext::TestSize.Level0)
364 {
365 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_001";
366 // Prepare test parameters
367 auto info = std::make_shared<WatcherInfo>(nullptr);
368 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_001";
369 info->events = IN_CREATE | IN_DELETE;
370 info->wd = EXPECTED_WD;
371 // Prepare test condition
372 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
373 watcher.notifyFd_ = 1;
374 watcher.dataCache_.AddWatcherInfo(info);
375 // Set mock behaviors
376 auto unistdMock = UnistdMock::GetMock();
377 auto inotifyMock = InotifyMock::GetMock();
378 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(0);
379 EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0));
380 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
381 // Do testing
382 int32_t result = watcher.StopNotify(info);
383 // Verify results
384 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
385 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
386 EXPECT_EQ(result, ERRNO_NOERR);
387 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_001";
388 }
389
390 /**
391 * @tc.name: FsFileWatcherMockTest_StopNotify_002
392 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when info is nullptr.
393 * @tc.size: SMALL
394 * @tc.type: FUNC
395 * @tc.level Level 1
396 */
397 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_002, testing::ext::TestSize.Level1)
398 {
399 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_002";
400 // Prepare test condition
401 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
402 // Do testing
403 int32_t result = watcher.StopNotify(nullptr);
404 // Verify results
405 EXPECT_EQ(result, EINVAL);
406 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_002";
407 }
408
409 /**
410 * @tc.name: FsFileWatcherMockTest_StopNotify_003
411 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when notifyFd_ is invalid.
412 * @tc.size: SMALL
413 * @tc.type: FUNC
414 * @tc.level Level 1
415 */
416 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_003, testing::ext::TestSize.Level1)
417 {
418 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_003";
419 // Prepare test condition
420 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
421 watcher.notifyFd_ = -1;
422 // Prepare test parameters
423 auto info = std::make_shared<WatcherInfo>(nullptr);
424 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_003";
425 info->events = IN_CREATE;
426 info->wd = EXPECTED_WD;
427 // Do testing
428 int32_t result = watcher.StopNotify(info);
429 // Verify results
430 EXPECT_EQ(result, EIO);
431 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_003";
432 }
433
434 /**
435 * @tc.name: FsFileWatcherMockTest_StopNotify_004
436 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when inotify_rm_watch fails.
437 * @tc.size: MEDIUM
438 * @tc.type: FUNC
439 * @tc.level Level 1
440 */
441 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_004, testing::ext::TestSize.Level1)
442 {
443 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_004";
444 // Prepare test parameters
445 auto info = std::make_shared<WatcherInfo>(nullptr);
446 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_004";
447 info->events = IN_DELETE;
448 info->wd = EXPECTED_WD;
449 // Prepare test condition
450 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
451 watcher.notifyFd_ = 1;
452 watcher.dataCache_.AddWatcherInfo(info);
453 // Set mock behaviors
454 auto unistdMock = UnistdMock::GetMock();
455 auto inotifyMock = InotifyMock::GetMock();
456 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
457 EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0));
458 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_))
459 .Times(1)
460 .WillOnce(testing::SetErrnoAndReturn(EIO, -1));
461 // Do testing
462 int32_t result = watcher.StopNotify(info);
463 // Verify results
464 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
465 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
466 EXPECT_EQ(result, EIO);
467 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_004";
468 }
469
470 /**
471 * @tc.name: FsFileWatcherMockTest_StopNotify_005
472 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS when rm watch fail.
473 * @tc.size: MEDIUM
474 * @tc.type: FUNC
475 * @tc.level Level 1
476 */
477 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_005, testing::ext::TestSize.Level1)
478 {
479 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_005";
480 // Prepare test parameters
481 auto info = std::make_shared<WatcherInfo>(nullptr);
482 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_005";
483 info->events = IN_DELETE;
484 info->wd = EXPECTED_WD;
485 // Prepare test condition
486 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
487 watcher.notifyFd_ = 1;
488 watcher.dataCache_.AddWatcherInfo(info);
489 // Set rm watch fail condition
490 watcher.closed_ = true;
491 watcher.reading_ = true;
492 // Set mock behaviors
493 auto unistdMock = UnistdMock::GetMock();
494 auto inotifyMock = InotifyMock::GetMock();
495 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(1));
496 EXPECT_CALL(*unistdMock, close(testing::_)).Times(0);
497 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0);
498 // Do testing
499 int32_t result = watcher.StopNotify(info);
500 // Verify results
501 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
502 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
503 EXPECT_EQ(result, ERRNO_NOERR);
504 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_005";
505 }
506
507 /**
508 * @tc.name: FsFileWatcherMockTest_StopNotify_006
509 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS when having remainingEvents but access
510 * file failed.
511 * @tc.size: MEDIUM
512 * @tc.type: FUNC
513 * @tc.level Level 1
514 */
515 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_006, testing::ext::TestSize.Level1)
516 {
517 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_006";
518 // Prepare test parameters
519 auto info = std::make_shared<WatcherInfo>(nullptr);
520 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_006";
521 info->events = IN_DELETE;
522 info->wd = EXPECTED_WD;
523 // Prepare test condition
524 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
525 watcher.notifyFd_ = 1;
526 watcher.dataCache_.AddWatcherInfo(info);
527 // Set having remainingEvents condition
528 auto remainingInfo = std::make_shared<WatcherInfo>(nullptr);
529 remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_006";
530 remainingInfo->events = IN_CREATE;
531 remainingInfo->wd = EXPECTED_WD;
532 watcher.dataCache_.AddWatcherInfo(remainingInfo);
533 // Set mock behaviors
534 auto unistdMock = UnistdMock::GetMock();
535 auto inotifyMock = InotifyMock::GetMock();
536 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1));
537 EXPECT_CALL(*unistdMock, close(testing::_)).Times(0);
538 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0);
539 // Do testing
540 int32_t result = watcher.StopNotify(info);
541 // Verify results
542 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
543 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
544 EXPECT_EQ(result, ERRNO_NOERR);
545 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_006";
546 }
547
548 /**
549 * @tc.name: FsFileWatcherMockTest_StopNotify_007
550 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS when having remainingEvents and
551 * NotifyToWatchNewEvents success.
552 * @tc.size: MEDIUM
553 * @tc.type: FUNC
554 * @tc.level Level 1
555 */
556 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_007, testing::ext::TestSize.Level1)
557 {
558 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_007";
559 // Prepare test parameters
560 auto info = std::make_shared<WatcherInfo>(nullptr);
561 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_007";
562 info->events = IN_DELETE;
563 info->wd = EXPECTED_WD;
564 // Prepare test condition
565 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
566 watcher.notifyFd_ = 1;
567 watcher.dataCache_.AddWatcherInfo(info);
568 // Set having remainingEvents condition
569 auto remainingInfo = std::make_shared<WatcherInfo>(nullptr);
570 remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_007";
571 remainingInfo->events = IN_CREATE;
572 remainingInfo->wd = EXPECTED_WD;
573 watcher.dataCache_.AddWatcherInfo(remainingInfo);
574 // Set mock behaviors
575 auto unistdMock = UnistdMock::GetMock();
576 auto inotifyMock = InotifyMock::GetMock();
577 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
578 EXPECT_CALL(*unistdMock, close(testing::_)).Times(0);
579 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0);
580 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
581 .Times(1)
582 .WillOnce(testing::Return(EXPECTED_WD));
583 // Do testing
584 int32_t result = watcher.StopNotify(info);
585 // Verify results
586 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
587 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
588 EXPECT_EQ(result, ERRNO_NOERR);
589 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_007";
590 }
591
592 /**
593 * @tc.name: FsFileWatcherMockTest_StopNotify_008
594 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when having remainingEvents but
595 * NotifyToWatchNewEvents fails for inotify_add_watch fails.
596 * @tc.size: MEDIUM
597 * @tc.type: FUNC
598 * @tc.level Level 1
599 */
600 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_008, testing::ext::TestSize.Level1)
601 {
602 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_008";
603 // Prepare test parameters
604 auto info = std::make_shared<WatcherInfo>(nullptr);
605 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_008";
606 info->events = IN_DELETE;
607 info->wd = EXPECTED_WD;
608 // Prepare test condition
609 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
610 watcher.notifyFd_ = 1;
611 watcher.dataCache_.AddWatcherInfo(info);
612 // Set having remainingEvents condition
613 auto remainingInfo = std::make_shared<WatcherInfo>(nullptr);
614 remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_008";
615 remainingInfo->events = IN_CREATE;
616 remainingInfo->wd = EXPECTED_WD;
617 watcher.dataCache_.AddWatcherInfo(remainingInfo);
618 // Set mock behaviors
619 auto unistdMock = UnistdMock::GetMock();
620 auto inotifyMock = InotifyMock::GetMock();
621 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
622 EXPECT_CALL(*unistdMock, close(testing::_)).Times(0);
623 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0);
624 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
625 .Times(1)
626 .WillOnce(testing::SetErrnoAndReturn(EIO, -1));
627 // Do testing
628 int32_t result = watcher.StopNotify(info);
629 // Verify results
630 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
631 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
632 EXPECT_EQ(result, EIO);
633 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_008";
634 }
635
636 /**
637 * @tc.name: FsFileWatcherMockTest_StopNotify_009
638 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when having remainingEvents but
639 * NotifyToWatchNewEvents fails for inotify_add_watch return another wd.
640 * @tc.size: MEDIUM
641 * @tc.type: FUNC
642 * @tc.level Level 1
643 */
644 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_009, testing::ext::TestSize.Level1)
645 {
646 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_009";
647 // Prepare test parameters
648 auto info = std::make_shared<WatcherInfo>(nullptr);
649 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_009";
650 info->events = IN_DELETE;
651 info->wd = EXPECTED_WD;
652 // Prepare test condition
653 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
654 watcher.notifyFd_ = 1;
655 watcher.dataCache_.AddWatcherInfo(info);
656 // Set having remainingEvents condition
657 auto remainingInfo = std::make_shared<WatcherInfo>(nullptr);
658 remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_009";
659 remainingInfo->events = IN_CREATE;
660 remainingInfo->wd = EXPECTED_WD;
661 watcher.dataCache_.AddWatcherInfo(remainingInfo);
662 // Set mock behaviors
663 auto unistdMock = UnistdMock::GetMock();
664 auto inotifyMock = InotifyMock::GetMock();
665 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
666 EXPECT_CALL(*unistdMock, close(testing::_)).Times(0);
667 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0);
668 EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
669 .Times(1)
670 .WillOnce(testing::Return(UNEXPECTED_WD));
671 // Do testing
672 int32_t result = watcher.StopNotify(info);
673 // Verify results
674 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
675 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
676 EXPECT_EQ(result, EIO);
677 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_009";
678 }
679
680 /**
681 * @tc.name: FsFileWatcherMockTest_StopNotify_010
682 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when CloseNotifyFd fails.
683 * @tc.size: MEDIUM
684 * @tc.type: FUNC
685 * @tc.level Level 1
686 */
687 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_010, testing::ext::TestSize.Level1)
688 {
689 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_010";
690 // Prepare test parameters
691 auto info = std::make_shared<WatcherInfo>(nullptr);
692 info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_010";
693 info->events = IN_DELETE;
694 info->wd = EXPECTED_WD;
695 // Prepare test condition
696 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
697 watcher.notifyFd_ = 1;
698 watcher.eventFd_ = 1;
699 watcher.dataCache_.AddWatcherInfo(info);
700 // Set mock behaviors
701 auto unistdMock = UnistdMock::GetMock();
702 auto inotifyMock = InotifyMock::GetMock();
703 EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(0);
704 EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(EIO));
705 EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
706 // Do testing
707 int32_t result = watcher.StopNotify(info);
708 // Verify results
709 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
710 testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
711 EXPECT_EQ(result, EIO);
712 EXPECT_EQ(watcher.notifyFd_, -1);
713 EXPECT_EQ(watcher.eventFd_, -1);
714 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_010";
715 }
716
717 /**
718 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_001
719 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when run_ is true.
720 * @tc.size: SMALL
721 * @tc.type: FUNC
722 * @tc.level Level 0
723 */
724 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_001, testing::ext::TestSize.Level0)
725 {
726 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_001";
727 // Prepare test condition
728 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
729 watcher.run_ = true;
730 // Set mock behaviors
731 auto pollMock = PollMock::GetMock();
732 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)).Times(0);
733 // Do testing
734 watcher.GetNotifyEvent();
735 // Verify results
736 testing::Mock::VerifyAndClearExpectations(pollMock.get());
737 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_001";
738 }
739
740 /**
741 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_002
742 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret > 0 and fds[0].revents has
743 * POLLNVAL.
744 * @tc.size: MEDIUM
745 * @tc.type: FUNC
746 * @tc.level Level 1
747 */
748 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_002, testing::ext::TestSize.Level1)
749 {
750 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_002";
751 // Prepare test condition
752 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
753 watcher.run_ = false;
754 watcher.notifyFd_ = 1;
755 watcher.eventFd_ = 2;
756 // Set mock behaviors
757 auto pollMock = PollMock::GetMock();
758 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_))
759 .Times(1)
__anon0115ed190102(struct pollfd *fds, nfds_t n, int timeout) 760 .WillOnce([](struct pollfd *fds, nfds_t n, int timeout) {
761 fds[0].revents = POLLNVAL;
762 return 1;
763 });
764 // Do testing
765 watcher.GetNotifyEvent();
766 // Verify results
767 testing::Mock::VerifyAndClearExpectations(pollMock.get());
768 EXPECT_FALSE(watcher.run_);
769 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_002";
770 }
771
772 /**
773 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_003
774 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret > 0 and fds[0].revents has
775 * POLLIN.
776 * @tc.size: MEDIUM
777 * @tc.type: FUNC
778 * @tc.level Level 1
779 */
780 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_003, testing::ext::TestSize.Level1)
781 {
782 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_003";
783 // Prepare test condition
784 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
785 watcher.run_ = false;
786 watcher.notifyFd_ = 1;
787 watcher.eventFd_ = 2;
788 // Set mock behaviors
789 auto pollMock = PollMock::GetMock();
790 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_))
791 .Times(1)
__anon0115ed190202(struct pollfd *fds, nfds_t n, int timeout) 792 .WillOnce([](struct pollfd *fds, nfds_t n, int timeout) {
793 fds[0].revents = POLLIN;
794 return 1;
795 });
796 // Do testing
797 watcher.GetNotifyEvent();
798 // Verify results
799 testing::Mock::VerifyAndClearExpectations(pollMock.get());
800 EXPECT_FALSE(watcher.run_);
801 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_003";
802 }
803
804 /**
805 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_004
806 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret > 0 and fds[1].revents has
807 * POLLIN.
808 * @tc.size: MEDIUM
809 * @tc.type: FUNC
810 * @tc.level Level 1
811 */
812 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_004, testing::ext::TestSize.Level1)
813 {
814 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_004";
815 // Prepare test condition
816 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
817 watcher.run_ = false;
818 watcher.notifyFd_ = 1;
819 watcher.eventFd_ = 2;
820 watcher.closed_ = true; // Avoid calling ReadNotifyEvent
821 // Set mock behaviors
822 auto pollMock = PollMock::GetMock();
823 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_))
824 .Times(1)
__anon0115ed190302(struct pollfd *fds, nfds_t n, int timeout) 825 .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) {
826 fds[1].revents = POLLIN;
827 watcher.run_ = false; // Ensure the loop will exit
828 return 1;
829 });
830 // Do testing
831 watcher.GetNotifyEvent();
832 // Verify results
833 testing::Mock::VerifyAndClearExpectations(pollMock.get());
834 EXPECT_FALSE(watcher.run_);
835 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_004";
836 }
837
838 /**
839 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_005
840 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret == 0.
841 * @tc.size: MEDIUM
842 * @tc.type: FUNC
843 * @tc.level Level 1
844 */
845 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_005, testing::ext::TestSize.Level1)
846 {
847 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_005";
848 // Prepare test condition
849 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
850 watcher.run_ = false;
851 watcher.notifyFd_ = 1;
852 watcher.eventFd_ = 2;
853 // Set mock behaviors
854 auto pollMock = PollMock::GetMock();
855 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_))
856 .Times(1)
__anon0115ed190402(struct pollfd *fds, nfds_t n, int timeout) 857 .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) {
858 watcher.run_ = false; // Ensure the loop will exit
859 return 0;
860 });
861 // Do testing
862 watcher.GetNotifyEvent();
863 // Verify results
864 testing::Mock::VerifyAndClearExpectations(pollMock.get());
865 EXPECT_FALSE(watcher.run_);
866 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_005";
867 }
868
869 /**
870 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_006
871 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret < 0 and errno == EINTR.
872 * @tc.size: MEDIUM
873 * @tc.type: FUNC
874 * @tc.level Level 1
875 */
876 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_006, testing::ext::TestSize.Level1)
877 {
878 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_006";
879 // Prepare test condition
880 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
881 watcher.run_ = false;
882 watcher.notifyFd_ = 1;
883 watcher.eventFd_ = 2;
884 // Set mock behaviors
885 auto pollMock = PollMock::GetMock();
886 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_))
887 .Times(1)
__anon0115ed190502(struct pollfd *fds, nfds_t n, int timeout) 888 .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) {
889 errno = EINTR;
890 watcher.run_ = false; // Ensure the loop will exit
891 return -1;
892 });
893 // Do testing
894 watcher.GetNotifyEvent();
895 // Verify results
896 testing::Mock::VerifyAndClearExpectations(pollMock.get());
897 EXPECT_FALSE(watcher.run_);
898 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_006";
899 }
900
901 /**
902 * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_007
903 * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret < 0 and errno != EINTR.
904 * @tc.size: MEDIUM
905 * @tc.type: FUNC
906 * @tc.level Level 1
907 */
908 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_007, testing::ext::TestSize.Level1)
909 {
910 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_007";
911 // Prepare test condition
912 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
913 watcher.run_ = false;
914 watcher.notifyFd_ = 1;
915 watcher.eventFd_ = 2;
916 // Set mock behaviors
917 auto pollMock = PollMock::GetMock();
918 EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_))
919 .Times(1)
__anon0115ed190602(struct pollfd *fds, nfds_t n, int timeout) 920 .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) {
921 errno = EIO;
922 watcher.run_ = false; // Ensure the loop will exit
923 return -1;
924 });
925 // Do testing
926 watcher.GetNotifyEvent();
927 // Verify results
928 testing::Mock::VerifyAndClearExpectations(pollMock.get());
929 EXPECT_FALSE(watcher.run_);
930 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_007";
931 }
932
933 /**
934 * @tc.name: FsFileWatcherMockTest_ReadNotifyEventLocked_001
935 * @tc.desc: Test ReadNotifyEventLocked when closed_ is false.
936 * @tc.size: SMALL
937 * @tc.type: FUNC
938 * @tc.level Level 0
939 */
940 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_001, testing::ext::TestSize.Level0)
941 {
942 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEventLocked_001";
943 // Prepare test condition
944 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
945 watcher.closed_ = false;
946 // Set mock behaviors
947 auto unistdMock = UnistdMock::GetMock();
948 EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(0));
949 EXPECT_CALL(*unistdMock, close(testing::_)).Times(0);
950 // Do testing
951 watcher.ReadNotifyEventLocked();
952 // Verify results
953 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
954 EXPECT_FALSE(watcher.reading_);
955 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEventLocked_001";
956 }
957
958 /**
959 * @tc.name: FsFileWatcherMockTest_ReadNotifyEventLocked_002
960 * @tc.desc: Test ReadNotifyEventLocked when close after read.
961 * @tc.size: SMALL
962 * @tc.type: FUNC
963 * @tc.level Level 1
964 */
965 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_002, testing::ext::TestSize.Level1)
966 {
967 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEventLocked_002";
968 // Prepare test condition
969 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
970 watcher.closed_ = false;
971 // Set mock behaviors
972 auto unistdMock = UnistdMock::GetMock();
973 EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_))
974 .Times(1)
__anon0115ed190702(int fd, void *buf, size_t count) 975 .WillOnce([&watcher](int fd, void *buf, size_t count) {
976 errno = EIO;
977 watcher.closed_ = true; // Set close after read condition
978 return 0;
979 });
980 EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0));
981 // Do testing
982 watcher.ReadNotifyEventLocked();
983 // Verify results
984 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
985 EXPECT_FALSE(watcher.closed_);
986 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEventLocked_002";
987 }
988
989 /**
990 * @tc.name: FsFileWatcherMockTest_ReadNotifyEvent_001
991 * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for SUCCESS when read valid event data.
992 * @tc.size: MEDIUM
993 * @tc.type: FUNC
994 * @tc.level Level 0
995 */
996 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_001, testing::ext::TestSize.Level0)
997 {
998 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_001";
999 // Prepare test condition
1000 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1001 int32_t len = static_cast<int32_t>(sizeof(struct inotify_event));
1002 // Set mock behaviors
1003 auto unistdMock = UnistdMock::GetMock();
1004 EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len));
1005 // Do testing
1006 watcher.ReadNotifyEvent();
1007 // Verify results
1008 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
1009 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_001";
1010 }
1011
1012 /**
1013 * @tc.name: FsFileWatcherMockTest_ReadNotifyEvent_002
1014 * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for FAILURE when read returns -1.
1015 * @tc.size: MEDIUM
1016 * @tc.type: FUNC
1017 * @tc.level Level 1
1018 */
1019 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_002, testing::ext::TestSize.Level1)
1020 {
1021 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_002";
1022 // Prepare test condition
1023 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1024 // Set mock behaviors
1025 auto unistdMock = UnistdMock::GetMock();
1026 EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_))
1027 .Times(1)
1028 .WillOnce(testing::SetErrnoAndReturn(EIO, -1));
1029 // Do testing
1030 watcher.ReadNotifyEvent();
1031 // Verify results
1032 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
1033 EXPECT_EQ(errno, EIO);
1034 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_002";
1035 }
1036
1037 /**
1038 * @tc.name: FsFileWatcherMockTest_ReadNotifyEvent_003
1039 * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for SUCCESS when read returns 0 (EOF).
1040 * @tc.size: SMALL
1041 * @tc.type: FUNC
1042 * @tc.level Level 1
1043 */
1044 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_003, testing::ext::TestSize.Level1)
1045 {
1046 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_003";
1047 // Prepare test condition
1048 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1049 // Set mock behaviors
1050 auto unistdMock = UnistdMock::GetMock();
1051 EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_))
1052 .Times(1)
1053 .WillOnce(testing::SetErrnoAndReturn(0, 0));
1054 // Do testing
1055 watcher.ReadNotifyEvent();
1056 // Verify results
1057 testing::Mock::VerifyAndClearExpectations(unistdMock.get());
1058 EXPECT_EQ(errno, 0);
1059 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_003";
1060 }
1061
1062 /**
1063 * @tc.name: FsFileWatcherMockTest_NotifyEvent_001
1064 * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for SUCCESS when valid event without filename.
1065 * @tc.size: MEDIUM
1066 * @tc.type: FUNC
1067 * @tc.level Level 0
1068 */
1069 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_001, testing::ext::TestSize.Level0)
1070 {
1071 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_001";
1072 // Prepare test parameters
1073 uint32_t mask = IN_CREATE;
1074 struct inotify_event event = { .wd = EXPECTED_WD, .mask = mask, .cookie = 0, .len = 0 };
1075 // Prepare test condition
1076 auto callback = std::make_shared<MockWatcherCallback>();
1077 auto info = std::make_shared<WatcherInfo>(callback);
1078 info->fileName = "fakePath/FsFileWatcherMockTest_NotifyEvent_001";
1079 info->events = mask;
1080 info->wd = EXPECTED_WD;
1081 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1082 watcher.dataCache_.AddWatcherInfo(info);
1083 // Set mock behaviors
1084 EXPECT_CALL(*callback, InvokeCallback(testing::_, testing::_, testing::_)).Times(1);
1085 // Do testing
1086 watcher.NotifyEvent(&event);
1087 // Verify results
1088 testing::Mock::VerifyAndClearExpectations(callback.get());
1089 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_001";
1090 }
1091
1092 /**
1093 * @tc.name: FsFileWatcherMockTest_NotifyEvent_002
1094 * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for SUCCESS when valid event with filename.
1095 * @tc.size: MEDIUM
1096 * @tc.type: FUNC
1097 * @tc.level Level 1
1098 */
1099 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_002, testing::ext::TestSize.Level1)
1100 {
1101 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_002";
1102 // Prepare test parameters
1103 const char *name = "test.txt";
1104 size_t len = strlen(name);
1105 uint32_t mask = IN_CREATE;
1106 size_t totalSize = sizeof(struct inotify_event) + len + 1;
1107 std::vector<char> buffer(totalSize);
1108 struct inotify_event *event = reinterpret_cast<struct inotify_event *>(buffer.data());
1109 event->wd = EXPECTED_WD;
1110 event->mask = mask;
1111 event->cookie = 0;
1112 event->len = len + 1;
1113 char *namePtr = reinterpret_cast<char *>(event + 1);
1114 int ret = memcpy_s(namePtr, len + 1, name, len + 1);
1115 if (ret != 0) {
1116 EXPECT_EQ(ret, 0);
1117 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_002";
1118 return;
1119 }
1120 // Prepare test condition
1121 auto callback = std::make_shared<MockWatcherCallback>();
1122 auto info = std::make_shared<WatcherInfo>(callback);
1123 info->fileName = "fakePath/FsFileWatcherMockTest_NotifyEvent_002";
1124 info->events = mask;
1125 info->wd = EXPECTED_WD;
1126 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1127 watcher.dataCache_.AddWatcherInfo(info);
1128 // Set mock behaviors
1129 EXPECT_CALL(*callback, InvokeCallback(testing::_, testing::_, testing::_)).Times(1);
1130 // Do testing
1131 watcher.NotifyEvent(event);
1132 // Verify results
1133 testing::Mock::VerifyAndClearExpectations(callback.get());
1134 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_002";
1135 }
1136
1137 /**
1138 * @tc.name: FsFileWatcherMockTest_NotifyEvent_003
1139 * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for FAILURE when event pointer is NULL.
1140 * @tc.size: SMALL
1141 * @tc.type: FUNC
1142 * @tc.level Level 1
1143 */
1144 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_003, testing::ext::TestSize.Level1)
1145 {
1146 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_003";
1147 // Prepare test condition
1148 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1149 // Do testing
1150 watcher.NotifyEvent(nullptr);
1151 // Verify results
1152 EXPECT_TRUE(watcher.dataCache_.wdFileNameCache_.empty());
1153 EXPECT_TRUE(watcher.dataCache_.watcherInfoCache_.empty());
1154 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_003";
1155 }
1156
1157 /**
1158 * @tc.name: FsFileWatcherMockTest_NotifyEvent_004
1159 * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for FAILURE when no matched watcher found.
1160 * @tc.size: SMALL
1161 * @tc.type: FUNC
1162 * @tc.level Level 1
1163 */
1164 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_004, testing::ext::TestSize.Level1)
1165 {
1166 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_004";
1167 // Prepare test parameters
1168 struct inotify_event event = { .wd = EXPECTED_WD, .mask = IN_CREATE, .cookie = 0, .len = 0 };
1169 // Prepare test condition
1170 auto callback = std::make_shared<MockWatcherCallback>();
1171 auto info = std::make_shared<WatcherInfo>(callback);
1172 info->fileName = "fakePath/FsFileWatcherMockTest_NotifyEvent_004";
1173 info->events = IN_MODIFY; // Not matched mask
1174 info->wd = UNEXPECTED_WD; // Not matched wd
1175 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1176 watcher.dataCache_.AddWatcherInfo(info);
1177 // Set mock behaviors
1178 EXPECT_CALL(*callback, InvokeCallback(testing::_, testing::_, testing::_)).Times(0);
1179 // Do testing
1180 watcher.NotifyEvent(&event);
1181 // Verify results
1182 testing::Mock::VerifyAndClearExpectations(callback.get());
1183 EXPECT_FALSE(watcher.dataCache_.wdFileNameCache_.empty());
1184 EXPECT_FALSE(watcher.dataCache_.watcherInfoCache_.empty());
1185 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_004";
1186 }
1187
1188 /**
1189 * @tc.name: FsFileWatcherMockTest_AddWatcherInfo_001
1190 * @tc.desc: Test function of FsFileWatcher::AddWatcherInfo interface for SUCCESS.
1191 * @tc.size: MEDIUM
1192 * @tc.type: FUNC
1193 * @tc.level Level 1
1194 */
1195 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_001, testing::ext::TestSize.Level1)
1196 {
1197 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_001";
1198 // Prepare test parameters
1199 auto info = std::make_shared<WatcherInfo>(nullptr);
1200 info->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001";
1201 info->events = IN_CREATE;
1202 // Prepare test condition
1203 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1204 auto cachedInfo0 = std::make_shared<WatcherInfo>(nullptr);
1205 cachedInfo0->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001_cachedInfo0";
1206 watcher.dataCache_.AddWatcherInfo(cachedInfo0);
1207
1208 auto cachedInfo1 = std::make_shared<WatcherInfo>(nullptr);
1209 cachedInfo1->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001";
1210 cachedInfo1->events = IN_DELETE;
1211 watcher.dataCache_.AddWatcherInfo(cachedInfo1);
1212
1213 auto callback = std::make_shared<MockWatcherCallback>();
1214 auto cachedInfo2 = std::make_shared<WatcherInfo>(callback);
1215 cachedInfo2->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001";
1216 cachedInfo2->events = IN_CREATE;
1217 watcher.dataCache_.AddWatcherInfo(cachedInfo2);
1218
1219 // Set mock behaviors
1220 EXPECT_CALL(*callback, IsStrictEquals(testing::_)).Times(1).WillOnce(testing::Return(false));
1221 // Do testing
1222 bool result = watcher.AddWatcherInfo(info);
1223 // Verify results
1224 testing::Mock::VerifyAndClearExpectations(callback.get());
1225 EXPECT_TRUE(result);
1226 cachedInfo2->callback = nullptr;
1227 watcher.dataCache_.ClearCache();
1228 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_AddWatcherInfo_001";
1229 }
1230
1231 /**
1232 * @tc.name: FsFileWatcherMockTest_AddWatcherInfo_002
1233 * @tc.desc: Test function of FsFileWatcher::AddWatcherInfo interface for FAILURE when param is nullptr.
1234 * @tc.size: SMALL
1235 * @tc.type: FUNC
1236 * @tc.level Level 1
1237 */
1238 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_002, testing::ext::TestSize.Level1)
1239 {
1240 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_002";
1241 // Prepare test condition
1242 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1243 // Do testing
1244 bool result = watcher.AddWatcherInfo(nullptr);
1245 // Verify results
1246 EXPECT_FALSE(result);
1247 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_AddWatcherInfo_002";
1248 }
1249
1250 /**
1251 * @tc.name: FsFileWatcherMockTest_AddWatcherInfo_003
1252 * @tc.desc: Test function of FsFileWatcher::AddWatcherInfo interface for FAILURE when having same info.
1253 * @tc.size: MEDIUM
1254 * @tc.type: FUNC
1255 * @tc.level Level 1
1256 */
1257 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_003, testing::ext::TestSize.Level1)
1258 {
1259 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_003";
1260 // Prepare test parameters
1261 auto info = std::make_shared<WatcherInfo>(nullptr);
1262 info->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_003";
1263 info->events = IN_CREATE;
1264 // Prepare test condition
1265 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1266 auto callback = std::make_shared<MockWatcherCallback>();
1267 auto cachedInfo = std::make_shared<WatcherInfo>(callback);
1268 cachedInfo->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_003";
1269 cachedInfo->events = IN_CREATE;
1270 watcher.dataCache_.AddWatcherInfo(cachedInfo);
1271 // Set mock behaviors
1272 EXPECT_CALL(*callback, IsStrictEquals(testing::_)).Times(1).WillOnce(testing::Return(true));
1273 // Do testing
1274 bool result = watcher.AddWatcherInfo(info);
1275 // Verify results
1276 testing::Mock::VerifyAndClearExpectations(callback.get());
1277 EXPECT_FALSE(result);
1278 cachedInfo->callback = nullptr;
1279 watcher.dataCache_.ClearCache();
1280 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_AddWatcherInfo_003";
1281 }
1282
1283 /**
1284 * @tc.name: FsFileWatcherMockTest_RemoveWatcherInfo_001
1285 * @tc.desc: Test function of FsFileWatcher::RemoveWatcherInfo interface for FAILURE when param is nullptr.
1286 * @tc.size: SMALL
1287 * @tc.type: FUNC
1288 * @tc.level Level 1
1289 */
1290 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_RemoveWatcherInfo_001, testing::ext::TestSize.Level1)
1291 {
1292 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_RemoveWatcherInfo_001";
1293 // Prepare test condition
1294 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1295 // Do testing
1296 auto result = watcher.RemoveWatcherInfo(nullptr);
1297 // Verify results
1298 EXPECT_EQ(result, EINVAL);
1299 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_RemoveWatcherInfo_001";
1300 }
1301
1302 /**
1303 * @tc.name: FsFileWatcherMockTest_DestroyTaskThead_001
1304 * @tc.desc: Test function of FsFileWatcher::DestroyTaskThead interface when taskRunning is true.
1305 * @tc.size: SMALL
1306 * @tc.type: FUNC
1307 * @tc.level Level 1
1308 */
1309 HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_DestroyTaskThead_001, testing::ext::TestSize.Level1)
1310 {
1311 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_DestroyTaskThead_001";
1312 // Prepare test condition
1313 FsFileWatcher &watcher = FsFileWatcher::GetInstance();
1314 watcher.taskRunning_ = true;
1315 // Do testing
1316 watcher.DestroyTaskThead();
1317 // Verify results
1318 EXPECT_FALSE(watcher.taskRunning_);
1319 GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_DestroyTaskThead_001";
1320 }
1321
1322 } // namespace Test
1323 } // namespace ModuleFileIO
1324 } // namespace FileManagement
1325 } // namespace OHOS
1326