1 /*
2 * Copyright (C) 2021 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 "dbinder_test_service_skeleton.h"
17
18 #include <cinttypes>
19
20 #include "dbinder_log.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "ipc_object_proxy.h"
24 #include "iremote_proxy.h"
25 #include "iservice_registry.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 using namespace OHOS::HiviewDFX;
31
32 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_TEST, "DBinderTestServiceProxy" };
33
34 // set wait time for raw data
35 static constexpr int RAW_DATA_TIMEOUT = 300;
36
DBinderTestServiceProxy(const sptr<IRemoteObject> & impl)37 DBinderTestServiceProxy::DBinderTestServiceProxy(const sptr<IRemoteObject> &impl)
38 : IRemoteProxy<IDBinderTestService>(impl)
39 {}
40
ReverseInt(int data,int & rep)41 int DBinderTestServiceProxy::ReverseInt(int data, int &rep)
42 {
43 DBINDER_LOGE(LOG_LABEL, "data = %{public}d", data);
44 int error;
45 MessageOption option;
46 MessageParcel dataParcel, replyParcel;
47 if (!dataParcel.WriteInt32(data)) {
48 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
49 return ERR_INVALID_STATE;
50 }
51 error = Remote()->SendRequest(REVERSEINT, dataParcel, replyParcel, option);
52
53 rep = replyParcel.ReadInt32();
54 DBINDER_LOGE(LOG_LABEL, "rep = %{public}d, error = %{public}d", rep, error);
55 return error;
56 }
57
GetChildId(uint64_t & rep)58 int DBinderTestServiceProxy::GetChildId(uint64_t &rep)
59 {
60 int error;
61 MessageOption option;
62 MessageParcel dataParcel, replyParcel;
63 error = Remote()->SendRequest(TRANS_TRACE_ID, dataParcel, replyParcel, option);
64
65 rep = replyParcel.ReadUint64();
66 DBINDER_LOGE(LOG_LABEL, "rep = %{public}" PRIu64 ", error = %{public}d", rep, error);
67 return error;
68 }
69
TransProxyObject(int data,sptr<IRemoteObject> & transObject,int operation,int & rep,int & withdrawRes)70 int DBinderTestServiceProxy::TransProxyObject(int data, sptr<IRemoteObject> &transObject, int operation, int &rep,
71 int &withdrawRes)
72 {
73 int error;
74 MessageOption option;
75 MessageParcel dataParcel, replyParcel;
76 if (!dataParcel.WriteInt32(data) || !dataParcel.WriteInt32(operation) ||
77 !dataParcel.WriteRemoteObject(transObject)) {
78 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
79 return ERR_INVALID_STATE;
80 }
81 error = Remote()->SendRequest(TRANS_PROXY_OBJECT, dataParcel, replyParcel, option);
82
83 rep = replyParcel.ReadInt32();
84 withdrawRes = replyParcel.ReadInt32();
85 return error;
86 }
87
TransProxyObjectRefCount(sptr<IRemoteObject> & transObject,int operation)88 int DBinderTestServiceProxy::TransProxyObjectRefCount(sptr<IRemoteObject> &transObject, int operation)
89 {
90 MessageOption option;
91 MessageParcel data, reply;
92 if (!data.WriteInt32(operation) || !data.WriteRemoteObject(transObject)) {
93 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
94 return ERR_INVALID_STATE;
95 }
96 int error = Remote()->SendRequest(TRANS_PROXY_OBJECT_REFCOUNT, data, reply, option);
97 if (error != ERR_NONE) {
98 DBINDER_LOGE(LOG_LABEL, "fail to send proxy object");
99 return ERR_INVALID_STATE;
100 }
101 return error;
102 }
103
TransProxyObjectAgain(int data,sptr<IRemoteObject> & transObject,int operation,int & rep,int & withdrawRes)104 int DBinderTestServiceProxy::TransProxyObjectAgain(int data, sptr<IRemoteObject> &transObject, int operation, int &rep,
105 int &withdrawRes)
106 {
107 int error;
108 MessageOption option;
109 MessageParcel dataParcel, replyParcel;
110 if (!dataParcel.WriteInt32(data) || !dataParcel.WriteInt32(operation) ||
111 !dataParcel.WriteRemoteObject(transObject)) {
112 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
113 return ERR_INVALID_STATE;
114 }
115 error = Remote()->SendRequest(TRANS_OBJECT_OVER_DEVICE_OVER_PROCESS, dataParcel, replyParcel, option);
116
117 rep = replyParcel.ReadInt32();
118 withdrawRes = replyParcel.ReadInt32();
119 return error;
120 }
121
TransStubObject(int data,sptr<IRemoteObject> & transObject,int & rep,int & stubRep)122 int DBinderTestServiceProxy::TransStubObject(int data, sptr<IRemoteObject> &transObject, int &rep, int &stubRep)
123 {
124 int error;
125 MessageOption option;
126 MessageParcel dataParcel, replyParcel;
127 if (!dataParcel.WriteInt32(data) || !dataParcel.WriteRemoteObject(transObject)) {
128 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
129 return ERR_INVALID_STATE;
130 }
131
132 error = Remote()->SendRequest(TRANS_STUB_OBJECT, dataParcel, replyParcel, option);
133 if (error != ERR_NONE) {
134 DBINDER_LOGE(LOG_LABEL, "fail to send stub object");
135 return ERR_INVALID_STATE;
136 }
137
138 rep = replyParcel.ReadInt32();
139
140 sptr<IRemoteObject> proxy = replyParcel.ReadRemoteObject();
141 if (proxy == nullptr) {
142 DBINDER_LOGE(LOG_LABEL, "fail to get remote stub object");
143 return ERR_INVALID_STATE;
144 }
145
146 MessageParcel dataStubParcel, replyStubParcel;
147 if (!dataStubParcel.WriteInt32(rep)) {
148 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
149 return ERR_INVALID_STATE;
150 }
151
152 error = proxy->SendRequest(REVERSEINT, dataStubParcel, replyStubParcel, option);
153 if (error != ERR_NONE) {
154 DBINDER_LOGE(LOG_LABEL, "fail to send data info");
155 return ERR_INVALID_STATE;
156 }
157
158 stubRep = replyStubParcel.ReadInt32();
159 return error;
160 }
161
TransStubObjectRefCount(sptr<IRemoteObject> & transObject,int operation)162 int DBinderTestServiceProxy::TransStubObjectRefCount(sptr<IRemoteObject> &transObject, int operation)
163 {
164 MessageOption option;
165 MessageParcel data, reply;
166 if (!data.WriteInt32(operation) || !data.WriteRemoteObject(transObject)) {
167 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
168 return ERR_INVALID_STATE;
169 }
170 int error = Remote()->SendRequest(TRANS_STUB_OBJECT_REFCOUNT, data, reply, option);
171 if (error != ERR_NONE) {
172 DBINDER_LOGE(LOG_LABEL, "fail to send stub object");
173 return ERR_INVALID_STATE;
174 }
175 return error;
176 }
177
GetRemoteObject(int type)178 sptr<IRemoteObject> DBinderTestServiceProxy::GetRemoteObject(int type)
179 {
180 MessageOption option;
181 MessageParcel dataParcel, replyParcel;
182 if (!dataParcel.WriteInt32(type)) {
183 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
184 return nullptr;
185 }
186
187 int error = Remote()->SendRequest(GET_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
188 if (error != ERR_NONE) {
189 DBINDER_LOGE(LOG_LABEL, "fail to send data info");
190 return nullptr;
191 }
192
193 sptr<IRemoteObject> proxy = replyParcel.ReadRemoteObject();
194 if (proxy == nullptr) {
195 DBINDER_LOGE(LOG_LABEL, "fail to get remote stub object");
196 return nullptr;
197 }
198 return proxy;
199 }
200
GetRemoteDecTimes()201 int DBinderTestServiceProxy::GetRemoteDecTimes()
202 {
203 MessageOption option;
204 MessageParcel dataParcel, replyParcel;
205
206 int error = Remote()->SendRequest(GET_REMOTE_DES_TIMES, dataParcel, replyParcel, option);
207 if (error != ERR_NONE) {
208 DBINDER_LOGE(LOG_LABEL, "fail to send data info");
209 return 0;
210 }
211
212 return replyParcel.ReadInt32();
213 }
214
ClearRemoteDecTimes()215 void DBinderTestServiceProxy::ClearRemoteDecTimes()
216 {
217 MessageOption option;
218 MessageParcel dataParcel, replyParcel;
219
220 int error = Remote()->SendRequest(CLEAR_REMOTE_DES_TIMES, dataParcel, replyParcel, option);
221 if (error != ERR_NONE) {
222 DBINDER_LOGE(LOG_LABEL, "fail to send data info");
223 }
224 }
225
TransOversizedPkt(const std::string & dataStr,std::string & repStr)226 int DBinderTestServiceProxy::TransOversizedPkt(const std::string &dataStr, std::string &repStr)
227 {
228 int error;
229 MessageOption option;
230 MessageParcel dataParcel, replyParcel;
231 if (!dataParcel.WriteString(dataStr)) {
232 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
233 return ERR_INVALID_STATE;
234 }
235 error = Remote()->SendRequest(TRANS_OVERSIZED_PKT, dataParcel, replyParcel, option);
236
237 repStr = replyParcel.ReadString();
238 return error;
239 }
240
ProxyTransRawData(int length)241 int DBinderTestServiceProxy::ProxyTransRawData(int length)
242 {
243 MessageParcel dataParcel, replyParcel;
244
245 MessageOption option;
246 option.SetWaitTime(RAW_DATA_TIMEOUT);
247 int waitTime = option.GetWaitTime();
248 DBINDER_LOGE(LOG_LABEL, "data length = %{public}d, wait time = %{public}d", length, waitTime);
249
250 if (length <= 1) {
251 DBINDER_LOGE(LOG_LABEL, "length should > 1, length is %{public}d", length);
252 return ERR_INVALID_STATE;
253 }
254 unsigned char *buffer = new (std::nothrow) unsigned char[length];
255 if (buffer == nullptr) {
256 DBINDER_LOGE(LOG_LABEL, "new buffer failed of length = %{public}d", length);
257 return ERR_INVALID_STATE;
258 }
259 buffer[0] = 'a';
260 buffer[length - 1] = 'z';
261 if (!dataParcel.WriteInt32(length) || !dataParcel.WriteRawData(buffer, length) || !dataParcel.WriteInt32(length)) {
262 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
263 delete[] buffer;
264 return ERR_INVALID_STATE;
265 }
266 delete[] buffer;
267 int ret = Remote()->SendRequest(TRANS_RAW_DATA, dataParcel, replyParcel, option);
268 if (ret != ERR_NONE) {
269 DBINDER_LOGE(LOG_LABEL, "fail to send request, ret = %{public}d", ret);
270 return ret;
271 }
272 if (length != replyParcel.ReadInt32()) {
273 DBINDER_LOGE(LOG_LABEL, "reply wrong length");
274 ret += ERR_TRANSACTION_FAILED;
275 }
276 return ret;
277 }
278
StubTransRawData(int length)279 int DBinderTestServiceProxy::StubTransRawData(int length)
280 {
281 MessageParcel dataParcel, replyParcel;
282
283 MessageOption option;
284 option.SetWaitTime(RAW_DATA_TIMEOUT);
285 int waitTime = option.GetWaitTime();
286 DBINDER_LOGE(LOG_LABEL, "data length = %{public}d, wait time = %{public}d", length, waitTime);
287
288 if (length <= 1) {
289 DBINDER_LOGE(LOG_LABEL, "length should > 1, length is %{public}d", length);
290 return ERR_INVALID_STATE;
291 }
292
293 if (!dataParcel.WriteInt32(length)) {
294 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
295 return ERR_INVALID_STATE;
296 }
297
298 int ret = Remote()->SendRequest(RECEIVE_RAW_DATA, dataParcel, replyParcel, option);
299 if (ret != ERR_NONE) {
300 DBINDER_LOGE(LOG_LABEL, "fail to send request, ret = %{public}d", ret);
301 return ret;
302 }
303
304 if (replyParcel.ReadInt32() != length) {
305 DBINDER_LOGE(LOG_LABEL, "reply false data");
306 return ERR_INVALID_DATA;
307 }
308
309 const char *buffer = nullptr;
310 if ((buffer = reinterpret_cast<const char *>(replyParcel.ReadRawData(length))) == nullptr) {
311 DBINDER_LOGE(LOG_LABEL, "fail to read raw data, length = %{public}d", length);
312 return ERR_INVALID_DATA;
313 }
314 if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
315 DBINDER_LOGE(LOG_LABEL, "received raw data is wrong, length = %{public}d", length);
316 return ERR_INVALID_DATA;
317 }
318
319 if (replyParcel.ReadInt32() != length) {
320 DBINDER_LOGE(LOG_LABEL, "fail to read length after raw data, length = %{public}d", length);
321 return ERR_INVALID_DATA;
322 }
323
324 return ERR_NONE;
325 }
326
FlushAsyncCommands(int count,int length)327 int DBinderTestServiceProxy::FlushAsyncCommands(int count, int length)
328 {
329 int ret;
330 MessageOption option = { MessageOption::TF_ASYNC };
331 MessageParcel dataParcel, replyParcel;
332 std::string dataStr(length, 'a');
333 if (!dataParcel.WriteString(dataStr)) {
334 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
335 return ERR_INVALID_STATE;
336 }
337 for (int i = 0; i < count; i++) {
338 ret = Remote()->SendRequest(TRANS_OVERSIZED_PKT, dataParcel, replyParcel, option);
339 if (ret != ERR_NONE) {
340 DBINDER_LOGE(LOG_LABEL, "fail to send request when count = %{public}d ret = %{public}d", i, ret);
341 return ret;
342 }
343 }
344 ret = IPCSkeleton::FlushCommands(this->AsObject());
345 return ret;
346 }
347
ReverseIntNullReply(int data,int & rep)348 int DBinderTestServiceProxy::ReverseIntNullReply(int data, int &rep)
349 {
350 int error;
351 MessageOption option;
352 MessageParcel dataParcel, replyParcel;
353 if (!dataParcel.WriteInt32(data)) {
354 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
355 return ERR_INVALID_STATE;
356 }
357 error = Remote()->SendRequest(REVERSEINT, dataParcel, replyParcel, option);
358
359 rep = replyParcel.ReadInt32();
360 return error;
361 }
362
ReverseIntVoidData(int data,int & rep)363 int DBinderTestServiceProxy::ReverseIntVoidData(int data, int &rep)
364 {
365 int error;
366 MessageOption option;
367 MessageParcel dataParcel, replyParcel;
368 // do not write data to parcel;
369 error = Remote()->SendRequest(REVERSEINT, dataParcel, replyParcel, option);
370
371 rep = replyParcel.ReadInt32();
372 return error;
373 }
374
ReverseIntDelay(int data,int & rep)375 int DBinderTestServiceProxy::ReverseIntDelay(int data, int &rep)
376 {
377 int error;
378 MessageOption option;
379 MessageParcel dataParcel, replyParcel;
380 if (!dataParcel.WriteInt32(data)) {
381 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
382 return ERR_INVALID_STATE;
383 }
384 error = Remote()->SendRequest(REVERSEINTDELAY, dataParcel, replyParcel, option);
385
386 rep = replyParcel.ReadInt32();
387 return error;
388 }
389
Delay(int data,int & rep)390 int DBinderTestServiceProxy::Delay(int data, int &rep)
391 {
392 int error;
393 MessageOption option;
394 MessageParcel dataParcel, replyParcel;
395 if (!dataParcel.WriteInt32(data)) {
396 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
397 return ERR_INVALID_STATE;
398 }
399 error = Remote()->SendRequest(ONLY_DELAY, dataParcel, replyParcel, option);
400
401 rep = replyParcel.ReadInt32();
402 return error;
403 }
404
ReverseIntDelayAsync(int data,int & rep)405 int DBinderTestServiceProxy::ReverseIntDelayAsync(int data, int &rep)
406 {
407 int error;
408 MessageOption option;
409 MessageParcel dataParcel, replyParcel;
410 if (!dataParcel.WriteInt32(data) ||
411 // 2:for data update check, only in test case
412 !replyParcel.WriteInt32(data * 2)) {
413 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
414 return ERR_INVALID_STATE;
415 }
416 error = Remote()->SendRequest(REVERSEINTDELAY, dataParcel, replyParcel, option);
417
418 rep = replyParcel.ReadInt32();
419 return error;
420 }
421
PingService(std::u16string & serviceName)422 int DBinderTestServiceProxy::PingService(std::u16string &serviceName)
423 {
424 int error;
425 MessageOption option;
426 MessageParcel dataParcel, replyParcel;
427 DBINDER_LOGE(LOG_LABEL, "TestServiceProxy:PingService");
428 if (!dataParcel.WriteString16(serviceName.data())) {
429 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
430 return ERR_INVALID_STATE;
431 }
432 error = Remote()->SendRequest(REVERSEINT, dataParcel, replyParcel, option);
433 replyParcel.ReadInt32();
434 return error;
435 }
436
437 pid_t DBinderTestServiceStub::g_lastCallingPid = 0;
438 pid_t DBinderTestServiceStub::g_lastCallinguid = 0;
439
GetLastCallingPid()440 pid_t DBinderTestServiceStub::GetLastCallingPid()
441 {
442 return g_lastCallingPid;
443 }
444
GetLastCallingUid()445 uid_t DBinderTestServiceStub::GetLastCallingUid()
446 {
447 return g_lastCallinguid;
448 }
449
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)450 int DBinderTestServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
451 MessageOption &option)
452 {
453 DBINDER_LOGE(LOG_LABEL, "TestServiceStub::OnReceived, cmd = %{public}d", code);
454 g_lastCallingPid = IPCSkeleton::GetCallingPid();
455 g_lastCallinguid = IPCSkeleton::GetCallingUid();
456 switch (code) {
457 case REVERSEINT: {
458 return OnReverseInt(data, reply);
459 }
460 case REVERSEINTDELAY: {
461 return OnReverseIntDelay(data, reply);
462 }
463 case PING_SERVICE: {
464 return OnPingService(data, reply);
465 }
466 case ONLY_DELAY: {
467 return OnDelay(data, reply);
468 }
469 case TRANS_PROXY_OBJECT:
470 case TRANS_RPC_OBJECT_TO_LOCAL: {
471 DBINDER_LOGE(LOG_LABEL, "TestServiceStub::TRANS_RPC_OBJECT_TO_LOCAL?, cmd = %{public}d", code);
472 return OnReceivedObject(data, reply);
473 }
474 case TRANS_OBJECT_OVER_DEVICE_OVER_PROCESS: {
475 return OnReceivedObjectTransAgain(data, reply);
476 }
477 case TRANS_STUB_OBJECT: {
478 return OnReceivedStubObject(data, reply);
479 }
480 case TRANS_STUB_OBJECT_REFCOUNT: {
481 return OnReceivedStubObjectRefCount(data, reply);
482 }
483 case TRANS_PROXY_OBJECT_REFCOUNT: {
484 return OnReceivedProxyObjectRefCount(data, reply);
485 }
486 case TRANS_OVERSIZED_PKT: {
487 return OnReceivedOversizedPkt(data, reply);
488 }
489 case TRANS_RAW_DATA: {
490 return OnReceivedRawData(data, reply);
491 }
492 case RECEIVE_RAW_DATA: {
493 return OnSentRawData(data, reply);
494 }
495 case TRANS_TRACE_ID: {
496 return OnGetChildId(data, reply);
497 }
498 case GET_REMOTE_STUB_OBJECT: {
499 return OnReceivedGetStubObject(data, reply);
500 }
501 case GET_REMOTE_DES_TIMES: {
502 return OnReceivedGetDecTimes(data, reply);
503 }
504 case CLEAR_REMOTE_DES_TIMES: {
505 return OnReceivedClearDecTimes(data, reply);
506 }
507 default: {
508 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
509 }
510 }
511 }
512
ReverseIntDelayAsync(int data,int & rep)513 int DBinderTestServiceStub::ReverseIntDelayAsync(int data, int &rep)
514 {
515 (void)data;
516 HiLog::Error(LOG_LABEL, "%{public}s: not valid operate", __func__);
517 return 0;
518 }
519
OnReverseInt(MessageParcel & data,MessageParcel & reply)520 int DBinderTestServiceStub::OnReverseInt(MessageParcel &data, MessageParcel &reply)
521 {
522 int result;
523 int32_t reqData = data.ReadInt32();
524 int ret = ReverseInt(reqData, result);
525 DBINDER_LOGI(LOG_LABEL, "ReverseInt result = %{public}d", result);
526 if (!reply.WriteInt32(result)) {
527 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
528 ret = ERR_INVALID_STATE;
529 }
530
531 return ret;
532 }
533
OnGetChildId(MessageParcel & data,MessageParcel & reply)534 int DBinderTestServiceStub::OnGetChildId(MessageParcel &data, MessageParcel &reply)
535 {
536 uint64_t reqData = HiTraceChain::GetId().GetChainId();
537 if (!reply.WriteUint64(reqData)) {
538 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
539 return ERR_INVALID_STATE;
540 }
541
542 DBINDER_LOGE(LOG_LABEL,
543 "before reset uid = %{public}d, callerId = %{public}s, localId = %{public}s, islocal = %{public}d",
544 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingDeviceID().c_str(),
545 IPCSkeleton::GetLocalDeviceID().c_str(), IPCSkeleton::IsLocalCalling());
546 std::string token = IPCSkeleton::ResetCallingIdentity();
547
548 DBINDER_LOGE(LOG_LABEL,
549 "before set uid = %{public}d, callerId = %{public}s, localId = %{public}s, islocal = %{public}d",
550 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingDeviceID().c_str(),
551 IPCSkeleton::GetLocalDeviceID().c_str(), IPCSkeleton::IsLocalCalling());
552 if (!IPCSkeleton::SetCallingIdentity(token)) {
553 DBINDER_LOGE(LOG_LABEL, "Set Calling Identity fail");
554 }
555
556 DBINDER_LOGE(LOG_LABEL,
557 "after set uid = %{public}d, callerId = %{public}s, localId = %{public}s, islocal = %{public}d",
558 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingDeviceID().c_str(),
559 IPCSkeleton::GetLocalDeviceID().c_str(), IPCSkeleton::IsLocalCalling());
560 return ERR_NONE;
561 }
562
OnReverseIntDelay(MessageParcel & data,MessageParcel & reply)563 int DBinderTestServiceStub::OnReverseIntDelay(MessageParcel &data, MessageParcel &reply)
564 {
565 int result;
566 int32_t reqData = data.ReadInt32();
567 int ret = ReverseIntDelay(reqData, result);
568 if (!reply.WriteInt32(result)) {
569 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
570 ret = ERR_INVALID_STATE;
571 }
572
573 return ret;
574 }
575
OnPingService(MessageParcel & data,MessageParcel & reply)576 int DBinderTestServiceStub::OnPingService(MessageParcel &data, MessageParcel &reply)
577 {
578 std::u16string serviceName = data.ReadString16();
579 int ret = PingService(serviceName);
580 DBINDER_LOGI(LOG_LABEL, "%s:PingService: ret=%d", __func__, ret);
581 if (!reply.WriteInt32(ret)) {
582 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
583 ret = ERR_INVALID_STATE;
584 }
585
586 return ret;
587 }
588
OnDelay(MessageParcel & data,MessageParcel & reply)589 int DBinderTestServiceStub::OnDelay(MessageParcel &data, MessageParcel &reply)
590 {
591 int result;
592 int32_t reqData = data.ReadInt32();
593 int ret = Delay(reqData, result);
594 if (!reply.WriteInt32(result)) {
595 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
596 ret = ERR_INVALID_STATE;
597 }
598
599 return ret;
600 }
601
OnReceivedObject(MessageParcel & data,MessageParcel & reply)602 int DBinderTestServiceStub::OnReceivedObject(MessageParcel &data, MessageParcel &reply)
603 {
604 int32_t reqData = data.ReadInt32();
605 int32_t operation = data.ReadInt32();
606 sptr<IRemoteObject> proxy = data.ReadRemoteObject();
607 if (proxy == nullptr) {
608 DBINDER_LOGE(LOG_LABEL, "null proxy");
609 return ERR_INVALID_STATE;
610 }
611
612 // use the received proxy to communicate
613 MessageOption option;
614 MessageParcel dataParcel, replyParcel;
615 if (!dataParcel.WriteInt32(reqData)) {
616 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
617 return ERR_INVALID_STATE;
618 }
619 DBINDER_LOGI(LOG_LABEL, "%{public}s:TRANSOBJECT: reqData=%{public}d", __func__, reqData);
620 int ret = proxy->SendRequest(REVERSEINT, dataParcel, replyParcel, option);
621 int reqResult = replyParcel.ReadInt32();
622 DBINDER_LOGI(LOG_LABEL, "%{public}s:TRANSOBJECT: result=%{public}d", __func__, reqResult);
623
624 if (!reply.WriteInt32(reqResult)) {
625 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
626 return ERR_INVALID_STATE;
627 }
628
629 if (operation == SAVE) {
630 recvProxy_ = proxy;
631 }
632
633 // received proxy is different from that of last time
634 if ((operation == WITHDRAW) && (recvProxy_ != proxy)) {
635 if (!reply.WriteInt32(1)) {
636 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
637 ret = ERR_INVALID_STATE;
638 }
639 return ret;
640 }
641
642 if (!reply.WriteInt32(0)) {
643 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
644 return ERR_INVALID_STATE;
645 }
646 return ret;
647 }
648
OnReceivedProxyObjectRefCount(MessageParcel & data,MessageParcel & reply)649 int DBinderTestServiceStub::OnReceivedProxyObjectRefCount(MessageParcel &data, MessageParcel &reply)
650 {
651 int32_t operation = data.ReadInt32();
652 sptr<IRemoteObject> proxy = data.ReadRemoteObject();
653 if (proxy == nullptr) {
654 DBINDER_LOGE(LOG_LABEL, "fail to get proxy");
655 return ERR_INVALID_STATE;
656 }
657 if (operation == IDBinderTestService::SAVE) {
658 recvProxy_ = proxy;
659 } else if (operation == IDBinderTestService::WITHDRAW) {
660 recvProxy_ = nullptr;
661 }
662 return ERR_NONE;
663 }
664
OnReceivedObjectTransAgain(MessageParcel & data,MessageParcel & reply)665 int DBinderTestServiceStub::OnReceivedObjectTransAgain(MessageParcel &data, MessageParcel &reply)
666 {
667 int32_t reqData = data.ReadInt32();
668 int32_t operation = data.ReadInt32();
669 sptr<IRemoteObject> proxy = data.ReadRemoteObject();
670 if (proxy == nullptr) {
671 DBINDER_LOGE(LOG_LABEL, "null proxy");
672 return ERR_INVALID_STATE;
673 }
674 sptr<ISystemAbilityManager> manager_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
675 if (manager_ == nullptr) {
676 DBINDER_LOGE(LOG_LABEL, "null manager_");
677 return ERR_INVALID_STATE;
678 }
679 DBINDER_LOGI(LOG_LABEL, "%{public}s:OnReceivedObjectTransAgain-1: reqData=%{public}d", __func__, reqData);
680
681 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE2);
682 if (object == nullptr) {
683 DBINDER_LOGE(LOG_LABEL, "null object of RPC_TEST_SERVICE2");
684 return ERR_INVALID_STATE;
685 }
686
687 MessageOption option;
688 MessageParcel dataParcel, replyParcel;
689 if (!dataParcel.WriteInt32(reqData) || !dataParcel.WriteInt32(operation) ||
690 !dataParcel.WriteRemoteObject(proxy)) {
691 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
692 return ERR_INVALID_STATE;
693 }
694 DBINDER_LOGI(LOG_LABEL, "%{public}s:OnReceivedObjectTransAgain-2: reqData=%{public}d", __func__, reqData);
695 int ret = object->SendRequest(TRANS_RPC_OBJECT_TO_LOCAL, dataParcel, replyParcel, option);
696
697 int reqResult = replyParcel.ReadInt32();
698 DBINDER_LOGI(LOG_LABEL, "%{public}s:OnReceivedObjectTransAgain-3: result=%{public}d", __func__, reqResult);
699
700 if (!reply.WriteInt32(reqResult)) {
701 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
702 return ERR_INVALID_STATE;
703 }
704 DBINDER_LOGI(LOG_LABEL, "%{public}s:OnReceivedObjectTransAgain-4: result=%{public}d", __func__, reqResult);
705 if (!reply.WriteInt32(0)) {
706 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
707 return ERR_INVALID_STATE;
708 }
709 DBINDER_LOGI(LOG_LABEL, "%{public}s:OnReceivedObjectTransAgain-5: result=%{public}d", __func__, reqResult);
710 return ret;
711 }
712
OnReceivedStubObject(MessageParcel & data,MessageParcel & reply)713 int DBinderTestServiceStub::OnReceivedStubObject(MessageParcel &data, MessageParcel &reply)
714 {
715 int32_t reqData = data.ReadInt32();
716 sptr<IRemoteObject> proxy = data.ReadRemoteObject();
717 if (proxy == nullptr) {
718 DBINDER_LOGE(LOG_LABEL, "fail to get proxy");
719 return ERR_INVALID_STATE;
720 }
721
722 MessageOption option;
723 MessageParcel dataParcel, replyParcel;
724 if (!dataParcel.WriteInt32(reqData)) {
725 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
726 return ERR_INVALID_STATE;
727 }
728
729 int error = proxy->SendRequest(REVERSEINT, dataParcel, replyParcel, option);
730 if (error != ERR_NONE) {
731 DBINDER_LOGE(LOG_LABEL, "fail to send data info");
732 return ERR_INVALID_STATE;
733 }
734 int reqResult = replyParcel.ReadInt32();
735 if (!reply.WriteInt32(reqResult)) {
736 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
737 return ERR_INVALID_STATE;
738 }
739
740 if (!reply.WriteRemoteObject(this)) {
741 DBINDER_LOGE(LOG_LABEL, "fail to write parcel stub");
742 return ERR_INVALID_STATE;
743 }
744
745 return error;
746 }
747
OnReceivedStubObjectRefCount(MessageParcel & data,MessageParcel & reply)748 int DBinderTestServiceStub::OnReceivedStubObjectRefCount(MessageParcel &data, MessageParcel &reply)
749 {
750 int32_t operation = data.ReadInt32();
751 sptr<IRemoteObject> proxy = data.ReadRemoteObject();
752 if (proxy == nullptr) {
753 DBINDER_LOGE(LOG_LABEL, "fail to get proxy");
754 return ERR_INVALID_STATE;
755 }
756 if (operation == IDBinderTestService::SAVE) {
757 recvProxy_ = proxy;
758 } else if (operation == IDBinderTestService::WITHDRAW) {
759 recvProxy_ = nullptr;
760 }
761 return ERR_NONE;
762 }
763
OnReceivedGetStubObject(MessageParcel & data,MessageParcel & reply)764 int DBinderTestServiceStub::OnReceivedGetStubObject(MessageParcel &data, MessageParcel &reply)
765 {
766 if (!reply.WriteRemoteObject(GetRemoteObject(data.ReadInt32()))) {
767 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
768 return ERR_INVALID_STATE;
769 }
770
771 return ERR_NONE;
772 }
773
OnReceivedGetDecTimes(MessageParcel & data,MessageParcel & reply)774 int DBinderTestServiceStub::OnReceivedGetDecTimes(MessageParcel &data, MessageParcel &reply)
775 {
776 if (!reply.WriteInt32(GetRemoteDecTimes())) {
777 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
778 return ERR_INVALID_STATE;
779 }
780
781 return ERR_NONE;
782 }
783
OnReceivedClearDecTimes(MessageParcel & data,MessageParcel & reply)784 int DBinderTestServiceStub::OnReceivedClearDecTimes(MessageParcel &data, MessageParcel &reply)
785 {
786 DBINDER_LOGE(LOG_LABEL, "OnReceivedClearDecTimes");
787
788 ClearRemoteDecTimes();
789 return ERR_NONE;
790 }
791
OnReceivedOversizedPkt(MessageParcel & data,MessageParcel & reply)792 int DBinderTestServiceStub::OnReceivedOversizedPkt(MessageParcel &data, MessageParcel &reply)
793 {
794 std::string reqStr = data.ReadString();
795 std::string resultStr = reqStr;
796 if (!reply.WriteString(resultStr)) {
797 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
798 return ERR_INVALID_STATE;
799 }
800
801 return ERR_NONE;
802 }
803
OnReceivedRawData(MessageParcel & data,MessageParcel & reply)804 int DBinderTestServiceStub::OnReceivedRawData(MessageParcel &data, MessageParcel &reply)
805 {
806 int length = data.ReadInt32();
807 if (length <= 1) {
808 DBINDER_LOGE(LOG_LABEL, "length should > 1, length is %{public}d", length);
809 if (!reply.WriteInt32(length)) {
810 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
811 }
812 return ERR_INVALID_DATA;
813 }
814 const char *buffer = nullptr;
815 if ((buffer = reinterpret_cast<const char *>(data.ReadRawData(length))) == nullptr) {
816 if (!reply.WriteInt32(0)) {
817 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
818 }
819 DBINDER_LOGE(LOG_LABEL, "read raw data failed, length = %{public}d", length);
820 return ERR_INVALID_DATA;
821 }
822 if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
823 if (!reply.WriteInt32(0)) {
824 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
825 }
826 DBINDER_LOGE(LOG_LABEL, "buffer error, length = %{public}d", length);
827 return ERR_INVALID_DATA;
828 }
829 if (data.ReadInt32() != length) {
830 if (!reply.WriteInt32(0)) {
831 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
832 }
833 DBINDER_LOGE(LOG_LABEL, "read raw data after failed, length = %{public}d", length);
834 return ERR_INVALID_DATA;
835 }
836 if (!reply.WriteInt32(length)) {
837 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
838 return ERR_INVALID_STATE;
839 }
840
841 return ERR_NONE;
842 }
843
OnSentRawData(MessageParcel & data,MessageParcel & reply)844 int DBinderTestServiceStub::OnSentRawData(MessageParcel &data, MessageParcel &reply)
845 {
846 int length = data.ReadInt32();
847 if (length <= 1) {
848 DBINDER_LOGE(LOG_LABEL, "length should > 1, length is %{public}d", length);
849 if (!reply.WriteInt32(length)) {
850 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
851 }
852 return ERR_INVALID_DATA;
853 }
854
855 unsigned char *buffer = new (std::nothrow) unsigned char[length];
856 if (buffer == nullptr) {
857 DBINDER_LOGE(LOG_LABEL, "new buffer failed of length = %{public}d", length);
858 return ERR_INVALID_STATE;
859 }
860 buffer[0] = 'a';
861 buffer[length - 1] = 'z';
862 if (!reply.WriteInt32(length) || !reply.WriteRawData(buffer, length) || !reply.WriteInt32(length)) {
863 DBINDER_LOGE(LOG_LABEL, "fail to write parcel");
864 delete[] buffer;
865 return ERR_INVALID_STATE;
866 }
867 delete[] buffer;
868 return ERR_NONE;
869 }
870
871 bool DBinderTestDeathRecipient::g_gotDeathRecipient = false;
GotDeathRecipient()872 bool DBinderTestDeathRecipient::GotDeathRecipient()
873 {
874 return g_gotDeathRecipient;
875 }
876
ClearDeathRecipient()877 void DBinderTestDeathRecipient::ClearDeathRecipient()
878 {
879 g_gotDeathRecipient = false;
880 }
881
OnRemoteDied(const wptr<IRemoteObject> & remote)882 void DBinderTestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
883 {
884 g_gotDeathRecipient = true;
885 printf("Succ! Remote Died!\n");
886 DBINDER_LOGE(LOG_LABEL, "recv death notification");
887 }
888
DBinderTestDeathRecipient()889 DBinderTestDeathRecipient::DBinderTestDeathRecipient() {}
890
~DBinderTestDeathRecipient()891 DBinderTestDeathRecipient::~DBinderTestDeathRecipient() {}
892 } // namespace OHOS
893