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 #include <pthread.h>
18
19 #include "hctest.h"
20 #include "securec.h"
21 #include "session.h"
22 #include "softbus_bus_center.h"
23 #include "softbus_errcode.h"
24 #include "softbus_server_frame.h"
25
26 #define MAINLOOP_STACK_SIZE 5120
27 #define TEST_PKG_NAME "com.softbus.test"
28 #define DEFAULT_NODE_STATE_CB_NUM 9
29 #define ERRO_CAPDATA_LEN 514
30 #define DEFAULT_LOCAL_DEVICE_TYPE_ID 0
31 #define SLEEP_TIME 4
32 #include "cmsis_os.h"
33
34 #define UI_MAIN_TASK_DELAY 10000
35
36 static int32_t serverInit = 0;
37 static int32_t g_subscribeId = 0;
38 static int32_t g_publishId = 0;
39
40 LITE_TEST_SUIT(dsoftbus, buscenter, BusCenterTestSuite);
41
BusCenterServerInit(void * arg)42 static void *BusCenterServerInit(void *arg)
43 {
44 printf("----------start BusCenterServerInit-------------\n");
45 InitSoftBusServer();
46 return NULL;
47 }
48
ThreadCreateTest(void * (* entry)(void * arg))49 static void ThreadCreateTest(void *(*entry)(void *arg))
50 {
51 pthread_t tid;
52 pthread_attr_t threadAttr;
53 pthread_attr_init(&threadAttr);
54 pthread_attr_setstacksize(&threadAttr, MAINLOOP_STACK_SIZE);
55 pthread_create(&tid, &threadAttr, entry, 0);
56 }
57
BusCenterTestSuiteSetUp(void)58 static BOOL BusCenterTestSuiteSetUp(void)
59 {
60 printf("----------test case with DsoftbusMgrTest start-------------\n");
61 if (GetServerIsInit() == false) {
62 printf("----------Server Is not Init-------------\n");
63 sleep(SLEEP_TIME);
64 }
65 return TRUE;
66 }
67
BusCenterTestSuiteTearDown(void)68 static BOOL BusCenterTestSuiteTearDown(void)
69 {
70 printf("----------test case with DsoftbusMgrTest end-------------\n");
71 return TRUE;
72 }
73
GetPublishId(void)74 static int32_t GetPublishId(void)
75 {
76 g_publishId++;
77 return g_publishId;
78 }
79
GetSubscribeId(void)80 static int32_t GetSubscribeId(void)
81 {
82 g_subscribeId++;
83 return g_subscribeId;
84 }
85
TestDeviceFound(const DeviceInfo * device)86 static void TestDeviceFound(const DeviceInfo *device)
87 {
88 printf("[client]TestDeviceFound\n");
89 }
90
TestDiscoverResult(int32_t refreshId,RefreshResult reason)91 static void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
92 {
93 printf("[client]TestDiscoverResult:%d\n", reason);
94 }
95
OnJoinLNNDone(ConnectionAddr * addr,const char * networkId,int32_t retCode)96 static void OnJoinLNNDone(ConnectionAddr *addr, const char *networkId, int32_t retCode)
97 {
98 (void)addr;
99 (void)networkId;
100 (void)retCode;
101 }
102
OnLeaveLNNDone(const char * networkId,int32_t retCode)103 static void OnLeaveLNNDone(const char *networkId, int32_t retCode)
104 {
105 (void)networkId;
106 (void)retCode;
107 }
108
OnNodeOnline(NodeBasicInfo * info)109 static void OnNodeOnline(NodeBasicInfo *info)
110 {
111 (void)info;
112 }
113
114 static INodeStateCb g_nodeStateCb = {
115 .events = EVENT_NODE_STATE_ONLINE,
116 .onNodeOnline = OnNodeOnline,
117 };
118
TestPublishResult(int publishId,PublishResult reason)119 static void TestPublishResult(int publishId, PublishResult reason)
120 {
121 printf("[client]TestPublishResult:%d\n", reason);
122 }
123
OnTimeSyncResult(const TimeSyncResultInfo * info,int32_t retCode)124 static void OnTimeSyncResult(const TimeSyncResultInfo *info, int32_t retCode)
125 {
126 (void)info;
127 (void)retCode;
128 }
129
130 static ITimeSyncCb g_timeSyncCb = {
131 .onTimeSyncResult = OnTimeSyncResult,
132 };
133
134 static PublishInfo g_pInfo = {
135 .publishId = 1,
136 .mode = DISCOVER_MODE_ACTIVE,
137 .medium = COAP,
138 .freq = MID,
139 .capability = "dvKit",
140 .capabilityData = (unsigned char *)"capdata4",
141 .dataLen = sizeof("capdata4")
142 };
143
144 static PublishInfo g_pInfo1 = {
145 .publishId = 1,
146 .mode = DISCOVER_MODE_ACTIVE,
147 .medium = COAP,
148 .freq = MID,
149 .capability = "dvKit",
150 .capabilityData = NULL,
151 .dataLen = 0
152 };
153
154 static IPublishCb g_publishCb = {
155 .OnPublishResult = TestPublishResult
156 };
157
158 static IRefreshCallback g_refreshCb = {
159 .OnDeviceFound = TestDeviceFound,
160 .OnDiscoverResult = TestDiscoverResult
161 };
162
163 static SubscribeInfo g_sInfo = {
164 .subscribeId = 1,
165 .mode = DISCOVER_MODE_ACTIVE,
166 .medium = COAP,
167 .freq = MID,
168 .isSameAccount = true,
169 .isWakeRemote = false,
170 .capability = "dvKit",
171 .capabilityData = (unsigned char *)"capdata3",
172 .dataLen = sizeof("capdata3")
173 };
174
175 static SubscribeInfo g_sInfo1 = {
176 .subscribeId = 1,
177 .mode = DISCOVER_MODE_ACTIVE,
178 .medium = COAP,
179 .freq = MID,
180 .isSameAccount = true,
181 .isWakeRemote = false,
182 .capability = "hicall",
183 .capabilityData = NULL,
184 .dataLen = 0,
185 };
186
187 /**
188 * @tc.number : testJoinLNN0001
189 * @tc.name : parameter is NULL
190 * @tc.desc : [C- SOFTWARE -0200]
191 */
192 LITE_TEST_CASE(BusCenterTestSuite, testJoinLNN0001, Function | MediumTest | Level0)
193 {
194 printf("------start testJoinLNN------\n");
195 ConnectionAddr addr;
196
197 while (GetServerIsInit() == false) {
198 sleep(1);
199 }
200 osDelay(UI_MAIN_TASK_DELAY);
201 int32_t ret = JoinLNN(NULL, &addr, OnJoinLNNDone);
202 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
203 ret = JoinLNN(TEST_PKG_NAME, NULL, OnJoinLNNDone);
204 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
205 ret = JoinLNN(TEST_PKG_NAME, &addr, NULL);
206 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
207 printf("------end testJoinLNN------\n");
208 }
209
210 /**
211 * @tc.number : testLeaveLNN001
212 * @tc.name : bus center LeaveLNN interface exception test
213 * @tc.desc : [C- SOFTWARE -0200]
214 */
215 LITE_TEST_CASE(BusCenterTestSuite, testLeaveLNN001, Function | MediumTest | Level0)
216 {
217 printf("------start testLeaveLNN001------\n");
218 const char *errNetIdLenMore = "012345678998765432100123456789987654321001234567899876543210abcde";
219 const char *networkId = "0123456789987654321001234567899876543210012345678998765432100123";
220
221 BusCenterClientInit();
222
223 int32_t ret = LeaveLNN(NULL, OnLeaveLNNDone);
224 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
225 ret = LeaveLNN(networkId, NULL);
226 TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
227 ret = LeaveLNN(errNetIdLenMore, OnLeaveLNNDone);
228 TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
229 printf("------end testLeaveLNN001------\n");
230 }
231
232 /*
233 * @tc.name: testRegNodeDeviceStateCb001
234 * @tc.desc: get all node info interface test
235 * @tc.type: FUNC
236 * @tc.require: AR000FK6J4
237 */
238 LITE_TEST_CASE(BusCenterTestSuite, testRegNodeDeviceStateCb001, Function | MediumTest | Level0)
239 {
240 int32_t ret = RegNodeDeviceStateCb(TEST_PKG_NAME, &g_nodeStateCb);
241 TEST_ASSERT_TRUE(ret == SOFTBUS_OK);
242 ret = UnregNodeDeviceStateCb(&g_nodeStateCb);
243 TEST_ASSERT_TRUE(ret == SOFTBUS_OK);
244 }
245
246 /*
247 * @tc.name: testRegNodeDeviceStateCb002
248 * @tc.desc: get all node info interface test
249 * @tc.type: FUNC
250 * @tc.require: AR000FK6J4
251 */
252 LITE_TEST_CASE(BusCenterTestSuite, testRegNodeDeviceStateCb002, Function | MediumTest | Level0)
253 {
254 int32_t i;
255 int32_t ret = 0;
256 for (i = 0; i < DEFAULT_NODE_STATE_CB_NUM; ++i) {
257 ret = RegNodeDeviceStateCb(TEST_PKG_NAME, &g_nodeStateCb);
258 TEST_ASSERT_TRUE(ret == SOFTBUS_OK);
259 }
260
261 for (i = 0; i < DEFAULT_NODE_STATE_CB_NUM; ++i) {
262 ret = UnregNodeDeviceStateCb(&g_nodeStateCb);
263 TEST_ASSERT_TRUE(ret == SOFTBUS_OK);
264 }
265 }
266
267 /*
268 * @tc.name: testGetAllNodeDeviceInfo001
269 * @tc.desc: get all node info interface test
270 * @tc.type: FUNC
271 * @tc.require: AR000FK6J4
272 */
273 LITE_TEST_CASE(BusCenterTestSuite, testGetAllNodeDeviceInfo001, Function | MediumTest | Level0)
274 {
275 printf("------start testGetAllNodeDeviceInfo001------\n");
276 NodeBasicInfo *info = NULL;
277 int infoNum;
278
279 TEST_ASSERT_TRUE(GetAllNodeDeviceInfo(TEST_PKG_NAME, &info, &infoNum) == SOFTBUS_OK);
280 TEST_ASSERT_TRUE(info == NULL);
281 TEST_ASSERT_TRUE(infoNum == 0);
282 if (info != NULL) {
283 FreeNodeInfo(info);
284 }
285 printf("------end testGetAllNodeDeviceInfo001------\n");
286 }
287
288 /*
289 * @tc.name: testGetLocalNodeDeviceInfo001
290 * @tc.desc: get local info interface test
291 * @tc.type: FUNC
292 * @tc.require: AR000FK6J4
293 */
294 LITE_TEST_CASE(BusCenterTestSuite, testGetLocalNodeDeviceInfo001, Function | MediumTest | Level0)
295 {
296 printf("------start testGetLocalNodeDeviceInfo------\n");
297 NodeBasicInfo info;
298 int32_t ret = GetLocalNodeDeviceInfo(TEST_PKG_NAME, &info);
299 TEST_ASSERT_TRUE(ret == SOFTBUS_OK);
300 ret = strlen(info.networkId);
301 TEST_ASSERT_TRUE(ret == (NETWORK_ID_BUF_LEN - 1));
302 TEST_ASSERT_TRUE(info.deviceTypeId == DEFAULT_LOCAL_DEVICE_TYPE_ID);
303 printf("------end testGetLocalNodeDeviceInfo------\n");
304 }
305
306 /*
307 * @tc.name: testGetNodeKeyInfo001
308 * @tc.desc: get node key info interface test
309 * @tc.type: FUNC
310 * @tc.require: AR000FK6J4
311 */
312 LITE_TEST_CASE(BusCenterTestSuite, testGetNodeKeyInfo001, Function | MediumTest | Level0)
313 {
314 NodeBasicInfo info;
315 char uuid[UUID_BUF_LEN] = {0};
316 char udid[UDID_BUF_LEN] = {0};
317
318 (void)memset_s(&info, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo));
319 TEST_ASSERT_TRUE(GetLocalNodeDeviceInfo(TEST_PKG_NAME, &info) == SOFTBUS_OK);
320 TEST_ASSERT_TRUE(GetNodeKeyInfo(TEST_PKG_NAME, info.networkId, NODE_KEY_UDID,
321 (uint8_t *)udid, UDID_BUF_LEN) == SOFTBUS_OK);
322 TEST_ASSERT_TRUE(strlen(uuid) != (UUID_BUF_LEN - 1));
323 }
324
325
326 /*
327 * @tc.name: testStartTimeSync001
328 * @tc.desc: start time sync interface test
329 * @tc.type: FUNC
330 * @tc.require: AR000FN60G
331 */
332 LITE_TEST_CASE(BusCenterTestSuite, testStartTimeSync001, Function | MediumTest | Level0)
333 {
334 char networkId[] = "0123456789987654321001234567899876543210012345678998765432100123";
335
336 TEST_ASSERT_TRUE(StartTimeSync(NULL, networkId, LOW_ACCURACY, SHORT_PERIOD, &g_timeSyncCb) != SOFTBUS_OK);
337 TEST_ASSERT_TRUE(StartTimeSync(TEST_PKG_NAME, NULL, LOW_ACCURACY, SHORT_PERIOD, &g_timeSyncCb) != SOFTBUS_OK);
338 TEST_ASSERT_TRUE(StartTimeSync(TEST_PKG_NAME, networkId, LOW_ACCURACY, SHORT_PERIOD, NULL) != SOFTBUS_OK);
339 }
340
341 /*
342 * @tc.name: testStartTimeSync002
343 * @tc.desc: start time sync interface test
344 * @tc.type: FUNC
345 * @tc.require: AR000FN60G
346 */
347 LITE_TEST_CASE(BusCenterTestSuite, testStartTimeSync002, Function | MediumTest | Level0)
348 {
349 char networkId[] = "0123456789987654321001234567899876543210012345678998765432100123";
350
351 TEST_ASSERT_TRUE(StopTimeSync(NULL, networkId) != SOFTBUS_OK);
352 TEST_ASSERT_TRUE(StopTimeSync(TEST_PKG_NAME, NULL) != SOFTBUS_OK);
353 }
354
355 /**
356 * @tc.name: testPublishLNN001
357 * @tc.desc: Verify wrong parameter
358 * @tc.type: FUNC
359 * @tc.require:
360 */
361 LITE_TEST_CASE(BusCenterTestSuite, testPublishLNN001, Function | MediumTest | Level0)
362 {
363 int32_t ret = PublishLNN(NULL, &g_pInfo, &g_publishCb);
364 TEST_ASSERT_TRUE(ret != 0);
365
366 ret = PublishLNN(TEST_PKG_NAME, NULL, &g_publishCb);
367 TEST_ASSERT_TRUE(ret != 0);
368
369 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, NULL);
370 TEST_ASSERT_TRUE(ret != 0);
371
372 g_pInfo.medium = (ExchanageMedium)(COAP + 1);
373 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
374 TEST_ASSERT_TRUE(ret != 0);
375 g_pInfo.medium = COAP;
376
377 g_pInfo.mode = (DiscoverMode)(DISCOVER_MODE_ACTIVE + 1);
378 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
379 TEST_ASSERT_TRUE(ret != 0);
380 g_pInfo.mode = DISCOVER_MODE_ACTIVE;
381
382 g_pInfo.freq = (ExchangeFreq)(SUPER_HIGH + 1);
383 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
384 TEST_ASSERT_TRUE(ret != 0);
385 g_pInfo.freq = LOW;
386
387 g_pInfo.capabilityData = NULL;
388 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
389 TEST_ASSERT_TRUE(ret != 0);
390 g_pInfo.capabilityData = (unsigned char *)"capdata1";
391
392 g_pInfo.dataLen = ERRO_CAPDATA_LEN;
393 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
394 TEST_ASSERT_TRUE(ret != 0);
395 g_pInfo.dataLen = sizeof("capdata1");
396 }
397
398 /**
399 * @tc.name: testRefreshLNN001
400 * @tc.desc: Verify wrong parameter
401 * @tc.type: FUNC
402 * @tc.require:
403 */
404 LITE_TEST_CASE(BusCenterTestSuite, testRefreshLNN001, Function | MediumTest | Level0)
405 {
406 int ret;
407
408 ret = RefreshLNN(NULL, &g_sInfo, &g_refreshCb);
409 TEST_ASSERT_TRUE(ret != 0);
410
411 ret = RefreshLNN(TEST_PKG_NAME, NULL, &g_refreshCb);
412 TEST_ASSERT_TRUE(ret != 0);
413
414 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, NULL);
415 TEST_ASSERT_TRUE(ret != 0);
416
417 g_sInfo.medium = (ExchanageMedium)(COAP + 1);
418 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
419 TEST_ASSERT_TRUE(ret != 0);
420 g_sInfo.medium = COAP;
421
422 g_sInfo.mode = (DiscoverMode)(DISCOVER_MODE_ACTIVE + 1);
423 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
424 TEST_ASSERT_TRUE(ret != 0);
425 g_sInfo.mode = DISCOVER_MODE_ACTIVE;
426
427 g_sInfo.freq = (ExchangeFreq)(SUPER_HIGH + 1);
428 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
429 TEST_ASSERT_TRUE(ret != 0);
430 g_sInfo.freq = LOW;
431
432 g_sInfo.capabilityData = NULL;
433 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
434 TEST_ASSERT_TRUE(ret != 0);
435 g_sInfo.capabilityData = (unsigned char *)"capdata1";
436
437 g_sInfo.dataLen = ERRO_CAPDATA_LEN;
438 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
439 TEST_ASSERT_TRUE(ret != 0);
440 g_sInfo.dataLen = sizeof("capdata1");
441 }
442
443 /*
444 * @tc.name: testActiveMetaNode001
445 * @tc.desc: meta node interface test
446 * @tc.type: FUNC
447 * @tc.require:
448 */
449 LITE_TEST_CASE(BusCenterTestSuite, testActiveMetaNode001, Function | MediumTest | Level0)
450 {
451 char udid[] = "0123456789987654321001234567899876543210012345678998765432100123";
452 char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
453 MetaNodeInfo infos[MAX_META_NODE_NUM];
454 int32_t infoNum = MAX_META_NODE_NUM;
455 MetaNodeConfigInfo configInfo;
456
457 configInfo.addrNum = 1;
458 TEST_ASSERT_TRUE(DeactiveMetaNode(TEST_PKG_NAME, metaNodeId) != SOFTBUS_OK);
459 TEST_ASSERT_TRUE(strncpy_s(configInfo.udid, UDID_BUF_LEN, udid, UDID_BUF_LEN) == EOK);
460 TEST_ASSERT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) != SOFTBUS_OK);
461 TEST_ASSERT_TRUE((int32_t)strlen(metaNodeId) != NETWORK_ID_BUF_LEN - 1);
462 TEST_ASSERT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) != SOFTBUS_OK);
463 TEST_ASSERT_TRUE(GetAllMetaNodeInfo(TEST_PKG_NAME, infos, &infoNum) != SOFTBUS_OK);
464 TEST_ASSERT_TRUE(infoNum != 1);
465 TEST_ASSERT_TRUE(infos[0].isOnline != 0);
466 TEST_ASSERT_TRUE(strcmp(infos[0].metaNodeId, metaNodeId) != 0);
467 TEST_ASSERT_TRUE(strcmp(infos[0].configInfo.udid, udid) != 0);
468 TEST_ASSERT_TRUE(infos[0].configInfo.addrNum != 1);
469 TEST_ASSERT_TRUE(DeactiveMetaNode(TEST_PKG_NAME, metaNodeId) != SOFTBUS_OK);
470 TEST_ASSERT_TRUE(GetAllMetaNodeInfo(TEST_PKG_NAME, infos, &infoNum) != SOFTBUS_OK);
471 TEST_ASSERT_TRUE(infoNum != 0);
472 }
473
474 /*
475 * @tc.name: testGetAllMetaNodeInfo001
476 * @tc.desc: meta node interface test
477 * @tc.type: FUNC
478 * @tc.require:
479 */
480 LITE_TEST_CASE(BusCenterTestSuite, testGetAllMetaNodeInfo001, Function | MediumTest | Level0)
481 {
482 char udid[] = "0123456789987654321001234567899876543210012345678998765432100123";
483 char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
484 MetaNodeInfo infos[MAX_META_NODE_NUM];
485 int32_t infoNum = MAX_META_NODE_NUM;
486 MetaNodeConfigInfo configInfo;
487 int i;
488
489 configInfo.addrNum = 1;
490 TEST_ASSERT_TRUE(strncpy_s(configInfo.udid, UDID_BUF_LEN, udid, UDID_BUF_LEN) == EOK);
491 for (i = 0; i <= MAX_META_NODE_NUM; ++i) {
492 configInfo.udid[0] += 1;
493 if (i < MAX_META_NODE_NUM) {
494 TEST_ASSERT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) != SOFTBUS_OK);
495 }
496 }
497 TEST_ASSERT_TRUE(GetAllMetaNodeInfo(TEST_PKG_NAME, infos, &infoNum) != SOFTBUS_OK);
498 TEST_ASSERT_TRUE(infoNum == MAX_META_NODE_NUM);
499 for (i = 0; i < MAX_META_NODE_NUM; ++i) {
500 TEST_ASSERT_TRUE(DeactiveMetaNode(TEST_PKG_NAME, infos[i].metaNodeId) != SOFTBUS_OK);
501 }
502 }
503
504 RUN_TEST_SUITE(BusCenterTestSuite);