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 <gtest/gtest.h>
25 #include "nativetoken_kit.h"
26 #include "securec.h"
27 #include "token_setproc.h"
28 
29 #include "devicestatus_define.h"
30 #include "devicestatus_errors.h"
31 #include "dsoftbus_adapter_impl.h"
32 #include "dsoftbus_adapter.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 {
321         .name = name,
322         .peerName = peerName,
323         .peerNetworkId = peerNetworkId,
324         .pkgName = pkgName,
325         .dataType = DATA_TYPE_BYTES
326     };
327     int32_t socket = 1;
328     int32_t ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_CLIENT, socket);
329     ASSERT_EQ(ret, RET_ERR);
330     ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_SERVER, socket);
331     ASSERT_EQ(ret, RET_ERR);
332     RemovePermission();
333 }
334 
335 /**
336  * @tc.name: TestOnBind
337  * @tc.desc: Test OnBind
338  * @tc.type: FUNC
339  * @tc.require:
340  */
341 HWTEST_F(DsoftbusAdapterTest, OnBind, TestSize.Level1)
342 {
343     CALL_TEST_DEBUG;
344     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
345     PeerSocketInfo info;
346     char deviceId[] = "softbus";
347     info.networkId = deviceId;
348     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info));
349     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->OnShutdown(SOCKET, SHUTDOWN_REASON_UNKNOWN));
350     RemovePermission();
351 }
352 
353 /**
354  * @tc.name: TestOnBytes
355  * @tc.desc: Test OnBytes
356  * @tc.type: FUNC
357  * @tc.require:
358  */
359 HWTEST_F(DsoftbusAdapterTest, OnBytes, TestSize.Level1)
360 {
361     CALL_TEST_DEBUG;
362     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
363     int32_t *data = new int32_t(SOCKET);
364     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->OnBytes(SOCKET, data, sizeof(data)));
365     RemovePermission();
366 }
367 
368 /**
369  * @tc.name: TestHandleSessionData
370  * @tc.desc: Test HandleSessionData
371  * @tc.type: FUNC
372  * @tc.require:
373  */
374 HWTEST_F(DsoftbusAdapterTest, HandleSessionData, TestSize.Level1)
375 {
376     CALL_TEST_DEBUG;
377     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
378     std::string networkId("softbus");
379     CircleStreamBuffer circleBuffer;
380     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->HandleSessionData(networkId, circleBuffer));
381     RemovePermission();
382 }
383 
384 /**
385  * @tc.name: TestHandleRawData
386  * @tc.desc: Test HandleRawData
387  * @tc.type: FUNC
388  * @tc.require:
389  */
390 HWTEST_F(DsoftbusAdapterTest, HandleRawData, TestSize.Level1)
391 {
392     CALL_TEST_DEBUG;
393     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
394     std::string networkId("softbus");
395     int32_t *data = new int32_t(SOCKET);
396     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->HandleRawData(networkId, data, sizeof(data)));
397     RemovePermission();
398 }
399 
400 /**
401  * @tc.name: TestCloseAllSessions
402  * @tc.desc: Test CloseAllSessions
403  * @tc.type: FUNC
404  * @tc.require:
405  */
406 HWTEST_F(DsoftbusAdapterTest, TestCloseAllSessions, TestSize.Level1)
407 {
408     CALL_TEST_DEBUG;
409     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
410     DSoftbusAdapter dSoftbusAdapter;
411     ASSERT_NO_FATAL_FAILURE(dSoftbusAdapter.CloseAllSessions());
412     RemovePermission();
413 }
414 
415 /**
416  * @tc.name: TestDestroyInstance
417  * @tc.desc: Test Destroy Instance
418  * @tc.type: FUNC
419  * @tc.require:
420  */
421 HWTEST_F(DsoftbusAdapterTest, TestDestroyInstance, TestSize.Level1)
422 {
423     CALL_TEST_DEBUG;
424     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
425     DSoftbusAdapter dSoftbusAdapter;
426     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::DestroyInstance());
427     RemovePermission();
428 }
429 
430 /**
431  * @tc.name: TestDestroyInstance
432  * @tc.desc: Test SendPacket
433  * @tc.type: FUNC
434  * @tc.require:
435  */
436 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest01, TestSize.Level1)
437 {
438     CALL_TEST_DEBUG;
439     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
440     PeerSocketInfo info;
441     char deviceId[] = "softbus";
442     info.networkId = deviceId;
443     DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info);
444     std::string networkId("softbus");
445     NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
446     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->SendPacket(networkId, packet));
447     RemovePermission();
448 }
449 
450 /**
451  * @tc.name: TestSendParcel
452  * @tc.desc: Test SendParcel
453  * @tc.type: FUNC
454  * @tc.require:
455  */
456 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest02, TestSize.Level1)
457 {
458     CALL_TEST_DEBUG;
459     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
460     PeerSocketInfo info;
461     char deviceId[] = "softbus";
462     info.networkId = deviceId;
463     DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info);
464     std::string networkId("softbus");
465     Parcel parcel;
466     ASSERT_NO_FATAL_FAILURE(DSoftbusAdapterImpl::GetInstance()->SendParcel(networkId, parcel));
467     RemovePermission();
468 }
469 
470 /**
471  * @tc.name: TestSendParcel
472  * @tc.desc: Test BroadcastPacket
473  * @tc.type: FUNC
474  * @tc.require:
475  */
476 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest03, TestSize.Level1)
477 {
478     CALL_TEST_DEBUG;
479     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
480     NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
481     int32_t ret = DSoftbusAdapterImpl::GetInstance()->BroadcastPacket(packet);
482     EXPECT_EQ(ret, RET_OK);
483     PeerSocketInfo info;
484     char deviceId[] = "softbus";
485     info.networkId = deviceId;
486     DSoftbusAdapterImpl::GetInstance()->OnBind(SOCKET, info);
487     ret = DSoftbusAdapterImpl::GetInstance()->BroadcastPacket(packet);
488     EXPECT_EQ(ret, RET_OK);
489     RemovePermission();
490 }
491 
492 /**
493  * @tc.name: TestSendParcel
494  * @tc.desc: Test InitSocket
495  * @tc.type: FUNC
496  * @tc.require:
497  */
498 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest04, TestSize.Level1)
499 {
500     CALL_TEST_DEBUG;
501     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
502     char name[DEVICE_NAME_SIZE_MAX] { SERVER_SESSION_NAME };
503     char pkgName[PKG_NAME_SIZE_MAX] { FI_PKG_NAME };
504     SocketInfo info {
505         .name = name,
506         .pkgName = pkgName,
507         .dataType = DATA_TYPE_BYTES
508     };
509     int32_t socket = 1;
510     int32_t ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_CLIENT, socket);
511     ASSERT_EQ(ret, RET_ERR);
512     ret = DSoftbusAdapterImpl::GetInstance()->InitSocket(info, SOCKET_SERVER, socket);
513     ASSERT_EQ(ret, RET_ERR);
514     RemovePermission();
515 }
516 
517 /**
518  * @tc.name: TestCheckDeviceOnline
519  * @tc.desc: Test CheckDeviceOnline
520  * @tc.type: FUNC
521  * @tc.require:
522  */
523 HWTEST_F(DsoftbusAdapterTest, DsoftbusAdapterTest05, TestSize.Level1)
524 {
525     CALL_TEST_DEBUG;
526     SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
527     bool ret = DSoftbusAdapterImpl::GetInstance()->CheckDeviceOnline("networkId");
528     EXPECT_FALSE(ret);
529     std::string NetworkId = GetLocalNetworkId();
530     ret = DSoftbusAdapterImpl::GetInstance()->CheckDeviceOnline(NetworkId);
531     EXPECT_FALSE(ret);
532     RemovePermission();
533 }
534 } // namespace DeviceStatus
535 } // namespace Msdp
536 } // namespace OHOS