diff --git a/common/include/interfaces/distributed_device_profile_enums.h b/common/include/interfaces/distributed_device_profile_enums.h index 3ca83d933ee149843c749445407f235bd2d08d9e..f482ecfba472b291b55d296a26f06d083f5827cd 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 413c643c081fd45f04fa222ef7d78d8077aad56b..b497fc2f4d90e5b98c0e3b8ac04151f79331e533 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 ad13bc05eeb5f818045b57d5ef4426744f81dce8..cfa42526c9a6c35c022ecf489914035c11830315 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 d9e96ee3268e7734972af85c4bc7b3cbac62b0f5..d033785f862ce3e9fcc0ca118d20bc755156bc34 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 5cc45f3f957081c626467c8012466f25abe1179d..35f878524a5e0a2ea8cef36761feaa0a8568fccb 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 29c71ee2800642cb81bfd3b9e322a61fd41b04c7..5c34c844619328d6ad7dabd7b98b3b162ebd3972 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 4345874baceb3004b3859313e81eb3b86a64d260..b89b4106fb31597852dcb368475e5cf4af4f28b5 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 9401f1295d489206681087e188b0ef7b582de8dc..42cb8a6c5ce5e06ea32f89d1297bdb8c456d4755 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 5bdd2026c391f369a763d956d294d848dbc22879..35ce01e654dad55f7630f020fd1fc9d0112cc1f4 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 c24d67447bfd80e1aed77402e4f9186c56023e4f..f4cd07687ad81ad105c8b5894c5ad56c0846bc04 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 8ca97f3af5916489514a6b5fdf13593e810f7d6e..8563da07b8d93ab60f6d0680c0ac19e9a1f463ee 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 489cb57ced354337ae36623198e937892613e824..320e2d1b6d5132ced6be3cbd13a0144aa35f8d15 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 e9d2419c154a0e941c64ad18885022e8d322cb0a..6a1f2dbd7e927dc444c34b80feaa867cee50a3ce 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 e9079dc9489199e60360c0179331019d65027c33..cc2b600ec5932cfba3f4048571576eb6b0e1b23f 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 c16f50bdd4f900f617b0456a41b56e4fefac67ad..0a9e9549cb59d1787ed7d7e1fa7e5dc1a4872798 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 bbfbfd9c979158e9df01a759ae3fca9e49ea0034..2cfc9c07c1ce646c32d228a0308ccc4afffe3a5e 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