From a3532fb395683d3056d1a18b71872f97fe6a0f23 Mon Sep 17 00:00:00 2001 From: yaoruozi Date: Sat, 13 Sep 2025 15:24:43 +0800 Subject: [PATCH] modify code Signed-off-by: yaoruozi --- .../include/copy/file_copy_manager.h | 7 +- .../include/copy/remote_file_copy_manager.h | 7 +- .../include/distributed_file_daemon_proxy.h | 12 + .../include/file_mount_manager.h | 51 +-- .../src/copy/file_copy_manager.cpp | 11 - .../src/copy/remote_file_copy_manager.cpp | 12 +- .../distributed_file_daemon_manager_impl.cpp | 6 +- .../src/distributed_file_daemon_proxy.cpp | 112 ++++++ .../src/file_mount_manager.cpp | 55 ++- services/distributedfiledaemon/BUILD.gn | 2 + .../include/connect_count/connect_count.h | 7 +- .../include/ipc/daemon.h | 22 +- .../include/ipc/daemon_event.h | 7 +- .../include/ipc/daemon_execute.h | 3 +- .../include/ipc/daemon_stub.h | 2 + ...stributed_file_daemon_ipc_interface_code.h | 4 +- .../include/ipc/i_daemon.h | 10 + .../src/connect_count/connect_count.cpp | 10 +- .../src/device/device_manager_agent.cpp | 8 +- .../src/device/device_profile_adapter.cpp | 10 +- .../distributedfiledaemon/src/ipc/daemon.cpp | 150 ++++++-- .../src/ipc/daemon_execute.cpp | 14 +- .../src/ipc/daemon_stub.cpp | 105 +++++- .../src/multiuser/os_account_observer.cpp | 1 - .../src/network/network_agent_template.cpp | 24 +- .../src/network/session_pool.cpp | 1 + .../softbus/softbus_session_listener.cpp | 5 +- .../test/mock/include/daemon_mock.h | 38 +- .../test/unittest/BUILD.gn | 58 +-- .../test/unittest/connect_count/BUILD.gn | 1 + .../connect_count/connect_count_test.cpp | 56 ++- .../device_profile_adapter_test.cpp | 8 +- .../test/unittest/ipc/daemon/daemon_test.cpp | 210 ++++++++--- .../daemon_execute/daemon_execute_test.cpp | 15 +- .../network/network_agent_template_test.cpp | 350 ------------------ .../unittest/network/session_pool_test.cpp | 5 +- .../softbus/softbus_agent_sup_test.cpp | 4 +- .../softbus/softbus_session_listener_test.cpp | 1 - test/fuzztest/daemonstub_fuzzer/BUILD.gn | 1 + .../daemonstub_fuzzer/daemonstub_fuzzer.cpp | 17 + .../distributed_file_daemon/BUILD.gn | 2 + .../daemon_stub_sup_test.cpp | 196 ++++++++++ .../daemon_stub_test.cpp | 8 + .../include/i_daemon_mock.h | 13 + .../unittests/distributed_file_inner/BUILD.gn | 11 + .../copy/file_copy_manager_test.cpp | 152 ++++---- .../copy/remote_file_copy_manager_test.cpp | 133 +++---- .../distributed_file_daemon_proxy_test.cpp | 252 ++++++++++++- utils/log/include/dfs_error.h | 1 + 49 files changed, 1335 insertions(+), 855 deletions(-) delete mode 100644 services/distributedfiledaemon/test/unittest/network/network_agent_template_test.cpp diff --git a/frameworks/native/distributed_file_inner/include/copy/file_copy_manager.h b/frameworks/native/distributed_file_inner/include/copy/file_copy_manager.h index ca95276ec..ae24f2f1b 100644 --- a/frameworks/native/distributed_file_inner/include/copy/file_copy_manager.h +++ b/frameworks/native/distributed_file_inner/include/copy/file_copy_manager.h @@ -67,8 +67,12 @@ struct FileInfos { class FileCopyManager final { public: + static FileCopyManager &GetInstance() + { + static FileCopyManager instance; + return instance; + } using ProcessCallback = std::function; - static std::shared_ptr GetInstance(); int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback &processCallback); int32_t Cancel(const std::string &srcUri, const std::string &destUri, const bool isKeepFiles = false); int32_t Cancel(const bool isKeepFiles = false); @@ -76,7 +80,6 @@ public: int32_t ExecLocal(std::shared_ptr infos); private: - static std::shared_ptr instance_; std::mutex FileInfosVecMutex_; std::vector> FileInfosVec_; diff --git a/frameworks/native/distributed_file_inner/include/copy/remote_file_copy_manager.h b/frameworks/native/distributed_file_inner/include/copy/remote_file_copy_manager.h index 295b0a5a7..a66e2d9ea 100644 --- a/frameworks/native/distributed_file_inner/include/copy/remote_file_copy_manager.h +++ b/frameworks/native/distributed_file_inner/include/copy/remote_file_copy_manager.h @@ -24,7 +24,11 @@ namespace DistributedFile { class RemoteFileCopyManager { public: - static std::shared_ptr GetInstance(); + static RemoteFileCopyManager &GetInstance() + { + static RemoteFileCopyManager instance; + return instance; + } int32_t RemoteCopy(const std::string &srcUri, const std::string &destUri, const sptr &listener, const int32_t userId, const std::string ©Path); int32_t RemoteCancel(const std::string &srcUri, const std::string &destUri); @@ -38,7 +42,6 @@ private: void AddFileInfos(std::shared_ptr infos); bool IsFile(const std::string &path); private: - static std::shared_ptr instance_; std::mutex FileInfosVecMutex_; std::vector> FileInfosVec_; }; diff --git a/frameworks/native/distributed_file_inner/include/distributed_file_daemon_proxy.h b/frameworks/native/distributed_file_inner/include/distributed_file_daemon_proxy.h index 755e8d11d..8a212d98c 100644 --- a/frameworks/native/distributed_file_inner/include/distributed_file_daemon_proxy.h +++ b/frameworks/native/distributed_file_inner/include/distributed_file_daemon_proxy.h @@ -52,7 +52,19 @@ public: const std::string &dstPath, const std::string &dstDeviceId, const std::string &sessionName) override; + // for ACL verification after 6.0 version + int32_t RequestSendFileACL(const std::string &srcUri, + const std::string &dstPath, + const std::string &dstDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo) override; + int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override; + // for ACL verification after 6.0 version + int32_t GetRemoteCopyInfoACL(const std::string &srcUri, + bool &isFile, + bool &isDir, + const AccountInfo &callerAccountInfo) override; int32_t PushAsset(int32_t userId, const sptr &assetObj, diff --git a/frameworks/native/distributed_file_inner/include/file_mount_manager.h b/frameworks/native/distributed_file_inner/include/file_mount_manager.h index 89f563249..9cac363cf 100644 --- a/frameworks/native/distributed_file_inner/include/file_mount_manager.h +++ b/frameworks/native/distributed_file_inner/include/file_mount_manager.h @@ -1,40 +1,43 @@ /* -* Copyright (c) 2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef OHOS_STORAGE_FILE_MOUNT_MANAGER_H #define OHOS_STORAGE_FILE_MOUNT_MANAGER_H + +#include "remote_file_share.h" #include #include #include -#include "remote_file_share.h" namespace OHOS { namespace Storage { namespace DistributedFile { class FileMountManager { public: - static int32_t GetDfsUrisDirFromLocal(const std::vector &uriList, - const int32_t userId, - std::unordered_map &uriToDfsUriMaps); - static std::shared_ptr GetInstance(); -private: - static std::shared_ptr instance_; + static FileMountManager &GetInstance() + { + static FileMountManager instance; + return instance; + } + static int32_t GetDfsUrisDirFromLocal( + const std::vector &uriList, + const int32_t userId, + std::unordered_map &uriToDfsUriMaps); }; -} -} -} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS #endif // OHOS_STORAGE_FILE_MOUNT_MANAGER_H diff --git a/frameworks/native/distributed_file_inner/src/copy/file_copy_manager.cpp b/frameworks/native/distributed_file_inner/src/copy/file_copy_manager.cpp index 560f95880..857076c39 100644 --- a/frameworks/native/distributed_file_inner/src/copy/file_copy_manager.cpp +++ b/frameworks/native/distributed_file_inner/src/copy/file_copy_manager.cpp @@ -64,7 +64,6 @@ static const int OPEN_TRUC_VERSION = 20; #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) && !defined(CROSS_PLATFORM) const uint32_t API_VERSION_MOD = 1000; #endif -std::shared_ptr FileCopyManager::instance_ = nullptr; uint32_t g_apiCompatibleVersion = 0; #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) && !defined(CROSS_PLATFORM) @@ -165,16 +164,6 @@ uint32_t GetApiCompatibleVersion() } #endif -std::shared_ptr FileCopyManager::GetInstance() -{ - static std::once_flag once; - std::call_once(once, []() { - FileCopyManager::instance_ = std::make_shared(); - }); - - return instance_; -} - int32_t FileCopyManager::Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback &processCallback) { LOGE("FileCopyManager Copy start"); diff --git a/frameworks/native/distributed_file_inner/src/copy/remote_file_copy_manager.cpp b/frameworks/native/distributed_file_inner/src/copy/remote_file_copy_manager.cpp index dc0418755..27940578e 100644 --- a/frameworks/native/distributed_file_inner/src/copy/remote_file_copy_manager.cpp +++ b/frameworks/native/distributed_file_inner/src/copy/remote_file_copy_manager.cpp @@ -39,7 +39,6 @@ static const std::string MEDIA_AUTHORITY = "media"; static const std::string FILE_MANAGER_AUTHORITY = "docs"; static const std::string FILE_SCHEMA = "file://"; static const std::string FILE_SEPARATOR = "/"; -std::shared_ptr RemoteFileCopyManager::instance_ = nullptr; static std::string GetBundleName(const std::string &uri) { @@ -117,15 +116,6 @@ static std::string GetFileName(const std::string &path) return path.substr(pos + 1); } -std::shared_ptr RemoteFileCopyManager::GetInstance() -{ - static std::once_flag once; - std::call_once(once, []() { - RemoteFileCopyManager::instance_ = std::make_shared(); - }); - return instance_; -} - void RemoteFileCopyManager::AddFileInfos(std::shared_ptr infos) { std::lock_guard lock(FileInfosVecMutex_); @@ -231,7 +221,7 @@ int32_t RemoteFileCopyManager::RemoteCopy(const std::string &srcUri, const std:: }; infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath, infos->srcUriIsFile, processCallback); - auto result = FileCopyManager::GetInstance()->ExecLocal(infos); + auto result = FileCopyManager::GetInstance().ExecLocal(infos); if (ChangeOwnerRecursive(infos->destPath, infos->callingUid, infos->callingUid) != 0) { LOGE("ChangeOwnerRecursive failed, calling uid= %{public}d", infos->callingUid); } diff --git a/frameworks/native/distributed_file_inner/src/distributed_file_daemon_manager_impl.cpp b/frameworks/native/distributed_file_inner/src/distributed_file_daemon_manager_impl.cpp index 433f02a27..bbadaa0f8 100644 --- a/frameworks/native/distributed_file_inner/src/distributed_file_daemon_manager_impl.cpp +++ b/frameworks/native/distributed_file_inner/src/distributed_file_daemon_manager_impl.cpp @@ -255,17 +255,17 @@ int32_t DistributedFileDaemonManagerImpl::IsDirectory(const std::string &uri, bo int32_t DistributedFileDaemonManagerImpl::Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback processCallback) { - return FileCopyManager::GetInstance()->Copy(srcUri, destUri, processCallback); + return FileCopyManager::GetInstance().Copy(srcUri, destUri, processCallback); } int32_t DistributedFileDaemonManagerImpl::Cancel(const std::string &srcUri, const std::string &destUri) { - return FileCopyManager::GetInstance()->Cancel(srcUri, destUri); + return FileCopyManager::GetInstance().Cancel(srcUri, destUri); } int32_t DistributedFileDaemonManagerImpl::Cancel() { - return FileCopyManager::GetInstance()->Cancel(); + return FileCopyManager::GetInstance().Cancel(); } } // namespace DistributedFile } // namespace Storage diff --git a/frameworks/native/distributed_file_inner/src/distributed_file_daemon_proxy.cpp b/frameworks/native/distributed_file_inner/src/distributed_file_daemon_proxy.cpp index 8e97cb8f2..8fcc13b78 100644 --- a/frameworks/native/distributed_file_inner/src/distributed_file_daemon_proxy.cpp +++ b/frameworks/native/distributed_file_inner/src/distributed_file_daemon_proxy.cpp @@ -387,6 +387,63 @@ int32_t DistributedFileDaemonProxy::RequestSendFile(const std::string &srcUri, return reply.ReadInt32(); } +int32_t DistributedFileDaemonProxy::RequestSendFileACL(const std::string &srcUri, + const std::string &dstPath, + const std::string &remoteDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return OHOS::FileManagement::E_BROKEN_IPC; + } + if (!data.WriteString(srcUri)) { + LOGE("Failed to send srcUri"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(dstPath)) { + LOGE("Failed to send dstPath"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(remoteDeviceId)) { + LOGE("Failed to send remoteDeviceId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(sessionName)) { + LOGE("Failed to send sessionName"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteInt32(callerAccountInfo.userId_)) { + LOGE("Failed to send userId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(callerAccountInfo.accountId_)) { + LOGE("Failed to send accountId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(callerAccountInfo.networkId_)) { + LOGE("Failed to send networkId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + auto remote = Remote(); + if (remote == nullptr) { + LOGE("remote is nullptr"); + return OHOS::FileManagement::E_BROKEN_IPC; + } + int32_t ret = remote->SendRequest( + static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE_ACL), data, + reply, option); + if (ret != 0) { + LOGE("SendRequest failed, ret = %{public}d", ret); + return OHOS::FileManagement::E_BROKEN_IPC; + } + return reply.ReadInt32(); +} + + int32_t DistributedFileDaemonProxy::GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) { MessageParcel data; @@ -427,6 +484,61 @@ int32_t DistributedFileDaemonProxy::GetRemoteCopyInfo(const std::string &srcUri, return ret; } +int32_t DistributedFileDaemonProxy::GetRemoteCopyInfoACL(const std::string &srcUri, + bool &isFile, + bool &isDir, + const AccountInfo &callerAccountInfo) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return OHOS::FileManagement::E_BROKEN_IPC; + } + if (!data.WriteString(srcUri)) { + LOGE("Failed to send srcUri"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteInt32(callerAccountInfo.userId_)) { + LOGE("Failed to send userId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(callerAccountInfo.accountId_)) { + LOGE("Failed to send accountId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!data.WriteString(callerAccountInfo.networkId_)) { + LOGE("Failed to send networkId"); + return OHOS::FileManagement::E_INVAL_ARG; + } + auto remote = Remote(); + if (remote == nullptr) { + LOGE("remote is nullptr"); + return OHOS::FileManagement::E_BROKEN_IPC; + } + auto ret = remote->SendRequest( + static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO_ACL), data, + reply, option); + if (ret != 0) { + LOGE("SendRequest failed, ret = %{public}d", ret); + return OHOS::FileManagement::E_BROKEN_IPC; + } + if (!reply.ReadBool(isFile)) { + LOGE("read isFile failed"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!reply.ReadBool(isDir)) { + LOGE("read isDir failed"); + return OHOS::FileManagement::E_INVAL_ARG; + } + if (!reply.ReadInt32(ret)) { + LOGE("read res failed"); + return OHOS::FileManagement::E_INVAL_ARG; + } + return ret; +} + int32_t DistributedFileDaemonProxy::CancelCopyTask(const std::string &sessionName) { MessageParcel data; diff --git a/frameworks/native/distributed_file_inner/src/file_mount_manager.cpp b/frameworks/native/distributed_file_inner/src/file_mount_manager.cpp index eeb1a42dd..cc47286af 100644 --- a/frameworks/native/distributed_file_inner/src/file_mount_manager.cpp +++ b/frameworks/native/distributed_file_inner/src/file_mount_manager.cpp @@ -1,23 +1,23 @@ /* -* Copyright (c) 2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -#include -#include #include "dfs_error.h" #include "ipc_skeleton.h" #include "utils_log.h" +#include +#include #undef LOG_DOMAIN #undef LOG_TAG @@ -27,22 +27,11 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -std::shared_ptr FileMountManager::instance_ = nullptr; - -std::shared_ptr FileMountManager::GetInstance() -{ - static std::once_flag once; - std::call_once(once, []() { - FileMountManager::instance_ = std::make_shared(); - }); - - return instance_; -} -int32_t FileMountManager::GetDfsUrisDirFromLocal(const std::vector &uriList, - const int32_t userId, - std::unordered_map &uriToDfsUriMaps) +int32_t FileMountManager::GetDfsUrisDirFromLocal( + const std::vector &uriList, + const int32_t userId, + std::unordered_map &uriToDfsUriMaps) { auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance(); if (!distributedFileDaemonProxy) { @@ -52,6 +41,6 @@ int32_t FileMountManager::GetDfsUrisDirFromLocal(const std::vector return distributedFileDaemonProxy->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps); } -} -} -} \ No newline at end of file +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledaemon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn index c0cd8cdbb..6c6af2282 100644 --- a/services/distributedfiledaemon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -175,6 +175,7 @@ ohos_shared_library("distributed_file_daemon_kit_inner") { cfi_cross_dso = true debug = false } + sources = [ "${distributedfile_path}/frameworks/native/distributed_file_inner/src/asset/asset_adapter_sa_client.cpp", "${distributedfile_path}/frameworks/native/distributed_file_inner/src/asset/asset_obj.cpp", @@ -210,6 +211,7 @@ ohos_shared_library("distributed_file_daemon_kit_inner") { "app_file_service:remote_file_share_native", "c_utils:utils", "data_share:datashare_consumer", + "dsoftbus:softbus_client", "hilog:libhilog", "ipc:ipc_single", "safwk:system_ability_fwk", diff --git a/services/distributedfiledaemon/include/connect_count/connect_count.h b/services/distributedfiledaemon/include/connect_count/connect_count.h index 8ac745925..95460f261 100644 --- a/services/distributedfiledaemon/include/connect_count/connect_count.h +++ b/services/distributedfiledaemon/include/connect_count/connect_count.h @@ -24,6 +24,7 @@ #include #include "ipc/i_file_dfs_listener.h" +#include "single_instance.h" namespace OHOS { namespace Storage { @@ -50,11 +51,8 @@ enum Status { }; class ConnectCount final { + DECLARE_SINGLE_INSTANCE(ConnectCount); public: - ConnectCount() = default; - ~ConnectCount() = default; - static std::shared_ptr GetInstance(); - void AddConnect(uint32_t callingTokenId, const std::string &networkId, sptr &listener); bool CheckCount(const std::string &networkId); void RemoveAllConnect(); @@ -69,7 +67,6 @@ public: void NotifyFileStatusChange(const std::string &networkId, const int32_t status, const std::string &path, StatusType type); private: - static std::shared_ptr instance_; std::recursive_mutex connectMutex_; std::unordered_set> connectList_; std::recursive_mutex fileConnectMutex_; diff --git a/services/distributedfiledaemon/include/ipc/daemon.h b/services/distributedfiledaemon/include/ipc/daemon.h index 4d89ae3dc..b96c88fdd 100644 --- a/services/distributedfiledaemon/include/ipc/daemon.h +++ b/services/distributedfiledaemon/include/ipc/daemon.h @@ -25,6 +25,7 @@ #include "daemon_eventhandler.h" #include "daemon_execute.h" #include "daemon_stub.h" +#include "device/device_profile_adapter.h" #include "dm_device_info.h" #include "file_trans_listener_proxy.h" #include "hmdfs_info.h" @@ -80,7 +81,18 @@ public: const std::string &dstPath, const std::string &dstDeviceId, const std::string &sessionName) override; + // for ACL verification after 6.0.1 version + int32_t RequestSendFileACL(const std::string &srcUri, + const std::string &dstPath, + const std::string &dstDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo) override; int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override; + // for ACL verification after 6.0.1 version + int32_t GetRemoteCopyInfoACL(const std::string &srcUri, + bool &isFile, + bool &isDir, + const AccountInfo &callerAccountInfo) override; int32_t PushAsset(int32_t userId, const sptr &assetObj, @@ -95,7 +107,8 @@ public: static int32_t Copy(const std::string &srcUri, const std::string &dstPath, const sptr &daemon, - const std::string &sessionName); + const std::string &sessionName, + const std::string &srcNetworkId); int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) override; int32_t UpdateDfsSwitchStatus(int32_t switchStatus) override; int32_t GetConnectedDeviceList(std::vector &deviceList) override; @@ -121,12 +134,17 @@ private: const std::string &dstUri, std::string &physicalPath, HmdfsInfo &info, - const sptr &daemon); + const sptr &daemon, + const std::string &networkId); int32_t CheckCopyRule(std::string &physicalPath, const std::string &dstUri, HapTokenInfo &hapTokenInfo, const bool &isSrcFile, HmdfsInfo &info); + int32_t HandleDestinationPathAndPermissions(const std::string &dstUri, + bool isSrcFile, + HmdfsInfo &info, + std::string &physicalPath); int32_t SendDfsDelayTask(const std::string &networkId); void RemoveDfsDelayTask(const std::string &networkId); void DisconnectDevice(const std::string networkId); diff --git a/services/distributedfiledaemon/include/ipc/daemon_event.h b/services/distributedfiledaemon/include/ipc/daemon_event.h index ee0abfdba..a89669bdb 100644 --- a/services/distributedfiledaemon/include/ipc/daemon_event.h +++ b/services/distributedfiledaemon/include/ipc/daemon_event.h @@ -67,13 +67,15 @@ struct PrepareSessionData { const std::string &sessionName, const sptr daemon, HmdfsInfo &info, - const std::shared_ptr> &prepareSessionBlock) + const std::shared_ptr> &prepareSessionBlock, + const std::string &srcNetworkId) : srcUri_(srcUri), physicalPath_(physicalPath), sessionName_(sessionName), daemon_(daemon), info_(info), - prepareSessionBlock_(prepareSessionBlock) + prepareSessionBlock_(prepareSessionBlock), + srcNetworkId_(srcNetworkId) { } @@ -83,6 +85,7 @@ struct PrepareSessionData { const sptr daemon_; HmdfsInfo &info_; const std::shared_ptr> prepareSessionBlock_; + const std::string srcNetworkId_; }; } // namespace DistributedFile diff --git a/services/distributedfiledaemon/include/ipc/daemon_execute.h b/services/distributedfiledaemon/include/ipc/daemon_execute.h index d96528d33..04e2bd146 100644 --- a/services/distributedfiledaemon/include/ipc/daemon_execute.h +++ b/services/distributedfiledaemon/include/ipc/daemon_execute.h @@ -47,7 +47,8 @@ private: std::string &physicalPath, const std::string &sessionName, const sptr &daemon, - HmdfsInfo &info); + HmdfsInfo &info, + const std::string &srcNetworkId); private: std::string GetZipName(const std::string &relativePath); std::vector GetFileList(const std::vector &uris, diff --git a/services/distributedfiledaemon/include/ipc/daemon_stub.h b/services/distributedfiledaemon/include/ipc/daemon_stub.h index 5d67c70aa..480bfea84 100644 --- a/services/distributedfiledaemon/include/ipc/daemon_stub.h +++ b/services/distributedfiledaemon/include/ipc/daemon_stub.h @@ -45,7 +45,9 @@ private: int32_t HandleCancelCopyTask(MessageParcel &data, MessageParcel &reply); int32_t HandleInnerCancelCopyTask(MessageParcel &data, MessageParcel &reply); int32_t HandleRequestSendFile(MessageParcel &data, MessageParcel &reply); + int32_t HandleRequestSendFileACL(MessageParcel &data, MessageParcel &reply); int32_t HandleGetRemoteCopyInfo(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetRemoteCopyInfoACL(MessageParcel &data, MessageParcel &reply); int32_t HandlePushAsset(MessageParcel &data, MessageParcel &reply); int32_t HandleRegisterRecvCallback(MessageParcel &data, MessageParcel &reply); diff --git a/services/distributedfiledaemon/include/ipc/distributed_file_daemon_ipc_interface_code.h b/services/distributedfiledaemon/include/ipc/distributed_file_daemon_ipc_interface_code.h index 4a14a2e71..17a4d7dc4 100644 --- a/services/distributedfiledaemon/include/ipc/distributed_file_daemon_ipc_interface_code.h +++ b/services/distributedfiledaemon/include/ipc/distributed_file_daemon_ipc_interface_code.h @@ -38,7 +38,9 @@ namespace DistributedFile { DISTRIBUTED_FILE_GET_CONNECTED_DEVICE_LIST, DISTRIBUTED_FILE_REGISTER_FILE_DFS_LISTENER, DISTRIBUTED_FILE_UNREGISTER_FILE_DFS_LISTENER, - DISTRIBUTED_FILE_IS_SAME_ACCOUNT_DEVICE + DISTRIBUTED_FILE_IS_SAME_ACCOUNT_DEVICE, + DISTRIBUTED_FILE_REQUEST_SEND_FILE_ACL, + DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO_ACL, }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/include/ipc/i_daemon.h b/services/distributedfiledaemon/include/ipc/i_daemon.h index b902a2be5..1b44eb70a 100644 --- a/services/distributedfiledaemon/include/ipc/i_daemon.h +++ b/services/distributedfiledaemon/include/ipc/i_daemon.h @@ -24,6 +24,7 @@ #include "hmdfs_info.h" #include "iremote_broker.h" #include "i_file_dfs_listener.h" +#include "network/softbus/softbus_permission_check.h" #include "remote_file_share.h" namespace OHOS { @@ -50,8 +51,17 @@ public: const std::string &dstPath, const std::string &remoteDeviceId, const std::string &sessionName) = 0; + virtual int32_t RequestSendFileACL(const std::string &srcUri, + const std::string &dstPath, + const std::string &remoteDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo) = 0; static inline const std::string SERVICE_NAME { "ohos.storage.distributedfile.daemon" }; virtual int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) = 0; + virtual int32_t GetRemoteCopyInfoACL(const std::string &srcUri, + bool &isFile, + bool &isDir, + const AccountInfo &callerAccountInfo) = 0; virtual int32_t PushAsset(int32_t userId, const sptr &assetObj, diff --git a/services/distributedfiledaemon/src/connect_count/connect_count.cpp b/services/distributedfiledaemon/src/connect_count/connect_count.cpp index 25428d24d..864ddc147 100644 --- a/services/distributedfiledaemon/src/connect_count/connect_count.cpp +++ b/services/distributedfiledaemon/src/connect_count/connect_count.cpp @@ -22,16 +22,8 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -std::shared_ptr ConnectCount::instance_ = nullptr; static const int32_t ON_STATUS_OFFLINE = 13900046; -std::shared_ptr ConnectCount::GetInstance() -{ - static std::once_flag once; - std::call_once(once, [&]() { - instance_ = std::make_shared(); - }); - return instance_; -} +IMPLEMENT_SINGLE_INSTANCE(ConnectCount); void ConnectCount::AddConnect(uint32_t callingTokenId, const std::string &networkId, sptr &listener) { diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 609923cdf..120adf323 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -255,6 +255,7 @@ void DeviceManagerAgent::OnDeviceOffline(const DistributedHardware::DmDeviceInfo GetStorageManager(); if (storageMgrProxy_ == nullptr) { LOGE("storageMgrProxy_ is null"); + return; } ret = storageMgrProxy_->UMountDisShareFile(userId, localNetworkId); if (ret != NO_ERROR) { @@ -540,9 +541,9 @@ int32_t DeviceManagerAgent::UMountDfsDocs(const std::string &networkId, const st LOGI("UMountDfsDocs success, deviceId %{public}s erase count", Utils::GetAnonyString(deviceId).c_str()); RemoveMountDfsCount(deviceId); - ConnectCount::GetInstance()->NotifyFileStatusChange(networkId, Status::DISCONNECT_OK, - MOUNT_PATH + networkId.substr(0, VALID_MOUNT_PATH_LEN), - StatusType::CONNECTION_STATUS); + ConnectCount::GetInstance().NotifyFileStatusChange(networkId, Status::DISCONNECT_OK, + MOUNT_PATH + networkId.substr(0, VALID_MOUNT_PATH_LEN), + StatusType::CONNECTION_STATUS); } LOGI("storageMgr.UMountDfsDocs end."); return ret; @@ -619,6 +620,7 @@ int32_t DeviceManagerAgent::AddRemoteReverseObj(uint32_t callingTokenId, sptr lock(appCallConnectMutex_); if (clear) { appCallConnect_.clear(); return FileManagement::E_OK; diff --git a/services/distributedfiledaemon/src/device/device_profile_adapter.cpp b/services/distributedfiledaemon/src/device/device_profile_adapter.cpp index 8937b743b..2b6051c86 100644 --- a/services/distributedfiledaemon/src/device/device_profile_adapter.cpp +++ b/services/distributedfiledaemon/src/device/device_profile_adapter.cpp @@ -73,11 +73,11 @@ bool DeviceProfileAdapter::IsRemoteDfsVersionLowerThanLocal(const std::string &r int32_t ret = GetLocalDfsVersion(packageName, localDfsVersion); if (ret != FileManagement::ERR_OK) { LOGE("GetLocalDfsVersion failed, ret=%{public}d", ret); - return false; + return true; } return IsRemoteDfsVersionLowerThanGiven(remoteNetworkId, localDfsVersion, packageName); #else - return false; + return true; #endif } @@ -89,17 +89,17 @@ bool DeviceProfileAdapter::IsRemoteDfsVersionLowerThanGiven(const std::string &r #ifdef SUPPORT_DEVICE_PROFILE if (remoteNetworkId.empty()) { LOGE("remoteNetworkId is empty"); - return false; + return true; } DfsVersion remoteDfsVersion; int32_t ret = GetDfsVersionFromNetworkId(remoteNetworkId, remoteDfsVersion, packageName); if (ret != FileManagement::ERR_OK) { LOGE("GetDfsVersionFromNetworkId failed, ret=%{public}d", ret); - return false; + return true; } return CompareDfsVersion(remoteDfsVersion, givenDfsVersion); #else - return false; + return true; #endif } diff --git a/services/distributedfiledaemon/src/ipc/daemon.cpp b/services/distributedfiledaemon/src/ipc/daemon.cpp index f60fa3f22..10defe316 100644 --- a/services/distributedfiledaemon/src/ipc/daemon.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon.cpp @@ -213,10 +213,10 @@ int32_t Daemon::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &devic sptr listener = nullptr; auto ret = ConnectionCount(deviceInfo); if (ret == E_OK) { - ConnectCount::GetInstance()->AddConnect(callingTokenId, deviceInfo.networkId, listener); + ConnectCount::GetInstance().AddConnect(callingTokenId, deviceInfo.networkId, listener); } else { if (ret == ERR_CHECKOUT_COUNT) { - ConnectCount::GetInstance()->RemoveConnect(callingTokenId, deviceInfo.networkId); + ConnectCount::GetInstance().RemoveConnect(callingTokenId, deviceInfo.networkId); } CleanUp(deviceInfo); } @@ -229,7 +229,7 @@ int32_t Daemon::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &devi LOGI("Close P2P Connection networkId %{public}s", Utils::GetAnonyString(deviceInfo.networkId).c_str()); auto callingTokenId = IPCSkeleton::GetCallingTokenID(); auto networkId = std::string(deviceInfo.networkId); - ConnectCount::GetInstance()->RemoveConnect(callingTokenId, networkId); + ConnectCount::GetInstance().RemoveConnect(callingTokenId, networkId); CleanUp(deviceInfo); return 0; } @@ -246,7 +246,7 @@ int32_t Daemon::ConnectionCount(const DistributedHardware::DmDeviceInfo &deviceI auto targetDir = ss.str(); auto networkId = std::string(deviceInfo.networkId); int32_t ret = 0; - if (!ConnectCount::GetInstance()->CheckCount(networkId)) { + if (!ConnectCount::GetInstance().CheckCount(networkId)) { ret = DeviceManagerAgent::GetInstance()->OnDeviceP2POnline(deviceInfo); DevslDispatcher::GetDeviceDevsl(networkId); if (ret == NO_ERROR) { @@ -273,7 +273,7 @@ int32_t Daemon::CleanUp(const DistributedHardware::DmDeviceInfo &deviceInfo) { LOGI("CleanUp start"); auto networkId = std::string(deviceInfo.networkId); - if (!ConnectCount::GetInstance()->CheckCount(networkId)) { + if (!ConnectCount::GetInstance().CheckCount(networkId)) { auto ret = SendDfsDelayTask(networkId); LOGI("Close P2P Connection"); return ret; @@ -297,12 +297,12 @@ int32_t Daemon::ConnectionAndMount(const DistributedHardware::DmDeviceInfo &devi if (ret != NO_ERROR) { LOGE("connection failed"); if (ret == ERR_CHECKOUT_COUNT) { - ConnectCount::GetInstance()->RemoveConnect(callingTokenId, networkId); + ConnectCount::GetInstance().RemoveConnect(callingTokenId, networkId); } return ret; } - ConnectCount::GetInstance()->AddConnect(callingTokenId, networkId, remoteReverseObj); + ConnectCount::GetInstance().AddConnect(callingTokenId, networkId, remoteReverseObj); if (!hasFileAccessManager) { LOGW("permission denied: FILE_ACCESS_MANAGER_PERMISSION"); @@ -312,12 +312,12 @@ int32_t Daemon::ConnectionAndMount(const DistributedHardware::DmDeviceInfo &devi auto deviceManager = DeviceManagerAgent::GetInstance(); ret = deviceManager->MountDfsDocs(networkId, mountPath, callingTokenId); if (ret != NO_ERROR) { - ConnectCount::GetInstance()->RemoveConnect(callingTokenId, networkId); + ConnectCount::GetInstance().RemoveConnect(callingTokenId, networkId); LOGE("[MountDfsDocs] failed, ret is %{public}d", ret); return ret; } - ConnectCount::GetInstance()->NotifyFileStatusChange(networkId, Status::CONNECT_OK, HMDFS_FATH + mountPath, - StatusType::CONNECTION_STATUS); + ConnectCount::GetInstance().NotifyFileStatusChange(networkId, Status::CONNECT_OK, HMDFS_FATH + mountPath, + StatusType::CONNECTION_STATUS); NotifyRemotePublishNotification(networkId); return ret; } @@ -408,7 +408,7 @@ int32_t Daemon::CloseP2PConnectionEx(const std::string &networkId) LOGE("strcpy failed, res = %{public}d", res); return E_INVAL_ARG_NAPI; } - ConnectCount::GetInstance()->RemoveConnect(IPCSkeleton::GetCallingTokenID(), networkId); + ConnectCount::GetInstance().RemoveConnect(IPCSkeleton::GetCallingTokenID(), networkId); int32_t ret = CleanUp(deviceInfo); if (ret != NO_ERROR) { LOGE("Daemon::CloseP2PConnectionEx disconnection failed"); @@ -473,6 +473,20 @@ int32_t Daemon::RequestSendFile(const std::string &srcUri, return ret; } +int32_t Daemon::RequestSendFileACL(const std::string &srcUri, + const std::string &dstPath, + const std::string &dstDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo) +{ + LOGI("RequestSendFileACL begin dstDeviceId: %{public}s", Utils::GetAnonyString(dstDeviceId).c_str()); + if (!SoftBusPermissionCheck::CheckSinkPermission(callerAccountInfo)) { + LOGE("CheckSinkPermission failed"); + return ERR_ACL_FAILED; + } + return RequestSendFile(srcUri, dstPath, dstDeviceId, sessionName); +} + int32_t Daemon::InnerCopy(const std::string &srcUri, const std::string &dstUri, const std::string &networkId, const sptr &listener, HmdfsInfo &info) { @@ -488,7 +502,7 @@ int32_t Daemon::InnerCopy(const std::string &srcUri, const std::string &dstUri, return ERR_BAD_VALUE; } OpenP2PConnection(deviceInfo); - auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(srcUri, dstUri, + auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance().RemoteCopy(srcUri, dstUri, listener, QueryActiveUserId(), info.copyPath); CloseP2PConnection(deviceInfo); LOGI("InnerCopy end, ret = %{public}d", ret); @@ -535,18 +549,18 @@ int32_t Daemon::PrepareSession(const std::string &srcUri, int32_t Daemon::CopyBaseOnRPC(const std::string &srcUri, const std::string &dstUri, - const std::string &srcDeviceId, + const std::string &srcNetworkId, const sptr &listenerCallback, HmdfsInfo &info) { - auto daemon = GetRemoteSA(srcDeviceId); + auto daemon = GetRemoteSA(srcNetworkId); if (daemon == nullptr) { LOGE("Daemon is nullptr"); return E_SA_LOAD_FAILED; } std::string physicalPath; - int32_t ret = GetRealPath(srcUri, dstUri, physicalPath, info, daemon); + int32_t ret = GetRealPath(srcUri, dstUri, physicalPath, info, daemon, srcNetworkId); if (ret != E_OK) { LOGE("GetRealPath failed, ret = %{public}d", ret); return ret; @@ -557,8 +571,8 @@ int32_t Daemon::CopyBaseOnRPC(const std::string &srcUri, } auto prepareSessionBlock = std::make_shared>(BLOCK_INTERVAL_SEND_FILE, ERR_BAD_VALUE); - auto prepareSessionData = std::make_shared( - srcUri, physicalPath, info.sessionName, daemon, info, prepareSessionBlock); + auto prepareSessionData = std::make_shared(srcUri, physicalPath, info.sessionName, daemon, info, + prepareSessionBlock, srcNetworkId); auto msgEvent = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PREPARE_SESSION, prepareSessionData, 0); { std::lock_guard lock(eventHandlerMutex_); @@ -599,7 +613,8 @@ int32_t Daemon::GetRealPath(const std::string &srcUri, const std::string &dstUri, std::string &physicalPath, HmdfsInfo &info, - const sptr &daemon) + const sptr &daemon, + const std::string &srcNetworkId) { bool isSrcFile = false; bool isSrcDir = false; @@ -612,26 +627,51 @@ int32_t Daemon::GetRealPath(const std::string &srcUri, LOGE("Path is forbidden"); return OHOS::FileManagement::E_ILLEGAL_URI; } - auto ret = daemon->GetRemoteCopyInfo(srcUri, isSrcFile, isSrcDir); + int32_t ret = E_OK; + if (!DeviceProfileAdapter::GetInstance().IsRemoteDfsVersionLowerThanGiven(srcNetworkId, FILEMANAGER_VERSION)) { + LOGI("Version >= 6.0.1, need ACL check"); + if (!SoftBusPermissionCheck::CheckSrcPermission(srcNetworkId)) { + LOGE("CheckSrcPermission failed"); + return ERR_ACL_FAILED; + } + + AccountInfo localAccountInfo; + if (!SoftBusPermissionCheck::GetLocalAccountInfo(localAccountInfo)) { + LOGE("GetLocalAccountInfo failed"); + return ERR_ACL_FAILED; + } + ret = daemon->GetRemoteCopyInfoACL(srcUri, isSrcFile, isSrcDir, localAccountInfo); + } else { + ret = daemon->GetRemoteCopyInfo(srcUri, isSrcFile, isSrcDir); + } + if (ret != E_OK) { LOGE("GetRemoteCopyInfo failed, ret = %{public}d", ret); RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED, - RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE, - RadarReporter::GET_REMOTE_COPY_INFO_ERROR); + RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE, + RadarReporter::GET_REMOTE_COPY_INFO_ERROR); return E_SOFTBUS_SESSION_FAILED; } + return HandleDestinationPathAndPermissions(dstUri, isSrcFile, info, physicalPath); +} +int32_t Daemon::HandleDestinationPathAndPermissions(const std::string &dstUri, + bool isSrcFile, + HmdfsInfo &info, + std::string &physicalPath) +{ HapTokenInfo hapTokenInfo; int result = AccessTokenKit::GetHapTokenInfo(IPCSkeleton::GetCallingTokenID(), hapTokenInfo); if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { LOGE("GetHapTokenInfo failed, errCode = %{public}d", result); RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED, - RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE, - RadarReporter::GET_HAP_TOKEN_INFO_ERROR, RadarReporter::PACKAGE_NAME, - RadarReporter::accessTokenKit + to_string(result)); + RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE, + RadarReporter::GET_HAP_TOKEN_INFO_ERROR, RadarReporter::PACKAGE_NAME, + RadarReporter::accessTokenKit + to_string(result)); return E_GET_USER_ID; } - ret = SandboxHelper::GetPhysicalPath(dstUri, std::to_string(hapTokenInfo.userID), physicalPath); + + int32_t ret = SandboxHelper::GetPhysicalPath(dstUri, std::to_string(hapTokenInfo.userID), physicalPath); if (ret != E_OK) { LOGE("invalid uri, ret = %{public}d", ret); return E_GET_PHYSICAL_PATH_FAILED; @@ -722,6 +762,19 @@ int32_t Daemon::GetRemoteCopyInfo(const std::string &srcUri, bool &isSrcFile, bo return E_OK; } +int32_t Daemon::GetRemoteCopyInfoACL(const std::string &srcUri, + bool &isSrcFile, + bool &srcIsDir, + const AccountInfo &callerAccountInfo) +{ + LOGI("GetRemoteCopyInfoACL begin."); + if (!SoftBusPermissionCheck::CheckSinkPermission(callerAccountInfo)) { + LOGE("CheckSinkPermission failed"); + return ERR_ACL_FAILED; + } + return GetRemoteCopyInfo(srcUri, isSrcFile, srcIsDir); +} + sptr Daemon::GetRemoteSA(const std::string &remoteDeviceId) { LOGI("GetRemoteSA begin, DeviceId: %{public}s", Utils::GetAnonyString(remoteDeviceId).c_str()); @@ -751,8 +804,13 @@ sptr Daemon::GetRemoteSA(const std::string &remoteDeviceId) int32_t Daemon::Copy(const std::string &srcUri, const std::string &dstPath, const sptr &daemon, - const std::string &sessionName) + const std::string &sessionName, + const std::string &srcNetworkId) { + if (daemon == nullptr) { + LOGE("Daemon::Copy daemon is nullptr"); + return E_INVAL_ARG_NAPI; + } if (!FileSizeUtils::IsFilePathValid(FileSizeUtils::GetRealUri(srcUri)) || !FileSizeUtils::IsFilePathValid(FileSizeUtils::GetRealUri(dstPath))) { LOGE("Path is forbidden"); @@ -765,12 +823,26 @@ int32_t Daemon::Copy(const std::string &srcUri, LOGE("GetLocalDeviceInfo failed, errCode = %{public}d", errCode); return E_GET_DEVICE_ID; } - if (daemon == nullptr) { - LOGE("Daemon::Copy daemon is nullptr"); - return E_INVAL_ARG_NAPI; - } LOGI("Copy localDeviceInfo.networkId: %{public}s", Utils::GetAnonyString(localDeviceInfo.networkId).c_str()); - auto ret = daemon->RequestSendFile(srcUri, dstPath, localDeviceInfo.networkId, sessionName); + + int32_t ret = E_OK; + if (!DeviceProfileAdapter::GetInstance().IsRemoteDfsVersionLowerThanGiven(srcNetworkId, FILEMANAGER_VERSION)) { + LOGI("Version >= 6.0.1, need ACL check"); + if (!SoftBusPermissionCheck::CheckSrcPermission(srcNetworkId)) { + LOGE("CheckSrcPermission failed"); + return ERR_ACL_FAILED; + } + + AccountInfo localAccountInfo; + if (!SoftBusPermissionCheck::GetLocalAccountInfo(localAccountInfo)) { + LOGE("GetLocalAccountInfo failed"); + return ERR_ACL_FAILED; + } + + ret = daemon->RequestSendFileACL(srcUri, dstPath, localDeviceInfo.networkId, sessionName, localAccountInfo); + } else { + ret = daemon->RequestSendFile(srcUri, dstPath, localDeviceInfo.networkId, sessionName); + } if (ret != E_OK) { LOGE("RequestSendFile failed, ret = %{public}d", ret); RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED, @@ -802,7 +874,7 @@ int32_t Daemon::CancelCopyTask(const std::string &sessionName) int32_t Daemon::CancelCopyTask(const std::string &srcUri, const std::string &dstUri) { - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, dstUri); + Storage::DistributedFile::RemoteFileCopyManager::GetInstance().RemoteCancel(srcUri, dstUri); return E_OK; } @@ -821,12 +893,12 @@ void Daemon::DfsListenerDeathRecipient::OnRemoteDied(const wptr & } uint32_t callingTokenId; - auto ret = ConnectCount::GetInstance()->FindCallingTokenIdForListerner(diedRemote, callingTokenId); + auto ret = ConnectCount::GetInstance().FindCallingTokenIdForListerner(diedRemote, callingTokenId); if (ret != E_OK) { LOGE("fail to FindCallingTokenIdForListerner"); return; } - std::vector networkIds = ConnectCount::GetInstance()->RemoveConnect(callingTokenId); + std::vector networkIds = ConnectCount::GetInstance().RemoveConnect(callingTokenId); if (networkIds.empty()) { LOGE("fail to get networkIdSet"); return; @@ -843,7 +915,7 @@ void Daemon::DfsListenerDeathRecipient::OnRemoteDied(const wptr & LOGE("strcpy failed, res = %{public}d", res); return; } - if (!ConnectCount::GetInstance()->CheckCount(*it)) { + if (!ConnectCount::GetInstance().CheckCount(*it)) { DeviceManagerAgent::GetInstance()->OnDeviceP2POffline(deviceInfo); } } @@ -1048,7 +1120,7 @@ int32_t Daemon::UpdateDfsSwitchStatus(int32_t switchStatus) return E_INVAL_ARG_NAPI; } for (const auto &ele : mountInfo.second.callingCountMap_) { - ConnectCount::GetInstance()->RemoveConnect(ele.first, networkId); + ConnectCount::GetInstance().RemoveConnect(ele.first, networkId); } ret = CleanUp(deviceInfo); if (ret != NO_ERROR) { @@ -1078,7 +1150,7 @@ int32_t Daemon::RegisterFileDfsListener(const std::string &instanceId, const spt LOGE("listener is nullptr"); return E_INVAL_ARG_NAPI; } - ConnectCount::GetInstance()->AddFileConnect(instanceId, listener); + ConnectCount::GetInstance().AddFileConnect(instanceId, listener); return E_OK; } @@ -1089,7 +1161,7 @@ int32_t Daemon::UnregisterFileDfsListener(const std::string &instanceId) LOGE("InstanceId length is invalid."); return E_INVAL_ARG_NAPI; } - if (!ConnectCount::GetInstance()->RmFileConnect(instanceId)) { + if (!ConnectCount::GetInstance().RmFileConnect(instanceId)) { LOGE("RmFileConnect failed"); return E_INVAL_ARG_NAPI; } @@ -1141,7 +1213,7 @@ void Daemon::DisconnectByRemote(const string &networkId) LOGE("strcpy failed, res = %{public}d", res); return; } - ConnectCount::GetInstance()->RemoveConnect(IPCSkeleton::GetCallingTokenID(), networkId); + ConnectCount::GetInstance().RemoveConnect(IPCSkeleton::GetCallingTokenID(), networkId); int32_t ret = CleanUp(deviceInfo); if (ret != NO_ERROR) { LOGE("DisconnectByRemote disconnection failed."); diff --git a/services/distributedfiledaemon/src/ipc/daemon_execute.cpp b/services/distributedfiledaemon/src/ipc/daemon_execute.cpp index 7f9b6774b..6f7316430 100644 --- a/services/distributedfiledaemon/src/ipc/daemon_execute.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon_execute.cpp @@ -217,19 +217,21 @@ void DaemonExecute::ExecutePrepareSession(const AppExecFwk::InnerEvent::Pointer return; } std::string srcUri = prepareSessionData->srcUri_; - std::string physicalPath = prepareSessionData->physicalPath_; - std::string sessionName = prepareSessionData->sessionName_; - sptr daemon = prepareSessionData->daemon_; + std::string physicalPath = prepareSessionData->physicalPath_; + std::string sessionName = prepareSessionData->sessionName_; + sptr daemon = prepareSessionData->daemon_; HmdfsInfo &info = prepareSessionData->info_; + std::string srcNetworkId = prepareSessionData->srcNetworkId_; - prepareSessionBlock->SetValue(PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info)); + prepareSessionBlock->SetValue(PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info, srcNetworkId)); } int32_t DaemonExecute::PrepareSessionInner(const std::string &srcUri, std::string &physicalPath, const std::string &sessionName, const sptr &daemon, - HmdfsInfo &info) + HmdfsInfo &info, + const std::string &srcNetworkId) { LOGI("PrepareSessionInner begin."); auto socketId = SoftBusHandler::GetInstance().CreateSessionServer(IDaemon::SERVICE_NAME, sessionName, @@ -243,7 +245,7 @@ int32_t DaemonExecute::PrepareSessionInner(const std::string &srcUri, LOGI("authority is media or docs"); physicalPath = "??" + info.dstPhysicalPath; } - auto ret = Daemon::Copy(srcUri, physicalPath, daemon, sessionName); + auto ret = Daemon::Copy(srcUri, physicalPath, daemon, sessionName, srcNetworkId); if (ret != E_OK) { LOGE("Remote copy failed,ret = %{public}d", ret); SoftBusHandler::GetInstance().CloseSession(socketId, sessionName); diff --git a/services/distributedfiledaemon/src/ipc/daemon_stub.cpp b/services/distributedfiledaemon/src/ipc/daemon_stub.cpp index 135dd40ca..448e17a80 100644 --- a/services/distributedfiledaemon/src/ipc/daemon_stub.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon_stub.cpp @@ -23,7 +23,7 @@ #include "ipc_skeleton.h" #include "securec.h" #include "utils_log.h" - +#include namespace OHOS { namespace Storage { @@ -89,6 +89,12 @@ void DaemonStub::InitDFileFunction() opToInterfaceMap_[static_cast( DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_IS_SAME_ACCOUNT_DEVICE)] = &DaemonStub::HandleIsSameAccountDevice; + opToInterfaceMap_[static_cast( + DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO_ACL)] = + &DaemonStub::HandleGetRemoteCopyInfoACL; + opToInterfaceMap_[static_cast( + DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE_ACL)] = + &DaemonStub::HandleRequestSendFileACL; } int32_t DaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -96,10 +102,14 @@ int32_t DaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageP if (data.ReadInterfaceToken() != GetDescriptor()) { return DFS_DAEMON_DESCRIPTOR_IS_EMPTY; } - if (code != static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE) && - code != static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO) && - !IPCSkeleton::IsLocalCalling()) { - LOGE("function is only allowed to be called locally."); + static const std::unordered_set remoteAllowedCodes = { + static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE), + static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO), + static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO_ACL), + static_cast(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE_ACL)}; + + if (remoteAllowedCodes.find(code) == remoteAllowedCodes.end() && !IPCSkeleton::IsLocalCalling()) { + LOGE("Function is only allowed to be called locally, code: %{public}u", code); return E_ALLOW_LOCAL_CALL_ONLY; } auto iter = opToInterfaceMap_.find(code); @@ -286,6 +296,51 @@ int32_t DaemonStub::HandleRequestSendFile(MessageParcel &data, MessageParcel &re return res; } +int32_t DaemonStub::HandleRequestSendFileACL(MessageParcel &data, MessageParcel &reply) +{ + LOGI("Begin HandleRequestSendFileACL"); + std::string srcUri; + if (!data.ReadString(srcUri)) { + LOGE("read srcUri failed"); + return E_IPC_READ_FAILED; + } + std::string dstPath; + if (!data.ReadString(dstPath)) { + LOGE("read dstPath failed"); + return E_IPC_READ_FAILED; + } + std::string dstDeviceId; + if (!data.ReadString(dstDeviceId)) { + LOGE("read remoteDeviceId failed"); + return E_IPC_READ_FAILED; + } + std::string sessionName; + if (!data.ReadString(sessionName)) { + LOGE("read sessionName failed"); + return E_IPC_READ_FAILED; + } + AccountInfo callerAccountInfo; + if (!data.ReadInt32(callerAccountInfo.userId_)) { + LOGE("read userId failed"); + return E_IPC_READ_FAILED; + } + if (!data.ReadString(callerAccountInfo.accountId_)) { + LOGE("read accountId failed"); + return E_IPC_READ_FAILED; + } + if (!data.ReadString(callerAccountInfo.networkId_)) { + LOGE("read networkId failed"); + return E_IPC_READ_FAILED; + } + auto res = RequestSendFileACL(srcUri, dstPath, dstDeviceId, sessionName, callerAccountInfo); + if (!reply.WriteInt32(res)) { + LOGE("write result to reply failed."); + return E_IPC_READ_FAILED; + } + LOGD("End HandleRequestSendFileACL, ret = %{public}d.", res); + return res; +} + int32_t DaemonStub::HandleGetRemoteCopyInfo(MessageParcel &data, MessageParcel &reply) { LOGI("Begin HandleGetRemoteCopyInfo"); @@ -317,6 +372,46 @@ int32_t DaemonStub::HandleGetRemoteCopyInfo(MessageParcel &data, MessageParcel & return res; } +int32_t DaemonStub::HandleGetRemoteCopyInfoACL(MessageParcel &data, MessageParcel &reply) +{ + LOGI("Begin HandleGetRemoteCopyInfoACL"); + std::string srcUri; + AccountInfo callerAccountInfo; + if (!data.ReadString(srcUri)) { + LOGE("read srcUri failed"); + return E_IPC_READ_FAILED; + } + if (!data.ReadInt32(callerAccountInfo.userId_)) { + LOGE("read userId failed"); + return E_IPC_READ_FAILED; + } + if (!data.ReadString(callerAccountInfo.accountId_)) { + LOGE("read accountId failed"); + return E_IPC_READ_FAILED; + } + if (!data.ReadString(callerAccountInfo.networkId_)) { + LOGE("read networkId failed"); + return E_IPC_READ_FAILED; + } + bool isFile = false; + bool isDir = false; + auto res = GetRemoteCopyInfoACL(srcUri, isFile, isDir, callerAccountInfo); + if (!reply.WriteBool(isFile)) { + LOGE("Write isFile failed"); + return E_IPC_WRITE_FAILED; + } + if (!reply.WriteBool(isDir)) { + LOGE("Write isDir failed"); + return E_IPC_WRITE_FAILED; + } + if (!reply.WriteInt32(res)) { + LOGE("Write res failed"); + return E_IPC_WRITE_FAILED; + } + LOGD("End GetRemoteCopyInfoACL, ret = %{public}d.", res); + return res; +} + int32_t DaemonStub::HandleCancelCopyTask(MessageParcel &data, MessageParcel &reply) { LOGI("Begin HandleCancelCopyTask"); diff --git a/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp b/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp index f499eae53..5b4dad3cf 100644 --- a/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp +++ b/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp @@ -28,7 +28,6 @@ namespace DistributedFile { using namespace std; namespace { static const std::string SAME_ACCOUNT = "account"; -const int32_t INVALID_USER_ID = -1; } // namespace OsAccountObserver::OsAccountObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) diff --git a/services/distributedfiledaemon/src/network/network_agent_template.cpp b/services/distributedfiledaemon/src/network/network_agent_template.cpp index 89b463dbe..5f9975d8e 100644 --- a/services/distributedfiledaemon/src/network/network_agent_template.cpp +++ b/services/distributedfiledaemon/src/network/network_agent_template.cpp @@ -111,7 +111,7 @@ void NetworkAgentTemplate::ConnectOnlineDevices() void NetworkAgentTemplate::DisconnectAllDevices() { sessionPool_.ReleaseAllSession(); - ConnectCount::GetInstance()->RemoveAllConnect(); + ConnectCount::GetInstance().RemoveAllConnect(); } // for closeP2P @@ -126,11 +126,11 @@ void NetworkAgentTemplate::DisconnectDeviceByP2PHmdfs(const DeviceInfo info) { LOGI("DeviceOffline, cid:%{public}s", Utils::GetAnonyString(info.GetCid()).c_str()); sessionPool_.ReleaseSession(info.GetCid(), true); - ConnectCount::GetInstance()->NotifyRemoteReverseObj(info.GetCid(), ON_STATUS_OFFLINE); - ConnectCount::GetInstance()->RemoveConnect(info.GetCid()); - ConnectCount::GetInstance()->NotifyFileStatusChange(info.GetCid(), Status::DEVICE_OFFLINE, - info.GetCid().substr(0, VALID_MOUNT_PATH_LEN), - StatusType::CONNECTION_STATUS); + ConnectCount::GetInstance().NotifyRemoteReverseObj(info.GetCid(), ON_STATUS_OFFLINE); + ConnectCount::GetInstance().RemoveConnect(info.GetCid()); + ConnectCount::GetInstance().NotifyFileStatusChange(info.GetCid(), Status::DEVICE_OFFLINE, + info.GetCid().substr(0, VALID_MOUNT_PATH_LEN), + StatusType::CONNECTION_STATUS); } // softbus offline, allConnect offline, hmdfs never has socket @@ -145,10 +145,10 @@ void NetworkAgentTemplate::CloseSessionForOneDevice(const string &cid) void NetworkAgentTemplate::CloseSessionForOneDeviceInner(std::string cid) { sessionPool_.ReleaseSession(cid, true); - ConnectCount::GetInstance()->NotifyRemoteReverseObj(cid, ON_STATUS_OFFLINE); - ConnectCount::GetInstance()->NotifyFileStatusChange( + ConnectCount::GetInstance().NotifyRemoteReverseObj(cid, ON_STATUS_OFFLINE); + ConnectCount::GetInstance().NotifyFileStatusChange( cid, Status::DEVICE_OFFLINE, cid.substr(0, VALID_MOUNT_PATH_LEN), StatusType::CONNECTION_STATUS); - ConnectCount::GetInstance()->RemoveConnect(cid); + ConnectCount::GetInstance().RemoveConnect(cid); } void NetworkAgentTemplate::AcceptSession(shared_ptr session, const std::string backStage) @@ -184,7 +184,7 @@ void NetworkAgentTemplate::GetSessionProcessInner(NotifyParam param) bool isServer = false; bool ifGetSession = sessionPool_.CheckIfGetSession(fd, isServer); sessionPool_.ReleaseSession(fd); - if (ifGetSession && ConnectCount::GetInstance()->CheckCount(cidStr)) { + if (ifGetSession && ConnectCount::GetInstance().CheckCount(cidStr)) { // for client GetSession(cidStr); } else { @@ -207,8 +207,8 @@ void NetworkAgentTemplate::GetSession(const string &cid) } LOGE("reget session failed"); sessionPool_.SinkOffline(cid); - ConnectCount::GetInstance()->NotifyRemoteReverseObj(cid, ON_STATUS_OFFLINE); - ConnectCount::GetInstance()->RemoveConnect(cid); + ConnectCount::GetInstance().NotifyRemoteReverseObj(cid, ON_STATUS_OFFLINE); + ConnectCount::GetInstance().RemoveConnect(cid); DeviceManagerAgent::GetInstance()->UMountDfsDocs(cid, cid.substr(0, VALID_MOUNT_PATH_LEN), true); } catch (const DfsuException &e) { LOGE("reget session failed, code: %{public}d", e.code()); diff --git a/services/distributedfiledaemon/src/network/session_pool.cpp b/services/distributedfiledaemon/src/network/session_pool.cpp index 08b41defe..210e96076 100644 --- a/services/distributedfiledaemon/src/network/session_pool.cpp +++ b/services/distributedfiledaemon/src/network/session_pool.cpp @@ -114,6 +114,7 @@ void SessionPool::ReleaseSession(const std::string &cid, bool isReleaseAll) bool SessionPool::FindCid(const std::string &cid) { + lock_guard lock(sessionPoolLock_); for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) { if ((*iter)->GetCid() == cid) { return true; diff --git a/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp index 6cbca9a8d..6abb58765 100644 --- a/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp @@ -62,12 +62,13 @@ std::vector SoftBusSessionListener::GetFileName(const std::vector #include "daemon.h" +#include namespace OHOS { namespace Storage { @@ -33,6 +33,12 @@ public: const std::string &dstPath, const std::string &remoteDeviceId, const std::string &sessionName)); + MOCK_METHOD5(RequestSendFileACL, + int32_t(const std::string &srcUri, + const std::string &dstPath, + const std::string &remoteDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo)); MOCK_METHOD5(PrepareSession, int32_t(const std::string &srcUri, const std::string &dstUri, @@ -42,11 +48,11 @@ public: MOCK_METHOD1(CancelCopyTask, int32_t(const std::string &sessionName)); MOCK_METHOD2(CancelCopyTask, int32_t(const std::string &srcUri, const std::string &dstUri)); MOCK_METHOD3(GetRemoteCopyInfo, int32_t(const std::string &srcUri, bool &isFile, bool &isDir)); + MOCK_METHOD4(GetRemoteCopyInfoACL, + int32_t(const std::string &srcUri, bool &isFile, bool &isDir, const AccountInfo &callerAccountInfo)); MOCK_METHOD3(PushAsset, - int32_t(int32_t userId, - const sptr &assetObj, - const sptr &sendCallback)); + int32_t(int32_t userId, const sptr &assetObj, const sptr &sendCallback)); MOCK_METHOD1(RegisterAssetCallback, int32_t(const sptr &recvCallback)); MOCK_METHOD1(UnRegisterAssetCallback, int32_t(const sptr &recvCallback)); MOCK_METHOD3(GetDfsUrisDirFromLocal, int32_t(const std::vector &uriList, diff --git a/services/distributedfiledaemon/test/unittest/BUILD.gn b/services/distributedfiledaemon/test/unittest/BUILD.gn index f6c8f2bf3..93fe50ebe 100644 --- a/services/distributedfiledaemon/test/unittest/BUILD.gn +++ b/services/distributedfiledaemon/test/unittest/BUILD.gn @@ -55,6 +55,7 @@ ohos_unittest("device_info_test") { "app_file_service:remote_file_share_native", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -371,63 +372,6 @@ ohos_unittest("kernel_talker_test") { } } -ohos_unittest("session_pool_test") { - branch_protector_ret = "pac_ret" - sanitize = { - integer_overflow = true - cfi = true - cfi_cross_dso = true - debug = false - } - - module_out_path = module_output_path - - sources = [ "network/session_pool_test.cpp" ] - - configs = [ - ":module_private_config", - "${utils_path}:compiler_configs", - ] - - deps = [ - "${services_path}/distributedfiledaemon:distributed_file_daemon_kit_inner", - "${services_path}/distributedfiledaemon:libdistributedfiledaemon", - "${utils_path}:libdistributedfileutils", - ] - - external_deps = [ - "ability_base:want", - "ability_base:zuri", - "access_token:libaccesstoken_sdk", - "app_file_service:sandbox_helper_native", - "c_utils:utils", - "common_event_service:cesfwk_innerkits", - "dataclassification:data_transit_mgr", - "device_auth:deviceauth_sdk", - "device_manager:devicemanagersdk", - "dsoftbus:softbus_client", - "eventhandler:libeventhandler", - "file_api:securitylabel", - "googletest:gmock_main", - "googletest:gtest_main", - "hilog:libhilog", - "hisysevent:libhisysevent", - "init:libbegetutil", - "ipc:ipc_single", - "os_account:os_account_innerkits", - "safwk:system_ability_fwk", - "samgr:samgr_proxy", - "storage_service:storage_manager_sa_proxy", - "zlib:shared_libz", - ] - - defines = [ "private=public" ] - - if (support_same_account) { - defines += [ "SUPPORT_SAME_ACCOUNT" ] - } -} - ohos_unittest("softbus_agent_test") { branch_protector_ret = "pac_ret" sanitize = { diff --git a/services/distributedfiledaemon/test/unittest/connect_count/BUILD.gn b/services/distributedfiledaemon/test/unittest/connect_count/BUILD.gn index 93786e145..a9640e6e7 100644 --- a/services/distributedfiledaemon/test/unittest/connect_count/BUILD.gn +++ b/services/distributedfiledaemon/test/unittest/connect_count/BUILD.gn @@ -63,6 +63,7 @@ ohos_unittest("connect_count_test") { "hilog:libhilog", "ipc:ipc_core", "json:nlohmann_json_static", + "safwk:system_ability_fwk", ] defines = [ diff --git a/services/distributedfiledaemon/test/unittest/connect_count/connect_count_test.cpp b/services/distributedfiledaemon/test/unittest/connect_count/connect_count_test.cpp index abc673508..f9c3e4b24 100644 --- a/services/distributedfiledaemon/test/unittest/connect_count/connect_count_test.cpp +++ b/services/distributedfiledaemon/test/unittest/connect_count/connect_count_test.cpp @@ -44,7 +44,6 @@ public: void TearDown(); public: - static inline std::shared_ptr connectCount_ = nullptr; static inline uint32_t testCallingTokenId = 1234; static inline std::string testNetworkId = "testNetworkId"; static inline sptr testListener = nullptr; @@ -55,7 +54,6 @@ public: void ConnectCountTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "ConnectCountTest SetUpTestCase"; - connectCount_ = ConnectCount::GetInstance(); testListener = new MockFileDfsListener(); // Use Mock class } @@ -63,7 +61,6 @@ void ConnectCountTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "ConnectCountTest TearDownTestCase"; - connectCount_ = nullptr; testListener = nullptr; } @@ -75,7 +72,7 @@ void ConnectCountTest::SetUp(void) void ConnectCountTest::TearDown(void) { GTEST_LOG_(INFO) << "TearDown"; - connectCount_->RemoveAllConnect(); + ConnectCount::GetInstance().RemoveAllConnect(); } /** @@ -87,9 +84,9 @@ void ConnectCountTest::TearDown(void) HWTEST_F(ConnectCountTest, GetInstance_001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetInstance_001 start"; - auto instance1 = ConnectCount::GetInstance(); - auto instance2 = ConnectCount::GetInstance(); - EXPECT_EQ(instance1, instance2); // Verify singleton instance + auto &instance1 = ConnectCount::GetInstance(); + auto &instance2 = ConnectCount::GetInstance(); + EXPECT_EQ(&instance1, &instance2); // Verify singleton instance GTEST_LOG_(INFO) << "GetInstance_001 end"; } @@ -102,8 +99,8 @@ HWTEST_F(ConnectCountTest, GetInstance_001, TestSize.Level1) HWTEST_F(ConnectCountTest, AddConnect_001, TestSize.Level1) { GTEST_LOG_(INFO) << "AddConnect_001 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - EXPECT_TRUE(connectCount_->CheckCount(testNetworkId)); // Verify connection is added + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + EXPECT_TRUE(ConnectCount::GetInstance().CheckCount(testNetworkId)); // Verify connection is added GTEST_LOG_(INFO) << "AddConnect_001 end"; } @@ -116,9 +113,9 @@ HWTEST_F(ConnectCountTest, AddConnect_001, TestSize.Level1) HWTEST_F(ConnectCountTest, AddConnect_002, TestSize.Level1) { GTEST_LOG_(INFO) << "AddConnect_002 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - auto networkIds = connectCount_->GetNetworkIds(testCallingTokenId); + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + auto networkIds = ConnectCount::GetInstance().GetNetworkIds(testCallingTokenId); EXPECT_EQ(networkIds.size(), 1); // Verify only one networkId is added GTEST_LOG_(INFO) << "AddConnect_002 end"; } @@ -132,10 +129,10 @@ HWTEST_F(ConnectCountTest, AddConnect_002, TestSize.Level1) HWTEST_F(ConnectCountTest, RemoveConnect_001, TestSize.Level1) { GTEST_LOG_(INFO) << "RemoveConnect_001 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - auto networkIds = connectCount_->RemoveConnect(testCallingTokenId); + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + auto networkIds = ConnectCount::GetInstance().RemoveConnect(testCallingTokenId); EXPECT_EQ(networkIds.size(), 1); // Verify networkId is returned - EXPECT_FALSE(connectCount_->CheckCount(testNetworkId)); // Verify connection is removed + EXPECT_FALSE(ConnectCount::GetInstance().CheckCount(testNetworkId)); // Verify connection is removed GTEST_LOG_(INFO) << "RemoveConnect_001 end"; } @@ -148,9 +145,9 @@ HWTEST_F(ConnectCountTest, RemoveConnect_001, TestSize.Level1) HWTEST_F(ConnectCountTest, RemoveConnect_002, TestSize.Level1) { GTEST_LOG_(INFO) << "RemoveConnect_002 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - connectCount_->RemoveConnect(testNetworkId); - EXPECT_FALSE(connectCount_->CheckCount(testNetworkId)); // Verify connection is removed + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + ConnectCount::GetInstance().RemoveConnect(testNetworkId); + EXPECT_FALSE(ConnectCount::GetInstance().CheckCount(testNetworkId)); // Verify connection is removed GTEST_LOG_(INFO) << "RemoveConnect_002 end"; } @@ -163,9 +160,9 @@ HWTEST_F(ConnectCountTest, RemoveConnect_002, TestSize.Level1) HWTEST_F(ConnectCountTest, RemoveConnect_003, TestSize.Level1) { GTEST_LOG_(INFO) << "RemoveConnect_003 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - connectCount_->RemoveConnect(testCallingTokenId, testNetworkId); - EXPECT_FALSE(connectCount_->CheckCount(testNetworkId)); // Verify connection is removed + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + ConnectCount::GetInstance().RemoveConnect(testCallingTokenId, testNetworkId); + EXPECT_FALSE(ConnectCount::GetInstance().CheckCount(testNetworkId)); // Verify connection is removed GTEST_LOG_(INFO) << "RemoveConnect_003 end"; } @@ -178,9 +175,9 @@ HWTEST_F(ConnectCountTest, RemoveConnect_003, TestSize.Level1) HWTEST_F(ConnectCountTest, RemoveAllConnect_001, TestSize.Level1) { GTEST_LOG_(INFO) << "RemoveAllConnect_001 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - connectCount_->RemoveAllConnect(); - EXPECT_FALSE(connectCount_->CheckCount(testNetworkId)); // Verify all connections are removed + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + ConnectCount::GetInstance().RemoveAllConnect(); + EXPECT_FALSE(ConnectCount::GetInstance().CheckCount(testNetworkId)); // Verify all connections are removed GTEST_LOG_(INFO) << "RemoveAllConnect_001 end"; } @@ -199,8 +196,8 @@ HWTEST_F(ConnectCountTest, NotifyRemoteReverseObj_001, TestSize.Level1) OnStatus(testNetworkId, ON_STATUS_OFFLINE, "", 0)) .Times(1); // 仅在当前测试用例中生效 - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); - connectCount_->NotifyRemoteReverseObj(testNetworkId, ON_STATUS_OFFLINE); + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); + ConnectCount::GetInstance().NotifyRemoteReverseObj(testNetworkId, ON_STATUS_OFFLINE); // 清除 EXPECT_CALL 断言 testing::Mock::VerifyAndClearExpectations(testListener.GetRefPtr()); @@ -217,9 +214,10 @@ HWTEST_F(ConnectCountTest, NotifyRemoteReverseObj_001, TestSize.Level1) HWTEST_F(ConnectCountTest, FindCallingTokenIdForListerner_001, TestSize.Level1) { GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_001 start"; - connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener); + ConnectCount::GetInstance().AddConnect(testCallingTokenId, testNetworkId, testListener); uint32_t foundCallingTokenId = 0; - int32_t ret = connectCount_->FindCallingTokenIdForListerner(testListener->AsObject(), foundCallingTokenId); + int32_t ret = + ConnectCount::GetInstance().FindCallingTokenIdForListerner(testListener->AsObject(), foundCallingTokenId); EXPECT_EQ(ret, FileManagement::E_OK); // Verify success EXPECT_EQ(foundCallingTokenId, testCallingTokenId); // Verify correct tokenId is found GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_001 end"; @@ -236,7 +234,7 @@ HWTEST_F(ConnectCountTest, FindCallingTokenIdForListerner_002, TestSize.Level1) GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_002 start"; sptr invalidListener = nullptr; uint32_t foundCallingTokenId = 0; - int32_t ret = connectCount_->FindCallingTokenIdForListerner(invalidListener, foundCallingTokenId); + int32_t ret = ConnectCount::GetInstance().FindCallingTokenIdForListerner(invalidListener, foundCallingTokenId); EXPECT_EQ(ret, FileManagement::ERR_BAD_VALUE); // Verify error is returned GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_002 end"; } diff --git a/services/distributedfiledaemon/test/unittest/device/device_profile_adapter/device_profile_adapter_test.cpp b/services/distributedfiledaemon/test/unittest/device/device_profile_adapter/device_profile_adapter_test.cpp index 3bebdc702..a6a335fb3 100644 --- a/services/distributedfiledaemon/test/unittest/device/device_profile_adapter/device_profile_adapter_test.cpp +++ b/services/distributedfiledaemon/test/unittest/device/device_profile_adapter/device_profile_adapter_test.cpp @@ -113,13 +113,13 @@ HWTEST_F(DeviceProfileAdapterTest, DeviceProfileAdapterTest_IsRemoteDfsVersionLo g_GetLocalDeviceInfo = ERR_BAD_VALUE; auto ret = DeviceProfileAdapter::GetInstance().IsRemoteDfsVersionLowerThanLocal(""); - EXPECT_EQ(ret, false); + EXPECT_EQ(ret, true); g_GetLocalDeviceInfo = FileManagement::ERR_OK; g_GetUdidByNetworkId = FileManagement::ERR_OK; g_GetCharacteristicProfile = FileManagement::ERR_OK; ret = DeviceProfileAdapter::GetInstance().IsRemoteDfsVersionLowerThanLocal(""); - EXPECT_EQ(ret, false); + EXPECT_EQ(ret, true); GTEST_LOG_(INFO) << "DeviceProfileAdapterTest_IsRemoteDfsVersionLowerThanLocal_001 end"; } @@ -135,12 +135,12 @@ HWTEST_F(DeviceProfileAdapterTest, DeviceProfileAdapterTest_IsRemoteDfsVersionLo GTEST_LOG_(INFO) << "DeviceProfileAdapterTest_IsRemoteDfsVersionLowerThanGiven_001 begin"; auto ret = DeviceProfileAdapter::GetInstance().IsRemoteDfsVersionLowerThanGiven("", {0, 0, 0}); - EXPECT_EQ(ret, false); + EXPECT_EQ(ret, true); g_GetUdidByNetworkId = -1; std::string remoteNetworkId = "remoteTest"; ret = DeviceProfileAdapter::GetInstance().IsRemoteDfsVersionLowerThanGiven(remoteNetworkId, {0, 0, 0}); - EXPECT_EQ(ret, false); + EXPECT_EQ(ret, true); g_GetUdidByNetworkId = FileManagement::ERR_OK; g_GetCharacteristicProfile = FileManagement::ERR_OK; diff --git a/services/distributedfiledaemon/test/unittest/ipc/daemon/daemon_test.cpp b/services/distributedfiledaemon/test/unittest/ipc/daemon/daemon_test.cpp index 994430d3c..be9a0eee8 100644 --- a/services/distributedfiledaemon/test/unittest/ipc/daemon/daemon_test.cpp +++ b/services/distributedfiledaemon/test/unittest/ipc/daemon/daemon_test.cpp @@ -48,6 +48,7 @@ #include "softbus_session_pool.h" #include "system_ability_definition.h" #include "system_ability_manager_client_mock.h" +#include "securec.h" namespace { bool g_isLocalItDevice = false; @@ -69,6 +70,9 @@ OHOS::Storage::DistributedFile::DfsVersion g_dfsVersion; bool g_isRemoteDfsVersionLowerThanGiven = false; int32_t g_getDeviceStatus = 0; int32_t g_putDeviceStatus = 0; +bool g_checkSrcPermission = true; +bool g_getLocalAccountInfo = true; +bool g_checkSinkPermission = true; } // namespace namespace { @@ -105,14 +109,6 @@ int32_t DeviceProfileAdapter::GetDeviceStatus(const std::string &networkId, bool return g_getDeviceStatus; } -bool DeviceProfileAdapter::IsRemoteDfsVersionLowerThanGiven( - const std::string &remoteNetworkId, - const DfsVersion &thresholdDfsVersion, - VersionPackageName packageName) -{ - return g_isRemoteDfsVersionLowerThanGiven; -} - int32_t DeviceProfileAdapter::PutDeviceStatus(bool status) { return g_putDeviceStatus; @@ -122,6 +118,28 @@ bool ControlCmdParser::IsLocalItDevice() { return g_isLocalItDevice; } + +bool DeviceProfileAdapter::IsRemoteDfsVersionLowerThanGiven(const std::string &remoteNetworkId, + const DfsVersion &givenDfsVersion, + VersionPackageName packageName) +{ + return g_isRemoteDfsVersionLowerThanGiven; +} + +bool SoftBusPermissionCheck::CheckSrcPermission(const std::string &sinkNetworkId) +{ + return g_checkSrcPermission; +} + +bool SoftBusPermissionCheck::GetLocalAccountInfo(AccountInfo &localAccountInfo) +{ + return g_getLocalAccountInfo; +} + +bool SoftBusPermissionCheck::CheckSinkPermission(const AccountInfo &callerAccountInfo) +{ + return g_checkSinkPermission; +} } // namespace OHOS::Storage::DistributedFile namespace OHOS::FileManagement { @@ -271,8 +289,6 @@ void DaemonTest::SetUpTestCase(void) ISoftBusHandlerAssetMock::iSoftBusHandlerAssetMock_ = softBusHandlerAssetMock_; softBusHandlerMock_ = std::make_shared(); ISoftBusHandlerMock::iSoftBusHandlerMock_ = softBusHandlerMock_; - deviceManagerImplMock_ = std::make_shared(); - DfsDeviceManagerImpl::dfsDeviceManagerImpl = deviceManagerImplMock_; std::string path = "/mnt/hmdfs/100/account/device_view/local/data/com.example.app"; if (!std::filesystem::exists(path)) { @@ -302,10 +318,6 @@ void DaemonTest::TearDownTestCase(void) softBusHandlerAssetMock_ = nullptr; ISoftBusHandlerMock::iSoftBusHandlerMock_ = nullptr; softBusHandlerMock_ = nullptr; - deviceManagerImplMock_ = nullptr; - DfsDeviceManagerImpl::dfsDeviceManagerImpl = nullptr; - deviceManagerImplMock_ = nullptr; - DfsDeviceManagerImpl::dfsDeviceManagerImpl = nullptr; std::string path = "/mnt/hmdfs/100/account/device_view/local/data/com.example.app"; if (std::filesystem::exists(path)) { @@ -320,8 +332,6 @@ void DaemonTest::SetUp(void) bool runOnCreate = true; daemon_ = new (std::nothrow) Daemon(saID, runOnCreate); ASSERT_TRUE(daemon_ != nullptr) << "daemon_ assert failed!"; - g_getDfsVersionFromNetworkId = 0; - g_dfsVersion = {0, 0, 0}; g_isRemoteDfsVersionLowerThanGiven = false; g_getDeviceStatus = 0; g_putDeviceStatus = 0; @@ -336,6 +346,15 @@ void DaemonTest::SetUp(void) g_isLocalItDevice = false; channelManagerMock_ = std::make_shared(); IChannelManagerMock::iChannelManagerMock = channelManagerMock_; + deviceManagerImplMock_ = std::make_shared(); + DfsDeviceManagerImpl::dfsDeviceManagerImpl = deviceManagerImplMock_; + + g_getDfsVersionFromNetworkId = 0; + g_dfsVersion = {0, 0, 0}; + g_isRemoteDfsVersionLowerThanGiven = false; + g_checkSrcPermission = true; + g_getLocalAccountInfo = true; + g_checkSinkPermission = true; } void DaemonTest::TearDown(void) @@ -344,6 +363,8 @@ void DaemonTest::TearDown(void) daemon_ = nullptr; channelManagerMock_ = nullptr; IChannelManagerMock::iChannelManagerMock = nullptr; + deviceManagerImplMock_ = nullptr; + DfsDeviceManagerImpl::dfsDeviceManagerImpl = nullptr; } /** @@ -561,7 +582,7 @@ HWTEST_F(DaemonTest, DaemonTest_OpenP2PConnection_001, TestSize.Level1) GTEST_LOG_(INFO) << "DaemonTest_OpenP2PConnection_001 begin"; ASSERT_NE(daemon_, nullptr); DistributedHardware::DmDeviceInfo deviceInfo; - ConnectCount::GetInstance()->RemoveAllConnect(); + ConnectCount::GetInstance().RemoveAllConnect(); EXPECT_CALL(*deviceManagerAgentMock_, OnDeviceP2POnline(_)).WillOnce(Return(ERR_BAD_VALUE)); EXPECT_EQ(daemon_->OpenP2PConnection(deviceInfo), ERR_BAD_VALUE); @@ -582,7 +603,7 @@ HWTEST_F(DaemonTest, DaemonTest_ConnectionCount_001, TestSize.Level1) GTEST_LOG_(INFO) << "DaemonTest_ConnectionCount_001 begin"; ASSERT_NE(daemon_, nullptr); DistributedHardware::DmDeviceInfo deviceInfo; - ConnectCount::GetInstance()->RemoveAllConnect(); + ConnectCount::GetInstance().RemoveAllConnect(); EXPECT_CALL(*deviceManagerAgentMock_, OnDeviceP2POnline(_)).WillOnce(Return(ERR_BAD_VALUE)); EXPECT_EQ(daemon_->ConnectionCount(deviceInfo), ERR_BAD_VALUE); @@ -603,13 +624,21 @@ HWTEST_F(DaemonTest, DaemonTest_CleanUp_001, TestSize.Level1) GTEST_LOG_(INFO) << "DaemonTest_CleanUp_001 begin"; ASSERT_NE(daemon_, nullptr); DistributedHardware::DmDeviceInfo deviceInfo; - ConnectCount::GetInstance()->RemoveAllConnect(); + string networkId = "testNetworkId"; + strcpy_s(deviceInfo.networkId, 96, networkId.c_str()); + + ConnectCount::GetInstance().RemoveAllConnect(); EXPECT_CALL(*deviceManagerAgentMock_, OnDeviceP2POffline(_)).WillOnce(Return((E_OK))); - EXPECT_EQ(daemon_->CleanUp(deviceInfo), E_NULLPTR); + EXPECT_EQ(daemon_->CleanUp(deviceInfo), E_EVENT_HANDLER); sleep(1); EXPECT_CALL(*deviceManagerAgentMock_, OnDeviceP2POffline(_)).WillOnce(Return((ERR_BAD_VALUE))); - EXPECT_EQ(daemon_->CleanUp(deviceInfo), E_NULLPTR); + EXPECT_EQ(daemon_->CleanUp(deviceInfo), E_EVENT_HANDLER); + sleep(1); + + sptr nullListener = nullptr; + ConnectCount::GetInstance().AddConnect(333, networkId, nullListener); + EXPECT_EQ(daemon_->CleanUp(deviceInfo), E_OK); sleep(1); GTEST_LOG_(INFO) << "DaemonTest_CleanUp_001 end"; } @@ -625,7 +654,7 @@ HWTEST_F(DaemonTest, DaemonTest_ConnectionAndMount_001, TestSize.Level1) GTEST_LOG_(INFO) << "DaemonTest_ConnectionAndMount_001 begin"; DistributedHardware::DmDeviceInfo deviceInfo = {.networkId = "test"}; sptr remoteReverseObj = nullptr; - ConnectCount::GetInstance()->RemoveAllConnect(); + ConnectCount::GetInstance().RemoveAllConnect(); // g_checkCallerPermission is ok but remote reject g_checkCallerPermission = true; @@ -748,22 +777,30 @@ HWTEST_F(DaemonTest, DaemonTest_RequestSendFile_001, TestSize.Level1) g_getCallingNetworkId = "testNetWork1"; std::vector deviceList; deviceList.push_back(deviceInfo); +#ifdef SUPPORT_SAME_ACCOUNT EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)) .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0))); +#endif daemon_->eventHandler_ = nullptr; EXPECT_EQ(daemon_->RequestSendFile("", "", "", ""), E_EVENT_HANDLER); +#ifdef SUPPORT_SAME_ACCOUNT EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)) .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0))); +#endif EXPECT_EQ(daemon_->RequestSendFile("../srcUri", "", "", ""), E_ILLEGAL_URI); +#ifdef SUPPORT_SAME_ACCOUNT EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)) .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0))); +#endif EXPECT_EQ(daemon_->RequestSendFile("", "../dstUri", "", ""), E_ILLEGAL_URI); daemon_->StartEventHandler(); +#ifdef SUPPORT_SAME_ACCOUNT EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)) .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0))); +#endif EXPECT_EQ(daemon_->RequestSendFile("", "", "", ""), ERR_BAD_VALUE); GTEST_LOG_(INFO) << "DaemonTest_RequestSendFile_001 end"; } @@ -909,39 +946,53 @@ HWTEST_F(DaemonTest, DaemonTest_GetRealPath_001, TestSize.Level1) ASSERT_NE(daemon_, nullptr); std::string physicalPath; HmdfsInfo info; - EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, nullptr), E_INVAL_ARG_NAPI); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, nullptr, "networkId"), E_INVAL_ARG_NAPI); sptr daemon = new (std::nothrow) DaemonMock(); ASSERT_TRUE(daemon != nullptr) << "daemon assert failed!"; - EXPECT_EQ(daemon_->GetRealPath("../srcUri", "", physicalPath, info, daemon), E_ILLEGAL_URI); + EXPECT_EQ(daemon_->GetRealPath("../srcUri", "", physicalPath, info, daemon, "networkId"), E_ILLEGAL_URI); + + EXPECT_EQ(daemon_->GetRealPath("", "../dstUri", physicalPath, info, daemon, "networkId"), E_ILLEGAL_URI); - EXPECT_EQ(daemon_->GetRealPath("", "../dstUri", physicalPath, info, daemon), E_ILLEGAL_URI); + g_checkSrcPermission = false; + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), ERR_ACL_FAILED); + + g_checkSrcPermission = true; + g_getLocalAccountInfo = false; + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), ERR_ACL_FAILED); + + g_checkSrcPermission = true; + g_getLocalAccountInfo = true; + EXPECT_CALL(*daemon, GetRemoteCopyInfoACL(_, _, _, _)).WillOnce(Return(ERR_BAD_VALUE)); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), E_SOFTBUS_SESSION_FAILED); + + g_isRemoteDfsVersionLowerThanGiven = true; EXPECT_CALL(*daemon, GetRemoteCopyInfo(_, _, _)).WillOnce(Return(ERR_BAD_VALUE)); - EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon), E_SOFTBUS_SESSION_FAILED); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), E_SOFTBUS_SESSION_FAILED); g_getHapTokenInfo = ERR_BAD_VALUE; EXPECT_CALL(*daemon, GetRemoteCopyInfo(_, _, _)).WillOnce(Return(E_OK)); - EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon), E_GET_USER_ID); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), E_GET_USER_ID); g_getHapTokenInfo = Security::AccessToken::AccessTokenKitRet::RET_SUCCESS; g_getPhysicalPath = ERR_BAD_VALUE; EXPECT_CALL(*daemon, GetRemoteCopyInfo(_, _, _)).WillOnce(Return(E_OK)); - EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon), E_GET_PHYSICAL_PATH_FAILED); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), E_GET_PHYSICAL_PATH_FAILED); g_getPhysicalPath = E_OK; g_checkValidPath = false; info.dirExistFlag = false; EXPECT_CALL(*daemon, GetRemoteCopyInfo(_, _, _)).WillOnce(Return(E_OK)); - EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon), E_GET_PHYSICAL_PATH_FAILED); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), E_GET_PHYSICAL_PATH_FAILED); g_checkValidPath = true; g_physicalPath = "test@test/test"; info.dirExistFlag = true; std::string dstUri = "file://com.example.app/data/storage/el2/distributedfiles/images/1.png"; EXPECT_CALL(*daemon, GetRemoteCopyInfo(_, _, _)).WillOnce(Return(E_OK)); - EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon), E_OK); + EXPECT_EQ(daemon_->GetRealPath("", "", physicalPath, info, daemon, "networkId"), E_OK); GTEST_LOG_(INFO) << "DaemonTest_GetRealPath_001 end"; } @@ -1066,15 +1117,19 @@ HWTEST_F(DaemonTest, DaemonTest_GetRemoteCopyInfo_001, TestSize.Level1) (void)memcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_NAME_LEN - 1, NETWORKID_ONE.c_str(), NETWORKID_ONE.size()); deviceList.push_back(deviceInfo); +#ifdef SUPPORT_SAME_ACCOUNT EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)) .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0))); +#endif EXPECT_CALL(*softBusSessionListenerMock_, GetRealPath(_)).WillOnce(Return("")); bool isSrcFile = false; bool srcIsDir = false; EXPECT_EQ(daemon_->GetRemoteCopyInfo("", isSrcFile, srcIsDir), E_SOFTBUS_SESSION_FAILED); +#ifdef SUPPORT_SAME_ACCOUNT EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)) .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0))); +#endif EXPECT_CALL(*softBusSessionListenerMock_, GetRealPath(_)).WillOnce(Return("test")); EXPECT_EQ(daemon_->GetRemoteCopyInfo("", isSrcFile, srcIsDir), E_OK); GTEST_LOG_(INFO) << "DaemonTest_GetRemoteCopyInfo_001 end"; @@ -1147,40 +1202,50 @@ HWTEST_F(DaemonTest, DaemonTest_GetRemoteSA_001, TestSize.Level1) HWTEST_F(DaemonTest, DaemonTest_Copy_001, TestSize.Level1) { GTEST_LOG_(INFO) << "DaemonTest_Copy_001 begin"; - ASSERT_NE(daemon_, nullptr); - EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)) - .WillOnce(Return(ERR_BAD_VALUE)); - EXPECT_EQ(daemon_->Copy("", "", nullptr, ""), E_GET_DEVICE_ID); - EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)) - .WillOnce(Return(E_OK)); - EXPECT_EQ(daemon_->Copy("", "", nullptr, ""), E_INVAL_ARG_NAPI); + EXPECT_EQ(daemon_->Copy("", "", nullptr, "", "networkId"), E_INVAL_ARG_NAPI); - EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)) - .WillOnce(Return(E_OK)); sptr daemon = new (std::nothrow) DaemonMock(); ASSERT_TRUE(daemon != nullptr) << "daemon assert failed!"; - EXPECT_CALL(*daemon, RequestSendFile(_, _, _, _)).WillOnce(Return(ERR_BAD_VALUE)); - EXPECT_EQ(daemon_->Copy("", "", daemon, ""), E_SA_LOAD_FAILED); - - EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)) - .WillOnce(Return(E_OK)); - EXPECT_CALL(*daemon, RequestSendFile(_, _, _, _)).WillOnce(Return(E_OK)); - EXPECT_EQ(daemon_->Copy("", "", daemon, ""), E_OK); string srcUri = "file://docs/storage/media/100/local/files/Docs/../A/1.txt"; string destPath = "/storage/media/100/local/files/Docs/aa1/"; - EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, ""), E_INVAL_ARG); + EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, "", "networkId"), E_INVAL_ARG); srcUri = "file://docs/storage/media/100/local/files/Docs/../A/1.txt?networkid=123456"; - EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, ""), E_INVAL_ARG); + EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, "", "networkId"), E_INVAL_ARG); srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt"; destPath = "/storage/media/100/local/files/Docs/../aa1/"; - EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, ""), E_INVAL_ARG); + EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, "", "networkId"), E_INVAL_ARG); destPath = "/storage/media/100/local/files/Docs/../aa1/?networkid=123456"; - EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, ""), E_INVAL_ARG); + EXPECT_EQ(daemon_->Copy(srcUri, destPath, daemon, "", "networkId"), E_INVAL_ARG); + + EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillOnce(Return(ERR_BAD_VALUE)); + EXPECT_EQ(daemon_->Copy("", "", daemon, "", "networkId"), E_GET_DEVICE_ID); + + EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillRepeatedly(Return(E_OK)); + g_checkSrcPermission = false; + EXPECT_EQ(daemon_->Copy("", "", daemon, "", "networkId"), ERR_ACL_FAILED); + + g_checkSrcPermission = true; + g_getLocalAccountInfo = false; + EXPECT_EQ(daemon_->Copy("", "", daemon, "", "networkId"), ERR_ACL_FAILED); + + g_checkSrcPermission = true; + g_getLocalAccountInfo = true; + EXPECT_CALL(*daemon, RequestSendFileACL(_, _, _, _, _)).WillOnce(Return(ERR_BAD_VALUE)); + EXPECT_EQ(daemon_->Copy("", "", daemon, "", "networkId"), E_SA_LOAD_FAILED); + + g_isRemoteDfsVersionLowerThanGiven = true; + EXPECT_CALL(*daemon, RequestSendFile(_, _, _, _)).WillOnce(Return(ERR_BAD_VALUE)); + EXPECT_EQ(daemon_->Copy("", "", daemon, "", "networkId"), E_SA_LOAD_FAILED); + + EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillOnce(Return(E_OK)); + EXPECT_CALL(*daemon, RequestSendFile(_, _, _, _)).WillOnce(Return(E_OK)); + EXPECT_EQ(daemon_->Copy("", "", daemon, "", "networkId"), E_OK); + GTEST_LOG_(INFO) << "DaemonTest_Copy_001 end"; } @@ -1692,7 +1757,7 @@ HWTEST_F(DaemonTest, DaemonTest_RegisterFileDfsListener_001, TestSize.Level1) EXPECT_EQ(result, E_OK); // Verify listener was added - EXPECT_TRUE(ConnectCount::GetInstance()->RmFileConnect(instanceId)); + EXPECT_TRUE(ConnectCount::GetInstance().RmFileConnect(instanceId)); GTEST_LOG_(INFO) << "DaemonTest_RegisterFileDfsListener_001 end"; } @@ -2014,5 +2079,48 @@ HWTEST_F(DaemonTest, DaemonTest_UpdateDfsSwitchStatus_005, TestSize.Level1) GTEST_LOG_(INFO) << "DaemonTest_UpdateDfsSwitchStatus_005 end"; } +/** + * @tc.name: DaemonTest_RequestSendFileACL_001 + * @tc.desc: verify RequestSendFileACL. + * @tc.type: FUNC + * @tc.require: I7TDJK + */ +HWTEST_F(DaemonTest, DaemonTest_RequestSendFileACL_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DaemonTest_RequestSendFileACL_001 begin"; + + AccountInfo accountInfo; + g_checkSinkPermission = false; + EXPECT_EQ(daemon_->RequestSendFileACL("", "", "", "", accountInfo), ERR_ACL_FAILED); + + g_checkSinkPermission = true; + EXPECT_NE(daemon_->RequestSendFileACL("", "", "", "", accountInfo), ERR_ACL_FAILED); + + GTEST_LOG_(INFO) << "DaemonTest_RequestSendFileACL_001 end"; +} + +/** + * @tc.name: DaemonTest_GetRemoteCopyInfoACL_001 + * @tc.desc: verify GetRemoteCopyInfoACL. + * @tc.type: FUNC + * @tc.require: I7TDJK + */ +HWTEST_F(DaemonTest, DaemonTest_GetRemoteCopyInfoACL_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DaemonTest_GetRemoteCopyInfoACL_001 begin"; + + AccountInfo accountInfo; + bool isSrcFile = false; + bool srcIsDir = false; + + g_checkSinkPermission = false; + EXPECT_EQ(daemon_->GetRemoteCopyInfoACL("", isSrcFile, srcIsDir, accountInfo), ERR_ACL_FAILED); + + g_checkSinkPermission = true; + EXPECT_CALL(*softBusSessionListenerMock_, GetRealPath(_)).WillOnce(Return("test")); + EXPECT_NE(daemon_->GetRemoteCopyInfoACL("", isSrcFile, srcIsDir, accountInfo), ERR_ACL_FAILED); + + GTEST_LOG_(INFO) << "DaemonTest_RequestSendFileACL_001 end"; +} } // namespace Test } // namespace OHOS::Storage::DistributedFile \ No newline at end of file diff --git a/services/distributedfiledaemon/test/unittest/ipc/daemon_execute/daemon_execute_test.cpp b/services/distributedfiledaemon/test/unittest/ipc/daemon_execute/daemon_execute_test.cpp index b83428bfc..e717ae386 100644 --- a/services/distributedfiledaemon/test/unittest/ipc/daemon_execute/daemon_execute_test.cpp +++ b/services/distributedfiledaemon/test/unittest/ipc/daemon_execute/daemon_execute_test.cpp @@ -399,7 +399,7 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_PrepareSessionInner_001, TestSize.Leve ASSERT_NE(daemonExecute_, nullptr); // Test case 1: CreateSessionServer fails EXPECT_CALL(*softBusHandlerMock_, CreateSessionServer(_, _, _, _)).WillOnce(Return(-1)); - EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info), + EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info, "networkId"), E_SOFTBUS_SESSION_FAILED); GTEST_LOG_(INFO) << "DaemonExecute_PrepareSessionInner_001 end"; @@ -423,7 +423,8 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_PrepareSessionInner_002, TestSize.Leve ASSERT_NE(daemonExecute_, nullptr); // Test case 2: CreateSessionServer success but daemon is nullptr EXPECT_CALL(*softBusHandlerMock_, CreateSessionServer(_, _, _, _)).WillOnce(Return(1)); - EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info), E_INVAL_ARG_NAPI); + EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info, "networkId"), + E_INVAL_ARG_NAPI); GTEST_LOG_(INFO) << "DaemonExecute_PrepareSessionInner_002 end"; } @@ -447,7 +448,7 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_PrepareSessionInner_003, TestSize.Leve ASSERT_NE(daemonExecute_, nullptr); // Test case 3: CreateSessionServer succeeds, but authority is not media or docs EXPECT_CALL(*softBusHandlerMock_, CreateSessionServer(_, _, _, _)).WillOnce(Return(1)); - EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info), E_OK); + EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info, "networkId"), E_OK); GTEST_LOG_(INFO) << "DaemonExecute_PrepareSessionInner_003 end"; } @@ -473,7 +474,7 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_PrepareSessionInner_004, TestSize.Leve // Test case 4: CreateSessionServer succeeds, authority is media or docs, but Copy fails EXPECT_CALL(*softBusHandlerMock_, CreateSessionServer(_, _, _, _)).WillOnce(Return(1)); - EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info), E_OK); + EXPECT_EQ(daemonExecute_->PrepareSessionInner(srcUri, physicalPath, sessionName, daemon, info, "networkId"), E_OK); GTEST_LOG_(INFO) << "DaemonExecute_PrepareSessionInner_004 end"; } @@ -532,7 +533,7 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_ExecutePrepareSession_003, TestSize.Le // // Initialize the PrepareSessionData auto prepareSessionData = - std::make_shared(srcUri, physicalPath, sessionName, daemon, info, nullBlock); + std::make_shared(srcUri, physicalPath, sessionName, daemon, info, nullBlock, "networkId"); // Initialize InnerEvent auto event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PREPARE_SESSION, prepareSessionData); @@ -561,7 +562,7 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_ExecutePrepareSession_004, TestSize.Le // Initialize PrepareSessionData auto prepareSessionData = std::make_shared("test_uri", "test_path", "test_session", daemon, - info, prepareSessionBlock); + info, prepareSessionBlock, "networkId"); auto event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PREPARE_SESSION, prepareSessionData, 0); @@ -593,7 +594,7 @@ HWTEST_F(DaemonExecuteTest, DaemonExecute_ExecutePrepareSession_005, TestSize.Le // Initialize PrepareSessionData auto prepareSessionData = std::make_shared("test_uri", "test_path", "test_session", daemon, - info, prepareSessionBlock); + info, prepareSessionBlock, "networkId"); auto event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PREPARE_SESSION, prepareSessionData, 0); diff --git a/services/distributedfiledaemon/test/unittest/network/network_agent_template_test.cpp b/services/distributedfiledaemon/test/unittest/network/network_agent_template_test.cpp deleted file mode 100644 index 09ebf9857..000000000 --- a/services/distributedfiledaemon/test/unittest/network/network_agent_template_test.cpp +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Copyright (c) 2024 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 "gtest/gtest.h" -#include "network/network_agent_template.h" -#include "utils_log.h" - -namespace OHOS { -namespace Storage { -namespace DistributedFile { -namespace Test { -using namespace testing::ext; -using namespace std; - -constexpr int TEST_SESSION_ID = 10; - -class NetworkAgentTemplateTest : public testing::Test { -public: - static void SetUpTestCase(void) {}; - static void TearDownTestCase(void) {}; - void SetUp() {}; - void TearDown() {}; -}; - -/** - * @tc.name: NetworkAgentTemplateTest_Start_Stop_0100 - * @tc.desc: Verify the Start/Stop function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_Start_Stop_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_Start_Stop_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::Start(); - NetworkAgentTemplate::Stop(); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_Start_Stop_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_ConnectDeviceAsync_0100 - * @tc.desc: Verify the ConnectDeviceAsync function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_ConnectDeviceAsync_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_ConnectDeviceAsync_0100 start"; - DistributedHardware::DmDeviceInfo info = { - .deviceId = "testdevid", - .deviceName = "testdevname", - .deviceTypeId = 1, - }; - DeviceInfo devInfo(info); - bool res = true; - try { - NetworkAgentTemplate::ConnectDeviceAsync(devInfo); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_ConnectDeviceAsync_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_ConnectDeviceByP2PAsync_0100 - * @tc.desc: Verify the ConnectDeviceByP2PAsync function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_ConnectDeviceByP2PAsync_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_ConnectDeviceByP2PAsync_0100 start"; - DistributedHardware::DmDeviceInfo info = { - .deviceId = "testdevid", - .deviceName = "testdevname", - .deviceTypeId = 1, - }; - DeviceInfo devInfo(info); - bool res = true; - try { - NetworkAgentTemplate::ConnectDeviceByP2PAsync(devInfo); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_ConnectDeviceByP2PAsync_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_ConnectOnlineDevices_0100 - * @tc.desc: Verify the ConnectOnlineDevices function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_ConnectDeviceAsync_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_ConnectDeviceAsync_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::ConnectOnlineDevices(); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_ConnectDeviceAsync_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_DisconnectAllDevices_0100 - * @tc.desc: Verify the DisconnectAllDevices function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_DisconnectAllDevices_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_DisconnectAllDevices_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::DisconnectAllDevices(); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_DisconnectAllDevices_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_DisconnectDevice_0100 - * @tc.desc: Verify the DisconnectDevice function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_DisconnectDevice_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_DisconnectDevice_0100 start"; - DistributedHardware::DmDeviceInfo info = { - .deviceId = "testdevid", - .deviceName = "testdevname", - .deviceTypeId = 1, - }; - DeviceInfo devInfo(info); - bool res = true; - try { - NetworkAgentTemplate::DisconnectDevice(devInfo); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_DisconnectDevice_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_DisconnectDeviceByP2P_0100 - * @tc.desc: Verify the DisconnectDeviceByP2P function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_DisconnectDeviceByP2P_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_DisconnectDeviceByP2P_0100 start"; - DistributedHardware::DmDeviceInfo info = { - .deviceId = "testdevid", - .deviceName = "testdevname", - .deviceTypeId = 1, - }; - DeviceInfo devInfo(info); - bool res = true; - try { - NetworkAgentTemplate::DisconnectDeviceByP2P(devInfo); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_DisconnectDeviceByP2P_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_OccupySession_0100 - * @tc.desc: Verify the OccupySession function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_OccupySession_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_OccupySession_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::OccupySession(TEST_SESSION_ID, 1); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_OccupySession_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_FindSession_0100 - * @tc.desc: Verify the FindSession function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_FindSession_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_FindSession_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::FindSession(TEST_SESSION_ID); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_FindSession_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_CloseSessionForOneDevice_0100 - * @tc.desc: Verify the CloseSessionForOneDevice function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_CloseSessionForOneDevice_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_CloseSessionForOneDevice_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::CloseSessionForOneDevice("testSession"); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_CloseSessionForOneDevice_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_AcceptSession_0100 - * @tc.desc: Verify the AcceptSession function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_AcceptSession_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_AcceptSession_0100 start"; - auto session = make_shared(TEST_SESSION_ID); - bool res = true; - try { - NetworkAgentTemplate::AcceptSession(session); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_AcceptSession_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_GetSessionProcess_0100 - * @tc.desc: Verify the GetSessionProcess function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_GetSessionProcess_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_GetSessionProcess_0100 start"; - NotifyParam param; - bool res = true; - try { - NetworkAgentTemplate::GetSessionProcess(param); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_GetSessionProcess_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_GetSessionProcessInner_0100 - * @tc.desc: Verify the GetSessionProcessInner function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_GetSessionProcessInner_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_GetSessionProcessInner_0100 start"; - NotifyParam param; - bool res = true; - try { - NetworkAgentTemplate::GetSessionProcessInner(param); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_GetSessionProcessInner_0100 end"; -} - -/** - * @tc.name: NetworkAgentTemplateTest_GetSession_0100 - * @tc.desc: Verify the GetSession function. - * @tc.type: FUNC - * @tc.require: SR000H0387 - */ -HWTEST_F(NetworkAgentTemplateTest, NetworkAgentTemplateTest_GetSession_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_GetSession_0100 start"; - bool res = true; - try { - NetworkAgentTemplate::GetSession("testsession", 1); - } catch (const exception &e) { - res = false; - LOGE("%{public}s", e.what()); - } - EXPECT_TRUE(res == true); - GTEST_LOG_(INFO) << "NetworkAgentTemplateTest_GetSession_0100 end"; -} -} // namespace Test -} // namespace DistributedFile -} // namespace Storage -} // namespace OHOS diff --git a/services/distributedfiledaemon/test/unittest/network/session_pool_test.cpp b/services/distributedfiledaemon/test/unittest/network/session_pool_test.cpp index f5ece8cc1..88145f02f 100644 --- a/services/distributedfiledaemon/test/unittest/network/session_pool_test.cpp +++ b/services/distributedfiledaemon/test/unittest/network/session_pool_test.cpp @@ -319,11 +319,12 @@ HWTEST_F(SessionPoolTest, SessionPoolTest_CheckIfGetSession_0100, TestSize.Level auto kernelTalker = std::make_shared(wmp, [](NotifyParam ¶m) {}, [](const std::string &) {}); shared_ptr pool = make_shared(kernelTalker); int32_t fd = -1; // -1: fd - bool ret = pool->CheckIfGetSession(fd); + bool isServer = false; + bool ret = pool->CheckIfGetSession(fd, isServer); EXPECT_EQ(ret, false); pool->usrSpaceSessionPool_.push_back(session); - ret = pool->CheckIfGetSession(fd); + ret = pool->CheckIfGetSession(fd, isServer); EXPECT_EQ(ret, true); GTEST_LOG_(INFO) << "SessionPoolTest_CheckIfGetSession_0100 end"; } diff --git a/services/distributedfiledaemon/test/unittest/network/softbus/softbus_agent_sup_test.cpp b/services/distributedfiledaemon/test/unittest/network/softbus/softbus_agent_sup_test.cpp index 8874f85c8..a6bd44795 100644 --- a/services/distributedfiledaemon/test/unittest/network/softbus/softbus_agent_sup_test.cpp +++ b/services/distributedfiledaemon/test/unittest/network/softbus/softbus_agent_sup_test.cpp @@ -199,13 +199,13 @@ HWTEST_F(SoftbusAgentSupTest, SoftbusAgentSupTest_GetSessionProcessInner_0100, T auto session = make_shared(1, "testNetworkId"); session->SetFromServer(false); agent->sessionPool_.AddSessionToPool(session); - ConnectCount::GetInstance()->RemoveAllConnect(); + ConnectCount::GetInstance().RemoveAllConnect(); agent->GetSessionProcessInner(param); EXPECT_FALSE(agent->sessionPool_.FindCid("testNetworkId")); sptr listener = nullptr; agent->sessionPool_.AddSessionToPool(session); - ConnectCount::GetInstance()->AddConnect(1, "testNetworkId", listener); + ConnectCount::GetInstance().AddConnect(1, "testNetworkId", listener); agent->GetSessionProcessInner(param); EXPECT_FALSE(agent->sessionPool_.FindCid("testNetworkId")); diff --git a/services/distributedfiledaemon/test/unittest/network/softbus/softbus_session_listener_test.cpp b/services/distributedfiledaemon/test/unittest/network/softbus/softbus_session_listener_test.cpp index 1188b323f..0e0618a50 100644 --- a/services/distributedfiledaemon/test/unittest/network/softbus/softbus_session_listener_test.cpp +++ b/services/distributedfiledaemon/test/unittest/network/softbus/softbus_session_listener_test.cpp @@ -241,7 +241,6 @@ HWTEST_F(SoftBusSessionListenerTest, SoftBusSessionListenerTest_GetFileName_0100 rltList = SoftBusSessionListener::GetFileName(fileList, "/data/test/test1/t1.txt", "/data/test"); EXPECT_EQ(rltList.size(), 1); EXPECT_EQ(rltList[0], "t1.txt"); - rltList.clear(); rltList = SoftBusSessionListener::GetFileName(fileList, "/data/test/test1/t1.txt", "??data/test/test1/t2.txt"); EXPECT_EQ(rltList.size(), 1); diff --git a/test/fuzztest/daemonstub_fuzzer/BUILD.gn b/test/fuzztest/daemonstub_fuzzer/BUILD.gn index 67ed23b8e..d6d28bcba 100644 --- a/test/fuzztest/daemonstub_fuzzer/BUILD.gn +++ b/test/fuzztest/daemonstub_fuzzer/BUILD.gn @@ -47,6 +47,7 @@ ohos_fuzztest("DaemonStubFuzzTest") { "cJSON:cjson_static", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", diff --git a/test/fuzztest/daemonstub_fuzzer/daemonstub_fuzzer.cpp b/test/fuzztest/daemonstub_fuzzer/daemonstub_fuzzer.cpp index e32d41f33..a46d530e6 100644 --- a/test/fuzztest/daemonstub_fuzzer/daemonstub_fuzzer.cpp +++ b/test/fuzztest/daemonstub_fuzzer/daemonstub_fuzzer.cpp @@ -99,11 +99,28 @@ public: return 0; } + int32_t RequestSendFileACL(const std::string &srcUri, + const std::string &dstPath, + const std::string &remoteDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo) override + { + return 0; + } + int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override { return 0; } + int32_t GetRemoteCopyInfoACL(const std::string &srcUri, + bool &isFile, + bool &isDir, + const AccountInfo &callerAccountInfo) override + { + return 0; + } + int32_t PushAsset(int32_t userId, const sptr &assetObj, const sptr &sendCallback) override diff --git a/test/unittests/distributed_file_daemon/BUILD.gn b/test/unittests/distributed_file_daemon/BUILD.gn index 6901ce3bf..8aa6d75b3 100644 --- a/test/unittests/distributed_file_daemon/BUILD.gn +++ b/test/unittests/distributed_file_daemon/BUILD.gn @@ -250,6 +250,7 @@ ohos_unittest("daemon_manager_impl_test") { "app_file_service:remote_file_share_native", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -346,6 +347,7 @@ ohos_unittest("daemon_service_proxy_test") { "app_file_service:remote_file_share_native", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", diff --git a/test/unittests/distributed_file_daemon/daemon_stub_sup_test.cpp b/test/unittests/distributed_file_daemon/daemon_stub_sup_test.cpp index b50f81d7b..127d604f2 100644 --- a/test/unittests/distributed_file_daemon/daemon_stub_sup_test.cpp +++ b/test/unittests/distributed_file_daemon/daemon_stub_sup_test.cpp @@ -122,6 +122,12 @@ public: const std::string &dstPath, const std::string &remoteDeviceId, const std::string &sessionName)); + MOCK_METHOD5(RequestSendFileACL, + int32_t(const std::string &srcUri, + const std::string &dstPath, + const std::string &remoteDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo)); MOCK_METHOD5(PrepareSession, int32_t(const std::string &srcUri, const std::string &dstUri, @@ -131,6 +137,8 @@ public: MOCK_METHOD1(CancelCopyTask, int32_t(const std::string &sessionName)); MOCK_METHOD2(CancelCopyTask, int32_t(const std::string &srcUri, const std::string &dstUri)); MOCK_METHOD3(GetRemoteCopyInfo, int32_t(const std::string &srcUri, bool &isFile, bool &isDir)); + MOCK_METHOD4(GetRemoteCopyInfoACL, + int32_t(const std::string &srcUri, bool &isFile, bool &isDir, const AccountInfo &callerAccountInfo)); MOCK_METHOD3(PushAsset, int32_t(int32_t userId, const sptr &assetObj, const sptr &sendCallback)); @@ -180,6 +188,7 @@ void DaemonStubSupPTest::SetUp(void) { GTEST_LOG_(INFO) << "SetUp"; g_checkCallerPermissionTrue = true; + UID = DATA_UID; } void DaemonStubSupPTest::TearDown(void) @@ -876,4 +885,191 @@ HWTEST_F(DaemonStubSupPTest, DaemonStubSupHandleIsSameAccountDevice, TestSize.Le EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "DaemonStubSupHandleIsSameAccountDevice End"; } + +/** + * @tc.name: DaemonStubSupHandleRequestSendFileACLTest_0001 + * @tc.desc: Verify the HandleRequestSendFileACL function (read failures) + * @tc.type: FUNC + * @tc.require: I7M6L1 + */ +HWTEST_F(DaemonStubSupPTest, DaemonStubSupHandleRequestSendFileACLTest_0001, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "DaemonStubSupHandleRequestSendFileACLTest_0001 Start"; + MessageParcel data; + MessageParcel reply; + + g_getCallingUidTrue = true; + // Test srcUri read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(false)); + auto ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test dstPath read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(false)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test dstDeviceId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(false)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test sessionName read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(false)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test userId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(false)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + GTEST_LOG_(INFO) << "DaemonStubSupHandleRequestSendFileACLTest_0001 End"; +} + +/** + * @tc.name: DaemonStubSupHandleRequestSendFileACLTest_0002 + * @tc.desc: Verify the HandleRequestSendFileACL function (success cases) + * @tc.type: FUNC + * @tc.require: I7M6L1 + */ +HWTEST_F(DaemonStubSupPTest, DaemonStubSupHandleRequestSendFileACLTest_0002, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "DaemonStubSupHandleRequestSendFileACLTest_0002 Start"; + MessageParcel data; + MessageParcel reply; + + g_getCallingUidTrue = true; + // Test accountId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + auto ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test networkId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test success path + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + EXPECT_CALL(*daemonStub_, RequestSendFileACL(_, _, _, _, _)).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_OK); + + // Test RequestSendFileACL returns error + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + EXPECT_CALL(*daemonStub_, RequestSendFileACL(_, _, _, _, _)).WillOnce(Return(E_INVAL_ARG)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleRequestSendFileACL(data, reply); + EXPECT_EQ(ret, E_INVAL_ARG); + + GTEST_LOG_(INFO) << "DaemonStubSupHandleRequestSendFileACLTest_0002 End"; +} + +/** + * @tc.name: DaemonStubSupHandleGetRemoteCopyInfoACLTest_0001 + * @tc.desc: Verify the HandleGetRemoteCopyInfoACL function (read failures) + * @tc.type: FUNC + * @tc.require: I7M6L1 + */ +HWTEST_F(DaemonStubSupPTest, DaemonStubSupHandleGetRemoteCopyInfoACLTest_0001, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "DaemonStubSupHandleGetRemoteCopyInfoACLTest_0001 Start"; + MessageParcel data; + MessageParcel reply; + + g_getCallingUidTrue = true; + // Test srcUri read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(false)); + auto ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test userId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(false)); + ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test accountId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + // Test networkId read failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_IPC_READ_FAILED); + + GTEST_LOG_(INFO) << "DaemonStubSupHandleGetRemoteCopyInfoACLTest_0001 End"; +} + +/** + * @tc.name: DaemonStubSupHandleGetRemoteCopyInfoACLTest_0002 + * @tc.desc: Verify the HandleGetRemoteCopyInfoACL function (write and success cases) + * @tc.type: FUNC + * @tc.require: I7M6L1 + */ +HWTEST_F(DaemonStubSupPTest, DaemonStubSupHandleGetRemoteCopyInfoACLTest_0002, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "DaemonStubSupHandleGetRemoteCopyInfoACLTest_0002 Start"; + MessageParcel data; + MessageParcel reply; + + g_getCallingUidTrue = true; + // Test isFile write failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + EXPECT_CALL(*daemonStub_, GetRemoteCopyInfoACL(_, _, _, _)).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, WriteBool(_)).WillOnce(Return(false)); + auto ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_IPC_WRITE_FAILED); + + // Test isDir write failure + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + EXPECT_CALL(*daemonStub_, GetRemoteCopyInfoACL(_, _, _, _)).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, WriteBool(_)).WillOnce(Return(true)).WillOnce(Return(false)); + ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_IPC_WRITE_FAILED); + + // Test success path + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + EXPECT_CALL(*daemonStub_, GetRemoteCopyInfoACL(_, _, _, _)).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, WriteBool(_)).WillOnce(Return(true)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_OK); + + // Test GetRemoteCopyInfoACL returns error + EXPECT_CALL(*messageParcelMock_, ReadString(_)).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(true)); + EXPECT_CALL(*daemonStub_, GetRemoteCopyInfoACL(_, _, _, _)).WillOnce(Return(E_INVAL_ARG)); + EXPECT_CALL(*messageParcelMock_, WriteBool(_)).WillOnce(Return(true)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); + ret = daemonStub_->HandleGetRemoteCopyInfoACL(data, reply); + EXPECT_EQ(ret, E_INVAL_ARG); + + GTEST_LOG_(INFO) << "DaemonStubSupHandleGetRemoteCopyInfoACLTest_0002 End"; +} } // namespace OHOS::Storage::DistributedFile::Test \ No newline at end of file diff --git a/test/unittests/distributed_file_daemon/daemon_stub_test.cpp b/test/unittests/distributed_file_daemon/daemon_stub_test.cpp index 5fb980d7c..0a8179fcb 100644 --- a/test/unittests/distributed_file_daemon/daemon_stub_test.cpp +++ b/test/unittests/distributed_file_daemon/daemon_stub_test.cpp @@ -75,6 +75,12 @@ public: const std::string &dstPath, const std::string &remoteDeviceId, const std::string &sessionName)); + MOCK_METHOD5(RequestSendFileACL, + int32_t(const std::string &srcUri, + const std::string &dstPath, + const std::string &remoteDeviceId, + const std::string &sessionName, + const AccountInfo &callerAccountInfo)); MOCK_METHOD5(PrepareSession, int32_t(const std::string &srcUri, const std::string &dstUri, @@ -84,6 +90,8 @@ public: MOCK_METHOD1(CancelCopyTask, int32_t(const std::string &sessionName)); MOCK_METHOD2(CancelCopyTask, int32_t(const std::string &srcUri, const std::string &dstUri)); MOCK_METHOD3(GetRemoteCopyInfo, int32_t(const std::string &srcUri, bool &isFile, bool &isDir)); + MOCK_METHOD4(GetRemoteCopyInfoACL, + int32_t(const std::string &srcUri, bool &isFile, bool &isDir, const AccountInfo &callerAccountInfo)); MOCK_METHOD3(PushAsset, int32_t(int32_t userId, diff --git a/test/unittests/distributed_file_daemon/include/i_daemon_mock.h b/test/unittests/distributed_file_daemon/include/i_daemon_mock.h index 35ff96d25..9663a1c5a 100644 --- a/test/unittests/distributed_file_daemon/include/i_daemon_mock.h +++ b/test/unittests/distributed_file_daemon/include/i_daemon_mock.h @@ -77,11 +77,24 @@ public: return FileManagement::E_OK; } + int32_t RequestSendFileACL(const std::string &srcUri, const std::string &dstPath, + const std::string &remoteDeviceId, const std::string &sessionName, + const AccountInfo &callerAccountInfo) + { + return FileManagement::E_OK; + } + int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) { return FileManagement::E_OK; } + int32_t GetRemoteCopyInfoACL(const std::string &srcUri, bool &isFile, bool &isDir, + const AccountInfo &callerAccountInfo) + { + return FileManagement::E_OK; + } + int32_t CancelCopyTask(const std::string &sessionName) { return FileManagement::E_OK; diff --git a/test/unittests/distributed_file_inner/BUILD.gn b/test/unittests/distributed_file_inner/BUILD.gn index 7b3be49dc..be94c7ad5 100644 --- a/test/unittests/distributed_file_inner/BUILD.gn +++ b/test/unittests/distributed_file_inner/BUILD.gn @@ -52,6 +52,7 @@ ohos_unittest("distributed_file_daemon_proxy_test") { "app_file_service:remote_file_share_native", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -101,6 +102,7 @@ ohos_unittest("distributed_file_daemon_proxy_sup_test") { "app_file_service:remote_file_share_native", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -193,6 +195,7 @@ ohos_unittest("asset_recv_callback_stub_test") { external_deps = [ "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -241,6 +244,7 @@ ohos_unittest("asset_send_callback_stub_test") { external_deps = [ "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -299,6 +303,7 @@ ohos_unittest("file_copy_manager_test") { "c_utils:utils", "data_share:datashare_consumer", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -363,6 +368,7 @@ ohos_unittest("remote_file_copy_manager_test") { "c_utils:utils", "data_share:datashare_consumer", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -428,6 +434,7 @@ ohos_unittest("trans_listener_test") { "c_utils:utils", "data_share:datashare_consumer", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -492,6 +499,7 @@ ohos_unittest("file_size_utils_test") { "c_utils:utils", "data_share:datashare_consumer", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -543,6 +551,7 @@ ohos_unittest("file_copy_listener_test") { external_deps = [ "ability_base:zuri", "app_file_service:fileuri_native", + "dsoftbus:softbus_client", "c_utils:utils", "googletest:gmock_main", "googletest:gtest_main", @@ -596,6 +605,7 @@ ohos_unittest("file_mount_manager_test") { "app_file_service:remote_file_share_native", "c_utils:utils", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", @@ -695,6 +705,7 @@ ohos_unittest("ipc_wrapper_test") { "c_utils:utils", "data_share:datashare_consumer", "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", diff --git a/test/unittests/distributed_file_inner/copy/file_copy_manager_test.cpp b/test/unittests/distributed_file_inner/copy/file_copy_manager_test.cpp index 5958f77b1..f22271b60 100644 --- a/test/unittests/distributed_file_inner/copy/file_copy_manager_test.cpp +++ b/test/unittests/distributed_file_inner/copy/file_copy_manager_test.cpp @@ -80,23 +80,23 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0001, TestSize.Level0) string localUri = "/data/test/test.txt"; string dstUri = "/data/test/test.txt"; - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy("", localUri, listener_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy("", localUri, listener_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, "", listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(localUri, "", listener_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy("", "", listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy("", "", listener_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, dstUri, listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(localUri, dstUri, listener_); EXPECT_EQ(ret, EINVAL); string remoteUri = "/data/test/Copy/?networkid=/"; if (!ForceCreateDirectory(remoteUri)) { GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 create dir err"; } - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(remoteUri, "", listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(remoteUri, "", listener_); EXPECT_EQ(ret, EINVAL); if (!ForceRemoveDirectory(remoteUri)) { GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 remove dir err"; @@ -121,7 +121,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0002, TestSize.Level0) ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0002!" << errno; close(fd); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(srcUri, destUri, listener_); EXPECT_EQ(ret, E_OK); ASSERT_EQ(remove(srcPath.c_str()), 0); ASSERT_EQ(remove(dstPath.c_str()), 0); @@ -145,7 +145,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0003, TestSize.Level0) ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0003!" << errno; close(fd); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(srcUri, destUri, listener_); EXPECT_EQ(ret, EINVAL); ASSERT_EQ(remove(srcPath.c_str()), 0); GTEST_LOG_(INFO) << "FileCopyManager_Copy_0003 End"; @@ -168,7 +168,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0004, TestSize.Level0) ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0004!" << errno; close(fd); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(srcUri, destUri, listener_); EXPECT_EQ(ret, E_OK); ASSERT_EQ(remove(srcPath.c_str()), 0); ASSERT_EQ(remove(destPath.c_str()), 0); @@ -187,16 +187,16 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0005, TestSize.Level0) string localUri = "/data/test/test.txt"; string dstUri = "/data/test/test.txt"; - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy("", localUri, emptyCallback_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy("", localUri, emptyCallback_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, "", listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(localUri, "", listener_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, dstUri, listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(localUri, dstUri, listener_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, dstUri, emptyCallback_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(localUri, dstUri, emptyCallback_); EXPECT_EQ(ret, EINVAL); GTEST_LOG_(INFO) << "FileCopyManager_Copy_0005 End"; @@ -216,10 +216,10 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0006, TestSize.Level0) string srcPath = "/storage/media/100/local/files/Docs/1.txt"; EXPECT_TRUE(OHOS::RemoveFile(srcPath)); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, emptyCallback_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(srcUri, destUri, emptyCallback_); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(srcUri, destUri, listener_); EXPECT_EQ(ret, ENOENT); GTEST_LOG_(INFO) << "FileCopyManager_Copy_0006 End"; @@ -239,7 +239,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0007, TestSize.Level0) string srcPath = "/storage/External/mtp/1.txt"; EXPECT_TRUE(OHOS::RemoveFile(srcPath)); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, emptyCallback_); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Copy(srcUri, destUri, emptyCallback_); EXPECT_EQ(ret, EINVAL); GTEST_LOG_(INFO) << "FileCopyManager_Copy_0007 End"; @@ -259,14 +259,14 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0001, TestSize.Level0) string srcpath = "/storage/media/100/local/files/Docs/11.txt"; string destpath = "/storage/media/100/local/files/Docs/dest11.txt"; // infos is nullptr - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(nullptr); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(nullptr); EXPECT_EQ(ret, EINVAL); auto infos = std::make_shared(); infos->srcUri = srcuri; infos->destUri = desturi; // infos localListener is nullptr - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, EINVAL); infos->srcUriIsFile = true; @@ -275,18 +275,18 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0001, TestSize.Level0) infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath, infos->srcUriIsFile, listener_); // srcPath and destPath is same - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, EINVAL); infos->destPath = destpath; // src file not exist - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, ENOENT); int fd = open(srcpath.c_str(), O_RDWR | O_CREAT); ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_ExecLocal_0001" << errno; close(fd); - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, E_OK); EXPECT_TRUE(std::filesystem::exists("/storage/media/100/local/files/Docs/dest11.txt")); ASSERT_EQ(remove(srcpath.c_str()), 0); @@ -295,7 +295,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0001, TestSize.Level0) infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath, infos->srcUriIsFile, emptyCallback_); // src file not exist, callback is nullptr - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, ENOENT); ASSERT_EQ(remove(destpath.c_str()), 0); GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0001 End"; @@ -317,7 +317,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0002, TestSize.Level0) string destpath = "/storage/media/100/local/files/Docs/dstaa11/"; std::error_code errCode; if (!std::filesystem::exists(srcpath, errCode) && errCode.value() == E_OK) { - int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath); + int res = Storage::DistributedFile::FileCopyManager::GetInstance().MakeDir(srcpath); if (res != E_OK) { GTEST_LOG_(INFO) <<"Failed to mkdir"; } @@ -333,16 +333,16 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0002, TestSize.Level0) ProcessCallback processCallback = [](uint64_t processSize, uint64_t totalSize) -> void {}; infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath, infos->srcUriIsFile, processCallback); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); // destpath not exist EXPECT_EQ(ret, ENOENT); if (!std::filesystem::exists(destpath, errCode) && errCode.value() == E_OK) { - int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(destpath); + int res = Storage::DistributedFile::FileCopyManager::GetInstance().MakeDir(destpath); EXPECT_EQ(res, E_OK); } // dir copy to dir - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, 0); ASSERT_EQ(remove(srcpath.c_str()), 0); @@ -365,7 +365,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0003, TestSize.Level0) string destpath = "/storage/media/100/local/files/Docs/dstaa12/"; std::error_code errCode; if (!std::filesystem::exists(srcpath, errCode) && errCode.value() == E_OK) { - int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath); + int res = Storage::DistributedFile::FileCopyManager::GetInstance().MakeDir(srcpath); if (res != E_OK) { GTEST_LOG_(INFO) <<"Failed to mkdir"; } @@ -383,7 +383,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0003, TestSize.Level0) infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath, infos->srcUriIsFile, processCallback); // srcUriIsFile is true, destpath not exist - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_NE(ret, E_OK); ASSERT_EQ(ForceRemoveDirectory(srcpath), true); if (!ForceRemoveDirectory(destpath)) { @@ -417,10 +417,10 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0004, TestSize.Level0) infos->srcUriIsFile, processCallback); if (!std::filesystem::exists(destpath, errCode) && errCode.value() == E_OK) { - int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(destpath); + int res = Storage::DistributedFile::FileCopyManager::GetInstance().MakeDir(destpath); EXPECT_EQ(res, E_OK); } - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); // src path not exist EXPECT_EQ(ret, E_OK); ASSERT_EQ(ForceRemoveDirectory(destpath.c_str()), true); @@ -446,7 +446,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0005, TestSize.Level0) std::error_code errCode; if (!std::filesystem::exists(destpath, errCode) && errCode.value() == E_OK) { - int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(destpath); + int res = Storage::DistributedFile::FileCopyManager::GetInstance().MakeDir(destpath); ASSERT_EQ(res, E_OK); } @@ -459,7 +459,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0005, TestSize.Level0) infos->destPath = destpath; infos->srcUriIsFile = true; // file to dir - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, EINVAL); ASSERT_EQ(remove(srcpath.c_str()), 0); EXPECT_TRUE(ForceRemoveDirectory(destpath)); @@ -488,7 +488,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0006, TestSize.Level0) infos->srcUriIsFile, listener_); // destpath is invalid - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().ExecLocal(infos); EXPECT_EQ(ret, ENOENT); GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0006 End"; } @@ -512,7 +512,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0001, TestSize.Level EXPECT_TRUE(std::filesystem::exists(infos->destPath)); // 调用 DeleteResFile - Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos); + Storage::DistributedFile::FileCopyManager::GetInstance().DeleteResFile(infos); // 验证文件是否被删除 EXPECT_FALSE(std::filesystem::exists(infos->destPath)); @@ -537,7 +537,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0002, TestSize.Level EXPECT_FALSE(std::filesystem::exists(infos->destPath)); // 调用 DeleteResFile - Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos); + Storage::DistributedFile::FileCopyManager::GetInstance().DeleteResFile(infos); // 验证文件仍然不存在 EXPECT_FALSE(std::filesystem::exists(infos->destPath)); @@ -574,7 +574,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0003, TestSize.Level }; // 调用 DeleteResFile - Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos); + Storage::DistributedFile::FileCopyManager::GetInstance().DeleteResFile(infos); // 验证文件和目录是否被删除 EXPECT_FALSE(std::filesystem::exists("/data/test/file1.txt")); @@ -583,7 +583,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0003, TestSize.Level // 调用 DeleteResFile infos->localListener = nullptr; - Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos); + Storage::DistributedFile::FileCopyManager::GetInstance().DeleteResFile(infos); GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0003 End"; } @@ -609,7 +609,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0004, TestSize.Level }; // 调用 DeleteResFile - Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos); + Storage::DistributedFile::FileCopyManager::GetInstance().DeleteResFile(infos); // 验证文件和目录仍然不存在 EXPECT_FALSE(std::filesystem::exists("/data/test/nonexistent_file.txt")); @@ -637,7 +637,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0001, TestSize.Level0) int32_t srcFd = -1; // Call the OpenSrcFile function - int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd); + int ret = Storage::DistributedFile::FileCopyManager::GetInstance().OpenSrcFile(infos->srcPath, infos, srcFd); // Validate the file descriptor is opened successfully EXPECT_EQ(ret, 1); @@ -667,7 +667,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0002, TestSize.Level0) std::ofstream(infos->srcPath).close(); // Call the OpenSrcFile function - int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd); + int ret = Storage::DistributedFile::FileCopyManager::GetInstance().OpenSrcFile(infos->srcPath, infos, srcFd); // Validate the file descriptor is opened successfully EXPECT_EQ(ret, 1); @@ -696,7 +696,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0003, TestSize.Level0) // Call the OpenSrcFile function int32_t srcFd = -1; - int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd); + int ret = Storage::DistributedFile::FileCopyManager::GetInstance().OpenSrcFile(infos->srcPath, infos, srcFd); // Validate that the function fails when file does not exist EXPECT_NE(ret, 0); // Expect error code @@ -727,7 +727,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0004, TestSize.Level0) // Call the OpenSrcFile function int32_t srcFd = -1; - int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd); + int ret = Storage::DistributedFile::FileCopyManager::GetInstance().OpenSrcFile(infos->srcPath, infos, srcFd); // Validate the file descriptor is opened successfully EXPECT_EQ(ret, 0); @@ -750,7 +750,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_CheckOrCreateLPath_0001, TestSize. GTEST_LOG_(INFO) << "FileCopyManager_CheckOrCreateLPath_0001 Start"; std::string destPath = ""; - int32_t ret = Storage::DistributedFile::FileCopyManager::GetInstance()->CheckOrCreatePath(destPath); + int32_t ret = Storage::DistributedFile::FileCopyManager::GetInstance().CheckOrCreatePath(destPath); EXPECT_EQ(ret, FILE_NOT_FOUND); GTEST_LOG_(INFO) << "FileCopyManager_CheckOrCreateLPath_0001 End"; @@ -774,22 +774,22 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_RemoveFileInfos_0001, TestSize.Lev infos2->destUri = "destUri2"; infos3->srcUri = "srcUri1"; infos3->destUri = "destUri2"; - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.clear(); - Storage::DistributedFile::FileCopyManager::GetInstance()->AddFileInfos(infos1); - Storage::DistributedFile::FileCopyManager::GetInstance()->AddFileInfos(infos2); - auto len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size(); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.clear(); + Storage::DistributedFile::FileCopyManager::GetInstance().AddFileInfos(infos1); + Storage::DistributedFile::FileCopyManager::GetInstance().AddFileInfos(infos2); + auto len = Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 2); // 2: vector size - Storage::DistributedFile::FileCopyManager::GetInstance()->RemoveFileInfos(infos2); - len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size(); + Storage::DistributedFile::FileCopyManager::GetInstance().RemoveFileInfos(infos2); + len = Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 1); // 1: vector size - Storage::DistributedFile::FileCopyManager::GetInstance()->RemoveFileInfos(infos3); - len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size(); + Storage::DistributedFile::FileCopyManager::GetInstance().RemoveFileInfos(infos3); + len = Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 1); // 1: vector size - Storage::DistributedFile::FileCopyManager::GetInstance()->RemoveFileInfos(infos1); - len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size(); + Storage::DistributedFile::FileCopyManager::GetInstance().RemoveFileInfos(infos1); + len = Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 0); // 0: vector size } @@ -805,7 +805,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_RecurCopyDir_0001, TestSize.Level0 auto infos = std::make_shared(); std::string srcPath = ""; std::string dstPath = ""; - auto res = Storage::DistributedFile::FileCopyManager::GetInstance()->RecurCopyDir(srcPath, dstPath, infos); + auto res = Storage::DistributedFile::FileCopyManager::GetInstance().RecurCopyDir(srcPath, dstPath, infos); EXPECT_EQ(res, E_OK); srcPath = "/data/test/RecurCopyDir/src/"; @@ -831,11 +831,11 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_RecurCopyDir_0001, TestSize.Level0 } infos->localListener = std::make_shared("", true, [](uint64_t processSize, uint64_t totalSize) -> void {}); - res = Storage::DistributedFile::FileCopyManager::GetInstance()->RecurCopyDir(srcPath, dstPath, infos); + res = Storage::DistributedFile::FileCopyManager::GetInstance().RecurCopyDir(srcPath, dstPath, infos); EXPECT_EQ(res, E_OK); dstPath = dstPath + "dir/dir1/dir2"; - res = Storage::DistributedFile::FileCopyManager::GetInstance()->RecurCopyDir(srcPath, dstPath, infos); + res = Storage::DistributedFile::FileCopyManager::GetInstance().RecurCopyDir(srcPath, dstPath, infos); EXPECT_EQ(res, ENOENT); std::string rootPath = "/data/test/RecurCopyDir"; @@ -859,17 +859,17 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_CopySubDir_0001, TestSize.Level0) std::string dstPath = "/data/test/CopySubDir/dst"; infos->localListener = std::make_shared("", true, [](uint64_t processSize, uint64_t totalSize) -> void {}); - auto res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopySubDir(srcPath, dstPath, infos); + auto res = Storage::DistributedFile::FileCopyManager::GetInstance().CopySubDir(srcPath, dstPath, infos); EXPECT_EQ(res, ENOENT); if (!ForceCreateDirectory(dstPath)) { GTEST_LOG_(INFO) << "FileCopyManager_CopySubDir_0001 create dir err" << dstPath; } - res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopySubDir(srcPath, dstPath, infos); + res = Storage::DistributedFile::FileCopyManager::GetInstance().CopySubDir(srcPath, dstPath, infos); EXPECT_EQ(res, E_OK); std::string tmpDstDir = dstPath + "/dir"; - res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopySubDir(srcPath, tmpDstDir, infos); + res = Storage::DistributedFile::FileCopyManager::GetInstance().CopySubDir(srcPath, tmpDstDir, infos); EXPECT_EQ(res, E_OK); std::string rootPath = "/data/test/CopySubDir"; if (!ForceRemoveDirectory(rootPath)) { @@ -892,18 +892,18 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_CopyDirFunc_0001, TestSize.Level0) std::string dstPath = "/data/test/CopyDirFunc/dst"; infos->localListener = std::make_shared("", true, [](uint64_t processSize, uint64_t totalSize) -> void {}); - auto res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopyDirFunc(srcPath, dstPath, infos); + auto res = Storage::DistributedFile::FileCopyManager::GetInstance().CopyDirFunc(srcPath, dstPath, infos); EXPECT_EQ(res, EINVAL); std::string tmpSrcPath = "/test/CopyDirFunc/"; - res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopyDirFunc(tmpSrcPath, dstPath, infos); + res = Storage::DistributedFile::FileCopyManager::GetInstance().CopyDirFunc(tmpSrcPath, dstPath, infos); EXPECT_EQ(res, ENOENT); srcPath += "src"; if (!ForceCreateDirectory(dstPath)) { GTEST_LOG_(INFO) << "FileCopyManager_CopyDirFunc_0001 create dir err" << dstPath; } - res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopyDirFunc(srcPath, dstPath, infos); + res = Storage::DistributedFile::FileCopyManager::GetInstance().CopyDirFunc(srcPath, dstPath, infos); EXPECT_EQ(res, E_OK); std::string rootPath = "/data/test/CopyDirFunc"; if (!ForceRemoveDirectory(rootPath)) { @@ -933,9 +933,9 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0001, TestSize.Level0) infos2->transListener = sptr(new (std::nothrow) TransListener("/data/test/test.txt", emptyCallback_)); infos2->localListener = std::make_shared("", true, [](uint64_t processSize, uint64_t totalSize) -> void {}); - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1); - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos2); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos1); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos2); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(); EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0001 End"; } @@ -958,8 +958,8 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0002, TestSize.Level0) std::string srcUri = "srcUri1"; std::string destUri = "destUri1"; - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri, true); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos1); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(srcUri, destUri, true); EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0002 End"; } @@ -985,9 +985,9 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0003, TestSize.Level0) infos2->transListener = sptr(new (std::nothrow) TransListener("/data/test/test.txt", emptyCallback_)); infos2->localListener = std::make_shared("", true, [](uint64_t processSize, uint64_t totalSize) -> void {}); - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1); - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos2); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(true); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos1); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos2); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(true); EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0003 End"; } @@ -1010,24 +1010,24 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0004, TestSize.Level0) std::string srcUri = "srcUri1"; std::string destUri = "destUri3"; - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1); - auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos1); + auto ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); srcUri = "srcUri2"; destUri = "destUri1"; - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); srcUri = "srcUri2"; destUri = "destUri2"; - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); srcUri = "srcUri1"; destUri = "destUri1"; - ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri); + ret = Storage::DistributedFile::FileCopyManager::GetInstance().Cancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); - Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.clear(); + Storage::DistributedFile::FileCopyManager::GetInstance().FileInfosVec_.clear(); GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0004 End"; } @@ -1042,7 +1042,7 @@ HWTEST_F(FileCopyManagerTest, FileCopyManager_MakeDir_0001, TestSize.Level1) GTEST_LOG_(INFO) << "FileCopyManager_MakeDir_0001"; string srcpath = "/storage/media/100/local/files/Docs/bb/"; std::error_code errCode; - int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath); + int res = Storage::DistributedFile::FileCopyManager::GetInstance().MakeDir(srcpath); EXPECT_EQ(res, E_OK); ASSERT_EQ(ForceRemoveDirectory(srcpath.c_str()), true); GTEST_LOG_(INFO) << "FileCopyManager_MakeDir_0001 End"; diff --git a/test/unittests/distributed_file_inner/copy/remote_file_copy_manager_test.cpp b/test/unittests/distributed_file_inner/copy/remote_file_copy_manager_test.cpp index 4ee604018..57662dc48 100644 --- a/test/unittests/distributed_file_inner/copy/remote_file_copy_manager_test.cpp +++ b/test/unittests/distributed_file_inner/copy/remote_file_copy_manager_test.cpp @@ -1,17 +1,17 @@ /* -* Copyright (c) 2024-2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright (c) 2024-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 "copy/remote_file_copy_manager.h" @@ -39,7 +39,8 @@ int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::st } } // namespace OHOS::AppFileService -namespace OHOS::Storage::DistributedFile::Test { +namespace OHOS::Test { +using namespace OHOS::Storage::DistributedFile; using namespace OHOS::FileManagement; using namespace testing; using namespace testing::ext; @@ -83,10 +84,10 @@ void RemoteFileCopyManagerTest::TearDown(void) } /** -* @tc.name: RemoteFileCopyManager_Copy_0001 -* @tc.desc: The execution of the Copy failed. -* @tc.type: FUNC -* @tc.require: I7TDJK + * @tc.name: RemoteFileCopyManager_Copy_0001 + * @tc.desc: The execution of the Copy failed. + * @tc.type: FUNC + * @tc.require: I7TDJK */ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0001, TestSize.Level0) { @@ -98,20 +99,20 @@ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0001, TestSize.Le sptr listenerCallback; - auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy("", localUri, listenerCallback, userId, copyPath); + auto ret = RemoteFileCopyManager::GetInstance().RemoteCopy("", localUri, listenerCallback, userId, copyPath); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(localUri, "", listenerCallback, userId, copyPath); + ret = RemoteFileCopyManager::GetInstance().RemoteCopy(localUri, "", listenerCallback, userId, copyPath); EXPECT_EQ(ret, EINVAL); - ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy("", "", listenerCallback, userId, copyPath); + ret = RemoteFileCopyManager::GetInstance().RemoteCopy("", "", listenerCallback, userId, copyPath); EXPECT_EQ(ret, EINVAL); string remoteUri = "/data/test/Copy/?networkid=/"; if (!ForceCreateDirectory(remoteUri)) { GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0001 create dir err"; } - ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(remoteUri, "", listenerCallback, userId, copyPath); + ret = RemoteFileCopyManager::GetInstance().RemoteCopy(remoteUri, "", listenerCallback, userId, copyPath); EXPECT_EQ(ret, EINVAL); if (!ForceRemoveDirectory(remoteUri)) { GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0001 remove dir err"; @@ -120,10 +121,10 @@ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0001, TestSize.Le } /** -* @tc.name: RemoteFileCopyManager_Copy_0002 -* @tc.desc: The execution of the Copy succeed. -* @tc.type: FUNC -* @tc.require: I7TDJK + * @tc.name: RemoteFileCopyManager_Copy_0002 + * @tc.desc: The execution of the Copy succeed. + * @tc.type: FUNC + * @tc.require: I7TDJK */ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0002, TestSize.Level0) { @@ -136,20 +137,20 @@ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0002, TestSize.Le sptr listenerCallback; int fd = open(srcPath.c_str(), O_RDWR | O_CREAT); - ASSERT_TRUE(fd != -1) <<"Failed to open file in RemoteFileCopyManager_Copy_0002!" << errno; + ASSERT_TRUE(fd != -1) << "Failed to open file in RemoteFileCopyManager_Copy_0002!" << errno; close(fd); - auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(srcUri, destUri, listenerCallback, userId, copyPath); + auto ret = RemoteFileCopyManager::GetInstance().RemoteCopy(srcUri, destUri, listenerCallback, userId, copyPath); EXPECT_EQ(ret, ENOENT); ASSERT_EQ(remove(srcPath.c_str()), 0); GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0002 End"; } /** -* @tc.name: RemoteFileCopyManager_RemoteCancel_0001 -* @tc.desc: The execution of the cancel succeed. -* @tc.type: FUNC -* @tc.require: I7TDJK + * @tc.name: RemoteFileCopyManager_RemoteCancel_0001 + * @tc.desc: The execution of the cancel succeed. + * @tc.type: FUNC + * @tc.require: I7TDJK */ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_RemoteCancel_0001, TestSize.Level0) { @@ -158,37 +159,37 @@ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_RemoteCancel_0001, Tes infos1->srcUri = "srcUri1"; infos1->destUri = "destUri1"; infos1->transListener = nullptr; - infos1->localListener = std::make_shared("", - true, [](uint64_t processSize, uint64_t totalSize) -> void {}); + infos1->localListener = + std::make_shared("", true, [](uint64_t processSize, uint64_t totalSize) -> void {}); std::string srcUri = "srcUri1"; std::string destUri = "destUri3"; - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1); - auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri); + RemoteFileCopyManager::GetInstance().FileInfosVec_.emplace_back(infos1); + auto ret = RemoteFileCopyManager::GetInstance().RemoteCancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); srcUri = "srcUri2"; destUri = "destUri1"; - ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri); + ret = RemoteFileCopyManager::GetInstance().RemoteCancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); srcUri = "srcUri2"; destUri = "destUri2"; - ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri); + ret = RemoteFileCopyManager::GetInstance().RemoteCancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); srcUri = "srcUri1"; destUri = "destUri1"; - ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri); + ret = RemoteFileCopyManager::GetInstance().RemoteCancel(srcUri, destUri); EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "RemoteFileCopyManager_RemoteCancel_0001 End"; } /** -* @tc.name: RemoteFileCopyManager_RemoveFileInfos_0001 -* @tc.desc: RemoveFileInfos -* @tc.type: FUNC -* @tc.require: I7TDJK -*/ + * @tc.name: RemoteFileCopyManager_RemoveFileInfos_0001 + * @tc.desc: RemoveFileInfos + * @tc.type: FUNC + * @tc.require: I7TDJK + */ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_RemoveFileInfos_0001, TestSize.Level0) { GTEST_LOG_(INFO) << "RemoteFileCopyManager_RemoveFileInfos_0001 Start"; @@ -201,31 +202,31 @@ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_RemoveFileInfos_0001, infos2->destUri = "destUri2"; infos3->srcUri = "srcUri1"; infos3->destUri = "destUri2"; - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.clear(); - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->AddFileInfos(infos1); - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->AddFileInfos(infos2); - auto len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size(); + RemoteFileCopyManager::GetInstance().FileInfosVec_.clear(); + RemoteFileCopyManager::GetInstance().AddFileInfos(infos1); + RemoteFileCopyManager::GetInstance().AddFileInfos(infos2); + auto len = RemoteFileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 2); // 2: vector size - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoveFileInfos(infos2); - len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size(); + RemoteFileCopyManager::GetInstance().RemoveFileInfos(infos2); + len = RemoteFileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 1); // 1: vector size - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoveFileInfos(infos3); - len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size(); + RemoteFileCopyManager::GetInstance().RemoveFileInfos(infos3); + len = RemoteFileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 1); // 1: vector size - Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoveFileInfos(infos1); - len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size(); + RemoteFileCopyManager::GetInstance().RemoveFileInfos(infos1); + len = RemoteFileCopyManager::GetInstance().FileInfosVec_.size(); EXPECT_EQ(len, 0); // 0: vector size } /** -* @tc.name: RemoteFileCopyManager_CreateFileInfos_0001 -* @tc.desc: CreateFileInfos -* @tc.type: FUNC -* @tc.require: I7TDJK -*/ + * @tc.name: RemoteFileCopyManager_CreateFileInfos_0001 + * @tc.desc: CreateFileInfos + * @tc.type: FUNC + * @tc.require: I7TDJK + */ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_CreateFileInfos_0001, TestSize.Level0) { GTEST_LOG_(INFO) << "RemoteFileCopyManager_CreateFileInfos_0001 Start"; @@ -233,23 +234,23 @@ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_CreateFileInfos_0001, infos->srcUri = ""; int32_t userId = 100; string copyPath = "/data/storage/el2/distributedfiles/123412345/test.txt"; - int32_t ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->CreateFileInfos("", "", infos, userId, copyPath); + int32_t ret = RemoteFileCopyManager::GetInstance().CreateFileInfos("", "", infos, userId, copyPath); EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "RemoteFileCopyManager_CreateFileInfos_0001 End"; } /** -* @tc.name: FileCopyManager_Cancel_0001 -* @tc.desc: The execution of the cancel succeed. -* @tc.type: FUNC -* @tc.require: I7TDJK + * @tc.name: FileCopyManager_Cancel_0001 + * @tc.desc: The execution of the cancel succeed. + * @tc.type: FUNC + * @tc.require: I7TDJK */ HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_IsMediaUri_0001, TestSize.Level0) { GTEST_LOG_(INFO) << "RemoteFileCopyManager_IsMediaUri_0001 Start"; string srcUri = "file://docs/storage/media/100/local/files/Docs/test.txt"; - int32_t ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->IsMediaUri(srcUri); + int32_t ret = RemoteFileCopyManager::GetInstance().IsMediaUri(srcUri); EXPECT_EQ(ret, false); GTEST_LOG_(INFO) << "RemoteFileCopyManager_IsMediaUri_0001 End"; } -} \ No newline at end of file +} // namespace OHOS::Test \ No newline at end of file diff --git a/test/unittests/distributed_file_inner/distributed_file_daemon_proxy_test.cpp b/test/unittests/distributed_file_inner/distributed_file_daemon_proxy_test.cpp index 08a4039d0..0a6b6f2c3 100644 --- a/test/unittests/distributed_file_inner/distributed_file_daemon_proxy_test.cpp +++ b/test/unittests/distributed_file_inner/distributed_file_daemon_proxy_test.cpp @@ -83,30 +83,29 @@ public: void DistributedFileDaemonProxyTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - mock_ = sptr(new DaemonServiceMock()); - proxy_ = make_shared(mock_); - messageParcelMock_ = make_shared(); - MessageParcelMock::messageParcel = messageParcelMock_; } void DistributedFileDaemonProxyTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; - mock_ = nullptr; - proxy_ = nullptr; - messageParcelMock_ = nullptr; - MessageParcelMock::messageParcel = nullptr; } void DistributedFileDaemonProxyTest::SetUp(void) { - ASSERT_TRUE(mock_ != nullptr); GTEST_LOG_(INFO) << "SetUp"; + mock_ = sptr(new DaemonServiceMock()); + proxy_ = make_shared(mock_); + messageParcelMock_ = make_shared(); + MessageParcelMock::messageParcel = messageParcelMock_; } void DistributedFileDaemonProxyTest::TearDown(void) { GTEST_LOG_(INFO) << "TearDown"; + mock_ = nullptr; + proxy_ = nullptr; + messageParcelMock_ = nullptr; + MessageParcelMock::messageParcel = nullptr; } /** @@ -885,27 +884,27 @@ HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_GetDfsUrisDirFrom EXPECT_EQ(ret, E_BROKEN_IPC); EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true)); - EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_INVAL_ARG)); ret = proxy_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps); EXPECT_EQ(ret, E_BROKEN_IPC); EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true)); - EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK)); EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(Return(false)); ret = proxy_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps); EXPECT_EQ(ret, E_BROKEN_IPC); EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true)); - EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK)); EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(DoAll(SetArgReferee<0>(E_INVAL_ARG), Return(true))); ret = proxy_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps); EXPECT_EQ(ret, E_INVAL_ARG); EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true)); - EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK)); EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(DoAll(SetArgReferee<0>(E_OK), Return(true))); ret = proxy_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps); @@ -930,7 +929,7 @@ HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_GetDfsUrisDirFrom g_context = "aa"; EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true)); - EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK)); EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(DoAll(SetArgReferee<0>(E_OK), Return(true))); auto ret = proxy_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps); @@ -938,7 +937,7 @@ HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_GetDfsUrisDirFrom g_context = "1"; EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true)); - EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true)); EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK)); EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).WillOnce(DoAll(SetArgReferee<0>(E_OK), Return(true))); g_readBatchUris = E_INVAL_ARG; @@ -1288,4 +1287,227 @@ HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_IsSameAccountDevi EXPECT_EQ(ret, E_OK); GTEST_LOG_(INFO) << "DistributedFileDaemon_IsSameAccountDevice End"; } + +/** + * @tc.name: DistributedFileDaemon_RequestSendFileACL_0100 + * @tc.desc: Verify RequestSendFileACL for parameter serialization failures. + * @tc.type: FUNC + * @tc.require: I7TDJK + */ +HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_RequestSendFileACL_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DistributedFileDaemon_RequestSendFileACL_0100 Start"; + AccountInfo accountInfo{123, 0, "account_id", "network_id"}; + + // Test WriteInterfaceToken failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(false)); + auto ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_BROKEN_IPC); + + // Test WriteString(srcUri) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(1).WillOnce(Return(false)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(dstPath) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(false)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(remoteDeviceId) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(false)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(sessionName) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(4).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(false)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteInt32(userId_) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(4).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(false)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(accountId_) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(5).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(networkId_) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(6).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + GTEST_LOG_(INFO) << "DistributedFileDaemon_RequestSendFileACL_0100 End"; +} + +/** + * @tc.name: DistributedFileDaemon_RequestSendFileACL_0200 + * @tc.desc: Verify RequestSendFileACL for remote object, IPC, and reply read failures, and success case. + * @tc.type: FUNC + * @tc.require: I7TDJK + */ +HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_RequestSendFileACL_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DistributedFileDaemon_RequestSendFileACL_0200 Start"; + AccountInfo accountInfo{123, 0, "account_id", "network_id"}; + + // Test remote object null + auto testProxy = make_shared(nullptr); + auto ret = testProxy->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_BROKEN_IPC); + + // Test SendRequest failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(6).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_INVAL_ARG)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_BROKEN_IPC); + + // Test success case + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(6).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, ReadInt32()).Times(1).WillOnce(Return(E_OK)); + ret = proxy_->RequestSendFileACL("uri", "path", "deviceId", "test", accountInfo); + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "DistributedFileDaemon_RequestSendFileACL_0200 End"; +} + +/** + * @tc.name: DistributedFileDaemon_GetRemoteCopyInfoACL_0100 + * @tc.desc: Verify GetRemoteCopyInfoACL for parameter serialization failures. + * @tc.type: FUNC + * @tc.require: I7TDJK + */ +HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_GetRemoteCopyInfoACL_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DistributedFileDaemon_GetRemoteCopyInfoACL_0100 Start"; + AccountInfo accountInfo{123, 0, "account_id", "network_id"}; + bool isFile = false; + bool isDir = false; + + // Test WriteInterfaceToken failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(false)); + auto ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_BROKEN_IPC); + + // Test WriteString(srcUri) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(1).WillOnce(Return(false)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteInt32(userId_) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(false)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(accountId_) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test WriteString(networkId_) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillOnce(Return(true)).WillOnce(Return(true)) + .WillOnce(Return(false)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + GTEST_LOG_(INFO) << "DistributedFileDaemon_GetRemoteCopyInfoACL_0100 End"; +} + +/** + * @tc.name: DistributedFileDaemon_GetRemoteCopyInfoACL_0200 + * @tc.desc: Verify GetRemoteCopyInfoACL for remote object, IPC, and reply read failures, and success case. + * @tc.type: FUNC + * @tc.require: I7TDJK + */ +HWTEST_F(DistributedFileDaemonProxyTest, DistributedFileDaemon_GetRemoteCopyInfoACL_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DistributedFileDaemon_GetRemoteCopyInfoACL_0200 Start"; + AccountInfo accountInfo{123, 0, "account_id", "network_id"}; + bool isFile = false; + bool isDir = false; + + // Test remote object null + auto testProxy = make_shared(nullptr); + auto ret = testProxy->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_BROKEN_IPC); + + // Test SendRequest failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_INVAL_ARG)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_BROKEN_IPC); + + // Test ReadBool(isFile) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, ReadBool(_)).Times(1).WillOnce(Return(false)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test ReadBool(isDir) failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, ReadBool(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(false)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test ReadInt32 failure + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, ReadBool(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).Times(1).WillOnce(Return(false)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_INVAL_ARG); + + // Test success case + EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteString(_)).Times(3).WillRepeatedly(Return(true)); + EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK)); + EXPECT_CALL(*messageParcelMock_, ReadBool(_)).Times(2).WillOnce(DoAll(SetArgReferee<0>(true), Return(true))) + .WillOnce(DoAll(SetArgReferee<0>(false), Return(true))); + EXPECT_CALL(*messageParcelMock_, ReadInt32(_)).Times(1).WillOnce(Return(true)); + ret = proxy_->GetRemoteCopyInfoACL("uri", isFile, isDir, accountInfo); + EXPECT_EQ(ret, E_OK); + EXPECT_TRUE(isFile); + EXPECT_FALSE(isDir); + + GTEST_LOG_(INFO) << "DistributedFileDaemon_GetRemoteCopyInfoACL_0200 End"; +} } // namespace OHOS::Storage::DistributedFile::Test diff --git a/utils/log/include/dfs_error.h b/utils/log/include/dfs_error.h index 4a02ced00..e6a3583b2 100644 --- a/utils/log/include/dfs_error.h +++ b/utils/log/include/dfs_error.h @@ -150,6 +150,7 @@ enum DFSErrCode { ERR_CHECKOUT_COUNT, ERR_NULLPTR, + ERR_ACL_FAILED, /* dfs version */ ERR_DFS_VERSION_BASE = 1000, -- Gitee