1 /*
2 * Copyright (c) 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
16 #include <memory>
17 #include <vector>
18
19 #include <unistd.h>
20
21 #include "accesstoken_kit.h"
22 #include "device_manager.h"
23 #include "dm_device_info.h"
24 #include "nativetoken_kit.h"
25 #include "securec.h"
26 #include "token_setproc.h"
27 #include <gtest/gtest.h>
28
29 #include "devicestatus_define.h"
30 #include "devicestatus_errors.h"
31 #include "dsoftbus_adapter.h"
32 #include "dsoftbus_adapter_impl.h"
33 #include "utility.h"
34
35 #undef LOG_TAG
36 #define LOG_TAG "DsoftbusAdapterTest"
37
38 namespace OHOS {
39 namespace Msdp {
40 namespace DeviceStatus {
41 using namespace testing::ext;
42 namespace {
43 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
44 const std::string SYSTEM_CORE { "system_core" };
45 uint64_t g_tokenID { 0 };
46 #define SERVER_SESSION_NAME "ohos.msdp.device_status.intention.serversession"
47 #define DSTB_HARDWARE DistributedHardware::DeviceManager::GetInstance()
48 const std::string PKG_NAME_PREFIX { "DBinderBus_Dms_" };
49 const std::string CLIENT_SESSION_NAME { "ohos.msdp.device_status.intention.clientsession." };
50 constexpr size_t DEVICE_NAME_SIZE_MAX { 256 };
51 constexpr size_t PKG_NAME_SIZE_MAX { 65 };
52 constexpr int32_t SOCKET_SERVER { 0 };
53 constexpr int32_t SOCKET_CLIENT { 1 };
54 constexpr int32_t SOCKET { 1 };
55 const char *g_cores[] = { "ohos.permission.INPUT_MONITORING" };
56 } // namespace
57
58 class DsoftbusAdapterTest : public testing::Test {
59 public:
60 void SetUp();
61 void TearDown();
62 static void SetUpTestCase();
63 static void SetPermission(const std::string &level, const char **perms, size_t permAmount);
64 static void RemovePermission();
65 static std::string GetLocalNetworkId();
66 };
67
SetPermission(const std::string & level,const char ** perms,size_t permAmount)68 void DsoftbusAdapterTest::SetPermission(const std::string &level, const char **perms, size_t permAmount)
69 {
70 CALL_DEBUG_ENTER;
71 if (perms == nullptr || permAmount == 0) {
72 FI_HILOGE("The perms is empty");
73 return;
74 }
75
76 NativeTokenInfoParams infoInstance = {
77 .dcapsNum = 0,
78 .permsNum = permAmount,
79 .aclsNum = 0,
80 .dcaps = nullptr,
81 .perms = perms,
82 .acls = nullptr,
83 .processName = "DsoftbusAdapterTest",
84 .aplStr = level.c_str(),
85 };
86 g_tokenID = GetAccessTokenId(&infoInstance);
87 SetSelfTokenID(g_tokenID);
88 OHOS::Security::AccessToken::AccessTokenKit::AccessTokenKit::ReloadNativeTokenInfo();
89 }
90
RemovePermission()91 void DsoftbusAdapterTest::RemovePermission()
92 {
93 CALL_DEBUG_ENTER;
94 int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(g_tokenID);
95 if (ret != RET_OK) {
96 FI_HILOGE("Failed to remove permission");
97 return;
98 }
99 }
100
SetUpTestCase()101 void DsoftbusAdapterTest::SetUpTestCase() { }
102
SetUp()103 void DsoftbusAdapterTest::SetUp() { }
104
TearDown()105 void DsoftbusAdapterTest::TearDown()
106 {
107 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
108 }
109
110 class DSoftbusObserver final : public IDSoftbusObserver {
111 public:
112 DSoftbusObserver() = default;
113 ~DSoftbusObserver() = default;
114
OnBind(const std::string & networkId)115 void OnBind(const std::string &networkId) { }
OnShutdown(const std::string & networkId)116 void OnShutdown(const std::string &networkId) { }
OnConnected(const std::string & networkId)117 void OnConnected(const std::string &networkId) { }
OnPacket(const std::string & networkId,NetPacket & packet)118 bool OnPacket(const std::string &networkId, NetPacket &packet)
119 {
120 return true;
121 }
OnRawData(const std::string & networkId,const void * data,uint32_t dataLen)122 bool OnRawData(const std::string &networkId, const void *data, uint32_t dataLen)
123 {
124 return true;
125 }
126 };
127
GetLocalNetworkId()128 std::string DsoftbusAdapterTest::GetLocalNetworkId()
129 {
130 auto packageName = PKG_NAME_PREFIX + std::to_string(getpid());
131 OHOS::DistributedHardware::DmDeviceInfo dmDeviceInfo;
132 if (int32_t errCode = DSTB_HARDWARE.GetLocalDeviceInfo(packageName, dmDeviceInfo); errCode != RET_OK) {
133 FI_HILOGE("GetLocalBasicInfo failed, errCode:%{public}d", errCode);
134 return {};
135 }
136 FI_HILOGD("LocalNetworkId:%{public}s", Utility::Anonymize(dmDeviceInfo.networkId).c_str());
137 return dmDeviceInfo.networkId;
138 }
139
140 /**
141 * @tc.name: TestEnable
142 * @tc.desc: Test Enable
143 * @tc.type: FUNC
144 * @tc.require:
145 */
146 HWTEST_F(DsoftbusAdapterTest, TestEnable, TestSize.Level1)
147 {
148 CALL_TEST_DEBUG;
149 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
150 DSoftbusAdapter dSoftbusAdapter;
151 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.Enable());
152 RemovePermission();
153 }
154
155 /**
156 * @tc.name: TestDisable
157 * @tc.desc: Test Disable
158 * @tc.type: FUNC
159 * @tc.require:
160 */
161 HWTEST_F(DsoftbusAdapterTest, TestDisable, TestSize.Level1)
162 {
163 CALL_TEST_DEBUG;
164 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
165 DSoftbusAdapter dSoftbusAdapter;
166 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.Disable());
167 RemovePermission();
168 }
169
170 /**
171 * @tc.name: TestAddObserver
172 * @tc.desc: Test AddObserver
173 * @tc.type: FUNC
174 * @tc.require:
175 */
176 HWTEST_F(DsoftbusAdapterTest, TestAddObserver, TestSize.Level1)
177 {
178 CALL_TEST_DEBUG;
179 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
180 DSoftbusAdapter dSoftbusAdapter;
181 std::shared_ptr<IDSoftbusObserver> observer = std::make_shared<DSoftbusObserver>();
182 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.Enable());
183 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.AddObserver(observer));
184 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.Disable());
185 RemovePermission();
186 }
187 /**
188 * @tc.name: TestRemoveObserver
189 * @tc.desc: Test RemoveObserver
190 * @tc.type: FUNC
191 * @tc.require:
192 */
193 HWTEST_F(DsoftbusAdapterTest, TestRemoveObserver, TestSize.Level1)
194 {
195 CALL_TEST_DEBUG;
196 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
197 DSoftbusAdapter dSoftbusAdapter;
198 std::shared_ptr<IDSoftbusObserver> observer = std::make_shared<DSoftbusObserver>();
199 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.Enable());
200 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.AddObserver(observer));
201 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.RemoveObserver(observer));
202 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.Disable());
203 RemovePermission();
204 }
205
206 /**
207 * @tc.name: TestOpenSession
208 * @tc.desc: Test OpenSession
209 * @tc.type: FUNC
210 * @tc.require:
211 */
212 HWTEST_F(DsoftbusAdapterTest, TestOpenSession, TestSize.Level1)
213 {
214 CALL_TEST_DEBUG;
215 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
216 DSoftbusAdapter dSoftbusAdapter;
217 std::string networkId("softbus");
218 int32_t ret = dSoftbusAdapter.OpenSession(networkId);
219 ASSERT_EQ(ret, RET_ERR);
220 RemovePermission();
221 }
222
223 /**
224 * @tc.name: TestCloseSession
225 * @tc.desc: Test CloseSession
226 * @tc.type: FUNC
227 * @tc.require:
228 */
229 HWTEST_F(DsoftbusAdapterTest, TestCloseSession, TestSize.Level1)
230 {
231 CALL_TEST_DEBUG;
232 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
233 DSoftbusAdapter dSoftbusAdapter;
234 std::string networkId("softbus");
235 int32_t ret = dSoftbusAdapter.OpenSession(networkId);
236 ASSERT_EQ(ret, RET_ERR);
237 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.CloseSession(networkId));
238 RemovePermission();
239 }
240
241 /**
242 * @tc.name: TestSendPacket
243 * @tc.desc: Test SendPacket
244 * @tc.type: FUNC
245 * @tc.require:
246 */
247 HWTEST_F(DsoftbusAdapterTest, SendPacket, TestSize.Level1)
248 {
249 CALL_TEST_DEBUG;
250 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
251 DSoftbusAdapter dSoftbusAdapter;
252 std::string networkId("softbus");
253 NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
254 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.SendPacket(networkId, packet));
255 RemovePermission();
256 }
257
258 /**
259 * @tc.name: TestSendParcel
260 * @tc.desc: Test SendParcel
261 * @tc.type: FUNC
262 * @tc.require:
263 */
264 HWTEST_F(DsoftbusAdapterTest, SendParcel, TestSize.Level1)
265 {
266 CALL_TEST_DEBUG;
267 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
268 DSoftbusAdapter dSoftbusAdapter;
269 std::string networkId("softbus");
270 Parcel parcel;
271 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.SendParcel(networkId, parcel));
272 RemovePermission();
273 }
274
275 /**
276 * @tc.name: TestSetupServer
277 * @tc.desc: Test SetupServer
278 * @tc.type: FUNC
279 * @tc.require:
280 */
281 HWTEST_F(DsoftbusAdapterTest, SetupServer, TestSize.Level1)
282 {
283 CALL_TEST_DEBUG;
284 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
285 DSoftbusAdapter dSoftbusAdapter;
286 int32_t ret = DSoftbusAdapterImpl::GetInstance()->SetupServer();
287 ASSERT_EQ(ret, RET_ERR);
288 RemovePermission();
289 }
290
291 /**
292 * @tc.name: TestConfigTcpAlive
293 * @tc.desc: Test ConfigTcpAlive
294 * @tc.type: FUNC
295 * @tc.require:
296 */
297 HWTEST_F(DsoftbusAdapterTest, ConfigTcpAlive, TestSize.Level1)
298 {
299 CALL_TEST_DEBUG;
300 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
301 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->ConfigTcpAlive(SOCKET));
302 RemovePermission();
303 }
304
305 /**
306 * @tc.name: TestInitSocket
307 * @tc.desc: Test InitSocket
308 * @tc.type: FUNC
309 * @tc.require:
310 */
311 HWTEST_F(DsoftbusAdapterTest, InitSocket, TestSize.Level1)
312 {
313 CALL_TEST_DEBUG;
314 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
315 DSoftbusAdapter dSoftbusAdapter;
316 char name[DEVICE_NAME_SIZE_MAX] {};
317 char peerName[DEVICE_NAME_SIZE_MAX] { SERVER_SESSION_NAME };
318 char peerNetworkId[PKG_NAME_SIZE_MAX] {};
319 char pkgName[PKG_NAME_SIZE_MAX] { FI_PKG_NAME };
320 SocketInfo info { .name = name,
321 .peerName = peerName,
322 .peerNetworkId = peerNetworkId,
323 .pkgName = pkgName,
324 .dataType = DATA_TYPE_BYTES };
325 int32_t socket = 1;
326 int32_t ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_CLIENT, socket);
327 ASSERT_EQ(ret, RET_ERR);
328 ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_SERVER, socket);
329 ASSERT_EQ(ret, RET_ERR);
330 RemovePermission();
331 }
332
333 /**
334 * @tc.name: TestOnBind
335 * @tc.desc: Test OnBind
336 * @tc.type: FUNC
337 * @tc.require:
338 */
339 HWTEST_F(DsoftbusAdapterTest, OnBind, TestSize.Level1)
340 {
341 CALL_TEST_DEBUG;
342 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
343 PeerSocketInfo info;
344 char deviceId[] = "softbus";
345 info.networkId = deviceId;
346 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info));
347 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->OnShutdown(SOCKET, SHUTDOWN_REASON_UNKNOWN));
348 RemovePermission();
349 }
350
351 /**
352 * @tc.name: TestOnBytes
353 * @tc.desc: Test OnBytes
354 * @tc.type: FUNC
355 * @tc.require:
356 */
357 HWTEST_F(DsoftbusAdapterTest, OnBytes, TestSize.Level1)
358 {
359 CALL_TEST_DEBUG;
360 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
361 int32_t *data = new int32_t(SOCKET);
362 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->OnBytes(SOCKET, data, sizeof(data)));
363 RemovePermission();
364 }
365
366 /**
367 * @tc.name: TestHandleSessionData
368 * @tc.desc: Test HandleSessionData
369 * @tc.type: FUNC
370 * @tc.require:
371 */
372 HWTEST_F(DsoftbusAdapterTest, HandleSessionData, TestSize.Level1)
373 {
374 CALL_TEST_DEBUG;
375 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
376 std::string networkId("softbus");
377 CircleStreamBuffer circleBuffer;
378 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->HandleSessionData(networkId, circleBuffer));
379 RemovePermission();
380 }
381
382 /**
383 * @tc.name: TestHandleRawData
384 * @tc.desc: Test HandleRawData
385 * @tc.type: FUNC
386 * @tc.require:
387 */
388 HWTEST_F(DsoftbusAdapterTest, HandleRawData, TestSize.Level1)
389 {
390 CALL_TEST_DEBUG;
391 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
392 std::string networkId("softbus");
393 int32_t *data = new int32_t(SOCKET);
394 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->HandleRawData(networkId, data, sizeof(data)));
395 RemovePermission();
396 }
397
398 /**
399 * @tc.name: TestCloseAllSessions
400 * @tc.desc: Test CloseAllSessions
401 * @tc.type: FUNC
402 * @tc.require:
403 */
404 HWTEST_F(DsoftbusAdapterTest, TestCloseAllSessions, TestSize.Level1)
405 {
406 CALL_TEST_DEBUG;
407 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
408 DSoftbusAdapter dSoftbusAdapter;
409 ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.CloseAllSessions());
410 RemovePermission();
411 }
412
413 /**
414 * @tc.name: TestDestroyInstance
415 * @tc.desc: Test Destroy Instance
416 * @tc.type: FUNC
417 * @tc.require:
418 */
419 HWTEST_F(DsoftbusAdapterTest, TestDestroyInstance, TestSize.Level1)
420 {
421 CALL_TEST_DEBUG;
422 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
423 DSoftbusAdapter dSoftbusAdapter;
424 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::DestroyInstance());
425 RemovePermission();
426 }
427
428 /**
429 * @tc.name: TestDestroyInstance
430 * @tc.desc: Test SendPacket
431 * @tc.type: FUNC
432 * @tc.require:
433 */
434 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest01, TestSize.Level1)
435 {
436 CALL_TEST_DEBUG;
437 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
438 PeerSocketInfo info;
439 char deviceId[] = "softbus";
440 info.networkId = deviceId;
441 DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info);
442 std::string networkId("softbus");
443 NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
444 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->SendPacket(networkId, packet));
445 RemovePermission();
446 }
447
448 /**
449 * @tc.name: TestSendParcel
450 * @tc.desc: Test SendParcel
451 * @tc.type: FUNC
452 * @tc.require:
453 */
454 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest02, TestSize.Level1)
455 {
456 CALL_TEST_DEBUG;
457 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
458 PeerSocketInfo info;
459 char deviceId[] = "softbus";
460 info.networkId = deviceId;
461 DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info);
462 std::string networkId("softbus");
463 Parcel parcel;
464 ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->SendParcel(networkId, parcel));
465 RemovePermission();
466 }
467
468 /**
469 * @tc.name: TestSendParcel
470 * @tc.desc: Test BroadcastPacket
471 * @tc.type: FUNC
472 * @tc.require:
473 */
474 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest03, TestSize.Level1)
475 {
476 CALL_TEST_DEBUG;
477 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
478 NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
479 int32_t ret = DSoftbusAdapterImpl::GetInstance()->BroadcastPacket(packet);
480 EXPECT_EQ(ret, RET_OK);
481 PeerSocketInfo info;
482 char deviceId[] = "softbus";
483 info.networkId = deviceId;
484 DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info);
485 ret = DSoftbusAdapterImpl::GetInstance()->BroadcastPacket(packet);
486 EXPECT_EQ(ret, RET_OK);
487 RemovePermission();
488 }
489
490 /**
491 * @tc.name: TestSendParcel
492 * @tc.desc: Test InitSocket
493 * @tc.type: FUNC
494 * @tc.require:
495 */
496 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest04, TestSize.Level1)
497 {
498 CALL_TEST_DEBUG;
499 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
500 char name[DEVICE_NAME_SIZE_MAX] { SERVER_SESSION_NAME };
501 char pkgName[PKG_NAME_SIZE_MAX] { FI_PKG_NAME };
502 SocketInfo info { .name = name, .pkgName = pkgName, .dataType = DATA_TYPE_BYTES };
503 int32_t socket = 1;
504 int32_t ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_CLIENT, socket);
505 ASSERT_EQ(ret, RET_ERR);
506 ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_SERVER, socket);
507 ASSERT_EQ(ret, RET_ERR);
508 RemovePermission();
509 }
510
511 /**
512 * @tc.name: TestCheckDeviceOnline
513 * @tc.desc: Test CheckDeviceOnline
514 * @tc.type: FUNC
515 * @tc.require:
516 */
517 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest05, TestSize.Level1)
518 {
519 CALL_TEST_DEBUG;
520 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
521 bool ret = DSoftbusAdapterImpl::GetInstance()->CheckDeviceOnline("networkId");
522 EXPECT_FALSE(ret);
523 std::string NetworkId = GetLocalNetworkId();
524 ret = DSoftbusAdapterImpl::GetInstance()->CheckDeviceOnline(NetworkId);
525 EXPECT_FALSE(ret);
526 RemovePermission();
527 }
528 } // namespace DeviceStatus
529 } // namespace Msdp
530 } // namespace OHOS