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 "test_service_skeleton.h"
17 #include <cinttypes>
18 #include <fcntl.h>
19 #include <iostream>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <thread>
27 #include "access_token_adapter.h"
28 #include "ipc_debug.h"
29 #include "string_ex.h"
30 #include "iremote_proxy.h"
31 #include "ipc_skeleton.h"
32 #include "ipc_file_descriptor.h"
33 #include "ipc_test_helper.h"
34 #include "if_system_ability_manager.h"
35 #include "iservice_registry.h"
36 #include "system_ability_definition.h"
37
38 namespace OHOS {
39 using namespace OHOS::HiviewDFX;
40
41 constexpr int32_t MAX_RECURSIVE_SENDS = 5;
42 constexpr int32_t SEDNREQUEST_TIMES = 2000;
43 constexpr int32_t NUMBER_OF_THREADS = 20;
44
InitMessageProcessMap()45 void TestServiceStub::InitMessageProcessMap()
46 {
47 funcMap_[static_cast<uint32_t>(TRANS_ID_SYNC_TRANSACTION)] = &TestServiceStub::ServerSyncTransaction;
48 funcMap_[static_cast<uint32_t>(TRANS_ID_ASYNC_TRANSACTION)] = &TestServiceStub::ServerAsyncTransaction;
49 funcMap_[static_cast<uint32_t>(TRANS_ID_PING_SERVICE)] = &TestServiceStub::ServerPingService;
50 funcMap_[static_cast<uint32_t>(TRANS_ID_GET_FOO_SERVICE)] = &TestServiceStub::ServerGetFooService;
51 funcMap_[static_cast<uint32_t>(TRANS_ID_TRANSACT_FILE_DESC)] = &TestServiceStub::ServerTransactFileDesc;
52 funcMap_[static_cast<uint32_t>(TRANS_ID_STRING_TRANSACTION)] = &TestServiceStub::ServerStringTransaction;
53 funcMap_[static_cast<uint32_t>(TRANS_ID_ZTRACE_TRANSACTION)] = &TestServiceStub::ServerZtraceTransaction;
54 funcMap_[static_cast<uint32_t>(TRANS_ID_RAWDATA_TRANSACTION)] = &TestServiceStub::TransferRawData;
55 funcMap_[static_cast<uint32_t>(TRANS_ID_RAWDATA_REPLY)] = &TestServiceStub::ReplyRawData;
56 funcMap_[static_cast<uint32_t>(TRANS_ID_FLUSH_ASYNC_CALLS)] = &TestServiceStub::ServerFlushAsyncCalls;
57 funcMap_[static_cast<uint32_t>(TRANS_ID_ASHMEM)] = &TestServiceStub::ReadAshmem;
58 funcMap_[static_cast<uint32_t>(TRANS_ID_MULTIPLE_PROCESSES)] = &TestServiceStub::TransferToNextProcess;
59 funcMap_[static_cast<uint32_t>(TRANS_ID_CALLING_UID_PID)] = &TestServiceStub::ServerCallingUidAndPid;
60 funcMap_[static_cast<uint32_t>(TRANS_ID_NESTING_SEND)] = &TestServiceStub::ServerNestingSend;
61 funcMap_[static_cast<uint32_t>(TRANS_ID_ACCESS_TOKENID)] = &TestServiceStub::ServerAccessTokenId;
62 funcMap_[static_cast<uint32_t>(TRANS_ID_ACCESS_TOKENID_64)] = &TestServiceStub::ServerAccessTokenId64;
63 funcMap_[static_cast<uint32_t>(TRANS_MESSAGE_PARCEL_ADDPED)] = &TestServiceStub::ServerMessageParcelAddped;
64 funcMap_[static_cast<uint32_t>(TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT)] =
65 &TestServiceStub::ServerMessageParcelAddpedWithObject;
66 funcMap_[static_cast<uint32_t>(TRANS_ENABLE_SERIAL_INVOKE_FLAG)] = &TestServiceStub::ServerEnableSerialInvokeFlag;
67 funcMap_[static_cast<uint32_t>(TRANS_ID_REGISTER_REMOTE_STUB_OBJECT)] = &TestServiceStub::RegisterRemoteStub;
68 funcMap_[static_cast<uint32_t>(TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT)] = &TestServiceStub::UnRegisterRemoteStub;
69 funcMap_[static_cast<uint32_t>(TRANS_ID_QUERY_REMOTE_PROXY_OBJECT)] = &TestServiceStub::QueryRemoteProxy;
70 }
71
72
TestServiceStub(bool serialInvokeFlag)73 TestServiceStub::TestServiceStub(bool serialInvokeFlag)
74 : IRemoteStub(serialInvokeFlag), serialInvokeFlag_(serialInvokeFlag)
75 {
76 InitMessageProcessMap();
77 }
78
TestServiceProxy(const sptr<IRemoteObject> & impl)79 TestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl)
80 : IRemoteProxy<ITestService>(impl)
81 {
82 }
83
TestEnableSerialInvokeFlag()84 int TestServiceProxy::TestEnableSerialInvokeFlag()
85 {
86 int error;
87 MessageOption option;
88 MessageParcel dataParcel, replyParcel;
89 std::string value = "testData";
90
91 ZLOGD(LABEL, "send to server data = %{public}s", value.c_str());
92 dataParcel.WriteString(value);
93 error = Remote()->SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
94 std::string reply = replyParcel.ReadString();
95 ZLOGD(LABEL, "get result from server data = %{public}s", reply.c_str());
96 return error;
97 }
98
TestSyncTransaction(int data,int & reply,int delayTime)99 int TestServiceProxy::TestSyncTransaction(int data, int &reply, int delayTime)
100 {
101 int error;
102 MessageOption option;
103 MessageParcel dataParcel, replyParcel;
104 ZLOGD(LABEL, "send to server data = %{public}d", data);
105 if (data > 0) {
106 dataParcel.WriteInt32(data);
107 dataParcel.WriteInt32(delayTime);
108 }
109 error = Remote()->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
110 reply = replyParcel.ReadInt32();
111 ZLOGD(LABEL, "get result from server data = %{public}d", reply);
112 return error;
113 }
114
TestSendTooManyRequest(int data,int & reply)115 int TestServiceProxy::TestSendTooManyRequest(int data, int &reply)
116 {
117 int error;
118 int delayTime = 0;
119 MessageOption option;
120 MessageParcel dataParcel;
121 MessageParcel replyParcel[SEDNREQUEST_TIMES];
122
123 ZLOGD(LABEL, "send to server data = %{public}d", data);
124 if (data > 0) {
125 dataParcel.WriteInt32(data);
126 dataParcel.WriteInt32(delayTime);
127 }
128 for (int32_t i = 0; i < SEDNREQUEST_TIMES; i++) {
129 error = Remote()->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel[i], option);
130 }
131 reply = replyParcel[0].ReadInt32();
132 ZLOGD(LABEL, "get result from server data = %{public}d", reply);
133 return error;
134 }
135
TestMultiThreadSendRequest(int data,int & reply)136 int TestServiceProxy::TestMultiThreadSendRequest(int data, int &reply)
137 {
138 int error = 0;
139
140 sptr<IRemoteObject> proxyObject = Remote();
141 for (int32_t i = 0; i < NUMBER_OF_THREADS; i++) {
142 // Different threads correspond to different sets of parcels.
143 std::thread t([&proxyObject, &data, &reply] {
144 MessageParcel dataParcel;
145 MessageParcel replyParcel;
146 MessageOption option;
147 if (data > 0) {
148 dataParcel.WriteInt32(data);
149 dataParcel.WriteInt32(0);
150 }
151 int error = proxyObject->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
152 if (error != 0) {
153 ZLOGD(LABEL, "SendRequest is failed: %{public}d", error);
154 return;
155 }
156 reply = replyParcel.ReadInt32();
157 });
158 t.detach();
159 std::cout << "Thead: " << i << std::endl;
160 }
161
162 return error;
163 }
164
TestAsyncTransaction(int data,int timeout)165 int TestServiceProxy::TestAsyncTransaction(int data, int timeout)
166 {
167 MessageOption option = { MessageOption::TF_ASYNC };
168 MessageParcel dataParcel, replyParcel;
169 ZLOGD(LABEL, "%{public}s:in, data = %{public}d", __func__, data);
170 dataParcel.WriteInt32(data);
171 dataParcel.WriteInt32(timeout);
172 dataParcel.WriteBool(false);
173 return Remote()->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
174 }
175
TestAsyncCallbackTrans(int data,int & reply,int timeout)176 int TestServiceProxy::TestAsyncCallbackTrans(int data, int &reply, int timeout)
177 {
178 int error;
179 MessageOption option = { MessageOption::TF_ASYNC };
180 MessageParcel dataParcel, replyParcel;
181 ZLOGD(LABEL, "%{public}s:in, data = %{public}d", __func__, data);
182 dataParcel.WriteInt32(data);
183 dataParcel.WriteInt32(timeout);
184 dataParcel.WriteBool(true);
185 sptr<FooStub> fooCallback = new FooStub();
186 dataParcel.WriteRemoteObject(fooCallback->AsObject());
187 error = Remote()->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
188 reply = fooCallback->WaitForAsyncReply(timeout);
189
190 FooStub::CleanDecTimes();
191 fooCallback = nullptr;
192 if (FooStub::GetDecTimes() != 1) {
193 error = ERR_TRANSACTION_FAILED;
194 }
195 return error;
196 }
197
TestZtraceTransaction(std::string & send,std::string & reply,int len)198 int TestServiceProxy::TestZtraceTransaction(std::string &send, std::string &reply, int len)
199 {
200 int error;
201 MessageParcel dataParcel, replyParcel;
202 MessageOption option;
203
204 dataParcel.WriteInt32(len);
205 dataParcel.WriteString(send);
206 error = Remote()->SendRequest(TRANS_ID_ZTRACE_TRANSACTION, dataParcel, replyParcel, option);
207 reply = replyParcel.ReadString();
208
209 return error;
210 }
211
TestPingService(const std::u16string & serviceName)212 int TestServiceProxy::TestPingService(const std::u16string &serviceName)
213 {
214 int error;
215 MessageOption option;
216 MessageParcel dataParcel, replyParcel;
217 ZLOGD(LABEL, "PingService");
218 dataParcel.WriteString16(serviceName);
219 error = Remote()->SendRequest(TRANS_ID_PING_SERVICE, dataParcel, replyParcel, option);
220 int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1;
221 ZLOGD(LABEL, "PingService result = %{public}d", result);
222 return result;
223 }
224
TestGetFooService()225 sptr<IFoo> TestServiceProxy::TestGetFooService()
226 {
227 MessageOption option;
228 MessageParcel dataParcel, replyParcel;
229 Remote()->SendRequest(TRANS_ID_GET_FOO_SERVICE, dataParcel, replyParcel, option);
230 auto rep = replyParcel.ReadRemoteObject();
231 if (rep == nullptr) {
232 ZLOGE(LABEL, "null foo service");
233 return nullptr;
234 }
235 return iface_cast<IFoo>(rep);
236 }
237
TestGetFileDescriptor()238 int TestServiceProxy::TestGetFileDescriptor()
239 {
240 MessageOption option;
241 MessageParcel dataParcel, replyParcel;
242 sptr<IPCFileDescriptor> desc;
243 option.SetFlags(MessageOption::TF_ACCEPT_FDS);
244 Remote()->SendRequest(TRANS_ID_TRANSACT_FILE_DESC, dataParcel, replyParcel, option);
245 int fd = replyParcel.ReadFileDescriptor();
246 return fd;
247 }
248
TestStringTransaction(const std::string & data)249 int TestServiceProxy::TestStringTransaction(const std::string &data)
250 {
251 MessageOption option;
252 MessageParcel dataParcel, replyParcel;
253 dataParcel.WriteString(data);
254 Remote()->SendRequest(TRANS_ID_STRING_TRANSACTION, dataParcel, replyParcel, option);
255 int testSize = replyParcel.ReadInt32();
256 return testSize;
257 }
258
TestDumpService()259 void TestServiceProxy::TestDumpService()
260 {
261 ZLOGD(LABEL, "call StartDumpService");
262 int fd = open("/data/dump.txt",
263 O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
264 if (fd != INVALID_FD) {
265 ZLOGD(LABEL, "Start Dump Service");
266 std::vector<std::u16string> args;
267 args.push_back(u"DumpTest");
268 Remote()->Dump(fd, args);
269 close(fd);
270 }
271 }
272
TestAsyncDumpService()273 void TestServiceProxy::TestAsyncDumpService()
274 {
275 int fd = open("/data/nonblockingDump.txt",
276 O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
277 if (fd == INVALID_FD) {
278 return;
279 }
280
281 ZLOGD(LABEL, "Start Async Dump Service");
282 std::vector<std::u16string> args;
283 args.push_back(u"NonblockingDumpTest");
284 MessageParcel data, reply;
285 MessageOption option {MessageOption::TF_ASYNC};
286 data.WriteFileDescriptor(fd);
287 data.WriteString16Vector(args);
288 (void)Remote()->SendRequest(DUMP_TRANSACTION, data, reply, option);
289 close(fd);
290 }
291
TestRawDataTransaction(int length,int & reply)292 int TestServiceProxy::TestRawDataTransaction(int length, int &reply)
293 {
294 int error;
295 MessageOption option;
296 MessageParcel dataParcel, replyParcel;
297 ZLOGE(LABEL, "send to server data length = %{public}d", length);
298 if (length <= 1 || static_cast<unsigned>(length) > dataParcel.GetRawDataCapacity()) {
299 ZLOGE(LABEL, "length should > 1, length is %{public}d", length);
300 return -1;
301 }
302 unsigned char *buffer = new (std::nothrow) unsigned char[length];
303 if (buffer == nullptr) {
304 ZLOGE(LABEL, "new buffer failed of length = %{public}d", length);
305 return -1;
306 }
307 buffer[0] = 'a';
308 buffer[length - 1] = 'z';
309
310 dataParcel.WriteInt32(length);
311 dataParcel.WriteRawData(buffer, length);
312 dataParcel.WriteInt32(length);
313 error = Remote()->SendRequest(TRANS_ID_RAWDATA_TRANSACTION, dataParcel, replyParcel, option);
314 reply = replyParcel.ReadInt32();
315 ZLOGE(LABEL, "get result from server data = %{public}d", reply);
316 delete [] buffer;
317 return error;
318 }
319
TestRawDataReply(int length)320 int TestServiceProxy::TestRawDataReply(int length)
321 {
322 MessageOption option;
323 MessageParcel dataParcel, replyParcel;
324 if (length <= 1 || static_cast<unsigned>(length) > dataParcel.GetRawDataCapacity()) {
325 ZLOGE(LABEL, "length should > 1, length is %{public}d", length);
326 return ERR_INVALID_STATE;
327 }
328
329 if (!dataParcel.WriteInt32(length)) {
330 ZLOGE(LABEL, "fail to write parcel");
331 return ERR_INVALID_STATE;
332 }
333
334 int ret = Remote()->SendRequest(TRANS_ID_RAWDATA_REPLY, dataParcel, replyParcel, option);
335 if (ret != ERR_NONE) {
336 ZLOGE(LABEL, "ret = %{public}d", ret);
337 return ret;
338 }
339
340 if (replyParcel.ReadInt32() != length) {
341 ZLOGE(LABEL, "reply false data");
342 return ERR_INVALID_DATA;
343 }
344
345 if (!replyParcel.ContainFileDescriptors()) {
346 ZLOGE(LABEL, "replied raw data is less than 32k");
347 }
348
349 const char *buffer = nullptr;
350 if ((buffer = reinterpret_cast<const char *>(replyParcel.ReadRawData((size_t)length))) == nullptr) {
351 ZLOGE(LABEL, "read raw data failed, length = %{public}d", length);
352 return ERR_INVALID_DATA;
353 }
354 if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
355 ZLOGE(LABEL, "buffer error, length = %{public}d", length);
356 return ERR_INVALID_DATA;
357 }
358
359 if (replyParcel.ReadInt32() != length) {
360 ZLOGE(LABEL, "read raw data after failed, length = %{public}d", length);
361 return ERR_INVALID_DATA;
362 }
363
364 return ERR_NONE;
365 }
366
TestCallingUidPid()367 int TestServiceProxy::TestCallingUidPid()
368 {
369 MessageOption option;
370 MessageParcel dataParcel, replyParcel;
371 int ret = Remote()->SendRequest(TRANS_ID_CALLING_UID_PID, dataParcel, replyParcel, option);
372 if (ret != ERR_NONE) {
373 ZLOGE(LABEL, "ret = %{public}d", ret);
374 return ret;
375 }
376
377 int uid = replyParcel.ReadInt32();
378 int pid = replyParcel.ReadInt32();
379
380 IPCTestHelper helper;
381 int actualUid = static_cast<int>(helper.GetUid());
382 int actualPid = static_cast<int>(helper.GetPid());
383 ZLOGD(LABEL, "uid = %{public}d, pid = %{public}d, actualUid = %{public}d, actualPid = %{public}d",
384 uid, pid, actualUid, actualPid);
385
386 if (uid == actualUid && pid == actualPid) {
387 return 0;
388 }
389 return -1;
390 }
391
TestRegisterRemoteStub(const char * descriptor,const sptr<IRemoteObject> object)392 int TestServiceProxy::TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object)
393 {
394 MessageOption option;
395 MessageParcel dataParcel;
396 MessageParcel replyParcel;
397 dataParcel.WriteString(descriptor);
398 dataParcel.WriteRemoteObject(object);
399 int ret = Remote()->SendRequest(TRANS_ID_REGISTER_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
400 if (ret != ERR_NONE) {
401 ZLOGE(LABEL, "ret = %{public}d", ret);
402 return ret;
403 }
404 return 0;
405 }
406
TestUnRegisterRemoteStub(const char * descriptor)407 int TestServiceProxy::TestUnRegisterRemoteStub(const char *descriptor)
408 {
409 MessageOption option;
410 MessageParcel dataParcel;
411 MessageParcel replyParcel;
412 dataParcel.WriteString(descriptor);
413 int ret = Remote()->SendRequest(TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
414 if (ret != ERR_NONE) {
415 ZLOGE(LABEL, "ret = %{public}d", ret);
416 return ret;
417 }
418 return 0;
419 }
420
TestQueryRemoteProxy(const char * descriptor)421 sptr<IRemoteObject> TestServiceProxy::TestQueryRemoteProxy(const char *descriptor)
422 {
423 MessageOption option;
424 MessageParcel dataParcel;
425 MessageParcel replyParcel;
426 dataParcel.WriteString(descriptor);
427 int ret = Remote()->SendRequest(TRANS_ID_QUERY_REMOTE_PROXY_OBJECT, dataParcel, replyParcel, option);
428 if (ret != ERR_NONE) {
429 ZLOGE(LABEL, "ret = %{public}d", ret);
430 return nullptr;
431 }
432 auto readRemoteObject = replyParcel.ReadRemoteObject();
433 return readRemoteObject;
434 }
435
436 constexpr char ACCESS_TOKEN_ID_IOCTL_BASE = 'A';
437
438 enum {
439 GET_TOKEN_ID = 1,
440 SET_TOKEN_ID,
441 GET_FTOKEN_ID,
442 SET_FTOKEN_ID,
443 ACCESS_TOKENID_MAX_NR,
444 };
445
446 #define ACCESS_TOKENID_SET_TOKENID \
447 _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_TOKEN_ID, unsigned long long)
448 #define ACCESS_TOKENID_SET_FTOKENID \
449 _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_FTOKEN_ID, unsigned long long)
450
451 constexpr int ACCESS_TOKEN_OK = 0;
452 constexpr int ACCESS_TOKEN_ERROR = -1;
453
454 constexpr char TOKENID_DEVNODE[] = "/dev/access_token_id";
455
RpcSetSelfTokenID(uint64_t tokenID)456 int RpcSetSelfTokenID(uint64_t tokenID)
457 {
458 int fd = open(TOKENID_DEVNODE, O_RDWR);
459 if (fd < 0) {
460 return ACCESS_TOKEN_ERROR;
461 }
462 int ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, &tokenID);
463 if (ret) {
464 close(fd);
465 return ACCESS_TOKEN_ERROR;
466 }
467
468 close(fd);
469 return ACCESS_TOKEN_OK;
470 }
471
RpcSetFirstCallerTokenID(uint64_t tokenID)472 int RpcSetFirstCallerTokenID(uint64_t tokenID)
473 {
474 int fd = open(TOKENID_DEVNODE, O_RDWR);
475 if (fd < 0) {
476 return ACCESS_TOKEN_ERROR;
477 }
478 int ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, &tokenID);
479 if (ret) {
480 close(fd);
481 return ACCESS_TOKEN_ERROR;
482 }
483
484 close(fd);
485 return ACCESS_TOKEN_OK;
486 }
487
CheckTokenSelf(uint64_t token,uint64_t tokenSelf,uint64_t ftoken,uint64_t ftoken_expected)488 bool TestServiceProxy::CheckTokenSelf(uint64_t token, uint64_t tokenSelf, uint64_t ftoken, uint64_t ftoken_expected)
489 {
490 if (token != tokenSelf) {
491 ZLOGE(LABEL, "token != tokenSelf");
492 return false;
493 }
494 if (ftoken != ftoken_expected) {
495 ZLOGE(LABEL, "ftoken != ftoken_expected");
496 return false;
497 }
498 return true;
499 }
500
CheckSetFirstToken(uint64_t ftoken_expected)501 bool TestServiceProxy::CheckSetFirstToken(uint64_t ftoken_expected)
502 {
503 int ret = RpcSetFirstCallerTokenID(ftoken_expected);
504 if (ret != 0) {
505 ZLOGE(LABEL, "RpcSetFirstCallerTokenID ret = %{public}d", ret);
506 return false;
507 }
508 uint64_t result = RpcGetFirstCallerTokenID();
509 if (result != ftoken_expected) {
510 ZLOGE(LABEL, "TestServiceProxy get ftoken after set: %{public}" PRIu64, result);
511 return false;
512 }
513 return true;
514 }
515
CheckSetSelfToken(uint64_t token_expected)516 bool TestServiceProxy::CheckSetSelfToken(uint64_t token_expected)
517 {
518 int ret = RpcSetSelfTokenID(token_expected);
519 if (ret != 0) {
520 ZLOGE(LABEL, "RpcSetSelfTokenID ret = %{public}d", ret);
521 return false;
522 }
523 uint64_t result = RpcGetSelfTokenID();
524 if (result != token_expected) {
525 ZLOGE(LABEL, "TestServiceProxy get selftoken after set: %{public}" PRIu64, result);
526 return false;
527 }
528 return true;
529 }
530
TestAccessTokenID64(uint64_t token_expected,uint64_t ftoken_expected)531 int TestServiceProxy::TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected)
532 {
533 MessageOption option;
534 MessageParcel dataParcel, replyParcel1, replyParcel2;
535 uint64_t token = IPCSkeleton::GetCallingFullTokenID();
536 uint64_t ftoken = IPCSkeleton::GetFirstFullTokenID();
537 uint64_t tokenSelf = IPCSkeleton::GetSelfTokenID();
538 uint64_t oldTokenSelf = tokenSelf;
539 int32_t ret = 0;
540
541 if (!CheckTokenSelf(token, tokenSelf, ftoken, 0)) {
542 return -1;
543 }
544 if (!CheckSetFirstToken(ftoken_expected)) {
545 ret = -1;
546 goto ERR;
547 }
548 if (!CheckSetSelfToken(token_expected)) {
549 ret = -1;
550 goto ERR;
551 }
552 ret = Remote()->SendRequest(TRANS_ID_ACCESS_TOKENID_64, dataParcel, replyParcel1, option);
553 if (ret != ERR_NONE) {
554 ZLOGE(LABEL, "SendRequest ret = %{public}d", ret);
555 ret = -1;
556 goto ERR;
557 }
558 token = replyParcel1.ReadUint64();
559 ftoken = replyParcel1.ReadUint64();
560
561 if (token != token_expected) {
562 ZLOGE(LABEL, "token != token_expected, token:%{public}" PRIu64, token);
563 ret = -1;
564 goto ERR;
565 }
566 if (ftoken != ftoken_expected) {
567 ZLOGE(LABEL, "ftoken != ftoken_expected, ftoken:%{public}" PRIu64, ftoken);
568 ret = -1;
569 goto ERR;
570 }
571
572 ERR:
573 RpcSetFirstCallerTokenID(0);
574 RpcSetSelfTokenID(oldTokenSelf);
575 return ret;
576 }
577
TestAccessTokenID(int32_t ftoken_expected)578 int TestServiceProxy::TestAccessTokenID(int32_t ftoken_expected)
579 {
580 MessageOption option;
581 MessageParcel dataParcel, replyParcel1, replyParcel2;
582
583 int32_t token = (int32_t)IPCSkeleton::GetCallingTokenID();
584 int32_t ftoken = (int32_t)IPCSkeleton::GetFirstTokenID();
585 int32_t tokenSelf = RpcGetSelfTokenID();
586 ZLOGE(LABEL, "TestServiceProxy tokenSelf: %{public}d", tokenSelf);
587 ZLOGE(LABEL, "TestServiceProxy ftoken: %{public}d", ftoken);
588 ZLOGE(LABEL, "TestServiceProxy ftoken_expected: %{public}d", ftoken_expected);
589 if (!CheckTokenSelf(token, tokenSelf, ftoken, 0)) {
590 ZLOGE(LABEL, "first");
591 return -1;
592 }
593 if (!CheckSetFirstToken(ftoken_expected)) {
594 return -1;
595 }
596 int ret = Remote()->SendRequest(TRANS_ID_ACCESS_TOKENID, dataParcel, replyParcel1, option);
597 if (ret != ERR_NONE) {
598 ZLOGE(LABEL, "SendRequest ret = %{public}d", ret);
599 return ret;
600 }
601 token = replyParcel1.ReadInt32();
602 ftoken = replyParcel1.ReadInt32();
603 if (!CheckTokenSelf(token, tokenSelf, ftoken, ftoken_expected)) {
604 ZLOGE(LABEL, "second");
605 return -1;
606 }
607 if (!CheckSetSelfToken(666)) { // 666: test data
608 return -1;
609 }
610 ret = Remote()->SendRequest(TRANS_ID_ACCESS_TOKENID, dataParcel, replyParcel2, option);
611 if (ret != ERR_NONE) {
612 ZLOGE(LABEL, "ret = %{public}d", ret);
613 return ret;
614 }
615 token = replyParcel2.ReadInt32();
616 ftoken = replyParcel2.ReadInt32();
617 if (!CheckTokenSelf(token, tokenSelf, ftoken, ftoken_expected)) {
618 ZLOGE(LABEL, "third");
619 return -1;
620 }
621 ret = RpcSetFirstCallerTokenID(0);
622 ret = RpcSetSelfTokenID(0);
623 if (ret != ERR_NONE) {
624 return -1;
625 }
626 return 0;
627 }
628
TestMessageParcelAppend(MessageParcel & dst,MessageParcel & src)629 int TestServiceProxy::TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src)
630 {
631 bool res = dst.Append(src);
632 if (!res) {
633 ZLOGE(LABEL, "message parcel append without ipc failed");
634 return -1;
635 }
636 return 0;
637 }
638
TestMessageParcelAppendWithIpc(MessageParcel & dst,MessageParcel & src,MessageParcel & reply,bool withObject)639 int TestServiceProxy::TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
640 MessageParcel &reply, bool withObject)
641 {
642 bool res = dst.Append(src);
643 if (!res) {
644 ZLOGE(LABEL, "message parcel append with ipc failed");
645 return -1;
646 }
647 MessageOption option;
648 uint32_t code = TRANS_MESSAGE_PARCEL_ADDPED;
649 if (withObject) {
650 code = TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT;
651 }
652 int ret = Remote()->SendRequest(code, dst, reply, option);
653 ZLOGE(LABEL, "TestMessageParcelAppend with ipc sendrequest ret = %{public}d", ret);
654 return ret;
655 }
656
TestFlushAsyncCalls(int count,int length)657 int TestServiceProxy::TestFlushAsyncCalls(int count, int length)
658 {
659 int ret;
660 MessageOption option = { MessageOption::TF_ASYNC };
661 MessageParcel dataParcel, replyParcel;
662 std::u16string legalData(length, 'a');
663 dataParcel.WriteString16(legalData);
664 for (int i = 0; i < count; i++) {
665 ret = Remote()->SendRequest(TRANS_ID_FLUSH_ASYNC_CALLS, dataParcel, replyParcel, option);
666 if (ret != ERR_NONE) {
667 ZLOGE(LABEL, "fail to send request when count = %{public}d, ret = %{public}d", count, ret);
668 return ret;
669 }
670 }
671
672 ret = IPCSkeleton::FlushCommands(this->AsObject());
673 return ret;
674 }
675
TestMultipleProcesses(int data,int & rep,int delayTime)676 int TestServiceProxy::TestMultipleProcesses(int data, int &rep, int delayTime)
677 {
678 int error;
679 MessageOption option;
680 MessageParcel dataParcel, replyParcel;
681 ZLOGD(LABEL, "send to server data = %{public}d", data);
682 if (data > 0) {
683 dataParcel.WriteInt32(data);
684 dataParcel.WriteInt32(delayTime);
685 }
686 error = Remote()->SendRequest(TRANS_ID_MULTIPLE_PROCESSES, dataParcel, replyParcel, option);
687 rep = replyParcel.ReadInt32();
688 error += replyParcel.ReadInt32();
689 return error;
690 }
691
TestAshmem(sptr<Ashmem> ashmem,int32_t contentSize)692 std::u16string TestServiceProxy::TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize)
693 {
694 if (ashmem == nullptr || contentSize <= 0) {
695 return u"";
696 }
697
698 MessageOption option;
699 MessageParcel dataParcel, replyParcel;
700 if (!dataParcel.WriteInt32(contentSize) || !dataParcel.WriteAshmem(ashmem)) {
701 return u"";
702 }
703
704 int error = Remote()->SendRequest(TRANS_ID_ASHMEM, dataParcel, replyParcel, option);
705 if (error != ERR_NONE) {
706 return u"";
707 }
708
709 int32_t readContentSize = replyParcel.ReadInt32();
710 if (readContentSize <= 0) {
711 return u"";
712 }
713
714 sptr<Ashmem> ashmem2 = replyParcel.ReadAshmem();
715 if (ashmem2 == nullptr || !ashmem2->MapReadAndWriteAshmem()) {
716 return u"";
717 }
718
719 const void *content = ashmem2->ReadFromAshmem(readContentSize, 0);
720 if (content == nullptr) {
721 ashmem2->UnmapAshmem();
722 return u"";
723 }
724
725 auto readContent = static_cast<const char *>(content);
726 std::string str(readContent, readContentSize);
727
728 ashmem2->UnmapAshmem();
729 ashmem2->CloseAshmem();
730 return Str8ToStr16(str);
731 }
732
TestNestingSend(int sendCode,int & replyCode)733 int TestServiceProxy::TestNestingSend(int sendCode, int &replyCode)
734 {
735 ZLOGW(LABEL, "%{public}s", __func__);
736 MessageOption option;
737 MessageParcel dataParcel, replyParcel;
738 sptr<IFoo> foo = new FooStub();
739 sptr<IRemoteObject> sendFoo = foo->AsObject();
740 sendFoo->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
741 if (!dataParcel.WriteRemoteObject(sendFoo) || !dataParcel.WriteInt32(sendCode)) {
742 ZLOGE(LABEL, "%{public}s: fail to write data", __func__);
743 return -1;
744 }
745 int error = Remote()->SendRequest(TRANS_ID_NESTING_SEND, dataParcel, replyParcel, option);
746 replyCode = replyParcel.ReadInt32();
747 ZLOGW(LABEL, "%{public}s: outer = %{public}d, inner = %{public}d", __func__, error, replyCode);
748 return error;
749 }
750
ServerSyncTransaction(MessageParcel & data,MessageParcel & reply)751 int32_t TestServiceStub::ServerSyncTransaction(MessageParcel &data, MessageParcel &reply)
752 {
753 int32_t result = 0;
754 int32_t reqData = data.ReadInt32();
755 int32_t delayTime = data.ReadInt32();
756 int32_t ret = TestSyncTransaction(reqData, result, delayTime);
757 reply.WriteInt32(result);
758 return ret;
759 }
760
ServerAsyncTransaction(MessageParcel & data,MessageParcel & reply)761 int32_t TestServiceStub::ServerAsyncTransaction(MessageParcel &data, MessageParcel &reply)
762 {
763 int32_t result = 0;
764 int32_t reqData = data.ReadInt32();
765 int timeout = data.ReadInt32();
766 bool acquireResult = data.ReadBool();
767 if (acquireResult) {
768 sptr<IFoo> fooProxy = iface_cast<IFoo>(data.ReadRemoteObject());
769 if (fooProxy != nullptr) {
770 TestAsyncCallbackTrans(reqData, result, timeout);
771 fooProxy->SendAsyncReply(result);
772 }
773 } else {
774 (void)TestAsyncTransaction(reqData, timeout);
775 }
776 return 0;
777 }
778
ServerPingService(MessageParcel & data,MessageParcel & reply)779 int32_t TestServiceStub::ServerPingService(MessageParcel &data, MessageParcel &reply)
780 {
781 std::u16string serviceName = data.ReadString16();
782 int32_t result = TestPingService(serviceName);
783 ZLOGD(LABEL, "%{public}s:PingService: result=%{public}d", __func__, result);
784 reply.WriteInt32(result);
785 return 0;
786 }
787
ServerGetFooService(MessageParcel & data,MessageParcel & reply)788 int32_t TestServiceStub::ServerGetFooService(MessageParcel &data, MessageParcel &reply)
789 {
790 sptr<IFoo> fooService = TestGetFooService();
791 sptr<IRemoteObject> object = fooService->AsObject();
792 object->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
793 reply.WriteRemoteObject(object);
794 return 0;
795 }
796
ServerTransactFileDesc(MessageParcel & data,MessageParcel & reply)797 int32_t TestServiceStub::ServerTransactFileDesc(MessageParcel &data, MessageParcel &reply)
798 {
799 int desc = TestGetFileDescriptor();
800 reply.WriteFileDescriptor(desc);
801 close(desc);
802 return 0;
803 }
804
ServerStringTransaction(MessageParcel & data,MessageParcel & reply)805 int32_t TestServiceStub::ServerStringTransaction(MessageParcel &data, MessageParcel &reply)
806 {
807 const std::string testString = data.ReadString();
808 int testSize = TestStringTransaction(testString);
809 reply.WriteInt32(testSize);
810 reply.WriteString(testString);
811 return 0;
812 }
813
ServerZtraceTransaction(MessageParcel & data,MessageParcel & reply)814 int32_t TestServiceStub::ServerZtraceTransaction(MessageParcel &data, MessageParcel &reply)
815 {
816 int32_t ret = 0;
817 int32_t len = data.ReadInt32();
818 if (len < 0) {
819 ZLOGE(LABEL, "%{public}s:get len failed, len = %{public}d", __func__, len);
820 return ret;
821 }
822 std::string recvString;
823 std::string replyString(len, 0);
824 recvString = data.ReadString();
825 ret = TestZtraceTransaction(recvString, replyString, len);
826 if (!ret) {
827 reply.WriteString(replyString);
828 }
829 return ret;
830 }
831
ServerCallingUidAndPid(MessageParcel & data,MessageParcel & reply)832 int32_t TestServiceStub::ServerCallingUidAndPid(MessageParcel &data, MessageParcel &reply)
833 {
834 reply.WriteInt32(IPCSkeleton::GetCallingUid());
835 reply.WriteInt32(IPCSkeleton::GetCallingPid());
836
837 ZLOGE(LABEL, "calling before reset uid = %{public}d, pid = %{public}d",
838 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
839 std::string token = IPCSkeleton::ResetCallingIdentity();
840
841 ZLOGE(LABEL, "calling before set uid = %{public}d, pid = %{public}d",
842 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
843 if (!IPCSkeleton::SetCallingIdentity(token)) {
844 ZLOGE(LABEL, "Set Calling Identity fail");
845 return 0;
846 }
847
848 ZLOGE(LABEL, "calling after set uid = %{public}d, pid = %{public}d",
849 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
850 return 0;
851 }
852
ServerNestingSend(MessageParcel & data,MessageParcel & reply)853 int32_t TestServiceStub::ServerNestingSend(MessageParcel &data, MessageParcel &reply)
854 {
855 sptr<IRemoteObject> object = data.ReadRemoteObject();
856 sptr<IFoo> foo = iface_cast<IFoo>(object);
857 int innerResult = foo->TestNestingSend(data.ReadInt32());
858 reply.WriteInt32(innerResult);
859 return 0;
860 }
861
ServerAccessTokenId(MessageParcel & data,MessageParcel & reply)862 int32_t TestServiceStub::ServerAccessTokenId(MessageParcel &data, MessageParcel &reply)
863 {
864 int32_t token = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
865 int32_t ftoken = static_cast<int32_t>(IPCSkeleton::GetFirstTokenID());
866 ZLOGE(LABEL, "server GetCallingTokenID:%{public}d", token);
867 ZLOGE(LABEL, "server GetFirstTokenID:%{public}d", ftoken);
868 reply.WriteInt32(token);
869 reply.WriteInt32(ftoken);
870 return 0;
871 }
872
ServerAccessTokenId64(MessageParcel & data,MessageParcel & reply)873 int32_t TestServiceStub::ServerAccessTokenId64(MessageParcel &data, MessageParcel &reply)
874 {
875 uint64_t token = IPCSkeleton::GetCallingFullTokenID();
876 uint64_t ftoken = IPCSkeleton::GetFirstFullTokenID();
877 ZLOGE(LABEL, "server GetCallingFullTokenID:%{public}" PRIu64, token);
878 ZLOGE(LABEL, "server GetFirstFullTokenID:%{public}" PRIu64, ftoken);
879 reply.WriteUint64(token);
880 reply.WriteUint64(ftoken);
881 return 0;
882 }
883
ServerMessageParcelAddped(MessageParcel & data,MessageParcel & reply)884 int32_t TestServiceStub::ServerMessageParcelAddped(MessageParcel &data, MessageParcel &reply)
885 {
886 reply.WriteInt32(data.ReadInt32());
887 reply.WriteInt32(data.ReadInt32());
888 reply.WriteString(data.ReadString());
889 return 0;
890 }
891
ServerMessageParcelAddpedWithObject(MessageParcel & data,MessageParcel & reply)892 int32_t TestServiceStub::ServerMessageParcelAddpedWithObject(MessageParcel &data, MessageParcel &reply)
893 {
894 reply.WriteInt32(data.ReadInt32());
895 reply.WriteInt32(data.ReadInt32());
896 reply.WriteString(data.ReadString());
897 reply.WriteRemoteObject(data.ReadRemoteObject());
898 reply.WriteFileDescriptor(data.ReadFileDescriptor());
899 return 0;
900 }
901
ServerEnableSerialInvokeFlag(MessageParcel & data,MessageParcel & reply)902 int32_t TestServiceStub::ServerEnableSerialInvokeFlag(MessageParcel &data, MessageParcel &reply)
903 {
904 int32_t ret = 0;
905 static int sendCount = 0;
906
907 if (sendCount == 0) {
908 if (serialInvokeFlag_) {
909 std::cout<< "Enable serial invoke flag" << std::endl;
910 } else {
911 std::cout<< "Not enable serial invoke flag" << std::endl;
912 }
913 }
914
915 reply.WriteString(data.ReadString());
916 sendCount++;
917
918 if (sendCount >= MAX_RECURSIVE_SENDS) {
919 return ret;
920 }
921
922 MessageOption option;
923 MessageParcel dataParcel;
924 MessageParcel replyParcel;
925 std::string value = std::to_string(sendCount); // The last time was 4.
926
927 std::cout << "Current thread ID = " << std::this_thread::get_id();
928 std::cout << " Send to server data = " << value << std::endl;
929 dataParcel.WriteString(value);
930 ret = IPCObjectStub::SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
931 std::string result = replyParcel.ReadString();
932
933 std::cout << "Current thread ID = " << std::this_thread::get_id();
934 std::cout << " Get result from server data = " << result << std::endl;
935 return ret;
936 }
937
RegisterRemoteStub(MessageParcel & data,MessageParcel & reply)938 int32_t TestServiceStub::RegisterRemoteStub(MessageParcel &data, MessageParcel &reply)
939 {
940 std::string descriptor = data.ReadString();
941 auto remoteObject = data.ReadRemoteObject();
942 return TestRegisterRemoteStub(descriptor.c_str(), remoteObject);
943 }
944
UnRegisterRemoteStub(MessageParcel & data,MessageParcel & reply)945 int32_t TestServiceStub::UnRegisterRemoteStub(MessageParcel &data, MessageParcel &reply)
946 {
947 std::string descriptor = data.ReadString();
948 return TestUnRegisterRemoteStub(descriptor.c_str());
949 }
950
QueryRemoteProxy(MessageParcel & data,MessageParcel & reply)951 int32_t TestServiceStub::QueryRemoteProxy(MessageParcel &data, MessageParcel &reply)
952 {
953 std::string descriptor = data.ReadString();
954 sptr<IRemoteObject> remoteObject = TestQueryRemoteProxy(descriptor.c_str());
955 return reply.WriteRemoteObject(remoteObject);
956 }
957
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)958 int TestServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
959 {
960 std::map<uint32_t, OHOS::TestServiceStub::TestServiceStubFunc>::iterator it = funcMap_.find(code);
961 if (it != funcMap_.end()) {
962 OHOS::TestServiceStub::TestServiceStubFunc itFunc = it->second;
963 if (itFunc != nullptr) {
964 return (this->*itFunc)(data, reply);
965 }
966 }
967 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
968 }
969
TransferRawData(MessageParcel & data,MessageParcel & reply)970 int32_t TestServiceStub::TransferRawData(MessageParcel &data, MessageParcel &reply)
971 {
972 ZLOGD(LABEL, "enter transfer raw data");
973 int length = data.ReadInt32();
974 if (length <= 1) {
975 ZLOGE(LABEL, "%{public}s: length should > 1, length is %{public}d", __func__, length);
976 if (!reply.WriteInt32(length)) {
977 ZLOGE(LABEL, "fail to write parcel");
978 }
979 return ERR_INVALID_DATA;
980 }
981
982 if (!data.ContainFileDescriptors()) {
983 ZLOGE(LABEL, "sent raw data is less than 32k");
984 }
985
986 const char *buffer = nullptr;
987 if ((buffer = reinterpret_cast<const char *>(data.ReadRawData((size_t)length))) == nullptr) {
988 ZLOGE(LABEL, "%{public}s:read raw data failed, length = %{public}d", __func__, length);
989 if (reply.WriteInt32(0)) {
990 ZLOGE(LABEL, "fail to write parcel");
991 }
992 return ERR_INVALID_DATA;
993 }
994 if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
995 ZLOGE(LABEL, "%{public}s:buffer error, length = %{public}d", __func__, length);
996 if (reply.WriteInt32(0)) {
997 ZLOGE(LABEL, "fail to write parcel");
998 }
999 return ERR_INVALID_DATA;
1000 }
1001 if (data.ReadInt32() != length) {
1002 ZLOGE(LABEL, "%{public}s:read raw data after failed, length = %{public}d", __func__, length);
1003 if (!reply.WriteInt32(0)) {
1004 ZLOGE(LABEL, "fail to write parcel");
1005 }
1006 return ERR_INVALID_DATA;
1007 }
1008 if (!reply.WriteInt32(length)) {
1009 ZLOGE(LABEL, "fail to write parcel");
1010 return ERR_INVALID_DATA;
1011 }
1012 return ERR_NONE;
1013 }
1014
ReplyRawData(MessageParcel & data,MessageParcel & reply)1015 int32_t TestServiceStub::ReplyRawData(MessageParcel &data, MessageParcel &reply)
1016 {
1017 ZLOGD(LABEL, "enter reply raw data");
1018 int length = data.ReadInt32();
1019 if (length <= 1) {
1020 ZLOGE(LABEL, "%{public}s: length should > 1, length is %{public}d", __func__, length);
1021 if (!reply.WriteInt32(length)) {
1022 ZLOGE(LABEL, "fail to write parcel");
1023 }
1024 return ERR_INVALID_DATA;
1025 }
1026
1027 unsigned char *buffer = new (std::nothrow) unsigned char[length];
1028 if (buffer == nullptr) {
1029 ZLOGE(LABEL, "new buffer failed of length = %{public}d", length);
1030 return ERR_INVALID_STATE;
1031 }
1032 buffer[0] = 'a';
1033 buffer[length - 1] = 'z';
1034 if (!reply.WriteInt32(length) ||
1035 !reply.WriteRawData(buffer, (size_t)length) ||
1036 !reply.WriteInt32(length)) {
1037 ZLOGE(LABEL, "fail to write parcel");
1038 delete [] buffer;
1039 return ERR_INVALID_STATE;
1040 }
1041
1042 delete [] buffer;
1043 return ERR_NONE;
1044 }
1045
TransferToNextProcess(MessageParcel & data,MessageParcel & reply)1046 int32_t TestServiceStub::TransferToNextProcess(MessageParcel &data, MessageParcel &reply)
1047 {
1048 int32_t reqData = data.ReadInt32();
1049 int32_t delayTime = data.ReadInt32();
1050 int result;
1051 int ret = TestSyncTransaction(reqData, result, delayTime);
1052 reply.WriteInt32(result);
1053
1054 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1055 if (saMgr == nullptr) {
1056 reply.WriteInt32(ERR_TRANSACTION_FAILED);
1057 return ERR_TRANSACTION_FAILED;
1058 }
1059
1060 sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_EXTRA_TEST_SERVICE);
1061 if (object == nullptr) {
1062 reply.WriteInt32(ERR_TRANSACTION_FAILED);
1063 return ERR_TRANSACTION_FAILED;
1064 }
1065
1066 sptr<ITestService> testService = iface_cast<ITestService>(object);
1067 if (testService == nullptr) {
1068 reply.WriteInt32(ERR_TRANSACTION_FAILED);
1069 return ERR_TRANSACTION_FAILED;
1070 }
1071
1072 ret += testService->TestSyncTransaction(reqData, result, delayTime);
1073 ret += testService->TestAsyncCallbackTrans(reqData, result, delayTime);
1074 if (ret != 0) {
1075 reply.WriteInt32(ERR_TRANSACTION_FAILED);
1076 return ERR_TRANSACTION_FAILED;
1077 }
1078
1079 reply.WriteInt32(ERR_NONE);
1080 return ERR_NONE;
1081 }
1082
ReadAshmem(MessageParcel & data,MessageParcel & reply)1083 int32_t TestServiceStub::ReadAshmem(MessageParcel &data, MessageParcel &reply)
1084 {
1085 int32_t contentSize = data.ReadInt32();
1086 if (contentSize < 0) {
1087 reply.WriteInt32(-1);
1088 return ERR_TRANSACTION_FAILED;
1089 }
1090
1091 sptr<Ashmem> ashmem = data.ReadAshmem();
1092 if (ashmem == nullptr) {
1093 reply.WriteInt32(-1);
1094 return ERR_TRANSACTION_FAILED;
1095 }
1096
1097 int32_t ashmemSize = ashmem->GetAshmemSize();
1098 if (ashmemSize < contentSize || !ashmem->MapReadOnlyAshmem()) {
1099 reply.WriteInt32(-1);
1100 return ERR_TRANSACTION_FAILED;
1101 }
1102
1103 const void *content = ashmem->ReadFromAshmem(contentSize, 0);
1104 if (content == nullptr) {
1105 reply.WriteInt32(-1);
1106 ashmem->UnmapAshmem();
1107 ashmem->CloseAshmem();
1108 return ERR_TRANSACTION_FAILED;
1109 }
1110
1111 auto pt = static_cast<const char *>(content);
1112 std::string str(pt, contentSize);
1113 ashmem->UnmapAshmem();
1114 ashmem->CloseAshmem();
1115
1116 std::string name = "ashmem2";
1117 sptr<Ashmem> ashmem2 = Ashmem::CreateAshmem(name.c_str(), ashmemSize);
1118 if (ashmem2 == nullptr || !ashmem2->MapReadAndWriteAshmem() ||
1119 !ashmem2->WriteToAshmem(str.c_str(), contentSize, 0)) {
1120 reply.WriteInt32(-1);
1121 if (ashmem2 != nullptr) {
1122 ashmem2->UnmapAshmem();
1123 ashmem2->CloseAshmem();
1124 }
1125 return ERR_TRANSACTION_FAILED;
1126 }
1127
1128 reply.WriteInt32(contentSize);
1129 reply.WriteAshmem(ashmem2);
1130
1131 ashmem2->UnmapAshmem();
1132 ashmem2->CloseAshmem();
1133 return ERR_NONE;
1134 }
1135
ServerFlushAsyncCalls(MessageParcel & data,MessageParcel & reply)1136 int32_t TestServiceStub::ServerFlushAsyncCalls(MessageParcel &data, MessageParcel &reply)
1137 {
1138 (void)data.ReadString16();
1139 return ERR_NONE;
1140 }
1141
1142 bool TestDeathRecipient::gotDeathRecipient_ = false;
1143
GotDeathRecipient()1144 bool TestDeathRecipient::GotDeathRecipient()
1145 {
1146 return gotDeathRecipient_;
1147 }
1148
OnRemoteDied(const wptr<IRemoteObject> & remote)1149 void TestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1150 {
1151 gotDeathRecipient_ = true;
1152 ZLOGD(LABEL, "recv death notice");
1153 }
1154
TestDeathRecipient()1155 TestDeathRecipient::TestDeathRecipient()
1156 {
1157 }
1158
~TestDeathRecipient()1159 TestDeathRecipient::~TestDeathRecipient()
1160 {
1161 }
1162 } // namespace OHOS
1163