From c2558c1ccf3df62d96fc4e9892ccb769aac935d2 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Tue, 2 Sep 2025 16:54:47 +0800 Subject: [PATCH 1/6] fix: Signed-off-by: weishaoxiong --- .../service/cloud/cloud_service_impl.cpp | 2 +- .../service/cloud/sync_manager.cpp | 11 +- .../service/cloud/sync_manager.h | 4 +- .../service/test/cloud_service_impl_test.cpp | 105 +++++++----------- 4 files changed, 44 insertions(+), 78 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 32334a08e..f1f963a31 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -808,7 +808,7 @@ int32_t CloudServiceImpl::OnReady(const std::string &device) Execute(GenTask(0, user, CloudSyncScene::NETWORK_RECOVERY, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_SUB })); } - syncManager_.OnNetworkConnected(); + syncManager_.OnNetworkConnected(users); return SUCCESS; } diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index ea2bc3db7..e552bcf1f 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1095,9 +1095,9 @@ void SyncManager::OnNetworkDisconnected() networkRecoveryManager_.OnNetworkDisconnected(); } -void SyncManager::OnNetworkConnected() +void SyncManager::OnNetworkConnected(const std::vector &users) { - networkRecoveryManager_.OnNetworkConnected(); + networkRecoveryManager_.OnNetworkConnected(users); } void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() @@ -1108,7 +1108,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() currentEvent_->disconnectTime = std::chrono::system_clock::now(); } -void SyncManager::NetworkRecoveryManager::OnNetworkConnected() +void SyncManager::NetworkRecoveryManager::OnNetworkConnected(const std::vector &users) { std::unique_ptr event; { @@ -1123,11 +1123,6 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() auto duration = now - event->disconnectTime; auto hours = std::chrono::duration_cast(duration).count(); bool timeout = (hours > NETWORK_DISCONNECT_TIMEOUT_HOURS); - std::vector users; - if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { - ZLOGE("no foreground user, skip sync."); - return; - } for (auto user : users) { const auto &syncApps = timeout ? GetAppList(user) : event->syncApps[user]; for (const auto &bundleName : syncApps) { diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index 1e152ca10..36445ec25 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -105,7 +105,7 @@ public: void CleanCompensateSync(int32_t userId); static std::string GetPath(const StoreMetaData &meta); void OnNetworkDisconnected(); - void OnNetworkConnected(); + void OnNetworkConnected(const std::vector &users); private: class NetworkRecoveryManager { @@ -114,7 +114,7 @@ private: { } void OnNetworkDisconnected(); - void OnNetworkConnected(); + void OnNetworkConnected(const std::vector &users); void RecordSyncApps(const int32_t user, const std::string &bundleName); private: diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index 6238c5271..c1b77b9cd 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -146,7 +146,6 @@ void CloudServiceImplTest::SetUpTestCase(void) size_t max = 12; size_t min = 5; auto executor = std::make_shared(max, min); - DeviceManagerAdapter::GetInstance().Init(executor); cloudServiceImpl_->OnBind( { "CloudServiceImplTest", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); Bootstrap::GetInstance().LoadCheckers(); @@ -996,11 +995,8 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - // 2 means that the QueryForegroundUsers interface will be called twice - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .Times(2) - .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); delegate_.isNetworkAvailable_ = false; CloudInfo::AppInfo appInfo; appInfo.bundleName = TEST_CLOUD_BUNDLE; @@ -1011,15 +1007,15 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) cloudInfo.apps = apps; cloudInfo.user = MOCK_USER; cloudInfo.enableCloud = true; + cloudInfo.id = TEST_CLOUD_APPID; MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); - auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - ASSERT_NE(recoveryManager.currentEvent_, nullptr); SchemaMeta schemaMeta; schemaMeta.bundleName = TEST_CLOUD_BUNDLE; SchemaMeta::Database database; database.name = TEST_CLOUD_STORE; + database.alias = TEST_CLOUD_STORE; schemaMeta.databases.emplace_back(database); MetaDataManager::GetInstance().SaveMeta(CloudInfo::GetSchemaKey(cloudInfo.user, TEST_CLOUD_BUNDLE), schemaMeta, true); @@ -1027,13 +1023,17 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) option.syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; auto async = [](const DistributedRdb::Details &details) {}; cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); - sleep(1); - EXPECT_EQ(recoveryManager.currentEvent_->syncApps.size(), 1); + sleep(2); + MetaDataManager::GetInstance().DelMeta(CloudLastSyncInfo::GetKey(MOCK_USER, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE), + true); delegate_.isNetworkAvailable_ = true; cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + sleep(1); + auto [status, result] = + cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); + EXPECT_EQ(status, CloudData::CloudService::SUCCESS); + EXPECT_TRUE(result.find(TEST_CLOUD_STORE) != result.end()); } /** @@ -1048,20 +1048,20 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) ASSERT_NE(cloudServiceImpl_, nullptr); EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); - // 2 means that the QueryForegroundUsers interface will be called twice - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .Times(2) - .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); - auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); + MetaDataManager::GetInstance().DelMeta(CloudLastSyncInfo::GetKey(MOCK_USER, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE), + true); cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - ASSERT_NE(recoveryManager.currentEvent_, nullptr); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + sleep(1); + auto [status, result] = + cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); + EXPECT_EQ(status, CloudData::CloudService::SUCCESS); + EXPECT_TRUE(result.find(TEST_CLOUD_STORE) != result.end()); } /** @@ -1077,77 +1077,48 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); + delegate_.isNetworkAvailable_ = false; CloudData::CloudService::Option option; option.syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; auto async = [](const DistributedRdb::Details &details) {}; - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); sleep(1); + MetaDataManager::GetInstance().DelMeta(CloudLastSyncInfo::GetKey(MOCK_USER, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE), + true); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + auto [status, result] = + cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); + EXPECT_EQ(status, CloudData::CloudService::SUCCESS); + EXPECT_TRUE(result.empty()); } /** * @tc.name: NetworkRecoveryTest004 - * @tc.desc: The QueryForegroundUsers interface call fails when the network is restored - * @tc.type: FUNC - * @tc.require: - * @tc.author: - */ -HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) -{ - ASSERT_NE(cloudServiceImpl_, nullptr); - EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; - ASSERT_NE(recoveryManager.currentEvent_, nullptr); - - EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .WillOnce(ReturnWithUserList({ MOCK_USER })) - .WillOnce(Return(false)); - cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_EQ(recoveryManager.currentEvent_, nullptr); - - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - ASSERT_NE(recoveryManager.currentEvent_, nullptr); - EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .WillOnce(ReturnWithUserList({ MOCK_USER })) - .WillOnce(ReturnWithUserList({})); - cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_EQ(recoveryManager.currentEvent_, nullptr); -} - -/** - * @tc.name: NetworkRecoveryTest005 * @tc.desc: The test network connection interface call fails when the load cloudInfo failed * @tc.type: FUNC * @tc.require: * @tc.author: */ -HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest005, TestSize.Level0) +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - // 2 means that the QueryForegroundUsers interface will be called twice - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .Times(2) - .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); CloudInfo cloudInfo; cloudInfo.user = MOCK_USER; MetaDataManager::GetInstance().DelMeta(cloudInfo.GetKey(), true); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - ASSERT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + auto [status, result] = + cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); + EXPECT_EQ(status, CloudData::CloudService::ERROR); + EXPECT_TRUE(result.empty()); if (accountDelegateMock != nullptr) { delete accountDelegateMock; accountDelegateMock = nullptr; -- Gitee From debe8eb196116d735491542241e4cfbd61794f3b Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Wed, 10 Sep 2025 11:49:29 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: weishaoxiong --- bundle.json | 3 +- .../service/object/BUILD.gn | 9 ++- .../service/object/asset_loader/BUILD.gn | 62 ++++++++++++++++++ .../asset_loader/asset_sync_manager.cpp | 63 +++++++++++++++++++ .../object/asset_loader/asset_sync_manager.h | 29 +++++++++ .../asset_loader/i_asset_sync_manager.h | 33 ++++++++++ .../object/include/object_asset_loader.h | 12 ++-- .../object/src/object_asset_loader.cpp | 49 ++++++++------- .../service/test/BUILD.gn | 2 + .../service/test/object_asset_loader_test.cpp | 13 ++++ 10 files changed, 243 insertions(+), 32 deletions(-) create mode 100644 services/distributeddataservice/service/object/asset_loader/BUILD.gn create mode 100644 services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp create mode 100644 services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h create mode 100644 services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h diff --git a/bundle.json b/bundle.json index 8b8985701..5e2cb4818 100644 --- a/bundle.json +++ b/bundle.json @@ -109,7 +109,8 @@ "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/rust/extension:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:build_module", "//foundation/distributeddatamgr/datamgr_service/conf:build_module", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share:build_module" + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share:build_module", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/object/asset_loader:asset_sync_manager" ], "inner_kits": [ { diff --git a/services/distributeddataservice/service/object/BUILD.gn b/services/distributeddataservice/service/object/BUILD.gn index b5bba90be..e1e6386d3 100644 --- a/services/distributeddataservice/service/object/BUILD.gn +++ b/services/distributeddataservice/service/object/BUILD.gn @@ -52,7 +52,10 @@ ohos_source_set("distributeddata_object") { "-Oz", ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "asset_loader", + ] configs = [ ":object_public_config" ] @@ -73,12 +76,12 @@ ohos_source_set("distributeddata_object") { external_deps = [ "access_token:libtokenid_sdk", "data_object:data_object_inner", - "dfs_service:cloudsync_asset_kit_inner", + "device_manager:devicemanagersdk", "dfs_service:distributed_file_daemon_kit_inner", "dmsfwk:distributed_sdk", "hilog:libhilog", - "ipc:ipc_single", "hisysevent:libhisysevent", + "ipc:ipc_single", "kv_store:datamgr_common", "kv_store:distributeddb", ] diff --git a/services/distributeddataservice/service/object/asset_loader/BUILD.gn b/services/distributeddataservice/service/object/asset_loader/BUILD.gn new file mode 100644 index 000000000..1823986e7 --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/BUILD.gn @@ -0,0 +1,62 @@ +# 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. +import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") + +config("object_public_config") { + visibility = [ ":*" ] +} + +ohos_shared_library("asset_sync_manager") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + boundary_sanitize = true + ubsan = true + } + + sources = [ "asset_sync_manager.cpp" ] + + cflags_cc = [ + "-fvisibility=hidden", + "-Oz", + ] + + include_dirs = [ + "../include", + "${data_service_path}/adapter/include/utils", + ] + + configs = [ ":object_public_config" ] + + cflags = [ + "-Werror", + "-Wno-multichar", + "-Wno-c99-designator", + "-D_LIBCPP_HAS_COND_CLOCKWAIT", + "-Oz", + ] + + deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] + + external_deps = [ + "dfs_service:cloudsync_asset_kit_inner", + "hilog:libhilog", + "kv_store:datamgr_common", + ] + + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" +} diff --git a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp new file mode 100644 index 000000000..cf22ce08c --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp @@ -0,0 +1,63 @@ +/* + * 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. + */ +#define LOG_TAG "AssetSyncManager" +#include "asset_sync_manager.h" + +#include + +#include "block_data.h" +#include "cloud_sync_asset_manager.h" +#include "log_print.h" +#include "object_common.h" +#include "utils/anonymous.h" +#include "visibility.h" + +namespace OHOS::DistributedObject { +using namespace OHOS::FileManagement::CloudSync; +using namespace OHOS::DistributedData; +static constexpr int WAIT_TIME = 60; +bool AssetSyncManager::Transfer(const int32_t userId, const std::string &bundleName, const std::string &deviceId, + const DistributedData::Asset &asset) +{ + AssetInfo assetInfo; + assetInfo.uri = asset.uri; + assetInfo.assetName = asset.name; + ZLOGI("Start transfer, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), + Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); + auto block = std::make_shared>>(WAIT_TIME, std::tuple{ true, OBJECT_SUCCESS }); + auto res = CloudSyncAssetManager::GetInstance().DownloadFile(userId, bundleName, deviceId, assetInfo, + [block](const std::string &uri, int32_t status) { block->SetValue({ false, status }); }); + if (res != OBJECT_SUCCESS) { + ZLOGE("fail, res: %{public}d, name: %{public}s, deviceId: %{public}s, bundleName: %{public}s", res, + Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str(), bundleName.c_str()); + return false; + } + auto [timeout, status] = block->GetValue(); + if (timeout || status != OBJECT_SUCCESS) { + ZLOGE("fail, timeout: %{public}d, status: %{public}d, name: %{public}s, deviceId: %{public}s ", timeout, + status, Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str()); + return false; + } + ZLOGD("Transfer end, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), + Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); + return true; +} +} // namespace OHOS::DistributedObject + +API_EXPORT std::shared_ptr Create() asm("CreateAssetSyncManager"); +std::shared_ptr Create() +{ + return std::make_shared(); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h new file mode 100644 index 000000000..fe518ccc7 --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h @@ -0,0 +1,29 @@ +/* + * 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 DISTRIBUTEDDATAMGR_ASSET_SYNC_MANAGER_H +#define DISTRIBUTEDDATAMGR_ASSET_SYNC_MANAGER_H + +#include "i_asset_sync_manager.h" + +namespace OHOS { +namespace DistributedObject { +class AssetSyncManager : public IAssetSyncManager { +public: + bool Transfer(const int32_t userId, const std::string &bundleName, const std::string &deviceId, + const DistributedData::Asset &asset) override; +}; +} // namespace DistributedObject +} // namespace OHOS +#endif // DISTRIBUTEDDATAMGR_ASSET_SYNC_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h b/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h new file mode 100644 index 000000000..8e01c4670 --- /dev/null +++ b/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h @@ -0,0 +1,33 @@ +/* + * 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 DISTRIBUTEDDATAMGR_I_ASSET_SYNC_MANAGER_H +#define DISTRIBUTEDDATAMGR_I_ASSET_SYNC_MANAGER_H + +#include +#include + +#include "store/general_value.h" + +namespace OHOS { +namespace DistributedObject { +class IAssetSyncManager { +public: + virtual ~IAssetSyncManager() {}; + virtual bool Transfer(const int32_t userId, const std::string &bundleName, const std::string &deviceId, + const DistributedData::Asset &asset) = 0; +}; +} // namespace DistributedObject +} // namespace OHOS +#endif // DISTRIBUTEDDATAMGR_I_ASSET_SYNC_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/object/include/object_asset_loader.h b/services/distributeddataservice/service/object/include/object_asset_loader.h index 160539faf..f8863f212 100644 --- a/services/distributeddataservice/service/object/include/object_asset_loader.h +++ b/services/distributeddataservice/service/object/include/object_asset_loader.h @@ -16,12 +16,14 @@ #define DISTRIBUTEDDATAMGR_OBJECT_ASSET_LOADER_H #include +#include + +#include "asset/asset_send_callback_stub.h" +#include "concurrent_map.h" #include "executor_pool.h" +#include "i_asset_sync_manager.h" #include "object_types.h" #include "store/general_value.h" -#include "concurrent_map.h" -#include -#include "asset/asset_send_callback_stub.h" namespace OHOS { namespace DistributedObject { using AssetObj = Storage::DistributedFile::AssetObj; @@ -58,7 +60,7 @@ private: bool IsDownloading(const DistributedData::Asset& asset); bool IsDownloaded(const DistributedData::Asset& asset); void UpdateDownloaded(const DistributedData::Asset& asset); - static constexpr int WAIT_TIME = 60; + std::shared_ptr GetAssetSyncManager(); static constexpr int LAST_DOWNLOAD_ASSET_SIZE = 200; std::shared_ptr executors_; std::mutex mutex_; @@ -67,6 +69,8 @@ private: ConcurrentMap tasks_; ConcurrentMap downloaded_; ConcurrentMap downloading_; + std::mutex assetSyncMutex_; + std::shared_ptr assetSyncManager_ = nullptr; }; } // namespace DistributedObject } // namespace OHOS diff --git a/services/distributeddataservice/service/object/src/object_asset_loader.cpp b/services/distributeddataservice/service/object/src/object_asset_loader.cpp index efabc95ba..22250ca7f 100644 --- a/services/distributeddataservice/service/object/src/object_asset_loader.cpp +++ b/services/distributeddataservice/service/object/src/object_asset_loader.cpp @@ -16,15 +16,14 @@ #include "object_asset_loader.h" #include "block_data.h" -#include "cloud_sync_asset_manager.h" #include "log_print.h" #include "object_common.h" #include "utils/anonymous.h" #include "object_radar_reporter.h" #include "distributed_file_daemon_manager.h" namespace OHOS::DistributedObject { -using namespace OHOS::FileManagement::CloudSync; using namespace OHOS::DistributedData; +using Creater = std::shared_ptr (*)(); ObjectAssetLoader &ObjectAssetLoader::GetInstance() { static ObjectAssetLoader loader; @@ -39,30 +38,12 @@ void ObjectAssetLoader::SetThreadPool(std::shared_ptr executors) bool ObjectAssetLoader::Transfer(const int32_t userId, const std::string& bundleName, const std::string& deviceId, const DistributedData::Asset& asset) { - AssetInfo assetInfo; - assetInfo.uri = asset.uri; - assetInfo.assetName = asset.name; - ZLOGI("Start transfer, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), - Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); - auto block = std::make_shared>>(WAIT_TIME, std::tuple{ true, OBJECT_SUCCESS }); - auto res = CloudSyncAssetManager::GetInstance().DownloadFile(userId, bundleName, deviceId, assetInfo, - [block](const std::string& uri, int32_t status) { - block->SetValue({ false, status }); - }); - if (res != OBJECT_SUCCESS) { - ZLOGE("fail, res: %{public}d, name: %{public}s, deviceId: %{public}s, bundleName: %{public}s", res, - Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str(), bundleName.c_str()); + auto syncManager = GetAssetSyncManager(); + if (syncManager == nullptr) { + ZLOGW("syncManager is nullptr"); return false; } - auto [timeout, status] = block->GetValue(); - if (timeout || status != OBJECT_SUCCESS) { - ZLOGE("fail, timeout: %{public}d, status: %{public}d, name: %{public}s, deviceId: %{public}s ", timeout, - status, Anonymous::Change(asset.name).c_str(), Anonymous::Change(deviceId).c_str()); - return false; - } - ZLOGD("Transfer end, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(), - Anonymous::Change(deviceId).c_str(), Anonymous::Change(assetInfo.assetName).c_str()); - return true; + return syncManager->Transfer(userId, bundleName, deviceId, asset); } void ObjectAssetLoader::TransferAssetsAsync(const int32_t userId, const std::string& bundleName, @@ -189,6 +170,26 @@ int32_t ObjectAssetLoader::PushAsset(int32_t userId, const sptr &asset return status; } +std::shared_ptr ObjectAssetLoader::GetAssetSyncManager() +{ + std::lock_guard lock(assetSyncMutex_); + if (assetSyncManager_ != nullptr) { + return assetSyncManager_; + } + void *handler = dlopen("libasset_sync_manager.z.so", RTLD_LAZY); + if (handler == nullptr) { + ZLOGW("dlopen failed"); + return nullptr; + } + auto creator = reinterpret_cast(dlsym(handler, "CreateAssetSyncManager")); + if (creator == nullptr) { + ZLOGE("dlsym failed, %{public}s", dlerror()); + return nullptr; + } + assetSyncManager_ = creator(); + return assetSyncManager_; +} + int32_t ObjectAssetsSendListener::OnSendResult(const sptr &assetObj, int32_t result) { if (assetObj == nullptr) { diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 3b8f38bff..d8c854626 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -40,6 +40,7 @@ config("module_private_config") { "${data_service_path}/service/matrix/include/", "${data_service_path}/service/kvdb", "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", "${data_service_path}/service/permission/include", "${data_service_path}/service/rdb/", "${data_service_path}/service/test/mock", @@ -789,6 +790,7 @@ ohos_unittest("ObjectAssetLoaderTest") { "../object/src/object_asset_loader.cpp", "../object/src/object_asset_machine.cpp", "../object/src/object_snapshot.cpp", + "../object/asset_loader/asset_sync_manager.cpp", "object_asset_loader_test.cpp", ] diff --git a/services/distributeddataservice/service/test/object_asset_loader_test.cpp b/services/distributeddataservice/service/test/object_asset_loader_test.cpp index dc5188fa0..3af346eaa 100644 --- a/services/distributeddataservice/service/test/object_asset_loader_test.cpp +++ b/services/distributeddataservice/service/test/object_asset_loader_test.cpp @@ -22,6 +22,7 @@ #include "executor_pool.h" #include "object_common.h" #include "snapshot/machine_status.h" +#include "asset_sync_manager.h" using namespace testing::ext; using namespace OHOS::DistributedObject; @@ -264,4 +265,16 @@ HWTEST_F(ObjectAssetLoaderTest, OnSendResult001, TestSize.Level1) ret = sendCallback->OnSendResult(assetObj, result); EXPECT_EQ(ret, result); } + +/** +* @tc.name: Transfer001 +* @tc.desc: asset sync manager test. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectAssetLoaderTest, Transfer001, TestSize.Level1) +{ + AssetSyncManager assetSyncManager; + auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_EQ(result, false); +} } // namespace OHOS::Test -- Gitee From 6c9386ff16b8e8b4a9352dea74612ffe3d565eec Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Wed, 10 Sep 2025 14:47:45 +0800 Subject: [PATCH 3/6] Revert "fix:" This reverts commit c2558c1ccf3df62d96fc4e9892ccb769aac935d2. Signed-off-by: weishaoxiong --- .../service/cloud/cloud_service_impl.cpp | 2 +- .../service/cloud/sync_manager.cpp | 11 +- .../service/cloud/sync_manager.h | 4 +- .../object/asset_loader/asset_sync_manager.h | 6 +- .../asset_loader/i_asset_sync_manager.h | 6 +- .../service/test/cloud_service_impl_test.cpp | 105 +++++++++++------- 6 files changed, 84 insertions(+), 50 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index f1f963a31..32334a08e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -808,7 +808,7 @@ int32_t CloudServiceImpl::OnReady(const std::string &device) Execute(GenTask(0, user, CloudSyncScene::NETWORK_RECOVERY, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_SUB })); } - syncManager_.OnNetworkConnected(users); + syncManager_.OnNetworkConnected(); return SUCCESS; } diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index e552bcf1f..ea2bc3db7 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1095,9 +1095,9 @@ void SyncManager::OnNetworkDisconnected() networkRecoveryManager_.OnNetworkDisconnected(); } -void SyncManager::OnNetworkConnected(const std::vector &users) +void SyncManager::OnNetworkConnected() { - networkRecoveryManager_.OnNetworkConnected(users); + networkRecoveryManager_.OnNetworkConnected(); } void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() @@ -1108,7 +1108,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() currentEvent_->disconnectTime = std::chrono::system_clock::now(); } -void SyncManager::NetworkRecoveryManager::OnNetworkConnected(const std::vector &users) +void SyncManager::NetworkRecoveryManager::OnNetworkConnected() { std::unique_ptr event; { @@ -1123,6 +1123,11 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected(const std::vectordisconnectTime; auto hours = std::chrono::duration_cast(duration).count(); bool timeout = (hours > NETWORK_DISCONNECT_TIMEOUT_HOURS); + std::vector users; + if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { + ZLOGE("no foreground user, skip sync."); + return; + } for (auto user : users) { const auto &syncApps = timeout ? GetAppList(user) : event->syncApps[user]; for (const auto &bundleName : syncApps) { diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index 36445ec25..1e152ca10 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -105,7 +105,7 @@ public: void CleanCompensateSync(int32_t userId); static std::string GetPath(const StoreMetaData &meta); void OnNetworkDisconnected(); - void OnNetworkConnected(const std::vector &users); + void OnNetworkConnected(); private: class NetworkRecoveryManager { @@ -114,7 +114,7 @@ private: { } void OnNetworkDisconnected(); - void OnNetworkConnected(const std::vector &users); + void OnNetworkConnected(); void RecordSyncApps(const int32_t user, const std::string &bundleName); private: diff --git a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h index fe518ccc7..e034e9876 100644 --- a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h +++ b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef DISTRIBUTEDDATAMGR_ASSET_SYNC_MANAGER_H -#define DISTRIBUTEDDATAMGR_ASSET_SYNC_MANAGER_H +#ifndef DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_ASSET_SYNC_MANAGER_H +#define DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_ASSET_SYNC_MANAGER_H #include "i_asset_sync_manager.h" @@ -26,4 +26,4 @@ public: }; } // namespace DistributedObject } // namespace OHOS -#endif // DISTRIBUTEDDATAMGR_ASSET_SYNC_MANAGER_H \ No newline at end of file +#endif // DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_ASSET_SYNC_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h b/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h index 8e01c4670..d32e89bf0 100644 --- a/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h +++ b/services/distributeddataservice/service/object/asset_loader/i_asset_sync_manager.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef DISTRIBUTEDDATAMGR_I_ASSET_SYNC_MANAGER_H -#define DISTRIBUTEDDATAMGR_I_ASSET_SYNC_MANAGER_H +#ifndef DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_I_ASSET_SYNC_MANAGER_H +#define DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_I_ASSET_SYNC_MANAGER_H #include #include @@ -30,4 +30,4 @@ public: }; } // namespace DistributedObject } // namespace OHOS -#endif // DISTRIBUTEDDATAMGR_I_ASSET_SYNC_MANAGER_H \ No newline at end of file +#endif // DISTRIBUTEDDATAMGR_SERVICE_OBJECT_ASSET_LOADER_I_ASSET_SYNC_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index c1b77b9cd..6238c5271 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -146,6 +146,7 @@ void CloudServiceImplTest::SetUpTestCase(void) size_t max = 12; size_t min = 5; auto executor = std::make_shared(max, min); + DeviceManagerAdapter::GetInstance().Init(executor); cloudServiceImpl_->OnBind( { "CloudServiceImplTest", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); Bootstrap::GetInstance().LoadCheckers(); @@ -995,8 +996,11 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); + // 2 means that the QueryForegroundUsers interface will be called twice + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .Times(2) + .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); delegate_.isNetworkAvailable_ = false; CloudInfo::AppInfo appInfo; appInfo.bundleName = TEST_CLOUD_BUNDLE; @@ -1007,15 +1011,15 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) cloudInfo.apps = apps; cloudInfo.user = MOCK_USER; cloudInfo.enableCloud = true; - cloudInfo.id = TEST_CLOUD_APPID; MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); SchemaMeta schemaMeta; schemaMeta.bundleName = TEST_CLOUD_BUNDLE; SchemaMeta::Database database; database.name = TEST_CLOUD_STORE; - database.alias = TEST_CLOUD_STORE; schemaMeta.databases.emplace_back(database); MetaDataManager::GetInstance().SaveMeta(CloudInfo::GetSchemaKey(cloudInfo.user, TEST_CLOUD_BUNDLE), schemaMeta, true); @@ -1023,17 +1027,13 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) option.syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; auto async = [](const DistributedRdb::Details &details) {}; cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); - sleep(2); - MetaDataManager::GetInstance().DelMeta(CloudLastSyncInfo::GetKey(MOCK_USER, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE), - true); + sleep(1); + EXPECT_EQ(recoveryManager.currentEvent_->syncApps.size(), 1); delegate_.isNetworkAvailable_ = true; cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - sleep(1); - auto [status, result] = - cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); - EXPECT_EQ(status, CloudData::CloudService::SUCCESS); - EXPECT_TRUE(result.find(TEST_CLOUD_STORE) != result.end()); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } /** @@ -1048,20 +1048,20 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) ASSERT_NE(cloudServiceImpl_, nullptr); EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); - MetaDataManager::GetInstance().DelMeta(CloudLastSyncInfo::GetKey(MOCK_USER, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE), - true); - cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + // 2 means that the QueryForegroundUsers interface will be called twice + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .Times(2) + .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - sleep(1); - auto [status, result] = - cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); - EXPECT_EQ(status, CloudData::CloudService::SUCCESS); - EXPECT_TRUE(result.find(TEST_CLOUD_STORE) != result.end()); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } /** @@ -1077,48 +1077,77 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); - delegate_.isNetworkAvailable_ = false; + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; CloudData::CloudService::Option option; option.syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; auto async = [](const DistributedRdb::Details &details) {}; + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); sleep(1); - MetaDataManager::GetInstance().DelMeta(CloudLastSyncInfo::GetKey(MOCK_USER, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE), - true); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - auto [status, result] = - cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); - EXPECT_EQ(status, CloudData::CloudService::SUCCESS); - EXPECT_TRUE(result.empty()); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } /** * @tc.name: NetworkRecoveryTest004 - * @tc.desc: The test network connection interface call fails when the load cloudInfo failed + * @tc.desc: The QueryForegroundUsers interface call fails when the network is restored * @tc.type: FUNC * @tc.require: * @tc.author: */ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) +{ + ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; + ASSERT_NE(recoveryManager.currentEvent_, nullptr); + + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .WillOnce(ReturnWithUserList({ MOCK_USER })) + .WillOnce(Return(false)); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .WillOnce(ReturnWithUserList({ MOCK_USER })) + .WillOnce(ReturnWithUserList({})); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); +} + +/** + * @tc.name: NetworkRecoveryTest005 + * @tc.desc: The test network connection interface call fails when the load cloudInfo failed + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest005, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); - EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillRepeatedly(Return(MOCK_USER)); + // 2 means that the QueryForegroundUsers interface will be called twice + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .Times(2) + .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); CloudInfo cloudInfo; cloudInfo.user = MOCK_USER; MetaDataManager::GetInstance().DelMeta(cloudInfo.GetKey(), true); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - auto [status, result] = - cloudServiceImpl_->QueryLastSyncInfo(TEST_CLOUD_APPID, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE); - EXPECT_EQ(status, CloudData::CloudService::ERROR); - EXPECT_TRUE(result.empty()); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); if (accountDelegateMock != nullptr) { delete accountDelegateMock; accountDelegateMock = nullptr; -- Gitee From 5b06e6c9f1d00608ca60debf5ac53dd3d8ed12d5 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Wed, 10 Sep 2025 16:29:46 +0800 Subject: [PATCH 4/6] fix: Signed-off-by: weishaoxiong --- .../service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn | 1 + .../service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn | 1 + .../service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn | 1 + .../service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn | 1 + 4 files changed, 4 insertions(+) diff --git a/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn index 8d654ce30..a04639664 100644 --- a/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectserviceimp1_fuzzer/BUILD.gn @@ -21,6 +21,7 @@ ohos_fuzztest("ObjectServiceImp1FuzzTest") { include_dirs = [ "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", ] fuzz_config_file = diff --git a/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn index 57ebaece8..62e3d4b43 100644 --- a/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectserviceimp2_fuzzer/BUILD.gn @@ -21,6 +21,7 @@ ohos_fuzztest("ObjectServiceImp2FuzzTest") { include_dirs = [ "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", ] fuzz_config_file = diff --git a/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn index 46a6be379..1e04771cc 100644 --- a/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectserviceimp3_fuzzer/BUILD.gn @@ -21,6 +21,7 @@ ohos_fuzztest("ObjectServiceImp3FuzzTest") { include_dirs = [ "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", ] fuzz_config_file = diff --git a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn index e1185088f..b899b86e2 100755 --- a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn @@ -29,6 +29,7 @@ ohos_fuzztest("ObjectServiceStubFuzzTest") { "${data_service_path}/service/config/include", "${data_service_path}/service/crypto/include", "${data_service_path}/service/object/include", + "${data_service_path}/service/object/asset_loader", "${data_service_path}/service/matrix/include", "${data_service_path}/adapter/include/communicator", "${data_service_path}/adapter/include/utils", -- Gitee From 103530a8943417acc197706f6cd6f5c6e7fe8cf2 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Fri, 12 Sep 2025 09:27:18 +0800 Subject: [PATCH 5/6] fix: modify UT Signed-off-by: weishaoxiong --- .../asset_loader/asset_sync_manager.cpp | 2 +- .../object/src/object_asset_loader.cpp | 7 +- .../service/test/BUILD.gn | 39 ++- .../service/test/object_asset_loader_test.cpp | 13 - .../test/object_asset_sync_manager_test.cpp | 228 ++++++++++++++++++ 5 files changed, 272 insertions(+), 17 deletions(-) create mode 100644 services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp diff --git a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp index cf22ce08c..10578cc2b 100644 --- a/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp +++ b/services/distributeddataservice/service/object/asset_loader/asset_sync_manager.cpp @@ -56,7 +56,7 @@ bool AssetSyncManager::Transfer(const int32_t userId, const std::string &bundleN } } // namespace OHOS::DistributedObject -API_EXPORT std::shared_ptr Create() asm("CreateAssetSyncManager"); +API_EXPORT std::shared_ptr Create() asm("CreateObjectAssetSyncManager"); std::shared_ptr Create() { return std::make_shared(); diff --git a/services/distributeddataservice/service/object/src/object_asset_loader.cpp b/services/distributeddataservice/service/object/src/object_asset_loader.cpp index 22250ca7f..a375e62a1 100644 --- a/services/distributeddataservice/service/object/src/object_asset_loader.cpp +++ b/services/distributeddataservice/service/object/src/object_asset_loader.cpp @@ -178,12 +178,15 @@ std::shared_ptr ObjectAssetLoader::GetAssetSyncManager() } void *handler = dlopen("libasset_sync_manager.z.so", RTLD_LAZY); if (handler == nullptr) { - ZLOGW("dlopen failed"); + const char* dlopenError = dlerror(); + ZLOGE("dlopen failed: %{public}s", dlopenError ? dlopenError : "unknown error"); return nullptr; } - auto creator = reinterpret_cast(dlsym(handler, "CreateAssetSyncManager")); + (void)dlerror(); + auto creator = reinterpret_cast(dlsym(handler, "CreateObjectAssetSyncManager")); if (creator == nullptr) { ZLOGE("dlsym failed, %{public}s", dlerror()); + dlclose(handler); return nullptr; } assetSyncManager_ = creator(); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index d8c854626..57060bf02 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -790,7 +790,6 @@ ohos_unittest("ObjectAssetLoaderTest") { "../object/src/object_asset_loader.cpp", "../object/src/object_asset_machine.cpp", "../object/src/object_snapshot.cpp", - "../object/asset_loader/asset_sync_manager.cpp", "object_asset_loader_test.cpp", ] @@ -820,6 +819,43 @@ ohos_unittest("ObjectAssetLoaderTest") { ] } +ohos_unittest("ObjectAssetSyncManagerTest") { + module_out_path = module_output_path + sources = [ + "../object/asset_loader/asset_sync_manager.cpp", + "../object/src/object_asset_loader.cpp", + "../object/src/object_asset_machine.cpp", + "../object/src/object_snapshot.cpp", + "object_asset_sync_manager_test.cpp", + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "c_utils:utils", + "data_object:data_object_inner", + "dfs_service:cloudsync_asset_kit_inner", + "dfs_service:distributed_file_daemon_kit_inner", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "json:nlohmann_json_static", + "kv_store:distributeddata_inner", + ] + + deps = [ + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] +} + ohos_unittest("ObjectAssetMachineTest") { module_out_path = module_output_path sources = [ @@ -2547,6 +2583,7 @@ group("unittest") { ":ObjectManagerTest", ":ObjectSnapshotTest", ":ObjectManagerMockTest", + ":ObjectAssetSyncManagerTest", ] } diff --git a/services/distributeddataservice/service/test/object_asset_loader_test.cpp b/services/distributeddataservice/service/test/object_asset_loader_test.cpp index 3af346eaa..dc5188fa0 100644 --- a/services/distributeddataservice/service/test/object_asset_loader_test.cpp +++ b/services/distributeddataservice/service/test/object_asset_loader_test.cpp @@ -22,7 +22,6 @@ #include "executor_pool.h" #include "object_common.h" #include "snapshot/machine_status.h" -#include "asset_sync_manager.h" using namespace testing::ext; using namespace OHOS::DistributedObject; @@ -265,16 +264,4 @@ HWTEST_F(ObjectAssetLoaderTest, OnSendResult001, TestSize.Level1) ret = sendCallback->OnSendResult(assetObj, result); EXPECT_EQ(ret, result); } - -/** -* @tc.name: Transfer001 -* @tc.desc: asset sync manager test. -* @tc.type: FUNC -*/ -HWTEST_F(ObjectAssetLoaderTest, Transfer001, TestSize.Level1) -{ - AssetSyncManager assetSyncManager; - auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); - EXPECT_EQ(result, false); -} } // namespace OHOS::Test diff --git a/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp b/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp new file mode 100644 index 000000000..5bf7f9f2d --- /dev/null +++ b/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp @@ -0,0 +1,228 @@ +/* + * 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. + */ + +#define LOG_TAG "ObjectAssetSyncManagerTest" +#include +#include +#include +#include + +#include "asset_sync_manager.h" +#include "cloud_sync_asset_manager.h" +#include "executor_pool.h" +#include "object_asset_loader.h" +#include "object_common.h" +#include "snapshot/machine_status.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS::DistributedObject; +using namespace OHOS::DistributedData; +using namespace OHOS::FileManagement::CloudSync; +using ::testing::NiceMock; + +class MockCloudSyncAssetManager : public CloudSyncAssetManager { +public: + MOCK_METHOD(int32_t, UploadAsset, (int32_t, const std::string &, std::string &), (override)); + MOCK_METHOD(int32_t, DownloadFile, (int32_t, const std::string &, AssetInfo &), (override)); + MOCK_METHOD(int32_t, DownloadFiles, + (int32_t, const std::string &, const std::vector &, std::vector &, int32_t), (override)); + MOCK_METHOD(int32_t, DeleteAsset, (int32_t, const std::string &), (override)); + + enum class CallbackType { SUCCESS, DIRECT_FAILURE, CALLBACK_FAILURE, TIMEOUT }; + + static void SetCallbackType(CallbackType type) + { + callbackType_ = type; + } + int32_t DownloadFile(const int32_t userId, const std::string &bundleName, const std::string &networkId, + AssetInfo &assetInfo, ResultCallback resultCallback) override + { + switch (callbackType_) { + case CallbackType::SUCCESS: + resultCallback("", OBJECT_SUCCESS); + return OBJECT_SUCCESS; + case CallbackType::DIRECT_FAILURE: + return OBJECT_INNER_ERROR; + case CallbackType::CALLBACK_FAILURE: + resultCallback("", OBJECT_INNER_ERROR); + return OBJECT_INNER_ERROR; + case CallbackType::TIMEOUT: + return OBJECT_SUCCESS; + default: + break; + } + return OBJECT_INNER_ERROR; + } + +private: + static inline CallbackType callbackType_ = CallbackType::SUCCESS; +}; +namespace OHOS::FileManagement::CloudSync { +CloudSyncAssetManager &CloudSyncAssetManager::GetInstance() +{ + static MockCloudSyncAssetManager instance; + return instance; +} +} + +namespace OHOS::Test { +class MockDlsym { +public: + MOCK_METHOD(void *, dlopen, (const char *fileName, int flag)); +}; + +NiceMock *mockDlsym; + +extern "C" { +// mock dlopen +void *dlopen(const char *fileName, int flag) +{ + if (mockDlsym == nullptr) { + mockDlsym = new NiceMock(); + } + return mockDlsym->dlopen(fileName, flag); +} +} + +class ObjectAssetSyncManagerTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + static void SetUpTestCase(void); + static void TearDownTestCase(void); + +protected: + Asset asset_; + std::string uri_; + int32_t userId_ = 0; + std::string bundleName_ = "test_bundleName_1"; + std::string deviceId_ = "test_deviceId__1"; +}; + +void ObjectAssetSyncManagerTest::SetUpTestCase(void) +{ + mockDlsym = new NiceMock(); +} + +void ObjectAssetSyncManagerTest::TearDownTestCase(void) +{ + delete mockDlsym; + mockDlsym = nullptr; +} +void ObjectAssetSyncManagerTest::SetUp() +{ + uri_ = "file:://com.example.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg"; + Asset asset{ + .name = "test_name", + .uri = uri_, + .modifyTime = "modifyTime", + .size = "size", + .hash = "modifyTime_size", + }; + asset_ = asset; +} + +void ObjectAssetSyncManagerTest::TearDown() +{ +} + +/** + * @tc.name: Transfer001 + * @tc.desc: dlopen is nullptr. + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(ObjectAssetSyncManagerTest, UploadTest001, TestSize.Level0) +{ + auto &assetLoader = ObjectAssetLoader::GetInstance(); + EXPECT_CALL(*mockDlsym, dlopen(::testing::_, ::testing::_)).WillOnce(Return(nullptr)); + auto result = assetLoader.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_EQ(result, false); +} + +/** + * @tc.name: UploadTest002 + * @tc.desc: dlsym return null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(ObjectAssetSyncManagerTest, UploadTest002, TestSize.Level0) +{ + auto &assetLoader = ObjectAssetLoader::GetInstance(); + void *handle = (void *)0x123456; + EXPECT_CALL(*mockDlsym, dlopen(::testing::_, ::testing::_)).WillOnce(Return(handle)); + auto result = assetLoader.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_EQ(result, false); +} + +/** + * @tc.name: TransferAsset_Success + * @tc.desc: Should succeed when download returns success immediately. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_Success, TestSize.Level0) +{ + AssetSyncManager assetSyncManager; + MockCloudSyncAssetManager::SetCallbackType(MockCloudSyncAssetManager::CallbackType::SUCCESS); + auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_TRUE(result); +} + +/** + * @tc.name: TransferAsset_DirectFailure + * @tc.desc: Should fail when DownloadFile returns error directly. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_DirectFailure, TestSize.Level0) +{ + AssetSyncManager assetSyncManager; + MockCloudSyncAssetManager::SetCallbackType(MockCloudSyncAssetManager::CallbackType::DIRECT_FAILURE); + auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_FALSE(result); +} + +/** + * @tc.name: TransferAsset_CallbackFailure + * @tc.desc: Should fail when download succeeds but callback returns error. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_CallbackFailure, TestSize.Level0) +{ + AssetSyncManager assetSyncManager; + MockCloudSyncAssetManager::SetCallbackType(MockCloudSyncAssetManager::CallbackType::CALLBACK_FAILURE); + auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_FALSE(result); +} + +/** + * @tc.name: TransferAsset_Timeout + * @tc.desc: Should timeout when callback never called. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_Timeout, TestSize.Level1) +{ + AssetSyncManager assetSyncManager; + MockCloudSyncAssetManager::SetCallbackType(MockCloudSyncAssetManager::CallbackType::TIMEOUT); + auto result = assetSyncManager.Transfer(userId_, bundleName_, deviceId_, asset_); + EXPECT_FALSE(result); +} +} // namespace OHOS::Test -- Gitee From efd05193154c88ee6fa1d98ddc309c6fef0deb08 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Fri, 12 Sep 2025 10:26:48 +0800 Subject: [PATCH 6/6] fix: Signed-off-by: weishaoxiong --- .../service/test/object_asset_sync_manager_test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp b/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp index 5bf7f9f2d..d67e7dabe 100644 --- a/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_asset_sync_manager_test.cpp @@ -164,7 +164,7 @@ HWTEST_F(ObjectAssetSyncManagerTest, UploadTest001, TestSize.Level0) HWTEST_F(ObjectAssetSyncManagerTest, UploadTest002, TestSize.Level0) { auto &assetLoader = ObjectAssetLoader::GetInstance(); - void *handle = (void *)0x123456; + void *handle = reinterpret_cast(0x123456); EXPECT_CALL(*mockDlsym, dlopen(::testing::_, ::testing::_)).WillOnce(Return(handle)); auto result = assetLoader.Transfer(userId_, bundleName_, deviceId_, asset_); EXPECT_EQ(result, false); @@ -174,7 +174,7 @@ HWTEST_F(ObjectAssetSyncManagerTest, UploadTest002, TestSize.Level0) * @tc.name: TransferAsset_Success * @tc.desc: Should succeed when download returns success immediately. * @tc.type: FUNC - * @tc.require: + * @tc.require: */ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_Success, TestSize.Level0) { @@ -188,7 +188,7 @@ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_Success, TestSize.Level0) * @tc.name: TransferAsset_DirectFailure * @tc.desc: Should fail when DownloadFile returns error directly. * @tc.type: FUNC - * @tc.require: + * @tc.require: */ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_DirectFailure, TestSize.Level0) { @@ -202,7 +202,7 @@ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_DirectFailure, TestSize.Level * @tc.name: TransferAsset_CallbackFailure * @tc.desc: Should fail when download succeeds but callback returns error. * @tc.type: FUNC - * @tc.require: + * @tc.require: */ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_CallbackFailure, TestSize.Level0) { @@ -216,7 +216,7 @@ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_CallbackFailure, TestSize.Lev * @tc.name: TransferAsset_Timeout * @tc.desc: Should timeout when callback never called. * @tc.type: FUNC - * @tc.require: + * @tc.require: */ HWTEST_F(ObjectAssetSyncManagerTest, TransferAsset_Timeout, TestSize.Level1) { -- Gitee