1 /*
2  * Copyright (c) 2022 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 <cstring>
17 #include "securec.h"
18 #define private public
19 #include "dbinder_service.h"
20 #undef private
21 #include "dbinder_remote_listener.h"
22 #include "gtest/gtest.h"
23 #include "rpc_feature_set.h"
24 #include "rpc_log.h"
25 #include "log_tags.h"
26 #include "string_ex.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::HiviewDFX;
31 
32 namespace {
33 constexpr int TEST_STUB_INDEX = 1234;
34 }
35 
36 class DBinderServiceUnitTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42     static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "DBinderServiceUnitTest" };
43 };
44 
SetUp()45 void DBinderServiceUnitTest::SetUp() {}
46 
TearDown()47 void DBinderServiceUnitTest::TearDown() {}
48 
SetUpTestCase()49 void DBinderServiceUnitTest::SetUpTestCase() {}
50 
TearDownTestCase()51 void DBinderServiceUnitTest::TearDownTestCase() {}
52 
53 class TestDeathRecipient : public IRemoteObject::DeathRecipient {
54 public:
TestDeathRecipient()55     TestDeathRecipient() {}
~TestDeathRecipient()56     virtual ~TestDeathRecipient() {}
OnRemoteDied(const wptr<IRemoteObject> & object)57     void OnRemoteDied(const wptr<IRemoteObject>& object) override {}
58 };
59 
60 class TestRpcSystemAbilityCallback : public RpcSystemAbilityCallback {
61 public:
GetSystemAbilityFromRemote(int32_t systemAbilityId)62     sptr<IRemoteObject> GetSystemAbilityFromRemote(int32_t systemAbilityId) override
63     {
64         return nullptr;
65     }
66 
LoadSystemAbilityFromRemote(const std::string & srcNetworkId,int32_t systemAbilityId)67     bool LoadSystemAbilityFromRemote(const std::string& srcNetworkId, int32_t systemAbilityId) override
68     {
69         return true;
70     }
IsDistributedSystemAbility(int32_t systemAbilityId)71     bool IsDistributedSystemAbility(int32_t systemAbilityId) override
72     {
73         return true;
74     }
75 };
76 
77 /*
78  * @tc.name: ProcessOnSessionClosed001
79  * @tc.desc: Verify the ProcessOnSessionClosed function
80  * @tc.type: FUNC
81  */
82 HWTEST_F(DBinderServiceUnitTest, ProcessOnSessionClosed001, TestSize.Level1)
83 {
84     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
85     EXPECT_TRUE(dBinderService != nullptr);
86     std::string networkId = "1234567890";
87     EXPECT_EQ(dBinderService->ProcessOnSessionClosed(networkId), true);
88 }
89 
90 /*
91  * @tc.name: ProcessOnSessionClosed002
92  * @tc.desc: Verify the ProcessOnSessionClosed function
93  * @tc.type: FUNC
94  */
95 HWTEST_F(DBinderServiceUnitTest, ProcessOnSessionClosed002, TestSize.Level1)
96 {
97     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
98     EXPECT_TRUE(dBinderService != nullptr);
99     std::string networkId = "";
100     EXPECT_EQ(dBinderService->ProcessOnSessionClosed(networkId), true);
101 }
102 
103 /**
104  * @tc.name: StartDBinderService001
105  * @tc.desc: Verify the StartDBinderService function
106  * @tc.type: FUNC
107  */
108 HWTEST_F(DBinderServiceUnitTest, StartDBinderService001, TestSize.Level1)
109 {
110     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
111     EXPECT_TRUE(dBinderService != nullptr);
112     std::shared_ptr<RpcSystemAbilityCallback> callbackImpl = nullptr;
113     bool res = dBinderService->StartDBinderService(callbackImpl);
114     EXPECT_EQ(res, false);
115 }
116 
117 /**
118  * @tc.name: StartDBinderService002
119  * @tc.desc: Verify the StartDBinderService function
120  * @tc.type: FUNC
121  */
122 HWTEST_F(DBinderServiceUnitTest, StartDBinderService002, TestSize.Level1)
123 {
124     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
125     EXPECT_TRUE(dBinderService != nullptr);
126     std::shared_ptr<RpcSystemAbilityCallback> callbackImpl = nullptr;
127     DBinderService::mainThreadCreated_ = true;
128     bool res = dBinderService->StartDBinderService(callbackImpl);
129     EXPECT_EQ(res, false);
130 }
131 
132 /**
133  * @tc.name: StartDBinderService003
134  * @tc.desc: Verify the StartDBinderService function
135  * @tc.type: FUNC
136  */
137 HWTEST_F(DBinderServiceUnitTest, StartDBinderService003, TestSize.Level1)
138 {
139     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
140     EXPECT_TRUE(dBinderService != nullptr);
141     std::shared_ptr<RpcSystemAbilityCallback> callbackImpl = nullptr;
142     DBinderService::mainThreadCreated_ = false;
143     dBinderService->remoteListener_ = nullptr;
144     bool res = dBinderService->StartDBinderService(callbackImpl);
145     EXPECT_EQ(res, false);
146 }
147 
148 /**
149  * @tc.name: StartDBinderService004
150  * @tc.desc: Verify the StartDBinderService function
151  * @tc.type: FUNC
152  */
153 HWTEST_F(DBinderServiceUnitTest, StartDBinderService004, TestSize.Level1)
154 {
155     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
156     EXPECT_TRUE(dBinderService != nullptr);
157     std::shared_ptr<RpcSystemAbilityCallback> callbackImpl = nullptr;
158     DBinderService::mainThreadCreated_ = false;
159     dBinderService->remoteListener_ = std::make_shared<DBinderRemoteListener>();
160     EXPECT_TRUE(dBinderService->remoteListener_ != nullptr);
161     bool res = dBinderService->StartDBinderService(callbackImpl);
162     EXPECT_EQ(res, true);
163 }
164 
165 /**
166  * @tc.name: ReStartRemoteListener001
167  * @tc.desc: Verify the ReStartRemoteListener function
168  * @tc.type: FUNC
169  */
170 HWTEST_F(DBinderServiceUnitTest, ReStartRemoteListener001, TestSize.Level1)
171 {
172     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
173     EXPECT_TRUE(dBinderService != nullptr);
174     dBinderService->remoteListener_ = nullptr;
175     bool res = dBinderService->ReStartRemoteListener();
176     EXPECT_EQ(res, false);
177 }
178 
179 /**
180  * @tc.name: ReStartRemoteListener002
181  * @tc.desc: Verify the ReStartRemoteListener function
182  * @tc.type: FUNC
183  */
184 HWTEST_F(DBinderServiceUnitTest, ReStartRemoteListener002, TestSize.Level1)
185 {
186     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
187     EXPECT_TRUE(dBinderService != nullptr);
188     dBinderService->remoteListener_ = std::make_shared<DBinderRemoteListener>();
189     EXPECT_TRUE(dBinderService->remoteListener_ != nullptr);
190     bool res = dBinderService->ReStartRemoteListener();
191     EXPECT_EQ(res, false);
192 }
193 
194 /**
195  * @tc.name: StartRemoteListener001
196  * @tc.desc: Verify the StartRemoteListener function
197  * @tc.type: FUNC
198  */
199 HWTEST_F(DBinderServiceUnitTest, StartRemoteListener001, TestSize.Level1)
200 {
201     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
202     EXPECT_TRUE(dBinderService != nullptr);
203     dBinderService->remoteListener_ = nullptr;
204     bool res = dBinderService->StartRemoteListener();
205     EXPECT_EQ(res, false);
206 }
207 
208 /**
209  * @tc.name: StartRemoteListener002
210  * @tc.desc: Verify the StartRemoteListener function
211  * @tc.type: FUNC
212  */
213 HWTEST_F(DBinderServiceUnitTest, StartRemoteListener002, TestSize.Level1)
214 {
215     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
216     EXPECT_TRUE(dBinderService != nullptr);
217     dBinderService->remoteListener_ = std::make_shared<DBinderRemoteListener>();
218     EXPECT_TRUE(dBinderService->remoteListener_ != nullptr);
219     bool res = dBinderService->StartRemoteListener();
220     EXPECT_EQ(res, true);
221 }
222 
223 /**
224  * @tc.name: RegisterRemoteProxy001
225  * @tc.desc: Verify the RegisterRemoteProxy function
226  * @tc.type: FUNC
227  */
228 HWTEST_F(DBinderServiceUnitTest, RegisterRemoteProxy001, TestSize.Level1)
229 {
230     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
231     EXPECT_TRUE(dBinderService != nullptr);
232     std::u16string serviceName = std::u16string();
233     sptr<IRemoteObject> binderObject = nullptr;
234     bool res = dBinderService->RegisterRemoteProxy(serviceName, binderObject);
235     EXPECT_EQ(res, false);
236 }
237 
238 /**
239  * @tc.name: RegisterRemoteProxy002
240  * @tc.desc: Verify the RegisterRemoteProxy function
241  * @tc.type: FUNC
242  */
243 HWTEST_F(DBinderServiceUnitTest, RegisterRemoteProxy002, TestSize.Level1)
244 {
245     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
246     EXPECT_TRUE(dBinderService != nullptr);
247     std::u16string serviceName = std::u16string();
248     int32_t systemAbilityId = 0;
249     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, systemAbilityId), false);
250 }
251 
252 /**
253  * @tc.name: QuerySessionObject001
254  * @tc.desc: Verify the QuerySessionObject function
255  * @tc.type: FUNC
256  */
257 HWTEST_F(DBinderServiceUnitTest, QuerySessionObject001, TestSize.Level1)
258 {
259     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
260     EXPECT_TRUE(dBinderService != nullptr);
261     binder_uintptr_t stub = 0;
262     std::shared_ptr<struct SessionInfo> testSession = nullptr;
263     testSession = dBinderService->QuerySessionObject(stub);
264     EXPECT_EQ(testSession, nullptr);
265 }
266 
267 /**
268  * @tc.name: QuerySessionObject002
269  * @tc.desc: Verify the QuerySessionObject function
270  * @tc.type: FUNC
271  */
272 HWTEST_F(DBinderServiceUnitTest, QuerySessionObject002, TestSize.Level1)
273 {
274     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
275     EXPECT_TRUE(dBinderService != nullptr);
276     binder_uintptr_t stub = 0;
277     std::shared_ptr<struct SessionInfo> Session = nullptr;
278     EXPECT_EQ(dBinderService->AttachSessionObject(Session, stub), true);
279     std::shared_ptr<struct SessionInfo> testSession = dBinderService->QuerySessionObject(stub);
280     EXPECT_EQ(testSession, Session);
281 }
282 
283 /**
284  * @tc.name: AttachDeathRecipient001
285  * @tc.desc: Verify the AttachDeathRecipient function
286  * @tc.type: FUNC
287  */
288 HWTEST_F(DBinderServiceUnitTest, AttachDeathRecipient001, TestSize.Level1)
289 {
290     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
291     EXPECT_TRUE(dBinderService != nullptr);
292     sptr<IRemoteObject> object = nullptr;
293     sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
294     bool res = dBinderService->AttachDeathRecipient(object, deathRecipient);
295     EXPECT_TRUE(res);
296 }
297 
298 /**
299  * @tc.name: AttachCallbackProxy001
300  * @tc.desc: Verify the AttachCallbackProxy function
301  * @tc.type: FUNC
302  */
303 HWTEST_F(DBinderServiceUnitTest, AttachCallbackProxy001, TestSize.Level1)
304 {
305     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
306     EXPECT_TRUE(dBinderService != nullptr);
307     sptr<IRemoteObject> object = nullptr;
308     DBinderServiceStub *dbStub = nullptr;
309     bool res = dBinderService->AttachCallbackProxy(object, dbStub);
310     EXPECT_TRUE(res);
311 }
312 
313 /**
314  * @tc.name: DetachProxyObject001
315  * @tc.desc: Verify the DetachProxyObject function
316  * @tc.type: FUNC
317  */
318 HWTEST_F(DBinderServiceUnitTest, DetachProxyObject001, TestSize.Level1)
319 {
320     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
321     EXPECT_TRUE(dBinderService != nullptr);
322     binder_uintptr_t binderObject = 0;
323     bool res = dBinderService->DetachProxyObject(binderObject);
324     EXPECT_EQ(res, false);
325 }
326 
327 /**
328  * @tc.name: ConvertToSecureDeviceIDTest001
329  * @tc.desc: Verify the ConvertToSecureDeviceID function
330  * @tc.type: FUNC
331  */
332 HWTEST_F(DBinderServiceUnitTest, ConvertToSecureDeviceIDTest001, TestSize.Level1)
333 {
334     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
335     EXPECT_TRUE(dBinderService != nullptr);
336     std::string deviceID;
337     EXPECT_EQ(dBinderService->ConvertToSecureDeviceID(deviceID), "****");
338 }
339 
340 /**
341  * @tc.name: ConvertToSecureDeviceIDTest002
342  * @tc.desc: Verify the ConvertToSecureDeviceID function
343  * @tc.type: FUNC
344  */
345 HWTEST_F(DBinderServiceUnitTest, ConvertToSecureDeviceIDTest002, TestSize.Level1)
346 {
347     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
348     EXPECT_TRUE(dBinderService != nullptr);
349     std::string deviceID("123456");
350     EXPECT_EQ(dBinderService->ConvertToSecureDeviceID(deviceID),
351     deviceID.substr(0, ENCRYPT_LENGTH) + "****" + deviceID.substr(strlen(deviceID.c_str()) - ENCRYPT_LENGTH));
352 }
353 
354 /**
355  * @tc.name: GetRemoteTransTypeTest003
356  * @tc.desc: Verify the GetRemoteTransType function
357  * @tc.type: FUNC
358  */
359 HWTEST_F(DBinderServiceUnitTest, GetRemoteTransTypeTest003, TestSize.Level1)
360 {
361     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
362     EXPECT_TRUE(dBinderService != nullptr);
363     EXPECT_EQ(dBinderService->GetRemoteTransType(), IRemoteObject::DATABUS_TYPE);
364 }
365 
366 /**
367  * @tc.name: StopRemoteListener001
368  * @tc.desc: Verify the StopRemoteListener function
369  * @tc.type: FUNC
370  */
371 HWTEST_F(DBinderServiceUnitTest, StopRemoteListener001, TestSize.Level1)
372 {
373     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
374     EXPECT_TRUE(dBinderService != nullptr);
375     std::shared_ptr<DBinderRemoteListener> testListener = std::make_shared<DBinderRemoteListener>();
376     EXPECT_TRUE(testListener != nullptr);
377     EXPECT_EQ(dBinderService->StartRemoteListener(), true);
378     dBinderService->StopRemoteListener();
379 }
380 
381 /**
382  * @tc.name: GetRemoteTransType001
383  * @tc.desc: Verify the GetRemoteTransType function
384  * @tc.type: FUNC
385  */
386 HWTEST_F(DBinderServiceUnitTest, GetRemoteListener001, TestSize.Level1)
387 {
388     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
389     EXPECT_TRUE(dBinderService != nullptr);
390     std::shared_ptr<DBinderRemoteListener> testDbinder = nullptr;
391     testDbinder = dBinderService->GetRemoteListener();
392     EXPECT_EQ(testDbinder, nullptr);
393 }
394 
395 /**
396  * @tc.name: GetRemoteListener002
397  * @tc.desc: Verify the GetRemoteListener function
398  * @tc.type: FUNC
399  */
400 HWTEST_F(DBinderServiceUnitTest, GetRemoteListener002, TestSize.Level1)
401 {
402     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
403     EXPECT_TRUE(dBinderService != nullptr);
404     std::shared_ptr<DBinderRemoteListener> testListener = std::make_shared<DBinderRemoteListener>();
405     EXPECT_TRUE(testListener != nullptr);
406     EXPECT_EQ(dBinderService->StartRemoteListener(), false);
407     std::shared_ptr<DBinderRemoteListener> testDbinder = nullptr;
408     testDbinder = dBinderService->GetRemoteListener();
409 }
410 
411 /**
412  * @tc.name: GetSeqNumber001
413  * @tc.desc: Verify the GetSeqNumber function
414  * @tc.type: FUNC
415  */
416 HWTEST_F(DBinderServiceUnitTest, GetSeqNumber001, TestSize.Level1)
417 {
418     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
419     EXPECT_TRUE(dBinderService != nullptr);
420     dBinderService->seqNumber_ = 0;
421     uint32_t ret = dBinderService->GetSeqNumber();
422     EXPECT_EQ(ret, dBinderService->seqNumber_++);
423 }
424 
425 /**
426  * @tc.name: IsDeviceIdIllegal001
427  * @tc.desc: Verify the IsDeviceIdIllegal function
428  * @tc.type: FUNC
429  */
430 HWTEST_F(DBinderServiceUnitTest, IsDeviceIdIllegal001, TestSize.Level1)
431 {
432     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
433     EXPECT_TRUE(dBinderService != nullptr);
434     std::string deviceID = "";
435     bool res = dBinderService->IsDeviceIdIllegal(deviceID);
436     EXPECT_EQ(res, true);
437 }
438 
439 /**
440  * @tc.name: IsDeviceIdIllegal002
441  * @tc.desc: Verify the IsDeviceIdIllegal function
442  * @tc.type: FUNC
443  */
444 HWTEST_F(DBinderServiceUnitTest, IsDeviceIdIllegal002, TestSize.Level1)
445 {
446     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
447     EXPECT_TRUE(dBinderService != nullptr);
448     std::string deviceID = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
449     bool res = dBinderService->IsDeviceIdIllegal(deviceID);
450     EXPECT_EQ(res, true);
451 }
452 
453 /**
454  * @tc.name: IsDeviceIdIllegal003
455  * @tc.desc: Verify the IsDeviceIdIllegal function
456  * @tc.type: FUNC
457  */
458 HWTEST_F(DBinderServiceUnitTest, IsDeviceIdIllegal003, TestSize.Level1)
459 {
460     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
461     EXPECT_TRUE(dBinderService != nullptr);
462     std::string deviceID = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
463     bool res = dBinderService->IsDeviceIdIllegal(deviceID);
464     EXPECT_EQ(res, false);
465 }
466 
467 /**
468  * @tc.name: CheckBinderObject001
469  * @tc.desc: Verify the CheckBinderObject function
470  * @tc.type: FUNC
471  */
472 HWTEST_F(DBinderServiceUnitTest, CheckBinderObject001, TestSize.Level1)
473 {
474     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
475     EXPECT_TRUE(dBinderService != nullptr);
476     sptr<DBinderServiceStub> stub = nullptr;
477     binder_uintptr_t binderObject = 1564618;
478     bool res = dBinderService->CheckBinderObject(stub, binderObject);
479     EXPECT_EQ(res, false);
480 }
481 
482 /**
483  * @tc.name: CheckBinderObject002
484  * @tc.desc: Verify the CheckBinderObject function
485  * @tc.type: FUNC
486  */
487 HWTEST_F(DBinderServiceUnitTest, CheckBinderObject002, TestSize.Level1)
488 {
489     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
490     EXPECT_TRUE(dBinderService != nullptr);
491     const std::string serviceName = "abc";
492     const std::string deviceID = "bcd";
493     binder_uintptr_t binderObject = 1564618;
494     sptr<DBinderServiceStub> stub = new DBinderServiceStub(serviceName, deviceID, binderObject);
495     EXPECT_TRUE(stub != nullptr);
496     bool res = dBinderService->CheckBinderObject(stub, binderObject);
497     EXPECT_EQ(res, false);
498 }
499 
500 /**
501  * @tc.name: IsSameStubObject001
502  * @tc.desc: Verify the IsSameStubObject function
503  * @tc.type: FUNC
504  */
505 HWTEST_F(DBinderServiceUnitTest, IsSameStubObject001, TestSize.Level1)
506 {
507     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
508     EXPECT_TRUE(dBinderService != nullptr);
509     sptr<DBinderServiceStub> stub = nullptr;
510     std::u16string service = std::u16string();
511     const std::string device = "";
512     bool res = dBinderService->IsSameStubObject(stub, service, device);
513     EXPECT_EQ(res, false);
514 }
515 
516 /**
517  * @tc.name: MakeRemoteBinder001
518  * @tc.desc: Verify the MakeRemoteBinder function
519  * @tc.type: FUNC
520  */
521 HWTEST_F(DBinderServiceUnitTest, MakeRemoteBinder001, TestSize.Level1)
522 {
523     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
524     EXPECT_TRUE(dBinderService != nullptr);
525     std::u16string serviceName = std::u16string();
526     std::string deviceID = "";
527     binder_uintptr_t binderObject = 12345;
528     uint32_t pid = 0;
529     uint32_t uid = 0;
530     EXPECT_EQ(dBinderService->MakeRemoteBinder(serviceName, deviceID, binderObject, pid, uid), nullptr);
531 }
532 
533 /**
534  * @tc.name: MakeRemoteBinder002
535  * @tc.desc: Verify the MakeRemoteBinder function
536  * @tc.type: FUNC
537  */
538 HWTEST_F(DBinderServiceUnitTest, MakeRemoteBinder002, TestSize.Level1)
539 {
540     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
541     EXPECT_TRUE(dBinderService != nullptr);
542     std::u16string serviceName;
543     std::string deviceID("001");
544     binder_uintptr_t binderObject = 12345;
545     uint32_t pid = 0;
546     uint32_t uid = 0;
547     EXPECT_EQ(dBinderService->MakeRemoteBinder(serviceName, deviceID, binderObject, pid, uid), nullptr);
548 }
549 
550 /**
551  * @tc.name: MakeRemoteBinderTest003
552  * @tc.desc: Verify the MakeRemoteBinder function
553  * @tc.type: FUNC
554  */
555 HWTEST_F(DBinderServiceUnitTest, MakeRemoteBinderTest003, TestSize.Level1)
556 {
557     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
558     EXPECT_TRUE(dBinderService != nullptr);
559     std::u16string serviceName;
560     std::string deviceID("001");
561     binder_uintptr_t binderObject = 12345;
562     uint32_t pid = 10;
563     uint32_t uid = 10;
564     EXPECT_EQ(dBinderService->MakeRemoteBinder(serviceName, deviceID, binderObject, pid, uid), nullptr);
565 }
566 
567 /**
568  * @tc.name: SendEntryToRemote001
569  * @tc.desc: Verify the SendEntryToRemote function
570  * @tc.type: FUNC
571  */
572 HWTEST_F(DBinderServiceUnitTest, SendEntryToRemote001, TestSize.Level1)
573 {
574     std::string serviceName = "testServiceName";
575     std::string deviceID = "testDeviceID";
576     binder_uintptr_t binderObject = 161561;
577     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
578     EXPECT_TRUE(dBinderService != nullptr);
579     sptr<DBinderServiceStub> stub = new DBinderServiceStub(serviceName, deviceID, binderObject);
580     EXPECT_TRUE(stub != nullptr);
581     uint32_t seqNumber = 0;
582     uint32_t pid = 0;
583     uint32_t uid = 0;
584     bool res = dBinderService->SendEntryToRemote(stub, seqNumber, pid, uid);
585     EXPECT_EQ(res, false);
586 }
587 
588 /**
589  * @tc.name: CheckSystemAbilityId001
590  * @tc.desc: Verify the CheckSystemAbilityId function
591  * @tc.type: FUNC
592  */
593 HWTEST_F(DBinderServiceUnitTest, CheckSystemAbilityId001, TestSize.Level1)
594 {
595     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
596     EXPECT_TRUE(dBinderService != nullptr);
597     int32_t systemAbilityId = 0x00000002;
598     bool res = dBinderService->CheckSystemAbilityId(systemAbilityId);
599     EXPECT_EQ(res, true);
600 }
601 
602 /**
603  * @tc.name: AllocFreeSocketPort001
604  * @tc.desc: Verify the AllocFreeSocketPort function
605  * @tc.type: FUNC
606  */
607 HWTEST_F(DBinderServiceUnitTest, AllocFreeSocketPort001, TestSize.Level1)
608 {
609     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
610     EXPECT_TRUE(dBinderService != nullptr);
611     uint16_t ret = dBinderService->AllocFreeSocketPort();
612     EXPECT_EQ(ret, 0);
613 }
614 
615 /**
616  * @tc.name: IsSameLoadSaItem001
617  * @tc.desc: Verify the IsSameLoadSaItem function
618  * @tc.type: FUNC
619  */
620 HWTEST_F(DBinderServiceUnitTest, IsSameLoadSaItem001, TestSize.Level1)
621 {
622     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
623     EXPECT_TRUE(dBinderService != nullptr);
624     std::string srcNetworkId = "aaaaaaaaaaaaaa";
625     int32_t systemAbilityId = 123;
626     std::shared_ptr<DHandleEntryTxRx> loadSaItem = std::make_shared<DHandleEntryTxRx>();
627     EXPECT_TRUE(loadSaItem != nullptr);
628     loadSaItem->stubIndex = 123;
629     strcpy_s(loadSaItem->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH, "aaaaaaaaaaaaaa");
630     bool res = dBinderService->IsSameLoadSaItem(srcNetworkId, systemAbilityId, loadSaItem);
631     EXPECT_EQ(res, true);
632 }
633 
634 /**
635  * @tc.name: IsSameLoadSaItem002
636  * @tc.desc: Verify the IsSameLoadSaItem function
637  * @tc.type: FUNC
638  */
639 HWTEST_F(DBinderServiceUnitTest, IsSameLoadSaItem002, TestSize.Level1)
640 {
641     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
642     EXPECT_TRUE(dBinderService != nullptr);
643     std::string srcNetworkId = "bbbbbbb";
644     int32_t systemAbilityId = 123;
645     std::shared_ptr<DHandleEntryTxRx> loadSaItem = std::make_shared<DHandleEntryTxRx>();
646     EXPECT_TRUE(loadSaItem != nullptr);
647     loadSaItem->stubIndex = 123;
648     strcpy_s(loadSaItem->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH, "aaaaaaaaaaaaaa");
649     bool res = dBinderService->IsSameLoadSaItem(srcNetworkId, systemAbilityId, loadSaItem);
650     EXPECT_EQ(res, false);
651 }
652 
653 /**
654  * @tc.name: IsSameLoadSaItem003
655  * @tc.desc: Verify the IsSameLoadSaItem function
656  * @tc.type: FUNC
657  */
658 HWTEST_F(DBinderServiceUnitTest, IsSameLoadSaItem003, TestSize.Level1)
659 {
660     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
661     EXPECT_TRUE(dBinderService != nullptr);
662     std::string srcNetworkId = "aaaaaaaaaaaaaa";
663     int32_t systemAbilityId = 321;
664     std::shared_ptr<DHandleEntryTxRx> loadSaItem = std::make_shared<DHandleEntryTxRx>();
665     EXPECT_TRUE(loadSaItem != nullptr);
666     loadSaItem->stubIndex = 123;
667     strcpy_s(loadSaItem->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH, "aaaaaaaaaaaaaa");
668     bool res = dBinderService->IsSameLoadSaItem(srcNetworkId, systemAbilityId, loadSaItem);
669     EXPECT_EQ(res, false);
670 }
671 
672 /**
673  * @tc.name: OnRemoteInvokerMessage002
674  * @tc.desc: Verify the OnRemoteInvokerMessage function
675  * @tc.type: FUNC
676  */
677 HWTEST_F(DBinderServiceUnitTest, OnRemoteInvokerMessage002, TestSize.Level1)
678 {
679     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
680     EXPECT_TRUE(dBinderService != nullptr);
681     std::shared_ptr<struct DHandleEntryTxRx> message = std::make_shared<DHandleEntryTxRx>();
682     EXPECT_TRUE(message != nullptr);
683     memset_s(message.get(), sizeof(DHandleEntryTxRx), 0, sizeof(DHandleEntryTxRx));
684     message->stubIndex = TEST_STUB_INDEX;
685     dBinderService->dbinderCallback_ = std::make_shared<TestRpcSystemAbilityCallback>();
686     EXPECT_TRUE(dBinderService->dbinderCallback_ != nullptr);
687     bool res = dBinderService->OnRemoteInvokerMessage(message);
688     EXPECT_EQ(res, true);
689 }
690 
691 /**
692  * @tc.name: GetDatabusNameByProxyTest001
693  * @tc.desc: Verify the GetDatabusNameByProxy function
694  * @tc.type: FUNC
695  */
696 HWTEST_F(DBinderServiceUnitTest, GetDatabusNameByProxyTest001, TestSize.Level1)
697 {
698     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
699     EXPECT_TRUE(dBinderService != nullptr);
700     IPCObjectProxy* proxy = nullptr;
701     std::string res = dBinderService->GetDatabusNameByProxy(proxy);
702     EXPECT_EQ(res, "");
703     IPCObjectProxy object(16);
704     res = dBinderService->GetDatabusNameByProxy(&object);
705     EXPECT_EQ(res, "");
706 }
707 
708 /**
709  * @tc.name: InvokerRemoteDBinderTest001
710  * @tc.desc: Verify the InvokerRemoteDBinder function
711  * @tc.type: FUNC
712  */
713 HWTEST_F(DBinderServiceUnitTest, InvokerRemoteDBinderTest001, TestSize.Level1)
714 {
715     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
716     EXPECT_TRUE(dBinderService != nullptr);
717     sptr<DBinderServiceStub> stub = nullptr;
718     uint32_t seqNumber = 123456;
719     uint32_t pid = 0;
720     uint32_t uid = 0;
721     int32_t ret = dBinderService->InvokerRemoteDBinder(stub, seqNumber, pid, uid);
722     EXPECT_EQ(ret, DBinderErrorCode::STUB_INVALID);
723     std::string serviceName("testServer");
724     std::string deviceID("123456");
725     binder_uintptr_t binderObject = 100;
726     stub = new DBinderServiceStub(serviceName, deviceID, binderObject);
727     EXPECT_TRUE(stub != nullptr);
728     ret = dBinderService->InvokerRemoteDBinder(stub, seqNumber, pid, uid);
729     EXPECT_EQ(ret, DBinderErrorCode::SEND_MESSAGE_FAILED);
730 }
731 
732 /**
733  * @tc.name: CreateDatabusNameTest001
734  * @tc.desc: Verify the CreateDatabusName function
735  * @tc.type: FUNC
736  */
737 HWTEST_F(DBinderServiceUnitTest, CreateDatabusNameTest001, TestSize.Level1)
738 {
739     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
740     EXPECT_TRUE(dBinderService != nullptr);
741     int pid = 0;
742     int uid = 0;
743     std::string res = dBinderService->CreateDatabusName(pid, uid);
744     EXPECT_EQ(res, "");
745     pid = 10;
746     uid = 10;
747     res = dBinderService->CreateDatabusName(pid, uid);
748     EXPECT_EQ(res, "");
749 }
750 
751 /**
752  * @tc.name: FindServicesByDeviceIDTest001
753  * @tc.desc: Verify the FindServicesByDeviceID function
754  * @tc.type: FUNC
755  */
756 HWTEST_F(DBinderServiceUnitTest, FindServicesByDeviceIDTest001, TestSize.Level1)
757 {
758     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
759     EXPECT_TRUE(dBinderService != nullptr);
760     std::string serviceName("testServer");
761     std::string deviceID("123456");
762     binder_uintptr_t binderObject = 100;
763     sptr<DBinderServiceStub> dBinderServiceStub = new DBinderServiceStub(serviceName, deviceID, binderObject);
764     EXPECT_TRUE(dBinderServiceStub != nullptr);
765     dBinderService->DBinderStubRegisted_.push_back(dBinderServiceStub);
766     std::list<std::u16string> serviceNames;
767     serviceNames.push_back(Str8ToStr16(serviceName));
768     EXPECT_EQ(dBinderService->FindServicesByDeviceID(deviceID), serviceNames);
769 }
770 
771 /**
772  * @tc.name: NoticeDeviceDieTest001
773  * @tc.desc: Verify the NoticeDeviceDie function
774  * @tc.type: FUNC
775  */
776 HWTEST_F(DBinderServiceUnitTest, NoticeDeviceDieTest001, TestSize.Level1)
777 {
778     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
779     EXPECT_TRUE(dBinderService != nullptr);
780     std::string deviceID;
781     EXPECT_EQ(dBinderService->NoticeDeviceDie(deviceID), DBINDER_SERVICE_INVALID_DATA_ERR);
782 }
783 
784 /**
785  * @tc.name: NoticeDeviceDieTest002
786  * @tc.desc: Verify the NoticeDeviceDie function
787  * @tc.type: FUNC
788  */
789 HWTEST_F(DBinderServiceUnitTest, NoticeDeviceDieTest002, TestSize.Level1)
790 {
791     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
792     EXPECT_TRUE(dBinderService != nullptr);
793     std::string deviceID("123456");
794     EXPECT_EQ(dBinderService->NoticeDeviceDie(deviceID), DBINDER_SERVICE_NOTICE_DIE_ERR);
795 }
796 
797 /**
798  * @tc.name: NoticeDeviceDieTest003
799  * @tc.desc: Verify the NoticeDeviceDie function
800  * @tc.type: FUNC
801  */
802 HWTEST_F(DBinderServiceUnitTest, NoticeDeviceDieTest003, TestSize.Level1)
803 {
804     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
805     EXPECT_TRUE(dBinderService != nullptr);
806     std::string deviceID("123456");
807     dBinderService->remoteListener_ = std::make_shared<DBinderRemoteListener>();
808     EXPECT_TRUE(dBinderService->remoteListener_ != nullptr);
809     EXPECT_EQ(dBinderService->NoticeDeviceDie(deviceID), DBINDER_SERVICE_NOTICE_DIE_ERR);
810 }
811 
812 /**
813  * @tc.name: NoticeServiceDieTest001
814  * @tc.desc: Verify the NoticeServiceDie function
815  * @tc.type: FUNC
816  */
817 HWTEST_F(DBinderServiceUnitTest, NoticeServiceDieTest001, TestSize.Level1)
818 {
819     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
820     EXPECT_TRUE(dBinderService != nullptr);
821     dBinderService->StartRemoteListener();
822     std::u16string serviceName;
823     std::string deviceID("123456");
824     EXPECT_EQ(dBinderService->NoticeServiceDie(serviceName, deviceID), DBINDER_SERVICE_INVALID_DATA_ERR);
825 }
826 
827 /**
828  * @tc.name: NoticeServiceDieInnerTest001
829  * @tc.desc: Verify the NoticeServiceDieInner function
830  * @tc.type: FUNC
831  */
832 HWTEST_F(DBinderServiceUnitTest, NoticeServiceDieInnerTest001, TestSize.Level1)
833 {
834     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
835     EXPECT_TRUE(dBinderService != nullptr);
836     dBinderService->StartRemoteListener();
837     std::u16string serviceName;
838     std::string deviceID("123456");
839     EXPECT_EQ(dBinderService->NoticeServiceDieInner(serviceName, deviceID), DBINDER_SERVICE_INVALID_DATA_ERR);
840 }
841 
842 /**
843  * @tc.name: NoticeServiceDieInnerTest002
844  * @tc.desc: Verify the NoticeServiceDieInner function
845  * @tc.type: FUNC
846  */
847 HWTEST_F(DBinderServiceUnitTest, NoticeServiceDieInnerTest002, TestSize.Level1)
848 {
849     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
850     EXPECT_TRUE(dBinderService != nullptr);
851     dBinderService->StartRemoteListener();
852     std::u16string serviceName(u"test");
853     std::string deviceID("123456");
854     EXPECT_EQ(dBinderService->NoticeServiceDieInner(serviceName, deviceID), ERR_NONE);
855 }
856 
857 /**
858  * @tc.name: NoticeServiceDieInnerTest003
859  * @tc.desc: Verify the NoticeServiceDieInner function
860  * @tc.type: FUNC
861  */
862 HWTEST_F(DBinderServiceUnitTest, NoticeServiceDieInnerTest003, TestSize.Level1)
863 {
864     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
865     EXPECT_TRUE(dBinderService != nullptr);
866     dBinderService->StartRemoteListener();
867     std::u16string serviceName(u"testServer");
868     std::string deviceID("123456");
869     EXPECT_EQ(dBinderService->NoticeServiceDieInner(serviceName, deviceID), ERR_NONE);
870 }
871 
872 /**
873  * @tc.name: ProcessCallbackProxyTest001
874  * @tc.desc: Verify the ProcessCallbackProxy function
875  * @tc.type: FUNC
876  */
877 HWTEST_F(DBinderServiceUnitTest, ProcessCallbackProxyTest001, TestSize.Level1)
878 {
879     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
880     EXPECT_TRUE(dBinderService != nullptr);
881     sptr<IRemoteObject> object = new IPCObjectProxy(16);
882     EXPECT_TRUE(object != nullptr);
883     std::string serviceName("testServer");
884     std::string deviceID("123456");
885     binder_uintptr_t binderObject = 100;
886     sptr<DBinderServiceStub> dBinderServiceStub = new DBinderServiceStub(serviceName, deviceID, binderObject);
887     EXPECT_TRUE(dBinderServiceStub != nullptr);
888     bool res = dBinderService->AttachCallbackProxy(object, dBinderServiceStub.GetRefPtr());
889     dBinderService->ProcessCallbackProxy(dBinderServiceStub);
890     EXPECT_TRUE(res);
891 }
892 
893 /**
894  * @tc.name: NoticeCallbackProxyTest001
895  * @tc.desc: Verify the NoticeCallbackProxy function
896  * @tc.type: FUNC
897  */
898 HWTEST_F(DBinderServiceUnitTest, NoticeCallbackProxyTest001, TestSize.Level1)
899 {
900     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
901     EXPECT_TRUE(dBinderService != nullptr);
902     sptr<IRemoteObject> object = new IPCObjectProxy(16);
903     EXPECT_TRUE(object != nullptr);
904     std::string serviceName("testServer");
905     std::string deviceID("123456");
906     binder_uintptr_t binderObject = 100;
907     sptr<DBinderServiceStub> dBinderServiceStub = new DBinderServiceStub(serviceName, deviceID, binderObject);
908     EXPECT_TRUE(dBinderServiceStub != nullptr);
909     dBinderService->AttachCallbackProxy(object, dBinderServiceStub.GetRefPtr());
910     EXPECT_EQ(dBinderService->NoticeCallbackProxy(dBinderServiceStub), false);
911 }
912 
913 /**
914  * @tc.name: DetachCallbackProxyTest001
915  * @tc.desc: Verify the DetachCallbackProxy function
916  * @tc.type: FUNC
917  */
918 HWTEST_F(DBinderServiceUnitTest, DetachCallbackProxyTest001, TestSize.Level1)
919 {
920     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
921     EXPECT_TRUE(dBinderService != nullptr);
922     sptr<IRemoteObject> object = new IPCObjectProxy(16);
923     EXPECT_TRUE(object != nullptr);
924     std::string serviceName("test1");
925     std::string deviceID("12345");
926     binder_uintptr_t binderObject = 100;
927     sptr<DBinderServiceStub> dBinderServiceStub = new DBinderServiceStub(serviceName, deviceID, binderObject);
928     EXPECT_TRUE(dBinderServiceStub != nullptr);
929     dBinderService->AttachCallbackProxy(object, dBinderServiceStub.GetRefPtr());
930     EXPECT_EQ(dBinderService->DetachCallbackProxy(object), true);
931 }
932 
933 /**
934  * @tc.name: DetachCallbackProxyTest002
935  * @tc.desc: Verify the DetachCallbackProxy function
936  * @tc.type: FUNC
937  */
938 HWTEST_F(DBinderServiceUnitTest, DetachCallbackProxyTest002, TestSize.Level1)
939 {
940     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
941     EXPECT_TRUE(dBinderService != nullptr);
942     sptr<IRemoteObject> object = new IPCObjectProxy(100);
943     EXPECT_TRUE(object != nullptr);
944     EXPECT_EQ(dBinderService->DetachCallbackProxy(object), false);
945 }
946 
947 /**
948  * @tc.name: QueryDeathRecipientTest001
949  * @tc.desc: Verify the QueryDeathRecipient function
950  * @tc.type: FUNC
951  */
952 HWTEST_F(DBinderServiceUnitTest, QueryDeathRecipientTest001, TestSize.Level1)
953 {
954     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
955     EXPECT_TRUE(dBinderService != nullptr);
956     sptr<IRemoteObject> object = new IPCObjectProxy(20);
957     EXPECT_TRUE(object != nullptr);
958     sptr<IRemoteObject::DeathRecipient> deathRecipient = new TestDeathRecipient();
959     EXPECT_TRUE(deathRecipient != nullptr);
960     dBinderService->AttachDeathRecipient(object, deathRecipient);
961     EXPECT_EQ(dBinderService->QueryDeathRecipient(object), deathRecipient);
962 }
963 
964 /**
965  * @tc.name: QueryDeathRecipientTest002
966  * @tc.desc: Verify the QueryDeathRecipient function
967  * @tc.type: FUNC
968  */
969 HWTEST_F(DBinderServiceUnitTest, QueryDeathRecipientTest002, TestSize.Level1)
970 {
971     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
972     EXPECT_TRUE(dBinderService != nullptr);
973     EXPECT_EQ(dBinderService->QueryDeathRecipient(nullptr), nullptr);
974 }
975 
976 /**
977  * @tc.name: AttachProxyObjectTest001
978  * @tc.desc: Verify the AttachProxyObject function
979  * @tc.type: FUNC
980  */
981 HWTEST_F(DBinderServiceUnitTest, AttachProxyObjectTest001, TestSize.Level1)
982 {
983     std::string name("Test");
984     binder_uintptr_t binderObject = 10;
985     binder_uintptr_t binderObject1 = 11;
986     sptr<IRemoteObject> object = new IPCObjectProxy(16);
987     EXPECT_TRUE(object != nullptr);
988     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
989     EXPECT_TRUE(dBinderService != nullptr);
990     EXPECT_EQ(dBinderService->AttachProxyObject(object, binderObject), true);
991     EXPECT_EQ(dBinderService->QueryProxyObject(binderObject), object);
992     EXPECT_EQ(dBinderService->QueryProxyObject(binderObject1), nullptr);
993 }
994 
995 /**
996  * @tc.name: AttachProxyObjectTest002
997  * @tc.desc: Verify the AttachProxyObject function
998  * @tc.type: FUNC
999  */
1000 HWTEST_F(DBinderServiceUnitTest, AttachProxyObjectTest002, TestSize.Level1)
1001 {
1002     uint32_t seqNumber = 10;
1003     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1004     EXPECT_TRUE(dBinderService != nullptr);
1005     std::shared_ptr<OHOS::ThreadLockInfo> threadLockInfo = std::make_shared<OHOS::ThreadLockInfo>();
1006     EXPECT_TRUE(threadLockInfo != nullptr);
1007     dBinderService->AttachThreadLockInfo(seqNumber, "networkId", threadLockInfo);
1008     dBinderService->WakeupThreadByStub(seqNumber);
1009     EXPECT_TRUE(dBinderService->QueryThreadLockInfo(seqNumber) != nullptr);
1010     EXPECT_EQ(dBinderService->QueryThreadLockInfo(seqNumber), threadLockInfo);
1011     dBinderService->DetachThreadLockInfo(seqNumber);
1012     dBinderService->WakeupThreadByStub(seqNumber);
1013     EXPECT_TRUE(dBinderService->QueryThreadLockInfo(seqNumber) == nullptr);
1014 }
1015 
1016 /**
1017  * @tc.name: MakeSessionByReplyMessageTest001
1018  * @tc.desc: Verify the MakeSessionByReplyMessage function
1019  * @tc.type: FUNC
1020  */
1021 HWTEST_F(DBinderServiceUnitTest, MakeSessionByReplyMessageTest001, TestSize.Level1)
1022 {
1023     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1024     EXPECT_TRUE(dBinderService != nullptr);
1025     std::shared_ptr<struct DHandleEntryTxRx> replyMessage = std::make_shared<DHandleEntryTxRx>();
1026     EXPECT_TRUE(replyMessage != nullptr);
1027     memset_s(replyMessage.get(), sizeof(DHandleEntryTxRx), 0, sizeof(DHandleEntryTxRx));
1028     dBinderService->MakeSessionByReplyMessage(replyMessage);
1029     EXPECT_EQ(dBinderService->HasDBinderStub(replyMessage->binderObject), false);
1030 
1031     std::string serviceName("testServer");
1032     std::string deviceID;
1033     binder_uintptr_t binderObject = 161561;
1034     sptr<DBinderServiceStub> stub = new DBinderServiceStub(serviceName, deviceID, binderObject);
1035     EXPECT_TRUE(stub != nullptr);
1036     replyMessage->stub = reinterpret_cast<binder_uintptr_t>(stub.GetRefPtr());
1037     dBinderService->MakeSessionByReplyMessage(replyMessage);
1038 }
1039 
1040 /**
1041  * @tc.name: RegisterRemoteProxyTest001
1042  * @tc.desc: Verify the RegisterRemoteProxy function
1043  * @tc.type: FUNC
1044  */
1045 HWTEST_F(DBinderServiceUnitTest, RegisterRemoteProxyTest001, TestSize.Level1)
1046 {
1047     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1048     EXPECT_TRUE(dBinderService != nullptr);
1049     std::u16string serviceName;
1050     int32_t systemAbilityId = 1;
1051     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, systemAbilityId), false);
1052     serviceName = u"testServer";
1053     systemAbilityId = 0;
1054     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, systemAbilityId), false);
1055     systemAbilityId = 1;
1056     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, systemAbilityId), true);
1057 }
1058 
1059 /**
1060  * @tc.name: RegisterRemoteProxyTest002
1061  * @tc.desc: Verify the RegisterRemoteProxy function
1062  * @tc.type: FUNC
1063  */
1064 HWTEST_F(DBinderServiceUnitTest, RegisterRemoteProxyTest002, TestSize.Level1)
1065 {
1066     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1067     EXPECT_TRUE(dBinderService != nullptr);
1068     std::u16string serviceName;
1069     sptr<IRemoteObject> binderObject = nullptr;
1070     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, binderObject), false);
1071     serviceName = u"testServer";
1072     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, binderObject), false);
1073     sptr<IRemoteObject> object = new IPCObjectProxy(16);
1074     EXPECT_TRUE(object != nullptr);
1075     EXPECT_EQ(dBinderService->RegisterRemoteProxy(serviceName, object), true);
1076 }
1077 
1078 /**
1079  * @tc.name: GetRegisterServiceTest001
1080  * @tc.desc: Verify the GetRegisterService function
1081  * @tc.type: FUNC
1082  */
1083 HWTEST_F(DBinderServiceUnitTest, GetRegisterServiceTest001, TestSize.Level1)
1084 {
1085     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1086     EXPECT_TRUE(dBinderService != nullptr);
1087     binder_uintptr_t binderObject = 1;
1088     EXPECT_EQ(dBinderService->GetRegisterService(binderObject), std::u16string());
1089     std::u16string serviceName(u"testServer");
1090     dBinderService->RegisterRemoteProxyInner(serviceName, binderObject);
1091     EXPECT_EQ(dBinderService->GetRegisterService(binderObject), serviceName);
1092 }
1093 
1094 /**
1095  * @tc.name: OnRemoteMessageTaskTest001
1096  * @tc.desc: Verify the OnRemoteMessageTask function
1097  * @tc.type: FUNC
1098  */
1099 HWTEST_F(DBinderServiceUnitTest, OnRemoteMessageTaskTest001, TestSize.Level1)
1100 {
1101     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1102     EXPECT_TRUE(dBinderService != nullptr);
1103     std::shared_ptr<struct DHandleEntryTxRx> handleEntryTxRx = nullptr;
1104     EXPECT_EQ(dBinderService->OnRemoteMessageTask(handleEntryTxRx), false);
1105     std::shared_ptr<DHandleEntryTxRx> message = std::make_shared<DHandleEntryTxRx>();
1106     EXPECT_TRUE(message != nullptr);
1107     message->head.len = 10;
1108     message->head.version = 1;
1109     message->transType = 0;
1110     message->fromPort = 1;
1111     message->toPort = 2;
1112     message->stubIndex = 1;
1113     message->seqNumber = 1;
1114     message->binderObject = 10;
1115     message->deviceIdInfo.tokenId = 1;
1116     message->deviceIdInfo.fromDeviceId[0] = 't';
1117     message->deviceIdInfo.toDeviceId[0] = 't';
1118     message->stub = 10;
1119     message->serviceNameLength = 10;
1120     message->serviceName[0] = 't';
1121     message->pid = 100;
1122     message->uid = 100;
1123     dBinderService->dbinderCallback_ = std::make_shared<TestRpcSystemAbilityCallback>();
1124     EXPECT_TRUE(dBinderService->dbinderCallback_ != nullptr);
1125     message->dBinderCode = DBinderCode::MESSAGE_AS_INVOKER;
1126     EXPECT_EQ(dBinderService->OnRemoteMessageTask(message), true);
1127     message->dBinderCode = DBinderCode::MESSAGE_AS_REPLY;
1128     EXPECT_EQ(dBinderService->OnRemoteMessageTask(message), true);
1129     message->dBinderCode = DBinderCode::MESSAGE_AS_OBITUARY;
1130     EXPECT_EQ(dBinderService->OnRemoteMessageTask(message), false);
1131     message->dBinderCode = DBinderCode::MESSAGE_AS_REMOTE_ERROR;
1132     EXPECT_EQ(dBinderService->OnRemoteMessageTask(message), true);
1133 }
1134 
1135 /**
1136  * @tc.name: OnRemoteInvokerDataBusMessageTest001
1137  * @tc.desc: Verify the OnRemoteInvokerDataBusMessage function
1138  * @tc.type: FUNC
1139  */
1140 HWTEST_F(DBinderServiceUnitTest, OnRemoteInvokerDataBusMessageTest001, TestSize.Level1)
1141 {
1142     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1143     EXPECT_TRUE(dBinderService != nullptr);
1144     IPCObjectProxy* proxy = nullptr;
1145     std::string remoteDeviceId;
1146     int pid = 1;
1147     int uid = 1;
1148     uint32_t tokenId = 1;
1149     std::shared_ptr<struct DHandleEntryTxRx> replyMessage = std::make_shared<DHandleEntryTxRx>();
1150     EXPECT_TRUE(replyMessage != nullptr);
1151     memset_s(replyMessage.get(), sizeof(DHandleEntryTxRx), 0, sizeof(DHandleEntryTxRx));
1152     EXPECT_EQ(dBinderService->OnRemoteInvokerDataBusMessage(
1153         proxy, replyMessage, remoteDeviceId, pid, uid, tokenId), DBinderErrorCode::DEVICEID_INVALID);
1154 }
1155 
1156 /**
1157  * @tc.name: OnRemoteInvokerDataBusMessageTest002
1158  * @tc.desc: Verify the OnRemoteInvokerDataBusMessage function
1159  * @tc.type: FUNC
1160  */
1161 HWTEST_F(DBinderServiceUnitTest, OnRemoteInvokerDataBusMessageTest002, TestSize.Level1)
1162 {
1163     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1164     EXPECT_TRUE(dBinderService != nullptr);
1165     std::string remoteDeviceId("test");
1166     int pid = 1;
1167     int uid = 1;
1168     uint32_t tokenId = 1;
1169     IPCObjectProxy objectProxy(0);
1170     std::shared_ptr<struct DHandleEntryTxRx> replyMessage = std::make_shared<DHandleEntryTxRx>();
1171     EXPECT_TRUE(replyMessage != nullptr);
1172     memset_s(replyMessage.get(), sizeof(DHandleEntryTxRx), 0, sizeof(DHandleEntryTxRx));
1173     EXPECT_EQ(dBinderService->OnRemoteInvokerDataBusMessage(
1174         &objectProxy, replyMessage, remoteDeviceId, pid, uid, tokenId), DBinderErrorCode::SESSION_NAME_NOT_FOUND);
1175 }
1176 
1177 /*
1178  * @tc.name: ProcessOnSessionClosedTest002
1179  * @tc.desc: Verify the ProcessOnSessionClosed function
1180  * @tc.type: FUNC
1181  */
1182 HWTEST_F(DBinderServiceUnitTest, ProcessOnSessionClosedTest002, TestSize.Level1)
1183 {
1184     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1185     EXPECT_TRUE(dBinderService != nullptr);
1186     std::shared_ptr<OHOS::ThreadLockInfo> threadLockInfo = std::make_shared<OHOS::ThreadLockInfo>();
1187     EXPECT_TRUE(threadLockInfo != nullptr);
1188     uint32_t seqNumber = 10;
1189     std::string networkId = "networkId";
1190     dBinderService->AttachThreadLockInfo(seqNumber, networkId, threadLockInfo);
1191     EXPECT_EQ(dBinderService->ProcessOnSessionClosed(networkId), true);
1192 }
1193 
1194 /**
1195  * @tc.name: FindDBinderStub001
1196  * @tc.desc: Verify the FindDBinderStub function
1197  * @tc.type: FUNC
1198  */
1199 HWTEST_F(DBinderServiceUnitTest, FindDBinderStub001, TestSize.Level1)
1200 {
1201     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1202     EXPECT_TRUE(dBinderService != nullptr);
1203     std::u16string service(u"test");
1204     std::string device = "aaa";
1205     binder_uintptr_t binderObject = 100;
1206     sptr<DBinderServiceStub> testDdBinderStub1 = dBinderService->FindOrNewDBinderStub(service, device, binderObject);
1207     EXPECT_TRUE(testDdBinderStub1 != nullptr);
1208     sptr<DBinderServiceStub> testDdBinderStub2 = dBinderService->FindOrNewDBinderStub(service, device, binderObject);
1209     EXPECT_TRUE(testDdBinderStub2 != nullptr);
1210     EXPECT_EQ(testDdBinderStub1.GetRefPtr(), testDdBinderStub2.GetRefPtr());
1211 
1212     sptr<DBinderServiceStub> testDdBinderStub3 = dBinderService->FindDBinderStub(service, device);
1213     EXPECT_TRUE(testDdBinderStub3 != nullptr);
1214     EXPECT_EQ(testDdBinderStub1.GetRefPtr(), testDdBinderStub3.GetRefPtr());
1215 
1216     std::u16string service1(u"test1");
1217     std::string device1 = "bbb";
1218     EXPECT_EQ(dBinderService->FindDBinderStub(service1, device1), nullptr);
1219 
1220     EXPECT_EQ(dBinderService->DeleteDBinderStub(service1, device1), false);
1221     EXPECT_EQ(dBinderService->DeleteDBinderStub(service, device), true);
1222 }
1223 
1224 /**
1225  * @tc.name: ReStartRemoteListenerTest001
1226  * @tc.desc: Verify the ReStartRemoteListener function
1227  * @tc.type: FUNC
1228  */
1229 HWTEST_F(DBinderServiceUnitTest, ReStartRemoteListenerTest001, TestSize.Level1)
1230 {
1231     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1232     EXPECT_TRUE(dBinderService != nullptr);
1233     dBinderService->remoteListener_ = nullptr;
1234     bool res = dBinderService->ReStartRemoteListener();
1235     EXPECT_EQ(res, false);
1236 }
1237 
1238 /**
1239  * @tc.name: ReStartRemoteListenerTest002
1240  * @tc.desc: Verify the ReStartRemoteListener function
1241  * @tc.type: FUNC
1242  */
1243 HWTEST_F(DBinderServiceUnitTest, ReStartRemoteListenerTest002, TestSize.Level1)
1244 {
1245     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1246     EXPECT_TRUE(dBinderService != nullptr);
1247     dBinderService->remoteListener_ = std::make_shared<DBinderRemoteListener>();
1248     EXPECT_TRUE(dBinderService->remoteListener_ != nullptr);
1249     bool res = dBinderService->ReStartRemoteListener();
1250     EXPECT_EQ(res, false);
1251 }
1252 
1253 /**
1254  * @tc.name: IsSameStubObjectTest002
1255  * @tc.desc: Verify the IsSameStubObject function
1256  * @tc.type: FUNC
1257  */
1258 HWTEST_F(DBinderServiceUnitTest, IsSameStubObjectTest002, TestSize.Level1)
1259 {
1260     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1261     EXPECT_TRUE(dBinderService != nullptr);
1262     std::string serviceName = "test";
1263     std::string deviceID = "001";
1264     binder_uintptr_t binderObject = 1;
1265     sptr<DBinderServiceStub> stub = new DBinderServiceStub(serviceName, deviceID, binderObject);
1266     EXPECT_TRUE(stub != nullptr);
1267     std::u16string service(u"test");
1268     bool res = dBinderService->IsSameStubObject(stub, service, deviceID);
1269     EXPECT_EQ(res, true);
1270 }
1271 
1272 /**
1273  * @tc.name: SendEntryToRemoteTest002
1274  * @tc.desc: Verify the SendEntryToRemote function
1275  * @tc.type: FUNC
1276  */
1277 HWTEST_F(DBinderServiceUnitTest, SendEntryToRemoteTest002, TestSize.Level1)
1278 {
1279     std::string serviceName("testServer");
1280     std::string deviceID;
1281     binder_uintptr_t binderObject = 161561;
1282     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1283     EXPECT_TRUE(dBinderService != nullptr);
1284     sptr<DBinderServiceStub> stub = new DBinderServiceStub(serviceName, deviceID, binderObject);
1285     EXPECT_TRUE(stub != nullptr);
1286     uint32_t seqNumber = 0;
1287     uint32_t pid = 0;
1288     uint32_t uid = 0;
1289     bool res = dBinderService->SendEntryToRemote(stub, seqNumber, pid, uid);
1290     EXPECT_EQ(res, false);
1291 }
1292 
1293 /**
1294  * @tc.name: PopLoadSaItemTest001
1295  * @tc.desc: Verify the PopLoadSaItem function
1296  * @tc.type: FUNC
1297  */
1298 HWTEST_F(DBinderServiceUnitTest, PopLoadSaItemTest001, TestSize.Level1)
1299 {
1300     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1301     EXPECT_TRUE(dBinderService != nullptr);
1302     std::string srcNetworkId;
1303     int32_t systemAbilityId = 1;
1304     EXPECT_EQ(dBinderService->PopLoadSaItem(srcNetworkId, systemAbilityId), nullptr);
1305 
1306     srcNetworkId = "t";
1307     std::shared_ptr<struct DHandleEntryTxRx> message = std::make_shared<DHandleEntryTxRx>();
1308     EXPECT_TRUE(message != nullptr);
1309     memset_s(message.get(), sizeof(DHandleEntryTxRx), 0, sizeof(DHandleEntryTxRx));
1310     message->stubIndex = systemAbilityId;
1311     message->deviceIdInfo.fromDeviceId[0] = 't';
1312     dBinderService->dbinderCallback_ = std::make_shared<TestRpcSystemAbilityCallback>();
1313     EXPECT_TRUE(dBinderService->dbinderCallback_ != nullptr);
1314     dBinderService->OnRemoteInvokerMessage(message);
1315     std::shared_ptr<DHandleEntryTxRx> dHandleEntryTxRx = dBinderService->PopLoadSaItem(srcNetworkId, systemAbilityId);
1316     EXPECT_TRUE(dHandleEntryTxRx != nullptr);
1317     sptr<IRemoteObject> remoteObject = nullptr;
1318     dBinderService->LoadSystemAbilityComplete("test", 2, remoteObject);
1319     dBinderService->LoadSystemAbilityComplete(srcNetworkId, systemAbilityId, remoteObject);
1320     sptr<IRemoteObject> remoteObject1 = new IPCObjectProxy(1);
1321     EXPECT_TRUE(remoteObject1 != nullptr);
1322     dBinderService->LoadSystemAbilityComplete(srcNetworkId, systemAbilityId, remoteObject1);
1323 }
1324 
1325 /**
1326  * @tc.name: SendReplyMessageToRemote001
1327  * @tc.desc: Verify the SendReplyMessageToRemote function
1328  * @tc.type: FUNC
1329  */
1330 HWTEST_F(DBinderServiceUnitTest, SendReplyMessageToRemote001, TestSize.Level1)
1331 {
1332     uint32_t dBinderCode = 4;
1333     uint32_t reason = 0;
1334     std::shared_ptr<DHandleEntryTxRx> replyMessage = std::make_shared<DHandleEntryTxRx>();
1335     EXPECT_TRUE(replyMessage != nullptr);
1336     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1337     EXPECT_TRUE(dBinderService != nullptr);
1338     dBinderService->remoteListener_ = std::make_shared<DBinderRemoteListener>();
1339     EXPECT_TRUE(dBinderService->remoteListener_ != nullptr);
1340     dBinderService->SendReplyMessageToRemote(dBinderCode, reason, replyMessage);
1341     dBinderCode = 1;
1342     dBinderService->SendReplyMessageToRemote(dBinderCode, reason, replyMessage);
1343     DBinderService *temp = new DBinderService();
1344     EXPECT_TRUE(temp != nullptr);
1345     DBinderService::instance_ = temp;
1346     dBinderService = DBinderService::GetInstance();
1347     EXPECT_TRUE(dBinderService != nullptr);
1348     EXPECT_EQ(dBinderService, DBinderService::instance_);
1349 }
1350 
1351 /**
1352  * @tc.name: AddAsynMessageTask001
1353  * @tc.desc: Verify the AddAsynMessageTask function
1354  * @tc.type: FUNC
1355  */
1356 HWTEST_F(DBinderServiceUnitTest, AddAsynMessageTask001, TestSize.Level1)
1357 {
1358     std::shared_ptr<struct DHandleEntryTxRx> message = std::make_shared<struct DHandleEntryTxRx>();
1359     EXPECT_NE(message.get(), nullptr);
1360     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1361     EXPECT_TRUE(dBinderService != nullptr);
1362     dBinderService->AddAsynMessageTask(message);
1363 }
1364 
1365 /**
1366  * @tc.name: IsSameSession002
1367  * @tc.desc: Verify the IsSameSession function
1368  * @tc.type: FUNC
1369  */
1370 HWTEST_F(DBinderServiceUnitTest, IsSameSession002, TestSize.Level1)
1371 {
1372     std::shared_ptr<struct SessionInfo> oldSession= std::make_shared<struct SessionInfo>();
1373     EXPECT_NE(oldSession.get(), nullptr);
1374     std::shared_ptr<struct SessionInfo> newSession= std::make_shared<struct SessionInfo>();
1375     EXPECT_NE(newSession.get(), nullptr);
1376     oldSession->stubIndex = 1;
1377     oldSession->toPort = 2;
1378     oldSession->fromPort = 3;
1379     oldSession->type = 4;
1380     oldSession->serviceName[0] = 't';
1381     newSession->stubIndex = 2;
1382     newSession->toPort = 2;
1383     newSession->fromPort = 3;
1384     newSession->type = 4;
1385     newSession->serviceName[0] = 't';
1386     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1387     EXPECT_TRUE(dBinderService != nullptr);
1388     EXPECT_EQ(dBinderService->IsSameSession(oldSession, newSession), false);
1389     newSession->stubIndex = 1;
1390     newSession->toPort = 12;
1391     EXPECT_EQ(dBinderService->IsSameSession(oldSession, newSession), false);
1392     newSession->toPort = 2;
1393     newSession->fromPort = 13;
1394     EXPECT_EQ(dBinderService->IsSameSession(oldSession, newSession), false);
1395     newSession->fromPort = 3;
1396     newSession->type = 14;
1397     EXPECT_EQ(dBinderService->IsSameSession(oldSession, newSession), false);
1398     newSession->type = 4;
1399     EXPECT_EQ(dBinderService->IsSameSession(oldSession, newSession), true);
1400 }
1401 
1402 /**
1403  * @tc.name: AttachSessionObject001
1404  * @tc.desc: Verify the AttachSessionObject function
1405  * @tc.type: FUNC
1406  */
1407 HWTEST_F(DBinderServiceUnitTest, AttachSessionObject001, TestSize.Level1)
1408 {
1409     std::shared_ptr<struct SessionInfo> object = nullptr;
1410     binder_uintptr_t stub = 0;
1411     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
1412     EXPECT_TRUE(dBinderService != nullptr);
1413     EXPECT_EQ(dBinderService->AttachSessionObject(object, stub), true);
1414 }
1415