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_proxy.h"
17 #include <cinttypes>
18 #include <fcntl.h>
19 #include <iostream>
20 #include <unistd.h>
21 #include <cstdio>
22 #include <cstdlib>
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 SEDNREQUEST_TIMES = 2000;
42 constexpr int32_t NUMBER_OF_THREADS = 20;
43
TestServiceProxy(const sptr<IRemoteObject> & impl)44 TestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl)
45 : IRemoteProxy<ITestService>(impl)
46 {
47 }
48
TestEnableSerialInvokeFlag()49 int TestServiceProxy::TestEnableSerialInvokeFlag()
50 {
51 MessageOption option;
52 MessageParcel dataParcel;
53 MessageParcel replyParcel;
54 std::string value = "testData";
55
56 ZLOGI(LABEL, "Send to server data = %{public}s", value.c_str());
57 dataParcel.WriteString(value);
58 auto remote = Remote();
59 if (remote == nullptr) {
60 ZLOGI(LABEL, "The obtained proxy is a null pointer");
61 return -1;
62 }
63
64 int ret = remote->SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
65 std::string reply = replyParcel.ReadString();
66 ZLOGI(LABEL, "Get result from server data = %{public}s", reply.c_str());
67 return ret;
68 }
69
TestSyncTransaction(int value,int & reply,int delayTime)70 int TestServiceProxy::TestSyncTransaction(int value, int &reply, int delayTime)
71 {
72 MessageOption option;
73 MessageParcel dataParcel;
74 MessageParcel replyParcel;
75 int ret = -1;
76
77 ZLOGI(LABEL, "Send to server data = %{public}d", value);
78 if (value > 0) {
79 dataParcel.WriteInt32(value);
80 dataParcel.WriteInt32(delayTime);
81 }
82 auto remote = Remote();
83 if (remote == nullptr) {
84 ZLOGI(LABEL, "The obtained proxy is a null pointer");
85 return ret;
86 }
87
88 ret = remote->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
89 if (ret != 0) {
90 return ret;
91 }
92
93 reply = replyParcel.ReadInt32();
94 ZLOGI(LABEL, "Get result from server data = %{public}d", reply);
95 return ret;
96 }
97
TestSendTooManyRequest(int data,int & reply)98 int TestServiceProxy::TestSendTooManyRequest(int data, int &reply)
99 {
100 int ret = 0;
101 int delayTime = 0;
102 MessageOption option;
103 MessageParcel dataParcel;
104 MessageParcel replyParcel[SEDNREQUEST_TIMES];
105
106 ZLOGI(LABEL, "Send to server data = %{public}d", data);
107 if (data > 0) {
108 dataParcel.WriteInt32(data);
109 dataParcel.WriteInt32(delayTime);
110 }
111 for (int32_t i = 0; i < SEDNREQUEST_TIMES; i++) {
112 ret = Remote()->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel[i], option);
113 }
114 reply = replyParcel[0].ReadInt32();
115 ZLOGI(LABEL, "Get result from server data = %{public}d", reply);
116 return ret;
117 }
118
TestMultiThreadSendRequest(int data,int & reply)119 int TestServiceProxy::TestMultiThreadSendRequest(int data, int &reply)
120 {
121 int ret = 0;
122
123 sptr<IRemoteObject> proxyObject = Remote();
124 for (int32_t i = 0; i < NUMBER_OF_THREADS; i++) {
125 // Different threads correspond to different sets of parcels.
126 std::thread t([&proxyObject, &data, &reply] {
127 MessageParcel dataParcel;
128 MessageParcel replyParcel;
129 MessageOption option;
130 if (data > 0) {
131 dataParcel.WriteInt32(data);
132 dataParcel.WriteInt32(0);
133 }
134 int ret = proxyObject->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
135 if (ret != 0) {
136 ZLOGI(LABEL, "SendRequest is failed: %{public}d", ret);
137 return;
138 }
139 reply = replyParcel.ReadInt32();
140 });
141 t.detach();
142 }
143 sleep(1); // Waiting for the thread to finish executing
144
145 return ret;
146 }
147
TestAsyncTransaction(int data,int timeout)148 int TestServiceProxy::TestAsyncTransaction(int data, int timeout)
149 {
150 MessageOption option = { MessageOption::TF_ASYNC };
151 MessageParcel dataParcel;
152 MessageParcel replyParcel;
153 ZLOGI(LABEL, "TestAsyncTransaction is called, data = %{public}d", data);
154 dataParcel.WriteInt32(data);
155 dataParcel.WriteInt32(timeout);
156 dataParcel.WriteBool(false);
157 auto remote = Remote();
158 if (remote == nullptr) {
159 ZLOGE(LABEL, "remote is nullptr");
160 return -1;
161 }
162 return remote->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
163 }
164
TestAsyncCallbackTrans(int data,int & reply,int timeout)165 int TestServiceProxy::TestAsyncCallbackTrans(int data, int &reply, int timeout)
166 {
167 int ret;
168 MessageOption option = { MessageOption::TF_ASYNC };
169 MessageParcel dataParcel;
170 MessageParcel replyParcel;
171 ZLOGI(LABEL, "TestAsyncCallbackTrans is called, data = %{public}d", data);
172 dataParcel.WriteInt32(data);
173 dataParcel.WriteInt32(timeout);
174 dataParcel.WriteBool(true);
175 sptr<FooStub> fooCallback = new FooStub();
176 dataParcel.WriteRemoteObject(fooCallback->AsObject());
177 ret = Remote()->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
178 reply = fooCallback->WaitForAsyncReply(timeout);
179
180 FooStub::CleanDecTimes();
181 fooCallback = nullptr;
182 if (FooStub::GetDecTimes() != 1) {
183 ret = ERR_TRANSACTION_FAILED;
184 }
185 return ret;
186 }
187
TestZtraceTransaction(std::string & send,std::string & reply,int len)188 int TestServiceProxy::TestZtraceTransaction(std::string &send, std::string &reply, int len)
189 {
190 MessageParcel dataParcel;
191 MessageParcel replyParcel;
192 MessageOption option;
193
194 dataParcel.WriteInt32(len);
195 dataParcel.WriteString(send);
196 int ret = Remote()->SendRequest(TRANS_ID_ZTRACE_TRANSACTION, dataParcel, replyParcel, option);
197 if (ret != 0) {
198 return ret;
199 }
200
201 if (!replyParcel.ReadString(reply)) {
202 return -1;
203 }
204
205 return ret;
206 }
207
TestPingService(const std::u16string & serviceName)208 int TestServiceProxy::TestPingService(const std::u16string &serviceName)
209 {
210 int ret;
211 MessageOption option;
212 MessageParcel dataParcel;
213 MessageParcel replyParcel;
214 ZLOGI(LABEL, "PingService");
215 dataParcel.WriteString16(serviceName);
216 ret = Remote()->SendRequest(TRANS_ID_PING_SERVICE, dataParcel, replyParcel, option);
217 int result = (ret == ERR_NONE) ? replyParcel.ReadInt32() : -1;
218 ZLOGI(LABEL, "PingService result = %{public}d", result);
219 return result;
220 }
221
TestGetFooService()222 sptr<IFoo> TestServiceProxy::TestGetFooService()
223 {
224 MessageOption option;
225 MessageParcel dataParcel;
226 MessageParcel replyParcel;
227 Remote()->SendRequest(TRANS_ID_GET_FOO_SERVICE, dataParcel, replyParcel, option);
228 auto rep = replyParcel.ReadRemoteObject();
229 if (rep == nullptr) {
230 ZLOGE(LABEL, "null foo service");
231 return nullptr;
232 }
233 return iface_cast<IFoo>(rep);
234 }
235
TestGetFileDescriptor()236 int TestServiceProxy::TestGetFileDescriptor()
237 {
238 MessageOption option;
239 MessageParcel dataParcel;
240 MessageParcel replyParcel;
241 sptr<IPCFileDescriptor> desc;
242 option.SetFlags(MessageOption::TF_ACCEPT_FDS);
243 Remote()->SendRequest(TRANS_ID_TRANSACT_FILE_DESC, dataParcel, replyParcel, option);
244 int fd = replyParcel.ReadFileDescriptor();
245 return fd;
246 }
247
TestStringTransaction(const std::string & data)248 int TestServiceProxy::TestStringTransaction(const std::string &data)
249 {
250 MessageOption option;
251 MessageParcel dataParcel;
252 MessageParcel 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 int TestServiceProxy::TestDumpService()
260 {
261 ZLOGI(LABEL, "call StartDumpService");
262 int fd = open("/data/dump.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
263 if (fd == INVALID_FD) {
264 ZLOGE(LABEL, "Call to open system function failed.");
265 return -1;
266 }
267 ZLOGI(LABEL, "Start Dump Service");
268 std::vector<std::u16string> args;
269 args.push_back(u"DumpTest");
270 int ret = Remote()->Dump(fd, args);
271 if (ret != 0) {
272 return -1;
273 }
274 close(fd);
275 return ret;
276 }
277
TestRawDataTransaction(int length,int & reply)278 int TestServiceProxy::TestRawDataTransaction(int length, int &reply)
279 {
280 int ret;
281 MessageOption option;
282 MessageParcel dataParcel;
283 MessageParcel replyParcel;
284 ZLOGE(LABEL, "Send to server data length = %{public}d", length);
285 if (length <= 1 || static_cast<unsigned>(length) > dataParcel.GetRawDataCapacity()) {
286 ZLOGE(LABEL, "length should > 1, length is %{public}d", length);
287 return -1;
288 }
289 unsigned char *buffer = new (std::nothrow) unsigned char[length];
290 if (buffer == nullptr) {
291 ZLOGE(LABEL, "New buffer failed of length = %{public}d", length);
292 return -1;
293 }
294 buffer[0] = 'a';
295 buffer[length - 1] = 'z';
296
297 dataParcel.WriteInt32(length);
298 dataParcel.WriteRawData(buffer, length);
299 dataParcel.WriteInt32(length);
300 ret = Remote()->SendRequest(TRANS_ID_RAWDATA_TRANSACTION, dataParcel, replyParcel, option);
301 reply = replyParcel.ReadInt32();
302 ZLOGE(LABEL, "Get result from server data = %{public}d", reply);
303 delete [] buffer;
304 return ret;
305 }
306
TestRawDataReply(int length)307 int TestServiceProxy::TestRawDataReply(int length)
308 {
309 MessageOption option;
310 MessageParcel dataParcel;
311 MessageParcel replyParcel;
312 if (length <= 1 || static_cast<unsigned>(length) > dataParcel.GetRawDataCapacity()) {
313 ZLOGE(LABEL, "Length should > 1, length is %{public}d", length);
314 return ERR_INVALID_STATE;
315 }
316
317 if (!dataParcel.WriteInt32(length)) {
318 ZLOGE(LABEL, "fail to write parcel");
319 return ERR_INVALID_STATE;
320 }
321
322 int ret = Remote()->SendRequest(TRANS_ID_RAWDATA_REPLY, dataParcel, replyParcel, option);
323 if (ret != ERR_NONE) {
324 ZLOGE(LABEL, "ret = %{public}d", ret);
325 return ret;
326 }
327
328 if (replyParcel.ReadInt32() != length) {
329 ZLOGE(LABEL, "reply false data");
330 return ERR_INVALID_DATA;
331 }
332
333 if (!replyParcel.ContainFileDescriptors()) {
334 ZLOGE(LABEL, "replied raw data is less than 32k");
335 }
336
337 const char *buffer = nullptr;
338 if ((buffer = reinterpret_cast<const char *>(replyParcel.ReadRawData((size_t)length))) == nullptr) {
339 ZLOGE(LABEL, "read raw data failed, length = %{public}d", length);
340 return ERR_INVALID_DATA;
341 }
342 if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
343 ZLOGE(LABEL, "Buffer error, length = %{public}d", length);
344 return ERR_INVALID_DATA;
345 }
346
347 if (replyParcel.ReadInt32() != length) {
348 ZLOGE(LABEL, "Read raw data after failed, length = %{public}d", length);
349 return ERR_INVALID_DATA;
350 }
351
352 return ERR_NONE;
353 }
354
TestCallingUidPid()355 int TestServiceProxy::TestCallingUidPid()
356 {
357 MessageOption option;
358 MessageParcel dataParcel;
359 MessageParcel replyParcel;
360 int ret = Remote()->SendRequest(TRANS_ID_CALLING_UID_PID, dataParcel, replyParcel, option);
361 if (ret != ERR_NONE) {
362 ZLOGE(LABEL, "ret = %{public}d", ret);
363 return ret;
364 }
365
366 int uid = replyParcel.ReadInt32();
367 int pid = replyParcel.ReadInt32();
368
369 IPCTestHelper helper;
370 int actualUid = static_cast<int>(helper.GetUid());
371 int actualPid = static_cast<int>(helper.GetPid());
372 ZLOGI(LABEL, "uid = %{public}d, pid = %{public}d, actualUid = %{public}d, actualPid = %{public}d",
373 uid, pid, actualUid, actualPid);
374
375 if (uid == actualUid && pid == actualPid) {
376 return 0;
377 }
378 return -1;
379 }
380
TestRegisterRemoteStub(const char * descriptor,const sptr<IRemoteObject> object)381 int TestServiceProxy::TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object)
382 {
383 MessageOption option;
384 MessageParcel dataParcel;
385 MessageParcel replyParcel;
386 dataParcel.WriteString(descriptor);
387 dataParcel.WriteRemoteObject(object);
388 int ret = Remote()->SendRequest(TRANS_ID_REGISTER_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
389 if (ret != ERR_NONE) {
390 ZLOGE(LABEL, "ret = %{public}d", ret);
391 return ret;
392 }
393 return 0;
394 }
395
TestUnRegisterRemoteStub(const char * descriptor)396 int TestServiceProxy::TestUnRegisterRemoteStub(const char *descriptor)
397 {
398 MessageOption option;
399 MessageParcel dataParcel;
400 MessageParcel replyParcel;
401 dataParcel.WriteString(descriptor);
402 int ret = Remote()->SendRequest(TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
403 if (ret != ERR_NONE) {
404 ZLOGE(LABEL, "ret = %{public}d", ret);
405 return ret;
406 }
407 return 0;
408 }
409
TestQueryRemoteProxy(const char * descriptor)410 sptr<IRemoteObject> TestServiceProxy::TestQueryRemoteProxy(const char *descriptor)
411 {
412 MessageOption option;
413 MessageParcel dataParcel;
414 MessageParcel replyParcel;
415 dataParcel.WriteString(descriptor);
416 int ret = Remote()->SendRequest(TRANS_ID_QUERY_REMOTE_PROXY_OBJECT, dataParcel, replyParcel, option);
417 if (ret != ERR_NONE) {
418 ZLOGE(LABEL, "SendRequest failed ret = %{public}d", ret);
419 return nullptr;
420 }
421 auto readRemoteObject = replyParcel.ReadRemoteObject();
422 return readRemoteObject;
423 }
424
425 constexpr char ACCESS_TOKEN_ID_IOCTL_BASE = 'A';
426
427 enum {
428 GET_TOKEN_ID = 1,
429 SET_TOKEN_ID,
430 GET_FTOKEN_ID,
431 SET_FTOKEN_ID,
432 ACCESS_TOKENID_MAX_NR,
433 };
434
435 #define ACCESS_TOKENID_SET_TOKENID \
436 _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_TOKEN_ID, unsigned long long)
437 #define ACCESS_TOKENID_SET_FTOKENID \
438 _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_FTOKEN_ID, unsigned long long)
439
440 constexpr int ACCESS_TOKEN_OK = 0;
441 constexpr int ACCESS_TOKEN_ERROR = -1;
442
443 constexpr char TOKENID_DEVNODE[] = "/dev/access_token_id";
444
RpcSetSelfTokenID(uint64_t tokenID)445 int RpcSetSelfTokenID(uint64_t tokenID)
446 {
447 int fd = open(TOKENID_DEVNODE, O_RDWR);
448 if (fd < 0) {
449 return ACCESS_TOKEN_ERROR;
450 }
451 int ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, &tokenID);
452 if (ret) {
453 close(fd);
454 return ACCESS_TOKEN_ERROR;
455 }
456
457 close(fd);
458 return ACCESS_TOKEN_OK;
459 }
460
RpcSetFirstCallerTokenID(uint64_t tokenID)461 int RpcSetFirstCallerTokenID(uint64_t tokenID)
462 {
463 int fd = open(TOKENID_DEVNODE, O_RDWR);
464 if (fd < 0) {
465 return ACCESS_TOKEN_ERROR;
466 }
467 int ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, &tokenID);
468 if (ret) {
469 close(fd);
470 return ACCESS_TOKEN_ERROR;
471 }
472
473 close(fd);
474 return ACCESS_TOKEN_OK;
475 }
476
CheckTokenSelf(uint64_t token,uint64_t tokenSelf,uint64_t ftoken,uint64_t ftoken_expected)477 bool TestServiceProxy::CheckTokenSelf(uint64_t token, uint64_t tokenSelf, uint64_t ftoken, uint64_t ftoken_expected)
478 {
479 if (token != tokenSelf) {
480 ZLOGE(LABEL, "token != tokenSelf");
481 return false;
482 }
483 if (ftoken != ftoken_expected) {
484 ZLOGE(LABEL, "ftoken != ftoken_expected");
485 return false;
486 }
487 return true;
488 }
489
CheckSetFirstToken(uint64_t ftoken_expected)490 bool TestServiceProxy::CheckSetFirstToken(uint64_t ftoken_expected)
491 {
492 int ret = RpcSetFirstCallerTokenID(ftoken_expected);
493 if (ret != 0) {
494 ZLOGE(LABEL, "RpcSetFirstCallerTokenID ret = %{public}d", ret);
495 return false;
496 }
497 uint64_t result = RpcGetFirstCallerTokenID();
498 if (result != ftoken_expected) {
499 ZLOGE(LABEL, "TestServiceProxy get ftoken after set: %{public}" PRIu64, result);
500 return false;
501 }
502 return true;
503 }
504
CheckSetSelfToken(uint64_t token_expected)505 bool TestServiceProxy::CheckSetSelfToken(uint64_t token_expected)
506 {
507 int ret = RpcSetSelfTokenID(token_expected);
508 if (ret != 0) {
509 ZLOGE(LABEL, "RpcSetSelfTokenID ret = %{public}d", ret);
510 return false;
511 }
512 uint64_t result = RpcGetSelfTokenID();
513 if (result != token_expected) {
514 ZLOGE(LABEL, "TestServiceProxy get selftoken after set: %{public}" PRIu64, result);
515 return false;
516 }
517 return true;
518 }
519
TestAccessTokenID64(uint64_t token_expected,uint64_t ftoken_expected)520 int TestServiceProxy::TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected)
521 {
522 MessageOption option;
523 MessageParcel dataParcel;
524 MessageParcel replyParcel1;
525 MessageParcel replyParcel2;
526 uint64_t token = IPCSkeleton::GetCallingFullTokenID();
527 uint64_t ftoken = IPCSkeleton::GetFirstFullTokenID();
528 uint64_t tokenSelf = IPCSkeleton::GetSelfTokenID();
529 uint64_t oldTokenSelf = tokenSelf;
530 int32_t ret = 0;
531
532 if (!CheckTokenSelf(token, tokenSelf, ftoken, 0)) {
533 return -1;
534 }
535 if (!CheckSetFirstToken(ftoken_expected)) {
536 ret = -1;
537 goto ERR;
538 }
539 if (!CheckSetSelfToken(token_expected)) {
540 ret = -1;
541 goto ERR;
542 }
543 ret = Remote()->SendRequest(TRANS_ID_ACCESS_TOKENID_64, dataParcel, replyParcel1, option);
544 if (ret != ERR_NONE) {
545 ZLOGE(LABEL, "SendRequest ret = %{public}d", ret);
546 ret = -1;
547 goto ERR;
548 }
549 token = replyParcel1.ReadUint64();
550 ftoken = replyParcel1.ReadUint64();
551
552 if (token != token_expected) {
553 ZLOGE(LABEL, "token != token_expected, token = %{public}" PRIu64, token);
554 ret = -1;
555 goto ERR;
556 }
557 if (ftoken != ftoken_expected) {
558 ZLOGE(LABEL, "ftoken != ftoken_expected, ftoken = %{public}" PRIu64, ftoken);
559 ret = -1;
560 goto ERR;
561 }
562
563 ERR:
564 RpcSetFirstCallerTokenID(0);
565 RpcSetSelfTokenID(oldTokenSelf);
566 return ret;
567 }
568
TestAccessTokenID(int32_t ftoken_expected)569 int TestServiceProxy::TestAccessTokenID(int32_t ftoken_expected)
570 {
571 MessageOption option;
572 MessageParcel dataParcel;
573 MessageParcel replyParcel1;
574 MessageParcel replyParcel2;
575
576 int32_t token = (int32_t)IPCSkeleton::GetCallingTokenID();
577 int32_t ftoken = (int32_t)IPCSkeleton::GetFirstTokenID();
578 int32_t tokenSelf = RpcGetSelfTokenID();
579 ZLOGI(LABEL, "TestServiceProxy tokenSelf: %{public}d", tokenSelf);
580 ZLOGI(LABEL, "TestServiceProxy ftoken: %{public}d", ftoken);
581 ZLOGI(LABEL, "TestServiceProxy ftoken_expected: %{public}d", ftoken_expected);
582 if (!CheckTokenSelf(token, tokenSelf, ftoken, 0)) {
583 ZLOGE(LABEL, "first");
584 return -1;
585 }
586 if (!CheckSetFirstToken(ftoken_expected)) {
587 return -1;
588 }
589 int ret = Remote()->SendRequest(TRANS_ID_ACCESS_TOKENID, dataParcel, replyParcel1, option);
590 if (ret != ERR_NONE) {
591 ZLOGE(LABEL, "SendRequest ret = %{public}d", ret);
592 return ret;
593 }
594 token = replyParcel1.ReadInt32();
595 ftoken = replyParcel1.ReadInt32();
596 if (!CheckTokenSelf(token, tokenSelf, ftoken, ftoken_expected)) {
597 ZLOGE(LABEL, "second");
598 return -1;
599 }
600 if (!CheckSetSelfToken(666)) { // 666: test data
601 return -1;
602 }
603 ret = Remote()->SendRequest(TRANS_ID_ACCESS_TOKENID, dataParcel, replyParcel2, option);
604 if (ret != ERR_NONE) {
605 ZLOGE(LABEL, "ret = %{public}d", ret);
606 return ret;
607 }
608 token = replyParcel2.ReadInt32();
609 ftoken = replyParcel2.ReadInt32();
610 if (!CheckTokenSelf(token, tokenSelf, ftoken, ftoken_expected)) {
611 ZLOGE(LABEL, "third");
612 return -1;
613 }
614 ret = RpcSetFirstCallerTokenID(0);
615 ret = RpcSetSelfTokenID(0);
616 if (ret != ERR_NONE) {
617 return -1;
618 }
619 return 0;
620 }
621
TestMessageParcelAppend(MessageParcel & dst,MessageParcel & src)622 int TestServiceProxy::TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src)
623 {
624 bool res = dst.Append(src);
625 if (!res) {
626 ZLOGE(LABEL, "Message parcel append without ipc failed");
627 return -1;
628 }
629 return 0;
630 }
631
TestMessageParcelAppendWithIpc(MessageParcel & dst,MessageParcel & src,MessageParcel & reply,bool withObject)632 int TestServiceProxy::TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
633 MessageParcel &reply, bool withObject)
634 {
635 bool res = dst.Append(src);
636 if (!res) {
637 ZLOGE(LABEL, "Message parcel append with ipc failed");
638 return -1;
639 }
640 MessageOption option;
641 uint32_t code = TRANS_MESSAGE_PARCEL_ADDPED;
642 if (withObject) {
643 code = TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT;
644 }
645 int ret = Remote()->SendRequest(code, dst, reply, option);
646 ZLOGE(LABEL, "TestMessageParcelAppend with ipc sendrequest ret = %{public}d", ret);
647 return ret;
648 }
649
TestFlushAsyncCalls(int count,int length)650 int TestServiceProxy::TestFlushAsyncCalls(int count, int length)
651 {
652 int ret;
653 MessageOption option = { MessageOption::TF_ASYNC };
654 MessageParcel dataParcel;
655 MessageParcel replyParcel;
656 std::u16string legalData(length, 'a');
657 dataParcel.WriteString16(legalData);
658 for (int i = 0; i < count; i++) {
659 ret = Remote()->SendRequest(TRANS_ID_FLUSH_ASYNC_CALLS, dataParcel, replyParcel, option);
660 if (ret != ERR_NONE) {
661 ZLOGE(LABEL, "Fail to send request when count = %{public}d, ret = %{public}d", count, ret);
662 return ret;
663 }
664 }
665
666 ret = IPCSkeleton::FlushCommands(this->AsObject());
667 return ret;
668 }
669
TestMultipleProcesses(int data,int & rep,int delayTime)670 int TestServiceProxy::TestMultipleProcesses(int data, int &rep, int delayTime)
671 {
672 int ret;
673 MessageOption option;
674 MessageParcel dataParcel;
675 MessageParcel replyParcel;
676 ZLOGI(LABEL, "Send to server data = %{public}d", data);
677 if (data > 0) {
678 dataParcel.WriteInt32(data);
679 dataParcel.WriteInt32(delayTime);
680 }
681 ret = Remote()->SendRequest(TRANS_ID_MULTIPLE_PROCESSES, dataParcel, replyParcel, option);
682 rep = replyParcel.ReadInt32();
683 ret += replyParcel.ReadInt32();
684 return ret;
685 }
686
TestAshmem(sptr<Ashmem> ashmem,int32_t contentSize)687 std::u16string TestServiceProxy::TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize)
688 {
689 if (ashmem == nullptr || contentSize <= 0) {
690 return u"";
691 }
692
693 MessageOption option;
694 MessageParcel dataParcel;
695 MessageParcel replyParcel;
696 if (!dataParcel.WriteInt32(contentSize) || !dataParcel.WriteAshmem(ashmem)) {
697 return u"";
698 }
699
700 int ret = Remote()->SendRequest(TRANS_ID_ASHMEM, dataParcel, replyParcel, option);
701 if (ret != ERR_NONE) {
702 return u"";
703 }
704
705 int32_t readContentSize = replyParcel.ReadInt32();
706 if (readContentSize <= 0) {
707 return u"";
708 }
709
710 sptr<Ashmem> ashmem2 = replyParcel.ReadAshmem();
711 if (ashmem2 == nullptr || !ashmem2->MapReadAndWriteAshmem()) {
712 return u"";
713 }
714
715 const void *content = ashmem2->ReadFromAshmem(readContentSize, 0);
716 if (content == nullptr) {
717 ashmem2->UnmapAshmem();
718 return u"";
719 }
720
721 auto readContent = static_cast<const char *>(content);
722 std::string str(readContent, readContentSize);
723
724 ashmem2->UnmapAshmem();
725 ashmem2->CloseAshmem();
726 return Str8ToStr16(str);
727 }
728
TestNestingSend(int sendCode,int & replyCode)729 int TestServiceProxy::TestNestingSend(int sendCode, int &replyCode)
730 {
731 ZLOGW(LABEL, "Start");
732 MessageOption option;
733 MessageParcel dataParcel;
734 MessageParcel replyParcel;
735 sptr<IFoo> foo = new FooStub();
736 sptr<IRemoteObject> sendFoo = foo->AsObject();
737 sendFoo->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
738 if (!dataParcel.WriteRemoteObject(sendFoo) || !dataParcel.WriteInt32(sendCode)) {
739 ZLOGE(LABEL, "Fail to write data.");
740 return -1;
741 }
742 int ret = Remote()->SendRequest(TRANS_ID_NESTING_SEND, dataParcel, replyParcel, option);
743 replyCode = replyParcel.ReadInt32();
744 ZLOGW(LABEL, "Outer = %{public}d, inner = %{public}d", ret, replyCode);
745 return ret;
746 }
747
748 } // namespace OHOS
749