From 27d4fd4d400cfdb1b2a77dd58eddf78322279a39 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Wed, 27 Aug 2025 11:47:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9D=99=E6=80=81=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=90=8C=E6=AD=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangzhaohao --- .../distributed_device_profile_enums.h | 3 +- .../interfaces/i_distributed_device_profile.h | 2 + .../distributed_device_profile_client.h | 1 + .../distributed_device_profile_proxy.h | 1 + .../src/distributed_device_profile_client.cpp | 20 +++++ .../src/distributed_device_profile_proxy.cpp | 17 ++++ .../listener/kv_sync_completed_listener.h | 1 + .../static_profile_manager.h | 12 ++- .../distributed_device_profile_service_new.h | 2 + .../distributed_device_profile_stub_new.h | 1 + .../listener/kv_sync_completed_listener.cpp | 36 ++++++-- .../static_profile_manager.cpp | 87 +++++++++++++++++++ ...distributed_device_profile_service_new.cpp | 13 +++ .../distributed_device_profile_stub_new.cpp | 18 ++++ ...stributed_device_profile_stub_new_test.cpp | 24 +++++ .../unittest/static_profile_manager_test.cpp | 45 ++++++++++ 16 files changed, 276 insertions(+), 7 deletions(-) diff --git a/common/include/interfaces/distributed_device_profile_enums.h b/common/include/interfaces/distributed_device_profile_enums.h index 3ca83d93..f482ecfb 100644 --- a/common/include/interfaces/distributed_device_profile_enums.h +++ b/common/include/interfaces/distributed_device_profile_enums.h @@ -92,7 +92,8 @@ enum class DPInterfaceCode : uint32_t { UNREGISTER_BUSINESS_CALLBACK = 73, PUT_BUSINESS_EVENT = 74, GET_BUSINESS_EVENT = 75, - MAX = 76 + SYNC_STATIC_PROFILE = 76, + MAX = 77 }; enum class DeviceIdType : uint32_t { diff --git a/common/include/interfaces/i_distributed_device_profile.h b/common/include/interfaces/i_distributed_device_profile.h index 413c643c..b497fc2f 100644 --- a/common/include/interfaces/i_distributed_device_profile.h +++ b/common/include/interfaces/i_distributed_device_profile.h @@ -84,6 +84,8 @@ public: virtual int32_t UnSubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType) = 0; virtual int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr syncCompletedCallback) = 0; + virtual int32_t SyncStaticProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, + sptr syncCompletedCallback) = 0; virtual int32_t SendSubscribeInfos(std::map listenerMap) = 0; virtual int32_t PutAllTrustedDevices(const std::vector deviceInfos) = 0; virtual int32_t PutProductInfoBatch(const std::vector& productInfos) = 0; diff --git a/interfaces/innerkits/core/include/distributed_device_profile_client.h b/interfaces/innerkits/core/include/distributed_device_profile_client.h index ad13bc05..cfa42526 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_client.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_client.h @@ -83,6 +83,7 @@ public: int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); int32_t SyncDeviceProfile(const DpSyncOptions& syncOptions, sptr syncCb); + int32_t SyncStaticProfile(const DpSyncOptions& syncOptions, sptr syncCb); int32_t SubscribeDeviceProfileInited(int32_t saId, sptr initedCb); int32_t UnSubscribeDeviceProfileInited(int32_t saId); int32_t SubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType, diff --git a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h index d9e96ee3..d033785f 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h @@ -77,6 +77,7 @@ public: sptr pinCodeCallback) override; int32_t UnSubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType) override; int32_t SyncDeviceProfile(const DpSyncOptions& syncOptions, sptr syncCompletedCallback) override; + int32_t SyncStaticProfile(const DpSyncOptions& syncOptions, sptr syncCompletedCallback) override; int32_t SendSubscribeInfos(std::map listenerMap) override; int32_t PutAllTrustedDevices(const std::vector deviceInfos) override; int32_t PutProductInfoBatch(const std::vector& productInfos) override; diff --git a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp index 5cc45f3f..35f87852 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp @@ -546,6 +546,26 @@ int32_t DistributedDeviceProfileClient::SyncDeviceProfile(const DpSyncOptions& s return dpService->SyncDeviceProfile(syncOptions, syncCompletedCallback); } +int32_t DistributedDeviceProfileClient::SyncStaticProfile(const DpSyncOptions& syncOptions, + sptr syncCb) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + if (syncCb == nullptr) { + HILOGE("SyncCb is nullptr!"); + return DP_SYNC_DEVICE_FAIL; + } + sptr syncCompletedCallback = syncCb->AsObject(); + if (syncCompletedCallback == nullptr) { + HILOGE("SyncCb ipc cast fail!"); + return DP_SYNC_DEVICE_FAIL; + } + return dpService->SyncStaticProfile(syncOptions, syncCompletedCallback); +} + sptr DistributedDeviceProfileClient::GetDeviceProfileService() { { diff --git a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp index 29c71ee2..5c34c844 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp @@ -638,6 +638,23 @@ int32_t DistributedDeviceProfileProxy::SyncDeviceProfile(const DpSyncOptions& sy return DP_SUCCESS; } +int32_t DistributedDeviceProfileProxy::SyncStaticProfile(const DpSyncOptions& syncOptions, + const sptr syncCompletedCallback) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!syncOptions.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + WRITE_HELPER(data, RemoteObject, syncCompletedCallback); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::SYNC_STATIC_PROFILE), data, reply); + return DP_SUCCESS; +} + int32_t DistributedDeviceProfileProxy::SendSubscribeInfos(std::map listenerMap) { sptr remote = nullptr; diff --git a/services/core/include/deviceprofilemanager/listener/kv_sync_completed_listener.h b/services/core/include/deviceprofilemanager/listener/kv_sync_completed_listener.h index 4345874b..b89b4106 100644 --- a/services/core/include/deviceprofilemanager/listener/kv_sync_completed_listener.h +++ b/services/core/include/deviceprofilemanager/listener/kv_sync_completed_listener.h @@ -35,6 +35,7 @@ public: private: void NotifySyncCompleted(const SyncResults& syncResults); + void NotifyStaticSyncCompleted(const SyncResults& syncResults); private: std::shared_ptr onSyncHandler_ = nullptr; diff --git a/services/core/include/deviceprofilemanager/static_profile_manager.h b/services/core/include/deviceprofilemanager/static_profile_manager.h index 9401f129..42cb8a6c 100644 --- a/services/core/include/deviceprofilemanager/static_profile_manager.h +++ b/services/core/include/deviceprofilemanager/static_profile_manager.h @@ -16,8 +16,11 @@ #ifndef OHOS_DP_STATIC_PROFILE_MANAGER_H #define OHOS_DP_STATIC_PROFILE_MANAGER_H -#include "characteristic_profile.h" #include "dm_device_info.h" +#include "iremote_object.h" + +#include "characteristic_profile.h" +#include "dp_sync_options.h" #include "kv_adapter.h" #include "single_instance.h" #include "trusted_device_info.h" @@ -36,14 +39,21 @@ public: const std::string& characteristicKey, CharacteristicProfile& charProfile); int32_t GetAllCharacteristicProfile(std::vector& staticCapabilityProfiles); void E2ESyncStaticProfile(const TrustedDeviceInfo& deviceInfo); + int32_t SyncStaticProfile(const DpSyncOptions& syncOptions, sptr syncCompletedCallback); + void GetSyncListeners(std::map>& syncListeners); + void RemoveSyncListeners(std::map> syncListeners); private: int32_t GenerateStaticInfoProfile(const CharacteristicProfile& staticCapabilityProfile, std::unordered_map& staticInfoProfiles); + int32_t AddSyncListener(const std::string& caller, sptr syncListener); private: std::mutex staticStoreMutex_; std::shared_ptr staticProfileStore_ = nullptr; + std::mutex syncListenerMapMutex_; + std::map> syncListenerMap_; + sptr syncListenerDeathRecipient_ = nullptr; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/include/distributed_device_profile_service_new.h b/services/core/include/distributed_device_profile_service_new.h index 5bdd2026..35ce01e6 100644 --- a/services/core/include/distributed_device_profile_service_new.h +++ b/services/core/include/distributed_device_profile_service_new.h @@ -99,6 +99,8 @@ public: int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override; int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr syncCompletedCallback) override; + int32_t SyncStaticProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, + sptr syncCompletedCallback) override; int32_t SubscribeDeviceProfileInited(int32_t saId, sptr dpInitedCallback) override; int32_t UnSubscribeDeviceProfileInited(int32_t saId) override; int32_t SubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType, diff --git a/services/core/include/distributed_device_profile_stub_new.h b/services/core/include/distributed_device_profile_stub_new.h index c24d6744..f4cd0768 100644 --- a/services/core/include/distributed_device_profile_stub_new.h +++ b/services/core/include/distributed_device_profile_stub_new.h @@ -59,6 +59,7 @@ public: int32_t SubscribeDeviceProfileInner(MessageParcel& data, MessageParcel& reply); int32_t UnSubscribeDeviceProfileInner(MessageParcel& data, MessageParcel& reply); int32_t SyncDeviceProfileInner(MessageParcel& data, MessageParcel& reply); + int32_t SyncStaticProfileInner(MessageParcel& data, MessageParcel& reply); int32_t SendSubscribeInfosInner(MessageParcel& data, MessageParcel& reply); int32_t SubscribeDeviceProfileInitedInner(MessageParcel& data, MessageParcel& reply); int32_t UnSubscribeDeviceProfileInitedInner(MessageParcel& data, MessageParcel& reply); diff --git a/services/core/src/deviceprofilemanager/listener/kv_sync_completed_listener.cpp b/services/core/src/deviceprofilemanager/listener/kv_sync_completed_listener.cpp index 8ca97f3a..8563da07 100644 --- a/services/core/src/deviceprofilemanager/listener/kv_sync_completed_listener.cpp +++ b/services/core/src/deviceprofilemanager/listener/kv_sync_completed_listener.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -21,16 +21,19 @@ #include "string_ex.h" #include "distributed_device_profile_log.h" +#include "event_handler_factory.h" +#include "i_sync_completed_callback.h" #include "profile_utils.h" #include "profile_cache.h" -#include "i_sync_completed_callback.h" -#include "event_handler_factory.h" +#include "static_profile_manager.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "KvSyncCompletedListener"; const std::string ON_SYNC_TASK_ID = "on_sync_task"; + const std::string DYNAMIC_STORE_ID = "dp_kv_store"; + const std::string STATIC_STORE_ID = "dp_kv_static_store"; } KvSyncCompletedListener::KvSyncCompletedListener(const std::string& storeId) @@ -60,7 +63,6 @@ KvSyncCompletedListener::~KvSyncCompletedListener() void KvSyncCompletedListener::SyncCompleted(const std::map& results) { HILOGD("called!"); - SyncResults syncResults; for (const auto& [deviceId, status] : results) { HILOGD("deviceId = %{public}s, status = %{public}d", ProfileUtils::GetAnonyString(deviceId).c_str(), status); @@ -68,7 +70,13 @@ void KvSyncCompletedListener::SyncCompleted(const std::map lock(reInitMutex_); @@ -100,6 +108,24 @@ void KvSyncCompletedListener::NotifySyncCompleted(const SyncResults& syncResults int64_t endTime = GetTickCount(); HILOGI("spend %{public}" PRId64 " ms", endTime - beginTime); } + +void KvSyncCompletedListener::NotifyStaticSyncCompleted(const SyncResults& syncResults) +{ + int64_t beginTime = GetTickCount(); + std::map> syncListeners; + StaticProfileManager::GetInstance().GetSyncListeners(syncListeners); + for (const auto& [_, syncListenerStub] : syncListeners) { + sptr syncListenerProxy = iface_cast(syncListenerStub); + if (syncListenerProxy == nullptr) { + HILOGE("Cast to ISyncCompletedCallback failed"); + continue; + } + syncListenerProxy->OnSyncCompleted(syncResults); + } + StaticProfileManager::GetInstance().RemoveSyncListeners(syncListeners); + int64_t endTime = GetTickCount(); + HILOGI("spend %{public}" PRId64 " ms", endTime - beginTime); +} } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/src/deviceprofilemanager/static_profile_manager.cpp b/services/core/src/deviceprofilemanager/static_profile_manager.cpp index 489cb57c..320e2d1b 100644 --- a/services/core/src/deviceprofilemanager/static_profile_manager.cpp +++ b/services/core/src/deviceprofilemanager/static_profile_manager.cpp @@ -218,5 +218,92 @@ void StaticProfileManager::E2ESyncStaticProfile(const TrustedDeviceInfo& deviceI return; } } + +int32_t StaticProfileManager::SyncStaticProfile(const DpSyncOptions& syncOptions, + sptr syncCompletedCallback) +{ + HILOGI("call!"); + if (syncCompletedCallback == nullptr) { + HILOGE("callback is empty"); + return DP_INVALID_PARAMS; + } + std::vector ohBasedDevices; + std::vector notOHBasedDevices; + ProfileCache::GetInstance().FilterAndGroupOnlineDevices(syncOptions.GetDeviceList(), + ohBasedDevices, notOHBasedDevices); + if (ohBasedDevices.empty()) { + HILOGE("ohBasedDevices is empty"); + return DP_INVALID_PARAMS; + } + std::string callerDescriptor = PermissionManager::GetInstance().GetCallerProcName(); + int32_t addRet = AddSyncListener(callerDescriptor, syncCompletedCallback); + if (addRet != DP_SUCCESS) { + return addRet; + } + { + std::lock_guard lock(staticStoreMutex_); + if (staticProfileStore_ == nullptr) { + HILOGE("staticProfileStore is nullptr"); + return DP_NULLPTR; + } + int32_t syncResult = staticProfileStore_->Sync(ohBasedDevices, syncOptions.GetSyncMode()); + if (syncResult != DP_SUCCESS) { + HILOGE("SyncStaticProfile fail, res: %{public}d!", syncResult); + return DP_SYNC_DEVICE_FAIL; + } + } + HILOGI("SyncStaticProfile success, caller: %{public}s!", callerDescriptor.c_str()); + return DP_SUCCESS; +} + +int32_t StaticProfileManager::AddSyncListener(const std::string& caller, sptr syncListener) +{ + if (caller.empty() || caller.size() > MAX_STRING_LEN || syncListener == nullptr) { + HILOGE("params is invalid!"); + return DP_INVALID_PARAMS; + } + { + std::lock_guard lock(syncListenerMapMutex_); + if (syncListenerMap_.size() > MAX_LISTENER_SIZE) { + HILOGE("syncListenerMap is exceed max listenerSize!"); + return DP_EXCEED_MAX_SIZE_FAIL; + } + HILOGI("caller %{public}s!", caller.c_str()); + syncListener->AddDeathRecipient(syncListenerDeathRecipient_); + syncListenerMap_[caller] = syncListener; + } + return DP_SUCCESS; +} + +void StaticProfileManager::GetSyncListeners(std::map>& syncListeners) +{ + HILOGD("call!"); + { + std::lock_guard lock(syncListenerMapMutex_); + for (const auto& item : syncListenerMap_) { + syncListeners[item.first] = item.second; + } + } +} + +void StaticProfileManager::RemoveSyncListeners(std::map> syncListeners) +{ + HILOGD("call!"); + { + std::lock_guard lock(syncListenerMapMutex_); + auto iter = syncListenerMap_.begin(); + while (iter!= syncListenerMap_.end()) { + if (syncListeners.count(iter->first) != 0) { + if (iter->second != nullptr) { + iter->second->RemoveDeathRecipient(syncListenerDeathRecipient_); + } + HILOGI("caller %{public}s!", iter->first.c_str()); + iter = syncListenerMap_.erase(iter); + } else { + iter++; + } + } + } +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/src/distributed_device_profile_service_new.cpp b/services/core/src/distributed_device_profile_service_new.cpp index e9d2419c..6a1f2dbd 100644 --- a/services/core/src/distributed_device_profile_service_new.cpp +++ b/services/core/src/distributed_device_profile_service_new.cpp @@ -908,6 +908,19 @@ int32_t DistributedDeviceProfileServiceNew::SyncDeviceProfile( return ret; } +int32_t DistributedDeviceProfileServiceNew::SyncStaticProfile( + const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr syncCompletedCallback) +{ + if (!PermissionManager::GetInstance().CheckCallerSyncPermission()) { + HILOGE("this caller is permission denied!"); + return DP_PERMISSION_DENIED; + } + HILOGD("CheckCallerSyncPermission success interface SyncStaticProfile"); + int32_t ret = StaticProfileManager::GetInstance().SyncStaticProfile(syncOptions, syncCompletedCallback); + DpRadarHelper::GetInstance().ReportSyncDeviceProfile(ret); + return ret; +} + int32_t DistributedDeviceProfileServiceNew::SendSubscribeInfos(std::map listenerMap) { return SubscribeProfileManager::GetInstance().SubscribeDeviceProfile(listenerMap); diff --git a/services/core/src/distributed_device_profile_stub_new.cpp b/services/core/src/distributed_device_profile_stub_new.cpp index e9079dc9..cc2b600e 100644 --- a/services/core/src/distributed_device_profile_stub_new.cpp +++ b/services/core/src/distributed_device_profile_stub_new.cpp @@ -177,6 +177,8 @@ int32_t DistributedDeviceProfileStubNew::NotifyEventInner(uint32_t code, Message return DeleteCharacteristicProfileInner(data, reply); case static_cast(DPInterfaceCode::SYNC_DEVICE_PROFILE_NEW): return SyncDeviceProfileInner(data, reply); + case static_cast(DPInterfaceCode::SYNC_STATIC_PROFILE): + return SyncStaticProfileInner(data, reply); case static_cast(DPInterfaceCode::PUT_SERVICE_INFO_PROFILE): return PutServiceInfoProfileInner(data, reply); case static_cast(DPInterfaceCode::DELETE_SERVICE_INFO_PROFILE): @@ -683,6 +685,22 @@ int32_t DistributedDeviceProfileStubNew::SyncDeviceProfileInner(MessageParcel& d return DP_SUCCESS; } +int32_t DistributedDeviceProfileStubNew::SyncStaticProfileInner(MessageParcel& data, MessageParcel& reply) +{ + DistributedDeviceProfile::DpSyncOptions syncOptions; + if (!syncOptions.UnMarshalling(data)) { + HILOGE("read parcel fail!"); + return DP_READ_PARCEL_FAIL; + } + sptr syncCompletedCallback = data.ReadRemoteObject(); + int32_t ret = SyncStaticProfile(syncOptions, syncCompletedCallback); + if (!reply.WriteInt32(ret)) { + HILOGE("Write reply failed"); + return ERR_FLATTEN_OBJECT; + } + return DP_SUCCESS; +} + int32_t DistributedDeviceProfileStubNew::SendSubscribeInfosInner(MessageParcel& data, MessageParcel& reply) { std::map listenerMap; diff --git a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp index c16f50bd..0a9e9549 100644 --- a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp @@ -72,6 +72,8 @@ class MockDistributedDeviceProfileStubNew : public DistributedDeviceProfileStubN int32_t PutAllTrustedDevices(const std::vector deviceInfos) override; int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr syncCompletedCallback) override; + int32_t SyncStaticProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, + sptr syncCompletedCallback) override; int32_t SendSubscribeInfos(std::map listenerMap) override; int32_t PutDeviceProfileBatch(std::vector& deviceProfiles) override; int32_t GetDeviceProfiles(DeviceProfileFilterOptions& options, @@ -332,6 +334,14 @@ int32_t MockDistributedDeviceProfileStubNew::SyncDeviceProfile( (void)syncCompletedCallback; return 0; } +int32_t MockDistributedDeviceProfileStubNew::SyncStaticProfile( + const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr syncCompletedCallback) +{ + (void)syncOptions; + (void)syncCompletedCallback; + return 0; +} + int32_t MockDistributedDeviceProfileStubNew::SendSubscribeInfos(std::map listenerMap) { (void)listenerMap; @@ -801,6 +811,20 @@ HWTEST_F(DistributedDeviceProfileStubNewTest, SyncDeviceProfileInner_001, TestSi EXPECT_EQ(DP_READ_PARCEL_FAIL, ret); } +/** + * @tc.name: SyncStaticProfileInner001 + * @tc.desc: SyncStaticProfileInner + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, SyncStaticProfileInner001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + int32_t ret = ProfileStub_->SyncStaticProfileInner(data, reply); + EXPECT_EQ(DP_READ_PARCEL_FAIL, ret); +} + /** * @tc.name: SendSubscribeInfosInner001 * @tc.desc: SendSubscribeInfosInner diff --git a/services/core/test/unittest/static_profile_manager_test.cpp b/services/core/test/unittest/static_profile_manager_test.cpp index bbfbfd9c..2cfc9c07 100644 --- a/services/core/test/unittest/static_profile_manager_test.cpp +++ b/services/core/test/unittest/static_profile_manager_test.cpp @@ -22,8 +22,10 @@ #include "distributed_device_profile_constants.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" +#include "i_sync_completed_callback.h" #include "profile_cache.h" #include "static_profile_manager.h" +#include "sync_completed_callback_stub.h" using namespace testing::ext; namespace OHOS { namespace DistributedDeviceProfile { @@ -55,6 +57,12 @@ void StaticProfileManagerTest::TearDown() { } +class SyncCallback : public SyncCompletedCallbackStub { +public: + void OnSyncCompleted(const map& syncResults) { + } +}; + /* * @tc.name: Init_001 * @tc.desc: Init @@ -292,5 +300,42 @@ HWTEST_F(StaticProfileManagerTest, GenerateStaticInfoProfile_005, TestSize.Level StaticProfileManager::GetInstance().GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles); EXPECT_EQ(result, DP_PARSE_STATIC_CAP_FAIL); } + +/** + * @tc.name: AddSyncListener001 + * @tc.desc: AddSyncListener all. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(StaticProfileManagerTest, AddSyncListener001, TestSize.Level1) +{ + string caller = "caller"; + OHOS::sptr syncListener = OHOS::sptr(new SyncCallback()); + + int32_t ret1 = StaticProfileManager::GetInstance().AddSyncListener(caller, syncListener); + EXPECT_EQ(DP_SUCCESS, ret1); + + for (int32_t i = 0; i < MAX_LISTENER_SIZE + 5; i++) { + string caller = "caller" + std::to_string(i); + OHOS::sptr syncListener1 = OHOS::sptr(new SyncCallback()); + StaticProfileManager::GetInstance().syncListenerMap_[caller] = syncListener1; + } + int32_t ret2 = StaticProfileManager::GetInstance().AddSyncListener(caller, syncListener); + EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret2); + + syncListener = nullptr; + int32_t ret3 = StaticProfileManager::GetInstance().AddSyncListener(caller, syncListener); + EXPECT_EQ(DP_INVALID_PARAMS, ret3); + + for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) { + caller += 'a'; + } + int32_t ret4 = StaticProfileManager::GetInstance().AddSyncListener(caller, syncListener); + EXPECT_EQ(DP_INVALID_PARAMS, ret4); + + caller = ""; + int32_t ret5 = StaticProfileManager::GetInstance().AddSyncListener(caller, syncListener); + EXPECT_EQ(DP_INVALID_PARAMS, ret5); +} } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee