From 2a72d9da720d871bddf4b0846107630409de85ee Mon Sep 17 00:00:00 2001 From: renguang1116 Date: Mon, 6 Jun 2022 15:05:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E5=A4=87=E4=BF=A1?= =?UTF-8?q?=E4=BB=BB=E5=91=A8=E6=9C=9F=E8=B6=85=E6=9C=9F=E5=90=8E=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E9=87=8D=E6=96=B0PIN=E7=A0=81=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20Signed-off-by:=20renguang1116=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/include/dm_constants.h | 4 +- .../include/authentication/dm_auth_manager.h | 4 +- .../dependency/hichain/hichain_connector.h | 2 +- .../include/dependency/timer/dm_timer.h | 129 +++++++---- .../devicestate/dm_device_state_manager.h | 3 +- .../include/discovery/dm_discovery_manager.h | 4 +- .../src/authentication/dm_auth_manager.cpp | 145 +++++------- .../dependency/hichain/hichain_connector.cpp | 28 ++- .../src/dependency/timer/dm_timer.cpp | 219 +++++++----------- .../devicestate/dm_device_state_manager.cpp | 37 ++- .../src/discovery/dm_discovery_manager.cpp | 43 ++-- test/unittest/UTTest_auth_request_state.cpp | 19 +- test/unittest/UTTest_auth_response_state.cpp | 1 + test/unittest/UTTest_dm_auth_manager.cpp | 14 +- test/unittest/UTTest_dm_discovery_manager.cpp | 14 -- 15 files changed, 301 insertions(+), 365 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index e497dd87..d36b557d 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -187,8 +187,8 @@ const std::string ETH_PORT = "ETH_PORT"; // ACE const int32_t ACE_X = 50; const int32_t ACE_Y = 300; -const int32_t ACE_WIDTH = 600; -const int32_t ACE_HEIGHT = 400; +const int32_t ACE_WIDTH = 580; +const int32_t ACE_HEIGHT = 520; const std::string EVENT_CONFIRM = "EVENT_CONFIRM"; const std::string EVENT_CANCEL = "EVENT_CANCEL"; const std::string EVENT_INIT = "EVENT_INIT"; diff --git a/services/devicemanagerservice/include/authentication/dm_auth_manager.h b/services/devicemanagerservice/include/authentication/dm_auth_manager.h index 799e61e3..3ca7ff45 100644 --- a/services/devicemanagerservice/include/authentication/dm_auth_manager.h +++ b/services/devicemanagerservice/include/authentication/dm_auth_manager.h @@ -153,7 +153,7 @@ public: int32_t SetAuthResponseState(std::shared_ptr authResponseState); int32_t GetPinCode(); std::string GenerateGroupName(); - int32_t HandleAuthenticateTimeout(); + void HandleAuthenticateTimeout(std::string name); void CancelDisplay(); int32_t GeneratePincode(); void ShowConfigDialog(); @@ -176,7 +176,7 @@ private: std::shared_ptr authRequestContext_; std::shared_ptr authResponseContext_; std::shared_ptr authMessageProcessor_; - std::map> timerMap_; + std::shared_ptr timer_; std::shared_ptr dmAbilityMgr_; bool isCryptoSupport_ = false; bool isFinishOfLocal_ = true; diff --git a/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h b/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h index b8aa3708..b6830955 100644 --- a/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h +++ b/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h @@ -62,7 +62,7 @@ public: int32_t DeleteGroup(const int32_t userId, std::string &groupId); bool IsDevicesInGroup(std::string hostDevice, std::string peerDevice); int32_t GetRelatedGroups(std::string DeviceId, std::vector &groupList); - int32_t GetGroupInfo(std::string queryParams, std::vector &groupList); + bool GetGroupInfo(const std::string &queryParams, std::vector &groupList); int32_t GetGroupInfo(const int32_t userId, std::string queryParams, std::vector &groupList); int32_t DeleteTimeOutGroup(const char* deviceId); private: diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index dd6eeaa1..b7401804 100644 --- a/services/devicemanagerservice/include/dependency/timer/dm_timer.h +++ b/services/devicemanagerservice/include/dependency/timer/dm_timer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,67 +13,94 @@ * limitations under the License. */ -#ifndef TIMER_H -#define TIMER_H -#include -#include -#include -#if !defined(__LITEOS_M__) -#include -#include -#include -#endif -#include -#include -#include +#ifndef DM_TIMER_H +#define DM_TIMER_H -#include "dm_log.h" +#include +#include +#include +#include +#include +#include +#include +#include namespace OHOS { namespace DistributedHardware { -class DmTimer; -typedef void (*TimeoutHandle)(void *data, DmTimer& timer); +constexpr int32_t DELAY_TICK_MILLSECONDS = 1000; +typedef std::chrono::steady_clock::time_point timerPoint; +typedef std::chrono::steady_clock steadyClock; +typedef std::chrono::duration> timerDuration; +using TimerCallback = std::function; +const int32_t MILLISECOND_TO_SECOND = 1000; +const int32_t MIN_TIME_OUT = 0; +const int32_t MAX_TIME_OUT = 300; -#define MAX_EVENTS 255 +class Timer { +public: + Timer(std::string name, int32_t time, TimerCallback callback); + ~Timer() {}; +public: + std::string timerName_; + timerPoint expire_; + bool state_; + int32_t timeOut_; + TimerCallback callback_; +}; -enum DmTimerStatus : int32_t { - DM_STATUS_INIT = 0, - DM_STATUS_RUNNING = 1, - DM_STATUS_BUSY = 2, - DM_STATUS_CREATE_ERROR = 3, - DM_STATUS_FINISH = 6, +struct TimerCmpare { +public: + bool operator () (std::shared_ptr frontTimer, std::shared_ptr timer) + { + int32_t frontTimerOut = frontTimer->timeOut_ - (std::chrono::duration_cast(steadyClock::now() + - frontTimer->expire_).count() / MILLISECOND_TO_SECOND); + int32_t timerOut = timer->timeOut_ - (std::chrono::duration_cast(steadyClock::now() + - timer->expire_).count() / MILLISECOND_TO_SECOND); + return frontTimerOut > timerOut; + } }; class DmTimer { public: - DmTimer(const std::string &name); + DmTimer(); ~DmTimer(); - DmTimerStatus Start(uint32_t timeOut, TimeoutHandle handle, void *data); - void Stop(int32_t code); - void WaitForTimeout(); - std::string GetTimerName(); -private: - int32_t CreateTimeFd(); - void Release(); -private: - DmTimerStatus mStatus_; - uint32_t mTimeOutSec_; - TimeoutHandle mHandle_; - void *mHandleData_; - int32_t mTimeFd_[2]; -#if defined(__LITEOS_M__) - void *timerId = NULL; -#else - struct epoll_event mEv_; - struct epoll_event mEvents_[MAX_EVENTS]; - int32_t mEpFd_; - std::thread mThread_; - std::mutex mTimerLock_; -#endif + /** + * @tc.name: DmTimer::StartTimer + * @tc.desc: start timer running + * @tc.type: FUNC + */ + int32_t StartTimer(std::string name, int32_t time, TimerCallback callback); - std::string mTimerName_; + /** + * @tc.name: DmTimer::DeleteTimer + * @tc.desc: delete timer + * @tc.type: FUNC + */ + int32_t DeleteTimer(std::string timerName); + + /** + * @tc.name: DmTimer::DeleteAll + * @tc.desc: delete all timer + * @tc.type: FUNC + */ + int32_t DeleteAll(); + + /** + * @tc.name: DmTimer::TimerRunning + * @tc.desc: timer running + * @tc.type: FUNC + */ + int32_t TimerRunning(); +private: + mutable std::mutex timerMutex_; + mutable std::mutex timerStateMutex_; + std::priority_queue, std::vector>, TimerCmpare> timerQueue_; + std::map> timerMap_; + std::atomic timerState_ {false}; + std::condition_variable runTimerCondition_; + std::condition_variable stopTimerCondition_; }; -} // namespace DistributedHardware -} // namespace OHOS -#endif +} +} +#endif \ No newline at end of file diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h index 759c6abf..ed845095 100755 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -58,7 +58,7 @@ public: int32_t RegisterSoftbusStateCallback(); void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo); void StartOffLineTimer(const DmDeviceInfo &deviceInfo); - void DeleteTimeOutGroup(std::string stateTimer); + void DeleteTimeOutGroup(std::string name); void RegisterDevStateCallback(const std::string &pkgName, const std::string &extra); void UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra); @@ -74,6 +74,7 @@ private: std::map remoteDeviceInfos_; std::map decisionInfos_; std::map stateTimerInfoMap_; + std::shared_ptr timer_; std::shared_ptr hiChainConnector_; std::string decisionSoName_; }; diff --git a/services/devicemanagerservice/include/discovery/dm_discovery_manager.h b/services/devicemanagerservice/include/discovery/dm_discovery_manager.h index c7381387..62a535c8 100644 --- a/services/devicemanagerservice/include/discovery/dm_discovery_manager.h +++ b/services/devicemanagerservice/include/discovery/dm_discovery_manager.h @@ -41,14 +41,14 @@ public: void OnDeviceFound(const std::string &pkgName, const DmDeviceInfo &info); void OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId); void OnDiscoveryFailed(const std::string &pkgName, int32_t subscribeId, int32_t failedReason); - void HandleDiscoveryTimeout(); + void HandleDiscoveryTimeout(std::string name); private: std::shared_ptr softbusConnector_; std::shared_ptr listener_; std::queue discoveryQueue_; std::map discoveryContextMap_; - std::shared_ptr discoveryTimer_; + std::shared_ptr timer_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index e38be895..58d93d22 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -41,23 +41,6 @@ const int32_t WAIT_REQUEST_TIMEOUT = 10; const int32_t CANCEL_PIN_CODE_DISPLAY = 1; const int32_t DEVICE_ID_HALF = 2; -static void TimeOut(void *data, DmTimer& timer) -{ - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("time out is not our timer"); - return; - } - - DmAuthManager *authMgr = (DmAuthManager *)data; - if (authMgr == nullptr) { - LOGE("authMgr is nullptr"); - return; - } - - authMgr->HandleAuthenticateTimeout(); -} - DmAuthManager::DmAuthManager(std::shared_ptr softbusConnector, std::shared_ptr listener, std::shared_ptr hiChainConnector) @@ -101,6 +84,14 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au return DM_INPUT_PARA_EMPTY; } + authPtr_ = authenticationMap_[authType]; + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + timer_->StartTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); authMessageProcessor_ = std::make_shared(shared_from_this()); authResponseContext_ = std::make_shared(); authRequestContext_ = std::make_shared(); @@ -131,9 +122,6 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au authRequestState_->SetAuthManager(shared_from_this()); authRequestState_->SetAuthContext(authRequestContext_); authRequestState_->Enter(); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; - authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this); LOGI("DmAuthManager::AuthenticateDevice complete"); return DM_OK; } @@ -174,12 +162,11 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam) return DM_AUTH_NOT_START; } std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end() - || timerMap_.find(INPUT_TIMEOUT_TASK) == timerMap_.end()) { + if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { LOGE("DmAuthManager::authenticationMap_ is null"); return DM_FAILED; } - timerMap_[INPUT_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timer_->DeleteTimer(INPUT_TIMEOUT_TASK); ptr = authenticationMap_[authResponseContext_->authType]; int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam); switch (ret) { @@ -210,12 +197,15 @@ void DmAuthManager::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int3 authResponseState_->SetAuthManager(shared_from_this()); authResponseState_->Enter(); authResponseContext_ = std::make_shared(); - std::shared_ptr waitStartTimer = std::make_shared(WAIT_NEGOTIATE_TIMEOUT_TASK); - timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] = waitStartTimer; - waitStartTimer->Start(WAIT_NEGOTIATE_TIMEOUT, TimeOut, this); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; - authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this); + timer_ = std::make_shared(); + timer_->StartTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); + timer_->StartTimer(WAIT_NEGOTIATE_TIMEOUT_TASK, WAIT_NEGOTIATE_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); } else { std::shared_ptr authMessageProcessor = std::make_shared(shared_from_this()); @@ -294,18 +284,16 @@ void DmAuthManager::OnDataReceived(int32_t sessionId, std::string message) switch (authResponseContext_->msgType) { case MSG_TYPE_NEGOTIATE: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT - && timerMap_.find(WAIT_NEGOTIATE_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT) { + timer_->DeleteTimer(WAIT_NEGOTIATE_TIMEOUT_TASK); authResponseState_->TransitionTo(std::make_shared()); } else { LOGE("Device manager auth state error"); } break; case MSG_TYPE_REQ_AUTH: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE - && timerMap_.find(WAIT_REQUEST_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[WAIT_REQUEST_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE) { + timer_->DeleteTimer(WAIT_REQUEST_TIMEOUT_TASK); authResponseState_->TransitionTo(std::make_shared()); } else { LOGE("Device manager auth state error"); @@ -359,8 +347,8 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) { LOGE("DmAuthManager OnMemberJoin start"); - if (authRequestState_ != nullptr && timerMap_.find(ADD_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[ADD_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authRequestState_ != nullptr) { + timer_->DeleteTimer(ADD_TIMEOUT_TASK); if (status != DM_OK || authResponseContext_->requestId != requestId) { if (authRequestState_ != nullptr) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; @@ -373,9 +361,9 @@ void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) } } -int32_t DmAuthManager::HandleAuthenticateTimeout() +void DmAuthManager::HandleAuthenticateTimeout(std::string name) { - LOGI("DmAuthManager::HandleAuthenticateTimeout start"); + LOGI("DmAuthManager::HandleAuthenticateTimeout start timer name %s", name.c_str()); if (authRequestState_ != nullptr && authRequestState_->GetStateType() != AuthState::AUTH_REQUEST_FINISH) { if (authResponseContext_ == nullptr) { authResponseContext_ = std::make_shared(); @@ -391,7 +379,6 @@ int32_t DmAuthManager::HandleAuthenticateTimeout() authResponseState_->TransitionTo(std::make_shared()); } LOGI("DmAuthManager::HandleAuthenticateTimeout start complete"); - return DM_OK; } int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId) @@ -419,9 +406,10 @@ void DmAuthManager::StartNegotiate(const int32_t &sessionId) authMessageProcessor_->SetResponseContext(authResponseContext_); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_NEGOTIATE); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); - std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); - timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; - negotiateStartTimer->Start(NEGOTIATE_TIMEOUT, TimeOut, this); + timer_->StartTimer(NEGOTIATE_TIMEOUT_TASK, NEGOTIATE_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); } void DmAuthManager::RespNegotiate(const int32_t &sessionId) @@ -449,7 +437,7 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); } authResponseContext_ = authResponseState_->GetAuthContext(); - if (jsonObject[TAG_CRYPTO_SUPPORT] == "true" && authResponseContext_->cryptoSupport == true) { + if (jsonObject[TAG_CRYPTO_SUPPORT] == true && authResponseContext_->cryptoSupport) { if (jsonObject[TAG_CRYPTO_NAME] == authResponseContext_->cryptoName && jsonObject[TAG_CRYPTO_VERSION] == authResponseContext_->cryptoVer) { isCryptoSupport_ = true; @@ -457,22 +445,20 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) return; } } - jsonObject[TAG_CRYPTO_SUPPORT] = "false"; + jsonObject[TAG_CRYPTO_SUPPORT] = false; message = jsonObject.dump(); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); - std::shared_ptr waitStartTimer = std::make_shared(WAIT_REQUEST_TIMEOUT_TASK); - timerMap_[WAIT_REQUEST_TIMEOUT_TASK] = waitStartTimer; - waitStartTimer->Start(WAIT_REQUEST_TIMEOUT, TimeOut, this); + timer_->StartTimer(WAIT_REQUEST_TIMEOUT_TASK, WAIT_REQUEST_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); } void DmAuthManager::SendAuthRequest(const int32_t &sessionId) { - LOGE("DmAuthManager::EstablishAuthChannel session id"); - if (timerMap_.find(NEGOTIATE_TIMEOUT_TASK) == timerMap_.end()) { - return; - } - timerMap_[NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); - if (authResponseContext_->cryptoSupport == true) { + LOGI("DmAuthManager::EstablishAuthChannel session id"); + timer_->DeleteTimer(NEGOTIATE_TIMEOUT_TASK); + if (authResponseContext_->cryptoSupport) { isCryptoSupport_ = true; } if (authResponseContext_->reply == DM_AUTH_PEER_REJECT) { @@ -483,9 +469,10 @@ void DmAuthManager::SendAuthRequest(const int32_t &sessionId) for (auto msg : messageList) { softbusConnector_->GetSoftbusSession()->SendData(sessionId, msg); } - std::shared_ptr confirmStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); - timerMap_[CONFIRM_TIMEOUT_TASK] = confirmStartTimer; - confirmStartTimer->Start(CONFIRM_TIMEOUT, TimeOut, this); + timer_->StartTimer(CONFIRM_TIMEOUT_TASK, CONFIRM_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); } int32_t DmAuthManager::StartAuthProcess(const int32_t &action) @@ -510,16 +497,14 @@ int32_t DmAuthManager::StartAuthProcess(const int32_t &action) void DmAuthManager::StartRespAuthProcess() { - LOGI("DmAuthManager::StartRespAuthProcess StartRespAuthProcess", authResponseContext_->sessionId); - if (timerMap_.find(CONFIRM_TIMEOUT_TASK) == timerMap_.end()) { - return; - } - timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + LOGI("DmAuthManager::StartRespAuthProcess", authResponseContext_->sessionId); + timer_->DeleteTimer(CONFIRM_TIMEOUT_TASK); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH) { - std::shared_ptr inputStartTimer = std::make_shared(INPUT_TIMEOUT_TASK); - timerMap_[INPUT_TIMEOUT_TASK] = inputStartTimer; - inputStartTimer->Start(INPUT_TIMEOUT, TimeOut, this); - authRequestState_->TransitionTo(std::make_shared()); + timer_->StartTimer(INPUT_TIMEOUT_TASK, INPUT_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); + authRequestState_->TransitionTo(std::make_shared()); } else { LOGE("do not accept"); authResponseContext_->state = AuthState::AUTH_REQUEST_REPLY; @@ -553,9 +538,10 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId) jsonObject[TAG_REQUEST_ID] = authResponseContext_->requestId; jsonObject[TAG_DEVICE_ID] = authResponseContext_->deviceId; std::string connectInfo = jsonObject.dump(); - std::shared_ptr joinStartTimer = std::make_shared(ADD_TIMEOUT_TASK); - timerMap_[ADD_TIMEOUT_TASK] = joinStartTimer; - joinStartTimer->Start(ADD_TIMEOUT, TimeOut, this); + timer_->StartTimer(ADD_TIMEOUT_TASK, ADD_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); int32_t ret = hiChainConnector_->AddMember(deviceId, connectInfo); if (ret != 0) { return DM_FAILED; @@ -581,11 +567,8 @@ std::string DmAuthManager::GetConnectAddr(std::string deviceId) int32_t DmAuthManager::JoinNetwork() { - LOGE("DmAuthManager JoinNetwork start"); - if (timerMap_.find(AUTHENTICATE_TIMEOUT_TASK) == timerMap_.end()) { - return DM_FAILED; - } - timerMap_[AUTHENTICATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + LOGI("DmAuthManager JoinNetwork start"); + timer_->DeleteTimer(AUTHENTICATE_TIMEOUT_TASK); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authRequestState_->TransitionTo(std::make_shared()); @@ -610,12 +593,7 @@ void DmAuthManager::AuthenticateFinish() std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_AUTH_TERMINATE); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); } - if (!timerMap_.empty()) { - for (auto &iter : timerMap_) { - iter.second->Stop(SESSION_CANCEL_TIMEOUT); - } - timerMap_.clear(); - } + timer_->DeleteAll(); isFinishOfLocal_ = true; authResponseContext_ = nullptr; authResponseState_ = nullptr; @@ -640,12 +618,7 @@ void DmAuthManager::AuthenticateFinish() listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId, authRequestContext_->token, authResponseContext_->state, authRequestContext_->reason); softbusConnector_->GetSoftbusSession()->CloseAuthSession(authRequestContext_->sessionId); - if (!timerMap_.empty()) { - for (auto &iter : timerMap_) { - iter.second->Stop(SESSION_CANCEL_TIMEOUT); - } - timerMap_.clear(); - } + timer_->DeleteAll(); isFinishOfLocal_ = true; authRequestContext_ = nullptr; authResponseContext_ = nullptr; diff --git a/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp b/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp index 974b8291..08a29d19 100644 --- a/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp +++ b/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp @@ -139,14 +139,14 @@ bool HiChainConnector::IsGroupCreated(std::string groupName, GroupInfo &groupInf return false; } -int32_t HiChainConnector::GetGroupInfo(std::string queryParams, std::vector &groupList) +bool HiChainConnector::GetGroupInfo(const std::string &queryParams, std::vector &groupList) { char *groupVec = nullptr; uint32_t num = 0; int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); if (userId < 0) { LOGE("get current process account user id failed"); - return DM_FAILED; + return false; } int32_t ret = deviceGroupManager_->getGroupInfo(userId, DM_PKG_NAME.c_str(), queryParams.c_str(), &groupVec, &num); if (ret != 0) { @@ -259,11 +259,15 @@ void HiChainConnector::onFinish(int64_t requestId, int operationCode, const char LOGI("HiChainConnector::onFinish reqId:%lld, operation:%d", requestId, operationCode); if (operationCode == GroupOperationCode::MEMBER_JOIN) { LOGI("Add Member To Group success"); - hiChainConnectorCallback_->OnMemberJoin(requestId, DM_OK); + if (hiChainConnectorCallback_ != nullptr) { + hiChainConnectorCallback_->OnMemberJoin(requestId, DM_OK); + } } if (operationCode == GroupOperationCode::GROUP_CREATE) { LOGI("Create group success"); - hiChainConnectorCallback_->OnGroupCreated(requestId, data); + if (hiChainConnectorCallback_ != nullptr) { + hiChainConnectorCallback_->OnGroupCreated(requestId, data); + } } if (operationCode == GroupOperationCode::MEMBER_DELETE) { LOGI("Delete Member from group success"); @@ -279,11 +283,15 @@ void HiChainConnector::onError(int64_t requestId, int operationCode, int errorCo LOGI("HichainAuthenCallBack::onError reqId:%lld, operation:%d, errorCode:%d.", requestId, operationCode, errorCode); if (operationCode == GroupOperationCode::MEMBER_JOIN) { LOGE("Add Member To Group failed"); - hiChainConnectorCallback_->OnMemberJoin(requestId, DM_FAILED); + if (hiChainConnectorCallback_ != nullptr) { + hiChainConnectorCallback_->OnMemberJoin(requestId, DM_FAILED); + } } if (operationCode == GroupOperationCode::GROUP_CREATE) { LOGE("Create group failed"); - hiChainConnectorCallback_->OnGroupCreated(requestId, "{}"); + if (hiChainConnectorCallback_ != nullptr) { + hiChainConnectorCallback_->OnGroupCreated(requestId, "{}"); + } } if (operationCode == GroupOperationCode::MEMBER_DELETE) { LOGE("Delete Member from group failed"); @@ -299,6 +307,10 @@ char *HiChainConnector::onRequest(int64_t requestId, int operationCode, const ch LOGE("HiChainAuthCallBack::onRequest operationCode %d", operationCode); return nullptr; } + if (hiChainConnectorCallback_ == nullptr) { + LOGE("HiChainAuthCallBack::hiChain connector callback is nullptr"); + return nullptr; + } int32_t pinCode = 0; pinCode = hiChainConnectorCallback_->GetPinCode(); nlohmann::json jsonObj; @@ -324,6 +336,10 @@ int64_t HiChainConnector::GenRequestId() std::string HiChainConnector::GetConnectPara(std::string deviceId, std::string reqDeviceId) { + if (hiChainConnectorCallback_ == nullptr) { + LOGE("GetConnectPara::hiChain connector callback is nullptr"); + return ""; + } std::string connectAddr = ""; connectAddr = hiChainConnectorCallback_->GetConnectAddr(deviceId); LOGE("HiChainConnector::GetConnectPara get addrInfo"); diff --git a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp index 2a4dd138..a0db259b 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,175 +13,122 @@ * limitations under the License. */ +#include "dm_constants.h" +#include "dm_log.h" #include "dm_timer.h" #include -#include "dm_constants.h" -#include "securec.h" - namespace OHOS { namespace DistributedHardware { -namespace { -const int32_t MILL_SECONDS_PER_SECOND = 1000; -} -DmTimer::DmTimer(const std::string &name) -{ - if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer name is null"); - return; - } +Timer::Timer(std::string name, int32_t time, TimerCallback callback) + : timerName_(name), expire_(steadyClock::now()), state_(true), timeOut_(time), callback_(callback) {}; - mStatus_ = DmTimerStatus::DM_STATUS_INIT; - mTimeOutSec_ = 0; - mHandle_ = nullptr; - mHandleData_ = nullptr; - (void)memset_s(mTimeFd_, sizeof(mTimeFd_), 0, sizeof(mTimeFd_)); - (void)memset_s(&mEv_, sizeof(mEv_), 0, sizeof(mEv_)); - (void)memset_s(mEvents_, sizeof(mEvents_), 0, sizeof(mEvents_)); - mEpFd_ = 0; - mTimerName_ = name; +DmTimer::DmTimer() +{ + LOGI("DmTimer constructor"); } DmTimer::~DmTimer() { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; + LOGI("DmTimer destructor"); + DeleteAll(); + if (timerState_) { + std::unique_lock locker(timerStateMutex_); + stopTimerCondition_.wait(locker, [this] { return static_cast(!timerState_); }); } - LOGI("DmTimer %s destroy in", mTimerName_.c_str()); - Stop(0); - - std::lock_guard lock(mTimerLock_); - Release(); } -DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) +int32_t DmTimer::StartTimer(std::string name, int32_t timeOut, TimerCallback callback) { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT - || handle == nullptr || data == nullptr) { - LOGE("DmTimer is not init or param empty"); - return DmTimerStatus::DM_STATUS_FINISH; + LOGI("DmTimer StartTimer %s", name.c_str()); + if (name.empty() || timeOut <= MIN_TIME_OUT || timeOut > MAX_TIME_OUT || callback == nullptr) { + LOGI("DmTimer StartTimer input value invalid"); + return ERR_DM_INPUT_PARAMETER_EMPTY; } - LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut); - if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) { - return DmTimerStatus::DM_STATUS_BUSY; + std::shared_ptr timer = std::make_shared(name, timeOut, callback); + { + std::lock_guard locker(timerMutex_); + timerQueue_.push(timer); + timerMap_[name] = timer; } - mTimeOutSec_ = timeOut; - mHandle_ = handle; - mHandleData_ = data; - - if (CreateTimeFd()) { - return DmTimerStatus::DM_STATUS_CREATE_ERROR; + if (timerState_) { + LOGI("DmTimer is running"); + return DM_OK; } - mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; - mThread_ = std::thread(&DmTimer::WaitForTimeout, this); - mThread_.detach(); - return mStatus_; + TimerRunning(); + { + std::unique_lock locker(timerStateMutex_); + runTimerCondition_.wait(locker, [this] { return static_cast(timerState_); }); + } + return DM_OK; } -void DmTimer::Stop(int32_t code) +int32_t DmTimer::DeleteTimer(std::string timerName) { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT - || mHandleData_ == nullptr) { - LOGE("DmTimer is not init"); - return; + if (timerName.empty()) { + LOGE("DmTimer DeleteTimer timer is null"); + return ERR_DM_INPUT_PARAMETER_EMPTY; } - if (mTimeFd_[1]) { - char event = 'S'; - if (write(mTimeFd_[1], &event, 1) < 0) { - return; + LOGI("DmTimer DeleteTimer name %s", timerName.c_str()); + std::lock_guard locker(timerMutex_); + for (auto iter : timerMap_) { + if (iter.second->timerName_ == timerName) { + iter.second->state_ = false; + return DM_OK; } } + LOGE("DmTimer DeleteTimer no have this timer"); + return ERR_DM_INPUT_PARAMETER_EMPTY; } -void DmTimer::WaitForTimeout() +int32_t DmTimer::DeleteAll() { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; + LOGI("DmTimer DeleteAll start"); + std::lock_guard locker(timerMutex_); + for (auto iter : timerMap_) { + iter.second->state_ = false; } - LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); + return DM_OK; +} - std::lock_guard lock(mTimerLock_); - int32_t nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND); - LOGI("DmTimer is triggering"); - if (nfds > 0) { - char event = 0; - if (mEvents_[0].events & EPOLLIN) { - int num = read(mTimeFd_[0], &event, 1); - LOGD("DmTimer %s exit with num=%d, event=%d, errno=%d", mTimerName_.c_str(), num, event, errno); +int32_t DmTimer::TimerRunning() +{ + std::thread([this] () { + { + timerState_ = true; + std::unique_lock locker(timerStateMutex_); + runTimerCondition_.notify_one(); } - } else if (nfds == 0) { - if (mHandleData_ != nullptr) { - mHandle_(mHandleData_, *this); - LOGI("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); + while (!timerQueue_.empty() && timerState_) { + std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_TICK_MILLSECONDS)); + while (std::chrono::duration_cast(steadyClock::now() + - timerQueue_.top()->expire_).count() / MILLISECOND_TO_SECOND >= timerQueue_.top()->timeOut_ + || !timerQueue_.top()->state_) { + std::string name = timerQueue_.top()->timerName_; + if (timerQueue_.top()->state_) { + timerQueue_.top()->callback_(name); + } + + std::lock_guard locker(timerMutex_); + timerQueue_.pop(); + timerMap_.erase(name); + if (timerQueue_.empty()) { + break; + } + } } - } else { - LOGE("DmTimer %s epoll_wait returned n=%d, error: %d", mTimerName_.c_str(), nfds, errno); - } - Release(); + { + timerState_ = false; + std::unique_lock locker(timerStateMutex_); + stopTimerCondition_.notify_one(); + } + }).detach(); + return DM_OK; } - -int32_t DmTimer::CreateTimeFd() -{ - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return DM_STATUS_FINISH; - } - LOGI("DmTimer %s creatTimeFd", mTimerName_.c_str()); - - int ret = pipe(mTimeFd_); - if (ret < 0) { - LOGE("DmTimer %s CreateTimeFd fail:(%d) errno(%d)", mTimerName_.c_str(), ret, errno); - return ret; - } - - std::lock_guard lock(mTimerLock_); - mEv_.data.fd = mTimeFd_[0]; - mEv_.events = EPOLLIN | EPOLLET; - mEpFd_ = epoll_create(MAX_EVENTS); - ret = epoll_ctl(mEpFd_, EPOLL_CTL_ADD, mTimeFd_[0], &mEv_); - if (ret != 0) { - Release(); - } - return ret; } - -void DmTimer::Release() -{ - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; - } - LOGI("DmTimer %s release in", mTimerName_.c_str()); - if (mStatus_ == DmTimerStatus::DM_STATUS_INIT) { - LOGE("DmTimer %s already Release", mTimerName_.c_str()); - return; - } - - mStatus_ = DmTimerStatus::DM_STATUS_INIT; - close(mTimeFd_[0]); - close(mTimeFd_[1]); - if (mEpFd_ >= 0) { - close(mEpFd_); - } - mTimerName_ = ""; - mTimeOutSec_ = 0; - mHandle_ = nullptr; - mHandleData_ = nullptr; - mTimeFd_[0] = 0; - mTimeFd_[1] = 0; - mEpFd_ = 0; -} - -std::string DmTimer::GetTimerName() -{ - return mTimerName_; -} -} // namespace DistributedHardware -} // namespace OHOS +} \ No newline at end of file diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index b8da8884..bc59bc8d 100755 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -24,23 +24,6 @@ namespace OHOS { namespace DistributedHardware { const int32_t SESSION_CANCEL_TIMEOUT = 0; -static void TimeOut(void *data, DmTimer& timer) -{ - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("time out is not our timer"); - return; - } - - DmDeviceStateManager *deviceStateMgr = (DmDeviceStateManager*)data; - if (deviceStateMgr == nullptr) { - LOGE("deviceStateMgr is nullptr"); - return; - } - - deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName()); -} - DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr softbusConnector, std::shared_ptr listener, std::shared_ptr hiChainConnector) : softbusConnector_(softbusConnector), listener_(listener), hiChainConnector_(hiChainConnector) @@ -268,11 +251,14 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) #endif for (auto &iter : stateTimerInfoMap_) { if (iter.second.netWorkId == deviceInfo.deviceId) { - iter.second.timer->Stop(SESSION_CANCEL_TIMEOUT); + timer_->DeleteTimer(iter.second.timerName); return; } } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(mCumulativeQuantity_++); std::shared_ptr offLineTimer = std::make_shared(timerName); if (offLineTimer != nullptr) { @@ -296,12 +282,15 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) LOGI("start offline timer"); for (auto &iter : stateTimerInfoMap_) { if (iter.second.netWorkId == deviceInfo.deviceId) { - iter.second.timer->Start(OFFLINE_TIMEOUT, TimeOut, this); + timer_->StartTimer(iter.second.timerName, OFFLINE_TIMEOUT, + [this] (std::string name) { + DmDeviceStateManager::DeleteTimeOutGroup(name); + }); } } } -void DmDeviceStateManager::DeleteTimeOutGroup(std::string stateTimer) +void DmDeviceStateManager::DeleteTimeOutGroup(std::string name) { #if defined(__LITEOS_M__) DmMutex mutexLock; @@ -309,14 +298,14 @@ void DmDeviceStateManager::DeleteTimeOutGroup(std::string stateTimer) std::lock_guard mutexLock(timerMapMutex_); #endif if (hiChainConnector_ != nullptr) { - auto iter = stateTimerInfoMap_.find(stateTimer); + auto iter = stateTimerInfoMap_.find(name); if (iter != stateTimerInfoMap_.end()) { LOGI("remove hichain group with device: %s", - GetAnonyString(stateTimerInfoMap_[stateTimer].deviceId).c_str()); - hiChainConnector_->DeleteTimeOutGroup(stateTimerInfoMap_[stateTimer].deviceId.c_str()); + GetAnonyString(stateTimerInfoMap_[name].deviceId).c_str()); + hiChainConnector_->DeleteTimeOutGroup(stateTimerInfoMap_[name].deviceId.c_str()); } } - stateTimerInfoMap_.erase(stateTimer); + stateTimerInfoMap_.erase(name); } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp index 462fd625..52d634c2 100644 --- a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp +++ b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp @@ -25,23 +25,6 @@ const std::string DISCOVERY_TIMEOUT_TASK = TIMER_PREFIX + "discovery"; const int32_t DISCOVERY_TIMEOUT = 120; const int32_t SESSION_CANCEL_TIMEOUT = 0; -static void TimeOut(void *data, DmTimer& timer) -{ - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) { - LOGE("time out is not our timer"); - return; - } - - DmDiscoveryManager *discoveryMgr = (DmDiscoveryManager *)data; - if (discoveryMgr == nullptr) { - LOGE("discoveryMgr is nullptr"); - return; - } - - discoveryMgr->HandleDiscoveryTimeout(); -} - DmDiscoveryManager::DmDiscoveryManager(std::shared_ptr softbusConnector, std::shared_ptr listener) : softbusConnector_(softbusConnector), listener_(listener) @@ -72,8 +55,13 @@ int32_t DmDiscoveryManager::StartDeviceDiscovery(const std::string &pkgName, con discoveryContextMap_.emplace(pkgName, context); softbusConnector_->RegisterSoftbusDiscoveryCallback(pkgName, std::shared_ptr(shared_from_this())); - discoveryTimer_ = std::make_shared(DISCOVERY_TIMEOUT_TASK); - discoveryTimer_->Start(DISCOVERY_TIMEOUT, TimeOut, this); + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + timer_->StartTimer(DISCOVERY_TIMEOUT_TASK, DISCOVERY_TIMEOUT, + [this] (std::string name) { + DmDiscoveryManager::HandleDiscoveryTimeout(name); + }); return softbusConnector_->StartDiscovery(subscribeInfo); } @@ -85,7 +73,7 @@ int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint if (!discoveryContextMap_.empty()) { discoveryContextMap_.erase(pkgName); softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName); - discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT); + timer_->DeleteTimer(DISCOVERY_TIMEOUT_TASK); } return softbusConnector_->StopDiscovery(subscribeId); } @@ -115,10 +103,21 @@ void DmDiscoveryManager::OnDiscoverySuccess(const std::string &pkgName, int32_t listener_->OnDiscoverySuccess(pkgName, subscribeId); } -void DmDiscoveryManager::HandleDiscoveryTimeout() +void DmDiscoveryManager::HandleDiscoveryTimeout(std::string name) { LOGI("DmDiscoveryManager::HandleDiscoveryTimeout"); - StopDeviceDiscovery(discoveryQueue_.front(), discoveryContextMap_[discoveryQueue_.front()].subscribeId); + if (discoveryQueue_.empty()) { + LOGE("HandleDiscoveryTimeout: discovery queue is empty."); + return; + } + + std::string pkgName = discoveryQueue_.front(); + auto iter = discoveryContextMap_.find(pkgName); + if (iter == discoveryContextMap_.end()) { + LOGE("HandleDiscoveryTimeout: subscribeId not found by pkgName %s", GetAnonyString(pkgName).c_str()); + return; + } + StopDeviceDiscovery(pkgName, discoveryContextMap_[pkgName].subscribeId); } } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_auth_request_state.cpp b/test/unittest/UTTest_auth_request_state.cpp index 939ea70e..a639ad69 100644 --- a/test/unittest/UTTest_auth_request_state.cpp +++ b/test/unittest/UTTest_auth_request_state.cpp @@ -110,8 +110,7 @@ HWTEST_F(AuthRequestStateTest, TransitionTo_002, testing::ext::TestSize.Level0) std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr context = std::make_shared(); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); - authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; + authManager->timer_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -236,6 +235,7 @@ HWTEST_F(AuthRequestStateTest, Enter_004, testing::ext::TestSize.Level0) authManager->authRequestContext_->deviceId = "111"; authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_); + authManager->timer_ = std::make_shared(); authRequestState->SetAuthManager(authManager); std::shared_ptr context = std::make_shared(); context->deviceId = "123456"; @@ -272,8 +272,7 @@ HWTEST_F(AuthRequestStateTest, Enter_006, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); - authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; + authManager->timer_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -334,8 +333,7 @@ HWTEST_F(AuthRequestStateTest, Enter_008, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr inputStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); - authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer; + authManager->timer_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -513,9 +511,9 @@ HWTEST_F(AuthRequestStateTest, Enter_014, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; - authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->timer_ = std::make_shared(); + authManager->authMessageProcessor_ = std::make_ + shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); @@ -573,8 +571,7 @@ HWTEST_F(AuthRequestStateTest, Enter_016, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr inputStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); - authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer; + authManager->timer_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); diff --git a/test/unittest/UTTest_auth_response_state.cpp b/test/unittest/UTTest_auth_response_state.cpp index e3db904f..ae10ad41 100644 --- a/test/unittest/UTTest_auth_response_state.cpp +++ b/test/unittest/UTTest_auth_response_state.cpp @@ -201,6 +201,7 @@ HWTEST_F(AuthResponseStateTest, Enter_003, testing::ext::TestSize.Level0) authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_); authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); authManager->softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authManager); + authManager->timer_ = std::make_shared(); authResponseState->SetAuthManager(authManager); std::shared_ptr context = std::make_shared(); context->deviceId = "123456"; diff --git a/test/unittest/UTTest_dm_auth_manager.cpp b/test/unittest/UTTest_dm_auth_manager.cpp index 362cbf83..fd869ef0 100644 --- a/test/unittest/UTTest_dm_auth_manager.cpp +++ b/test/unittest/UTTest_dm_auth_manager.cpp @@ -68,16 +68,16 @@ HWTEST_F(DmAuthManagerTest, UnAuthenticateDevice_001, testing::ext::TestSize.Lev */ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSize.Level0) { + std::string name = "test"; std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authRequestState = std::make_shared(); - authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->authResponseContext_ = nullptr; authManager->SetAuthRequestState(authRequestState); - int32_t ret = authManager->HandleAuthenticateTimeout(); - ASSERT_EQ(ret, DM_OK); + authManager->HandleAuthenticateTimeout(name); + ASSERT_TRUE(authManager->authResponseContext_ != nullptr); } /** @@ -88,6 +88,7 @@ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSiz */ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSize.Level0) { + std::string name = "test"; std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authRequestState = std::make_shared(); @@ -95,8 +96,8 @@ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSiz authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->SetAuthRequestState(authRequestState); - int32_t ret = authManager->HandleAuthenticateTimeout(); - ASSERT_EQ(ret, DM_OK); + authManager->HandleAuthenticateTimeout(name); + ASSERT_TRUE(authManager->authRequestState_ != nullptr); } /** @@ -225,8 +226,7 @@ HWTEST_F(DmAuthManagerTest, JoinNetwork_001, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; + authManager->timer_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); diff --git a/test/unittest/UTTest_dm_discovery_manager.cpp b/test/unittest/UTTest_dm_discovery_manager.cpp index 694b38b2..8e029a42 100644 --- a/test/unittest/UTTest_dm_discovery_manager.cpp +++ b/test/unittest/UTTest_dm_discovery_manager.cpp @@ -225,20 +225,6 @@ HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_002, testing::ext::TestSize. std ::string ret = pReq->GetPkgName(); EXPECT_EQ(ret, pkgName); } - -/** - * @tc.name: HandleDiscoveryTimeout_001 - * @tc.desc: set pkgName not null and return 1(true) - * @tc.type: FUNC - * @tc.require: AR000GHSJK - */ -HWTEST_F(DmDiscoveryManagerTest, HandleDiscoveryTimeout_001, testing::ext::TestSize.Level0) -{ - std::string pkgName = "com.ohos.helloworld"; - discoveryMgr_->HandleDiscoveryTimeout(); - int ret = discoveryMgr_->discoveryContextMap_.count(pkgName); - EXPECT_EQ(ret, 1); -} } // namespace } // namespace DistributedHardware } // namespace OHOS