/* * 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 "dm_auth_manager.h" #include "nlohmann/json.hpp" #include "dm_log.h" #include "dm_constants.h" #include "auth_message_processor.h" #include "UTTest_dm_auth_manager.h" namespace OHOS { namespace DistributedHardware { void DmAuthManagerTest::SetUp() { } void DmAuthManagerTest::TearDown() { } void DmAuthManagerTest::SetUpTestCase() { } void DmAuthManagerTest::TearDownTestCase() { } namespace { std::string AUTHENTICATE_TIMEOUT_TASK = "authenticateTimeoutTask"; std::string NEGOTIATE_TIMEOUT_TASK = "negotiateTimeoutTask"; std::string CONFIRM_TIMEOUT_TASK = "confirmTimeoutTask"; 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(); /** * @tc.name: DmAuthManager::UnAuthenticateDevice_001 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_ FAILED * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, UnAuthenticateDevice_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr sessionSession = std::shared_ptr(); std::shared_ptr authRequestContext = std::make_shared(); std::string pkgName = ""; std::string deviceId = "222"; int32_t ret = authManager->UnAuthenticateDevice(pkgName, deviceId); ASSERT_EQ(ret, DM_FAILED); } /** * @tc.name: DmAuthManager::HandleAuthenticateTimeout_001 * @tc.desc: authResponseContext_= nullptr; Call handleauthenticatemeout to check whether the return value is DM_FAILED * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authRequestState = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->authResponseContext_ = nullptr; authManager->SetAuthRequestState(authRequestState); int32_t ret = authManager->HandleAuthenticateTimeout(); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::HandleAuthenticateTimeout_002 * @tc.desc: authResponseContext_= nullptr; Call handleauthenticatemeout to check whether the return value is DM_OK * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authRequestState = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->SetAuthRequestState(authRequestState); int32_t ret = authManager->HandleAuthenticateTimeout(); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::EstablishAuthChannel_001 * @tc.desc: Call establishauthchannel to check whether the return value is DM_ FAILED * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, EstablishAuthChannel_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr sessionSession = std::shared_ptr(); std::shared_ptr authRequestContext = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); std::string deviceId1; int32_t ret = authManager->EstablishAuthChannel(deviceId1); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::StartAuthProcess_001 * @tc.desc: Whether the return value of calling startauthprocess is DM_FAILED * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, StartAuthProcess_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authResponseState = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authResponseState_ = std::make_shared(); authManager->SetAuthResponseState(authResponseState); int32_t action = 0; authManager->StartAuthProcess(action); bool ret = authManager->authRequestContext_->deviceName.empty(); ASSERT_EQ(ret, true); } /** * @tc.name: DmAuthManager::StartAuthProcess_002 * @tc.desc: Whether the return value of calling startauthprocess is DM_ OK * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, StartAuthProcess_002, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authResponseState = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authResponseState_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->SetAuthResponseState(authResponseState); authManager->authResponseContext_->sessionId = 111; authManager->softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authManager); int32_t action = 1; int32_t ret = authManager->StartAuthProcess(action); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::CreateGroup_001 * @tc.desc: Whether the return value of calling creategroup is DM_ OK * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, CreateGroup_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authResponseState = std::make_shared(); std::shared_ptr hiChainConnector = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authResponseState_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->SetAuthResponseState(authResponseState); authManager->authResponseContext_->requestId = 111; authManager->authResponseContext_->groupName = "111"; int32_t ret = authManager->CreateGroup(); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::AddMember_001 * @tc.desc: Whether the return value of calling addmember is DM_ OK * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, AddMember_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authResponseState = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); std::shared_ptr hiChainConnector = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); nlohmann::json jsonObject; authManager->authResponseContext_->groupId = "111"; authManager->authResponseContext_->groupName = "222"; authManager->authResponseContext_->code = 123; authManager->authResponseContext_->requestId = 234; authManager->authResponseContext_->deviceId = "234"; std::string deviceId = "44444"; authManager->hiChainConnector_->RegisterHiChainCallback(authManager); authManager->SetAuthResponseState(authResponseState); int32_t ret = authManager->AddMember(deviceId); ASSERT_EQ(ret, DM_OK); sleep(15); } /** * @tc.name: DmAuthManager::JoinNetwork_001 * @tc.desc: Whether the return value of calling joinnetwork is DM_ OK * @tc.type: FUNC * @tc.require: AR000GHSJK */ 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->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->SetAuthRequestState(authRequestState); int32_t ret = authManager->JoinNetwork(); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::SetAuthResponseState_001 * @tc.desc: Is the authresponsestate assignment successful * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, SetAuthResponseState_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authResponseState = std::make_shared(); authManager->authResponseState_ = std::make_shared(); authManager->SetAuthResponseState(authResponseState); int32_t ret = authManager->SetAuthResponseState(authResponseState); ASSERT_EQ(ret, DM_OK); } /** * @tc.name: DmAuthManager::GetPinCode_001 * @tc.desc: Return authresponsecontext - > code * @tc.type: FUNC * @tc.require: AR000GHSJK */ HWTEST_F(DmAuthManagerTest, GetPinCode_001, testing::ext::TestSize.Level0) { std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); authManager->authResponseContext_ = std::make_shared(); int32_t ret = authManager->GetPinCode(); ASSERT_EQ(ret, authManager->authResponseContext_->code); } } // namespace } // namespace DistributedHardware } // namespace OHOS