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 39a0eec92eaa704918293698f6e7c6382395f45f..4f738fc7cac35b6dbc1356a306d56fc22437940e 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 @@ -112,6 +112,7 @@ public: virtual void ClearCacheWhenLogout(int32_t userId, const std::string &oldAccountId) = 0; virtual void HandleScreenLockEvent(bool isLock) = 0; virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; + virtual void HandleUserSwitchEvent(int32_t currentUserId, int32_t beforeUserId) = 0; }; using CreateDMServiceExtResidentFuncPtr = IDMServiceImplExtResident *(*)(void); diff --git a/services/implementation/src/ability/standard/dm_dialog_manager.cpp b/services/implementation/src/ability/standard/dm_dialog_manager.cpp index 1f8d7d094e01acf7195fd2f420ce1293052f2254..156d95b885f03fb87cee74eed45afebabacaf55b 100644 --- a/services/implementation/src/ability/standard/dm_dialog_manager.cpp +++ b/services/implementation/src/ability/standard/dm_dialog_manager.cpp @@ -236,11 +236,11 @@ void DmDialogManager::SendMsgRequest(const sptr& remoteObject) if (abilityName_ == INPUT_ABILITY_NAME) { param["sysDialogZOrder"] = WINDOW_LEVEL_UPPER; } - std::string pinCodeHash = GetAnonyString(Crypto::Sha256(pinCode_)); - LOGI("OnAbilityConnectDone pinCodeHash: %{public}s", pinCodeHash.c_str()); param["isProxyBind"] = isProxyBind_; param["appUserData"] = appUserData_; param["title"] = title_; + std::string pinCodeHash = GetAnonyString(Crypto::Sha256(pinCode_)); + LOGI("OnAbilityConnectDone pinCodeHash: %{public}s", pinCodeHash.c_str()); param["pinCode"] = pinCode_; param["deviceName"] = deviceName_; param["appOperationStr"] = appOperationStr_; diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 51a976963e901454e6c5e14e09be8d329ae51543..dea0e21e5c724f5af9d35c7efeb5dddbc1e8b724 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -277,6 +277,7 @@ int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const ImportConfig(authMgr_, tokenId, pkgName); int32_t ret = AddAuthMgr(tokenId, sessionId, authMgr_); if (ret != DM_OK) { + authMgr_->ClearSoftbusSessionCallback(); return ret; } } @@ -423,7 +424,7 @@ int32_t DeviceManagerServiceImpl::AddAuthMgr(uint64_t tokenId, int sessionId, st } { std::lock_guard lock(authMgrMapMtx_); - if (authMgrMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { + if (authMgrMap_.size() >= MAX_NEW_PROC_SESSION_COUNT_TEMP) { LOGE("Other bind session exist, can not start new one. authMgrMap_.size:%{public}zu", authMgrMap_.size()); return ERR_DM_AUTH_BUSINESS_BUSY; } @@ -1911,7 +1912,7 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P "pkgName:%{public}s, tokenId:%{public}" PRIu64, pkgName.c_str(), tokenId); return ERR_DM_AUTH_BUSINESS_BUSY; } - if (tokenIdSessionIdMap_.size() > MAX_NEW_PROC_SESSION_COUNT_TEMP) { + if (tokenIdSessionIdMap_.size() >= MAX_NEW_PROC_SESSION_COUNT_TEMP) { LOGE("Other bind exist, can not start new one. size:%{public}zu", tokenIdSessionIdMap_.size()); return ERR_DM_AUTH_BUSINESS_BUSY; } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 069a2ad13e7f8ffb62039b4ea70a995ac1fc86be..709e7301f82ebded3a8b48bc725e3199ce8e06b4 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -340,31 +340,15 @@ private: std::string &udidHash); void HandleAccountLogout(int32_t userId, const std::string &accountId, const std::string &accountName); void HandleUserRemoved(int32_t removedUserId); - /** - * @brief process the user switch - * - * @param currentUserId the user id which switched to foreground. - * @param beforeUserId the user id which switched to backend. - */ - void HandleUserSwitched(int32_t curUserId, int32_t preUserId); void HandleUserIdsBroadCast(const std::vector &remoteUserIdInfos, const std::string &remoteUdid, bool isNeedResponse); void HandleShareUnbindBroadCast(const int32_t userId, const std::string &credId); - void NotifyRemoteLocalUserSwitch(int32_t curUserId, int32_t preUserId, const std::vector &peerUdids, - const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); - void NotifyRemoteLocalUserSwitchByWifi(int32_t curUserId, int32_t preUserId, - const std::map &wifiDevices, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds); - int32_t SendUserIdsByWifi(const std::string &networkId, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds); - void HandleUserSwitchTimeout(int32_t curUserId, int32_t preUserId, const std::string &udid); bool InitDPLocalServiceInfo(const DMLocalServiceInfo &serviceInfo, DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceItem); void InitServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceItem, DMLocalServiceInfo &serviceInfo); void InitServiceInfos(const std::vector &dpLocalServiceItems, std::vector &serviceInfos); - void HandleUserSwitched(); void NotifyRemoteUninstallApp(int32_t userId, int32_t tokenId); void NotifyRemoteUninstallAppByWifi(int32_t userId, int32_t tokenId, @@ -378,13 +362,6 @@ private: int32_t SendUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra, const std::string &networkId, const std::string &udid); - void NotifyRemoteLocalUserSwitch(const std::string &localUdid, const std::vector &peerUdids, - const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); - void NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, - const std::map &wifiDevices, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds); - void HandleUserSwitchTimeout(const std::string &localUdid, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds, const std::string &udid); void UpdateAclAndDeleteGroup(const std::string &localUdid, const std::vector &deviceVec, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); void HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId); diff --git a/services/service/include/devicenamemgr/device_name_manager.h b/services/service/include/devicenamemgr/device_name_manager.h index ed68ca896c0116ba810c879ec5e77f2ceb7aed27..20aa157bcd851f7e44fc9471ff9439045b59f375 100644 --- a/services/service/include/devicenamemgr/device_name_manager.h +++ b/services/service/include/devicenamemgr/device_name_manager.h @@ -45,6 +45,7 @@ public: int32_t RestoreLocalDeviceName(); int32_t InitDeviceNameWhenLanguageOrRegionChanged(); std::string GetUserDefinedDeviceName(); + std::string GetLocalMarketName(); private: DeviceNameManager() = default; @@ -60,7 +61,6 @@ private: std::string GetSystemLanguage(); std::string GetSystemRegion(); - std::string GetLocalMarketName(); int32_t InitDisplayDeviceNameToSettingsData(const std::string &nickName, const std::string &deviceName, int32_t userId); diff --git a/services/service/include/permission/lite/permission_manager.h b/services/service/include/permission/lite/permission_manager.h index 0421f096a1ce9a2171760921b735db22bd6f0558..ff1e02302f2dd65e04663c37c4c05b55ee0500bf 100644 --- a/services/service/include/permission/lite/permission_manager.h +++ b/services/service/include/permission/lite/permission_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -41,6 +41,7 @@ public: bool CheckProcessNameValidModifyRemoteDeviceName(const std::string &processName); bool CheckProcessNameValidPutDeviceProfileInfoList(const std::string &processName); bool CheckProcessValidOnGetTrustedDeviceList(); + bool CheckReadLocalDeviceName(void); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/include/permission/standard/permission_manager.h b/services/service/include/permission/standard/permission_manager.h index d5aab748c28af3a3fdf91c9fcad9076ebaa51c2b..21d057b9a027ee22cf8dcd427555dbc40335c7c3 100644 --- a/services/service/include/permission/standard/permission_manager.h +++ b/services/service/include/permission/standard/permission_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -40,6 +40,7 @@ public: bool CheckProcessNameValidModifyRemoteDeviceName(const std::string &processName); bool CheckProcessNameValidPutDeviceProfileInfoList(const std::string &processName); bool CheckProcessValidOnGetTrustedDeviceList(); + bool CheckReadLocalDeviceName(void); private: bool VerifyAccessTokenByPermissionName(const std::string& permissionName); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index bf85b5fca313bd8ddf358eec90b32fb44a33ce4a..707429bb2109385a93a67e18411d1d82fb86d426 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -2381,50 +2381,6 @@ void DeviceManagerService::HandleAccountCommonEvent(const std::string commonEven NotifyRemoteAccountCommonEvent(commonEventType, localUdid, peerUdids, foregroundUserVec, backgroundUserVec); } -void DeviceManagerService::HandleUserSwitched() -{ - LOGI("start."); - std::vector foregroundUserVec; - int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); - std::vector backgroundUserVec; - int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); - if (retFront != DM_OK || retBack != DM_OK || foregroundUserVec.empty()) { - LOGE("Get userids failed, retFront: %{public}d, retBack: %{public}d, foreground user num: %{public}d", - retFront, retBack, static_cast(foregroundUserVec.size())); - return; - } - char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); - std::string localUdid = std::string(localUdidTemp); - CHECK_NULL_VOID(discoveryMgr_); - if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { - LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); - return; - } - if (!discoveryMgr_->GetCommonDependencyObj()->CheckAclStatusAndForegroundNotMatch(localUdid, - foregroundUserVec, backgroundUserVec)) { - LOGI("no unreasonable data."); - return; - } - std::map curUserDeviceMap = - discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(foregroundUserVec, localUdid); - std::map preUserDeviceMap = - discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(backgroundUserVec, localUdid); - std::vector peerUdids; - for (const auto &item : curUserDeviceMap) { - peerUdids.push_back(item.first); - } - for (const auto &item : preUserDeviceMap) { - if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) { - peerUdids.push_back(item.first); - } - } - if (peerUdids.empty()) { - return; - } - NotifyRemoteLocalUserSwitch(localUdid, peerUdids, foregroundUserVec, backgroundUserVec); -} - #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void DeviceManagerService::NotifyRemoteAccountCommonEvent(const std::string commonEventType, const std::string &localUdid, const std::vector &peerUdids, @@ -2543,88 +2499,6 @@ void DeviceManagerService::UpdateAcl(const std::string &localUdid, backgroundUserIds); } -void DeviceManagerService::NotifyRemoteLocalUserSwitch(const std::string &localUdid, - const std::vector &peerUdids, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds) -{ - LOGI("onstart UserSwitch, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s", - GetIntegerList(foregroundUserIds).c_str(), GetIntegerList(backgroundUserIds).c_str()); - if (peerUdids.empty()) { - return; - } - if (softbusListener_ == nullptr) { - UpdateAclAndDeleteGroup(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); - LOGE("softbusListener_ is null"); - return; - } - std::vector bleUdids; - std::map wifiDevices; - for (const auto &udid : peerUdids) { - std::string netWorkId = ""; - SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId); - if (netWorkId.empty()) { - LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str()); - bleUdids.push_back(udid); - continue; - } - int32_t networkType = 0; - int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType); - if (ret != DM_OK || networkType <= 0) { - LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str()); - bleUdids.push_back(udid); - continue; - } - uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE; - if ((static_cast(networkType) & addrTypeMask) != 0x0) { - bleUdids.push_back(udid); - } else { - wifiDevices.insert(std::pair(udid, netWorkId)); - } - } - if (!bleUdids.empty()) { - UpdateAclAndDeleteGroup(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); - SendUserIdsBroadCast(bleUdids, foregroundUserIds, backgroundUserIds, true); - } - if (!wifiDevices.empty()) { - NotifyRemoteLocalUserSwitchByWifi(localUdid, wifiDevices, foregroundUserIds, backgroundUserIds); - } -} - -void DeviceManagerService::NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, - const std::map &wifiDevices, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds) -{ - for (const auto &it : wifiDevices) { - int32_t result = SendUserIdsByWifi(it.second, foregroundUserIds, backgroundUserIds); - if (result != DM_OK) { - LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); - std::vector updateUdids; - updateUdids.push_back(it.first); - UpdateAclAndDeleteGroup(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); - continue; - } - if (timer_ == nullptr) { - timer_ = std::make_shared(); - } - std::string udid = it.first; - timer_->StartTimer(std::string(USER_SWITCH_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), - USER_SWITCH_BY_WIFI_TIMEOUT_S, - [this, localUdid, foregroundUserIds, backgroundUserIds, udid] (std::string name) { - DeviceManagerService::HandleUserSwitchTimeout(localUdid, foregroundUserIds, backgroundUserIds, udid); - }); - } -} - -void DeviceManagerService::HandleUserSwitchTimeout(const std::string &localUdid, - const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, - const std::string &udid) -{ - LOGI("start udid: %{public}s", GetAnonyString(udid).c_str()); - std::vector updateUdids; - updateUdids.push_back(udid); - UpdateAclAndDeleteGroup(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); -} - void DeviceManagerService::UpdateAclAndDeleteGroup(const std::string &localUdid, const std::vector &deviceVec, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) @@ -2680,41 +2554,6 @@ void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string } } -void DeviceManagerService::HandleUserSwitched(int32_t curUserId, int32_t preUserId) -{ - LOGI("currentUserId: %{public}d. previousUserId: %{public}d", curUserId, preUserId); - if (!IsDMServiceImplReady()) { - LOGE("Init impl failed."); - return; - } - std::map curUserDeviceMap; - std::map preUserDeviceMap; - std::vector peerUdids; - curUserDeviceMap = dmServiceImpl_->GetDeviceIdAndBindLevel(curUserId); - preUserDeviceMap = dmServiceImpl_->GetDeviceIdAndBindLevel(preUserId); - for (const auto &item : curUserDeviceMap) { - peerUdids.push_back(item.first); - } - for (const auto &item : preUserDeviceMap) { - if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) { - peerUdids.push_back(item.first); - } - } - if (peerUdids.empty()) { - return; - } - std::vector foregroundUserVec; - int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); - std::vector backgroundUserVec; - int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); - if (retFront != DM_OK || retBack != DM_OK || foregroundUserVec.empty()) { - LOGE("Get userids failed, retFront: %{public}d, retBack: %{public}d, foreground user num: %{public}d", - retFront, retBack, static_cast(foregroundUserVec.size())); - return; - } - NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserVec, backgroundUserVec); -} - void DeviceManagerService::HandleUserRemoved(int32_t removedUserId) { LOGI("PreUserId %{public}d.", removedUserId); @@ -3658,51 +3497,6 @@ int32_t DeviceManagerService::GetAnonyLocalUdid(const std::string &pkgName, std: } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) -void DeviceManagerService::NotifyRemoteLocalUserSwitch(int32_t curUserId, int32_t preUserId, - const std::vector &peerUdids, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds) -{ - LOGI("Send local foreground and background userids"); - if (peerUdids.empty()) { - return; - } - if (softbusListener_ == nullptr) { - dmServiceImpl_->HandleUserSwitched(peerUdids, curUserId, preUserId); - LOGE("softbusListener_ is null"); - return; - } - std::vector bleUdids; - std::map wifiDevices; - for (const auto &udid : peerUdids) { - std::string netWorkId = ""; - SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId); - if (netWorkId.empty()) { - LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str()); - bleUdids.push_back(udid); - continue; - } - int32_t networkType = 0; - int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType); - if (ret != DM_OK || networkType <= 0) { - LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str()); - bleUdids.push_back(udid); - continue; - } - uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE; - if ((static_cast(networkType) & addrTypeMask) != 0x0) { - bleUdids.push_back(udid); - } else { - wifiDevices.insert(std::pair(udid, netWorkId)); - } - } - if (!bleUdids.empty()) { - dmServiceImpl_->HandleUserSwitched(bleUdids, curUserId, preUserId); - SendUserIdsBroadCast(bleUdids, foregroundUserIds, backgroundUserIds, true); - } - if (!wifiDevices.empty()) { - NotifyRemoteLocalUserSwitchByWifi(curUserId, preUserId, wifiDevices, foregroundUserIds, backgroundUserIds); - } -} void DeviceManagerService::NotifyRemoteUninstallApp(int32_t userId, int32_t tokenId) { @@ -3882,70 +3676,6 @@ void DeviceManagerService::GetNotifyRemoteUnBindAppWay(int32_t userId, int32_t t } } -void DeviceManagerService::NotifyRemoteLocalUserSwitchByWifi(int32_t curUserId, int32_t preUserId, - const std::map &wifiDevices, const std::vector &foregroundUserIds, - const std::vector &backgroundUserIds) -{ - for (const auto &it : wifiDevices) { - int32_t result = SendUserIdsByWifi(it.second, foregroundUserIds, backgroundUserIds); - if (result != DM_OK) { - LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); - std::vector updateUdids; - updateUdids.push_back(it.first); - dmServiceImpl_->HandleUserSwitched(updateUdids, curUserId, preUserId); - continue; - } - if (timer_ == nullptr) { - timer_ = std::make_shared(); - } - std::string udid = it.first; - timer_->StartTimer(std::string(USER_SWITCH_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), - USER_SWITCH_BY_WIFI_TIMEOUT_S, [this, curUserId, preUserId, udid] (std::string name) { - DeviceManagerService::HandleUserSwitchTimeout(curUserId, preUserId, udid); - }); - } -} - -int32_t DeviceManagerService::SendUserIdsByWifi(const std::string &networkId, - const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) -{ - LOGI("Try open softbus session to exchange foreground/background userid"); - std::vector foregroundUserIdsUInt; - for (auto const &u : foregroundUserIds) { - foregroundUserIdsUInt.push_back(static_cast(u)); - } - std::vector backgroundUserIdsUInt; - for (auto const &u : backgroundUserIds) { - backgroundUserIdsUInt.push_back(static_cast(u)); - } - return DMCommTool::GetInstance()->SendUserIds(networkId, foregroundUserIdsUInt, backgroundUserIdsUInt); -} - -void DeviceManagerService::HandleUserSwitchTimeout(int32_t curUserId, int32_t preUserId, const std::string &udid) -{ - LOGI("start udid: %{public}s", GetAnonyString(udid).c_str()); - std::vector updateUdids; - updateUdids.push_back(udid); - dmServiceImpl_->HandleUserSwitched(updateUdids, curUserId, preUserId); -} - -void DeviceManagerService::HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId) -{ - LOGI("In"); - DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId); - MultipleUserConnector::SetAccountInfo(currentUserId, MultipleUserConnector::GetCurrentDMAccountInfo()); - if (IsPC()) { - return; - } - if (beforeUserId == -1 || currentUserId == -1) { - HandleUserSwitched(); - return; - } - if (beforeUserId != -1 && currentUserId != -1) { - HandleUserSwitched(currentUserId, beforeUserId); - } -} - void DeviceManagerService::HandleUserStopEvent(int32_t stopUserId) { LOGI("stopUserId %{public}s.", GetAnonyInt32(stopUserId).c_str()); @@ -4473,7 +4203,12 @@ int32_t DeviceManagerService::UnRegisterPinHolderCallback(const std::string &pkg int32_t DeviceManagerService::GetLocalDeviceName(std::string &deviceName) { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, deviceName); + if (PermissionManager::GetInstance().CheckReadLocalDeviceName()) { + return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, deviceName); + } else { + deviceName = DeviceNameManager::GetInstance().GetLocalMarketName(); + return DM_OK; + } #endif (void) deviceName; return DM_OK; @@ -4553,6 +4288,11 @@ void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &comm [this, commonEventType] () { DeviceManagerService::HandleAccountCommonEvent(commonEventType); }); + if (IsDMServiceAdapterResidentLoad()) { + dmServiceImplExtResident_->HandleUserSwitchEvent(currentUserId, beforeUserId); + } else { + LOGW("HandleUserSwitchEvent fail, adapter instance not init or init failed."); + } } void DeviceManagerService::GetHoOsTypeUdids(std::vector &peerUdids) diff --git a/services/service/src/devicenamemgr/device_name_manager.cpp b/services/service/src/devicenamemgr/device_name_manager.cpp index f586020495c9850a0aad41cd83405668ab2dce4b..4e05f530fb8a1ea59fe42f936e333c89237ac267 100644 --- a/services/service/src/devicenamemgr/device_name_manager.cpp +++ b/services/service/src/devicenamemgr/device_name_manager.cpp @@ -525,7 +525,6 @@ std::string DeviceNameManager::GetLocalMarketName() return ""; } localMarketName_ = marketName; - free((char *)marketName); } std::vector prefixs = DeviceManagerService::GetInstance().GetDeviceNamePrefixs(); for (const auto &item : prefixs) { diff --git a/services/service/src/permission/lite/permission_manager.cpp b/services/service/src/permission/lite/permission_manager.cpp index dbfa6d7f3631c3c95e130bac52f4c2592e427342..bf6e407426040ae3f69bca6e2effcfc43f6e1076 100644 --- a/services/service/src/permission/lite/permission_manager.cpp +++ b/services/service/src/permission/lite/permission_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -105,5 +105,10 @@ bool PermissionManager::CheckProcessValidOnGetTrustedDeviceList() { return true; } + +bool PermissionManager::CheckReadLocalDeviceName(void) +{ + return true; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/permission/standard/permission_manager.cpp b/services/service/src/permission/standard/permission_manager.cpp index fbcd7077505b4f93b95c59b93b06ef7b46a29846..d7036d738c73225d768b1b6a1710840f54ca305d 100644 --- a/services/service/src/permission/standard/permission_manager.cpp +++ b/services/service/src/permission/standard/permission_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -98,6 +98,8 @@ constexpr const static char* GET_TRUSTED_DEVICE_LIST_WHITE_LIST[] = { "distributedsched", }; constexpr uint32_t GET_TRUSTED_DEVICE_LIST_WHITE_LIST_NUM = std::size(GET_TRUSTED_DEVICE_LIST_WHITE_LIST); + +constexpr const char* READ_LOCAL_DEVICE_NAME_PERMISSION = "ohos.permission.READ_LOCAL_DEVICE_NAME"; } bool PermissionManager::CheckPermission(void) @@ -366,5 +368,22 @@ bool PermissionManager::CheckProcessValidOnGetTrustedDeviceList() } return false; } + +bool PermissionManager::CheckReadLocalDeviceName(void) +{ + AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + if (tokenCaller == 0) { + LOGE("CheckReadLocalDeviceName GetCallingTokenID error."); + return false; + } + ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller); + if ((tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) && + (AccessTokenKit::VerifyAccessToken(tokenCaller, READ_LOCAL_DEVICE_NAME_PERMISSION) == + PermissionState::PERMISSION_GRANTED)) { + return true; + } + LOGE("Read local device name permission is denied, please apply for corresponding permissions."); + return false; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/commonunittest/UTTest_permission_manager.cpp b/test/commonunittest/UTTest_permission_manager.cpp index 2ece3cf82720bafcbd8c136de865997b84f72d43..6e953b2ea6285ffa59fbb1c8a666710586eb6940 100644 --- a/test/commonunittest/UTTest_permission_manager.cpp +++ b/test/commonunittest/UTTest_permission_manager.cpp @@ -14,6 +14,7 @@ */ #include "UTTest_permission_manager.h" +#include "access_token.h" #include "accesstoken_kit.h" #include "nativetoken_kit.h" #include "token_setproc.h" @@ -271,6 +272,25 @@ HWTEST_F(PermissionManagerTest, CheckProcessNameValidOnGetDeviceInfo_001, testin ret = PermissionManager::GetInstance().CheckProcessNameValidOnGetDeviceInfo(processName); ASSERT_TRUE(ret); } + +HWTEST_F(PermissionManagerTest, CheckReadLocalDeviceName_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*ipcSkeletonMock_, GetCallingTokenID()).WillOnce(Return(0)); + bool ret = PermissionManager::GetInstance().CheckReadLocalDeviceName(); + ASSERT_FALSE(ret); + + EXPECT_CALL(*ipcSkeletonMock_, GetCallingTokenID()).WillOnce(Return(10)); + EXPECT_CALL(*accessTokenKitMock_, VerifyAccessToken(_, _)) + .WillOnce(Return(Security::AccessToken::PermissionState::PERMISSION_DENIED)); + ret = PermissionManager::GetInstance().CheckPermission(); + ASSERT_FALSE(ret); + + EXPECT_CALL(*ipcSkeletonMock_, GetCallingTokenID()).WillOnce(Return(10)); + EXPECT_CALL(*accessTokenKitMock_, VerifyAccessToken(_, _)) + .WillOnce(Return(Security::AccessToken::PermissionState::PERMISSION_GRANTED)); + ret = PermissionManager::GetInstance().CheckPermission(); + ASSERT_TRUE(ret); +} } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/commonunittest/UTTest_permission_manager.h b/test/commonunittest/UTTest_permission_manager.h index 20b409c342cc84ba9ae87f3d2f629f57a30125a5..459496bd3d40e5731e720ce1f73ce512880358b9 100644 --- a/test/commonunittest/UTTest_permission_manager.h +++ b/test/commonunittest/UTTest_permission_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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,10 +21,10 @@ #include #include +#include "dm_accesstoken_kit_mock.h" #include "dm_constants.h" -#include "permission_manager.h" #include "dm_ipc_skeleton_mock.h" -#include "dm_accesstoken_kit_mock.h" +#include "permission_manager.h" namespace OHOS { namespace DistributedHardware { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 53280a13ecad922482dedfce42d84da4fdc5a0c0..02d5243d7e47e5d24349ce822911061599f15593 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2024 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -482,6 +482,7 @@ ohos_unittest("UTTest_device_manager_service") { "UTTest_device_manager_service_two.cpp", "mock/app_manager_mock.cpp", "mock/device_manager_service_impl_mock.cpp", + "mock/device_name_manager_mock.cpp", "mock/deviceprofile_connector_mock.cpp", "mock/dm_comm_tool_mock.cpp", "mock/dm_crypto_mock.cpp", @@ -499,6 +500,7 @@ ohos_unittest("UTTest_device_manager_service") { "access_token:libnativetoken", "access_token:libtoken_setproc", "cJSON:cjson", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index bf2d124ea3e49f78a26cd4914f2dea39306772da..3496f2e2be571f840653a387b01ef134776b34e8 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -71,6 +71,7 @@ void DeviceManagerServiceTest::TearDown() Mock::VerifyAndClearExpectations(multipleUserConnectorMock_.get()); Mock::VerifyAndClearExpectations(dMCommToolMock_.get()); Mock::VerifyAndClearExpectations(deviceProfileConnectorMock_.get()); + Mock::VerifyAndClearExpectations(deviceNameManagerMock_.get()); } void DeviceManagerServiceTest::SetUpTestCase() @@ -85,6 +86,7 @@ void DeviceManagerServiceTest::SetUpTestCase() DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnectorMock_; DmDMCommTool::dmDMCommTool = dMCommToolMock_; DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_; + DmDeviceNameManager::dmDeviceNameManager_ = deviceNameManagerMock_; } void DeviceManagerServiceTest::TearDownTestCase() @@ -109,6 +111,8 @@ void DeviceManagerServiceTest::TearDownTestCase() dMCommToolMock_ = nullptr; DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr; deviceProfileConnectorMock_ = nullptr; + DmDeviceNameManager::dmDeviceNameManager_ = nullptr; + deviceNameManagerMock_ = nullptr; } namespace { diff --git a/test/unittest/UTTest_device_manager_service.h b/test/unittest/UTTest_device_manager_service.h index 4b7b8e87eb914c989461fe8dc1284168ac56ea1b..317c1dd2dccfba4e2477cfd241c05316af2d11da 100644 --- a/test/unittest/UTTest_device_manager_service.h +++ b/test/unittest/UTTest_device_manager_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -22,21 +22,21 @@ #include #include +#include "app_manager_mock.h" +#include "common_event_support.h" #include "device_manager_service.h" #include "device_manager_service_listener.h" -#include "device_manager_service_impl.h" -#include "dm_single_instance.h" -#include "common_event_support.h" -#include "permission_manager_mock.h" -#include "softbus_listener_mock.h" -#include "app_manager_mock.h" -#include "kv_adapter_manager_mock.h" #include "device_manager_service_impl_mock.h" -#include "dm_softbus_cache_mock.h" +#include "device_name_manager_mock.h" +#include "deviceprofile_connector_mock.h" +#include "dm_comm_tool_mock.h" #include "dm_crypto_mock.h" +#include "dm_single_instance.h" +#include "dm_softbus_cache_mock.h" +#include "kv_adapter_manager_mock.h" #include "multiple_user_connector_mock.h" -#include "dm_comm_tool_mock.h" -#include "deviceprofile_connector_mock.h" +#include "permission_manager_mock.h" +#include "softbus_listener_mock.h" namespace OHOS { namespace DistributedHardware { @@ -49,22 +49,24 @@ public: static inline std::shared_ptr permissionManagerMock_ = std::make_shared(); - static inline std::shared_ptr softbusListenerMock_ = + static inline std::shared_ptr softbusListenerMock_ = std::make_shared(); - static inline std::shared_ptr appManagerMock_ = + static inline std::shared_ptr appManagerMock_ = std::make_shared(); - static inline std::shared_ptr kVAdapterManagerMock_ = + static inline std::shared_ptr kVAdapterManagerMock_ = std::make_shared(); - static inline std::shared_ptr deviceManagerServiceImplMock_ = + static inline std::shared_ptr deviceManagerServiceImplMock_ = std::make_shared(); - static inline std::shared_ptr softbusCacheMock_ = + static inline std::shared_ptr softbusCacheMock_ = std::make_shared(); - static inline std::shared_ptr cryptoMock_ = std::make_shared(); - static inline std::shared_ptr multipleUserConnectorMock_ = + static inline std::shared_ptr cryptoMock_ = std::make_shared(); + static inline std::shared_ptr multipleUserConnectorMock_ = std::make_shared(); - static inline std::shared_ptr dMCommToolMock_ = std::make_shared(); - static inline std::shared_ptr deviceProfileConnectorMock_ = + static inline std::shared_ptr dMCommToolMock_ = std::make_shared(); + static inline std::shared_ptr deviceProfileConnectorMock_ = std::make_shared(); + static inline std::shared_ptr deviceNameManagerMock_ = + std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index 3c1e0aed2233392ac63cfa4585c80afa7b0861a9..e44d72c59f8e456e7f64882c022b5c39d81e11b6 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -2109,13 +2109,16 @@ HWTEST_F(DeviceManagerServiceImplTest, InitNewProtocolAuthMgr_001, testing::ext: uint64_t logicalSessionId = 456789; int sessionId = 1; const std::string pkgName = "InitNewProtocolAuthMgr"; + deviceManagerServiceImpl_->authMgrMap_.clear(); int32_t ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); + deviceManagerServiceImpl_->authMgrMap_.clear(); isSrcSide = false; ret = deviceManagerServiceImpl_->InitNewProtocolAuthMgr(isSrcSide, tokenId, logicalSessionId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); + deviceManagerServiceImpl_->authMgrMap_.clear(); } HWTEST_F(DeviceManagerServiceImplTest, InitOldProtocolAuthMgr_001, testing::ext::TestSize.Level1) @@ -2123,12 +2126,15 @@ HWTEST_F(DeviceManagerServiceImplTest, InitOldProtocolAuthMgr_001, testing::ext: uint64_t tokenId = 100002311; int sessionId = 1; const std::string pkgName = "InitOldProtocolAuthMgr"; + deviceManagerServiceImpl_->authMgrMap_.clear(); int32_t ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); + deviceManagerServiceImpl_->authMgrMap_.clear(); deviceManagerServiceImpl_->authMgr_ = nullptr; ret = deviceManagerServiceImpl_->InitOldProtocolAuthMgr(tokenId, pkgName, sessionId); EXPECT_EQ(ret, DM_OK); + deviceManagerServiceImpl_->authMgrMap_.clear(); } HWTEST_F(DeviceManagerServiceImplTest, CleanSessionMap_001, testing::ext::TestSize.Level1) diff --git a/test/unittest/UTTest_device_manager_service_three.cpp b/test/unittest/UTTest_device_manager_service_three.cpp index 5ca56ab1440f80cfc8d88825cf57f927149b6c42..2b27ca0d7e2760c568f3800a12174fc79a1ebec1 100644 --- a/test/unittest/UTTest_device_manager_service_three.cpp +++ b/test/unittest/UTTest_device_manager_service_three.cpp @@ -411,11 +411,7 @@ HWTEST_F(DeviceManagerServiceThreeTest, ExportAuthCode_301, testing::ext::TestSi EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); DeviceManagerService::GetInstance().HandleAccountLogout(userId, accountId, accountName); - int32_t curUserId = 0; int32_t preUserId = 1; - EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); - DeviceManagerService::GetInstance().HandleUserSwitched(curUserId, preUserId); - EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); DeviceManagerService::GetInstance().HandleUserRemoved(preUserId); } diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index 7c658a6ab6956673a24a6b5d73e4f0c3a28f2962..9f4011c5c63b642e5737d5b98b0b4c768d27dd98 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -656,20 +656,6 @@ HWTEST_F(DeviceManagerServiceTest, UnAuthenticateDevice_201, testing::ext::TestS EXPECT_CALL(*cryptoMock_, GetAccountIdHash(_, _)).WillOnce(Return(ERR_DM_FAILED)); DeviceManagerService::GetInstance().HandleAccountLogout(userId, accountId, accountName); - int32_t curUserId = 0; - int32_t preUserId = 1; - std::map curUserDeviceMap; - curUserDeviceMap["curUserId"] = userId; - curUserDeviceMap["preUserId"] = userId; - std::map preUserDeviceMap; - preUserDeviceMap["accountId"] = userId; - preUserDeviceMap["accountName"] = 1; - EXPECT_CALL(*deviceManagerServiceImplMock_, - GetDeviceIdAndBindLevel(_)).WillOnce(Return(curUserDeviceMap)).WillOnce(Return(preUserDeviceMap)); - EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)).WillOnce(Return(ERR_DM_FAILED)); - EXPECT_CALL(*multipleUserConnectorMock_, GetBackgroundUserIds(_)).WillOnce(Return(ERR_DM_FAILED)); - DeviceManagerService::GetInstance().HandleUserSwitched(curUserId, preUserId); - int32_t removeId = 123; deviceMap.insert(std::make_pair("removeId", removeId)); EXPECT_CALL(*deviceManagerServiceImplMock_, GetDeviceIdAndUserId(_)).WillOnce(Return(deviceMap)); @@ -1052,31 +1038,13 @@ HWTEST_F(DeviceManagerServiceTest, UnBindDevice_205, testing::ext::TestSize.Leve int32_t ret = DeviceManagerService::GetInstance().UnBindDevice(pkgName, udidHash, extra); EXPECT_NE(ret, DM_OK); - int32_t userId = 123456; - int32_t curUserId = 0; - int32_t preUserId = 1; - std::map curUserDeviceMap; - curUserDeviceMap["curUserId"] = userId; - curUserDeviceMap["preUserId"] = userId; - std::map preUserDeviceMap; - preUserDeviceMap["accountId"] = userId; - preUserDeviceMap["accountName"] = 1; - std::vector foregroundUserVec; - foregroundUserVec.push_back(101); - EXPECT_CALL(*deviceManagerServiceImplMock_, - GetDeviceIdAndBindLevel(_)).WillOnce(Return(curUserDeviceMap)).WillOnce(Return(preUserDeviceMap)); - EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)) - .Times(::testing::AtLeast(1)).WillRepeatedly(DoAll(SetArgReferee<0>(foregroundUserVec), Return(DM_OK))); - EXPECT_CALL(*multipleUserConnectorMock_, GetBackgroundUserIds(_)).WillRepeatedly(Return(DM_OK)); - DeviceManagerService::GetInstance().HandleUserSwitched(curUserId, preUserId); - std::vector remoteUserIdInfos; std::string remoteUdid; bool isNeedResponse = false; DeviceManagerService::GetInstance().HandleUserIdsBroadCast(remoteUserIdInfos, remoteUdid, isNeedResponse); std::vector peerUdids; - userId = 123; + int32_t userId = 123; uint64_t tokenId = 1; int32_t bindLevel = 1; uint64_t peerTokenId = 1; @@ -1122,90 +1090,6 @@ HWTEST_F(DeviceManagerServiceTest, GetAnonyLocalUdid_202, testing::ext::TestSize EXPECT_EQ(ret, DM_OK); } -HWTEST_F(DeviceManagerServiceTest, NotifyRemoteLocalUserSwitch_201, testing::ext::TestSize.Level1) -{ - int32_t curUserId = 1; - int32_t preUserId = 1; - std::vector peerUdids; - std::vector foregroundUserIds; - std::vector backgroundUserIds; - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserIds, - backgroundUserIds); - - peerUdids.push_back("peerUdid001"); - peerUdids.push_back("peerUdid002"); - DeviceManagerService::GetInstance().softbusListener_ = nullptr; - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserIds, - backgroundUserIds); - EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr); - - DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)).Times(::testing::AtLeast(2)) - .WillOnce(DoAll(SetArgReferee<1>(""), Return(DM_OK))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserIds, - backgroundUserIds); - EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr); - - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)).Times(::testing::AtLeast(2)) - .WillOnce(DoAll(SetArgReferee<1>("networkId"), Return(DM_OK))); - EXPECT_CALL(*softbusListenerMock_, GetNetworkTypeByNetworkId(_, _)) - .WillOnce(DoAll(SetArgReferee<1>(0), Return(ERR_DM_FAILED))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserIds, - backgroundUserIds); - EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr); - - DeviceManagerService::GetInstance().timer_ = std::make_shared(); - EXPECT_CALL(*dMCommToolMock_, SendUserIds(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)).Times(::testing::AtLeast(2)) - .WillOnce(DoAll(SetArgReferee<1>("networkId"), Return(DM_OK))); - EXPECT_CALL(*softbusListenerMock_, GetNetworkTypeByNetworkId(_, _)) - .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserIds, - backgroundUserIds); - EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr); - - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)).Times(::testing::AtLeast(2)) - .WillOnce(DoAll(SetArgReferee<1>("networkId"), Return(DM_OK))); - EXPECT_CALL(*softbusListenerMock_, GetNetworkTypeByNetworkId(_, _)) - .WillOnce(DoAll(SetArgReferee<1>(4), Return(DM_OK))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserIds, - backgroundUserIds); - EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr); -} - -HWTEST_F(DeviceManagerServiceTest, NotifyRemoteLocalUserSwitchByWifi_201, testing::ext::TestSize.Level1) -{ - DeviceManagerService::GetInstance().timer_ = std::make_shared(); - int32_t curUserId = 1; - int32_t preUserId = 1; - std::map wifiDevices; - std::vector foregroundUserIds; - std::vector backgroundUserIds; - wifiDevices.insert(std::make_pair("kdmalsalskalw002", "networkId008")); - EXPECT_CALL(*dMCommToolMock_, SendUserIds(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitchByWifi(curUserId, preUserId, wifiDevices, - foregroundUserIds, backgroundUserIds); - EXPECT_NE(DeviceManagerService::GetInstance().timer_, nullptr); - - std::string udid = "udid"; - DeviceManagerService::GetInstance().HandleUserSwitchTimeout(curUserId, preUserId, udid); -} - -HWTEST_F(DeviceManagerServiceTest, SendUserIdsByWifi_201, testing::ext::TestSize.Level1) -{ - std::string networkId = "networkId001"; - std::vector foregroundUserIds; - std::vector backgroundUserIds; - foregroundUserIds.push_back(101); - foregroundUserIds.push_back(102); - backgroundUserIds.push_back(103); - backgroundUserIds.push_back(104); - EXPECT_CALL(*dMCommToolMock_, SendUserIds(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); - int32_t ret = DeviceManagerService::GetInstance().SendUserIdsByWifi(networkId, foregroundUserIds, - backgroundUserIds); - EXPECT_EQ(ret, ERR_DM_FAILED); -} - HWTEST_F(DeviceManagerServiceTest, StopAuthenticateDevice_004, testing::ext::TestSize.Level1) { std::string pkgName = "pkgName_004"; @@ -1421,10 +1305,14 @@ HWTEST_F(DeviceManagerServiceTest, GetLocalDisplayDeviceName_202, testing::ext:: { std::string pkgName = "packName"; int32_t maxNameLength = 1; - std::string displayName = "displayName"; + std::string displayName = ""; + std::string deviceName = "displayName"; + EXPECT_CALL(*deviceNameManagerMock_, GetLocalDisplayDeviceName(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(deviceName), Return(0))); EXPECT_CALL(*permissionManagerMock_, CheckPermission()).WillOnce(Return(true)); int32_t ret = DeviceManagerService::GetInstance().GetLocalDisplayDeviceName(pkgName, maxNameLength, displayName); - EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(displayName, deviceName); } HWTEST_F(DeviceManagerServiceTest, GetDeviceNamePrefixs_201, testing::ext::TestSize.Level1) @@ -1562,51 +1450,6 @@ HWTEST_F(DeviceManagerServiceTest, UpdateLocalServiceInfo_201, testing::ext::Tes EXPECT_CALL(*deviceProfileConnectorMock_, UpdateLocalServiceInfo(_)).WillOnce(Return(DM_OK)); int32_t ret = DeviceManagerService::GetInstance().UpdateLocalServiceInfo(serviceInfo); EXPECT_EQ(ret, DM_OK); - - std::string localUdid = "localUdid"; - std::vector peerUdids{"kxjasdkaj"}; - std::vector foregroundUserIds{1, 2}; - std::vector backgroundUserIds{1, 2}; - DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)).WillOnce(DoAll(SetArgReferee<1>(""), Return(DM_OK))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(localUdid, peerUdids, - foregroundUserIds, backgroundUserIds); - - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)) - .WillOnce(DoAll(SetArgReferee<1>("net*****7"), Return(DM_OK))); - EXPECT_CALL(*softbusListenerMock_, GetNetworkTypeByNetworkId(_, _)) - .WillOnce(DoAll(SetArgReferee<1>(1), Return(ERR_DM_FAILED))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(localUdid, peerUdids, - foregroundUserIds, backgroundUserIds); - - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)) - .WillOnce(DoAll(SetArgReferee<1>("net*****7"), Return(DM_OK))); - EXPECT_CALL(*softbusListenerMock_, GetNetworkTypeByNetworkId(_, _)) - .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); - EXPECT_CALL(*dMCommToolMock_, SendUserIds(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(localUdid, peerUdids, - foregroundUserIds, backgroundUserIds); - - EXPECT_CALL(*softbusCacheMock_, GetNetworkIdFromCache(_, _)) - .WillOnce(DoAll(SetArgReferee<1>("net*****7"), Return(DM_OK))); - EXPECT_CALL(*softbusListenerMock_, GetNetworkTypeByNetworkId(_, _)) - .WillOnce(DoAll(SetArgReferee<1>(4), Return(DM_OK))); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(localUdid, peerUdids, - foregroundUserIds, backgroundUserIds); - - std::map wifiDevices; - wifiDevices.insert(std::make_pair("deviceName", "networkwifi")); - EXPECT_CALL(*dMCommToolMock_, SendUserIds(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitchByWifi(localUdid, wifiDevices, - foregroundUserIds, backgroundUserIds); - - GTEST_LOG_(INFO) << "NotifyRemoteLocalUserSwitchByWifi SendUserIds is ok" ; - EXPECT_CALL(*dMCommToolMock_, SendUserIds(_, _, _)).WillOnce(Return(DM_OK)); - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitchByWifi(localUdid, wifiDevices, - foregroundUserIds, backgroundUserIds); - GTEST_LOG_(INFO) << "NotifyRemoteLocalUserSwitchByWifi end" ; - sleep(3); - DeviceManagerService::GetInstance().softbusListener_ = nullptr; } HWTEST_F(DeviceManagerServiceTest, GetLocalServiceInfoByBundleNameAndPinExchangeType_201, @@ -1621,40 +1464,6 @@ HWTEST_F(DeviceManagerServiceTest, GetLocalServiceInfoByBundleNameAndPinExchange int32_t ret = DeviceManagerService::GetInstance().GetLocalServiceInfoByBundleNameAndPinExchangeType(bundleName, pinExchangeType, serviceInfo); EXPECT_EQ(ret, DM_OK); - - std::vector foregroundUserVec{1, 2, 3}; - DeviceManagerService::GetInstance().InitDMServiceListener(); - EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)) - .WillOnce(DoAll(SetArgReferee<0>(foregroundUserVec), Return(DM_OK))); - EXPECT_CALL(*multipleUserConnectorMock_, GetBackgroundUserIds(_)).WillOnce(Return(DM_OK)); - EXPECT_CALL(*deviceProfileConnectorMock_, CheckAclStatusAndForegroundNotMatch(_, _, _)).WillOnce(Return(false)); - DeviceManagerService::GetInstance().HandleUserSwitched(); - - EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)) - .WillOnce(DoAll(SetArgReferee<0>(foregroundUserVec), Return(DM_OK))); - EXPECT_CALL(*multipleUserConnectorMock_, GetBackgroundUserIds(_)).WillOnce(Return(DM_OK)); - EXPECT_CALL(*deviceProfileConnectorMock_, CheckAclStatusAndForegroundNotMatch(_, _, _)).WillOnce(Return(true)); - std::map curUserDeviceMap; - std::map preUserDeviceMap; - curUserDeviceMap.insert(std::make_pair("curdevice***ww", 10)); - preUserDeviceMap.insert(std::make_pair("preUser******info", 11)); - EXPECT_CALL(*deviceProfileConnectorMock_, - GetDeviceIdAndBindLevel(_, _)).WillOnce(Return(curUserDeviceMap)).WillOnce(Return(preUserDeviceMap)); - DeviceManagerService::GetInstance().HandleUserSwitched(); - - std::string localUdid = "localUdid"; - std::string udid = "u*********90"; - std::vector backgroundUserIds{1, 2, 3}; - DeviceManagerService::GetInstance().HandleUserSwitchTimeout(localUdid, foregroundUserVec, backgroundUserIds, udid); - - std::vector peerUdids; - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(localUdid, peerUdids, foregroundUserVec, - backgroundUserIds); - - DeviceManagerService::GetInstance().softbusListener_ = nullptr; - DeviceManagerService::GetInstance().NotifyRemoteLocalUserSwitch(localUdid, peerUdids, foregroundUserVec, - backgroundUserIds); - DeviceManagerService::GetInstance().UninitDMServiceListener(); } HWTEST_F(DeviceManagerServiceTest, InitDPLocalServiceInfo_001, testing::ext::TestSize.Level0) @@ -1952,9 +1761,22 @@ HWTEST_F(DeviceManagerServiceTest, HandleShareUnbindBroadCast_003, testing::ext: HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceName_201, testing::ext::TestSize.Level1) { - std::string deviceName = ""; - int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceName(deviceName); + std::string deviceName = "123"; + std::string displayName = ""; + EXPECT_CALL(*permissionManagerMock_, CheckReadLocalDeviceName()).WillOnce(Return(true)); + EXPECT_CALL(*deviceNameManagerMock_, GetLocalDisplayDeviceName(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(deviceName), Return(0))); + int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceName(displayName); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(displayName, deviceName); + + displayName = ""; + std::string marketName = "456"; + EXPECT_CALL(*permissionManagerMock_, CheckReadLocalDeviceName()).WillOnce(Return(false)); + EXPECT_CALL(*deviceNameManagerMock_, GetLocalMarketName()).WillOnce(Return(marketName)); + ret = DeviceManagerService::GetInstance().GetLocalDeviceName(displayName); EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(displayName, marketName); } HWTEST_F(DeviceManagerServiceTest, ValidateUnBindDeviceParams_201, testing::ext::TestSize.Level1) diff --git a/test/unittest/mock/device_name_manager_mock.cpp b/test/unittest/mock/device_name_manager_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21c7ca6c8b808e9bbcd0151c78324317bf276fcd --- /dev/null +++ b/test/unittest/mock/device_name_manager_mock.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_name_manager_mock.h" + +#include "gtest/gtest.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DeviceNameManager); +std::string DeviceNameManager::GetLocalMarketName() +{ + return DmDeviceNameManager::dmDeviceNameManager_->GetLocalMarketName(); +} + +int32_t DeviceNameManager::GetLocalDisplayDeviceName(int32_t maxNamelength, std::string &displayName) +{ + return DmDeviceNameManager::dmDeviceNameManager_->GetLocalDisplayDeviceName(maxNamelength, displayName); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_name_manager_mock.h b/test/unittest/mock/device_name_manager_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..3b34329ce3d8b7639c59a9de864facd8417d0a94 --- /dev/null +++ b/test/unittest/mock/device_name_manager_mock.h @@ -0,0 +1,40 @@ +/* + * 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_DM_DEVICE_NAME_MANAGER_MOCK_H +#define OHOS_DM_DEVICE_NAME_MANAGER_MOCK_H + +#include +#include + +#include "device_name_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class DmDeviceNameManager { +public: + virtual ~DmDeviceNameManager() = default; + virtual std::string GetLocalMarketName() = 0; + virtual int32_t GetLocalDisplayDeviceName(int32_t maxNamelength, std::string &displayName) = 0; + static inline std::shared_ptr dmDeviceNameManager_ = nullptr; +}; + +class DeviceNameManagerMock : public DmDeviceNameManager { +public: + MOCK_METHOD(std::string, GetLocalMarketName, ()); + MOCK_METHOD(int32_t, GetLocalDisplayDeviceName, (int32_t, std::string &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_DEVICE_NAME_MANAGER_MOCK_H diff --git a/test/unittest/mock/dm_service_impl_ext_resident_mock.h b/test/unittest/mock/dm_service_impl_ext_resident_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..b1b7291fe6f2baaf90ad0c4f047827c8caf50f86 --- /dev/null +++ b/test/unittest/mock/dm_service_impl_ext_resident_mock.h @@ -0,0 +1,59 @@ +/* + * 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_DM_SERVICE_IMPL_EXT_RESIDENT_MOCK_H +#define OHOS_DM_SERVICE_IMPL_EXT_RESIDENT_MOCK_H + +#include +#include + +#include "i_dm_service_impl_ext_resident.h" + +namespace OHOS { +namespace DistributedHardware { +class DMServiceImplExtResidentMock : public IDMServiceImplExtResident { +public: + MOCK_METHOD(int32_t, Initialize, (const std::shared_ptr &)); + MOCK_METHOD(int32_t, Release, ()); + MOCK_METHOD(bool, IsDMServiceAdapterLoad, ()); + MOCK_METHOD(bool, IsDMServiceAdapterSoLoaded, ()); + MOCK_METHOD(int32_t, BindTargetExt, (const std::string &, const PeerTargetId &, + (const std::map &))); + MOCK_METHOD(int32_t, UnbindTargetExt, (const std::string &, const PeerTargetId &, + (const std::map &))); + MOCK_METHOD(int32_t, HandleDeviceStatusChange, (DmDeviceState, const DmDeviceInfo &)); + MOCK_METHOD(int32_t, ReplyUiAction, (const std::string &, int32_t, const std::string &)); + MOCK_METHOD(int32_t, AccountIdLogout, (int32_t, const std::string &, (const std::vector &))); + MOCK_METHOD(void, HandleDeviceNotTrust, (const std::string &)); + MOCK_METHOD(int32_t, SetDnPolicy, (int32_t, int32_t)); + MOCK_METHOD(int32_t, AccountUserSwitched, (int32_t, const std::string &)); + MOCK_METHOD(int32_t, GetDeviceProfileInfoList, (const std::string &, const DmDeviceProfileInfoFilterOptions &)); + MOCK_METHOD(int32_t, GetDeviceIconInfo, (const std::string &, const DmDeviceIconInfoFilterOptions &)); + MOCK_METHOD(int32_t, PutDeviceProfileInfoList, (const std::string &, + (const std::vector &))); + MOCK_METHOD((std::vector), GetDeviceNamePrefixs, ()); + MOCK_METHOD(void, HandleNetworkConnected, (int32_t)); + MOCK_METHOD(int32_t, SetLocalDeviceName, (const std::string &, const std::string &)); + MOCK_METHOD(int32_t, SetRemoteDeviceName, (const std::string &, const std::string &, const std::string &)); + MOCK_METHOD(int32_t, GetDeviceProfileInfosFromLocalCache, (const NetworkIdQueryFilter &, + (std::vector &))); + MOCK_METHOD(int32_t, RestoreLocalDeviceName, ()); + MOCK_METHOD(void, ClearCacheWhenLogout, (int32_t, const std::string &)); + MOCK_METHOD(void, HandleScreenLockEvent, (bool)); + MOCK_METHOD(int32_t, OpenAuthSessionWithPara, (const std::string &, int32_t, bool)); + MOCK_METHOD(void, HandleUserSwitchEvent, (int32_t, int32_t)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_SERVICE_IMPL_EXT_RESIDENT_MOCK_H diff --git a/test/unittest/mock/permission_manager_mock.cpp b/test/unittest/mock/permission_manager_mock.cpp index becbab5e69719d172c38fd31877cfed11d31471e..675b86e1b782225d3b3bf819683e75311cd4e2a0 100644 --- a/test/unittest/mock/permission_manager_mock.cpp +++ b/test/unittest/mock/permission_manager_mock.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 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 @@ -69,5 +69,10 @@ bool PermissionManager::CheckProcessNameValidPutDeviceProfileInfoList(const std: { return DmPermissionManager::dmPermissionManager->CheckProcessNameValidPutDeviceProfileInfoList(processName); } + +bool PermissionManager::CheckReadLocalDeviceName() +{ + return DmPermissionManager::dmPermissionManager->CheckReadLocalDeviceName(); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/permission_manager_mock.h b/test/unittest/mock/permission_manager_mock.h index 29bb5ba5120897bf9fc648b218c69100de6b2d37..7cef7f02bfcf1398cc165b6aec82e38a10afd93f 100644 --- a/test/unittest/mock/permission_manager_mock.h +++ b/test/unittest/mock/permission_manager_mock.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Huawei Device Co., Ltd. + * Copyright (C) 2024-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 @@ -36,6 +36,7 @@ public: virtual bool CheckProcessNameValidModifyLocalDeviceName(const std::string &processName) = 0; virtual bool CheckProcessNameValidModifyRemoteDeviceName(const std::string &processName) = 0; virtual bool CheckProcessNameValidPutDeviceProfileInfoList(const std::string &processName) = 0; + virtual bool CheckReadLocalDeviceName() = 0; public: static inline std::shared_ptr dmPermissionManager = nullptr; }; @@ -52,6 +53,7 @@ public: MOCK_METHOD(bool, CheckProcessNameValidModifyLocalDeviceName, (const std::string &)); MOCK_METHOD(bool, CheckProcessNameValidModifyRemoteDeviceName, (const std::string &)); MOCK_METHOD(bool, CheckProcessNameValidPutDeviceProfileInfoList, (const std::string &)); + MOCK_METHOD(bool, CheckReadLocalDeviceName, ()); }; } }