diff --git a/bundle.json b/bundle.json index b6d72ecfff21cdeb9e0b8493e75a298889c2a5b0..2cfcfd484827056388015ea39f6b4470f963ec0e 100644 --- a/bundle.json +++ b/bundle.json @@ -64,9 +64,7 @@ } } ], - "test": [ - "//foundation/distributedhardware/devicemanager/test:test" - ] + "test": [ "//foundation/distributedhardware/devicemanager/test:test" ] } } } diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index a2124ae27128f8252eb9e7dc5350e57997f2b544..a00b5a76fd3a0fcfb3db5f10f4724b8d6692cb99 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -85,12 +85,14 @@ enum { DM_AUTH_NOT_AUTH, DM_AUTH_DONT_AUTH, DM_AUTH_NOT_START, + DM_MESSAGE_NOT_COMPLETE, DM_SOFTBUS_FAILED = 3000, DM_SOFTBUS_CREATE_SESSION_SERVER_FAILED, DM_HICHAIN_FAILED = 4000, DM_HICHAIN_GROUP_CREATE_FAILED, DM_HICHAIN_MEMBER_ADD_FAILED, DM_HICHAIN_CREATE_CHANNEL_FAILED, + DM_AUTH_NO_TIMER, }; const std::string TARGET_PKG_NAME_KEY = "targetPkgName"; const std::string HOST_PKG_NAME_KEY = "hostPackageName"; @@ -209,6 +211,12 @@ const std::string WAIT_REQUEST_TIMEOUT_TASK = TIMER_PREFIX + "waitRequest"; const std::string STATE_TIMER_PREFIX = "stateTimer_"; const int32_t TIMER_PREFIX_LENGTH = 19; const int32_t TIMER_DEFAULT = 0; +const int32_t NO_TIMER = -1; +const int32_t INIT_SIZE = 3; +const int32_t MAX_EVENT_NUMBER = 10; +const int32_t EXPAND_TWICE = 2; +const int32_t SEC_TO_MM = 1000; +const int32_t MAX_EVENTS = 5; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H diff --git a/services/devicemanagerservice/include/authentication/auth_message_processor.h b/services/devicemanagerservice/include/authentication/auth_message_processor.h index ec545b81c2dc8a7d9a9eeaa0fb71b573a7badaf9..2802690493974369eb366888d8e7b78640f76abf 100644 --- a/services/devicemanagerservice/include/authentication/auth_message_processor.h +++ b/services/devicemanagerservice/include/authentication/auth_message_processor.h @@ -47,7 +47,7 @@ private: void CreateSyncGroupMessage(nlohmann::json &json); void CreateResponseAuthMessage(nlohmann::json &json); void ParseAuthResponseMessage(nlohmann::json &json); - int32_t ParseAuthRequestMessage(); + int32_t ParseAuthRequestMessage(nlohmann::json &json); void ParseNegotiateMessage(const nlohmann::json &json); void CreateResponseFinishMessage(nlohmann::json &json); void ParseResponseFinishMessage(nlohmann::json &json); diff --git a/services/devicemanagerservice/include/authentication/dm_auth_manager.h b/services/devicemanagerservice/include/authentication/dm_auth_manager.h index a463dbf3ff3a7d8fced23f5c233c9b018c00d6aa..700c965fdbd399ff4dbe51e32aa3226be5d47cbb 100644 --- a/services/devicemanagerservice/include/authentication/dm_auth_manager.h +++ b/services/devicemanagerservice/include/authentication/dm_auth_manager.h @@ -384,7 +384,7 @@ private: std::shared_ptr authRequestContext_; std::shared_ptr authResponseContext_; std::shared_ptr authMessageProcessor_; - std::map> timerMap_; + std::shared_ptr timerHeap_; std::shared_ptr dmAbilityMgr_; bool isCryptoSupport_ = false; bool isFinishOfLocal_ = true; diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index 27def3ae2959329288bde3f5f7659f3bbbd5d68c..63822d47712c363efc273b90b50015c7e7d418b5 100644 --- a/services/devicemanagerservice/include/dependency/timer/dm_timer.h +++ b/services/devicemanagerservice/include/dependency/timer/dm_timer.h @@ -15,89 +15,84 @@ #ifndef TIMER_H #define TIMER_H + +#include #include -#include +#include +#include #include -#if !defined(__LITEOS_M__) #include -#include +#include +#include #include -#include -#endif -#include -#include - -#include "dm_log.h" - namespace OHOS { namespace DistributedHardware { -class DmTimer; -typedef void (*TimeoutHandle)(void *data, DmTimer& timer); - -#define MAX_EVENTS 255 - -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, +typedef void (*TimeoutHandle)(void *data, std::string timerName); +class DmTimer { +public: + DmTimer(std::string name, time_t expire, void *user, TimeoutHandle mHandle) + :userData_(user), timerName_(name), expire_(expire), mHandle_(mHandle) {}; +public: + void *userData_; + bool isTrigger = false; + std::string timerName_; + time_t expire_; + TimeoutHandle mHandle_;; }; -class DmTimer { +class TimeHeap { public: - explicit DmTimer(const std::string &name); - ~DmTimer(); + TimeHeap(); + ~TimeHeap(); + /** + * @tc.name: TimeHeap::AddTimer + * @tc.desc: Add timer to time heap + * @tc.type: FUNC + */ + int32_t AddTimer(std::string name, int timeout, TimeoutHandle mHandle, void *user); /** - * @tc.name: DmTimer::Start - * @tc.desc: Start of the DmTimer + * @tc.name: TimeHeap::DelTimer + * @tc.desc: Delete timer of the time heap * @tc.type: FUNC */ - DmTimerStatus Start(uint32_t timeOut, TimeoutHandle handle, void *data); + int32_t DelTimer(std::string name); /** - * @tc.name: DmTimer::Stop - * @tc.desc: Stop of the DmTimer + * @tc.name: TimeHeap::DelAll + * @tc.desc: Delete all timer of the time heap * @tc.type: FUNC */ - void Stop(int32_t code); + int32_t DelAll(); +private: /** - * @tc.name: DmTimer::WaitForTimeout - * @tc.desc: WaitFor Timeout of the DmTimer + * @tc.name: TimeHeap::Run + * @tc.desc: timer wait for timeout * @tc.type: FUNC */ - void WaitForTimeout(); + void Run(); /** - * @tc.name: DmTimer::GetTimerName - * @tc.desc: Get TimerName of the DmTimer + * @tc.name: TimeHeap::Run + * @tc.desc: timerout event triggering * @tc.type: FUNC */ - std::string GetTimerName(); -private: - int32_t CreateTimeFd(); - void Release(); + int32_t Tick(); + /** + * @tc.name: TimeHeap::Run + * @tc.desc: sort the time heap + * @tc.type: FUNC + */ + int32_t MoveUp(std::shared_ptr timer); 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_; + int32_t hsize_ = 0; + int32_t epollFd_; + int32_t pipefd[2]; std::thread mThread_; - std::mutex mTimerLock_; -#endif - - std::string mTimerName_; + std::vector> minHeap_; }; -} // namespace DistributedHardware -} // namespace OHOS -#endif // TIMER_H +} +} +#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 2cf315b82a7b71c96de61f9497b49b415a9d9b18..95a59c5116816a0701173d3b1f07ae1e60c06a81 100755 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -36,7 +36,6 @@ struct StateTimerInfo { std::string timerName; std::string netWorkId; std::string deviceId; - std::shared_ptr timer; }; class DmDeviceStateManager final : public ISoftbusStateCallback, @@ -164,6 +163,7 @@ private: std::map remoteDeviceInfos_; std::map decisionInfos_; std::map stateTimerInfoMap_; + std::shared_ptr timerHeap_; 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 db0e97d2848cbf9cd8cc7d1cc5d44162a5f6ec9f..00e756b360cbfc9d466d735e96731bd2a8dc19ec 100644 --- a/services/devicemanagerservice/include/discovery/dm_discovery_manager.h +++ b/services/devicemanagerservice/include/discovery/dm_discovery_manager.h @@ -84,7 +84,7 @@ private: std::shared_ptr listener_; std::queue discoveryQueue_; std::map discoveryContextMap_; - std::shared_ptr discoveryTimer_; + std::shared_ptr timerHeap_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/authentication/auth_message_processor.cpp b/services/devicemanagerservice/src/authentication/auth_message_processor.cpp index 486aa989e6c62a87af01be034263cd8d0e3bb92d..7d2443155d3c7ab270ccaf2b55c9a8524ab55c77 100644 --- a/services/devicemanagerservice/src/authentication/auth_message_processor.cpp +++ b/services/devicemanagerservice/src/authentication/auth_message_processor.cpp @@ -164,33 +164,15 @@ int32_t AuthMessageProcessor::ParseMessage(const std::string &message) LOGE("err json string, first time"); return DM_FAILED; } - int32_t sliceNum = 0; int32_t msgType = jsonObject[TAG_TYPE]; authResponseContext_->msgType = msgType; - LOGI("AuthMessageProcessor::ParseAuthRequestMessage======== %d", authResponseContext_->msgType); + LOGI("AuthMessageProcessor::ParseAuthRequestMessage msgType: %d", authResponseContext_->msgType); switch (msgType) { case MSG_TYPE_NEGOTIATE: ParseNegotiateMessage(jsonObject); break; case MSG_TYPE_REQ_AUTH: - if (!jsonObject.contains(TAG_INDEX) || !jsonObject.contains(TAG_DEVICE_ID) || - !jsonObject.contains(TAG_SLICE_NUM)) { - LOGE("err json string, first time"); - return DM_FAILED; - } - authResponseContext_->deviceId = jsonObject[TAG_DEVICE_ID]; - authResponseContext_->authType = jsonObject[TAG_AUTH_TYPE]; - authResponseContext_->appDesc = jsonObject[TAG_APP_DESCRIPTION]; - authResponseContext_->token = jsonObject[TAG_TOKEN]; - authResponseContext_->targetPkgName = jsonObject[TAG_TARGET]; - authResponseContext_->appName = jsonObject[TAG_APP_NAME]; - LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s", authResponseContext_->deviceId.c_str()); - sliceNum = jsonObject[TAG_SLICE_NUM]; - if ((int32_t)authSplitJsonList_.size() < sliceNum) { - authSplitJsonList_.push_back(message); - } else { - ParseAuthRequestMessage(); - } + return ParseAuthRequestMessage(jsonObject); break; case MSG_TYPE_RESP_AUTH: ParseAuthResponseMessage(jsonObject); @@ -209,6 +191,37 @@ void AuthMessageProcessor::ParseResponseFinishMessage(nlohmann::json &json) authResponseContext_->reply = json[TAG_REPLY]; } +int32_t AuthMessageProcessor::ParseAuthRequestMessage(nlohmann::json &json) +{ + LOGE("start ParseAuthRequestMessage"); + int32_t sliceNum = 0; + int32_t idx = 0; + if (!json.contains(TAG_INDEX) || !json.contains(TAG_DEVICE_ID) || + !json.contains(TAG_SLICE_NUM)) { + LOGE("err json string, first time"); + return DM_FAILED; + } + + idx = json[TAG_INDEX]; + sliceNum = json[TAG_SLICE_NUM]; + if (idx == 0) { + authResponseContext_->deviceId = json[TAG_DEVICE_ID]; + authResponseContext_->authType = json[TAG_AUTH_TYPE]; + authResponseContext_->appDesc = json[TAG_APP_DESCRIPTION]; + authResponseContext_->token = json[TAG_TOKEN]; + authResponseContext_->targetPkgName = json[TAG_TARGET]; + authResponseContext_->appName = json[TAG_APP_NAME]; + authResponseContext_->appThumbnail = ""; + } + + if (idx < sliceNum && json.contains(TAG_APP_THUMBNAIL)) { + std::string appSliceThumbnail = json[TAG_APP_THUMBNAIL]; + authResponseContext_->appThumbnail = authResponseContext_->appThumbnail + appSliceThumbnail; + return DM_MESSAGE_NOT_COMPLETE; + } + return DM_OK; +} + void AuthMessageProcessor::ParseAuthResponseMessage(nlohmann::json &json) { LOGI("AuthMessageProcessor::ParseAuthResponseMessage "); @@ -227,25 +240,6 @@ void AuthMessageProcessor::ParseAuthResponseMessage(nlohmann::json &json) LOGI("AuthMessageProcessor::ParseAuthResponseMessage "); } -int32_t AuthMessageProcessor::ParseAuthRequestMessage() -{ - nlohmann::json jsonObject = authSplitJsonList_.front(); - authResponseContext_->deviceId = jsonObject[TAG_DEVICE_ID]; - authResponseContext_->reply = jsonObject[TAG_REPLY]; - authResponseContext_->authType = jsonObject[TAG_AUTH_TYPE]; - LOGI("AuthMessageProcessor::ParseAuthResponseMessage %d", authResponseContext_->reply); - LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s", authResponseContext_->deviceId.c_str()); - if (authResponseContext_->reply == AUTH_REPLY_ACCEPT) { - authResponseContext_->networkId = jsonObject[TAG_NET_ID]; - authResponseContext_->groupId = jsonObject[TAG_GROUP_ID]; - authResponseContext_->groupName = jsonObject[TAG_GROUP_NAME]; - authResponseContext_->requestId = jsonObject[TAG_REQUEST_ID]; - return DM_FAILED; - } - authSplitJsonList_.clear(); - return DM_OK; -} - void AuthMessageProcessor::ParseNegotiateMessage(const nlohmann::json &json) { if (json.contains(TAG_CRYPTO_SUPPORT)) { diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 86d31b3e77b26b988401fe8d8474982760a9e820..79f9153a9e5923fb6011df022d5a847fe61a3c32 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -30,7 +30,6 @@ namespace OHOS { namespace DistributedHardware { -const int32_t SESSION_CANCEL_TIMEOUT = 0; const int32_t AUTHENTICATE_TIMEOUT = 120; const int32_t CONFIRM_TIMEOUT = 60; const int32_t NEGOTIATE_TIMEOUT = 10; @@ -41,10 +40,10 @@ 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) +static void TimeOut(void *data, std::string timerName) { - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGI("time out %s", timerName.c_str()); + if (data == nullptr || timerName.find(TIMER_PREFIX) != TIMER_DEFAULT) { LOGE("time out is not our timer"); return; } @@ -131,9 +130,10 @@ 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); + if (timerHeap_ == nullptr) { + timerHeap_ = std::make_shared(); + } + timerHeap_->AddTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, TimeOut, this); LOGI("DmAuthManager::AuthenticateDevice complete"); return DM_OK; } @@ -174,12 +174,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); + timerHeap_->DelTimer(INPUT_TIMEOUT_TASK); ptr = authenticationMap_[authResponseContext_->authType]; int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam); switch (ret) { @@ -210,12 +209,9 @@ 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); + timerHeap_ = std::make_shared(); + timerHeap_->AddTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(WAIT_NEGOTIATE_TIMEOUT_TASK, WAIT_NEGOTIATE_TIMEOUT, TimeOut, this); } else { std::shared_ptr authMessageProcessor = std::make_shared(shared_from_this()); @@ -294,18 +290,16 @@ void DmAuthManager::OnDataReceived(const int32_t sessionId, const std::string me 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) { + timerHeap_->DelTimer(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) { + timerHeap_->DelTimer(WAIT_REQUEST_TIMEOUT_TASK); authResponseState_->TransitionTo(std::make_shared()); } else { LOGE("Device manager auth state error"); @@ -359,8 +353,8 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) { LOGI("DmAuthManager OnMemberJoin start"); - if (authRequestState_ != nullptr && timerMap_.find(ADD_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[ADD_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authRequestState_ != nullptr) { + timerHeap_->DelTimer(ADD_TIMEOUT_TASK); if (status != DM_OK || authResponseContext_->requestId != requestId) { if (authRequestState_ != nullptr) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; @@ -419,9 +413,7 @@ 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); + timerHeap_->AddTimer(NEGOTIATE_TIMEOUT_TASK, NEGOTIATE_TIMEOUT, TimeOut, this); } void DmAuthManager::RespNegotiate(const int32_t &sessionId) @@ -460,18 +452,13 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) 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); + timerHeap_->AddTimer(WAIT_REQUEST_TIMEOUT_TASK, WAIT_REQUEST_TIMEOUT, TimeOut, this); } void DmAuthManager::SendAuthRequest(const int32_t &sessionId) { LOGI("DmAuthManager::EstablishAuthChannel session id"); - if (timerMap_.find(NEGOTIATE_TIMEOUT_TASK) == timerMap_.end()) { - return; - } - timerMap_[NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(NEGOTIATE_TIMEOUT_TASK); if (authResponseContext_->cryptoSupport) { isCryptoSupport_ = true; } @@ -483,9 +470,7 @@ 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); + timerHeap_->AddTimer(CONFIRM_TIMEOUT_TASK, CONFIRM_TIMEOUT, TimeOut, this); } int32_t DmAuthManager::StartAuthProcess(const int32_t &action) @@ -511,15 +496,9 @@ int32_t DmAuthManager::StartAuthProcess(const int32_t &action) void DmAuthManager::StartRespAuthProcess() { LOGI("DmAuthManager::StartRespAuthProcess", authResponseContext_->sessionId); - if (timerMap_.find(CONFIRM_TIMEOUT_TASK) == timerMap_.end()) { - return; - } - - timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(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); + timerHeap_->AddTimer(INPUT_TIMEOUT_TASK, INPUT_TIMEOUT, TimeOut, this); authRequestState_->TransitionTo(std::make_shared()); } else { LOGE("do not accept"); @@ -555,9 +534,7 @@ 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); + timerHeap_->AddTimer(ADD_TIMEOUT_TASK, ADD_TIMEOUT, TimeOut, this); int32_t ret = hiChainConnector_->AddMember(deviceId, connectInfo); if (ret != 0) { return DM_FAILED; @@ -584,10 +561,7 @@ std::string DmAuthManager::GetConnectAddr(std::string deviceId) int32_t DmAuthManager::JoinNetwork() { LOGI("DmAuthManager JoinNetwork start"); - if (timerMap_.find(AUTHENTICATE_TIMEOUT_TASK) == timerMap_.end()) { - return DM_FAILED; - } - timerMap_[AUTHENTICATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(AUTHENTICATE_TIMEOUT_TASK); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authRequestState_->TransitionTo(std::make_shared()); @@ -612,12 +586,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(); - } + timerHeap_->DelAll(); isFinishOfLocal_ = true; authResponseContext_ = nullptr; authResponseState_ = nullptr; @@ -642,12 +611,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(); - } + timerHeap_->DelAll(); isFinishOfLocal_ = true; authRequestContext_ = nullptr; authResponseContext_ = nullptr; diff --git a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp index d103e8822cd615fae270f3e3b695a81e84b96647..ffe223494ef1f311613848f1f5871a3527dda6ad 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -13,177 +13,187 @@ * limitations under the License. */ -#include "dm_timer.h" - -#include - #include "dm_constants.h" -#include "securec.h" +#include "dm_log.h" +#include "dm_timer.h" namespace OHOS { namespace DistributedHardware { -namespace { -const int32_t MILL_SECONDS_PER_SECOND = 1000; -} - -DmTimer::DmTimer(const std::string &name) +int32_t TimeHeap::Tick() { - if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer name is null"); - mTimerName_ = ""; - return; - } - - 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() -{ - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; + LOGI("Tick start"); + if (hsize_ == 0) { + LOGE("Timer count is 0"); + return DM_AUTH_NO_TIMER; } - LOGI("DmTimer %s destroy in", mTimerName_.c_str()); - Stop(0); - std::lock_guard lock(mTimerLock_); - Release(); + time_t cur_time = time(NULL); + std::shared_ptr top = minHeap_.front(); + top->isTrigger = true; + do { + top->mHandle_(top->userData_, top->timerName_); + DelTimer(top->timerName_); + + if (hsize_ > 0) { + top = minHeap_.front(); + } else { + break; + } + } while (top->expire_ <= cur_time); + + return DM_OK; } -DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) +int32_t TimeHeap::MoveUp(std::shared_ptr timer) { - 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; + LOGE("MoveUp timer"); + if (timer == nullptr) { + LOGE("MoveUp timer is null"); + return DM_INVALID_VALUE; } - LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut); - if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) { - return DmTimerStatus::DM_STATUS_BUSY; + if (hsize_ == 0) { + LOGE("Add timer failed"); + return DM_INVALID_VALUE; } - mTimeOutSec_ = timeOut; - mHandle_ = handle; - mHandleData_ = data; - - if (CreateTimeFd()) { - return DmTimerStatus::DM_STATUS_CREATE_ERROR; + for (int32_t i = 1;; i++) { + LOGE("MoveUp 1 = %d, h = %d", i, hsize_); + if (i == hsize_) { + minHeap_.insert(minHeap_.begin() + (i - 1), timer); + break; + } + if (timer->expire_ < minHeap_[i - 1]->expire_) { + minHeap_.insert(minHeap_.begin() + (i -1), timer); + break; + } } - - mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; - mThread_ = std::thread(&DmTimer::WaitForTimeout, this); - mThread_.detach(); - return mStatus_; + return DM_OK; } -void DmTimer::Stop(int32_t code) +void TimeHeap::Run() { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT || mHandleData_ == nullptr) { - LOGE("DmTimer is not init"); - return; - } + epoll_event event; + event.data.fd = pipefd[0]; + event.events = EPOLLIN | EPOLLET; + epoll_ctl(epollFd_, EPOLL_CTL_ADD, pipefd[0], &event); + epoll_event events[MAX_EVENT_NUMBER]; + bool stop = false; + int timeout = NO_TIMER; + + while(!stop) { + int number = epoll_wait(epollFd_, events, MAX_EVENT_NUMBER, timeout); + + LOGI("RunTimer is doing"); + if (number < 0) { + LOGE("DmTimer %s epoll_wait error: %d", minHeap_.front()->timerName_.c_str(), errno); + DelTimer(minHeap_.front()->timerName_); + } + if (!number) { + Tick(); + } else { + int buffer = 0; + recv(pipefd[0], (char*)&buffer, sizeof(buffer), 0); + } - if (mTimeFd_[1]) { - char event = 'S'; - if (write(mTimeFd_[1], &event, 1) < 0) { - return; + if (hsize_ == 0) { + break; + } else { + timeout = (minHeap_.front()->expire_ - time(NULL)) * SEC_TO_MM; } } } -void DmTimer::WaitForTimeout() +TimeHeap::TimeHeap(): epollFd_(epoll_create(MAX_EVENTS)) { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; - } - LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); - - 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); - } - } else if (nfds == 0) { - if (mHandleData_ != nullptr) { - mHandle_(mHandleData_, *this); - LOGI("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); - } - } else { - LOGI("DmTimer %s epoll_wait return nfds=%d, errno=%d", mTimerName_.c_str(), nfds, errno); + minHeap_.resize(INIT_SIZE); + + int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd); + if (ret != 0) { + LOGE("open pipe failed"); } - Release(); + assert(ret == 0); +} + +TimeHeap::~TimeHeap() +{ + DelAll(); + close(epollFd_); } -int32_t DmTimer::CreateTimeFd() +int32_t TimeHeap::AddTimer(std::string name, int timeout, TimeoutHandle mHandle, void *user) { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return DM_STATUS_FINISH; + if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer name is not DM timer"); + return DM_INVALID_VALUE; } - 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; + LOGI("AddTimer %s", name.c_str()); + if (timeout <= 0 || mHandle == nullptr || user == nullptr) { + LOGE("DmTimer %s invalid value", name.c_str()); + return DM_INVALID_VALUE; } - 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(); + if (hsize_ == 0) { + mThread_ = std::thread(&TimeHeap::Run, this); + mThread_.detach(); + } + if (hsize_ == (int32_t)(minHeap_.size() - 1)) { + minHeap_.resize(EXPAND_TWICE * minHeap_.size()); } - return ret; + + std::shared_ptr timer = std::make_shared(name, timeout + time(NULL), user, mHandle); + ++hsize_; + MoveUp(timer); + if(timer == minHeap_.front()) { + char msg = 1; + send(pipefd[1], (char*)&msg, sizeof(msg), 0); + } + LOGE("AddTimer %s complete", name.c_str()); + return DM_OK; } -void DmTimer::Release() +int32_t TimeHeap::DelTimer(std::string name) { - 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; + if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer name is not DM timer"); + return DM_INVALID_VALUE; + } + + LOGI("DelTimer %s", name.c_str()); + int32_t location = 0; + bool have = false; + for (int32_t i = 0; i < hsize_; i++) { + if (minHeap_[i]->timerName_ == name) { + location = i; + have = true; + break; + } + } + + if (!have) { + LOGE("heap is not have this %s", name.c_str()); + return DM_INVALID_VALUE; + } + + if (minHeap_[location] == minHeap_.front() && minHeap_[location]->isTrigger == false) { + char msg = 1; + send(pipefd[1], &msg, sizeof(msg), 0); + } + minHeap_.erase(minHeap_.begin() + location); + hsize_--; + LOGI("DelTimer %s complete , timer count %d", name.c_str(), hsize_); + return DM_OK; } -std::string DmTimer::GetTimerName() +int32_t TimeHeap::DelAll() { - return mTimerName_; + LOGI("DelAll start"); + for (int32_t i = hsize_ ; i > 0; i--) { + DelTimer(minHeap_[i - 1]->timerName_); + } + LOGI("DelAll complete"); + return DM_OK; +} } -} // 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 e8d0d95ad7cf7938b62951855ba20d76eed7e316..6701c6e9b3abb30125ae0aae667e25eade68a179 100755 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -22,12 +22,10 @@ namespace OHOS { namespace DistributedHardware { -const int32_t SESSION_CANCEL_TIMEOUT = 0; - -static void TimeOut(void *data, DmTimer& timer) +static void TimeOut(void *data, std::string timerName) { - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGI("time out %s", timerName.c_str()); + if (data == nullptr || timerName.find(TIMER_PREFIX) != TIMER_DEFAULT) { LOGE("time out is not our timer"); return; } @@ -38,7 +36,7 @@ static void TimeOut(void *data, DmTimer& timer) return; } - deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName()); + deviceStateMgr->DeleteTimeOutGroup(timerName); } DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr softbusConnector, @@ -278,22 +276,21 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) #endif for (auto &iter : stateTimerInfoMap_) { if (iter.second.netWorkId == deviceInfo.deviceId) { - iter.second.timer->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(iter.second.timerName); return; } } - std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(cumulativeQuantity_++); - std::shared_ptr offLineTimer = std::make_shared(timerName); - if (offLineTimer != nullptr) { - StateTimerInfo stateTimer = { - .timerName = timerName, - .netWorkId = deviceInfo.deviceId, - .deviceId = deviceId, - .timer = offLineTimer - }; - stateTimerInfoMap_[timerName] = stateTimer; + if (timerHeap_ == nullptr) { + timerHeap_ = std::make_shared(); } + std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(cumulativeQuantity_++); + StateTimerInfo stateTimer = { + .timerName = timerName, + .netWorkId = deviceInfo.deviceId, + .deviceId = deviceId, + }; + stateTimerInfoMap_[timerName] = stateTimer; } void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) @@ -306,7 +303,7 @@ 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); + timerHeap_->AddTimer(iter.second.timerName, OFFLINE_TIMEOUT, TimeOut, this); } } } diff --git a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp index bc8f80ad314c1c91a29898b4bdfa953c0f57d536..ec0cf6383517a148b4a72637abe91c47189bf9c0 100644 --- a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp +++ b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp @@ -23,12 +23,11 @@ namespace OHOS { namespace DistributedHardware { 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) +static void TimeOut(void *data, std::string timerName) { - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) { + LOGI("time out %s", timerName.c_str()); + if (data == nullptr || timerName != DISCOVERY_TIMEOUT_TASK) { LOGE("time out is not our timer"); return; } @@ -72,8 +71,10 @@ 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 (timerHeap_ == nullptr) { + timerHeap_ = std::make_shared(); + } + timerHeap_->AddTimer(DISCOVERY_TIMEOUT_TASK, DISCOVERY_TIMEOUT, TimeOut, this); return softbusConnector_->StartDiscovery(subscribeInfo); } @@ -85,7 +86,7 @@ int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint if (!discoveryContextMap_.empty()) { discoveryContextMap_.erase(pkgName); softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName); - discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(DISCOVERY_TIMEOUT_TASK); } return softbusConnector_->StopDiscovery(subscribeId); } diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 6c01ffbf369847647d00985a290b0eddbc5d961a..0c29b78735af8fedfc66c70042e0a5602bdc2f70 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -140,9 +140,9 @@ ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr pBaseReq, Message if (pBaseReq == nullptr) { return DM_FAILED; } - std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); - std::string pkgName = pBaseReq->GetPkgName(); + std::string pkgName = pReq->GetPkgName(); std::string deviceId = pReq->GetDeviceId(); std::string token = pReq->GetPinToken(); int32_t status = pReq->GetStatus(); @@ -185,9 +185,10 @@ ON_IPC_SET_REQUEST(SERVER_VERIFY_AUTH_RESULT, std::shared_ptr pBaseReq, if (pBaseReq == nullptr) { return DM_FAILED; } - std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pReq = + std::static_pointer_cast(pBaseReq); - std::string pkgName = pBaseReq->GetPkgName(); + std::string pkgName = pReq->GetPkgName(); std::string deviceId = pReq->GetDeviceId(); int32_t result = pReq->GetResult(); int32_t flag = pReq->GetFlag(); @@ -226,9 +227,9 @@ ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY, std::shared_ptr pBaseReq, Me return DM_FAILED; } - std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); - std::string packagname = pBaseReq->GetPkgName(); + std::string packagname = pReq->GetPkgName(); std::string paramJson = pReq->GetJsonParam(); if (!data.WriteString(packagname)) { LOGE("write pkgName failed"); diff --git a/test/BUILD.gn b/test/BUILD.gn index 87552dc685b4bef675733a99a5209267d271b51f..a3b75ab3436e0280f9b8fb8e0c657060801fda28 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -16,6 +16,9 @@ group("test") { deps = [] if (support_jsapi) { - deps += [ "unittest:unittest" ] + deps += [ + "fuzztest:fuzztest", + "unittest:unittest", + ] } } diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..aa8aa90ffa888b00a9466bfdd75222b9a8026435 --- /dev/null +++ b/test/fuzztest/BUILD.gn @@ -0,0 +1,28 @@ +# 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 +# +# 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. + +group("fuzztest") { + testonly = true + + deps = [ + "devicemanagerimpl_fuzzer:fuzztest", + "devicemanagernotify_fuzzer:fuzztest", + "devicemanagerservice_fuzzer:fuzztest", + "ipcclientmanager_fuzzer:fuzztest", + "ipccmdregister_fuzzer:fuzztest", + "ipcserverclientproxy_fuzzer:fuzztest", + "ipcserverlistener_fuzzer:fuzztest", + "ipcserverstub_fuzzer:fuzztest", + "nativedevicemanagerjs_fuzzer:fuzztest", + ] +} diff --git a/test/fuzztest/devicemanagerimpl_fuzzer/BUILD.gn b/test/fuzztest/devicemanagerimpl_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4a669037b1aa46cfb2ead04b5735d99ad027fa59 --- /dev/null +++ b/test/fuzztest/devicemanagerimpl_fuzzer/BUILD.gn @@ -0,0 +1,132 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DeviceManagerImplFuzzTest") { + module_out_path = "devicemanager/devicemanagerimpl_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/devicemanagerimpl_fuzzer" + + include_dirs = [ + "${utils_path}/include", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include/ipc/standard", + "${services_path}/include", + "${services_path}/include/dependency/timer", + "${services_path}/include/discovery", + "${services_path}/include/dependency/softbus", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo/", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "device_manager_impl_fuzzer.cpp" ] + + deps = [ + "${utils_path}:devicemanagerutils", + "//foundation/ace/napi:ace_napi", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//utils/native/base:utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DeviceManagerImplFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DeviceManagerImplFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/devicemanagerimpl_fuzzer/corpus/init b/test/fuzztest/devicemanagerimpl_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/devicemanagerimpl_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp b/test/fuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5bcdbaeb647eb940a195b7e8f060ef12f3ad7e7 --- /dev/null +++ b/test/fuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp @@ -0,0 +1,252 @@ +/* + * 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 + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include "device_manager_impl.h" +#include "device_manager.h" +#include "dm_constants.h" +#include "dm_log.h" +#include "device_manager_callback.h" +#include "dm_app_image_info.h" +#include "dm_device_info.h" +#include "dm_native_event.h" +#include "dm_subscribe_info.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nlohmann/json.hpp" +#include "native_devicemanager_js.h" +#include "device_manager_impl_fuzzer.h" + +const int nCapabiltyBufferSize = 65; + +namespace OHOS { +namespace DistributedHardware { +void InitDeviceManagerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string packName(reinterpret_cast(data), size); + std::string bundleName(reinterpret_cast(data), size); + napi_env env; + std::shared_ptr initCallback = std::make_shared(env, bundleName); + + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, initCallback); + ret = DeviceManager::GetInstance().UnInitDeviceManager(packName); +} + +void DeviceListFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string extra(reinterpret_cast(data), size); + std::vector devList; + std::string bundleName(reinterpret_cast(data), size); + DmDeviceInfo deviceInfo; + + int32_t ret = DeviceManager::GetInstance().GetTrustedDeviceList(bundleName, extra, devList); + ret = DeviceManager::GetInstance().GetLocalDeviceInfo(bundleName, deviceInfo); +} + +void DeviceDiscoveryFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string bundleName(reinterpret_cast(data), size); + + DmSubscribeInfo subInfo; + subInfo.subscribeId = *(reinterpret_cast(data)); + subInfo.mode = *(reinterpret_cast(data)); + subInfo.medium = *(reinterpret_cast(data)); + subInfo.freq = *(reinterpret_cast(data)); + subInfo.isSameAccount = *(reinterpret_cast(data)); + subInfo.isWakeRemote = *(reinterpret_cast(data)); + strncpy_s(subInfo.capability, DM_MAX_DEVICE_CAPABILITY_LEN, (char*)data, nCapabiltyBufferSize); + napi_env env; + std::string extra(reinterpret_cast(data), size); + int16_t subscribeId = *(reinterpret_cast(data)); + std::shared_ptr discoverCallback = + std::make_shared(env, bundleName); + + int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(bundleName, + subInfo, extra, discoverCallback); + ret = DeviceManager::GetInstance().StopDeviceDiscovery(bundleName, subscribeId); +} + +void AuthenticateDeviceFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string pkgName(reinterpret_cast(data), size); + int32_t authType = *(reinterpret_cast(data)); + DmDeviceInfo deviceInfo; + std::string extraString(reinterpret_cast(data), size); + napi_env env; + std::shared_ptr authCallback = + std::make_shared(env, pkgName); + + DeviceManager::GetInstance().AuthenticateDevice(pkgName, + authType, deviceInfo, extraString, authCallback); +} + +void UnAuthenticateDeviceFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string bundleName(reinterpret_cast(data), size); + std::string deviceId(reinterpret_cast(data), size); + + DeviceManager::GetInstance().UnAuthenticateDevice(bundleName, deviceId); +} + +void VerifyAuthenticationFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string bundleName(reinterpret_cast(data), size); + std::string authParam(reinterpret_cast(data), size); + napi_env env; + std::shared_ptr verifyCallback = + std::make_shared(env, bundleName); + + DeviceManager::GetInstance().VerifyAuthentication(bundleName, authParam, verifyCallback); +} + +void RegisterDeviceManagerFaCallbackFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string bundleName(reinterpret_cast(data), size); + std::string packageName(reinterpret_cast(data), size); + napi_env env; + auto callback = std::make_shared(env, bundleName); + + DeviceManager::GetInstance().RegisterDeviceManagerFaCallback(packageName, callback); +} + +void UnRegisterDeviceManagerFaCallbackFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string packName(reinterpret_cast(data), size); + DeviceManager::GetInstance().UnRegisterDeviceManagerFaCallback(packName); +} + +void GetFaParamFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string packName(reinterpret_cast(data), size); + DmAuthParam authParam; + + DeviceManager::GetInstance().GetFaParam(packName, authParam); +} + +void SetUserOperationFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string pkgName(reinterpret_cast(data), size); + int32_t action = *(reinterpret_cast(data)); + + DeviceManager::GetInstance().SetUserOperation(pkgName, action); +} + +void GetUdidByNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string pkgName(reinterpret_cast(data), size); + std::string netWorkId(reinterpret_cast(data), size); + std::string udid(reinterpret_cast(data), size); + + DeviceManager::GetInstance().GetUdidByNetworkId(pkgName, netWorkId, udid); +} + +void GetUuidByNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string packName(reinterpret_cast(data), size); + std::string netWorkId(reinterpret_cast(data), size); + std::string uuid(reinterpret_cast(data), size); + + DeviceManager::GetInstance().GetUuidByNetworkId(packName, netWorkId, uuid); +} + +void RegisterDevStateCallbackFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string bundleName(reinterpret_cast(data), size); + napi_env env; + auto callback = std::make_shared(env, bundleName); + std::string extra(reinterpret_cast(data), size); + + int32_t ret = DeviceManager::GetInstance().RegisterDevStateCallback(bundleName, + extra, callback); + ret = DeviceManager::GetInstance().UnRegisterDevStateCallback(bundleName); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::InitDeviceManagerFuzzTest(data, size); + OHOS::DistributedHardware::DeviceListFuzzTest(data, size); + OHOS::DistributedHardware::RegisterDevStateCallbackFuzzTest(data, size); + OHOS::DistributedHardware::DeviceDiscoveryFuzzTest(data, size); + OHOS::DistributedHardware::AuthenticateDeviceFuzzTest(data, size); + OHOS::DistributedHardware::UnAuthenticateDeviceFuzzTest(data, size); + OHOS::DistributedHardware::VerifyAuthenticationFuzzTest(data, size); + OHOS::DistributedHardware::RegisterDeviceManagerFaCallbackFuzzTest(data, size); + OHOS::DistributedHardware::UnRegisterDeviceManagerFaCallbackFuzzTest(data, size); + OHOS::DistributedHardware::GetFaParamFuzzTest(data, size); + OHOS::DistributedHardware::SetUserOperationFuzzTest(data, size); + OHOS::DistributedHardware::GetUdidByNetworkIdFuzzTest(data, size); + OHOS::DistributedHardware::GetUuidByNetworkIdFuzzTest(data, size); + + return 0; +} diff --git a/test/fuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.h b/test/fuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..2e4d1d838278e88631babab722819b8b84a0ff17 --- /dev/null +++ b/test/fuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 DEVICE_MANAGER_IMPL_FUZZER_H +#define DEVICE_MANAGER_IMPL_FUZZER_H + +#define FUZZ_PROJECT_NAME "devicemanagerimpl_fuzzer" + +#endif // DEVICE_MANAGER_IMPL_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/devicemanagerimpl_fuzzer/project.xml b/test/fuzztest/devicemanagerimpl_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/devicemanagerimpl_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/devicemanagernotify_fuzzer/BUILD.gn b/test/fuzztest/devicemanagernotify_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..81c5b2a5f5570f3ae5d1b0ff4b3f925939b93001 --- /dev/null +++ b/test/fuzztest/devicemanagernotify_fuzzer/BUILD.gn @@ -0,0 +1,101 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DeviceManagerNotifyFuzzTest") { + module_out_path = "devicemanager/devicemanagernotify_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/devicemanagernotify_fuzzer" + + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include", + "${services_path}/include/dependency/timer", + "${services_path}/include/discovery", + "${services_path}/include/dependency/softbus", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo/", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "device_manager_notify_fuzzer.cpp" ] + + deps = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DeviceManagerNotifyFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "ipc:ipc_core", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DeviceManagerNotifyFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/devicemanagernotify_fuzzer/corpus/init b/test/fuzztest/devicemanagernotify_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/devicemanagernotify_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/devicemanagernotify_fuzzer/device_manager_notify_fuzzer.cpp b/test/fuzztest/devicemanagernotify_fuzzer/device_manager_notify_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb45fa70e9a7566c28343df45ee08778d7636224 --- /dev/null +++ b/test/fuzztest/devicemanagernotify_fuzzer/device_manager_notify_fuzzer.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include "ipc_client_manager.h" +#include "ipc_set_useroperation_req.h" +#include "ipc_rsp.h" +#include "ipc_def.h" +#include "device_manager_notify.h" +#include "dm_device_info.h" +#include "ipc_client_stub.h" +#include "ipc_register_listener_req.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "dm_constants.h" +#include "system_ability_definition.h" +#include "device_manager_notify_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void DeviceManagerNotifyUnRegisterFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string pkgName(reinterpret_cast(data), size); + std::string deviceId(reinterpret_cast(data), size); + uint16_t subscribeId = *(reinterpret_cast(data)); + + DeviceManagerNotify::GetInstance().UnRegisterDeathRecipientCallback(pkgName); + DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName); + DeviceManagerNotify::GetInstance().UnRegisterPackageCallback(pkgName); + DeviceManagerNotify::GetInstance().UnRegisterVerifyAuthenticationCallback(pkgName); + DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName); + DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId); + DeviceManagerNotify::GetInstance().UnRegisterAuthenticateCallback(pkgName, deviceId); + DeviceManagerNotify::GetInstance().OnFaCall(pkgName, deviceId); +} +void DeviceManagerNotifyDeviceStatusFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string pkgName(reinterpret_cast(data), size); + std::string deviceId(reinterpret_cast(data), size); + std::string authPara(reinterpret_cast(data), size); + std::string token(reinterpret_cast(data), size); + uint16_t subscribeId = *(reinterpret_cast(data)); + int32_t resultCode = *(reinterpret_cast(data)); + int32_t flag = *(reinterpret_cast(data)); + int32_t failedReason = *(reinterpret_cast(data)); + uint32_t status = *(reinterpret_cast(data)); + uint32_t reason = *(reinterpret_cast(data)); + DmDeviceInfo deviceInfo; + + DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, deviceInfo); + DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, deviceInfo); + DeviceManagerNotify::GetInstance().OnDeviceChanged(pkgName, deviceInfo); + DeviceManagerNotify::GetInstance().OnDeviceReady(pkgName, deviceInfo); + DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, deviceInfo); + + DeviceManagerNotify::GetInstance().OnDiscoveryFailed(pkgName, subscribeId, failedReason); + DeviceManagerNotify::GetInstance().OnDiscoverySuccess(pkgName, subscribeId); + DeviceManagerNotify::GetInstance().OnAuthResult(pkgName, deviceId, token, status, reason); + DeviceManagerNotify::GetInstance().OnVerifyAuthResult(pkgName, deviceId, resultCode, flag); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::DeviceManagerNotifyUnRegisterFuzzTest(data, size); + OHOS::DistributedHardware::DeviceManagerNotifyDeviceStatusFuzzTest(data, size); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/devicemanagernotify_fuzzer/device_manager_notify_fuzzer.h b/test/fuzztest/devicemanagernotify_fuzzer/device_manager_notify_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..35bbace987f1b13df9ba28d394dfab8ec2cf2bf1 --- /dev/null +++ b/test/fuzztest/devicemanagernotify_fuzzer/device_manager_notify_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 DEVICE_MANAGER_NOTIFY_FUZZER_H +#define DEVICE_MANAGER_NOTIFY_FUZZER_H + +#define FUZZ_PROJECT_NAME "devicemanagernotify_fuzzer" + +#endif // DEVICE_MANAGER_NOTIFY_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/devicemanagernotify_fuzzer/project.xml b/test/fuzztest/devicemanagernotify_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/devicemanagernotify_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/devicemanagerservice_fuzzer/BUILD.gn b/test/fuzztest/devicemanagerservice_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ada13c730ce4bd3504afdc0946d2be75f2a4ce4e --- /dev/null +++ b/test/fuzztest/devicemanagerservice_fuzzer/BUILD.gn @@ -0,0 +1,101 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DeviceManagerServiceFuzzTest") { + module_out_path = "devicemanager/devicemanagerservice_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/devicemanagerservice_fuzzer" + + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include", + "${services_path}/include/dependency/timer", + "${services_path}/include/discovery", + "${services_path}/include/dependency/softbus", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo/", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "device_manager_service_fuzzer.cpp" ] + + deps = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DeviceManagerServiceFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "ipc:ipc_core", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DeviceManagerServiceFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/devicemanagerservice_fuzzer/corpus/init b/test/fuzztest/devicemanagerservice_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/devicemanagerservice_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp b/test/fuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78bf587cdef0f1986e3b890c0970e7c5dda00592 --- /dev/null +++ b/test/fuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp @@ -0,0 +1,61 @@ +/* + * 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 + * + * 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 +#include +#include "device_manager_service.h" +#include "device_manager_service_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void DeviceManagerServiceFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string pkgName(reinterpret_cast(data), size); + std::string extra(reinterpret_cast(data), size); + int32_t authType = *(reinterpret_cast(data)); + uint16_t subscribeId = *(reinterpret_cast(data)); + DmSubscribeInfo subscribeInfo; + std::vector deviceList; + DmDeviceInfo info; + DmAuthParam authParam; + + int32_t ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeInfo, extra); + ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(info); + ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList); + ret = DeviceManagerService::GetInstance().GetUdidByNetworkId(pkgName, pkgName, pkgName); + ret = DeviceManagerService::GetInstance().GetUuidByNetworkId(pkgName, pkgName, pkgName); + ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, pkgName, extra); + ret = DeviceManagerService::GetInstance().VerifyAuthentication(pkgName); + ret = DeviceManagerService::GetInstance().GetFaParam(pkgName, authParam); + ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, extra); + ret = DeviceManagerService::GetInstance().SetUserOperation(pkgName, authType); + ret = DeviceManagerService::GetInstance().RegisterDevStateCallback(pkgName, extra); + ret = DeviceManagerService::GetInstance().UnRegisterDevStateCallback(pkgName, extra); + ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::DeviceManagerServiceFuzzTest(data, size); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.h b/test/fuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..346817263387763b878561f960254beadf364d8b --- /dev/null +++ b/test/fuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 DEVICE_MANAGER_SERVICE_FUZZER_H +#define DEVICE_MANAGER_SERVICE_FUZZER_H + +#define FUZZ_PROJECT_NAME "devicemanagerservice_fuzzer" + +#endif // DEVICE_MANAGER_SERVICE_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/devicemanagerservice_fuzzer/project.xml b/test/fuzztest/devicemanagerservice_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/devicemanagerservice_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/ipcclientmanager_fuzzer/BUILD.gn b/test/fuzztest/ipcclientmanager_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..52efb6e4bf38bd79344fc0256d2ad5c38337de33 --- /dev/null +++ b/test/fuzztest/ipcclientmanager_fuzzer/BUILD.gn @@ -0,0 +1,146 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("IpcClientManagerFuzzTest") { + module_out_path = "devicemanager/ipcclientmanager_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/ipcclientmanager_fuzzer" + + include_dirs = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include/dependency/timer", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//third_party/json/include", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "ipc_client_manager_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"IpcClientManagerFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] + + public_deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":IpcClientManagerFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/ipcclientmanager_fuzzer/corpus/init b/test/fuzztest/ipcclientmanager_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/ipcclientmanager_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/ipcclientmanager_fuzzer/ipc_client_manager_fuzzer.cpp b/test/fuzztest/ipcclientmanager_fuzzer/ipc_client_manager_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39bf2b41751d0e35e9f248c799a3369c1820d984 --- /dev/null +++ b/test/fuzztest/ipcclientmanager_fuzzer/ipc_client_manager_fuzzer.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include "ipc_client_manager.h" +#include "ipc_client_manager_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void IpcClientManagerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string pkgName(reinterpret_cast(data), size); + int32_t cmdCode = *(reinterpret_cast(data)); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + + std::shared_ptr instance = std::make_shared(); + int32_t ret = instance->Init(pkgName); + ret = instance->UnInit(pkgName); + ret = instance->SendRequest(cmdCode, req, rsp); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::IpcClientManagerFuzzTest(data, size); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/ipcclientmanager_fuzzer/ipc_client_manager_fuzzer.h b/test/fuzztest/ipcclientmanager_fuzzer/ipc_client_manager_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..46ab9c81cb103de4033ef50687127e959ddeba4b --- /dev/null +++ b/test/fuzztest/ipcclientmanager_fuzzer/ipc_client_manager_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 IPC_CLIENT_MANAGER_FUZZER_H +#define IPC_CLIENT_MANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "ipcclientmanager_fuzzer" + +#endif // IPC_CLIENT_MANAGER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/ipcclientmanager_fuzzer/project.xml b/test/fuzztest/ipcclientmanager_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/ipcclientmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/ipccmdregister_fuzzer/BUILD.gn b/test/fuzztest/ipccmdregister_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..02ed42753823b9793d54f0513962ce29ba555127 --- /dev/null +++ b/test/fuzztest/ipccmdregister_fuzzer/BUILD.gn @@ -0,0 +1,147 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("IpcCmdRegisterFuzzTest") { + module_out_path = "devicemanager/ipccmdregister_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/ipccmdregister_fuzzer" + + include_dirs = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include/dependency/timer", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//third_party/json/include", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/ipc/lite", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "ipc_cmd_register_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"IpcCmdRegisterFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] + + public_deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":IpcCmdRegisterFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/ipccmdregister_fuzzer/corpus/init b/test/fuzztest/ipccmdregister_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/ipccmdregister_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/ipccmdregister_fuzzer/ipc_cmd_register_fuzzer.cpp b/test/fuzztest/ipccmdregister_fuzzer/ipc_cmd_register_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8fa8300c6efcae510fc3dcd5a3b2e946eecf6b9 --- /dev/null +++ b/test/fuzztest/ipccmdregister_fuzzer/ipc_cmd_register_fuzzer.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include "ipc_cmd_register.h" +#include "ipc_client_manager.h" +#include "ipc_register_listener_req.h" +#include "ipc_get_trustdevice_rsp.h" +#include "ipc_get_info_by_network_rsp.h" +#include "ipc_get_info_by_network_req.h" +#include "ipc_get_trustdevice_req.h" +#include "ipc_start_discovery_req.h" +#include "ipc_stop_discovery_req.h" +#include "ipc_get_dmfaparam_rsp.h" +#include "ipc_register_dev_state_callback_req.h" +#include "ipc_set_useroperation_req.h" +#include "ipc_verify_authenticate_req.h" +#include "ipc_unauthenticate_device_req.h" +#include "ipc_authenticate_device_req.h" +#include "ipc_get_local_device_info_rsp.h" +#include "device_manager_notify.h" +#include "ipc_req.h" +#include "dm_device_info.h" +#include "dm_constants.h" +#include "ipc_def.h" +#include "dm_log.h" +#include "ipc_server_listenermgr.h" +#include "ipc_rsp.h" +#include "ipc_cmd_register_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void IpcCmdRegisterFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + int32_t cmdCode = *(reinterpret_cast(data)); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + MessageParcel data1; + MessageParcel reply1; + + int32_t ret = IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, data1); + ret = IpcCmdRegister::GetInstance().ReadResponse(cmdCode, data1, rsp); + ret = IpcCmdRegister::GetInstance().OnIpcCmd(cmdCode, data1, reply1); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::IpcCmdRegisterFuzzTest(data, size); + + return 0; +} diff --git a/test/fuzztest/ipccmdregister_fuzzer/ipc_cmd_register_fuzzer.h b/test/fuzztest/ipccmdregister_fuzzer/ipc_cmd_register_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..4e2e06d2b251debce2ac19ca0d0f0072acb3a7e6 --- /dev/null +++ b/test/fuzztest/ipccmdregister_fuzzer/ipc_cmd_register_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 IPC_CMD_REGISTER_FUZZER_H +#define IPC_CMD_REGISTER_FUZZER_H + +#define FUZZ_PROJECT_NAME "ipccmdregister_fuzzer" + +#endif // IPC_CMD_REGISTER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/ipccmdregister_fuzzer/project.xml b/test/fuzztest/ipccmdregister_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/ipccmdregister_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/ipcserverclientproxy_fuzzer/BUILD.gn b/test/fuzztest/ipcserverclientproxy_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..330bffd6bb0c9b2e32ac0d69739f20fdcb71e634 --- /dev/null +++ b/test/fuzztest/ipcserverclientproxy_fuzzer/BUILD.gn @@ -0,0 +1,146 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("IpcServerClientProxyFuzzTest") { + module_out_path = "devicemanager/ipcserverclientproxy_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/ipcserverclientproxy_fuzzer" + + include_dirs = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include/dependency/timer", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//third_party/json/include", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "ipc_server_client_proxy_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"IpcServerClientProxyFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] + + public_deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":IpcServerClientProxyFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/ipcserverclientproxy_fuzzer/corpus/init b/test/fuzztest/ipcserverclientproxy_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/ipcserverclientproxy_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/ipcserverclientproxy_fuzzer/ipc_server_client_proxy_fuzzer.cpp b/test/fuzztest/ipcserverclientproxy_fuzzer/ipc_server_client_proxy_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc4b171f257c7ad668fb2bb171209e7328e9935c --- /dev/null +++ b/test/fuzztest/ipcserverclientproxy_fuzzer/ipc_server_client_proxy_fuzzer.cpp @@ -0,0 +1,53 @@ +/* + * 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 + * + * 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 +#include +#include +#include +#include +#include "ipc_server_client_proxy.h" +#include "ipc_server_client_proxy_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void IpcServerClientProxyFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + int32_t cmdCode = *(reinterpret_cast(data)); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + sptr remoteObject = nullptr; + + auto instance = new IpcServerClientProxy(remoteObject); + if (instance != nullptr) { + instance->SendCmd(cmdCode, req, rsp); + delete instance; + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::IpcServerClientProxyFuzzTest(data, size); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/ipcserverclientproxy_fuzzer/ipc_server_client_proxy_fuzzer.h b/test/fuzztest/ipcserverclientproxy_fuzzer/ipc_server_client_proxy_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..05c7d3d6c698feb844bd2e836b8f3426504d4c31 --- /dev/null +++ b/test/fuzztest/ipcserverclientproxy_fuzzer/ipc_server_client_proxy_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 IPC_SERVER_CLIENT_PROXY_FUZZER_H +#define IPC_SERVER_CLIENT_PROXY_FUZZER_H + +#define FUZZ_PROJECT_NAME "ipcserverclientproxy_fuzzer" + +#endif // IPC_SERVER_CLIENT_PROXY_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/ipcserverclientproxy_fuzzer/project.xml b/test/fuzztest/ipcserverclientproxy_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/ipcserverclientproxy_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/ipcserverlistener_fuzzer/BUILD.gn b/test/fuzztest/ipcserverlistener_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..804d2b453f07fa771b1ec450ec980adbc3a66430 --- /dev/null +++ b/test/fuzztest/ipcserverlistener_fuzzer/BUILD.gn @@ -0,0 +1,146 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("IpcServerListenerFuzzTest") { + module_out_path = "devicemanager/ipcserverlistener_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/ipcserverlistener_fuzzer" + + include_dirs = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include/dependency/timer", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//third_party/json/include", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "ipc_server_listener_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"IpcServerListenerFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] + + public_deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":IpcServerListenerFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/ipcserverlistener_fuzzer/corpus/init b/test/fuzztest/ipcserverlistener_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/ipcserverlistener_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/ipcserverlistener_fuzzer/ipc_server_listener_fuzzer.cpp b/test/fuzztest/ipcserverlistener_fuzzer/ipc_server_listener_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4e8ff62be41c889722ca8eebeb4e5c429fc8900 --- /dev/null +++ b/test/fuzztest/ipcserverlistener_fuzzer/ipc_server_listener_fuzzer.cpp @@ -0,0 +1,49 @@ +/* + * 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 + * + * 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 +#include +#include +#include +#include +#include "ipc_server_listener.h" +#include "ipc_server_listener_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void IpcServerListenerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + int32_t cmdCode = *(reinterpret_cast(data)); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + + std::shared_ptr ipcServerListener = std::make_shared(); + int32_t ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + ret = ipcServerListener->SendAll(cmdCode, req, rsp); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::IpcServerListenerFuzzTest(data, size); + + return 0; +} diff --git a/test/fuzztest/ipcserverlistener_fuzzer/ipc_server_listener_fuzzer.h b/test/fuzztest/ipcserverlistener_fuzzer/ipc_server_listener_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..4c80252e8c5ef9d6b5384201e736d4375a1e37a5 --- /dev/null +++ b/test/fuzztest/ipcserverlistener_fuzzer/ipc_server_listener_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 IPC_SERVER_LISTENER_FUZZER_H +#define IPC_SERVER_LISTENER_FUZZER_H + +#define FUZZ_PROJECT_NAME "ipcserverlistener_fuzzer" + +#endif // IPC_SERVER_LISTENER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/ipcserverlistener_fuzzer/project.xml b/test/fuzztest/ipcserverlistener_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/ipcserverlistener_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/ipcserverstub_fuzzer/BUILD.gn b/test/fuzztest/ipcserverstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..dabc2a15f9ed9f972e9475d3922c56a9ac799f99 --- /dev/null +++ b/test/fuzztest/ipcserverstub_fuzzer/BUILD.gn @@ -0,0 +1,146 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("IpcServerStubFuzzTest") { + module_out_path = "devicemanager/ipcserverstub_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/ipcserverstub_fuzzer" + + include_dirs = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include/dependency/timer", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//third_party/json/include", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "ipc_server_stub_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"IpcServerStubFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] + + public_deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":IpcServerStubFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/ipcserverstub_fuzzer/corpus/init b/test/fuzztest/ipcserverstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/ipcserverstub_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/ipcserverstub_fuzzer/ipc_server_stub_fuzzer.cpp b/test/fuzztest/ipcserverstub_fuzzer/ipc_server_stub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b05ae5c91d2207c88062b544e57de1ae560ed484 --- /dev/null +++ b/test/fuzztest/ipcserverstub_fuzzer/ipc_server_stub_fuzzer.cpp @@ -0,0 +1,66 @@ +/* + * 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 + * + * 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 +#include +#include + +#include "ipc_client_stub.h" +#include "dm_device_info.h" +#include "ipc_server_stub.h" +#include "device_manager_impl.h" +#include "dm_constants.h" +#include "if_system_ability_manager.h" +#include "ipc_cmd_register.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" +#include "ipc_server_stub_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void IpcServerStubFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + uint32_t code = *(reinterpret_cast(data)); + MessageParcel data1; + MessageParcel reply; + MessageOption option; + std::string pkgName(reinterpret_cast(data), size); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + sptr listener = data1.ReadRemoteObject(); + + int32_t ret = IpcServerStub::GetInstance().OnRemoteRequest(code, data1, reply, option); + ret = IpcServerStub::GetInstance().SendCmd(code, req, rsp); + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); + sptr ipcPtr = IpcServerStub::GetInstance().GetDmListener(pkgName); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::IpcServerStubFuzzTest(data, size); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/ipcserverstub_fuzzer/ipc_server_stub_fuzzer.h b/test/fuzztest/ipcserverstub_fuzzer/ipc_server_stub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..7d9f7876fecf453348dc75b1421f81e73a603c19 --- /dev/null +++ b/test/fuzztest/ipcserverstub_fuzzer/ipc_server_stub_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 IPC_SERVER_STUB_FUZZER_H +#define IPC_SERVER_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "ipcserverstub_fuzzer" + +#endif // IPC_SERVER_STUB_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/ipcserverstub_fuzzer/project.xml b/test/fuzztest/ipcserverstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/ipcserverstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/nativedevicemanagerjs_fuzzer/BUILD.gn b/test/fuzztest/nativedevicemanagerjs_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1659eca5b8d68e376929e27edc5441ef365d73ad --- /dev/null +++ b/test/fuzztest/nativedevicemanagerjs_fuzzer/BUILD.gn @@ -0,0 +1,146 @@ +# 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/devicemanager/devicemanager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("NativeDevicemanagerJsFuzzTest") { + module_out_path = "devicemanager/nativedevicemanagerjs_fuzzer" + fuzz_config_file = "//foundation/distributedhardware/devicemanager/test/fuzztest/nativedevicemanagerjs_fuzzer" + + include_dirs = [ + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js/include", + "//third_party/node/src", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include/dependency/timer", + "${services_path}/include/dependency/softbus", + "${services_path}/include/authentication", + "${services_path}/include/adapter", + "${services_path}/include", + "${services_path}/include/ipc/standard", + "${services_path}/include/discovery", + "${services_path}/include/dependency/hichain", + "${services_path}/include/deviceinfo", + "${services_path}/include/devicestate", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedhardware/devicemanager/ext/mini/services/devicemanagerservice/include/dispatch", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/distributedhardware/devicemanager/ext/profile/include", + "//foundation/deviceprofile/device_profile_core/interfaces/innerkits/core/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/distributedhardware/devicemanager/ext/mini/common/include", + "//base/security/deviceauth/interfaces/innerkits", + "${services_path}/include/ability", + "${services_path}/include/config", + "//utils/native/lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//third_party/bounds_checking_function/include", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", + "//third_party/json/include", + "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite", + "//foundation/ace/napi/interfaces/innerkits/napi", + "//foundation/ace/napi/interfaces/innerkits", + "//third_party/jsframework/runtime/main/extend", + "//foundation/windowmanager/interfaces/kits", + "//third_party/jsframework/runtime/main/extend/systemplugin", + "//third_party/node/benchmark", + "//foundation/windowmanager/interfaces/kits/napi/screen_runtime", + "//third_party/libuv/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "native_devicemanager_js_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"NativeDevicemanagerJsFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] + + public_deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":NativeDevicemanagerJsFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/nativedevicemanagerjs_fuzzer/corpus/init b/test/fuzztest/nativedevicemanagerjs_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /dev/null +++ b/test/fuzztest/nativedevicemanagerjs_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/nativedevicemanagerjs_fuzzer/native_devicemanager_js_fuzzer.cpp b/test/fuzztest/nativedevicemanagerjs_fuzzer/native_devicemanager_js_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9ac34f9259548c453087f94820e471e4cc697f6 --- /dev/null +++ b/test/fuzztest/nativedevicemanagerjs_fuzzer/native_devicemanager_js_fuzzer.cpp @@ -0,0 +1,95 @@ +/* + * 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 + * + * 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 +#include "native_devicemanager_js.h" +#include "device_manager.h" +#include "dm_constants.h" +#include "dm_log.h" +#include "device_manager_callback.h" +#include "dm_app_image_info.h" +#include "dm_device_info.h" +#include "dm_native_event.h" +#include "dm_subscribe_info.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nlohmann/json.hpp" +#include "native_devicemanager_js_fuzzer.h" + +namespace OHOS { +namespace DistributedHardware { +void NativeDeviceManagerStaticFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + napi_env env = *(reinterpret_cast(data)); + napi_callback_info info = *(reinterpret_cast(data)); + std::string buldleName(reinterpret_cast(data), size); + DeviceManagerNapi *deviceManagerNapiPtr = DeviceManagerNapi::GetDeviceManagerNapi(buldleName); + if (deviceManagerNapiPtr == nullptr) { + return; + } + + napi_value result = deviceManagerNapiPtr->CreateDeviceManager(env, info); + result = deviceManagerNapiPtr->GetTrustedDeviceListSync(env, info); + result = deviceManagerNapiPtr->AuthenticateDevice(env, info); + result = deviceManagerNapiPtr->VerifyAuthInfo(env, info); + result = deviceManagerNapiPtr->JsOn(env, info); + result = deviceManagerNapiPtr->JsOff(env, info); + result = deviceManagerNapiPtr->ReleaseDeviceManager(env, info); +} + +void NativeDeviceManagerOnFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + std::string buldleName(reinterpret_cast(data), size); + std::string token(reinterpret_cast(data), size); + int32_t reason = *(reinterpret_cast(data)); + uint16_t subscribeId = *(reinterpret_cast(data)); + DmNapiDevStateChangeAction action = *(reinterpret_cast(data)); + DmDeviceInfo deviceInfo = *(reinterpret_cast(data)); + + uv_work_t *work = new (std::nothrow) uv_work_t; + std::unique_ptr jsCallback_; + jsCallback_ = std::make_unique(buldleName, 0, 0, deviceInfo); + work->data = reinterpret_cast(jsCallback_.get()); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + delete work; + + DeviceManagerNapi *deviceManagerNapiPtr = DeviceManagerNapi::GetDeviceManagerNapi(buldleName); + if (deviceManagerNapiPtr == nullptr) { + return; + } + deviceManagerNapiPtr->OnDeviceStateChange(action, callback->deviceInfo_); + deviceManagerNapiPtr->OnDeviceFound(subscribeId, callback->deviceInfo_); + deviceManagerNapiPtr->OnDiscoveryFailed(subscribeId, reason); + deviceManagerNapiPtr->OnAuthResult(buldleName, token, reason, reason); + deviceManagerNapiPtr->OnVerifyResult(buldleName, reason, reason); + deviceManagerNapiPtr->OnDmfaCall(buldleName); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::NativeDeviceManagerStaticFuzzTest(data, size); + OHOS::DistributedHardware::NativeDeviceManagerOnFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/nativedevicemanagerjs_fuzzer/native_devicemanager_js_fuzzer.h b/test/fuzztest/nativedevicemanagerjs_fuzzer/native_devicemanager_js_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..89ec6f7ed3986065e56183290471b2c31d147c02 --- /dev/null +++ b/test/fuzztest/nativedevicemanagerjs_fuzzer/native_devicemanager_js_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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 NATIVE_DEVICEMANAGER_JS_FUZZER_H +#define NATIVE_DEVICEMANAGER_JS_FUZZER_H + +#define FUZZ_PROJECT_NAME "nativedevicemanagerjs_fuzzer" + +#endif // NATIVE_DEVICEMANAGER_JS_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/nativedevicemanagerjs_fuzzer/project.xml b/test/fuzztest/nativedevicemanagerjs_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..27c26d11b7d89f7aef7835fb5f95f9e5ecaf6f9d --- /dev/null +++ b/test/fuzztest/nativedevicemanagerjs_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/unittest/UTTest_auth_request_state.cpp b/test/unittest/UTTest_auth_request_state.cpp index 8b22a6c9f73323392512133c5c79f607e85d5413..4669afacd697e230bb56346ba05d2d4b5f527e5a 100644 --- a/test/unittest/UTTest_auth_request_state.cpp +++ b/test/unittest/UTTest_auth_request_state.cpp @@ -35,9 +35,6 @@ void AuthRequestStateTest::TearDownTestCase() } namespace { -std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask"; -std::string ADD_TIMEOUT_TASK = "addTimeoutTask"; - std::shared_ptr softbusConnector = std::make_shared(); std::shared_ptr listener = std::make_shared(); std::shared_ptr hiChainConnector = std::make_shared(); @@ -184,8 +181,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->timerHeap_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -310,6 +306,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->timerHeap_ = std::make_shared(); authRequestState->SetAuthManager(authManager); std::shared_ptr context = std::make_shared(); context->deviceId = "123456"; @@ -364,8 +361,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->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -426,8 +422,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->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -605,8 +600,7 @@ 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->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -665,8 +659,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->timerHeap_ = 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 fc28c482191cdfc5d7b9a9b3b96df917762b3853..1e85f56adce0b70ac44e0a598aa2d7731c899608 100644 --- a/test/unittest/UTTest_auth_response_state.cpp +++ b/test/unittest/UTTest_auth_response_state.cpp @@ -238,6 +238,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->timerHeap_ = 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 4250b6f275b0c9dbe62de7c8e1a6f6ba3496c4b6..2ef3244b358b60ddc3771d78969185d355da0fa2 100644 --- a/test/unittest/UTTest_dm_auth_manager.cpp +++ b/test/unittest/UTTest_dm_auth_manager.cpp @@ -225,8 +225,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->timerHeap_ = 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_timer.cpp b/test/unittest/UTTest_dm_timer.cpp index affcd4655e5e30e4c4c546b198d43399dcd8e8b3..8fac1a272cbf8a53566c1d7444ddb32a0971e56f 100644 --- a/test/unittest/UTTest_dm_timer.cpp +++ b/test/unittest/UTTest_dm_timer.cpp @@ -23,232 +23,331 @@ namespace OHOS { namespace DistributedHardware { -void DmTimerTest::SetUp() +void TimeHeapTest::SetUp() { } -void DmTimerTest::TearDown() +void TimeHeapTest::TearDown() { } -void DmTimerTest::SetUpTestCase() +void TimeHeapTest::SetUpTestCase() { } -void DmTimerTest::TearDownTestCase() +void TimeHeapTest::TearDownTestCase() { } namespace { +static void TimeOut(void *data, std::string timerName) +{ + // time out; +} + +/** + * @tc.name: TimeHeapTest::Tick_001 + * @tc.desc: Timeout event trigger + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(TimeHeapTest, Tick_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->hsize_ = 0; + int32_t ret = timerHeap->Tick(); + EXPECT_EQ(DM_AUTH_NO_TIMER, ret); +} + /** - * @tc.name: DmTimerTest::DmTimer_001 - * @tc.desc: to check when name is empty + * @tc.name: TimeHeapTest::Tick_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, DmTimer_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, Tick_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 10, TimeOut, timer); + int32_t ret = timerHeap->Tick(); + EXPECT_EQ(DM_OK, ret); + timerHeap->DelAll(); +} + +/** + * @tc.name: TimeHeapTest::Tick_003 + * @tc.desc: Timeout event trigger + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(TimeHeapTest, Tick_003, testing::ext::TestSize.Level0) +{ + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 10, TimeOut, timer); + timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 20, TimeOut, timer); + int32_t ret = timerHeap->Tick(); + EXPECT_EQ(DM_OK, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::DmTimer_002 - * @tc.desc: to check when name is not empty + * @tc.name: TimeHeapTest::MoveUp_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, DmTimer_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, MoveUp_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "123"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - EXPECT_EQ(DmTimerStatus::DM_STATUS_INIT, Timer_->mStatus_); - EXPECT_EQ(0, Timer_->mTimeOutSec_); - EXPECT_EQ(nullptr, Timer_->mHandle_); - EXPECT_EQ(nullptr, Timer_->mHandleData_); - EXPECT_EQ(0, Timer_->mTimeFd_[1]); - EXPECT_EQ(0, Timer_->mEv_.events); - EXPECT_EQ(0, Timer_->mEvents_[0].events); - EXPECT_EQ(0, Timer_->mEpFd_); - EXPECT_EQ(DM_TIMER_TASK, Timer_->mTimerName_); + std::shared_ptr timerHeap = std::make_shared(); + std::shared_ptr timer = nullptr; + int32_t ret = timerHeap->MoveUp(timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Start_001 - * @tc.desc: to check when mTimerName_ is empty,handle is nullptr,data is nullptr + * @tc.name: TimeHeapTest::MoveUp_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Start_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, MoveUp_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - uint32_t timeOut = 10; - TimeoutHandle handle = nullptr; - void *data = nullptr; - DmTimerStatus timerStatus = Timer_->Start(timeOut, handle, data); - EXPECT_EQ(DM_STATUS_FINISH, timerStatus); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + std::shared_ptr timer = std::make_shared(name, timeout + time(NULL), nullptr, TimeOut); + timerHeap->hsize_ = 0; + int32_t ret = timerHeap->MoveUp(timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Start_002 - * @tc.desc: to check when mTimerName_ is not empty, handle is not nullptr, data is not nullptr, - * @mStatus_ != DmTimerStatus::DM_STATUS_INIT + * @tc.name: TimeHeapTest::MoveUp_003 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -static void TimeOutTest(void *data, DmTimer& timer) +HWTEST_F(TimeHeapTest, MoveUp_003, testing::ext::TestSize.Level0) { - LOGE("time out test"); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + std::shared_ptr timer = std::make_shared(name, timeout + time(NULL), nullptr, TimeOut); + timerHeap->hsize_ = 1; + int32_t ret = timerHeap->MoveUp(timer); + EXPECT_EQ(DM_OK, ret); + + timerHeap->DelAll(); } -HWTEST_F(DmTimerTest, Start_002, testing::ext::TestSize.Level0) +// /** +// * @tc.name: TimeHeapTest::Run_001 +// * @tc.desc: Timeout event trigger +// * @tc.type: FUNC +// * @tc.require: AR000GHSJK +// */ +// HWTEST_F(TimeHeapTest, Run_001, testing::ext::TestSize.Level0) +// { +// std::shared_ptr timerHeap = std::make_shared(); +// timerHeap->Run(); +// } + +/** + * @tc.name: TimeHeapTest::AddTimer_001 + * @tc.desc: Timeout event trigger + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(TimeHeapTest, AddTimer_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "1234"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - uint32_t timeOut = 10; - int idx = 1; - void *data = &idx; - Timer_->mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; - DmTimerStatus timerStatus = Timer_->Start(timeOut, TimeOutTest, data); - EXPECT_EQ(DmTimerStatus::DM_STATUS_BUSY, timerStatus); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_OK, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Stop_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::AddTimer_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Stop_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t code = 1; - Timer_->Stop(code); + std::string name = ""; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Stop_002 - * @tc.desc: to check when mTimerName_ is not empty + * @tc.name: TimeHeapTest::AddTimer_003 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Stop_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_003, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t code = 1; - Timer_->Stop(code); + std::string name = "timer"; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Stop_003 - * @tc.desc: to check when mTimerName_ is not empty,mTimeFd_[1] is not 0 + * @tc.name: TimeHeapTest::AddTimer_004 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Stop_003, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_004, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t code = 1; - Timer_->mTimeFd_[1] = 1; - Timer_->Stop(code); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = -1; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::WaitForTimeout_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::AddTimer_005 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, WaitForTimeout_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_005, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->WaitForTimeout(); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = -1; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::WaitForTimeout_002 - * @tc.desc: to check when mTimerName_ is not empty + * @tc.name: TimeHeapTest::DelTimer_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, WaitForTimeout_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->WaitForTimeout(); + std::string name = ""; + + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_INVALID_VALUE, ret); } /** - * @tc.name: DmTimerTest::CreateTimeFd_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::DelTimer_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, CreateTimeFd_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t result = Timer_->CreateTimeFd(); - EXPECT_EQ(DM_STATUS_FINISH, result); + std::string name = "timer"; + + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_INVALID_VALUE, ret); } /** - * @tc.name: DmTimerTest::CreateTimeFd_002 - * @tc.desc: to check when mTimerName_ is not empty + * @tc.name: TimeHeapTest::DelTimer_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, CreateTimeFd_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_003, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "123"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->CreateTimeFd(); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_INVALID_VALUE, ret); } /** - * @tc.name: DmTimerTest::Release_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::DelTimer_004 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Release_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_004, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->Release(); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->AddTimer(name, timeout, TimeOut, timer); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_OK, ret); } /** - * @tc.name: DmTimerTest::Release_002 - * @tc.desc: to check when mTimerName_ is not empty,mStatus_ is 0 + * @tc.name: TimeHeapTest::DelAll_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Release_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelAll_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->mStatus_ = DmTimerStatus::DM_STATUS_INIT; - Timer_->Release(); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->AddTimer(name, timeout, TimeOut, timer); + int32_t ret = timerHeap->DelAll(); + EXPECT_EQ(DM_OK, ret); + EXPECT_EQ(timerHeap->hsize_, 0); } /** - * @tc.name: DmTimerTest::GetTimerName_001 - * @tc.desc: to check whether the return value is the same as mTimerName_ + * @tc.name: TimeHeapTest::DelAll_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, GetTimerName_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelAll_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - std::string strTimer = Timer_->GetTimerName(); - EXPECT_EQ(DM_TIMER_TASK, strTimer); + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelAll(); + EXPECT_EQ(DM_OK, ret); } } } diff --git a/test/unittest/UTTest_dm_timer.h b/test/unittest/UTTest_dm_timer.h index c697c779f4115ecb0f6b358b2cfd5d68a2018140..6f8302e20893e5294a7c3c486a0c198eb24cdc0c 100644 --- a/test/unittest/UTTest_dm_timer.h +++ b/test/unittest/UTTest_dm_timer.h @@ -14,12 +14,12 @@ */ #ifndef OHOS_UTTEST_DM_TIMER_H #define OHOS_UTTEST_DM_TIMER_H - +#define private public #include namespace OHOS { namespace DistributedHardware { -class DmTimerTest : public testing::Test { +class TimeHeapTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase();