From 2fcab273d749c2d81b5b3e9fd0f55b56a24e3a36 Mon Sep 17 00:00:00 2001 From: WangKe Date: Wed, 28 May 2025 15:39:06 +0800 Subject: [PATCH] =?UTF-8?q?randomaccessfile=E6=8E=A5=E5=8F=A3TDD=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I03d4a78bcbcabad3f275c1fbe032842e9bbbcdc6 Signed-off-by: WangKe --- .../create_randomaccessfile_core.cpp | 4 - interfaces/test/unittest/BUILD.gn | 1 + interfaces/test/unittest/js/BUILD.gn | 54 ++- .../fs_randomaccessfile_mock_test.cpp | 250 +++++++++++ .../fs_randomaccessfile_test.cpp | 417 ++++++++++++++++++ ...create_randomaccessfile_core_mock_test.cpp | 108 +++++ .../create_randomaccessfile_core_test.cpp | 189 ++++++++ .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 38 +- .../js/mod_fs/properties/mock/uv_fs_mock.h | 36 +- 9 files changed, 1073 insertions(+), 24 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp index 1ef94cd44..d7d99779d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp @@ -30,10 +30,6 @@ using namespace std; static tuple ParseStringToFileInfo(const string &path) { - if (strlen(path.c_str()) < 0) { - HILOGE("The first argument requires filepath/file"); - return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; - } OHOS::DistributedFS::FDGuard sfd; auto fdg = CreateUniquePtr(sfd, false); if (fdg == nullptr) { diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb26..924b53b39 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -16,6 +16,7 @@ group("file_api_unittest") { deps = [ "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", + "js:ani_file_fs_mock_test", "js:ani_file_fs_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index a22cb0a2b..d3fc839a0 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -20,8 +20,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -34,12 +32,14 @@ ohos_unittest("ani_file_fs_test") { ] sources = [ + "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", + "mod_fs/class_stream/fs_stream_test.cpp", "mod_fs/properties/close_core_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", - "mod_fs/class_stream/fs_stream_test.cpp", ] deps = [ @@ -61,6 +61,54 @@ ohos_unittest("ani_file_fs_test") { "libuv:uv", ] + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + defines = [ "private=public", ] 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 new file mode 100644 index 000000000..021be07b9 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "file_entity.h" +#include "fs_randomaccessfile.h" +#include "uv_fs_mock.h" + + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsRandomAccessFileMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +protected: + std::unique_ptr rafEntity; + std::unique_ptr raf; +}; + +void FsRandomAccessFileMockTest::SetUpTestCase(void) +{ + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsRandomAccessFileMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsRandomAccessFileMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + rafEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = std::make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = std::make_unique(std::move(rafEntity)); +} + +void FsRandomAccessFileMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_001 + * @tc.desc: Test function of ReadSync() interface for is failed for options is std::nullopt. + * @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); + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->ReadSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_001"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_002 + * @tc.desc: Test function of ReadSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_002"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.offset = 10; + options.length = 10; + raf->rafEntity->filePointer = 20; + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), true); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_002"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_003 + * @tc.desc: Test function of WriteSync() interface for is failed for options is std::nullopt. + * @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"; + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(data, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_003"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_004 + * @tc.desc: Test function of WriteSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_004"; + + std::string data = "test data"; + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_004"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_005 + * @tc.desc: Test function of WriteSync() interface for is failed for uv_fs_write() return -1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_005"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_005"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_006 + * @tc.desc: Test function of WriteSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_006"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), true); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_006"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_CloseSync_007 + * @tc.desc: Test function of CloseSync() interface for is failed for uv_fs_write() return -1. + * @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"; + + EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(-1)); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_007"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_CloseSync_008 + * @tc.desc: Test function of CloseSync() interface for SUCCESS. + * @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"; + + EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(0)); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_008"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp new file mode 100644 index 000000000..6f44d1d39 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp @@ -0,0 +1,417 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "file_entity.h" +#include "fs_randomaccessfile.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsRandomAccessFileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +protected: + std::unique_ptr rafEntity; + std::unique_ptr raf; +}; + +void FsRandomAccessFileTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsRandomAccessFileTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsRandomAccessFileTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + rafEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = std::make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = std::make_unique(std::move(rafEntity)); +} + +void FsRandomAccessFileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +// 测试Constructor +/** + * @tc.name: FsRandomAccessFileTest_Constructor_001 + * @tc.desc: Test function of Constructor() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_Constructor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_Constructor_001"; + + auto result = FsRandomAccessFile::Constructor(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_Constructor_001"; +} + +// 测试GetFD +/** + * @tc.name: FsRandomAccessFileTest_GetFD_002 + * @tc.desc: Test function of GetFD() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFD_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFD_002"; + + auto result = raf->GetFD(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFD_002"; +} + +/** + * @tc.name: FsRandomAccessFileTest_GetFD_003 + * @tc.desc: Test function of GetFD() interface for is failed for FsRandomAccessFile is nullopt. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFD_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFD_003"; + + raf = std::make_unique(nullptr); + auto result = raf->GetFD(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFD_003"; +} + +// GetFPointer +/** + * @tc.name: FsRandomAccessFileTest_GetFPointer_004 + * @tc.desc: Test function of GetFPointer() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFPointer_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFPointer_004"; + + raf->rafEntity->filePointer = 100; + auto result = raf->GetFPointer(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFPointer_004"; +} + +/** + * @tc.name: FsRandomAccessFileTest_GetFPointer_005 + * @tc.desc: Test function of GetFPointer() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFPointer_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFPointer_005"; + + raf = std::make_unique(nullptr); + auto result = raf->GetFPointer(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFPointer_005"; +} + +// SetFilePointerSync +/** + * @tc.name: FsRandomAccessFileTest_SetFilePointerSync_006 + * @tc.desc: Test function of SetFilePointerSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_SetFilePointerSync_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_SetFilePointerSync_006"; + + auto result = raf->SetFilePointerSync(50); + EXPECT_EQ(result.IsSuccess(), true); + EXPECT_EQ(raf->rafEntity->filePointer, 50); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_SetFilePointerSync_006"; +} + +/** + * @tc.name: FsRandomAccessFileTest_SetFilePointerSync_007 + * @tc.desc: Test function of SetFilePointerSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_SetFilePointerSync_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_SetFilePointerSync_007"; + + raf = std::make_unique(nullptr); + auto result = raf->SetFilePointerSync(50); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_SetFilePointerSync_007"; +} + +// ReadSync +/** + * @tc.name: FsRandomAccessFileTest_ReadSync_008 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_ReadSync_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_ReadSync_008"; + + raf = std::make_unique(nullptr); + ArrayBuffer buffer(malloc(100), 100); + + auto result = raf->ReadSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_ReadSync_008"; +} + +/** + * @tc.name: FsRandomAccessFileTest_ReadSync_009 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_ReadSync_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_ReadSync_009"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.offset = -5; + + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_ReadSync_009"; +} + +/** + * @tc.name: FsRandomAccessFileTest_ReadSync_010 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_ReadSync_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_ReadSync_010"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.length = -1; + + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_ReadSync_010"; +} + +// WriteSync +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_011 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_011"; + + raf = std::make_unique(nullptr); + std::string data = "test data"; + auto result = raf->WriteSync(data, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_011"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_012 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_012"; + + raf = std::make_unique(nullptr); + ArrayBuffer buffer(malloc(100), 100); + auto result = raf->WriteSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_012"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_013 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_013"; + + std::string data = "test data"; + WriteOptions options; + options.offset = -5; + + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_013"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_014 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_014"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.offset = -5; + + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_014"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_015 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_015"; + + std::string data = "test data"; + WriteOptions options; + options.length = -5; + + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_015"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_016 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_016"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = -5; + + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_016"; +} + +// CloseSync +/** + * @tc.name: FsRandomAccessFileTest_CloseSync_017 + * @tc.desc: Test function of CloseSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_CloseSync_017, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_CloseSync_017"; + + raf = std::make_unique(nullptr); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_CloseSync_017"; +} + +} \ No newline at end of file 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 new file mode 100644 index 000000000..2dc12961c --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "create_randomaccessfile_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CreateRandomAccessFileCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + 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"; +} + +void CreateRandomAccessFileCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CreateRandomAccessFileCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CreateRandomAccessFileCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; + + string path = "/test/path.txt"; + int32_t mode = 0; + optional options = nullopt; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = 0; + opts.end = 100; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp new file mode 100644 index 000000000..848d97672 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "create_randomaccessfile_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CreateRandomAccessFileCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void CreateRandomAccessFileCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CreateRandomAccessFileCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CreateRandomAccessFileCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CreateRandomAccessFileCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify an invalid mode. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001"; + + string path = "/test/path.txt"; + int32_t mode = -5; + optional options = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = -1; + opts.end = 100; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = 10; + opts.end = -1; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003"; +} + +/**' + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; + + int fd = -1; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), false); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; +} + +/**' + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify the path is invalid. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005"; + + string path = ""; + int32_t mode = 0; + optional options = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; + + int fd = 3; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file 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 index 13ceb7b54..aa3534347 100644 --- 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 @@ -17,8 +17,8 @@ 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) +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); } @@ -68,12 +68,22 @@ int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, u 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) +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); @@ -99,4 +109,22 @@ 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) {} +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); +} 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 index 6b0f3c5fd..736eebb6e 100644 --- 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 @@ -16,7 +16,7 @@ #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 "read_core.h" +#include "uv.h" #include @@ -32,46 +32,58 @@ public: 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_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 *newPath, int flags, uv_fs_cb cb) = 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 *newPath, 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; }; 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)); + 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_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 *newPath, int flags, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, 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)); + 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)); }; } // namespace OHOS::FileManagement::ModuleFileIO -- Gitee