1/*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "UTTest_softbus_session.h"
16
17#include "dm_anonymous.h"
18#include "dm_constants.h"
19#include "dm_log.h"
20#include "nlohmann/json.hpp"
21#include "softbus_connector.h"
22#include "softbus_session.h"
23#include "softbus_error_code.h"
24
25namespace OHOS {
26namespace DistributedHardware {
27void SoftbusSessionTest::SetUp()
28{
29}
30void SoftbusSessionTest::TearDown()
31{
32}
33void SoftbusSessionTest::SetUpTestCase()
34{
35}
36void SoftbusSessionTest::TearDownTestCase()
37{
38}
39
40namespace {
41std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
42std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
43std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
44std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
45std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
46std::shared_ptr<DmAuthManager> discoveryMgr =
47    std::make_shared<DmAuthManager>(softbusConnector, hiChainConnector, listener, hiChainAuthConnector);
48
49/**
50 * @tc.name: OpenAuthSession_001
51 * @tc.desc: set deviceId =null, return sessionId(1)
52 * @tc.type: FUNC
53 * @tc.require: AR000GHSJK
54 */
55HWTEST_F(SoftbusSessionTest, OpenAuthSession_001, testing::ext::TestSize.Level0)
56{
57    std::string deviceId = "";
58    if (softbusSession == nullptr) {
59        softbusSession = std::make_shared<SoftbusSession>();
60    }
61    int ret = softbusSession->OpenAuthSession(deviceId);
62    EXPECT_EQ(ret, -1);
63}
64
65/**
66 * @tc.name: OpenAuthSession_002
67 * @tc.desc: set deviceId = "123456";and return sessionId
68 * @tc.type: FUNC
69 * @tc.require: AR000GHSJK
70 */
71HWTEST_F(SoftbusSessionTest, OpenAuthSession_002, testing::ext::TestSize.Level0)
72{
73    std::string deviceId = "123456";
74    if (softbusSession == nullptr) {
75        softbusSession = std::make_shared<SoftbusSession>();
76    }
77    int ret = softbusSession->OpenAuthSession(deviceId);
78    EXPECT_EQ(ret, -1);
79}
80
81/**
82 * @tc.name: SendData_001
83 * @tc.desc: set message null and return ERR_DM_FAILED
84 * @tc.type: FUNC
85 * @tc.require: AR000GHSJK
86 */
87HWTEST_F(SoftbusSessionTest, SendData_001, testing::ext::TestSize.Level0)
88{
89    std::string message = "";
90    int32_t sessionId = -1;
91    if (softbusSession == nullptr) {
92        softbusSession = std::make_shared<SoftbusSession>();
93    }
94    int ret = softbusSession->SendData(sessionId, message);
95    EXPECT_EQ(ret, ERR_DM_FAILED);
96}
97
98/**
99 * @tc.name: SendData_002
100 * @tc.desc: set sessionId = 0, go to the SendBytes'smaster and return SOFTBUS_TRANS_SESSION_SERVER_NOINIT
101 * @tc.type: FUNC
102 * @tc.require: AR000GHSJK
103 */
104HWTEST_F(SoftbusSessionTest, SendData_002, testing::ext::TestSize.Level0)
105{
106    int32_t msgType = 2;
107    nlohmann::json jsonObj;
108    jsonObj[TAG_VER] = DM_ITF_VER;
109    jsonObj[TAG_MSG_TYPE] = msgType;
110    std::string message = jsonObj.dump();
111    int32_t sessionId = 0;
112    if (softbusSession == nullptr) {
113        softbusSession = std::make_shared<SoftbusSession>();
114    }
115    softbusSession->RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback>(discoveryMgr));
116    int ret = softbusSession->SendData(sessionId, message);
117    EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT);
118}
119
120/**
121 * @tc.name: SendData_003
122 * @tc.desc: set jsonObject[TAG_MSG_TYPE] is string and return ERR_DM_FAILED
123 * @tc.type: FUNC
124 * @tc.require: AR000GHSJK
125 */
126HWTEST_F(SoftbusSessionTest, SendData_003, testing::ext::TestSize.Level0)
127{
128    std::string message = R"(
129    {
130        "MSG_TYPE": "messageTest"
131    }
132    )";
133    int32_t sessionId = 0;
134    if (softbusSession == nullptr) {
135        softbusSession = std::make_shared<SoftbusSession>();
136    }
137    int32_t ret = softbusSession->SendData(sessionId, message);
138    EXPECT_EQ(ret, ERR_DM_FAILED);
139}
140
141/**
142 * @tc.name: SoftbusSession_001
143 * @tc.desc: set SoftbusSession to make a new pointer, and it not nullptr
144 * @tc.type: FUNC
145 * @tc.require: AR000GHSJK
146 */
147HWTEST_F(SoftbusSessionTest, SoftbusSession_001, testing::ext::TestSize.Level0)
148{
149    std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>();
150    ASSERT_NE(m_SoftbusSession, nullptr);
151}
152
153/**
154 * @tc.name: SoftbusSession_002
155 * @tc.desc: set SoftbusSession to make a new pointer, it not nullptr and delete it
156 * @tc.type: FUNC
157 * @tc.require: AR000GHSJK
158 */
159HWTEST_F(SoftbusSessionTest, SoftbusSession_002, testing::ext::TestSize.Level0)
160{
161    std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>();
162    m_SoftbusSession.reset();
163    EXPECT_EQ(m_SoftbusSession, nullptr);
164}
165
166/**
167 * @tc.name: CloseAuthSession_001
168 * @tc.desc: set sessionId = 3, and return DM_OK
169 * @tc.type: FUNC
170 * @tc.require: AR000GHSJK
171 */
172HWTEST_F(SoftbusSessionTest, CloseAuthSession_001, testing::ext::TestSize.Level0)
173{
174    int32_t sessionId = 3;
175    if (softbusSession == nullptr) {
176        softbusSession = std::make_shared<SoftbusSession>();
177    }
178    int ret = softbusSession->CloseAuthSession(sessionId);
179    EXPECT_EQ(ret, DM_OK);
180}
181
182/**
183 * @tc.name: GetPeerDeviceId_001
184 * @tc.desc: set sessionId = 3 and return DM_OK
185 * @tc.type: FUNC
186 * @tc.require: AR000GHSJK
187 */
188HWTEST_F(SoftbusSessionTest, GetPeerDeviceId_001, testing::ext::TestSize.Level0)
189{
190    int32_t sessionId = 3;
191    std::string peerDevId;
192    if (softbusSession == nullptr) {
193        softbusSession = std::make_shared<SoftbusSession>();
194    }
195    int ret = softbusSession->GetPeerDeviceId(sessionId, peerDevId);
196    EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT);
197}
198
199/**
200 * @tc.name: RegisterSessionCallback_001
201 * @tc.desc: set info to null and return DM_OK
202 * @tc.type: FUNC
203 * @tc.require: AR000GHSJK
204 */
205HWTEST_F(SoftbusSessionTest, RegisterSessionCallback_001, testing::ext::TestSize.Level0)
206{
207    std::shared_ptr<ISoftbusSessionCallback> callback;
208    if (softbusSession == nullptr) {
209        softbusSession = std::make_shared<SoftbusSession>();
210    }
211    int ret = softbusSession->RegisterSessionCallback(callback);
212    EXPECT_EQ(ret, DM_OK);
213}
214
215/**
216 * @tc.name: UnRegisterSessionCallback_001
217 * @tc.desc: set info to null and return ERR_DM_FAILED
218 * @tc.type: FUNC
219 * @tc.require: AR000GHSJK
220 */
221HWTEST_F(SoftbusSessionTest, UnRegisterSessionCallback_001, testing::ext::TestSize.Level0)
222{
223    if (softbusSession == nullptr) {
224        softbusSession = std::make_shared<SoftbusSession>();
225    }
226    int ret = softbusSession->UnRegisterSessionCallback();
227    EXPECT_EQ(ret, DM_OK);
228}
229
230/**
231 * @tc.name: OnSessionOpened_001
232 * @tc.desc: return DM_OK
233 * @tc.type: FUNC
234 * @tc.require: AR000GHSJK
235 */
236HWTEST_F(SoftbusSessionTest, OnSessionOpened_001, testing::ext::TestSize.Level0)
237{
238    if (softbusSession == nullptr) {
239        softbusSession = std::make_shared<SoftbusSession>();
240    }
241    softbusSession->RegisterSessionCallback(discoveryMgr);
242    int sessionId = 1;
243    int result = 0;
244    void *data = nullptr;
245    unsigned int dataLen = 1;
246    softbusSession->OnBytesReceived(sessionId, data, dataLen);
247    softbusSession->OnBytesReceived(sessionId, data, -1);
248    sessionId = -1;
249    softbusSession->OnBytesReceived(sessionId, data, dataLen);
250    int ret = softbusSession->OnSessionOpened(sessionId, result);
251    softbusSession->OnSessionClosed(sessionId);
252    EXPECT_EQ(ret, DM_OK);
253}
254} // namespace
255} // namespace DistributedHardware
256} // namespace OHOS
257