From 505e9cf84de3ff13cf3662e3d12e52cb94c4ad96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 4 Sep 2025 15:34:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96tdd=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id15f6699d740f2d78ddbadfa65040d135727b1fd Signed-off-by: 姜小林 --- .../js/src/mod_fs/properties/stat_core.cpp | 2 +- interfaces/test/unittest/js/BUILD.gn | 8 +- .../mod_fs/class_file/fs_file_mock_test.cpp | 39 +- .../fs_randomaccessfile_mock_test.cpp | 52 +- .../fs_reader_iterator_mock_test.cpp | 20 +- .../mod_fs/class_stat/fs_stat_mock_test.cpp | 7 +- .../class_stream/fs_stream_mock_test.cpp | 55 +- .../fs_file_watcher_mock_test.cpp | 3 +- .../class_watcher/fs_watcher_mock_test.cpp | 3 +- .../mock/mock_watcher_callback.h | 0 .../{properties => }/mock/system_mock.cpp | 0 .../{properties => }/mock/system_mock.h | 0 .../unittest/js/mod_fs/mock/unistd_mock.cpp | 21 + .../unittest/js/mod_fs/mock/unistd_mock.h | 2 + .../unittest/js/mod_fs/mock/uv_err_mock.cpp | 82 +++ .../unittest/js/mod_fs/mock/uv_err_mock.h | 47 ++ .../unittest/js/mod_fs/mock/uv_fs_mock.cpp | 576 ++++++++++++++++++ .../test/unittest/js/mod_fs/mock/uv_fs_mock.h | 95 +++ .../properties/access_core_mock_test.cpp | 23 +- .../properties/close_core_mock_test.cpp | 15 +- .../mod_fs/properties/copy_core_mock_test.cpp | 20 +- .../properties/copy_file_core_mock_test.cpp | 61 +- ...create_randomaccessfile_core_mock_test.cpp | 20 +- .../mod_fs/properties/dup_core_mock_test.cpp | 21 +- .../properties/fdatasync_core_mock_test.cpp | 20 +- .../properties/fsync_core_mock_test.cpp | 21 +- .../properties/lstat_core_mock_test.cpp | 19 +- .../properties/mkdir_core_mock_test.cpp | 24 +- .../properties/mkdtemp_core_mock_test.cpp | 22 +- .../js/mod_fs/properties/mock/dfs_mock.cpp | 81 +++ .../js/mod_fs/properties/mock/dfs_mock.h | 118 ++++ .../mock/mock_progress_listener.h | 0 .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 137 ----- .../js/mod_fs/properties/mock/uv_fs_mock.h | 91 --- .../mod_fs/properties/move_core_mock_test.cpp | 111 +++- .../mod_fs/properties/open_core_mock_test.cpp | 27 +- .../mod_fs/properties/read_core_mock_test.cpp | 16 +- .../properties/read_lines_core_mock_test.cpp | 22 +- .../properties/rename_core_mock_test.cpp | 22 +- .../mod_fs/properties/stat_core_mock_test.cpp | 73 ++- .../js/mod_fs/properties/stat_core_test.cpp | 1 - .../properties/symlink_core_mock_test.cpp | 20 +- .../properties/trans_listener_mock_test.cpp | 147 +---- .../properties/truncate_core_mock_test.cpp | 34 +- .../properties/unlink_core_mock_test.cpp | 18 +- .../properties/utimes_core_mock_test.cpp | 18 +- .../properties/watcher_core_mock_test.cpp | 3 +- .../properties/write_core_mock_test.cpp | 83 ++- .../js/mod_fs/properties/write_core_test.cpp | 70 +-- .../properties/xattr_core_mock_test.cpp | 5 +- 50 files changed, 1640 insertions(+), 735 deletions(-) rename interfaces/test/unittest/js/mod_fs/{ => class_watcher}/mock/mock_watcher_callback.h (100%) rename interfaces/test/unittest/js/mod_fs/{properties => }/mock/system_mock.cpp (100%) rename interfaces/test/unittest/js/mod_fs/{properties => }/mock/system_mock.h (100%) create mode 100644 interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.h rename interfaces/test/unittest/js/mod_fs/{ => properties}/mock/mock_progress_listener.h (100%) delete mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp delete mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h diff --git a/interfaces/kits/js/src/mod_fs/properties/stat_core.cpp b/interfaces/kits/js/src/mod_fs/properties/stat_core.cpp index 17269b133..fd1acbdff 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat_core.cpp @@ -74,7 +74,7 @@ static int32_t CheckFsStat(const FileInfo &fileInfo, uv_fs_t *req) if (fileInfo.isPath) { int ret = uv_fs_stat(nullptr, req, fileInfo.path.get(), nullptr); if (ret < 0) { - HILOGD("Failed to stat file with path, ret is %{public}d", ret); + HILOGE("Failed to stat file with path, ret is %{public}d", ret); return ret; } } else { diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 368bb6062..a1071b19b 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -138,6 +138,7 @@ ohos_unittest("ani_file_fs_mock_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_watcher/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", @@ -155,7 +156,10 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/mock/eventfd_mock.cpp", "mod_fs/mock/inotify_mock.cpp", "mod_fs/mock/poll_mock.cpp", + "mod_fs/mock/system_mock.cpp", "mod_fs/mock/unistd_mock.cpp", + "mod_fs/mock/uv_err_mock.cpp", + "mod_fs/mock/uv_fs_mock.cpp", "mod_fs/properties/access_core_mock_test.cpp", "mod_fs/properties/close_core_mock_test.cpp", "mod_fs/properties/copy_core_mock_test.cpp", @@ -167,8 +171,7 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/lstat_core_mock_test.cpp", "mod_fs/properties/mkdir_core_mock_test.cpp", "mod_fs/properties/mkdtemp_core_mock_test.cpp", - "mod_fs/properties/mock/system_mock.cpp", - "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/mock/dfs_mock.cpp", "mod_fs/properties/move_core_mock_test.cpp", "mod_fs/properties/open_core_mock_test.cpp", "mod_fs/properties/read_core_mock_test.cpp", @@ -213,7 +216,6 @@ ohos_unittest("ani_file_fs_mock_test") { libs = [ "dl" ] defines = [ - "ENABLE_DISTRIBUTED_FILE_MOCK", "private=public", ] } diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp index ee3215fc0..8aeeb491a 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ -#include "file_entity.h" #include "fs_file.h" -#include "system_mock.h" -#include "uv_fs_mock.h" #include #include +#include + +#include "file_entity.h" +#include "system_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -32,7 +34,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; std::unique_ptr fileEntity; std::unique_ptr fsFile; }; @@ -40,17 +41,16 @@ public: void FsFileMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "FsFileMockTest"); SystemMock::EnableMock(); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void FsFileMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); SystemMock::DisableMock(); + GTEST_LOG_(INFO) << "TearDownTestCase"; } void FsFileMockTest::SetUp(void) @@ -85,6 +85,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Leve uv_fs_t mock_req; mock_req.ptr = const_cast("/data/test/file_test.txt"); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t *, uv_fs_t *req, const char *, uv_fs_cb) { *req = mock_req; @@ -92,6 +93,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Leve })); auto result = fsFile->GetPath(); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_001"; } @@ -108,9 +110,11 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Leve GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_002"; fsFile->fileEntity->path_ = "/invalid/path"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetPath(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_002"; } @@ -127,9 +131,11 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_003, testing::ext::TestSize.Leve GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_003"; fsFile->fileEntity->path_ = "/invalid/path"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_003"; } @@ -146,9 +152,11 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Leve GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004"; fsFile->fileEntity->path_ = "file_test.txt"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_004"; } @@ -165,9 +173,11 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_005, testing::ext::TestSize.Le GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_005"; fsFile->fileEntity->path_ = "/invalid/path"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_005"; } @@ -185,7 +195,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Leve uv_fs_t mock_req; mock_req.ptr = const_cast("/file_test.txt"); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t *, uv_fs_t *req, const char *, uv_fs_cb) { *req = mock_req; @@ -193,6 +203,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Leve })); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_006"; } @@ -211,6 +222,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Le uv_fs_t mock_req; mock_req.ptr = const_cast("/test/dir_test"); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t *, uv_fs_t *req, const char *, uv_fs_cb) { *req = mock_req; @@ -218,6 +230,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Le })); auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_007"; } @@ -235,7 +248,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Leve uv_fs_t mock_req; mock_req.ptr = const_cast("file_test.txt"); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t *, uv_fs_t *req, const char *, uv_fs_cb) { *req = mock_req; @@ -243,6 +256,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Leve })); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_008"; } @@ -260,7 +274,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Le uv_fs_t mock_req; mock_req.ptr = const_cast("dir_test"); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t *, uv_fs_t *req, const char *, uv_fs_cb) { *req = mock_req; @@ -268,6 +282,7 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Le })); auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_009"; } diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp index 321d4f59e..e97a76cbb 100644 --- a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp @@ -13,14 +13,15 @@ * limitations under the License. */ +#include "fs_randomaccessfile.h" + #include #include +#include #include "file_entity.h" -#include "fs_randomaccessfile.h" #include "uv_fs_mock.h" - namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -32,7 +33,7 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; + protected: std::unique_ptr rafEntity; std::unique_ptr raf; @@ -40,15 +41,14 @@ protected: void FsRandomAccessFileMockTest::SetUpTestCase(void) { - uvMock = std::make_shared(); - Uvfs::ins = uvMock; GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "FsRandomAccessFileMockTest"); + UvfsMock::EnableMock(); } void FsRandomAccessFileMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -74,16 +74,18 @@ void FsRandomAccessFileMockTest::TearDown(void) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_001"; ArrayBuffer buffer(malloc(100), 100); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto result = raf->ReadSync(buffer, std::nullopt); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); free(buffer.buf); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_001"; @@ -95,7 +97,7 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_001, te * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, testing::ext::TestSize.Level1) { @@ -106,10 +108,11 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, te options.offset = 10; options.length = 10; raf->rafEntity->filePointer = 20; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(0)); auto result = raf->ReadSync(buffer, options); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); free(buffer.buf); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_002"; @@ -121,16 +124,18 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, te * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_003"; std::string data = "test data"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto result = raf->WriteSync(data, std::nullopt); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_003"; } @@ -141,7 +146,7 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_003, t * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, testing::ext::TestSize.Level1) { @@ -151,10 +156,11 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, t WriteOptions options; options.length = 4; options.offset = 0; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); auto result = raf->WriteSync(data, options); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_004"; } @@ -165,7 +171,7 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, t * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, testing::ext::TestSize.Level1) { @@ -175,10 +181,11 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, t WriteOptions options; options.length = 4; options.offset = 0; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto result = raf->WriteSync(buffer, options); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); free(buffer.buf); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_005"; @@ -190,7 +197,7 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, t * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, testing::ext::TestSize.Level1) { @@ -200,10 +207,11 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, t WriteOptions options; options.length = 4; options.offset = 0; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); auto result = raf->WriteSync(buffer, options); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); free(buffer.buf); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_006"; @@ -215,15 +223,17 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, t * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_007"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(-1)); auto result = raf->CloseSync(); EXPECT_EQ(result.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_007"; } @@ -234,17 +244,19 @@ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_007, t * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - + */ HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_008, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_008"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(0)); auto result = raf->CloseSync(); EXPECT_EQ(result.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_008"; } -} \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp index 734a1d6ab..867dbdb7a 100644 --- a/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ +#include "fs_reader_iterator.h" + #include #include #include #include +#include -#include "fs_reader_iterator.h" #include "read_lines_core.h" #include "uv_fs_mock.h" @@ -35,7 +37,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; filesystem::path FsReaderIteratorMockTest::tempFilePath; @@ -43,20 +44,19 @@ filesystem::path FsReaderIteratorMockTest::tempFilePath; void FsReaderIteratorMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "FsReaderIteratorMockTest"); tempFilePath = std::filesystem::temp_directory_path() / "test_file.txt"; ofstream tempfile(tempFilePath); tempfile << ""; tempfile.close(); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void FsReaderIteratorMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; filesystem::remove(tempFilePath); - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); + GTEST_LOG_(INFO) << "TearDownTestCase"; } void FsReaderIteratorMockTest::SetUp(void) @@ -79,10 +79,11 @@ void FsReaderIteratorMockTest::TearDown(void) HWTEST_F(FsReaderIteratorMockTest, FsReaderIteratorMockTest_Next_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsReaderIteratorMockTest-begin FsReaderIteratorMockTest_Next_001"; - + std::string path = tempFilePath.string(); std::optional option = std::nullopt; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); auto result = ReadLinesCore::DoReadLines(path, option); @@ -95,8 +96,9 @@ HWTEST_F(FsReaderIteratorMockTest, FsReaderIteratorMockTest_Next_001, testing::e EXPECT_TRUE(nextResult.IsSuccess()); EXPECT_TRUE(nextResult.GetData().value().done); EXPECT_EQ(nextResult.GetData().value().value, ""); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsReaderIteratorMockTest-end FsReaderIteratorMockTest_Next_001"; } -} \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp index 77813f9b4..786ccc34c 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp @@ -13,11 +13,13 @@ * limitations under the License. */ +#include "fs_stat.h" + #include #include +#include #include "fs_stat_entity.h" -#include "fs_stat.h" #include "securec.h" #include "system_mock.h" @@ -37,13 +39,14 @@ public: void FsStatMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "FsStatMockTest"); SystemMock::EnableMock(); } void FsStatMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; SystemMock::DisableMock(); + GTEST_LOG_(INFO) << "TearDownTestCase"; } void FsStatMockTest::SetUp(void) 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 79eb1db42..95b65daab 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 @@ -13,9 +13,12 @@ * limitations under the License. */ +#include "create_stream_core.h" + #include +#include + #include "c_mock.h" -#include "create_stream_core.h" #include "fs_utils.h" namespace OHOS { @@ -28,23 +31,39 @@ static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; class FsStreamMockTest : public testing::Test { public: - static void SetUpTestCase(void) - { - CMock::EnableMock(); - int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); - if (fd < 0) { - GTEST_LOG_(ERROR) << "Open test file failed! ret: " << fd << ", errno: " << errno; - ASSERT_TRUE(false); - } - close(fd); - }; - static void TearDownTestCase() - { - CMock::DisableMock(); - rmdir(g_streamFilePath.c_str()); - }; - void SetUp() {}; - void TearDown() {}; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsStreamMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "FsStreamMockTest"); + CMock::EnableMock(); + int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); + if (fd < 0) { + GTEST_LOG_(ERROR) << "Open test file failed! ret: " << fd << ", errno: " << errno; + ASSERT_TRUE(false); + } + close(fd); +}; + +void FsStreamMockTest::TearDownTestCase() +{ + CMock::DisableMock(); + rmdir(g_streamFilePath.c_str()); + GTEST_LOG_(INFO) << "TearDownTestCase"; +}; + +void FsStreamMockTest::SetUp() +{ + GTEST_LOG_(INFO) << "SetUp"; +}; +void FsStreamMockTest::TearDown() +{ + GTEST_LOG_(INFO) << "TearDown"; }; /** 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 d472a26df..ce8c46b22 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 @@ -13,6 +13,8 @@ * limitations under the License. */ +#include "fs_file_watcher.h" + #include #include @@ -22,7 +24,6 @@ #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" 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 30042270a..13f80dbe6 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 @@ -13,6 +13,8 @@ * limitations under the License. */ +#include "fs_watcher.h" + #include #include @@ -23,7 +25,6 @@ #include "filemgmt_libhilog.h" #include "fs_err_code.h" #include "fs_file_watcher.h" -#include "fs_watcher.h" #include "inotify_mock.h" #include "unistd_mock.h" diff --git a/interfaces/test/unittest/js/mod_fs/mock/mock_watcher_callback.h b/interfaces/test/unittest/js/mod_fs/class_watcher/mock/mock_watcher_callback.h similarity index 100% rename from interfaces/test/unittest/js/mod_fs/mock/mock_watcher_callback.h rename to interfaces/test/unittest/js/mod_fs/class_watcher/mock/mock_watcher_callback.h diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/system_mock.cpp similarity index 100% rename from interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp rename to interfaces/test/unittest/js/mod_fs/mock/system_mock.cpp diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h b/interfaces/test/unittest/js/mod_fs/mock/system_mock.h similarity index 100% rename from interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h rename to interfaces/test/unittest/js/mod_fs/mock/system_mock.h 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 0d05f8b64..ca8702620 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp @@ -142,5 +142,26 @@ ssize_t write(int fd, const void *buf, size_t count) return realWrite(fd, buf, count); } +int dup(int fd) +{ + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->dup(fd); + } + + static int (*realDup)(int fd) = []() { + auto func = (int (*)(int))dlsym(RTLD_NEXT, "dup"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real dup: " << dlerror(); + } + return func; + }(); + + if (!realDup) { + return 0; + } + + return realDup(fd); +} + } // 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 3771b1424..f625dca91 100644 --- a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h @@ -31,6 +31,7 @@ public: virtual int close(int) = 0; virtual ssize_t read(int, void *, size_t) = 0; virtual ssize_t write(int, const void *, size_t) = 0; + virtual int dup(int) = 0; }; class UnistdMock : public IUnistdMock { @@ -39,6 +40,7 @@ public: MOCK_METHOD(int, close, (int), (override)); MOCK_METHOD(ssize_t, read, (int, void *, size_t), (override)); MOCK_METHOD(ssize_t, write, (int, const void *, size_t), (override)); + MOCK_METHOD(int, dup, (int), (override)); public: static std::shared_ptr GetMock(); diff --git a/interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.cpp new file mode 100644 index 000000000..db98f9247 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.cpp @@ -0,0 +1,82 @@ +/* + * 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 "uv_err_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr UvErrMock::uvErrMock = nullptr; +thread_local bool UvErrMock::mockable = false; + +std::shared_ptr UvErrMock::GetMock() +{ + if (uvErrMock == nullptr) { + uvErrMock = std::make_shared(); + } + return uvErrMock; +} + +void UvErrMock::EnableMock() +{ + mockable = true; +} + +void UvErrMock::DisableMock() +{ + uvErrMock = nullptr; + mockable = false; +} + +bool UvErrMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +const char *uv_err_name(int err) +{ + if (UvErrMock::IsMockable()) { + return UvErrMock::GetMock()->uv_err_name(err); + } + + static const char *(*realUvErrName)(int) = []() { + auto func = (const char *(*)(int))dlsym(RTLD_NEXT, "uv_err_name"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_err_name: " << dlerror(); + } + return func; + }(); + + if (!realUvErrName) { + return ""; + } + + return realUvErrName(err); +} +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.h b/interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.h new file mode 100644 index 000000000..8fbf3b496 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/uv_err_mock.h @@ -0,0 +1,47 @@ +/* + * 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_PROPERTIES_MOCK_UV_ERR_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_ERR_MOCK_H + +#include "uv.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +class IUvErr { +public: + virtual ~IUvErr() = default; + virtual const char *uv_err_name(int err) = 0; +}; + +class UvErrMock : public IUvErr { +public: + MOCK_METHOD(const char *, uv_err_name, (int err), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr uvErrMock; + static thread_local bool mockable; +}; + +} // namespace OHOS::FileManagement::ModuleFileIO::Test +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_ERR_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.cpp new file mode 100644 index 000000000..8110d1d69 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.cpp @@ -0,0 +1,576 @@ +/* + * 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 "uv_fs_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr UvfsMock::uvfsMock = nullptr; +thread_local bool UvfsMock::mockable = false; + +std::shared_ptr UvfsMock::GetMock() +{ + if (uvfsMock == nullptr) { + uvfsMock = std::make_shared(); + } + return uvfsMock; +} + +void UvfsMock::EnableMock() +{ + mockable = true; +} + +void UvfsMock::DisableMock() +{ + uvfsMock = nullptr; + mockable = false; +} + +bool UvfsMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int uv_fs_read( + uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_read(loop, req, file, bufs, nbufs, off, cb); + } + + static int (*realUvfsRead)( + uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb))dlsym( + RTLD_NEXT, "uv_fs_read"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_read: " << dlerror(); + } + return func; + }(); + + if (!realUvfsRead) { + return -1; + } + + return realUvfsRead(loop, req, file, bufs, nbufs, off, cb); +} + +int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_readlink(loop, req, path, cb); + } + + static int (*realUvfsReadlink)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_readlink"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_readlink: " << dlerror(); + } + return func; + }(); + + if (!realUvfsReadlink) { + return -1; + } + + return realUvfsReadlink(loop, req, path, cb); +} + +int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_stat(loop, req, path, cb); + } + + static int (*realUvfsStat)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_stat"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_stat: " << dlerror(); + } + return func; + }(); + + if (!realUvfsStat) { + return -1; + } + + return realUvfsStat(loop, req, path, cb); +} + +int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_utime(loop, req, path, atime, mtime, cb); + } + + static int (*realUvfsUtime)(uv_loop_t *, uv_fs_t *, const char *, double, double, uv_fs_cb) = []() { + auto func = + (int (*)(uv_loop_t *, uv_fs_t *, const char *, double, double, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_utime"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_utime: " << dlerror(); + } + return func; + }(); + + if (!realUvfsUtime) { + return -1; + } + + return realUvfsUtime(loop, req, path, atime, mtime, cb); +} + +int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_scandir(loop, req, path, flags, cb); + } + + static int (*realUvfsScandir)(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_scandir"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_scandir: " << dlerror(); + } + return func; + }(); + + if (!realUvfsScandir) { + return -1; + } + + return realUvfsScandir(loop, req, path, flags, cb); +} + +int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_scandir_next(req, ent); + } + + static int (*realUvfsScandirNext)(uv_fs_t *, uv_dirent_t *) = []() { + auto func = (int (*)(uv_fs_t *, uv_dirent_t *))dlsym(RTLD_NEXT, "uv_fs_scandir_next"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_scandir_next: " << dlerror(); + } + return func; + }(); + + if (!realUvfsScandirNext) { + return -1; + } + + return realUvfsScandirNext(req, ent); +} + +int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_rmdir(loop, req, path, cb); + } + + static int (*realUvfsRmdir)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_rmdir"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_rmdir: " << dlerror(); + } + return func; + }(); + + if (!realUvfsRmdir) { + return -1; + } + + return realUvfsRmdir(loop, req, path, cb); +} + +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_symlink(loop, req, path, new_path, flags, cb); + } + + static int (*realUvfsSymlink)(uv_loop_t *, uv_fs_t *, const char *, const char *, int flags, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, const char *, int flags, uv_fs_cb))dlsym( + RTLD_NEXT, "uv_fs_symlink"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_symlink: " << dlerror(); + } + return func; + }(); + + if (!realUvfsSymlink) { + return -1; + } + + return realUvfsSymlink(loop, req, path, new_path, flags, cb); +} + +int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_open(loop, req, path, flags, mode, cb); + } + + static int (*realUvfsOpen)(uv_loop_t *, uv_fs_t *, const char *, int, int, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, int, int, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_open"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_open: " << dlerror(); + } + return func; + }(); + + if (!realUvfsOpen) { + return -1; + } + + return realUvfsOpen(loop, req, path, flags, mode, cb); +} + +int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_ftruncate(loop, req, fd, offset, cb); + } + + static int (*realUvfsFtruncate)(uv_loop_t *, uv_fs_t *, uv_file, int64_t, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, int64_t, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_ftruncate"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_ftruncate: " << dlerror(); + } + return func; + }(); + + if (!realUvfsFtruncate) { + return -1; + } + + return realUvfsFtruncate(loop, req, fd, offset, cb); +} + +int uv_fs_write( + uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_write(loop, req, fd, bufs, nbufs, offset, cb); + } + + static int (*realUvfsWrite)( + uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb))dlsym( + RTLD_NEXT, "uv_fs_write"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_write: " << dlerror(); + } + return func; + }(); + + if (!realUvfsWrite) { + return -1; + } + + return realUvfsWrite(loop, req, fd, bufs, nbufs, offset, cb); +} + +int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_realpath(loop, req, path, cb); + } + + static int (*realUvfsRealpath)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_realpath"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_realpath: " << dlerror(); + } + return func; + }(); + + if (!realUvfsRealpath) { + return -1; + } + + return realUvfsRealpath(loop, req, path, cb); +} + +int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_close(loop, req, file, cb); + } + + static int (*realUvfsClose)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_close"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_close: " << dlerror(); + } + return func; + }(); + + if (!realUvfsClose) { + return -1; + } + + return realUvfsClose(loop, req, file, cb); +} + +int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_fdatasync(loop, req, file, cb); + } + + static int (*realUvfsFdatasync)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_fdatasync"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_fdatasync: " << dlerror(); + } + return func; + }(); + + if (!realUvfsFdatasync) { + return -1; + } + + return realUvfsFdatasync(loop, req, file, cb); +} + +int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_mkdir(loop, req, path, mode, cb); + } + + static int (*realUvfsMkdir)(uv_loop_t *, uv_fs_t *, const char *, int mode, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, int mode, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_mkdir"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_mkdir: " << dlerror(); + } + return func; + }(); + + if (!realUvfsMkdir) { + return -1; + } + + return realUvfsMkdir(loop, req, path, mode, cb); +} + +int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_access(loop, req, path, flags, cb); + } + + static int (*realUvfsAccess)(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_access"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_access: " << dlerror(); + } + return func; + }(); + + if (!realUvfsAccess) { + return -1; + } + + return realUvfsAccess(loop, req, path, flags, cb); +} + +int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_mkdtemp(loop, req, tpl, cb); + } + + static int (*realUvfsMkdtemp)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_mkdtemp"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_mkdtemp: " << dlerror(); + } + return func; + }(); + + if (!realUvfsMkdtemp) { + return -1; + } + + return realUvfsMkdtemp(loop, req, tpl, cb); +} + +int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_unlink(loop, req, path, cb); + } + + static int (*realUvfsUnlink)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_unlink"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_unlink: " << dlerror(); + } + return func; + }(); + + if (!realUvfsUnlink) { + return -1; + } + + return realUvfsUnlink(loop, req, path, cb); +} + +void uv_fs_req_cleanup(uv_fs_t *req) +{ + if (UvfsMock::IsMockable()) { + UvfsMock::GetMock()->uv_fs_req_cleanup(req); + return; + } + + static void (*realUvfsCleanup)(uv_fs_t *) = []() { + auto func = (void (*)(uv_fs_t *))dlsym(RTLD_NEXT, "uv_fs_req_cleanup"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_req_cleanup: " << dlerror(); + } + return func; + }(); + + if (!realUvfsCleanup) { + return; + } + + realUvfsCleanup(req); +} + +int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_rename(loop, req, path, newPath, cb); + } + + static int (*realUvfsRename)(uv_loop_t *, uv_fs_t *, const char *, const char *, uv_fs_cb) = []() { + auto func = + (int (*)(uv_loop_t *, uv_fs_t *, const char *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_rename"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_rename: " << dlerror(); + } + return func; + }(); + + if (!realUvfsRename) { + return -1; + } + + return realUvfsRename(loop, req, path, newPath, cb); +} + +int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_fsync(loop, req, file, cb); + } + + static int (*realUvfsFsync)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_fsync"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_fsync: " << dlerror(); + } + return func; + }(); + + if (!realUvfsFsync) { + return -1; + } + + return realUvfsFsync(loop, req, file, cb); +} + +int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); + } + + static int (*realUvfsSendfile)(uv_loop_t *, uv_fs_t *, uv_file, uv_file, int64_t, size_t, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, uv_file, int64_t, size_t, uv_fs_cb))dlsym( + RTLD_NEXT, "uv_fs_sendfile"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_sendfile: " << dlerror(); + } + return func; + }(); + + if (!realUvfsSendfile) { + return -1; + } + + return realUvfsSendfile(loop, req, outFd, inFd, off, len, cb); +} + +int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_lstat(loop, req, path, cb); + } + + static int (*realUvfsLstat)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_lstat"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_lstat: " << dlerror(); + } + return func; + }(); + + if (!realUvfsLstat) { + return -1; + } + + return realUvfsLstat(loop, req, path, cb); +} + +int uv_fs_fstat(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + if (UvfsMock::IsMockable()) { + return UvfsMock::GetMock()->uv_fs_fstat(loop, req, file, cb); + } + + static int (*realUvfsFstat)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = []() { + auto func = (int (*)(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb))dlsym(RTLD_NEXT, "uv_fs_fstat"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real uv_fs_fstat: " << dlerror(); + } + return func; + }(); + + if (!realUvfsFstat) { + return -1; + } + + return realUvfsFstat(loop, req, file, cb); +} +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.h new file mode 100644 index 000000000..1e970f8d9 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/uv_fs_mock.h @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this 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_PROPERTIES_MOCK_UV_FS_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H + +#include "uv.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +class IUvfs { +public: + virtual ~IUvfs() = default; + virtual int uv_fs_read(uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb) = 0; + virtual int uv_fs_readlink(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_stat(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_utime(uv_loop_t *, uv_fs_t *, const char *, double, double, uv_fs_cb) = 0; + virtual int uv_fs_scandir(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb) = 0; + virtual int uv_fs_scandir_next(uv_fs_t *, uv_dirent_t *) = 0; + virtual int uv_fs_rmdir(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_symlink(uv_loop_t *, uv_fs_t *, const char *, const char *, int, uv_fs_cb) = 0; + virtual int uv_fs_open(uv_loop_t *, uv_fs_t *, const char *, int, int, uv_fs_cb) = 0; + virtual int uv_fs_ftruncate(uv_loop_t *, uv_fs_t *, uv_file, int64_t, uv_fs_cb) = 0; + virtual int uv_fs_write(uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb) = 0; + virtual int uv_fs_realpath(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_close(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = 0; + virtual int uv_fs_fdatasync(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = 0; + virtual int uv_fs_mkdir(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb) = 0; + virtual int uv_fs_access(uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb) = 0; + virtual int uv_fs_mkdtemp(uv_loop_t *, uv_fs_t *, const char *tpl, uv_fs_cb) = 0; + virtual int uv_fs_unlink(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_rename(uv_loop_t *, uv_fs_t *, const char *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_fsync(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t *, uv_fs_t *, uv_file, uv_file, int64_t, size_t, uv_fs_cb) = 0; + virtual int uv_fs_lstat(uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb) = 0; + virtual int uv_fs_fstat(uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb) = 0; + virtual void uv_fs_req_cleanup(uv_fs_t *) = 0; +}; + +class UvfsMock : public IUvfs { +public: + MOCK_METHOD(int, uv_fs_read, (uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb), + (override)); + MOCK_METHOD(int, uv_fs_readlink, (uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_stat, (uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_utime, (uv_loop_t *, uv_fs_t *, const char *, double, double, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_scandir, (uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_scandir_next, (uv_fs_t *, uv_dirent_t *), (override)); + MOCK_METHOD(int, uv_fs_rmdir, (uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_symlink, (uv_loop_t *, uv_fs_t *, const char *, const char *, int, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_open, (uv_loop_t *, uv_fs_t *, const char *, int, int, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_ftruncate, (uv_loop_t *, uv_fs_t *, uv_file, int64_t, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_write, (uv_loop_t *, uv_fs_t *, uv_file, const uv_buf_t[], unsigned int, int64_t, uv_fs_cb), + (override)); + MOCK_METHOD(int, uv_fs_realpath, (uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_close, (uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_fdatasync, (uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_mkdir, (uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_access, (uv_loop_t *, uv_fs_t *, const char *, int, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_mkdtemp, (uv_loop_t *, uv_fs_t *, const char *tpl, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_unlink, (uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_rename, (uv_loop_t *, uv_fs_t *, const char *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_fsync, (uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_sendfile, (uv_loop_t *, uv_fs_t *, uv_file, uv_file, int64_t, size_t, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_lstat, (uv_loop_t *, uv_fs_t *, const char *, uv_fs_cb), (override)); + MOCK_METHOD(int, uv_fs_fstat, (uv_loop_t *, uv_fs_t *, uv_file, uv_fs_cb), (override)); + MOCK_METHOD(void, uv_fs_req_cleanup, (uv_fs_t *), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr uvfsMock; + static thread_local bool mockable; +}; + +} // namespace OHOS::FileManagement::ModuleFileIO::Test +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp index 234800498..9eb458c77 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp @@ -13,11 +13,12 @@ * limitations under the License. */ - #include "access_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -30,21 +31,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline std::shared_ptr uvMock = nullptr; const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; }; void AccessCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "AccessCoreMockTest"); + UvfsMock::EnableMock(); } void AccessCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -72,9 +71,11 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::Test std::string path = "TEST"; std::optional mode; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-1)); auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_001"; } @@ -93,10 +94,12 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::Test std::string path = "TEST"; std::optional mode; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_002"; } @@ -116,9 +119,11 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::Test AccessModeType mode = AccessModeType::EXIST; AccessFlag flag = DEFAULT_FLAG; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-1)); auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_003"; } @@ -138,12 +143,14 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_004, testing::ext::Test AccessModeType mode = AccessModeType::EXIST; AccessFlag flag = DEFAULT_FLAG; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_004"; } -} // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/close_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/close_core_mock_test.cpp index 8c9eafcfe..19495cc09 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/close_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/close_core_mock_test.cpp @@ -13,16 +13,18 @@ * limitations under the License. */ +#include "close_core.h" + #include #include #include -#include "close_core.h" -#include "mock/uv_fs_mock.h" +#include "uv_fs_mock.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { +namespace Test { using namespace std; class CloseCoreMockTest : public testing::Test { public: @@ -30,21 +32,18 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline std::shared_ptr uvMock = nullptr; }; void CloseCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; prctl(PR_SET_NAME, "CloseCoreMockTest"); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void CloseCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -75,6 +74,7 @@ HWTEST_F(CloseCoreMockTest, CloseCoreMockTest_DoClose_001, testing::ext::TestSiz // Prepare test parameters int fd = EXPECTED_FD; // Set mock behaviors + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_close(testing::_, testing::_, testing::_, testing::_)) .Times(1) .WillOnce(testing::Return(UV_EBADF)); @@ -87,6 +87,7 @@ HWTEST_F(CloseCoreMockTest, CloseCoreMockTest_DoClose_001, testing::ext::TestSiz GTEST_LOG_(INFO) << "CloseCoreMockTest-end CloseCoreMockTest_DoClose_001"; } +} // namespace Test } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS \ No newline at end of file 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 053b6cc9c..92da9a3ef 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 @@ -13,17 +13,19 @@ * limitations under the License. */ +#include "copy_core.h" + #include #include #include #include +#include -#include "copy_core.h" #include "inotify_mock.h" #include "mock_progress_listener.h" -#include "mock/uv_fs_mock.h" #include "poll_mock.h" #include "unistd_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -36,7 +38,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; static const string testDir; static const string srcDir; @@ -60,6 +61,7 @@ const string CopyCoreMockTest::destFile = destDir + "/dest.txt"; void CopyCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "CopyCoreMockTest"); mkdir(testDir.c_str(), permission0755); mkdir(srcDir.c_str(), permission0755); mkdir(destDir.c_str(), permission0755); @@ -69,8 +71,7 @@ void CopyCoreMockTest::SetUpTestCase(void) ASSERT_TRUE(false); } close(fd); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); InotifyMock::EnableMock(); PollMock::EnableMock(); UnistdMock::EnableMock(); @@ -84,8 +85,7 @@ void CopyCoreMockTest::TearDownTestCase(void) rmdir(srcDir.c_str()); rmdir(destDir.c_str()); rmdir(testDir.c_str()); - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); InotifyMock::DisableMock(); PollMock::DisableMock(); UnistdMock::DisableMock(); @@ -118,12 +118,15 @@ HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopyFile_001, testing::ext::TestSize infos->srcPath = CopyCoreMockTest::srcFile; infos->destPath = CopyCoreMockTest::destFile; auto unistdMock = UnistdMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto res = CopyCore::CopyFile(srcFile, destFile, infos); EXPECT_EQ(res, errno); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopyFile_001"; } @@ -143,6 +146,7 @@ HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_DoCopy_001, testing::ext::TestSize.L string destUri = "file://" + destFile; optional options; auto unistdMock = UnistdMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).WillOnce(Return(0)); @@ -152,6 +156,8 @@ HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_DoCopy_001, testing::ext::TestSize.L EXPECT_TRUE(filesystem::exists(destFile)); int ret = remove(destFile.c_str()); EXPECT_TRUE(ret == 0); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_DoCopy_001"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp index 9371a60cd..ff7bcc0a0 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp @@ -14,9 +14,11 @@ */ #include "copy_file_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,20 +31,18 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvfs = nullptr; }; void CopyFileCoreMockTest::SetUpTestCase(void) { - uvfs = std::make_shared(); - Uvfs::ins = uvfs; GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "CopyFileCoreMockTest"); + UvfsMock::EnableMock(); } void CopyFileCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvfs = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -93,10 +93,11 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_003, testing::ext src.isPath = true; dest.isPath = false; - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); - + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_003"; } @@ -119,8 +120,8 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_005, testing::ext int fd = open("CopyFileCoreMockTest_DoCopyFile_005.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); ASSERT_NE(fd, -1); src.fdg = make_unique(fd); - - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); @@ -146,8 +147,8 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_006, testing::ext int fd = open("CopyFileCoreMockTest_DoCopyFile_006.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); ASSERT_NE(fd, -1); src.fdg = make_unique(fd); - - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), true); @@ -177,10 +178,12 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_007, testing::ext src.fdg = make_unique(srcFd); dest.fdg = make_unique(destFd); - EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(-1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_007"; } @@ -204,8 +207,8 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_008, testing::ext ASSERT_NE(fd, -1); src.fdg = make_unique(fd); dest.fdg = make_unique(); - - EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); @@ -235,8 +238,8 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_009, testing::ext ASSERT_NE(destfd, -1); src.fdg = make_unique(srcfd); dest.fdg = make_unique(destfd); - - EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), true); @@ -266,9 +269,9 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0010, testing::ex ssize_t len = write(srcfd, data, strlen(data)); ASSERT_NE(len, -1); src.fdg = make_unique(srcfd); - - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); - EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); @@ -298,9 +301,9 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0011, testing::ex ssize_t len = write(srcfd, data, strlen(data)); ASSERT_NE(len, -1); src.fdg = make_unique(srcfd); - - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); - EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len + 1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len + 1)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); @@ -330,9 +333,9 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0012, testing::ex ssize_t len = write(srcfd, data, strlen(data)); ASSERT_NE(len, -1); src.fdg = make_unique(srcfd); - - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); - EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), true); @@ -362,9 +365,9 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0013, testing::ex ssize_t len = write(srcfd, data, strlen(data)); ASSERT_NE(len, -1); src.fdg = make_unique(srcfd); - - EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); - EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(0)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(0)); auto res = CopyFileCore::DoCopyFile(src, dest); EXPECT_EQ(res.IsSuccess(), false); diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp index 2dc12961c..d1c9b940a 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp @@ -13,10 +13,12 @@ * limitations under the License. */ +#include "create_randomaccessfile_core.h" + #include #include +#include -#include "create_randomaccessfile_core.h" #include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -30,20 +32,18 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void CreateRandomAccessFileCoreMockTest::SetUpTestCase(void) { - uvMock = std::make_shared(); - Uvfs::ins = uvMock; GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "CreateRandomAccessFileCoreMockTest"); + UvfsMock::EnableMock(); } void CreateRandomAccessFileCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -65,7 +65,7 @@ void CreateRandomAccessFileCoreMockTest::TearDown(void) * @tc.level Level 1 */ HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001, - testing::ext::TestSize.Level1) + testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; @@ -73,9 +73,11 @@ HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_ int32_t mode = 0; optional options = nullopt; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; } @@ -88,7 +90,7 @@ HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_ * @tc.level Level 1 */ HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002, - testing::ext::TestSize.Level1) + testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; @@ -98,9 +100,11 @@ HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_ opts.start = 0; opts.end = 100; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp index 826208e31..e5e4a06d2 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp @@ -14,9 +14,12 @@ */ #include "dup_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "unistd_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,20 +32,20 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void DupCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "DupCoreMockTest"); + UvfsMock::EnableMock(); + UnistdMock::EnableMock(); } void DupCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); + UnistdMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -68,10 +71,14 @@ HWTEST_F(DupCoreMockTest, DupCoreMockTest_DoDup_001, testing::ext::TestSize.Leve GTEST_LOG_(INFO) << "DupCoreMockTest-begin DupCoreMockTest_DoDup_001"; int32_t fd = 1; - + + auto unistdMock = UnistdMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_readlink(_, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*unistdMock, dup(_)).WillOnce(Return(1)); auto res = DupCore::DoDup(fd); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "DupCoreMockTest-end DupCoreMockTest_DoDup_001"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp index c6308de50..f4bf00252 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ +#include "fdatasync_core.h" + #include #include #include #include +#include -#include "fdatasync_core.h" -#include "mock/uv_fs_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -32,21 +34,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void FDataSyncCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "FDataSyncCoreMockTest"); + UvfsMock::EnableMock(); } void FDataSyncCoreMockTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); } void FDataSyncCoreMockTest::SetUp(void) @@ -71,10 +71,11 @@ HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_001, testing:: GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_001"; int fd = 3; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(1)); auto res = FDataSyncCore::DoFDataSync(fd); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_001"; } @@ -91,10 +92,11 @@ HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_002, testing:: GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_002"; int fd = 3; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(-1)); auto res = FDataSyncCore::DoFDataSync(fd); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_002"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp index 688a5bca0..9dea8e837 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp @@ -14,9 +14,11 @@ */ #include "fsync_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,20 +31,18 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvfs = nullptr; }; void FsyncCoreMockTest::SetUpTestCase(void) { - uvfs = std::make_shared(); - Uvfs::ins = uvfs; GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "FsyncCoreMockTest"); + UvfsMock::EnableMock(); } void FsyncCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvfs = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -67,10 +67,12 @@ HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_001"; - EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_fsync(_, _, _, _)).WillOnce(Return(-1)); auto res = FsyncCore::DoFsync(1); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_001"; } @@ -86,13 +88,14 @@ HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_002, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_002"; - EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_fsync(_, _, _, _)).WillOnce(Return(1)); auto res = FsyncCore::DoFsync(1); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_002"; } - } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp index 691f2b3de..402081b59 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp @@ -13,10 +13,12 @@ * limitations under the License. */ +#include "lstat_core.h" + #include #include +#include -#include "lstat_core.h" #include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -30,21 +32,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void LstatCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "LstatCoreMockTest"); + UvfsMock::EnableMock(); } void LstatCoreMockTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); } void LstatCoreMockTest::SetUp(void) @@ -67,13 +67,14 @@ void LstatCoreMockTest::TearDown(void) HWTEST_F(LstatCoreMockTest, LstatCoreMockTest_DoLstat_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "LstatCoreMockTest-begin LstatCoreMockTest_DoLstat_001"; - - EXPECT_CALL(*uvMock, uv_fs_lstat(_, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_lstat(_, _, _, _)).WillOnce(Return(-1)); auto res = LstatCore::DoLstat("/data/test/lstat.txt"); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "LstatCoreMockTest-end LstatCoreMockTest_DoLstat_001"; } -} \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp index a50211973..ef0032b83 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp @@ -13,12 +13,14 @@ * limitations under the License. */ +#include "mkdir_core.h" + #include #include #include #include +#include -#include "mkdir_core.h" #include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -33,7 +35,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; filesystem::path MkdirCoreMockTest::tempFilePath; @@ -41,18 +42,17 @@ filesystem::path MkdirCoreMockTest::tempFilePath; void MkdirCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "MkdirCoreMockTest"); tempFilePath = filesystem::temp_directory_path() / "mkdir_test"; std::filesystem::create_directory(tempFilePath); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void MkdirCoreMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; + UvfsMock::DisableMock(); filesystem::remove_all(tempFilePath); - Uvfs::ins = nullptr; - uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; } void MkdirCoreMockTest::SetUp(void) @@ -76,11 +76,13 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0001, testing::ext::TestSi { GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0001"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(0)); string path = tempFilePath.string() + "/test01"; auto ret = MkdirCore::DoMkdir(path); EXPECT_EQ(ret.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0001"; } @@ -96,11 +98,13 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0002, testing::ext::TestSi { GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0002"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-2)).WillOnce(Return(0)); string path = tempFilePath.string() + "/test02/testDir"; auto ret = MkdirCore::DoMkdir(path, true); EXPECT_EQ(ret.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0002"; } @@ -116,11 +120,13 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0003, testing::ext::TestSi { GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0003"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(1)); string path = tempFilePath.string() + "/test03"; auto ret = MkdirCore::DoMkdir(path); EXPECT_EQ(ret.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0003"; } @@ -136,6 +142,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0004, testing::ext::TestSi { GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0004"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); string path = "/"; @@ -146,6 +153,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0004, testing::ext::TestSi EXPECT_EQ(errCode, 13900015); auto msg = err.GetErrMsg(); EXPECT_EQ(msg, "File exists"); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0004"; } @@ -161,6 +169,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0005, testing::ext::TestSi { GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0005"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(2)); string path = ""; @@ -171,6 +180,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0005, testing::ext::TestSi EXPECT_EQ(errCode, 13900002); auto msg = err.GetErrMsg(); EXPECT_EQ(msg, "No such file or directory"); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0005"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp index d207773de..c134cc73b 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ +#include "mkdtemp_core.h" + #include #include #include #include +#include -#include "mkdtemp_core.h" -#include "mock/uv_fs_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -33,7 +35,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; filesystem::path MkdtempCoreMockTest::tempFilePath; @@ -41,18 +42,17 @@ filesystem::path MkdtempCoreMockTest::tempFilePath; void MkdtempCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "MkdtempCoreMockTest"); tempFilePath = filesystem::temp_directory_path() / "test"; std::filesystem::create_directory(tempFilePath); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void MkdtempCoreMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; + UvfsMock::DisableMock(); filesystem::remove_all(tempFilePath); - Uvfs::ins = nullptr; - uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; } void MkdtempCoreMockTest::SetUp(void) @@ -79,14 +79,16 @@ HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0001, testing::ext:: uv_fs_t mock_req; mock_req.path = const_cast("/data/local/tmp/test/XXXXXX"); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)) - .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + .WillOnce(Invoke([&](uv_loop_t *, uv_fs_t *req, const char *, uv_fs_cb) { *req = mock_req; return 0; })); auto ret = MkdtempCore::DoMkdtemp("/data/local/tmp/test/XXXXXX"); EXPECT_EQ(ret.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0001"; } @@ -104,9 +106,11 @@ HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0002, testing::ext:: string path = tempFilePath.string() + "/XXXXXX"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)).WillOnce(Return(-1)); auto ret = MkdtempCore::DoMkdtemp(path); EXPECT_EQ(ret.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0002"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.cpp new file mode 100644 index 000000000..4f09ef06e --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.cpp @@ -0,0 +1,81 @@ +/* + * 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 "dfs_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr DfsMock::dfsMock = nullptr; +thread_local bool DfsMock::mockable = false; + +std::shared_ptr DfsMock::GetMock() +{ + if (dfsMock == nullptr) { + dfsMock = std::make_shared(); + } + return dfsMock; +} + +void DfsMock::EnableMock() +{ + mockable = true; +} + +void DfsMock::DisableMock() +{ + dfsMock = nullptr; + mockable = false; +} + +bool DfsMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +namespace OHOS::Storage::DistributedFile { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +DistributedFileDaemonManager &DistributedFileDaemonManager::GetInstance() +{ + if (DfsMock::IsMockable()) { + return *DfsMock::GetMock(); + } + + static DistributedFileDaemonManager &(*realDfsGetInstance)() = []() { + auto func = (DistributedFileDaemonManager & (*)()) + dlsym(RTLD_NEXT, "_ZN4OHOS7Storage15DistributedFile28DistributedFileDaemonManager11GetInstanceEv"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real DistributedFileDaemonManager::GetInstance: " << dlerror(); + } + return func; + }(); + + if (!realDfsGetInstance) { + return *DfsMock::GetMock(); + } + + return realDfsGetInstance(); +} +} // namespace OHOS::Storage::DistributedFile diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.h new file mode 100644 index 000000000..16ac71324 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/dfs_mock.h @@ -0,0 +1,118 @@ +/* + * 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_PROPERTIES_MOCK_DFS_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_DFS_MOCK_H + +#include "distributed_file_daemon_manager.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace OHOS::Storage::DistributedFile; + +class DfsMock final : public DistributedFileDaemonManager { +public: + MOCK_METHOD(int32_t, PrepareSession, + (const std::string &srcUri, const std::string &dstUri, const std::string &srcDeviceId, + const sptr &listener, HmdfsInfo &info), + (override)); + + int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override + { + return 0; + } + int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override + { + return 0; + } + int32_t OpenP2PConnectionEx(const std::string &networkId, sptr remoteReverseObj) override + { + return 0; + } + int32_t CloseP2PConnectionEx(const std::string &networkId) override + { + return 0; + } + int32_t CancelCopyTask(const std::string &sessionName) override + { + return 0; + } + int32_t CancelCopyTask(const std::string &srcUri, const std::string &dstUri) + { + return 0; + } + int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) + { + return 0; + } + int32_t UpdateDfsSwitchStatus(int32_t switchStatus) + { + return 0; + } + int32_t GetConnectedDeviceList(std::vector &deviceList) + { + return 0; + } + int32_t PushAsset( + int32_t userId, const sptr &assetObj, const sptr &sendCallback) override + { + return 0; + } + int32_t RegisterAssetCallback(const sptr &recvCallback) override + { + return 0; + } + int32_t UnRegisterAssetCallback(const sptr &recvCallback) override + { + return 0; + } + int32_t GetSize(const std::string &uri, bool isSrcUri, uint64_t &size) override + { + return 0; + } + int32_t IsDirectory(const std::string &uri, bool isSrcUri, bool &isDirectory) override + { + return 0; + } + int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback processCallback) override + { + return 0; + } + int32_t Cancel(const std::string &srcUri, const std::string &destUri) override + { + return 0; + } + int32_t Cancel() override + { + return 0; + } + + DfsMock() = default; + ~DfsMock() = default; + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr dfsMock; + static thread_local bool mockable; +}; + +} // namespace OHOS::FileManagement::ModuleFileIO::Test +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_DFS_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/mock_progress_listener.h b/interfaces/test/unittest/js/mod_fs/properties/mock/mock_progress_listener.h similarity index 100% rename from interfaces/test/unittest/js/mod_fs/mock/mock_progress_listener.h rename to interfaces/test/unittest/js/mod_fs/properties/mock/mock_progress_listener.h diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp deleted file mode 100644 index b656a1ad1..000000000 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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 "uv_fs_mock.h" - - -using namespace OHOS::FileManagement::ModuleFileIO; - -int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, - uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_read(loop, req, file, bufs, nbufs, off, cb); -} - -int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_readlink(loop, req, path, cb); -} - -int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_stat(loop, req, path, cb); -} - -int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_utime(loop, req, path, atime, mtime, cb); -} - -int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_scandir(loop, req, path, flags, cb); -} - -int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) -{ - return Uvfs::ins->uv_fs_scandir_next(req, ent); -} - -int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); -} - -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); -} - -int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_open(loop, req, path, flags, mode, cb); -} - -int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_ftruncate(loop, req, fd, offset, cb); -} - -int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, - uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_write(loop, req, fd, bufs, nbufs, offset, cb); -} - -int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_realpath(loop, req, path, cb); -} - -int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_close(loop, req, file, cb); -} - -int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_fdatasync(loop, req, file, cb); -} - -int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_mkdir(loop, req, path, mode, cb); -} - -int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_access(loop, req, path, flags, cb); -} - -int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_mkdtemp(loop, req, tpl, cb); -} - -int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_unlink(loop, req, path, cb); -} - -void uv_fs_req_cleanup(uv_fs_t *req) -{ - return; -} - -int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_rename(loop, req, path, newPath, cb); -} - -int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_fsync(loop, req, file, cb); -} - -int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); -} - -int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) -{ - return Uvfs::ins->uv_fs_lstat(loop, req, path, cb); -} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h deleted file mode 100644 index a94f2dc32..000000000 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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_PROPERTIES_MOCK_UV_FS_MOCK_H -#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H - -#include "uv.h" - -#include - -namespace OHOS::FileManagement::ModuleFileIO { - -class Uvfs { -public: - static inline std::shared_ptr ins = nullptr; -public: - virtual ~Uvfs() = default; - virtual int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, - int64_t off, uv_fs_cb cb) = 0; - virtual int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, - double mtime, uv_fs_cb cb) = 0; - virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; - virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; - virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, - uv_fs_cb cb) = 0; - virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; - virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; - virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, - int64_t offset, uv_fs_cb cb) = 0; - virtual int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; - virtual int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; - virtual int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) = 0; - virtual int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; - virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; - virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; - virtual int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) = 0; - virtual int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, - int64_t off, size_t len, uv_fs_cb cb) = 0; - virtual int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; -}; - -class UvfsMock : public Uvfs { -public: - MOCK_METHOD7(uv_fs_read, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, - int64_t off, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_readlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_stat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, - uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); - MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); - MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, - uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); - MOCK_METHOD7(uv_fs_write, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, - int64_t offset, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_realpath, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_close, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_fdatasync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_mkdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_access, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb)); - MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, - int64_t off, size_t len, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_lstat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); -}; - -} // OHOS::FileManagement::ModuleFileIO -#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp index 175fd3ab1..68a28680d 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp @@ -14,9 +14,12 @@ */ #include "move_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_err_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,21 +32,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline std::shared_ptr uvMock = nullptr; }; void MoveCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "MoveCoreMockTest"); + UvfsMock::EnableMock(); } void MoveCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void MoveCoreMockTest::SetUp(void) @@ -56,13 +57,6 @@ void MoveCoreMockTest::TearDown(void) GTEST_LOG_(INFO) << "TearDown"; } -extern "C" { - const char* uv_err_name(int err) - { - return "EXDEV"; - } -} - /** * @tc.name: MoveCoreMockTest_DoMove_000 * @tc.desc: Test function of MoveCore::DoMove interface for SUCCESS. @@ -77,11 +71,13 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_000, testing::ext::TestSize.L std::string src; std::string dest; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(0)); auto res = MoveCore::DoMove(src, dest); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_000"; } @@ -99,12 +95,18 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_001, testing::ext::TestSize.L std::string src; std::string dest; - + UvErrMock::EnableMock(); + auto uvMock = UvfsMock::GetMock(); + auto uvErrMock = UvErrMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_001"; } @@ -123,12 +125,13 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_002, testing::ext::TestSize.L std::string src; std::string dest; optional mode = std::make_optional(MODE_FORCE_MOVE); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_002"; } @@ -145,11 +148,12 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_003, testing::ext::TestSize.L std::string src; std::string dest; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-1)); auto res = MoveCore::DoMove(src, dest); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_003"; } @@ -168,11 +172,12 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_004, testing::ext::TestSize.L std::string src; std::string dest; optional mode = std::make_optional(MODE_THROW_ERR); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)).WillOnce(Return(0)); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_004"; } @@ -191,11 +196,12 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_005, testing::ext::TestSize.L std::string src; std::string dest; optional mode = std::make_optional(MODE_THROW_ERR); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)).WillOnce(Return(-1)); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_005"; } @@ -214,19 +220,20 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_006, testing::ext::TestSize.L std::string src; std::string dest; optional mode = std::make_optional(MODE_FORCE_MOVE); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_006"; } /** * @tc.name: MoveCoreMockTest_DoMove_007 - * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.desc: Test function of MoveCore::DoMove interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -238,19 +245,20 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_007, testing::ext::TestSize.L std::string src; std::string dest; optional mode = std::make_optional(MODE_FORCE_MOVE); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_007"; } /** * @tc.name: MoveCoreMockTest_DoMove_008 - * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.desc: Test function of MoveCore::DoMove interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -261,12 +269,13 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_008, testing::ext::TestSize.L std::string src; std::string dest; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); auto res = MoveCore::DoMove(src, dest); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_008"; } @@ -285,13 +294,19 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_009, testing::ext::TestSize.L std::string src; std::string dest; optional mode = std::make_optional(MODE_FORCE_MOVE); - + UvErrMock::EnableMock(); + auto uvErrMock = UvErrMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_009"; } @@ -308,18 +323,24 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0010, testing::ext::TestSize. GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_0010"; std::string src; - std::string dest = "file.txt"; + std::string dest = "MoveCoreMockTest_DoMove_0010"; optional mode = std::make_optional(MODE_FORCE_MOVE); int fd = open(dest.c_str(), O_RDWR | O_CREAT, 0666); ASSERT_NE(fd, -1); - + UvErrMock::EnableMock(); + auto uvErrMock = UvErrMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); close(fd); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0010"; @@ -339,13 +360,19 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0011, testing::ext::TestSize. std::string src; std::string dest; optional mode = std::make_optional(MODE_FORCE_MOVE); - + UvErrMock::EnableMock(); + auto uvErrMock = UvErrMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0011"; } @@ -367,17 +394,23 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0012, testing::ext::TestSize. int fd = open(src.c_str(), O_RDWR | O_CREAT, 0666); ASSERT_NE(fd, -1); - + UvErrMock::EnableMock(); + auto uvErrMock = UvErrMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)).WillOnce(Return(-1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); - ASSERT_EQ(unlink(src.c_str()), 0); close(fd); + ASSERT_EQ(unlink(src.c_str()), 0); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0012"; } @@ -400,16 +433,23 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0013, testing::ext::TestSize. int fd = open(src.c_str(), O_RDWR | O_CREAT, 0666); ASSERT_NE(fd, -1); + UvErrMock::EnableMock(); + auto uvErrMock = UvErrMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)).WillOnce(Return(1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); - ASSERT_EQ(unlink(src.c_str()), 0); close(fd); + ASSERT_EQ(unlink(src.c_str()), 0); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0013"; } @@ -431,20 +471,25 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0014, testing::ext::TestSize. int fd = open(src.c_str(), O_RDWR | O_CREAT, 0666); ASSERT_NE(fd, -1); - + UvErrMock::EnableMock(); + auto uvErrMock = UvErrMock::GetMock(); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvErrMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); auto res = MoveCore::DoMove(src, dest, mode); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + testing::Mock::VerifyAndClearExpectations(uvErrMock.get()); + UvErrMock::DisableMock(); - ASSERT_EQ(unlink(src.c_str()), 0); close(fd); + ASSERT_EQ(unlink(src.c_str()), 0); GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0013"; } - } // namespace OHOS::FileManagement::ModuleFileIO::Test \ 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 abee054b1..7abf8f876 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,16 @@ * limitations under the License. */ +#include "open_core.h" + #include #include #include #include +#include -#include "mock/uv_fs_mock.h" -#include "open_core.h" #include "unistd_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -33,25 +35,21 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - -private: - static inline shared_ptr uvMock = nullptr; }; void OpenCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "OpenCoreMockTest"); + UvfsMock::EnableMock(); UnistdMock::EnableMock(); } void OpenCoreMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; + UvfsMock::DisableMock(); UnistdMock::DisableMock(); + GTEST_LOG_(INFO) << "TearDownTestCase"; } void OpenCoreMockTest::SetUp(void) @@ -78,9 +76,11 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.L string path = "/test/OpenCoreMockTest_DoOpen_001"; int32_t mode = 0; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); auto res = OpenCore::DoOpen(path, mode); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_001"; } @@ -98,13 +98,14 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_002, testing::ext::TestSize.L string path = "file://test/OpenCoreMockTest_DoOpen_002"; int32_t mode = 0; - + auto uvMock = UvfsMock::GetMock(); 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)); auto res = OpenCore::DoOpen(path, mode); testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); EXPECT_EQ(res.IsSuccess(), true); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_002"; @@ -124,11 +125,13 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_003, testing::ext::TestSize.L string path = "file://test/OpenCoreMockTest_DoOpen_003"; int32_t mode = 0; + auto uvMock = UvfsMock::GetMock(); 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)); auto res = OpenCore::DoOpen(path, mode); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); testing::Mock::VerifyAndClearExpectations(&unistdMock); EXPECT_EQ(res.IsSuccess(), false); @@ -149,8 +152,10 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_004, testing::ext::TestSize.L string path = "/test/OpenCoreMockTest_DoOpen_004"; int32_t mode = 0; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = OpenCore::DoOpen(path, mode); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); EXPECT_EQ(res.IsSuccess(), false); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_004"; diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp index b5e5506e0..1e92b6b84 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp @@ -14,11 +14,13 @@ */ #include "read_core.h" -#include "uv_fs_mock.h" #include #include #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -31,21 +33,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline std::shared_ptr uvMock = nullptr; }; void ReadCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "ReadCoreMockTest"); + UvfsMock::EnableMock(); } void ReadCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void ReadCoreMockTest::SetUp(void) @@ -73,10 +73,12 @@ HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_001, testing::ext::TestSize.L void *buf = nullptr; ArrayBuffer arrayBuffer(buf, 0); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_001"; } @@ -96,10 +98,12 @@ HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_002, testing::ext::TestSize.L void *buf = nullptr; ArrayBuffer arrayBuffer(buf, 0); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(1)); auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_002"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp index 118e5e28a..ff7046af6 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ +#include "read_lines_core.h" + #include #include #include #include +#include -#include "mock/uv_fs_mock.h" -#include "read_lines_core.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -33,7 +35,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; filesystem::path ReadLinesCoreMockTest::tempFilePath; @@ -41,19 +42,18 @@ filesystem::path ReadLinesCoreMockTest::tempFilePath; void ReadLinesCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "ReadLinesCoreMockTest"); tempFilePath = filesystem::temp_directory_path() / "read_lines_test_file.txt"; ofstream(tempFilePath) << "Test content\n123\n456"; ofstream(tempFilePath).close(); - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void ReadLinesCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); filesystem::remove(tempFilePath); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void ReadLinesCoreMockTest::SetUp(void) @@ -81,9 +81,11 @@ HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_001, testing:: Options option; option.encoding = "utf-8"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); auto res = ReadLinesCore::DoReadLines(path, option); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_001"; } @@ -100,10 +102,11 @@ HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_002, testing:: GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_002"; string path = tempFilePath.string(); - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); auto res = ReadLinesCore::DoReadLines(path); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_002"; } @@ -122,10 +125,11 @@ HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_003, testing:: string path = tempFilePath.string(); Options option; option.encoding = "utf-8"; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); auto res = ReadLinesCore::DoReadLines(path, option); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_003"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp index 910487a21..f27d55ea0 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp @@ -14,9 +14,11 @@ */ #include "rename_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,20 +31,18 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvfs = nullptr; }; void RenameCoreMockTest::SetUpTestCase(void) { - uvfs = std::make_shared(); - Uvfs::ins = uvfs; GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "RenameCoreMockTest"); + UvfsMock::EnableMock(); } void RenameCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvfs = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -69,10 +69,12 @@ HWTEST_F(RenameCoreMockTest, RenameCoreMockTest_DoRename_001, testing::ext::Test string src; string dest; - EXPECT_CALL(*uvfs, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); auto res = RenameCore::DoRename(src, dest); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "RenameCoreMockTest-end RenameCoreMockTest_DoRename_001"; } @@ -89,14 +91,14 @@ HWTEST_F(RenameCoreMockTest, RenameCoreMockTest_DoRename_002, testing::ext::Test GTEST_LOG_(INFO) << "RenameCoreMockTest-begin RenameCoreMockTest_DoRename_002"; string src; string dest; - - EXPECT_CALL(*uvfs, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); auto res = RenameCore::DoRename(src, dest); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "RenameCoreMockTest-end RenameCoreMockTest_DoRename_002"; } - } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp index b8e67585e..4c61d322e 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp @@ -14,10 +14,15 @@ */ #include "stat_core.h" -#include "uv_fs_mock.h" #include +#include + #include +#include + +#include "securec.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -30,20 +35,18 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvfs = nullptr; }; void StatCoreMockTest::SetUpTestCase(void) { - uvfs = std::make_shared(); - Uvfs::ins = uvfs; GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "StatCoreMockTest"); + UvfsMock::EnableMock(); } void StatCoreMockTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvfs = nullptr; + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -57,9 +60,23 @@ void StatCoreMockTest::TearDown(void) GTEST_LOG_(INFO) << "TearDown"; } +static bool SetPathForFileInfo(FileInfo &fileInfo, const string_view &path) +{ + auto len = path.length() + 1; + auto pathPtr = std::make_unique(len); + int ret = strcpy_s(pathPtr.get(), len, path.data()); + if (ret != 0) { + GTEST_LOG_(ERROR) << "SetPathForFileInfo failed! ret: " << ret; + return false; + } + fileInfo.path = std::move(pathPtr); + fileInfo.isPath = true; + return true; +} + /** * @tc.name: StatCoreMockTest_DoStat_001 - * @tc.desc: Test function of StatCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE when is path. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -69,20 +86,23 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_001, testing::ext::TestSize.L GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_001"; FileInfo fileinfo; - fileinfo.path = std::make_unique(1); - fileinfo.isPath = true; + fileinfo.fdg = nullptr; + auto succ = SetPathForFileInfo(fileinfo, "StatCoreMockTest_DoStat_001"); + ASSERT_TRUE(succ); - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); auto res = StatCore::DoStat(fileinfo); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_001"; } /** * @tc.name: StatCoreMockTest_DoStat_002 - * @tc.desc: Test function of StatCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for SUCCESS when is path. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -92,20 +112,22 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_002, testing::ext::TestSize.L GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_002"; FileInfo fileinfo; - fileinfo.path = std::make_unique(1); - fileinfo.isPath = true; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + fileinfo.fdg = nullptr; + auto succ = SetPathForFileInfo(fileinfo, "StatCoreMockTest_DoStat_002"); + ASSERT_TRUE(succ); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); auto res = StatCore::DoStat(fileinfo); - EXPECT_EQ(res.IsSuccess(), false); + EXPECT_TRUE(res.IsSuccess()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_002"; } /** * @tc.name: StatCoreMockTest_DoStat_003 - * @tc.desc: Test function of StatCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE when is FD. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -115,20 +137,22 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_003, testing::ext::TestSize.L GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_003"; FileInfo fileinfo; - fileinfo.path = std::make_unique(1); + fileinfo.fdg = std::make_unique(1, false); + fileinfo.path = nullptr; fileinfo.isPath = false; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_fstat(_, _, _, _)).WillOnce(Return(-1)); auto res = StatCore::DoStat(fileinfo); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_003"; } /** * @tc.name: StatCoreMockTest_DoStat_004 - * @tc.desc: Test function of StatCore::DoStat interface for SUCCESS. + * @tc.desc: Test function of StatCore::DoStat interface for SUCCESS when is FD. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -138,14 +162,15 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_004, testing::ext::TestSize.L GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_004"; FileInfo fileinfo; - fileinfo.path = std::make_unique(1); fileinfo.fdg = std::make_unique(1, false); + fileinfo.path = nullptr; fileinfo.isPath = false; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_fstat(_, _, _, _)).WillOnce(Return(1)); auto res = StatCore::DoStat(fileinfo); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_004"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp index f11d92625..74f273db8 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp @@ -81,7 +81,6 @@ HWTEST_F(StatCoreTest, StatCoreTest_DoStat_002, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_002"; FileInfo fileinfo; - fileinfo.path = std::make_unique(1); fileinfo.fdg = std::make_unique(-1); fileinfo.isPath = false; diff --git a/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp index dd6e4b43b..95bfd2786 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp @@ -13,11 +13,12 @@ * limitations under the License. */ - #include "symlink_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -30,21 +31,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void SymlinkCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "SymlinkCoreMockTest"); + UvfsMock::EnableMock(); } void SymlinkCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void SymlinkCoreMockTest::SetUp(void) @@ -71,10 +70,12 @@ HWTEST_F(SymlinkCoreMockTest, SymlinkCoreMockTest_DoSymlink_001, testing::ext::T string target; string srcPath; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = SymlinkCore::DoSymlink(target, srcPath); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreMockTest_DoSymlink_001"; } @@ -92,13 +93,14 @@ HWTEST_F(SymlinkCoreMockTest, SymlinkCoreMockTest_DoSymlink_002, testing::ext::T string target; string srcPath; - + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(1)); auto res = SymlinkCore::DoSymlink(target, srcPath); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreMockTest_DoSymlink_002"; } -} // OHOS::FileManagement::ModuleFileIO::Test +} // namespace OHOS::FileManagement::ModuleFileIO::Test 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 376307c3e..57d5d5411 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 @@ -18,128 +18,20 @@ #include #include #include +#include #include "copy_core.h" +#include "dfs_mock.h" #include "unistd_mock.h" using namespace OHOS; using namespace OHOS::Storage::DistributedFile; -class MockDistributedFileDaemonManager final : public DistributedFileDaemonManager { -public: - MOCK_METHOD(int32_t, PrepareSession, - (const std::string &srcUri, const std::string &dstUri, const std::string &srcDeviceId, - const sptr &listener, HmdfsInfo &info), - (override)); - - int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override - { - return 0; - } - int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override - { - return 0; - } - int32_t OpenP2PConnectionEx(const std::string &networkId, sptr remoteReverseObj) override - { - return 0; - } - int32_t CloseP2PConnectionEx(const std::string &networkId) override - { - return 0; - } - int32_t CancelCopyTask(const std::string &sessionName) override - { - return 0; - } - int32_t CancelCopyTask(const std::string &srcUri, const std::string &dstUri) - { - return 0; - } - int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) - { - return 0; - } - int32_t UpdateDfsSwitchStatus(int32_t switchStatus) - { - return 0; - } - int32_t GetConnectedDeviceList(std::vector &deviceList) - { - return 0; - } - int32_t PushAsset( - int32_t userId, const sptr &assetObj, const sptr &sendCallback) override - { - return 0; - } - int32_t RegisterAssetCallback(const sptr &recvCallback) override - { - return 0; - } - int32_t UnRegisterAssetCallback(const sptr &recvCallback) override - { - return 0; - } - int32_t GetSize(const std::string &uri, bool isSrcUri, uint64_t &size) override - { - return 0; - } - int32_t IsDirectory(const std::string &uri, bool isSrcUri, bool &isDirectory) override - { - return 0; - } - int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback processCallback) override - { - return 0; - } - int32_t Cancel(const std::string &srcUri, const std::string &destUri) override - { - return 0; - } - int32_t Cancel() override - { - return 0; - } - - MockDistributedFileDaemonManager() = default; - ~MockDistributedFileDaemonManager() = default; - -public: - static std::shared_ptr GetMock(); - static void DisableMock(); - -private: - static thread_local std::shared_ptr managerMock; -}; - -thread_local std::shared_ptr MockDistributedFileDaemonManager::managerMock = nullptr; - -std::shared_ptr MockDistributedFileDaemonManager::GetMock() -{ - if (managerMock == nullptr) { - managerMock = std::make_shared(); - } - return managerMock; -} - -void MockDistributedFileDaemonManager::DisableMock() -{ - managerMock = nullptr; -} - class MockTaskSignalListener : public OHOS::DistributedFS::ModuleTaskSignal::TaskSignalListener { public: MOCK_METHOD(void, OnCancel, (), (override)); }; -#ifdef ENABLE_DISTRIBUTED_FILE_MOCK -DistributedFileDaemonManager &DistributedFileDaemonManager::GetInstance() -{ - return *MockDistributedFileDaemonManager::GetMock(); -} -#endif - namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -165,6 +57,8 @@ public: void TransListenerCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "TransListenerCoreMockTest"); + int32_t fd = open(g_path.c_str(), O_CREAT | O_RDWR, 0644); if (fd < 0) { GTEST_LOG_(ERROR) << "Open test file failed! ret: " << fd << ", errno: " << errno; @@ -172,12 +66,13 @@ void TransListenerCoreMockTest::SetUpTestCase(void) } close(fd); UnistdMock::EnableMock(); + DfsMock::EnableMock(); } void TransListenerCoreMockTest::TearDownTestCase(void) { rmdir(g_path.c_str()); - MockDistributedFileDaemonManager::DisableMock(); + DfsMock::DisableMock(); UnistdMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -205,18 +100,16 @@ HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_PrepareCopySession Storage::DistributedFile::HmdfsInfo info; info.authority = FILE_MANAGER_AUTHORITY; - info.authority = MEDIA_AUTHORITY; - string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + string srcUri = "TransListenerCoreMockTest_PrepareCopySession_001_src"; + string destUri = "TransListenerCoreMockTest_PrepareCopySession_001_dest"; string disSandboxPath = "disSandboxPath"; - auto unistdMock = UnistdMock::GetMock(); - auto managerMock = MockDistributedFileDaemonManager::GetMock(); - - EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); - EXPECT_CALL(*managerMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) + auto dfsMock = DfsMock::GetMock(); + EXPECT_CALL(*dfsMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return(ERRNO_NOERR)); - auto result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + auto result = TransListenerCore::PrepareCopySession(srcUri, destUri, nullptr, info, disSandboxPath); EXPECT_EQ(result, ERRNO_NOERR); + testing::Mock::VerifyAndClearExpectations(dfsMock.get()); GTEST_LOG_(INFO) << "TransListenerCoreMockTest-end TransListenerCoreMockTest_PrepareCopySession_001"; } @@ -232,21 +125,21 @@ HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_CopyFileFromSoftBu { GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_CopyFileFromSoftBus_001"; - Storage::DistributedFile::HmdfsInfo info; - info.authority = FILE_MANAGER_AUTHORITY; - - string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + string srcUri = "TransListenerCoreMockTest_CopyFileFromSoftBus_001"; + string destUri = "http://media?networkid=AD125AD1CF"; std::shared_ptr transListener = std::make_shared(); std::shared_ptr infos = std::make_shared(); auto unistdMock = UnistdMock::GetMock(); - auto managerMock = MockDistributedFileDaemonManager::GetMock(); + auto dfsMock = DfsMock::GetMock(); EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); - EXPECT_CALL(*managerMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) - .WillOnce(testing::Return(ERRNO_NOERR)); + EXPECT_CALL(*dfsMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(EIO)); - auto res = transListener->CopyFileFromSoftBus(srcUri, "destUri", infos, nullptr); + auto res = transListener->CopyFileFromSoftBus(srcUri, destUri, infos, nullptr); EXPECT_EQ(res, EIO); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + testing::Mock::VerifyAndClearExpectations(dfsMock.get()); GTEST_LOG_(INFO) << "TransListenerCoreMockTest-end TransListenerCoreMockTest_CopyFileFromSoftBus_001"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp index 15181a63d..0c0a44954 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp @@ -14,9 +14,11 @@ */ #include "truncate_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,21 +31,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void TruncateCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "TruncateCoreMockTest"); + UvfsMock::EnableMock(); } void TruncateCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void TruncateCoreMockTest::SetUp(void) @@ -69,11 +69,13 @@ HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_001, testing::ext FileInfo fileInfo; fileInfo.isPath = true; - fileInfo.fdg = std::make_unique(1); + fileInfo.fdg = std::make_unique(1, false); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = TruncateCore::DoTruncate(fileInfo); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_001"; } @@ -91,12 +93,14 @@ HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_002, testing::ext FileInfo fileInfo; fileInfo.isPath = true; - fileInfo.fdg = std::make_unique(1); + fileInfo.fdg = std::make_unique(1, false); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); auto res = TruncateCore::DoTruncate(fileInfo); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_002"; } @@ -114,12 +118,14 @@ HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_003, testing::ext FileInfo fileInfo; fileInfo.isPath = true; - fileInfo.fdg = std::make_unique(1); + fileInfo.fdg = std::make_unique(1, false); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); auto res = TruncateCore::DoTruncate(fileInfo); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_003"; } @@ -136,11 +142,12 @@ HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_004, testing::ext GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_004"; FileInfo fileInfo; - fileInfo.fdg = std::make_unique(1); - + fileInfo.fdg = std::make_unique(1, false); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); auto res = TruncateCore::DoTruncate(fileInfo); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_004"; } @@ -157,11 +164,12 @@ HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_005, testing::ext GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_005"; FileInfo fileInfo; - fileInfo.fdg = std::make_unique(1); - + fileInfo.fdg = std::make_unique(1, false); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); auto res = TruncateCore::DoTruncate(fileInfo); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_005"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp index a602df5c8..276d02d64 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ +#include "unlink_core.h" + #include #include #include #include +#include -#include "unlink_core.h" #include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -34,7 +36,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; filesystem::path UnlinkCoreMockTest::tempFilePath; @@ -42,18 +43,17 @@ filesystem::path UnlinkCoreMockTest::tempFilePath; void UnlinkCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "UnlinkCoreMockTest"); tempFilePath = filesystem::temp_directory_path() / "unlink_test_file.txt"; ofstream(tempFilePath) << "Test content\n123\n456"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + UvfsMock::EnableMock(); } void UnlinkCoreMockTest::TearDownTestCase(void) { - GTEST_LOG_(INFO) << "TearDownTestCase"; + UvfsMock::DisableMock(); filesystem::remove(tempFilePath); - Uvfs::ins = nullptr; - uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; } void UnlinkCoreMockTest::SetUp(void) @@ -77,11 +77,13 @@ HWTEST_F(UnlinkCoreMockTest, UnlinkCoreMockTest_DoUnlink_001, testing::ext::Test { GTEST_LOG_(INFO) << "UnlinkCoreMockTest-begin UnlinkCoreMockTest_DoUnlink_001"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(1)); string path = tempFilePath.string(); auto res = UnlinkCore::DoUnlink(path); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "UnlinkCoreMockTest-end UnlinkCoreMockTest_DoUnlink_001"; } @@ -97,11 +99,13 @@ HWTEST_F(UnlinkCoreMockTest, UnlinkCoreMockTest_DoUnlink_002, testing::ext::Test { GTEST_LOG_(INFO) << "UnlinkCoreMockTest-begin UnlinkCoreMockTest_DoUnlink_002"; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)); string path = tempFilePath.string(); auto res = UnlinkCore::DoUnlink(path); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "UnlinkCoreMockTest-end UnlinkCoreMockTest_DoUnlink_002"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp index c7d7e60c2..f77db627d 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp @@ -14,9 +14,11 @@ */ #include "utimes_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,21 +31,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void UtimesCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "UtimesCoreMockTest"); + UvfsMock::EnableMock(); } void UtimesCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void UtimesCoreMockTest::SetUp(void) @@ -70,9 +70,11 @@ HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_001, testing::ext::Test string path; double mtime = 1; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); auto res = UtimesCore::DoUtimes(path, mtime); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_001"; } @@ -91,10 +93,12 @@ HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_002, testing::ext::Test string path; double mtime = 1; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = UtimesCore::DoUtimes(path, mtime); EXPECT_EQ(res.IsSuccess(), false); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_002"; } @@ -113,10 +117,12 @@ HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_003, testing::ext::Test string path; double mtime = 1; + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(1)); auto res = UtimesCore::DoUtimes(path, mtime); EXPECT_EQ(res.IsSuccess(), true); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_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 index e9e20cf19..eaedd980b 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 @@ -13,6 +13,8 @@ * limitations under the License. */ +#include "watcher_core.h" + #include #include @@ -24,7 +26,6 @@ #include "fs_file_watcher.h" #include "inotify_mock.h" #include "mock_watcher_callback.h" -#include "watcher_core.h" namespace OHOS { namespace FileManagement { diff --git a/interfaces/test/unittest/js/mod_fs/properties/write_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/write_core_mock_test.cpp index e16651fad..dd5d44937 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/write_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/write_core_mock_test.cpp @@ -14,9 +14,11 @@ */ #include "write_core.h" -#include "uv_fs_mock.h" #include +#include + +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -29,21 +31,19 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvMock = nullptr; }; void WriteCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); - Uvfs::ins = uvMock; + prctl(PR_SET_NAME, "WriteCoreMockTest"); + UvfsMock::EnableMock(); } void WriteCoreMockTest::TearDownTestCase(void) { + UvfsMock::DisableMock(); GTEST_LOG_(INFO) << "TearDownTestCase"; - Uvfs::ins = nullptr; - uvMock = nullptr; } void WriteCoreMockTest::SetUp(void) @@ -57,66 +57,91 @@ void WriteCoreMockTest::TearDown(void) } /** - * @tc.name: WriteCoreMockTest_DoWrite1_001 - * @tc.desc: Test function of WriteCore::DoWrite3 interface for FALSE. + * @tc.name: WriteCoreMockTest_DoWrite_001 + * @tc.desc: Test function of WriteCore::DoWrite(StringBuffer) interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite1_001, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite1_001"; + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite_001"; int32_t fd = 1; string buffer; - - EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(1)); auto res = WriteCore::DoWrite(fd, buffer); - EXPECT_EQ(res.IsSuccess(), false); + EXPECT_TRUE(res.IsSuccess()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); - GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite1_001"; + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite_001"; } /** - * @tc.name: WriteCoreMockTest_DoWrite1_002 - * @tc.desc: Test function of WriteCore::DoWrite3 interface for SUCCESS. + * @tc.name: WriteCoreMockTest_DoWrite_002 + * @tc.desc: Test function of WriteCore::DoWrite(StringBuffer) interface for FAILURE when uv_fs_write fails. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite1_002, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite1_002"; + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite_002"; int32_t fd = 1; string buffer; + auto uvMock = UvfsMock::GetMock(); + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = WriteCore::DoWrite(fd, buffer); + EXPECT_FALSE(res.IsSuccess()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite_002"; +} + +/** + * @tc.name: WriteCoreMockTest_DoWrite_003 + * @tc.desc: Test function of WriteCore::DoWrite(ArrayBuffer) interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite_003"; + + int32_t fd = 1; + ArrayBuffer buffer(nullptr, 1); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(1)); auto res = WriteCore::DoWrite(fd, buffer); - EXPECT_EQ(res.IsSuccess(), true); + EXPECT_TRUE(res.IsSuccess()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); - GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite1_002"; + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite_003"; } /** - * @tc.name: WriteCoreMockTest_DoWrite2_003 - * @tc.desc: Test function of WriteCore::DoWrite2 interface for FALSE. + * @tc.name: WriteCoreMockTest_DoWrite_004 + * @tc.desc: Test function of WriteCore::DoWrite(ArrayBuffer) interface for FAILURE when uv_fs_write fails. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite2_003, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite2_003"; - - int32_t fd = -1; - string buffer; + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite_004"; + int32_t fd = 1; + ArrayBuffer buffer(nullptr, 1); + auto uvMock = UvfsMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto res = WriteCore::DoWrite(fd, buffer); - EXPECT_EQ(res.IsSuccess(), false); + EXPECT_FALSE(res.IsSuccess()); + testing::Mock::VerifyAndClearExpectations(uvMock.get()); - GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite2_003"; + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite_004"; } } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp index 1741f168f..52c68341c 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp @@ -51,98 +51,80 @@ void WriteCoreTest::TearDown(void) } /** - * @tc.name: WriteCoreTest_DoWrite1_001 - * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite_001 + * @tc.desc: Test function of WriteCore::DoWrite(StringBuffer) interface for FAILURE when fd < 0. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_001, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_001"; + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite_001"; int32_t fd = -1; string buffer; auto res = WriteCore::DoWrite(fd, buffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_001"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite_001"; } /** - * @tc.name: WriteCoreTest_DoWrite2_001 - * @tc.desc: Test function of WriteCore::DoWrite2 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite_002 + * @tc.desc: Test function of WriteCore::DoWrite(ArrayBuffer) interface for FAILURE when fd < 0. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite2_001, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite2_001"; + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite_002"; int32_t fd = -1; - string buffer; - - auto res = WriteCore::DoWrite(fd, buffer); - EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite2_001"; -} - -#if defined(_WIN64) || defined(__X86_64__) || defined(__ppc64__) || defined(__LP64__) -/** - * @tc.name: WriteCoreTest_DoWrite1_002 - * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_002, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_002"; - int32_t fd = -1; - ArrayBuffer buffer(nullptr, 0); + ArrayBuffer buffer(nullptr, 1); auto res = WriteCore::DoWrite(fd, buffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_002"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite_002"; } -#else -#endif /** - * @tc.name: WriteCoreTest_DoWrite1_003 - * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite_003 + * @tc.desc: Test function of WriteCore::DoWrite(StringBuffer) interface for FAILURE when offset < 0. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_003, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_003"; + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite_003"; int32_t fd = 1; - ArrayBuffer buffer(nullptr, 1); + string buffer; std::optional options = std::make_optional(WriteOptions()); options->offset = std::make_optional(-1); auto res = WriteCore::DoWrite(fd, buffer, options); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_003"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite_003"; } /** - * @tc.name: WriteCoreTest_DoWrite1_004 - * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite_004 + * @tc.desc: Test function of WriteCore::DoWrite(ArrayBuffer) interface for FAILURE when GetActualLen fails. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_004, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_004"; + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite_004"; int32_t fd = 1; ArrayBuffer buffer(nullptr, 1); - auto res = WriteCore::DoWrite(fd, buffer); + std::optional options = std::make_optional(WriteOptions()); + options->length = std::make_optional(-1); + + auto res = WriteCore::DoWrite(fd, buffer, options); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_004"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite_004"; } } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file 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 b52b1ad99..f9624bc87 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 @@ -13,12 +13,14 @@ * limitations under the License. */ +#include "xattr_core.h" + #include #include +#include #include #include "system_mock.h" -#include "xattr_core.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -36,6 +38,7 @@ public: void XattrCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + prctl(PR_SET_NAME, "XattrCoreMockTest"); SystemMock::EnableMock(); } -- Gitee