1 /*
2 * Copyright (c) 2021 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
18 #include "net_trans_common.h"
19 #include "wifi_utils.h"
20 #include "accesstoken_kit.h"
21
22 using namespace std;
23 using namespace testing::ext;
24
25 static INodeStateCb* g_nodeStateCallback = NULL;
26 static ISessionListener* g_sessionlist4Data = NULL;
27 static ISessionListener* g_sessionlist4Ctrl = NULL;
28 static ISessionListener* g_sessionlist4Perf = NULL;
29 static ISessionListener* g_sessionlist4Pass = NULL;
30 static ISessionListener* g_sessionlist4File = NULL;
31 static ISessionListener* g_sessionlist4Proxy = NULL;
32 static ISessionListener *g_sessionlist4Stream = NULL;
33
34 static uint64_t g_transTimeEnd;
35
36 static const char* def_passwd = "OH2022@xa";
37 static const char* def_ssid = "OpenHarmony_Private_Net_01";
38 static const char* slave_ssid = "OpenHarmony_Private_Net_02";
39
40 static const int three_seconds = 3;
41 static const int six_seconds = 6;
42 static const int ten_seconds = 10;
43
44 static void SetupCallback(void);
45 static void TeardownCallback(void);
46
47 class dsoftbusTest : public testing::Test {
48 public:
49 static void SetUpTestCase();
50 static void TearDownTestCase();
51 void SetUp();
52 void TearDown();
53 };
54
SetUp()55 void dsoftbusTest ::SetUp() {}
56
TearDown()57 void dsoftbusTest ::TearDown() {}
58
SetUpTestCase()59 void dsoftbusTest ::SetUpTestCase()
60 {
61 LOG("SetUpTestCase");
62 AddPermission();
63 sleep(1);
64 system("pidof accesstoken_ser | xargs kill -9");
65 sleep(1);
66 TestSetUp();
67 SetupCallback();
68 int ret = RegNodeDeviceStateCb(DEF_PKG_NAME, g_nodeStateCallback);
69 EXPECT_EQ(SOFTBUS_OK, ret) << "call reg node state callback fail";
70 }
71
TearDownTestCase()72 void dsoftbusTest::TearDownTestCase()
73 {
74 LOG("TearDownTestCase");
75 int ret = UnregNodeDeviceStateCb(g_nodeStateCallback);
76 EXPECT_EQ(SOFTBUS_OK, ret) << "call unReg node state callback fail";
77 TeardownCallback();
78 TestTearDown();
79 }
80
OnFileSessionOpened(int sessionId,int result)81 static int OnFileSessionOpened(int sessionId, int result)
82 {
83 LOG("[cb][file]open session sid[%d],rst[%d]", sessionId, result);
84 return SOFTBUS_OK;
85 }
86
OnFileSessionClosed(int sessionId)87 static void OnFileSessionClosed(int sessionId)
88 {
89 LOG("[cb][file]close session sid[%d]", sessionId);
90 }
91
OnFileBytesReceived(int sessionId,const void * data,unsigned int dataLen)92 static void OnFileBytesReceived(int sessionId, const void* data, unsigned int dataLen)
93 {
94 LOG("[cb][file]ByteRec sid:%d, data len:%d", sessionId, dataLen);
95 if (data == NULL) {
96 LOG("[cb][file]ByteRec invalid data=null sid[%d]", sessionId);
97 }
98 }
99
OnFileMessageReceived(int sessionId,const void * data,unsigned int dataLen)100 static void OnFileMessageReceived(int sessionId, const void* data, unsigned int dataLen)
101 {
102 LOG("[cb][file]MessageRec sid:%d, data len:%d", sessionId, dataLen);
103 if (data == NULL) {
104 LOG("[cb][file]MessageRec invalid data=null sid[%d]", sessionId);
105 }
106 }
107
OnStreamSessionOpened(int sessionId,int result)108 static int OnStreamSessionOpened(int sessionId, int result)
109 {
110 LOG("[cb][stream]open session sid[%d],rst[%d]", sessionId, result);
111 return SOFTBUS_OK;
112 }
113
OnStreamSessionClosed(int sessionId)114 static void OnStreamSessionClosed(int sessionId)
115 {
116 LOG("[cb][stream]close session sid[%d]", sessionId);
117 }
118
GetNumberInStreamData(const char * streamData)119 static inline int GetNumberInStreamData(const char *streamData)
120 {
121 if (streamData[0] == '1') {
122 return (streamData[1] - '0');
123 } else if (streamData[0] == '2') {
124 return (streamData[1] - '0') * 10 + (streamData[2] - '0');
125 } else if (streamData[0] == '3') {
126 return (streamData[1] - '0') * 100 + (streamData[2] - '0') * 10 + (streamData[3] - '0');
127 } else if (streamData[0] == '4') {
128 return (streamData[1] - '0') * 1000 + (streamData[2] - '0') * 100 + (streamData[3] - '0' * 10 + (streamData[4] - '0'));
129 }
130 return -1;
131 }
132
StreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frame)133 static void StreamReceived(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *frame)
134 {
135 g_transTimeEnd = GetCurrentTimeOfMs();
136 int i = GetNumberInStreamData((const char *)data->buf);
137 if (i < 0) {
138 return;
139 }
140 if (i % 60 == 0)
141 {
142 LOG("### RECV counts = %d ", i );
143 } else
144 {
145 LOG("### RECV counts = %d ", i );
146 }
147 if (data != NULL) {
148 LOG("[cb][stream]Rec sid:%d, data= %.*s.\n", sessionId, data->bufLen, data->buf);
149 }
150 }
151
152 /* session callback for data */
OnDataSessionOpened(int sessionId,int result)153 static int OnDataSessionOpened(int sessionId, int result)
154 {
155 LOG("[cb][data]session opened sid:%d,ret:%d", sessionId, result);
156 return SOFTBUS_OK;
157 }
158
OnDataSessionClosed(int sessionId)159 static void OnDataSessionClosed(int sessionId)
160 {
161 LOG("[cb][data]session closed sid:%d", sessionId);
162 }
163
OnDataBytesReceived(int sessionId,const void * data,unsigned int dataLen)164 static void OnDataBytesReceived(int sessionId, const void* data, unsigned int dataLen)
165 {
166 if (sessionId < 0) {
167 LOG("[cb][data]byte received invalid session id[%d]", sessionId);
168 return;
169 }
170 LOG("[cb][data]byte received sid:%d, data-len:%d", sessionId, dataLen);
171 int ret = SendBytes(sessionId, data, dataLen);
172 LOG("[cb][data]byte received send back:%d", ret);
173 }
174
OnDataMessageReceived(int sessionId,const void * data,unsigned int dataLen)175 static void OnDataMessageReceived(int sessionId, const void* data, unsigned int dataLen)
176 {
177 if (sessionId < 0) {
178 LOG("[cb][data]mesg received invalid session id[%d]", sessionId);
179 return;
180 }
181 LOG("[cb][data]mesg received sid:%d, data-len:%d", sessionId, dataLen);
182 int ret = SendMessage(sessionId, data, dataLen);
183 LOG("[cb][data]mesg received send back:%d", ret);
184 }
185
186 /* session callback for control */
GetCodeByMsg(const string & msg)187 static int GetCodeByMsg(const string& msg)
188 {
189 LOG("[ctrl] recv msg content[%s]", msg.c_str());
190 int ret = -1;
191 if (strncmp(msg.c_str(), CTRL_MSG_CLOSE_WIFI_TEN_SEC, strlen(CTRL_MSG_CLOSE_WIFI_TEN_SEC)) == 0) {
192 ret = CTRL_CODE_CLOSE_WIFI_TEN_SEC;
193 } else if (strncmp(msg.c_str(), CTRL_MSG_CLOSE_WIFI_THREE_SEC, strlen(CTRL_MSG_CLOSE_WIFI_THREE_SEC)) == 0) {
194 ret = CTRL_CODE_CLOSE_WIFI_THREE_SEC;
195 } else if (strncmp(msg.c_str(), CTRL_MSG_CLOSE_WIFI_TEN_MIN, strlen(CTRL_MSG_CLOSE_WIFI_TEN_MIN)) == 0) {
196 ret = CTRL_CODE_CLOSE_WIFI_TEN_MIN;
197 } else if (strncmp(msg.c_str(), CTRL_MSG_CLOSE_WIFI_FIVE_MIN, strlen(CTRL_MSG_CLOSE_WIFI_FIVE_MIN)) == 0) {
198 ret = CTRL_CODE_CLOSE_WIFI_FIVE_MIN;
199 } else if (strncmp(msg.c_str(), CTRL_MSG_CHANGE_WIFI_TEN_SEC, strlen(CTRL_MSG_CHANGE_WIFI_TEN_SEC)) == 0) {
200 ret = CTRL_CODE_CHANGE_WIFI_TEN_SEC;
201 } else if (strncmp(msg.c_str(), CTRL_MSG_CHANGE_WIFI_SIXTY_SEC, strlen(CTRL_MSG_CHANGE_WIFI_SIXTY_SEC)) == 0) {
202 ret = CTRL_CODE_CHANGE_WIFI_SIXTY_SEC;
203 } else if (strncmp(msg.c_str(), CTRL_MSG_CLOSE_BR, strlen(CTRL_MSG_CLOSE_BR)) == 0) {
204 ret = CTRL_CODE_CLOSE_BR;
205 } else if (strncmp(msg.c_str(), CTRL_MSG_CLOSE_OPEN_BR, strlen(CTRL_MSG_CLOSE_OPEN_BR)) == 0) {
206 ret = CTRL_CODE_CLOSE_OPEN_BR;
207 } else if (strncmp(msg.c_str(), CTRL_MSG_OPEN_SESSION, strlen(CTRL_MSG_OPEN_SESSION)) == 0) {
208 ret = CTRL_CODE_OPEN_SESSION;
209 } else if (strncmp(msg.c_str(), CTRL_MSG_OPEN_SESSION_MSG, strlen(CTRL_MSG_OPEN_SESSION_MSG)) == 0) {
210 ret = CTRL_CODE_OPEN_SESSION_MSG;
211 } else if (strncmp(msg.c_str(), CTRL_MSG_OPEN_SESSION_NOT_EXIST, strlen(CTRL_MSG_OPEN_SESSION_NOT_EXIST)) == 0) {
212 ret = CTRL_CODE_OPEN_SESSION_NOT_EXIST;
213 }
214 return ret;
215 }
216
CtrlOperateTask(void * param)217 void* CtrlOperateTask(void* param)
218 {
219 LOG("[operate] start...");
220 int code = -1;
221 if (param != NULL) {
222 code = *((int*)param);
223 }
224 LOG("[operate] code:%d", code);
225 int ret;
226 int sleepTime;
227 switch (code) {
228 case CTRL_CODE_CLOSE_WIFI_TEN_SEC:
229 WiFiUtils ::DisableThenEnableAndConnect(ten_seconds, def_ssid, def_passwd);
230 break;
231 case CTRL_CODE_CLOSE_WIFI_THREE_SEC:
232 WiFiUtils ::DisableThenEnableAndConnect(three_seconds, def_ssid, def_passwd);
233 break;
234 case CTRL_CODE_CLOSE_WIFI_TEN_MIN:
235 sleepTime = six_seconds * ten_seconds * ten_seconds;
236 WiFiUtils ::DisableWifi();
237 while (sleepTime > 0) {
238 sleep(ten_seconds);
239 sleepTime -= ten_seconds;
240 LOG("[operate][close wifi 10mins] wait:%d", sleepTime);
241 }
242 WiFiUtils ::EnableThenConnect(def_ssid, def_passwd);
243 break;
244 case CTRL_CODE_CLOSE_WIFI_FIVE_MIN:
245 sleepTime = three_seconds * ten_seconds * ten_seconds;
246 WiFiUtils ::DisableWifi();
247 while (sleepTime > 0) {
248 sleep(ten_seconds);
249 sleepTime -= ten_seconds;
250 LOG("[operate][close wifi 5mins] wait:%d", sleepTime);
251 }
252 WiFiUtils ::EnableThenConnect(def_ssid, def_passwd);
253 break;
254 case CTRL_CODE_CHANGE_WIFI_TEN_SEC:
255 sleepTime = ten_seconds;
256 ret = WiFiUtils ::ConnectToNew(slave_ssid, def_passwd);
257 LOG("[operate]connect to salve ret:%d", ret);
258 LOG("[operate]start sleep:%d", sleepTime);
259 sleep(sleepTime);
260 ret = WiFiUtils ::ConnectToNew(def_ssid, def_passwd);
261 LOG("[operate]connect to default ret:%d", ret);
262 break;
263 case CTRL_CODE_CHANGE_WIFI_SIXTY_SEC:
264 sleepTime = six_seconds * ten_seconds;
265 ret = WiFiUtils ::ConnectToNew(slave_ssid, def_passwd);
266 LOG("[operate]connect to salve ret:%d", ret);
267 LOG("[operate]start sleep:%d", sleepTime);
268 sleep(sleepTime);
269 ret = WiFiUtils ::ConnectToNew(def_ssid, def_passwd);
270 LOG("[operate]connect to default ret:%d", ret);
271 break;
272 case CTRL_CODE_CLOSE_BR:
273 // close br
274 LOG("[operate]close br TODO...");
275 break;
276 case CTRL_CODE_CLOSE_OPEN_BR:
277 // close open br
278 LOG("[operate]close-open br TODO...");
279 break;
280 case CTRL_CODE_OPEN_SESSION:
281 // open session and send byte
282 LOG("[operate]open session TODO...");
283 break;
284 case CTRL_CODE_OPEN_SESSION_NOT_EXIST:
285 // open session not exist
286 LOG("[operate]open session not exist TODO...");
287 break;
288 case CTRL_CODE_OPEN_SESSION_MSG:
289 // open session and send msg
290 LOG("[operate]open session not exist TODO...");
291 break;
292 default:
293 LOG("[operate]not support this code");
294 break;
295 }
296 free(param);
297 LOG("[operate] end");
298 return nullptr;
299 }
300
OnCtrlSessionOpened(int sessionId,int result)301 static int OnCtrlSessionOpened(int sessionId, int result)
302 {
303 LOG("[cb][ctrl]session opened sid:%d, ret:%d", sessionId, result);
304 return SOFTBUS_OK;
305 }
306
OnCtrlSessionClosed(int sessionId)307 static void OnCtrlSessionClosed(int sessionId)
308 {
309 LOG("[cb][ctrl]session closed sid:%d", sessionId);
310 }
311
OnCtrlBytesReceived(int sessionId,const void * data,unsigned int dataLen)312 static void OnCtrlBytesReceived(int sessionId, const void* data, unsigned int dataLen)
313 {
314 if (sessionId < 0) {
315 LOG("[cb][ctrl]byte received invalid session id[%d]", sessionId);
316 return;
317 }
318 LOG("[cb][ctrl]byte received sid:%d, data-len:%d", sessionId, dataLen);
319 int ret = SendBytes(sessionId, data, dataLen);
320 LOG("[cb][ctrl]byte received send back:%d", ret);
321 }
322
OnCtrlMessageReceived(int sessionId,const void * data,unsigned int dataLen)323 static void OnCtrlMessageReceived(int sessionId, const void* data, unsigned int dataLen)
324 {
325 if (sessionId < 0) {
326 LOG("[cb][ctrl]mesg received invalid session id[%d]", sessionId);
327 return;
328 }
329 LOG("[cb][ctrl]mesg received sid:%d, data-len:%d", sessionId, dataLen);
330 int ret = SendMessage(sessionId, data, dataLen);
331 LOG("[cb][ctrl]mesg received send back:%d", ret);
332
333 // operate
334 unsigned int maxCtrlLen = 25;
335 if (dataLen < maxCtrlLen) {
336 int* code = (int*)malloc(sizeof(int));
337 *code = GetCodeByMsg((char*)data);
338 LOG("[cb][ctrl]GetCodeByMsg is:%d", *code);
339 if (*code != -1) {
340 pthread_t ctrlThread;
341 ret = pthread_create(&ctrlThread, nullptr, CtrlOperateTask, code);
342 LOG("[cb][ctrl]create ctrl thread ret:%d", ret);
343 } else {
344 free(code);
345 }
346 }
347 }
348
349 /* session callback for performance */
OnPerfSessionOpened(int sessionId,int result)350 static int OnPerfSessionOpened(int sessionId, int result)
351 {
352 LOG("[cb][perf]session opened sid:%d, ret:%d", sessionId, result);
353 return SOFTBUS_OK;
354 }
355
OnPerfSessionClosed(int sessionId)356 static void OnPerfSessionClosed(int sessionId)
357 {
358 LOG("[cb][perf]session closed sid:%d", sessionId);
359 }
360
OnPerfBytesReceived(int sessionId,const void * data,unsigned int dataLen)361 static void OnPerfBytesReceived(int sessionId, const void* data, unsigned int dataLen)
362 {
363 if (sessionId < 0) {
364 LOG("[cb][perf]byte received invalid session id[%d]", sessionId);
365 return;
366 }
367 LOG("[cb][perf]byte received sid:%d, data-len:%d", sessionId, dataLen);
368 int ret = SendBytes(sessionId, data, dataLen);
369 LOG("[cb][perf]byte received send back:%d", ret);
370 }
371
OnPerfMessageReceived(int sessionId,const void * data,unsigned int dataLen)372 static void OnPerfMessageReceived(int sessionId, const void* data, unsigned int dataLen)
373 {
374 if (sessionId < 0) {
375 LOG("[cb][perf]mesg received invalid session id[%d]", sessionId);
376 return;
377 }
378 LOG("[cb][perf]mesg received sid:%d, data-len:%d", sessionId, dataLen);
379 int ret = SendMessage(sessionId, data, dataLen);
380 LOG("[cb][perf]mesg received send back:%d", ret);
381 }
382
OnPassSessionOpened(int sessionId,int result)383 static int OnPassSessionOpened(int sessionId, int result)
384 {
385 LOG("[cb][pass]session opened sid:%d, ret:%d", sessionId, result);
386 return SOFTBUS_OK;
387 }
388
OnPassSessionClosed(int sessionId)389 static void OnPassSessionClosed(int sessionId)
390 {
391 LOG("[cb][pass]session closed sid:%d", sessionId);
392 }
393
OnPassBytesReceived(int sessionId,const void * data,unsigned int dataLen)394 static void OnPassBytesReceived(int sessionId, const void* data, unsigned int dataLen)
395 {
396 if (sessionId < 0) {
397 LOG("[cb][pass]byte received invalid session id[%d]", sessionId);
398 return;
399 }
400 LOG("[cb][pass]byte received sid:%d, data-len:%d", sessionId, dataLen);
401 }
402
OnPassMessageReceived(int sessionId,const void * data,unsigned int dataLen)403 static void OnPassMessageReceived(int sessionId, const void* data, unsigned int dataLen)
404 {
405 if (sessionId < 0) {
406 LOG("[cb][pass]mesg received invalid session id[%d]", sessionId);
407 return;
408 }
409 LOG("[cb][pass]mesg received sid:%d, data-len:%d", sessionId, dataLen);
410 }
411
OnProxySessionOpened(int sessionId,int result)412 static int OnProxySessionOpened(int sessionId, int result)
413 {
414 LOG("[cb][Proxy]session opened sid:%d, ret:%d", sessionId, result);
415 return SOFTBUS_OK;
416 }
417
OnProxySessionClosed(int sessionId)418 static void OnProxySessionClosed(int sessionId)
419 {
420 LOG("[cb][Proxy]session closed sid:%d", sessionId);
421 }
422
OnProxyBytesReceived(int sessionId,const void * data,unsigned int dataLen)423 static void OnProxyBytesReceived(int sessionId, const void* data, unsigned int dataLen)
424 {
425 if (sessionId < 0) {
426 LOG("[cb][Proxy]byte received invalid session id[%d]", sessionId);
427 return;
428 }
429 LOG("[cb][Proxy]byte received sid:%d, data-len:%d", sessionId, dataLen);
430 int ret = SendBytes(sessionId, data, dataLen);
431 LOG("[cb][Proxy]byte received send back:%d", ret);
432 }
433
OnProxyMessageReceived(int sessionId,const void * data,unsigned int dataLen)434 static void OnProxyMessageReceived(int sessionId, const void* data, unsigned int dataLen)
435 {
436 if (sessionId < 0) {
437 LOG("[cb][Proxy]mesg received invalid session id[%d]", sessionId);
438 return;
439 }
440 LOG("[cb][Proxy]mesg received sid:%d, data-len:%d", sessionId, dataLen);
441 int ret = SendMessage(sessionId, data, dataLen);
442 LOG("[cb][Proxy]mesg received send back:%d", ret);
443 }
444
445 /* net state callback */
OnNodeOnline(NodeBasicInfo * info)446 static void OnNodeOnline(NodeBasicInfo* info)
447 {
448 if (info == NULL) {
449 LOG("[cb]Online: info is null");
450 }
451
452 LOG("[cb]Online id:%s, name:%s ,type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
453 }
454
OnNodeOffline(NodeBasicInfo * info)455 static void OnNodeOffline(NodeBasicInfo* info)
456 {
457 if (info == NULL) {
458 LOG("[cb]Offline: info is null");
459 return;
460 }
461
462 LOG("[cb]Offline id:%s, name:%s ,type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
463 }
464
OnNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)465 static void OnNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo* info)
466 {
467 if (info == NULL) {
468 LOG("[cb]InfoChanged: info is null, type[%d]", type);
469 return;
470 }
471 LOG("[cb]InfoChanged id: %s, name: %s", info->networkId, info->deviceName);
472 }
473
onDefNodeStatusChanged(NodeStatusType type,NodeStatus * status)474 static void onDefNodeStatusChanged(NodeStatusType type, NodeStatus *status)
475 {
476 if (status == NULL) {
477 LOG("[cb]StatusChanged: info is null, type[%d]", type);
478 return;
479 }
480 LOG("[cb]StatusChanged id: %s,status: %d", status->basicInfo.networkId, status->authStatus);
481 }
482
SetupCallback(void)483 static void SetupCallback(void)
484 {
485 if (g_sessionlist4Data == NULL) {
486 g_sessionlist4Data = (ISessionListener*)calloc(1, sizeof(ISessionListener));
487 g_sessionlist4Data->OnSessionOpened = OnDataSessionOpened;
488 g_sessionlist4Data->OnSessionClosed = OnDataSessionClosed;
489 g_sessionlist4Data->OnMessageReceived = OnDataMessageReceived;
490 g_sessionlist4Data->OnBytesReceived = OnDataBytesReceived;
491 }
492
493 if (g_sessionlist4Ctrl == NULL) {
494 g_sessionlist4Ctrl = (ISessionListener*)calloc(1, sizeof(ISessionListener));
495 g_sessionlist4Ctrl->OnSessionOpened = OnCtrlSessionOpened;
496 g_sessionlist4Ctrl->OnSessionClosed = OnCtrlSessionClosed;
497 g_sessionlist4Ctrl->OnMessageReceived = OnCtrlMessageReceived;
498 g_sessionlist4Ctrl->OnBytesReceived = OnCtrlBytesReceived;
499 }
500
501 if (g_sessionlist4Perf == NULL) {
502 g_sessionlist4Perf = (ISessionListener*)calloc(1, sizeof(ISessionListener));
503 g_sessionlist4Perf->OnSessionOpened = OnPerfSessionOpened;
504 g_sessionlist4Perf->OnSessionClosed = OnPerfSessionClosed;
505 g_sessionlist4Perf->OnMessageReceived = OnPerfMessageReceived;
506 g_sessionlist4Perf->OnBytesReceived = OnPerfBytesReceived;
507 }
508
509 if (g_sessionlist4Pass == NULL) {
510 g_sessionlist4Pass = (ISessionListener*)calloc(1, sizeof(ISessionListener));
511 g_sessionlist4Pass->OnSessionOpened = OnPassSessionOpened;
512 g_sessionlist4Pass->OnSessionClosed = OnPassSessionClosed;
513 g_sessionlist4Pass->OnMessageReceived = OnPassMessageReceived;
514 g_sessionlist4Pass->OnBytesReceived = OnPassBytesReceived;
515 }
516
517 if (g_sessionlist4Proxy == NULL) {
518 g_sessionlist4Proxy = (ISessionListener*)calloc(1, sizeof(ISessionListener));
519 g_sessionlist4Proxy->OnSessionOpened = OnProxySessionOpened;
520 g_sessionlist4Proxy->OnSessionClosed = OnProxySessionClosed;
521 g_sessionlist4Proxy->OnMessageReceived = OnProxyMessageReceived;
522 g_sessionlist4Proxy->OnBytesReceived = OnProxyBytesReceived;
523 }
524
525 if (g_sessionlist4File == NULL) {
526 g_sessionlist4File = (ISessionListener*)calloc(1, sizeof(ISessionListener));
527 g_sessionlist4File->OnSessionOpened = OnFileSessionOpened;
528 g_sessionlist4File->OnSessionClosed = OnFileSessionClosed;
529 g_sessionlist4File->OnMessageReceived = OnFileMessageReceived;
530 g_sessionlist4File->OnBytesReceived = OnFileBytesReceived;
531 }
532
533 if (g_nodeStateCallback == NULL) {
534 g_nodeStateCallback = (INodeStateCb*)calloc(1, sizeof(INodeStateCb));
535 g_nodeStateCallback->events = EVENT_NODE_STATE_MASK;
536 g_nodeStateCallback->onNodeOnline = OnNodeOnline;
537 g_nodeStateCallback->onNodeOffline = OnNodeOffline;
538 g_nodeStateCallback->onNodeBasicInfoChanged = OnNodeBasicInfoChanged;
539 g_nodeStateCallback->onNodeStatusChanged = onDefNodeStatusChanged;
540 }
541
542 if (g_sessionlist4Stream == NULL) {
543 g_sessionlist4Stream= (ISessionListener*)calloc(1, sizeof(ISessionListener));
544 g_sessionlist4Stream->OnSessionOpened = OnStreamSessionOpened;
545 g_sessionlist4Stream->OnSessionClosed = OnStreamSessionClosed;
546 g_sessionlist4Stream->OnStreamReceived = StreamReceived;
547 }
548 }
549
TeardownCallback(void)550 static void TeardownCallback(void)
551 {
552 if (g_sessionlist4Data != NULL) {
553 free(g_sessionlist4Data);
554 g_sessionlist4Data = NULL;
555 }
556 if (g_sessionlist4Ctrl != NULL) {
557 free(g_sessionlist4Ctrl);
558 g_sessionlist4Ctrl = NULL;
559 }
560 if (g_sessionlist4Perf != NULL) {
561 free(g_sessionlist4Perf);
562 g_sessionlist4Perf = NULL;
563 }
564 if (g_sessionlist4Pass != NULL) {
565 free(g_sessionlist4Pass);
566 g_sessionlist4Pass = NULL;
567 }
568 if (g_sessionlist4Proxy != NULL) {
569 free(g_sessionlist4Proxy);
570 g_sessionlist4Proxy = NULL;
571 }
572 if (g_sessionlist4File != NULL) {
573 free(g_sessionlist4File);
574 g_sessionlist4File = NULL;
575 }
576 if (g_nodeStateCallback != NULL) {
577 free(g_nodeStateCallback);
578 g_nodeStateCallback = NULL;
579 }
580 if (g_sessionlist4Stream != NULL) {
581 free(g_sessionlist4Stream);
582 g_sessionlist4Stream = NULL;
583 }
584 }
585
586 /**
587 * @tc.number : SUB_Softbus_Trans_SelfNet_0100
588 * @tc.name : 创建SS,等待opensession和消息传输
589 * @tc.desc : 测试自组网下传输功能,模拟服务端
590 * @tc.type : FUNC
591 * @tc.size : MediumTest
592 */
593 HWTEST_F(dsoftbusTest, test_create_ss, TestSize.Level3)
594 {
595 int dataRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA, g_sessionlist4Data);
596 LOG("CreateSs[data] ret:%d", dataRet);
597 int ctrlRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_CTL, g_sessionlist4Ctrl);
598 LOG("CreateSs[ctrl] ret:%d", ctrlRet);
599 int perfRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_PERF, g_sessionlist4Perf);
600 LOG("CreateSs[perf] ret:%d", perfRet);
601 int passRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_PASS, g_sessionlist4Pass);
602 LOG("CreateSs[pass] ret:%d", passRet);
603 int proxyRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_PROXY, g_sessionlist4Proxy);
604 LOG("CreateSs[Proxy] ret:%d", proxyRet);
605 int fileRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_FILE, g_sessionlist4File);
606 LOG("CreateSs[file] ret:%d", fileRet);
607 int streamRet = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_STREAM, g_sessionlist4Stream);
608 LOG("CreateSs[stream] ret:%d", streamRet);
609
610 int runtime = 0;
611 if (dataRet == SOFTBUS_OK && ctrlRet == SOFTBUS_OK && perfRet == SOFTBUS_OK
612 && passRet == SOFTBUS_OK && fileRet == SOFTBUS_OK && streamRet == SOFTBUS_OK && proxyRet == SOFTBUS_OK) {
613 LOG("CreateSs ok");
614 }
615
616 int ret = SetFileSendListener(DEF_PKG_NAME, SESSION_NAME_FILE, GetSendFileListener());
617 if (ret != SOFTBUS_OK) {
618 LOG("##set send listener fail:%d", ret);
619 }
620 ret = SetFileReceiveListener(DEF_PKG_NAME, SESSION_NAME_FILE, GetRecvFileListener(), RECV_FILE_PATH);
621 if (ret != SOFTBUS_OK) {
622 LOG("##set recv listener fail:%d", ret);
623 }
624
625 while (1) {
626 sleep(1);
627 runtime += 1;
628 if (runtime % 60 == 0) {
629 LOG("### test run:%d s", runtime);
630 }
631 }
632
633 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA);
634 LOG("RemoveSs[data] ret:%d", ret);
635 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_CTL);
636 LOG("RemoveSs[ctrl] ret:%d", ret);
637 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_PERF);
638 LOG("RemoveSs[perf] ret:%d", ret);
639 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_PASS);
640 LOG("RemoveSs[pass] ret:%d", ret);
641 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_PROXY);
642 LOG("RemoveSs[Proxy] ret:%d", ret);
643 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_FILE);
644 LOG("RemoveSs[file] ret:%d", ret);
645 ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_FILE);
646 LOG("RemoveSs[stram] ret:%d", ret);
647 }