1 /*
2 * Copyright (c) 2022-2025 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 using namespace testing;
37 using namespace testing::ext;
38 namespace OHOS {
39 namespace DistributedHardware {
40
41 class SoftbusStateCallbackTest : public ISoftbusStateCallback {
42 public:
SoftbusStateCallbackTest()43 SoftbusStateCallbackTest() {}
~SoftbusStateCallbackTest()44 virtual ~SoftbusStateCallbackTest() {}
OnDeviceOnline(std::string deviceId,int32_t authForm)45 void OnDeviceOnline(std::string deviceId, int32_t authForm) {}
OnDeviceOffline(std::string deviceId)46 void OnDeviceOffline(std::string deviceId) {}
DeleteOffLineTimer(std::string udidHash)47 void DeleteOffLineTimer(std::string udidHash) override {}
48 };
49
SetUp()50 void SoftbusConnectorTest::SetUp()
51 {
52 }
TearDown()53 void SoftbusConnectorTest::TearDown()
54 {
55 }
SetUpTestCase()56 void SoftbusConnectorTest::SetUpTestCase()
57 {
58 SoftbusCenterInterface::softbusCenterInterface_ = softbusCenterMock_;
59 DmCrypto::dmCrypto = cryptoMock_;
60 }
TearDownTestCase()61 void SoftbusConnectorTest::TearDownTestCase()
62 {
63 SoftbusCenterInterface::softbusCenterInterface_ = nullptr;
64 softbusCenterMock_ = nullptr;
65 DmCrypto::dmCrypto = nullptr;
66 cryptoMock_ = nullptr;
67 }
68
69 namespace {
70 std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
71
CheckSoftbusRes(int32_t ret)72 bool CheckSoftbusRes(int32_t ret)
73 {
74 return ret == SOFTBUS_INVALID_PARAM || ret == SOFTBUS_NETWORK_NOT_INIT || ret == SOFTBUS_NETWORK_LOOPER_ERR ||
75 ret == SOFTBUS_IPC_ERR;
76 }
77
78 /**
79 * @tc.name: SoftbusConnector_001
80 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr
81 * @tc.type: FUNC
82 * @tc.require: AR000GHSJK
83 */
84 HWTEST_F(SoftbusConnectorTest, SoftbusConnector_001, testing::ext::TestSize.Level1)
85 {
86 std::shared_ptr<SoftbusConnector> m_SoftbusConnector = std::make_shared<SoftbusConnector>();
87 ASSERT_NE(m_SoftbusConnector, nullptr);
88 }
89
90 /**
91 * @tc.name: SoftbusConnector_002
92 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
93 * @tc.type: FUNC
94 * @tc.require: AR000GHSJK
95 */
96 HWTEST_F(SoftbusConnectorTest, SoftbusConnector_002, testing::ext::TestSize.Level1)
97 {
98 std::shared_ptr<SoftbusConnector> m_SoftbusConnector = std::make_shared<SoftbusConnector>();
99 m_SoftbusConnector.reset();
100 EXPECT_EQ(m_SoftbusConnector, nullptr);
101 }
102
103 /**
104 * @tc.name: GetUdidByNetworkId_001
105 * @tc.desc: get StartDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
106 * @tc.type: FUNC
107 * @tc.require: AR000GHSJK
108 */
109 HWTEST_F(SoftbusConnectorTest, GetUdidByNetworkId_001, testing::ext::TestSize.Level1)
110 {
111 const char *networkId = "123456";
112 std::string udid;
113 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
114 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(ERR_DM_FAILED));
115 int ret = softbusConnector->GetUdidByNetworkId(networkId, udid);
116 EXPECT_EQ(ret, ERR_DM_FAILED);
117 }
118
119 /**
120 * @tc.name: GetUuidByNetworkId_001
121 * @tc.desc: get StartDiscovery to wrong branch and return ERR_DM_DISCOVERY_FAILED
122 * @tc.type: FUNC
123 * @tc.require: AR000GHSJK
124 */
125 HWTEST_F(SoftbusConnectorTest, GetUuidByNetworkId_001, testing::ext::TestSize.Level1)
126 {
127 const char *networkId = "123456";
128 std::string uuid;
129 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
130 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(ERR_DM_FAILED));
131 int ret = softbusConnector->GetUuidByNetworkId(networkId, uuid);
132 EXPECT_EQ(ret, ERR_DM_FAILED);
133 }
134
135 /**
136 * @tc.name: GetSoftbusSession_001
137 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
138 * @tc.type: FUNC
139 * @tc.require: AR000GHSJK
140 */
141 HWTEST_F(SoftbusConnectorTest, GetSoftbusSession_001, testing::ext::TestSize.Level1)
142 {
143 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
144 std::shared_ptr<SoftbusSession> softSession = softbusConnector->GetSoftbusSession();
145 EXPECT_NE(softSession, nullptr);
146 }
147
148 /**
149 * @tc.name: GetSoftbusSession_001
150 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
151 * @tc.type: FUNC
152 * @tc.require: AR000GHSJK
153 */
154 HWTEST_F(SoftbusConnectorTest, HaveDeviceInMap_001, testing::ext::TestSize.Level1)
155 {
156 std::string deviceId = "12345678";
157 SoftbusConnector::discoveryDeviceInfoMap_[deviceId];
158 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
159 bool ret = softbusConnector->HaveDeviceInMap(deviceId);
160 EXPECT_EQ(ret, true);
161 SoftbusConnector::discoveryDeviceInfoMap_.clear();
162 }
163
164 /**
165 * @tc.name: GetSoftbusSession_001
166 * @tc.desc: set SoftbusConnector to new a pointer, and the pointer nou equal nullptr, and delete it
167 * @tc.type: FUNC
168 * @tc.require: AR000GHSJK
169 */
170 HWTEST_F(SoftbusConnectorTest, HaveDeviceInMap_002, testing::ext::TestSize.Level1)
171 {
172 std::string deviceId = "12345678";
173 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
174 bool ret = softbusConnector->HaveDeviceInMap(deviceId);
175 EXPECT_EQ(ret, false);
176 SoftbusConnector::discoveryDeviceInfoMap_.clear();
177 }
178
179 /**
180 * @tc.name: GetConnectAddrByType_001
181 * @tc.desc: set deviceInfo'pointer null, go to first branch, and return nullptr
182 * @tc.require: AR000GHSJK
183 */
184 HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_001, testing::ext::TestSize.Level1)
185 {
186 ConnectionAddrType type;
187 type = CONNECTION_ADDR_MAX;
188 ConnectionAddr *p = nullptr;
189 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
190 ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(nullptr, type);
191 EXPECT_EQ(p, ret);
192 }
193
194 /**
195 * @tc.name: GetConnectAddrByType_002
196 * @tc.desc:set deviceInfo to some corrort para, and return nullptr
197 * @tc.type: FUNC
198 * @tc.require: AR000GHSJK
199 */
200 HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_002, testing::ext::TestSize.Level1)
201 {
202 DeviceInfo deviceInfo;
203 deviceInfo.addrNum = 1;
204 ConnectionAddrType type;
205 type = CONNECTION_ADDR_BR;
206 ConnectionAddr *p = nullptr;
207 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
208 ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(&deviceInfo, type);
209 EXPECT_EQ(ret, p);
210 }
211
212 /**
213 * @tc.name: GetConnectAddr_001
214 * @tc.desc: set deviceId to null, and return nullptr
215 * @tc.type: FUNC
216 * @tc.require: AR000GHSJK
217 */
218 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_001, testing::ext::TestSize.Level1)
219 {
220 std::string deviceId;
221 std::string connectAddr;
222 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
223 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
224 EXPECT_EQ(ret, nullptr);
225 }
226
227 /**
228 * @tc.name: GetConnectAddr_002
229 * @tc.desc:set deviceId nit null set deviceInfo.addrNum = -1; and return nullptr
230 * @tc.type: FUNC
231 * @tc.require: AR000GHSJK
232 */
233 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_002, testing::ext::TestSize.Level1)
234 {
235 std::string deviceId = "123345";
236 std::string connectAddr;
237 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
238 deviceInfo->addrNum = -1;
239 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
240 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
241 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
242 EXPECT_EQ(ret, nullptr);
243 SoftbusConnector::discoveryDeviceInfoMap_.clear();
244 }
245
246 /**
247 * @tc.name: GetConnectAddr_003
248 * @tc.desc:set deviceInfo.addrNum = 1
249 * @tc.type: FUNC
250 * @tc.require: AR000GHSJK
251 */
252 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_003, testing::ext::TestSize.Level1)
253 {
254 std::string deviceId = "123345";
255 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
256 std::string connectAddr;
257 constexpr char ethIp[] = "0.0.0.0";
258 deviceInfo->addrNum = 1;
259 deviceInfo->addr[0].type = CONNECTION_ADDR_ETH;
260 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, ethIp, strlen(ethIp));
261 deviceInfo->addr[0].info.ip.port = 0;
262 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
263 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
264 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
265 EXPECT_NE(ret, nullptr);
266 SoftbusConnector::discoveryDeviceInfoMap_.clear();
267 }
268
269 /**
270 * @tc.name: GetConnectAddr_004
271 * @tc.desc:set deviceInfo.addrNum = 1
272 * @tc.type: FUNC
273 * @tc.require: AR000GHSJK
274 */
275 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_004, testing::ext::TestSize.Level1)
276 {
277 std::string deviceId = "123345";
278 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
279 std::string connectAddr;
280 constexpr char wlanIp[] = "1.1.1.1";
281 deviceInfo->addrNum = 1;
282 deviceInfo->addr[0].type = CONNECTION_ADDR_WLAN;
283 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, wlanIp, strlen(wlanIp));
284 deviceInfo->addr[0].info.ip.port = 0;
285 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
286 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
287 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
288 EXPECT_NE(ret, nullptr);
289 SoftbusConnector::discoveryDeviceInfoMap_.clear();
290 }
291
292 /**
293 * @tc.name: GetConnectAddr_005
294 * @tc.desc:get brMac addr
295 * @tc.type: FUNC
296 * @tc.require: AR000GHSJK
297 */
298 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_005, testing::ext::TestSize.Level1)
299 {
300 std::string deviceId = "123345";
301 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
302 std::string connectAddr;
303 deviceInfo->addrNum = 1;
304 constexpr char brMac[] = "2:2:2:2";
305 deviceInfo->addr[0].type = CONNECTION_ADDR_BR;
306 (void)strncpy_s(deviceInfo->addr[0].info.br.brMac, IP_STR_MAX_LEN, brMac, strlen(brMac));
307 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
308 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
309 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
310 EXPECT_NE(ret, nullptr);
311 SoftbusConnector::discoveryDeviceInfoMap_.clear();
312 }
313
314 /**
315 * @tc.name: GetConnectAddr_006
316 * @tc.desc:get bleMac addr
317 * @tc.type: FUNC
318 * @tc.require: AR000GHSJK
319 */
320 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_006, testing::ext::TestSize.Level1)
321 {
322 std::string deviceId = "123345";
323 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
324 std::string connectAddr;
325 constexpr char bleMac[] = "3:3:3:3";
326 deviceInfo->addrNum = 1;
327 deviceInfo->addr[0].type = CONNECTION_ADDR_BLE;
328 (void)strncpy_s(deviceInfo->addr[0].info.ble.bleMac, IP_STR_MAX_LEN, bleMac, strlen(bleMac));
329 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
330 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
331 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
332 EXPECT_NE(ret, nullptr);
333 SoftbusConnector::discoveryDeviceInfoMap_.clear();
334 }
335
336 /**
337 * @tc.name: GetConnectAddr_007
338 * @tc.desc:set deviceInfo.addrNum = 1
339 * @tc.type: FUNC
340 * @tc.require: AR000GHSJK
341 */
342 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_007, testing::ext::TestSize.Level1)
343 {
344 std::string deviceId = "123345";
345 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
346 std::string connectAddr;
347 constexpr char ncmIp[] = "4.4.4.4";
348 deviceInfo->addrNum = 1;
349 deviceInfo->addr[0].type = CONNECTION_ADDR_NCM;
350 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, ncmIp, strlen(ncmIp));
351 deviceInfo->addr[0].info.ip.port = 0;
352 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
353 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
354 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
355 EXPECT_NE(ret, nullptr);
356 SoftbusConnector::discoveryDeviceInfoMap_.clear();
357 }
358
359 /**
360 * @tc.name: GetConnectAddr_008
361 * @tc.desc:set deviceInfo.addrNum = 1
362 * @tc.type: FUNC
363 * @tc.require: AR000GHSJK
364 */
365 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_008, testing::ext::TestSize.Level1)
366 {
367 std::string deviceId = "123345";
368 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
369 std::string connectAddr;
370 constexpr char addrIp[] = "5.5.5.5";
371 deviceInfo->addrNum = 1;
372 deviceInfo->addr[0].type = CONNECTION_ADDR_MAX;
373 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, addrIp, strlen(addrIp));
374 deviceInfo->addr[0].info.ip.port = 0;
375 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
376 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
377 std::shared_ptr<ConnectionAddr> ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
378 EXPECT_EQ(ret, nullptr);
379 SoftbusConnector::discoveryDeviceInfoMap_.clear();
380 }
381
382 /**
383 * @tc.name: ConvertNodeBasicInfoToDmDevice_001
384 * @tc.desc: go to the correct case and return DM_OK
385 * @tc.type: FUNC
386 * @tc.require: AR000GHSJK
387 */
388 HWTEST_F(SoftbusConnectorTest, ConvertDeviceInfoToDmDevice_001, testing::ext::TestSize.Level1)
389 {
390 DeviceInfo deviceInfo = {
391 .devId = "123456",
392 .devType = (DeviceType)1,
393 .devName = "11111"
394 };
395 DmDeviceInfo dm;
396 DmDeviceInfo dm_1 = {
397 .deviceId = "123456",
398 .deviceName = "11111",
399 .deviceTypeId = 1
400 };
401 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
402 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dm);
403 bool ret = false;
404 if (strcmp(dm.deviceId, dm_1.deviceId) == 0) {
405 ret = true;
406 }
407 EXPECT_EQ(ret, true);
408 }
409
410 /**
411 * @tc.name: JoinLnn_001
412 * @tc.desc: set deviceId null
413 * @tc.type: FUNC
414 */
415 HWTEST_F(SoftbusConnectorTest, JoinLnn_001, testing::ext::TestSize.Level1)
416 {
417 std::string deviceId;
418 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
419 softbusConnector->JoinLnn(deviceId);
420 EXPECT_EQ(SoftbusConnector::discoveryDeviceInfoMap_.empty(), true);
421 }
422
423 /**
424 * @tc.name: ConvertDeviceInfoToDmDevice_002
425 * @tc.desc: set deviceInfo not null
426 * @tc.type: FUNC
427 */
428 HWTEST_F(SoftbusConnectorTest, ConvertDeviceInfoToDmDevice_002, testing::ext::TestSize.Level1)
429 {
430 DeviceInfo deviceInfo = {
431 .devId = "123456",
432 .devType = (DeviceType)1,
433 .devName = "11111"
434 };
435 DmDeviceBasicInfo dmDeviceBasicInfo;
436 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
437 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dmDeviceBasicInfo);
438 EXPECT_EQ(dmDeviceBasicInfo.deviceTypeId, deviceInfo.devType);
439 }
440
441 /**
442 * @tc.name: GetDeviceUdidByUdidHash_001
443 * @tc.desc: set udidHash null
444 * @tc.type: FUNC
445 */
446 HWTEST_F(SoftbusConnectorTest, GetDeviceUdidByUdidHash_001, testing::ext::TestSize.Level1)
447 {
448 std::string udidHash;
449 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
450 std::string str = softbusConnector->GetDeviceUdidByUdidHash(udidHash);
451 EXPECT_EQ(str.empty(), true);
452 }
453
454 /**
455 * @tc.name: RegisterSoftbusStateCallback_001
456 * @tc.desc: set callback null
457 * @tc.type: FUNC
458 */
459 HWTEST_F(SoftbusConnectorTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level1)
460 {
461 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
462 std::shared_ptr<ISoftbusStateCallback> callback = std::make_shared<SoftbusStateCallbackTest>();
463 int32_t ret = softbusConnector->RegisterSoftbusStateCallback(callback);
464 EXPECT_EQ(ret, DM_OK);
465 }
466
467 /**
468 * @tc.name: UnRegisterSoftbusStateCallback_001
469 * @tc.type: FUNC
470 */
471 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusStateCallback_001, testing::ext::TestSize.Level1)
472 {
473 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
474 int32_t ret = softbusConnector->UnRegisterSoftbusStateCallback();
475 EXPECT_EQ(ret, DM_OK);
476 }
477
478 /**
479 * @tc.name: OnSoftbusJoinLNNResult_001
480 * @tc.desc: set addr null
481 * @tc.desc: set networkId null
482 * @tc.desc: set result 0
483 * @tc.type: FUNC
484 */
485 HWTEST_F(SoftbusConnectorTest, OnSoftbusJoinLNNResult_001, testing::ext::TestSize.Level1)
486 {
487 ConnectionAddr *addr = nullptr;
488 char *networkId = nullptr;
489 int32_t result = 0;
490 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
491 softbusConnector->OnSoftbusJoinLNNResult(addr, networkId, result);
492 EXPECT_EQ(SoftbusConnector::discoveryDeviceInfoMap_.empty(), true);
493 }
494
495 /**
496 * @tc.name: AddMemberToDiscoverMap_001
497 * @tc.type: FUNC
498 */
499 HWTEST_F(SoftbusConnectorTest, AddMemberToDiscoverMap_001, testing::ext::TestSize.Level1)
500 {
501 std::string deviceId;
502 std::shared_ptr<DeviceInfo> deviceInfo = nullptr;
503 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
504 int32_t ret = softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo);
505 EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
506 }
507
508 /**
509 * @tc.name: AddMemberToDiscoverMap_002
510 * @tc.type: FUNC
511 */
512 HWTEST_F(SoftbusConnectorTest, AddMemberToDiscoverMap_002, testing::ext::TestSize.Level1)
513 {
514 std::string deviceId = "deviceId";
515 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
516 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
517 int32_t ret = softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo);
518 EXPECT_EQ(ret, DM_OK);
519 }
520
521 /**
522 * @tc.name: SetPkgName_001
523 * @tc.type: FUNC
524 */
525 HWTEST_F(SoftbusConnectorTest, SetProcessInfo_001, testing::ext::TestSize.Level1)
526 {
527 ProcessInfo processInfo;
528 std::vector<ProcessInfo> processInfoVec;
529 std::string pkgName = "pkgName";
530 std::vector<std::string> pkgNameVec;
531 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
532 softbusConnector->SetProcessInfoVec(processInfoVec);
533 softbusConnector->SetProcessInfo(processInfo);
534 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), false);
535 }
536
537 /**
538 * @tc.name: GetDeviceUdidHashByUdid_001
539 * @tc.type: FUNC
540 */
541 HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_001, testing::ext::TestSize.Level1)
542 {
543 std::string udid = "123456789";
544 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
545 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED));
546 std::string ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
547 EXPECT_EQ(ret.empty(), true);
548 }
549
550 /**
551 * @tc.name: EraseUdidFromMap_001
552 * @tc.type: FUNC
553 */
554 HWTEST_F(SoftbusConnectorTest, EraseUdidFromMap_001, testing::ext::TestSize.Level1)
555 {
556 std::string udid = "123456789";
557 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
558 softbusConnector->EraseUdidFromMap(udid);
559 EXPECT_EQ(softbusConnector->deviceUdidMap_.empty(), true);
560 }
561
562 /**
563 * @tc.name: GetLocalDeviceName_001
564 * @tc.type: FUNC
565 */
566 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceName_001, testing::ext::TestSize.Level1)
567 {
568 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
569 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
570 std::string ret = softbusConnector->GetLocalDeviceName();
571 EXPECT_EQ(ret.empty(), true);
572
573 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK));
574 ret = softbusConnector->GetLocalDeviceName();
575 EXPECT_EQ(ret.empty(), true);
576 }
577
578 /**
579 * @tc.name: GetNetworkIdByDeviceId_001
580 * @tc.type: FUNC
581 */
582 HWTEST_F(SoftbusConnectorTest, GetNetworkIdByDeviceId_001, testing::ext::TestSize.Level1)
583 {
584 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
585 std::string deviceId = "deviceId";
586 std::string strNetworkId = "net******12";
587 int32_t deviceCount = 1;
588 NodeBasicInfo nodeBasicInfo = {
589 .networkId = "network*1",
590 .deviceName = "deviceName",
591 .deviceTypeId = 1
592 };
593 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
594 std::string ret = softbusConnector->GetNetworkIdByDeviceId(deviceId);
595 EXPECT_EQ(ret.empty(), true);
596
597 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon0a6755f70202(int32_t *infoNum) 598 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
599 infoNum = &deviceCount;
600 return DM_OK;
601 })));
602 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon0a6755f70302(uint8_t *info) 603 .WillOnce(WithArgs<3>(Invoke([deviceId](uint8_t *info) {
604 memcpy_s(info, (deviceId.length() + 1), deviceId.c_str(), deviceId.length());
605 return DM_OK;
606 })));
607 ret = softbusConnector->GetNetworkIdByDeviceId(deviceId);
608 EXPECT_EQ(ret.empty(), true);
609
610 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK));
611 ret = softbusConnector->GetNetworkIdByDeviceId(deviceId);
612 EXPECT_EQ(ret.empty(), true);
613 }
614
615 /**
616 * @tc.name: SetProcessInfoVec_001
617 * @tc.type: FUNC
618 */
619 HWTEST_F(SoftbusConnectorTest, SetProcessInfoVec_001, testing::ext::TestSize.Level1)
620 {
621 std::vector<ProcessInfo> processInfoVec;
622 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
623 softbusConnector->SetProcessInfoVec(processInfoVec);
624 EXPECT_EQ(processInfoVec.empty(), true);
625 }
626
627 /**
628 * @tc.name: GetPkgName_001
629 * @tc.type: FUNC
630 */
631 HWTEST_F(SoftbusConnectorTest, GetProcessInfo_001, testing::ext::TestSize.Level1)
632 {
633 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
634 auto ret = softbusConnector->GetProcessInfo();
635 EXPECT_EQ(ret.empty(), true);
636 }
637
638 /**
639 * @tc.name: ClearPkgName_001
640 * @tc.type: FUNC
641 */
642 HWTEST_F(SoftbusConnectorTest, ClearProcessInfo_001, testing::ext::TestSize.Level1)
643 {
644 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
645 softbusConnector->ClearProcessInfo();
646 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
647 }
648
649 /**
650 * @tc.name: HandleDeviceOnline_001
651 * @tc.type: FUNC
652 */
653 HWTEST_F(SoftbusConnectorTest, HandleDeviceOnline_001, testing::ext::TestSize.Level1)
654 {
655 std::string deviceId = "deviceId";
656 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
657 std::shared_ptr<ISoftbusStateCallback> callback = std::make_shared<SoftbusStateCallbackTest>();
658 softbusConnector->RegisterSoftbusStateCallback(callback);
659 softbusConnector->HandleDeviceOnline(deviceId, DmAuthForm::ACROSS_ACCOUNT);
660 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
661 }
662
663 /**
664 * @tc.name: HandleDeviceOffline_001
665 * @tc.type: FUNC
666 */
667 HWTEST_F(SoftbusConnectorTest, HandleDeviceOffline_001, testing::ext::TestSize.Level1)
668 {
669 std::string deviceId = "deviceId";
670 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
671 std::shared_ptr<ISoftbusStateCallback> callback = std::make_shared<SoftbusStateCallbackTest>();
672 softbusConnector->RegisterSoftbusStateCallback(callback);
673 softbusConnector->HandleDeviceOffline(deviceId);
674 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
675 }
676
677 /**
678 * @tc.name: CheckIsOnline_001
679 * @tc.type: FUNC
680 */
681 HWTEST_F(SoftbusConnectorTest, CheckIsOnline_001, testing::ext::TestSize.Level1)
682 {
683 std::string targetId = "targetDeviceId";
684 int32_t deviceCount = 1;
685 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
686 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
687 bool ret = softbusConnector->CheckIsOnline(targetId);
688 EXPECT_FALSE(ret);
689
690 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon0a6755f70402(int32_t *infoNum) 691 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
692 infoNum = &deviceCount;
693 return DM_OK;
694 })));
695 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon0a6755f70502(uint8_t *info) 696 .WillOnce(WithArgs<3>(Invoke([targetId](uint8_t *info) {
697 memcpy_s(info, (targetId.length() + 1), targetId.c_str(), targetId.length());
698 return DM_OK;
699 })));
700 ret = softbusConnector->CheckIsOnline(targetId);
701 EXPECT_FALSE(ret);
702
703 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK));
704 ret = softbusConnector->CheckIsOnline(targetId);
705 EXPECT_FALSE(ret);
706 }
707
708 /**
709 * @tc.name: GetDeviceInfoByDeviceId_001
710 * @tc.type: FUNC
711 */
712 HWTEST_F(SoftbusConnectorTest, GetDeviceInfoByDeviceId_001, testing::ext::TestSize.Level1)
713 {
714 std::string deviceId = "deviceId";
715 int32_t deviceCount = 1;
716 std::string strNetworkId = "networkId**1";
717 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
718 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
719 auto ret = softbusConnector->GetDeviceInfoByDeviceId(deviceId);
720 EXPECT_EQ(ret.deviceId == deviceId, false);
721
722 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon0a6755f70602(int32_t *infoNum) 723 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
724 infoNum = &deviceCount;
725 return DM_OK;
726 })));
727 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED));
728 EXPECT_EQ(ret.deviceId == deviceId, false);
729
730 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon0a6755f70702(int32_t *infoNum) 731 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
732 infoNum = &deviceCount;
733 return DM_OK;
734 })));
735 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(DM_OK));
736 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon0a6755f70802(uint8_t *info) 737 .WillOnce(WithArgs<3>(Invoke([](uint8_t *info) {
738 info[0] = 'd';
739 info[1] = 'e';
740 info[2] = 'v';
741 info[3] = 'i';
742 return DM_OK;
743 })));
744 EXPECT_EQ(ret.deviceId == deviceId, false);
745
746 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon0a6755f70902(int32_t *infoNum) 747 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
748 infoNum = &deviceCount;
749 return DM_OK;
750 })));
751 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(DM_OK));
752 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon0a6755f70a02(uint8_t *info) 753 .WillOnce(WithArgs<3>(Invoke([deviceId, strNetworkId](uint8_t *info) {
754 memcpy_s(info, (strNetworkId.length() + 1), strNetworkId.c_str(), (strNetworkId.length()));
755 return DM_OK;
756 })));
757 EXPECT_EQ(ret.deviceId == deviceId, false);
758 }
759
760 /**
761 * @tc.name: ConvertNodeBasicInfoToDmDevice_001
762 * @tc.type: FUNC
763 */
764 HWTEST_F(SoftbusConnectorTest, ConvertNodeBasicInfoToDmDevice_001, testing::ext::TestSize.Level1)
765 {
766 NodeBasicInfo nodeBasicInfo = {
767 .networkId = "123456",
768 .deviceName = "name",
769 };
770 DmDeviceInfo dmDeviceInfo;
771 JsonObject extraJson;
772 extraJson[PARAM_KEY_OS_TYPE] = 1;
773 std::vector<int32_t> versions = {0};
774 extraJson[PARAM_KEY_OS_VERSION] = versions;
775 dmDeviceInfo.extraData = extraJson.Dump();
776 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
777 softbusConnector->ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo);
778 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
779 }
780
781 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceTypeId_001, testing::ext::TestSize.Level1)
782 {
783 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
784 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED));
785 int32_t ret = softbusConnector->GetLocalDeviceTypeId();
786 EXPECT_EQ(ret, static_cast<int32_t>(DmDeviceType::DEVICE_TYPE_UNKNOWN));
787
788 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _))
__anon0a6755f70b02(NodeBasicInfo *info) 789 .WillOnce(WithArgs<1>(Invoke([](NodeBasicInfo *info) {
790 if (info != nullptr) {
791 info->deviceTypeId = 1;
792 }
793 return DM_OK;
794 })));
795 ret = softbusConnector->GetLocalDeviceTypeId();
796 EXPECT_EQ(ret, 1);
797 }
798
799 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceNetworkId_001, testing::ext::TestSize.Level1)
800 {
801 std::string networkId = "network*1";
802 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
803 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED));
804 auto ret = softbusConnector->GetLocalDeviceNetworkId();
805 EXPECT_EQ(ret.empty(), true);
806
807 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _))
__anon0a6755f70c02(NodeBasicInfo *info) 808 .WillOnce(WithArgs<1>(Invoke([networkId](NodeBasicInfo *info) {
809 if (info != nullptr) {
810 memcpy_s(info->networkId, sizeof(info->networkId), networkId.c_str(), networkId.length());
811 }
812 return DM_OK;
813 })));
814 ret = softbusConnector->GetLocalDeviceNetworkId();
815 EXPECT_EQ(ret.empty(), false);
816 }
817
818 HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_002, testing::ext::TestSize.Level1)
819 {
820 std::string udid = "1********69";
821 std::string udidHashTemp = "ajj*********47";
822 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
823 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED));
824 std::string ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
825 EXPECT_EQ(ret.empty(), true);
826
__anon0a6755f70d02(unsigned char *udidHash) 827 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(WithArgs<1>(Invoke([udidHashTemp](unsigned char *udidHash) {
828 memcpy_s(udidHash, (udidHashTemp.length() + 1), udidHashTemp.c_str(), (udidHashTemp.length()));
829 return DM_OK;
830 })));
831 ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
832 EXPECT_EQ(ret.empty(), false);
833
834 ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
835 EXPECT_EQ(ret.empty(), false);
836
837 int32_t sessionId = 1;
838 int32_t sessionKeyId = 1;
839 int32_t remoteSessionKeyId = 1;
840 softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId);
841
842 sessionId = 0;
843 softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId);
844 std::string deviceId = "deviceId";
845 bool isForceJoin = false;
846 softbusConnector->JoinLnn(deviceId, isForceJoin);
847 }
848 } // namespace
849 } // namespace DistributedHardware
850 } // namespace OHOS-