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 <gtest/gtest.h>
17 #include <gmock/gmock.h>
18
19 #define private public
20 #define protected public
21 #include "comm_auth_info.h"
22 #include "databus_session_callback.h"
23 #include "dbinder_databus_invoker.h"
24 #include "dbinder_session_object.h"
25 #include "binder_invoker.h"
26 #include "ipc_debug.h"
27 #include "ipc_skeleton.h"
28 #include "ipc_object_proxy.h"
29 #include "ipc_object_stub.h"
30 #include "ipc_process_skeleton.h"
31 #include "ipc_thread_skeleton.h"
32 #include "dbinder_session_object.h"
33 #include "message_option.h"
34 #include "mock_session_impl.h"
35 #include "mock_iremote_invoker.h"
36 #undef protected
37 #undef private
38
39 using namespace testing::ext;
40 using namespace OHOS;
41
42 namespace {
43 constexpr pid_t ALLOWED_UID = 10000;
44 }
45
46 class IPCObjectStubTest : public testing::Test {
47 public:
48 static void SetUpTestCase(void);
49 static void TearDownTestCase(void);
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase()54 void IPCObjectStubTest::SetUpTestCase()
55 {
56 }
57
TearDownTestCase()58 void IPCObjectStubTest::TearDownTestCase()
59 {
60 }
61
SetUp()62 void IPCObjectStubTest::SetUp()
63 {
64 }
65
TearDown()66 void IPCObjectStubTest::TearDown()
67 {
68 }
69
70 /**
71 * @tc.name: OnRemoteDumpTest001
72 * @tc.desc: Verify the OnRemoteDump function
73 * @tc.type: FUNC
74 */
75 HWTEST_F(IPCObjectStubTest, OnRemoteDumpTest001, TestSize.Level1)
76 {
77 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
78 uint32_t code = 0;
79 MessageParcel data;
80 MessageParcel reply;
81 MessageOption option;
82 int result = testStub->OnRemoteDump(code, data, reply, option);
83 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
84 }
85
86 /**
87 * @tc.name: SendRequestTest001
88 * @tc.desc: Verify the SendRequest function
89 * @tc.type: FUNC
90 */
91 HWTEST_F(IPCObjectStubTest, SendRequestTest001, TestSize.Level1)
92 {
93 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
94 uint32_t code = INTERFACE_TRANSACTION;
95 MessageParcel data;
96 MessageParcel reply;
97 MessageOption option;
98 int result = testStub->SendRequest(code, data, reply, option);
99 EXPECT_EQ(result, ERR_NONE);
100 }
101
102 /**
103 * @tc.name: SendRequestTest002
104 * @tc.desc: Verify the SendRequest function
105 * @tc.type: FUNC
106 */
107 HWTEST_F(IPCObjectStubTest, SendRequestTest002, TestSize.Level1)
108 {
109 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
110 uint32_t code = SYNCHRONIZE_REFERENCE;
111 MessageParcel data;
112 MessageParcel reply;
113 MessageOption option;
114 int result = testStub->SendRequest(code, data, reply, option);
115 EXPECT_EQ(result, ERR_NONE);
116 }
117
118 /**
119 * @tc.name: SendRequestTest003
120 * @tc.desc: Verify the SendRequest function
121 * @tc.type: FUNC
122 */
123 HWTEST_F(IPCObjectStubTest, SendRequestTest003, TestSize.Level1)
124 {
125 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
126 uint32_t code = DUMP_TRANSACTION;
127 MessageParcel data;
128 MessageParcel reply;
129 MessageOption option;
130 int result = testStub->SendRequest(code, data, reply, option);
131 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
132 }
133
134 /**
135 * @tc.name: SendRequestTest004
136 * @tc.desc: Verify the SendRequest function
137 * @tc.type: FUNC
138 */
139 HWTEST_F(IPCObjectStubTest, SendRequestTest004, TestSize.Level1)
140 {
141 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
142 uint32_t code = GET_PROTO_INFO;
143 MessageParcel data;
144 MessageParcel reply;
145 MessageOption option;
146 int result = testStub->SendRequest(code, data, reply, option);
147 EXPECT_EQ(result, ERR_NONE);
148 }
149
150 /**
151 * @tc.name: GetLastRequestTimeTest001
152 * @tc.desc: Verify the GetLastRequestTime function
153 * @tc.type: FUNC
154 */
155 HWTEST_F(IPCObjectStubTest, GetLastRequestTimeTest001, TestSize.Level1)
156 {
157 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
158
159 uint64_t ret = testStub->GetLastRequestTime();
160 EXPECT_EQ(ret, 0);
161 }
162
163 /**
164 * @tc.name: GetLastRequestTimeTest002
165 * @tc.desc: Verify the GetLastRequestTime function
166 * @tc.type: FUNC
167 */
168 HWTEST_F(IPCObjectStubTest, GetLastRequestTimeTest002, TestSize.Level1)
169 {
170 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
171 uint32_t code = LAST_CALL_TRANSACTION;
172 MessageParcel data;
173 MessageParcel reply;
174 MessageOption option;
175
176 int res = testStub->SendRequest(code, data, reply, option);
177 EXPECT_EQ(res, IPC_STUB_UNKNOW_TRANS_ERR);
178
179 uint64_t ret = testStub->GetLastRequestTime();
180 ASSERT_TRUE(ret > 0);
181 }
182
183 #ifndef CONFIG_IPC_SINGLE
184 /**
185 * @tc.name: SendRequestTest005
186 * @tc.desc: Verify the SendRequest function
187 * @tc.type: FUNC
188 */
189 HWTEST_F(IPCObjectStubTest, SendRequestTest005, TestSize.Level1)
190 {
191 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
192 uint32_t code = INVOKE_LISTEN_THREAD;
193 MessageParcel data;
194 MessageParcel reply;
195 MessageOption option;
196 int result = testStub->SendRequest(code, data, reply, option);
197 EXPECT_EQ(result, IPC_STUB_INVOKE_THREAD_ERR);
198 }
199
200 /**
201 * @tc.name: SendRequestTest006
202 * @tc.desc: Verify the SendRequest function
203 * @tc.type: FUNC
204 */
205 HWTEST_F(IPCObjectStubTest, SendRequestTest006, TestSize.Level1)
206 {
207 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
208 uint32_t code = INVOKE_LISTEN_THREAD;
209 MessageParcel data;
210 MessageParcel reply;
211 MessageOption option;
212 BinderInvoker *invoker = new BinderInvoker();
213 invoker->status_ = IRemoteInvoker::ACTIVE_INVOKER;
214 invoker->callerUid_ = ALLOWED_UID;
215 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
216 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
217 int result = testStub->SendRequest(code, data, reply, option);
218 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
219 current->invokers_.clear();
220 delete invoker;
221 }
222
223 /**
224 * @tc.name: SendRequestTest007
225 * @tc.desc: Verify the SendRequest function
226 * @tc.type: FUNC
227 */
228 HWTEST_F(IPCObjectStubTest, SendRequestTest007, TestSize.Level1)
229 {
230 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
231 uint32_t code = DBINDER_INCREFS_TRANSACTION;
232 MessageParcel data;
233 MessageParcel reply;
234 MessageOption option;
235
236 BinderInvoker *invoker = new BinderInvoker();
237 invoker->status_ = IRemoteInvoker::ACTIVE_INVOKER;
238 invoker->callerUid_ = ALLOWED_UID;
239 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
240 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
241
242 int result = testStub->SendRequest(code, data, reply, option);
243 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
244 current->invokers_.clear();
245 delete invoker;
246 }
247
248 /**
249 * @tc.name: SendRequestTest008
250 * @tc.desc: Verify the SendRequest function
251 * @tc.type: FUNC
252 */
253 HWTEST_F(IPCObjectStubTest, SendRequestTest008, TestSize.Level1)
254 {
255 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
256 uint32_t code = DBINDER_DECREFS_TRANSACTION;
257 MessageParcel data;
258 MessageParcel reply;
259 MessageOption option;
260 int result = testStub->SendRequest(code, data, reply, option);
261 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
262 }
263
264 /**
265 * @tc.name: SendRequestTest009
266 * @tc.desc: Verify the SendRequest function
267 * @tc.type: FUNC
268 */
269 HWTEST_F(IPCObjectStubTest, SendRequestTest009, TestSize.Level1)
270 {
271 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
272 uint32_t code = DBINDER_DECREFS_TRANSACTION;
273 MessageParcel data;
274 MessageParcel reply;
275 MessageOption option;
276
277 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
278 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
279 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
280
281 DBinderDatabusInvoker *dbinderInvoker = new DBinderDatabusInvoker();
282 current->invokers_[IRemoteObject::IF_PROT_DATABUS] = dbinderInvoker;
283
284 EXPECT_CALL(*invoker, GetStatus())
285 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
286
287 EXPECT_CALL(*invoker, IsLocalCalling())
288 .WillRepeatedly(testing::Return(false));
289
290 int result = testStub->SendRequest(code, data, reply, option);
291 EXPECT_EQ(result, ERR_NONE);
292 current->invokers_.clear();
293 delete invoker;
294 delete dbinderInvoker;
295 }
296
297 /**
298 * @tc.name: SendRequestTest010
299 * @tc.desc: Verify the SendRequest function
300 * @tc.type: FUNC
301 */
302 HWTEST_F(IPCObjectStubTest, SendRequestTest010, TestSize.Level1)
303 {
304 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
305 uint32_t code = DBINDER_ADD_COMMAUTH;
306 MessageParcel data;
307 MessageParcel reply;
308 MessageOption option;
309 int result = testStub->SendRequest(code, data, reply, option);
310 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
311 }
312
313 /**
314 * @tc.name: SendRequestTest011
315 * @tc.desc: Verify the SendRequest function
316 * @tc.type: FUNC
317 */
318 HWTEST_F(IPCObjectStubTest, SendRequestTest011, TestSize.Level1)
319 {
320 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
321 uint32_t code = GET_PID_UID;
322 MessageParcel data;
323 MessageParcel reply;
324 MessageOption option;
325
326 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
327 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
328 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
329 EXPECT_CALL(*invoker, GetStatus())
330 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
331
332 EXPECT_CALL(*invoker, IsLocalCalling())
333 .WillRepeatedly(testing::Return(true));
334
335 int result = testStub->SendRequest(code, data, reply, option);
336 EXPECT_EQ(result, ERR_NONE);
337 delete invoker;
338 current->invokers_.clear();
339 }
340
341 /**
342 * @tc.name: SendRequestTest012
343 * @tc.desc: Verify the SendRequest function
344 * @tc.type: FUNC
345 */
346 HWTEST_F(IPCObjectStubTest, SendRequestTest012, TestSize.Level1)
347 {
348 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
349 uint32_t code = GET_SESSION_NAME;
350 MessageParcel data;
351 MessageParcel reply;
352 MessageOption option;
353 int result = testStub->SendRequest(code, data, reply, option);
354 EXPECT_EQ(result, ERR_NONE);
355 }
356
357 /**
358 * @tc.name: SendRequestTest013
359 * @tc.desc: Verify the SendRequest function
360 * @tc.type: FUNC
361 */
362 HWTEST_F(IPCObjectStubTest, SendRequestTest013, TestSize.Level1)
363 {
364 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
365 uint32_t code = GET_GRANTED_SESSION_NAME;
366 MessageParcel data;
367 MessageParcel reply;
368 MessageOption option;
369
370 int result = testStub->SendRequest(code, data, reply, option);
371 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
372 }
373
374 /**
375 * @tc.name: SendRequestTest014
376 * @tc.desc: Verify the SendRequest function
377 * @tc.type: FUNC
378 */
379 HWTEST_F(IPCObjectStubTest, SendRequestTest014, TestSize.Level1)
380 {
381 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
382 uint32_t code = GET_SESSION_NAME_PID_UID;
383 MessageParcel data;
384 MessageParcel reply;
385 MessageOption option;
386 int result = testStub->SendRequest(code, data, reply, option);
387 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
388 }
389
390 /**
391 * @tc.name: SendRequestTest015
392 * @tc.desc: Verify the SendRequest function
393 * @tc.type: FUNC
394 */
395 HWTEST_F(IPCObjectStubTest, SendRequestTest015, TestSize.Level1)
396 {
397 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
398 uint32_t code = GET_SESSION_NAME;
399 MessageParcel data;
400 MessageParcel reply;
401 MessageOption option;
402
403 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
404 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
405 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
406
407 EXPECT_CALL(*invoker, GetStatus())
408 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
409
410 EXPECT_CALL(*invoker, IsLocalCalling())
411 .WillRepeatedly(testing::Return(false));
412
413 int result = testStub->SendRequest(code, data, reply, option);
414 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
415 current->invokers_.clear();
416 delete invoker;
417 }
418
419 /**
420 * @tc.name: SendRequestTest016
421 * @tc.desc: Verify the SendRequest function
422 * @tc.type: FUNC
423 */
424 HWTEST_F(IPCObjectStubTest, SendRequestTest016, TestSize.Level1)
425 {
426 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
427 uint32_t code = DBINDER_ADD_COMMAUTH;
428 MessageParcel reply;
429 MessageOption option;
430 MessageParcel data;
431
432 uint32_t remotePid = 1;
433 data.WriteUint32(remotePid);
434 uint32_t remoteUid = 1;
435 data.WriteUint32(remoteUid);
436 std::string remoteDeviceId = "testRemoteDeviceId";
437 data.WriteString(remoteDeviceId);
438 uint32_t remoteFeature = 1;
439 data.WriteUint32(remoteFeature);
440 uint64_t stubIndex = 1;
441 data.WriteUint64(stubIndex);
442
443 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
444 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
445 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
446
447 DBinderDatabusInvoker *dbinderInvoker = new DBinderDatabusInvoker();
448 current->invokers_[IRemoteObject::IF_PROT_DATABUS] = dbinderInvoker;
449
450 EXPECT_CALL(*invoker, GetStatus())
451 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
452
453 EXPECT_CALL(*invoker, IsLocalCalling())
454 .WillRepeatedly(testing::Return(false));
455
456 EXPECT_CALL(*invoker, GetCallerUid())
457 .WillRepeatedly(testing::Return(ALLOWED_UID - 1));
458
459 int result = testStub->SendRequest(code, data, reply, option);
460 EXPECT_NE(result, IPC_STUB_INVALID_DATA_ERR);
461 current->invokers_.clear();
462 delete invoker;
463 }
464
465 /**
466 * @tc.name: SendRequestTest017
467 * @tc.desc: Verify the SendRequest function
468 * @tc.type: FUNC
469 */
470 HWTEST_F(IPCObjectStubTest, SendRequestTest017, TestSize.Level1)
471 {
472 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
473 uint32_t code = GET_PID_UID;
474 MessageParcel data;
475 MessageParcel reply;
476 MessageOption option;
477
478 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
479 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
480 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
481 EXPECT_CALL(*invoker, GetStatus())
482 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
483
484 EXPECT_CALL(*invoker, IsLocalCalling())
485 .WillRepeatedly(testing::Return(false));
486
487 int result = testStub->SendRequest(code, data, reply, option);
488 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
489 current->invokers_.clear();
490 delete invoker;
491 }
492 #endif
493
494 /**
495 * @tc.name: SendRequestTest018
496 * @tc.desc: Sending requests in serial mode enabled
497 * @tc.type: FUNC
498 */
499 HWTEST_F(IPCObjectStubTest, SendRequestTest018, TestSize.Level1)
500 {
501 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub", true);
502 uint32_t code = LAST_CALL_TRANSACTION;
503 MessageParcel data;
504 MessageParcel reply;
505 MessageOption option;
506
507 int res = testStub->SendRequest(code, data, reply, option);
508 EXPECT_EQ(res, IPC_STUB_UNKNOW_TRANS_ERR);
509 }
510
511 #ifndef CONFIG_IPC_SINGLE
512
513 /**
514 * @tc.name: InvokerDataBusThread001
515 * @tc.desc: Verify the InvokerDataBusThread function
516 * @tc.type: FUNC
517 */
518 HWTEST_F(IPCObjectStubTest, InvokerDataBusThread001, TestSize.Level1)
519 {
520 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
521
522 MessageParcel data;
523 MessageParcel reply;
524
525 std::string deviceId = "testdeviceId";
526 data.WriteString(deviceId);
527 uint32_t remotePid = 1;
528 data.WriteUint32(remotePid);
529 uint32_t remoteUid = 1;
530 data.WriteUint32(remoteUid);
531 std::string remoteDeviceId = "testremoteDeviceId";
532 data.WriteString(remoteDeviceId);
533 std::string sessionName = "testsessionName";
534 data.WriteString(sessionName);
535 uint32_t featureSet = 1;
536 data.WriteUint32(featureSet);
537
538 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
539 current->stubObjects_[0] = testStub.GetRefPtr();
540 auto ret = testStub->InvokerDataBusThread(data, reply);
541 EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR);
542 current->stubObjects_.clear();
543 }
544
545 /**
546 * @tc.name: InvokerDataBusThread002
547 * @tc.desc: Verify the InvokerDataBusThread function
548 * @tc.type: FUNC
549 */
550 HWTEST_F(IPCObjectStubTest, InvokerDataBusThread002, TestSize.Level1)
551 {
552 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
553
554 MessageParcel data;
555 MessageParcel reply;
556 std::string deviceId = "testdeviceId";
557 data.WriteString(deviceId);
558 uint32_t remotePid = 1;
559 data.WriteUint32(remotePid);
560 uint32_t remoteUid = 1;
561 data.WriteUint32(remoteUid);
562 std::string remoteDeviceId = "testremoteDeviceId";
563 data.WriteString(remoteDeviceId);
564 std::string sessionName = "testsessionName";
565 data.WriteString(sessionName);
566 uint32_t featureSet = 1;
567 data.WriteUint32(featureSet);
568
569 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
570 current->stubObjects_[1] = testStub.GetRefPtr();
571
572 std::string appInfo = remoteDeviceId +
573 std::to_string(remotePid) + std::to_string(remoteUid);
574 current->appInfoToStubIndex_[appInfo] = std::map<uint64_t, uint32_t> { { 1, 1 } };
575
576 auto ret = testStub->InvokerDataBusThread(data, reply);
577 EXPECT_EQ(ret, ERR_NONE);
578 current->stubObjects_.clear();
579 current->appInfoToStubIndex_.clear();
580 }
581
582 /**
583 * @tc.name: InvokerDataBusThread003
584 * @tc.desc: Verify the InvokerDataBusThread function
585 * @tc.type: FUNC
586 */
587 HWTEST_F(IPCObjectStubTest, InvokerDataBusThread003, TestSize.Level1)
588 {
589 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
590
591 MessageParcel data;
592 MessageParcel reply;
593 std::string deviceId = "testdeviceId";
594 data.WriteString(deviceId);
595 uint32_t remotePid = 1;
596 data.WriteUint32(remotePid);
597 uint32_t remoteUid = 1;
598 data.WriteUint32(remoteUid);
599 std::string remoteDeviceId = "testremoteDeviceId";
600 data.WriteString(remoteDeviceId);
601 std::string sessionName = "testsessionName";
602 data.WriteString(sessionName);
603 uint32_t featureSet = 1;
604 data.WriteUint32(featureSet);
605
606 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
607 current->stubObjects_[1] = testStub.GetRefPtr();
608
609 std::string appInfo = remoteDeviceId +
610 std::to_string(remotePid) + std::to_string(remoteUid);
611 current->appInfoToStubIndex_[appInfo] = std::map<uint64_t, uint32_t> { { 0, 1 } };
612
613 auto ret = testStub->InvokerDataBusThread(data, reply);
614 EXPECT_EQ(ret, ERR_NONE);
615 current->stubObjects_.clear();
616 current->appInfoToStubIndex_.clear();
617 }
618
619 /**
620 * @tc.name: InvokerDataBusThread004
621 * @tc.desc: Verify the InvokerDataBusThread function
622 * @tc.type: FUNC
623 */
624 HWTEST_F(IPCObjectStubTest, InvokerDataBusThread004, TestSize.Level1)
625 {
626 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
627
628 MessageParcel data;
629 MessageParcel reply;
630
631 std::string deviceId = "";
632 data.WriteString(deviceId);
633 uint32_t remotePid = 1;
634 data.WriteUint32(remotePid);
635 uint32_t remoteUid = 1;
636 data.WriteUint32(remoteUid);
637 std::string remoteDeviceId = "testremoteDeviceId";
638 data.WriteString(remoteDeviceId);
639 std::string sessionName = "testsessionName";
640 data.WriteString(sessionName);
641 uint32_t featureSet = 1;
642 data.WriteUint32(featureSet);
643
644 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
645 current->stubObjects_[0] = testStub.GetRefPtr();
646 auto ret = testStub->InvokerDataBusThread(data, reply);
647 EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR);
648 current->stubObjects_.clear();
649 }
650
651 /**
652 * @tc.name: InvokerThread001
653 * @tc.desc: Verify the InvokerThread function
654 * @tc.type: FUNC
655 */
656 HWTEST_F(IPCObjectStubTest, InvokerThread001, TestSize.Level1)
657 {
658 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
659
660 uint32_t code = 1;
661 MessageParcel data;
662 MessageParcel reply;
663 uint32_t type = IRemoteObject::DATABUS_TYPE;
664 data.WriteUint32(type);
665 std::string deviceId = "";
666 data.WriteString(deviceId);
667 uint32_t remotePid = 1;
668 data.WriteUint32(remotePid);
669 uint32_t remoteUid = 1;
670 data.WriteUint32(remoteUid);
671 std::string remoteDeviceId = "";
672 data.WriteString(remoteDeviceId);
673 std::string sessionName = "";
674 data.WriteString(sessionName);
675 uint32_t featureSet = 1;
676 data.WriteUint32(featureSet);
677 MessageOption option;
678
679 auto ret = testStub->InvokerThread(code, data, reply, option);
680 EXPECT_EQ(ret, IPC_STUB_INVOKE_THREAD_ERR);
681 }
682
683 /**
684 * @tc.name: NoticeServiceDieTest001
685 * @tc.desc: Verify the NoticeServiceDie function
686 * @tc.type: FUNC
687 */
688 HWTEST_F(IPCObjectStubTest, NoticeServiceDieTest001, TestSize.Level1)
689 {
690 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
691
692 sptr<IPCObjectProxy> objectProxy = new IPCObjectProxy(
693 1, u"test", IPCProcessSkeleton::DBINDER_HANDLE_BASE);
694 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
695 current->noticeStub_[objectProxy.GetRefPtr()] = testStub;
696
697 MessageParcel data;
698 MessageParcel reply;
699 MessageOption option;
700 auto result = testStub->NoticeServiceDie(data, reply, option);
701 EXPECT_EQ(result, ERR_NONE);
702 current->noticeStub_.clear();
703 }
704
705 /**
706 * @tc.name: NoticeServiceDieTest002
707 * @tc.desc: Verify the NoticeServiceDie function
708 * @tc.type: FUNC
709 */
710 HWTEST_F(IPCObjectStubTest, NoticeServiceDieTest002, TestSize.Level1)
711 {
712 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
713
714 MessageParcel data;
715 MessageParcel reply;
716 MessageOption option;
717 auto result = testStub->NoticeServiceDie(data, reply, option);
718 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
719 }
720
721 /**
722 * @tc.name: AddAuthInfoeTest001
723 * @tc.desc: Verify the AddAuthInfoe function
724 * @tc.type: FUNC
725 */
726 HWTEST_F(IPCObjectStubTest, AddAuthInfoeTest001, TestSize.Level1)
727 {
728 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
729 MessageParcel data;
730 MessageParcel reply;
731 uint32_t code = 0;
732 int32_t ret = testStub->AddAuthInfo(data, reply, code);
733 EXPECT_EQ(ret, IPC_STUB_INVALID_DATA_ERR);
734 }
735
736 /**
737 * @tc.name: AddAuthInfoeTest002
738 * @tc.desc: Verify the AddAuthInfoe function
739 * @tc.type: FUNC
740 */
741 HWTEST_F(IPCObjectStubTest, AddAuthInfoeTest002, TestSize.Level1)
742 {
743 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
744 MessageParcel data;
745 uint32_t remotePid = 1;
746 data.WriteUint32(remotePid);
747 uint32_t remoteUid = 1;
748 data.WriteUint32(remoteUid);
749 std::string remoteDeviceId = "testRemoteDeviceId";
750 data.WriteString(remoteDeviceId);
751 uint32_t remoteFeature = 1;
752 data.WriteUint32(remoteFeature);
753 uint64_t stubIndex = 1;
754 data.WriteUint64(stubIndex);
755
756 uint32_t tokenId = 1;
757 MessageParcel reply;
758 uint32_t code = DBINDER_ADD_COMMAUTH;
759
760 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
761 std::shared_ptr<CommAuthInfo> info =
762 std::make_shared<CommAuthInfo>(testStub.GetRefPtr(), remotePid, remoteUid, tokenId, remoteDeviceId);
763 current->commAuth_.push_back(info);
764
765 int32_t ret = testStub->AddAuthInfo(data, reply, code);
766 EXPECT_EQ(ret, ERR_NONE);
767 info->stub_ = nullptr;
768 current->commAuth_.remove(info);
769 }
770
771 /**
772 * @tc.name: AddAuthInfoeTest003
773 * @tc.desc: Verify the AddAuthInfoe function
774 * @tc.type: FUNC
775 */
776 HWTEST_F(IPCObjectStubTest, AddAuthInfoeTest003, TestSize.Level1)
777 {
778 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
779 MessageParcel data;
780 uint32_t remotePid = 1;
781 data.WriteUint32(remotePid);
782 uint32_t remoteUid = 1;
783 data.WriteUint32(remoteUid);
784 std::string remoteDeviceId = "testRemoteDeviceId";
785 data.WriteString(remoteDeviceId);
786 uint32_t remoteFeature = 1;
787 data.WriteUint32(remoteFeature);
788 uint64_t stubIndex = 1;
789 data.WriteUint64(stubIndex);
790
791 MessageParcel reply;
792 uint32_t code = DBINDER_ADD_COMMAUTH;
793
794 int32_t ret = testStub->AddAuthInfo(data, reply, code);
795 EXPECT_EQ(ret, ERR_NONE);
796 }
797
798 /**
799 * @tc.name: AddAuthInfoeTest004
800 * @tc.desc: Verify the AddAuthInfoe function
801 * @tc.type: FUNC
802 */
803 HWTEST_F(IPCObjectStubTest, AddAuthInfoeTest004, TestSize.Level1)
804 {
805 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
806 MessageParcel data;
807 uint32_t remotePid = 1;
808 data.WriteUint32(remotePid);
809 uint32_t remoteUid = 1;
810 data.WriteUint32(remoteUid);
811 std::string remoteDeviceId = "testRemoteDeviceId";
812 data.WriteString(remoteDeviceId);
813 uint32_t remoteFeature = 1;
814 data.WriteUint32(remoteFeature);
815 uint64_t stubIndex = 1;
816 data.WriteUint64(stubIndex);
817 uint32_t tokenId = 1;
818 MessageParcel reply;
819 uint32_t code = DBINDER_ADD_COMMAUTH;
820
821 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
822 std::shared_ptr<CommAuthInfo> info =
823 std::make_shared<CommAuthInfo>(testStub.GetRefPtr(), remotePid, remoteUid, tokenId, remoteDeviceId);
824 current->commAuth_.push_back(info);
825
826 int32_t ret = testStub->AddAuthInfo(data, reply, code);
827 EXPECT_EQ(ret, ERR_NONE);
828 info->stub_ = nullptr;
829 current->commAuth_.remove(info);
830 }
831
832 /**
833 * @tc.name: AddAuthInfoeTest005
834 * @tc.desc: Verify the AddAuthInfoe function
835 * @tc.type: FUNC
836 */
837 HWTEST_F(IPCObjectStubTest, AddAuthInfoeTest005, TestSize.Level1)
838 {
839 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
840 MessageParcel data;
841 uint32_t remotePid = 1;
842 data.WriteUint32(remotePid);
843 uint32_t remoteUid = 1;
844 data.WriteUint32(remoteUid);
845 std::string remoteDeviceId = "testRemoteDeviceId";
846 data.WriteString(remoteDeviceId);
847 uint32_t remoteFeature = 1;
848 data.WriteUint32(remoteFeature);
849 uint64_t stubIndex = 0;
850 data.WriteUint64(stubIndex);
851 uint32_t tokenId = 1;
852 MessageParcel reply;
853 uint32_t code = DBINDER_ADD_COMMAUTH;
854
855 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
856 std::shared_ptr<CommAuthInfo> info =
857 std::make_shared<CommAuthInfo>(testStub.GetRefPtr(), remotePid, remoteUid, tokenId, remoteDeviceId);
858 current->commAuth_.push_back(info);
859
860 int32_t ret = testStub->AddAuthInfo(data, reply, code);
861 EXPECT_EQ(ret, ERR_NONE);
862 info->stub_ = nullptr;
863 current->commAuth_.remove(info);
864 }
865
866 /**
867 * @tc.name: AddAuthInfoeTest006
868 * @tc.desc: Verify the AddAuthInfoe function
869 * @tc.type: FUNC
870 */
871 HWTEST_F(IPCObjectStubTest, AddAuthInfoeTest006, TestSize.Level1)
872 {
873 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
874 MessageParcel data;
875 uint32_t remotePid = 1;
876 data.WriteUint32(remotePid);
877 uint32_t remoteUid = 1;
878 data.WriteUint32(remoteUid);
879 std::string remoteDeviceId = "testRemoteDeviceId";
880 data.WriteString(remoteDeviceId);
881 uint32_t remoteFeature = 1;
882 data.WriteUint32(remoteFeature);
883 uint64_t stubIndex = 1;
884 data.WriteUint64(stubIndex);
885 uint32_t tokenId = 1;
886 MessageParcel reply;
887 uint32_t code = DBINDER_ADD_COMMAUTH;
888
889 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
890 std::shared_ptr<CommAuthInfo> info =
891 std::make_shared<CommAuthInfo>(testStub.GetRefPtr(), remotePid, remoteUid, tokenId, remoteDeviceId);
892 current->commAuth_.push_back(info);
893
894 std::string appInfo = remoteDeviceId + std::to_string(remotePid) + std::to_string(remoteUid);
895 current->appInfoToStubIndex_[appInfo] = std::map<uint64_t, uint32_t> { { 0, 1 } };
896
897 int32_t ret = testStub->AddAuthInfo(data, reply, code);
898 EXPECT_EQ(ret, ERR_NONE);
899 info->stub_ = nullptr;
900 current->commAuth_.remove(info);
901 }
902
903 /**
904 * @tc.name: IsSamgrCallTest001
905 * @tc.desc: Verify the IsSamgrCall function
906 * @tc.type: FUNC
907 */
908 HWTEST_F(IPCObjectStubTest, IsSamgrCallTest001, TestSize.Level1)
909 {
910 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
911
912 auto ret = testStub->IsSamgrCall();
913 ASSERT_FALSE(ret);
914 }
915
916 /**
917 * @tc.name: GetGrantedSessionNameTest001
918 * @tc.desc: Verify the GetGrantedSessionName function
919 * @tc.type: FUNC
920 */
921 HWTEST_F(IPCObjectStubTest, GetGrantedSessionNameTest001, TestSize.Level1)
922 {
923 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
924 uint32_t code = DBINDER_DECREFS_TRANSACTION;
925 MessageParcel data;
926 MessageParcel reply;
927 MessageOption option;
928
929 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
930 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
931 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
932
933 EXPECT_CALL(*invoker, GetStatus())
934 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
935
936 EXPECT_CALL(*invoker, GetCallerPid())
937 .WillRepeatedly(testing::Return(1111));
938
939 EXPECT_CALL(*invoker, GetCallerUid())
940 .WillRepeatedly(testing::Return(1112));
941
942 EXPECT_CALL(*invoker, SendRequest(testing::_, testing::_, testing::_, testing::_, testing::_))
943 .WillRepeatedly(testing::Return(1));
944
945 int result = testStub->GetGrantedSessionName(code, data, reply, option);
946 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
947 current->invokers_.clear();
948 delete invoker;
949 }
950
951 /**
952 * @tc.name: GetSessionNameForPidUidTest001
953 * @tc.desc: Verify the GetSessionNameForPidUid function
954 * @tc.type: FUNC
955 */
956 HWTEST_F(IPCObjectStubTest, GetSessionNameForPidUidTest001, TestSize.Level1)
957 {
958 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
959 uint32_t code = DBINDER_DECREFS_TRANSACTION;
960 MessageParcel data;
961 MessageParcel reply;
962 MessageOption option;
963
964 uint32_t remotePid = 1111;
965 data.WriteUint32(remotePid);
966 uint32_t remoteUid = 1112;
967 data.WriteUint32(remoteUid);
968
969 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
970 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
971 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
972
973 EXPECT_CALL(*invoker, GetStatus())
974 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
975
976 EXPECT_CALL(*invoker, GetCallerPid())
977 .WillRepeatedly(testing::Return(1111));
978
979 EXPECT_CALL(*invoker, GetCallerUid())
980 .WillRepeatedly(testing::Return(1112));
981
982 EXPECT_CALL(*invoker, SendRequest(testing::_, testing::_, testing::_, testing::_, testing::_))
983 .WillRepeatedly(testing::Return(1));
984
985 int result = testStub->GetSessionNameForPidUid(code, data, reply, option);
986 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
987 current->invokers_.clear();
988 delete invoker;
989 }
990
991 /**
992 * @tc.name: GetSessionNameForPidUidTest002
993 * @tc.desc: Verify the GetSessionNameForPidUid function
994 * @tc.type: FUNC
995 */
996 HWTEST_F(IPCObjectStubTest, GetSessionNameForPidUidTest002, TestSize.Level1)
997 {
998 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
999 uint32_t code = DBINDER_DECREFS_TRANSACTION;
1000 MessageParcel data;
1001 MessageParcel reply;
1002 MessageOption option;
1003
1004 uint32_t remotePid = 1111;
1005 data.WriteUint32(remotePid);
1006 uint32_t remoteUid = 1112;
1007 data.WriteUint32(remoteUid);
1008
1009 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
1010 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
1011 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
1012
1013 EXPECT_CALL(*invoker, GetStatus())
1014 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
1015
1016 EXPECT_CALL(*invoker, GetCallerPid())
1017 .WillRepeatedly(testing::Return(1113));
1018
1019 EXPECT_CALL(*invoker, GetCallerUid())
1020 .WillRepeatedly(testing::Return(1114));
1021
1022 EXPECT_CALL(*invoker, SendRequest(testing::_, testing::_, testing::_, testing::_, testing::_))
1023 .WillRepeatedly(testing::Return(1));
1024
1025 int result = testStub->GetSessionNameForPidUid(code, data, reply, option);
1026 EXPECT_EQ(result, IPC_STUB_INVALID_DATA_ERR);
1027 current->invokers_.clear();
1028 delete invoker;
1029 }
1030
1031 /**
1032 * @tc.name: CreateSessionNameTest002
1033 * @tc.desc: Verify the CreateSessionName function
1034 * @tc.type: FUNC
1035 */
1036 HWTEST_F(IPCObjectStubTest, CreateSessionNameTest002, TestSize.Level1)
1037 {
1038 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
1039
1040 int uid = 1111;
1041 int pid = 1112;
1042
1043 auto ret = testStub->CreateSessionName(uid, pid);
1044 ASSERT_TRUE(ret.size() == 0);
1045 }
1046 #endif
1047
1048 /**
1049 * @tc.name: GetCallingTokenIDTest001
1050 * @tc.desc: Verify the GetCallingTokenID function
1051 * @tc.type: FUNC
1052 */
1053 HWTEST_F(IPCObjectStubTest, GetCallingTokenIDTest001, TestSize.Level1)
1054 {
1055 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
1056 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
1057 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
1058
1059 EXPECT_CALL(*invoker, GetStatus())
1060 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
1061
1062 EXPECT_CALL(*invoker, GetCallerTokenID())
1063 .WillRepeatedly(testing::Return(1));
1064
1065 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
1066
1067 auto ret = testStub->GetCallingTokenID();
1068 EXPECT_EQ(ret, 1);
1069 current->invokers_.clear();
1070 delete invoker;
1071 }
1072
1073 /**
1074 * @tc.name: GetCallingFullTokenIDTest001
1075 * @tc.desc: Verify the GetCallingFullTokenID function
1076 * @tc.type: FUNC
1077 */
1078 HWTEST_F(IPCObjectStubTest, GetCallingFullTokenIDTest001, TestSize.Level1)
1079 {
1080 MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
1081 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
1082 current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
1083
1084 EXPECT_CALL(*invoker, GetStatus())
1085 .WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
1086
1087 EXPECT_CALL(*invoker, GetCallerTokenID())
1088 .WillRepeatedly(testing::Return(1));
1089 EXPECT_CALL(*invoker, GetSelfTokenID())
1090 .WillRepeatedly(testing::Return(1));
1091
1092 sptr<IPCObjectStub> testStub = new IPCObjectStub(u"testStub");
1093
1094 auto ret = testStub->GetCallingFullTokenID();
1095 EXPECT_EQ(ret, 1);
1096 current->invokers_.clear();
1097 delete invoker;
1098 }