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 #include "ipc_types.h"
16 #include "iremote_object.h"
17 #include "log_tags.h"
18 #include "message_parcel.h"
19 #include "rpc_log.h"
20 #include "gtest/gtest.h"
21 #include "securec.h"
22 #include <iostream>
23
24 #define private public
25 #define protected public
26 #include "dbinder_service.h"
27 #include "dbinder_service_stub.h"
28 #include "sys_binder.h"
29 #undef protected
30 #undef private
31
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::HiviewDFX;
35
36 namespace {
37 constexpr binder_uintptr_t BINDER_OBJECT = 11;
38 constexpr uint32_t PROCESS_PROTO_CODE = 11;
39 constexpr uint32_t SESSION_TYPE_UNKNOWN = 99;
40 constexpr int32_t UNKNOWN_TRANSACTION_CODE = 999;
41 }
42
43 typedef unsigned long long binder_uintptr_t;
44 class DBinderServiceStubUnitTest : public testing::Test {
45 public:
46 static void SetUpTestCase(void);
47 static void TearDownTestCase(void);
48 void SetUp();
49 void TearDown();
50 };
51
SetUp()52 void DBinderServiceStubUnitTest::SetUp()
53 {}
54
TearDown()55 void DBinderServiceStubUnitTest::TearDown()
56 {}
57
SetUpTestCase()58 void DBinderServiceStubUnitTest::SetUpTestCase()
59 {}
60
TearDownTestCase()61 void DBinderServiceStubUnitTest::TearDownTestCase()
62 {}
63
64 /**
65 * @tc.name: DBinderServiceStub001
66 * @tc.desc: Verify the DBinderServiceStub function
67 * @tc.type: FUNC
68 */
69 HWTEST_F(DBinderServiceStubUnitTest, DBinderServiceStub001, TestSize.Level1)
70 {
71 const std::string service = "serviceTest";
72 const std::string device = "deviceTest";
73 binder_uintptr_t object = BINDER_OBJECT;
74 DBinderServiceStub dBinderServiceStub(service, device, object);
75 int32_t num = dBinderServiceStub.GetObjectRefCount();
76 EXPECT_NE(num, 0);
77 }
78
79 /**
80 * @tc.name: DBinderServiceStub002
81 * @tc.desc: Verify the DBinderServiceStub function
82 * @tc.type: FUNC
83 */
84 HWTEST_F(DBinderServiceStubUnitTest, DBinderServiceStub002, TestSize.Level1)
85 {
86 const std::string service = "";
87 const std::string device = "deviceTest";
88 binder_uintptr_t object = BINDER_OBJECT;
89 DBinderServiceStub dBinderServiceStub(service, device, object);
90 int32_t num = dBinderServiceStub.GetObjectRefCount();
91 EXPECT_NE(num, 0);
92 }
93
94 /**
95 * @tc.name: DBinderServiceStub003
96 * @tc.desc: Verify the DBinderServiceStub function
97 * @tc.type: FUNC
98 */
99 HWTEST_F(DBinderServiceStubUnitTest, DBinderServiceStub003, TestSize.Level1)
100 {
101 const std::string service = "serviceTest";
102 const std::string device = "";
103 binder_uintptr_t object = BINDER_OBJECT;
104 DBinderServiceStub dBinderServiceStub(service, device, object);
105 int32_t num = dBinderServiceStub.GetObjectRefCount();
106 EXPECT_NE(num, 0);
107 }
108
109 /**
110 * @tc.name: DBinderServiceStub004
111 * @tc.desc: Verify the DBinderServiceStub function
112 * @tc.type: FUNC
113 */
114 HWTEST_F(DBinderServiceStubUnitTest, DBinderServiceStub004, TestSize.Level1)
115 {
116 const std::string service = "serviceTest";
117 const std::string device = "deviceTest";
118 binder_uintptr_t object = UINT_MAX;
119 DBinderServiceStub dBinderServiceStub(service, device, object);
120 int32_t num = dBinderServiceStub.GetObjectRefCount();
121 EXPECT_NE(num, 0);
122 }
123
124
125 /**
126 * @tc.name: GetServiceName001
127 * @tc.desc: Verify the GetServiceName function
128 * @tc.type: FUNC
129 */
130 HWTEST_F(DBinderServiceStubUnitTest, GetServiceName001, TestSize.Level1)
131 {
132 const std::string service = "serviceTest";
133 const std::string device = "deviceTest";
134 binder_uintptr_t object = BINDER_OBJECT;
135 DBinderServiceStub dBinderServiceStub(service, device, object);
136 std::string ret = dBinderServiceStub.GetServiceName();
137 EXPECT_EQ(ret, "serviceTest");
138 }
139
140 /**
141 * @tc.name: GetServiceName002
142 * @tc.desc: Verify the GetServiceName function
143 * @tc.type: FUNC
144 */
145 HWTEST_F(DBinderServiceStubUnitTest, GetServiceName002, TestSize.Level1)
146 {
147 const std::string service;
148 const std::string device = "deviceTest";
149 binder_uintptr_t object = BINDER_OBJECT;
150 DBinderServiceStub dBinderServiceStub(service, device, object);
151 std::string ret = dBinderServiceStub.GetServiceName();
152 EXPECT_EQ(ret, "");
153 }
154
155 /**
156 * @tc.name: GetDeviceID001
157 * @tc.desc: Verify the GetDeviceID function
158 * @tc.type: FUNC
159 */
160 HWTEST_F(DBinderServiceStubUnitTest, GetDeviceID001, TestSize.Level1)
161 {
162 const std::string service = "serviceTest";
163 const std::string device = "deviceTest";
164 binder_uintptr_t object = BINDER_OBJECT;
165 DBinderServiceStub dBinderServiceStub(service, device, object);
166 std::string ret = dBinderServiceStub.GetDeviceID();
167 EXPECT_EQ(ret, "deviceTest");
168 }
169
170 /**
171 * @tc.name: GetDeviceID002
172 * @tc.desc: Verify the GetDeviceID function
173 * @tc.type: FUNC
174 */
175 HWTEST_F(DBinderServiceStubUnitTest, GetDeviceID002, TestSize.Level1)
176 {
177 const std::string service = "serviceTest";
178 const std::string device;
179 binder_uintptr_t object = BINDER_OBJECT;
180 DBinderServiceStub dBinderServiceStub(service, device, object);
181 std::string ret = dBinderServiceStub.GetDeviceID();
182 EXPECT_EQ(ret, "");
183 }
184
185 /**
186 * @tc.name: GetBinderObject001
187 * @tc.desc: Verify the GetBinderObject function
188 * @tc.type: FUNC
189 */
190 HWTEST_F(DBinderServiceStubUnitTest, GetBinderObject001, TestSize.Level1)
191 {
192 const std::string service = "serviceTest";
193 const std::string device = "deviceTest";
194 binder_uintptr_t object = BINDER_OBJECT;
195 DBinderServiceStub dBinderServiceStub(service, device, object);
196 binder_uintptr_t ret = dBinderServiceStub.GetBinderObject();
197 EXPECT_EQ(ret, BINDER_OBJECT);
198 }
199
200 /**
201 * @tc.name: GetBinderObject002
202 * @tc.desc: Verify the GetBinderObject function
203 * @tc.type: FUNC
204 */
205 HWTEST_F(DBinderServiceStubUnitTest, GetBinderObject002, TestSize.Level1)
206 {
207 const std::string service = "serviceTest";
208 const std::string device = "deviceTest";
209 binder_uintptr_t object = UINT_MAX;
210 DBinderServiceStub dBinderServiceStub(service, device, object);
211 binder_uintptr_t ret = dBinderServiceStub.GetBinderObject();
212 EXPECT_EQ(ret, UINT_MAX);
213 }
214
215 /**
216 * @tc.name: ProcessProto001
217 * @tc.desc: Verify the ProcessProto function
218 * @tc.type: FUNC
219 */
220 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto001, TestSize.Level1)
221 {
222 const std::string service = "serviceTest";
223 const std::string device = "deviceTest";
224 binder_uintptr_t object = BINDER_OBJECT;
225 DBinderServiceStub dBinderServiceStub(service, device, object);
226 uint32_t code = PROCESS_PROTO_CODE;
227 MessageParcel data;
228 MessageParcel reply;
229 MessageOption option;
230 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
231 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
232 }
233
234 /**
235 * @tc.name: ProcessProto002
236 * @tc.desc: Verify the ProcessProto function
237 * @tc.type: FUNC
238 */
239 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto002, TestSize.Level1)
240 {
241 const std::string service = "serviceTest";
242 const std::string device = "deviceTest";
243 binder_uintptr_t object = BINDER_OBJECT;
244 DBinderServiceStub dBinderServiceStub(service, device, object);
245 binder_uintptr_t key = reinterpret_cast<binder_uintptr_t>(&dBinderServiceStub);
246 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
247 EXPECT_TRUE(dBinderService != nullptr);
248 std::shared_ptr<struct SessionInfo> sessionInfo = std::make_shared<struct SessionInfo>();
249 EXPECT_TRUE(sessionInfo != nullptr);
250 dBinderService->sessionObject_[key] = sessionInfo;
251 uint32_t code = PROCESS_PROTO_CODE;
252 MessageParcel data;
253 MessageParcel reply;
254 MessageOption option;
255 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
256 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
257 }
258
259 /**
260 * @tc.name: ProcessProto003
261 * @tc.desc: Verify the ProcessProto function
262 * @tc.type: FUNC
263 */
264 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto003, TestSize.Level1)
265 {
266 const std::string service = "serviceTest";
267 const std::string device = "deviceTest";
268 binder_uintptr_t object = BINDER_OBJECT;
269 DBinderServiceStub dBinderServiceStub(service, device, object);
270 uint32_t code = PROCESS_PROTO_CODE;
271 MessageParcel data;
272 MessageParcel reply;
273 MessageOption option;
274 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
275 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
276 }
277
278 /**
279 * @tc.name: ProcessProto004
280 * @tc.desc: Verify the ProcessProto function with unknown session type
281 * @tc.type: FUNC
282 */
283 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto004, TestSize.Level1)
284 {
285 const std::string service = "serviceTest";
286 const std::string device = "deviceTest";
287 binder_uintptr_t object = BINDER_OBJECT;
288 DBinderServiceStub dBinderServiceStub(service, device, object);
289
290 uint32_t code = PROCESS_PROTO_CODE;
291 MessageParcel data;
292 MessageParcel reply;
293 MessageOption option;
294
295 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
296 EXPECT_TRUE(dBinderService != nullptr);
297 binder_uintptr_t key = reinterpret_cast<binder_uintptr_t>(&dBinderServiceStub);
298 std::shared_ptr<struct SessionInfo> sessionInfo = std::make_shared<struct SessionInfo>();
299 EXPECT_TRUE(sessionInfo != nullptr);
300 sessionInfo->type = SESSION_TYPE_UNKNOWN;
301 dBinderService->sessionObject_[key] = sessionInfo;
302
303 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
304 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
305 }
306
307 /**
308 * @tc.name: ProcessProto005
309 * @tc.desc: Verify the ProcessProto function
310 * @tc.type: FUNC
311 */
312 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto005, TestSize.Level1)
313 {
314 const std::string service = "serviceTest";
315 const std::string device = "deviceTest";
316 binder_uintptr_t object = BINDER_OBJECT;
317 DBinderServiceStub dBinderServiceStub(service, device, object);
318 uint32_t code = UINT_MAX;
319 MessageParcel data;
320 MessageParcel reply;
321 MessageOption option;
322 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
323 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
324 }
325
326 /**
327 * @tc.name: OnRemoteRequest001
328 * @tc.desc: Verify the OnRemoteRequest function
329 * @tc.type: FUNC
330 */
331 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest001, TestSize.Level1)
332 {
333 const std::string service = "serviceTest";
334 const std::string device = "deviceTest";
335 binder_uintptr_t object = BINDER_OBJECT;
336 DBinderServiceStub dBinderServiceStub(service, device, object);
337
338 uint32_t code = GET_PROTO_INFO;
339 MessageParcel data;
340 MessageParcel reply;
341 MessageOption option;
342 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
343 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
344 }
345
346 /**
347 * @tc.name: OnRemoteRequest002
348 * @tc.desc: Verify the OnRemoteRequest function
349 * @tc.type: FUNC
350 */
351 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest002, TestSize.Level1)
352 {
353 const std::string service = "serviceTest";
354 const std::string device = "deviceTest";
355 binder_uintptr_t object = BINDER_OBJECT;
356 DBinderServiceStub dBinderServiceStub(service, device, object);
357
358 uint32_t code = DBINDER_OBITUARY_TRANSACTION;
359 MessageParcel data;
360 MessageParcel reply;
361 MessageOption option;
362 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
363 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
364 }
365
366 /**
367 * @tc.name: OnRemoteRequest003
368 * @tc.desc: Verify the OnRemoteRequest function
369 * @tc.type: FUNC
370 */
371 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest003, TestSize.Level1)
372 {
373 const std::string service = "serviceTest";
374 const std::string device = "deviceTest";
375 binder_uintptr_t object = BINDER_OBJECT;
376 DBinderServiceStub dBinderServiceStub(service, device, object);
377
378 uint32_t code = PROCESS_PROTO_CODE;
379 MessageParcel data;
380 MessageParcel reply;
381 MessageOption option;
382 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
383 EXPECT_EQ(ret, DBINDER_SERVICE_UNKNOW_TRANS_ERR);
384 }
385
386 /**
387 * @tc.name: ProcessDeathRecipient001
388 * @tc.desc: Verify the ProcessDeathRecipient function
389 * @tc.type: FUNC
390 */
391 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient001, TestSize.Level1)
392 {
393 const std::string service = "serviceTest";
394 const std::string device = "deviceTest";
395 binder_uintptr_t object = BINDER_OBJECT;
396 DBinderServiceStub dBinderServiceStub(service, device, object);
397
398 MessageParcel data;
399 data.WriteInt32(IRemoteObject::DeathRecipient::ADD_DEATH_RECIPIENT);
400 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data);
401 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
402 }
403
404 /**
405 * @tc.name: ProcessDeathRecipient002
406 * @tc.desc: Verify the ProcessDeathRecipient function
407 * @tc.type: FUNC
408 */
409 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient002, TestSize.Level1)
410 {
411 const std::string service = "serviceTest";
412 const std::string device = "deviceTest";
413 binder_uintptr_t object = BINDER_OBJECT;
414 DBinderServiceStub dBinderServiceStub(service, device, object);
415
416 MessageParcel data;
417 data.WriteInt32(IRemoteObject::DeathRecipient::REMOVE_DEATH_RECIPIENT);
418 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data);
419 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
420 }
421
422 /**
423 * @tc.name: ProcessDeathRecipient003
424 * @tc.desc: Verify the ProcessDeathRecipient function with unknown type
425 * @tc.type: FUNC
426 */
427 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient003, TestSize.Level1)
428 {
429 const std::string service = "serviceTest";
430 const std::string device = "deviceTest";
431 binder_uintptr_t object = BINDER_OBJECT;
432 DBinderServiceStub dBinderServiceStub(service, device, object);
433
434 MessageParcel data;
435 data.WriteInt32(UNKNOWN_TRANSACTION_CODE);
436
437 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data);
438 EXPECT_EQ(ret, DBINDER_SERVICE_UNKNOW_TRANS_ERR);
439 }
440
441 /**
442 * @tc.name: ProcessDeathRecipient004
443 * @tc.desc: Verify the ProcessDeathRecipient function with unknown type
444 * @tc.type: FUNC
445 */
446 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient004, TestSize.Level1)
447 {
448 const std::string service = "serviceTest";
449 const std::string device = "deviceTest";
450 binder_uintptr_t object = BINDER_OBJECT;
451 DBinderServiceStub dBinderServiceStub(service, device, object);
452
453 MessageParcel data;
454 data.WriteString("");
455
456 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data);
457 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
458 }
459
460 /**
461 * @tc.name: AddDbinderDeathRecipient001
462 * @tc.desc: Verify the AddDbinderDeathRecipient function
463 * @tc.type: FUNC
464 */
465 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient001, TestSize.Level1)
466 {
467 const std::string service = "serviceTest";
468 const std::string device = "deviceTest";
469 binder_uintptr_t object = BINDER_OBJECT;
470 DBinderServiceStub dBinderServiceStub(service, device, object);
471 MessageParcel data;
472 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data);
473 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
474 }
475
476 /**
477 * @tc.name: AddDbinderDeathRecipient002
478 * @tc.desc: Verify the AddDbinderDeathRecipient function
479 * @tc.type: FUNC
480 */
481 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient002, TestSize.Level1)
482 {
483 const std::string service = "serviceTest";
484 const std::string device = "deviceTest";
485 binder_uintptr_t object = BINDER_OBJECT;
486 DBinderServiceStub dBinderServiceStub(service, device, object);
487
488 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub(u"testStub");
489 EXPECT_TRUE(callbackStub != nullptr);
490 MessageParcel data;
491 data.WriteRemoteObject(callbackStub);
492 data.WriteString("");
493 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data);
494 EXPECT_EQ(ret, DBINDER_SERVICE_ADD_DEATH_ERR);
495 }
496
497 /**
498 * @tc.name: AddDbinderDeathRecipient003
499 * @tc.desc: Verify the AddDbinderDeathRecipient function
500 * @tc.type: FUNC
501 */
502 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient003, TestSize.Level1)
503 {
504 const std::string service = "serviceTest";
505 const std::string device = "deviceTest";
506 binder_uintptr_t object = BINDER_OBJECT;
507 DBinderServiceStub dBinderServiceStub(service, device, object);
508
509 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub(u"testStub");
510 EXPECT_TRUE(callbackStub != nullptr);
511 MessageParcel data;
512 data.WriteRemoteObject(callbackStub);
513 data.WriteString("test");
514 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data);
515 EXPECT_EQ(ret, DBINDER_SERVICE_ADD_DEATH_ERR);
516 }
517
518 /**
519 * @tc.name: AddDbinderDeathRecipient004
520 * @tc.desc: Verify the AddDbinderDeathRecipient function
521 * @tc.type: FUNC
522 */
523 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient004, TestSize.Level1)
524 {
525 const std::string service = "serviceTest";
526 const std::string device = "deviceTest";
527 binder_uintptr_t object = BINDER_OBJECT;
528 DBinderServiceStub dBinderServiceStub(service, device, object);
529
530 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(0);
531 EXPECT_TRUE(callbackProxy != nullptr);
532 MessageParcel data;
533 data.WriteRemoteObject(callbackProxy);
534 data.WriteString("test");
535 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data);
536 EXPECT_EQ(ret, ERR_NONE);
537 }
538
539 /**
540 * @tc.name: AddDbinderDeathRecipient005
541 * @tc.desc: Verify the AddDbinderDeathRecipient function
542 * @tc.type: FUNC
543 */
544 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient005, TestSize.Level1)
545 {
546 const std::string service = "serviceTest";
547 const std::string device = "deviceTest";
548 binder_uintptr_t object = BINDER_OBJECT;
549 DBinderServiceStub dBinderServiceStub(service, device, object);
550
551 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(0);
552 EXPECT_TRUE(callbackProxy != nullptr);
553 MessageParcel data;
554 data.WriteRemoteObject(callbackProxy);
555 data.WriteString("");
556 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data);
557 EXPECT_EQ(ret, DBINDER_SERVICE_ADD_DEATH_ERR);
558 }
559
560 /**
561 * @tc.name: RemoveDbinderDeathRecipient001
562 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
563 * @tc.type: FUNC
564 */
565 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient001, TestSize.Level1)
566 {
567 const std::string service = "serviceTest";
568 const std::string device = "deviceTest";
569 binder_uintptr_t object = BINDER_OBJECT;
570 DBinderServiceStub dBinderServiceStub(service, device, object);
571
572 MessageParcel data;
573 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data);
574 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
575 }
576
577 /**
578 * @tc.name: RemoveDbinderDeathRecipient002
579 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
580 * @tc.type: FUNC
581 */
582 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient002, TestSize.Level1)
583 {
584 const std::string service = "serviceTest";
585 const std::string device = "deviceTest";
586 binder_uintptr_t object = BINDER_OBJECT;
587 DBinderServiceStub dBinderServiceStub(service, device, object);
588
589 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(0);
590 EXPECT_TRUE(callbackProxy != nullptr);
591 MessageParcel data;
592 data.WriteRemoteObject(callbackProxy);
593 data.WriteString("test");
594 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data);
595 EXPECT_EQ(ret, ERR_NONE);
596 }
597
598 /**
599 * @tc.name: RemoveDbinderDeathRecipient003
600 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
601 * @tc.type: FUNC
602 */
603 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient003, TestSize.Level1)
604 {
605 const std::string service = "serviceTest";
606 const std::string device = "deviceTest";
607 binder_uintptr_t object = BINDER_OBJECT;
608 DBinderServiceStub dBinderServiceStub(service, device, object);
609
610 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(0);
611 EXPECT_TRUE(callbackProxy != nullptr);
612 MessageParcel data;
613 data.WriteRemoteObject(callbackProxy);
614 data.WriteString("");
615 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data);
616 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
617 }
618
619 /**
620 * @tc.name: RemoveDbinderDeathRecipient004
621 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
622 * @tc.type: FUNC
623 */
624 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient004, TestSize.Level1)
625 {
626 const std::string service = "serviceTest";
627 const std::string device = "deviceTest";
628 binder_uintptr_t object = BINDER_OBJECT;
629 DBinderServiceStub dBinderServiceStub(service, device, object);
630
631 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub(u"testStub");
632 EXPECT_TRUE(callbackStub != nullptr);
633 MessageParcel data;
634 data.WriteRemoteObject(callbackStub);
635 data.WriteString("test");
636 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data);
637 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
638 }
639
640 /**
641 * @tc.name: RemoveDbinderDeathRecipient005
642 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
643 * @tc.type: FUNC
644 */
645 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient005, TestSize.Level1)
646 {
647 const std::string service = "serviceTest";
648 const std::string device = "deviceTest";
649 binder_uintptr_t object = BINDER_OBJECT;
650 DBinderServiceStub dBinderServiceStub(service, device, object);
651
652 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub(u"testStub");
653 EXPECT_TRUE(callbackStub != nullptr);
654 MessageParcel data;
655 data.WriteRemoteObject(callbackStub);
656 data.WriteString("");
657 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data);
658 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
659 }
660
661 /**
662 * @tc.name: GetAndSaveDBinderData001
663 * @tc.desc: Verify the GetAndSaveDBinderData function
664 * @tc.type: FUNC
665 */
666 HWTEST_F(DBinderServiceStubUnitTest, GetAndSaveDBinderData001, TestSize.Level1)
667 {
668 const std::string service = "serviceTest";
669 const std::string device = "deviceTest";
670 binder_uintptr_t object = BINDER_OBJECT;
671 DBinderServiceStub dBinderServiceStub(service, device, object);
672
673 pid_t pid = -1;
674 uid_t uid = -1;
675
676 int ret = dBinderServiceStub.GetAndSaveDBinderData(pid, uid);
677 EXPECT_EQ(ret, DBINDER_SERVICE_FILL_DATA_ERR);
678 }
679
680 /**
681 * @tc.name: GetAndSaveDBinderData002
682 * @tc.desc: Verify the GetAndSaveDBinderData function
683 * @tc.type: FUNC
684 */
685 HWTEST_F(DBinderServiceStubUnitTest, GetAndSaveDBinderData002, TestSize.Level1)
686 {
687 const std::string service = "serviceTest";
688 const std::string device = "deviceTest";
689 binder_uintptr_t object = BINDER_OBJECT;
690 DBinderServiceStub dBinderServiceStub(service, device, object);
691
692 pid_t pid = 1234;
693 uid_t uid = 1001;
694 int ret = dBinderServiceStub.GetAndSaveDBinderData(pid, uid);
695 EXPECT_EQ(ret, DBINDER_SERVICE_FILL_DATA_ERR);
696 }
697
698 /**
699 * @tc.name: Marshalling001
700 * @tc.desc: Verify the Marshalling function
701 * @tc.type: FUNC
702 */
703 HWTEST_F(DBinderServiceStubUnitTest, Marshalling001, TestSize.Level1)
704 {
705 sptr<IRemoteObject> nullObject = nullptr;
706 const std::string service = "serviceTest";
707 const std::string device = "deviceTest";
708 binder_uintptr_t object = BINDER_OBJECT;
709 DBinderServiceStub dBinderServiceStub(service, device, object);
710
711 Parcel parcel;
712 bool result = dBinderServiceStub.Marshalling(parcel, nullObject);
713 EXPECT_FALSE(result);
714 }
715
716 /**
717 * @tc.name: Marshalling002
718 * @tc.desc: Verify the Marshalling function
719 * @tc.type: FUNC
720 */
721 HWTEST_F(DBinderServiceStubUnitTest, Marshalling002, TestSize.Level1)
722 {
723 const std::string service = "serviceTest";
724 const std::string device = "deviceTest";
725 binder_uintptr_t object = BINDER_OBJECT;
726 DBinderServiceStub dBinderServiceStub(service, device, object);
727
728 const std::string service2 = "serviceTest2";
729 const std::string device2 = "deviceTest2";
730 sptr<IRemoteObject> stubObject = new DBinderServiceStub(service2, device2, object);
731 EXPECT_TRUE(stubObject != nullptr);
732 Parcel parcel;
733 bool result = dBinderServiceStub.Marshalling(parcel, stubObject);
734 EXPECT_TRUE(result);
735 }
736
737 /**
738 * @tc.name: Marshalling003
739 * @tc.desc: Verify the Marshalling function
740 * @tc.type: FUNC
741 */
742 HWTEST_F(DBinderServiceStubUnitTest, Marshalling003, TestSize.Level1)
743 {
744 const std::string service1 = "serviceTest1";
745 const std::string device1 = "deviceTest1";
746 binder_uintptr_t object = BINDER_OBJECT;
747 DBinderServiceStub dBinderServiceStub1(service1, device1, object);
748
749 const std::string service2 = "serviceTest2";
750 const std::string device2 = "deviceTest2";
751 DBinderServiceStub* dBinderServiceStub2 = new DBinderServiceStub(service2, device2, object);
752 dBinderServiceStub2->dbinderData_ = nullptr;
753 sptr<IRemoteObject> stubObject = dBinderServiceStub2;
754
755 Parcel parcel;
756 bool result = dBinderServiceStub1.Marshalling(parcel, stubObject);
757 EXPECT_FALSE(result);
758 }
759
760 /**
761 * @tc.name: SaveDBinderData001
762 * @tc.desc: Verify the SaveDBinderData function
763 * @tc.type: FUNC
764 */
765 HWTEST_F(DBinderServiceStubUnitTest, SaveDBinderData001, TestSize.Level1)
766 {
767 const std::string service1 = "serviceTest1";
768 const std::string device1 = "deviceTest1";
769 binder_uintptr_t object = BINDER_OBJECT;
770 DBinderServiceStub dbinderServiceStub(service1, device1, object);
771 std::string localBusName = "localBusName";
772 int ret = dbinderServiceStub.SaveDBinderData(localBusName);
773 ASSERT_EQ(ret, DBINDER_SERVICE_FILL_DATA_ERR);
774 }
775
776 /**
777 * @tc.name: SaveDBinderData002
778 * @tc.desc: Verify the SaveDBinderData function
779 * @tc.type: FUNC
780 */
781 HWTEST_F(DBinderServiceStubUnitTest, SaveDBinderData002, TestSize.Level1)
782 {
783 const std::string service1 = "serviceTest1";
784 const std::string device1 = "deviceTest1";
785 binder_uintptr_t object = BINDER_OBJECT;
786 DBinderServiceStub dbinderServiceStub(service1, device1, object);
787 std::string localBusName = "localBusName";
788 dbinderServiceStub.dbinderData_ = std::make_unique<uint8_t[]>(sizeof(DBinderNegotiationData));
789 ASSERT_NE(dbinderServiceStub.dbinderData_, nullptr);
790 int ret = dbinderServiceStub.SaveDBinderData(localBusName);
791 ASSERT_EQ(ret, DBINDER_SERVICE_FILL_DATA_ERR);
792 }
793
794 /**
795 * @tc.name: SaveDBinderData003
796 * @tc.desc: Verify the SaveDBinderData function
797 * @tc.type: FUNC
798 */
799 HWTEST_F(DBinderServiceStubUnitTest, SaveDBinderData003, TestSize.Level1)
800 {
801 const std::string service1 = "serviceTest1";
802 const std::string device1 = "deviceTest1";
803 binder_uintptr_t object = BINDER_OBJECT;
804 DBinderServiceStub dbinderServiceStub(service1, device1, object);
805 binder_uintptr_t objectAddress = reinterpret_cast<binder_uintptr_t>(&dbinderServiceStub);
806 std::string localBusName = "localBusName";
807 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
808 std::shared_ptr<SessionInfo> sessionInfo = std::make_shared<SessionInfo>();
809 EXPECT_TRUE(sessionInfo != nullptr);
810 sessionInfo->type = SESSION_TYPE_UNKNOWN;
811 bool isInitialized = dBinderService->AttachSessionObject(sessionInfo, objectAddress);
812 ASSERT_TRUE(isInitialized);
813 dbinderServiceStub.dbinderData_ = nullptr;
814 int ret = dbinderServiceStub.SaveDBinderData(localBusName);
815 ASSERT_EQ(ret, DBINDER_SERVICE_MALLOC_ERR);
816 bool result = dBinderService->DetachSessionObject(objectAddress);
817 ASSERT_TRUE(result);
818 }
819
820 /**
821 * @tc.name: SaveDBinderData004
822 * @tc.desc: Verify the SaveDBinderData function
823 * @tc.type: FUNC
824 */
825 HWTEST_F(DBinderServiceStubUnitTest, SaveDBinderData004, TestSize.Level1)
826 {
827 const std::string service1 = "serviceTest1";
828 const std::string device1 = "deviceTest1";
829 binder_uintptr_t object = BINDER_OBJECT;
830 DBinderServiceStub dbinderServiceStub(service1, device1, object);
831 std::string localBusName = "localBusName";
832 dbinderServiceStub.dbinderData_ = std::make_unique<uint8_t[]>(sizeof(DBinderNegotiationData));
833 ASSERT_NE(dbinderServiceStub.dbinderData_, nullptr);
834 DBinderNegotiationData data;
835 data.stubIndex = 1;
836 data.peerTokenId = 1;
837 data.peerServiceName = "target_name";
838 data.peerDeviceId = "target_device";
839 data.localDeviceId = "local_device";
840 data.localServiceName = "local_name";
841 memcpy_s(dbinderServiceStub.dbinderData_.get(), sizeof(DBinderNegotiationData),
842 &data, sizeof(DBinderNegotiationData));
843 int ret = dbinderServiceStub.SaveDBinderData(localBusName);
844 ASSERT_EQ(ret, DBINDER_SERVICE_FILL_DATA_ERR);
845 }
846
847 /**
848 * @tc.name: SaveDBinderData005
849 * @tc.desc: Verify the SaveDBinderData function
850 * @tc.type: FUNC
851 */
852 HWTEST_F(DBinderServiceStubUnitTest, SaveDBinderData005, TestSize.Level1)
853 {
854 const std::string service1 = "serviceTest1";
855 const std::string device1 = "deviceTest1";
856 binder_uintptr_t object = BINDER_OBJECT;
857 DBinderServiceStub dbinderServiceStub(service1, device1, object);
858 binder_uintptr_t objectAddress = reinterpret_cast<binder_uintptr_t>(&dbinderServiceStub);
859 std::string localBusName = "localBusName";
860 dbinderServiceStub.dbinderData_ = std::make_unique<uint8_t[]>(sizeof(dbinder_negotiation_data));
861 ASSERT_NE(dbinderServiceStub.dbinderData_, nullptr);
862 dbinder_negotiation_data *dbinderData = reinterpret_cast<dbinder_negotiation_data *>(
863 dbinderServiceStub.dbinderData_.get());
864 dbinderData->stub_index = 1;
865 dbinderData->tokenid = 1;
866 auto ret = strcpy_s(dbinderData->target_name, SESSION_NAME_LENGTH, "target_name");
867 ret += strcpy_s(dbinderData->target_device, OHOS::DEVICEID_LENGTH, "target_device");
868 ret += strcpy_s(dbinderData->local_device, OHOS::DEVICEID_LENGTH, "local_device");
869 ret += strcpy_s(dbinderData->local_name, SESSION_NAME_LENGTH, "local_name");
870 ASSERT_EQ(ret, EOK);
871 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
872 std::shared_ptr<SessionInfo> sessionInfo = std::make_shared<SessionInfo>();
873 EXPECT_TRUE(sessionInfo != nullptr);
874 sessionInfo->type = SESSION_TYPE_UNKNOWN;
875 bool isInitialized = dBinderService->AttachSessionObject(sessionInfo, objectAddress);
876 ASSERT_TRUE(isInitialized);
877 ret = dbinderServiceStub.SaveDBinderData(localBusName);
878 ASSERT_EQ(ret, ERR_NONE);
879 bool result = dBinderService->DetachSessionObject(objectAddress);
880 ASSERT_TRUE(result);
881 }