1 /**
2 * Copyright (c) 2021-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 #include <string.h>
16 #include <unistd.h>
17
18 #include "hctest.h"
19 #include "securec.h"
20 #include "session.h"
21 #include "softbus_def.h"
22 #include "softbus_config_type.h"
23 #include "softbus_bus_center.h"
24 #include "softbus_errcode.h"
25
26 #define TEST_PKG_NAME "com.softbus.test"
27 #define DEFAULT_NODE_STATE_CB_NUM 9
28 #define LOG2_DBG(fmt, ...) printf("DEBUG:%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
29 #define LOG2_INFO(fmt, ...) printf("INFO:%s:%d " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
30 #define LOG2_WARN(fmt, ...) printf("WARN:%s:%d " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
31 #define LOG2_ERR(fmt, ...) printf("ERROR:%s:%d " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
32
33 enum StatusNum {
34 TRANS_STATE_NONE = 0,
35 TRANS_STATE_INIT,
36 TRANS_STATE_CREATE_SESSION_SERVER,
37 TRANS_SET_FILE_SEND_LISTENER,
38 TRANS_STATE_OPEN,
39 TRANS_STATE_SEND_BYTE,
40 TRANS_STATE_SEND_MESSAGE,
41 TRANS_STATE_SEND_FILE,
42 TRANS_STATE_CLOSE,
43 TRANS_STATE_REMOVE_SESSION_SERVER,
44 TRANS_STATE_CREATE_PHONE = 10,
45 TRANS_STATE_GET_SESSION_NAME,
46 TRANS_STATE_GET_DEVICE_ID,
47 TRANS_CLEAR_LOG,
48 TRANS_TEST_FIN,
49
50 LNN_STATE_JOINLNN = 20,
51 LNN_STATE_LEAVELNN,
52 };
53
54 const char *g_pkgName = "dms";
55 const char *g_sessionName = "ohos.distributedschedule.dms.test";
56 const char *g_networkid = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
57 const char *g_groupid = "TEST_GROUP_ID";
58 const char *g_testModuleName = "com.plrdtest.dsoftbus";
59 const char *g_testSessionName = "com.plrdtest.dsoftbus.JtOnOpenFileSession";
60 const char *g_testSessionNamE2 = "com.plrdtest.dsoftbus.JtOpenFileSession";
61 static SessionAttribute g_sessionAttr = {
62 .dataType = TYPE_BYTES,
63 };
64 const int FILE_NUM = 4;
65 int32_t g_sessionId = -1;
66 int32_t g_stateDebug = LNN_STATE_JOINLNN;
67
TestChangeDebugState(int32_t state)68 static void TestChangeDebugState(int32_t state)
69 {
70 g_stateDebug = state;
71 LOG2_INFO("change to debug state: %d", state);
72 }
73
OnSendFileProcess(int sessionId,uint64_t bytesUpload,uint64_t bytesTotal)74 static int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
75 {
76 LOG2_INFO("OnSendFileProcess sessionId = %d, bytesUpload = %llu, total = %llu\n",
77 sessionId, bytesUpload, bytesTotal);
78 return 0;
79 }
80
OnSendFileFinished(int sessionId,const char * firstFile)81 static int OnSendFileFinished(int sessionId, const char *firstFile)
82 {
83 LOG2_INFO("OnSendFileFinished sessionId = %d, first file = %s\n", sessionId, firstFile);
84 TestChangeDebugState(TRANS_STATE_CLOSE);
85 return 0;
86 }
87
OnFileTransError(int sessionId)88 static void OnFileTransError(int sessionId)
89 {
90 LOG2_INFO("OnFileTransError sessionId = %d\n", sessionId);
91 }
92
93 static IFileSendListener g_fileSendListener = {
94 .OnSendFileProcess = OnSendFileProcess,
95 .OnSendFileFinished = OnSendFileFinished,
96 .OnFileTransError = OnFileTransError,
97 };
98
99 static ISessionListener g_sessionlistener = {
100 .OnSessionOpened = 1,
101 };
102
OnReceiveFileStarted(int sessionId,const char * files,int fileCnt)103 static int OnReceiveFileStarted(int sessionId, const char *files, int fileCnt)
104 {
105 LOG2_INFO("File receive start sessionId = %d, first file = %s, fileCnt = %d\n", sessionId, files, fileCnt);
106 return 0;
107 }
108
OnReceiveFileFinished(int sessionId,const char * files,int fileCnt)109 static void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt)
110 {
111 LOG2_INFO("File receive finished sessionId = %d, first file = %s, fileCnt = %d\n", sessionId, files, fileCnt);
112 }
113
114 static IFileReceiveListener g_fileRecvListener = {
115 .OnReceiveFileStarted = OnReceiveFileStarted,
116 .OnReceiveFileFinished = OnReceiveFileFinished,
117 .OnFileTransError = OnFileTransError,
118 };
119
120 LITE_TEST_SUIT(dsoftbus, session, SessionTestSuite);
121
SessionTestSuiteSetUp(void)122 static BOOL SessionTestSuiteSetUp(void)
123 {
124 printf("----------test case with SessionTestSuite start-------------\n");
125 return TRUE;
126 }
127
SessionTestSuiteTearDown(void)128 static BOOL SessionTestSuiteTearDown(void)
129 {
130 printf("----------test case with SessionTestSuite end-------------\n");
131 return TRUE;
132 }
133
134 /**
135 * @tc.name: TestCreateSessionServer001
136 * @tc.desc: extern module active publish, use the wrong parameter.
137 * @tc.type: FUNC99
138 * @tc.require:
139 */
140 LITE_TEST_CASE(SessionTestSuite, TestCreateSessionServer001, Function | MediumTest | Level0)
141 {
142 int ret;
143 ret = CreateSessionServer(NULL, g_sessionName, &g_sessionlistener);
144 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
145
146 ret = CreateSessionServer(g_pkgName, NULL, &g_sessionlistener);
147 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
148
149 ret = CreateSessionServer(g_pkgName, g_sessionName, NULL);
150 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
151 }
152
153 /**
154 * @tc.name: TestRemoveSessionServer001
155 * @tc.desc: extern module active publish, use the wrong parameter.
156 * @tc.type: FUNC
157 * @tc.require:
158 */
159 LITE_TEST_CASE(SessionTestSuite, TestRemoveSessionServer001, Function | MediumTest | Level0)
160 {
161 int ret;
162 ret = RemoveSessionServer(NULL, g_sessionName);
163 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
164
165 ret = RemoveSessionServer(g_pkgName, NULL);
166 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
167 }
168
169 /**
170 * @tc.name: TestOpenSession001
171 * @tc.desc: extern module active publish, use the wrong parameter.
172 * @tc.type: FUNC
173 * @tc.require:
174 */
175 LITE_TEST_CASE(SessionTestSuite, TestOpenSession001, Function | MediumTest | Level0)
176 {
177 int ret;
178 g_sessionAttr.dataType = TYPE_BYTES;
179
180 ret = OpenSession(NULL, g_sessionName, g_networkid, g_groupid, &g_sessionAttr);
181 TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);
182
183 ret = OpenSession(g_sessionName, NULL, g_networkid, g_groupid, &g_sessionAttr);
184 TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);
185
186 ret = OpenSession(g_sessionName, g_sessionName, NULL, g_groupid, &g_sessionAttr);
187 TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);
188
189 ret = OpenSession(g_sessionName, g_sessionName, g_networkid, NULL, &g_sessionAttr);
190 TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);
191
192 ret = OpenSession(g_sessionName, g_sessionName, g_networkid, g_groupid, NULL);
193 TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);
194 }
195
196 /**
197 * @tc.name: TestSendBytes001
198 * @tc.desc: extern module active publish, use the wrong parameter.
199 * @tc.type: FUNC
200 * @tc.require:
201 */
202 LITE_TEST_CASE(SessionTestSuite, TestSendBytes001, Function | MediumTest | Level0)
203 {
204 int ret;
205 int sessionId = 1;
206 const char *data = "testdata";
207 uint32_t len = strlen(data);
208 uint32_t maxLen;
209
210 ret = SendBytes(-1, data, len);
211 TEST_ASSERT_EQUAL_INT(SOFTBUS_TRANS_INVALID_SESSION_ID, ret);
212
213 ret = SendBytes(sessionId, NULL, len);
214 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
215
216 ret = SendBytes(sessionId, data, 0);
217 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
218 }
219
220 /**
221 * @tc.name: TestSendMessage001
222 * @tc.desc: extern module active publish, use the wrong parameter.
223 * @tc.type: FUNC
224 * @tc.require:
225 */
226 LITE_TEST_CASE(SessionTestSuite, TestSendMessage001, Function | MediumTest | Level0)
227 {
228 int ret;
229 int sessionId = 1;
230 const char *data = "testdata";
231 uint32_t len = strlen(data);
232 uint32_t maxLen;
233
234 ret = SendMessage(-1, data, len);
235 TEST_ASSERT_EQUAL_INT(SOFTBUS_TRANS_INVALID_SESSION_ID, ret);
236
237 ret = SendMessage(sessionId, NULL, len);
238 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
239
240 ret = SendMessage(sessionId, data, 0);
241 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
242 }
243
244
245 /**
246 * @tc.name: TestSendStream001
247 * @tc.desc: extern module active publish, use the wrong parameter.
248 * @tc.type: FUNC
249 * @tc.require:
250 */
251 LITE_TEST_CASE(SessionTestSuite, TestSendStream001, Function | MediumTest | Level0)
252 {
253 int ret;
254 int sessionId = 1;
255 const StreamData streamData = {0};
256 const StreamData ext = {0};
257 const StreamFrameInfo param = {0};
258
259 ret = SendStream(-1, &streamData, &ext, ¶m);
260 TEST_ASSERT_EQUAL_INT(SOFTBUS_TRANS_INVALID_SESSION_ID, ret);
261
262 ret = SendStream(sessionId, NULL, &ext, ¶m);
263 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
264
265 ret = SendStream(sessionId, &streamData, NULL, ¶m);
266 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
267
268 ret = SendStream(sessionId, &streamData, &ext, NULL);
269 TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
270 }
271
272 /**
273 * @tc.name: TestQosReport00
274 * @tc.desc: test the wrong parameter.
275 * @tc.type: FUNC
276 * @tc.require:
277 */
278 LITE_TEST_CASE(SessionTestSuite, TestQosReport001, Function | MediumTest | Level0)
279 {
280 int ret;
281 ret = QosReport(1, 1, QOS_IMPROVE);
282 TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
283
284 ret = QosReport(1, 1, QOS_RECOVER);
285 TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
286
287 ret = QosReport(1, 1, QOS_IMPROVE);
288 TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
289 }
290
291 /**
292 * @tc.name: TestSetFileSendListener001
293 * @tc.desc: test the wrong parameter.
294 * @tc.type: FUNC
295 * @tc.require:
296 */
297 LITE_TEST_CASE(SessionTestSuite, TestSetFileSendListener001, Function | MediumTest | Level0)
298 {
299 int ret = SetFileSendListener(g_testModuleName, g_testSessionName, NULL);
300 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
301 }
302
303 /**
304 * @tc.name: TestSendFile001
305 * @tc.desc: test the wrong parameter.
306 * @tc.type: FUNC
307 * @tc.require:
308 */
309 LITE_TEST_CASE(SessionTestSuite, TestSendFile001, Function | MediumTest | Level0)
310 {
311 const char *sfileList[] = {
312 "/data/big.tar",
313 "/data/richu.jpg",
314 "/data/richu-002.jpg",
315 "/data/richu-003.jpg",
316 };
317 int ret = SendFile(g_sessionId, NULL, NULL, FILE_NUM);
318 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
319
320 ret = SendFile(g_sessionId, sfileList, NULL, 0);
321 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
322
323 ret = SendFile(g_sessionId, sfileList, NULL, FILE_NUM);
324 TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
325 }
326
327 /**
328 * @tc.name: TestSetFileReceiveListener001
329 * @tc.desc: test the wrong parameter.
330 * @tc.type: FUNC
331 * @tc.require:
332 */
333 LITE_TEST_CASE(SessionTestSuite, TestSetFileReceiveListener001, Function | MediumTest | Level0)
334 {
335 int ret = SetFileReceiveListener(g_testModuleName, g_testSessionNamE2, NULL, "/data/");
336 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
337 }
338
339 /**
340 * @tc.name: TestGetMySessionName
341 * @tc.desc: extern module active publish, use the wrong parameter.
342 * @tc.type: FUNC
343 * @tc.require:
344 */
345 LITE_TEST_CASE(SessionTestSuite, TestGetMySessionName001, Function | MediumTest | Level0)
346 {
347 int ret;
348 int sessionId = -1;
349 int len = 5;
350 ret = GetMySessionName(sessionId, NULL, len);
351 TEST_ASSERT_EQUAL_INT(ret, SOFTBUS_INVALID_PARAM);
352 }
353
354 /**
355 * @tc.name: TestGetPeerSessionName001
356 * @tc.desc: extern module active publish, use the wrong parameter.
357 * @tc.type: FUNC
358 * @tc.require:
359 */
360 LITE_TEST_CASE(SessionTestSuite, TestGetPeerSessionName001, Function | MediumTest | Level0)
361 {
362 int ret;
363 int sessionId = -1;
364 int len = 5;
365 ret = GetPeerSessionName(sessionId, NULL, len);
366 TEST_ASSERT_EQUAL_INT(ret, SOFTBUS_INVALID_PARAM);
367 }
368
369 /**
370 * @tc.name: TestGetPeerDeviceId001
371 * @tc.desc: extern module active publish, use the wrong parameter.
372 * @tc.type: FUNC
373 * @tc.require:
374 */
375 LITE_TEST_CASE(SessionTestSuite, TestGetPeerDeviceId001, Function | MediumTest | Level0)
376 {
377 int ret;
378 int sessionId = -1;
379 int len = 5;
380 ret = GetPeerDeviceId(sessionId, NULL, len);
381 TEST_ASSERT_EQUAL_INT(ret, SOFTBUS_INVALID_PARAM);
382 }
383
384 /**
385 * @tc.name: TestGetSessionSide001
386 * @tc.desc: extern module active publish, use the wrong parameter.
387 * @tc.type: FUNC
388 * @tc.require:
389 */
390 LITE_TEST_CASE(SessionTestSuite, TestGetSessionSide001, Function | MediumTest | Level0)
391 {
392 int ret = GetSessionSide(g_sessionId);
393 TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
394 }
395
396 RUN_TEST_SUITE(SessionTestSuite);