From 9a9e1f5ef0fba275e42f9907e15222175ce95283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sun, 29 Jun 2025 18:11:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E6=95=B4tdd=E7=94=A8=E4=BE=8Bmock?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=94=AF=E6=8C=81=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E7=9C=9F=E5=AE=9E=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I9f1c16ab281cb631d25bebee0b31e81365a00d14 Signed-off-by: 姜小林 --- interfaces/test/unittest/js/BUILD.gn | 20 +- .../class_stream/fs_stream_mock_test.cpp | 15 +- .../js/mod_fs/class_stream/mock/c_mock.cpp | 90 +++++++-- .../js/mod_fs/class_stream/mock/c_mock.h | 43 ++-- .../fs_file_watcher_mock_test.cpp | 183 +++++++++++++++++- .../class_watcher/fs_watcher_mock_test.cpp | 6 +- .../unittest/js/mod_fs/mock/eventfd_mock.cpp | 33 +++- .../unittest/js/mod_fs/mock/eventfd_mock.h | 5 +- .../unittest/js/mod_fs/mock/inotify_mock.cpp | 57 +++++- .../unittest/js/mod_fs/mock/inotify_mock.h | 5 +- .../unittest/js/mod_fs/mock/poll_mock.cpp | 33 +++- .../test/unittest/js/mod_fs/mock/poll_mock.h | 5 +- .../unittest/js/mod_fs/mock/unistd_mock.cpp | 57 +++++- .../unittest/js/mod_fs/mock/unistd_mock.h | 5 +- .../mod_fs/properties/copy_core_mock_test.cpp | 6 +- .../mod_fs/properties/open_core_mock_test.cpp | 3 +- .../properties/trans_listener_mock_test.cpp | 3 +- .../properties/watcher_core_mock_test.cpp | 40 +--- 18 files changed, 492 insertions(+), 117 deletions(-) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index d4863929c..21089f494 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -44,18 +44,16 @@ ohos_unittest("ani_file_environment_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "libuv:uv", - "ipc:ipc_core", "init:libbegetutil", + "ipc:ipc_core", + "libuv:uv", "openssl:libcrypto_shared", "os_account:os_account_innerkits", "runtime_core:ani", "runtime_core:libarkruntime", ] - defines = [ - "private=public", - ] + defines = [ "private=public" ] } ohos_unittest("ani_file_fs_test") { @@ -86,9 +84,9 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/class_tasksignal/fs_task_signal_test.cpp", "mod_fs/properties/access_core_test.cpp", "mod_fs/properties/close_core_test.cpp", - "mod_fs/properties/copy_file_core_test.cpp", "mod_fs/properties/copy_core_test.cpp", "mod_fs/properties/copy_dir_core_test.cpp", + "mod_fs/properties/copy_file_core_test.cpp", "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/dup_core_test.cpp", @@ -97,13 +95,13 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/lseek_core_test.cpp", "mod_fs/properties/lstat_core_test.cpp", "mod_fs/properties/move_core_test.cpp", - "mod_fs/properties/stat_core_test.cpp", "mod_fs/properties/movedir_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/stat_core_test.cpp", "mod_fs/properties/trans_listener_test.cpp", "mod_fs/properties/truncate_core_test.cpp", "mod_fs/properties/utimes_core_test.cpp", @@ -172,8 +170,8 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/mock/poll_mock.cpp", "mod_fs/mock/unistd_mock.cpp", "mod_fs/properties/access_core_mock_test.cpp", - "mod_fs/properties/copy_file_core_mock_test.cpp", "mod_fs/properties/copy_core_mock_test.cpp", + "mod_fs/properties/copy_file_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", @@ -184,11 +182,11 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/mock/system_mock.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", "mod_fs/properties/move_core_mock_test.cpp", - "mod_fs/properties/rename_core_mock_test.cpp", - "mod_fs/properties/stat_core_mock_test.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/rename_core_mock_test.cpp", + "mod_fs/properties/stat_core_mock_test.cpp", "mod_fs/properties/symlink_core_mock_test.cpp", "mod_fs/properties/trans_listener_mock_test.cpp", "mod_fs/properties/truncate_core_mock_test.cpp", @@ -220,6 +218,8 @@ ohos_unittest("ani_file_fs_mock_test") { "libuv:uv", ] + libs = [ "dl" ] + defines = [ "ENABLE_DISTRIBUTED_FILE_MOCK", "private=public", diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp index 6abc56da0..5641bd86b 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -21,6 +21,7 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { +namespace Test { using namespace std; static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; @@ -29,21 +30,17 @@ class FsStreamMockTest : public testing::Test { public: static void SetUpTestCase(void) { - mock_ = std::make_shared(); - ICMock::ins = mock_; + CMock::EnableMock(); int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { - ICMock::ins = nullptr; - mock_ = nullptr; + CMock::DisableMock(); rmdir(g_streamFilePath.c_str()); }; void SetUp() {}; void TearDown() {}; - - static inline std::shared_ptr mock_ = nullptr; }; /** @@ -60,6 +57,7 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); + auto mock_ = CMock::GetMock(); EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); auto retSk = result->Seek(1); @@ -85,6 +83,7 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); + auto mock_ = CMock::GetMock(); EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); @@ -111,6 +110,7 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1 ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); + auto mock_ = CMock::GetMock(); EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); WriteOptions opt; @@ -138,6 +138,7 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1 ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); + auto mock_ = CMock::GetMock(); EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); WriteOptions opt; @@ -169,6 +170,7 @@ HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) void *buffer = std::malloc(4096); ArrayBuffer arrayBuffer(buffer, 4096); + auto mock_ = CMock::GetMock(); EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); ReadOptions opt; @@ -183,6 +185,7 @@ HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; } +} // namespace Test } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp index 8ba1451ed..b28d7e150 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -1,30 +1,86 @@ /* -* 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. -*/ + * 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 "c_mock.h" -using namespace OHOS::FileManagement::ModuleFileIO; +#include +namespace OHOS::FileManagement::ModuleFileIO::Test { + +thread_local std::shared_ptr CMock::cMock = nullptr; +thread_local bool CMock::mockable = false; + +std::shared_ptr CMock::GetMock() +{ + if (cMock == nullptr) { + cMock = std::make_shared(); + } + return cMock; +} + +void CMock::EnableMock() +{ + mockable = true; +} + +void CMock::DisableMock() +{ + cMock = nullptr; + mockable = false; +} + +bool CMock::IsMockable() +{ + return mockable; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test + +#ifdef __cplusplus extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +static int (*real_fseek)(FILE *, long, int) = nullptr; +static long (*real_ftell)(FILE *) = nullptr; + int fseek(FILE *stream, long len, int offset) { - return ICMock::ins->fseek(stream, len, offset); + if (CMock::IsMockable()) { + return CMock::GetMock()->fseek(stream, len, offset); + } + + real_fseek = (int (*)(FILE *, long, int))dlsym(RTLD_NEXT, "fseek"); + if (!real_fseek) { + GTEST_LOG_(ERROR) << "Failed to resolve real fseek" << dlerror(); + return -1; + } + return real_fseek(stream, len, offset); } long ftell(FILE *stream) { - return ICMock::ins->ftell(stream); + if (CMock::IsMockable()) { + return CMock::GetMock()->ftell(stream); + } + + real_ftell = (long (*)(FILE *))dlsym(RTLD_NEXT, "ftell"); + if (!real_ftell) { + GTEST_LOG_(ERROR) << "Failed to resolve real ftell" << dlerror(); + return -1; + } + return real_ftell(stream); } -} \ No newline at end of file +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h index 622d50fc9..9baf3240c 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -1,17 +1,17 @@ /* -* 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. -*/ + * 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_CLASS_STREAM_MOCK_C_MOCK_H #define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H @@ -20,11 +20,9 @@ #include #include -namespace OHOS::FileManagement::ModuleFileIO { +namespace OHOS::FileManagement::ModuleFileIO::Test { class ICMock { -public: - static inline std::shared_ptr ins = nullptr; public: virtual ~ICMock() = default; virtual int fseek(FILE *, long, int) = 0; @@ -35,8 +33,17 @@ class CMock : public ICMock { public: MOCK_METHOD(int, fseek, (FILE *, long, int), (override)); MOCK_METHOD(long, ftell, (FILE *), (override)); -}; +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr cMock; + static thread_local bool mockable; +}; -} // OHOS::FileManagement::ModuleFileIO +} // namespace OHOS::FileManagement::ModuleFileIO::Test #endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H \ 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 index adf4db15e..98c9c60f7 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 @@ -44,14 +44,18 @@ public: void FsFileWatcherMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + EventfdMock::EnableMock(); + InotifyMock::EnableMock(); + PollMock::EnableMock(); + UnistdMock::EnableMock(); } void FsFileWatcherMockTest::TearDownTestCase(void) { - EventfdMock::DestroyMock(); - InotifyMock::DestroyMock(); - PollMock::DestroyMock(); - UnistdMock::DestroyMock(); + EventfdMock::DisableMock(); + InotifyMock::DisableMock(); + PollMock::DisableMock(); + UnistdMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -311,6 +315,43 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_005, testing:: GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_005"; } +/** + * @tc.name: FsFileWatcherMockTest_StartNotify_006 + * @tc.desc: Test function of FsFileWatcher::StartNotify interface for SUCCESS when path is already watched. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_006, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_006"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_StartNotify_006"; + info->events = IN_CREATE; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.notifyFd_ = 1; // Valid notifyFd + int32_t expectedWd = 100; + auto cachedInfo = std::make_shared(nullptr); + cachedInfo->fileName = "/test/FsFileWatcherMockTest_StartNotify_006"; + cachedInfo->events = IN_DELETE; + cachedInfo->wd = expectedWd; + watcher.dataCache_.AddWatcherInfo(cachedInfo); + // Set mock behaviors + auto inotifyMock = InotifyMock::GetMock(); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(expectedWd)); + // Do testing + int32_t result = watcher.StartNotify(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); + EXPECT_EQ(result, ERRNO_NOERR); + EXPECT_EQ(info->wd, expectedWd); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_006"; +} + /** * @tc.name: FsFileWatcherMockTest_StopNotify_001 * @tc.desc: Test function of FsFileWatcher::StopNotify interface for SUCCESS. @@ -1157,6 +1198,140 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_004, testing:: GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_NotifyEvent_004"; } +/** + * @tc.name: FsFileWatcherMockTest_AddWatcherInfo_001 + * @tc.desc: Test function of FsFileWatcher::AddWatcherInfo interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_001"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001"; + info->events = IN_CREATE; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + auto cachedInfo0 = std::make_shared(nullptr); + cachedInfo0->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001_cachedInfo0"; + watcher.dataCache_.AddWatcherInfo(cachedInfo0); + + auto cachedInfo1 = std::make_shared(nullptr); + cachedInfo1->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001"; + cachedInfo1->events = IN_DELETE; + watcher.dataCache_.AddWatcherInfo(cachedInfo1); + + auto callback = std::make_shared(); + auto cachedInfo2 = std::make_shared(callback); + cachedInfo2->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001"; + cachedInfo2->events = IN_CREATE; + watcher.dataCache_.AddWatcherInfo(cachedInfo2); + + // Set mock behaviors + EXPECT_CALL(*callback, IsStrictEquals(testing::_)).Times(1).WillOnce(testing::Return(false)); + // Do testing + bool result = watcher.AddWatcherInfo(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(callback.get()); + EXPECT_TRUE(result); + cachedInfo2->callback = nullptr; + watcher.dataCache_.ClearCache(); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_AddWatcherInfo_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_AddWatcherInfo_002 + * @tc.desc: Test function of FsFileWatcher::AddWatcherInfo interface for FAILURE when param is nullptr. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_002"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Do testing + bool result = watcher.AddWatcherInfo(nullptr); + // Verify results + EXPECT_FALSE(result); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_AddWatcherInfo_002"; +} + +/** + * @tc.name: FsFileWatcherMockTest_AddWatcherInfo_003 + * @tc.desc: Test function of FsFileWatcher::AddWatcherInfo interface for FAILURE when having same info. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_003"; + // Prepare test parameters + auto info = std::make_shared(nullptr); + info->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_003"; + info->events = IN_CREATE; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + auto callback = std::make_shared(); + auto cachedInfo = std::make_shared(callback); + cachedInfo->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_003"; + cachedInfo->events = IN_CREATE; + watcher.dataCache_.AddWatcherInfo(cachedInfo); + // Set mock behaviors + EXPECT_CALL(*callback, IsStrictEquals(testing::_)).Times(1).WillOnce(testing::Return(true)); + // Do testing + bool result = watcher.AddWatcherInfo(info); + // Verify results + testing::Mock::VerifyAndClearExpectations(callback.get()); + EXPECT_FALSE(result); + cachedInfo->callback = nullptr; + watcher.dataCache_.ClearCache(); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_AddWatcherInfo_003"; +} + +/** + * @tc.name: FsFileWatcherMockTest_RemoveWatcherInfo_001 + * @tc.desc: Test function of FsFileWatcher::RemoveWatcherInfo interface for FAILURE when param is nullptr. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_RemoveWatcherInfo_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_RemoveWatcherInfo_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + // Do testing + auto result = watcher.RemoveWatcherInfo(nullptr); + // Verify results + EXPECT_EQ(result, EINVAL); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_RemoveWatcherInfo_001"; +} + +/** + * @tc.name: FsFileWatcherMockTest_DestroyTaskThead_001 + * @tc.desc: Test function of FsFileWatcher::DestroyTaskThead interface when taskRunning is true. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_DestroyTaskThead_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_DestroyTaskThead_001"; + // Prepare test condition + FsFileWatcher &watcher = FsFileWatcher::GetInstance(); + watcher.taskRunning_ = true; + // Do testing + watcher.DestroyTaskThead(); + // Verify results + EXPECT_FALSE(watcher.taskRunning_); + GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_DestroyTaskThead_001"; +} + } // namespace Test } // namespace ModuleFileIO } // namespace FileManagement 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 74a74b809..805c6af56 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 @@ -42,12 +42,14 @@ public: void FsWatcherMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + InotifyMock::EnableMock(); + UnistdMock::EnableMock(); } void FsWatcherMockTest::TearDownTestCase(void) { - InotifyMock::DestroyMock(); - UnistdMock::DestroyMock(); + InotifyMock::DisableMock(); + UnistdMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } 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 3406f521c..bbbe0e834 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp @@ -15,12 +15,15 @@ #include "eventfd_mock.h" +#include + namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace Test { thread_local std::shared_ptr EventfdMock::eventfdMock = nullptr; +thread_local bool EventfdMock::mockable = false; std::shared_ptr EventfdMock::GetMock() { @@ -30,9 +33,20 @@ std::shared_ptr EventfdMock::GetMock() return eventfdMock; } -void EventfdMock::DestroyMock() +void EventfdMock::EnableMock() +{ + mockable = true; +} + +void EventfdMock::DisableMock() { eventfdMock = nullptr; + mockable = false; +} + +bool EventfdMock::IsMockable() +{ + return mockable; } } // namespace Test @@ -40,12 +54,25 @@ void EventfdMock::DestroyMock() } // namespace FileManagement } // namespace OHOS +#ifdef __cplusplus extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; +static int (*real_eventfd)(unsigned int count, int flags) = nullptr; + int eventfd(unsigned int count, int flags) { - return EventfdMock::GetMock()->eventfd(count, flags); + if (EventfdMock::IsMockable()) { + return EventfdMock::GetMock()->eventfd(count, flags); + } + + real_eventfd = (int (*)(unsigned int, int))dlsym(RTLD_NEXT, "eventfd"); + if (!real_eventfd) { + GTEST_LOG_(ERROR) << "Failed to resolve real eventfd" << dlerror(); + return -1; + } + return real_eventfd(count, flags); } -} // extern "C" \ No newline at end of file +} // extern "C" +#endif \ 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 e1bbd820d..97dd8430f 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h @@ -36,10 +36,13 @@ public: public: static std::shared_ptr GetMock(); - static void DestroyMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); private: static thread_local std::shared_ptr eventfdMock; + static thread_local bool mockable; }; } // namespace Test 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 6c4deb01a..9bb5b04ec 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp @@ -15,12 +15,15 @@ #include "inotify_mock.h" +#include + namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace Test { thread_local std::shared_ptr InotifyMock::inotifyMock = nullptr; +thread_local bool InotifyMock::mockable = false; std::shared_ptr InotifyMock::GetMock() { @@ -30,9 +33,20 @@ std::shared_ptr InotifyMock::GetMock() return inotifyMock; } -void InotifyMock::DestroyMock() +void InotifyMock::EnableMock() +{ + mockable = true; +} + +void InotifyMock::DisableMock() { inotifyMock = nullptr; + mockable = false; +} + +bool InotifyMock::IsMockable() +{ + return mockable; } } // namespace Test @@ -40,22 +54,55 @@ void InotifyMock::DestroyMock() } // namespace FileManagement } // namespace OHOS +#ifdef __cplusplus extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; +static int (*real_inotify_init)() = nullptr; +static int (*real_inotify_add_watch)(int, const char *, uint32_t) = nullptr; +static int (*real_inotify_rm_watch)(int, int) = nullptr; + int inotify_init() { - return InotifyMock::GetMock()->inotify_init(); + if (InotifyMock::IsMockable()) { + return InotifyMock::GetMock()->inotify_init(); + } + + real_inotify_init = (int (*)())dlsym(RTLD_NEXT, "inotify_init"); + if (!real_inotify_init) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_init" << dlerror(); + return -1; + } + return real_inotify_init(); } int inotify_add_watch(int fd, const char *pathname, uint32_t mask) { - return InotifyMock::GetMock()->inotify_add_watch(fd, pathname, mask); + if (InotifyMock::IsMockable()) { + return InotifyMock::GetMock()->inotify_add_watch(fd, pathname, mask); + } + + real_inotify_add_watch = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch"); + if (!real_inotify_add_watch) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_add_watch" << dlerror(); + return -1; + } + return real_inotify_add_watch(fd, pathname, mask); } int inotify_rm_watch(int fd, int wd) { - return InotifyMock::GetMock()->inotify_rm_watch(fd, wd); + if (InotifyMock::IsMockable()) { + return InotifyMock::GetMock()->inotify_rm_watch(fd, wd); + } + + real_inotify_rm_watch = (int (*)(int, int))dlsym(RTLD_NEXT, "inotify_rm_watch"); + if (!real_inotify_rm_watch) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_rm_watch" << dlerror(); + return -1; + } + return real_inotify_rm_watch(fd, wd); } -} // extern "C" \ No newline at end of file +} // extern "C" +#endif \ 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 188f63a28..233403309 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h @@ -40,10 +40,13 @@ public: public: static std::shared_ptr GetMock(); - static void DestroyMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); private: static thread_local std::shared_ptr inotifyMock; + static thread_local bool mockable; }; } // namespace Test 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 fa66cf055..937cb57dd 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp @@ -15,12 +15,15 @@ #include "poll_mock.h" +#include + namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace Test { thread_local std::shared_ptr PollMock::pollMock = nullptr; +thread_local bool PollMock::mockable = false; std::shared_ptr PollMock::GetMock() { @@ -30,9 +33,20 @@ std::shared_ptr PollMock::GetMock() return pollMock; } -void PollMock::DestroyMock() +void PollMock::EnableMock() +{ + mockable = true; +} + +void PollMock::DisableMock() { pollMock = nullptr; + mockable = false; +} + +bool PollMock::IsMockable() +{ + return mockable; } } // namespace Test @@ -40,12 +54,25 @@ void PollMock::DestroyMock() } // namespace FileManagement } // namespace OHOS +#ifdef __cplusplus extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; +static int (*real_poll)(struct pollfd *fds, nfds_t n, int timeout) = nullptr; + int poll(struct pollfd *fds, nfds_t n, int timeout) { - return PollMock::GetMock()->poll(fds, n, timeout); + if (PollMock::IsMockable()) { + return PollMock::GetMock()->poll(fds, n, timeout); + } + + real_poll = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll"); + if (!real_poll) { + GTEST_LOG_(ERROR) << "Failed to resolve real poll" << dlerror(); + return -1; + } + return real_poll(fds, n, timeout); } -} // extern "C" \ No newline at end of file +} // extern "C" +#endif \ 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 0dd005710..da771e4ec 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h @@ -36,10 +36,13 @@ public: public: static std::shared_ptr GetMock(); - static void DestroyMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); private: static thread_local std::shared_ptr pollMock; + static thread_local bool mockable; }; } // namespace Test 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 bb646967b..65d9618cb 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp @@ -15,12 +15,15 @@ #include "unistd_mock.h" +#include + namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace Test { thread_local std::shared_ptr UnistdMock::unistdMock = nullptr; +thread_local bool UnistdMock::mockable = false; std::shared_ptr UnistdMock::GetMock() { @@ -30,9 +33,20 @@ std::shared_ptr UnistdMock::GetMock() return unistdMock; } -void UnistdMock::DestroyMock() +void UnistdMock::EnableMock() +{ + mockable = true; +} + +void UnistdMock::DisableMock() { unistdMock = nullptr; + mockable = false; +} + +bool UnistdMock::IsMockable() +{ + return mockable; } } // namespace Test @@ -40,22 +54,55 @@ void UnistdMock::DestroyMock() } // namespace FileManagement } // namespace OHOS +#ifdef __cplusplus extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; +static int (*real_access)(const char *filename, int amode) = nullptr; +static int (*real_close)(int fd) = nullptr; +static ssize_t (*real_read)(int fd, void *buf, size_t count) = nullptr; + int access(const char *filename, int amode) { - return UnistdMock::GetMock()->access(filename, amode); + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->access(filename, amode); + } + + real_access = (int (*)(const char *, int))dlsym(RTLD_NEXT, "access"); + if (!real_access) { + GTEST_LOG_(ERROR) << "Failed to resolve real access" << dlerror(); + return -1; + } + return real_access(filename, amode); } int close(int fd) { - return UnistdMock::GetMock()->close(fd); + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->close(fd); + } + + real_close = (int (*)(int))dlsym(RTLD_NEXT, "close"); + if (!real_close) { + GTEST_LOG_(ERROR) << "Failed to resolve real close" << dlerror(); + return -1; + } + return real_close(fd); } ssize_t read(int fd, void *buf, size_t count) { - return UnistdMock::GetMock()->read(fd, buf, count); + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->read(fd, buf, count); + } + + real_read = (ssize_t(*)(int, void *, size_t))dlsym(RTLD_NEXT, "read"); + if (!real_read) { + GTEST_LOG_(ERROR) << "Failed to resolve real read" << dlerror(); + return 0; + } + return real_read(fd, buf, count); } -} // extern "C" \ No newline at end of file +} // extern "C" +#endif \ 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 37dc6d5cf..ec6f961e1 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h @@ -40,10 +40,13 @@ public: public: static std::shared_ptr GetMock(); - static void DestroyMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); private: static thread_local std::shared_ptr unistdMock; + static thread_local bool mockable; }; } // namespace Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp index 2580d8791..7f7dae9cb 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp @@ -65,6 +65,8 @@ void CopyCoreMockTest::SetUpTestCase(void) close(fd); uvMock = std::make_shared(); Uvfs::ins = uvMock; + UnistdMock::EnableMock(); + InotifyMock::EnableMock(); } void CopyCoreMockTest::TearDownTestCase(void) @@ -77,8 +79,8 @@ void CopyCoreMockTest::TearDownTestCase(void) rmdir(testDir.c_str()); Uvfs::ins = nullptr; uvMock = nullptr; - UnistdMock::DestroyMock(); - InotifyMock::DestroyMock(); + UnistdMock::DisableMock(); + InotifyMock::DisableMock(); } void CopyCoreMockTest::SetUp(void) 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 9463f6399..abee054b1 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 @@ -43,6 +43,7 @@ void OpenCoreMockTest::SetUpTestCase(void) GTEST_LOG_(INFO) << "SetUpTestCase"; uvMock = std::make_shared(); Uvfs::ins = uvMock; + UnistdMock::EnableMock(); } void OpenCoreMockTest::TearDownTestCase(void) @@ -50,7 +51,7 @@ void OpenCoreMockTest::TearDownTestCase(void) GTEST_LOG_(INFO) << "TearDownTestCase"; Uvfs::ins = nullptr; uvMock = nullptr; - UnistdMock::DestroyMock(); + UnistdMock::DisableMock(); } void OpenCoreMockTest::SetUp(void) diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp index 808520785..8dfd7c68a 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp @@ -149,12 +149,13 @@ void TransListenerCoreMockTest::SetUpTestCase(void) EXPECT_TRUE(false); } close(fd); + UnistdMock::EnableMock(); } void TransListenerCoreMockTest::TearDownTestCase(void) { rmdir(g_path.c_str()); - UnistdMock::DestroyMock(); + UnistdMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } 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 322bca06b..680a2ce32 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 @@ -41,12 +41,14 @@ public: void WatcherCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + EventfdMock::EnableMock(); + InotifyMock::EnableMock(); } void WatcherCoreMockTest::TearDownTestCase(void) { - EventfdMock::DestroyMock(); - InotifyMock::DestroyMock(); + EventfdMock::DisableMock(); + InotifyMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -185,40 +187,6 @@ HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_005, testing:: 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 - 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 = expectedWd; - 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 -- Gitee From 1b6147d09085ce27eaca6e6d3def9bd53b75ef8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 3 Jul 2025 10:47:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81=E8=BF=9B=E8=A1=8C=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I6432ced14395c8260982f097d85ec4dd3613841f Signed-off-by: 姜小林 --- .../js/mod_fs/class_stream/mock/c_mock.cpp | 33 ++-- .../fs_file_watcher_mock_test.cpp | 160 ++++++++---------- .../unittest/js/mod_fs/mock/eventfd_mock.cpp | 17 +- .../unittest/js/mod_fs/mock/inotify_mock.cpp | 49 ++++-- .../unittest/js/mod_fs/mock/poll_mock.cpp | 17 +- .../unittest/js/mod_fs/mock/unistd_mock.cpp | 49 ++++-- 6 files changed, 183 insertions(+), 142 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp index b28d7e150..fa513e4fe 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -52,21 +52,25 @@ bool CMock::IsMockable() extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; -static int (*real_fseek)(FILE *, long, int) = nullptr; -static long (*real_ftell)(FILE *) = nullptr; - int fseek(FILE *stream, long len, int offset) { if (CMock::IsMockable()) { return CMock::GetMock()->fseek(stream, len, offset); } - real_fseek = (int (*)(FILE *, long, int))dlsym(RTLD_NEXT, "fseek"); - if (!real_fseek) { - GTEST_LOG_(ERROR) << "Failed to resolve real fseek" << dlerror(); + static int (*realFseek)(FILE *, long, int) = []() { + auto func = (int (*)(FILE *, long, int))dlsym(RTLD_NEXT, "fseek"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real fseek: " << dlerror(); + } + return func; + }(); + + if (!realFseek) { return -1; } - return real_fseek(stream, len, offset); + + return realFseek(stream, len, offset); } long ftell(FILE *stream) @@ -75,12 +79,19 @@ long ftell(FILE *stream) return CMock::GetMock()->ftell(stream); } - real_ftell = (long (*)(FILE *))dlsym(RTLD_NEXT, "ftell"); - if (!real_ftell) { - GTEST_LOG_(ERROR) << "Failed to resolve real ftell" << dlerror(); + static long (*realFtell)(FILE *) = []() { + auto func = (long (*)(FILE *))dlsym(RTLD_NEXT, "ftell"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real ftell: " << dlerror(); + } + return func; + }(); + + if (!realFtell) { return -1; } - return real_ftell(stream); + + return realFtell(stream); } } // extern "C" #endif \ 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 index 98c9c60f7..fe585ef71 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 @@ -78,6 +78,9 @@ void FsFileWatcherMockTest::TearDown(void) GTEST_LOG_(INFO) << "TearDown"; } +inline const int32_t EXPECTED_WD = 100; +inline const int32_t UNEXPECTED_WD = 200; + /** * @tc.name: FsFileWatcherMockTest_GetNotifyId_001 * @tc.desc: Test function of FsFileWatcher::GetNotifyId interface. @@ -190,23 +193,22 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_001, testing:: GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_001"; // Prepare test parameters auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_StartNotify_001"; + info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_001"; info->events = IN_CREATE | IN_DELETE; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; // Set mock behaviors - int32_t expectedWd = 100; auto inotifyMock = InotifyMock::GetMock(); EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(expectedWd)); + .WillOnce(testing::Return(EXPECTED_WD)); // Do testing int32_t result = watcher.StartNotify(info); // Verify results testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); - EXPECT_EQ(info->wd, expectedWd); + EXPECT_EQ(info->wd, EXPECTED_WD); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_001"; } @@ -222,14 +224,13 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_002, testing:: { 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->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_002"; info->events = IN_CREATE | IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors auto inotifyMock = InotifyMock::GetMock(); @@ -239,7 +240,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_002, testing:: int32_t result = watcher.StartNotify(info); // Verify results EXPECT_EQ(result, ERRNO_NOERR); - EXPECT_EQ(info->wd, expectedWd); + EXPECT_EQ(info->wd, EXPECTED_WD); } /** @@ -273,10 +274,10 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_004, testing:: GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_004"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = -1; // Invalid notifyFd + watcher.notifyFd_ = -1; // Build test parameters auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_StartNotify_004"; + info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_004"; info->events = IN_CREATE; // Do testing int32_t result = watcher.StartNotify(info); @@ -297,10 +298,10 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_005, testing:: GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_005"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; // Build test parameters auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_StartNotify_005"; + info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_005"; info->events = IN_DELETE; // Set mock behaviors for inotify_add_watch failure auto inotifyMock = InotifyMock::GetMock(); @@ -327,28 +328,27 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StartNotify_006, testing:: GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StartNotify_006"; // Prepare test parameters auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_StartNotify_006"; + info->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_006"; info->events = IN_CREATE; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd - int32_t expectedWd = 100; + watcher.notifyFd_ = 1; auto cachedInfo = std::make_shared(nullptr); - cachedInfo->fileName = "/test/FsFileWatcherMockTest_StartNotify_006"; + cachedInfo->fileName = "fakePath/FsFileWatcherMockTest_StartNotify_006"; cachedInfo->events = IN_DELETE; - cachedInfo->wd = expectedWd; + cachedInfo->wd = EXPECTED_WD; watcher.dataCache_.AddWatcherInfo(cachedInfo); // Set mock behaviors auto inotifyMock = InotifyMock::GetMock(); EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(expectedWd)); + .WillOnce(testing::Return(EXPECTED_WD)); // Do testing int32_t result = watcher.StartNotify(info); // Verify results testing::Mock::VerifyAndClearExpectations(inotifyMock.get()); EXPECT_EQ(result, ERRNO_NOERR); - EXPECT_EQ(info->wd, expectedWd); + EXPECT_EQ(info->wd, EXPECTED_WD); GTEST_LOG_(INFO) << "FsFileWatcherMockTest-end FsFileWatcherMockTest_StartNotify_006"; } @@ -363,14 +363,13 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_001, testing::e { 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_001"; info->events = IN_CREATE | IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors auto unistdMock = UnistdMock::GetMock(); @@ -418,13 +417,12 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_003, testing::e GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_StopNotify_003"; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = -1; // Invalid notifyFd + watcher.notifyFd_ = -1; // Prepare test parameters - int32_t expectedWd = 100; auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_StopNotify_003"; + info->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_003"; info->events = IN_CREATE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Do testing int32_t result = watcher.StopNotify(info); // Verify results @@ -443,14 +441,13 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_004"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors auto unistdMock = UnistdMock::GetMock(); @@ -480,14 +477,13 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_005"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set rm watch fail condition watcher.closed_ = true; @@ -519,20 +515,19 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_006"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set having remainingEvents condition auto remainingInfo = std::make_shared(nullptr); - remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_006"; + remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_006"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = expectedWd; + remainingInfo->wd = EXPECTED_WD; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors auto unistdMock = UnistdMock::GetMock(); @@ -561,20 +556,19 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_007"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set having remainingEvents condition auto remainingInfo = std::make_shared(nullptr); - remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_007"; + remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_007"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = expectedWd; + remainingInfo->wd = EXPECTED_WD; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors auto unistdMock = UnistdMock::GetMock(); @@ -584,7 +578,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_007, testing::e EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(expectedWd)); + .WillOnce(testing::Return(EXPECTED_WD)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results @@ -606,20 +600,19 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_008"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set having remainingEvents condition auto remainingInfo = std::make_shared(nullptr); - remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_008"; + remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_008"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = expectedWd; + remainingInfo->wd = EXPECTED_WD; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors auto unistdMock = UnistdMock::GetMock(); @@ -651,23 +644,21 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_009"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set having remainingEvents condition auto remainingInfo = std::make_shared(nullptr); - remainingInfo->fileName = "/test/FsFileWatcherMockTest_StopNotify_009"; + remainingInfo->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_009"; remainingInfo->events = IN_CREATE; - remainingInfo->wd = expectedWd; + remainingInfo->wd = EXPECTED_WD; watcher.dataCache_.AddWatcherInfo(remainingInfo); // Set mock behaviors - int32_t unexpectedWd = 200; auto unistdMock = UnistdMock::GetMock(); auto inotifyMock = InotifyMock::GetMock(); EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); @@ -675,7 +666,7 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_StopNotify_009, testing::e EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_)).Times(0); EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)) .Times(1) - .WillOnce(testing::Return(unexpectedWd)); + .WillOnce(testing::Return(UNEXPECTED_WD)); // Do testing int32_t result = watcher.StopNotify(info); // Verify results @@ -696,14 +687,13 @@ 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->fileName = "fakePath/FsFileWatcherMockTest_StopNotify_010"; info->events = IN_DELETE; - info->wd = expectedWd; + info->wd = EXPECTED_WD; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); - watcher.notifyFd_ = 1; // Valid notifyFd + watcher.notifyFd_ = 1; watcher.eventFd_ = 1; watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors @@ -1079,15 +1069,14 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_001, testing:: { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_001"; // Prepare test parameters - int32_t expectedWd = 100; uint32_t mask = IN_CREATE; - struct inotify_event event = { .wd = expectedWd, .mask = mask, .cookie = 0, .len = 0 }; + struct inotify_event event = { .wd = EXPECTED_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->fileName = "fakePath/FsFileWatcherMockTest_NotifyEvent_001"; info->events = mask; - info->wd = expectedWd; + info->wd = EXPECTED_WD; FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors @@ -1110,14 +1099,13 @@ 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); 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 = expectedWd; + event->wd = EXPECTED_WD; event->mask = mask; event->cookie = 0; event->len = len + 1; @@ -1131,9 +1119,9 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_002, testing:: // Prepare test condition auto callback = std::make_shared(); auto info = std::make_shared(callback); - info->fileName = "/test/FsFileWatcherMockTest_NotifyEvent_002"; + info->fileName = "fakePath/FsFileWatcherMockTest_NotifyEvent_002"; info->events = mask; - info->wd = expectedWd; + info->wd = EXPECTED_WD; FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors @@ -1176,15 +1164,13 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_NotifyEvent_004, testing:: { GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_NotifyEvent_004"; // Prepare test parameters - int32_t expectedWd = 100; - int32_t unexpectedWd = 200; - struct inotify_event event = { .wd = expectedWd, .mask = IN_CREATE, .cookie = 0, .len = 0 }; + struct inotify_event event = { .wd = EXPECTED_WD, .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->fileName = "fakePath/FsFileWatcherMockTest_NotifyEvent_004"; info->events = IN_MODIFY; // Not matched mask - info->wd = unexpectedWd; // Not matched wd + info->wd = UNEXPECTED_WD; // Not matched wd FsFileWatcher &watcher = FsFileWatcher::GetInstance(); watcher.dataCache_.AddWatcherInfo(info); // Set mock behaviors @@ -1210,22 +1196,22 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_001, testin GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_001"; // Prepare test parameters auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001"; + info->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001"; info->events = IN_CREATE; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); auto cachedInfo0 = std::make_shared(nullptr); - cachedInfo0->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001_cachedInfo0"; + cachedInfo0->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001_cachedInfo0"; watcher.dataCache_.AddWatcherInfo(cachedInfo0); auto cachedInfo1 = std::make_shared(nullptr); - cachedInfo1->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001"; + cachedInfo1->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001"; cachedInfo1->events = IN_DELETE; watcher.dataCache_.AddWatcherInfo(cachedInfo1); auto callback = std::make_shared(); auto cachedInfo2 = std::make_shared(callback); - cachedInfo2->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_001"; + cachedInfo2->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_001"; cachedInfo2->events = IN_CREATE; watcher.dataCache_.AddWatcherInfo(cachedInfo2); @@ -1272,13 +1258,13 @@ HWTEST_F(FsFileWatcherMockTest, FsFileWatcherMockTest_AddWatcherInfo_003, testin GTEST_LOG_(INFO) << "FsFileWatcherMockTest-begin FsFileWatcherMockTest_AddWatcherInfo_003"; // Prepare test parameters auto info = std::make_shared(nullptr); - info->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_003"; + info->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_003"; info->events = IN_CREATE; // Prepare test condition FsFileWatcher &watcher = FsFileWatcher::GetInstance(); auto callback = std::make_shared(); auto cachedInfo = std::make_shared(callback); - cachedInfo->fileName = "/test/FsFileWatcherMockTest_AddWatcherInfo_003"; + cachedInfo->fileName = "fakePath/FsFileWatcherMockTest_AddWatcherInfo_003"; cachedInfo->events = IN_CREATE; watcher.dataCache_.AddWatcherInfo(cachedInfo); // Set mock behaviors 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 bbbe0e834..1b140d042 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp @@ -58,20 +58,25 @@ bool EventfdMock::IsMockable() extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; -static int (*real_eventfd)(unsigned int count, int flags) = nullptr; - int eventfd(unsigned int count, int flags) { if (EventfdMock::IsMockable()) { return EventfdMock::GetMock()->eventfd(count, flags); } - real_eventfd = (int (*)(unsigned int, int))dlsym(RTLD_NEXT, "eventfd"); - if (!real_eventfd) { - GTEST_LOG_(ERROR) << "Failed to resolve real eventfd" << dlerror(); + static int (*realEventfd)(unsigned int, int) = []() { + auto func = (int (*)(unsigned int, int))dlsym(RTLD_NEXT, "eventfd"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real eventfd: " << dlerror(); + } + return func; + }(); + + if (!realEventfd) { return -1; } - return real_eventfd(count, flags); + + return realEventfd(count, flags); } } // extern "C" 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 9bb5b04ec..729d21a55 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp @@ -58,22 +58,25 @@ bool InotifyMock::IsMockable() extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; -static int (*real_inotify_init)() = nullptr; -static int (*real_inotify_add_watch)(int, const char *, uint32_t) = nullptr; -static int (*real_inotify_rm_watch)(int, int) = nullptr; - int inotify_init() { if (InotifyMock::IsMockable()) { return InotifyMock::GetMock()->inotify_init(); } - real_inotify_init = (int (*)())dlsym(RTLD_NEXT, "inotify_init"); - if (!real_inotify_init) { - GTEST_LOG_(ERROR) << "Failed to resolve real inotify_init" << dlerror(); + static int (*realInotifyInit)() = []() { + auto func = (int (*)())dlsym(RTLD_NEXT, "inotify_init"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_init: " << dlerror(); + } + return func; + }(); + + if (!realInotifyInit) { return -1; } - return real_inotify_init(); + + return realInotifyInit(); } int inotify_add_watch(int fd, const char *pathname, uint32_t mask) @@ -82,12 +85,19 @@ int inotify_add_watch(int fd, const char *pathname, uint32_t mask) return InotifyMock::GetMock()->inotify_add_watch(fd, pathname, mask); } - real_inotify_add_watch = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch"); - if (!real_inotify_add_watch) { - GTEST_LOG_(ERROR) << "Failed to resolve real inotify_add_watch" << dlerror(); + static int (*realInotifyAddWatch)(int, const char *, uint32_t) = []() { + auto func = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_add_watch: " << dlerror(); + } + return func; + }(); + + if (!realInotifyAddWatch) { return -1; } - return real_inotify_add_watch(fd, pathname, mask); + + return realInotifyAddWatch(fd, pathname, mask); } int inotify_rm_watch(int fd, int wd) @@ -96,12 +106,19 @@ int inotify_rm_watch(int fd, int wd) return InotifyMock::GetMock()->inotify_rm_watch(fd, wd); } - real_inotify_rm_watch = (int (*)(int, int))dlsym(RTLD_NEXT, "inotify_rm_watch"); - if (!real_inotify_rm_watch) { - GTEST_LOG_(ERROR) << "Failed to resolve real inotify_rm_watch" << dlerror(); + static int (*realInotifyRmWatch)(int, int) = []() { + auto func = (int (*)(int, int))dlsym(RTLD_NEXT, "inotify_rm_watch"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_rm_watch: " << dlerror(); + } + return func; + }(); + + if (!realInotifyRmWatch) { return -1; } - return real_inotify_rm_watch(fd, wd); + + return realInotifyRmWatch(fd, wd); } } // extern "C" 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 937cb57dd..6e2095ab2 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp @@ -58,20 +58,25 @@ bool PollMock::IsMockable() extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; -static int (*real_poll)(struct pollfd *fds, nfds_t n, int timeout) = nullptr; - int poll(struct pollfd *fds, nfds_t n, int timeout) { if (PollMock::IsMockable()) { return PollMock::GetMock()->poll(fds, n, timeout); } - real_poll = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll"); - if (!real_poll) { - GTEST_LOG_(ERROR) << "Failed to resolve real poll" << dlerror(); + static int (*realPoll)(struct pollfd * fds, nfds_t n, int timeout) = []() { + auto func = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real poll: " << dlerror(); + } + return func; + }(); + + if (!realPoll) { return -1; } - return real_poll(fds, n, timeout); + + return realPoll(fds, n, timeout); } } // extern "C" 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 65d9618cb..cda59d9bf 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp @@ -58,22 +58,25 @@ bool UnistdMock::IsMockable() extern "C" { using namespace OHOS::FileManagement::ModuleFileIO::Test; -static int (*real_access)(const char *filename, int amode) = nullptr; -static int (*real_close)(int fd) = nullptr; -static ssize_t (*real_read)(int fd, void *buf, size_t count) = nullptr; - int access(const char *filename, int amode) { if (UnistdMock::IsMockable()) { return UnistdMock::GetMock()->access(filename, amode); } - real_access = (int (*)(const char *, int))dlsym(RTLD_NEXT, "access"); - if (!real_access) { - GTEST_LOG_(ERROR) << "Failed to resolve real access" << dlerror(); + static int (*realAccess)(const char *filename, int amode) = []() { + auto func = (int (*)(const char *, int))dlsym(RTLD_NEXT, "access"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real access: " << dlerror(); + } + return func; + }(); + + if (!realAccess) { return -1; } - return real_access(filename, amode); + + return realAccess(filename, amode); } int close(int fd) @@ -82,12 +85,19 @@ int close(int fd) return UnistdMock::GetMock()->close(fd); } - real_close = (int (*)(int))dlsym(RTLD_NEXT, "close"); - if (!real_close) { - GTEST_LOG_(ERROR) << "Failed to resolve real close" << dlerror(); + static int (*realClose)(int fd) = []() { + auto func = (int (*)(int))dlsym(RTLD_NEXT, "close"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real close: " << dlerror(); + } + return func; + }(); + + if (!realClose) { return -1; } - return real_close(fd); + + return realClose(fd); } ssize_t read(int fd, void *buf, size_t count) @@ -96,12 +106,19 @@ ssize_t read(int fd, void *buf, size_t count) return UnistdMock::GetMock()->read(fd, buf, count); } - real_read = (ssize_t(*)(int, void *, size_t))dlsym(RTLD_NEXT, "read"); - if (!real_read) { - GTEST_LOG_(ERROR) << "Failed to resolve real read" << dlerror(); + static ssize_t (*realRead)(int fd, void *buf, size_t count) = []() { + auto func = (ssize_t(*)(int, void *, size_t))dlsym(RTLD_NEXT, "read"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real read: " << dlerror(); + } + return func; + }(); + + if (!realRead) { return 0; } - return real_read(fd, buf, count); + + return realRead(fd, buf, count); } } // extern "C" -- Gitee