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
25 namespace OHOS {
26 namespace DistributedHardware {
SetUp()27 void SoftbusSessionTest::SetUp()
28 {
29 }
TearDown()30 void SoftbusSessionTest::TearDown()
31 {
32 }
SetUpTestCase()33 void SoftbusSessionTest::SetUpTestCase()
34 {
35 }
TearDownTestCase()36 void SoftbusSessionTest::TearDownTestCase()
37 {
38 }
39
40 namespace {
41 std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
42 std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
43 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
44 std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
45 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
46 std::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 */
55 HWTEST_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 */
71 HWTEST_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 */
87 HWTEST_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 */
104 HWTEST_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 */
126 HWTEST_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 */
147 HWTEST_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 */
159 HWTEST_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 */
172 HWTEST_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: CloseUnbindSession_001
184 * @tc.desc: set socket = 1, and return DM_OK
185 * @tc.type: FUNC
186 */
187 HWTEST_F(SoftbusSessionTest, CloseUnbindSession_001, testing::ext::TestSize.Level0)
188 {
189 int32_t socket = 1;
190 if (softbusSession == nullptr) {
191 softbusSession = std::make_shared<SoftbusSession>();
192 }
193 int32_t ret = softbusSession->CloseUnbindSession(socket);
194 EXPECT_EQ(ret, DM_OK);
195 }
196
197 /**
198 * @tc.name: GetPeerDeviceId_001
199 * @tc.desc: set sessionId = 3 and return DM_OK
200 * @tc.type: FUNC
201 * @tc.require: AR000GHSJK
202 */
203 HWTEST_F(SoftbusSessionTest, GetPeerDeviceId_001, testing::ext::TestSize.Level0)
204 {
205 int32_t sessionId = 3;
206 std::string peerDevId;
207 if (softbusSession == nullptr) {
208 softbusSession = std::make_shared<SoftbusSession>();
209 }
210 int ret = softbusSession->GetPeerDeviceId(sessionId, peerDevId);
211 EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT);
212 }
213
214 /**
215 * @tc.name: RegisterSessionCallback_001
216 * @tc.desc: set info to null and return DM_OK
217 * @tc.type: FUNC
218 * @tc.require: AR000GHSJK
219 */
220 HWTEST_F(SoftbusSessionTest, RegisterSessionCallback_001, testing::ext::TestSize.Level0)
221 {
222 std::shared_ptr<ISoftbusSessionCallback> callback;
223 if (softbusSession == nullptr) {
224 softbusSession = std::make_shared<SoftbusSession>();
225 }
226 int ret = softbusSession->RegisterSessionCallback(callback);
227 EXPECT_EQ(ret, DM_OK);
228 }
229
230 /**
231 * @tc.name: UnRegisterSessionCallback_001
232 * @tc.desc: set info to null and return ERR_DM_FAILED
233 * @tc.type: FUNC
234 * @tc.require: AR000GHSJK
235 */
236 HWTEST_F(SoftbusSessionTest, UnRegisterSessionCallback_001, testing::ext::TestSize.Level0)
237 {
238 if (softbusSession == nullptr) {
239 softbusSession = std::make_shared<SoftbusSession>();
240 }
241 int ret = softbusSession->UnRegisterSessionCallback();
242 EXPECT_EQ(ret, DM_OK);
243 }
244
245 /**
246 * @tc.name: OnSessionOpened_001
247 * @tc.desc: return DM_OK
248 * @tc.type: FUNC
249 * @tc.require: AR000GHSJK
250 */
251 HWTEST_F(SoftbusSessionTest, OnSessionOpened_001, testing::ext::TestSize.Level0)
252 {
253 if (softbusSession == nullptr) {
254 softbusSession = std::make_shared<SoftbusSession>();
255 }
256 softbusSession->RegisterSessionCallback(discoveryMgr);
257 int sessionId = 1;
258 int result = 0;
259 void *data = nullptr;
260 unsigned int dataLen = 1;
261 softbusSession->OnBytesReceived(sessionId, data, dataLen);
262 softbusSession->OnBytesReceived(sessionId, data, -1);
263 sessionId = -1;
264 softbusSession->OnBytesReceived(sessionId, data, dataLen);
265 int ret = softbusSession->OnSessionOpened(sessionId, result);
266 softbusSession->OnSessionClosed(sessionId);
267 EXPECT_EQ(ret, DM_OK);
268 }
269 } // namespace
270 } // namespace DistributedHardware
271 } // namespace OHOS
272