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 <iostream>
22
23 #define private public
24 #define protected public
25 #include "dbinder_service.h"
26 #include "dbinder_service_stub.h"
27 #undef protected
28 #undef private
29
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::HiviewDFX;
33
34 typedef unsigned long long binder_uintptr_t;
35 class DBinderServiceStubUnitTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41 };
42
SetUp()43 void DBinderServiceStubUnitTest::SetUp()
44 {}
45
TearDown()46 void DBinderServiceStubUnitTest::TearDown()
47 {}
48
SetUpTestCase()49 void DBinderServiceStubUnitTest::SetUpTestCase()
50 {}
51
TearDownTestCase()52 void DBinderServiceStubUnitTest::TearDownTestCase()
53 {}
54
55 /**
56 * @tc.name: DBinderServiceStub001
57 * @tc.desc: Verify the DBinderServiceStub function
58 * @tc.type: FUNC
59 */
60 HWTEST_F(DBinderServiceStubUnitTest, DBinderServiceStub001, TestSize.Level1)
61 {
62 const std::string service = "serviceTest";
63 const std::string device = "deviceTest";
64 binder_uintptr_t object = 11;
65 DBinderServiceStub dBinderServiceStub(service, device, object);
66 int32_t num = dBinderServiceStub.GetObjectRefCount();
67 EXPECT_NE(num, 0);
68 }
69
70 /**
71 * @tc.name: GetServiceName001
72 * @tc.desc: Verify the GetServiceName function
73 * @tc.type: FUNC
74 */
75 HWTEST_F(DBinderServiceStubUnitTest, GetServiceName001, TestSize.Level1)
76 {
77 const std::string service = "serviceTest";
78 const std::string device = "deviceTest";
79 binder_uintptr_t object = 11;
80 DBinderServiceStub dBinderServiceStub(service, device, object);
81 std::string ret = dBinderServiceStub.GetServiceName();
82 EXPECT_EQ(ret, "serviceTest");
83 }
84
85 /**
86 * @tc.name: GetDeviceID001
87 * @tc.desc: Verify the GetDeviceID function
88 * @tc.type: FUNC
89 */
90 HWTEST_F(DBinderServiceStubUnitTest, GetDeviceID001, TestSize.Level1)
91 {
92 const std::string service = "serviceTest";
93 const std::string device = "deviceTest";
94 binder_uintptr_t object = 11;
95 DBinderServiceStub dBinderServiceStub(service, device, object);
96 std::string ret = dBinderServiceStub.GetDeviceID();
97 EXPECT_EQ(ret, "deviceTest");
98 }
99
100 /**
101 * @tc.name: GetBinderObject001
102 * @tc.desc: Verify the GetBinderObject function
103 * @tc.type: FUNC
104 */
105 HWTEST_F(DBinderServiceStubUnitTest, GetBinderObject001, TestSize.Level1)
106 {
107 const std::string service = "serviceTest";
108 const std::string device = "deviceTest";
109 binder_uintptr_t object = 11;
110 DBinderServiceStub dBinderServiceStub(service, device, object);
111 binder_uintptr_t ret = dBinderServiceStub.GetBinderObject();
112 EXPECT_EQ(ret, 11);
113 }
114
115 /**
116 * @tc.name: ProcessProto001
117 * @tc.desc: Verify the ProcessProto function
118 * @tc.type: FUNC
119 */
120 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto001, TestSize.Level1)
121 {
122 const std::string service = "serviceTest";
123 const std::string device = "deviceTest";
124 binder_uintptr_t object = 11;
125 DBinderServiceStub dBinderServiceStub(service, device, object);
126 uint32_t code = 11;
127 MessageParcel data;
128 MessageParcel reply;
129 MessageOption option;
130 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
131 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
132 }
133
134 /**
135 * @tc.name: ProcessProto002
136 * @tc.desc: Verify the ProcessProto function
137 * @tc.type: FUNC
138 */
139 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto002, TestSize.Level1)
140 {
141 const std::string service = "serviceTest";
142 const std::string device = "deviceTest";
143 binder_uintptr_t object = 11;
144 DBinderServiceStub dBinderServiceStub(service, device, object);
145 binder_uintptr_t key = reinterpret_cast<binder_uintptr_t>(&dBinderServiceStub);
146 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
147 std::shared_ptr<struct SessionInfo> sessionInfo = std::make_shared<struct SessionInfo>();
148 dBinderService->sessionObject_[key] = sessionInfo;
149 uint32_t code = 11;
150 MessageParcel data;
151 MessageParcel reply;
152 MessageOption option;
153 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
154 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
155 }
156
157 /**
158 * @tc.name: ProcessProto003
159 * @tc.desc: Verify the ProcessProto function
160 * @tc.type: FUNC
161 */
162 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto003, TestSize.Level1)
163 {
164 const std::string service = "serviceTest";
165 const std::string device = "deviceTest";
166 binder_uintptr_t object = 11;
167 DBinderServiceStub dBinderServiceStub(service, device, object);
168 uint32_t code = 11;
169 MessageParcel data;
170 MessageParcel reply;
171 MessageOption option;
172 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
173 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
174 }
175
176 /**
177 * @tc.name: OnRemoteRequest001
178 * @tc.desc: Verify the OnRemoteRequest function
179 * @tc.type: FUNC
180 */
181 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest001, TestSize.Level1)
182 {
183 const std::string service = "serviceTest";
184 const std::string device = "deviceTest";
185 binder_uintptr_t object = 11;
186 DBinderServiceStub dBinderServiceStub(service, device, object);
187
188 uint32_t code = GET_PROTO_INFO;
189 MessageParcel data;
190 MessageParcel reply;
191 MessageOption option;
192 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
193 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
194 }
195
196 /**
197 * @tc.name: OnRemoteRequest002
198 * @tc.desc: Verify the OnRemoteRequest function
199 * @tc.type: FUNC
200 */
201 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest002, TestSize.Level1)
202 {
203 const std::string service = "serviceTest";
204 const std::string device = "deviceTest";
205 binder_uintptr_t object = 11;
206 DBinderServiceStub dBinderServiceStub(service, device, object);
207
208 uint32_t code = DBINDER_OBITUARY_TRANSACTION;
209 MessageParcel data;
210 MessageParcel reply;
211 MessageOption option;
212 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
213 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
214 }
215
216 /**
217 * @tc.name: OnRemoteRequest003
218 * @tc.desc: Verify the OnRemoteRequest function
219 * @tc.type: FUNC
220 */
221 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest003, TestSize.Level1)
222 {
223 const std::string service = "serviceTest";
224 const std::string device = "deviceTest";
225 binder_uintptr_t object = 11;
226 DBinderServiceStub dBinderServiceStub(service, device, object);
227
228 uint32_t code = 11;
229 MessageParcel data;
230 MessageParcel reply;
231 MessageOption option;
232 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
233 EXPECT_EQ(ret, DBINDER_SERVICE_UNKNOW_TRANS_ERR);
234 }
235
236 /**
237 * @tc.name: ProcessDeathRecipient001
238 * @tc.desc: Verify the ProcessDeathRecipient function
239 * @tc.type: FUNC
240 */
241 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient001, TestSize.Level1)
242 {
243 const std::string service = "serviceTest";
244 const std::string device = "deviceTest";
245 binder_uintptr_t object = 11;
246 DBinderServiceStub dBinderServiceStub(service, device, object);
247
248 MessageParcel data;
249 data.WriteInt32(IRemoteObject::DeathRecipient::ADD_DEATH_RECIPIENT);
250 MessageParcel reply;
251 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data, reply);
252 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
253 }
254
255 /**
256 * @tc.name: ProcessDeathRecipient002
257 * @tc.desc: Verify the ProcessDeathRecipient function
258 * @tc.type: FUNC
259 */
260 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient002, TestSize.Level1)
261 {
262 const std::string service = "serviceTest";
263 const std::string device = "deviceTest";
264 binder_uintptr_t object = 11;
265 DBinderServiceStub dBinderServiceStub(service, device, object);
266
267 MessageParcel data;
268 data.WriteInt32(IRemoteObject::DeathRecipient::REMOVE_DEATH_RECIPIENT);
269 MessageParcel reply;
270 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data, reply);
271 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
272 }
273
274 /**
275 * @tc.name: AddDbinderDeathRecipient001
276 * @tc.desc: Verify the AddDbinderDeathRecipient function
277 * @tc.type: FUNC
278 */
279 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient001, TestSize.Level1)
280 {
281 const std::string service = "serviceTest";
282 const std::string device = "deviceTest";
283 binder_uintptr_t object = 11;
284 DBinderServiceStub dBinderServiceStub(service, device, object);
285 MessageParcel data;
286 MessageParcel reply;
287 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data, reply);
288 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
289 }
290
291 /**
292 * @tc.name: AddDbinderDeathRecipient002
293 * @tc.desc: Verify the AddDbinderDeathRecipient function
294 * @tc.type: FUNC
295 */
296 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient002, TestSize.Level1)
297 {
298 const std::string service = "serviceTest";
299 const std::string device = "deviceTest";
300 binder_uintptr_t object = 11;
301 DBinderServiceStub dBinderServiceStub(service, device, object);
302
303 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub(u"testStub");
304 MessageParcel data;
305 data.WriteRemoteObject(callbackStub);
306 data.WriteString("");
307 MessageParcel reply;
308 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data, reply);
309 EXPECT_EQ(ret, DBINDER_SERVICE_ADD_DEATH_ERR);
310 }
311
312 /**
313 * @tc.name: AddDbinderDeathRecipient003
314 * @tc.desc: Verify the AddDbinderDeathRecipient function
315 * @tc.type: FUNC
316 */
317 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient003, TestSize.Level1)
318 {
319 const std::string service = "serviceTest";
320 const std::string device = "deviceTest";
321 binder_uintptr_t object = 11;
322 DBinderServiceStub dBinderServiceStub(service, device, object);
323
324 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub(u"testStub");
325 MessageParcel data;
326 data.WriteRemoteObject(callbackStub);
327 data.WriteString("test");
328 MessageParcel reply;
329 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data, reply);
330 EXPECT_EQ(ret, DBINDER_SERVICE_ADD_DEATH_ERR);
331 }
332
333 /**
334 * @tc.name: AddDbinderDeathRecipient004
335 * @tc.desc: Verify the AddDbinderDeathRecipient function
336 * @tc.type: FUNC
337 */
338 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient004, TestSize.Level1)
339 {
340 const std::string service = "serviceTest";
341 const std::string device = "deviceTest";
342 binder_uintptr_t object = 11;
343 DBinderServiceStub dBinderServiceStub(service, device, object);
344
345 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(1);
346 MessageParcel data;
347 data.WriteRemoteObject(callbackProxy);
348 data.WriteString("test");
349 MessageParcel reply;
350 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data, reply);
351 EXPECT_EQ(ret, ERR_NONE);
352 }
353
354 /**
355 * @tc.name: RemoveDbinderDeathRecipient001
356 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
357 * @tc.type: FUNC
358 */
359 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient001, TestSize.Level1)
360 {
361 const std::string service = "serviceTest";
362 const std::string device = "deviceTest";
363 binder_uintptr_t object = 11;
364 DBinderServiceStub dBinderServiceStub(service, device, object);
365
366 MessageParcel data;
367 MessageParcel reply;
368 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data, reply);
369 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
370 }
371
372 /**
373 * @tc.name: RemoveDbinderDeathRecipient002
374 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
375 * @tc.type: FUNC
376 */
377 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient002, TestSize.Level1)
378 {
379 const std::string service = "serviceTest";
380 const std::string device = "deviceTest";
381 binder_uintptr_t object = 11;
382 DBinderServiceStub dBinderServiceStub(service, device, object);
383
384 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(1);
385 MessageParcel data;
386 data.WriteRemoteObject(callbackProxy);
387 data.WriteString("test");
388 MessageParcel reply;
389 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data, reply);
390 EXPECT_EQ(ret, ERR_NONE);
391 }
392