• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "dfs_session.h"
20 #include "inner_session.h"
21 #include "session.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_common.h"
24 #include "softbus_def.h"
25 #include "softbus_error_code.h"
26 #include "client_trans_session_manager.h"
27 #include "client_trans_socket_manager.h"
28 #include "client_trans_session_service.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(int32_t sessionId,int32_t result)68 static int32_t OnSessionOpened(int32_t sessionId, int32_t result)
69 {
70     TRANS_LOGI(TRANS_TEST, "session opened, sesisonId=%{public}d", sessionId);
71     return SOFTBUS_OK;
72 }
73 
OnSessionClosed(int32_t sessionId)74 static void OnSessionClosed(int32_t 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.Level1)
91 {
92     int32_t ret;
93     int32_t sessionId = 1;
94     char *key = (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, nullptr, 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.Level1)
117 {
118     int32_t ret;
119     int32_t sessionId = 1;
120     char *key = (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.Level1)
134 {
135     int32_t ret;
136     int32_t sessionId = 1;
137     int32_t handle = 1;
138 
139     ret = GetSessionHandle(-1, &handle);
140     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
141 
142     ret = GetSessionHandle(sessionId, nullptr);
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.Level1)
153 {
154     int32_t ret;
155     int32_t sessionId = 1;
156     int32_t 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.Level1)
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.Level1)
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.Level1)
198 {
199     int32_t ret;
200 
201     ret = OpenAuthSession(nullptr, &(g_addrInfo), 1, nullptr);
202     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
203 
204     ret = OpenAuthSession(g_testSessionName, nullptr, 1, nullptr);
205     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
206 
207     ret = OpenAuthSession(g_testSessionName, &(g_addrInfo), -1, nullptr);
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.Level1)
218 {
219     int32_t ret;
220 
221     ret = OpenAuthSession(g_testSessionName, &(g_addrInfo), 1, nullptr);
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.Level1)
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.Level1)
246 {
247     int32_t sessionId = 1;
248     int32_t ret = SendFile(sessionId, nullptr, nullptr, 1);
249     EXPECT_NE(SOFTBUS_OK, ret);
250 
251     const char *sFileList[] = { TEST_FILE_NAME };
252     ret = SendFile(sessionId, sFileList, nullptr, 0);
253     EXPECT_NE(SOFTBUS_OK, ret);
254 
255     ret = SendFile(sessionId, sFileList, nullptr, 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.Level1)
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     uint64_t timestamp = 0;
287     (void)ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, mySessionName, &g_sessionlistener, &timestamp);
288     (void)ClientAddSession(&param, &sessionId, &isEnabled);
289 
290     int32_t ret = SendFile(sessionId, sFileList, nullptr, 1);
291     EXPECT_NE(SOFTBUS_OK, ret);
292 }
293 
294 /**
295  * @tc.name: QosReportTest001
296  * @tc.desc: use the normal parameter.
297  * @tc.type: FUNC
298  * @tc.require:
299  */
300 HWTEST_F(TransSessionTest, QosReportTest001, TestSize.Level1)
301 {
302     int32_t sessionId = 1;
303     int32_t appType = 1;
304     int32_t quality = -1;
305     int32_t ret = QosReport(sessionId, appType, quality);
306     EXPECT_NE(SOFTBUS_OK, ret);
307 
308     quality = 1;
309     ret = QosReport(sessionId, appType, quality);
310     EXPECT_NE(SOFTBUS_OK, ret);
311 }
312 
313 
314 /**
315  * @tc.name: ClientCleanAllSessionWhenServerDeathTest001
316  * @tc.desc: use the normal parameter.
317  * @tc.type: FUNC
318  * @tc.require:
319  */
320 HWTEST_F(TransSessionTest, ClientCleanAllSessionWhenServerDeathTest001, TestSize.Level1)
321 {
322     ListNode sessionServerList;
323     ListInit(&sessionServerList);
324     ClientCleanAllSessionWhenServerDeath(&sessionServerList);
325     SessionServerInfo *infoNode = nullptr;
326     SessionServerInfo *infoNodeNext = nullptr;
327     LIST_FOR_EACH_ENTRY_SAFE(infoNode, infoNodeNext, &(sessionServerList), SessionServerInfo, node) {
328         ListDelete(&infoNode->node);
329         SoftBusFree(infoNode);
330     }
331 
332     char pkgName[TEST_PKG_NAME_LEN] = "com.test.trans.session";
333     char mySessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
334     char peerSessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
335     char peerNetworkId[TEST_NETWORK_ID_LEN] = "1234567789";
336     char groupId[TEST_GROUP_ID_LEN] = "123";
337     SessionAttribute attr;
338     attr.dataType = 1;
339     attr.linkTypeNum = 0;
340     SessionParam param = {
341         .sessionName = mySessionName,
342         .peerSessionName = peerSessionName,
343         .peerDeviceId = peerNetworkId,
344         .groupId = groupId,
345         .attr = &attr,
346     };
347 
348     int32_t sessionId = INVALID_SESSION_ID;
349     SessionEnableStatus isEnabled = ENABLE_STATUS_INIT;
350     uint64_t timestamp = 0;
351     int32_t ret = ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, mySessionName, &g_sessionlistener, &timestamp);
352     EXPECT_EQ(ret, SOFTBUS_SERVER_NAME_REPEATED);
353     ret = ClientAddSession(&param, &sessionId, &isEnabled);
354     EXPECT_EQ(ret, SOFTBUS_OK);
355     ClientCleanAllSessionWhenServerDeath(&sessionServerList);
356     infoNode = nullptr;
357     infoNodeNext = nullptr;
358     LIST_FOR_EACH_ENTRY_SAFE(infoNode, infoNodeNext, &(sessionServerList), SessionServerInfo, node) {
359         ListDelete(&infoNode->node);
360         SoftBusFree(infoNode);
361     }
362 }
363 
364 } // namespace OHOS
365