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 #include "UTTest_softbus_connector.h"
16
17 #include <securec.h>
18 #include <unistd.h>
19 #include <cstdlib>
20 #include <string>
21 #include <thread>
22
23 #include "dm_anonymous.h"
24 #include "dm_constants.h"
25 #include "dm_device_info.h"
26 #include "dm_log.h"
27 #include "ipc_notify_auth_result_req.h"
28 #include "ipc_notify_device_state_req.h"
29 #include "ipc_notify_device_found_req.h"
30 #include "ipc_notify_discover_result_req.h"
31 #include "ipc_notify_publish_result_req.h"
32 #include "ipc_notify_verify_auth_result_req.h"
33 #include "parameter.h"
34 #include "system_ability_definition.h"
35
36 namespace OHOS {
37 namespace DistributedHardware {
SetUp()38 void SoftbusConnectorTest::SetUp()
39 {
40 }
TearDown()41 void SoftbusConnectorTest::TearDown()
42 {
43 }
SetUpTestCase()44 void SoftbusConnectorTest::SetUpTestCase()
45 {
46 }
TearDownTestCase()47 void SoftbusConnectorTest::TearDownTestCase()
48 {
49 }
50
51 namespace {
52 std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
53 std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
54 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
55 std::shared_ptr<SoftbusListener> softbusListener = std::make_shared<SoftbusListener>();
56 std::shared_ptr<DmDeviceStateManager> deviceStateMgr =
57 std::make_shared<DmDeviceStateManager>(softbusConnector, listener, hiChainConnector);
58 std::shared_ptr<DmDiscoveryManager> discoveryMgr = std::make_shared<DmDiscoveryManager>(softbusConnector, listener);
59 std::shared_ptr<DmPublishManager> publishMgr = std::make_shared<DmPublishManager>(softbusConnector, listener);
60 /**
61 * @tc.name: DeviceOnLine_001
62 * @tc.desc: go to the corrort case and return DM_OK
63 * @tc.type: FUNC
64 * @tc.require: AR000GHSJK
65 */
66 HWTEST_F(SoftbusConnectorTest, DeviceOnLine_001, testing::ext::TestSize.Level0)
67 {
68 std::string pkgName = "123";
69 DmDeviceInfo info;
70 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
71 deviceStateMgr->RegisterSoftbusStateCallback();
72 DeviceOnLine(info);
73 bool ret = false;
74 if (listener->ipcServerListener_.req_ != nullptr) {
75 ret = true;
76 listener->ipcServerListener_.req_ = nullptr;
77 }
78 EXPECT_EQ(ret, false);
79 SoftbusConnector::stateCallbackMap_.clear();
80 }
81
82 /**
83 * @tc.name: DeviceOnLine_002
84 * @tc.desc: go to the corrort case and return DM_OK
85 * @tc.type: FUNC
86 * @tc.require: AR000GHSJK
87 */
88 HWTEST_F(SoftbusConnectorTest, DeviceOnLine_002, testing::ext::TestSize.Level0)
89 {
90 std::string pkgName = "123";
91 DmDeviceInfo info;
92 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
93 DeviceOnLine(info);
94 bool ret = false;
95 if (listener->ipcServerListener_.req_ != nullptr) {
96 ret = true;
97 listener->ipcServerListener_.req_ = nullptr;
98 }
99 EXPECT_EQ(ret, false);
100 SoftbusConnector::stateCallbackMap_.clear();
101 }
102
103 /**
104 * @tc.name: DeviceOffLine_001
105 * @tc.desc: go to the corrort case and return false
106 * @tc.type: FUNC
107 * @tc.require: AR000GHSJK
108 */
109 HWTEST_F(SoftbusConnectorTest, DeviceOffLine_001, testing::ext::TestSize.Level0)
110 {
111 std::string pkgName = "123";
112 DmDeviceInfo info;
113 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
114 deviceStateMgr->RegisterSoftbusStateCallback();
115 DeviceOffLine(info);
116 bool ret = false;
117 if (listener->ipcServerListener_.req_ != nullptr) {
118 ret = true;
119 listener->ipcServerListener_.req_ = nullptr;
120 }
121 EXPECT_EQ(ret, true);
122 SoftbusConnector::stateCallbackMap_.clear();
123 }
124
125 /**
126 * @tc.name: DeviceOffLine_002
127 * @tc.desc: go to the corrort case and return false
128 * @tc.type: FUNC
129 * @tc.require: AR000GHSJK
130 */
131 HWTEST_F(SoftbusConnectorTest, DeviceOffLine_002, testing::ext::TestSize.Level0)
132 {
133 std::string pkgName = "123";
134 DmDeviceInfo info;
135 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
136 DeviceOffLine(info);
137 bool ret = false;
138 if (listener->ipcServerListener_.req_ != nullptr) {
139 ret = true;
140 listener->ipcServerListener_.req_ = nullptr;
141 }
142 EXPECT_EQ(ret, false);
143 SoftbusConnector::stateCallbackMap_.clear();
144 }
145
146 /**
147 * @tc.name: SoftbusConnector_001
148 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr
149 * @tc.type: FUNC
150 * @tc.require: AR000GHSJK
151 */
152 HWTEST_F(SoftbusConnectorTest, SoftbusConnector_001, testing::ext::TestSize.Level0)
153 {
154 std::shared_ptr<SoftbusConnector> m_SoftbusConnector = std::make_shared<SoftbusConnector>();
155 ASSERT_NE(m_SoftbusConnector, nullptr);
156 }
157
158 /**
159 * @tc.name: SoftbusConnector_002
160 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
161 * @tc.type: FUNC
162 * @tc.require: AR000GHSJK
163 */
164 HWTEST_F(SoftbusConnectorTest, SoftbusConnector_002, testing::ext::TestSize.Level0)
165 {
166 std::shared_ptr<SoftbusConnector> m_SoftbusConnector = std::make_shared<SoftbusConnector>();
167 m_SoftbusConnector.reset();
168 EXPECT_EQ(m_SoftbusConnector, nullptr);
169 }
170
171 /**
172 * @tc.name: RegisterSoftbusDiscoveryCallback_001
173 * @tc.desc: set pkgName = "com.ohos.helloworld";call RegisterSoftbusDiscoveryCallback function to corrort, return DM_OK
174 * @tc.type: FUNC
175 * @tc.require: AR000GHSJK
176 */
177 HWTEST_F(SoftbusConnectorTest, RegisterSoftbusDiscoveryCallback_001, testing::ext::TestSize.Level0)
178 {
179 std::string pkgName = "com.ohos.helloworld";
180 int ret1 = softbusConnector->RegisterSoftbusDiscoveryCallback(
181 pkgName, std::shared_ptr<ISoftbusDiscoveryCallback>(discoveryMgr));
182 int ret = SoftbusConnector::discoveryCallbackMap_.count(pkgName);
183 EXPECT_EQ(ret1, DM_OK);
184 EXPECT_EQ(ret, 1);
185 }
186
187 /**
188 * @tc.name: UnRegisterSoftbusDiscoveryCallback_001
189 * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusDiscoveryCallback function to corrort, return
190 * DM_OK
191 * @tc.type: FUNC
192 * @tc.require: AR000GHSJK
193 */
194 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusDiscoveryCallback_001, testing::ext::TestSize.Level0)
195 {
196 std::string pkgName = "com.ohos.helloworld";
197 int ret = softbusConnector->UnRegisterSoftbusDiscoveryCallback(pkgName);
198 int ret1 = SoftbusConnector::discoveryCallbackMap_.count(pkgName);
199 EXPECT_EQ(ret1, 0);
200 EXPECT_EQ(ret, DM_OK);
201 }
202
203 /**
204 * @tc.name: RegisterSoftbusPublishCallback_001
205 * @tc.desc: set pkgName = "com.ohos.helloworld";call RegisterSoftbusPublishCallback function to corrort, return DM_OK
206 * @tc.type: FUNC
207 * @tc.require: I5N1K3
208 */
209 HWTEST_F(SoftbusConnectorTest, RegisterSoftbusPublishCallback_001, testing::ext::TestSize.Level0)
210 {
211 std::string pkgName = "com.ohos.helloworld";
212 int ret1 = softbusConnector->RegisterSoftbusPublishCallback(
213 pkgName, std::shared_ptr<ISoftbusPublishCallback>(publishMgr));
214 int ret = SoftbusConnector::publishCallbackMap_.count(pkgName);
215 EXPECT_EQ(ret1, DM_OK);
216 EXPECT_EQ(ret, 1);
217 }
218
219 /**
220 * @tc.name: UnRegisterSoftbusPublishCallback_001
221 * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusPublishyCallback function to corrort, return
222 * DM_OK
223 * @tc.type: FUNC
224 * @tc.require: I5N1K3
225 */
226 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusPublishCallback_001, testing::ext::TestSize.Level0)
227 {
228 std::string pkgName = "com.ohos.helloworld";
229 int ret = softbusConnector->UnRegisterSoftbusPublishCallback(pkgName);
230 int ret1 = SoftbusConnector::publishCallbackMap_.count(pkgName);
231 EXPECT_EQ(ret1, 0);
232 EXPECT_EQ(ret, DM_OK);
233 }
234
235 /**
236 * @tc.name: RegisterSoftbusStateCallback_001
237 * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusStateCallback function to corrort, return DM_OK
238 * @tc.type: FUNC
239 * @tc.require: AR000GHSJK
240 */
241 HWTEST_F(SoftbusConnectorTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
242 {
243 std::string pkgName = "com.ohos.helloworld";
244 int ret = softbusConnector->RegisterSoftbusStateCallback(
245 pkgName, std::shared_ptr<ISoftbusStateCallback>(deviceStateMgr));
246 int ret1 = SoftbusConnector::discoveryCallbackMap_.count(pkgName);
247 EXPECT_EQ(ret1, DM_OK);
248 EXPECT_EQ(ret, 0);
249 softbusConnector->UnRegisterSoftbusStateCallback(pkgName);
250 }
251
252 /**
253 * @tc.name: UnRegisterSoftbusStateCallback_001
254 * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusStateCallback function to corrort, return DM_OK
255 * @tc.type: FUNC
256 * @tc.require: AR000GHSJK
257 */
258 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
259 {
260 std::string pkgName = "com.ohos.helloworld";
261 int ret = softbusConnector->UnRegisterSoftbusStateCallback(pkgName);
262 int ret1 = SoftbusConnector::stateCallbackMap_.count(pkgName);
263 EXPECT_EQ(ret1, 0);
264 EXPECT_EQ(ret, DM_OK);
265 }
266
267 /**
268 * @tc.name: StartDiscovery_001
269 * @tc.desc: get StartDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
270 * @tc.type: FUNC
271 * @tc.require: AR000GHSJK
272 */
273 HWTEST_F(SoftbusConnectorTest, StartDiscovery_001, testing::ext::TestSize.Level0)
274 {
275 DmSubscribeInfo dmSubscribeInfo;
276 int ret = softbusConnector->StartDiscovery(dmSubscribeInfo);
277 EXPECT_EQ(ret, ERR_DM_DISCOVERY_FAILED);
278 }
279
280 /**
281 * @tc.name: StopDiscovery_001
282 * @tc.desc: get StartDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
283 * @tc.type: FUNC
284 * @tc.require: AR000GHSJK
285 */
286 HWTEST_F(SoftbusConnectorTest, StopDiscovery_001, testing::ext::TestSize.Level0)
287 {
288 uint16_t subscribeId = static_cast<uint16_t>(123456);
289 int ret = softbusConnector->StopDiscovery(subscribeId);
290 EXPECT_NE(ret, 0);
291 }
292
293 /**
294 * @tc.name: PublishDiscovery_001
295 * @tc.desc: get PublishDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
296 * @tc.type: FUNC
297 * @tc.require: I5N1K3
298 */
299 HWTEST_F(SoftbusConnectorTest, PublishDiscovery_001, testing::ext::TestSize.Level0)
300 {
301 DmPublishInfo dmPublishInfo;
302 int ret = softbusConnector->PublishDiscovery(dmPublishInfo);
303 EXPECT_EQ(ret, ERR_DM_PUBLISH_FAILED);
304 }
305
306 /**
307 * @tc.name: UnPublishDiscovery_001
308 * @tc.desc: get UnPublishDiscovery to wrong branch and return ERR_DM_PUBLISH_FAILED
309 * @tc.type: FUNC
310 * @tc.require: I5N1K3
311 */
312 HWTEST_F(SoftbusConnectorTest, UnPublishDiscovery_001, testing::ext::TestSize.Level0)
313 {
314 int32_t publishId = 123456;
315 int ret = softbusConnector->UnPublishDiscovery(publishId);
316 EXPECT_NE(ret, 0);
317 }
318
319 /**
320 * @tc.name: GetUdidByNetworkId_001
321 * @tc.desc: get StartDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
322 * @tc.type: FUNC
323 * @tc.require: AR000GHSJK
324 */
325 HWTEST_F(SoftbusConnectorTest, GetUdidByNetworkId_001, testing::ext::TestSize.Level0)
326 {
327 const char *networkId = "123456";
328 std::string udid;
329 int ret = softbusConnector->GetUdidByNetworkId(networkId, udid);
330 EXPECT_NE(ret, 0);
331 }
332
333 /**
334 * @tc.name: GetUuidByNetworkId_001
335 * @tc.desc: get StartDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
336 * @tc.type: FUNC
337 * @tc.require: AR000GHSJK
338 */
339 HWTEST_F(SoftbusConnectorTest, GetUuidByNetworkId_001, testing::ext::TestSize.Level0)
340 {
341 const char *networkId = "123456";
342 std::string udid;
343 int ret = softbusConnector->GetUdidByNetworkId(networkId, udid);
344 EXPECT_NE(ret, 0);
345 }
346
347 /**
348 * @tc.name: IsDeviceOnLine_001
349 * @tc.desc: set deviceId = "12333", and return false
350 * @tc.type: FUNC
351 * @tc.require: AR000GHSJK
352 */
353 HWTEST_F(SoftbusConnectorTest, IsDeviceOnLine_001, testing::ext::TestSize.Level0)
354 {
355 std::string deviceId = "12333";
356 bool ret = softbusConnector->IsDeviceOnLine(deviceId);
357 EXPECT_EQ(ret, false);
358 }
359
360 /**
361 * @tc.name: IsDeviceOnLine_002
362 * @tc.desc:
363 * @tc.type: FUNC
364 * @tc.require: AR000GHSJK
365 */
366 HWTEST_F(SoftbusConnectorTest, IsDeviceOnLine_002, testing::ext::TestSize.Level0)
367 {
368 std::string deviceId = "145677";
369 NodeBasicInfo info;
370 strcpy_s(info.networkId, DM_MAX_DEVICE_ID_LEN, "145677");
371 bool ret = softbusConnector->IsDeviceOnLine(deviceId);
372 EXPECT_EQ(ret, false);
373 }
374
375 /**
376 * @tc.name: GetSoftbusSession_001
377 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
378 * @tc.type: FUNC
379 * @tc.require: AR000GHSJK
380 */
381 HWTEST_F(SoftbusConnectorTest, GetSoftbusSession_001, testing::ext::TestSize.Level0)
382 {
383 std::shared_ptr<SoftbusSession> softSession = softbusConnector->GetSoftbusSession();
384 EXPECT_NE(softSession, nullptr);
385 }
386
387 /**
388 * @tc.name: GetSoftbusSession_001
389 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
390 * @tc.type: FUNC
391 * @tc.require: AR000GHSJK
392 */
393 HWTEST_F(SoftbusConnectorTest, HaveDeviceInMap_001, testing::ext::TestSize.Level0)
394 {
395 std::string deviceId = "12345678";
396 SoftbusConnector::discoveryDeviceInfoMap_[deviceId];
397 bool ret = softbusConnector->HaveDeviceInMap(deviceId);
398 EXPECT_EQ(ret, true);
399 SoftbusConnector::discoveryDeviceInfoMap_.clear();
400 }
401
402 /**
403 * @tc.name: GetSoftbusSession_001
404 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
405 * @tc.type: FUNC
406 * @tc.require: AR000GHSJK
407 */
408 HWTEST_F(SoftbusConnectorTest, HaveDeviceInMap_002, testing::ext::TestSize.Level0)
409 {
410 std::string deviceId = "12345678";
411 bool ret = softbusConnector->HaveDeviceInMap(deviceId);
412 EXPECT_EQ(ret, false);
413 SoftbusConnector::discoveryDeviceInfoMap_.clear();
414 }
415
416 /**
417 * @tc.name: GetConnectionIpAddress_001
418 * @tc.desc: set deviceId null, and return ERR_DM_FAILED
419 * @tc.type: FUNC
420 * @tc.require: AR000GHSJK
421 */
422 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_001, testing::ext::TestSize.Level0)
423 {
424 std::string deviceId;
425 std::string ipAddress = "123456";
426 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
427 EXPECT_EQ(ret, ERR_DM_FAILED);
428 }
429
430 /**
431 * @tc.name: GetConnectionIpAddress_002
432 * @tc.desc: set some corrort para, and return ERR_DM_FAILED
433 * @tc.type: FUNC
434 * @tc.require: AR000GHSJK
435 */
436 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_002, testing::ext::TestSize.Level0)
437 {
438 std::string deviceId = "213456";
439 std::string ipAddress = "123456";
440 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
441 EXPECT_EQ(ret, ERR_DM_FAILED);
442 }
443
444 /**
445 * @tc.name: GetConnectionIpAddress_003
446 * @tc.desc: set deviceInfo.addrNum = -1;go to second branch and return ERR_DM_FAILED
447 * @tc.type: FUNC
448 * @tc.require: AR000GHSJK
449 */
450 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_003, testing::ext::TestSize.Level0)
451 {
452 DeviceInfo deviceInfo;
453 deviceInfo.addrNum = -1;
454 std::string ipAddress;
455 std::string deviceId = "3338848";
456 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
457 EXPECT_EQ(ret, ERR_DM_FAILED);
458 }
459
460 /**
461 * @tc.name: GetConnectionIpAddress_004
462 * @tc.desc: set deviceInfo.addrNum = 1;go to second branch and return ERR_DM_FAILED
463 * @tc.type: FUNC
464 * @tc.require: AR000GHSJK
465 */
466 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_004, testing::ext::TestSize.Level0)
467 {
468 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
469 constexpr char ETH_IP[] = "0.0.0.0";
470 deviceInfo->addrNum = 1;
471 deviceInfo->addr[0].type = CONNECTION_ADDR_WLAN;
472 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, ETH_IP, strlen(ETH_IP));
473 std::string ipAddress;
474 std::string deviceId = "3338848";
475 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
476 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
477 EXPECT_EQ(ret, DM_OK);
478 SoftbusConnector::discoveryDeviceInfoMap_.clear();
479 }
480
481 /**
482 * @tc.name: GetConnectAddrByType_001
483 * @tc.desc: set deviceInfo'pointer null, go to first branch, and return nullptr
484 * @tc.require: AR000GHSJK
485 */
486 HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_001, testing::ext::TestSize.Level0)
487 {
488 ConnectionAddrType type;
489 type = CONNECTION_ADDR_MAX;
490 ConnectionAddr *p = nullptr;
491 ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(nullptr, type);
492 EXPECT_EQ(p, ret);
493 }
494
495 /**
496 * @tc.name: GetConnectAddrByType_002
497 * @tc.desc:set deviceInfo to some corrort para, and return nullptr
498 * @tc.type: FUNC
499 * @tc.require: AR000GHSJK
500 */
501 HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_002, testing::ext::TestSize.Level0)
502 {
503 DeviceInfo deviceInfo;
504 deviceInfo.addrNum = 1;
505 ConnectionAddrType type;
506 type = CONNECTION_ADDR_BR;
507 ConnectionAddr *p = nullptr;
508 ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(&deviceInfo, type);
509 EXPECT_EQ(ret, p);
510 }
511
512 /**
513 * @tc.name: GetConnectAddr_001
514 * @tc.desc: set deviceId to null, and return nullptr
515 * @tc.type: FUNC
516 * @tc.require: AR000GHSJK
517 */
518 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_001, testing::ext::TestSize.Level0)
519 {
520 std::string deviceId;
521 std::string connectAddr;
522 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
523 EXPECT_EQ(ret, nullptr);
524 }
525
526 /**
527 * @tc.name: GetConnectAddr_002
528 * @tc.desc:set deviceId nit null set deviceInfo.addrNum = -1; and return nullptr
529 * @tc.type: FUNC
530 * @tc.require: AR000GHSJK
531 */
532 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_002, testing::ext::TestSize.Level0)
533 {
534 std::string deviceId = "123345";
535 std::string connectAddr;
536 DeviceInfo deviceInfo;
537 deviceInfo.addrNum = -1;
538 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
539 EXPECT_EQ(ret, nullptr);
540 }
541
542 /**
543 * @tc.name: GetConnectAddr_003
544 * @tc.desc:set deviceInfo.addrNum = 1
545 * @tc.type: FUNC
546 * @tc.require: AR000GHSJK
547 */
548 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_003, testing::ext::TestSize.Level0)
549 {
550 std::string deviceId = "123345";
551 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
552 std::string connectAddr;
553 constexpr char ETH_IP[] = "0.0.0.0";
554 deviceInfo->addrNum = 1;
555 deviceInfo->addr[0].type = CONNECTION_ADDR_ETH;
556 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, ETH_IP, strlen(ETH_IP));
557 deviceInfo->addr[0].info.ip.port = 0;
558 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
559 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
560 EXPECT_NE(ret, nullptr);
561 SoftbusConnector::discoveryDeviceInfoMap_.clear();
562 }
563
564 /**
565 * @tc.name: GetConnectAddr_004
566 * @tc.desc:set deviceInfo.addrNum = 1
567 * @tc.type: FUNC
568 * @tc.require: AR000GHSJK
569 */
570 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_004, testing::ext::TestSize.Level0)
571 {
572 std::string deviceId = "123345";
573 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
574 std::string connectAddr;
575 constexpr char WLAN_IP[] = "1.1.1.1";
576 deviceInfo->addrNum = 1;
577 deviceInfo->addr[0].type = CONNECTION_ADDR_WLAN;
578 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, WLAN_IP, strlen(WLAN_IP));
579 deviceInfo->addr[0].info.ip.port = 0;
580 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
581 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
582 EXPECT_NE(ret, nullptr);
583 SoftbusConnector::discoveryDeviceInfoMap_.clear();
584 }
585
586 /**
587 * @tc.name: GetConnectAddr_005
588 * @tc.desc:get brMac addr
589 * @tc.type: FUNC
590 * @tc.require: AR000GHSJK
591 */
592 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_005, testing::ext::TestSize.Level0)
593 {
594 std::string deviceId = "123345";
595 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
596 std::string connectAddr;
597 deviceInfo->addrNum = 1;
598 constexpr char BR_MAC[] = "2:2:2:2";
599 deviceInfo->addr[0].type = CONNECTION_ADDR_BR;
600 (void)strncpy_s(deviceInfo->addr[0].info.br.brMac, IP_STR_MAX_LEN, BR_MAC, strlen(BR_MAC));
601 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
602 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
603 EXPECT_NE(ret, nullptr);
604 SoftbusConnector::discoveryDeviceInfoMap_.clear();
605 }
606
607 /**
608 * @tc.name: GetConnectAddr_006
609 * @tc.desc:get bleMac addr
610 * @tc.type: FUNC
611 * @tc.require: AR000GHSJK
612 */
613 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_006, testing::ext::TestSize.Level0)
614 {
615 std::string deviceId = "123345";
616 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
617 std::string connectAddr;
618 constexpr char BLE_MAC[] = "3:3:3:3";
619 deviceInfo->addrNum = 1;
620 deviceInfo->addr[0].type = CONNECTION_ADDR_BLE;
621 (void)strncpy_s(deviceInfo->addr[0].info.ble.bleMac, IP_STR_MAX_LEN, BLE_MAC, strlen(BLE_MAC));
622 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
623 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
624 EXPECT_NE(ret, nullptr);
625 SoftbusConnector::discoveryDeviceInfoMap_.clear();
626 }
627
628 /**
629 * @tc.name: ConvertNodeBasicInfoToDmDevice_001
630 * @tc.desc: go to the correct case and return DM_OK
631 * @tc.type: FUNC
632 * @tc.require: AR000GHSJK
633 */
634 HWTEST_F(SoftbusConnectorTest, ConvertNodeBasicInfoToDmDevice_001, testing::ext::TestSize.Level0)
635 {
636 NodeBasicInfo nodeBasicInfo;
637 DmDeviceInfo dmDeviceInfo;
638 int ret = softbusListener->ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo);
639 EXPECT_EQ(ret, DM_OK);
640 }
641
642 /**
643 * @tc.name: ConvertNodeBasicInfoToDmDevice_001
644 * @tc.desc: go to the correct case and return DM_OK
645 * @tc.type: FUNC
646 * @tc.require: AR000GHSJK
647 */
648 HWTEST_F(SoftbusConnectorTest, ConvertDeviceInfoToDmDevice_001, testing::ext::TestSize.Level0)
649 {
650 DeviceInfo deviceInfo = {
651 .devId = "123456",
652 .devType = (DeviceType)1,
653 .devName = "11111"
654 };
655 DmDeviceInfo dm;
656 DmDeviceInfo dm_1 = {
657 .deviceId = "123456",
658 .deviceName = "11111",
659 .deviceTypeId = 1
660 };
661 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dm);
662 bool ret = false;
663 if (strcmp(dm.deviceId, dm_1.deviceId) == 0) {
664 ret = true;
665 }
666 EXPECT_EQ(ret, true);
667 }
668
669 /**
670 * @tc.name: OnSoftBusDeviceOnline_001
671 * @tc.desc: go to the correct case and return DM_OK
672 * @tc.type: FUNC
673 * @tc.require: AR000GHSJK
674 */
675 HWTEST_F(SoftbusConnectorTest, OnSoftBusDeviceOnline_001, testing::ext::TestSize.Level0)
676 {
677 std::string deviceId = "123456";
678 SoftbusConnector::discoveryDeviceInfoMap_[deviceId];
679 NodeBasicInfo *info = nullptr;
680 softbusListener->OnSoftBusDeviceOnline(info);
681 bool ret = false;
682 if (listener->ipcServerListener_.req_ != nullptr) {
683 listener->ipcServerListener_.req_ = nullptr;
684 ret = true;
685 }
686 EXPECT_EQ(ret, false);
687 }
688
689 /**
690 * @tc.name: OnSoftBusDeviceOnline_001
691 * @tc.desc: go to the correct case and return DM_OK
692 * @tc.type: FUNC
693 * @tc.require: AR000GHSJK
694 */
695 HWTEST_F(SoftbusConnectorTest, OnSoftBusDeviceOnline_002, testing::ext::TestSize.Level0)
696 {
697 std::string deviceId = "123456";
698 NodeBasicInfo info = {
699 .networkId = "123456",
700 .deviceName = "123456",
701 .deviceTypeId = 1
702 };
703 std::string pkgName = "com.ohos.helloworld";
704 softbusConnector->RegisterSoftbusStateCallback(
705 pkgName, std::shared_ptr<ISoftbusStateCallback>(deviceStateMgr));
706 softbusListener->OnSoftBusDeviceOnline(&info);
707 sleep(1);
708 bool ret = false;
709 if (listener->ipcServerListener_.req_ != nullptr) {
710 listener->ipcServerListener_.req_ = nullptr;
711 ret = true;
712 }
713 EXPECT_EQ(ret, true);
714 softbusConnector->UnRegisterSoftbusStateCallback(pkgName);
715 }
716
717 /**
718 * @tc.name: OnSoftBusDeviceOnline_001
719 * @tc.desc: go to the correct case and return DM_OK
720 * @tc.type: FUNC
721 * @tc.require: AR000GHSJK
722 */
723 HWTEST_F(SoftbusConnectorTest, OnSoftBusDeviceOnline_003, testing::ext::TestSize.Level0)
724 {
725 std::string deviceId = "123456";
726 NodeBasicInfo info = {
727 .networkId = "123456",
728 .deviceName = "123456",
729 .deviceTypeId = 1
730 };
731 std::string oldName = std::string(DM_PKG_NAME);
732 std::string pkgName = "com.ohos.test";
733 softbusListener->OnSoftBusDeviceOnline(&info);
734 bool ret = false;
735 if (listener->ipcServerListener_.req_ != nullptr) {
736 listener->ipcServerListener_.req_ = nullptr;
737 ret = true;
738 }
739 EXPECT_EQ(ret, false);
740 pkgName = oldName;
741 }
742
743 /**
744 * @tc.name: OnSoftbusDeviceOffline_001
745 * @tc.desc: go to the correct case and return DM_OK
746 * @tc.type: FUNC
747 * @tc.require: AR000GHSJK
748 */
749 HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceOffline_001, testing::ext::TestSize.Level0)
750 {
751 NodeBasicInfo *info = nullptr;
752 softbusListener->OnSoftbusDeviceOffline(info);
753 bool ret = false;
754 if (listener->ipcServerListener_.req_ != nullptr) {
755 ret = true;
756 }
757 EXPECT_EQ(ret, false);
758 }
759
760 /**
761 * @tc.name: OnSoftbusDeviceOffline_002
762 * @tc.desc: go to the correct case and return DM_OK
763 * @tc.type: FUNC
764 * @tc.require: AR000GHSJK
765 */
766 HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceOffline_002, testing::ext::TestSize.Level0)
767 {
768 NodeBasicInfo info = {
769 .networkId = "123456",
770 .deviceName = "123456",
771 .deviceTypeId = 1
772 };
773 std::string pkgName = "com.ohos.helloworld";
774 softbusConnector->RegisterSoftbusStateCallback(
775 pkgName, std::shared_ptr<ISoftbusStateCallback>(deviceStateMgr));
776 softbusListener->OnSoftbusDeviceOffline(&info);
777 sleep(1);
778 bool ret = false;
779 if (listener->ipcServerListener_.req_ != nullptr) {
780 listener->ipcServerListener_.req_ = nullptr;
781 ret = true;
782 }
783 EXPECT_EQ(ret, true);
784 softbusConnector->UnRegisterSoftbusStateCallback(pkgName);
785 }
786
787 /**
788 * @tc.name: OnSoftbusDeviceFound_001
789 * @tc.desc: go to the correct case and return DM_OK
790 * @tc.type: FUNC
791 * @tc.require: AR000GHSJK
792 */
793 HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceFound_001, testing::ext::TestSize.Level0)
794 {
795 DeviceInfo *device = nullptr;
796 softbusConnector->OnSoftbusDeviceFound(device);
797 bool ret = false;
798 if (listener->ipcServerListener_.req_ != nullptr) {
799 listener->ipcServerListener_.req_ = nullptr;
800 ret = true;
801 }
802 EXPECT_EQ(ret, false);
803 }
804
805 /**
806 * @tc.name: OnSoftbusDeviceFound_002
807 * @tc.desc: go to the correct case and return DM_OK
808 * @tc.type: FUNC
809 * @tc.require: AR000GHSJK
810 */
811 HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceFound_002, testing::ext::TestSize.Level0)
812 {
813 DeviceInfo device = {
814 .devId = "123456",
815 .devType = (DeviceType)1,
816 .devName = "11111"
817 };
818 softbusConnector->OnSoftbusDeviceFound(&device);
819 bool ret = false;
820 if (listener->ipcServerListener_.req_ != nullptr) {
821 listener->ipcServerListener_.req_ = nullptr;
822 ret = true;
823 }
824 EXPECT_EQ(ret, false);
825 }
826
827 /**
828 * @tc.name: OnSoftbusDiscoveryResult_001
829 * @tc.desc: go to the correct case and return DM_OK
830 * @tc.type: FUNC
831 * @tc.require: AR000GHSJK
832 */
833 HWTEST_F(SoftbusConnectorTest, OnSoftbusDiscoveryResult_001, testing::ext::TestSize.Level0)
834 {
835 int32_t subscribeId= 123456;
836 RefreshResult result = (RefreshResult)1;
837 std::string pkgName = "com.ohos.helloworld";
838 softbusConnector->RegisterSoftbusDiscoveryCallback(
839 pkgName, std::shared_ptr<ISoftbusDiscoveryCallback>(discoveryMgr));
840 softbusConnector->OnSoftbusDiscoveryResult(subscribeId, result);
841 bool ret = false;
842 if (listener->ipcServerListener_.req_ != nullptr) {
843 listener->ipcServerListener_.req_ = nullptr;
844 ret = true;
845 }
846 EXPECT_EQ(ret, true);
847 softbusConnector->UnRegisterSoftbusDiscoveryCallback(pkgName);
848 }
849
850 /**
851 * @tc.name: OnSoftbusDiscoveryResult_001
852 * @tc.desc: go to the correct case and return DM_OK
853 * @tc.type: FUNC
854 * @tc.require: AR000GHSJK
855 */
856 HWTEST_F(SoftbusConnectorTest, OnSoftbusDiscoveryResult_002, testing::ext::TestSize.Level0)
857 {
858 int32_t subscribeId= 123456;
859 RefreshResult result = (RefreshResult)0;
860 std::string pkgName = "com.ohos.helloworld";
861 softbusConnector->RegisterSoftbusDiscoveryCallback(
862 pkgName, std::shared_ptr<ISoftbusDiscoveryCallback>(discoveryMgr));
863 softbusConnector->OnSoftbusDiscoveryResult(subscribeId, result);
864 bool ret = false;
865 if (listener->ipcServerListener_.req_ != nullptr) {
866 listener->ipcServerListener_.req_ = nullptr;
867 ret = true;
868 }
869 EXPECT_EQ(ret, true);
870 softbusConnector->UnRegisterSoftbusDiscoveryCallback(pkgName);
871 }
872
873 /**
874 * @tc.name: OnSoftbusPublishResult_001
875 * @tc.desc: go to the correct case and return DM_OK
876 * @tc.type: FUNC
877 * @tc.require: I5N1K3
878 */
879 HWTEST_F(SoftbusConnectorTest, OnSoftbusPublishResult_001, testing::ext::TestSize.Level0)
880 {
881 int32_t publishId = 123456;
882 PublishResult failReason = (PublishResult)1;
883 std::string pkgName = "com.ohos.helloworld";
884 softbusConnector->RegisterSoftbusPublishCallback(
885 pkgName, std::shared_ptr<ISoftbusPublishCallback>(publishMgr));
886 softbusConnector->OnSoftbusPublishResult(publishId, failReason);
887 bool ret = false;
888 if (listener->ipcServerListener_.req_ != nullptr) {
889 listener->ipcServerListener_.req_ = nullptr;
890 ret = true;
891 }
892 EXPECT_EQ(ret, true);
893 softbusConnector->UnRegisterSoftbusPublishCallback(pkgName);
894 }
895
896 /**
897 * @tc.name: OnSoftbusPublishResult_004
898 * @tc.desc: go to the correct case and return DM_OK
899 * @tc.type: FUNC
900 * @tc.require: I5N1K3
901 */
902 HWTEST_F(SoftbusConnectorTest, OnSoftbusPublishResult_002, testing::ext::TestSize.Level0)
903 {
904 int32_t publishId= 123456;
905 std::string pkgName = "com.ohos.helloworld";
906 PublishResult failReason = (PublishResult)0;
907 softbusConnector->RegisterSoftbusPublishCallback(
908 pkgName, std::shared_ptr<ISoftbusPublishCallback>(publishMgr));
909 softbusConnector->OnSoftbusPublishResult(publishId, failReason);
910 bool ret = false;
911 if (listener->ipcServerListener_.req_ != nullptr) {
912 listener->ipcServerListener_.req_ = nullptr;
913 ret = true;
914 }
915 EXPECT_EQ(ret, true);
916 softbusConnector->UnRegisterSoftbusPublishCallback(pkgName);
917 }
918 } // namespace
919 } // namespace DistributedHardware
920 } // namespace OHOS-