From 81da5577387e58367d41e97caef3c2e82e2ebdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Mon, 21 Jul 2025 11:16:05 +0800 Subject: [PATCH] service_code_1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- common/include/dm_constants.h | 6 + common/src/dm_constants.cpp | 6 + .../include/deviceprofile_connector.h | 17 + .../src/deviceprofile_connector.cpp | 180 ++++++++ .../native_cpp/include/dm_device_info.h | 89 +++- .../authentication_v2/dm_auth_context.h | 1 + .../dm_auth_message_processor.h | 3 + .../include/authentication_v2/dm_auth_state.h | 5 + .../auth_stages/auth_credential.cpp | 13 + .../dm_auth_message_processor.cpp | 154 ++++++- .../src/authentication_v2/dm_auth_state.cpp | 48 ++ .../service/include/device_manager_service.h | 27 +- .../include/softbus/softbus_listener.h | 2 + .../service/src/device_manager_service.cpp | 278 ++++++++++- .../service/src/softbus/softbus_listener.cpp | 14 + ...TTest_dm_deviceprofile_connector_three.cpp | 431 +++++++++++++++++ .../UTTest_dm_deviceprofile_connector_three.h | 44 ++ test/unittest/BUILD.gn | 32 ++ .../UTTest_device_manager_service_three.cpp | 436 ++++++++++++++++++ .../UTTest_device_manager_service_three.h | 6 + .../UTTest_dm_auth_message_processor.cpp | 393 ++++++++++++++++ .../UTTest_dm_auth_message_processor.h | 2 + .../mock/device_manager_service_mock.cpp | 16 + .../mock/device_manager_service_mock.h | 10 +- .../mock/deviceprofile_connector_mock.cpp | 18 + .../mock/deviceprofile_connector_mock.h | 10 + ...distributed_device_profile_client_mock.cpp | 14 + .../distributed_device_profile_client_mock.h | 6 + test/unittest/mock/softbus_listener_mock.cpp | 5 + test/unittest/mock/softbus_listener_mock.h | 2 + 30 files changed, 2255 insertions(+), 13 deletions(-) create mode 100644 test/commonunittest/UTTest_dm_deviceprofile_connector_three.cpp create mode 100644 test/commonunittest/UTTest_dm_deviceprofile_connector_three.h diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 2505b96ba..09c64db96 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -174,6 +174,12 @@ DM_EXPORT extern const char* DM_BUSINESS_ID; DM_EXPORT extern const char* DM_RISK; DM_EXPORT extern const char* DM_SAFE; +DM_EXPORT extern const char* TAG_SERVICE_BIND; +DM_EXPORT extern const char* TAG_SERVICE_INFO; +DM_EXPORT extern const char* TAG_SERVICE_INFO_NUM; +DM_EXPORT extern const char* IS_BIND_SERVICE; +DM_EXPORT extern const char* SERVICE_ID; + // screen state enum ScreenState { DM_SCREEN_UNKNOWN = -1, diff --git a/common/src/dm_constants.cpp b/common/src/dm_constants.cpp index 2a268e59e..da2dfa8ae 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -164,6 +164,12 @@ const char* DM_BUSINESS_ID = "business_id"; const char* DM_RISK = "risk"; const char* DM_SAFE = "safe"; +const char* TAG_SERVICE_BIND = "SERVICE_BIND"; +const char* TAG_SERVICE_INFO = "SERVICE_INFO"; +const char* TAG_SERVICE_INFO_NUM = "SERVICE_INFO_NUM"; +const char* IS_BIND_SERVICE = "isBindService"; +const char* SERVICE_ID = "serviceId"; + // errCode map const std::map MAP_ERROR_CODE = { { ERR_DM_TIME_OUT, 96929745 }, { ERR_DM_UNSUPPORTED_AUTH_TYPE, 96929760 }, { ERR_DM_AUTH_BUSINESS_BUSY, 96929761 }, diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index b6e6df3ec..ed1584e68 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -262,6 +262,18 @@ public: DM_EXPORT void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, const std::string &localUdid, int32_t localUserId, const std::string &localAccountId); + DM_EXPORT int32_t GetAllServiceInfoProfileList(std::vector &dmServiceInfoProfiles); + DM_EXPORT int32_t GetServiceInfoProfileListByTokenId(const DmServiceInfoUniqueKey &key, + std::vector &serviceInfos); + DM_EXPORT int32_t GetServiceInfoProfileListByDeviceId(const std::string &deviceId, + std::vector &serviceInfos); + DM_EXPORT int32_t DeleteServiceInfoProfile(const DmServiceInfoUniqueKey& key); + DM_EXPORT void GetLocalTokenIdByDeviceIdAndUserId(std::string localUdid, std::string udid, + std::map &tokenToKey); + DM_EXPORT int32_t GetServiceInfoByTokenId(DmServiceInfoUniqueKey key, + std::vector &serviceInfos); + + DM_EXPORT int32_t DeleteSessionKey(int32_t userId, int32_t sessionKeyId); DM_EXPORT int32_t GetSessionKey(int32_t userId, int32_t sessionKeyId, std::vector &sessionKeyArray); @@ -427,6 +439,11 @@ private: const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid); bool CheckExtWhiteList(const std::string &bundleName); + + // void TransferDpToDmServiceInfoProfile(const DmServiceInfoProfile &dmServiceInfoProfile, + // DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile); + // void TransferDpToDmServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile, + // DMServiceInfoProfile &dmServiceInfoProfile); }; extern "C" IDeviceProfileConnector *CreateDpConnectorInstance(); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 132a0a795..3fe0de948 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -592,6 +592,56 @@ void DeviceProfileConnector::FilterNeedDeleteACLInfos( } } +// void DeviceProfileConnector::TransferDpToDmServiceInfoProfile(const DmServiceInfoProfile &dmServiceInfoProfile, +// DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile) +// { +// dpServiceInfoProfile.SetDeviceId(dmServiceInfoProfile.deviceId); +// dpServiceInfoProfile.SetuserId(dmServiceInfoProfile.userId); +// dpServiceInfoProfile.SetNetworkId(dmServiceInfoProfile.networkId); +// dpServiceInfoProfile.SetTokenId(dmServiceInfoProfile.tokenId); +// dpServiceInfoProfile.SetServiceId(dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceId); +// dpServiceInfoProfile.SetServiceName(dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceName); +// dpServiceInfoProfile.SetServiceDisplayName(dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceDisplayName); +// dpServiceInfoProfile.SetCustomData(dmServiceInfoProfile.serviceRegInfo.serviceInfo.customData); +// dpServiceInfoProfile.SetCustomDataLen(dmServiceInfoProfile.serviceRegInfo.serviceInfo.dataLen); +// dpServiceInfoProfile.SetBundleName(dmServiceInfoProfile.serviceRegInfo.serviceInfo.bundleName); +// dpServiceInfoProfile.SetModuleName (dmServiceInfoProfile.serviceRegInfo.serviceInfo.moduleName); +// dpServiceInfoProfile.SetAbilityName(dmServiceInfoProfile.sserviceRegInfo.serviceInfo.abilityName); +// dpServiceInfoProfile.SetAuthBoxType(dmServiceInfoProfile.serviceRegInfo.authBoxType); +// dpServiceInfoProfile.SetAuthType(dmServiceInfoProfile.serviceRegInfo.authType); +// dpServiceInfoProfile.SetPinExchangeType (dmServiceInfoProfile.serviceRegInfo.pinExchangeType); +// dpServiceInfoProfile.SetPinCode(dmServiceInfoProfile.serviceRegInfo.pinCode); +// dpServiceInfoProfile.SetDescription(dmServiceInfoProfile.serviceRegInfo.description); +// dpServiceInfoProfile.SetServiceDicoveryScope(dmServiceInfoProfile.serviceRegInfo.serviceDiscoveryScope); +// dpServiceInfoProfile.SetExtraInfo(dmServiceInfoProfile.extraInfo); +// } + +// void DeviceProfileConnector::TransferDpToDmServiceInfoProfile( +// const DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile, +// DMServiceInfoProfile &dmServiceInfoProfile) +// { +// dmServiceInfoProfile.deviceId = dpServiceInfoProfile.GetDeviceId(); +// dmServiceInfoProfile.userId = dpServiceInfoProfile.GetUserId(); +// dmServiceInfoProfile.networkId = dpServiceInfoProfile.GetNetworkId(); +// dmServiceInfoProfile.tokenId = dpServiceInfoProfile.GetTokenId(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceId = dpServiceInfoProfile.GetServiceId(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceType = dpServiceInfoProfile.GetServiceType(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceName = dpServiceInfoProfile.GetServiceName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceDisplayName = dpServiceInfoProfile.GetServiceDisplayName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.customData = dpServiceInfoProfile.GetCustomData(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.dataLen = dpServiceInfoProfile.GetCustomDataLen(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.bundleName = dpServiceInfoProfile.GetBundleName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.moduleName = dpServiceInfoProfile.GetModuleName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.abilityName = dpServiceInfoProfile.GetAbilityName(); +// dmServiceInfoProfile.serviceRegInfo.authBoxType = dpServiceInfoProfile.GetAuthBoxType(); +// dmServiceInfoProfile.serviceReginfo.authType = dpServiceInfoProfile.GetAuthType(); +// dmServiceInfoProfile.serviceRegInfo.pinExchangeType= dpServiceInfoProfile.GetPinExchangeType(); +// dmServiceInfoProfile.serviceRegInfo.pinCode = dpServiceInfoProfile.GetPinCode(); +// dmServiceInfoProfile.serviceRegInfo.description = dpServiceInfoProfile.GetDescription(); +// dmServiceInfoProfile.serviceRegInfo.serviceDiscoveryScope = dpServiceInfoProfile.GetServieDicoveryScope(); +// dmServiceInfoProfile.extraInfo = dpServiceInfoProfile.GetExtraInfo(); +// } + DM_EXPORT std::vector DeviceProfileConnector::GetAccessControlProfile() { std::vector profiles; @@ -2716,6 +2766,136 @@ DM_EXPORT void DeviceProfileConnector::HandleDeviceUnBind(int32_t bindType, } } +DM_EXPORT int32_t DeviceProfileConnector::GetAllServiceInfoProfileList(std::vector &dmServiceInfoProfiles) +{ + // std::vector dpServiceInfoProfiles; + // int32_t ret = DistributedDeviceProfileClient::Getinstance().GetAllServiceInfoProfileList(dmServiceInfoProfiles); + // if (ret != DM_OK) { + // LOGE("failed: %{public}d",ret); + // return ret; + // } + // for (const auto &dpProfile : dpServiceInfoProfiles){ + // DmServiceInfoProfile dmProfile; + // TransferDpToDmServiceInfoProfile(dpProfile,dmProfile); + // dmServiceInfoProfiles.emplace_back(dmProfile); + // } + return DM_OK; +} + +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoProfileListByTokenId(const DmServiceInfoUniqueKey &key, + std::vector &serviceInfos) +{ + ServiceInfoUniqueKey dpKey; + dpKey.SetTokenId(key.tokenId); + dpKey.SetDeviceId(key.deviceId); + dpKey.SetUserId(key.userId); + std::vector dpServiceInfos; + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId(dpKey, + dpServiceInfos); + if (ret != DM_OK) { + LOGE("failed: %{public}d",ret); + return ret; + } + for (auto dpServiceInfoProfile : dpServiceInfos) { + DmServiceInfoProfile dmServiceInfoProfile; + //TransferDpToDmServiceInfoProfile(dpServiceInfoProfile,dmServiceInfoProfile); + serviceInfos.push_back(dmServiceInfoProfile); + } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileListByDeviceId(const std::string &deviceId, + std::vector &serviceInfos) +{ + std::vector serviceInfoProfiles; + + if (GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + if (serviceInfoProfiles.empty()) { + LOGE("GetServiceInfosByTokenId failed, serviceInfoProfiles is empty."); + return ERR_DM_FAILED; + } + for (auto const &item : serviceInfoProfiles) { + if (item.deviceId == deviceId) { + serviceInfos.push_back(item.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +int32_t DeviceProfileConnector::DeleteServiceInfoProfile(const DmServiceInfoUniqueKey& key) +{ + DistributedDeviceProfile::ServiceInfoUniqueKey dpKey; + dpKey.SetDeviceId(key.deviceId); + dpKey.SetServiceId(key.serviceId); + dpKey.SetTokenId(key.tokenId); + dpKey.SetUserId(key.userId); + int32_t ret = DistributedDeviceProfileClient::GetInstance().DeleteServiceInfoProfile(dpKey); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +void DeviceProfileConnector::GetLocalTokenIdByDeviceIdAndUserId(std::string localUdid, std::string udid, + std::map &tokenToKey) +{ + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::vector profiles = GetAclProfileByDeviceIdAndUserId(localUdid, userId); + + uint64_t localToken; + uint64_t peerToken; + DmServiceInfoUniqueKey key = { 0 }; + key.deviceId = udid; + for (auto &item : profiles){ + if (item.GetStatus()!= ACTIVE) { + continue; + } + if (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + item.GetAccesser().GetAccesserDeviceId() == udid) { + localToken = static_cast(item.GetAccessee().GetAccesseeTokenId()); + peerToken = static_cast(item.GetAccesser().GetAccesserTokenId()); + key.tokenId = std::to_string(peerToken); + key.userId = item.GetAccesser().GetAccesserUserId(); + } + if (item.GetAccessee().GetAccesseeDeviceId() == udid && + item.GetAccesser().GetAccesserDeviceId() == localUdid) { + peerToken = static_cast(item.GetAccessee().GetAccesseeTokenId()); + localToken = static_cast(item.GetAccesser().GetAccesserTokenId()); + key.tokenId = std::to_string(peerToken); + key.userId = item.GetAccessee().GetAccesseeUserId(); + } + tokenToKey.insert(std::pair(std::to_string(localToken), key)); + } +} + +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoByTokenId(DmServiceInfoUniqueKey key, + std::vector &serviceInfos) +{ + std::vector infos; + DistributedDeviceProfile::ServiceInfoUniqueKey keyIndex(key.deviceId, key.userId, key.tokenId, -1); + auto ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId(keyIndex, infos); + if (ret != 0) { + return ret; + } + DmServiceInfo serviceInfo; + for (auto info : infos) { + serviceInfo.serviceId = info.GetServiceId(); + serviceInfo.serviceType = info.GetServiceType(); + serviceInfo.serviceName = info.GetServiceName(); + serviceInfo.serviceDisplayName = info.GetServiceDisplayName(); + serviceInfo.customData = info.GetCustomData(); + serviceInfo.customDataLen = info.GetCustomDataLen(); + serviceInfo.bundleName = info.GetBundleName(); + serviceInfo.moduleName = info.GetModuleName(); + serviceInfo.abilityName = info.GetAbilityName(); + serviceInfos.push_back(serviceInfo); + } + return DM_OK; +} + DM_EXPORT int32_t DeviceProfileConnector::SubscribeDeviceProfileInited( sptr dpInitedCallback) { diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index 67c3a32ae..d624a2675 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -30,6 +30,80 @@ namespace OHOS { namespace DistributedHardware { + +typedef struct DmServiceInfo { + int64_t serviceId = 0; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; + uint32_t customDataLen = 0; + std::string bundleName; + std::string moduleName; + std::string abilityName; + //std::string extraInfo; +} DmServiceInfo; + +typedef struct ServiceRegInfo { + DmServiceInfo serviceInfo; + int32_t authBoxType = 0; + int32_t authType = 0; + int32_t pinExchangeType = 0; + std::string pinCode; + std::string description; + std::string serviceDiscoveryScope; +} ServiceRegInfo; + +typedef struct DmServiceInfoUniqueKey { + std::string deviceId; + int32_t userId = 0; + std::string tokenId; + int64_t serviceId = 0; +} DmServiceInfoUniqueKey; + +typedef struct DmServiceInfoProfile { + ServiceRegInfo serviceRegInfo; + std::string deviceId; + int32_t userId = 0; + std::string networkId; + std::string tokenId; + std::string extraInfo; +} DmServiceInfoProfile; + +typedef struct ServiceBindLocalInfo { + std::string localUdid; + int32_t userId = 0; + uint32_t tokenId = 0; +} ServiceBindLocalInfo; + +typedef struct DmServiceAddress{ + std::string bleMac; + uint32_t actionId = 0; +} DmServiceAddress; + +typedef struct DmServiceInfoDeviceProfile{ + std::string deviceId; + int32_t userId = 0; + std::string networkId; + std::string tokenId; + int64_t serviceId = 0; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; + int32_t customDataLen = 0; + std::string bundleName; + std::string moduleName; + std::string abilityName; + int32_t authBoxType = 0; + int32_t authType = 0; + int32_t pinExchangeType = 0; + std::string pinCode; + std::string description; + std::string serviceDicoveryScope; + std::string extraInfo; +} DmServiceInfoDeviceProfile; + /** * @brief Device manager event notify. */ @@ -192,6 +266,9 @@ typedef struct DmDeviceInfo { * include json keys: "CONN_ADDR_TYPE", "BR_MAC_", "BLE_MAC", "WIFI_IP", "WIFI_PORT", "CUSTOM_DATA" */ std::string extraData; + + //std::vector serviceInfos; + std::map> tokenToServiceInfos; } DmDeviceInfo; /** @@ -292,11 +369,17 @@ typedef struct PeerTargetId { * wlan ip port. */ uint16_t wifiPort = 0; + /** + * service id. + */ + int64_t serviceId = 0; + bool operator==(const PeerTargetId &other) const { return (deviceId == other.deviceId) && (brMac == other.brMac) && - (bleMac == other.bleMac) && (wifiIp == other.wifiIp) && (wifiPort == other.wifiPort); + (bleMac == other.bleMac) && (wifiIp == other.wifiIp) && (wifiPort == other.wifiPort) && + (serviceId == other.serviceId); } bool operator<(const PeerTargetId &other) const @@ -306,7 +389,9 @@ typedef struct PeerTargetId { (deviceId == other.deviceId && brMac == other.brMac && bleMac < other.bleMac) || (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp < other.wifiIp) || (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp == other.wifiIp && - wifiPort < other.wifiPort); + wifiPort < other.wifiPort) || + (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp == other.wifiIp && + wifiPort == other.wifiPort && serviceId < other.serviceId); } } PeerTargetId; diff --git a/services/implementation/include/authentication_v2/dm_auth_context.h b/services/implementation/include/authentication_v2/dm_auth_context.h index f4308c856..4d9d52f1c 100644 --- a/services/implementation/include/authentication_v2/dm_auth_context.h +++ b/services/implementation/include/authentication_v2/dm_auth_context.h @@ -182,6 +182,7 @@ struct DmAccess { std::string pkgName; // store pkgName set by client std::string language; int64_t serviceId; // Reserved field, to be used in HM 6.0 + std::vector serviceInfos; std::string accesserHapSignature; int32_t bindLevel; int32_t lnnBindType; diff --git a/services/implementation/include/authentication_v2/dm_auth_message_processor.h b/services/implementation/include/authentication_v2/dm_auth_message_processor.h index 98144d108..d5972c021 100644 --- a/services/implementation/include/authentication_v2/dm_auth_message_processor.h +++ b/services/implementation/include/authentication_v2/dm_auth_message_processor.h @@ -236,6 +236,9 @@ private: // Parse the authentication start message int32_t ParseAuthStartMessage(const JsonObject &jsonObject, std::shared_ptr context); + void ParseSrcServiceInfoToContext(const JsonObject &serviceJson, std::shared_ptr context); + void ParseSinkServiceInfoToContext(const JsonObject &serviceJson, std::shared_ptr context); + // Parse the 80 message int32_t ParseNegotiateMessage(const JsonObject &jsonObject, std::shared_ptr context); int32_t ParseProxyNegotiateMessage(const JsonObject &jsonObject, std::shared_ptr context); diff --git a/services/implementation/include/authentication_v2/dm_auth_state.h b/services/implementation/include/authentication_v2/dm_auth_state.h index cd7de24cb..df567bbf9 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state.h +++ b/services/implementation/include/authentication_v2/dm_auth_state.h @@ -176,6 +176,11 @@ public: static void DeleteAclAndSk(std::shared_ptr context, const DistributedDeviceProfile::AccessControlProfile &profile); void GetPeerDeviceId(std::shared_ptr context, std::string &peerDeviceId); + + int32_t GetSrcServiceInfoParam(std::vector &serviceInfoProfiles, + std::shared_ptr context); + int32_t GetSinkServiceInfoParam(std::vector &serviceInfoProfiles, + std::shared_ptr context); protected: bool NeedReqUserConfirm(std::shared_ptr context); bool NeedAgreeAcl(std::shared_ptr context); diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_credential.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_credential.cpp index 5cd699d25..b748fda7d 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_credential.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_credential.cpp @@ -558,6 +558,12 @@ int32_t AuthSrcCredentialExchangeState::Action(std::shared_ptr co return ret; } + std::vector serviceInfoProfiles; + ret = GetSrcServiceInfoParam(serviceInfoProfiles, context); + if (ret != DM_OK) { + LOGE("AuthManager::GetAuthParam GetSrcServiceInfoParam failed."); + } + std::string message = context->authMessageProcessor->CreateMessage(MSG_TYPE_REQ_CREDENTIAL_EXCHANGE, context); LOGI("AuthSrcCredentialExchangeState::Action() leave."); return context->softbusConnector->GetSoftbusSession()->SendData(context->sessionId, message); @@ -580,6 +586,13 @@ int32_t AuthSinkCredentialExchangeState::Action(std::shared_ptr c return ret; } + // Generate service information + std::vector serviceInfoProfiles; + ret = GetSinkServiceInfoParam(serviceInfoProfiles, context); + if (ret != DM_OK) { + LOGE("AuthSinkNegotiateStateMachine::GetAuthParam GetSinkServiceInfoParam failed."); + } + // First authentication lnn cred if (context->accessee.isGenerateLnnCredential && context->accessee.bindLevel != USER) { // Generate credentials and public key diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index 8eb382827..84b9fe201 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -76,6 +76,21 @@ const char* TAG_TOKEN_ID = "tokenId"; const char* TAG_NETWORKID_ID = "networkId"; const char* TAG_ISSUER = "issuer"; +//service info related +const char* TAG_SERVICE_INFOS = "serviceInfos"; +const char* TAG_SERVICE_ID = "serviceId"; +const char* TAG_SERVICE_TYPE = "serviceType"; +const char* TAG_SERVICE_NAME = "serviceName"; +const char* TAG_SERVICE_DISPLAY_NAME = "serviceDisplayName"; +const char* TAG_CUSTOM_DATA = "customData"; +const char* TAG_CUSTOM_DATA_LEN = "customDataLen"; +//const char* TAG_BUNDLE_NAME = "bundleName"; +const char* TAG_MODULE_NAME = "moduleName"; +const char* TAG_ABILIY_NAME = "abilityName"; +const char* TAG_DESCRIPTION = "description"; +const char* TAG_SERVICE_DICCOVERY_SCOPE = "serviceDiscoveryScope"; +const char* TAG_EXTRA_INFO = "extraInfo"; + const char* TAG_DEVICE_VERSION = "deviceVersion"; const char* TAG_DEVICE_NAME = "deviceName"; const char* TAG_DEVICE_ID_HASH = "deviceIdHash"; @@ -89,7 +104,6 @@ const char* TAG_STATE = "state"; const char* TAG_REASON = "reason"; const char* TAG_PEER_USER_ID = "peerUserId"; const char* TAG_PEER_DISPLAY_ID = "peerDisplayId"; -const char* TAG_EXTRA_INFO = "extraInfo"; const char* TAG_ACL_TYPE_LIST = "aclTypeList"; const char* TAG_CERT_TYPE_LIST = "certTypeList"; @@ -667,11 +681,15 @@ int32_t DmAuthMessageProcessor::ParseMessageReqCredExchange(const JsonObject &js LOGE("DmAuthMessageProcessor::ParseMessageReqCredExchange, MSG_TYPE_REQ_CREDENTIAL_EXCHANGE message error."); return ERR_DM_FAILED; } + ParseSrcServiceInfoToContext(jsonData, context); + context->accesser.transmitPublicKey = jsonData[TAG_TRANSMIT_PUBLIC_KEY].Get(); context->accesser.deviceId = jsonData[TAG_DEVICE_ID].Get(); context->accesser.userId = jsonData[TAG_PEER_USER_SPACE_ID].Get(); context->accesser.tokenId = jsonData[TAG_TOKEN_ID].Get(); ParseProxyCredExchangeToSync(context, jsonData); + context->accesser.networkId = jsonData[TAG_NETWORKID_ID].Get(); + //这里需要put DP数据库 context->authStateMachine->TransitionTo(std::make_shared()); return DM_OK; } @@ -714,11 +732,16 @@ int32_t DmAuthMessageProcessor::ParseMessageRspCredExchange(const JsonObject &js "message error."); return ERR_DM_FAILED; } + + ParseSinkServiceInfoToContext(jsonData, context); + context->accessee.transmitPublicKey = jsonData[TAG_TRANSMIT_PUBLIC_KEY].Get(); context->accessee.deviceId = jsonData[TAG_DEVICE_ID].Get(); context->accessee.userId = jsonData[TAG_PEER_USER_SPACE_ID].Get(); context->accessee.tokenId = jsonData[TAG_TOKEN_ID].Get(); ParseProxyCredExchangeToSync(context, jsonData); + context->accessee.networkId = jsonData[TAG_NETWORKID_ID].Get(); + //这里需要 put DP数据库 context->authStateMachine->TransitionTo(std::make_shared()); return DM_OK; } @@ -982,6 +1005,25 @@ int32_t DmAuthMessageProcessor::CreateMessageReqCredExchange(std::shared_ptraccesser.userId; jsonData[TAG_TOKEN_ID] = context->accesser.tokenId; CreateProxyCredExchangeMessage(context, jsonData); + jsonData[TAG_NETWORKID_ID] = context->accesser.networkId; + + //create service infos + JsonObject serviceInfosJson(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + for (auto item : context->accesser.serviceInfos) { + JsonObject childServiceJson(JsonCreateType::JSON_CREATE_TYPE_OBJECT); + childServiceJson[TAG_SERVICE_ID] = item.serviceId; + childServiceJson[TAG_SERVICE_TYPE] = item.serviceType; + childServiceJson[TAG_SERVICE_NAME] = item.serviceName; + childServiceJson[TAG_SERVICE_DISPLAY_NAME] = item.serviceDisplayName; + childServiceJson[TAG_CUSTOM_DATA] = item.customData; + childServiceJson[TAG_CUSTOM_DATA_LEN] = item.customDataLen; + childServiceJson[TAG_BUNDLE_NAME] = item.bundleName; + childServiceJson[TAG_MODULE_NAME] = item.moduleName; + childServiceJson[TAG_ABILIY_NAME] = item.abilityName; + serviceInfosJson.PushBack(childServiceJson); + } + jsonData[TAG_SERVICE_INFOS] = serviceInfosJson.Dump(); + std::string plainText = jsonData.Dump(); std::string cipherText; int32_t ret = cryptoMgr_->EncryptMessage(plainText, cipherText); @@ -1007,6 +1049,25 @@ int32_t DmAuthMessageProcessor::CreateMessageRspCredExchange(std::shared_ptraccessee.userId; jsonData[TAG_TOKEN_ID] = context->accessee.tokenId; CreateProxyCredExchangeMessage(context, jsonData); + jsonData[TAG_NETWORKID_ID] = context->accessee.networkId; + + //create service infos + JsonObject serviceInfosJson(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + for (auto item : context->accessee.serviceInfos) { + JsonObject childServiceJson(JsonCreateType::JSON_CREATE_TYPE_OBJECT); + childServiceJson[TAG_SERVICE_ID] = item.serviceId; + childServiceJson[TAG_SERVICE_TYPE] = item.serviceType; + childServiceJson[TAG_SERVICE_NAME] = item.serviceName; + childServiceJson[TAG_SERVICE_DISPLAY_NAME] = item.serviceDisplayName; + childServiceJson[TAG_CUSTOM_DATA] = item.customData; + childServiceJson[TAG_CUSTOM_DATA_LEN] = item.customDataLen; + childServiceJson[TAG_BUNDLE_NAME] = item.bundleName; + childServiceJson[TAG_MODULE_NAME] = item.moduleName; + childServiceJson[TAG_ABILIY_NAME] = item.abilityName; + serviceInfosJson.PushBack(childServiceJson); + } + jsonObject[TAG_SERVICE_INFOS] = serviceInfosJson.Dump(); + std::string plainText = jsonData.Dump(); std::string cipherText; LOGI("plainText=%{public}s", GetAnonyJsonString(plainText).c_str()); @@ -1382,6 +1443,97 @@ int32_t DmAuthMessageProcessor::CheckLogicalSessionId(const JsonObject &jsonObje return DM_OK; } +void DmAuthMessageProcessor::ParseSrcServiceInfoToContext(const JsonObject &serviceJson, + std::shared_ptr context) +{ + if (context == nullptr || !serviceJson.Contains(TAG_SERVICE_INFOS)) { + LOGE("DmAuthMessageProcessor::ParseSrcServiceInfoToContext context is null or TAG_SERVICE_INFOS not found"); + return; + } + + if (serviceJson.Contains(TAG_SERVICE_INFOS) && serviceJson[TAG_SERVICE_INFOS].IsString()) { + JsonObject serviceinfos(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + serviceinfos.Parse(serviceJson[TAG_SERVICE_INFOS].Get()); + std::vector serviceVec = serviceinfos.Items(); + for (auto item : serviceVec) { + DmServiceInfo serviceInfo; + if (item.Contains(TAG_SERVICE_ID) && item[TAG_SERVICE_ID].IsString()) { + serviceInfo.serviceId = item[TAG_SERVICE_ID].Get(); + } + if (item.Contains(TAG_SERVICE_TYPE) && item[TAG_SERVICE_TYPE].IsString()) { + serviceInfo.serviceType = item[TAG_SERVICE_TYPE].Get(); + } + if (item.Contains(TAG_SERVICE_NAME) && item[TAG_SERVICE_NAME].IsString()) { + serviceInfo.serviceName = item[TAG_SERVICE_NAME].Get(); + } + if (item.Contains(TAG_SERVICE_DISPLAY_NAME) && item[TAG_SERVICE_DISPLAY_NAME].IsString()) { + serviceInfo.serviceDisplayName = item[TAG_SERVICE_DISPLAY_NAME].Get(); + } + if (item.Contains(TAG_CUSTOM_DATA) && item[TAG_CUSTOM_DATA].IsString()) { + serviceInfo.customData = item[TAG_CUSTOM_DATA].Get(); + } + if (item.Contains(TAG_CUSTOM_DATA_LEN) && item[TAG_CUSTOM_DATA_LEN].IsNumberInteger()) { + serviceInfo.customDataLen = item[TAG_CUSTOM_DATA_LEN].Get(); + } + if (item.Contains(TAG_BUNDLE_NAME) && item[TAG_BUNDLE_NAME].IsString()) { + serviceInfo.bundleName = item[TAG_BUNDLE_NAME].Get(); + } + if (item.Contains(TAG_MODULE_NAME) && item[TAG_MODULE_NAME].IsString()) { + serviceInfo.moduleName = item[TAG_MODULE_NAME].Get(); + } + if (item.Contains(TAG_ABILIY_NAME) && item[TAG_ABILIY_NAME].IsString()) { + serviceInfo.abilityName = item[TAG_ABILIY_NAME].Get(); + } + context->accesser.serviceInfos.push_back(serviceInfo); + } + } +} + +void DmAuthMessageProcessor::ParseSinkServiceInfoToContext(const JsonObject &serviceJson, + std::shared_ptr context) +{ + if ((context == nullptr) || (!serviceJson.Contains(TAG_SERVICE_INFOS))) { + LOGE("DmAuthMessageProcessor::ParseSinkServiceInfoToContext context is null or TAG_SERVICE_INFOS not found"); + return; + } + if (serviceJson[TAG_SERVICE_INFOS].IsString()) { + JsonObject serviceinfos(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + serviceinfos.Parse(serviceJson[TAG_SERVICE_INFOS].Get()); + std::vector serviceVec = serviceinfos.Items(); + for (auto item : serviceVec) { + DmServiceInfo serviceInfo; + if (item.Contains(TAG_SERVICE_ID) && item[TAG_SERVICE_ID].IsString()) { + serviceInfo.serviceId = item[TAG_SERVICE_ID].Get(); + } + if (item.Contains(TAG_SERVICE_TYPE) && item[TAG_SERVICE_TYPE].IsString()) { + serviceInfo.serviceType = item[TAG_SERVICE_TYPE].Get(); + } + if (item.Contains(TAG_SERVICE_NAME) && item[TAG_SERVICE_NAME].IsString()) { + serviceInfo.serviceName = item[TAG_SERVICE_NAME].Get(); + } + if (item.Contains(TAG_SERVICE_DISPLAY_NAME) && item[TAG_SERVICE_DISPLAY_NAME].IsString()) { + serviceInfo.serviceDisplayName = item[TAG_SERVICE_DISPLAY_NAME].Get(); + } + if (item.Contains(TAG_CUSTOM_DATA) && item[TAG_CUSTOM_DATA].IsString()) { + serviceInfo.customData = item[TAG_CUSTOM_DATA].Get(); + } + if (item.Contains(TAG_CUSTOM_DATA_LEN) && item[TAG_CUSTOM_DATA_LEN].IsNumberInteger()) { + serviceInfo.customDataLen = item[TAG_CUSTOM_DATA_LEN].Get(); + } + if (item.Contains(TAG_BUNDLE_NAME) && item[TAG_BUNDLE_NAME].IsString()) { + serviceInfo.bundleName = item[TAG_BUNDLE_NAME].Get(); + } + if (item.Contains(TAG_MODULE_NAME) && item[TAG_MODULE_NAME].IsString()) { + serviceInfo.moduleName = item[TAG_MODULE_NAME].Get(); + } + if (item.Contains(TAG_ABILIY_NAME) && item[TAG_ABILIY_NAME].IsString()) { + serviceInfo.abilityName = item[TAG_ABILIY_NAME].Get(); + } + context->accessee.serviceInfos.push_back(serviceInfo); + } + } +} + int32_t DmAuthMessageProcessor::ParseNegotiateMessage( const JsonObject &jsonObject, std::shared_ptr context) { diff --git a/services/implementation/src/authentication_v2/dm_auth_state.cpp b/services/implementation/src/authentication_v2/dm_auth_state.cpp index df52e3b14..8f2075b56 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state.cpp @@ -580,6 +580,54 @@ void DmAuthState::DeleteAcl(std::shared_ptr context, DeviceProfileConnector::GetInstance().DeleteAccessControlById(profile.GetAccessControlId()); } +int32_t DmAuthState::GetSrcServiceInfoParam(std::vector &serviceInfoProfiles, std::shared_ptr context) +{ + if (DeviceProfileConnector::GetInstance().GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + if (serviceInfoProfiles.empty()) { + LOGE("GetSrcServiceInfoParam failed, serviceInfoProfiles is empty."); + return ERR_DM_FAILED; + } + std::string tokenId; + for (auto const &item : serviceInfoProfiles) { + if (item.serviceRegInfo.serviceInfo.serviceId == context->peerTargetId.serviceId) { + tokenId = item.tokenId; + break; + } + } + for (auto const &item : serviceInfoProfiles) { + if (item.tokenId == tokenId) { + context->accesser.serviceInfos.push_back(item.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +int32_t DmAuthState::GetSinkServiceInfoParam(std::vector &serviceInfoProfiles, std::shared_ptr context) +{ + if (DeviceProfileConnector::GetInstance().GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + if (serviceInfoProfiles.empty()) { + LOGE("GetSinkServiceInfoParam failed, serviceInfoProfiles is empty."); + return ERR_DM_FAILED; + } + std::string tokenId; + for (auto const &item : serviceInfoProfiles) { + if (item.serviceRegInfo.serviceInfo.serviceId == context->peerTargetId.serviceId) { + tokenId = item.tokenId; + break; + } + } + for (auto const &item : serviceInfoProfiles) { + if (item.tokenId == tokenId) { + context->accessee.serviceInfos.push_back(item.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + void DmAuthState::SetProcessInfo(std::shared_ptr context) { CHECK_NULL_VOID(context); diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 069a2ad13..11d8b10c5 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -190,11 +190,12 @@ public: int32_t StopAdvertising(const std::string &pkgName, const std::map &advertiseParam); - int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &bindParam); - - int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &unbindParam); + int32_t BindTarget(const std::string &pkgName, PeerTargetId &targetId, + std::map &bindParam); + int32_t UnbindTarget(const std::string &pkgName, PeerTargetId &targetId, + std::map &unbindParam); + + void ServiceInfoBindParmamHandle(PeerTargetId &targetId, std::map &bindParam); int32_t DpAclAdd(const std::string &udid); @@ -275,6 +276,22 @@ public: bool CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee); bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee); bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee); + + int32_t GetOnlineServiceList(const std::map &filterOptions, + std::vector &serviceList); + int32_t GetTrustedServiceList(const std::map &filterOptions, + std::vector &serviceList); + int32_t GetServiceInfoByServiceId(const int64_t &serviceId, DmServiceInfo &dmServiceInfo); + + int32_t GetCallerServiceInfo(std::vector &serviceInfos); + + int32_t GetServiceInfosByTokenId(const int64_t &tokenId, + std::vector &serviceInfos); + +private: + int32_t GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo); + bool CheckIsDeviceOnline(const std::vector &onlineDeviceList, const std::string &peerUdid); + int32_t GetServiceInfoProfileByServiceId(const int64_t &serviceId, DmServiceInfoProfile &dmServiceInfoProfile); private: bool IsDMServiceImplReady(); bool IsDMImplSoLoaded(); diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index cc50ac399..405f01572 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -132,6 +132,8 @@ public: std::vector &deviceList); int32_t GetUdidFromDp(const std::string &udidHash, std::string &udid); static void GetActionId(const std::string &deviceId, int32_t &actionId); + + int32_t GetPeerServiceAddress(int64_t serviceId, DmServiceAddress &addr); private: static int32_t FillDeviceInfo(const DeviceInfo &device, DmDeviceInfo &dmDevice); static void ParseConnAddrInfo(const ConnectionAddr *addrInfo, JsonObject &jsonObj); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index f68075c02..2f6ca704e 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1833,8 +1833,8 @@ int32_t DeviceManagerService::StopAdvertising(const std::string &pkgName, return advertiseMgr_->StopAdvertising(pkgName, publishId); } -int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &bindParam) +int32_t DeviceManagerService::BindTarget(const std::string &pkgName, PeerTargetId &targetId, + std::map &bindParam) { LOGI("DeviceManagerService::BindTarget"); if (!PermissionManager::GetInstance().CheckNewPermission()) { @@ -1846,6 +1846,7 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } + ServiceInfoBindParmamHandle(targetId, bindParam); if (bindParam.find(PARAM_KEY_META_TYPE) == bindParam.end()) { if (!IsDMServiceImplReady()) { LOGE("BindTarget failed, DMServiceImpl instance not init or init failed."); @@ -1878,8 +1879,8 @@ int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerT return dmServiceImplExtResident_->BindTargetExt(pkgName, targetId, bindParam); } -int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &unbindParam) +int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, PeerTargetId &targetId, + std::map &unbindParam) { if (!PermissionManager::GetInstance().CheckNewPermission()) { LOGE("The caller does not have permission to call"); @@ -1902,6 +1903,37 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee LOGE("The caller does not have permission to call"); return ERR_DM_NO_PERMISSION; } + + if (targetId.serviceId != 0) { + unbindParam[IS_BIND_SERVICE] = "true"; + unbindParam[SERVICE_ID] = std::to_string(targetId.serviceId); + DmServiceAddress addr; + softbusListener_->GetPeerServiceAddress(targetId.serviceId, addr); + targetId.bleMac = addr.bleMac; + } + std::vector serviceInfoProfiles; + std::string tokenId; + if (DeviceProfileConnector::GetInstance().GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + int32_t i; + for (i = 0; i < serviceInfoProfiles.size(); i++) { + if (serviceInfoProfiles[i].serviceRegInfo.serviceInfo.serviceId == targetId.serviceId) { + tokenId = serviceInfoProfiles[i].tokenId; + break; + } + } + DmServiceInfoUniqueKey serviceInfoUniqueKey; + serviceInfoUniqueKey.tokenId = tokenId; + for (i = 0; i < serviceInfoProfiles.size(); i++) { + if (serviceInfoProfiles[i].tokenId == tokenId) { + serviceInfoUniqueKey.deviceId = serviceInfoProfiles[i].deviceId; + serviceInfoUniqueKey.serviceId = serviceInfoProfiles[i].serviceRegInfo.serviceInfo.serviceId; + serviceInfoUniqueKey.userId = serviceInfoProfiles[i].userId; + //UnRegisterServiceInfo(serviceInfoUniqueKey); + } + } + std::string realDeviceId = targetId.deviceId; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) std::string udidHashTemp = ""; @@ -1918,6 +1950,29 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee return dmServiceImplExtResident_->UnbindTargetExt(pkgName, targetId, unbindParamWithUdid); } +void DeviceManagerService::ServiceInfoBindParmamHandle(PeerTargetId &targetId, + std::map &bindParam) +{ + if (targetId.serviceId != 0) { + DmServiceAddress addr; + if (softbusListener_->GetPeerServiceAddress(targetId.serviceId, addr) == DM_OK) { + bindParam[SERVICE_ID] = std::to_string(targetId.serviceId); + bindParam[IS_BIND_SERVICE] = "ture"; + targetId.bleMac = addr.bleMac; + } + else { + DmServiceInfoProfile serviceInfoProfile; + if (GetServiceInfoProfileByServiceId(targetId.serviceId, serviceInfoProfile) == DM_OK) { + std::string udid = ""; + SoftbusListener::GetUdidByNetworkId(serviceInfoProfile.networkId.c_str(), udid); + } + } + } + else { + LOGE("DeviceManagerService::BindTarget serviceId is 0, not a service bind."); + } +} + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) bool DeviceManagerService::InitDPLocalServiceInfo(const DMLocalServiceInfo &serviceInfo, DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceInfo) @@ -4638,5 +4693,220 @@ void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &c MultipleUserConnector::GetCurrentDMAccountInfo()); } #endif +int32_t DeviceManagerService::GetOnlineServiceList(const std::map &filterOptions, + std::vector &serviceList) +{ + LOGI("DeviceManagerService::GetOnlineServiceList Start"); + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + std::vector allTrustDeviceProfiles; + //std::vector allTrustDeviceProfiles = + // DeviceProfileConnector::GetInstance().GetAccessControProfileByUserId(localInfo.userId); + std::vector onlineDeviceList; + CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL); + ret = softbusListener_->GetTrustedDeviceList(onlineDeviceList); + if (ret != DM_OK) { + LOGE("GetTrustedDeviceList failed"); + return ret; + } + for (auto &profile : allTrustDeviceProfiles) { + DistributedDeviceProfile::Accesser accesser = profile.GetAccesser(); + DistributedDeviceProfile::Accessee accessee = profile.GetAccessee(); + std::string peerUdid = ""; + std::string peerTokenId = ""; + int32_t peerUserId = -1; + if (accesser.GetAccesserDeviceId() == localInfo.localUdid && + accesser.GetAccesserTokenId() == localInfo.tokenId) { + peerUdid = accessee.GetAccesseeDeviceId(); + peerTokenId = accessee.GetAccesseeTokenId(); + peerUserId = accessee.GetAccesseeUserId(); + } else if (accessee.GetAccesseeDeviceId() == localInfo.localUdid && accessee.GetAccesseeTokenId()== localInfo.tokenId) { + peerUdid = accesser.GetAccesserDeviceId(); + peerTokenId = accesser.GetAccesserTokenId(); + peerUserId = accesser.GetAccesserUserId(); + } + if (!CheckIsDeviceOnline(onlineDeviceList, peerUdid)) { + LOGE("DeviceManagerService::GetOnlineServiceList Device Is Offline"); + continue; + } + DmServiceInfoUniqueKey key; + key.tokenId = peerTokenId; + key.deviceId = peerUdid; + key.userId = peerUserId; + std::vector serviceInfoProfiles; + //DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, serviceInfoProfiles); + for (auto serviceInfoProfile : serviceInfoProfiles) { + serviceList.emplace_back(serviceInfoProfile.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +int32_t DeviceManagerService::GetTrustedServiceList(const std::map &filterOptions, + std::vector &serviceList) +{ + LOGI("DeviceManagerService::GetTrustedServiceList Start"); + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + std::vector allTrustDeviceProfiles = + DeviceProfileConnector::GetInstance().GetAccessControlProfileByUserId(localInfo.userId); + + for (auto &profile : allTrustDeviceProfiles) { + DistributedDeviceProfile::Accesser accesser = profile.GetAccesser(); + DistributedDeviceProfile::Accessee accessee = profile.GetAccessee(); + std::string peerUdid = ""; + std::string peerTokenId = ""; + int32_t peerUserId = -1; + if (accesser.GetAccesserDeviceId() == localInfo.localUdid && + accesser.GetAccesserTokenId() == localInfo.tokenId) { + peerUdid = accessee.GetAccesseeDeviceId(); + peerTokenId = accessee.GetAccesseeTokenId(); + peerUserId = accessee.GetAccesseeUserId(); + } else if (accessee.GetAccesseeDeviceId() == localInfo.localUdid && + accessee.GetAccesseeTokenId() == localInfo.tokenId) { + peerUdid = accesser.GetAccesserDeviceId(); + peerTokenId = accesser.GetAccesserTokenId(); + peerUserId = accesser.GetAccesserUserId(); + } + DmServiceInfoUniqueKey key; + key.tokenId = peerTokenId; + key.deviceId = peerUdid; + key.userId = peerUserId; + std::vector serviceInfoProfiles; + //DeviceProfileConnector::Getinstance().GetServiceInfoProfileListByTokenId(key, serviceInfoProfiles); + for (auto serviceInfoProfile : serviceInfoProfiles) { + serviceList.emplace_back(serviceInfoProfile.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +int32_t DeviceManagerService::GetServiceInfoByServiceId(const int64_t &serviceId, DmServiceInfo &dmServiceInfo) +{ + LOGI("DeviceManagerService::GetServiceInfoByServiceId Start"); + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + DmServiceInfoUniqueKey serviceInfoUniqueKey; + serviceInfoUniqueKey.serviceId = serviceId; + serviceInfoUniqueKey.tokenId = std::to_string(localInfo.tokenId); + serviceInfoUniqueKey.userId = localInfo.userId; + serviceInfoUniqueKey.deviceId = localInfo.localUdid; + // LOGI("SQW GetServiceInfoByServiceId key serviceid=%{public}ld, tokenid=%{public}s, userid= + // %{public}d, deviceid=%{public}s", + // serviceInfoUniqueKey.serviceId, serviceInfoUniqueKey.tokenId.c_str(), serviceInfoUniqu + // eKey.userId, + // serviceInfoUniqueKey.deviceId.c_str()); + DmServiceInfoProfile dmServiceInfoProfile; + //ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey(serviceInfoUniqueKey, + // dmServiceInfoProfile); + // LOGI("SQW GetServiceInfoByServiceId ret=%{public}d, ser"viceId=%{public}ld, service name=%{ + // public}s", + // ret, dmServiceInfoProfile.serviceRegInfo.servicelInfo.serviceId, dmServiceInfoProfile. + // erviceRegInfo.serviceInfo.serviceName.c_str()); + if (ret != DM_OK) { + return ret; + } + dmServiceInfo = dmServiceInfoProfile.serviceRegInfo.serviceInfo; + return DM_OK; +} + +int32_t DeviceManagerService::GetServiceInfoProfileByServiceId(const int64_t &serviceId, + DmServiceInfoProfile &dmServiceInfoProfile) +{ + LOGI("DeviceManagerService::GetServiceInfoProfileByServiceId Start"); + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + DmServiceInfoUniqueKey serviceInfoUniqueKey; + serviceInfoUniqueKey.serviceId = serviceId; + serviceInfoUniqueKey.tokenId = std::to_string(localInfo.tokenId); + serviceInfoUniqueKey.userId = localInfo.userId; + serviceInfoUniqueKey.deviceId = localInfo.localUdid; + + //ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey(serviceInfoUniqueKey, + // dmServiceInfoProfile); + if (ret != DM_OK) { + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerService::GetCallerServiceInfo(std::vector &serviceInfos) +{ + LOGI("DeviceManagerService::GetCallerServiceInfo Start"); + std::vector serviceInfoProfiles; + + int64_t tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + + if (DeviceProfileConnector::GetInstance().GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + if (serviceInfoProfiles.empty()) { + LOGE("GetServiceInfosByTokenId failed, serviceInfoProfiles is empty."); + return ERR_DM_FAILED; + } + for (auto const &item : serviceInfoProfiles) { + if (item.tokenId == std::to_string(tokenId)) { + serviceInfos.push_back(item.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +int32_t DeviceManagerService::GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo) +{ + DmDeviceInfo localDeviceInfo; + int32_t ret = GetLocalDeviceInfo(localDeviceInfo); + if (ret != DM_OK) { + LOGE("GetLocalDeviceInfo failed"); + return ret; + } + localInfo.localUdid = localDeviceInfo.deviceId; + localInfo.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + MultipleUserConnector::GetCallerUserId(localInfo.userId); + + return DM_OK; +} + +int32_t DeviceManagerService::GetServiceInfosByTokenId(const int64_t &tokenId, + std::vector &serviceInfos) +{ + std::vector serviceInfoProfiles; + + if (DeviceProfileConnector::GetInstance().GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + if (serviceInfoProfiles.empty()) { + LOGE("GetServiceInfosByTokenId failed, serviceInfoProfiles is empty."); + return ERR_DM_FAILED; + } + for (auto const &item : serviceInfoProfiles) { + if (item.tokenId == std::to_string(tokenId)) { + serviceInfos.push_back(item.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +bool DeviceManagerService::CheckIsDeviceOnline(const std::vector &onlineDeviceList, const std::string &peerUdid) +{ + for (auto deviceInfo : onlineDeviceList) { + if (deviceInfo.deviceId == peerUdid) { + return true; + } + } + return false; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 2491cc636..ac4e89b33 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1521,5 +1521,19 @@ int32_t SoftbusListener::PutOstypeData(const std::string &peerUdid, int32_t osTy return KVAdapterManager::GetInstance().PutOstypeData(peerUdid, osTypeStr); } #endif +int32_t SoftbusListener::GetPeerServiceAddress(int64_t serviceId, DmServiceAddress &addr) +{ + std::string serviceIdStr = std::to_string(serviceId); + + // DistributedDeviceProfile::ServiceAddress addrinfo; + // int32_t ret = ::GetPeerServiceAddress(serviceIdStr.c_str(),&addrinfo); + // if(ret != DM_OK) { + // LOGE("[SOFTBUS]GetPeerServiceAddress failed,ret %{public}d.",ret); + // return ret; + // } + // addr.bleMac=addrinfo.ble.bleMac; + // addr.actionid = addrinfo.action.actionId; + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector_three.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector_three.cpp new file mode 100644 index 000000000..b303d01fc --- /dev/null +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector_three.cpp @@ -0,0 +1,431 @@ +/* + * 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 "UTTest_dm_deviceprofile_connector_three.h" + +#include "dm_constants.h" +#include "deviceprofile_connector.h" +#include +#include "dp_inited_callback_stub.h" +#include "dm_error_type.h" +#include "dm_crypto.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +void DeviceProfileConnectorThreeTest::SetUp() +{ +} + +void DeviceProfileConnectorThreeTest::TearDown() +{ +} + +void DeviceProfileConnectorThreeTest::SetUpTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = + distributedDeviceProfileClientMock_; + DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_; +} + +void DeviceProfileConnectorThreeTest::TearDownTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = nullptr; + distributedDeviceProfileClientMock_ = nullptr; + DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr; + deviceProfileConnectorMock_ = nullptr; +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByTokenId_001, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "deviceId"; + key.tokenId = "tokenId"; + key.userId = 1; + DistributedDeviceProfile::ServiceInfoProfile profile1; + profile1.SetServiceId(1001); + profile1.SetServiceName("TestService1"); + profile1.SetServiceType("Type1"); + profile1.SetDeviceId("deviceId"); + profile1.SetUserId(1); + + DistributedDeviceProfile::ServiceInfoProfile profile2; + profile2.SetServiceId(1002); + profile2.SetServiceName("TestService2"); + profile2.SetServiceType("Type2"); + profile2.SetDeviceId("deviceId"); + profile2.SetUserId(1); + + std::vector dpServiceInfos = { profile1, profile2 }; + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetServiceInfoProfileListByTokenId(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(dpServiceInfos), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceInfos.size(), dpServiceInfos.size()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByTokenId_002, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "deviceId"; + key.tokenId = "tokenId"; + key.userId = 1; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetServiceInfoProfileListByTokenId(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByTokenId_003, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "deviceId"; + key.tokenId = "tokenId"; + key.userId = 1; + + std::vector dpServiceInfos; + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetServiceInfoProfileListByTokenId(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(dpServiceInfos), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByDeviceId_001, testing::ext::TestSize.Level1) +{ + std::string deviceId = "testDeviceId"; + + DmServiceInfoProfile profile1; + profile1.deviceId = "testDeviceId"; + profile1.serviceRegInfo.serviceInfo.serviceId = 1001; + profile1.serviceRegInfo.serviceInfo.serviceName = "TestService1"; + + DmServiceInfoProfile profile2; + profile2.deviceId = "testDeviceId"; + profile2.serviceRegInfo.serviceInfo.serviceId = 1002; + profile2.serviceRegInfo.serviceInfo.serviceName = "TestService2"; + + std::vector serviceInfoProfiles = { profile1, profile2 }; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(_)) + .WillOnce(DoAll(SetArgReferee<0>(serviceInfoProfiles), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByDeviceId(deviceId, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceInfos.size(), 2); + // EXPECT_EQ(serviceInfos[0].serviceId, 1001); + // EXPECT_EQ(serviceInfos[0].serviceName, "TestService1"); + // EXPECT_EQ(serviceInfos[1].serviceId, 1002); + // EXPECT_EQ(serviceInfos[1].serviceName, "TestService2"); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByDeviceId_Failure_002, testing::ext::TestSize.Level1) +{ + std::string deviceId = "testDeviceId"; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByDeviceId(deviceId, serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByDeviceId_003, testing::ext::TestSize.Level1) +{ + std::string deviceId = "testDeviceId"; + std::vector serviceInfoProfiles; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(_)) + .WillOnce(DoAll(SetArgReferee<0>(serviceInfoProfiles), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByDeviceId(deviceId, serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoProfileListByDeviceId_004, testing::ext::TestSize.Level1) +{ + std::string deviceId = "nonMatchingDeviceId"; + + DmServiceInfoProfile profile1; + profile1.deviceId = "testDeviceId"; + profile1.serviceRegInfo.serviceInfo.serviceId = 1001; + profile1.serviceRegInfo.serviceInfo.serviceName = "TestService1"; + + std::vector serviceInfoProfiles = { profile1 }; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(_)) + .WillOnce(DoAll(SetArgReferee<0>(serviceInfoProfiles), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByDeviceId(deviceId, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, DeleteServiceInfoProfile_001, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "deviceId"; + key.userId = 1; + key.tokenId = "tokenId"; + key.serviceId = 12345; + DistributedDeviceProfile::ServiceInfoUniqueKey dpKey; + dpKey.SetDeviceId(key.deviceId); + dpKey.SetServiceId(key.serviceId); + dpKey.SetTokenId(key.tokenId); + dpKey.SetUserId(key.userId); + + EXPECT_CALL(*distributedDeviceProfileClientMock_, + DeleteServiceInfoProfile(testing::Truly([&dpKey](const DistributedDeviceProfile::ServiceInfoUniqueKey& key) { + return key.GetDeviceId() == dpKey.GetDeviceId() && + key.GetServiceId() == dpKey.GetServiceId() && + key.GetTokenId() == dpKey.GetTokenId() && + key.GetUserId() == dpKey.GetUserId(); + }))) + .WillOnce(Return(DM_OK)); + + int32_t result = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(key); + EXPECT_EQ(result, DM_OK); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, DeleteServiceInfoProfile_002, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "deviceId"; + key.userId = 1; + key.tokenId = "tokenId"; + key.serviceId = 12345; + DistributedDeviceProfile::ServiceInfoUniqueKey dpKey; + dpKey.SetDeviceId(key.deviceId); + dpKey.SetServiceId(key.serviceId); + dpKey.SetTokenId(key.tokenId); + dpKey.SetUserId(key.userId); + + EXPECT_CALL(*distributedDeviceProfileClientMock_, + DeleteServiceInfoProfile(testing::Truly([&dpKey](const DistributedDeviceProfile::ServiceInfoUniqueKey& key) { + return key.GetDeviceId() == dpKey.GetDeviceId() && + key.GetServiceId() == dpKey.GetServiceId() && + key.GetTokenId() == dpKey.GetTokenId() && + key.GetUserId() == dpKey.GetUserId(); + }))) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t result = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(key); + EXPECT_EQ(result, ERR_DM_FAILED); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetLocalTokenIdByDeviceIdAndUserId_001, testing::ext::TestSize.Level1) +{ + std::string localUdid = "localDeviceId"; + std::string udid = "remoteDeviceId"; + std::map tokenToKey; + + DistributedDeviceProfile::AccessControlProfile inactiveProfile; + inactiveProfile.SetStatus(INACTIVE); + std::vector profiles = {inactiveProfile}; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAclProfileByDeviceIdAndUserId(_, _)) + .WillOnce(Return(profiles)); + + DeviceProfileConnector::GetInstance().GetLocalTokenIdByDeviceIdAndUserId(localUdid, udid, tokenToKey); + EXPECT_TRUE(tokenToKey.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetLocalTokenIdByDeviceIdAndUserId_002, testing::ext::TestSize.Level1) +{ + std::string localUdid = "localDeviceId"; + std::string udid = "remoteDeviceId"; + std::map tokenToKey; + + DistributedDeviceProfile::AccessControlProfile matchingProfile; + matchingProfile.SetStatus(ACTIVE); + matchingProfile.GetAccessee().SetAccesseeDeviceId(localUdid); + matchingProfile.GetAccesser().SetAccesserDeviceId(udid); + matchingProfile.GetAccessee().SetAccesseeTokenId(123); + matchingProfile.GetAccesser().SetAccesserTokenId(456); + matchingProfile.GetAccesser().SetAccesserUserId(1); + std::vector profiles = {matchingProfile}; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAclProfileByDeviceIdAndUserId(_, _)) + .WillOnce(Return(profiles)); + + DeviceProfileConnector::GetInstance().GetLocalTokenIdByDeviceIdAndUserId(localUdid, udid, tokenToKey); + + EXPECT_EQ(tokenToKey.size(), 1); + // EXPECT_EQ(tokenToKey["123"].deviceId, udid); + // EXPECT_EQ(tokenToKey["123"].tokenId, "456"); + // EXPECT_EQ(tokenToKey["123"].userId, 1); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetLocalTokenIdByDeviceIdAndUserId_003, testing::ext::TestSize.Level1) +{ + std::string localUdid = "localDeviceId"; + std::string udid = "remoteDeviceId"; + std::map tokenToKey; + + DistributedDeviceProfile::AccessControlProfile reversedProfile; + reversedProfile.SetStatus(ACTIVE); + reversedProfile.GetAccessee().SetAccesseeDeviceId(udid); + reversedProfile.GetAccesser().SetAccesserDeviceId(localUdid); + reversedProfile.GetAccessee().SetAccesseeTokenId(789); + reversedProfile.GetAccesser().SetAccesserTokenId(101112); + reversedProfile.GetAccessee().SetAccesseeUserId(2); + std::vector profiles = {reversedProfile}; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAclProfileByDeviceIdAndUserId(_, _)) + .WillOnce(Return(profiles)); + DeviceProfileConnector::GetInstance().GetLocalTokenIdByDeviceIdAndUserId(localUdid, udid, tokenToKey); + + EXPECT_EQ(tokenToKey.size(), 1); + // EXPECT_EQ(tokenToKey["101112"].deviceId, udid); + // EXPECT_EQ(tokenToKey["101112"].tokenId, "789"); + // EXPECT_EQ(tokenToKey["101112"].userId, 2); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetLocalTokenIdByDeviceIdAndUserId_004, testing::ext::TestSize.Level1) +{ + std::string localUdid = "localDeviceId"; + std::string udid = "remoteDeviceId"; + std::map tokenToKey; + + DistributedDeviceProfile::AccessControlProfile profile1; + profile1.SetStatus(ACTIVE); + profile1.GetAccessee().SetAccesseeDeviceId(localUdid); + profile1.GetAccesser().SetAccesserDeviceId(udid); + profile1.GetAccessee().SetAccesseeTokenId(123); + profile1.GetAccesser().SetAccesserTokenId(456); + profile1.GetAccesser().SetAccesserUserId(1); + + DistributedDeviceProfile::AccessControlProfile profile2; + profile2.SetStatus(ACTIVE); + profile2.GetAccessee().SetAccesseeDeviceId(udid); + profile2.GetAccesser().SetAccesserDeviceId(localUdid); + profile2.GetAccessee().SetAccesseeTokenId(789); + profile2.GetAccesser().SetAccesserTokenId(101112); + profile2.GetAccessee().SetAccesseeUserId(2); + + std::vector profiles = {profile1, profile2}; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAclProfileByDeviceIdAndUserId(_, _)) + .WillOnce(Return(profiles)); + + DeviceProfileConnector::GetInstance().GetLocalTokenIdByDeviceIdAndUserId(localUdid, udid, tokenToKey); + EXPECT_EQ(tokenToKey.size(), 2); + // EXPECT_EQ(tokenToKey["123"].deviceId, udid); + // EXPECT_EQ(tokenToKey["123"].tokenId, "456"); + // EXPECT_EQ(tokenToKey["123"].userId, 1); + + // EXPECT_EQ(tokenToKey["101112"].deviceId, udid); + // EXPECT_EQ(tokenToKey["101112"].tokenId, "789"); + // EXPECT_EQ(tokenToKey["101112"].userId, 2); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoByTokenId_001, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "testDeviceId"; + key.userId = 1; + key.tokenId = "testTokenId"; + + std::vector dpServiceInfos; + DistributedDeviceProfile::ServiceInfoProfile profile; + profile.SetServiceId(1001); + profile.SetServiceType("Type1"); + profile.SetServiceName("Service1"); + profile.SetServiceDisplayName("DisplayName1"); + profile.SetCustomData("CustomData1"); + profile.SetCustomDataLen(10); + profile.SetBundleName("Bundle1"); + profile.SetModuleName("Module1"); + profile.SetAbilityName("Ability1"); + dpServiceInfos.push_back(profile); + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetServiceInfoProfileListByTokenId(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(dpServiceInfos), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(key, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceInfos.size(), 1); + // EXPECT_EQ(serviceInfos[0].serviceId, 1001); + // EXPECT_EQ(serviceInfos[0].serviceType, "Type1"); + // EXPECT_EQ(serviceInfos[0].serviceName, "Service1"); + // EXPECT_EQ(serviceInfos[0].serviceDisplayName, "DisplayName1"); + // EXPECT_EQ(serviceInfos[0].customData, "CustomData1"); + // EXPECT_EQ(serviceInfos[0].customDataLen, 10); + // EXPECT_EQ(serviceInfos[0].bundleName, "Bundle1"); + // EXPECT_EQ(serviceInfos[0].moduleName, "Module1"); + // EXPECT_EQ(serviceInfos[0].abilityName, "Ability1"); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoByTokenId_002, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "testDeviceId"; + key.userId = 1; + key.tokenId = "testTokenId"; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetServiceInfoProfileListByTokenId(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(key, serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceProfileConnectorThreeTest, GetServiceInfoByTokenId_003, testing::ext::TestSize.Level1) +{ + DmServiceInfoUniqueKey key; + key.deviceId = "testDeviceId"; + key.userId = 1; + key.tokenId = "testTokenId"; + + std::vector dpServiceInfos; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetServiceInfoProfileListByTokenId(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(dpServiceInfos), Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(key, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_TRUE(serviceInfos.empty()); +} +} +} \ No newline at end of file diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector_three.h b/test/commonunittest/UTTest_dm_deviceprofile_connector_three.h new file mode 100644 index 000000000..baae5e6b8 --- /dev/null +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector_three.h @@ -0,0 +1,44 @@ +/* + * 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_UTTEST_DM_DEVICEPROFILE_CONNECTOR_THREE_H +#define OHOS_UTTEST_DM_DEVICEPROFILE_CONNECTOR_THREE_H + +#include +#include + +#include "access_control_profile.h" +#include "deviceprofile_connector.h" +#include "distributed_device_profile_client_mock.h" +#include "deviceprofile_connector_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceProfileConnectorThreeTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static inline std::shared_ptr + distributedDeviceProfileClientMock_ = + std::make_shared(); + static inline std::shared_ptr deviceProfileConnectorMock_ = + std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_UTTEST_DM_DEVICEPROFILE_CONNECTOR_THREE_H \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index b405c21c9..2e7f5c0cb 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -58,6 +58,7 @@ group("unittest") { ":UTTest_dm_device_state_manager_two", ":UTTest_dm_deviceprofile_connector", ":UTTest_dm_deviceprofile_connector_second", + ":UTTest_dm_deviceprofile_connector_three", ":UTTest_dm_dfx", ":UTTest_dm_distributed_hardware_load", ":UTTest_dm_import_auth_code", @@ -507,6 +508,7 @@ ohos_unittest("UTTest_device_manager_service_three") { "mock/device_manager_service_mock.cpp", "mock/permission_manager_mock.cpp", "mock/softbus_listener_mock.cpp", + "mock/deviceprofile_connector_mock.cpp", ] deps = [ ":device_manager_test_common" ] @@ -1746,6 +1748,36 @@ ohos_unittest("UTTest_dm_deviceprofile_connector_second") { ## UTTest_dm_deviceprofile_connector_second }}} +############################# +## UnitTest UTTest_dm_deviceprofile_connector_three {{{ +ohos_unittest("UTTest_dm_deviceprofile_connector_three") { + module_out_path = module_out_path + + include_dirs = [ + "${devicemanager_path}/commondependency/include", + "${devicemanager_path}/test/commonunittest", + ] + + sources = [ + "${devicemanager_path}/test/commonunittest/UTTest_dm_deviceprofile_connector_three.cpp", + "${devicemanager_path}/test/unittest/mock/distributed_device_profile_client_mock.cpp", + "${devicemanager_path}/test/unittest/mock/deviceprofile_connector_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] +} + +## UTTest_dm_deviceprofile_connector_three }}} + ############################# ## UnitTest UTTest_dm_distributed_hardware_load {{{ diff --git a/test/unittest/UTTest_device_manager_service_three.cpp b/test/unittest/UTTest_device_manager_service_three.cpp index 5ca56ab14..0113d6525 100644 --- a/test/unittest/UTTest_device_manager_service_three.cpp +++ b/test/unittest/UTTest_device_manager_service_three.cpp @@ -64,6 +64,8 @@ void DeviceManagerServiceThreeTest::TearDown() Mock::VerifyAndClearExpectations(permissionManagerMock_.get()); Mock::VerifyAndClearExpectations(softbusListenerMock_.get()); Mock::VerifyAndClearExpectations(deviceManagerServiceImplMock_.get()); + Mock::VerifyAndClearExpectations(deviceProfileConnectorMock_.get()); + Mock::VerifyAndClearExpectations(multipleUserConnectorMock_.get()); } void DeviceManagerServiceThreeTest::SetUpTestCase() @@ -72,6 +74,8 @@ void DeviceManagerServiceThreeTest::SetUpTestCase() DmPermissionManager::dmPermissionManager = permissionManagerMock_; DmSoftbusListener::dmSoftbusListener = softbusListenerMock_; DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = deviceManagerServiceImplMock_; + DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_; + DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnectorMock_; } void DeviceManagerServiceThreeTest::TearDownTestCase() @@ -84,6 +88,10 @@ void DeviceManagerServiceThreeTest::TearDownTestCase() softbusListenerMock_ = nullptr; DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = nullptr; deviceManagerServiceImplMock_ = nullptr; + DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr; + deviceProfileConnectorMock_ = nullptr; + DmMultipleUserConnector::dmMultipleUserConnector = nullptr; + multipleUserConnectorMock_ = nullptr; } namespace { @@ -909,6 +917,434 @@ HWTEST_F(DeviceManagerServiceThreeTest, UnloadDMDeviceRiskDetect_301, testing::e DeviceManagerService::GetInstance().UnloadDMDeviceRiskDetect(); EXPECT_EQ(DeviceManagerService::GetInstance().dmDeviceRiskDetect_, nullptr); } + +HWTEST_F(DeviceManagerServiceThreeTest, UnbindTarget_001, testing::ext::TestSize.Level1) { + PeerTargetId targetId; + targetId.serviceId = 12345; + targetId.deviceId = "testDeviceId"; + + std::map unbindParam; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(std::vector{ + { .serviceRegInfo = { .serviceInfo = { .serviceId = 12345 } }, .tokenId = "testTokenId", + .deviceId = "testDeviceId", .userId = 1 } + }), testing::Return(DM_OK))); + + int32_t ret = DeviceManagerService::GetInstance().UnbindTarget("testPkgName", targetId, unbindParam); + EXPECT_EQ(ret, DM_OK); + // EXPECT_EQ(unbindParam[IS_BIND_SERVICE], "true"); + // EXPECT_EQ(unbindParam[SERVICE_ID], "12345"); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnbindTarget_002, testing::ext::TestSize.Level1) { + PeerTargetId targetId; + targetId.serviceId = 12345; + + std::map unbindParam; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().UnbindTarget("testPkgName", targetId, unbindParam); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnbindTarget_003, testing::ext::TestSize.Level1) { + PeerTargetId targetId; + targetId.serviceId = 12345; + + std::map unbindParam; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(std::vector{ + { .serviceRegInfo = { .serviceInfo = { .serviceId = 12345 } }, + .tokenId = "testTokenId", .deviceId = "testDeviceId", .userId = 1 } + }), testing::Return(DM_OK))); + + int32_t ret = DeviceManagerService::GetInstance().UnbindTarget("testPkgName", targetId, unbindParam); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, ServiceInfoBindParmamHandle_001, testing::ext::TestSize.Level1) +{ + PeerTargetId targetId; + targetId.serviceId = 12345; + std::map bindParam; + + DmServiceAddress addr; + addr.bleMac = "testBleMac"; + EXPECT_CALL(*softbusListenerMock_, GetPeerServiceAddress(targetId.serviceId, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<1>(addr), testing::Return(DM_OK))); + + DeviceManagerService::GetInstance().ServiceInfoBindParmamHandle(targetId, bindParam); + + // EXPECT_EQ(bindParam[SERVICE_ID], "12345"); + // EXPECT_EQ(bindParam[IS_BIND_SERVICE], "ture"); + EXPECT_EQ(targetId.bleMac, "testBleMac"); +} + +HWTEST_F(DeviceManagerServiceThreeTest, ServiceInfoBindParmamHandle_002, testing::ext::TestSize.Level1) +{ + PeerTargetId targetId; + targetId.serviceId = 12345; + std::map bindParam; + + EXPECT_CALL(*softbusListenerMock_, GetPeerServiceAddress(targetId.serviceId, testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + EXPECT_CALL(*softbusListenerMock_, GetUdidByNetworkId(testing::_, testing::_)) + .WillOnce(testing::Return(DM_OK)); + + DeviceManagerService::GetInstance().ServiceInfoBindParmamHandle(targetId, bindParam); + + EXPECT_TRUE(bindParam.find(SERVICE_ID) == bindParam.end()); + EXPECT_TRUE(bindParam.find(IS_BIND_SERVICE) == bindParam.end()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, ServiceInfoBindParmamHandle_003, testing::ext::TestSize.Level1) +{ + PeerTargetId targetId; + targetId.serviceId = 0; + std::map bindParam; + + EXPECT_CALL(*softbusListenerMock_, GetPeerServiceAddress(testing::_, testing::_)).Times(0); + + DeviceManagerService::GetInstance().ServiceInfoBindParmamHandle(targetId, bindParam); + + EXPECT_TRUE(bindParam.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, OpenAuthSessionWithPara_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceAdapterResidentLoad()) + .WillOnce(testing::Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().OpenAuthSessionWithPara("deviceId", 123, true); + EXPECT_EQ(ret, ERR_DM_UNSUPPORTED_METHOD); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetOnlineServiceList_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(testing::_)).Times(0); + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + std::map filterOptions; + std::vector serviceList; + int32_t ret = DeviceManagerService::GetInstance().GetOnlineServiceList(filterOptions, serviceList); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetOnlineServiceList_002, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(DM_OK)); + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + std::map filterOptions; + std::vector serviceList; + int32_t ret = DeviceManagerService::GetInstance().GetOnlineServiceList(filterOptions, serviceList); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetOnlineServiceList_003, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(DM_OK)); + + std::vector onlineDeviceList = { { "onlineDeviceId", "deviceName", 1 } }; + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(onlineDeviceList), testing::Return(DM_OK))); + + EXPECT_CALL(*deviceManagerServiceMock_, CheckIsDeviceOnline(testing::_, testing::_)) + .WillOnce(testing::Return(false)); + + std::map filterOptions; + std::vector serviceList; + int32_t ret = DeviceManagerService::GetInstance().GetOnlineServiceList(filterOptions, serviceList); + EXPECT_EQ(ret, DM_OK); + EXPECT_TRUE(serviceList.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetOnlineServiceList_004, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(DM_OK)); + + std::vector onlineDeviceList = { { "onlineDeviceId", "deviceName", 1 } }; + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(onlineDeviceList), testing::Return(DM_OK))); + + EXPECT_CALL(*deviceManagerServiceMock_, CheckIsDeviceOnline(testing::_, testing::_)) + .WillOnce(testing::Return(true)); + + std::map filterOptions; + std::vector serviceList; + int32_t ret = DeviceManagerService::GetInstance().GetOnlineServiceList(filterOptions, serviceList); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceList.size(), 1); + // EXPECT_EQ(serviceList[0].serviceId, "serviceId"); + // EXPECT_EQ(serviceList[0].serviceName, "serviceName"); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetTrustedServiceList_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + std::map filterOptions; + std::vector serviceList; + int32_t ret = DeviceManagerService::GetInstance().GetTrustedServiceList(filterOptions, serviceList); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceList.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetTrustedServiceList_002, testing::ext::TestSize.Level1) +{ + ServiceBindLocalInfo localInfo = { "localUdid", 123, 1 }; + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(DoAll(testing::SetArgReferee<0>(localInfo), testing::Return(DM_OK))); + + std::vector allTrustDeviceProfiles; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfileByUserId(localInfo.userId)) + .WillOnce(testing::Return(allTrustDeviceProfiles)); + + std::map filterOptions; + std::vector serviceList; + int32_t ret = DeviceManagerService::GetInstance().GetTrustedServiceList(filterOptions, serviceList); + + EXPECT_EQ(ret, DM_OK); + EXPECT_TRUE(serviceList.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoByServiceId_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + DmServiceInfo dmServiceInfo; + int64_t serviceId = 12345; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoByServiceId(serviceId, dmServiceInfo); + + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoByServiceId_002, testing::ext::TestSize.Level1) +{ + ServiceBindLocalInfo localInfo = { "localUdid", 123, 1 }; + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(DoAll(testing::SetArgReferee<0>(localInfo), testing::Return(DM_OK))); + + DmServiceInfoUniqueKey serviceInfoUniqueKey; + serviceInfoUniqueKey.serviceId = 12345; + serviceInfoUniqueKey.tokenId = std::to_string(localInfo.tokenId); + serviceInfoUniqueKey.userId = localInfo.userId; + serviceInfoUniqueKey.deviceId = localInfo.localUdid; + + DmServiceInfo dmServiceInfo; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoByServiceId( + serviceInfoUniqueKey.serviceId, dmServiceInfo); + + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoProfileByServiceId_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + DmServiceInfoProfile dmServiceInfoProfile; + int64_t serviceId = 12345; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoProfileByServiceId(serviceId, dmServiceInfoProfile); + + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoProfileByServiceId_002, testing::ext::TestSize.Level1) +{ + ServiceBindLocalInfo localInfo = { "localUdid", 123, 1 }; + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(testing::_)) + .WillOnce(DoAll(testing::SetArgReferee<0>(localInfo), testing::Return(DM_OK))); + + DmServiceInfoUniqueKey serviceInfoUniqueKey; + serviceInfoUniqueKey.serviceId = 12345; + serviceInfoUniqueKey.tokenId = std::to_string(localInfo.tokenId); + serviceInfoUniqueKey.userId = localInfo.userId; + serviceInfoUniqueKey.deviceId = localInfo.localUdid; + + DmServiceInfoProfile dmServiceInfoProfile; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoProfileByServiceId( + serviceInfoUniqueKey.serviceId, dmServiceInfoProfile); + + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetCallerServiceInfo_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + std::vector serviceInfos; + int32_t ret = DeviceManagerService::GetInstance().GetCallerServiceInfo(serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetCallerServiceInfo_002, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(std::vector()), testing::Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceManagerService::GetInstance().GetCallerServiceInfo(serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetCallerServiceInfo_003, testing::ext::TestSize.Level1) +{ + int64_t tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + + DmServiceInfoProfile profile1; + profile1.tokenId = std::to_string(tokenId); + profile1.serviceRegInfo.serviceInfo.serviceId = 12345; + profile1.serviceRegInfo.serviceInfo.serviceName = "TestService"; + + DmServiceInfoProfile profile2; + profile2.tokenId = "nonMatchingTokenId"; + + std::vector serviceInfoProfiles = { profile1, profile2 }; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(serviceInfoProfiles), testing::Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceManagerService::GetInstance().GetCallerServiceInfo(serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceInfos.size(), 1); + // EXPECT_EQ(serviceInfos[0].serviceId, profile1.serviceRegInfo.serviceInfo.serviceId); + // EXPECT_EQ(serviceInfos[0].serviceName, profile1.serviceRegInfo.serviceInfo.serviceName); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceBindLocalInfo_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceManagerServiceMock_, GetLocalDeviceInfo(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + ServiceBindLocalInfo localInfo; + int32_t ret = DeviceManagerService::GetInstance().GetServiceBindLocalInfo(localInfo); + + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceBindLocalInfo_002, testing::ext::TestSize.Level1) +{ + DmDeviceInfo localDeviceInfo = { "localDeviceId", "localDeviceName", 1 }; + EXPECT_CALL(*deviceManagerServiceMock_, GetLocalDeviceInfo(testing::_)) + .WillOnce(DoAll(testing::SetArgReferee<0>(localDeviceInfo), testing::Return(DM_OK))); + + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)) + .WillOnce(testing::Invoke([](int& userId) { + userId = 123; + return DM_OK; + })); + + ServiceBindLocalInfo localInfo; + int32_t ret = DeviceManagerService::GetInstance().GetServiceBindLocalInfo(localInfo); + + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(localInfo.localUdid, localDeviceInfo.deviceId); + EXPECT_EQ(localInfo.tokenId, OHOS::IPCSkeleton::GetCallingTokenID()); + EXPECT_EQ(localInfo.userId, 123); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfosByTokenId_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + std::vector serviceInfos; + int64_t tokenId = 12345; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfosByTokenId(tokenId, serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfosByTokenId_002, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(std::vector()), testing::Return(DM_OK))); + + std::vector serviceInfos; + int64_t tokenId = 12345; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfosByTokenId(tokenId, serviceInfos); + + EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_TRUE(serviceInfos.empty()); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfosByTokenId_003, testing::ext::TestSize.Level1) +{ + int64_t tokenId = 12345; + + DmServiceInfoProfile profile1; + profile1.tokenId = std::to_string(tokenId); + profile1.serviceRegInfo.serviceInfo.serviceId = 1001; + profile1.serviceRegInfo.serviceInfo.serviceName = "TestService1"; + + DmServiceInfoProfile profile2; + profile2.tokenId = "nonMatchingTokenId"; + + std::vector serviceInfoProfiles = { profile1, profile2 }; + + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllServiceInfoProfileList(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(serviceInfoProfiles), testing::Return(DM_OK))); + + std::vector serviceInfos; + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfosByTokenId(tokenId, serviceInfos); + + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceInfos.size(), 1); + // EXPECT_EQ(serviceInfos[0].serviceId, profile1.serviceRegInfo.serviceInfo.serviceId); + // EXPECT_EQ(serviceInfos[0].serviceName, profile1.serviceRegInfo.serviceInfo.serviceName); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsDeviceOnline_001, testing::ext::TestSize.Level1) +{ + std::vector onlineDeviceList = { + { "deviceId1", "deviceName1", 1 }, + { "deviceId2", "deviceName2", 2 } + }; + std::string peerUdid = "deviceId1"; + bool result = DeviceManagerService::GetInstance().CheckIsDeviceOnline(onlineDeviceList, peerUdid); + + EXPECT_TRUE(result); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsDeviceOnline_002, testing::ext::TestSize.Level1) +{ + std::vector onlineDeviceList = { + { "deviceId1", "deviceName1", 1 }, + { "deviceId2", "deviceName2", 2 } + }; + std::string peerUdid = "deviceId3"; + bool result = DeviceManagerService::GetInstance().CheckIsDeviceOnline(onlineDeviceList, peerUdid); + + EXPECT_FALSE(result); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsDeviceOnline_003, testing::ext::TestSize.Level1) +{ + std::vector onlineDeviceList; + std::string peerUdid = "deviceId1"; + bool result = DeviceManagerService::GetInstance().CheckIsDeviceOnline(onlineDeviceList, peerUdid); + + EXPECT_FALSE(result); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_three.h b/test/unittest/UTTest_device_manager_service_three.h index 4f7c7666b..772fdcda3 100644 --- a/test/unittest/UTTest_device_manager_service_three.h +++ b/test/unittest/UTTest_device_manager_service_three.h @@ -31,6 +31,8 @@ #include "softbus_listener_mock.h" #include "device_manager_service_mock.h" #include "device_manager_service_impl_mock.h" +#include "deviceprofile_connector_mock.h" +#include "multiple_user_connector_mock.h" namespace OHOS { namespace DistributedHardware { @@ -49,6 +51,10 @@ public: std::make_shared(); static inline std::shared_ptr deviceManagerServiceImplMock_ = std::make_shared(); + static inline std::shared_ptr deviceProfileConnectorMock_ = + std::make_shared(); + static inline std::shared_ptr multipleUserConnectorMock_ = + std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_dm_auth_message_processor.cpp b/test/unittest/UTTest_dm_auth_message_processor.cpp index 43793179d..8f817268e 100644 --- a/test/unittest/UTTest_dm_auth_message_processor.cpp +++ b/test/unittest/UTTest_dm_auth_message_processor.cpp @@ -27,10 +27,13 @@ namespace DistributedHardware { void DmAuthMessageProcessorTest::SetUpTestCase() { + DmCryptoMgr::dmCryptoMgr = cryptoMgrMock_; } void DmAuthMessageProcessorTest::TearDownTestCase() { + DmCryptoMgr::dmCryptoMgr = nullptr; + cryptoMgrMock_ = nullptr; } void DmAuthMessageProcessorTest::SetUp() @@ -195,6 +198,396 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseNegotiateMessage_006, testing::ext::Te EXPECT_EQ(ret, DM_OK); EXPECT_EQ(context->businessId, "testBusinessId"); } + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageReqCredExchange_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accesser.isGenerateLnnCredential = true; + context->accesser.bindLevel = 1; + context->accesser.lnnPublicKey = "lnnPublicKey"; + context->accesser.transmitPublicKey = "transmitPublicKey"; + context->accesser.deviceId = "deviceId"; + context->accesser.userId = 123; + context->accesser.tokenId = 456; + context->accesser.networkId = "networkId"; + + DmServiceInfo serviceInfo; + serviceInfo.serviceId = 12345; + serviceInfo.serviceType = "serviceType"; + serviceInfo.serviceName = "serviceName"; + serviceInfo.serviceDisplayName = "serviceDisplayName"; + serviceInfo.customData = "customData"; + serviceInfo.customDataLen = 10; + serviceInfo.bundleName = "bundleName"; + serviceInfo.moduleName = "moduleName"; + serviceInfo.abilityName = "abilityName"; + context->accesser.serviceInfos.push_back(serviceInfo); + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("encryptedMessage"), Return(DM_OK))); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageReqCredExchange(context, jsonObject); + + EXPECT_EQ(ret, DM_OK); + //EXPECT_EQ(jsonObject[TAG_DATA].Get(), "encryptedMessage"); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageReqCredExchange_002, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accesser.isGenerateLnnCredential = true; + context->accesser.bindLevel = 1; + context->accesser.lnnPublicKey = "lnnPublicKey"; + context->accesser.transmitPublicKey = "transmitPublicKey"; + context->accesser.deviceId = "deviceId"; + context->accesser.userId = 123; + context->accesser.tokenId = 456; + context->accesser.networkId = "networkId"; + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageReqCredExchange(context, jsonObject); + + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageReqCredExchange_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accesser.isGenerateLnnCredential = false; + context->accesser.bindLevel = static_cast(USER); + context->accesser.transmitPublicKey = "transmitPublicKey"; + context->accesser.deviceId = "deviceId"; + context->accesser.userId = 123; + context->accesser.tokenId = 456; + context->accesser.networkId = "networkId"; + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("encryptedMessage"), Return(DM_OK))); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageReqCredExchange(context, jsonObject); + + EXPECT_EQ(ret, DM_OK); + //EXPECT_EQ(jsonObject[TAG_DATA].Get(), "encryptedMessage"); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageReqCredExchange_004, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accesser.isGenerateLnnCredential = true; + context->accesser.bindLevel = 1; + context->accesser.lnnPublicKey = "lnnPublicKey"; + context->accesser.transmitPublicKey = "transmitPublicKey"; + context->accesser.deviceId = "deviceId"; + context->accesser.userId = 123; + context->accesser.tokenId = 456; + context->accesser.networkId = "networkId"; + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("encryptedMessage"), Return(DM_OK))); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageReqCredExchange(context, jsonObject); + + EXPECT_EQ(ret, DM_OK); + //EXPECT_EQ(jsonObject[TAG_DATA].Get(), "encryptedMessage"); +} + + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageRspCredExchange_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accessee.isGenerateLnnCredential = true; + context->accessee.bindLevel = 1; // Not USER + context->accessee.lnnPublicKey = "lnnPublicKey"; + context->accessee.transmitPublicKey = "transmitPublicKey"; + context->accessee.deviceId = "deviceId"; + context->accessee.userId = 123; + context->accessee.tokenId = 456; + context->accessee.networkId = "networkId"; + + DmServiceInfo serviceInfo; + serviceInfo.serviceId = 12345; + serviceInfo.serviceType = "serviceType"; + serviceInfo.serviceName = "serviceName"; + serviceInfo.serviceDisplayName = "serviceDisplayName"; + serviceInfo.customData = "customData"; + serviceInfo.customDataLen = 10; + serviceInfo.bundleName = "bundleName"; + serviceInfo.moduleName = "moduleName"; + serviceInfo.abilityName = "abilityName"; + context->accessee.serviceInfos.push_back(serviceInfo); + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("encryptedMessage"), Return(DM_OK))); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageRspCredExchange(context, jsonObject); + + EXPECT_EQ(ret, DM_OK); + //EXPECT_EQ(jsonObject[TAG_DATA].Get(), "encryptedMessage"); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageRspCredExchange_002, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accessee.isGenerateLnnCredential = true; + context->accessee.bindLevel = 1; // Not USER + context->accessee.lnnPublicKey = "lnnPublicKey"; + context->accessee.transmitPublicKey = "transmitPublicKey"; + context->accessee.deviceId = "deviceId"; + context->accessee.userId = 123; + context->accessee.tokenId = 456; + context->accessee.networkId = "networkId"; + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageRspCredExchange(context, jsonObject); + + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageRspCredExchange_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accessee.isGenerateLnnCredential = false; + context->accessee.bindLevel = static_cast(USER); + context->accessee.transmitPublicKey = "transmitPublicKey"; + context->accessee.deviceId = "deviceId"; + context->accessee.userId = 123; + context->accessee.tokenId = 456; + context->accessee.networkId = "networkId"; + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("encryptedMessage"), Return(DM_OK))); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageRspCredExchange(context, jsonObject); + + EXPECT_EQ(ret, DM_OK); + //EXPECT_EQ(jsonObject[TAG_DATA].Get(), "encryptedMessage"); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessageRspCredExchange_004, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + context->accessee.isGenerateLnnCredential = true; + context->accessee.bindLevel = 1; + context->accessee.lnnPublicKey = "lnnPublicKey"; + context->accessee.transmitPublicKey = "transmitPublicKey"; + context->accessee.deviceId = "deviceId"; + context->accessee.userId = 123; + context->accessee.tokenId = 456; + context->accessee.networkId = "networkId"; + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("encryptedMessage"), Return(DM_OK))); + + JsonObject jsonObject; + std::shared_ptr processor = std::make_shared(); + int32_t ret = processor->CreateMessageRspCredExchange(context, jsonObject); + + EXPECT_EQ(ret, DM_OK); + //EXPECT_EQ(jsonObject[TAG_DATA].Get(), "encryptedMessage"); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSrcServiceInfoToContext_001, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + std::shared_ptr processor = std::make_shared(); + processor->ParseSrcServiceInfoToContext(serviceJson, nullptr); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSrcServiceInfoToContext_002, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSrcServiceInfoToContext(serviceJson, context); + + EXPECT_TRUE(context->accesser.serviceInfos.empty()); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSrcServiceInfoToContext_003, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + serviceJson["serviceInfos"] = 123; + + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSrcServiceInfoToContext(serviceJson, context); + + EXPECT_TRUE(context->accesser.serviceInfos.empty()); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSrcServiceInfoToContext_004, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + JsonObject serviceInfo1(JsonCreateType::JSON_CREATE_TYPE_OBJECT); + serviceInfo1["serviceId"] = "12345"; + serviceInfo1["serviceType"] = "Type1"; + serviceInfo1["serviceName"] = "Service1"; + serviceInfo1["serviceDisplayName"] = "DisplayName1"; + serviceInfo1["customData"] = "CustomData1"; + serviceInfo1["customDataLen"] = 10; + serviceInfo1[TAG_BUNDLE_NAME] = "Bundle1"; + serviceInfo1["moduleName"] = "Module1"; + serviceInfo1["abilityName"] = "Ability1"; + + JsonObject serviceInfos(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + serviceInfos.PushBack(serviceInfo1); + serviceJson["serviceInfos"] = serviceInfos.Dump(); + + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSrcServiceInfoToContext(serviceJson, context); + + EXPECT_EQ(context->accesser.serviceInfos.size(), 1); + // EXPECT_EQ(context->accesser.serviceInfos[0].serviceId, 12345); + // EXPECT_EQ(context->accesser.serviceInfos[0].serviceType, "Type1"); + // EXPECT_EQ(context->accesser.serviceInfos[0].serviceName, "Service1"); + // EXPECT_EQ(context->accesser.serviceInfos[0].serviceDisplayName, "DisplayName1"); + // EXPECT_EQ(context->accesser.serviceInfos[0].customData, "CustomData1"); + // EXPECT_EQ(context->accesser.serviceInfos[0].customDataLen, 10); + // EXPECT_EQ(context->accesser.serviceInfos[0].bundleName, "Bundle1"); + // EXPECT_EQ(context->accesser.serviceInfos[0].moduleName, "Module1"); + // EXPECT_EQ(context->accesser.serviceInfos[0].abilityName, "Ability1"); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSrcServiceInfoToContext_005, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + JsonObject serviceInfo1(JsonCreateType::JSON_CREATE_TYPE_OBJECT); + serviceInfo1["serviceId"] = 12345; + serviceInfo1["serviceType"] = 123; + serviceInfo1["serviceName"] = true; + + JsonObject serviceInfos(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + serviceInfos.PushBack(serviceInfo1); + + serviceJson["serviceInfos"] = serviceInfos.Dump(); + + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSrcServiceInfoToContext(serviceJson, context); + + EXPECT_TRUE(context->accesser.serviceInfos.empty()); +} + + +HWTEST_F(DmAuthMessageProcessorTest, ParseSinkServiceInfoToContext_001, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + //std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSinkServiceInfoToContext(serviceJson, nullptr); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSinkServiceInfoToContext_002, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSinkServiceInfoToContext(serviceJson, context); + + EXPECT_TRUE(context->accessee.serviceInfos.empty()); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSinkServiceInfoToContext_003, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + serviceJson["serviceInfos"] = 123; + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSinkServiceInfoToContext(serviceJson, context); + + EXPECT_TRUE(context->accessee.serviceInfos.empty()); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSinkServiceInfoToContext_004, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + JsonObject serviceInfo1(JsonCreateType::JSON_CREATE_TYPE_OBJECT); + serviceInfo1["serviceId"] = "12345"; + serviceInfo1["serviceType"] = "Type1"; + serviceInfo1["serviceName"] = "Service1"; + serviceInfo1["serviceDisplayName"] = "DisplayName1"; + serviceInfo1["customData"] = "CustomData1"; + serviceInfo1["customDataLen"] = 10; + serviceInfo1[TAG_BUNDLE_NAME] = "Bundle1"; + serviceInfo1["moduleName"] = "Module1"; + serviceInfo1["abilityName"] = "Ability1"; + + JsonObject serviceInfos(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + serviceInfos.PushBack(serviceInfo1); + + serviceJson["serviceInfos"] = serviceInfos.Dump(); + + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSinkServiceInfoToContext(serviceJson, context); + + EXPECT_EQ(context->accessee.serviceInfos.size(), 1); + // EXPECT_EQ(context->accessee.serviceInfos[0].serviceId, 12345); + // EXPECT_EQ(context->accessee.serviceInfos[0].serviceType, "Type1"); + // EXPECT_EQ(context->accessee.serviceInfos[0].serviceName, "Service1"); + // EXPECT_EQ(context->accessee.serviceInfos[0].serviceDisplayName, "DisplayName1"); + // EXPECT_EQ(context->accessee.serviceInfos[0].customData, "CustomData1"); + // EXPECT_EQ(context->accessee.serviceInfos[0].customDataLen, 10); + // EXPECT_EQ(context->accessee.serviceInfos[0].bundleName, "Bundle1"); + // EXPECT_EQ(context->accessee.serviceInfos[0].moduleName, "Module1"); + // EXPECT_EQ(context->accessee.serviceInfos[0].abilityName, "Ability1"); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSinkServiceInfoToContext_005, testing::ext::TestSize.Level1) +{ + JsonObject serviceJson; + JsonObject serviceInfo1(JsonCreateType::JSON_CREATE_TYPE_OBJECT); + serviceInfo1["serviceId"] = 12345; + serviceInfo1["serviceType"] = 123; + serviceInfo1["serviceName"] = true; + + JsonObject serviceInfos(JsonCreateType::JSON_CREATE_TYPE_ARRAY); + serviceInfos.PushBack(serviceInfo1); + + serviceJson["serviceInfos"] = serviceInfos.Dump(); + + std::shared_ptr context = std::make_shared(); + //context->authStateMachine = std::make_shared(context); + std::shared_ptr processor = std::make_shared(); + processor->ParseSinkServiceInfoToContext(serviceJson, context); + + EXPECT_TRUE(context->accessee.serviceInfos.empty()); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_dm_auth_message_processor.h b/test/unittest/UTTest_dm_auth_message_processor.h index ff63a0967..5966801b3 100644 --- a/test/unittest/UTTest_dm_auth_message_processor.h +++ b/test/unittest/UTTest_dm_auth_message_processor.h @@ -30,6 +30,8 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + + static inline std::shared_ptr cryptoMgrMock_ = std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/device_manager_service_mock.cpp b/test/unittest/mock/device_manager_service_mock.cpp index a6db438a6..52cd87c69 100644 --- a/test/unittest/mock/device_manager_service_mock.cpp +++ b/test/unittest/mock/device_manager_service_mock.cpp @@ -39,5 +39,21 @@ bool DeviceManagerService::IsDMServiceAdapterSoLoaded() { return DmDeviceManagerService::dmDeviceManagerService->IsDMServiceAdapterSoLoaded(); } + +int32_t DeviceManagerService::GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo) +{ + return DmDeviceManagerService::dmDeviceManagerService->GetServiceBindLocalInfo(localInfo); +} + +bool DeviceManagerService::CheckIsDeviceOnline(const std::vector &onlineDeviceList, + const std::string &peerUdid) +{ + return DmDeviceManagerService::dmDeviceManagerService->CheckIsDeviceOnline(onlineDeviceList, peerUdid); +} + +int32_t DeviceManagerService::GetLocalDeviceInfo(DmDeviceInfo &info) +{ + return DmDeviceManagerService::dmDeviceManagerService->GetLocalDeviceInfo(info); +} } // 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 2d5230c0e..9c8ae3534 100644 --- a/test/unittest/mock/device_manager_service_mock.h +++ b/test/unittest/mock/device_manager_service_mock.h @@ -29,7 +29,11 @@ public: virtual bool IsDMServiceImplReady() = 0; virtual int32_t GetTrustedDeviceList(const std::string &pkgName, std::vector &deviceList) = 0; virtual bool IsDMServiceAdapterResidentLoad() = 0; - virtual bool IsDMServiceAdapterSoLoaded() = 0;; + virtual bool IsDMServiceAdapterSoLoaded() = 0; + virtual int32_t GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo) = 0; + virtual bool CheckIsDeviceOnline(const std::vector &onlineDeviceList, + const std::string &peerUdid) = 0; + virtual int32_t GetLocalDeviceInfo(DmDeviceInfo &info) = 0; public: static inline std::shared_ptr dmDeviceManagerService = nullptr; }; @@ -40,6 +44,10 @@ public: MOCK_METHOD(int32_t, GetTrustedDeviceList, (const std::string &, std::vector &)); MOCK_METHOD(bool, IsDMServiceAdapterResidentLoad, ()); MOCK_METHOD(bool, IsDMServiceAdapterSoLoaded, ()); + MOCK_METHOD(int32_t, GetServiceBindLocalInfo, (ServiceBindLocalInfo &localInfo)); + MOCK_METHOD(bool, CheckIsDeviceOnline, (const std::vector &onlineDeviceList, + const std::string &peerUdid)); + MOCK_METHOD(int32_t, GetLocalDeviceInfo, (DmDeviceInfo &info)); }; } } diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index 603e20b2a..2d27bdbae 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -200,5 +200,23 @@ bool DeviceProfileConnector::IsLnnAcl(const DistributedDeviceProfile::AccessCont { return DmDeviceProfileConnector::dmDeviceProfileConnector->IsLnnAcl(profile); } + +int32_t DeviceProfileConnector::GetAllServiceInfoProfileList(std::vector &serviceInfos) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetAllServiceInfoProfileList( + serviceInfos); +} + +std::vector DeviceProfileConnector::GetAclProfileByDeviceIdAndUserId( + const std::string &deviceId, int32_t userId) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetAclProfileByDeviceIdAndUserId(deviceId, userId); +} + +std::vector DeviceProfileConnector::GetAccessControlProfileByUserId( + int32_t userId) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetAccessControlProfileByUserId(userId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/deviceprofile_connector_mock.h b/test/unittest/mock/deviceprofile_connector_mock.h index 17d9b9333..9b2641e16 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -75,6 +75,11 @@ public: virtual void CacheAcerAclId(const DistributedDeviceProfile::AccessControlProfile &profile, std::vector &aclInfos) = 0; virtual bool IsLnnAcl(const DistributedDeviceProfile::AccessControlProfile &profile) = 0; + virtual int32_t GetAllServiceInfoProfileList(std::vector &serviceInfoProfiles) = 0; + virtual std::vector GetAclProfileByDeviceIdAndUserId( + const std::string &deviceId, int32_t userId) = 0; + virtual std::vector GetAccessControlProfileByUserId( + int32_t userId) = 0; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -124,6 +129,11 @@ public: MOCK_METHOD(void, CacheAcerAclId, (const DistributedDeviceProfile::AccessControlProfile &profile, std::vector &aclInfos)); MOCK_METHOD(bool, IsLnnAcl, (const DistributedDeviceProfile::AccessControlProfile &profile)); + MOCK_METHOD(int32_t, GetAllServiceInfoProfileList, (std::vector &serviceInfoProfiles)); + MOCK_METHOD(std::vector, GetAclProfileByDeviceIdAndUserId, + (const std::string &deviceId, int32_t userId)); + MOCK_METHOD(std::vector, GetAccessControlProfileByUserId, + (int32_t userId)); }; } } diff --git a/test/unittest/mock/distributed_device_profile_client_mock.cpp b/test/unittest/mock/distributed_device_profile_client_mock.cpp index c67eda3c1..43abeb700 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.cpp +++ b/test/unittest/mock/distributed_device_profile_client_mock.cpp @@ -96,5 +96,19 @@ int32_t DistributedDeviceProfileClient::PutBusinessEvent(const DistributedDevice { return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->PutBusinessEvent(event); } + +int32_t DistributedDeviceProfileClient::GetServiceInfoProfileListByTokenId( + const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &dpServiceInfos) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->GetServiceInfoProfileListByTokenId( + key, dpServiceInfos); +} + +int32_t DistributedDeviceProfileClient::DeleteServiceInfoProfile( + const DistributedDeviceProfile::ServiceInfoUniqueKey &key) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->DeleteServiceInfoProfile(key); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/distributed_device_profile_client_mock.h b/test/unittest/mock/distributed_device_profile_client_mock.h index 13495ff3a..c858b3fa3 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.h +++ b/test/unittest/mock/distributed_device_profile_client_mock.h @@ -42,6 +42,9 @@ public: int32_t pinExchangeType, LocalServiceInfo& localServiceInfo) = 0; virtual int32_t GetBusinessEvent(BusinessEvent &event) = 0; virtual int32_t PutBusinessEvent(const DistributedDeviceProfile::BusinessEvent &event) = 0; + virtual int32_t GetServiceInfoProfileListByTokenId(const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &dpServiceInfos) = 0; + virtual int32_t DeleteServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoUniqueKey &key) = 0; public: static inline std::shared_ptr dpDistributedDeviceProfileClient = nullptr; }; @@ -62,6 +65,9 @@ public: MOCK_METHOD(int32_t, GetLocalServiceInfoByBundleAndPinType, (const std::string&, int32_t, LocalServiceInfo&)); MOCK_METHOD(int32_t, GetBusinessEvent, (BusinessEvent &event)); MOCK_METHOD(int32_t, PutBusinessEvent, (const DistributedDeviceProfile::BusinessEvent &event)); + MOCK_METHOD(int32_t, GetServiceInfoProfileListByTokenId, (const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &dpServiceInfos)); + MOCK_METHOD(int32_t, DeleteServiceInfoProfile, (const DistributedDeviceProfile::ServiceInfoUniqueKey &key)); }; } } diff --git a/test/unittest/mock/softbus_listener_mock.cpp b/test/unittest/mock/softbus_listener_mock.cpp index eb67a660c..ee2d809e8 100644 --- a/test/unittest/mock/softbus_listener_mock.cpp +++ b/test/unittest/mock/softbus_listener_mock.cpp @@ -100,5 +100,10 @@ void SoftbusListener::SendAclChangedBroadcast(const std::string &msg) { DmSoftbusListener::dmSoftbusListener->SendAclChangedBroadcast(msg); } + +int32_t SoftbusListener::GetPeerServiceAddress(int64_t serviceId, DmServiceAddress &addr) +{ + return DmSoftbusListener::dmSoftbusListener->GetPeerServiceAddress(serviceId, addr); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/softbus_listener_mock.h b/test/unittest/mock/softbus_listener_mock.h index 5a3f1aab7..3df98d0f8 100644 --- a/test/unittest/mock/softbus_listener_mock.h +++ b/test/unittest/mock/softbus_listener_mock.h @@ -45,6 +45,7 @@ public: std::vector &deviceList) = 0; virtual int32_t StopRefreshSoftbusLNN(uint16_t subscribeId) = 0; virtual void SendAclChangedBroadcast(const std::string &msg) = 0; + virtual int32_t GetPeerServiceAddress(int64_t serviceId, DmServiceAddress &addr) = 0; public: static inline std::shared_ptr dmSoftbusListener = nullptr; }; @@ -68,6 +69,7 @@ public: std::vector &)); MOCK_METHOD(int32_t, StopRefreshSoftbusLNN, (uint16_t)); MOCK_METHOD(void, SendAclChangedBroadcast, (const std::string &msg)); + MOCK_METHOD(int32_t, GetPeerServiceAddress, (int64_t serviceId, DmServiceAddress &addr)); }; } } -- Gitee