diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp index 203ead742fda8d818fec8031e45a0118a1436bd9..6ae96ff21a7ef2e09a8dd11e0f4e26b654e4d8c9 100644 --- a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp @@ -25,14 +25,7 @@ #include "filemgmt_libhilog.h" #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -#include - -#include "bundle_mgr_proxy.h" -#include "if_system_ability_manager.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" #include "rust_file.h" -#include "system_ability_definition.h" #endif #ifdef FILE_API_TRACE @@ -43,30 +36,6 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; -using namespace AppExecFwk; - -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -const string CLOUDDISK_FILE_PREFIX = "/data/storage/el2/cloud"; -const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; -const string PACKAGE_NAME_FLAG = ""; -const string USER_ID_FLAG = ""; -const string PHYSICAL_PATH_PREFIX = "/mnt/hmdfs//account/device_view/local/data/"; -const string CLOUD_FILE_LOCATION = "user.cloud.location"; -const char POSITION_LOCAL = '1'; -const char POSITION_BOTH = '3'; -const int BASE_USER_RANGE = 200000; -#endif - -enum AccessFlag : int32_t { - DEFAULT_FLAG = -1, - LOCAL_FLAG, -}; - -struct AccessArgs { - string path; - int mode = -1; - int flag = DEFAULT_FLAG; -}; static int UvAccess(const string &path, int mode) { @@ -78,93 +47,6 @@ static int UvAccess(const string &path, int mode) return uv_fs_access(nullptr, access_req.get(), path.c_str(), mode, nullptr); } -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -static bool IsCloudOrDistributedFilePath(const string &path) -{ - return path.find(CLOUDDISK_FILE_PREFIX) == 0 || path.find(DISTRIBUTED_FILE_PREFIX) == 0; -} - -static int GetCurrentUserId() -{ - int uid = IPCSkeleton::GetCallingUid(); - int userId = uid / BASE_USER_RANGE; - return userId; -} - -static sptr GetBundleMgrProxy() -{ - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - HILOGE("fail to get system ability mgr"); - return nullptr; - } - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - HILOGE("fail to get bundle manager proxy"); - return nullptr; - } - - return iface_cast(remoteObject); -} - -static string GetSelfBundleName() -{ - sptr bundleMgrProxy = GetBundleMgrProxy(); - if (!bundleMgrProxy) { - HILOGE("bundleMgrProxy is nullptr"); - return ""; - } - BundleInfo bundleInfo; - auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo); - if (ret != 0) { - HILOGE("bundleName get fail"); - return ""; - } - return bundleInfo.name; -} - -static int HandleLocalCheck(const string &path, int mode) -{ - // check if the file of /data/storage/el2/cloud is on the local - if (path.find(CLOUDDISK_FILE_PREFIX) == 0) { - char val[2] = { '\0' }; - if (getxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), val, sizeof(val)) < 0) { - HILOGI("get cloud file location fail, err: %{public}d", errno); - return errno; - } - if (val[0] == POSITION_LOCAL || val[0] == POSITION_BOTH) { - return 0; - } - return ENOENT; - } - // check if the distributed file of /data/storage/el2/distributedfiles is on the local, - // convert into physical path(/mnt/hmdfs//account/device_view/local/data/) and check - if (path.find(DISTRIBUTED_FILE_PREFIX) == 0) { - int userId = GetCurrentUserId(); - string bundleName = GetSelfBundleName(); - string relativePath = path.substr(DISTRIBUTED_FILE_PREFIX.length()); - string physicalPath = PHYSICAL_PATH_PREFIX + relativePath; - physicalPath.replace(physicalPath.find(USER_ID_FLAG), USER_ID_FLAG.length(), to_string(userId)); - physicalPath.replace(physicalPath.find(PACKAGE_NAME_FLAG), PACKAGE_NAME_FLAG.length(), bundleName); - - return UvAccess(physicalPath, mode); - } - - return ENOENT; -} -#endif - -static int AccessCore(const string &path, int mode, int flag = DEFAULT_FLAG) -{ -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - if (flag == LOCAL_FLAG && IsCloudOrDistributedFilePath(path)) { - return HandleLocalCheck(path, mode); - } -#endif - return UvAccess(path, mode); -} - static int MkdirCore(const string &path) { std::unique_ptr mkdir_req = { new uv_fs_t, FsUtils::FsReqCleanup }; @@ -179,7 +61,7 @@ static int32_t MkdirExec(const string &path, bool recursion, bool hasOption) { #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if (hasOption) { - int ret = AccessCore(path, 0); + int ret = UvAccess(path, 0); if (ret == ERRNO_NOERR) { HILOGD("The path already exists"); return EEXIST; @@ -192,7 +74,7 @@ static int32_t MkdirExec(const string &path, bool recursion, bool hasOption) HILOGD("Failed to create directories, error: %{public}d", errno); return errno; } - ret = AccessCore(path, 0); + ret = UvAccess(path, 0); if (ret) { HILOGE("Failed to verify the result of Mkdirs function"); return ret; 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 new file mode 100644 index 0000000000000000000000000000000000000000..a22c38ab406dcfc14af4fa789ba51c9d345eb621 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp @@ -0,0 +1,184 @@ +/* + * 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 "mkdir_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MkdirCoreMockTest : 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 MkdirCoreMockTest::tempFilePath; + +void MkdirCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "test"; + std::filesystem::create_directory(tempFilePath); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void MkdirCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void MkdirCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MkdirCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0001 + * @tc.desc: Test function of DoMkdir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0001"; + + EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(0)); + + string path = tempFilePath.string() + "/test01"; + auto ret = MkdirCore::DoMkdir(path); + EXPECT_EQ(ret.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0001"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0002 + * @tc.desc: Test function of DoMkdir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0002"; + + 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); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0002"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0003 + * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0003"; + + EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(1)); + + string path = tempFilePath.string() + "/test03"; + auto ret = MkdirCore::DoMkdir(path); + EXPECT_EQ(ret.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0003"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0004 + * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0004"; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + + string path = "/"; + auto ret = MkdirCore::DoMkdir(path, true); + EXPECT_EQ(ret.IsSuccess(), false); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900015); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "File exists"); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0004"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0005 + * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0005"; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(2)); + + string path = ""; + auto ret = MkdirCore::DoMkdir(path, true); + EXPECT_EQ(ret.IsSuccess(), false); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900002); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "No such file or directory"); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0005"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..e49357a368ab2fc2f360642ea8b68174e62fad8a --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp @@ -0,0 +1,117 @@ +/* + * 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 "mkdtemp_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MkdtempCoreMockTest : 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 MkdtempCoreMockTest::tempFilePath; + +void MkdtempCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "test"; + std::filesystem::create_directory(tempFilePath); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void MkdtempCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void MkdtempCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MkdtempCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MkdtempCoreMockTest_DoMkdtemp_0001 + * @tc.desc: Test function of DoMkdtemp() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-begin MkdtempCoreMockTest_DoMkdtemp_0001"; + + uv_fs_t mock_req; + mock_req.path = const_cast("/data/local/tmp/test/XXXXXX"); + + EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)) + .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); + + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0001"; +} + +/** + * @tc.name: MkdtempCoreMockTest_DoMkdtemp_0002 + * @tc.desc: Test function of DoMkdtemp() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-begin MkdtempCoreMockTest_DoMkdtemp_0002"; + + string path = tempFilePath.string() + "/XXXXXX"; + + EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)).WillOnce(Return(-1)); + auto ret = MkdtempCore::DoMkdtemp(path); + EXPECT_EQ(ret.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/movedir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/movedir_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17c95f4bf6d33b377406edd11c83c361da81ed5f --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/movedir_core_test.cpp @@ -0,0 +1,343 @@ +/* + * 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 "movedir_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MoveDirCoreTest : public testing::Test { +public: + static filesystem::path srcPath; + static filesystem::path destPath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path MoveDirCoreTest::srcPath; +filesystem::path MoveDirCoreTest::destPath; + +void MoveDirCoreTest::SetUpTestCase(void) +{ + srcPath = filesystem::temp_directory_path() / "src/"; + destPath = filesystem::temp_directory_path() / "dest/"; + std::filesystem::create_directory(srcPath); + std::filesystem::create_directory(destPath); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void MoveDirCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(srcPath); + filesystem::remove_all(destPath); +} + +void MoveDirCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MoveDirCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0001 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0001"; + + string src = srcPath.string() + "/test01"; + string dest = destPath.string(); + filesystem::create_directories(src); + + auto result = MoveDirCore::DoMoveDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0001"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0002 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0002"; + + string src = srcPath.string() + "/test02"; + string dest = destPath.string(); + filesystem::create_directories(src); + + int invalidMode = DIRMODE_MAX + 1; + auto result = MoveDirCore::DoMoveDir(src, dest, optional(invalidMode)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0002"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0003 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0002"; + + string src = srcPath.string() + "/test03"; + string dest = destPath.string(); + + auto result = MoveDirCore::DoMoveDir(src, dest, optional(DIRMODE_DIRECTORY_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0003"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0004 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0002"; + + string src = srcPath.string(); + string dest = destPath.string() + "/test04"; + + auto result = MoveDirCore::DoMoveDir(src, dest, optional(DIRMODE_DIRECTORY_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0004"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0005 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0005"; + + string src = "/data/local/test05/src/src/src/test05"; + string dest = destPath.string() + "/src"; + filesystem::create_directories(src); + filesystem::create_directories(dest); + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test05/", destPath.string(), optional(DIRMODE_DIRECTORY_THROW_ERR)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900032); + EXPECT_FALSE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test05"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0005"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0006 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0006"; + + string src = "/data/local/test06/src/src/src/test06"; + string dest = destPath.string() + "/src"; + filesystem::create_directories(src); + filesystem::create_directories(dest); + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test06/src", destPath.string(), optional(DIRMODE_DIRECTORY_REPLACE)); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + + filesystem::remove_all("/data/local/test06"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0006"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0007 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0007"; + + filesystem::create_directories("/data/local/test07/src"); + filesystem::create_directories("/data/local/test07/dest"); + filesystem::path srcFile = "/data/local/test07/src/test_file.txt"; + ofstream(srcFile) << "Test content\n123\n456"; + filesystem::path destFile = "/data/local/test07/dest/test_file.txt"; + ofstream(destFile) << "Test content\ndest"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test07/src/", "/data/local/test07/dest/", optional(DIRMODE_FILE_THROW_ERR)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900015); + EXPECT_TRUE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test07"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0007"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0008 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0008"; + + filesystem::create_directories("/data/local/test08/src"); + filesystem::create_directories("/data/local/test08/dest/test_file/test_file"); + filesystem::path srcFile = "/data/local/test08/src/test_file"; + ofstream(srcFile) << "Test content\n123\n456"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test08/src/", "/data/local/test08/dest/test_file/", optional(DIRMODE_FILE_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_TRUE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test08"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0008"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0009 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0009"; + + filesystem::create_directories("/data/local/test09/src"); + filesystem::create_directories("/data/local/test09/dest"); + filesystem::path srcFile = "/data/local/test09/src/test_file.txt"; + ofstream(srcFile) << "Test content\n123\n456"; + filesystem::path destFile = "/data/local/test09/dest/test_file.txt"; + ofstream(destFile) << "Test content\ndest"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test09/src/", "/data/local/test09/dest/", optional(DIRMODE_FILE_REPLACE)); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test09"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0009"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0010 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0010"; + + filesystem::create_directories("/data/local/test09/src/test_file.txt"); + filesystem::create_directories("/data/local/test09/dest"); + filesystem::path destFile = "/data/local/test09/dest/test_file.txt"; + ofstream(destFile) << "Test content\ndest"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test09/src/", "/data/local/test09/dest/", optional(DIRMODE_FILE_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_TRUE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test09"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0010"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file