From c2c7d59854b98581c01b689e06fad847acf45408 Mon Sep 17 00:00:00 2001 From: daiyunlong Date: Sat, 30 Aug 2025 15:33:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=87=AA=E6=A3=80=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: daiyunlong --- .../dependency/hichain/hichain_auth_connector.h | 4 +++- .../src/dependency/hichain/hichain_auth_connector.cpp | 10 ++++++++++ services/service/src/pinholder/pin_holder_session.cpp | 8 +++++++- test/commonunittest/UTTest_hichain_auth_connector.cpp | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/services/implementation/include/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h index 8c3234d36..726cc0467 100644 --- a/services/implementation/include/dependency/hichain/hichain_auth_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_auth_connector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,8 @@ #ifndef OHOS_HICHAIN_AUTH_CONNECTOR_H #define OHOS_HICHAIN_AUTH_CONNECTOR_H +#define HICHAIN_DATA_SIZE 10240 + #include #include diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 23756a20f..24974f9e9 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -283,6 +283,11 @@ int32_t HiChainAuthConnector::AuthCredentialPinCode(int32_t osAccountId, int64_t bool HiChainAuthConnector::onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) { LOGI("AuthDevice onTransmit, requestId %{public}" PRId64, requestId); + if (dataLen > HICHAIN_DATA_SIZE) { + LOGE("dataLen = %{public}u is invalid.", dataLen); + return false; + } + CHECK_NULL_RETURN(data, false); auto dmDeviceAuthCallback = GetDeviceAuthCallback(requestId); if (dmDeviceAuthCallback == nullptr) { LOGE("HiChainAuthConnector::onTransmit dmDeviceAuthCallback_ is nullptr."); @@ -336,6 +341,11 @@ void HiChainAuthConnector::onError(int64_t requestId, int operationCode, int err void HiChainAuthConnector::onSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) { LOGI("start."); + if (sessionKeyLen > HICHAIN_DATA_SIZE) { + LOGE("sessionKeyLen = %{public}u is invalid.", sessionKeyLen); + return; + } + CHECK_NULL_VOID(sessionKey); auto dmDeviceAuthCallback = GetDeviceAuthCallback(requestId); if (dmDeviceAuthCallback == nullptr) { LOGE("HiChainAuthConnector::onSessionKeyReturned dmDeviceAuthCallback_ is nullptr."); diff --git a/services/service/src/pinholder/pin_holder_session.cpp b/services/service/src/pinholder/pin_holder_session.cpp index 44b8a3b8d..6eb5932ed 100644 --- a/services/service/src/pinholder/pin_holder_session.cpp +++ b/services/service/src/pinholder/pin_holder_session.cpp @@ -22,6 +22,12 @@ namespace OHOS { namespace DistributedHardware { + +namespace { + +constexpr int32_t MAX_DATA_LEN = 4096000; // Maximum MTU Value for Logical Session:4M + +} std::shared_ptr PinHolderSession::pinholderSessionCallback_ = nullptr; std::mutex PinHolderSession::pinHolderSessionLock_; PinHolderSession::PinHolderSession() @@ -110,7 +116,7 @@ void PinHolderSession::OnSessionClosed(int sessionId) void PinHolderSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen) { - if (sessionId < 0 || data == nullptr || dataLen <= 0) { + if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > MAX_DATA_LEN) { LOGE("[SOFTBUS]fail to receive data from softbus with sessionId: %{public}d, dataLen: %{public}d.", sessionId, dataLen); return; diff --git a/test/commonunittest/UTTest_hichain_auth_connector.cpp b/test/commonunittest/UTTest_hichain_auth_connector.cpp index fe744fccc..50bcad8cc 100644 --- a/test/commonunittest/UTTest_hichain_auth_connector.cpp +++ b/test/commonunittest/UTTest_hichain_auth_connector.cpp @@ -121,7 +121,7 @@ HWTEST_F(HiChainAuthConnectorTest, onTransmit_002, testing::ext::TestSize.Level1 uint32_t dataLen = 0; hiChain_->dmDeviceAuthCallback_ = std::make_shared(); bool ret = hiChain_->onTransmit(requestId, data, dataLen); - EXPECT_EQ(ret, true); + EXPECT_EQ(ret, false); } HWTEST_F(HiChainAuthConnectorTest, onRequest_001, testing::ext::TestSize.Level1) -- Gitee From 87c0c174a45143fcbe96f79d1e7f7332bfa64b33 Mon Sep 17 00:00:00 2001 From: daiyunlong Date: Mon, 1 Sep 2025 21:39:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ffrt=E5=90=8C=E6=AD=A5=E5=8E=9F=E8=AF=AD?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: daiyunlong --- .../hichain/hichain_auth_connector.h | 2 - .../hichain/hichain_auth_connector.cpp | 6 ++ .../service/src/softbus/softbus_listener.cpp | 31 +++++-- services/softbuscache/BUILD.gn | 3 +- .../softbuscache/include/dm_softbus_cache.h | 10 ++- .../softbuscache/src/dm_softbus_cache.cpp | 90 ++++++++++++++++++- .../UTTest_softbus_listener.cpp | 2 +- test/unittest/BUILD.gn | 1 + test/unittest/UTTest_dm_softbus_cache.cpp | 20 ++--- 9 files changed, 141 insertions(+), 24 deletions(-) diff --git a/services/implementation/include/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h index 726cc0467..93b92b29f 100644 --- a/services/implementation/include/dependency/hichain/hichain_auth_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_auth_connector.h @@ -16,8 +16,6 @@ #ifndef OHOS_HICHAIN_AUTH_CONNECTOR_H #define OHOS_HICHAIN_AUTH_CONNECTOR_H -#define HICHAIN_DATA_SIZE 10240 - #include #include diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 24974f9e9..5ee041f79 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -27,6 +27,12 @@ namespace OHOS { namespace DistributedHardware { +namespace { + +constexpr int32_t HICHAIN_DATA_SIZE = 10240; + +} + std::shared_ptr HiChainAuthConnector::dmDeviceAuthCallback_ = nullptr; std::map> HiChainAuthConnector::dmDeviceAuthCallbackMap_; std::mutex HiChainAuthConnector::dmDeviceAuthCallbackMutex_; diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 036b1e43b..fda986130 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -70,14 +70,21 @@ static std::mutex g_deviceMapMutex; static std::mutex g_lnnCbkMapMutex; static std::mutex g_radarLoadLock; static std::mutex g_onlineDeviceNumLock; + +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +static ffrt::mutex g_lockDeviceTrustedChange; +static ffrt::mutex g_lockUserIdCheckSumChange; +static ffrt::mutex g_credentialAuthStatus; +static ffrt::mutex g_dmSoftbusEventQueueLock; +#else static std::mutex g_lockDeviceTrustedChange; -static std::mutex g_lockUserIdCheckSumChange; +static std::mutex g_credentialAuthStatus; +#endif static std::mutex g_lockDeviceOnLine; static std::mutex g_lockDeviceOffLine; static std::mutex g_lockDevInfoChange; static std::mutex g_lockDeviceIdSet; static std::mutex g_lockDevScreenStatusChange; -static std::mutex g_credentialAuthStatus; static std::map>>> discoveredDeviceMap; static std::map> lnnOpsCbkMap; @@ -90,7 +97,6 @@ static std::mutex g_hostNameMutex; std::string SoftbusListener::hostName_ = ""; int32_t g_onlineDeviceNum = 0; static std::map> g_dmSoftbusEventQueueMap; -static std::mutex g_dmSoftbusEventQueueLock; static int OnSessionOpened(int sessionId, int result) { @@ -165,14 +171,18 @@ void SoftbusListener::DeviceNameChange(DmDeviceInfo deviceInfo) void SoftbusListener::DeviceNotTrust(const std::string &msg) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard lock(g_lockDeviceTrustedChange); +#else std::lock_guard lock(g_lockDeviceTrustedChange); +#endif DeviceManagerService::GetInstance().HandleDeviceNotTrust(msg); } void SoftbusListener::DeviceTrustedChange(const std::string &msg) { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - std::lock_guard lock(g_lockDeviceTrustedChange); + std::lock_guard lock(g_lockDeviceTrustedChange); DeviceManagerService::GetInstance().HandleDeviceTrustedChange(msg); #else (void)msg; @@ -182,7 +192,7 @@ void SoftbusListener::DeviceTrustedChange(const std::string &msg) void SoftbusListener::DeviceUserIdCheckSumChange(const std::string &msg) { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - std::lock_guard lock(g_lockUserIdCheckSumChange); + std::lock_guard lock(g_lockUserIdCheckSumChange); DeviceManagerService::GetInstance().HandleUserIdCheckSumChange(msg); #else (void)msg; @@ -197,7 +207,11 @@ void SoftbusListener::DeviceScreenStatusChange(DmDeviceInfo deviceInfo) void SoftbusListener::CredentialAuthStatusProcess(std::string deviceList, uint16_t deviceTypeId, int32_t errcode) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard lock(g_credentialAuthStatus); +#else std::lock_guard lock(g_credentialAuthStatus); +#endif DeviceManagerService::GetInstance().HandleCredentialAuthStatus(deviceList, deviceTypeId, errcode); } @@ -267,7 +281,7 @@ void SoftbusListener::SoftbusEventQueueHandle(std::string deviceId) { std::queue eventQueue; { - std::lock_guard lock(g_dmSoftbusEventQueueLock); + std::lock_guard lock(g_dmSoftbusEventQueueLock); auto it = g_dmSoftbusEventQueueMap.find(deviceId); if (it == g_dmSoftbusEventQueueMap.end()) { return; @@ -297,7 +311,7 @@ void SoftbusListener::SoftbusEventQueueHandle(std::string deviceId) } } { - std::lock_guard lock(g_dmSoftbusEventQueueLock); + std::lock_guard lock(g_dmSoftbusEventQueueLock); auto it = g_dmSoftbusEventQueueMap.find(deviceId); if (it == g_dmSoftbusEventQueueMap.end()) { return; @@ -321,7 +335,7 @@ int32_t SoftbusListener::SoftbusEventQueueAdd(DmSoftbusEvent &dmSoftbusEventInfo std::string deviceId(dmSoftbusEventInfo.dmDeviceInfo.deviceId); LOGI("deviceIdHash:%{public}s.", GetAnonyString(deviceId).c_str()); { - std::lock_guard lock(g_dmSoftbusEventQueueLock); + std::lock_guard lock(g_dmSoftbusEventQueueLock); auto it = g_dmSoftbusEventQueueMap.find(deviceId); if (it == g_dmSoftbusEventQueueMap.end()) { std::queue eventQueue; @@ -1387,6 +1401,7 @@ int32_t SoftbusListener::SetLocalDisplayName(const std::string &displayName) int32_t ret = ::SetDisplayName(DM_PKG_NAME, displayName.c_str(), len); if (ret != DM_OK) { LOGE("SoftbusListener SetLocalDisplayName failed!"); + return ret; } return DM_OK; } diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 49a243c36..47d07cf6b 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2024 Huawei Device Co., Ltd. +# Copyright (c) 2024-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -93,6 +93,7 @@ if (defined(ohos_lite)) { external_deps = [ "bounds_checking_function:libsec_shared", "dsoftbus:softbus_client", + "ffrt:libffrt", "hilog:libhilog", ] diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 4bf4a3f6c..002650522 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,6 +20,9 @@ #include #include #include "dm_device_info.h" +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +#include "ffrt.h" +#endif #include "dm_single_instance.h" #include "softbus_bus_center.h" #include "softbus_common.h" @@ -60,8 +63,13 @@ private: int32_t GetDevLevelFromBus(const char *networkId, int32_t &securityLevel); private: +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ffrt::mutex deviceInfosMutex_; + ffrt::mutex deviceSecurityLevelMutex_; +#else std::mutex deviceInfosMutex_; std::mutex deviceSecurityLevelMutex_; +#endif std::unordered_map> deviceInfo_; std::unordered_map deviceSecurityLevel_; }; diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 16fd352b6..0a6e6e5fd 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,11 +26,19 @@ DM_IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); std::atomic g_online{false}; std::atomic g_getLocalDevInfo{false}; DmDeviceInfo localDeviceInfo_; +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +ffrt::mutex localDevInfoMutex_; +#else std::mutex localDevInfoMutex_; +#endif void SoftbusCache::SaveLocalDeviceInfo() { LOGI("start"); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(localDevInfoMutex_); +#else std::lock_guard mutexLock(localDevInfoMutex_); +#endif if (g_online) { return; } @@ -51,7 +59,11 @@ void SoftbusCache::SaveLocalDeviceInfo() void SoftbusCache::DeleteLocalDeviceInfo() { LOGI("networkid %{public}s.", GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(localDevInfoMutex_); +#else std::lock_guard mutexLock(localDevInfoMutex_); +#endif g_online = false; g_getLocalDevInfo = false; if (memset_s(&localDeviceInfo_, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo)) != DM_OK) { @@ -62,7 +74,11 @@ void SoftbusCache::DeleteLocalDeviceInfo() int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(localDevInfoMutex_); +#else std::lock_guard mutexLock(localDevInfoMutex_); +#endif if (g_getLocalDevInfo) { nodeInfo = localDeviceInfo_; LOGD("dm cache success."); @@ -92,7 +108,11 @@ void SoftbusCache::UpDataLocalDevInfo() LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); return; } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(localDevInfoMutex_); +#else std::lock_guard mutexLock(localDevInfoMutex_); +#endif ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); ChangeDeviceInfo(localDeviceInfo_); } @@ -146,7 +166,11 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) LOGE("SaveDeviceInfo copy deviceId failed."); return; } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif CHECK_SIZE_VOID(deviceInfo_); deviceInfo_[udid] = std::pair(uuid, deviceInfo); LOGI("success udid %{public}s, networkId %{public}s", @@ -156,7 +180,11 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) { LOGI("networkId %{public}s", GetAnonyString(std::string(nodeInfo.networkId)).c_str()); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(nodeInfo.networkId)) { LOGI("success udid %{public}s", GetAnonyString(item.first).c_str()); @@ -168,7 +196,11 @@ void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) void SoftbusCache::DeleteDeviceInfo() { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif deviceInfo_.clear(); } @@ -177,7 +209,11 @@ void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) LOGI("start"); std::string udid = ""; GetUdidByNetworkId(deviceInfo.networkId, udid); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif if (deviceInfo_.find(udid) != deviceInfo_.end()) { if (memcpy_s(deviceInfo_[udid].second.deviceName, sizeof(deviceInfo_[udid].second.deviceName), deviceInfo.deviceName, sizeof(deviceInfo.deviceName)) != DM_OK) { @@ -200,7 +236,11 @@ void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) int32_t SoftbusCache::GetDeviceInfoFromCache(std::vector &deviceInfoList) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { continue; @@ -234,7 +274,11 @@ void SoftbusCache::UpdateDeviceInfoCache() int32_t SoftbusCache::GetUdidFromCache(const char *networkId, std::string &udid) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(networkId)) { udid = item.first; @@ -254,7 +298,11 @@ int32_t SoftbusCache::GetUdidFromCache(const char *networkId, std::string &udid) int32_t SoftbusCache::GetUuidFromCache(const char *networkId, std::string &uuid) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(networkId)) { uuid = item.second.first; @@ -302,7 +350,11 @@ int32_t SoftbusCache::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeIn void SoftbusCache::SaveDeviceSecurityLevel(const char *networkId) { LOGI("networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceSecurityLevelMutex_); +#else std::lock_guard mutexLock(deviceSecurityLevelMutex_); +#endif if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { return; } @@ -319,7 +371,11 @@ void SoftbusCache::SaveDeviceSecurityLevel(const char *networkId) void SoftbusCache::DeleteDeviceSecurityLevel(const char *networkId) { LOGI("networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceSecurityLevelMutex_); +#else std::lock_guard mutexLock(deviceSecurityLevelMutex_); +#endif if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { deviceSecurityLevel_.erase(std::string(networkId)); } @@ -327,7 +383,11 @@ void SoftbusCache::DeleteDeviceSecurityLevel(const char *networkId) int32_t SoftbusCache::GetSecurityDeviceLevel(const char *networkId, int32_t &securityLevel) { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceSecurityLevelMutex_); +#else std::lock_guard mutexLock(deviceSecurityLevelMutex_); +#endif for (const auto &item : deviceSecurityLevel_) { if (item.first == std::string(networkId)) { securityLevel = item.second; @@ -363,7 +423,11 @@ int32_t SoftbusCache::GetDevLevelFromBus(const char *networkId, int32_t &securit int32_t SoftbusCache::GetDevInfoByNetworkId(const std::string &networkId, DmDeviceInfo &nodeInfo) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == networkId) { nodeInfo = item.second.second; @@ -407,7 +471,11 @@ int32_t SoftbusCache::GetDevInfoFromBus(const std::string &networkId, DmDeviceIn int32_t SoftbusCache::GetUdidByUdidHash(const std::string &udidHash, std::string &udid) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { if (std::string(item.second.second.deviceId) == udidHash) { udid = item.first; @@ -423,7 +491,11 @@ int32_t SoftbusCache::GetUdidByUdidHash(const std::string &udidHash, std::string int32_t SoftbusCache::GetUuidByUdid(const std::string &udid, std::string &uuid) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif if (deviceInfo_.find(udid) != deviceInfo_.end()) { uuid = deviceInfo_[udid].first; LOGI("success uuid %{public}s.", GetAnonyString(uuid).c_str()); @@ -437,7 +509,11 @@ int32_t SoftbusCache::GetUuidByUdid(const std::string &udid, std::string &uuid) int32_t SoftbusCache::GetNetworkIdFromCache(const std::string &udid, std::string &networkId) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif if (deviceInfo_.find(udid) != deviceInfo_.end()) { networkId = deviceInfo_[udid].second.networkId; LOGI("success networkId %{public}s, udid %{public}s.", @@ -452,7 +528,11 @@ int32_t SoftbusCache::GetNetworkIdFromCache(const std::string &udid, std::string int32_t SoftbusCache::GetDeviceNameFromCache(const std::string &udid, std::string &deviceName) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif if (deviceInfo_.find(udid) != deviceInfo_.end()) { deviceName = deviceInfo_[udid].second.deviceName; LOGI("success deviceName: %{public}s, udid: %{public}s.", @@ -467,7 +547,11 @@ int32_t SoftbusCache::GetDeviceNameFromCache(const std::string &udid, std::strin bool SoftbusCache::CheckIsOnline(const std::string &udidHash) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif for (const auto &item : deviceInfo_) { LOGI("deviceId %{public}s, cache udidHash %{public}s.", GetAnonyString(udidHash).c_str(), GetAnonyString(std::string(item.second.second.deviceId)).c_str()); @@ -483,7 +567,11 @@ bool SoftbusCache::CheckIsOnline(const std::string &udidHash) bool SoftbusCache::CheckIsOnlineByPeerUdid(const std::string &peerUdid) { { +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::lock_guard mutexLock(deviceInfosMutex_); +#else std::lock_guard mutexLock(deviceInfosMutex_); +#endif if (deviceInfo_.find(peerUdid) != deviceInfo_.end()) { LOGI("peerUdid %{public}s is online.", GetAnonyString(peerUdid).c_str()); return true; diff --git a/test/softbusunittest/UTTest_softbus_listener.cpp b/test/softbusunittest/UTTest_softbus_listener.cpp index b68adb900..3131b49c6 100644 --- a/test/softbusunittest/UTTest_softbus_listener.cpp +++ b/test/softbusunittest/UTTest_softbus_listener.cpp @@ -1303,7 +1303,7 @@ HWTEST_F(SoftbusListenerTest, SetLocalDisplayName_001, testing::ext::TestSize.Le softbusListener = std::make_shared(); } int32_t ret = softbusListener->SetLocalDisplayName(displayName); - EXPECT_EQ(ret, DM_OK); + EXPECT_NE(ret, DM_OK); std::vector onlineDevInfoVec; EXPECT_CALL(*softbusCacheMock_, GetDeviceInfoFromCache(_)).Times(::testing::AtLeast(1)) .WillOnce(DoAll(SetArgReferee<0>(onlineDevInfoVec), Return(DM_OK))); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 02d5243d7..6fce5adcc 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1456,6 +1456,7 @@ ohos_unittest("UTTest_device_manager_service_listener") { "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", "dsoftbus:softbus_client", + "ffrt:libffrt", "eventhandler:libeventhandler", "googletest:gmock", "googletest:gmock_main", diff --git a/test/unittest/UTTest_dm_softbus_cache.cpp b/test/unittest/UTTest_dm_softbus_cache.cpp index 8f35ee466..346c55223 100644 --- a/test/unittest/UTTest_dm_softbus_cache.cpp +++ b/test/unittest/UTTest_dm_softbus_cache.cpp @@ -84,7 +84,7 @@ HWTEST_F(DMSoftbusCacheTest, GetDeviceInfoFromCache_001, testing::ext::TestSize. .deviceTypeId = 1 }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } @@ -105,7 +105,7 @@ HWTEST_F(DMSoftbusCacheTest, CheckIsOnline_001, testing::ext::TestSize.Level1) .deviceTypeId = 1, }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } @@ -123,7 +123,7 @@ HWTEST_F(DMSoftbusCacheTest, GetUuidByUdid_001, testing::ext::TestSize.Level1) .networkId = "networkid" }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } @@ -145,7 +145,7 @@ HWTEST_F(DMSoftbusCacheTest, GetNetworkIdFromCache_001, testing::ext::TestSize.L .networkId = "networkid" }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } @@ -168,7 +168,7 @@ HWTEST_F(DMSoftbusCacheTest, GetUdidByUdidHash_001, testing::ext::TestSize.Level }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } @@ -190,7 +190,7 @@ HWTEST_F(DMSoftbusCacheTest, GetDevInfoByNetworkId_001, testing::ext::TestSize.L .networkId = "networkid" }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } @@ -205,7 +205,7 @@ HWTEST_F(DMSoftbusCacheTest, GetDevInfoByNetworkId_001, testing::ext::TestSize.L HWTEST_F(DMSoftbusCacheTest, GetSecurityDeviceLevel_001, testing::ext::TestSize.Level1) { { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceSecurityLevelMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceSecurityLevelMutex_); SoftbusCache::GetInstance().deviceSecurityLevel_.clear(); SoftbusCache::GetInstance().deviceSecurityLevel_["networkid"] = 1; } @@ -216,7 +216,7 @@ HWTEST_F(DMSoftbusCacheTest, GetSecurityDeviceLevel_001, testing::ext::TestSize. .WillOnce(Return(ERR_DM_FAILED)); EXPECT_NE(SoftbusCache::GetInstance().GetSecurityDeviceLevel("test", securityLevel), DM_OK); { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceSecurityLevelMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceSecurityLevelMutex_); SoftbusCache::GetInstance().deviceSecurityLevel_.clear(); } } @@ -241,7 +241,7 @@ HWTEST_F(DMSoftbusCacheTest, GetUuidFromCache_001, testing::ext::TestSize.Level1 }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } std::string uuid = ""; @@ -260,7 +260,7 @@ HWTEST_F(DMSoftbusCacheTest, GetUdidFromCache_001, testing::ext::TestSize.Level1 }; { - std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); + std::lock_guard mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_); SoftbusCache::GetInstance().deviceInfo_.clear(); SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair("uuid", deviceInfo); } -- Gitee