diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp index f3924031b36ad9062b839a98a80229e6ea2d3d79..08761f487dad713c4451c76e24010ffc5d26910e 100644 --- a/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp @@ -230,4 +230,84 @@ HWTEST_F(WatcherEntityTest, WatcherEntityTest_InitNotify_003, testing::ext::Test GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_InitNotify_003"; } +/** + * @tc.name: WatcherEntityTest_ReadNotifyEvent_001 + * @tc.desc: Test function of WatcherEntityTest::ReadNotifyEvent interface for SUCCESS when read valid event data. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_ReadNotifyEvent_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_ReadNotifyEvent_001"; + + auto &watcher = FileWatcher::GetInstance(); + int32_t len = static_cast(sizeof(struct inotify_event)); + WatcherCallback callback = nullptr; + + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len)); + + watcher.ReadNotifyEvent(callback); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_ReadNotifyEvent_001"; +} + +/** + * @tc.name: WatcherEntityTest_ReadNotifyEvent_002 + * @tc.desc: Test first if branch - remaining data < sizeof(inotify_event) + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_ReadNotifyEvent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_ReadNotifyEvent_002"; + + auto &watcher = FileWatcher::GetInstance(); + int32_t eventSize = static_cast(sizeof(struct inotify_event)); + int32_t partialSize = eventSize - 4; + WatcherCallback callback = nullptr; + + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(partialSize)); + + watcher.ReadNotifyEvent(callback); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_ReadNotifyEvent_002"; +} + +/** + * @tc.name: WatcherEntityTest_ReadNotifyEvent_003 + * @tc.desc: Test second if branch - event->len exceeds available buffer + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_ReadNotifyEvent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_ReadNotifyEvent_003"; + + auto &watcher = FileWatcher::GetInstance(); + int32_t eventSize = static_cast(sizeof(struct inotify_event)); + uint32_t nameLen = 20; + int32_t bufferSize = eventSize + 10; + WatcherCallback callback = nullptr; + + char buf[BUF_SIZE] = {0}; + struct inotify_event* mockEvent = reinterpret_cast(buf); + mockEvent->wd = 1; + mockEvent->len = nameLen; + + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(bufferSize)); + + watcher.ReadNotifyEvent(callback); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_ReadNotifyEvent_003"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file