From f5cf5bf5d2af332e5551b25ecab2b0d515717136 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 09:53:11 +0800 Subject: [PATCH 01/12] =?UTF-8?q?dataObject=E5=85=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=A0=A1=E9=AA=8C-=E6=96=B0=E8=80=81=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../route_head_handler_impl.cpp | 15 +- .../service/object/BUILD.gn | 1 + .../service/object/include/object_manager.h | 5 + .../service/object/src/object_manager.cpp | 77 ++++++- .../service/test/BUILD.gn | 67 ++++++ .../service/test/mock/BUILD.gn | 2 + .../service/test/mock/device_matrix_mock.cpp | 29 +++ .../service/test/mock/device_matrix_mock.h | 37 +++ .../test/mock/meta_data_manager_mock.cpp | 6 + .../test/mock/meta_data_manager_mock.h | 2 + .../service/test/object_manager_mock_test.cpp | 216 ++++++++++++++++++ .../service/test/object_manager_test.cpp | 29 --- .../service/test/object_service_impl_test.cpp | 27 ++- 13 files changed, 478 insertions(+), 35 deletions(-) create mode 100644 services/distributeddataservice/service/test/mock/device_matrix_mock.cpp create mode 100644 services/distributeddataservice/service/test/mock/device_matrix_mock.h create mode 100644 services/distributeddataservice/service/test/object_manager_mock_test.cpp diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 56d347e2e..1d45e771d 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -39,6 +39,8 @@ using DmAdapter = DistributedData::DeviceManagerAdapter; using DBManager = DistributedDB::KvStoreDelegateManager; constexpr const int ALIGN_WIDTH = 8; constexpr const char *DEFAULT_USERID = "0"; +constexpr const char *DATA_OBJECT_DB_STORE_ID = "distributedObject_"; + std::shared_ptr RouteHeadHandlerImpl::Create(const ExtendInfo &info) { auto handler = std::make_shared(info); @@ -100,11 +102,19 @@ std::string RouteHeadHandlerImpl::GetTargetUserId() DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize) { ZLOGD("begin"); - headSize = 0; if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ == Bootstrap::GetInstance().GetMetaDBName()) { ZLOGI("meta data permitted"); return DistributedDB::OK; } + // udmf名字待修改 + if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && + (storeId_ == udmf || storeId_ == DATA_OBJECT_DB_STORE_ID)) { + bool flag = false; + auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); + if (!flag) { + return DistributedDB::OK; + } + } if (!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { ZLOGD("devicdId:%{public}s is not oh type", Anonymous::Change(session_.targetDeviceId).c_str()); @@ -117,8 +127,6 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize } return DistributedDB::OK; } - bool flag = false; - auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); if (!flag) { ZLOGI("get peer cap failed"); return DistributedDB::DB_ERROR; @@ -134,6 +142,7 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize ZLOGI("no valid session to peer device"); return DistributedDB::DB_ERROR; } + headSize = 0; size_t expectSize = sizeof(RouteHead) + sizeof(SessionDevicePair) + sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int) + sizeof(SessionAppId) + session_.appId.size() + sizeof(SessionStoreId) + session_.storeId.size() + sizeof(SessionAccountId) + session_.accountId.size(); diff --git a/services/distributeddataservice/service/object/BUILD.gn b/services/distributeddataservice/service/object/BUILD.gn index 35412b733..7ebc67331 100644 --- a/services/distributeddataservice/service/object/BUILD.gn +++ b/services/distributeddataservice/service/object/BUILD.gn @@ -18,6 +18,7 @@ config("object_public_config") { include_dirs = [ "${data_service_path}/service/common", + "${data_service_path}/service/matrix/include", "${data_service_path}/adapter/include/communicator", "${data_service_path}/adapter/include/utils", ] diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index a5026a5d0..662c1ae08 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -21,6 +21,7 @@ #include "device_manager_adapter.h" #include "kv_store_delegate_manager.h" #include "kvstore_sync_callback.h" +#include "metadata/store_meta_data.h" #include "object_asset_loader.h" #include "object_callback.h" #include "object_callback_proxy.h" @@ -64,6 +65,7 @@ class ObjectStoreManager { public: using DmAdaper = OHOS::DistributedData::DeviceManagerAdapter; using UriToSnapshot = std::shared_ptr>>; + using StoreMetaData = OHOS::DistributedData::StoreMetaData; enum RestoreStatus : int32_t { NONE = 0, @@ -159,6 +161,7 @@ private: DistributedDB::KvStoreNbDelegate *OpenObjectKvStore(); void FlushClosedStore(); void Close(); + void ForceClose(); int32_t SetSyncStatus(bool status); int32_t SaveToStore(const std::string &appId, const std::string &sessionId, const std::string &toDeviceId, const ObjectRecord &data); @@ -171,6 +174,8 @@ private: void ProcessSyncCallback(const std::map &results, const std::string &appId, const std::string &sessionId, const std::string &deviceId); void SaveUserToMeta(); + bool IsNeedMetaSync(const StoreMetaData &meta, const std::vector &networkIds); + int32_t DoSync(const std::string &prefix, const std::vector &deviceList, uint64_t sequenceId); std::string GetCurrentUser(); void DoNotify(uint32_t tokenId, const CallbackInfo& value, const std::map& data, bool allReady); diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 8e08a4cf6..ccd0496fa 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -26,6 +26,9 @@ #include "common/string_utils.h" #include "datetime_ex.h" #include "distributed_file_daemon_manager.h" +#include "device_matrix.h" +#include "ipc_skeleton.h" +#include "metadata/capability_meta_data.h" #include "kvstore_utils.h" #include "log_print.h" #include "metadata/meta_data_manager.h" @@ -44,6 +47,8 @@ using Account = OHOS::DistributedData::AccountDelegate; using AccessTokenKit = Security::AccessToken::AccessTokenKit; using ValueProxy = OHOS::DistributedData::ValueProxy; using DistributedFileDaemonManager = Storage::DistributedFile::DistributedFileDaemonManager; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; + constexpr const char *SAVE_INFO = "p_###SAVEINFO###"; constexpr int32_t PROGRESS_MAX = 100; constexpr int32_t PROGRESS_INVALID = -1; @@ -319,7 +324,7 @@ int32_t ObjectStoreManager::Clear() result = RevokeSaveToStore(""); callbacks_.Clear(); processCallbacks_.Clear(); - Close(); + ForceClose(); return result; } @@ -571,6 +576,34 @@ void ObjectStoreManager::ComputeStatus(const std::string& objectKey, const SaveI }); } +bool ObjectStoreManager::IsNeedMetaSync(const StoreMetaData &meta, const std::vector &uuids) +{ + bool isAfterMeta = false; + for (const auto &uuid : uuids) { + auto metaData = meta; + metaData.deviceId = uuid; + CapMetaData capMeta; + auto capKey = CapMetaRow::GetKeyFor(uuid); + bool flag = !MetaDataManager::GetInstance().LoadMeta(std::string(capKey.begin(), capKey.end()), capMeta) || + !MetaDataManager::GetInstance().LoadMeta(metaData.GetKeyWithoutPath(), metaData); + if (flag) { + isAfterMeta = true; + break; + } + auto [exist, mask] = DeviceMatrix::GetInstance().GetRemoteMask(uuid); + if ((mask & DeviceMatrix::META_STORE_MASK) == DeviceMatrix::META_STORE_MASK) { + isAfterMeta = true; + break; + } + auto [existLocal, localMask] = DeviceMatrix::GetInstance().GetMask(uuid); + if ((localMask & DeviceMatrix::META_STORE_MASK) == DeviceMatrix::META_STORE_MASK) { + isAfterMeta = true; + break; + } + } + return isAfterMeta; +} + void ObjectStoreManager::NotifyDataChanged(const std::map& data, const SaveInfo& saveInfo) { for (auto const& [objectKey, results] : data) { @@ -852,6 +885,22 @@ int32_t ObjectStoreManager::Open() return OBJECT_SUCCESS; } +void ObjectStoreManager::ForceClose() +{ + std::lock_guard lock(kvStoreMutex_); + if (delegate_ == nullptr) { + return; + } + auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); + if (status != DistributedDB::DBStatus::OK) { + ZLOGE("GetEntries fail %{public}d", status); + return; + } + delegate_ == nullptr + taskCount = 0; + syncCount_ = 0; +} + void ObjectStoreManager::Close() { std::lock_guard lock(kvStoreMutex_); @@ -1006,10 +1055,34 @@ int32_t ObjectStoreManager::SyncOnStore( return OBJECT_SUCCESS; } uint64_t sequenceId = SequenceSyncManager::GetInstance()->AddNotifier(userId_, callback); + + int32_t id = AccountDelegate::GetInstance()->GetUserByToken(IPCSkeleton::GetCallingFullTokenID()); + StoreMetaData meta = StoreMetaData(std::to_string(id), Bootstrap::GetInstance().GetProcessLabel(), + DistributedObject::ObjectCommon::OBJECTSTORE_DB_STOREID); + auto uuids = DmAdapter::GetInstance().ToUUID(syncDevices); + bool isNeedMetaSync = IsNeedMetaSync(meta, uuids); + if (!isNeedMetaSync) { + return DoSync(prefix, syncDevices, sequenceId); + } + bool result = MetaDataManager::GetInstance().Sync(uuids, [this, prefix, syncDevices, sequenceId](auto &results) { + auto status = DoSync(prefix, syncDevices, sequenceId); + ZLOGI("Store sync end, status:%{public}d", status); + }); + ZLOGI("prefix:%{public}s, meta sync end, result:%{public}d", prefix.c_str(), result); + return result ? OBJECT_SUCCESS : DoSync(prefix, syncDevices, sequenceId); +} + +int32_t ObjectStoreManager::DoSync(const std::string &prefix, const std::vector &deviceList, + uint64_t sequenceId) +{ DistributedDB::Query dbQuery = DistributedDB::Query::Select(); dbQuery.PrefixKey(std::vector(prefix.begin(), prefix.end())); ZLOGI("Start sync data, sequenceId: 0x%{public}" PRIx64 "", sequenceId); - auto status = delegate_->Sync(syncDevices, DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY, + if (delegate_ == nullptr) { + ZLOGE("delegate_ == nullptr"); + return E_DB_ERROR; + } + auto status = delegate_->Sync(deviceList, DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY, [this, sequenceId](const std::map &devicesMap) { ZLOGI("Sync data finished, sequenceId: 0x%{public}" PRIx64 "", sequenceId); std::map result; diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index c93164adb..276c412d9 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -862,6 +862,72 @@ ohos_unittest("ObjectManagerTest") { ] } +ohos_unittest("ObjectManagerMockTest") { + module_out_path = module_output_path + sources = [ + "${data_service_path}/app/src/kvstore_meta_manager.cpp", + "${data_service_path}/service/common/common_types_utils.cpp", + "${data_service_path}/service/common/value_proxy.cpp", + "../object/src/object_asset_loader.cpp", + "../object/src/object_asset_machine.cpp", + "../object/src/object_callback_proxy.cpp", + "../object/src/object_data_listener.cpp", + "../object/src/object_dms_handler.cpp", + "../object/src/object_manager.cpp", + "../object/src/object_service_impl.cpp", + "../object/src/object_service_stub.cpp", + "../object/src/object_snapshot.cpp", + "../object/src/object_types_utils.cpp", + "mock/device_manager_adapter_mock.cpp", + "mock/kv_store_nb_delegate_mock.cpp", + "object_manager_mock_test.cpp", + "mock/meta_data_manager_mock.cpp", + "mock/device_matrix_mock.cpp", + ] + + include_dirs = [ + "${data_service_path}/adapter/include/utils", + "${data_service_path}/app/src", + "${data_service_path}/service/common", + "${data_service_path}/service/test/mock", + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", + "c_utils:utils", + "data_object:distributeddataobject_impl", + "data_object:data_object_inner", + "dataclassification:data_transit_mgr", + "dfs_service:cloudsync_asset_kit_inner", + "dfs_service:distributed_file_daemon_kit_inner", + "dmsfwk:distributed_sdk", + "googletest:gmock_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "cJSON:cjson", + "kv_store:distributeddata_inner", + "kv_store:distributeddata_mgr", + "kv_store:distributeddb", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + ] + + deps = [ + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] +} + ohos_unittest("ObjectSnapshotTest") { module_out_path = module_output_path sources = [ @@ -2240,6 +2306,7 @@ group("unittest") { ":ObjectDmsHandlerTest", ":ObjectManagerTest", ":ObjectSnapshotTest", + ":ObjectManagerMockTest", ] } diff --git a/services/distributeddataservice/service/test/mock/BUILD.gn b/services/distributeddataservice/service/test/mock/BUILD.gn index ce0a05278..3f49d2d2e 100644 --- a/services/distributeddataservice/service/test/mock/BUILD.gn +++ b/services/distributeddataservice/service/test/mock/BUILD.gn @@ -19,6 +19,7 @@ config("module_private_config") { include_dirs = [ "${data_service_path}/adapter/include/communicator", "${data_service_path}/service/kvdb", + "${data_service_path}/service/matrix/include/", "../../../framework/include/", "../../../service/rdb/", "./", @@ -48,6 +49,7 @@ ohos_static_library("distributeddata_mock_static") { "network_delegate_mock.cpp", "screen_lock_mock.cpp", "user_delegate_mock.cpp", + "device_matrix_mock.cpp", ] deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] diff --git a/services/distributeddataservice/service/test/mock/device_matrix_mock.cpp b/services/distributeddataservice/service/test/mock/device_matrix_mock.cpp new file mode 100644 index 000000000..4cba3f335 --- /dev/null +++ b/services/distributeddataservice/service/test/mock/device_matrix_mock.cpp @@ -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. + */ +#include "device_matrix_mock.h" + +namespace OHOS { +namespace DistributedData { +std::pair DeviceMatrix::GetRemoteMask(const std::string &device, DeviceMatrix::LevelType type) +{ + return BDeviceMatrix::deviceMatrix->GetRemoteMask(device, type); +} + +std::pair DeviceMatrix::GetMask(const std::string &device, DeviceMatrix::LevelType type) +{ + return BDeviceMatrix::deviceMatrix->GetMask(device, type); +} +} // namespace DistributedData +} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/device_matrix_mock.h b/services/distributeddataservice/service/test/mock/device_matrix_mock.h new file mode 100644 index 000000000..f67eb94ca --- /dev/null +++ b/services/distributeddataservice/service/test/mock/device_matrix_mock.h @@ -0,0 +1,37 @@ +/* + * 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_DEVICE_MATRIX_MOCK_H +#define OHOS_DEVICE_MATRIX_MOCK_H + +#include + +#include "device_matrix.h" + +namespace OHOS::DistributedData { +class BDeviceMatrix { +public: + virtual std::pair GetMask(const std::string &, DeviceMatrix::LevelType); + virtual std::pair GetRemoteMask(const std::string &, DeviceMatrix::LevelType); + BDeviceMatrix() = default; + virtual ~BDeviceMatrix() = default; + static inline std::shared_ptr deviceMatrix = nullptr; +}; +class DeviceMatrixMock : public BDeviceMatrix { +public: + MOCK_METHOD((std::pair), GetMask, (const std::string &, DeviceMatrix::LevelType), (override)); + MOCK_METHOD((std::pair), GetRemoteMask, (const std::string &, DeviceMatrix::LevelType), (override)); +}; +} // namespace OHOS::DistributedData +#endif //OHOS_DEVICE_MATRIX_MOCK_H diff --git a/services/distributeddataservice/service/test/mock/meta_data_manager_mock.cpp b/services/distributeddataservice/service/test/mock/meta_data_manager_mock.cpp index 634760740..fff2933da 100644 --- a/services/distributeddataservice/service/test/mock/meta_data_manager_mock.cpp +++ b/services/distributeddataservice/service/test/mock/meta_data_manager_mock.cpp @@ -40,6 +40,12 @@ bool OHOS::DistributedData::MetaDataManager::LoadMeta(const std::string &key, Se return BMetaDataManager::metaDataManager->LoadMeta(key, value, isLocal); } +bool OHOS::DistributedData::MetaDataManager::Sync(const std::vector &devices, + MetaDataManager::OnComplete complete, bool wait) +{ + return BMetaDataManager::metaDataManager->Sync(devices, complete, wait); +} + template<> bool OHOS::DistributedData::MetaDataManager::LoadMeta( const std::string &prefix, std::vector &values, bool isLocal) diff --git a/services/distributeddataservice/service/test/mock/meta_data_manager_mock.h b/services/distributeddataservice/service/test/mock/meta_data_manager_mock.h index 6db1bf937..42926ab92 100644 --- a/services/distributeddataservice/service/test/mock/meta_data_manager_mock.h +++ b/services/distributeddataservice/service/test/mock/meta_data_manager_mock.h @@ -26,6 +26,7 @@ namespace OHOS::DistributedData { class BMetaDataManager { public: virtual bool LoadMeta(const std::string &, Serializable &, bool) = 0; + virtual bool Sync(const std::vector &, MetaDataManager::OnComplete, bool) = 0; BMetaDataManager() = default; virtual ~BMetaDataManager() = default; static inline std::shared_ptr metaDataManager = nullptr; @@ -33,6 +34,7 @@ public: class MetaDataManagerMock : public BMetaDataManager { public: MOCK_METHOD(bool, LoadMeta, (const std::string &, Serializable &, bool), (override)); + MOCK_METHOD(bool, Sync, (const std::vector &, MetaDataManager::OnComplete, bool), (override)); }; template class BMetaData { diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp new file mode 100644 index 000000000..085afd0b9 --- /dev/null +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -0,0 +1,216 @@ +/* +* 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 "ObjectManagerMockTest" +#include "object_manager.h" + +#include +#include "gtest/gtest.h" + +#include "device_matrix_mock.h" +#include "mock/meta_data_manager_mock.h" +#include "device_manager_adapter_mock.h" + +using namespace OHOS::DistributedObject; +using namespace OHOS::DistributedData; +using namespace testing::ext; +using namespace testing; +using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; +using OnComplete = OHOS::DistributedData::MetaDataManager::OnComplete; + +namespace OHOS::Test { +namespace DistributedDataTest { +class ObjectManagerMockTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + metaDataManagerMock = std::make_shared(); + BMetaDataManager::metaDataManager = metaDataManagerMock; + metaDataMock = std::make_shared>(); + BMetaData::metaDataManager = metaDataMock; + devMgrAdapterMock = std::make_shared(); + BDeviceManagerAdapter::deviceManagerAdapter = devMgrAdapterMock; + deviceMatrixMock = std::make_shared(); + BDeviceMatrix::deviceMatrix = deviceMatrixMock; + } + static void TearDownTestCase(void) + { + metaDataManagerMock = nullptr; + BMetaDataManager::metaDataManager = nullptr; + metaDataMock = nullptr; + BMetaData::metaDataManager = nullptr; + devMgrAdapterMock = nullptr; + BDeviceManagerAdapter::deviceManagerAdapter = nullptr; + deviceMatrixMock = nullptr; + BDeviceMatrix::deviceMatrix = nullptr; + } + + static inline std::shared_ptr metaDataManagerMock = nullptr; + static inline std::shared_ptr> metaDataMock = nullptr; + static inline std::shared_ptr devMgrAdapterMock = nullptr; + static inline std::shared_ptr deviceMatrixMock = nullptr; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: IsNeedMetaSync001 + * @tc.desc: Test IsNeedMetaSync when LoadMeta fails for CapMetaData. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhaojh + */ +HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync001, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + StoreMetaData meta; + meta.deviceId = "test_device_id"; + meta.user = "0"; + meta.storeId = "distributedObject_"; + meta.bundleName = "test_bundle"; + std::vector uuids = {"test_uuid"}; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(false)); + bool isNeedSync = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(isNeedSync, true); + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(true)); + isNeedSync = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(isNeedSync, true); + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(true)) + .WillOnce(testing::Return(false)); + isNeedSync = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(isNeedSync, true); +} + +/** + * @tc.name: IsNeedMetaSync002 + * @tc.desc: Test IsNeedMetaSync when LoadMeta fails for StoreMetaData. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhaojh + */ +HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync002, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + StoreMetaData meta; + meta.deviceId = "test_device_id"; + meta.user = "0"; + meta.storeId = "distributedObject_"; + meta.bundleName = "test_bundle"; + + std::vector uuids = {"test_uuid"}; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillRepeatedly(Return((true))); + + EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)) + .WillRepeatedly(Return(std::make_pair(true, DeviceMatrix::META_STORE_MASK))); + + bool result = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(result, true); +} + +/** + * @tc.name: IsNeedMetaSync003 + * @tc.desc: Test IsNeedMetaSync when LoadMeta fails for StoreMetaData. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhaojh + */ +HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + StoreMetaData meta; + meta.deviceId = "test_device_id"; + meta.user = "0"; + meta.storeId = "distributedObject_"; + meta.bundleName = "test_bundle"; + + std::vector uuids = {"test_uuid"}; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillRepeatedly(Return(true)); + + EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)) + .WillOnce(Return(std::make_pair(true, 0))); + + EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)) + .WillOnce(Return(std::make_pair(true, DeviceMatrix::META_STORE_MASK))); + + bool result = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(result, true); +} + +HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + auto func = [](const std::map &results) { + return results; + }; + std::string prefix = "ObjectManagerTest"; + StoreMetaData meta; + meta.deviceId = "test_device_id"; + meta.user = "0"; + meta.storeId = "distributedObject_"; + meta.bundleName = "test_bundle"; + std::vector uuids = {"test_uuid"}; + + + // local device + { + std::vector localDeviceList = {"local"}; + auto result = manager->SyncOnStore(prefix, localDeviceList, func); + EXPECT_EQ(result, OBJECT_SUCCESS); + } + + // remote device. IsNeedMetaSync: true; Sync: true + { + std::vector remoteDeviceList = {"remote_device_1"}; + + EXPECT_CALL(*devMgrAdapterMock, GetUuidByNetworkId(_)).WillRepeatedly(Return("mock_uuid")); + EXPECT_CALL(*devMgrAdapterMock, ToUUID(_)) + .WillOnce(Return(std::vector{"mock_uuid_1"})); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(false)); + EXPECT_CALL(*metaDataManagerMock, Sync(_, _, _)).WillOnce(testing::Return(true)); + auto result = manager->SyncOnStore(prefix, remoteDeviceList, func); + EXPECT_EQ(result, OBJECT_SUCCESS); + } + + // remote device. IsNeedMetaSync: true; Sync: false + { + std::vector remoteDeviceList = {"remote_device_1"}; + + EXPECT_CALL(*devMgrAdapterMock, GetUuidByNetworkId(_)).WillRepeatedly(Return("mock_uuid")); + EXPECT_CALL(*devMgrAdapterMock, ToUUID(_)) + .WillOnce(Return(std::vector{"mock_uuid_1"})); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(false)); + EXPECT_CALL(*metaDataManagerMock, Sync(_, _, _)).WillOnce(testing::Return(false)); + auto result = manager->SyncOnStore(prefix, remoteDeviceList, func); + EXPECT_EQ(result, E_DB_ERROR); + } +} +}; // namespace OHOS::Test +} \ No newline at end of file diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index e68fbf262..838ee4020 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -529,35 +529,6 @@ HWTEST_F(ObjectManagerTest, Close001, TestSize.Level0) ASSERT_EQ(manager->syncCount_, 0); // 0 is for testing } -/** -* @tc.name: SyncOnStore001 -* @tc.desc: SyncOnStore test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: wangbin -*/ -HWTEST_F(ObjectManagerTest, SyncOnStore001, TestSize.Level0) -{ - auto manager = ObjectStoreManager::GetInstance(); - manager->delegate_ = manager->OpenObjectKvStore(); - std::function &results)> func; - func = [](const std::map &results) { - return results; - }; - std::string prefix = "ObjectManagerTest"; - std::vector deviceList; - // not local device & syncDevices empty - deviceList.push_back("local1"); - EXPECT_CALL(*devMgrAdapterMock, IsSameAccount(_)).WillOnce(Return(true)); - auto result = manager->SyncOnStore(prefix, deviceList, func); - ASSERT_NE(result, OBJECT_SUCCESS); - // local device - deviceList.push_back("local"); - EXPECT_CALL(*devMgrAdapterMock, IsSameAccount(_)).WillOnce(Return(true)); - result = manager->SyncOnStore(prefix, deviceList, func); - ASSERT_EQ(result, OBJECT_SUCCESS); -} - /** * @tc.name: RetrieveFromStore001 * @tc.desc: RetrieveFromStore test. diff --git a/services/distributeddataservice/service/test/object_service_impl_test.cpp b/services/distributeddataservice/service/test/object_service_impl_test.cpp index 11a66c41f..d433822af 100644 --- a/services/distributeddataservice/service/test/object_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/object_service_impl_test.cpp @@ -20,11 +20,14 @@ #include #include "accesstoken_kit.h" +#include "device_manager_adapter_mock.h" #include "ipc_skeleton.h" #include "token_setproc.h" using namespace testing::ext; using namespace OHOS::DistributedObject; +using namespace std; +using namespace testing; namespace OHOS::Test { class ObjectServiceImplTest : public testing::Test { public: @@ -45,6 +48,7 @@ protected: ObjectStore::AssetBindInfo assetBindInfo_; pid_t pid_ = 10; uint32_t tokenId_ = 100; + static inline std::shared_ptr devMgrAdapterMock = nullptr; }; void ObjectServiceImplTest::SetUp() @@ -73,9 +77,14 @@ void ObjectServiceImplTest::SetUp() .assetName = "asset1.jpg", }; assetBindInfo_ = AssetBindInfo; + devMgrAdapterMock = make_shared(); + BDeviceManagerAdapter::deviceManagerAdapter = devMgrAdapterMock; } -void ObjectServiceImplTest::TearDown() {} +void ObjectServiceImplTest::TearDown() +{ + devMgrAdapterMock = nullptr; +} /** * @tc.name: OnAssetChanged001 @@ -152,6 +161,22 @@ HWTEST_F(ObjectServiceImplTest, ResolveAutoLaunch001, TestSize.Level1) EXPECT_EQ(ret, OBJECT_SUCCESS); } +/** + * @tc.name: OnInitialize001 + * @tc.desc: OnInitialize test. + * @tc.type: FUNC + */ +HWTEST_F(ObjectServiceImplTest, OnInitialize001, TestSize.Level1) +{ + std::shared_ptr objectServiceImpl = std::make_shared(); + objectServiceImpl->executors_ = nullptr; + DeviceInfo devInfo = { .uuid = "123" }; + EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()) + .WillOnce(Return(devInfo)); + int32_t ret = objectServiceImpl->OnInitialize(); + EXPECT_EQ(ret, OBJECT_INNER_ERROR); +} + /** * @tc.name: RegisterProgressObserver001 * @tc.desc: RegisterProgressObserver test. -- Gitee From 29bde7e70dd1f338c3333deac92778adcfd591ad Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 11:34:58 +0800 Subject: [PATCH 02/12] TDD Signed-off-by: Hollokin --- .../route_head_handler_impl.cpp | 9 ++++----- .../service/object/src/object_manager.cpp | 2 +- .../test/kvdb_general_store_abnormal_test.cpp | 4 ++-- .../test/mock/device_manager_adapter_mock.cpp | 9 +++++++++ .../test/mock/device_manager_adapter_mock.h | 2 ++ .../service/test/object_manager_mock_test.cpp | 19 ++++++++++++------- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 1d45e771d..137be1823 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -40,6 +40,7 @@ using DBManager = DistributedDB::KvStoreDelegateManager; constexpr const int ALIGN_WIDTH = 8; constexpr const char *DEFAULT_USERID = "0"; constexpr const char *DATA_OBJECT_DB_STORE_ID = "distributedObject_"; +constexpr const char *UDMF_DB_STORE_ID = "drag"; std::shared_ptr RouteHeadHandlerImpl::Create(const ExtendInfo &info) { @@ -102,13 +103,13 @@ std::string RouteHeadHandlerImpl::GetTargetUserId() DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize) { ZLOGD("begin"); + headSize = 0; if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ == Bootstrap::GetInstance().GetMetaDBName()) { ZLOGI("meta data permitted"); return DistributedDB::OK; } - // udmf名字待修改 if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && - (storeId_ == udmf || storeId_ == DATA_OBJECT_DB_STORE_ID)) { + (storeId_ == UDMF_DB_STORE_ID || storeId_ == DATA_OBJECT_DB_STORE_ID)) { bool flag = false; auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); if (!flag) { @@ -116,8 +117,7 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize } } if (!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { - ZLOGD("devicdId:%{public}s is not oh type", - Anonymous::Change(session_.targetDeviceId).c_str()); + ZLOGD("devicdId:%{public}s is not oh type", Anonymous::Change(session_.targetDeviceId).c_str()); if (appId_.empty()) { return DistributedDB::DB_ERROR; } @@ -142,7 +142,6 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize ZLOGI("no valid session to peer device"); return DistributedDB::DB_ERROR; } - headSize = 0; size_t expectSize = sizeof(RouteHead) + sizeof(SessionDevicePair) + sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int) + sizeof(SessionAppId) + session_.appId.size() + sizeof(SessionStoreId) + session_.storeId.size() + sizeof(SessionAccountId) + session_.accountId.size(); diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index ccd0496fa..0ee980f30 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -896,7 +896,7 @@ void ObjectStoreManager::ForceClose() ZLOGE("GetEntries fail %{public}d", status); return; } - delegate_ == nullptr + delegate_ = nullptr; taskCount = 0; syncCount_ = 0; } diff --git a/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp index 7be5d9029..dea3bb04a 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp @@ -132,9 +132,9 @@ HWTEST_F(KVDBGeneralStoreAbnormalTest, SetEqualIdentifier, TestSize.Level0) mockDelegate.taskCountMock_ = 1; store->delegate_ = &mockDelegate; EXPECT_NE(store->delegate_, nullptr); - EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::_)).WillOnce(testing::Return(uuids)); + EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::A>())).WillOnce(testing::Return(uuids)); auto uuids1 = DMAdapter::ToUUID(DMAdapter::GetInstance().GetRemoteDevices()); - EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::_)).WillOnce(testing::Return(uuids)); + EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::A>())).WillOnce(testing::Return(uuids)); EXPECT_CALL(*deviceManagerAdapterMock, IsOHOSType("uuidtest01")) .WillOnce(testing::Return(false)).WillOnce(testing::Return(false)); store->SetEqualIdentifier(metaData_.appId, metaData_.storeId); diff --git a/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.cpp b/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.cpp index 7bc2b7e82..5924460d9 100644 --- a/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.cpp +++ b/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.cpp @@ -34,6 +34,15 @@ std::vector OHOS::DistributedData::DeviceManagerAdapter::ToUUID(std return BDeviceManagerAdapter::deviceManagerAdapter->ToUUID(devices); } +std::vector OHOS::DistributedData::DeviceManagerAdapter::ToUUID(const std::vector &devices) +{ + if (BDeviceManagerAdapter::deviceManagerAdapter == nullptr) { + std::vector vec; + return vec; + } + return BDeviceManagerAdapter::deviceManagerAdapter->ToUUID(devices); +} + bool OHOS::DistributedData::DeviceManagerAdapter::IsOHOSType(const std::string &id) { if (BDeviceManagerAdapter::deviceManagerAdapter == nullptr) { diff --git a/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.h b/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.h index 2a47a5510..d423327a7 100644 --- a/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.h +++ b/services/distributeddataservice/service/test/mock/device_manager_adapter_mock.h @@ -30,6 +30,7 @@ public: virtual std::vector GetRemoteDevices() = 0; virtual bool IsOHOSType(const std::string &) = 0; virtual std::vector ToUUID(std::vector) = 0; + virtual std::vector ToUUID(const std::vector &) = 0; virtual Status StartWatchDeviceChange(const AppDeviceChangeListener *, const PipeInfo &) = 0; virtual Status StopWatchDeviceChange(const AppDeviceChangeListener *, const PipeInfo &) = 0; virtual bool IsSameAccount(const AccessCaller &, const AccessCallee &) = 0; @@ -49,6 +50,7 @@ public: MOCK_METHOD(std::vector, GetRemoteDevices, ()); MOCK_METHOD(bool, IsOHOSType, (const std::string &)); MOCK_METHOD((std::vector), ToUUID, (std::vector)); + MOCK_METHOD((std::vector), ToUUID, (const std::vector &)); MOCK_METHOD(Status, StartWatchDeviceChange, (const AppDeviceChangeListener *, const PipeInfo &)); MOCK_METHOD(Status, StopWatchDeviceChange, (const AppDeviceChangeListener *, const PipeInfo &)); MOCK_METHOD(bool, IsSameAccount, (const AccessCaller &, const AccessCallee &)); diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 085afd0b9..f8833b1f4 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -83,17 +83,10 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync001, TestSize.Level0) std::vector uuids = {"test_uuid"}; EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) - .WillOnce(testing::Return(false)) .WillOnce(testing::Return(false)); bool isNeedSync = manager->IsNeedMetaSync(meta, uuids); EXPECT_EQ(isNeedSync, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) - .WillOnce(testing::Return(false)) - .WillOnce(testing::Return(true)); - isNeedSync = manager->IsNeedMetaSync(meta, uuids); - EXPECT_EQ(isNeedSync, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) .WillOnce(testing::Return(true)) .WillOnce(testing::Return(false)); @@ -158,6 +151,18 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) bool result = manager->IsNeedMetaSync(meta, uuids); EXPECT_EQ(result, true); + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillRepeatedly(Return(true)); + + EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)) + .WillOnce(Return(std::make_pair(true, 0))); + + EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)) + .WillOnce(Return(std::make_pair(true, 0))); + + result = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(result, faklse); } HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) -- Gitee From de12e17f826d0bb312227af8af687a22d518d354 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 12:16:41 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../route_head_handler_impl.cpp | 38 ++++++++----------- .../service/object/src/object_manager.cpp | 2 +- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 137be1823..f26d55830 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -115,6 +115,13 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize if (!flag) { return DistributedDB::OK; } + if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && + peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { + // older versions ignore pack extend head + ZLOGI("ignore older version device, appId:%{public}s, version:%{public}d", + appId_.c_str(), peerCap.version); + return DistributedDB::OK; + } } if (!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { ZLOGD("devicdId:%{public}s is not oh type", Anonymous::Change(session_.targetDeviceId).c_str()); @@ -127,17 +134,6 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize } return DistributedDB::OK; } - if (!flag) { - ZLOGI("get peer cap failed"); - return DistributedDB::DB_ERROR; - } - if ((appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ != Bootstrap::GetInstance().GetMetaDBName() - && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) - || peerCap.version == CapMetaData::INVALID_VERSION) { - // older versions ignore pack extend head - ZLOGI("ignore older version device, appId:%{public}s, version:%{public}d", appId_.c_str(), peerCap.version); - return DistributedDB::OK; - } if (!session_.IsValid()) { ZLOGI("no valid session to peer device"); return DistributedDB::DB_ERROR; @@ -289,17 +285,15 @@ bool RouteHeadHandlerImpl::ParseHeadDataLen(const uint8_t *data, uint32_t totalL ZLOGI("other type device received. device:%{public}s", Anonymous::Change(device).c_str()); return true; } - - bool flag = false; - auto peerCap = UpgradeManager::GetInstance().GetCapability(device, flag); - if (!flag) { - ZLOGI("get peer cap failed"); - return false; - } - if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ != Bootstrap::GetInstance().GetMetaDBName() - && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { - ZLOGI("ignore older version device for udmf or object"); - return false; + + if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && + (storeId_ == UDMF_DB_STORE_ID || storeId_ == DATA_OBJECT_DB_STORE_ID) { + bool flag = false; + auto peerCap = UpgradeManager::GetInstance().GetCapability(device, flag); + if (flag && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { + ZLOGI("get peer cap failed"); + return false; + } } RouteHead head = { 0 }; diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 0ee980f30..969e35ed2 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -893,7 +893,7 @@ void ObjectStoreManager::ForceClose() } auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); if (status != DistributedDB::DBStatus::OK) { - ZLOGE("GetEntries fail %{public}d", status); + ZLOGE("CloseKvStore fail %{public}d", status); return; } delegate_ = nullptr; -- Gitee From 69b26095e339deee2d7368c3905ee858be16d01e Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 12:33:20 +0800 Subject: [PATCH 04/12] update Signed-off-by: Hollokin --- .../route_head_handler_impl.cpp | 22 ++++++++----------- .../service/object/src/object_manager.cpp | 12 +++++----- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index f26d55830..566aed39a 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -39,8 +39,8 @@ using DmAdapter = DistributedData::DeviceManagerAdapter; using DBManager = DistributedDB::KvStoreDelegateManager; constexpr const int ALIGN_WIDTH = 8; constexpr const char *DEFAULT_USERID = "0"; -constexpr const char *DATA_OBJECT_DB_STORE_ID = "distributedObject_"; -constexpr const char *UDMF_DB_STORE_ID = "drag"; +constexpr const char *DATA_OBJECT_STORE_ID = "distributedObject_"; +constexpr const char *UDMF_STORE_ID = "drag"; std::shared_ptr RouteHeadHandlerImpl::Create(const ExtendInfo &info) { @@ -104,22 +104,18 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize { ZLOGD("begin"); headSize = 0; - if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ == Bootstrap::GetInstance().GetMetaDBName()) { + if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ != UDMF_STORE_ID && + storeId_ != DATA_OBJECT_STORE_ID) { ZLOGI("meta data permitted"); return DistributedDB::OK; } if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && - (storeId_ == UDMF_DB_STORE_ID || storeId_ == DATA_OBJECT_DB_STORE_ID)) { + (storeId_ == UDMF_STORE_ID || storeId_ == DATA_OBJECT_STORE_ID)) { bool flag = false; auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); - if (!flag) { - return DistributedDB::OK; - } - if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && - peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { + if (!flag || peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { // older versions ignore pack extend head - ZLOGI("ignore older version device, appId:%{public}s, version:%{public}d", - appId_.c_str(), peerCap.version); + ZLOGI("ignore older version device, flag:%{public}d, version:%{public}d", flag, peerCap.version); return DistributedDB::OK; } } @@ -287,11 +283,11 @@ bool RouteHeadHandlerImpl::ParseHeadDataLen(const uint8_t *data, uint32_t totalL } if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && - (storeId_ == UDMF_DB_STORE_ID || storeId_ == DATA_OBJECT_DB_STORE_ID) { + (storeId_ == UDMF_STORE_ID || storeId_ == DATA_OBJECT_STORE_ID)) { bool flag = false; auto peerCap = UpgradeManager::GetInstance().GetCapability(device, flag); if (flag && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { - ZLOGI("get peer cap failed"); + ZLOGI("get peer cap success, peerCap's version is %{public}d, less then 3", peerCap.version); return false; } } diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 969e35ed2..2afc18659 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -897,7 +897,7 @@ void ObjectStoreManager::ForceClose() return; } delegate_ = nullptr; - taskCount = 0; + isSyncing_ = false; syncCount_ = 0; } @@ -1066,7 +1066,7 @@ int32_t ObjectStoreManager::SyncOnStore( } bool result = MetaDataManager::GetInstance().Sync(uuids, [this, prefix, syncDevices, sequenceId](auto &results) { auto status = DoSync(prefix, syncDevices, sequenceId); - ZLOGI("Store sync end, status:%{public}d", status); + ZLOGI("Store sync after meta sync end, status:%{public}d", status); }); ZLOGI("prefix:%{public}s, meta sync end, result:%{public}d", prefix.c_str(), result); return result ? OBJECT_SUCCESS : DoSync(prefix, syncDevices, sequenceId); @@ -1075,13 +1075,13 @@ int32_t ObjectStoreManager::SyncOnStore( int32_t ObjectStoreManager::DoSync(const std::string &prefix, const std::vector &deviceList, uint64_t sequenceId) { - DistributedDB::Query dbQuery = DistributedDB::Query::Select(); - dbQuery.PrefixKey(std::vector(prefix.begin(), prefix.end())); - ZLOGI("Start sync data, sequenceId: 0x%{public}" PRIx64 "", sequenceId); if (delegate_ == nullptr) { - ZLOGE("delegate_ == nullptr"); + ZLOGE("db store was closed."); return E_DB_ERROR; } + DistributedDB::Query dbQuery = DistributedDB::Query::Select(); + dbQuery.PrefixKey(std::vector(prefix.begin(), prefix.end())); + ZLOGI("Start sync data, sequenceId: 0x%{public}" PRIx64 "", sequenceId); auto status = delegate_->Sync(deviceList, DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY, [this, sequenceId](const std::map &devicesMap) { ZLOGI("Sync data finished, sequenceId: 0x%{public}" PRIx64 "", sequenceId); -- Gitee From ff20e20cecff7f5df7aff43a037d0b2886930bb2 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 14:17:08 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/test/object_manager_mock_test.cpp | 79 +++++++------------ 1 file changed, 30 insertions(+), 49 deletions(-) diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index f8833b1f4..92022300c 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -14,14 +14,13 @@ */ #define LOG_TAG "ObjectManagerMockTest" -#include "object_manager.h" - #include -#include "gtest/gtest.h" +#include "device_manager_adapter_mock.h" #include "device_matrix_mock.h" +#include "gtest/gtest.h" #include "mock/meta_data_manager_mock.h" -#include "device_manager_adapter_mock.h" +#include "object_manager.h" using namespace OHOS::DistributedObject; using namespace OHOS::DistributedData; @@ -61,8 +60,8 @@ public: static inline std::shared_ptr> metaDataMock = nullptr; static inline std::shared_ptr devMgrAdapterMock = nullptr; static inline std::shared_ptr deviceMatrixMock = nullptr; - void SetUp() {}; - void TearDown() {}; + void SetUp(){}; + void TearDown(){}; }; /** @@ -80,13 +79,11 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync001, TestSize.Level0) meta.user = "0"; meta.storeId = "distributedObject_"; meta.bundleName = "test_bundle"; - std::vector uuids = {"test_uuid"}; + std::vector uuids = { "test_uuid" }; - EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) - .WillOnce(testing::Return(false)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)).WillOnce(testing::Return(false)); bool isNeedSync = manager->IsNeedMetaSync(meta, uuids); EXPECT_EQ(isNeedSync, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) .WillOnce(testing::Return(true)) .WillOnce(testing::Return(false)); @@ -109,15 +106,12 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync002, TestSize.Level0) meta.user = "0"; meta.storeId = "distributedObject_"; meta.bundleName = "test_bundle"; - - std::vector uuids = {"test_uuid"}; - - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillRepeatedly(Return((true))); + std::vector uuids = { "test_uuid" }; + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)).WillRepeatedly(Return((true))); EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)) .WillRepeatedly(Return(std::make_pair(true, DeviceMatrix::META_STORE_MASK))); - + bool result = manager->IsNeedMetaSync(meta, uuids); EXPECT_EQ(result, true); } @@ -137,63 +131,51 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) meta.user = "0"; meta.storeId = "distributedObject_"; meta.bundleName = "test_bundle"; - - std::vector uuids = {"test_uuid"}; + std::vector uuids = { "test_uuid" }; - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillRepeatedly(Return(true)); - - EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)) - .WillOnce(Return(std::make_pair(true, 0))); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)).WillRepeatedly(Return(true)); + EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)).WillOnce(Return(std::make_pair(true, 0))); + EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)).WillOnce(Return(std::make_pair(true, DeviceMatrix::META_STORE_MASK))); - EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)) - .WillOnce(Return(std::make_pair(true, DeviceMatrix::META_STORE_MASK))); - bool result = manager->IsNeedMetaSync(meta, uuids); EXPECT_EQ(result, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillRepeatedly(Return(true)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)).WillRepeatedly(Return(true)); - EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)) - .WillOnce(Return(std::make_pair(true, 0))); + EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)).WillOnce(Return(std::make_pair(true, 0))); - EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)) - .WillOnce(Return(std::make_pair(true, 0))); + EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)).WillOnce(Return(std::make_pair(true, 0))); result = manager->IsNeedMetaSync(meta, uuids); - EXPECT_EQ(result, faklse); + EXPECT_EQ(result, false); } HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) { auto manager = ObjectStoreManager::GetInstance(); - auto func = [](const std::map &results) { - return results; - }; + std::function &results)> func; + func = [](const std::map &results) { return results; }; std::string prefix = "ObjectManagerTest"; StoreMetaData meta; meta.deviceId = "test_device_id"; meta.user = "0"; meta.storeId = "distributedObject_"; meta.bundleName = "test_bundle"; - std::vector uuids = {"test_uuid"}; - + std::vector uuids = { "test_uuid" }; // local device { - std::vector localDeviceList = {"local"}; + std::vector localDeviceList = { "local" }; auto result = manager->SyncOnStore(prefix, localDeviceList, func); EXPECT_EQ(result, OBJECT_SUCCESS); } // remote device. IsNeedMetaSync: true; Sync: true { - std::vector remoteDeviceList = {"remote_device_1"}; - + std::vector remoteDeviceList = { "remote_device_1" }; EXPECT_CALL(*devMgrAdapterMock, GetUuidByNetworkId(_)).WillRepeatedly(Return("mock_uuid")); - EXPECT_CALL(*devMgrAdapterMock, ToUUID(_)) - .WillOnce(Return(std::vector{"mock_uuid_1"})); + EXPECT_CALL(*devMgrAdapterMock, ToUUID(testing::A &>())) + .WillOnce(Return(std::vector{ "mock_uuid_1" })); EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) .WillOnce(testing::Return(false)) .WillOnce(testing::Return(false)); @@ -204,11 +186,10 @@ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) // remote device. IsNeedMetaSync: true; Sync: false { - std::vector remoteDeviceList = {"remote_device_1"}; - + std::vector remoteDeviceList = { "remote_device_1" }; EXPECT_CALL(*devMgrAdapterMock, GetUuidByNetworkId(_)).WillRepeatedly(Return("mock_uuid")); - EXPECT_CALL(*devMgrAdapterMock, ToUUID(_)) - .WillOnce(Return(std::vector{"mock_uuid_1"})); + EXPECT_CALL(*devMgrAdapterMock, ToUUID(testing::A &>())) + .WillOnce(Return(std::vector{ "mock_uuid_1" })); EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) .WillOnce(testing::Return(false)) .WillOnce(testing::Return(false)); @@ -217,5 +198,5 @@ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) EXPECT_EQ(result, E_DB_ERROR); } } -}; // namespace OHOS::Test -} \ No newline at end of file +}; // namespace DistributedDataTest +} // namespace OHOS::Test \ No newline at end of file -- Gitee From edc9648239d487fb5629d4dc9007ddde2ed2a28d Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 15:17:59 +0800 Subject: [PATCH 06/12] update Signed-off-by: Hollokin --- .../route_head_handler_impl.cpp | 30 +++++++++++-------- .../service/object/src/object_manager.cpp | 2 +- .../test/kvdb_general_store_abnormal_test.cpp | 11 ++++--- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 566aed39a..42c98cfc6 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -109,18 +109,8 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize ZLOGI("meta data permitted"); return DistributedDB::OK; } - if (appId_ == Bootstrap::GetInstance().GetProcessLabel() && - (storeId_ == UDMF_STORE_ID || storeId_ == DATA_OBJECT_STORE_ID)) { - bool flag = false; - auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); - if (!flag || peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { - // older versions ignore pack extend head - ZLOGI("ignore older version device, flag:%{public}d, version:%{public}d", flag, peerCap.version); - return DistributedDB::OK; - } - } - if (!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { - ZLOGD("devicdId:%{public}s is not oh type", Anonymous::Change(session_.targetDeviceId).c_str()); + if(!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { + ZLOGD("deviceId:%{public}s is not oh type", Anonymous::Change(session_.targetDeviceId).c_str()); if (appId_.empty()) { return DistributedDB::DB_ERROR; } @@ -130,6 +120,20 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize } return DistributedDB::OK; } + bool flag = false; + auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); + if (!flag) { + ZLOGI("get peer cap failed"); + return (storeId_ == UDMF_STORE_ID || storeId_ == DATA_OBJECT_STORE_ID) ? DistributedDB::OK + : DistributedDB::DB_ERROR; + } + if ((appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ != Bootstrap::GetInstance().GetMetaDBName() + && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) + || peerCap.version == CapMetaData::INVALID_VERSION) { + // older versions ignore pack extend head + ZLOGI("ignore older version device, appId:%{public}s, version:%{public}d", appId_.c_str(), peerCap.version); + return DistributedDB::OK; + } if (!session_.IsValid()) { ZLOGI("no valid session to peer device"); return DistributedDB::DB_ERROR; @@ -287,7 +291,7 @@ bool RouteHeadHandlerImpl::ParseHeadDataLen(const uint8_t *data, uint32_t totalL bool flag = false; auto peerCap = UpgradeManager::GetInstance().GetCapability(device, flag); if (flag && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) { - ZLOGI("get peer cap success, peerCap's version is %{public}d, less then 3", peerCap.version); + ZLOGI("get peer cap success, peerCap's version is %{public}d, less than 3", peerCap.version); return false; } } diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 2afc18659..5252a8de5 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -1068,7 +1068,7 @@ int32_t ObjectStoreManager::SyncOnStore( auto status = DoSync(prefix, syncDevices, sequenceId); ZLOGI("Store sync after meta sync end, status:%{public}d", status); }); - ZLOGI("prefix:%{public}s, meta sync end, result:%{public}d", prefix.c_str(), result); + ZLOGI("prefix:%{public}s, meta sync end, result:%{public}d", Anonymous::Change(prefix).c_str(), result); return result ? OBJECT_SUCCESS : DoSync(prefix, syncDevices, sequenceId); } diff --git a/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp index dea3bb04a..1e78eb6ab 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_abnormal_test.cpp @@ -127,16 +127,19 @@ HWTEST_F(KVDBGeneralStoreAbnormalTest, GetDBOptionTest001, TestSize.Level0) HWTEST_F(KVDBGeneralStoreAbnormalTest, SetEqualIdentifier, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - std::vector uuids{"uuidtest01"}; + std::vector uuids{ "uuidtest01" }; KvStoreNbDelegateMock mockDelegate; mockDelegate.taskCountMock_ = 1; store->delegate_ = &mockDelegate; EXPECT_NE(store->delegate_, nullptr); - EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::A>())).WillOnce(testing::Return(uuids)); + EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::A>())) + .WillOnce(testing::Return(uuids)); auto uuids1 = DMAdapter::ToUUID(DMAdapter::GetInstance().GetRemoteDevices()); - EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::A>())).WillOnce(testing::Return(uuids)); + EXPECT_CALL(*deviceManagerAdapterMock, ToUUID(testing::A>())) + .WillOnce(testing::Return(uuids)); EXPECT_CALL(*deviceManagerAdapterMock, IsOHOSType("uuidtest01")) - .WillOnce(testing::Return(false)).WillOnce(testing::Return(false)); + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(false)); store->SetEqualIdentifier(metaData_.appId, metaData_.storeId); EXPECT_EQ(uuids1, uuids); store->delegate_ = nullptr; -- Gitee From d34609d23be67494b4dfd36a609d1cc0cc90a2cd Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 9 Jul 2025 20:34:09 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E6=9B=B4=E6=96=B0TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/test/object_manager_mock_test.cpp | 9 +++++---- .../service/test/object_manager_test.cpp | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 92022300c..172db1b3d 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -184,16 +184,17 @@ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) EXPECT_EQ(result, OBJECT_SUCCESS); } - // remote device. IsNeedMetaSync: true; Sync: false + // remote device. IsNeedMetaSync: false { std::vector remoteDeviceList = { "remote_device_1" }; EXPECT_CALL(*devMgrAdapterMock, GetUuidByNetworkId(_)).WillRepeatedly(Return("mock_uuid")); EXPECT_CALL(*devMgrAdapterMock, ToUUID(testing::A &>())) .WillOnce(Return(std::vector{ "mock_uuid_1" })); EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(testing::Return(false)) - .WillOnce(testing::Return(false)); - EXPECT_CALL(*metaDataManagerMock, Sync(_, _, _)).WillOnce(testing::Return(false)); + .WillOnce(testing::Return(true)) + .WillOnce(testing::Return(true)); + EXPECT_CALL(*deviceMatrixMock, GetRemoteMask(_, _)).WillOnce(Return(std::make_pair(true, 0))); + EXPECT_CALL(*deviceMatrixMock, GetMask(_, _)).WillOnce(Return(std::make_pair(true, 0))); auto result = manager->SyncOnStore(prefix, remoteDeviceList, func); EXPECT_EQ(result, E_DB_ERROR); } diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index 838ee4020..c4937cfff 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -844,7 +844,7 @@ HWTEST_F(ObjectManagerTest, RegisterAssetsLister001, TestSize.Level0) auto result = manager->RegisterAssetsLister(); ASSERT_EQ(result, true); manager->objectAssetsSendListener_ = new ObjectAssetsSendListener(); - manager->objectAssetsRecvListener_ = new ObjectAssetsRecvListener();; + manager->objectAssetsRecvListener_ = new ObjectAssetsRecvListener(); result = manager->RegisterAssetsLister(); ASSERT_EQ(result, true); } -- Gitee From 0af28f2507caca971258f3df8a156be1a0b6a461 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Thu, 10 Jul 2025 14:52:44 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/test/object_manager_mock_test.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 172db1b3d..dc03bec6f 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -69,7 +69,6 @@ public: * @tc.desc: Test IsNeedMetaSync when LoadMeta fails for CapMetaData. * @tc.type: FUNC * @tc.require: - * @tc.author: zhaojh */ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync001, TestSize.Level0) { @@ -96,7 +95,6 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync001, TestSize.Level0) * @tc.desc: Test IsNeedMetaSync when LoadMeta fails for StoreMetaData. * @tc.type: FUNC * @tc.require: - * @tc.author: zhaojh */ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync002, TestSize.Level0) { @@ -121,7 +119,6 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync002, TestSize.Level0) * @tc.desc: Test IsNeedMetaSync when LoadMeta fails for StoreMetaData. * @tc.type: FUNC * @tc.require: - * @tc.author: zhaojh */ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) { @@ -150,6 +147,12 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) EXPECT_EQ(result, false); } +/** + * @tc.name: SyncOnStore001 + * @tc.desc: Test SyncOnStore. + * @tc.type: FUNC + * @tc.require: + */ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) { auto manager = ObjectStoreManager::GetInstance(); -- Gitee From d462f743b66c28eb13580090290e75d5652fec2a Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Jul 2025 09:46:53 +0800 Subject: [PATCH 09/12] update Signed-off-by: Hollokin --- .../app/src/session_manager/route_head_handler_impl.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 42c98cfc6..b76ee6459 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -124,8 +124,10 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.targetDeviceId, flag); if (!flag) { ZLOGI("get peer cap failed"); - return (storeId_ == UDMF_STORE_ID || storeId_ == DATA_OBJECT_STORE_ID) ? DistributedDB::OK - : DistributedDB::DB_ERROR; + return (appId_ == Bootstrap::GetInstance().GetProcessLabel() && + (storeId_ == UDMF_STORE_ID || storeId_ == DATA_OBJECT_STORE_ID)) + ? DistributedDB::OK + : DistributedDB::DB_ERROR; } if ((appId_ == Bootstrap::GetInstance().GetProcessLabel() && storeId_ != Bootstrap::GetInstance().GetMetaDBName() && peerCap.version < CapMetaData::UDMF_AND_OBJECT_VERSION) -- Gitee From ac1c94a2f19d5394343f6531f61161205ecf6b4b Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Jul 2025 11:56:00 +0800 Subject: [PATCH 10/12] =?UTF-8?q?delegate=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/object/include/object_manager.h | 1 + .../service/object/src/object_manager.cpp | 67 +++++++++++++------ .../service/test/object_manager_test.cpp | 2 + 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index 662c1ae08..1b5bef26d 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -217,6 +217,7 @@ private: return std::string(USERID) + SEPERATOR + userId + SEPERATOR + appId + SEPERATOR + DmAdaper::GetInstance().GetLocalDevice().udid; }; + mutable std::shared_timed_mutex rwMutex_; std::recursive_mutex kvStoreMutex_; std::shared_ptr kvStoreDelegateManager_ = nullptr; DistributedDB::KvStoreNbDelegate *delegate_ = nullptr; diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 5252a8de5..041beb9a0 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -52,6 +52,7 @@ using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; constexpr const char *SAVE_INFO = "p_###SAVEINFO###"; constexpr int32_t PROGRESS_MAX = 100; constexpr int32_t PROGRESS_INVALID = -1; +constexpr uint32_t LOCK_TIMEOUT = 3600; // second ObjectStoreManager::ObjectStoreManager() { ZLOGI("ObjectStoreManager construct"); @@ -869,7 +870,7 @@ int32_t ObjectStoreManager::Open() ZLOGE("Kvstore delegate manager not init"); return OBJECT_INNER_ERROR; } - std::lock_guard lock(kvStoreMutex_); + std::unique_lock lock(rwMutex_); if (delegate_ == nullptr) { delegate_ = OpenObjectKvStore(); if (delegate_ == nullptr) { @@ -887,7 +888,7 @@ int32_t ObjectStoreManager::Open() void ObjectStoreManager::ForceClose() { - std::lock_guard lock(kvStoreMutex_); + std::unique_lock lock(rwMutex_, std::chrono::seconds(LOCK_TIMEOUT)); if (delegate_ == nullptr) { return; } @@ -903,11 +904,14 @@ void ObjectStoreManager::ForceClose() void ObjectStoreManager::Close() { - std::lock_guard lock(kvStoreMutex_); - if (delegate_ == nullptr) { - return; + int32_t taskCount = 0; + { + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + return; + } + taskCount = delegate_->GetTaskCount(); } - int32_t taskCount = delegate_->GetTaskCount(); if (taskCount > 0 && syncCount_ == 1) { CloseAfterMinute(); ZLOGW("Store is busy, close after a minute, task count: %{public}d", taskCount); @@ -943,7 +947,7 @@ void ObjectStoreManager::SyncCompleted( void ObjectStoreManager::FlushClosedStore() { - std::lock_guard lock(kvStoreMutex_); + std::shared_lock lock(rwMutex_); if (!isSyncing_ && syncCount_ == 0 && delegate_ != nullptr) { ZLOGD("close store"); auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); @@ -962,14 +966,17 @@ void ObjectStoreManager::FlushClosedStore() void ObjectStoreManager::ProcessOldEntry(const std::string &appId) { std::vector entries; - auto status = delegate_->GetEntries(std::vector(appId.begin(), appId.end()), entries); - if (status == DistributedDB::DBStatus::NOT_FOUND) { - ZLOGI("Get old entries empty, bundleName: %{public}s", appId.c_str()); - return; - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("Get old entries failed, bundleName: %{public}s, status %{public}d", appId.c_str(), status); - return; + { + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + ZLOGE("delegate is nullptr."); + return; + } + auto status = delegate_->GetEntries(std::vector(appId.begin(), appId.end()), entries); + if (status != DistributedDB::DBStatus::OK) { + ZLOGE("Get old entries failed, bundleName: %{public}s, status %{public}d", appId.c_str(), status); + return; + } } std::map sessionIds; int64_t oldestTime = 0; @@ -1024,12 +1031,19 @@ int32_t ObjectStoreManager::SaveToStore(const std::string &appId, const std::str entry.value = item.second; entries.emplace_back(entry); } - auto status = delegate_->PutBatch(entries); - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("PutBatch failed, bundleName: %{public}s, sessionId: %{public}s, dstNetworkId: %{public}s, " - "status: %{public}d", - appId.c_str(), Anonymous::Change(sessionId).c_str(), Anonymous::Change(toDeviceId).c_str(), status); - return status; + { + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + ZLOGE("delegate is nullptr."); + return E_DB_ERROR; + } + auto status = delegate_->PutBatch(entries); + if (status != DistributedDB::DBStatus::OK) { + ZLOGE("PutBatch failed, bundleName: %{public}s, sessionId: %{public}s, dstNetworkId: %{public}s, " + "status: %{public}d", + appId.c_str(), Anonymous::Change(sessionId).c_str(), Anonymous::Change(toDeviceId).c_str(), status); + return status; + } } ZLOGI("PutBatch success, bundleName: %{public}s, sessionId: %{public}s, dstNetworkId: %{public}s, " "count: %{public}zu", @@ -1075,6 +1089,7 @@ int32_t ObjectStoreManager::SyncOnStore( int32_t ObjectStoreManager::DoSync(const std::string &prefix, const std::vector &deviceList, uint64_t sequenceId) { + std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { ZLOGE("db store was closed."); return E_DB_ERROR; @@ -1112,6 +1127,11 @@ int32_t ObjectStoreManager::SetSyncStatus(bool status) int32_t ObjectStoreManager::RevokeSaveToStore(const std::string &prefix) { std::vector entries; + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + ZLOGE("db store was closed."); + return E_DB_ERROR; + } auto status = delegate_->GetEntries(std::vector(prefix.begin(), prefix.end()), entries); if (status == DistributedDB::DBStatus::NOT_FOUND) { ZLOGI("Get entries empty, prefix: %{public}s", Anonymous::Change(prefix).c_str()); @@ -1144,6 +1164,11 @@ int32_t ObjectStoreManager::RetrieveFromStore(const std::string &appId, const st { std::vector entries; std::string prefix = GetPrefixWithoutDeviceId(appId, sessionId); + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + ZLOGE("db store was closed."); + return E_DB_ERROR; + } auto status = delegate_->GetEntries(std::vector(prefix.begin(), prefix.end()), entries); if (status == DistributedDB::DBStatus::NOT_FOUND) { ZLOGI("Get entries empty, prefix: %{public}s, status: %{public}d", Anonymous::Change(prefix).c_str(), status); diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index c4937cfff..3462063eb 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -444,6 +444,8 @@ HWTEST_F(ObjectManagerTest, Open001, TestSize.Level0) manager->delegate_ = manager->OpenObjectKvStore(); result = manager->Open(); ASSERT_EQ(result, DistributedObject::OBJECT_SUCCESS); + manager->ForceClose(); + ASSERT_EQ(manager->delegate_, nullptr); } /** -- Gitee From b3e4b36e2d3bf7c9338233b3dbfb18255abfd187 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Tue, 15 Jul 2025 09:30:35 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=86=99?= =?UTF-8?q?=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/object/src/object_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 041beb9a0..8f4852395 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -944,10 +944,10 @@ void ObjectStoreManager::SyncCompleted( } } } - + void ObjectStoreManager::FlushClosedStore() { - std::shared_lock lock(rwMutex_); + std::unique_lock lock(rwMutex_); if (!isSyncing_ && syncCount_ == 0 && delegate_ != nullptr) { ZLOGD("close store"); auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); -- Gitee From 9305730a14111f6fe9d1449a8c7f2beee7c0d615 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Tue, 15 Jul 2025 15:38:11 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=8E=9F=E5=AD=90=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../app/src/session_manager/route_head_handler_impl.cpp | 2 +- .../service/object/include/object_manager.h | 5 ++--- .../service/object/src/object_manager.cpp | 6 ++---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index b76ee6459..fc509ff74 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -109,7 +109,7 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize ZLOGI("meta data permitted"); return DistributedDB::OK; } - if(!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { + if (!DmAdapter::GetInstance().IsOHOSType(session_.targetDeviceId)) { ZLOGD("deviceId:%{public}s is not oh type", Anonymous::Change(session_.targetDeviceId).c_str()); if (appId_.empty()) { return DistributedDB::DB_ERROR; diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index 1b5bef26d..eb61ef3f1 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -218,15 +218,14 @@ private: + DmAdaper::GetInstance().GetLocalDevice().udid; }; mutable std::shared_timed_mutex rwMutex_; - std::recursive_mutex kvStoreMutex_; std::shared_ptr kvStoreDelegateManager_ = nullptr; DistributedDB::KvStoreNbDelegate *delegate_ = nullptr; ObjectDataListener objectDataListener_; sptr objectAssetsRecvListener_ = nullptr; sptr objectAssetsSendListener_ = nullptr; - uint32_t syncCount_ = 0; + std::atomic syncCount_{ 0 }; std::string userId_; - std::atomic isSyncing_ = false; + std::atomic isSyncing_{ false }; ConcurrentMap callbacks_; ConcurrentMap processCallbacks_; std::shared_ptr executors_; diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 8f4852395..e0665f740 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -881,7 +881,7 @@ int32_t ObjectStoreManager::Open() ZLOGI("Open object kvstore success"); } else { syncCount_++; - ZLOGI("Object kvstore syncCount: %{public}d", syncCount_); + ZLOGI("Object kvstore syncCount: %{public}d", syncCount_.load()); } return OBJECT_SUCCESS; } @@ -918,7 +918,7 @@ void ObjectStoreManager::Close() return; } syncCount_--; - ZLOGI("closed a store, syncCount = %{public}d", syncCount_); + ZLOGI("closed a store, syncCount = %{public}d", syncCount_.load()); FlushClosedStore(); } @@ -928,7 +928,6 @@ void ObjectStoreManager::SyncCompleted( std::string userId; SequenceSyncManager::Result result = SequenceSyncManager::GetInstance()->Process(sequenceId, results, userId); if (result == SequenceSyncManager::SUCCESS_USER_HAS_FINISHED && userId == userId_) { - std::lock_guard lock(kvStoreMutex_); SetSyncStatus(false); FlushClosedStore(); } @@ -1119,7 +1118,6 @@ int32_t ObjectStoreManager::DoSync(const std::string &prefix, const std::vector< int32_t ObjectStoreManager::SetSyncStatus(bool status) { - std::lock_guard lock(kvStoreMutex_); isSyncing_ = status; return OBJECT_SUCCESS; } -- Gitee