From b85da2223722402e10d5879bc627131c7ff25058 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Wed, 5 Mar 2025 09:22:03 +0800 Subject: [PATCH] time optimize for quick mirrow Signed-off-by: zhuzhihui7 Change-Id: I109b61562d8975fd50f66f3f59e78039666eeadf --- .../include/deviceprofile_connector.h | 6 +- .../src/deviceprofile_connector.cpp | 16 ++--- .../include/authentication/dm_auth_manager.h | 6 ++ .../authentication/auth_message_processor.cpp | 6 +- .../src/authentication/dm_auth_manager.cpp | 60 +++++++++++++++++-- .../UTTest_dm_auth_manager_first.cpp | 7 ++- .../mock/deviceprofile_connector_mock.cpp | 4 +- .../mock/deviceprofile_connector_mock.h | 4 +- 8 files changed, 84 insertions(+), 25 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 30869a252..1547a55da 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -184,9 +184,9 @@ public: DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile); int32_t GetServiceInfoProfileListByTokenId(const DistributedDeviceProfile::ServiceInfoUniqueKey &key, std::vector &serviceInfoProfiles); - int32_t GetServiceInfoProfileListByBundleName(const DistributedDeviceProfile::ServiceInfoUniqueKey& key, - std::vector& serviceInfoProfiles); - int32_t PutSessionKey(const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId); + int32_t GetServiceInfoProfileListByBundleName(const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &serviceInfoProfiles); + int32_t PutSessionKey(const std::vector &sessionKeyArray, int32_t &sessionKeyId); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 63207c775..d625a2023 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1807,8 +1807,8 @@ int32_t DeviceProfileConnector::GetServiceInfoProfileListByTokenId( } int32_t DeviceProfileConnector::GetServiceInfoProfileListByBundleName( - const DistributedDeviceProfile::ServiceInfoUniqueKey& key, - std::vector& serviceInfoProfiles) + const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &serviceInfoProfiles) { int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByBundleName( key, serviceInfoProfiles); @@ -1819,19 +1819,13 @@ int32_t DeviceProfileConnector::GetServiceInfoProfileListByBundleName( return DM_OK; } -int32_t DeviceProfileConnector::PutSessionKey( - const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId) +int32_t DeviceProfileConnector::PutSessionKey(const std::vector &sessionKeyArray, int32_t &sessionKeyId) { - if (sessionKey == nullptr) { - LOGE("sessionKey nullptr"); - return ERR_DM_FAILED; - } - if (length > MAX_SESSION_KEY_LENGTH) { - LOGE("SessionKey too long, len: %{public}d", length); + if (sessionKeyArray.size() > MAX_SESSION_KEY_LENGTH) { + LOGE("SessionKey too long"); return ERR_DM_FAILED; } uint32_t userId = static_cast(MultipleUserConnector::GetCurrentAccountUserID()); - std::vector sessionKeyArray(sessionKey, sessionKey + length); int32_t ret = DistributedDeviceProfileClient::GetInstance().PutSessionKey(userId, sessionKeyArray, sessionKeyId); if (ret != DM_OK) { LOGE("failed: %{public}d", ret); diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 5d9135d4b..7380cdf75 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -484,6 +484,7 @@ public: static bool IsPinCodeValid(const std::string strpin); static bool IsPinCodeValid(int32_t numpin); bool IsImportedAuthCodeValid(); + bool IsSrc(); private: bool IsHmlSessionType(); @@ -536,6 +537,7 @@ public: void AuthDeviceError(int64_t requestId, int32_t errorCode); void GetRemoteDeviceId(std::string &deviceId); void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen); + int32_t GetSessionKeyIdSync(int64_t requestId); void OnAuthDeviceDataReceived(const int32_t sessionId, const std::string message); void OnScreenLocked(); void HandleDeviceNotTrust(const std::string &udid); @@ -551,6 +553,7 @@ private: const std::string &extra); void ParseJsonObject(nlohmann::json jsonObject); void ParseHmlInfoInJsonObject(nlohmann::json jsonObject); + void PutSessionKeyAsync(int64_t requestId, std::vector hash); int32_t DeleteAcl(const std::string &pkgName, const std::string &localUdid, const std::string &remoteUdid, int32_t bindLevel, const std::string &extra); void ProcessAuthRequestExt(const int32_t &sessionId); @@ -624,6 +627,9 @@ private: int32_t authenticationType_ = USER_OPERATION_TYPE_ALLOW_AUTH; DistributedDeviceProfile::ServiceInfoProfile serviceInfoProfile_; bool pincodeDialogEverShown_ = false; + std::mutex sessionKeyIdMutex_; + std::condition_variable sessionKeyIdCondition_; + std::map> sessionKeyIdAsyncResult_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index e6a49044f..ace746cda 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -230,7 +230,11 @@ void AuthMessageProcessor::CreatePublicKeyMessageExt(nlohmann::json &json) return; } else { nlohmann::json jsonTemp; - jsonTemp[TAG_SESSIONKEY_ID] = authResponseContext_->localSessionKeyId; + auto sptr = authMgr_.lock(); + if (sptr != nullptr && !sptr->IsSrc()) { + authResponseContext_->localSessionKeyId = sptr->GetSessionKeyIdSync(authResponseContext_->requestId); + jsonTemp[TAG_SESSIONKEY_ID] = authResponseContext_->localSessionKeyId; + } jsonTemp[TAG_PUBLICKEY] = authResponseContext_->publicKey; std::string strTemp = SafetyDump(jsonTemp); std::string encryptStr = ""; diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 04d2d8f37..616563652 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -82,6 +82,7 @@ const int32_t AUTH_DEVICE_TIMEOUT = 10; const int32_t SESSION_HEARTBEAT_TIMEOUT = 50; const int32_t ALREADY_BIND = 1; const int32_t STRTOLL_BASE_10 = 10; +const int32_t MAX_PUT_SESSIONKEY_TIMEOUT = 100; //ms constexpr const char* AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate"; constexpr const char* NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate"; @@ -1465,6 +1466,8 @@ void DmAuthManager::AuthenticateFinish() std::lock_guard lock(srcReqMsgLock_); srcReqMsg_ = ""; isNeedProcCachedSrcReqMsg_ = false; + std::lock_guard guard(sessionKeyIdMutex_); + sessionKeyIdAsyncResult_.clear(); } pincodeDialogEverShown_ = false; serviceInfoProfile_ = {}; @@ -2180,6 +2183,15 @@ bool DmAuthManager::IsImportedAuthCodeValid() return false; } +bool DmAuthManager::IsSrc() +{ + if (authRequestState_ != nullptr) { + return true; + } else { + return false; + } +} + bool DmAuthManager::IsAuthTypeSupported(const int32_t &authType) { if (authenticationMap_.find(authType) == authenticationMap_.end()) { @@ -2480,13 +2492,52 @@ void DmAuthManager::AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessi return; } authResponseContext_->localSessionKeyId = 0; + { + std::lock_guard guard(sessionKeyIdMutex_); + sessionKeyIdAsyncResult_.clear(); + sessionKeyIdAsyncResult_[requestId] = std::optional(); + } unsigned char hash[SHA256_DIGEST_LENGTH] = { 0 }; Crypto::DmGenerateStrHash(sessionKey, sessionKeyLen, hash, SHA256_DIGEST_LENGTH, 0); - int32_t sessionKeyId = 0; - ret = DeviceProfileConnector::GetInstance().PutSessionKey(hash, SHA256_DIGEST_LENGTH, sessionKeyId); - if (ret == DM_OK && sessionKeyId > 0) { - authResponseContext_->localSessionKeyId = sessionKeyId; + std::vector hashVector(hash, hash + SHA256_DIGEST_LENGTH); + std::shared_ptr sharePtrThis = shared_from_this(); + auto asyncTaskFunc = [sharePtrThis, requestId, hashVector]() { + sharePtrThis->PutSessionKeyAsync(requestId, hashVector); + }; + ffrt::submit(asyncTaskFunc, ffrt::task_attr().delay(0)); +} + +void DmAuthManager::PutSessionKeyAsync(int64_t requestId, std::vector hash) +{ + { + std::lock_guard guard(sessionKeyIdMutex_); + int32_t sessionKeyId = 0; + int32_t ret = DeviceProfileConnector::GetInstance().PutSessionKey(hash, sessionKeyId); + if (ret != DM_OK) { + LOGI("PutSessionKey failed."); + sessionKeyId = 0; + } + sessionKeyIdAsyncResult_[requestId] = sessionKeyId; + } + sessionKeyIdCondition_.notify_one(); +} + +int32_t DmAuthManager::GetSessionKeyIdSync(int64_t requestId) +{ + std::unique_lock guard(sessionKeyIdMutex_); + if (sessionKeyIdAsyncResult_.find(requestId) == sessionKeyIdAsyncResult_.end()) { + LOGW("GetSessionKeyIdSync failed, not find by requestId"); + return 0; + } + if (sessionKeyIdAsyncResult_[requestId].has_value()) { + LOGI("GetSessionKeyIdSync, already ready"); + return sessionKeyIdAsyncResult_[requestId].value(); } + LOGI("GetSessionKeyIdSync need wait"); + sessionKeyIdCondition_.wait_for(guard, std::chrono::milliseconds(MAX_PUT_SESSIONKEY_TIMEOUT)); + int32_t keyid = sessionKeyIdAsyncResult_[requestId].value_or(0); + LOGI("GetSessionKeyIdSync exit"); + return keyid; } void DmAuthManager::GetRemoteDeviceId(std::string &deviceId) @@ -3230,6 +3281,7 @@ void DmAuthManager::JoinLnn(const std::string &deviceId, bool isForceJoin) CHECK_NULL_VOID(authResponseContext_); CHECK_NULL_VOID(softbusConnector_); if (IsHmlSessionType()) { + authResponseContext_->localSessionKeyId = GetSessionKeyIdSync(authResponseContext_->requestId); softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId, authResponseContext_->localSessionKeyId, authResponseContext_->remoteSessionKeyId); return; diff --git a/test/commonunittest/UTTest_dm_auth_manager_first.cpp b/test/commonunittest/UTTest_dm_auth_manager_first.cpp index 1b8f73341..eb246dde7 100644 --- a/test/commonunittest/UTTest_dm_auth_manager_first.cpp +++ b/test/commonunittest/UTTest_dm_auth_manager_first.cpp @@ -1010,6 +1010,7 @@ HWTEST_F(DmAuthManagerTest, AuthDeviceError005, testing::ext::TestSize.Level0) authManager_->AuthDeviceError(requestId, errorCode); authManager_->AuthDeviceSessionKey(requestId, nullptr, sessionKeyLen); ASSERT_EQ(authManager_->isAuthDevice_, false); + authManager_->GetSessionKeyIdSync(requestId); } HWTEST_F(DmAuthManagerTest, AuthDeviceError006, testing::ext::TestSize.Level0) @@ -1033,6 +1034,7 @@ HWTEST_F(DmAuthManagerTest, AuthDeviceSessionKey001, testing::ext::TestSize.Leve uint32_t sessionKeyLen = 0; authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); ASSERT_EQ(authManager_->isAuthDevice_, false); + authManager_->GetSessionKeyIdSync(requestId); } HWTEST_F(DmAuthManagerTest, GetRemoteDeviceId001, testing::ext::TestSize.Level0) @@ -2060,9 +2062,10 @@ HWTEST_F(DmAuthManagerTest, CheckNeedShowAuthInfoDialog_001, testing::ext::TestS authManager_->authResponseContext_->requestId = 1; authManager_->authMessageProcessor_ = std::make_shared(authManager_); EXPECT_CALL(*cryptoMgrMock_, SaveSessionKey(_, _)).WillOnce(Return(DM_OK)); - EXPECT_CALL(*deviceProfileConnectorMock_, PutSessionKey(_, _, _)) - .WillOnce(DoAll(SetArgReferee<2>(1), Return(DM_OK))); + EXPECT_CALL(*deviceProfileConnectorMock_, PutSessionKey(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); + authManager_->GetSessionKeyIdSync(requestId); } HWTEST_F(DmAuthManagerTest, IsPinCodeValid_001, testing::ext::TestSize.Level0) diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index 15a6be1e2..be3dd7f8d 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -126,9 +126,9 @@ int32_t DeviceProfileConnector::GetServiceInfoProfileListByBundleName( serviceInfoProfiles); } -int32_t DeviceProfileConnector::PutSessionKey(const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId) +int32_t DeviceProfileConnector::PutSessionKey(const std::vector &sessionKeyArray, int32_t &sessionKeyId) { - return DmDeviceProfileConnector::dmDeviceProfileConnector->PutSessionKey(sessionKey, length, sessionKeyId); + return DmDeviceProfileConnector::dmDeviceProfileConnector->PutSessionKey(sessionKeyArray, sessionKeyId); } } // 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 f6096c8b3..fc11aeb8d 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -56,7 +56,7 @@ public: virtual int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId) = 0; virtual int32_t GetServiceInfoProfileListByBundleName(const DistributedDeviceProfile::ServiceInfoUniqueKey& key, std::vector& serviceInfoProfiles) = 0; - virtual int32_t PutSessionKey(const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId) = 0; + virtual int32_t PutSessionKey(const std::vector &sessionKeyArray, int32_t &sessionKeyId) = 0; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -87,7 +87,7 @@ public: MOCK_METHOD(int32_t, CheckDeviceInfoPermission, (const std::string &, const std::string &)); MOCK_METHOD(int32_t, GetServiceInfoProfileListByBundleName, (const DistributedDeviceProfile::ServiceInfoUniqueKey&, (std::vector&))); - MOCK_METHOD(int32_t, PutSessionKey, (const uint8_t*, uint32_t, int32_t&)); + MOCK_METHOD(int32_t, PutSessionKey, (const std::vector &, int32_t&)); }; } } -- Gitee