1 /*
2  * Copyright (C) 2021-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 <algorithm>
17 #include <gtest/gtest.h>
18 
19 #define private public
20 #define protected public
21 #include "comm_auth_info.h"
22 #include "dbinder_databus_invoker.h"
23 #include "dbinder_session_object.h"
24 #include "binder_invoker.h"
25 #include "ipc_debug.h"
26 #include "ipc_skeleton.h"
27 #include "ipc_object_proxy.h"
28 #include "ipc_object_stub.h"
29 #include "ipc_process_skeleton.h"
30 #include "ipc_thread_skeleton.h"
31 #include "test_service_skeleton.h"
32 #include "test_service.h"
33 #include "test_service_command.h"
34 #include "test_service_client.h"
35 #include "ipc_test_helper.h"
36 #include "if_system_ability_manager.h"
37 #include "iservice_registry.h"
38 #include "dbinder_session_object.h"
39 #include "message_option.h"
40 #include "mock_iremote_invoker.h"
41 #include "stub_refcount_object.h"
42 #include "system_ability_definition.h"
43 #include "log_tags.h"
44 #undef protected
45 #undef private
46 #ifndef CONFIG_STANDARD_SYSTEM
47 #include "jni_help.h"
48 #endif
49 
50 using namespace testing::ext;
51 using namespace OHOS;
52 using namespace OHOS::HiviewDFX;
53 
54 namespace {
55 constexpr int MAX_TEST_COUNT = 1000;
56 constexpr bool SUPPORT_ZBINDER = false;
57 constexpr uint32_t INVAL_TOKEN_ID = 0x0;
58 constexpr int MAX_WAIT_TIME = 3000;
59 constexpr int INVALID_LEN = 9999;
60 }
61 
62 class IPCNativeUnitTest : public testing::Test {
63 public:
64     static void SetUpTestCase(void);
65     static void TearDownTestCase(void);
66     static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "IPCUnitTest" };
67 
68 private:
69     static inline IPCTestHelper *g_globalHelper = { nullptr };
70 };
71 
SetUpTestCase()72 void IPCNativeUnitTest::SetUpTestCase()
73 {
74     if (g_globalHelper == nullptr) {
75         g_globalHelper = new IPCTestHelper();
76         bool res = g_globalHelper->PrepareTestSuite();
77         ASSERT_TRUE(res);
78     }
79 }
80 
TearDownTestCase()81 void IPCNativeUnitTest::TearDownTestCase()
82 {
83     if (g_globalHelper != nullptr) {
84         bool res = g_globalHelper->TearDownTestSuite();
85         ASSERT_TRUE(res);
86         delete g_globalHelper;
87         g_globalHelper = nullptr;
88     }
89 }
90 
91 /**
92  * @tc.name: DeathRecipient001
93  * @tc.desc: The Stub should not support AddDeathRecipient
94  * @tc.type: FUNC
95  */
96 HWTEST_F(IPCNativeUnitTest, DeathRecipient001, TestSize.Level1)
97 {
98     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
99     bool res = testStub->AddDeathRecipient(nullptr);
100     EXPECT_FALSE(res);
101 }
102 
103 /**
104  * @tc.name: DeathRecipient002
105  * @tc.desc: The Stub should not support RemoveDeathRecipient
106  * @tc.type: FUNC
107  */
108 HWTEST_F(IPCNativeUnitTest, DeathRecipient002, TestSize.Level1)
109 {
110     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
111     bool res = testStub->RemoveDeathRecipient(nullptr);
112     EXPECT_FALSE(res);
113 }
114 
115 /**
116  * @tc.name: GetObjectRefCountTest001
117  * @tc.desc: Verify the GetObjectRefCount function
118  * @tc.type: FUNC
119  */
120 HWTEST_F(IPCNativeUnitTest, GetObjectRefCountTest001, TestSize.Level1)
121 {
122     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
123     int count = testStub->GetObjectRefCount();
124     EXPECT_GE(count, 0);
125 }
126 
127 /**
128  * @tc.name: DumpTest001
129  * @tc.desc: The Stub should not support Dump
130  * @tc.type: FUNC
131  */
132 HWTEST_F(IPCNativeUnitTest, DumpTest001, TestSize.Level1)
133 {
134     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
135     std::vector<std::u16string> args;
136     args.push_back(u"test");
137     int res = testStub->Dump(0, args);
138     EXPECT_EQ(res, 0);
139 }
140 
141 
142 /**
143  * @tc.name: ProxyJudgment001
144  * @tc.desc: act as stub role, should return false
145  * @tc.type: FUNC
146  */
147 HWTEST_F(IPCNativeUnitTest, ProxyJudgment001, TestSize.Level1)
148 {
149     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
150     bool res = testStub->IsProxyObject();
151     EXPECT_FALSE(res);
152 }
153 
154 /**
155  * @tc.name: GetCallingPidTest001
156  * @tc.desc: Verify the GetCallingPid function
157  * @tc.type: FUNC
158  */
159 HWTEST_F(IPCNativeUnitTest, GetCallingPidTest001, TestSize.Level1)
160 {
161     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
162     pid_t id = testStub->GetCallingPid();
163     EXPECT_NE(id, -1);
164 }
165 
166 /**
167  * @tc.name: GetCallingUidTest001
168  * @tc.desc: Verify the GetCallingUid function
169  * @tc.type: FUNC
170  */
171 HWTEST_F(IPCNativeUnitTest, GetCallingUidTest001, TestSize.Level1)
172 {
173     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
174     pid_t id = testStub->GetCallingUid();
175     EXPECT_NE(id, -1);
176 }
177 
178 /**
179  * @tc.name: GetCallingTokenIDTest001
180  * @tc.desc: Verify the GetCallingTokenID function
181  * @tc.type: FUNC
182  */
183 HWTEST_F(IPCNativeUnitTest, GetCallingTokenIDTest001, TestSize.Level1)
184 {
185     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
186     uint32_t token = testStub->GetCallingTokenID();
187     EXPECT_NE(token, INVAL_TOKEN_ID);
188 }
189 
190 /**
191  * @tc.name: GetCallingFullTokenIDTest001
192  * @tc.desc: Verify the GetCallingFullTokenID function
193  * @tc.type: FUNC
194  */
195 HWTEST_F(IPCNativeUnitTest, GetCallingFullTokenIDTest001, TestSize.Level1)
196 {
197     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
198     uint64_t token1 = testStub->GetCallingFullTokenID();
199     uint64_t token2 = static_cast<uint64_t>(testStub->GetCallingTokenID());
200     EXPECT_NE(token1, INVAL_TOKEN_ID);
201     EXPECT_EQ(token1, token2);
202 }
203 
204 /**
205  * @tc.name: GetCallingFullTokenIDTest002
206  * @tc.desc: Verify the GetCallingFullTokenID function
207  * @tc.type: FUNC
208  */
209 HWTEST_F(IPCNativeUnitTest, GetCallingFullTokenIDTest002, TestSize.Level1)
210 {
211     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
212     uint64_t token1 = testStub->GetCallingFullTokenID();
213     uint64_t token2 = IPCSkeleton::GetSelfTokenID();
214     EXPECT_NE(token1, INVAL_TOKEN_ID);
215     EXPECT_EQ(token1, token2);
216 }
217 
218 /**
219  * @tc.name: GetFirstTokenIDTest001
220  * @tc.desc: Verify the GetFirstTokenID function
221  * @tc.type: FUNC
222  */
223 HWTEST_F(IPCNativeUnitTest, GetFirstTokenIDTest001, TestSize.Level1)
224 {
225     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
226     uint32_t token = testStub->GetFirstTokenID();
227     EXPECT_EQ(token, INVAL_TOKEN_ID);
228 }
229 
230 /**
231  * @tc.name: GetFirstFullTokenIDTest001
232  * @tc.desc: Verify the GetFirstFullTokenID function
233  * @tc.type: FUNC
234  */
235 HWTEST_F(IPCNativeUnitTest, GetFirstFullTokenIDTest001, TestSize.Level1)
236 {
237     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
238     uint64_t token = testStub->GetFirstFullTokenID();
239     EXPECT_EQ(token, INVAL_TOKEN_ID);
240 }
241 
242 /**
243  * @tc.name: GetObjectTypeTest001
244  * @tc.desc: Verify the GetObjectType function
245  * @tc.type: FUNC
246  */
247 HWTEST_F(IPCNativeUnitTest, GetObjectTypeTest001, TestSize.Level1)
248 {
249     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
250     int ret = testStub->GetObjectType();
251     EXPECT_EQ(ret, IPCObjectStub::OBJECT_TYPE_NATIVE);
252 }
253 
254 /**
255  * @tc.name: IsDeviceIdIllegalTest001
256  * @tc.desc: Verify the IsDeviceIdIllegal function
257  * @tc.type: FUNC
258  */
259 HWTEST_F(IPCNativeUnitTest, IsDeviceIdIllegalTest001, TestSize.Level1)
260 {
261     std::string deviceID = "test";
262     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
263     bool ret = testStub->IsDeviceIdIllegal(deviceID);
264     EXPECT_EQ(ret, false);
265 }
266 
267 /**
268  * @tc.name: IsDeviceIdIllegalTest002
269  * @tc.desc: Verify the IsDeviceIdIllegal function
270  * @tc.type: FUNC
271  */
272 HWTEST_F(IPCNativeUnitTest, IsDeviceIdIllegalTest002, TestSize.Level1)
273 {
274     std::string deviceID = "";
275     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
276     bool ret = testStub->IsDeviceIdIllegal(deviceID);
277     EXPECT_EQ(ret, true);
278 }
279 
280 /**
281  * @tc.name: IsDeviceIdIllegalTest003
282  * @tc.desc: Verify the IsDeviceIdIllegal function
283  * @tc.type: FUNC
284  */
285 HWTEST_F(IPCNativeUnitTest, IsDeviceIdIllegalTest003, TestSize.Level1)
286 {
287     std::string deviceID(INVALID_LEN, '1');
288     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
289     bool ret = testStub->IsDeviceIdIllegal(deviceID);
290     EXPECT_EQ(ret, true);
291 }
292 
293 #ifndef CONFIG_IPC_SINGLE
294 /**
295  * @tc.name: OnRemoteRequestTest001
296  * @tc.desc: Verify the OnRemoteRequest function
297  * @tc.type: FUNC
298  */
299 HWTEST_F(IPCNativeUnitTest, OnRemoteRequestTest001, TestSize.Level1)
300 {
301     std::string deviceID(INVALID_LEN, '1');
302     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
303     uint32_t code = DBINDER_OBITUARY_TRANSACTION;
304     MessageParcel data;
305     MessageParcel reply;
306     MessageOption option;
307     auto ret = testStub->OnRemoteRequest(code, data, reply, option);
308     EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR);
309 }
310 
311 /**
312  * @tc.name: OnRemoteRequestTest002
313  * @tc.desc: Verify the OnRemoteRequest function
314  * @tc.type: FUNC
315  */
316 HWTEST_F(IPCNativeUnitTest, OnRemoteRequestTest002, TestSize.Level1)
317 {
318     std::string deviceID(INVALID_LEN, '1');
319     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
320     uint32_t code = DBINDER_OBITUARY_TRANSACTION;
321     MessageParcel data;
322     MessageParcel reply;
323     MessageOption option;
324 
325     BinderInvoker *invoker = new BinderInvoker();
326     invoker->status_ = IRemoteInvoker::ACTIVE_INVOKER;
327     invoker->callerTokenID_ = 1;
328     IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
329     current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
330 
331     auto ret = testStub->OnRemoteRequest(code, data, reply, option);
332     EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR);
333     std::fill(current->invokers_, current->invokers_ + IPCThreadSkeleton::INVOKER_MAX_COUNT, nullptr);
334     delete invoker;
335 }
336 
337 /**
338  * @tc.name: OnRemoteRequestTest003
339  * @tc.desc: Verify the OnRemoteRequest function
340  * @tc.type: FUNC
341  */
342 HWTEST_F(IPCNativeUnitTest, OnRemoteRequestTest003, TestSize.Level1)
343 {
344     std::string deviceID(INVALID_LEN, '1');
345     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
346     uint32_t code = DBINDER_OBITUARY_TRANSACTION;
347     MessageParcel data;
348     data.WriteInt32(IRemoteObject::DeathRecipient::NOTICE_DEATH_RECIPIENT);
349     MessageParcel reply;
350     MessageOption option;
351     auto ret = testStub->OnRemoteRequest(code, data, reply, option);
352     EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR);
353 }
354 
355 /**
356  * @tc.name: OnRemoteRequestTest004
357  * @tc.desc: Verify the OnRemoteRequest function
358  * @tc.type: FUNC
359  */
360 HWTEST_F(IPCNativeUnitTest, OnRemoteRequestTest004, TestSize.Level1)
361 {
362     std::string deviceID(INVALID_LEN, '1');
363     sptr<IPCObjectStub> testStub = new IPCObjectStub(u"test");
364     uint32_t code = DBINDER_INCREFS_TRANSACTION;
365     MessageParcel data;
366     data.WriteInt32(IRemoteObject::DeathRecipient::NOTICE_DEATH_RECIPIENT);
367     MessageParcel reply;
368     MessageOption option;
369     auto ret = testStub->OnRemoteRequest(code, data, reply, option);
370     EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR);
371 }
372 #endif
373 
374 #ifndef CONFIG_STANDARD_SYSTEM
375 /**
376  * @tc.name: ProxyJudgment002
377  * @tc.desc: act as proxy role, should return true
378  * @tc.type: FUNC
379  */
380 HWTEST_F(IPCNativeUnitTest, ProxyJudgment002, TestSize.Level1)
381 {
382     sptr<IRemoteObject> remote = SystemAbilityManagerClient::GetInstance().GetRegistryRemoteObject();
383     ASSERT_TRUE(remote != nullptr);
384     EXPECT_TRUE(remote->IsProxyObject());
385 }
386 
387 /**
388  * @tc.name: RemoteId001.
389  * @tc.desc:
390  * @tc.type: FUNC
391  */
392 HWTEST_F(IPCNativeUnitTest, RemoteId001, TestSize.Level1)
393 {
394     sptr<IRemoteObject> remote = SystemAbilityManagerClient::GetInstance().GetRegistryRemoteObject();
395     ASSERT_TRUE(remote != nullptr);
396 
397     IPCObjectProxy *proxy = reinterpret_cast<IPCObjectProxy *>(remote.GetRefPtr());
398     ASSERT_TRUE(proxy != nullptr);
399 
400     int remoteId = proxy->GetHandle();
401     EXPECT_GE(remoteId, 0);
402 }
403 #endif
404 
405 /**
406  * @tc.name: ProxyJudgment003
407  * @tc.desc: transform interface instance to object.
408  * @tc.type: FUNC
409  */
410 HWTEST_F(IPCNativeUnitTest, ProxyJudgment003, TestSize.Level1)
411 {
412     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
413     ASSERT_TRUE(saMgr != nullptr);
414 
415     sptr<IRemoteObject> asObject = saMgr->AsObject();
416     ASSERT_TRUE(asObject != nullptr);
417 }
418 
419 /**
420  * @tc.name: ProxyJudgment004
421  * @tc.desc: Press test to validate Get Register instance..
422  * @tc.type: FUNC
423  */
424 HWTEST_F(IPCNativeUnitTest, ProxyJudgment004, TestSize.Level1)
425 {
426     std::vector<sptr<ISystemAbilityManager>> registryObjs;
427     registryObjs.resize(100);
428 
429     for (int i = 0; i < 100; i++) {
430         registryObjs[i] = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
431         ASSERT_TRUE(registryObjs[i] != nullptr);
432     }
433 }
434 
435 /**
436  * @tc.name: MaxWorkThread001
437  * @tc.desc: when multi-transaction called,
438  * the driver will spawn new thread.but it should not exceed the max num.
439  * @tc.type: FUNC
440  */
441 HWTEST_F(IPCNativeUnitTest, MaxWorkThread001, TestSize.Level1)
442 {
443     IPCTestHelper helper;
444     IPCSkeleton::SetMaxWorkThreadNum(8);
445     std::vector<pid_t> childPids;
446     helper.GetChildPids(childPids);
447     ASSERT_GE(childPids.size(), (const unsigned long)1);
448 }
449 
450 /**
451  * @tc.name: SyncTransaction001
452  * @tc.desc: Test IPC data transaction.
453  * @tc.type: FUNC
454  */
455 HWTEST_F(IPCNativeUnitTest, SyncTransaction001, TestSize.Level1)
456 {
457     IPCTestHelper helper;
458     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
459     ASSERT_TRUE(res);
460 
461     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
462     ASSERT_TRUE(saMgr != nullptr);
463 
464     // test get service and call it
465     sptr<IRemoteObject> service = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
466     sptr<ITestService> testService = iface_cast<ITestService>(service);
467     ASSERT_TRUE(testService != nullptr);
468 
469     if (service->IsProxyObject()) {
470         int reply = 0;
471         ZLOGD(LABEL, "Got Proxy node");
472         TestServiceProxy *proxy = static_cast<TestServiceProxy *>(testService.GetRefPtr());
473         int ret = proxy->TestSyncTransaction(2019, reply);
474         EXPECT_EQ(ret, 0);
475         EXPECT_EQ(reply, 9102);
476     } else {
477         ZLOGD(LABEL, "Got Stub node");
478     }
479 }
480 
481 /**
482  * @tc.name: AsyncTransaction001
483  * @tc.desc: Test IPC data transaction.
484  * @tc.type: FUNC
485  * @tc.require: AR000DPV5F
486  */
487 HWTEST_F(IPCNativeUnitTest, AsyncTransaction001, TestSize.Level1)
488 {
489     IPCTestHelper helper;
490     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
491     ASSERT_TRUE(res);
492 
493     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
494     ASSERT_TRUE(saMgr != nullptr);
495 
496     sptr<IRemoteObject> service = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
497     sptr<ITestService> testService = iface_cast<ITestService>(service);
498     ASSERT_TRUE(testService != nullptr);
499 
500     ZLOGD(LABEL, "Get test.service OK\n");
501     if (service->IsProxyObject()) {
502         ZLOGD(LABEL,  "Got Proxy node\n");
503         TestServiceProxy *proxy = static_cast<TestServiceProxy *>(testService.GetRefPtr());
504         int reply = 0;
505         int ret = proxy->TestAsyncTransaction(2019, reply);
506         EXPECT_EQ(ret, ERR_NONE);
507     } else {
508         ZLOGD(LABEL, "Got Stub node\n");
509     }
510 }
511 
512 /**
513  * @tc.name: SyncTransaction002
514  * @tc.desc: Test IPC data transaction.
515  * @tc.type: FUNC
516  * @tc.require: AR000DPV5E
517  */
518 HWTEST_F(IPCNativeUnitTest, SyncTransaction002, TestSize.Level1)
519 {
520     int refCount = 0;
521     IPCTestHelper helper;
522     sptr<TestService> stub = new TestService();
523     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
524     ASSERT_TRUE(saMgr != nullptr);
525 
526     refCount = stub->GetObjectRefCount();
527     EXPECT_EQ(refCount, 1);
528 
529     int result = saMgr->AddSystemAbility(IPC_TEST_SERVICE, new TestService());
530     EXPECT_EQ(result, ERR_NONE);
531 
532     refCount = stub->GetObjectRefCount();
533 
534     if (SUPPORT_ZBINDER) {
535         EXPECT_GE(refCount, 2);
536     } else {
537         EXPECT_GE(refCount, 1);
538     }
539 
540     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_CLIENT);
541     ASSERT_TRUE(res);
542 
543     refCount = stub->GetObjectRefCount();
544     if (SUPPORT_ZBINDER) {
545         EXPECT_GE(refCount, 3);
546     } else {
547         EXPECT_GE(refCount, 1);
548     }
549 
550     helper.StopTestApp(IPCTestHelper::IPC_TEST_CLIENT);
551     refCount = stub->GetObjectRefCount();
552     if (SUPPORT_ZBINDER) {
553         EXPECT_GE(refCount, 2);
554     } else {
555         EXPECT_GE(refCount, 1);
556     }
557 }
558 
559 /**
560  * @tc.name: SyncTransaction003
561  * @tc.desc: Test IPC data transaction.
562  * @tc.type: FUNC
563  * @tc.require: AR000DPV5F
564  */
565 HWTEST_F(IPCNativeUnitTest, SyncTransaction003, TestSize.Level1)
566 {
567     int refCount = 0;
568     IPCTestHelper helper;
569     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
570     ASSERT_TRUE(res);
571 
572     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
573     ASSERT_TRUE(saMgr != nullptr);
574 
575     sptr<IRemoteObject> proxy = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
576     ASSERT_TRUE(proxy != nullptr);
577 
578     refCount = proxy->GetObjectRefCount();
579     if (SUPPORT_ZBINDER) {
580         EXPECT_GE(refCount, 2);
581     } else {
582         EXPECT_GE(refCount, 1);
583     }
584 
585     res = helper.StartTestApp(IPCTestHelper::IPC_TEST_CLIENT);
586     ASSERT_TRUE(res);
587 
588     refCount = proxy->GetObjectRefCount();
589     if (SUPPORT_ZBINDER) {
590         EXPECT_GE(refCount, 3);
591     } else {
592         EXPECT_GE(refCount, 1);
593     }
594 
595     helper.StopTestApp(IPCTestHelper::IPC_TEST_CLIENT);
596     refCount = proxy->GetObjectRefCount();
597 
598     if (SUPPORT_ZBINDER) {
599         EXPECT_GE(refCount, 2);
600     } else {
601         EXPECT_GE(refCount, 1);
602     }
603 }
604 
605 /**
606  * @tc.name: SyncTransaction004
607  * @tc.desc: Test IPC data transaction.
608  * @tc.type: FUNC
609  * @tc.require: AR000DPV5E
610  */
611 HWTEST_F(IPCNativeUnitTest, SyncTransaction004, TestSize.Level1)
612 {
613     IPCTestHelper helper;
614     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
615     ASSERT_TRUE(res);
616 
617     res = helper.StartTestApp(IPCTestHelper::IPC_TEST_CLIENT, static_cast<int>(TestCommand::TEST_CMD_LOOP_TRANSACTION));
618     ASSERT_TRUE(res);
619 
620     std::unique_ptr<TestServiceClient> testClient = std::make_unique<TestServiceClient>();
621     int result = testClient->ConnectService();
622     ASSERT_EQ(result, 0);
623 
624     int count = testClient->StartLoopTest(MAX_TEST_COUNT);
625     EXPECT_EQ(count, MAX_TEST_COUNT);
626 }
627 
628 /**
629  * @tc.name: SyncTransaction005
630  * @tc.desc: Test get context object.
631  * @tc.type: FUNC
632  * @tc.require: SR000DFJQF AR000DFJQG
633  */
634 HWTEST_F(IPCNativeUnitTest, SyncTransaction005, TestSize.Level1)
635 {
636     sptr<IRemoteObject> remote = IPCSkeleton::GetContextObject();
637     ASSERT_TRUE(remote != nullptr);
638 }
639 
640 /**
641  * @tc.name: SyncTransaction006
642  * @tc.desc: Test set context object.
643  * @tc.type: FUNC
644  * @tc.require: SR000DFJQF AR000DFJQG
645 
646  */
647 HWTEST_F(IPCNativeUnitTest, SyncTransaction006, TestSize.Level1)
648 {
649     sptr<IRemoteObject> remoteObj = IPCSkeleton::GetContextObject();
650     ASSERT_TRUE(remoteObj != nullptr);
651     bool ret = IPCSkeleton::SetContextObject(remoteObj);
652     ASSERT_FALSE(ret);
653 }
654 
655 #ifndef CONFIG_STANDARD_SYSTEM
656 /**
657  * @tc.name: SyncTransaction007
658  * @tc.desc: Test get context object through jni.
659  * @tc.type: FUNC
660  * @tc.require: SR000DFJQF AR000DFJQG
661  */
662 HWTEST_F(IPCNativeUnitTest, SyncTransaction007, TestSize.Level1)
663 {
664     JNIEnv *env = nullptr;
665     sptr<IRemoteObject> remoteObj = IPCSkeleton::GetContextObject();
666     ASSERT_TRUE(remoteObj != nullptr);
667     jobject testObj = JNIHelperGetJavaRemoteObject(env, remoteObj);
668     ASSERT_TRUE(testObj == nullptr);
669 }
670 #endif
671 
672 /**
673  * @tc.name: SyncTransaction008
674  * @tc.desc: Test write and read interface token in MessageParcel.
675  * @tc.type: FUNC
676  * @tc.require: SR000DFJQF AR000DFJQG
677  */
678 HWTEST_F(IPCNativeUnitTest, SyncTransaction008, TestSize.Level1)
679 {
680     MessageParcel parcel;
681     std::u16string descriptor = u"TokenDescriptor";
682     parcel.WriteInterfaceToken(descriptor);
683     std::u16string readDescriptor = parcel.ReadInterfaceToken();
684     ASSERT_EQ(readDescriptor, descriptor);
685 }
686 
687 /**
688  * @tc.name: SyncTransaction009
689  * @tc.desc: Test IPC stub data Normal release.
690  * @tc.type: FUNC
691  */
692 HWTEST_F(IPCNativeUnitTest, SyncTransaction009, TestSize.Level1)
693 {
694     IPCTestHelper helper;
695     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
696     ASSERT_TRUE(res);
697 
698     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
699     ASSERT_TRUE(saMgr != nullptr);
700 
701     sptr<IRemoteObject> service = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
702     sptr<ITestService> testService = iface_cast<ITestService>(service);
703     ASSERT_TRUE(testService != nullptr);
704 
705     ZLOGD(LABEL, "Get test.service OK\n");
706     if (service->IsProxyObject()) {
707         ZLOGD(LABEL,  "Got Proxy node\n");
708         TestServiceProxy *proxy = static_cast<TestServiceProxy *>(testService.GetRefPtr());
709         int reply = 0;
710         int ret = proxy->TestAsyncTransaction(2019, reply);
711         EXPECT_EQ(ret, ERR_NONE);
712     } else {
713         ZLOGD(LABEL, "Got Stub node\n");
714     }
715 }
716 
717 /**
718  * @tc.name: MessageOptionTest001
719  * @tc.desc: Test set waiting time.
720  * @tc.type: FUNC
721  * @tc.require: AR000ER7PF
722  */
723 HWTEST_F(IPCNativeUnitTest, MessageOptionTest001, TestSize.Level1)
724 {
725     MessageOption messageOption;
726     ASSERT_EQ(messageOption.GetWaitTime(), MessageOption::TF_WAIT_TIME);
727     messageOption.SetWaitTime(-1);
728     ASSERT_EQ(messageOption.GetWaitTime(), MessageOption::TF_WAIT_TIME);
729 }
730 
731 /**
732  * @tc.name: MessageOptionTest002
733  * @tc.desc:  Verify the SetWaitTime function
734  * @tc.type: FUNC
735  * @tc.require: AR000ER7PF
736  */
737 HWTEST_F(IPCNativeUnitTest, MessageOptionTest002, TestSize.Level1)
738 {
739     MessageOption messageOption;
740     messageOption.SetWaitTime(MAX_WAIT_TIME + 1);
741     ASSERT_EQ(messageOption.GetWaitTime(), MAX_WAIT_TIME);
742 }
743 
744 /**
745  * @tc.name: MessageOptionTest003
746  * @tc.desc:  Verify the SetWaitTime function
747  * @tc.type: FUNC
748  * @tc.require: AR000ER7PF
749  */
750 HWTEST_F(IPCNativeUnitTest, MessageOptionTest003, TestSize.Level1)
751 {
752     MessageOption messageOption;
753     messageOption.SetWaitTime(MessageOption::TF_ASYNC);
754     ASSERT_EQ(messageOption.GetWaitTime(), MessageOption::TF_ASYNC);
755 }
756 
757 /**
758  * @tc.name: AccessTokenid001
759  * @tc.desc: Test IPC AccessTokenid transport
760  * @tc.type: FUNC
761  */
762 HWTEST_F(IPCNativeUnitTest, AccessTokenid001, TestSize.Level1)
763 {
764     IPCTestHelper helper;
765     bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
766     ASSERT_TRUE(res);
767 
768     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
769     ASSERT_TRUE(saMgr != nullptr);
770 
771     // test get service and call it
772     sptr<IRemoteObject> service = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
773     sptr<ITestService> testService = iface_cast<ITestService>(service);
774     ASSERT_TRUE(testService != nullptr);
775 
776     if (service->IsProxyObject()) {
777         ZLOGD(LABEL, "Got Proxy node");
778         TestServiceProxy *proxy = static_cast<TestServiceProxy *>(testService.GetRefPtr());
779         EXPECT_EQ(proxy->TestAccessTokenID64(3560, 3571), 0);
780         EXPECT_EQ(proxy->TestAccessTokenID(3571), 0);
781     } else {
782         ZLOGE(LABEL, "Got Stub node");
783     }
784 }
785 
786 /**
787  * @tc.name: GetStubObjectTest001
788  * @tc.desc: Verify the StubRefCountObject class
789  * @tc.type: FUNC
790  */
791 HWTEST_F(IPCNativeUnitTest, GetStubObjectTest001, TestSize.Level1)
792 {
793     sptr<IRemoteObject> remoteObj = IPCSkeleton::GetContextObject();
794     ASSERT_TRUE(remoteObj != nullptr);
795 
796     IRemoteObject *stub = remoteObj.GetRefPtr();
797     int remotePid = 1;
798     std::string deviceId = "test";
799     StubRefCountObject object(stub, remotePid, deviceId);
800     EXPECT_NE(object.GetStubObject(), nullptr);
801 }
802 
803 /**
804  * @tc.name: GetRemotePidTest002
805  * @tc.desc: Verify the StubRefCountObject::GetRemotePid function
806  * @tc.type: FUNC
807  */
808 HWTEST_F(IPCNativeUnitTest, GetRemotePidTest002, TestSize.Level1)
809 {
810     sptr<IRemoteObject> remoteObj = IPCSkeleton::GetContextObject();
811     ASSERT_TRUE(remoteObj != nullptr);
812 
813     IRemoteObject *stub = remoteObj.GetRefPtr();
814     int remotePid = 1;
815     std::string deviceId = "test";
816     StubRefCountObject object(stub, remotePid, deviceId);
817     int pid = object.GetRemotePid();
818     EXPECT_EQ(pid, 1);
819 }
820 
821 /**
822  * @tc.name: GetDeviceIdTest003
823  * @tc.desc: Verify the StubRefCountObject::GetDeviceId function
824  * @tc.type: FUNC
825  */
826 HWTEST_F(IPCNativeUnitTest, GetDeviceIdTest003, TestSize.Level1)
827 {
828     sptr<IRemoteObject> remoteObj = IPCSkeleton::GetContextObject();
829     ASSERT_TRUE(remoteObj != nullptr);
830 
831     IRemoteObject *stub = remoteObj.GetRefPtr();
832     int remotePid = 1;
833     std::string deviceId = "test";
834     StubRefCountObject object(stub, remotePid, deviceId);
835     std::string res = object.GetDeviceId();
836     EXPECT_STREQ(res.c_str(), deviceId.c_str());
837 }
838 
839 /**
840  * @tc.name: FlushCommandsTest001
841  * @tc.desc: Verify the StubRefCountObject class
842  * @tc.type: FUNC
843  */
844 HWTEST_F(IPCNativeUnitTest, FlushCommandsTest001, TestSize.Level1)
845 {
846     sptr<IRemoteObject> remoteObj = IPCSkeleton::GetContextObject();
847     ASSERT_TRUE(remoteObj != nullptr);
848 
849     int ret = IPCSkeleton::FlushCommands(remoteObj);
850     EXPECT_EQ(ret, ERR_NONE);
851 }
852 
853 /**
854  * @tc.name: CommAuthInfoGetStubObjectTest001
855  * @tc.desc: Verify the CommAuthInfo::GetStubObject function
856  * @tc.type: FUNC
857  */
858 HWTEST_F(IPCNativeUnitTest, CommAuthInfoGetStubObjectTest001, TestSize.Level1)
859 {
860     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
861     sptr<IRemoteObject> object = saMgr->AsObject();
862 
863     std::string deviceId = "testdeviceId";
864     CommAuthInfo commAuthInfo(object, 1, 1, 1, deviceId);
865     ASSERT_TRUE(commAuthInfo.GetStubObject() != nullptr);
866 }
867 
868 /**
869  * @tc.name: CommAuthInfoGetRemotePidTest001
870  * @tc.desc: Verify the CommAuthInfo::GetRemotePid function
871  * @tc.type: FUNC
872  */
873 HWTEST_F(IPCNativeUnitTest, CommAuthInfoGetRemotePidTest001, TestSize.Level1)
874 {
875     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
876     sptr<IRemoteObject> object = saMgr->AsObject();
877 
878     std::string deviceId = "testdeviceId";
879     CommAuthInfo commAuthInfo(object, 1, 1, 1, deviceId);
880     EXPECT_EQ(commAuthInfo.GetRemotePid(), 1);
881 }
882 
883 /**
884  * @tc.name: CommAuthInfoGetRemoteUidTest001
885  * @tc.desc: Verify the CommAuthInfo::GetRemoteUid function
886  * @tc.type: FUNC
887  */
888 HWTEST_F(IPCNativeUnitTest, CommAuthInfoGetRemoteUidTest001, TestSize.Level1)
889 {
890     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
891     sptr<IRemoteObject> object = saMgr->AsObject();
892 
893     std::string deviceId = "testdeviceId";
894     CommAuthInfo commAuthInfo(object, 1, 1, 1, deviceId);
895     EXPECT_EQ(commAuthInfo.GetRemoteUid(), 1);
896 }
897 
898 /**
899  * @tc.name: CommAuthInfoGetRemoteDeviceIdTest001
900  * @tc.desc: Verify the CommAuthInfo::GetRemoteDeviceId function
901  * @tc.type: FUNC
902  */
903 HWTEST_F(IPCNativeUnitTest, CommAuthInfoGetRemoteDeviceIdTest001, TestSize.Level1)
904 {
905     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
906     sptr<IRemoteObject> object = saMgr->AsObject();
907 
908     std::string deviceId = "testdeviceId";
909     CommAuthInfo commAuthInfo(object, 1, 1, 1, deviceId);
910     EXPECT_STREQ(commAuthInfo.GetRemoteDeviceId().c_str(), deviceId.c_str());
911 }
912