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 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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
219 {
220 std::string deviceId;
221 std::string connectAddr;
222 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
223 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.Level0)
234 {
235 std::string deviceId = "123345";
236 std::string connectAddr;
237 DeviceInfo deviceInfo;
238 deviceInfo.addrNum = -1;
239 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
240 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
241 EXPECT_EQ(ret, nullptr);
242 }
243
244 /**
245 * @tc.name: GetConnectAddr_003
246 * @tc.desc:set deviceInfo.addrNum = 1
247 * @tc.type: FUNC
248 * @tc.require: AR000GHSJK
249 */
250 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_003, testing::ext::TestSize.Level0)
251 {
252 std::string deviceId = "123345";
253 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
254 std::string connectAddr;
255 constexpr char ethIp[] = "0.0.0.0";
256 deviceInfo->addrNum = 1;
257 deviceInfo->addr[0].type = CONNECTION_ADDR_ETH;
258 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, ethIp, strlen(ethIp));
259 deviceInfo->addr[0].info.ip.port = 0;
260 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
261 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
262 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
263 EXPECT_NE(ret, nullptr);
264 SoftbusConnector::discoveryDeviceInfoMap_.clear();
265 }
266
267 /**
268 * @tc.name: GetConnectAddr_004
269 * @tc.desc:set deviceInfo.addrNum = 1
270 * @tc.type: FUNC
271 * @tc.require: AR000GHSJK
272 */
273 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_004, testing::ext::TestSize.Level0)
274 {
275 std::string deviceId = "123345";
276 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
277 std::string connectAddr;
278 constexpr char wlanIp[] = "1.1.1.1";
279 deviceInfo->addrNum = 1;
280 deviceInfo->addr[0].type = CONNECTION_ADDR_WLAN;
281 (void)strncpy_s(deviceInfo->addr[0].info.ip.ip, IP_STR_MAX_LEN, wlanIp, strlen(wlanIp));
282 deviceInfo->addr[0].info.ip.port = 0;
283 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
284 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
285 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
286 EXPECT_NE(ret, nullptr);
287 SoftbusConnector::discoveryDeviceInfoMap_.clear();
288 }
289
290 /**
291 * @tc.name: GetConnectAddr_005
292 * @tc.desc:get brMac addr
293 * @tc.type: FUNC
294 * @tc.require: AR000GHSJK
295 */
296 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_005, testing::ext::TestSize.Level0)
297 {
298 std::string deviceId = "123345";
299 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
300 std::string connectAddr;
301 deviceInfo->addrNum = 1;
302 constexpr char brMac[] = "2:2:2:2";
303 deviceInfo->addr[0].type = CONNECTION_ADDR_BR;
304 (void)strncpy_s(deviceInfo->addr[0].info.br.brMac, IP_STR_MAX_LEN, brMac, strlen(brMac));
305 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
306 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
307 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
308 EXPECT_NE(ret, nullptr);
309 SoftbusConnector::discoveryDeviceInfoMap_.clear();
310 }
311
312 /**
313 * @tc.name: GetConnectAddr_006
314 * @tc.desc:get bleMac addr
315 * @tc.type: FUNC
316 * @tc.require: AR000GHSJK
317 */
318 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_006, testing::ext::TestSize.Level0)
319 {
320 std::string deviceId = "123345";
321 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
322 std::string connectAddr;
323 constexpr char bleMac[] = "3:3:3:3";
324 deviceInfo->addrNum = 1;
325 deviceInfo->addr[0].type = CONNECTION_ADDR_BLE;
326 (void)strncpy_s(deviceInfo->addr[0].info.ble.bleMac, IP_STR_MAX_LEN, bleMac, strlen(bleMac));
327 SoftbusConnector::discoveryDeviceInfoMap_[deviceId] = deviceInfo;
328 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
329 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
330 EXPECT_NE(ret, nullptr);
331 SoftbusConnector::discoveryDeviceInfoMap_.clear();
332 }
333
334 /**
335 * @tc.name: ConvertNodeBasicInfoToDmDevice_001
336 * @tc.desc: go to the correct case and return DM_OK
337 * @tc.type: FUNC
338 * @tc.require: AR000GHSJK
339 */
340 HWTEST_F(SoftbusConnectorTest, ConvertDeviceInfoToDmDevice_001, testing::ext::TestSize.Level0)
341 {
342 DeviceInfo deviceInfo = {
343 .devId = "123456",
344 .devType = (DeviceType)1,
345 .devName = "11111"
346 };
347 DmDeviceInfo dm;
348 DmDeviceInfo dm_1 = {
349 .deviceId = "123456",
350 .deviceName = "11111",
351 .deviceTypeId = 1
352 };
353 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
354 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dm);
355 bool ret = false;
356 if (strcmp(dm.deviceId, dm_1.deviceId) == 0) {
357 ret = true;
358 }
359 EXPECT_EQ(ret, true);
360 }
361
362 /**
363 * @tc.name: JoinLnn_001
364 * @tc.desc: set deviceId null
365 * @tc.type: FUNC
366 */
367 HWTEST_F(SoftbusConnectorTest, JoinLnn_001, testing::ext::TestSize.Level0)
368 {
369 std::string deviceId;
370 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
371 softbusConnector->JoinLnn(deviceId);
372 EXPECT_EQ(SoftbusConnector::discoveryDeviceInfoMap_.empty(), true);
373 }
374
375 /**
376 * @tc.name: ConvertDeviceInfoToDmDevice_002
377 * @tc.desc: set deviceInfo not null
378 * @tc.type: FUNC
379 */
380 HWTEST_F(SoftbusConnectorTest, ConvertDeviceInfoToDmDevice_002, testing::ext::TestSize.Level0)
381 {
382 DeviceInfo deviceInfo = {
383 .devId = "123456",
384 .devType = (DeviceType)1,
385 .devName = "11111"
386 };
387 DmDeviceBasicInfo dmDeviceBasicInfo;
388 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
389 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dmDeviceBasicInfo);
390 EXPECT_EQ(dmDeviceBasicInfo.deviceTypeId, deviceInfo.devType);
391 }
392
393 /**
394 * @tc.name: GetDeviceUdidByUdidHash_001
395 * @tc.desc: set udidHash null
396 * @tc.type: FUNC
397 */
398 HWTEST_F(SoftbusConnectorTest, GetDeviceUdidByUdidHash_001, testing::ext::TestSize.Level0)
399 {
400 std::string udidHash;
401 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
402 std::string str = softbusConnector->GetDeviceUdidByUdidHash(udidHash);
403 EXPECT_EQ(str.empty(), true);
404 }
405
406 /**
407 * @tc.name: RegisterSoftbusStateCallback_001
408 * @tc.desc: set callback null
409 * @tc.type: FUNC
410 */
411 HWTEST_F(SoftbusConnectorTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
412 {
413 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
414 std::shared_ptr<ISoftbusStateCallback> callback = std::make_shared<SoftbusStateCallbackTest>();
415 int32_t ret = softbusConnector->RegisterSoftbusStateCallback(callback);
416 EXPECT_EQ(ret, DM_OK);
417 }
418
419 /**
420 * @tc.name: UnRegisterSoftbusStateCallback_001
421 * @tc.type: FUNC
422 */
423 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
424 {
425 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
426 int32_t ret = softbusConnector->UnRegisterSoftbusStateCallback();
427 EXPECT_EQ(ret, DM_OK);
428 }
429
430 /**
431 * @tc.name: OnSoftbusJoinLNNResult_001
432 * @tc.desc: set addr null
433 * @tc.desc: set networkId null
434 * @tc.desc: set result 0
435 * @tc.type: FUNC
436 */
437 HWTEST_F(SoftbusConnectorTest, OnSoftbusJoinLNNResult_001, testing::ext::TestSize.Level0)
438 {
439 ConnectionAddr *addr = nullptr;
440 char *networkId = nullptr;
441 int32_t result = 0;
442 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
443 softbusConnector->OnSoftbusJoinLNNResult(addr, networkId, result);
444 EXPECT_EQ(SoftbusConnector::discoveryDeviceInfoMap_.empty(), true);
445 }
446
447 /**
448 * @tc.name: AddMemberToDiscoverMap_001
449 * @tc.type: FUNC
450 */
451 HWTEST_F(SoftbusConnectorTest, AddMemberToDiscoverMap_001, testing::ext::TestSize.Level0)
452 {
453 std::string deviceId;
454 std::shared_ptr<DeviceInfo> deviceInfo = nullptr;
455 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
456 int32_t ret = softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo);
457 EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
458 }
459
460 /**
461 * @tc.name: AddMemberToDiscoverMap_002
462 * @tc.type: FUNC
463 */
464 HWTEST_F(SoftbusConnectorTest, AddMemberToDiscoverMap_002, testing::ext::TestSize.Level0)
465 {
466 std::string deviceId = "deviceId";
467 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>();
468 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
469 int32_t ret = softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo);
470 EXPECT_EQ(ret, DM_OK);
471 }
472
473 /**
474 * @tc.name: SetPkgName_001
475 * @tc.type: FUNC
476 */
477 HWTEST_F(SoftbusConnectorTest, SetProcessInfo_001, testing::ext::TestSize.Level0)
478 {
479 ProcessInfo processInfo;
480 std::vector<ProcessInfo> processInfoVec;
481 std::string pkgName = "pkgName";
482 std::vector<std::string> pkgNameVec;
483 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
484 softbusConnector->SetProcessInfoVec(processInfoVec);
485 softbusConnector->SetProcessInfo(processInfo);
486 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), false);
487 }
488
489 /**
490 * @tc.name: GetDeviceUdidHashByUdid_001
491 * @tc.type: FUNC
492 */
493 HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_001, testing::ext::TestSize.Level0)
494 {
495 std::string udid = "123456789";
496 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
497 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED));
498 std::string ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
499 EXPECT_EQ(ret.empty(), true);
500 }
501
502 /**
503 * @tc.name: EraseUdidFromMap_001
504 * @tc.type: FUNC
505 */
506 HWTEST_F(SoftbusConnectorTest, EraseUdidFromMap_001, testing::ext::TestSize.Level0)
507 {
508 std::string udid = "123456789";
509 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
510 softbusConnector->EraseUdidFromMap(udid);
511 EXPECT_EQ(softbusConnector->deviceUdidMap_.empty(), true);
512 }
513
514 /**
515 * @tc.name: GetLocalDeviceName_001
516 * @tc.type: FUNC
517 */
518 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceName_001, testing::ext::TestSize.Level0)
519 {
520 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
521 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
522 std::string ret = softbusConnector->GetLocalDeviceName();
523 EXPECT_EQ(ret.empty(), true);
524
525 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK));
526 ret = softbusConnector->GetLocalDeviceName();
527 EXPECT_EQ(ret.empty(), true);
528 }
529
530 /**
531 * @tc.name: GetNetworkIdByDeviceId_001
532 * @tc.type: FUNC
533 */
534 HWTEST_F(SoftbusConnectorTest, GetNetworkIdByDeviceId_001, testing::ext::TestSize.Level0)
535 {
536 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
537 std::string deviceId = "deviceId";
538 std::string strNetworkId = "net******12";
539 int32_t deviceCount = 1;
540 NodeBasicInfo nodeBasicInfo = {
541 .networkId = "network*1",
542 .deviceName = "deviceName",
543 .deviceTypeId = 1
544 };
545 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
546 std::string ret = softbusConnector->GetNetworkIdByDeviceId(deviceId);
547 EXPECT_EQ(ret.empty(), true);
548
549 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon5b07f1550202(int32_t *infoNum) 550 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
551 infoNum = &deviceCount;
552 return DM_OK;
553 })));
554 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon5b07f1550302(uint8_t *info) 555 .WillOnce(WithArgs<3>(Invoke([deviceId](uint8_t *info) {
556 memcpy_s(info, (deviceId.length() + 1), deviceId.c_str(), deviceId.length());
557 return DM_OK;
558 })));
559 ret = softbusConnector->GetNetworkIdByDeviceId(deviceId);
560 EXPECT_EQ(ret.empty(), true);
561
562 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK));
563 ret = softbusConnector->GetNetworkIdByDeviceId(deviceId);
564 EXPECT_EQ(ret.empty(), true);
565 }
566
567 /**
568 * @tc.name: SetProcessInfoVec_001
569 * @tc.type: FUNC
570 */
571 HWTEST_F(SoftbusConnectorTest, SetProcessInfoVec_001, testing::ext::TestSize.Level0)
572 {
573 std::vector<ProcessInfo> processInfoVec;
574 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
575 softbusConnector->SetProcessInfoVec(processInfoVec);
576 EXPECT_EQ(processInfoVec.empty(), true);
577 }
578
579 /**
580 * @tc.name: GetPkgName_001
581 * @tc.type: FUNC
582 */
583 HWTEST_F(SoftbusConnectorTest, GetProcessInfo_001, testing::ext::TestSize.Level0)
584 {
585 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
586 auto ret = softbusConnector->GetProcessInfo();
587 EXPECT_EQ(ret.empty(), true);
588 }
589
590 /**
591 * @tc.name: ClearPkgName_001
592 * @tc.type: FUNC
593 */
594 HWTEST_F(SoftbusConnectorTest, ClearProcessInfo_001, testing::ext::TestSize.Level0)
595 {
596 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
597 softbusConnector->ClearProcessInfo();
598 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
599 }
600
601 /**
602 * @tc.name: HandleDeviceOnline_001
603 * @tc.type: FUNC
604 */
605 HWTEST_F(SoftbusConnectorTest, HandleDeviceOnline_001, testing::ext::TestSize.Level0)
606 {
607 std::string deviceId = "deviceId";
608 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
609 std::shared_ptr<ISoftbusStateCallback> callback = std::make_shared<SoftbusStateCallbackTest>();
610 softbusConnector->RegisterSoftbusStateCallback(callback);
611 softbusConnector->HandleDeviceOnline(deviceId, DmAuthForm::ACROSS_ACCOUNT);
612 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
613 }
614
615 /**
616 * @tc.name: HandleDeviceOffline_001
617 * @tc.type: FUNC
618 */
619 HWTEST_F(SoftbusConnectorTest, HandleDeviceOffline_001, testing::ext::TestSize.Level0)
620 {
621 std::string deviceId = "deviceId";
622 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
623 std::shared_ptr<ISoftbusStateCallback> callback = std::make_shared<SoftbusStateCallbackTest>();
624 softbusConnector->RegisterSoftbusStateCallback(callback);
625 softbusConnector->HandleDeviceOffline(deviceId);
626 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
627 }
628
629 /**
630 * @tc.name: CheckIsOnline_001
631 * @tc.type: FUNC
632 */
633 HWTEST_F(SoftbusConnectorTest, CheckIsOnline_001, testing::ext::TestSize.Level0)
634 {
635 std::string targetId = "targetDeviceId";
636 int32_t deviceCount = 1;
637 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
638 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
639 bool ret = softbusConnector->CheckIsOnline(targetId);
640 EXPECT_FALSE(ret);
641
642 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon5b07f1550402(int32_t *infoNum) 643 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
644 infoNum = &deviceCount;
645 return DM_OK;
646 })));
647 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon5b07f1550502(uint8_t *info) 648 .WillOnce(WithArgs<3>(Invoke([targetId](uint8_t *info) {
649 memcpy_s(info, (targetId.length() + 1), targetId.c_str(), targetId.length());
650 return DM_OK;
651 })));
652 ret = softbusConnector->CheckIsOnline(targetId);
653 EXPECT_FALSE(ret);
654
655 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK));
656 ret = softbusConnector->CheckIsOnline(targetId);
657 EXPECT_FALSE(ret);
658 }
659
660 /**
661 * @tc.name: GetDeviceInfoByDeviceId_001
662 * @tc.type: FUNC
663 */
664 HWTEST_F(SoftbusConnectorTest, GetDeviceInfoByDeviceId_001, testing::ext::TestSize.Level0)
665 {
666 std::string deviceId = "deviceId";
667 int32_t deviceCount = 1;
668 std::string strNetworkId = "networkId**1";
669 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
670 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
671 auto ret = softbusConnector->GetDeviceInfoByDeviceId(deviceId);
672 EXPECT_EQ(ret.deviceId == deviceId, false);
673
674 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon5b07f1550602(int32_t *infoNum) 675 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
676 infoNum = &deviceCount;
677 return DM_OK;
678 })));
679 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED));
680 EXPECT_EQ(ret.deviceId == deviceId, false);
681
682 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon5b07f1550702(int32_t *infoNum) 683 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
684 infoNum = &deviceCount;
685 return DM_OK;
686 })));
687 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(DM_OK));
688 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon5b07f1550802(uint8_t *info) 689 .WillOnce(WithArgs<3>(Invoke([](uint8_t *info) {
690 info[0] = 'd';
691 info[1] = 'e';
692 info[2] = 'v';
693 info[3] = 'i';
694 return DM_OK;
695 })));
696 EXPECT_EQ(ret.deviceId == deviceId, false);
697
698 EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _))
__anon5b07f1550902(int32_t *infoNum) 699 .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) {
700 infoNum = &deviceCount;
701 return DM_OK;
702 })));
703 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(DM_OK));
704 EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _))
__anon5b07f1550a02(uint8_t *info) 705 .WillOnce(WithArgs<3>(Invoke([deviceId, strNetworkId](uint8_t *info) {
706 memcpy_s(info, (strNetworkId.length() + 1), strNetworkId.c_str(), (strNetworkId.length()));
707 return DM_OK;
708 })));
709 EXPECT_EQ(ret.deviceId == deviceId, false);
710 }
711
712 /**
713 * @tc.name: ConvertNodeBasicInfoToDmDevice_001
714 * @tc.type: FUNC
715 */
716 HWTEST_F(SoftbusConnectorTest, ConvertNodeBasicInfoToDmDevice_001, testing::ext::TestSize.Level0)
717 {
718 NodeBasicInfo nodeBasicInfo = {
719 .networkId = "123456",
720 .deviceName = "name",
721 };
722 DmDeviceInfo dmDeviceInfo;
723 JsonObject extraJson;
724 extraJson[PARAM_KEY_OS_TYPE] = 1;
725 std::vector<int32_t> versions = {0};
726 extraJson[PARAM_KEY_OS_VERSION] = versions;
727 dmDeviceInfo.extraData = extraJson.Dump();
728 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
729 softbusConnector->ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo);
730 EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true);
731 }
732
733 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceTypeId_001, testing::ext::TestSize.Level0)
734 {
735 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
736 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED));
737 int32_t ret = softbusConnector->GetLocalDeviceTypeId();
738 EXPECT_EQ(ret, static_cast<int32_t>(DmDeviceType::DEVICE_TYPE_UNKNOWN));
739
740 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _))
__anon5b07f1550b02(NodeBasicInfo *info) 741 .WillOnce(WithArgs<1>(Invoke([](NodeBasicInfo *info) {
742 if (info != nullptr) {
743 info->deviceTypeId = 1;
744 }
745 return DM_OK;
746 })));
747 ret = softbusConnector->GetLocalDeviceTypeId();
748 EXPECT_EQ(ret, 1);
749 }
750
751 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceNetworkId_001, testing::ext::TestSize.Level0)
752 {
753 std::string networkId = "network*1";
754 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
755 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED));
756 auto ret = softbusConnector->GetLocalDeviceNetworkId();
757 EXPECT_EQ(ret.empty(), true);
758
759 EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _))
__anon5b07f1550c02(NodeBasicInfo *info) 760 .WillOnce(WithArgs<1>(Invoke([networkId](NodeBasicInfo *info) {
761 if (info != nullptr) {
762 memcpy_s(info->networkId, sizeof(info->networkId), networkId.c_str(), networkId.length());
763 }
764 return DM_OK;
765 })));
766 ret = softbusConnector->GetLocalDeviceNetworkId();
767 EXPECT_EQ(ret.empty(), false);
768 }
769
770 HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_002, testing::ext::TestSize.Level0)
771 {
772 std::string udid = "1********69";
773 std::string udidHashTemp = "ajj*********47";
774 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
775 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED));
776 std::string ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
777 EXPECT_EQ(ret.empty(), true);
778
__anon5b07f1550d02(unsigned char *udidHash) 779 EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(WithArgs<1>(Invoke([udidHashTemp](unsigned char *udidHash) {
780 memcpy_s(udidHash, (udidHashTemp.length() + 1), udidHashTemp.c_str(), (udidHashTemp.length()));
781 return DM_OK;
782 })));
783 ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
784 EXPECT_EQ(ret.empty(), false);
785
786 ret = softbusConnector->GetDeviceUdidHashByUdid(udid);
787 EXPECT_EQ(ret.empty(), false);
788
789 int32_t sessionId = 1;
790 int32_t sessionKeyId = 1;
791 int32_t remoteSessionKeyId = 1;
792 softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId);
793
794 sessionId = 0;
795 softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId);
796 std::string deviceId = "deviceId";
797 bool isForceJoin = false;
798 softbusConnector->JoinLnn(deviceId, isForceJoin);
799 }
800 } // namespace
801 } // namespace DistributedHardware
802 } // namespace OHOS-