From f7cc7cabf8af4d75b29b2a2b0127984bde1773c7 Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Wed, 25 Dec 2024 14:18:13 +0800 Subject: [PATCH] miniso Signed-off-by: gaoqiang_strong --- .../include/i_dm_service_impl_ext_resident.h | 58 +++++++++++ .../service/include/device_manager_service.h | 6 +- .../service/src/device_manager_service.cpp | 99 +++++-------------- .../UTTest_device_manager_service.cpp | 21 ---- .../UTTest_device_manager_service_three.cpp | 11 +-- .../mock/device_manager_service_mock.cpp | 9 +- .../mock/device_manager_service_mock.h | 6 +- 7 files changed, 101 insertions(+), 109 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h index 560a86f25..c07cc4a63 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h @@ -25,6 +25,64 @@ public: virtual ~IDMServiceImplExtResident() = default; virtual int32_t Initialize(const std::shared_ptr &listener) = 0; virtual int32_t Release() = 0; + virtual bool IsDMServiceAdapterLoad() = 0; + virtual bool IsDMServiceAdapterSoLoaded() = 0; + /** + * @tc.name: IDMServiceImplExt::BindTargetExt + * @tc.desc: BindTargetExt + * @tc.type: FUNC + */ + virtual int32_t BindTargetExt(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) = 0; + + /** + * @tc.name: IDMServiceImplExt::UnbindTargetExt + * @tc.desc: UnbindTargetExt + * @tc.type: FUNC + */ + virtual int32_t UnbindTargetExt(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &unbindParam) = 0; + + /** + * @tc.name: IDMServiceImplExt::HandleDeviceStatusChange + * @tc.desc: HandleDeviceStatusChange + * @tc.type: FUNC + */ + virtual int32_t HandleDeviceStatusChange(DmDeviceState devState, const DmDeviceInfo &devInfo) = 0; + + /** + * @tc.name: IDMServiceImplExt::ReplyUiAction + * @tc.desc: ReplyUiAction + * @tc.type: FUNC + */ + virtual int32_t ReplyUiAction(const std::string &pkgName, int32_t action, const std::string &result) = 0; + + /** + * @tc.name: IDMServiceImplExt::AccountIdLogout + * @tc.desc: AccountIdLogout + * @tc.type: FUNC + */ + virtual int32_t AccountIdLogout(int32_t userId, const std::string &oldAccountId) = 0; + /** + * @tc.name: IDMServiceImplExt::HandleDeviceNotTrust + * @tc.desc: HandleDeviceNotTrust + * @tc.type: FUNC + */ + virtual void HandleDeviceNotTrust(const std::string &udid) = 0; + + /** + * @tc.name: IDMServiceImplExt::SetDnPolicy + * @tc.desc: SetDnPolicy + * @tc.type: FUNC + */ + virtual int32_t SetDnPolicy(int32_t policy, int32_t timeOut) = 0; + + /** + * @tc.name: IDMServiceImplExt::AccountUserSwitched + * @tc.desc: AccountUserSwitched + * @tc.type: FUNC + */ + virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId) = 0; }; using CreateDMServiceExtResidentFuncPtr = IDMServiceImplExtResident *(*)(void); diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 4cf68d0fa..960a637cb 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -212,11 +212,10 @@ public: private: bool IsDMServiceImplReady(); - bool IsDMServiceAdapterLoad(); bool IsDMImplSoLoaded(); + bool IsDMServiceAdapterSoLoaded(); bool IsDMServiceAdapterResidentLoad(); void UnloadDMServiceImplSo(); - void UnloadDMServiceAdapter(); void UnloadDMServiceAdapterResident(); void SendUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId, int32_t bindLevel); @@ -285,11 +284,9 @@ private: private: bool isImplsoLoaded_ = false; - bool isAdapterSoLoaded_ = false; bool isAdapterResidentSoLoaded_ = false; void *residentSoHandle_ = nullptr; std::mutex isImplLoadLock_; - std::mutex isAdapterLoadLock_; std::mutex isAdapterResidentLoadLock_; std::shared_ptr advertiseMgr_; std::shared_ptr discoveryMgr_; @@ -297,7 +294,6 @@ private: std::shared_ptr hichainListener_; std::shared_ptr listener_; std::shared_ptr dmServiceImpl_; - std::shared_ptr dmServiceImplExt_; std::shared_ptr dmServiceImplExtResident_; std::string localDeviceId_; std::shared_ptr pinHolder_; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 0868ce035..82ae479ad 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -86,7 +86,6 @@ DeviceManagerService::~DeviceManagerService() { LOGI("DeviceManagerService destructor"); UnloadDMServiceImplSo(); - UnloadDMServiceAdapter(); UnloadDMServiceAdapterResident(); } @@ -674,8 +673,8 @@ int32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t act LOGE("DeviceManagerService::SetUserOperation error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); return ERR_DM_INPUT_PARA_INVALID; } - if (IsDMServiceAdapterLoad()) { - dmServiceImplExt_->ReplyUiAction(pkgName, action, params); + if (IsDMServiceAdapterSoLoaded()) { + dmServiceImplExtResident_->ReplyUiAction(pkgName, action, params); } if (!IsDMServiceImplReady()) { LOGE("SetUserOperation failed, instance not init or init failed."); @@ -689,8 +688,8 @@ void DeviceManagerService::HandleDeviceStatusChange(DmDeviceState devState, DmDe if (IsDMServiceImplReady()) { dmServiceImpl_->HandleDeviceStatusChange(devState, devInfo); } - if (IsDMServiceAdapterLoad()) { - dmServiceImplExt_->HandleDeviceStatusChange(devState, devInfo); + if (IsDMServiceAdapterSoLoaded()) { + dmServiceImplExtResident_->HandleDeviceStatusChange(devState, devInfo); } } @@ -945,6 +944,16 @@ bool DeviceManagerService::IsDMImplSoLoaded() return isImplsoLoaded_; } +bool DeviceManagerService::IsDMServiceAdapterSoLoaded() +{ + LOGI("In"); + std::lock_guard lock(isAdapterResidentLoadLock_); + if (!isAdapterResidentSoLoaded_ || (dmServiceImplExtResident_ == nullptr)) { + return false; + } + return dmServiceImplExtResident_->IsDMServiceAdapterSoLoaded(); +} + int32_t DeviceManagerService::DmHiDumper(const std::vector& args, std::string &result) { LOGI("HiDump GetTrustedDeviceList"); @@ -1155,62 +1164,6 @@ void DeviceManagerService::UnloadDMServiceImplSo() } } -bool DeviceManagerService::IsDMServiceAdapterLoad() -{ - LOGI("Start."); - if (listener_ == nullptr) { - LOGE("Dm service is not init."); - return false; - } - std::lock_guard lock(isAdapterLoadLock_); - if (isAdapterSoLoaded_ && (dmServiceImplExt_ != nullptr)) { - return true; - } - - void *so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NODELETE | RTLD_NOLOAD); - if (so_handle == nullptr) { - so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NODELETE); - } - if (so_handle == nullptr) { - LOGE("load dm service adapter so failed."); - return false; - } - dlerror(); - auto func = (CreateDMServiceImplExtFuncPtr)dlsym(so_handle, "CreateDMServiceImplExtObject"); - if (dlerror() != nullptr || func == nullptr) { - dlclose(so_handle); - LOGE("Create object function is not exist."); - return false; - } - - dmServiceImplExt_ = std::shared_ptr(func()); - if (dmServiceImplExt_->Initialize(listener_) != DM_OK) { - dmServiceImplExt_ = nullptr; - isAdapterSoLoaded_ = false; - LOGE("dm service adapter impl ext init failed."); - return false; - } - isAdapterSoLoaded_ = true; - LOGI("Success."); - return true; -} - -void DeviceManagerService::UnloadDMServiceAdapter() -{ - LOGI("Start."); - std::lock_guard lock(isAdapterLoadLock_); - if (dmServiceImplExt_ != nullptr) { - dmServiceImplExt_->Release(); - } - dmServiceImplExt_ = nullptr; - - void *so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NOLOAD); - if (so_handle != nullptr) { - LOGI("dm service adapter so_handle is not nullptr."); - dlclose(so_handle); - } -} - bool DeviceManagerService::IsDMServiceAdapterResidentLoad() { LOGI("Start."); @@ -1415,12 +1368,12 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT const_cast &>(noConstBindParam); return dmServiceImpl_->BindTarget(pkgName, targetId, constBindParam); } - if (!IsDMServiceAdapterLoad()) { + if (!IsDMServiceAdapterResidentLoad()) { LOGE("BindTarget failed, adapter instance not init or init failed."); return ERR_DM_UNSUPPORTED_METHOD; } LOGI("BindTarget unstardard begin."); - return dmServiceImplExt_->BindTargetExt(pkgName, targetId, bindParam); + return dmServiceImplExtResident_->BindTargetExt(pkgName, targetId, bindParam); } int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, @@ -1435,7 +1388,7 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } - if (!IsDMServiceAdapterLoad()) { + if (!IsDMServiceAdapterResidentLoad()) { LOGE("UnbindTarget failed, instance not init or init failed."); return ERR_DM_UNSUPPORTED_METHOD; } @@ -1443,7 +1396,7 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee LOGE("input unbind parameter not contains META_TYPE, dm service adapter not supported."); return ERR_DM_INPUT_PARA_INVALID; } - return dmServiceImplExt_->UnbindTargetExt(pkgName, targetId, unbindParam); + return dmServiceImplExtResident_->UnbindTargetExt(pkgName, targetId, unbindParam); } int32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName) @@ -1680,8 +1633,8 @@ void DeviceManagerService::AccountCommonEventCallback(const std::string commonEv } else if (beforeUserId != -1 && currentUserId != -1) { HandleUserSwitched(currentUserId, beforeUserId); } - if (IsDMServiceAdapterLoad()) { - dmServiceImplExt_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); + if (IsDMServiceAdapterSoLoaded()) { + dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); } } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { DMAccountInfo dmAccountInfo; @@ -1709,8 +1662,8 @@ void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string { LOGI("UserId: %{public}d, accountId: %{public}s, accountName: %{public}s", userId, GetAnonyString(accountId).c_str(), GetAnonyString(accountName).c_str()); - if (IsDMServiceAdapterLoad()) { - dmServiceImplExt_->AccountIdLogout(userId, accountId); + if (IsDMServiceAdapterSoLoaded()) { + dmServiceImplExtResident_->AccountIdLogout(userId, accountId); } if (!IsDMServiceImplReady()) { LOGE("Init impl failed."); @@ -1944,8 +1897,8 @@ void DeviceManagerService::HandleDeviceNotTrust(const std::string &msg) if (IsDMServiceImplReady()) { dmServiceImpl_->HandleDeviceNotTrust(udid); } - if (IsDMServiceAdapterLoad()) { - dmServiceImplExt_->HandleDeviceNotTrust(udid); + if (IsDMServiceAdapterSoLoaded()) { + dmServiceImplExtResident_->HandleDeviceNotTrust(udid); } return; } @@ -1991,11 +1944,11 @@ int32_t DeviceManagerService::SetDnPolicy(const std::string &pkgName, std::mapsecond.c_str()); int32_t timeOut = std::atoi(timeOutIter->second.c_str()); LOGD("strategy: %{public}d, timeOut: %{public}d", policyStrategy, timeOut); - if (!IsDMServiceAdapterLoad()) { + if (!IsDMServiceAdapterResidentLoad()) { LOGE("SetDnPolicy failed, instance not init or init failed."); return ERR_DM_UNSUPPORTED_METHOD; } - return dmServiceImplExt_->SetDnPolicy(policyStrategy, timeOut); + return dmServiceImplExtResident_->SetDnPolicy(policyStrategy, timeOut); } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index cee773b82..e8ddea5e0 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -1647,27 +1647,6 @@ HWTEST_F(DeviceManagerServiceTest, UnloadDMServiceImplSo_001, testing::ext::Test EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr); } -HWTEST_F(DeviceManagerServiceTest, IsDMServiceAdapterLoad_001, testing::ext::TestSize.Level0) -{ - DeviceManagerService::GetInstance().isImplsoLoaded_ = false; - DeviceManagerService::GetInstance().IsDMServiceAdapterLoad(); - bool ret = DeviceManagerService::GetInstance().IsDMServiceImplReady(); - EXPECT_EQ(ret, true); -} - -HWTEST_F(DeviceManagerServiceTest, UnloadDMServiceAdapter_001, testing::ext::TestSize.Level0) -{ - DeviceManagerService::GetInstance().UnloadDMServiceAdapter(); - EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr); -} - -HWTEST_F(DeviceManagerServiceTest, UnloadDMServiceAdapter_002, testing::ext::TestSize.Level0) -{ - DeviceManagerService::GetInstance().dmServiceImplExt_ = nullptr; - DeviceManagerService::GetInstance().UnloadDMServiceAdapter(); - EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr); -} - HWTEST_F(DeviceManagerServiceTest, StartDiscovering_001, testing::ext::TestSize.Level0) { DeletePermission(); diff --git a/test/unittest/UTTest_device_manager_service_three.cpp b/test/unittest/UTTest_device_manager_service_three.cpp index 77a8e4bf9..4f701fd43 100644 --- a/test/unittest/UTTest_device_manager_service_three.cpp +++ b/test/unittest/UTTest_device_manager_service_three.cpp @@ -293,7 +293,7 @@ HWTEST_F(DeviceManagerServiceThreeTest, BindTarget_301, testing::ext::TestSize.L bindParam.insert(std::make_pair(PARAM_KEY_META_TYPE, pkgName)); EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(true)); - EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterLoad()).WillOnce(Return(false)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterResidentLoad()).WillOnce(Return(false)); ret = DeviceManagerService::GetInstance().BindTarget(pkgName, targetId, bindParam); EXPECT_EQ(ret, ERR_DM_UNSUPPORTED_METHOD); } @@ -373,7 +373,7 @@ HWTEST_F(DeviceManagerServiceThreeTest, ExportAuthCode_301, testing::ext::TestSi int32_t userId = 0; std::string accountId; std::string accountName; - EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterLoad()).WillOnce(Return(false)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterSoLoaded()).WillOnce(Return(false)); EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); DeviceManagerService::GetInstance().HandleAccountLogout(userId, accountId, accountName); @@ -391,7 +391,7 @@ HWTEST_F(DeviceManagerServiceThreeTest, UnbindTarget_301, testing::ext::TestSize std::string pkgName = "pkgName"; PeerTargetId targetId; std::map unbindParam; - EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterLoad()).WillOnce(Return(false)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterResidentLoad()).WillOnce(Return(false)); int32_t ret = DeviceManagerService::GetInstance().UnbindTarget(pkgName, targetId, unbindParam); EXPECT_EQ(ret, ERR_DM_UNSUPPORTED_METHOD); } @@ -404,9 +404,8 @@ HWTEST_F(DeviceManagerServiceThreeTest, SetDnPolicy_301, testing::ext::TestSize. policy[PARAM_KEY_POLICY_STRATEGY_FOR_BLE] = "100"; policy[PARAM_KEY_POLICY_TIME_OUT] = "10"; std::string processName = "collaboration_service"; - EXPECT_CALL(*permissionManagerMock_, GetCallerProcessName(_)) - .WillOnce(DoAll(SetArgReferee<0>(processName), Return(DM_OK))); - EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterLoad()).WillOnce(Return(false)); + EXPECT_CALL(*permissionManagerMock_, CheckInterfacePermission(_)).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterResidentLoad()).WillOnce(Return(false)); int32_t ret = DeviceManagerService::GetInstance().SetDnPolicy(packName, policy); ASSERT_EQ(ret, ERR_DM_UNSUPPORTED_METHOD); diff --git a/test/unittest/mock/device_manager_service_mock.cpp b/test/unittest/mock/device_manager_service_mock.cpp index eb31d8b47..a6db438a6 100644 --- a/test/unittest/mock/device_manager_service_mock.cpp +++ b/test/unittest/mock/device_manager_service_mock.cpp @@ -30,9 +30,14 @@ int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, s return DmDeviceManagerService::dmDeviceManagerService->GetTrustedDeviceList(pkgName, deviceList); } -bool DeviceManagerService::IsDMServiceAdapterLoad() +bool DeviceManagerService::IsDMServiceAdapterResidentLoad() { - return DmDeviceManagerService::dmDeviceManagerService->IsDMServiceAdapterLoad(); + return DmDeviceManagerService::dmDeviceManagerService->IsDMServiceAdapterResidentLoad(); +} + +bool DeviceManagerService::IsDMServiceAdapterSoLoaded() +{ + return DmDeviceManagerService::dmDeviceManagerService->IsDMServiceAdapterSoLoaded(); } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_manager_service_mock.h b/test/unittest/mock/device_manager_service_mock.h index 3339012e6..ebbb059b6 100644 --- a/test/unittest/mock/device_manager_service_mock.h +++ b/test/unittest/mock/device_manager_service_mock.h @@ -28,7 +28,8 @@ public: public: virtual bool IsDMServiceImplReady() = 0; virtual int32_t GetTrustedDeviceList(const std::string &pkgName, std::vector &deviceList) = 0; - virtual bool IsDMServiceAdapterLoad() = 0; + virtual bool IsDMServiceAdapterResidentLoad() = 0; + virtual bool IsDMServiceAdapterSoLoaded() = 0; public: static inline std::shared_ptr dmDeviceManagerService = nullptr; }; @@ -37,7 +38,8 @@ class DeviceManagerServiceMock : public DmDeviceManagerService { public: MOCK_METHOD(bool, IsDMServiceImplReady, ()); MOCK_METHOD(int32_t, GetTrustedDeviceList, (const std::string &, std::vector &)); - MOCK_METHOD(bool, IsDMServiceAdapterLoad, ()); + MOCK_METHOD(bool, IsDMServiceAdapterResidentLoad, ()); + MOCK_METHOD(bool, IsDMServiceAdapterSoLoaded, ()); }; } } -- Gitee