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