diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp index a5cd88374fc3bf4e0af4e4eba32427439116a59f..2a9b29bcf03347e7759354a5829d21f4711d6c0b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp @@ -33,6 +33,7 @@ void MkdirkAni::MkdirSync0(ani_env *env, [[maybe_unused]] ani_class clazz, ani_s ErrorHandler::Throw(env, EINVAL); return; } + auto ret = MkdirCore::DoMkdir(pathStr); if (!ret.IsSuccess()) { HILOGE("Mkdir failed"); @@ -50,6 +51,7 @@ void MkdirkAni::MkdirSync1(ani_env *env, [[maybe_unused]] ani_class clazz, ani_s ErrorHandler::Throw(env, EINVAL); return; } + auto ret = MkdirCore::DoMkdir(pathStr, recursion); if (!ret.IsSuccess()) { HILOGE("DoMkdir failed"); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h index 8ed780e9619e99f3390c2db0493d9063a9b37e33..195d0db0014c47ddacfe206b4838b47e8388b135 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_MKDIR_ANI_H -#define INTERFACES_KITS_JS_SRC_MOD_FS_MKDIR_ANI_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_MKDIR_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_MKDIR_ANI_H #include @@ -34,4 +34,4 @@ public: } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_MKDIR_ANI_H +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_MKDIR_ANI_H diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp index 33bfb77cfeb053b95b2a4b6499ec36202ff7e9a2..77633e1b5267a2026da780686522c2f879505867 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp @@ -129,7 +129,7 @@ void MoveDirAni::MoveDirSync( auto [succMode, optMode] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { - HILOGE("Failed to convert mode to int32"); + HILOGE("Invalid mode"); ErrorHandler::Throw(env, EINVAL); return; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.cpp index 49b26f784cd53a4650918a391e78cf725a63d9cd..10c3ce394cbca152b0576a82b45d521f9f23fc9f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.cpp @@ -53,7 +53,7 @@ ani_object ReadLinesAni::ReadLinesSync( { auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { - HILOGE("Invalid path"); + HILOGE("Invalid path from ETS first argument"); ErrorHandler::Throw(env, EINVAL); return nullptr; } @@ -64,6 +64,7 @@ ani_object ReadLinesAni::ReadLinesSync( ErrorHandler::Throw(env, EINVAL); return nullptr; } + FsResult ret = ReadLinesCore::DoReadLines(filePath, opt); if (!ret.IsSuccess()) { HILOGE("Readlines failed"); @@ -71,6 +72,7 @@ ani_object ReadLinesAni::ReadLinesSync( ErrorHandler::Throw(env, err); return nullptr; } + const FsReaderIterator *readerIterator = ret.GetData().value(); auto result = ReaderIteratorAni::Wrap(env, move(readerIterator)); if (result == nullptr) { @@ -79,6 +81,7 @@ ani_object ReadLinesAni::ReadLinesSync( } return result; } + } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.h index 387298f813beb8631a9e49a2a30a196267bd12b6..3f1bfeb7252e7c71937aa5ee63ff3631f4ec8926 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_lines_ani.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_READ_LINES_ANI_H -#define INTERFACES_KITS_JS_SRC_MOD_FS_READ_LINES_ANI_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_READ_LINES_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_READ_LINES_ANI_H #include @@ -33,4 +33,4 @@ public: } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_READ_LINES_ANI_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_READ_LINES_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp index 91a929d134e2b5443fe840fc16441cedf4e45f5e..763d6e72ed69c93edc91c7831a914408ab7e77d2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp @@ -29,7 +29,7 @@ void UnlinkAni::UnlinkSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_s { auto [succ, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succ) { - HILOGE("Invalid path"); + HILOGE("Invalid path from ETS first argument"); ErrorHandler::Throw(env, EINVAL); return; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h index 817b258000b9f13b5dbbc8f498b00cc74e7dc1e0..64aee8bd1952c4da7d0ef61922f219da98ddb62f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_UNLINK_ANI_H -#define INTERFACES_KITS_JS_SRC_MOD_FS_UNLINK_ANI_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_UNLINK_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_UNLINK_ANI_H #include @@ -33,4 +33,4 @@ public: } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_UNLINK_ANI_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_UNLINK_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/xattr_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/xattr_ani.cpp index 47e6d92a513b2e3ed172026caa532a295519dabe..2a69e0bd2eec613648097dd54ae5ff88e71b276c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/xattr_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/xattr_ani.cpp @@ -37,14 +37,14 @@ void XattrAni::SetXattrSync( auto [keySucc, keyStr] = TypeConverter::ToUTF8String(env, key); if (!keySucc) { - HILOGE("Invalid key"); + HILOGE("Invalid xattr key"); ErrorHandler::Throw(env, EINVAL); return; } auto [valueSucc, valueStr] = TypeConverter::ToUTF8String(env, value); if (!valueSucc) { - HILOGE("Invalid value"); + HILOGE("Invalid xattr value"); ErrorHandler::Throw(env, EINVAL); return; } @@ -69,7 +69,7 @@ ani_string XattrAni::GetXattrSync(ani_env *env, [[maybe_unused]] ani_class clazz auto [keySucc, keyStr] = TypeConverter::ToUTF8String(env, key); if (!keySucc) { - HILOGE("Invalid key"); + HILOGE("Invalid xattr key"); ErrorHandler::Throw(env, EINVAL); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp index 57b479f2bf329f2cd5a97e4852e32d906bc69bcc..38996fda03d167806fb900266596364df9b134f0 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp @@ -24,7 +24,6 @@ #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { -using namespace std; FsResult FDataSyncCore::DoFDataSync(const int32_t &fd) { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h index 8efb6f90722e0f6c342d58c293a84995ac1efda5..5e26c9140dc98c0d582a2ac75d743ae1618546d1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h @@ -24,6 +24,6 @@ class FDataSyncCore final { public: static FsResult DoFDataSync(const int32_t &fd); }; -const std::string PROCEDURE_FDATASYNC_NAME = "FileIOFdatasync"; + } // namespace OHOS::FileManagement::ModuleFileIO #endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h index cc6793693dca3c355419aae57de9ccaaf7749d9a..46bc81a7005e32adade346fba90378a25bc9306d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h @@ -28,7 +28,7 @@ public: static FsResult DoMkdir(const std::string& path, std::optional recursion = std::nullopt); }; constexpr int DIR_DEFAULT_PERM = 0770; -const std::string PROCEDURE_READTEXT_NAME = "FileIOMkdir"; + } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h index e638655ba71a3985d200fdb6e360e872d49a5fc0..df936597f265e739e766715b4792ab4c565a8688 100644 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp_core.h @@ -27,7 +27,7 @@ class MkdtempCore final { public: static FsResult DoMkdtemp(const std::string &path); }; -const std::string PROCEDURE_MKDTEMP_NAME = "FileIOMkdtemp"; + } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir_core.h b/interfaces/kits/js/src/mod_fs/properties/movedir_core.h index e4db3553d908beb9d1966a6852d91afd26bd2fe1..f10eaa5321bce542448c6d5bce604c63e6060f7a 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/movedir_core.h @@ -58,7 +58,6 @@ public: const std::string &src, const std::string &dest, std::optional mode = std::nullopt); }; -const std::string PROCEDURE_MOVEDIR_NAME = "FileIOMoveDir"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp index 819215c30e9be4c365a6315fc8e078ade722960e..1f986915199031de14d6eedf10ebb6115003e9c6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp @@ -25,9 +25,9 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; -static int CheckOptionArg(optional option) +static int CheckOptionArg(Options option) { - auto encoding = option->encoding; + auto encoding = option.encoding; if (encoding != "utf-8") { return EINVAL; } @@ -79,7 +79,7 @@ static FsResult InstantiateReaderIterator(void *iterator, in FsResult ReadLinesCore::DoReadLines(const string &path, optional option) { if (option.has_value()) { - int ret = CheckOptionArg(option); + int ret = CheckOptionArg(option.value()); if (ret) { HILOGE("Invalid option.encoding parameter"); return FsResult::Error(ret); @@ -98,11 +98,7 @@ FsResult ReadLinesCore::DoReadLines(const string &path, opti HILOGE("Failed to get size of the file"); return FsResult::Error(ret); } - auto readeriterator = InstantiateReaderIterator(iterator, offset); - if (!readeriterator.IsSuccess()) { - return FsResult::Error(ENOMEM); - } - return FsResult::Success(readeriterator.GetData().value()); + return InstantiateReaderIterator(iterator, offset); } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.h b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.h index a450cc43188f2765786fde02ed8a2c8d07166ae9..67cae2c52183c08fd066431aee5daee23db5b776 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.h @@ -37,7 +37,6 @@ public: const std::string &path, std::optional option = std::nullopt); }; -const std::string PROCEDURE_READLINES_NAME = "FileIOReadLines"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/unlink_core.cpp b/interfaces/kits/js/src/mod_fs/properties/unlink_core.cpp index ecb92452e2eeb401b856238f52175487ae824523..339b56db0664a30f426edd4bedc2e173952569ab 100644 --- a/interfaces/kits/js/src/mod_fs/properties/unlink_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/unlink_core.cpp @@ -28,13 +28,13 @@ using namespace std; FsResult UnlinkCore::DoUnlink(const std::string &src) { - std::unique_ptr unlink_req = { + std::unique_ptr unlinkReq = { new uv_fs_t, FsUtils::FsReqCleanup }; - if (!unlink_req) { + if (!unlinkReq) { HILOGE("Failed to request heap memory."); return FsResult::Error(ENOMEM); } - int ret = uv_fs_unlink(nullptr, unlink_req.get(), src.c_str(), nullptr); + int ret = uv_fs_unlink(nullptr, unlinkReq.get(), src.c_str(), nullptr); if (ret < 0) { HILOGD("Failed to unlink with path"); return FsResult::Error(ret); diff --git a/interfaces/kits/js/src/mod_fs/properties/unlink_core.h b/interfaces/kits/js/src/mod_fs/properties/unlink_core.h index b2ea553f4b62130bc691bda5daca16dbe7bc28d4..5989bbda29217d938c1f539bedc29197198be31c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/unlink_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/unlink_core.h @@ -27,7 +27,7 @@ class UnlinkCore final { public: static FsResult DoUnlink(const std::string &src); }; -const std::string PROCEDURE_READTEXT_NAME = "FileIOUnlink"; + } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index e4e6a9d52bb0398a1fa2cc095b3c3d8f1c9e0f3d..29ca03b71aad54fc3a00e080ef81b8d4e6a34b35 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -22,7 +22,7 @@ ohos_unittest("ani_file_fs_test") { include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", - "mock/uv_fs_mock.h", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", ] sources = [ 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 new file mode 100644 index 0000000000000000000000000000000000000000..c6308de5056abc61308bd14e74bd42bec19f3003 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "fdatasync_core.h" +#include "mock/uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FDataSyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + 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; +} + +void FDataSyncCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void FDataSyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FDataSyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_001 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_001"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_001"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_002 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_002"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(-1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_002"; +} + +} // 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 c4f112268ebfe3ef27f7127b1024a5196348b1ec..13ceb7b5405e3cca678d7b6a7e93f5a8b23c6cb1 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 @@ -13,74 +13,90 @@ * 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) +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) +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) +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) +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) +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) +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) +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) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) { - return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); + return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, 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) +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) +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) +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_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) {} 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 ad0522f9ee28ca3ecdb1e5d3e8e66fbb8892200a..6b0f3c5fdebacac71bd14803b4b7cc9691584e1a 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 @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef UV_FS_READ_MOCK_H -#define UV_FS_READ_MOCK_H +#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" @@ -25,59 +25,54 @@ 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, + 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 *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_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_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; }; 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_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 *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)); + 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)); }; - -} // OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +} // namespace 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/read_lines_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..118e5e28a9c116433cf08fb5ab3f2557a0dd8566 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "mock/uv_fs_mock.h" +#include "read_lines_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class ReadLinesCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path ReadLinesCoreMockTest::tempFilePath; + +void ReadLinesCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + 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; +} + +void ReadLinesCoreMockTest::TearDownTestCase(void) +{ + filesystem::remove(tempFilePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void ReadLinesCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ReadLinesCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadLinesCoreMockTest_DoReadLines_001 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_001"; + + string path = tempFilePath.string(); + Options option; + option.encoding = "utf-8"; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_001"; +} + +/** + * @tc.name: ReadLinesCoreMockTest_DoReadLines_002 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_002"; + + string path = tempFilePath.string(); + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + auto res = ReadLinesCore::DoReadLines(path); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_002"; +} + +/** + * @tc.name: ReadLinesCoreMockTest_DoReadLines_003 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_003"; + + string path = tempFilePath.string(); + Options option; + option.encoding = "utf-8"; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92e4f19b429030d609720ca723a95115d2e281d1 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_test.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "read_lines_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class ReadLinesCoreTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path ReadLinesCoreTest::tempFilePath; + +void ReadLinesCoreTest::SetUpTestCase(void) +{ + tempFilePath = filesystem::temp_directory_path() / "read_lines_test_file.txt"; + ofstream(tempFilePath) << "Test content\n123\n456"; + ofstream(tempFilePath).close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void ReadLinesCoreTest::TearDownTestCase(void) +{ + filesystem::remove(tempFilePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void ReadLinesCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ReadLinesCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadLinesCoreTest_DoReadLines_001 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreTest, ReadLinesCoreTest_DoReadLines_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLinesCoreTest-begin ReadLinesCoreTest_DoReadLines_001"; + + string path = "ReadLinesCoreTest_DoReadLines_001"; + Options option; + option.encoding = "utf-8"; + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "ReadLinesCoreTest-end ReadLinesCoreTest_DoReadLines_001"; +} + +/** + * @tc.name: ReadLinesCoreTest_DoReadLines_002 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreTest, ReadLinesCoreTest_DoReadLines_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLinesCoreTest-begin ReadLinesCoreTest_DoReadLines_002"; + + string path = "ReadLinesCoreTest_DoReadLines_002"; + Options option; + option.encoding = "utf-16"; + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "ReadLinesCoreTest-end ReadLinesCoreTest_DoReadLines_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file