179a732c7Sopenharmony_ci/* 279a732c7Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License. 579a732c7Sopenharmony_ci * You may obtain a copy of the License at 679a732c7Sopenharmony_ci * 779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 879a732c7Sopenharmony_ci * 979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and 1379a732c7Sopenharmony_ci * limitations under the License. 1479a732c7Sopenharmony_ci */ 1579a732c7Sopenharmony_ci#include "UTTest_softbus_session.h" 1679a732c7Sopenharmony_ci 1779a732c7Sopenharmony_ci#include "dm_anonymous.h" 1879a732c7Sopenharmony_ci#include "dm_constants.h" 1979a732c7Sopenharmony_ci#include "dm_log.h" 2079a732c7Sopenharmony_ci#include "nlohmann/json.hpp" 2179a732c7Sopenharmony_ci#include "softbus_connector.h" 2279a732c7Sopenharmony_ci#include "softbus_session.h" 2379a732c7Sopenharmony_ci#include "softbus_error_code.h" 2479a732c7Sopenharmony_ci 2579a732c7Sopenharmony_cinamespace OHOS { 2679a732c7Sopenharmony_cinamespace DistributedHardware { 2779a732c7Sopenharmony_civoid SoftbusSessionTest::SetUp() 2879a732c7Sopenharmony_ci{ 2979a732c7Sopenharmony_ci} 3079a732c7Sopenharmony_civoid SoftbusSessionTest::TearDown() 3179a732c7Sopenharmony_ci{ 3279a732c7Sopenharmony_ci} 3379a732c7Sopenharmony_civoid SoftbusSessionTest::SetUpTestCase() 3479a732c7Sopenharmony_ci{ 3579a732c7Sopenharmony_ci} 3679a732c7Sopenharmony_civoid SoftbusSessionTest::TearDownTestCase() 3779a732c7Sopenharmony_ci{ 3879a732c7Sopenharmony_ci} 3979a732c7Sopenharmony_ci 4079a732c7Sopenharmony_cinamespace { 4179a732c7Sopenharmony_cistd::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>(); 4279a732c7Sopenharmony_cistd::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>(); 4379a732c7Sopenharmony_cistd::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>(); 4479a732c7Sopenharmony_cistd::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>(); 4579a732c7Sopenharmony_cistd::shared_ptr<HiChainAuthConnector> hiChainAuthConnector = std::make_shared<HiChainAuthConnector>(); 4679a732c7Sopenharmony_cistd::shared_ptr<DmAuthManager> discoveryMgr = 4779a732c7Sopenharmony_ci std::make_shared<DmAuthManager>(softbusConnector, hiChainConnector, listener, hiChainAuthConnector); 4879a732c7Sopenharmony_ci 4979a732c7Sopenharmony_ci/** 5079a732c7Sopenharmony_ci * @tc.name: OpenAuthSession_001 5179a732c7Sopenharmony_ci * @tc.desc: set deviceId =null, return sessionId(1) 5279a732c7Sopenharmony_ci * @tc.type: FUNC 5379a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 5479a732c7Sopenharmony_ci */ 5579a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, OpenAuthSession_001, testing::ext::TestSize.Level0) 5679a732c7Sopenharmony_ci{ 5779a732c7Sopenharmony_ci std::string deviceId = ""; 5879a732c7Sopenharmony_ci if (softbusSession == nullptr) { 5979a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 6079a732c7Sopenharmony_ci } 6179a732c7Sopenharmony_ci int ret = softbusSession->OpenAuthSession(deviceId); 6279a732c7Sopenharmony_ci EXPECT_EQ(ret, -1); 6379a732c7Sopenharmony_ci} 6479a732c7Sopenharmony_ci 6579a732c7Sopenharmony_ci/** 6679a732c7Sopenharmony_ci * @tc.name: OpenAuthSession_002 6779a732c7Sopenharmony_ci * @tc.desc: set deviceId = "123456";and return sessionId 6879a732c7Sopenharmony_ci * @tc.type: FUNC 6979a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 7079a732c7Sopenharmony_ci */ 7179a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, OpenAuthSession_002, testing::ext::TestSize.Level0) 7279a732c7Sopenharmony_ci{ 7379a732c7Sopenharmony_ci std::string deviceId = "123456"; 7479a732c7Sopenharmony_ci if (softbusSession == nullptr) { 7579a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 7679a732c7Sopenharmony_ci } 7779a732c7Sopenharmony_ci int ret = softbusSession->OpenAuthSession(deviceId); 7879a732c7Sopenharmony_ci EXPECT_EQ(ret, -1); 7979a732c7Sopenharmony_ci} 8079a732c7Sopenharmony_ci 8179a732c7Sopenharmony_ci/** 8279a732c7Sopenharmony_ci * @tc.name: SendData_001 8379a732c7Sopenharmony_ci * @tc.desc: set message null and return ERR_DM_FAILED 8479a732c7Sopenharmony_ci * @tc.type: FUNC 8579a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 8679a732c7Sopenharmony_ci */ 8779a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, SendData_001, testing::ext::TestSize.Level0) 8879a732c7Sopenharmony_ci{ 8979a732c7Sopenharmony_ci std::string message = ""; 9079a732c7Sopenharmony_ci int32_t sessionId = -1; 9179a732c7Sopenharmony_ci if (softbusSession == nullptr) { 9279a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 9379a732c7Sopenharmony_ci } 9479a732c7Sopenharmony_ci int ret = softbusSession->SendData(sessionId, message); 9579a732c7Sopenharmony_ci EXPECT_EQ(ret, ERR_DM_FAILED); 9679a732c7Sopenharmony_ci} 9779a732c7Sopenharmony_ci 9879a732c7Sopenharmony_ci/** 9979a732c7Sopenharmony_ci * @tc.name: SendData_002 10079a732c7Sopenharmony_ci * @tc.desc: set sessionId = 0, go to the SendBytes'smaster and return SOFTBUS_TRANS_SESSION_SERVER_NOINIT 10179a732c7Sopenharmony_ci * @tc.type: FUNC 10279a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 10379a732c7Sopenharmony_ci */ 10479a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, SendData_002, testing::ext::TestSize.Level0) 10579a732c7Sopenharmony_ci{ 10679a732c7Sopenharmony_ci int32_t msgType = 2; 10779a732c7Sopenharmony_ci nlohmann::json jsonObj; 10879a732c7Sopenharmony_ci jsonObj[TAG_VER] = DM_ITF_VER; 10979a732c7Sopenharmony_ci jsonObj[TAG_MSG_TYPE] = msgType; 11079a732c7Sopenharmony_ci std::string message = jsonObj.dump(); 11179a732c7Sopenharmony_ci int32_t sessionId = 0; 11279a732c7Sopenharmony_ci if (softbusSession == nullptr) { 11379a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 11479a732c7Sopenharmony_ci } 11579a732c7Sopenharmony_ci softbusSession->RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback>(discoveryMgr)); 11679a732c7Sopenharmony_ci int ret = softbusSession->SendData(sessionId, message); 11779a732c7Sopenharmony_ci EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT); 11879a732c7Sopenharmony_ci} 11979a732c7Sopenharmony_ci 12079a732c7Sopenharmony_ci/** 12179a732c7Sopenharmony_ci * @tc.name: SendData_003 12279a732c7Sopenharmony_ci * @tc.desc: set jsonObject[TAG_MSG_TYPE] is string and return ERR_DM_FAILED 12379a732c7Sopenharmony_ci * @tc.type: FUNC 12479a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 12579a732c7Sopenharmony_ci */ 12679a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, SendData_003, testing::ext::TestSize.Level0) 12779a732c7Sopenharmony_ci{ 12879a732c7Sopenharmony_ci std::string message = R"( 12979a732c7Sopenharmony_ci { 13079a732c7Sopenharmony_ci "MSG_TYPE": "messageTest" 13179a732c7Sopenharmony_ci } 13279a732c7Sopenharmony_ci )"; 13379a732c7Sopenharmony_ci int32_t sessionId = 0; 13479a732c7Sopenharmony_ci if (softbusSession == nullptr) { 13579a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 13679a732c7Sopenharmony_ci } 13779a732c7Sopenharmony_ci int32_t ret = softbusSession->SendData(sessionId, message); 13879a732c7Sopenharmony_ci EXPECT_EQ(ret, ERR_DM_FAILED); 13979a732c7Sopenharmony_ci} 14079a732c7Sopenharmony_ci 14179a732c7Sopenharmony_ci/** 14279a732c7Sopenharmony_ci * @tc.name: SoftbusSession_001 14379a732c7Sopenharmony_ci * @tc.desc: set SoftbusSession to make a new pointer, and it not nullptr 14479a732c7Sopenharmony_ci * @tc.type: FUNC 14579a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 14679a732c7Sopenharmony_ci */ 14779a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, SoftbusSession_001, testing::ext::TestSize.Level0) 14879a732c7Sopenharmony_ci{ 14979a732c7Sopenharmony_ci std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>(); 15079a732c7Sopenharmony_ci ASSERT_NE(m_SoftbusSession, nullptr); 15179a732c7Sopenharmony_ci} 15279a732c7Sopenharmony_ci 15379a732c7Sopenharmony_ci/** 15479a732c7Sopenharmony_ci * @tc.name: SoftbusSession_002 15579a732c7Sopenharmony_ci * @tc.desc: set SoftbusSession to make a new pointer, it not nullptr and delete it 15679a732c7Sopenharmony_ci * @tc.type: FUNC 15779a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 15879a732c7Sopenharmony_ci */ 15979a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, SoftbusSession_002, testing::ext::TestSize.Level0) 16079a732c7Sopenharmony_ci{ 16179a732c7Sopenharmony_ci std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>(); 16279a732c7Sopenharmony_ci m_SoftbusSession.reset(); 16379a732c7Sopenharmony_ci EXPECT_EQ(m_SoftbusSession, nullptr); 16479a732c7Sopenharmony_ci} 16579a732c7Sopenharmony_ci 16679a732c7Sopenharmony_ci/** 16779a732c7Sopenharmony_ci * @tc.name: CloseAuthSession_001 16879a732c7Sopenharmony_ci * @tc.desc: set sessionId = 3, and return DM_OK 16979a732c7Sopenharmony_ci * @tc.type: FUNC 17079a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 17179a732c7Sopenharmony_ci */ 17279a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, CloseAuthSession_001, testing::ext::TestSize.Level0) 17379a732c7Sopenharmony_ci{ 17479a732c7Sopenharmony_ci int32_t sessionId = 3; 17579a732c7Sopenharmony_ci if (softbusSession == nullptr) { 17679a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 17779a732c7Sopenharmony_ci } 17879a732c7Sopenharmony_ci int ret = softbusSession->CloseAuthSession(sessionId); 17979a732c7Sopenharmony_ci EXPECT_EQ(ret, DM_OK); 18079a732c7Sopenharmony_ci} 18179a732c7Sopenharmony_ci 18279a732c7Sopenharmony_ci/** 18379a732c7Sopenharmony_ci * @tc.name: GetPeerDeviceId_001 18479a732c7Sopenharmony_ci * @tc.desc: set sessionId = 3 and return DM_OK 18579a732c7Sopenharmony_ci * @tc.type: FUNC 18679a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 18779a732c7Sopenharmony_ci */ 18879a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, GetPeerDeviceId_001, testing::ext::TestSize.Level0) 18979a732c7Sopenharmony_ci{ 19079a732c7Sopenharmony_ci int32_t sessionId = 3; 19179a732c7Sopenharmony_ci std::string peerDevId; 19279a732c7Sopenharmony_ci if (softbusSession == nullptr) { 19379a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 19479a732c7Sopenharmony_ci } 19579a732c7Sopenharmony_ci int ret = softbusSession->GetPeerDeviceId(sessionId, peerDevId); 19679a732c7Sopenharmony_ci EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT); 19779a732c7Sopenharmony_ci} 19879a732c7Sopenharmony_ci 19979a732c7Sopenharmony_ci/** 20079a732c7Sopenharmony_ci * @tc.name: RegisterSessionCallback_001 20179a732c7Sopenharmony_ci * @tc.desc: set info to null and return DM_OK 20279a732c7Sopenharmony_ci * @tc.type: FUNC 20379a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 20479a732c7Sopenharmony_ci */ 20579a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, RegisterSessionCallback_001, testing::ext::TestSize.Level0) 20679a732c7Sopenharmony_ci{ 20779a732c7Sopenharmony_ci std::shared_ptr<ISoftbusSessionCallback> callback; 20879a732c7Sopenharmony_ci if (softbusSession == nullptr) { 20979a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 21079a732c7Sopenharmony_ci } 21179a732c7Sopenharmony_ci int ret = softbusSession->RegisterSessionCallback(callback); 21279a732c7Sopenharmony_ci EXPECT_EQ(ret, DM_OK); 21379a732c7Sopenharmony_ci} 21479a732c7Sopenharmony_ci 21579a732c7Sopenharmony_ci/** 21679a732c7Sopenharmony_ci * @tc.name: UnRegisterSessionCallback_001 21779a732c7Sopenharmony_ci * @tc.desc: set info to null and return ERR_DM_FAILED 21879a732c7Sopenharmony_ci * @tc.type: FUNC 21979a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 22079a732c7Sopenharmony_ci */ 22179a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, UnRegisterSessionCallback_001, testing::ext::TestSize.Level0) 22279a732c7Sopenharmony_ci{ 22379a732c7Sopenharmony_ci if (softbusSession == nullptr) { 22479a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 22579a732c7Sopenharmony_ci } 22679a732c7Sopenharmony_ci int ret = softbusSession->UnRegisterSessionCallback(); 22779a732c7Sopenharmony_ci EXPECT_EQ(ret, DM_OK); 22879a732c7Sopenharmony_ci} 22979a732c7Sopenharmony_ci 23079a732c7Sopenharmony_ci/** 23179a732c7Sopenharmony_ci * @tc.name: OnSessionOpened_001 23279a732c7Sopenharmony_ci * @tc.desc: return DM_OK 23379a732c7Sopenharmony_ci * @tc.type: FUNC 23479a732c7Sopenharmony_ci * @tc.require: AR000GHSJK 23579a732c7Sopenharmony_ci */ 23679a732c7Sopenharmony_ciHWTEST_F(SoftbusSessionTest, OnSessionOpened_001, testing::ext::TestSize.Level0) 23779a732c7Sopenharmony_ci{ 23879a732c7Sopenharmony_ci if (softbusSession == nullptr) { 23979a732c7Sopenharmony_ci softbusSession = std::make_shared<SoftbusSession>(); 24079a732c7Sopenharmony_ci } 24179a732c7Sopenharmony_ci softbusSession->RegisterSessionCallback(discoveryMgr); 24279a732c7Sopenharmony_ci int sessionId = 1; 24379a732c7Sopenharmony_ci int result = 0; 24479a732c7Sopenharmony_ci void *data = nullptr; 24579a732c7Sopenharmony_ci unsigned int dataLen = 1; 24679a732c7Sopenharmony_ci softbusSession->OnBytesReceived(sessionId, data, dataLen); 24779a732c7Sopenharmony_ci softbusSession->OnBytesReceived(sessionId, data, -1); 24879a732c7Sopenharmony_ci sessionId = -1; 24979a732c7Sopenharmony_ci softbusSession->OnBytesReceived(sessionId, data, dataLen); 25079a732c7Sopenharmony_ci int ret = softbusSession->OnSessionOpened(sessionId, result); 25179a732c7Sopenharmony_ci softbusSession->OnSessionClosed(sessionId); 25279a732c7Sopenharmony_ci EXPECT_EQ(ret, DM_OK); 25379a732c7Sopenharmony_ci} 25479a732c7Sopenharmony_ci} // namespace 25579a732c7Sopenharmony_ci} // namespace DistributedHardware 25679a732c7Sopenharmony_ci} // namespace OHOS 257