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 <arpa/inet.h>
17 #include <cstdio>
18 #include <dlfcn.h>
19 #include <gtest/gtest.h>
20 #include <netdb.h>
21 #include <pthread.h>
22 #include <sys/socket.h>
23 #include <vector>
24
25 #include "nativetoken_kit.h"
26 #include "shm_utils.h"
27 #include "softbus_permission.h"
28 #include "token_setproc.h"
29 #include "unistd.h"
30 #include "net_trans_common.h"
31 using namespace std;
32 using namespace NetTrans;
33 using namespace testing::ext;
34
35 const int MAX_DATA_LENGTH = 1024;
36 const char DEF_GROUP_ID[50] = "DEF_GROUP_ID";
37 const char DEF_PKG_NAME[50] = "com.communication.demo";
38 const char SESSION_NAME_DATA[50] = "com.communication.demo.data";
39 const char SHM_DATA_RES[50] = "9999";
40
41 const int WAIT_DEF_VALUE = -1;
42 const int WAIT_SUCCESS_VALUE = 1;
43 const int WAIT_FAIL_VALUE = 0;
44 const int SESSION_ID_MIN = 1;
45 const int MAX_SESSION_NUM = 16;
46 const int INT_TRUE = 1;
47 const int FALSE = 0;
48 const int RET_SUCCESS = 0;
49
50 const int STR_PREFIX_FOUR = 4;
51 const int SLEEP_SECOND_ONE = 1;
52 const int SLEEP_SECOND_TEO = 2;
53 const int SLEEP_SECOND_TEN = 10;
54
55 const int CODE_LEN_FIVE = 5;
56 const int CODE_PREFIX_FOUR = 4;
57
58 static int32_t g_currentSessionId4Data = -1;
59 static int32_t g_waitFlag = WAIT_DEF_VALUE;
60 static int32_t g_waitFlag4Ctl = WAIT_DEF_VALUE;
61 static int32_t g_waitFlag4Data = WAIT_DEF_VALUE;
62 static int32_t g_nodeOnlineCount = 0;
63 static int32_t g_nodeOfflineCount = 0;
64 static int32_t g_sessionOpenCount4Data = 0;
65 static SessionAttribute* g_sessionAttr4Data = nullptr;
66 static ISessionListener* g_sessionlistener4Data = nullptr;
67 static char g_networkId[NETWORK_ID_BUF_LEN] = { 0 };
68 static INodeStateCb g_defNodeStateCallback;
69 static ConnectionAddr g_ethAddr = {
70 .type = CONNECTION_ADDR_WLAN,
71 };
72
73 static vector<string> g_networkIds;
74 static int g_subscribeId = 0;
75 static bool g_sessionClosed = false;
76 namespace NetTrans {
77 class NetTransCommon : public testing::Test {
78 public:
79 static void SetUpTestCase(void);
80 static void TearDownTestCase(void);
81 void SetUp();
82 void TearDown();
83 NetTransCommon();
84 };
SetUpTestCase(void)85 void NetTransCommon::SetUpTestCase(void)
86 {
87 uint64_t tokenId;
88 const char* perms[2];
89 perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
90 perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
91 NativeTokenInfoParams infoTnstance = {
92 .dcapsNum = 0,
93 .permsNum = 2,
94 .aclsNum = 0,
95 .dcaps = NULL,
96 .perms = perms,
97 .acls = NULL,
98 .processName = "dsoftbus_service",
99 .aplStr = "system_core",
100 };
101 tokenId = GetAccessTokenId(&infoTnstance);
102 SetSelfTokenID(tokenId);
103 sleep(1);
104 system("pidof accesstoken_ser | xargs kill -9");
105 sleep(1);
106 }
107
TearDownTestCase(void)108 void NetTransCommon::TearDownTestCase(void) {}
SetUp(void)109 void NetTransCommon::SetUp(void) {}
110
TearDown(void)111 void NetTransCommon::TearDown(void) {}
NetTransCommon(void)112 NetTransCommon::NetTransCommon(void) {}
113
Wait4Session(int timeout,WaitSessionType type)114 int Wait4Session(int timeout, WaitSessionType type)
115 {
116 int hitFlag = -1;
117 while (timeout > 0) {
118 sleep(SLEEP_SECOND_ONE);
119 switch (type) {
120 case WaitSessionType::SESSION_4CTL:
121 if (g_waitFlag4Ctl != WAIT_DEF_VALUE) {
122 LOG("Wait4Session success,flag:%d", g_waitFlag4Ctl);
123 hitFlag = 1;
124 }
125 break;
126 case WaitSessionType::SESSION_4DATA:
127 if (g_waitFlag4Data != WAIT_DEF_VALUE) {
128 LOG("Wait4Session success,flag:%d", g_waitFlag4Data);
129 hitFlag = 1;
130 }
131 break;
132 default:
133 LOG("Wait4Session type error");
134 hitFlag = 1;
135 break;
136 }
137 if (hitFlag != -1) {
138 break;
139 }
140 timeout--;
141 }
142 switch (type) {
143 case WaitSessionType::SESSION_4CTL:
144 if (g_waitFlag4Ctl != WAIT_SUCCESS_VALUE) {
145 LOG("Wait4Session FAIL,flag:%d", g_waitFlag4Ctl);
146 return SOFTBUS_ERR;
147 }
148 break;
149 case WaitSessionType::SESSION_4DATA:
150 if (g_waitFlag4Data != WAIT_SUCCESS_VALUE) {
151 LOG("Wait4Session FAIL,flag:%d", g_waitFlag4Data);
152 return SOFTBUS_ERR;
153 }
154 break;
155 default:
156 LOG("Wait4Session type error");
157 return SOFTBUS_ERR;
158 }
159 return SOFTBUS_OK;
160 }
161
OnDefNodeOnline(NodeBasicInfo * info)162 void OnDefNodeOnline(NodeBasicInfo* info)
163 {
164 LOG("OnDefNodeOnline");
165 if (info == nullptr) {
166 LOG("OnDefNodeOnline info is nullptr");
167 return;
168 }
169 if (strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, info->networkId, NETWORK_ID_BUF_LEN) != RET_SUCCESS) {
170 return;
171 }
172 LOG("Online id:%s,name:%s,type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
173 if (g_networkIds.empty()) {
174 g_nodeOnlineCount++;
175 g_networkIds.push_back(info->networkId);
176 LOG("test_wifi_onoff: g_networkIds.empty() g_nodeOnlineCount = %d", g_nodeOnlineCount);
177 LOG("test_wifi_onoff: g_networkIds.empty() g_sessionClosed= %d", g_sessionClosed);
178
179 for (const auto& networkId : g_networkIds) {
180 LOG("networkId = %s", networkId.c_str());
181 }
182
183 return;
184 }
185 std::vector<string>::const_iterator it;
186 for (it = g_networkIds.begin(); it != g_networkIds.end(); it++) {
187 LOG("test_wifi_onoff: *it = %s", (*it).c_str());
188 LOG("test_wifi_onoff: info->networkId = %s", info->networkId);
189 if (strcmp(info->networkId, (*it).c_str()) == 0) {
190 LOG("test_wifi_onoff: strcmp(info->networkId, *it) == 0");
191 return;
192 } else {
193 g_nodeOnlineCount++;
194 g_networkIds.push_back(info->networkId);
195 LOG("test_wifi_onoff: g_nodeOnlineCount = %d", g_nodeOnlineCount);
196 LOG("test_wifi_onoff: OnDefNodeOnline g_sessionClosed= %d", g_sessionClosed);
197 return;
198 }
199 }
200 }
201
OnDefNodeOffline(NodeBasicInfo * info)202 void OnDefNodeOffline(NodeBasicInfo* info)
203 {
204 LOG("OnDefNodeOffline");
205 if (info == nullptr) {
206 LOG("OnDefNodeOffline info is nullptr");
207 return;
208 }
209 LOG("Offline id:%s,name:%s,type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
210 if (g_networkIds.empty()) {
211 LOG("test_wifi_onoff: g_networkIds.empty() g_nodeOnlineCount = %d", g_nodeOnlineCount);
212 LOG("test_wifi_onoff: g_networkIds.empty() g_nodeOfflineCount = %d", g_nodeOfflineCount);
213 LOG("test_wifi_onoff: g_networkIds.empty() g_sessionClosed= %d", g_sessionClosed);
214 return;
215 }
216
217 std::vector<string>::const_iterator it;
218 for (it = g_networkIds.begin(); it != g_networkIds.end(); it++) {
219 LOG("*it = %s", (*it).c_str());
220 LOG("info->networkId = %s", info->networkId);
221 if (strcmp(info->networkId, (*it).c_str()) == 0) {
222 g_nodeOfflineCount++;
223 g_nodeOnlineCount--;
224 it = g_networkIds.erase(it);
225
226 LOG("test_wifi_onoff: g_nodeOnlineCount = %d", g_nodeOnlineCount);
227 LOG("test_wifi_onoff: g_nodeOfflineCount = %d", g_nodeOfflineCount);
228 LOG("test_wifi_onoff: g_sessionClosed= %d", g_sessionClosed);
229 return;
230 } else {
231 LOG("test_wifi_onoff: strcmp(info->networkId, *it) !!= 0");
232 return;
233 }
234 }
235 }
236
OnDefNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)237 void OnDefNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo* info)
238 {
239 if (info == nullptr) {
240 LOG("OnDefNodeBasicInfoChanged info is nullptr");
241 return;
242 }
243
244 LOG("InfoChanged id:%s,name:%s", info->networkId, info->deviceName);
245 }
246
DataSessionOpened(int sessionId,int result)247 int DataSessionOpened(int sessionId, int result)
248 {
249 LOG("DataSessionOpened sessionId=%d,result=%d", sessionId, result);
250
251 g_sessionOpenCount4Data++;
252 if (result == SOFTBUS_OK) {
253 g_sessionClosed = false;
254 SetCurrentSessionId4Data(sessionId);
255 if (sessionId == g_currentSessionId4Data) {
256 LOG("openSession check success sessionId=%d", sessionId);
257 g_waitFlag4Data = WAIT_SUCCESS_VALUE;
258 } else {
259 LOG("open session callback %d not match open sid %d", sessionId, g_currentSessionId4Data);
260 g_waitFlag4Data = WAIT_FAIL_VALUE;
261 }
262 } else {
263 g_waitFlag4Data = WAIT_FAIL_VALUE;
264 }
265 LOG("DataSessionOpened,g_waitFlag4Data:%d", g_waitFlag4Data);
266 return SOFTBUS_OK;
267 }
268
ResetWaitFlag4Data(void)269 void ResetWaitFlag4Data(void)
270 {
271 g_waitFlag = WAIT_DEF_VALUE;
272 }
273
ResetWaitCount4Offline(void)274 void ResetWaitCount4Offline(void)
275 {
276 g_nodeOfflineCount = 0;
277 }
278
ResetWaitCount4Online(void)279 void ResetWaitCount4Online(void)
280 {
281 g_nodeOnlineCount = 0;
282 }
283
DataSessionClosed(int sessionId)284 void DataSessionClosed(int sessionId)
285 {
286 g_sessionClosed = true;
287 LOG("close session %d", sessionId);
288 LOG("DataSessionClosed g_sessionClosed= %d", g_sessionClosed);
289 }
290
DataBytesReceived(int sessionId,const void * data,unsigned int dataLen)291 void DataBytesReceived(int sessionId, const void* data, unsigned int dataLen)
292 {
293 LOG("byteRec start");
294 }
295
DataMessageReceived(int sessionId,const void * data,unsigned int dataLen)296 void DataMessageReceived(int sessionId, const void* data, unsigned int dataLen)
297 {
298 LOG("MsgRec start sessionId=%d, dataLen = %d, data=%s", sessionId, dataLen, (char*)data);
299
300 unsigned int maxLen = MAX_DATA_LENGTH;
301 if (dataLen <= maxLen) {
302 int* code = (int*)malloc(sizeof(int));
303 char* buf = (char*)malloc(MAX_DATA_LENGTH);
304 (void)memset_s(buf, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
305 if (strncpy_s(buf, MAX_DATA_LENGTH, (char*)data, dataLen) != RET_SUCCESS) {
306 return;
307 }
308 if (*code != -1) {
309 pthread_t thread;
310 int ret = pthread_create(&thread, nullptr, DataOperateTask, buf);
311 pthread_join(thread, nullptr);
312 LOG("create thread ret:%d", ret);
313 } else {
314 free(code);
315 }
316 }
317 }
318
RegisterDeviceStateDefCallback(void)319 int RegisterDeviceStateDefCallback(void)
320 {
321 return RegNodeDeviceStateCb(DEF_PKG_NAME, &g_defNodeStateCallback);
322 }
323
UnRegisterDeviceStateDefCallback(void)324 int UnRegisterDeviceStateDefCallback(void)
325 {
326 return UnregNodeDeviceStateCb(&g_defNodeStateCallback);
327 }
328
CreateSsAndOpenSession4Data()329 int CreateSsAndOpenSession4Data()
330 {
331 int ret = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA, g_sessionlistener4Data);
332 if (ret != SOFTBUS_OK) {
333 LOG("call createSessionServer fail, ret:%d", ret);
334 return ret;
335 }
336
337 pthread_t sendThread;
338 pthread_create(&sendThread, nullptr, SendMsgTask, nullptr);
339 pthread_join(sendThread, nullptr);
340
341 return ret;
342 }
343
OpenSession4Data(void)344 int OpenSession4Data(void)
345 {
346 int sessionId;
347
348 sessionId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, DEF_GROUP_ID, g_sessionAttr4Data);
349 if (sessionId < SESSION_ID_MIN) {
350 LOG("call open session fail ssid:%d", sessionId);
351 return SOFTBUS_ERR;
352 }
353
354 LOG("call open session SUCCESS ssid:%d", sessionId);
355 sleep(SLEEP_SECOND_TEN);
356 return 0;
357 }
358
SendDataMsgToRemote(CtrlCodeType code,char * data)359 int SendDataMsgToRemote(CtrlCodeType code, char* data)
360 {
361 int ret = -1;
362 int size = MAX_DATA_LENGTH;
363 char* msg = (char*)malloc(size);
364 if (msg == nullptr) {
365 return ret;
366 }
367 (void)memset_s(msg, size, 0, size);
368
369 if (strcpy_s(msg, size, data) != RET_SUCCESS) {
370 return ret;
371 }
372 ret = SendMessage(g_currentSessionId4Data, msg, strlen(msg));
373 LOG("send msg ret:%d", ret);
374 free(data);
375 return ret;
376 }
377
CloseSessionAndRemoveSs4Data(void)378 int CloseSessionAndRemoveSs4Data(void)
379 {
380 int ret;
381 int timeout = 10;
382 ResetWaitFlag4Data();
383 CloseSession(g_currentSessionId4Data);
384 ret = Wait4Session(timeout, WaitSessionType::SESSION_4DATA);
385 if (ret == SOFTBUS_OK) {
386 LOG("close session success");
387 }
388
389 int retss = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA);
390 if (retss != SOFTBUS_OK) {
391 LOG("remove session ret:%d", retss);
392 }
393
394 if (ret != SOFTBUS_OK || retss == SOFTBUS_OK) {
395 return SOFTBUS_ERR;
396 } else {
397 return SOFTBUS_OK;
398 }
399 }
400
IncrementSubId(void)401 int IncrementSubId(void)
402 {
403 g_subscribeId++;
404 return g_subscribeId;
405 }
406
OnDataMessageReceived(int sessionId,const char * data,unsigned int dataLen)407 void OnDataMessageReceived(int sessionId, const char* data, unsigned int dataLen)
408 {
409 LOG("msg received %s", data);
410 if (sessionId < 0 || sessionId > MAX_SESSION_NUM) {
411 LOG("message received invalid session %d", sessionId);
412 return;
413 }
414
415 LOG("msg received sid:%d, data-len:%d", sessionId, dataLen);
416
417 unsigned int maxLen = MAX_DATA_LENGTH;
418 if (dataLen <= maxLen) {
419 int* code = (int*)malloc(sizeof(int));
420 char* buf = (char*)malloc(MAX_DATA_LENGTH);
421 if (strcpy_s(buf, MAX_DATA_LENGTH, data) != RET_SUCCESS) {
422 return;
423 }
424 if (*code != -1) {
425 pthread_t thread;
426 int ret = pthread_create(&thread, nullptr, DataOperateTask, buf);
427 pthread_join(thread, nullptr);
428 LOG("create thread ret:%d", ret);
429 } else {
430 free(code);
431 }
432 }
433 }
434
SendMsgTask(void * param)435 void* SendMsgTask(void* param)
436 {
437 LOG("SendMsgTask send...%s", (char*)param);
438 int sessionId;
439 int timeout = 10;
440 int ret = 0;
441 ResetWaitFlag4Data();
442
443 sessionId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, DEF_GROUP_ID, g_sessionAttr4Data);
444 if (sessionId < SESSION_ID_MIN) {
445 LOG("call open session faild ret:%d", sessionId);
446 }
447 SetCurrentSessionId4Data(sessionId);
448 LOG("call open session success sid:%d", sessionId);
449
450 ret = Wait4Session(timeout, WaitSessionType::SESSION_4DATA);
451 if (ret != SOFTBUS_OK) {
452 LOG("open session fail");
453 }
454
455 if (createShm(SHM_SEND_KEY) == -1) {
456 LOG("create shm faild");
457 return nullptr;
458 }
459
460 initShm();
461
462 char str[MAX_DATA_LENGTH] = { 0 };
463 LOG("test_wifi_onoff: while readDataFromShmNoClear ");
464 LOG("test_wifi_onoff: while g_sessionClosed=%d ", g_sessionClosed);
465 while (INT_TRUE) {
466 if (g_sessionClosed) {
467 break;
468 }
469
470 if (readDataFromShmNoClear(str) != 0) {
471 sleep(SLEEP_SECOND_TEO);
472 continue;
473 }
474
475 if (strncmp(SHM_DATA_RES, str, STR_PREFIX_FOUR) == 0) {
476 if (readDataFromShm(str) == 0) {
477 LOG("read result");
478 }
479 } else {
480 if (readDataFromShm(str) == 0) {
481 LOG("SendData send...%s", str);
482 SendMessage(g_currentSessionId4Data, str, strlen(str));
483 (void)memset_s(str, strlen(str) + 1, 0, strlen(str) + 1);
484 }
485 }
486
487 sleep(SLEEP_SECOND_TEO);
488 }
489
490 LOG("sendMsgTask end");
491 return nullptr;
492 }
493
DataOperateTask(void * param)494 void* DataOperateTask(void* param)
495 {
496 LOG("operate start...");
497 int code = -1;
498 char* codeType = (char*)malloc(CODE_LEN_FIVE);
499 if (codeType == nullptr) {
500 LOG("malloc fail");
501 return nullptr;
502 }
503 (void)memset_s(codeType, CODE_LEN_FIVE, 0, CODE_LEN_FIVE);
504 if (strncpy_s(codeType, CODE_LEN_FIVE, (char*)param, CODE_PREFIX_FOUR) != RET_SUCCESS) {
505 return nullptr;
506 }
507 if (sscanf_s(codeType, "%d", &code) <= RET_SUCCESS) {
508 return nullptr;
509 }
510 LOG("code :%d", code);
511
512 void* handle = nullptr;
513 int (*ProcessData)(int, char*);
514 char* error;
515 int ret = 0;
516 if (code == int(CtrlCodeType::CTRL_CODE_RESULT_TYPE)) {
517 writeDataToShm((char*)param);
518 free((char*)param);
519 return nullptr;
520 } else if (code > int(CtrlCodeType::CTRL_CODE_DATAMGR_TYPE) && code < int(CtrlCodeType::CTRL_CODE_DM_TYPE)) {
521 handle = dlopen("/system/lib64/libdisDataProcess.z.so", RTLD_LAZY);
522 if (!handle) {
523 LOG("dlopen failed %s", dlerror());
524 }
525 ProcessData = (int (*)(int, char*))dlsym(handle, "_Z14ProcessDataMgriPc");
526 if ((error = dlerror()) != nullptr) {
527 LOG("dlsym failed %s", dlerror());
528 }
529 ret = (*ProcessData)(code, (char*)param);
530 LOG("code:%d", ret);
531 }
532
533 char* str = (char*)malloc(MAX_DATA_LENGTH);
534 if (str == nullptr) {
535 return nullptr;
536 }
537 (void)memset_s(str, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
538 int resSprint = sprintf_s(str, MAX_DATA_LENGTH, "%d:%d", CtrlCodeType::CTRL_CODE_RESULT_TYPE, ret);
539 if (resSprint < FALSE) {
540 return nullptr;
541 }
542 SendDataMsgToRemote(CtrlCodeType::CTRL_CODE_RESULT_TYPE, str);
543 free((char*)param);
544 LOG("operate end");
545 return nullptr;
546 }
547
CheckRemoteDeviceIsNull(int isSetNetId)548 int CheckRemoteDeviceIsNull(int isSetNetId)
549 {
550 int nodeNum = 0;
551 NodeBasicInfo* nodeInfo = nullptr;
552 int ret = GetAllNodeDeviceInfo(DEF_PKG_NAME, &nodeInfo, &nodeNum);
553 LOG("get node number is %d, ret %d", nodeNum, ret);
554 if (nodeInfo != nullptr && nodeNum > 0) {
555 LOG("get neiId is %s", nodeInfo->networkId);
556 if (isSetNetId == INT_TRUE) {
557 if (strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, nodeInfo->networkId, NETWORK_ID_BUF_LEN) != RET_SUCCESS) {
558 return SOFTBUS_ERR;
559 }
560 }
561 FreeNodeInfo(nodeInfo);
562 return SOFTBUS_OK;
563 } else {
564 LOG("nodeInfo is nullptr");
565 return SOFTBUS_ERR;
566 }
567 }
568
GetSessionListenser4Data(void)569 ISessionListener* GetSessionListenser4Data(void)
570 {
571 return g_sessionlistener4Data;
572 }
573
SetCurrentSessionId4Data(int sessionId)574 void SetCurrentSessionId4Data(int sessionId)
575 {
576 g_currentSessionId4Data = sessionId;
577 }
578
GetCurrentSessionId4Data(void)579 int GetCurrentSessionId4Data(void)
580 {
581 return g_currentSessionId4Data;
582 }
583
GetConnectAddr(void)584 ConnectionAddr* GetConnectAddr(void)
585 {
586 return &g_ethAddr;
587 }
588
init(void)589 void init(void)
590 {
591 g_defNodeStateCallback.events = EVENT_NODE_STATE_MASK;
592 g_defNodeStateCallback.onNodeOnline = OnDefNodeOnline;
593 g_defNodeStateCallback.onNodeOffline = OnDefNodeOffline;
594 g_defNodeStateCallback.onNodeBasicInfoChanged = OnDefNodeBasicInfoChanged;
595
596 if (g_sessionlistener4Data == nullptr) {
597 g_sessionlistener4Data = (ISessionListener*)calloc(1, sizeof(ISessionListener));
598 g_sessionlistener4Data->OnSessionOpened = DataSessionOpened;
599 g_sessionlistener4Data->OnSessionClosed = DataSessionClosed;
600 g_sessionlistener4Data->OnMessageReceived = DataMessageReceived;
601 g_sessionlistener4Data->OnBytesReceived = DataBytesReceived;
602 }
603
604 if (g_sessionAttr4Data == nullptr) {
605 g_sessionAttr4Data = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
606 g_sessionAttr4Data->dataType = TYPE_BYTES;
607 }
608
609 // SoftBusPermission::AddPermission(DEF_PKG_NAME);
610
611 int ret = RegisterDeviceStateDefCallback();
612 if (ret != SOFTBUS_OK) {
613 LOG("RegisterDeviceStateDefCallback FAILED ret=%d", ret);
614 }
615 }
616
destroy(void)617 void destroy(void)
618 {
619 if (g_sessionlistener4Data != nullptr) {
620 free(g_sessionlistener4Data);
621 g_sessionlistener4Data = nullptr;
622 }
623 }
624 /**
625 * @tc.number: DisTest_0001
626 * @tc.name: net trans common
627 * @tc.desc: net trans common
628 * @tc.type: FUNC
629 */
630 HWTEST_F(NetTransCommon, DisTest_0001, TestSize.Level1)
631 {
632 LOG("enter main");
633 init();
634 while (INT_TRUE) {
635 int ret = CheckRemoteDeviceIsNull(INT_TRUE);
636 if (ret == SOFTBUS_OK) {
637 break;
638 } else {
639 sleep(SLEEP_SECOND_TEO);
640 }
641 }
642
643 int ret = CreateSsAndOpenSession4Data();
644 if (ret != SOFTBUS_OK) {
645 LOG("CreateSsAndOpenSession4Ctl failed ret=%d", ret);
646 }
647
648 while (INT_TRUE) {
649 LOG("enter while success");
650 LOG("while g_nodeOnlineCount = %d", g_nodeOnlineCount);
651 LOG("while g_sessionClosed= %d", g_sessionClosed);
652 if (g_sessionClosed && (g_nodeOnlineCount > 0)) {
653 LOG("test_wifi_onoff:while (INT_TRUE)");
654 ret = CreateSsAndOpenSession4Data();
655 if (ret != SOFTBUS_OK) {
656 LOG("CreateSsAndOpenSession4Ctl failed ret=%d", ret);
657 }
658 }
659 sleep(SLEEP_SECOND_TEN);
660 }
661 }
662 }; // namespace NetTrans
663