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