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
16 #include <gtest/gtest.h>
17 #include <string>
18
19 #include "client_trans_session_manager.h"
20 #include "client_trans_session_service.h"
21 #include "client_trans_socket_manager.h"
22 #include "dfs_session.h"
23 #include "inner_session.h"
24 #include "session.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_common.h"
27 #include "softbus_def.h"
28 #include "softbus_errcode.h"
29 #include "trans_log.h"
30
31 using namespace testing::ext;
32
33 namespace OHOS {
34
35 ConnectionAddr g_addrInfo;
36
37 const char *g_testSessionName = "ohos.distributedschedule.dms.test";
38 std::string g_testData = "TranSessionTest_GetSessionKeyTestData";
39
40 #define TEST_FILE_NAME "test.filename.01"
41 #define TEST_PKG_NAME_LEN (64)
42 #define TEST_SESSION_NAME_LEN (64)
43 #define TEST_NETWORK_ID_LEN (64)
44 #define TEST_GROUP_ID_LEN (64)
45
46 class TransSessionTest : public testing::Test {
47 public:
TransSessionTest()48 TransSessionTest()
49 {}
~TransSessionTest()50 ~TransSessionTest()
51 {}
52 static void SetUpTestCase(void);
53 static void TearDownTestCase(void);
SetUp()54 void SetUp() override
55 {}
TearDown()56 void TearDown() override
57 {}
58 };
59
SetUpTestCase(void)60 void TransSessionTest::SetUpTestCase(void)
61 {
62 (void)TransClientInit();
63 }
64
TearDownTestCase(void)65 void TransSessionTest::TearDownTestCase(void)
66 {
67 }
OnSessionOpened(int sessionId,int result)68 static int OnSessionOpened(int sessionId, int result)
69 {
70 TRANS_LOGI(TRANS_TEST, "session opened, sesisonId=%{public}d", sessionId);
71 return SOFTBUS_OK;
72 }
73
OnSessionClosed(int sessionId)74 static void OnSessionClosed(int sessionId)
75 {
76 TRANS_LOGI(TRANS_TEST, "session closed, sessionId=%{public}d", sessionId);
77 }
78
79 static ISessionListener g_sessionlistener = {
80 .OnSessionOpened = OnSessionOpened,
81 .OnSessionClosed = OnSessionClosed,
82 };
83
84 /**
85 * @tc.name: GetSessionKeyTest001
86 * @tc.desc: use the wrong parameter.
87 * @tc.type: FUNC
88 * @tc.require: I5HZ6N
89 */
90 HWTEST_F(TransSessionTest, GetSessionKeyTest001, TestSize.Level0)
91 {
92 int32_t ret;
93 int32_t sessionId = 1;
94 char *key = const_cast<char *>(g_testData.c_str());
95 unsigned int len = strlen(key);
96
97 ret = GetSessionKey(-1, key, len);
98 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
99
100 ret = GetSessionKey(sessionId, NULL, len);
101 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
102
103 ret = GetSessionKey(sessionId, key, 0);
104 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
105
106 ret = GetSessionKey(sessionId, key, SESSION_KEY_LEN - 1);
107 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
108 }
109
110 /**
111 * @tc.name: GetSessionKeyTest002
112 * @tc.desc: use the normal parameter.
113 * @tc.type: FUNC
114 * @tc.require: I5HZ6N
115 */
116 HWTEST_F(TransSessionTest, GetSessionKeyTest002, TestSize.Level0)
117 {
118 int32_t ret;
119 int32_t sessionId = 1;
120 char *key = const_cast<char *>(g_testData.c_str());
121 unsigned int len = strlen(key);
122
123 ret = GetSessionKey(sessionId, key, len);
124 EXPECT_EQ(SOFTBUS_TRANS_FUNC_NOT_SUPPORT, ret);
125 }
126
127 /**
128 * @tc.name: GetSessionHandleTest001
129 * @tc.desc: use the wrong parameter.
130 * @tc.type: FUNC
131 * @tc.require: I5HZ6N
132 */
133 HWTEST_F(TransSessionTest, GetSessionHandleTest001, TestSize.Level0)
134 {
135 int32_t ret;
136 int32_t sessionId = 1;
137 int handle = 1;
138
139 ret = GetSessionHandle(-1, &handle);
140 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
141
142 ret = GetSessionHandle(sessionId, NULL);
143 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
144 }
145
146 /**
147 * @tc.name: GetSessionHandleTest002
148 * @tc.desc: use the normal parameter.
149 * @tc.type: FUNC
150 * @tc.require: I5HZ6N
151 */
152 HWTEST_F(TransSessionTest, GetSessionHandleTest002, TestSize.Level0)
153 {
154 int32_t ret;
155 int32_t sessionId = 1;
156 int handle = 1;
157
158 ret = GetSessionHandle(sessionId, &handle);
159 EXPECT_EQ(SOFTBUS_TRANS_FUNC_NOT_SUPPORT, ret);
160 }
161
162 /**
163 * @tc.name: DisableSessionListenerTest001
164 * @tc.desc: use the wrong parameter.
165 * @tc.type: FUNC
166 * @tc.require: I5HZ6N
167 */
168 HWTEST_F(TransSessionTest, DisableSessionListenerTest001, TestSize.Level0)
169 {
170 int32_t ret;
171
172 ret = DisableSessionListener(-1);
173 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
174 }
175
176 /**
177 * @tc.name: DisableSessionListenerTest002
178 * @tc.desc: use the normal parameter.
179 * @tc.type: FUNC
180 * @tc.require: I5HZ6N
181 */
182 HWTEST_F(TransSessionTest, DisableSessionListenerTest002, TestSize.Level0)
183 {
184 int32_t ret;
185 int32_t sessionId = 1;
186
187 ret = DisableSessionListener(sessionId);
188 EXPECT_EQ(SOFTBUS_TRANS_FUNC_NOT_SUPPORT, ret);
189 }
190
191 /**
192 * @tc.name: OpenAuthSessionTest001
193 * @tc.desc: use the wrong parameter.
194 * @tc.type: FUNC
195 * @tc.require: I5HZ6N
196 */
197 HWTEST_F(TransSessionTest, OpenAuthSessionTest001, TestSize.Level0)
198 {
199 int ret;
200
201 ret = OpenAuthSession(NULL, &(g_addrInfo), 1, NULL);
202 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
203
204 ret = OpenAuthSession(g_testSessionName, NULL, 1, NULL);
205 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
206
207 ret = OpenAuthSession(g_testSessionName, &(g_addrInfo), -1, NULL);
208 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
209 }
210
211 /**
212 * @tc.name: OpenAuthSessionTest002
213 * @tc.desc: use the normal parameter.
214 * @tc.type: FUNC
215 * @tc.require:
216 */
217 HWTEST_F(TransSessionTest, OpenAuthSessionTest002, TestSize.Level0)
218 {
219 int ret;
220
221 ret = OpenAuthSession(g_testSessionName, &(g_addrInfo), 1, NULL);
222 EXPECT_GE(SOFTBUS_OK, ret);
223 }
224
225 /**
226 * @tc.name: NotifyAuthSuccessTest001
227 * @tc.desc: use the normal parameter.
228 * @tc.type: FUNC
229 * @tc.require:
230 */
231 HWTEST_F(TransSessionTest, NotifyAuthSuccessTest001, TestSize.Level0)
232 {
233 int32_t ret = TransClientInit();
234 EXPECT_EQ(SOFTBUS_OK, ret);
235 int32_t sessionId = 1;
236 NotifyAuthSuccess(sessionId);
237 }
238
239 /**
240 * @tc.name: SendFileTest001
241 * @tc.desc: use the normal parameter.
242 * @tc.type: FUNC
243 * @tc.require:
244 */
245 HWTEST_F(TransSessionTest, SendFileTest001, TestSize.Level0)
246 {
247 int32_t sessionId = 1;
248 int32_t ret = SendFile(sessionId, NULL, NULL, 1);
249 EXPECT_NE(SOFTBUS_OK, ret);
250
251 const char *sFileList[] = { TEST_FILE_NAME };
252 ret = SendFile(sessionId, sFileList, NULL, 0);
253 EXPECT_NE(SOFTBUS_OK, ret);
254
255 ret = SendFile(sessionId, sFileList, NULL, 1);
256 EXPECT_NE(SOFTBUS_OK, ret);
257 }
258
259 /**
260 * @tc.name: SendFileTest002
261 * @tc.desc: use the normal parameter.
262 * @tc.type: FUNC
263 * @tc.require:
264 */
265 HWTEST_F(TransSessionTest, SendFileTest002, TestSize.Level0)
266 {
267 char pkgName[TEST_PKG_NAME_LEN] = "com.test.trans.session";
268 char mySessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
269 char peerSessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
270 char peerNetworkId[TEST_NETWORK_ID_LEN] = "1234567789";
271 char groupId[TEST_GROUP_ID_LEN] = "123";
272 SessionAttribute attr;
273 attr.dataType = 1;
274 attr.linkTypeNum = 0;
275 SessionParam param = {
276 .sessionName = mySessionName,
277 .peerSessionName = peerSessionName,
278 .peerDeviceId = peerNetworkId,
279 .groupId = groupId,
280 .attr = &attr,
281 };
282
283 const char *sFileList[] = { TEST_FILE_NAME };
284 int32_t sessionId = INVALID_SESSION_ID;
285 SessionEnableStatus isEnabled = ENABLE_STATUS_INIT;
286 (void)ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, mySessionName, &g_sessionlistener);
287 (void)ClientAddSession(¶m, &sessionId, &isEnabled);
288
289 int32_t ret = SendFile(sessionId, sFileList, NULL, 1);
290 EXPECT_NE(SOFTBUS_OK, ret);
291 }
292
293 /**
294 * @tc.name: QosReportTest001
295 * @tc.desc: use the normal parameter.
296 * @tc.type: FUNC
297 * @tc.require:
298 */
299 HWTEST_F(TransSessionTest, QosReportTest001, TestSize.Level0)
300 {
301 int32_t sessionId = 1;
302 int32_t appType = 1;
303 int32_t quality = -1;
304 int32_t ret = QosReport(sessionId, appType, quality);
305 EXPECT_NE(SOFTBUS_OK, ret);
306
307 quality = 1;
308 ret = QosReport(sessionId, appType, quality);
309 EXPECT_NE(SOFTBUS_OK, ret);
310 }
311
312
313 /**
314 * @tc.name: ClientCleanAllSessionWhenServerDeathTest001
315 * @tc.desc: use the normal parameter.
316 * @tc.type: FUNC
317 * @tc.require:
318 */
319 HWTEST_F(TransSessionTest, ClientCleanAllSessionWhenServerDeathTest001, TestSize.Level0)
320 {
321 ListNode sessionServerList;
322 ListInit(&sessionServerList);
323 ClientCleanAllSessionWhenServerDeath(&sessionServerList);
324 SessionServerInfo *infoNode = NULL;
325 SessionServerInfo *infoNodeNext = NULL;
326 LIST_FOR_EACH_ENTRY_SAFE(infoNode, infoNodeNext, &(sessionServerList), SessionServerInfo, node) {
327 ListDelete(&infoNode->node);
328 SoftBusFree(infoNode);
329 }
330
331 char pkgName[TEST_PKG_NAME_LEN] = "com.test.trans.session";
332 char mySessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
333 char peerSessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
334 char peerNetworkId[TEST_NETWORK_ID_LEN] = "1234567789";
335 char groupId[TEST_GROUP_ID_LEN] = "123";
336 SessionAttribute attr;
337 attr.dataType = 1;
338 attr.linkTypeNum = 0;
339 SessionParam param = {
340 .sessionName = mySessionName,
341 .peerSessionName = peerSessionName,
342 .peerDeviceId = peerNetworkId,
343 .groupId = groupId,
344 .attr = &attr,
345 };
346
347 int32_t sessionId = INVALID_SESSION_ID;
348 SessionEnableStatus isEnabled = ENABLE_STATUS_INIT;
349 int32_t ret = ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, mySessionName, &g_sessionlistener);
350 EXPECT_EQ(ret, SOFTBUS_SERVER_NAME_REPEATED);
351 ret = ClientAddSession(¶m, &sessionId, &isEnabled);
352 EXPECT_EQ(ret, SOFTBUS_OK);
353 ClientCleanAllSessionWhenServerDeath(&sessionServerList);
354 infoNode = NULL;
355 infoNodeNext = NULL;
356 LIST_FOR_EACH_ENTRY_SAFE(infoNode, infoNodeNext, &(sessionServerList), SessionServerInfo, node) {
357 ListDelete(&infoNode->node);
358 SoftBusFree(infoNode);
359 }
360 }
361
362 } // namespace OHOS
363