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 }
67
68 /**
69 * @tc.name: GetServiceName001
70 * @tc.desc: Verify the GetServiceName function
71 * @tc.type: FUNC
72 */
73 HWTEST_F(DBinderServiceStubUnitTest, GetServiceName001, TestSize.Level1)
74 {
75 const std::string service = "serviceTest";
76 const std::string device = "deviceTest";
77 binder_uintptr_t object = 11;
78 DBinderServiceStub dBinderServiceStub(service, device, object);
79 std::string ret = dBinderServiceStub.GetServiceName();
80 EXPECT_EQ(ret, "serviceTest");
81 }
82
83 /**
84 * @tc.name: GetDeviceID001
85 * @tc.desc: Verify the GetDeviceID function
86 * @tc.type: FUNC
87 */
88 HWTEST_F(DBinderServiceStubUnitTest, GetDeviceID001, TestSize.Level1)
89 {
90 const std::string service = "serviceTest";
91 const std::string device = "deviceTest";
92 binder_uintptr_t object = 11;
93 DBinderServiceStub dBinderServiceStub(service, device, object);
94 std::string ret = dBinderServiceStub.GetDeviceID();
95 EXPECT_EQ(ret, "deviceTest");
96 }
97
98 /**
99 * @tc.name: GetBinderObject001
100 * @tc.desc: Verify the GetBinderObject function
101 * @tc.type: FUNC
102 */
103 HWTEST_F(DBinderServiceStubUnitTest, GetBinderObject001, TestSize.Level1)
104 {
105 const std::string service = "serviceTest";
106 const std::string device = "deviceTest";
107 binder_uintptr_t object = 11;
108 DBinderServiceStub dBinderServiceStub(service, device, object);
109 binder_uintptr_t ret = dBinderServiceStub.GetBinderObject();
110 EXPECT_EQ(ret, 11);
111 }
112
113 /**
114 * @tc.name: ProcessProto001
115 * @tc.desc: Verify the ProcessProto function
116 * @tc.type: FUNC
117 */
118 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto001, TestSize.Level1)
119 {
120 const std::string service = "serviceTest";
121 const std::string device = "deviceTest";
122 binder_uintptr_t object = 11;
123 DBinderServiceStub dBinderServiceStub(service, device, object);
124 uint32_t code = 11;
125 MessageParcel data;
126 MessageParcel reply;
127 MessageOption option;
128 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
129 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
130 }
131
132 /**
133 * @tc.name: ProcessProto002
134 * @tc.desc: Verify the ProcessProto function
135 * @tc.type: FUNC
136 */
137 HWTEST_F(DBinderServiceStubUnitTest, ProcessProto002, TestSize.Level1)
138 {
139 const std::string service = "serviceTest";
140 const std::string device = "deviceTest";
141 binder_uintptr_t object = 11;
142 DBinderServiceStub dBinderServiceStub(service, device, object);
143 binder_uintptr_t key = reinterpret_cast<binder_uintptr_t>(&dBinderServiceStub);
144 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
145 std::shared_ptr<struct SessionInfo> sessionInfo = std::make_shared<struct SessionInfo>();
146 dBinderService->sessionObject_[key] = sessionInfo;
147 uint32_t code = 11;
148 MessageParcel data;
149 MessageParcel reply;
150 MessageOption option;
151 int32_t ret = dBinderServiceStub.ProcessProto(code, data, reply, option);
152 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
153 }
154
155 /**
156 * @tc.name: OnRemoteRequest001
157 * @tc.desc: Verify the OnRemoteRequest function
158 * @tc.type: FUNC
159 */
160 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest001, TestSize.Level1)
161 {
162 const std::string service = "serviceTest";
163 const std::string device = "deviceTest";
164 binder_uintptr_t object = 11;
165 DBinderServiceStub dBinderServiceStub(service, device, object);
166
167 uint32_t code = GET_PROTO_INFO;
168 MessageParcel data;
169 MessageParcel reply;
170 MessageOption option;
171 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
172 EXPECT_EQ(ret, DBINDER_SERVICE_PROCESS_PROTO_ERR);
173 }
174
175 /**
176 * @tc.name: OnRemoteRequest002
177 * @tc.desc: Verify the OnRemoteRequest function
178 * @tc.type: FUNC
179 */
180 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest002, TestSize.Level1)
181 {
182 const std::string service = "serviceTest";
183 const std::string device = "deviceTest";
184 binder_uintptr_t object = 11;
185 DBinderServiceStub dBinderServiceStub(service, device, object);
186
187 uint32_t code = DBINDER_OBITUARY_TRANSACTION;
188 MessageParcel data;
189 MessageParcel reply;
190 MessageOption option;
191 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
192 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
193 }
194
195 /**
196 * @tc.name: OnRemoteRequest003
197 * @tc.desc: Verify the OnRemoteRequest function
198 * @tc.type: FUNC
199 */
200 HWTEST_F(DBinderServiceStubUnitTest, OnRemoteRequest003, TestSize.Level1)
201 {
202 const std::string service = "serviceTest";
203 const std::string device = "deviceTest";
204 binder_uintptr_t object = 11;
205 DBinderServiceStub dBinderServiceStub(service, device, object);
206
207 uint32_t code = 11;
208 MessageParcel data;
209 MessageParcel reply;
210 MessageOption option;
211 int32_t ret = dBinderServiceStub.OnRemoteRequest(code, data, reply, option);
212 EXPECT_EQ(ret, DBINDER_SERVICE_UNKNOW_TRANS_ERR);
213 }
214
215 /**
216 * @tc.name: ProcessDeathRecipient001
217 * @tc.desc: Verify the ProcessDeathRecipient function
218 * @tc.type: FUNC
219 */
220 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient001, TestSize.Level1)
221 {
222 const std::string service = "serviceTest";
223 const std::string device = "deviceTest";
224 binder_uintptr_t object = 11;
225 DBinderServiceStub dBinderServiceStub(service, device, object);
226
227 MessageParcel data;
228 data.WriteInt32(IRemoteObject::DeathRecipient::ADD_DEATH_RECIPIENT);
229 MessageParcel reply;
230 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data, reply);
231 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
232 }
233
234 /**
235 * @tc.name: ProcessDeathRecipient002
236 * @tc.desc: Verify the ProcessDeathRecipient function
237 * @tc.type: FUNC
238 */
239 HWTEST_F(DBinderServiceStubUnitTest, ProcessDeathRecipient002, TestSize.Level1)
240 {
241 const std::string service = "serviceTest";
242 const std::string device = "deviceTest";
243 binder_uintptr_t object = 11;
244 DBinderServiceStub dBinderServiceStub(service, device, object);
245
246 MessageParcel data;
247 data.WriteInt32(IRemoteObject::DeathRecipient::REMOVE_DEATH_RECIPIENT);
248 MessageParcel reply;
249 int32_t ret = dBinderServiceStub.ProcessDeathRecipient(data, reply);
250 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
251 }
252
253 /**
254 * @tc.name: AddDbinderDeathRecipient001
255 * @tc.desc: Verify the AddDbinderDeathRecipient function
256 * @tc.type: FUNC
257 */
258 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient001, TestSize.Level1)
259 {
260 const std::string service = "serviceTest";
261 const std::string device = "deviceTest";
262 binder_uintptr_t object = 11;
263 DBinderServiceStub dBinderServiceStub(service, device, object);
264 MessageParcel data;
265 MessageParcel reply;
266 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data, reply);
267 EXPECT_EQ(ret, DBINDER_SERVICE_INVALID_DATA_ERR);
268 }
269
270 /**
271 * @tc.name: AddDbinderDeathRecipient002
272 * @tc.desc: Verify the AddDbinderDeathRecipient function
273 * @tc.type: FUNC
274 */
275 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient002, TestSize.Level1)
276 {
277 const std::string service = "serviceTest";
278 const std::string device = "deviceTest";
279 binder_uintptr_t object = 11;
280 DBinderServiceStub dBinderServiceStub(service, device, object);
281
282 sptr<IPCObjectStub> callbackStub = new (std::nothrow) IPCObjectStub();
283 MessageParcel data;
284 data.WriteRemoteObject(callbackStub);
285 data.WriteString("");
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: AddDbinderDeathRecipient003
293 * @tc.desc: Verify the AddDbinderDeathRecipient function
294 * @tc.type: FUNC
295 */
296 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient003, 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();
304 MessageParcel data;
305 data.WriteRemoteObject(callbackStub);
306 data.WriteString("test");
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: AddDbinderDeathRecipient004
314 * @tc.desc: Verify the AddDbinderDeathRecipient function
315 * @tc.type: FUNC
316 */
317 HWTEST_F(DBinderServiceStubUnitTest, AddDbinderDeathRecipient004, 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<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(1);
325 MessageParcel data;
326 data.WriteRemoteObject(callbackProxy);
327 data.WriteString("test");
328 MessageParcel reply;
329 int32_t ret = dBinderServiceStub.AddDbinderDeathRecipient(data, reply);
330 EXPECT_EQ(ret, ERR_NONE);
331 }
332
333 /**
334 * @tc.name: RemoveDbinderDeathRecipient001
335 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
336 * @tc.type: FUNC
337 */
338 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient001, 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 MessageParcel data;
346 MessageParcel reply;
347 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data, reply);
348 EXPECT_EQ(ret, DBINDER_SERVICE_REMOVE_DEATH_ERR);
349 }
350
351 /**
352 * @tc.name: RemoveDbinderDeathRecipient002
353 * @tc.desc: Verify the RemoveDbinderDeathRecipient function
354 * @tc.type: FUNC
355 */
356 HWTEST_F(DBinderServiceStubUnitTest, RemoveDbinderDeathRecipient002, TestSize.Level1)
357 {
358 const std::string service = "serviceTest";
359 const std::string device = "deviceTest";
360 binder_uintptr_t object = 11;
361 DBinderServiceStub dBinderServiceStub(service, device, object);
362
363 sptr<IPCObjectProxy> callbackProxy = new (std::nothrow) IPCObjectProxy(1);
364 MessageParcel data;
365 data.WriteRemoteObject(callbackProxy);
366 data.WriteString("test");
367 MessageParcel reply;
368 int32_t ret = dBinderServiceStub.RemoveDbinderDeathRecipient(data, reply);
369 EXPECT_EQ(ret, ERR_NONE);
370 }
371