From 6cab5ca43ee9b20937e1b04d6a579151762b05e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 12 Jun 2025 16:52:47 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=88=86=E7=A6=BB=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I70ba255ad35f30dfe83e805b2f1730e9c4eb03e3 Signed-off-by: 姜小林 --- interfaces/kits/js/BUILD.gn | 1 + .../mod_fs/class_watcher/fs_file_watcher.cpp | 151 +++++++--------- .../mod_fs/class_watcher/fs_file_watcher.h | 26 ++- .../class_watcher/watcher_data_cache.cpp | 163 ++++++++++++++++++ .../mod_fs/class_watcher/watcher_data_cache.h | 54 ++++++ .../js/src/mod_fs/properties/watcher_core.cpp | 2 +- 6 files changed, 291 insertions(+), 106 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp create mode 100644 interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 97cbc5d2f..0d00b5f2f 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -716,6 +716,7 @@ ohos_shared_library("ani_file_fs") { "src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp", "src/mod_fs/class_watcher/fs_file_watcher.cpp", "src/mod_fs/class_watcher/fs_watcher.cpp", + "src/mod_fs/class_watcher/watcher_data_cache.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_fs/properties/access_core.cpp", "src/mod_fs/properties/ani/access_ani.cpp", diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp index 24e09e958..b5f0ce5d5 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp @@ -28,12 +28,6 @@ namespace OHOS::FileManagement::ModuleFileIO { using namespace std; -mutex FsFileWatcher::watchMutex_; - -FsFileWatcher::FsFileWatcher() {} - -FsFileWatcher::~FsFileWatcher() {} - int32_t FsFileWatcher::GetNotifyId() { return notifyFd_; @@ -54,51 +48,38 @@ bool FsFileWatcher::InitNotify() return true; } -tuple FsFileWatcher::CheckEventWatched(const string &fileName, const uint32_t &event) -{ - int32_t wd = -1; - auto iter = wdFileNameMap_.find(fileName); - if (iter == wdFileNameMap_.end()) { - return { false, wd }; - } - wd = iter->second.first; - if ((iter->second.second & event) == event) { - return { true, wd }; - } - return { false, wd }; -} - int32_t FsFileWatcher::StartNotify(shared_ptr info) { - lock_guard lock(watchMutex_); if (notifyFd_ < 0) { HILOGE("Failed to start notify notifyFd_:%{public}d", notifyFd_); return EIO; } - auto [isWatched, wd] = CheckEventWatched(info->fileName, info->events); + auto [isWatched, wd] = dataCache_.FindWatchedWd(info->fileName, info->events); if (isWatched && wd > 0) { info->wd = wd; return ERRNO_NOERR; } + uint32_t watchEvents = 0; if (wd != -1) { - watchEvents = wdFileNameMap_[info->fileName].second | info->events; + watchEvents = dataCache_.GetFileEvents(info->fileName) | info->events; } else { watchEvents = info->events; } + int32_t newWd = inotify_add_watch(notifyFd_, info->fileName.c_str(), watchEvents); if (newWd < 0) { HILOGE("Failed to start notify errCode:%{public}d", errno); return errno; } + info->wd = newWd; - wdFileNameMap_[info->fileName].first = newWd; - wdFileNameMap_[info->fileName].second = watchEvents; + dataCache_.UpdateWatchedEvents(info->fileName, newWd, watchEvents); return ERRNO_NOERR; } -int32_t FsFileWatcher::NotifyToWatchNewEvents(const string &fileName, const int32_t &wd, const uint32_t &watchEvents) +int32_t FsFileWatcher::NotifyToWatchNewEvents(const string &fileName, int32_t wd, uint32_t watchEvents) { int32_t newWd = inotify_add_watch(notifyFd_, fileName.c_str(), watchEvents); if (newWd < 0) { @@ -110,7 +91,8 @@ int32_t FsFileWatcher::NotifyToWatchNewEvents(const string &fileName, const int3 HILOGE("New notify wd is error"); return EIO; } - wdFileNameMap_[fileName].second = watchEvents; + + dataCache_.UpdateWatchedEvents(fileName, wd, watchEvents); return ERRNO_NOERR; } @@ -118,19 +100,22 @@ int32_t FsFileWatcher::CloseNotifyFd() { int32_t closeRet = ERRNO_NOERR; - if (watcherInfoSet_.size() == 0) { + if (!dataCache_.HasWatcherInfo()) { run_ = false; closeRet = close(notifyFd_); if (closeRet != 0) { HILOGE("Failed to stop notify close fd errCode:%{public}d", errno); } notifyFd_ = -1; + closeRet = close(eventFd_); if (closeRet != 0) { HILOGE("Failed to close eventfd errCode:%{public}d", errno); } eventFd_ = -1; + HILOGI("Note: DestroyTaskThead begin..."); DestroyTaskThead(); + HILOGI("Note: DestroyTaskThead done!"); } closed_ = false; @@ -152,20 +137,21 @@ int FsFileWatcher::CloseNotifyFdLocked() int32_t FsFileWatcher::StopNotify(shared_ptr info) { - unique_lock lock(watchMutex_); if (notifyFd_ < 0) { HILOGE("Failed to stop notify notifyFd_:%{public}d", notifyFd_); return EIO; } - uint32_t newEvents = RemoveWatcherInfo(info); - if (newEvents > 0) { + + uint32_t remainingEvents = RemoveWatcherInfo(info); + if (remainingEvents > 0) { if (access(info->fileName.c_str(), F_OK) == 0) { - return NotifyToWatchNewEvents(info->fileName, info->wd, newEvents); + return NotifyToWatchNewEvents(info->fileName, info->wd, remainingEvents); } HILOGE("The Watched file does not exist, and the remaining monitored events will be invalid."); return ERRNO_NOERR; } - int oldWd = -1; + + int32_t oldWd = -1; { lock_guard lock(readMutex_); if (!(closed_ && reading_)) { @@ -174,17 +160,16 @@ int32_t FsFileWatcher::StopNotify(shared_ptr info) HILOGE("rm watch fail"); } } + + int32_t rmRet = 0; if (oldWd == -1) { - int rmErr = errno; - if (access(info->fileName.c_str(), F_OK) == 0) { - HILOGE("Failed to stop notify errCode:%{public}d", rmErr); - wdFileNameMap_.erase(info->fileName); - CloseNotifyFdLocked(); - return rmErr; - } + rmRet = errno; + HILOGE("Failed to stop notify errCode:%{public}d", rmRet); } - wdFileNameMap_.erase(info->fileName); - return CloseNotifyFdLocked(); + + dataCache_.RemoveFileWatcher(info->fileName); + int32_t closeRet = CloseNotifyFdLocked(); + return rmRet != 0 ? rmRet : closeRet; } void FsFileWatcher::ReadNotifyEvent() @@ -193,6 +178,7 @@ void FsFileWatcher::ReadNotifyEvent() int32_t index = 0; char buf[BUF_SIZE] = { 0 }; struct inotify_event *event = nullptr; + do { len = read(notifyFd_, &buf, sizeof(buf)); if (len < 0 && errno != EINTR) { @@ -200,6 +186,7 @@ void FsFileWatcher::ReadNotifyEvent() break; } } while (len < 0); + while (index < len) { event = reinterpret_cast(buf + index); NotifyEvent(event); @@ -217,7 +204,9 @@ void FsFileWatcher::ReadNotifyEventLocked() } reading_ = true; } + ReadNotifyEvent(); + { lock_guard lock(readMutex_); reading_ = false; @@ -243,6 +232,7 @@ void FsFileWatcher::GetNotifyEvent() if (run_) { return; } + run_ = true; nfds_t nfds = 2; struct pollfd fds[2]; @@ -251,87 +241,65 @@ void FsFileWatcher::GetNotifyEvent() fds[1].fd = notifyFd_; fds[1].events = POLLIN; int32_t ret = 0; + while (run_) { - ret = poll(fds, nfds, -1); + ret = poll(fds, nfds, 500); + if (ret > 0) { if (static_cast(fds[0].revents) & POLLNVAL) { run_ = false; return; } + if (static_cast(fds[1].revents) & POLLIN) { ReadNotifyEventLocked(); } - } else if (ret < 0 && errno == EINTR) { continue; - } else { + } + + if (ret == 0) { + // Note: 超时了 + continue; + } + + if (ret < 0 && errno == EINTR) { + continue; + } + + if (ret < 0 && errno != EINTR) { HILOGE("Failed to poll NotifyFd, errno=%{public}d", errno); return; } } } -bool FsFileWatcher::AddWatcherInfo(const string &fileName, shared_ptr info) +bool FsFileWatcher::AddWatcherInfo(shared_ptr info) { - for (auto &iter : watcherInfoSet_) { - if (iter->fileName == info->fileName && iter->events == info->events) { - bool isSame = iter->callback->IsStrictEquals(info->callback); - if (isSame) { - HILOGE("Faile to add watcher, fileName:%{public}s the callback is same", fileName.c_str()); - return false; - } - } - } - watcherInfoSet_.insert(info); - return true; + return dataCache_.AddWatcherInfo(info); } uint32_t FsFileWatcher::RemoveWatcherInfo(shared_ptr info) { - watcherInfoSet_.erase(info); - uint32_t otherEvents = 0; - for (const auto &iter : watcherInfoSet_) { - if (iter->fileName == info->fileName && iter->wd > 0) { - otherEvents |= iter->events; - } - } - return otherEvents; -} - -static bool CheckIncludeEvent(const uint32_t &mask, const uint32_t &event) -{ - if ((mask & event) > 0) { - return true; - } - return false; + return dataCache_.RemoveWatcherInfo(info); } void FsFileWatcher::NotifyEvent(const struct inotify_event *event) { - lock_guard lock(watchMutex_); - string tempFileName; - auto found = find_if(wdFileNameMap_.begin(), wdFileNameMap_.end(), - [event](const pair> &iter) { return iter.second.first == event->wd; }); - if (found != wdFileNameMap_.end()) { - tempFileName = found->first; + auto [matched, fileName, watcherInfos] = dataCache_.FindWatcherInfos(event->wd, event->mask); + if (!matched) { + HILOGE("Cannot find matched watcherInfos"); + return; } - for (const auto &iter : watcherInfoSet_) { - string fileName = tempFileName; - uint32_t watchEvent = 0; - if ((iter->fileName == fileName) && (iter->wd > 0)) { - watchEvent = iter->events; - } - if (!CheckIncludeEvent(event->mask, watchEvent)) { - continue; - } + for (const auto &info : watcherInfos) { if (event->len > 0) { fileName += "/" + string(event->name); } - iter->TriggerCallback(fileName, event->mask & IN_ALL_EVENTS, event->cookie); + info->TriggerCallback(fileName, event->mask & IN_ALL_EVENTS, event->cookie); } } -bool FsFileWatcher::CheckEventValid(const uint32_t &event) +bool FsFileWatcher::CheckEventValid(uint32_t event) { if ((event & IN_ALL_EVENTS) == event) { return true; @@ -351,5 +319,8 @@ void FsFileWatcher::DestroyTaskThead() if (taskRunning_) { taskRunning_ = false; } + + // Note: 可能需要加上状态控制 + dataCache_.ClearCache(); } } // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h index 17be3a40a..5fb73c8b3 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h @@ -18,14 +18,13 @@ #include #include #include -#include -#include #include #include "filemgmt_libfs.h" #include "fs_watch_entity.h" #include "singleton.h" +#include "watcher_data_cache.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; @@ -34,31 +33,32 @@ constexpr int BUF_SIZE = 1024; class FsFileWatcher : public Singleton { public: - FsFileWatcher(); - ~FsFileWatcher(); int32_t GetNotifyId(); bool InitNotify(); int StartNotify(shared_ptr info); int StopNotify(shared_ptr info); void GetNotifyEvent(); void AsyncGetNotifyEvent(); - bool AddWatcherInfo(const string &fileName, shared_ptr info); - bool CheckEventValid(const uint32_t &event); + bool AddWatcherInfo(shared_ptr info); + bool CheckEventValid(uint32_t event); + +public: + FsFileWatcher() = default; + ~FsFileWatcher() = default; + FsFileWatcher(const FsFileWatcher &) = delete; + FsFileWatcher &operator=(const FsFileWatcher &) = delete; private: uint32_t RemoveWatcherInfo(shared_ptr info); - tuple CheckEventWatched(const string &fileName, const uint32_t &event); void NotifyEvent(const struct inotify_event *event); int CloseNotifyFd(); int CloseNotifyFdLocked(); - int NotifyToWatchNewEvents(const string &fileName, const int &wd, const uint32_t &watchEvents); + int NotifyToWatchNewEvents(const string &fileName, int wd, uint32_t watchEvents); void ReadNotifyEvent(); void ReadNotifyEventLocked(); void DestroyTaskThead(); private: - static mutex watchMutex_; - mutex taskMutex_; mutex readMutex_; @@ -70,11 +70,7 @@ private: bool closed_ = false; int32_t notifyFd_ = -1; int32_t eventFd_ = -1; - unordered_set> watcherInfoSet_; - unordered_map> wdFileNameMap_; - - FsFileWatcher(const FsFileWatcher &) = delete; - FsFileWatcher &operator=(const FsFileWatcher &) = delete; + WatcherDataCache dataCache_; }; } // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp new file mode 100644 index 000000000..a59c73eb0 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "watcher_data_cache.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::ModuleFileIO { + +bool WatcherDataCache::AddWatcherInfo(std::shared_ptr info) +{ + std::lock_guard lock(cacheMutex_); + for (auto &iter : watcherInfoCache_) { + if (iter->fileName == info->fileName && iter->events == info->events) { + bool isSame = iter->callback->IsStrictEquals(info->callback); + if (isSame) { + HILOGE("Failed to add watcher, fileName:%{private}s the callback is same", info->fileName.c_str()); + return false; + } + } + } + watcherInfoCache_.push_back(info); + wdFileNameCache_[info->fileName] = std::make_pair(info->wd, info->events); + return true; +} + +uint32_t WatcherDataCache::RemoveWatcherInfo(std::shared_ptr info) +{ + std::lock_guard lock(cacheMutex_); + auto it = std::find(watcherInfoCache_.begin(), watcherInfoCache_.end(), info); + if (it != watcherInfoCache_.end()) { + watcherInfoCache_.erase(it); + } + + uint32_t remainingEvents = 0; + for (const auto &iter : watcherInfoCache_) { + if (iter->fileName == info->fileName && iter->wd > 0) { + remainingEvents |= iter->events; + } + } + + return remainingEvents; +} + +bool WatcherDataCache::RemoveFileWatcher(const std::string &fileName) +{ + std::lock_guard lock(cacheMutex_); + + auto iter = wdFileNameCache_.find(fileName); + if (iter == wdFileNameCache_.end()) { + return false; + } + wdFileNameCache_.erase(iter); + + watcherInfoCache_.erase(std::remove_if(watcherInfoCache_.begin(), watcherInfoCache_.end(), + [&fileName](const std::shared_ptr &info) { + return info->fileName == fileName; + }), + watcherInfoCache_.end()); + + return true; +} + +std::tuple WatcherDataCache::FindWatchedWd(const std::string &fileName, uint32_t event) +{ + std::lock_guard lock(cacheMutex_); + + int32_t wd = -1; + auto iter = wdFileNameCache_.find(fileName); + if (iter == wdFileNameCache_.end()) { + return { false, wd }; + } + + wd = iter->second.first; + if ((iter->second.second & event) == event) { + return { true, wd }; + } + + return { false, wd }; +} + +bool WatcherDataCache::UpdateWatchedEvents(const std::string &fileName, int32_t wd, uint32_t events) +{ + std::lock_guard lock(cacheMutex_); + auto iter = wdFileNameCache_.find(fileName); + if (iter == wdFileNameCache_.end()) { + return false; + } + + iter->second = std::make_pair(wd, events); + return true; +} + +static bool CheckIncludeEvent(uint32_t mask, uint32_t event) +{ + return (mask & event) > 0; +} + +std::tuple>> WatcherDataCache::FindWatcherInfos( + int32_t wd, uint32_t eventMask) +{ + std::lock_guard lock(cacheMutex_); + std::string fileName; + bool found = false; + for (const auto &[key, val] : wdFileNameCache_) { + if (val.first == wd) { + fileName = key; + found = true; + break; + } + } + + if (!found) { + return { false, "", {} }; + } + + std::vector> matchedInfos; + for (const auto &info : watcherInfoCache_) { + uint32_t watchEvent = 0; + if ((info->fileName == fileName) && (info->wd > 0)) { + watchEvent = info->events; + } + if (CheckIncludeEvent(eventMask, watchEvent)) { + matchedInfos.push_back(info); + } + } + return { !matchedInfos.empty(), fileName, matchedInfos }; +} + +uint32_t WatcherDataCache::GetFileEvents(const std::string &fileName) +{ + std::lock_guard lock(cacheMutex_); + auto iter = wdFileNameCache_.find(fileName); + if (iter == wdFileNameCache_.end()) { + return 0; + } + return iter->second.second; +} + +bool WatcherDataCache::HasWatcherInfo() const +{ + std::lock_guard lock(cacheMutex_); + return !watcherInfoCache_.empty(); +} + +void WatcherDataCache::ClearCache() +{ + std::lock_guard lock(cacheMutex_); + watcherInfoCache_.clear(); + wdFileNameCache_.clear(); +} + +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.h new file mode 100644 index 000000000..e18d584f1 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_DATA_CACHE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_DATA_CACHE_H + +#include +#include +#include +#include +#include + +#include "fs_watch_entity.h" + +namespace OHOS::FileManagement::ModuleFileIO { + +class WatcherDataCache { +public: + bool AddWatcherInfo(std::shared_ptr info); + uint32_t RemoveWatcherInfo(std::shared_ptr info); + bool RemoveFileWatcher(const std::string &fileName); + std::tuple FindWatchedWd(const std::string &fileName, uint32_t event); + bool UpdateWatchedEvents(const std::string &fileName, int32_t wd, uint32_t events); + std::tuple>> FindWatcherInfos( + int32_t wd, uint32_t eventMask); + uint32_t GetFileEvents(const std::string &fileName); + bool HasWatcherInfo() const; + void ClearCache(); + +public: + WatcherDataCache() = default; + ~WatcherDataCache() = default; + WatcherDataCache(const WatcherDataCache &) = delete; + WatcherDataCache &operator=(const WatcherDataCache &) = delete; + +private: + mutable std::mutex cacheMutex_; + std::vector> watcherInfoCache_; + std::unordered_map> wdFileNameCache_; +}; + +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_DATA_CACHE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher_core.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher_core.cpp index 382f494ec..1ea4a713a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher_core.cpp @@ -98,7 +98,7 @@ FsResult WatcherCore::DoCreateWatcher( watchEntity->data_ = info; - bool ret = FsFileWatcher::GetInstance().AddWatcherInfo(info->fileName, info); + bool ret = FsFileWatcher::GetInstance().AddWatcherInfo(info); if (!ret) { HILOGE("Failed to add watcher info."); return FsResult::Error(EINVAL); -- Gitee From 4c09d446a3b9999d3d292a6633374734afb53d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Mon, 16 Jun 2025 10:48:09 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AD=BB=E9=94=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib6af2f3b35888e9813dea0c1013ebb206ceecc6d Signed-off-by: 姜小林 --- .../mod_fs/class_watcher/fs_file_watcher.cpp | 39 ++++++++++--------- .../mod_fs/class_watcher/fs_file_watcher.h | 2 + .../class_watcher/watcher_data_cache.cpp | 7 ++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp index b5f0ce5d5..44cd49b4c 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp @@ -113,9 +113,7 @@ int32_t FsFileWatcher::CloseNotifyFd() HILOGE("Failed to close eventfd errCode:%{public}d", errno); } eventFd_ = -1; - HILOGI("Note: DestroyTaskThead begin..."); DestroyTaskThead(); - HILOGI("Note: DestroyTaskThead done!"); } closed_ = false; @@ -235,40 +233,38 @@ void FsFileWatcher::GetNotifyEvent() run_ = true; nfds_t nfds = 2; - struct pollfd fds[2]; + struct pollfd fds[2] = { { 0 } }; fds[0].fd = eventFd_; - fds[0].events = 0; + fds[0].events = POLLIN; fds[1].fd = notifyFd_; fds[1].events = POLLIN; int32_t ret = 0; while (run_) { - ret = poll(fds, nfds, 500); - + ret = poll(fds, nfds, pollTimeoutMs); if (ret > 0) { if (static_cast(fds[0].revents) & POLLNVAL) { run_ = false; - return; + break; + } + if (static_cast(fds[0].revents) & POLLIN) { + run_ = false; + break; } - if (static_cast(fds[1].revents) & POLLIN) { ReadNotifyEventLocked(); } continue; } - if (ret == 0) { - // Note: 超时了 continue; } - if (ret < 0 && errno == EINTR) { continue; } - if (ret < 0 && errno != EINTR) { HILOGE("Failed to poll NotifyFd, errno=%{public}d", errno); - return; + break; } } } @@ -312,15 +308,20 @@ bool FsFileWatcher::CheckEventValid(uint32_t event) void FsFileWatcher::DestroyTaskThead() { if (taskThead_.joinable()) { - taskThead_.join(); + if (taskThead_.get_id() != std::this_thread::get_id()) { + taskThead_.join(); + } else { + taskThead_.detach(); + } } - lock_guard lock(taskMutex_); - if (taskRunning_) { - taskRunning_ = false; + { + lock_guard lock(taskMutex_); + if (taskRunning_) { + taskRunning_ = false; + run_ = false; + } } - - // Note: 可能需要加上状态控制 dataCache_.ClearCache(); } } // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h index 5fb73c8b3..9af0f9f38 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h @@ -59,6 +59,8 @@ private: void DestroyTaskThead(); private: + static constexpr int32_t pollTimeoutMs = 500; + mutex taskMutex_; mutex readMutex_; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp index a59c73eb0..83b5e2a51 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_data_cache.cpp @@ -63,10 +63,9 @@ bool WatcherDataCache::RemoveFileWatcher(const std::string &fileName) wdFileNameCache_.erase(iter); watcherInfoCache_.erase(std::remove_if(watcherInfoCache_.begin(), watcherInfoCache_.end(), - [&fileName](const std::shared_ptr &info) { - return info->fileName == fileName; - }), - watcherInfoCache_.end()); + [&fileName](const std::shared_ptr &info) { + return info->fileName == fileName; + }), watcherInfoCache_.end()); return true; } -- Gitee From d67ebeffc95f1788d192b3dc54b7246cb74926ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 18 Jun 2025 12:04:53 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0watcher=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E7=9B=B8=E5=85=B3TDD=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Icf4be11ee68c001ed45274fcd3c3b46610a8862d Signed-off-by: 姜小林 --- .../mod_fs/class_watcher/fs_file_watcher.cpp | 23 + interfaces/test/unittest/js/BUILD.gn | 59 +- .../fs_file_watcher_mock_test.cpp | 1148 +++++++++++++++++ .../class_watcher/fs_watcher_mock_test.cpp | 260 ++++ .../unittest/js/mod_fs/mock/eventfd_mock.cpp | 41 + .../unittest/js/mod_fs/mock/eventfd_mock.h | 51 + .../unittest/js/mod_fs/mock/inotify_mock.cpp | 51 + .../unittest/js/mod_fs/mock/inotify_mock.h | 55 + .../js/mod_fs/mock/mock_watcher_callback.h | 41 + .../unittest/js/mod_fs/mock/poll_mock.cpp | 41 + .../test/unittest/js/mod_fs/mock/poll_mock.h | 51 + .../unittest/js/mod_fs/mock/unistd_mock.cpp | 51 + .../unittest/js/mod_fs/mock/unistd_mock.h | 55 + .../mod_fs/properties/open_core_mock_test.cpp | 17 +- .../properties/watcher_core_mock_test.cpp | 222 ++++ 15 files changed, 2132 insertions(+), 34 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/mock_watcher_callback.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/poll_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp index 44cd49b4c..ebf4a403d 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp @@ -50,6 +50,11 @@ bool FsFileWatcher::InitNotify() int32_t FsFileWatcher::StartNotify(shared_ptr info) { + if (!info) { + HILOGE("Invalid param: info"); + return EINVAL; + } + if (notifyFd_ < 0) { HILOGE("Failed to start notify notifyFd_:%{public}d", notifyFd_); return EIO; @@ -135,6 +140,11 @@ int FsFileWatcher::CloseNotifyFdLocked() int32_t FsFileWatcher::StopNotify(shared_ptr info) { + if (!info) { + HILOGE("Invalid param: info"); + return EINVAL; + } + if (notifyFd_ < 0) { HILOGE("Failed to stop notify notifyFd_:%{public}d", notifyFd_); return EIO; @@ -271,16 +281,29 @@ void FsFileWatcher::GetNotifyEvent() bool FsFileWatcher::AddWatcherInfo(shared_ptr info) { + if (!info) { + HILOGE("Invalid param: info"); + return false; + } return dataCache_.AddWatcherInfo(info); } uint32_t FsFileWatcher::RemoveWatcherInfo(shared_ptr info) { + if (!info) { + HILOGE("Invalid param: info"); + return EINVAL; + } return dataCache_.RemoveWatcherInfo(info); } void FsFileWatcher::NotifyEvent(const struct inotify_event *event) { + if (!event) { + HILOGE("Invalid inotify event"); + return; + } + auto [matched, fileName, watcherInfos] = dataCache_.FindWatcherInfos(event->wd, event->mask); if (!matched) { HILOGE("Cannot find matched watcherInfos"); diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index f3acdf930..8f7978b8c 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -102,12 +102,12 @@ ohos_unittest("ani_file_fs_test") { ] deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_fs", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/native:task_signal_native", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${file_api_path}/interfaces/kits/js:ani_file_fs", ] external_deps = [ @@ -120,9 +120,7 @@ ohos_unittest("ani_file_fs_test") { "libuv:uv", ] - defines = [ - "private=public", - ] + defines = [ "private=public" ] } ohos_unittest("ani_file_fs_mock_test") { @@ -138,8 +136,10 @@ ohos_unittest("ani_file_fs_mock_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/mock", "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", ] @@ -149,30 +149,37 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/class_stat/fs_stat_mock_test.cpp", "mod_fs/class_stream/fs_stream_mock_test.cpp", "mod_fs/class_stream/mock/c_mock.cpp", + "mod_fs/class_watcher/fs_file_watcher_mock_test.cpp", + "mod_fs/class_watcher/fs_watcher_mock_test.cpp", + "mod_fs/mock/eventfd_mock.cpp", + "mod_fs/mock/inotify_mock.cpp", + "mod_fs/mock/poll_mock.cpp", + "mod_fs/mock/unistd_mock.cpp", "mod_fs/properties/access_core_mock_test.cpp", "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", "mod_fs/properties/dup_core_mock_test.cpp", "mod_fs/properties/fdatasync_core_mock_test.cpp", - "mod_fs/properties/read_core_mock_test.cpp", - "mod_fs/properties/read_lines_core_mock_test.cpp", - "mod_fs/properties/symlink_core_mock_test.cpp", - "mod_fs/properties/truncate_core_mock_test.cpp", - "mod_fs/properties/utimes_core_mock_test.cpp", "mod_fs/properties/lstat_core_mock_test.cpp", "mod_fs/properties/mock/system_mock.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", "mod_fs/properties/open_core_mock_test.cpp", + "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/read_lines_core_mock_test.cpp", + "mod_fs/properties/symlink_core_mock_test.cpp", + "mod_fs/properties/truncate_core_mock_test.cpp", "mod_fs/properties/unlink_core_mock_test.cpp", + "mod_fs/properties/utimes_core_mock_test.cpp", + "mod_fs/properties/watcher_core_mock_test.cpp", "mod_fs/properties/xattr_core_mock_test.cpp", ] deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_fs", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/native:task_signal_native", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${file_api_path}/interfaces/kits/js:ani_file_fs", ] external_deps = [ @@ -186,9 +193,7 @@ ohos_unittest("ani_file_fs_mock_test") { "libuv:uv", ] - defines = [ - "private=public", - ] + defines = [ "private=public" ] } ohos_unittest("ani_file_hash_test") { @@ -196,9 +201,7 @@ ohos_unittest("ani_file_hash_test") { resource_config_file = "../resource/ohos_test.xml" - sources = [ - "mod_hash/hash_core_test.cpp", - ] + sources = [ "mod_hash/hash_core_test.cpp" ] include_dirs = [ "mock/libuv", @@ -206,17 +209,17 @@ ohos_unittest("ani_file_hash_test") { ] deps = [ - "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${file_api_path}/interfaces/kits/js:ani_file_hash", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ "c_utils:utils", - "hilog:libhilog", - "libuv:uv", "googletest:gmock_main", "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", ] } @@ -225,9 +228,7 @@ ohos_unittest("ani_file_securitylabel_test") { resource_config_file = "../resource/ohos_test.xml" - sources = [ - "mod_securitylabel/securitylabel_core_test.cpp", - ] + sources = [ "mod_securitylabel/securitylabel_core_test.cpp" ] include_dirs = [ "mock/libuv", @@ -236,16 +237,16 @@ ohos_unittest("ani_file_securitylabel_test") { deps = [ "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", - "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ "c_utils:utils", - "hilog:libhilog", - "libuv:uv", "googletest:gmock_main", "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", ] } @@ -254,9 +255,7 @@ ohos_unittest("ani_file_statvfs_test") { resource_config_file = "../resource/ohos_test.xml" - sources = [ - "mod_statvfs/statvfs_core_test.cpp", - ] + sources = [ "mod_statvfs/statvfs_core_test.cpp" ] include_dirs = [ "mock/libuv", @@ -276,4 +275,4 @@ ohos_unittest("ani_file_statvfs_test") { "hilog:libhilog", "libuv:uv", ] -} \ No newline at end of file +} diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp new file mode 100644 index 000000000..ef6e04858 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp @@ -0,0 +1,1148 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include "eventfd_mock.h" +#include "filemgmt_libhilog.h" +#include "fs_file_watcher.h" +#include "inotify_mock.h" +#include "mock_watcher_callback.h" +#include "poll_mock.h" +#include "securec.h" +#include "unistd_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class FsFileWatcherMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsFileWatcherMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsFileWatcherMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsFileWatcherMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + errno = 0; // Reset errno +} + +void FsFileWatcherMockTest::TearDown(void) +{ + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.taskRunning_ = false; + watcher.run_ = false; + watcher.reading_ = false; + watcher.closed_ = false; + watcher.notifyFd_ = -1; + watcher.eventFd_ = -1; + watcher.dataCache_.ClearCache(); + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyId_001 + * @tc.desc: Test function of FsFileWatcher::GetNotifyId interface. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyId_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyId_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + int32_t expected = -1; + // Do testing + int32_t result = watcher.GetNotifyId(); + // Verify results + EXPECT_EQ(result, expected); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyId_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_InitNotify_001 + * @tc.desc: Test function of FsFileWatcher::InitNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + testing::StrictMock &eventfdMock = static_cast &>(GetEventfdMock()); + EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2)); + // Do testing + bool result = watcher.InitNotify(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(&eventfdMock); + EXPECT_TRUE(result); + EXPECT_EQ(watcher.notifyFd_, 1); + EXPECT_EQ(watcher.eventFd_, 2); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_InitNotify_002 + * @tc.desc: Test function of FsFileWatcher::InitNotify interface for FAILURE when inotify_init fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_002"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + bool result = watcher.InitNotify(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_FALSE(result); + EXPECT_EQ(watcher.notifyFd_, -1); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_InitNotify_003 + * @tc.desc: Test function of FsFileWatcher::InitNotify interface for FAILURE when eventfd fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_003"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + testing::StrictMock &eventfdMock = static_cast &>(GetEventfdMock()); + EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + bool result = watcher.InitNotify(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(&eventfdMock); + EXPECT_FALSE(result); + EXPECT_EQ(watcher.notifyFd_, 1); + EXPECT_EQ(watcher.eventFd_, -1); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StartNotify_001 + * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is not watched. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_001"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StartNotify_001"; + info->events = IN_CREATE | IN_DELETE; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(100)); + // Do testing + int32_t result = watcher.StartNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, ERRNO_NOERR); + EXPECT_EQ(info->wd, 100); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StartNotify_002 + * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is already watched with same + * events. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_002"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StartNotify_002"; + info->events = IN_CREATE | IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).Times(0); + // Do testing + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + int32_t result = watcher.StartNotify(info); + // Verify results + EXPECT_EQ(result, ERRNO_NOERR); + EXPECT_EQ(info->wd, 100); +} + +/** + * @tc.name: FsFileWatcherMockTest_StartNotify_003 + * @tc.desc: Test function of FsFileWatcher::StartNotify interface for FAILURE when info is nullptr. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_003"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Do testing with nullptr parameter + int32_t result = watcher.StartNotify(nullptr); + // Verify results + EXPECT_EQ(result, EINVAL); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StartNotify_004 + * @tc.desc: Test function of FsFileWatcher::StartNotify interface for FAILURE when notifyFd_ is invalid. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_004"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = -1; // Invalid notifyFd + // Build test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StartNotify_004"; + info->events = IN_CREATE; + // Do testing + int32_t result = watcher.StartNotify(info); + // Verify results + EXPECT_EQ(result, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_004"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StartNotify_005 + * @tc.desc: Test function of FsFileWatcher::StartNotify interface for FAILURE when inotify_add_watch fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_005"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + // Build test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StartNotify_005"; + info->events = IN_DELETE; + // Set mock behaviors for inotify_add_watch failure + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + int32_t result = watcher.StartNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_005"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_001 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_001"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_001"; + info->events = IN_CREATE | IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); + EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, ERRNO_NOERR); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_002 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when info is nullptr. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_002"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Do testing + int32_t result = watcher.StopNotify(nullptr); + // Verify results + EXPECT_EQ(result, EINVAL); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_003 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when notifyFd_ is invalid. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_003"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = -1; // Invalid notifyFd + // Build test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_003"; + info->events = IN_CREATE; + info->wd = 100; + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + EXPECT_EQ(result, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_004 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when inotify_rm_watch fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_004"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_004"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); + EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_004"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_005 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS when rm watch fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_005"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_005"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set rm watch fail condition + watcher.closed_ = true; + watcher.reading_ = true; + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); + EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, ERRNO_NOERR); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_005"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_006 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS when having remainingEvents but access + * file failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_006"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_006"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set having remainingEvents condition + auto remainingInfo = std::make_shared(nullptr); + remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_006"; + remainingInfo->events = IN_CREATE; + remainingInfo->wd = 100; + watcher.dataCache_.AddWatcherInfo(remainingInfo); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, ERRNO_NOERR); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_006"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_007 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS when having remainingEvents and + * NotifyToWatchNewEvents success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_007"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_007"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set having remainingEvents condition + auto remainingInfo = std::make_shared(nullptr); + remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_007"; + remainingInfo->events = IN_CREATE; + remainingInfo->wd = 100; + watcher.dataCache_.AddWatcherInfo(remainingInfo); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(100)); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, ERRNO_NOERR); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_007"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_008 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when having remainingEvents but + * NotifyToWatchNewEvents fails for inotify_add_watch fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_008"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_008"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set having remainingEvents condition + auto remainingInfo = std::make_shared(nullptr); + remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_008"; + remainingInfo->events = IN_CREATE; + remainingInfo->wd = 100; + watcher.dataCache_.AddWatcherInfo(remainingInfo); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_008"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_009 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when having remainingEvents but + * NotifyToWatchNewEvents fails for inotify_add_watch return another wd. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_009"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_009"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.dataCache_.AddWatcherInfo(info); + // Set having remainingEvents condition + auto remainingInfo = std::make_shared(nullptr); + remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_009"; + remainingInfo->events = IN_CREATE; + remainingInfo->wd = 100; + watcher.dataCache_.AddWatcherInfo(remainingInfo); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(200)); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_009"; +} + +/** + * @tc.name: FsFileWatcherMockTest_StopNotify_010 + * @tc.desc: Test function of FsFileWatcher::StopNotify interface for FAILURE when CloseNotifyFd fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_010"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StopNotify_010"; + info->events = IN_DELETE; + info->wd = 100; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.eventFd_ = 1; + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); + EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(EIO)); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + // Do testing + int32_t result = watcher.StopNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_EQ(result, EIO); + EXPECT_EQ(watcher.notifyFd_, -1); + EXPECT_EQ(watcher.eventFd_, -1); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_010"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_001 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when run_ is true. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = true; + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)).Times(0); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_002 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret > 0 and fds[0].revents has + * POLLNVAL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_002"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = false; + watcher.notifyFd_ = 1; + watcher.eventFd_ = 2; + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([](struct pollfd *fds, nfds_t n, int timeout) { + fds[0].revents = POLLNVAL; + return 1; + }); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + EXPECT_FALSE(watcher.run_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_003 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret > 0 and fds[0].revents has + * POLLIN. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_003"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = false; + watcher.notifyFd_ = 1; + watcher.eventFd_ = 2; + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([](struct pollfd *fds, nfds_t n, int timeout) { + fds[0].revents = POLLIN; + return 1; + }); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + EXPECT_FALSE(watcher.run_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_004 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret > 0 and fds[1].revents has + * POLLIN. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_004"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = false; + watcher.notifyFd_ = 1; + watcher.eventFd_ = 2; + watcher.closed_ = true; // Avoid calling ReadNotifyEvent + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { + fds[1].revents = POLLIN; + watcher.run_ = false; // Ensure the loop will exit + return 1; + }); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + EXPECT_FALSE(watcher.run_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_004"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_005 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret == 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_005"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = false; + watcher.notifyFd_ = 1; + watcher.eventFd_ = 2; + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { + watcher.run_ = false; // Ensure the loop will exit + return 0; + }); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + EXPECT_FALSE(watcher.run_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_005"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_006 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret < 0 and errno == EINTR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_006"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = false; + watcher.notifyFd_ = 1; + watcher.eventFd_ = 2; + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { + errno = EINTR; + watcher.run_ = false; // Ensure the loop will exit + return -1; + }); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + EXPECT_FALSE(watcher.run_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_006"; +} + +/** + * @tc.name: FsFileWatcherMockTest_GetNotifyEvent_007 + * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when poll returns ret < 0 and errno != EINTR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_007"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.run_ = false; + watcher.notifyFd_ = 1; + watcher.eventFd_ = 2; + // Set mock behaviors + testing::StrictMock &pollMock = static_cast &>(GetPollMock()); + EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { + errno = EIO; + watcher.run_ = false; // Ensure the loop will exit + return -1; + }); + // Do testing + watcher.GetNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&pollMock); + EXPECT_FALSE(watcher.run_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_007"; +} + +/** + * @tc.name: FsFileWatcherMockTest_ReadNotifyEventLocked_001 + * @tc.desc: Test ReadNotifyEventLocked when closed_ is false. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEventLocked_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.closed_ = false; + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + // Do testing + watcher.ReadNotifyEventLocked(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + EXPECT_FALSE(watcher.reading_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEventLocked_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_ReadNotifyEventLocked_002 + * @tc.desc: Test ReadNotifyEventLocked when close after read. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEventLocked_002"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.closed_ = false; + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([&watcher](int fd, void *buf, size_t count) { + errno = EIO; + watcher.closed_ = true; // Set close after read condition + return 0; + }); + EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + // Do testing + watcher.ReadNotifyEventLocked(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + EXPECT_FALSE(watcher.closed_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEventLocked_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_ReadNotifyEvent_001 + * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for SUCCESS when read valid event data. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + int32_t len = static_cast(sizeof(struct inotify_event)); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len)); + // Do testing + watcher.ReadNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_ReadNotifyEvent_002 + * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for FAILURE when read returns -1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_002"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + watcher.ReadNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + EXPECT_EQ(errno, EIO); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_ReadNotifyEvent_003 + * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for SUCCESS when read returns 0 (EOF). + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_003"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Set mock behaviors + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(0, 0)); + // Do testing + watcher.ReadNotifyEvent(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&unistdMock); + EXPECT_EQ(errno, 0); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_NotifyEvent_001 + * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for SUCCESS when valid event without filename. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_001"; + // Prepare test parameters + int32_t wd = 1; + uint32_t mask = IN_CREATE; + struct inotify_event event = { .wd = wd, .mask = mask, .cookie = 0, .len = 0 }; + // Prepare test condition + auto callback = std::make_shared(); + auto info = std::make_shared(callback); + info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_001"; + info->events = mask; + info->wd = wd; + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + auto cbMock = std::dynamic_pointer_cast(callback); + EXPECT_CALL(*cbMock, InvokeCallback(testing::_, testing::_, testing::_)).Times(1); + // Do testing + watcher.NotifyEvent(&event); + // Verify results + testing::Mock::VerifyAndClearExpectations(cbMock.get()); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_NotifyEvent_002 + * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for SUCCESS when valid event with filename. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_002"; + // Prepare test parameters + const char *name = "test.txt"; + size_t len = strlen(name); + int32_t wd = 1; + uint32_t mask = IN_CREATE; + size_t totalSize = sizeof(struct inotify_event) + len + 1; + std::vector buffer(totalSize); + struct inotify_event *event = reinterpret_cast(buffer.data()); + event->wd = wd; + event->mask = mask; + event->cookie = 0; + event->len = len + 1; + char *namePtr = reinterpret_cast(event + 1); + int ret = memcpy_s(namePtr, len + 1, name, len + 1); + if (ret != 0) { + EXPECT_EQ(ret, 0); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_002"; + return; + } + // Prepare test condition + auto callback = std::make_shared(); + auto info = std::make_shared(callback); + info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_002"; + info->events = mask; + info->wd = wd; + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + auto cbMock = std::dynamic_pointer_cast(callback); + EXPECT_CALL(*cbMock, InvokeCallback(testing::_, testing::_, testing::_)).Times(1); + // Do testing + watcher.NotifyEvent(event); + // Verify results + testing::Mock::VerifyAndClearExpectations(cbMock.get()); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_NotifyEvent_003 + * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for FAILURE when event pointer is NULL. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_003"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Do testing + watcher.NotifyEvent(nullptr); + // Verify results + EXPECT_TRUE(watcher.dataCache_.wdFileNameCache_.empty()); + EXPECT_TRUE(watcher.dataCache_.watcherInfoCache_.empty()); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_NotifyEvent_004 + * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for FAILURE when no matched watcher found. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_004"; + // Prepare test parameters + struct inotify_event event = { .wd = 1, .mask = IN_CREATE, .cookie = 0, .len = 0 }; + // Prepare test condition + auto callback = std::make_shared(); + auto info = std::make_shared(callback); + info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_004"; + info->events = IN_MODIFY; // Not matched mask + info->wd = 2; // Not matched wd + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + auto cbMock = std::dynamic_pointer_cast(callback); + EXPECT_CALL(*cbMock, InvokeCallback(testing::_, testing::_, testing::_)).Times(0); + // Do testing + watcher.NotifyEvent(&event); + // Verify results + testing::Mock::VerifyAndClearExpectations(cbMock.get()); + EXPECT_FALSE(watcher.dataCache_.wdFileNameCache_.empty()); + EXPECT_FALSE(watcher.dataCache_.watcherInfoCache_.empty()); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_004"; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp new file mode 100644 index 000000000..c3ba0c4d8 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp @@ -0,0 +1,260 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fs_file_watcher.h" +#include "fs_watcher.h" +#include "inotify_mock.h" +#include "unistd_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class FsWatcherMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsWatcherMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsWatcherMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsWatcherMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + errno = 0; // Reset errno +} + +void FsWatcherMockTest::TearDown(void) +{ + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.taskRunning_ = false; + watcher.run_ = false; + watcher.reading_ = false; + watcher.closed_ = false; + watcher.notifyFd_ = -1; + watcher.eventFd_ = -1; + watcher.dataCache_.ClearCache(); + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsWatcherTest_Constructor_001 + * @tc.desc: Test function of FsWatcher::Constructor interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Constructor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Constructor_001"; + // Do testing + auto result = FsWatcher::Constructor(); + // Verify results + EXPECT_TRUE(result.IsSuccess()); + auto *watcher = result.GetData().value(); + EXPECT_NE(watcher, nullptr); + if (watcher) { + auto *watcherEntity = watcher->GetWatchEntity(); + EXPECT_NE(watcherEntity, nullptr); + } + delete watcher; + watcher = nullptr; + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Constructor_001"; +} + +/** + * @tc.name: FsWatcherTest_Start_001 + * @tc.desc: Test function of FsWatcher::Start interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_001"; + // Prepare test condition + auto watchEntity = CreateUniquePtr(); + FsWatcher fsWatcher(std::move(watchEntity)); + std::shared_ptr info = std::make_shared(nullptr); + fsWatcher.GetWatchEntity()->data_ = info; + // Prepare test condition for FsFileWatcher + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + watcher.taskRunning_ = true; // Avoid starting thread + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(100)); + // Do testing + auto result = fsWatcher.Start(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_TRUE(result.IsSuccess()); + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_001"; +} + +/** + * @tc.name: FsWatcherTest_Start_002 + * @tc.desc: Test function of FsWatcher::Start interface for FAILURE when watchEntity is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_002"; + // Prepare test condition + FsWatcher fsWatcher(nullptr); + // Do testing + auto result = fsWatcher.Start(); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + auto errCode = result.GetError().GetErrNo(); + EXPECT_EQ(errCode, 13900020); + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_002"; +} + +/** + * @tc.name: FsWatcherTest_Start_003 + * @tc.desc: Test function of FsWatcher::Start interface for FAILURE when StartNotify fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_003"; + // Prepare test condition + auto watchEntity = CreateUniquePtr(); + FsWatcher fsWatcher(std::move(watchEntity)); + std::shared_ptr info = std::make_shared(nullptr); + fsWatcher.GetWatchEntity()->data_ = info; + // Prepare test condition for FsFileWatcher + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = -1; // Invalid notifyFd + // Do testing + auto result = fsWatcher.Start(); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + auto errCode = result.GetError().GetErrNo(); + EXPECT_EQ(errCode, 13900005); + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_003"; +} + +/** + * @tc.name: FsWatcherTest_Stop_001 + * @tc.desc: Test function of FsWatcher::Stop interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_001"; + // Prepare test condition + auto watchEntity = CreateUniquePtr(); + FsWatcher fsWatcher(std::move(watchEntity)); + std::shared_ptr info = std::make_shared(nullptr); + fsWatcher.GetWatchEntity()->data_ = info; + // Prepare test condition for FsFileWatcher + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(0, 0)); + EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + // Do testing + auto result = fsWatcher.Stop(); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(&unistdMock); + EXPECT_TRUE(result.IsSuccess()); + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_001"; +} + +/** + * @tc.name: FsWatcherTest_Stop_002 + * @tc.desc: Test function of FsWatcher::Stop interface for FAILURE when watchEntity is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_002"; + // Prepare test condition + FsWatcher fsWatcher(nullptr); + // Do testing + auto result = fsWatcher.Stop(); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + auto errCode = result.GetError().GetErrNo(); + EXPECT_EQ(errCode, 13900020); + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_002"; +} + +/** + * @tc.name: FsWatcherTest_Stop_003 + * @tc.desc: Test function of FsWatcher::Stop interface for FAILURE when StopNotify fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_003"; + // Prepare test condition + auto watchEntity = CreateUniquePtr(); + FsWatcher fsWatcher(std::move(watchEntity)); + std::shared_ptr info = std::make_shared(nullptr); + fsWatcher.GetWatchEntity()->data_ = info; + // Prepare test condition for FsFileWatcher + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = -1; // Invalid notifyFd + // Do testing + auto result = fsWatcher.Stop(); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + auto errCode = result.GetError().GetErrNo(); + EXPECT_EQ(errCode, 13900005); + GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_003"; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp new file mode 100644 index 000000000..ccb3cc777 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "eventfd_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +EventfdMock &GetEventfdMock() +{ + return EventfdMock::GetMock(); +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int eventfd(unsigned int count, int flags) +{ + return GetEventfdMock().eventfd(count, flags); +} + +} // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h new file mode 100644 index 000000000..1a14254e2 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_EVENTFD_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_EVENTFD_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IEventfdMock { +public: + virtual ~IEventfdMock() = default; + virtual int eventfd(unsigned int, int) = 0; +}; + +class EventfdMock : public IEventfdMock { +public: + MOCK_METHOD(int, eventfd, (unsigned int, int), (override)); + + static EventfdMock &GetMock() + { + static EventfdMock mock; + return mock; + } +}; + +EventfdMock &GetEventfdMock(); + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_EVENTFD_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp new file mode 100644 index 000000000..6724290bc --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "inotify_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +InotifyMock &GetInotifyMock() +{ + return InotifyMock::GetMock(); +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int inotify_init() +{ + return GetInotifyMock().inotify_init(); +} + +int inotify_add_watch(int fd, const char *pathname, uint32_t mask) +{ + return GetInotifyMock().inotify_add_watch(fd, pathname, mask); +} + +int inotify_rm_watch(int fd, int wd) +{ + return GetInotifyMock().inotify_rm_watch(fd, wd); +} + +} // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h new file mode 100644 index 000000000..13f0cd3ec --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_INOTIFY_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_INOTIFY_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IInotifyMock { +public: + virtual ~IInotifyMock() = default; + virtual int inotify_init() = 0; + virtual int inotify_add_watch(int, const char *, uint32_t) = 0; + virtual int inotify_rm_watch(int, int) = 0; +}; + +class InotifyMock : public IInotifyMock { +public: + MOCK_METHOD(int, inotify_init, (), (override)); + MOCK_METHOD(int, inotify_add_watch, (int, const char *, uint32_t), (override)); + MOCK_METHOD(int, inotify_rm_watch, (int, int), (override)); + + static InotifyMock &GetMock() + { + static InotifyMock mock; + return mock; + } +}; + +InotifyMock &GetInotifyMock(); + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_INOTIFY_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/mock_watcher_callback.h b/interfaces/test/unittest/js/mod_fs/mock/mock_watcher_callback.h new file mode 100644 index 000000000..c1004d84f --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/mock_watcher_callback.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_MOCK_WATCHER_CALLBACK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_MOCK_WATCHER_CALLBACK_H + +#include +#include +#include "i_watcher_callback.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { +using namespace OHOS::FileManagement::ModuleFileIO; + +class MockWatcherCallback : public IWatcherCallback { +public: + MOCK_METHOD(bool, IsStrictEquals, (const std::shared_ptr &other), (const, override)); + MOCK_METHOD( + void, InvokeCallback, (const std::string &fileName, uint32_t event, uint32_t cookie), (const, override)); + MOCK_METHOD(std::string, GetClassName, (), (const, override)); +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_MOCK_WATCHER_CALLBACK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp new file mode 100644 index 000000000..42bd7f78d --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "poll_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +PollMock &GetPollMock() +{ + return PollMock::GetMock(); +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int poll(struct pollfd *fds, nfds_t n, int timeout) +{ + return GetPollMock().poll(fds, n, timeout); +} + +} // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h new file mode 100644 index 000000000..46859b499 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_POLL_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_POLL_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IPollMock { +public: + virtual ~IPollMock() = default; + virtual int poll(struct pollfd *, nfds_t, int) = 0; +}; + +class PollMock : public IPollMock { +public: + MOCK_METHOD(int, poll, (struct pollfd *, nfds_t, int), (override)); + + static PollMock &GetMock() + { + static PollMock mock; + return mock; + } +}; + +PollMock &GetPollMock(); + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_POLL_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp new file mode 100644 index 000000000..a06922ba3 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "unistd_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +UnistdMock &GetUnistdMock() +{ + return UnistdMock::GetMock(); +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int access(const char *filename, int amode) +{ + return GetUnistdMock().access(filename, amode); +} + +int close(int fd) +{ + return GetUnistdMock().close(fd); +} + +ssize_t read(int fd, void *buf, size_t count) +{ + return GetUnistdMock().read(fd, buf, count); +} + +} // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h new file mode 100644 index 000000000..c442c194d --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IUnistdMock { +public: + virtual ~IUnistdMock() = default; + virtual int access(const char *, int) = 0; + virtual int close(int) = 0; + virtual ssize_t read(int, void *, size_t) = 0; +}; + +class UnistdMock : public IUnistdMock { +public: + MOCK_METHOD(int, access, (const char *, int), (override)); + MOCK_METHOD(int, close, (int), (override)); + MOCK_METHOD(ssize_t, read, (int, void *, size_t), (override)); + + static UnistdMock &GetMock() + { + static UnistdMock mock; + return mock; + } +}; + +UnistdMock &GetUnistdMock(); + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp index 33f25f3cd..284a71160 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp @@ -13,14 +13,15 @@ * limitations under the License. */ -#include "mock/uv_fs_mock.h" -#include "open_core.h" - #include #include #include #include +#include "mock/uv_fs_mock.h" +#include "open_core.h" +#include "unistd_mock.h" + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -72,7 +73,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.L string path = "/test/open_test.txt"; int32_t mode = 0; - + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); auto res = OpenCore::DoOpen(path, mode); EXPECT_EQ(res.IsSuccess(), true); @@ -94,8 +95,12 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_002, testing::ext::TestSize.L string path = "file://test/open_test.txt"; int32_t mode = 0; + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); auto res = OpenCore::DoOpen(path, mode); + testing::Mock::VerifyAndClearExpectations(&unistdMock); EXPECT_EQ(res.IsSuccess(), true); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_002"; @@ -115,8 +120,12 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_003, testing::ext::TestSize.L string path = "file://test/open_test.txt"; int32_t mode = 0; + testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); auto res = OpenCore::DoOpen(path, mode); + testing::Mock::VerifyAndClearExpectations(&unistdMock); EXPECT_EQ(res.IsSuccess(), false); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_003"; diff --git a/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp new file mode 100644 index 000000000..a32f49161 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include "eventfd_mock.h" +#include "filemgmt_libhilog.h" +#include "fs_file_watcher.h" +#include "inotify_mock.h" +#include "mock_watcher_callback.h" +#include "watcher_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class WatcherCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void WatcherCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void WatcherCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void WatcherCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + errno = 0; // Reset errno +} + +void WatcherCoreMockTest::TearDown(void) +{ + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.taskRunning_ = false; + watcher.run_ = false; + watcher.reading_ = false; + watcher.closed_ = false; + watcher.notifyFd_ = -1; + watcher.eventFd_ = -1; + watcher.dataCache_.ClearCache(); + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: WatcherCoreMockTest_DoCreateWatcher_001 + * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_001"; + // Prepare test parameters + std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_001"; + int32_t events = IN_CREATE; + std::shared_ptr callback = std::make_shared(); + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + testing::StrictMock &eventfdMock = static_cast &>(GetEventfdMock()); + EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2)); + // Do testing + auto result = WatcherCore::DoCreateWatcher(path, events, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(&eventfdMock); + EXPECT_TRUE(result.IsSuccess()); + GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_001"; +} + +/** + * @tc.name: WatcherCoreMockTest_DoCreateWatcher_002 + * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when InitNotify fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_002"; + // Prepare test parameters + std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_002"; + int32_t events = IN_CREATE; + std::shared_ptr callback = std::make_shared(); + // Set mock behaviors + testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); + EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + auto result = WatcherCore::DoCreateWatcher(path, events, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(&inotifyMock); + EXPECT_FALSE(result.IsSuccess()); + GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_002"; +} + +/** + * @tc.name: WatcherCoreMockTest_DoCreateWatcher_003 + * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when events are invalid. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_003"; + // Prepare test parameters + std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_003"; + int32_t events = -1; + std::shared_ptr callback = std::make_shared(); + // Do testing + auto result = WatcherCore::DoCreateWatcher(path, events, callback); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_003"; +} + +/** + * @tc.name: WatcherCoreMockTest_DoCreateWatcher_004 + * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when CheckEventValid false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_004"; + // Prepare test parameters + std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_004"; + int32_t events = 9999; + std::shared_ptr callback = std::make_shared(); + // Do testing + auto result = WatcherCore::DoCreateWatcher(path, events, callback); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_004"; +} + +/** + * @tc.name: WatcherCoreMockTest_DoCreateWatcher_005 + * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when callback is null. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_005"; + // Prepare test parameters + std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_005"; + int32_t events = IN_CREATE; + std::shared_ptr callback = nullptr; + // Do testing + auto result = WatcherCore::DoCreateWatcher(path, events, callback); + // Verify results + EXPECT_FALSE(result.IsSuccess()); + GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_005"; +} + +/** + * @tc.name: WatcherCoreMockTest_DoCreateWatcher_006 + * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when AddWatcherInfo fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_006"; + // Prepare test parameters + std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_006"; + int32_t events = IN_CREATE; + std::shared_ptr callback = std::make_shared(); + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + auto info = std::make_shared(callback); + info->fileName = "/test/WatcherCoreMockTest_DoCreateWatcher_006"; + info->events = IN_CREATE; + info->wd = 100; + watcher.dataCache_.AddWatcherInfo(info); + // Set mock behaviors + auto cbMock = std::dynamic_pointer_cast(callback); + EXPECT_CALL(*cbMock, IsStrictEquals(testing::_)).Times(1).WillOnce(testing::Return(true)); + // Do testing + auto result = WatcherCore::DoCreateWatcher(path, events, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(cbMock.get()); + EXPECT_FALSE(result.IsSuccess()); + GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_006"; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file -- Gitee From 9ce0b468a9662398d705d5c8a157a9872ef710ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Tue, 24 Jun 2025 11:52:25 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81=E5=AE=8C=E6=88=90=E7=9B=B8=E5=BA=94=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I515fa42ee2d3c40fd1199046a4dc24e3e5d923e0 Signed-off-by: 姜小林 --- .../mod_fs/class_watcher/fs_file_watcher.cpp | 30 +- .../mod_fs/class_watcher/fs_file_watcher.h | 12 +- interfaces/test/unittest/js/BUILD.gn | 5 +- .../fs_file_watcher_mock_test.cpp | 367 +++++++++--------- .../class_watcher/fs_watcher_mock_test.cpp | 46 ++- .../unittest/js/mod_fs/common/fs_err_code.h | 34 ++ .../unittest/js/mod_fs/mock/eventfd_mock.cpp | 16 +- .../unittest/js/mod_fs/mock/eventfd_mock.h | 13 +- .../unittest/js/mod_fs/mock/inotify_mock.cpp | 20 +- .../unittest/js/mod_fs/mock/inotify_mock.h | 13 +- .../unittest/js/mod_fs/mock/poll_mock.cpp | 16 +- .../test/unittest/js/mod_fs/mock/poll_mock.h | 13 +- .../unittest/js/mod_fs/mock/unistd_mock.cpp | 20 +- .../unittest/js/mod_fs/mock/unistd_mock.h | 15 +- .../mod_fs/properties/open_core_mock_test.cpp | 25 +- .../properties/watcher_core_mock_test.cpp | 31 +- .../properties/xattr_core_mock_test.cpp | 4 +- 17 files changed, 387 insertions(+), 293 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/common/fs_err_code.h diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp index ebf4a403d..9f77d49df 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.cpp @@ -125,7 +125,7 @@ int32_t FsFileWatcher::CloseNotifyFd() return closeRet; } -int FsFileWatcher::CloseNotifyFdLocked() +int32_t FsFileWatcher::CloseNotifyFdLocked() { { lock_guard lock(readMutex_); @@ -152,6 +152,7 @@ int32_t FsFileWatcher::StopNotify(shared_ptr info) uint32_t remainingEvents = RemoveWatcherInfo(info); if (remainingEvents > 0) { + // There are still events remaining to be listened for. if (access(info->fileName.c_str(), F_OK) == 0) { return NotifyToWatchNewEvents(info->fileName, info->wd, remainingEvents); } @@ -159,6 +160,7 @@ int32_t FsFileWatcher::StopNotify(shared_ptr info) return ERRNO_NOERR; } + // No events remain to be listened for, and proceed to the file watch removal process. int32_t oldWd = -1; { lock_guard lock(readMutex_); @@ -169,15 +171,18 @@ int32_t FsFileWatcher::StopNotify(shared_ptr info) } } - int32_t rmRet = 0; if (oldWd == -1) { - rmRet = errno; - HILOGE("Failed to stop notify errCode:%{public}d", rmRet); + int32_t rmErr = errno; + if (access(info->fileName.c_str(), F_OK) == 0) { + HILOGE("Failed to stop notify errCode:%{public}d", rmErr); + dataCache_.RemoveFileWatcher(info->fileName); + CloseNotifyFdLocked(); + return rmErr; + } } dataCache_.RemoveFileWatcher(info->fileName); - int32_t closeRet = CloseNotifyFdLocked(); - return rmRet != 0 ? rmRet : closeRet; + return CloseNotifyFdLocked(); } void FsFileWatcher::ReadNotifyEvent() @@ -243,7 +248,7 @@ void FsFileWatcher::GetNotifyEvent() run_ = true; nfds_t nfds = 2; - struct pollfd fds[2] = { { 0 } }; + struct pollfd fds[2] = { { -1 } }; fds[0].fd = eventFd_; fds[0].events = POLLIN; fds[1].fd = notifyFd_; @@ -264,18 +269,11 @@ void FsFileWatcher::GetNotifyEvent() if (static_cast(fds[1].revents) & POLLIN) { ReadNotifyEventLocked(); } - continue; - } - if (ret == 0) { - continue; - } - if (ret < 0 && errno == EINTR) { - continue; - } - if (ret < 0 && errno != EINTR) { + } else if (ret < 0 && errno != EINTR) { HILOGE("Failed to poll NotifyFd, errno=%{public}d", errno); break; } + // Ignore cases where poll returns 0 (timeout) or EINTR (interrupted system call) } } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h index 9af0f9f38..57b83d331 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_file_watcher.h @@ -29,14 +29,14 @@ namespace OHOS::FileManagement::ModuleFileIO { using namespace std; -constexpr int BUF_SIZE = 1024; +constexpr int32_t BUF_SIZE = 1024; class FsFileWatcher : public Singleton { public: int32_t GetNotifyId(); bool InitNotify(); - int StartNotify(shared_ptr info); - int StopNotify(shared_ptr info); + int32_t StartNotify(shared_ptr info); + int32_t StopNotify(shared_ptr info); void GetNotifyEvent(); void AsyncGetNotifyEvent(); bool AddWatcherInfo(shared_ptr info); @@ -51,9 +51,9 @@ public: private: uint32_t RemoveWatcherInfo(shared_ptr info); void NotifyEvent(const struct inotify_event *event); - int CloseNotifyFd(); - int CloseNotifyFdLocked(); - int NotifyToWatchNewEvents(const string &fileName, int wd, uint32_t watchEvents); + int32_t CloseNotifyFd(); + int32_t CloseNotifyFdLocked(); + int32_t NotifyToWatchNewEvents(const string &fileName, int32_t wd, uint32_t watchEvents); void ReadNotifyEvent(); void ReadNotifyEventLocked(); void DestroyTaskThead(); diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 8f7978b8c..71d82ce8f 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -89,10 +89,10 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/dup_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", - "mod_fs/properties/read_core_test.cpp", - "mod_fs/properties/read_lines_core_test.cpp", "mod_fs/properties/lstat_core_test.cpp", "mod_fs/properties/open_core_test.cpp", + "mod_fs/properties/read_core_test.cpp", + "mod_fs/properties/read_lines_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", "mod_fs/properties/rmdir_core_test.cpp", "mod_fs/properties/truncate_core_test.cpp", @@ -139,6 +139,7 @@ ohos_unittest("ani_file_fs_mock_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/common", "${file_api_path}/interfaces/test/unittest/js/mod_fs/mock", "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", ] diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp index ef6e04858..adf4db15e 100644 --- a/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_file_watcher_mock_test.cpp @@ -48,6 +48,10 @@ void FsFileWatcherMockTest::SetUpTestCase(void) void FsFileWatcherMockTest::TearDownTestCase(void) { + EventfdMock::DestroyMock(); + InotifyMock::DestroyMock(); + PollMock::DestroyMock(); + UnistdMock::DestroyMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -75,9 +79,9 @@ void FsFileWatcherMockTest::TearDown(void) * @tc.desc: Test function of FsFileWatcher::GetNotifyId interface. * @tc.size: SMALL * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyId_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyId_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyId_001"; // Prepare test condition @@ -95,23 +99,23 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyId_001, testing:: * @tc.desc: Test function of FsFileWatcher::InitNotify interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_InitNotify_001"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - testing::StrictMock &eventfdMock = static_cast &>(GetEventfdMock()); - EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); - EXPECT_CALL(eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2)); + auto eventfdMock = EventfdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(*eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2)); // Do testing bool result = watcher.InitNotify(); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); - testing::Mock::VerifyAndClearExpectations(&eventfdMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); + testing::Mock::VerifyAndClearExpectations(eventfdMock.get()); EXPECT_TRUE(result); EXPECT_EQ(watcher.notifyFd_, 1); EXPECT_EQ(watcher.eventFd_, 2); @@ -131,12 +135,12 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_002, testing::e // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing bool result = watcher.InitNotify(); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_FALSE(result); EXPECT_EQ(watcher.notifyFd_, -1); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_InitNotify_002"; @@ -155,15 +159,15 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_003, testing::e // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - testing::StrictMock &eventfdMock = static_cast &>(GetEventfdMock()); - EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); - EXPECT_CALL(eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + auto inotifyMock = InotifyMock::GetMock(); + auto eventfdMock = EventfdMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(*eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing bool result = watcher.InitNotify(); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); - testing::Mock::VerifyAndClearExpectations(&eventfdMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); + testing::Mock::VerifyAndClearExpectations(eventfdMock.get()); EXPECT_FALSE(result); EXPECT_EQ(watcher.notifyFd_, 1); EXPECT_EQ(watcher.eventFd_, -1); @@ -175,9 +179,9 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_InitNotify_003, testing::e * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is not watched. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_001"; // Prepare test parameters @@ -188,16 +192,17 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing:: FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + int32_t expectedWd = 100; + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(100)); + .WillOnce(testing::Return(expectedWd)); // Do testing int32_t result = watcher.StartNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); - EXPECT_EQ(info->wd, 100); + EXPECT_EQ(info->wd, expectedWd); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_001"; } @@ -207,29 +212,30 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing:: * events. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_002, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_002, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_002"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StartNotify_002"; info->events = IN_CREATE | IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).Times(0); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).Times(0); // Do testing - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); int32_t result = watcher.StartNotify(info); // Verify results EXPECT_EQ(result, ERRNO_NOERR); - EXPECT_EQ(info->wd, 100); + EXPECT_EQ(info->wd, expectedWd); } /** @@ -293,14 +299,14 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_005, testing:: info->fileName = "/test/FsFileWatcherMockTest_StartNotify_005"; info->events = IN_DELETE; // Set mock behaviors for inotify_add_watch failure - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing int32_t result = watcher.StartNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, EIO); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_005"; } @@ -310,31 +316,32 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_005, testing:: * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_001"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_001"; info->events = IN_CREATE | IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); - EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(0); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_001"; } @@ -371,11 +378,12 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_003, testing::e // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = -1; // Invalid notifyFd - // Build test parameters + // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_003"; info->events = IN_CREATE; - info->wd = 100; + info->wd = expectedWd; // Do testing int32_t result = watcher.StopNotify(info); // Verify results @@ -394,27 +402,28 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_004, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_004"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_004"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); - EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)) + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)) .Times(1) .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, EIO); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_004"; } @@ -430,10 +439,11 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_005, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_005"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_005"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd @@ -442,16 +452,16 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_005, testing::e watcher.closed_ = true; watcher.reading_ = true; // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); - EXPECT_CALL(unistdMock, close(testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_005"; } @@ -468,10 +478,11 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_006, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_006"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_006"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd @@ -480,19 +491,19 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_006, testing::e auto remainingInfo = std::make_shared(nullptr); remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_006"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = 100; + remainingInfo->wd = expectedWd; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); - EXPECT_CALL(unistdMock, close(testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_006"; } @@ -509,10 +520,11 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_007, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_007"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_007"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd @@ -521,22 +533,22 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_007, testing::e auto remainingInfo = std::make_shared(nullptr); remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_007"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = 100; + remainingInfo->wd = expectedWd; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); - EXPECT_CALL(unistdMock, close(testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(100)); + .WillOnce(testing::Return(expectedWd)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_007"; } @@ -553,10 +565,11 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_008, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_008"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_008"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd @@ -565,22 +578,22 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_008, testing::e auto remainingInfo = std::make_shared(nullptr); remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_008"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = 100; + remainingInfo->wd = expectedWd; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); - EXPECT_CALL(unistdMock, close(testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, EIO); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_008"; } @@ -597,10 +610,11 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_009, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_009"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_009"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd @@ -609,22 +623,23 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_009, testing::e auto remainingInfo = std::make_shared(nullptr); remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_009"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = 100; + remainingInfo->wd = expectedWd; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); - EXPECT_CALL(unistdMock, close(testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + int32_t unexpectedWd = 200; + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(200)); + .WillOnce(testing::Return(unexpectedWd)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, EIO); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StopNotify_009"; } @@ -640,26 +655,27 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_010, testing::e { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_010"; // Prepare test parameters + int32_t expectedWd = 100; auto info = std::make_shared(nullptr); info->fileName = "/test/FsFileWatcherMockTest_StopNotify_010"; info->events = IN_DELETE; - info->wd = 100; + info->wd = expectedWd; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd watcher.eventFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).Times(0); - EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(EIO)); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + auto unistdMock = UnistdMock::GetMock(); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(0); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(EIO)); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, EIO); EXPECT_EQ(watcher.notifyFd_, -1); EXPECT_EQ(watcher.eventFd_, -1); @@ -671,21 +687,21 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_010, testing::e * @tc.desc: Test function of FsFileWatcher::GetNotifyEvent interface when run_ is true. * @tc.size: SMALL * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_GetNotifyEvent_001"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.run_ = true; // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)).Times(0); + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)).Times(0); // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_001"; } @@ -706,8 +722,8 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_002, testin watcher.notifyFd_ = 1; watcher.eventFd_ = 2; // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([](struct pollfd *fds, nfds_t n, int timeout) { fds[0].revents = POLLNVAL; @@ -716,7 +732,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_002, testin // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); EXPECT_FALSE(watcher.run_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_002"; } @@ -738,8 +754,8 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_003, testin watcher.notifyFd_ = 1; watcher.eventFd_ = 2; // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([](struct pollfd *fds, nfds_t n, int timeout) { fds[0].revents = POLLIN; @@ -748,7 +764,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_003, testin // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); EXPECT_FALSE(watcher.run_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_003"; } @@ -771,8 +787,8 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_004, testin watcher.eventFd_ = 2; watcher.closed_ = true; // Avoid calling ReadNotifyEvent // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { fds[1].revents = POLLIN; @@ -782,7 +798,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_004, testin // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); EXPECT_FALSE(watcher.run_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_004"; } @@ -803,8 +819,8 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_005, testin watcher.notifyFd_ = 1; watcher.eventFd_ = 2; // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { watcher.run_ = false; // Ensure the loop will exit @@ -813,7 +829,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_005, testin // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); EXPECT_FALSE(watcher.run_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_005"; } @@ -834,8 +850,8 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_006, testin watcher.notifyFd_ = 1; watcher.eventFd_ = 2; // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { errno = EINTR; @@ -845,7 +861,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_006, testin // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); EXPECT_FALSE(watcher.run_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_006"; } @@ -866,8 +882,8 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_007, testin watcher.notifyFd_ = 1; watcher.eventFd_ = 2; // Set mock behaviors - testing::StrictMock &pollMock = static_cast &>(GetPollMock()); - EXPECT_CALL(pollMock, poll(testing::_, testing::_, testing::_)) + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([&watcher](struct pollfd *fds, nfds_t n, int timeout) { errno = EIO; @@ -877,7 +893,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_007, testin // Do testing watcher.GetNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&pollMock); + testing::Mock::VerifyAndClearExpectations(pollMock.get()); EXPECT_FALSE(watcher.run_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_GetNotifyEvent_007"; } @@ -887,22 +903,22 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_GetNotifyEvent_007, testin * @tc.desc: Test ReadNotifyEventLocked when closed_ is false. * @tc.size: SMALL * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEventLocked_001"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.closed_ = false; // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); - EXPECT_CALL(unistdMock, close(testing::_)).Times(0); + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); // Do testing watcher.ReadNotifyEventLocked(); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_FALSE(watcher.reading_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEventLocked_001"; } @@ -921,19 +937,19 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_002, FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.closed_ = false; // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)) + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) .Times(1) .WillOnce([&watcher](int fd, void *buf, size_t count) { errno = EIO; watcher.closed_ = true; // Set close after read condition return 0; }); - EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); // Do testing watcher.ReadNotifyEventLocked(); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_FALSE(watcher.closed_); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEventLocked_002"; } @@ -943,21 +959,21 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEventLocked_002, * @tc.desc: Test function of FsFileWatcher::ReadNotifyEvent interface for SUCCESS when read valid event data. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_ReadNotifyEvent_001"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); int32_t len = static_cast(sizeof(struct inotify_event)); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len)); + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len)); // Do testing watcher.ReadNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_001"; } @@ -974,14 +990,14 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_002, testi // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)) + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) .Times(1) .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing watcher.ReadNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_EQ(errno, EIO); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_002"; } @@ -999,14 +1015,14 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_003, testi // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); // Set mock behaviors - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)) + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) .Times(1) .WillOnce(testing::SetErrnoAndReturn(0, 0)); // Do testing watcher.ReadNotifyEvent(); // Verify results - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_EQ(errno, 0); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_ReadNotifyEvent_003"; } @@ -1016,30 +1032,29 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_ReadNotifyEvent_003, testi * @tc.desc: Test function of FsFileWatcher::NotifyEvent interface for SUCCESS when valid event without filename. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_001, testing::ext::TestSize.Level1) +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_001"; // Prepare test parameters - int32_t wd = 1; + int32_t expectedWd = 100; uint32_t mask = IN_CREATE; - struct inotify_event event = { .wd = wd, .mask = mask, .cookie = 0, .len = 0 }; + struct inotify_event event = { .wd = expectedWd, .mask = mask, .cookie = 0, .len = 0 }; // Prepare test condition auto callback = std::make_shared(); auto info = std::make_shared(callback); info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_001"; info->events = mask; - info->wd = wd; + info->wd = expectedWd; FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - auto cbMock = std::dynamic_pointer_cast(callback); - EXPECT_CALL(*cbMock, InvokeCallback(testing::_, testing::_, testing::_)).Times(1); + EXPECT_CALL(*callback, InvokeCallback(testing::_, testing::_, testing::_)).Times(1); // Do testing watcher.NotifyEvent(&event); // Verify results - testing::Mock::VerifyAndClearExpectations(cbMock.get()); + testing::Mock::VerifyAndClearExpectations(callback.get()); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_001"; } @@ -1054,14 +1069,14 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_002, testing:: { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_002"; // Prepare test parameters + int32_t expectedWd = 100; const char *name = "test.txt"; size_t len = strlen(name); - int32_t wd = 1; uint32_t mask = IN_CREATE; size_t totalSize = sizeof(struct inotify_event) + len + 1; std::vector buffer(totalSize); struct inotify_event *event = reinterpret_cast(buffer.data()); - event->wd = wd; + event->wd = expectedWd; event->mask = mask; event->cookie = 0; event->len = len + 1; @@ -1077,16 +1092,15 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_002, testing:: auto info = std::make_shared(callback); info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_002"; info->events = mask; - info->wd = wd; + info->wd = expectedWd; FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - auto cbMock = std::dynamic_pointer_cast(callback); - EXPECT_CALL(*cbMock, InvokeCallback(testing::_, testing::_, testing::_)).Times(1); + EXPECT_CALL(*callback, InvokeCallback(testing::_, testing::_, testing::_)).Times(1); // Do testing watcher.NotifyEvent(event); // Verify results - testing::Mock::VerifyAndClearExpectations(cbMock.get()); + testing::Mock::VerifyAndClearExpectations(callback.get()); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_002"; } @@ -1121,22 +1135,23 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_004, testing:: { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_004"; // Prepare test parameters - struct inotify_event event = { .wd = 1, .mask = IN_CREATE, .cookie = 0, .len = 0 }; + int32_t expectedWd = 100; + int32_t unexpectedWd = 200; + struct inotify_event event = { .wd = expectedWd, .mask = IN_CREATE, .cookie = 0, .len = 0 }; // Prepare test condition auto callback = std::make_shared(); auto info = std::make_shared(callback); info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_004"; info->events = IN_MODIFY; // Not matched mask - info->wd = 2; // Not matched wd + info->wd = unexpectedWd; // Not matched wd FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors - auto cbMock = std::dynamic_pointer_cast(callback); - EXPECT_CALL(*cbMock, InvokeCallback(testing::_, testing::_, testing::_)).Times(0); + EXPECT_CALL(*callback, InvokeCallback(testing::_, testing::_, testing::_)).Times(0); // Do testing watcher.NotifyEvent(&event); // Verify results - testing::Mock::VerifyAndClearExpectations(cbMock.get()); + testing::Mock::VerifyAndClearExpectations(callback.get()); EXPECT_FALSE(watcher.dataCache_.wdFileNameCache_.empty()); EXPECT_FALSE(watcher.dataCache_.watcherInfoCache_.empty()); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_004"; diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp index c3ba0c4d8..74a74b809 100644 --- a/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/fs_watcher_mock_test.cpp @@ -20,6 +20,7 @@ #include "file_utils.h" #include "filemgmt_libhilog.h" +#include "fs_err_code.h" #include "fs_file_watcher.h" #include "fs_watcher.h" #include "inotify_mock.h" @@ -45,6 +46,8 @@ void FsWatcherMockTest::SetUpTestCase(void) void FsWatcherMockTest::TearDownTestCase(void) { + InotifyMock::DestroyMock(); + UnistdMock::DestroyMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -72,9 +75,9 @@ void FsWatcherMockTest::TearDown(void) * @tc.desc: Test function of FsWatcher::Constructor interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsWatcherMockTest, FsWatcherTest_Constructor_001, testing::ext::TestSize.Level1) +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Constructor_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Constructor_001"; // Do testing @@ -97,12 +100,13 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Constructor_001, testing::ext::TestSiz * @tc.desc: Test function of FsWatcher::Start interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_001, testing::ext::TestSize.Level1) +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_001"; // Prepare test condition + int32_t expectedWd = 100; auto watchEntity = CreateUniquePtr(); FsWatcher fsWatcher(std::move(watchEntity)); std::shared_ptr info = std::make_shared(nullptr); @@ -112,14 +116,14 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_001, testing::ext::TestSize.Leve watcher.notifyFd_ = 1; // Valid notifyFd watcher.taskRunning_ = true; // Avoid starting thread // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(100)); + .WillOnce(testing::Return(expectedWd)); // Do testing auto result = fsWatcher.Start(); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_TRUE(result.IsSuccess()); GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_001"; } @@ -141,7 +145,7 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_002, testing::ext::TestSize.Leve // Verify results EXPECT_FALSE(result.IsSuccess()); auto errCode = result.GetError().GetErrNo(); - EXPECT_EQ(errCode, 13900020); + EXPECT_EQ(errCode, E_INVAL_CODE); GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_002"; } @@ -168,7 +172,7 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_003, testing::ext::TestSize.Leve // Verify results EXPECT_FALSE(result.IsSuccess()); auto errCode = result.GetError().GetErrNo(); - EXPECT_EQ(errCode, 13900005); + EXPECT_EQ(errCode, E_IO_CODE); GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_003"; } @@ -177,9 +181,9 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_003, testing::ext::TestSize.Leve * @tc.desc: Test function of FsWatcher::Stop interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_001, testing::ext::TestSize.Level1) +HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_001"; // Prepare test condition @@ -189,19 +193,19 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_001, testing::ext::TestSize.Level fsWatcher.GetWatchEntity()->data_ = info; // Prepare test condition for FsFileWatcher FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); - EXPECT_CALL(inotifyMock, inotify_rm_watch(testing::_, testing::_)) + auto inotifyMock = InotifyMock::GetMock(); + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)) .Times(1) .WillOnce(testing::SetErrnoAndReturn(0, 0)); - EXPECT_CALL(unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0)); // Do testing auto result = fsWatcher.Stop(); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_TRUE(result.IsSuccess()); GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_001"; } @@ -223,7 +227,7 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_002, testing::ext::TestSize.Level // Verify results EXPECT_FALSE(result.IsSuccess()); auto errCode = result.GetError().GetErrNo(); - EXPECT_EQ(errCode, 13900020); + EXPECT_EQ(errCode, E_INVAL_CODE); GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_002"; } @@ -250,7 +254,7 @@ HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_003, testing::ext::TestSize.Level // Verify results EXPECT_FALSE(result.IsSuccess()); auto errCode = result.GetError().GetErrNo(); - EXPECT_EQ(errCode, 13900005); + EXPECT_EQ(errCode, E_IO_CODE); GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_003"; } diff --git a/interfaces/test/unittest/js/mod_fs/common/fs_err_code.h b/interfaces/test/unittest/js/mod_fs/common/fs_err_code.h new file mode 100644 index 000000000..7e8a4cbbd --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/common/fs_err_code.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_COMMON_FS_ERR_CODE_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_COMMON_FS_ERR_CODE_H + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +enum FsErrCode { + E_IO_CODE = 13900005, + E_INVAL_CODE = 13900020, +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_COMMON_FS_ERR_CODE_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp index ccb3cc777..3406f521c 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp @@ -20,9 +20,19 @@ namespace FileManagement { namespace ModuleFileIO { namespace Test { -EventfdMock &GetEventfdMock() +thread_local std::shared_ptr EventfdMock::eventfdMock = nullptr; + +std::shared_ptr EventfdMock::GetMock() +{ + if (eventfdMock == nullptr) { + eventfdMock = std::make_shared(); + } + return eventfdMock; +} + +void EventfdMock::DestroyMock() { - return EventfdMock::GetMock(); + eventfdMock = nullptr; } } // namespace Test @@ -35,7 +45,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::Test; int eventfd(unsigned int count, int flags) { - return GetEventfdMock().eventfd(count, flags); + return EventfdMock::GetMock()->eventfd(count, flags); } } // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h index 1a14254e2..e1bbd820d 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h @@ -34,14 +34,13 @@ class EventfdMock : public IEventfdMock { public: MOCK_METHOD(int, eventfd, (unsigned int, int), (override)); - static EventfdMock &GetMock() - { - static EventfdMock mock; - return mock; - } -}; +public: + static std::shared_ptr GetMock(); + static void DestroyMock(); -EventfdMock &GetEventfdMock(); +private: + static thread_local std::shared_ptr eventfdMock; +}; } // namespace Test } // namespace ModuleFileIO diff --git a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp index 6724290bc..6c4deb01a 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp @@ -20,9 +20,19 @@ namespace FileManagement { namespace ModuleFileIO { namespace Test { -InotifyMock &GetInotifyMock() +thread_local std::shared_ptr InotifyMock::inotifyMock = nullptr; + +std::shared_ptr InotifyMock::GetMock() +{ + if (inotifyMock == nullptr) { + inotifyMock = std::make_shared(); + } + return inotifyMock; +} + +void InotifyMock::DestroyMock() { - return InotifyMock::GetMock(); + inotifyMock = nullptr; } } // namespace Test @@ -35,17 +45,17 @@ using namespace OHOS::FileManagement::ModuleFileIO::Test; int inotify_init() { - return GetInotifyMock().inotify_init(); + return InotifyMock::GetMock()->inotify_init(); } int inotify_add_watch(int fd, const char *pathname, uint32_t mask) { - return GetInotifyMock().inotify_add_watch(fd, pathname, mask); + return InotifyMock::GetMock()->inotify_add_watch(fd, pathname, mask); } int inotify_rm_watch(int fd, int wd) { - return GetInotifyMock().inotify_rm_watch(fd, wd); + return InotifyMock::GetMock()->inotify_rm_watch(fd, wd); } } // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h index 13f0cd3ec..188f63a28 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h @@ -38,14 +38,13 @@ public: MOCK_METHOD(int, inotify_add_watch, (int, const char *, uint32_t), (override)); MOCK_METHOD(int, inotify_rm_watch, (int, int), (override)); - static InotifyMock &GetMock() - { - static InotifyMock mock; - return mock; - } -}; +public: + static std::shared_ptr GetMock(); + static void DestroyMock(); -InotifyMock &GetInotifyMock(); +private: + static thread_local std::shared_ptr inotifyMock; +}; } // namespace Test } // namespace ModuleFileIO diff --git a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp index 42bd7f78d..fa66cf055 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp @@ -20,9 +20,19 @@ namespace FileManagement { namespace ModuleFileIO { namespace Test { -PollMock &GetPollMock() +thread_local std::shared_ptr PollMock::pollMock = nullptr; + +std::shared_ptr PollMock::GetMock() +{ + if (pollMock == nullptr) { + pollMock = std::make_shared(); + } + return pollMock; +} + +void PollMock::DestroyMock() { - return PollMock::GetMock(); + pollMock = nullptr; } } // namespace Test @@ -35,7 +45,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::Test; int poll(struct pollfd *fds, nfds_t n, int timeout) { - return GetPollMock().poll(fds, n, timeout); + return PollMock::GetMock()->poll(fds, n, timeout); } } // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h index 46859b499..0dd005710 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h @@ -34,14 +34,13 @@ class PollMock : public IPollMock { public: MOCK_METHOD(int, poll, (struct pollfd *, nfds_t, int), (override)); - static PollMock &GetMock() - { - static PollMock mock; - return mock; - } -}; +public: + static std::shared_ptr GetMock(); + static void DestroyMock(); -PollMock &GetPollMock(); +private: + static thread_local std::shared_ptr pollMock; +}; } // namespace Test } // namespace ModuleFileIO diff --git a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp index a06922ba3..bb646967b 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp @@ -20,9 +20,19 @@ namespace FileManagement { namespace ModuleFileIO { namespace Test { -UnistdMock &GetUnistdMock() +thread_local std::shared_ptr UnistdMock::unistdMock = nullptr; + +std::shared_ptr UnistdMock::GetMock() +{ + if (unistdMock == nullptr) { + unistdMock = std::make_shared(); + } + return unistdMock; +} + +void UnistdMock::DestroyMock() { - return UnistdMock::GetMock(); + unistdMock = nullptr; } } // namespace Test @@ -35,17 +45,17 @@ using namespace OHOS::FileManagement::ModuleFileIO::Test; int access(const char *filename, int amode) { - return GetUnistdMock().access(filename, amode); + return UnistdMock::GetMock()->access(filename, amode); } int close(int fd) { - return GetUnistdMock().close(fd); + return UnistdMock::GetMock()->close(fd); } ssize_t read(int fd, void *buf, size_t count) { - return GetUnistdMock().read(fd, buf, count); + return UnistdMock::GetMock()->read(fd, buf, count); } } // extern "C" \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h index c442c194d..37dc6d5cf 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h @@ -38,18 +38,17 @@ public: MOCK_METHOD(int, close, (int), (override)); MOCK_METHOD(ssize_t, read, (int, void *, size_t), (override)); - static UnistdMock &GetMock() - { - static UnistdMock mock; - return mock; - } -}; +public: + static std::shared_ptr GetMock(); + static void DestroyMock(); -UnistdMock &GetUnistdMock(); +private: + static thread_local std::shared_ptr unistdMock; +}; } // namespace Test } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H \ No newline at end of file +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp index 284a71160..9463f6399 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp @@ -33,6 +33,8 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + +private: static inline shared_ptr uvMock = nullptr; }; @@ -48,6 +50,7 @@ void OpenCoreMockTest::TearDownTestCase(void) GTEST_LOG_(INFO) << "TearDownTestCase"; Uvfs::ins = nullptr; uvMock = nullptr; + UnistdMock::DestroyMock(); } void OpenCoreMockTest::SetUp(void) @@ -71,7 +74,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_001"; - string path = "/test/open_test.txt"; + string path = "/test/OpenCoreMockTest_DoOpen_001"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); @@ -92,15 +95,15 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_002, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_002"; - string path = "file://test/open_test.txt"; + string path = "file://test/OpenCoreMockTest_DoOpen_002"; int32_t mode = 0; - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + auto unistdMock = UnistdMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); auto res = OpenCore::DoOpen(path, mode); - testing::Mock::VerifyAndClearExpectations(&unistdMock); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_EQ(res.IsSuccess(), true); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_002"; @@ -117,13 +120,13 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_003, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_003"; - string path = "file://test/open_test.txt"; + string path = "file://test/OpenCoreMockTest_DoOpen_003"; int32_t mode = 0; - testing::StrictMock &unistdMock = static_cast &>(GetUnistdMock()); + auto unistdMock = UnistdMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); - EXPECT_CALL(unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); - EXPECT_CALL(unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); auto res = OpenCore::DoOpen(path, mode); testing::Mock::VerifyAndClearExpectations(&unistdMock); EXPECT_EQ(res.IsSuccess(), false); @@ -142,7 +145,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_004, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_004"; - string path = "/test/open_test.txt"; + string path = "/test/OpenCoreMockTest_DoOpen_004"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); diff --git a/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp index a32f49161..322bca06b 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/watcher_core_mock_test.cpp @@ -45,6 +45,8 @@ void WatcherCoreMockTest::SetUpTestCase(void) void WatcherCoreMockTest::TearDownTestCase(void) { + EventfdMock::DestroyMock(); + InotifyMock::DestroyMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -72,9 +74,9 @@ void WatcherCoreMockTest::TearDown(void) * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC - * @tc.level Level 1 + * @tc.level Level 0 */ -HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_001, testing::ext::TestSize.Level1) +HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_001, testing::ext::TestSize.Level0) { GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_001"; // Prepare test parameters @@ -82,15 +84,15 @@ HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_001, testing:: int32_t events = IN_CREATE; std::shared_ptr callback = std::make_shared(); // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - testing::StrictMock &eventfdMock = static_cast &>(GetEventfdMock()); - EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); - EXPECT_CALL(eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2)); + auto inotifyMock = InotifyMock::GetMock(); + auto eventfdMock = EventfdMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1)); + EXPECT_CALL(*eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2)); // Do testing auto result = WatcherCore::DoCreateWatcher(path, events, callback); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); - testing::Mock::VerifyAndClearExpectations(&eventfdMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); + testing::Mock::VerifyAndClearExpectations(eventfdMock.get()); EXPECT_TRUE(result.IsSuccess()); GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_001"; } @@ -110,12 +112,12 @@ HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_002, testing:: int32_t events = IN_CREATE; std::shared_ptr callback = std::make_shared(); // Set mock behaviors - testing::StrictMock &inotifyMock = static_cast &>(GetInotifyMock()); - EXPECT_CALL(inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1)); // Do testing auto result = WatcherCore::DoCreateWatcher(path, events, callback); // Verify results - testing::Mock::VerifyAndClearExpectations(&inotifyMock); + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_FALSE(result.IsSuccess()); GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_002"; } @@ -153,10 +155,10 @@ HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_004, testing:: GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_004"; // Prepare test parameters std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_004"; - int32_t events = 9999; + int32_t invalidEvents = ~IN_ALL_EVENTS; std::shared_ptr callback = std::make_shared(); // Do testing - auto result = WatcherCore::DoCreateWatcher(path, events, callback); + auto result = WatcherCore::DoCreateWatcher(path, invalidEvents, callback); // Verify results EXPECT_FALSE(result.IsSuccess()); GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_004"; @@ -198,12 +200,13 @@ HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_006, testing:: int32_t events = IN_CREATE; std::shared_ptr callback = std::make_shared(); // Prepare test condition + int32_t expectedWd = 100; FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.notifyFd_ = 1; // Valid notifyFd auto info = std::make_shared(callback); info->fileName = "/test/WatcherCoreMockTest_DoCreateWatcher_006"; info->events = IN_CREATE; - info->wd = 100; + info->wd = expectedWd; watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors auto cbMock = std::dynamic_pointer_cast(callback); diff --git a/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp index 5ff953b3e..ed8996f46 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp @@ -84,7 +84,7 @@ HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_001, testing::ext::Test string key = "test_key"; string value = "test_value"; - EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(SetErrnoAndReturn(EIO, -1)); auto ret = XattrCore::DoSetXattr(path, key, value); EXPECT_FALSE(ret.IsSuccess()); @@ -127,7 +127,7 @@ HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_001, testing::ext::Test string path = tempFilePath.string(); string key = "test_key"; - EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillRepeatedly(Return(-1)); + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillRepeatedly(SetErrnoAndReturn(EIO, -1)); auto ret = XattrCore::DoGetXattr(path, key); EXPECT_TRUE(ret.IsSuccess()); -- Gitee