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 <iostream>
17 #include <cstdio>
18 #include <cstring>
19 #include <ctime>
20 #include <chrono>
21 #include <cinttypes>
22 #include <string>
23 #include <unistd.h>
24 #include <gtest/gtest.h>
25 #include <sys/types.h>
26 #include <securec.h>
27
28 #include "dbinder_service.h"
29 #include "dbinder_service_test_helper.h"
30 #include "dbinder_test_service_skeleton.h"
31 #include "ipc_skeleton.h"
32 #include "ipc_object_proxy.h"
33 #include "ipc_types.h"
34 #include "if_system_ability_manager.h"
35 #include "string_ex.h"
36 #include "iservice_registry.h"
37 #include "system_ability_definition.h"
38 #include "distributed_major.h"
39 #include "log_tags.h"
40 #include "dbinder_test_service.h"
41 #include "dbinder_log.h"
42
43 using namespace OHOS;
44 using namespace testing::ext;
45 using namespace OHOS::DistributeSystemTest;
46 using namespace OHOS::HiviewDFX;
47
48 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DbinderTest" };
49
50 class DbinderTest : public DistributeTest {
51 public:
52 static const int UNIT = 1024;
53 /* The threshold of packet size is 64 * 1024, including header.
54 * The following definitions are used to test legal and illegal packets.
55 */
56 static const int LEGAL_SIZE_S = 3 * 1024;
57 static const int LEGAL_SIZE_M = 16 * 1024;
58 static const int LEGAL_SIZE_L = 63 * 1024;
59 static const int ILLEGAL_SIZE = 2 * 1024 * 1024;
60 static const int REPEAT_TIMES = 1000;
61 static const int REPEAT_RAW_DATA_TIMES = 100;
62 static const int MULTIPLEX_TIMES = 10;
63 static sptr<ISystemAbilityManager> manager_;
64 static char serverId_[DEVICEID_LENGTH + 1];
65 // Test transferring big data with different sizes
66 const int rawData10K = 10 * 1024;
67 const int rawData100K = 100 * 1024;
68 const int rawData1M = 1024 * 1024;
69 const int rawData10M = 10 * 1024 * 1024;
70 const int rawData100M = 100 * 1024 * 1024;
71
72 DbinderTest() = default;
73 ~DbinderTest() = default;
74 static void SetUpTestCase();
75 static void TearDownTestCase();
76 virtual void SetUp();
TearDown()77 virtual void TearDown() {}
78 std::string IpToDeviceId(const std::string &localIp);
79 bool GetRemoteDeviceId();
80 };
81
82 sptr<ISystemAbilityManager> DbinderTest::manager_ = nullptr;
83 char DbinderTest::serverId_[DEVICEID_LENGTH + 1];
84
SetUpTestCase()85 void DbinderTest::SetUpTestCase()
86 {
87 DBINDER_LOGI(LOG_LABEL, "enter SetUpTestCase");
88 StartDBinderServiceTestService();
89 }
90
TearDownTestCase()91 void DbinderTest::TearDownTestCase()
92 {
93 DBINDER_LOGI(LOG_LABEL, "enter TearDownTestCase");
94 StopDBinderServiceTestService();
95 }
96
SetUp()97 void DbinderTest::SetUp()
98 {
99 bool ret = GetRemoteDeviceId();
100 ASSERT_TRUE(ret);
101
102 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
103 ASSERT_TRUE(dBinderService != nullptr);
104
105 manager_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
106 ASSERT_TRUE(manager_ != nullptr);
107 }
108
GetRemoteDeviceId()109 bool DbinderTest::GetRemoteDeviceId()
110 {
111 std::string msg = "Ask Device ID";
112 int ret = SendMessage(AGENT_NO::ONE, msg, strlen(msg.c_str()), [&](const std::string &retId, int retLen) -> bool {
113 if (memcpy_s(serverId_, DEVICEID_LENGTH, retId.c_str(), DEVICEID_LENGTH) != 0 || retLen != DEVICEID_LENGTH) {
114 DBINDER_LOGE(LOG_LABEL, "fail to copy string");
115 return false;
116 }
117 serverId_[DEVICEID_LENGTH] = '\0';
118 return true;
119 });
120
121 return ret > 0;
122 }
123
124 /*
125 * @tc.name: DbinderRemoteCall001
126 * @tc.desc: Verify local client can acquire registered system ability
127 * and invoke remote function on remote server.
128 * @tc.type: FUNC
129 * @tc.require: SR000CS1C1 SR000CS1CA SR000CUFFU I5GORX
130 */
131 HWTEST_F(DbinderTest, DbinderRemoteCall001, TestSize.Level3)
132 {
133 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_001);
134 DBINDER_LOGI(LOG_LABEL, "");
135
136 /*
137 * @tc.steps: step1.Get a proxy (called testService) from remote server.
138 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
139 */
140 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
141 ASSERT_TRUE(object != nullptr);
142
143 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
144 ASSERT_TRUE(testService != nullptr);
145
146 /*
147 * @tc.steps: step2.Use the proxy object to invoke remote function.
148 * @tc.expected: step2.Remote call succeeds and returns 0.
149 */
150 int reply = 0;
151 int result = testService->ReverseInt(2019, reply);
152 EXPECT_EQ(result, 0);
153 EXPECT_EQ(reply, 9102);
154
155 SetCurrentTestCase(DBINDER_TEST_INIT);
156 }
157 /*
158 * @tc.name: DbinderRemoteCall002
159 * @tc.desc: Verify local client cannot acquire unregistered system ability
160 * @tc.type: FUNC
161 * @tc.require: SR000CS1C1 SR000CS1CA SR000CUFFU I5GORX
162 */
163 HWTEST_F(DbinderTest, DbinderRemoteCall002, TestSize.Level3)
164 {
165 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_002);
166 DBINDER_LOGI(LOG_LABEL, "");
167
168 /*
169 * @tc.steps: step1.Get an unregistered System Ability from remote server.
170 * @tc.expected: step1.Failed to get the SA and return nullptr.
171 */
172 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_UNREGISTERED_TEST_SERVICE, serverId_);
173 ASSERT_TRUE(object == nullptr);
174
175 SetCurrentTestCase(DBINDER_TEST_INIT);
176 }
177
178 /*
179 * @tc.name: DbinderRemoteCall003
180 * @tc.desc: Verify the limit to the size of data sent to remote server
181 * @tc.type: FUNC
182 * @tc.require: SR000CS1C1/SR000CS1CA/SR000CUFFU
183 */
184 HWTEST_F(DbinderTest, DbinderRemoteCall003, TestSize.Level3)
185 {
186 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_003);
187 DBINDER_LOGI(LOG_LABEL, "");
188
189 /*
190 * @tc.steps: step1.Get a proxy (called testService) from remote server.
191 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
192 */
193 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
194 ASSERT_TRUE(object != nullptr);
195
196 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
197 ASSERT_TRUE(testService != nullptr);
198
199 /*
200 * @tc.steps: step2.Use the proxy object to invoke remote function with legal and illegal data respectively.
201 * @tc.expected: step2.Succeed in transferring legal data but fail to transfer illegal data.
202 */
203 std::string reply;
204 std::string emptyData;
205 testService->TransOversizedPkt(emptyData, reply);
206 ASSERT_TRUE(emptyData == reply);
207
208 std::string legalDataS(LEGAL_SIZE_S, 'a');
209 int64_t startTime = GetCurrentTime();
210 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
211 testService->TransOversizedPkt(legalDataS, reply);
212 ASSERT_TRUE(legalDataS == reply);
213 }
214 int64_t finishTime = GetCurrentTime();
215 float speed = GetSpeed(finishTime - startTime, LEGAL_SIZE_S, MULTIPLEX_TIMES);
216 printf("Transfer 3k data with speed of %.2fk/s.\n", speed);
217
218 std::string legalDataM(LEGAL_SIZE_M, 'a');
219 startTime = GetCurrentTime();
220 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
221 testService->TransOversizedPkt(legalDataM, reply);
222 ASSERT_TRUE(legalDataM == reply);
223 }
224 finishTime = GetCurrentTime();
225 speed = GetSpeed(finishTime - startTime, LEGAL_SIZE_M, MULTIPLEX_TIMES);
226 printf("Transfer 16k data with speed of %.2fk/s.\n", speed);
227
228 std::string legalDataL(LEGAL_SIZE_L, 'a');
229 startTime = GetCurrentTime();
230 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
231 testService->TransOversizedPkt(legalDataL, reply);
232 ASSERT_TRUE(legalDataL == reply);
233 }
234 finishTime = GetCurrentTime();
235 speed = GetSpeed(finishTime - startTime, LEGAL_SIZE_L, MULTIPLEX_TIMES);
236 printf("Transfer 63k data with speed of %.2fk/s.\n", speed);
237
238 std::string illegalData(ILLEGAL_SIZE, 'a');
239 testService->TransOversizedPkt(illegalData, reply);
240 ASSERT_TRUE(illegalData != reply);
241
242 SetCurrentTestCase(DBINDER_TEST_INIT);
243 }
244
245 /*
246 * @tc.name: DbinderRemoteCall004
247 * @tc.desc: Verify the communication with remote server is stable
248 * @tc.type: PERF
249 * @tc.require: SR000CS1C1/SR000CS1CA/SR000CUFFU
250 */
251 HWTEST_F(DbinderTest, DbinderRemoteCall004, TestSize.Level3)
252 {
253 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_004);
254 DBINDER_LOGI(LOG_LABEL, "");
255
256 /*
257 * @tc.steps: step1.Get a proxy (called testService) from remote server.
258 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
259 */
260 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
261 ASSERT_TRUE(object != nullptr);
262
263 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
264 ASSERT_TRUE(testService != nullptr);
265
266 /*
267 * @tc.steps: step2.Use the proxy object to call remote function repeatedly.
268 * @tc.expected: step2.All remote calls succeed and return 0.
269 */
270 for (int i = 0; i < REPEAT_TIMES; i++) {
271 int reply = 0;
272 int result = testService->ReverseInt(2019, reply);
273 EXPECT_EQ(result, 0);
274 EXPECT_EQ(reply, 9102);
275 }
276
277 SetCurrentTestCase(DBINDER_TEST_INIT);
278 }
279
280 /*
281 * @tc.name: DbinderRemoteCall005
282 * @tc.desc: Test the delay of remote call with remote server
283 * @tc.type: PERF
284 * @tc.require: SR000CS1C1/SR000CS1CA/SR000CUFFU
285 */
286 HWTEST_F(DbinderTest, DbinderRemoteCall005, TestSize.Level3)
287 {
288 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_005);
289 DBINDER_LOGI(LOG_LABEL, "");
290
291 /*
292 * @tc.steps: step1.Get a proxy (called testService) from remote server.
293 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
294 */
295 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
296 ASSERT_TRUE(object != nullptr);
297
298 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
299 ASSERT_TRUE(testService != nullptr);
300
301 /*
302 * @tc.steps: step2.Use the proxy object to call remote function, and calculate the time consumption.
303 * @tc.expected: step2.Remote call succeeds and the time delay is close to 15ms.
304 */
305 int64_t startTime = GetCurrentTime();
306 int reply = 0;
307 int result = testService->ReverseInt(2019, reply);
308 int64_t finishTime = GetCurrentTime();
309 EXPECT_GE(finishTime - startTime, 0L);
310 printf("Remote call costs %" PRId64"ms\n", (finishTime - startTime));
311 EXPECT_EQ(result, 0);
312 EXPECT_EQ(reply, 9102);
313
314 SetCurrentTestCase(DBINDER_TEST_INIT);
315 }
316
317 /*
318 * @tc.name: DbinderRemoteCall006
319 * @tc.desc: Verify the communications with remote device can be multiplexed
320 * @tc.type: FUNC
321 * @tc.require: SR000CS1C1/SR000CS1CA/SR000CUFFU
322 */
323 HWTEST_F(DbinderTest, DbinderRemoteCall006, TestSize.Level3)
324 {
325 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_006);
326 DBINDER_LOGI(LOG_LABEL, "");
327
328 vector<sptr<IRemoteObject>> objects;
329 vector<sptr<IDBinderTestService>> testServices;
330 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
331 /*
332 * @tc.steps: step1.Get a proxy from remote server and stores it.
333 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
334 */
335 objects.push_back(manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_));
336 testServices.push_back(iface_cast<IDBinderTestService>(objects[i]));
337
338 /*
339 * @tc.steps: step2.Use the proxy object to invoke remote function.
340 * @tc.expected: step2.Remote call succeeds and returns 0.
341 */
342 int reply = 0;
343 int result = testServices[i]->ReverseInt(2019, reply);
344 EXPECT_EQ(result, 0);
345 EXPECT_EQ(reply, 9102);
346 }
347
348 SetCurrentTestCase(DBINDER_TEST_INIT);
349 }
350
351 /*
352 * @tc.name: DbinderRemoteCall007
353 * @tc.desc: Verify local client can transfer a local object to remote device.
354 * @tc.type: FUNC
355 * @tc.require: SR000CS1C8/SR000CS1CA/SR000CUFFU
356 */
357 HWTEST_F(DbinderTest, DbinderRemoteCall007, TestSize.Level3)
358 {
359 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_007);
360 DBINDER_LOGI(LOG_LABEL, "");
361
362 /*
363 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
364 * @tc.expected: step1.Get both objects successfully.
365 */
366 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE);
367 ASSERT_TRUE(object != nullptr);
368 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
369 ASSERT_TRUE(remoteObject != nullptr);
370
371 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
372 ASSERT_TRUE(remoteTestService != nullptr);
373
374 /*
375 * @tc.steps: step2.Transfer the binder object to remote device.
376 * @tc.expected: step2.Remote device receives the object and use it to communicate with server.
377 */
378 int reply = 0;
379 int withdrawRes = 0;
380 int result =
381 remoteTestService->TransProxyObject(2019, object, OHOS::DBinderTestServiceProxy::NOT_SAVE, reply, withdrawRes);
382 EXPECT_EQ(result, 0);
383 EXPECT_EQ(reply, 9102);
384 EXPECT_EQ(withdrawRes, 0);
385
386 SetCurrentTestCase(DBINDER_TEST_INIT);
387 }
388
389 /*
390 * @tc.name: DbinderRemoteCall008
391 * @tc.desc: Verify local client can transfer two different local objects to remote device.
392 * @tc.type: FUNC
393 * @tc.require: SR000CS1C8/SR000CS1CA/SR000CUFFU
394 */
395 HWTEST_F(DbinderTest, DbinderRemoteCall008, TestSize.Level3)
396 {
397 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_008);
398 DBINDER_LOGI(LOG_LABEL, "");
399
400 /*
401 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
402 * @tc.expected: step1.Get both objects successfully.
403 */
404 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE);
405 ASSERT_TRUE(object != nullptr);
406 sptr<IRemoteObject> object2 = manager_->GetSystemAbility(RPC_TEST_SERVICE);
407 ASSERT_TRUE(object2 != nullptr);
408 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
409 ASSERT_TRUE(remoteObject != nullptr);
410
411 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
412 ASSERT_TRUE(remoteTestService != nullptr);
413
414 /*
415 * @tc.steps: step2.Transfer two binder objects to remote device.
416 * @tc.expected: step2.Remote device receives the objects and use them to communicate with server.
417 */
418 int reply = 0;
419 int withdrawRes = 0;
420 int result =
421 remoteTestService->TransProxyObject(2019, object, OHOS::DBinderTestServiceProxy::NOT_SAVE, reply, withdrawRes);
422 EXPECT_EQ(result, 0);
423 EXPECT_EQ(reply, 9102);
424 EXPECT_EQ(withdrawRes, 0);
425
426 result =
427 remoteTestService->TransProxyObject(2019, object2, OHOS::DBinderTestServiceProxy::NOT_SAVE, reply, withdrawRes);
428 EXPECT_EQ(result, 0);
429 EXPECT_EQ(reply, 9102);
430 EXPECT_EQ(withdrawRes, 0);
431
432 SetCurrentTestCase(DBINDER_TEST_INIT);
433 }
434
435 /*
436 * @tc.name: DbinderRemoteCall009
437 * @tc.desc: Verify local client is transmitting the same proxy every time to server.
438 * @tc.type: FUNC
439 * @tc.require: SR000CS1C8/SR000CS1CA/SR000CUFFU
440 */
441 HWTEST_F(DbinderTest, DbinderRemoteCall009, TestSize.Level3)
442 {
443 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_009);
444 DBINDER_LOGI(LOG_LABEL, "");
445
446 /*
447 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
448 * @tc.expected: step1.Get both objects successfully.
449 */
450 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE);
451 ASSERT_TRUE(object != nullptr);
452 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
453 ASSERT_TRUE(remoteObject != nullptr);
454
455 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
456 ASSERT_TRUE(remoteTestService != nullptr);
457
458 /*
459 * @tc.steps: step2.Transfer the binder object to remote device twice.
460 * @tc.expected: step2.Remote device receives same object in two transmissions.
461 */
462 int reply = 0;
463 int withdrawRes = 0;
464 int result = remoteTestService->TransProxyObject(2019, object, DBinderTestServiceProxy::SAVE, reply, withdrawRes);
465 EXPECT_EQ(result, 0);
466 EXPECT_EQ(reply, 9102);
467 EXPECT_EQ(withdrawRes, 0);
468
469 result = remoteTestService->TransProxyObject(2019, object, DBinderTestServiceProxy::WITHDRAW, reply, withdrawRes);
470 EXPECT_EQ(result, 0);
471 EXPECT_EQ(reply, 9102);
472 EXPECT_EQ(withdrawRes, 0);
473
474 SetCurrentTestCase(DBINDER_TEST_INIT);
475 }
476
477 /*
478 * @tc.name: DbinderRemoteCall011
479 * @tc.desc: Verify transferring objects between remote devices is stable
480 * @tc.type: FUNC
481 * @tc.require: SR000CS1C8/SR000CS1CA/SR000CUFFU
482 */
483 HWTEST_F(DbinderTest, DbinderRemoteCall011, TestSize.Level3)
484 {
485 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_011);
486 DBINDER_LOGI(LOG_LABEL, "");
487
488 /*
489 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
490 * @tc.expected: step1.Get both objects successfully.
491 */
492 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE);
493 ASSERT_TRUE(object != nullptr);
494 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
495 ASSERT_TRUE(remoteObject != nullptr);
496
497 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
498 ASSERT_TRUE(remoteTestService != nullptr);
499
500 /*
501 * @tc.steps: step2.Transfer the binder object to remote device repeatedly.
502 * @tc.expected: step2.Remote device receives the object and use it to communicate with server.
503 */
504 for (int i = 0; i < REPEAT_TIMES; i++) {
505 int reply = 0;
506 int withdrawRes = 0;
507 int result =
508 remoteTestService->TransProxyObject(2019, object, DBinderTestServiceProxy::NOT_SAVE, reply, withdrawRes);
509 EXPECT_EQ(result, 0);
510 EXPECT_EQ(reply, 9102);
511 }
512
513 SetCurrentTestCase(DBINDER_TEST_INIT);
514 }
515
516 /*
517 * @tc.name: DbinderRemoteCall012
518 * @tc.desc: Test the delay of transferring an object between remote devices
519 * @tc.type: PERF
520 * @tc.require: SR000CS1C8/SR000CS1CA/SR000CUFFU
521 */
522 HWTEST_F(DbinderTest, DbinderRemoteCall012, TestSize.Level3)
523 {
524 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_012);
525 DBINDER_LOGI(LOG_LABEL, "");
526
527 /*
528 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
529 * @tc.expected: step1.Get both objects successfully.
530 */
531 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE);
532 ASSERT_TRUE(object != nullptr);
533 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
534 ASSERT_TRUE(remoteObject != nullptr);
535
536 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
537 ASSERT_TRUE(remoteTestService != nullptr);
538
539 /*
540 * @tc.steps: step2.Use the proxy object to transfer object, and calculate the time consumption.
541 * @tc.expected: step2.Remote call succeeds and the time delay is close to 50ms.
542 */
543 int64_t startTime = GetCurrentTime();
544 int reply = 0;
545 int withdrawRes = 0;
546 int result =
547 remoteTestService->TransProxyObject(2019, object, DBinderTestServiceProxy::NOT_SAVE, reply, withdrawRes);
548 int64_t finishTime = GetCurrentTime();
549 EXPECT_GE(finishTime - startTime, 0L);
550 printf("Transferring an object costs %" PRId64 "ms\n", (finishTime - startTime));
551 EXPECT_EQ(result, 0);
552 EXPECT_EQ(reply, 9102);
553
554 SetCurrentTestCase(DBINDER_TEST_INIT);
555 }
556
557 /*
558 * @tc.name: DbinderRemoteCall014
559 * @tc.desc: Verify adding and removing death recipient successfully
560 * @tc.type: FUNC
561 * @tc.require: SR000CS1C8/SR000CS1CA
562 */
563 HWTEST_F(DbinderTest, DbinderRemoteCall014, TestSize.Level3)
564 {
565 SetCurrentTestCase(DBINDER_TEST_DEATH_RECIPIENT_001);
566 DBINDER_LOGI(LOG_LABEL, "");
567
568 /*
569 * @tc.steps: step1.Get a proxy (called testService) from remote server.
570 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
571 */
572 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
573 ASSERT_TRUE(object != nullptr);
574
575 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
576 ASSERT_TRUE(testService != nullptr);
577
578 /*
579 * @tc.steps: step2.Add death recipient.
580 * @tc.expected: step2.Register death notification successfully.
581 */
582 sptr<IRemoteObject::DeathRecipient> deathRecipient(new DBinderTestDeathRecipient());
583 bool result = object->AddDeathRecipient(deathRecipient);
584 EXPECT_EQ(result, true);
585
586 /*
587 * @tc.steps: step3.Remove death recipient
588 * @tc.expected: step3.Unregister death notification successfully.
589 */
590 result = object->RemoveDeathRecipient(deathRecipient);
591 EXPECT_EQ(result, true);
592
593 /*
594 * @tc.steps: step4.Use the proxy object to invoke remote function.
595 * @tc.expected: step4.Remote call succeeds and returns 0.
596 */
597 int reply = 0;
598 int res = testService->ReverseInt(2019, reply);
599 EXPECT_EQ(res, 0);
600 EXPECT_EQ(reply, 9102);
601
602 SetCurrentTestCase(DBINDER_TEST_INIT);
603 }
604
605 /*
606 * @tc.name: DbinderRemoteCall015
607 * @tc.desc: Verify adding and removing death recipient successfully
608 * @tc.type: FUNC
609 * @tc.require: SR000CS1C8/SR000CS1CA
610 */
611 HWTEST_F(DbinderTest, DbinderRemoteCall015, TestSize.Level3)
612 {
613 SetCurrentTestCase(DBINDER_TEST_DEATH_RECIPIENT_002);
614 DBINDER_LOGI(LOG_LABEL, "");
615
616 /*
617 * @tc.steps: step1.Get a proxy (called testService) from remote server.
618 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
619 */
620 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
621 ASSERT_TRUE(object != nullptr);
622
623 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
624 ASSERT_TRUE(testService != nullptr);
625
626 sptr<IRemoteObject::DeathRecipient> deathRecipient;
627 bool result = true;
628 int reply = 0, res = 0;
629 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
630 /*
631 * @tc.steps: step2.Add death recipient.
632 * @tc.expected: step2.Register death notification successfully.
633 */
634 deathRecipient = new DBinderTestDeathRecipient();
635 result = object->AddDeathRecipient(deathRecipient);
636 EXPECT_EQ(result, true);
637
638 /*
639 * @tc.steps: step3.Remove death recipient
640 * @tc.expected: step3.Unregister death notification successfully.
641 */
642 result = object->RemoveDeathRecipient(deathRecipient);
643 EXPECT_EQ(result, true);
644
645 /*
646 * @tc.steps: step4.Use the proxy object to invoke remote function.
647 * @tc.expected: step4.Remote call succeeds and returns 0.
648 */
649 res = testService->ReverseInt(2019, reply);
650 EXPECT_EQ(res, 0);
651 EXPECT_EQ(reply, 9102);
652 }
653
654 SetCurrentTestCase(DBINDER_TEST_INIT);
655 }
656
657 /*
658 * @tc.name: DbinderRemoteCall016
659 * @tc.desc: Verify adding and removing death recipient successfully
660 * @tc.type: FUNC
661 * @tc.require: SR000CS1C8/SR000CS1CA
662 */
663 HWTEST_F(DbinderTest, DbinderRemoteCall016, TestSize.Level3)
664 {
665 SetCurrentTestCase(DBINDER_TEST_DEATH_RECIPIENT_003);
666 DBINDER_LOGI(LOG_LABEL, "");
667
668 /*
669 * @tc.steps: step1.Get a proxy (called testService) from remote server.
670 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
671 */
672 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
673 ASSERT_TRUE(object != nullptr);
674
675 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
676 ASSERT_TRUE(testService != nullptr);
677
678 /*
679 * @tc.steps: step2.Add two death recipients.
680 * @tc.expected: step2.Register two death notifications successfully.
681 */
682 sptr<IRemoteObject::DeathRecipient> deathRecipient(new DBinderTestDeathRecipient());
683 bool result = object->AddDeathRecipient(deathRecipient);
684 EXPECT_EQ(result, true);
685
686 sptr<IRemoteObject::DeathRecipient> deathRecipientRepeat(new DBinderTestDeathRecipient());
687 result = object->AddDeathRecipient(deathRecipientRepeat);
688 EXPECT_EQ(result, true);
689
690 /*
691 * @tc.steps: step3.Remove two death recipients.
692 * @tc.expected: step3.Return false when unregisterring 1st death notification because there is another one left.
693 * Return true when unregisterring 2nd death notification because all clean.
694 */
695 result = object->RemoveDeathRecipient(deathRecipient);
696 EXPECT_EQ(result, false);
697
698 result = object->RemoveDeathRecipient(deathRecipientRepeat);
699 EXPECT_EQ(result, true);
700
701 /*
702 * @tc.steps: step4.Use the proxy object to invoke remote function.
703 * @tc.expected: step4.Remote call succeeds and returns 0.
704 */
705 int reply = 0;
706 int res = testService->ReverseInt(2019, reply);
707 EXPECT_EQ(res, 0);
708 EXPECT_EQ(reply, 9102);
709
710 SetCurrentTestCase(DBINDER_TEST_INIT);
711 }
712
713 /*
714 * @tc.name: DbinderRemoteCall017
715 * @tc.desc: Verify receiving death notification when remote device dies
716 * @tc.type: FUNC
717 * @tc.require: SR000CS1C8/SR000CS1CA
718 */
719 HWTEST_F(DbinderTest, DbinderRemoteCall017, TestSize.Level3)
720 {
721 SetCurrentTestCase(DBINDER_TEST_DEATH_RECIPIENT_004);
722 DBINDER_LOGI(LOG_LABEL, "");
723
724 /*
725 * @tc.steps: step1.Get a proxy (called testService) from remote server.
726 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
727 */
728 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
729 ASSERT_TRUE(object != nullptr);
730
731 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
732 ASSERT_TRUE(testService != nullptr);
733
734 /*
735 * @tc.steps: step2.Add death recipient.
736 * @tc.expected: step2.Register death notification successfully.
737 */
738 sptr<IRemoteObject::DeathRecipient> deathRecipient(new DBinderTestDeathRecipient());
739 bool result = object->AddDeathRecipient(deathRecipient);
740 ASSERT_TRUE(result == true);
741
742 /*
743 * @tc.steps: step3.Stop remote service. Wait 10s, then check death notification.
744 * @tc.expected: step3.Stop it successfully, and receive death notification.
745 */
746 std::string command = "KILL";
747 std::string cmdArgs = "server";
748 std::string expectValue = "0";
749 bool ret = RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue);
750 EXPECT_EQ(ret, true);
751 EXPECT_EQ(GetReturnVal(), 0);
752
753 // wait for killing remote service
754 sleep(10);
755 result = DBinderTestDeathRecipient::GotDeathRecipient();
756 EXPECT_EQ(result, true);
757 DBinderTestDeathRecipient::ClearDeathRecipient();
758 printf("Succ! Recv death notification!\n");
759
760 /*
761 * @tc.steps: step4.Remove death recipient
762 * @tc.expected: step4.Fail to remove death recipient
763 * because when receiving death notification, it remove death recipient automatically.
764 */
765 result = object->RemoveDeathRecipient(deathRecipient);
766 EXPECT_EQ(result, false);
767
768 /*
769 * @tc.steps: step5.Restart remote service and wait 10s.
770 * @tc.expected: step5.Restart it successfully.
771 */
772 std::string restartCommand = "RESTART";
773 ret = RunCmdOnAgent(AGENT_NO::ONE, restartCommand, cmdArgs, expectValue);
774 EXPECT_EQ(ret, true);
775 EXPECT_EQ(GetReturnVal(), 0);
776
777 // wait for restarting server
778 sleep(10);
779
780 /*
781 * @tc.steps: step6.Get a proxy (called testService2) from remote server.
782 * @tc.expected: step6.Get the proxy successfully, and the proxy points to remote stub.
783 */
784 sptr<IRemoteObject> object2 = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
785 ASSERT_TRUE(object2 != nullptr);
786
787 sptr<IDBinderTestService> testService2 = iface_cast<IDBinderTestService>(object2);
788 ASSERT_TRUE(testService2 != nullptr);
789
790 /*
791 * @tc.steps: step7.Use the proxy object to invoke remote function.
792 * @tc.expected: step7.Remote call succeeds and returns 0.
793 */
794 int reply = 0;
795 int res = testService2->ReverseInt(2019, reply);
796 EXPECT_EQ(res, 0);
797 EXPECT_EQ(reply, 9102);
798
799 object = nullptr;
800 testService = nullptr;
801 object2 = nullptr;
802 testService2 = nullptr;
803
804 SetCurrentTestCase(DBINDER_TEST_INIT);
805 }
806
807 /*
808 * @tc.name: DbinderRemoteCall018
809 * @tc.desc: Verify transferring raw data
810 * @tc.type: FUNC
811 * @tc.require: AR000ER7PF
812 */
813 HWTEST_F(DbinderTest, DbinderRemoteCall018, TestSize.Level3)
814 {
815 SetCurrentTestCase(DBINDER_TEST_RAW_DATA_001);
816 DBINDER_LOGI(LOG_LABEL, "");
817
818 /*
819 * @tc.steps: step1.Get a proxy (called testService) from remote server.
820 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
821 */
822 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
823 ASSERT_TRUE(object != nullptr);
824
825 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
826 ASSERT_TRUE(testService != nullptr);
827
828 /*
829 * @tc.steps: step2.Use the proxy object to transfer raw data.
830 * @tc.expected: step2.Remote call succeed and return the size of raw data.
831 */
832 // ProxyTransRawData cannot transfer data less than 2.
833 int result = testService->ProxyTransRawData(rawData100M);
834 EXPECT_EQ(result, 0);
835 result = testService->ProxyTransRawData(rawData100M);
836 EXPECT_EQ(result, 0);
837
838 SetCurrentTestCase(DBINDER_TEST_INIT);
839 }
840
841 /*
842 * @tc.name: DbinderRemoteCall019
843 * @tc.desc: Test the speed of transferring raw data by proxy
844 * @tc.type: PERF
845 * @tc.require: SR000D48A5
846 */
847 HWTEST_F(DbinderTest, DbinderRemoteCall019, TestSize.Level3)
848 {
849 SetCurrentTestCase(DBINDER_TEST_RAW_DATA_002);
850 DBINDER_LOGI(LOG_LABEL, "");
851
852 /*
853 * @tc.steps: step1.Get a proxy (called testService) from remote server.
854 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
855 */
856 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
857 ASSERT_TRUE(object != nullptr);
858
859 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
860 ASSERT_TRUE(testService != nullptr);
861
862 /*
863 * @tc.steps: step2.Use the proxy object to transfer raw data with different size.
864 * @tc.expected: step2.Remote calls succeed and return the size of raw data.
865 */
866 int result;
867 int64_t startTime = GetCurrentTime();
868 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
869 result = testService->ProxyTransRawData(rawData10K);
870 EXPECT_EQ(result, 0);
871 }
872 int64_t finishTime = GetCurrentTime();
873 float speed = GetSpeed(finishTime - startTime, rawData10K, MULTIPLEX_TIMES);
874 printf("Transfer 10K raw data with speed of %.2fk/s.\n", speed);
875
876 startTime = GetCurrentTime();
877 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
878 result = testService->ProxyTransRawData(rawData100K);
879 EXPECT_EQ(result, 0);
880 }
881 finishTime = GetCurrentTime();
882 speed = GetSpeed(finishTime - startTime, rawData100K, MULTIPLEX_TIMES);
883 printf("Transfer 100K raw data with speed of %.2fk/s.\n", speed);
884
885 startTime = GetCurrentTime();
886 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
887 result = testService->ProxyTransRawData(rawData1M);
888 EXPECT_EQ(result, 0);
889 }
890 finishTime = GetCurrentTime();
891 speed = GetSpeed(finishTime - startTime, rawData1M, MULTIPLEX_TIMES);
892 printf("Transfer 1M raw data with speed of %.2fk/s.\n", speed);
893
894 startTime = GetCurrentTime();
895 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
896 result = testService->ProxyTransRawData(rawData10M);
897 EXPECT_EQ(result, 0);
898 }
899 finishTime = GetCurrentTime();
900 speed = GetSpeed(finishTime - startTime, rawData10M, MULTIPLEX_TIMES);
901 printf("Transfer 10M raw data with speed of %.2fk/s.\n", speed);
902
903 SetCurrentTestCase(DBINDER_TEST_INIT);
904 }
905
906 /*
907 * @tc.name: DbinderRemoteCall020
908 * @tc.desc: Verify it is stable to transfer raw data
909 * @tc.type: FUNC
910 * @tc.require: SR000D48A5
911 */
912 HWTEST_F(DbinderTest, DbinderRemoteCall020, TestSize.Level3)
913 {
914 SetCurrentTestCase(DBINDER_TEST_RAW_DATA_003);
915 DBINDER_LOGI(LOG_LABEL, "");
916
917 /*
918 * @tc.steps: step1.Get a proxy (called testService) from remote server.
919 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
920 */
921 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
922 ASSERT_TRUE(object != nullptr);
923
924 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
925 ASSERT_TRUE(testService != nullptr);
926
927 /*
928 * @tc.steps: step2.Use the proxy object to transfer raw data with different size.
929 * @tc.expected: step2.Remote calls succeed and return the size of raw data.
930 */
931 int result;
932 for (int i = 0; i < REPEAT_RAW_DATA_TIMES; i++) {
933 result = testService->ProxyTransRawData(rawData10M);
934 EXPECT_EQ(result, 0);
935 }
936
937 SetCurrentTestCase(DBINDER_TEST_INIT);
938 }
939
940 /*
941 * @tc.name: DbinderRemoteCall021
942 * @tc.desc: Verify reply can carry big raw data
943 * @tc.type: FUNC
944 * @tc.require: AR000DAPPO
945 */
946 HWTEST_F(DbinderTest, DbinderRemoteCall021, TestSize.Level3)
947 {
948 SetCurrentTestCase(DBINDER_TEST_RAW_DATA_004);
949 DBINDER_LOGI(LOG_LABEL, "");
950 /*
951 * @tc.steps: step1.Get a proxy (called testService) from remote server.
952 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
953 */
954 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
955 ASSERT_TRUE(object != nullptr);
956
957 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
958 ASSERT_TRUE(testService != nullptr);
959
960 /*
961 * @tc.steps: step2.Use the proxy object to ask stub to transfer raw data.
962 * @tc.expected: step2.Remote call succeed and return 0.
963 */
964 // StubTransRawData cannot ask data less than 2.
965 int result = testService->StubTransRawData(rawData10M);
966 EXPECT_EQ(result, 0);
967 result = testService->StubTransRawData(rawData100M);
968 EXPECT_EQ(result, 0);
969
970 SetCurrentTestCase(DBINDER_TEST_INIT);
971 }
972
973
974 /*
975 * @tc.name: DbinderRemoteCall022
976 * @tc.desc: Test the speed of transferring raw data by stub
977 * @tc.type: PERF
978 * @tc.require: AR000DAPPO
979 */
980 HWTEST_F(DbinderTest, DbinderRemoteCall022, TestSize.Level3)
981 {
982 SetCurrentTestCase(DBINDER_TEST_RAW_DATA_005);
983 DBINDER_LOGI(LOG_LABEL, "");
984
985 /*
986 * @tc.steps: step1.Get a proxy (called testService) from remote server.
987 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
988 */
989 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
990 ASSERT_TRUE(object != nullptr);
991
992 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
993 ASSERT_TRUE(testService != nullptr);
994
995 /*
996 * @tc.steps: step2.Use the proxy object to transfer raw data with different size.
997 * @tc.expected: step2.Remote calls succeed and return the size of raw data.
998 */
999 int result;
1000 int64_t startTime = GetCurrentTime();
1001 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
1002 result = testService->StubTransRawData(rawData10K);
1003 EXPECT_EQ(result, 0);
1004 }
1005 int64_t finishTime = GetCurrentTime();
1006 float speed = GetSpeed(finishTime - startTime, rawData10K, MULTIPLEX_TIMES);
1007 printf("Receive 10K raw data with speed of %.2fk/s.\n", speed);
1008
1009 startTime = GetCurrentTime();
1010 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
1011 result = testService->StubTransRawData(rawData100K);
1012 EXPECT_EQ(result, 0);
1013 }
1014 finishTime = GetCurrentTime();
1015 speed = GetSpeed(finishTime - startTime, rawData100K, MULTIPLEX_TIMES);
1016 printf("Receive 100K raw data with speed of %.2fk/s.\n", speed);
1017
1018 startTime = GetCurrentTime();
1019 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
1020 result = testService->StubTransRawData(rawData1M);
1021 EXPECT_EQ(result, 0);
1022 }
1023 finishTime = GetCurrentTime();
1024 speed = GetSpeed(finishTime - startTime, rawData1M, MULTIPLEX_TIMES);
1025 printf("Receive 1M raw data with speed of %.2fk/s.\n", speed);
1026
1027 startTime = GetCurrentTime();
1028 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
1029 result = testService->StubTransRawData(rawData10M);
1030 EXPECT_EQ(result, 0);
1031 }
1032 finishTime = GetCurrentTime();
1033 speed = GetSpeed(finishTime - startTime, rawData10M, MULTIPLEX_TIMES);
1034 printf("Receive 10M raw data with speed of %.2fk/s.\n", speed);
1035
1036 SetCurrentTestCase(DBINDER_TEST_INIT);
1037 }
1038
1039 /*
1040 * @tc.name: DbinderRemoteCall023
1041 * @tc.desc: Verify it is stable to transfer raw data by stub
1042 * @tc.type: FUNC
1043 * @tc.require: AR000DAPPO
1044 */
1045 HWTEST_F(DbinderTest, DbinderRemoteCall023, TestSize.Level3)
1046 {
1047 SetCurrentTestCase(DBINDER_TEST_RAW_DATA_006);
1048 DBINDER_LOGI(LOG_LABEL, "");
1049
1050 /*
1051 * @tc.steps: step1.Get a proxy (called testService) from remote server.
1052 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
1053 */
1054 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1055 ASSERT_TRUE(object != nullptr);
1056
1057 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
1058 ASSERT_TRUE(testService != nullptr);
1059
1060 /*
1061 * @tc.steps: step2.Use the proxy object to transfer raw data with different size.
1062 * @tc.expected: step2.Remote calls succeed and return the size of raw data.
1063 */
1064 int result;
1065 for (int i = 0; i < REPEAT_RAW_DATA_TIMES; i++) {
1066 result = testService->StubTransRawData(rawData10M);
1067 EXPECT_EQ(result, 0);
1068 }
1069
1070 SetCurrentTestCase(DBINDER_TEST_INIT);
1071 }
1072
1073 /*
1074 * @tc.name: DbinderRemoteCall024
1075 * @tc.desc: trace test
1076 * @tc.type: FUNC
1077 * @tc.require: SR000CPN5H AR000CPN5I AR000CPN5J
1078 */
1079 HWTEST_F(DbinderTest, DbinderRemoteCall024, TestSize.Level3)
1080 {
1081 DBINDER_LOGI(LOG_LABEL, "");
1082 SetCurrentTestCase(DBINDER_TEST_TRACE_001);
1083 HiTraceId traceId = HiTraceChain::Begin("rpc hitrace", 0);
1084
1085 /*
1086 * @tc.steps: step1.Get a proxy (called testService) from remote server.
1087 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
1088 */
1089 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1090 ASSERT_TRUE(object != nullptr);
1091
1092 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
1093 ASSERT_TRUE(testService != nullptr);
1094
1095 /*
1096 * @tc.steps: step2.Use the proxy object to transfer trace id.
1097 * @tc.expected: step2.Remote calls succeed and trace id equal local trace id.
1098 */
1099 uint64_t childId = 0;
1100 int result = testService->GetChildId(childId);
1101 EXPECT_EQ(result, 0);
1102 EXPECT_EQ(childId, traceId.GetChainId());
1103
1104 SetCurrentTestCase(DBINDER_TEST_INIT);
1105 }
1106
1107 /*
1108 * @tc.name: DbinderRemoteCall025
1109 * @tc.desc: Test trans stub object
1110 * @tc.type: PERF
1111 * @tc.require: SR000CQSAB AR000DAPPM
1112 */
1113 HWTEST_F(DbinderTest, DbinderRemoteCall025, TestSize.Level3)
1114 {
1115 SetCurrentTestCase(DBINDER_TEST_TRANS_STUB_001);
1116 DBINDER_LOGI(LOG_LABEL, "");
1117
1118 /*
1119 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
1120 * @tc.expected: step1.Get both objects successfully.
1121 */
1122 sptr<IRemoteObject> object = new DBinderTestService();
1123 ASSERT_TRUE(object != nullptr);
1124 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1125 ASSERT_TRUE(remoteObject != nullptr);
1126
1127 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
1128 ASSERT_TRUE(remoteTestService != nullptr);
1129
1130 /*
1131 * @tc.steps: step2.Use the proxy object to transfer stub object
1132 * @tc.expected: step2.Remote call succeeds.
1133 */
1134 int reply = 0;
1135 int stubReply = 0;
1136 int result = remoteTestService->TransStubObject(2019, object, reply, stubReply);
1137 EXPECT_EQ(result, 0);
1138 EXPECT_EQ(reply, 9102);
1139 EXPECT_EQ(stubReply, 2019);
1140
1141 SetCurrentTestCase(DBINDER_TEST_INIT);
1142 }
1143
1144 /*
1145 * @tc.name: DbinderRemoteCall026
1146 * @tc.desc: Verify it is stable to transfer raw data by stub
1147 * @tc.type: FUNC
1148 * @tc.require: AR000DHOVU SR000DHOVT
1149 */
1150 HWTEST_F(DbinderTest, DbinderRemoteCall026, TestSize.Level3)
1151 {
1152 SetCurrentTestCase(DBINDER_TEST_FLUSH_COMMAND_001);
1153 DBINDER_LOGI(LOG_LABEL, "");
1154
1155 /*
1156 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
1157 * @tc.expected: step1.Get both objects successfully.
1158 */
1159 sptr<IRemoteObject> object = new DBinderTestService();
1160 ASSERT_TRUE(object != nullptr);
1161 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1162 ASSERT_TRUE(remoteObject != nullptr);
1163
1164 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
1165 ASSERT_TRUE(remoteTestService != nullptr);
1166
1167 /*
1168 * @tc.steps: step2.Use the proxy object to flush commands
1169 * @tc.expected: step2.Remote call succeeds.
1170 */
1171 int result = remoteTestService->FlushAsyncCommands(MULTIPLEX_TIMES, rawData10K);
1172 EXPECT_EQ(result, 0);
1173
1174 SetCurrentTestCase(DBINDER_TEST_INIT);
1175 }
1176
1177 /*
1178 * @tc.name: DbinderRemoteCall027
1179 * @tc.desc: Death notice for anonymous objects
1180 * @tc.type: FUNC
1181 * @tc.require: SR000DP5BC
1182 */
1183 HWTEST_F(DbinderTest, DbinderRemoteCall027, TestSize.Level3)
1184 {
1185 SetCurrentTestCase(DBINDER_TEST_DEATH_RECIPIENT_007);
1186 DBINDER_LOGI(LOG_LABEL, "");
1187
1188 /*
1189 * @tc.steps: step1.Get a proxy (called testService) from remote server.
1190 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
1191 */
1192 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1193 ASSERT_TRUE(object != nullptr);
1194
1195 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
1196 ASSERT_TRUE(testService != nullptr);
1197
1198 /*
1199 * @tc.steps: step2.Get two anonymous objects.
1200 * @tc.expected: step2.Get the proxy successfully, and the proxy points to remote stub.
1201 */
1202 sptr<IRemoteObject> proxy1 = testService->GetRemoteObject(IDBinderTestService::FIRST_OBJECT);
1203 ASSERT_TRUE(proxy1 != nullptr);
1204
1205 sptr<IRemoteObject> proxy2 = testService->GetRemoteObject(IDBinderTestService::SECOND_OBJECT);
1206 ASSERT_TRUE(proxy2 != nullptr);
1207
1208 sptr<IDBinderTestService> remoteTestService1 = iface_cast<IDBinderTestService>(proxy1);
1209 ASSERT_TRUE(remoteTestService1 != nullptr);
1210
1211 sptr<IDBinderTestService> remoteTestService2 = iface_cast<IDBinderTestService>(proxy2);
1212 ASSERT_TRUE(remoteTestService2 != nullptr);
1213
1214 /*
1215 * @tc.steps: step3.Use the proxy object to invoke remote function.
1216 * @tc.expected: step3.Remote call succeeds and returns 0.
1217 */
1218 int reply;
1219 int result = remoteTestService1->ReverseInt(2019, reply);
1220 EXPECT_EQ(result, 0);
1221 EXPECT_EQ(reply, 9102);
1222
1223 result = remoteTestService2->ReverseInt(2019, reply);
1224 EXPECT_EQ(result, 0);
1225 EXPECT_EQ(reply, 9102);
1226
1227 /*
1228 * @tc.steps: step4.Add death recipient.
1229 * @tc.expected: step4.Register death notification successfully.
1230 */
1231 sptr<IRemoteObject::DeathRecipient> deathRecipient1(new DBinderTestDeathRecipient());
1232 bool ret = proxy1->AddDeathRecipient(deathRecipient1);
1233 ASSERT_TRUE(ret == true);
1234
1235 sptr<IRemoteObject::DeathRecipient> deathRecipient2(new DBinderTestDeathRecipient());
1236 ret = proxy2->AddDeathRecipient(deathRecipient2);
1237 ASSERT_TRUE(ret == true);
1238
1239 /*
1240 * @tc.steps: step5.Stop remote service. Wait 10s, then check death notification.
1241 * @tc.expected: step5.Stop it successfully, and receive death notification.
1242 */
1243 std::string command = "KILL";
1244 std::string cmdArgs = "server";
1245 std::string expectValue = "0";
1246 ret = RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue);
1247 EXPECT_EQ(ret, true);
1248 EXPECT_EQ(GetReturnVal(), 0);
1249
1250 // wait for killing remote service
1251 sleep(10);
1252 EXPECT_EQ(DBinderTestDeathRecipient::GotDeathRecipient(), true);
1253 EXPECT_EQ(DBinderTestDeathRecipient::GotDeathRecipient(), true);
1254 DBinderTestDeathRecipient::ClearDeathRecipient();
1255 printf("Succ! Recv death notification!\n");
1256
1257 /*
1258 * @tc.steps: step6.Remove death recipient
1259 * @tc.expected: step6.Fail to remove death recipient
1260 * because when receiving death notification, it remove death recipient automatically.
1261 */
1262 ret = proxy1->RemoveDeathRecipient(deathRecipient1);
1263 EXPECT_EQ(ret, false);
1264
1265 ret = proxy2->RemoveDeathRecipient(deathRecipient2);
1266 EXPECT_EQ(ret, false);
1267
1268 /*
1269 * @tc.steps: step7.Restart remote service and wait 10s.
1270 * @tc.expected: step7.Restart it successfully.
1271 */
1272 std::string restartCommand = "RESTART";
1273 ret = RunCmdOnAgent(AGENT_NO::ONE, restartCommand, cmdArgs, expectValue);
1274 EXPECT_EQ(ret, true);
1275 EXPECT_EQ(GetReturnVal(), 0);
1276
1277 // wait for restarting server
1278 sleep(10);
1279
1280 /*
1281 * @tc.steps: step8.Get a proxy (called testService2) from remote server.
1282 * @tc.expected: step8.Get the proxy successfully, and the proxy points to remote stub.
1283 */
1284 sptr<IRemoteObject> object2 = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1285 ASSERT_TRUE(object2 != nullptr);
1286
1287 sptr<IDBinderTestService> testService2 = iface_cast<IDBinderTestService>(object2);
1288 ASSERT_TRUE(testService2 != nullptr);
1289
1290 /*
1291 * @tc.steps: step9.Use the proxy object to invoke remote function.
1292 * @tc.expected: step9.Remote call succeeds and returns 0.
1293 */
1294 result = testService2->ReverseInt(2019, reply);
1295 EXPECT_EQ(result, 0);
1296 EXPECT_EQ(reply, 9102);
1297
1298 object = nullptr;
1299 testService = nullptr;
1300 object2 = nullptr;
1301 testService2 = nullptr;
1302
1303 SetCurrentTestCase(DBINDER_TEST_INIT);
1304 }
1305
1306 /*
1307 * @tc.name: DbinderRemoteCall028
1308 * @tc.desc: Death notice for anonymous objects
1309 * @tc.type: FUNC
1310 * @tc.require: SR000CS1C8/SR000CS1CA
1311 */
1312 HWTEST_F(DbinderTest, DbinderRemoteCall028, TestSize.Level3)
1313 {
1314 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_015);
1315 DBINDER_LOGI(LOG_LABEL, "");
1316
1317 /*
1318 * @tc.steps: step1.Get a proxy (called testService) from remote server.
1319 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
1320 */
1321 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1322 ASSERT_TRUE(object != nullptr);
1323
1324 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
1325 ASSERT_TRUE(testService != nullptr);
1326
1327 /*
1328 * @tc.steps: step2.Get two anonymous objects.
1329 * @tc.expected: step2.Get the proxy successfully, and the proxy points to remote stub.
1330 */
1331 sptr<IRemoteObject> proxy1 = testService->GetRemoteObject(IDBinderTestService::FIRST_OBJECT);
1332 ASSERT_TRUE(proxy1 != nullptr);
1333
1334 sptr<IRemoteObject> proxy2 = testService->GetRemoteObject(IDBinderTestService::FIRST_OBJECT);
1335 ASSERT_TRUE(proxy2 != nullptr);
1336
1337 sptr<IDBinderTestService> remoteTestService1 = iface_cast<IDBinderTestService>(proxy1);
1338 ASSERT_TRUE(remoteTestService1 != nullptr);
1339
1340 sptr<IDBinderTestService> remoteTestService2 = iface_cast<IDBinderTestService>(proxy2);
1341 ASSERT_TRUE(remoteTestService2 != nullptr);
1342
1343 /*
1344 * @tc.steps: step3.Use the proxy object to invoke remote function.
1345 * @tc.expected: step3.Remote call succeeds and returns 0.
1346 */
1347 int reply;
1348 int result = remoteTestService1->ReverseInt(2019, reply);
1349 EXPECT_EQ(result, 0);
1350 EXPECT_EQ(reply, 9102);
1351
1352 result = remoteTestService2->ReverseInt(2019, reply);
1353 EXPECT_EQ(result, 0);
1354 EXPECT_EQ(reply, 9102);
1355
1356 testService->ClearRemoteDecTimes();
1357
1358 proxy1 = nullptr;
1359 remoteTestService1 = nullptr;
1360 EXPECT_EQ(testService->GetRemoteDecTimes(), 1);
1361
1362 proxy2 = nullptr;
1363 remoteTestService2 = nullptr;
1364 EXPECT_EQ(testService->GetRemoteDecTimes(), 2);
1365
1366 object = nullptr;
1367 testService = nullptr;
1368
1369 SetCurrentTestCase(DBINDER_TEST_INIT);
1370 }
1371
1372 /*
1373 * @tc.name: DbinderRemoteCall029
1374 * @tc.desc: Death notice for anonymous objects
1375 * @tc.type: FUNC
1376 * @tc.require: SR000CS1C8/SR000CS1CA
1377 */
1378 HWTEST_F(DbinderTest, DbinderRemoteCall029, TestSize.Level3)
1379 {
1380 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_016);
1381 DBINDER_LOGI(LOG_LABEL, "");
1382
1383 /*
1384 * @tc.steps: step1.Get a proxy (called testService) from remote server.
1385 * @tc.expected: step1.Get the proxy successfully, and the proxy points to remote stub.
1386 */
1387 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1388 ASSERT_TRUE(object != NULL);
1389
1390 sptr<IDBinderTestService> testService = iface_cast<IDBinderTestService>(object);
1391 ASSERT_TRUE(testService != NULL);
1392
1393 /*
1394 * @tc.steps: step2.Get two anonymous objects.
1395 * @tc.expected: step2.Get the proxy successfully, and the proxy points to remote stub.
1396 */
1397 sptr<IRemoteObject> proxy1 = testService->GetRemoteObject(IDBinderTestService::SECOND_OBJECT);
1398 ASSERT_TRUE(proxy1 != nullptr);
1399
1400 sptr<IRemoteObject> proxy2 = testService->GetRemoteObject(IDBinderTestService::SECOND_OBJECT);
1401 ASSERT_TRUE(proxy2 != nullptr);
1402 ASSERT_TRUE(proxy2 == proxy1);
1403
1404 sptr<IDBinderTestService> remoteTestService1 = iface_cast<IDBinderTestService>(proxy1);
1405 ASSERT_TRUE(remoteTestService1 != nullptr);
1406
1407 sptr<IDBinderTestService> remoteTestService2 = iface_cast<IDBinderTestService>(proxy2);
1408 ASSERT_TRUE(remoteTestService2 != nullptr);
1409
1410 /*
1411 * @tc.steps: step3.Use the proxy object to invoke remote function.
1412 * @tc.expected: step3.Remote call succeeds and returns 0.
1413 */
1414 int reply;
1415 int result = remoteTestService1->ReverseInt(2019, reply);
1416 EXPECT_EQ(result, 0);
1417 EXPECT_EQ(reply, 9102);
1418
1419 result = remoteTestService2->ReverseInt(2019, reply);
1420 EXPECT_EQ(result, 0);
1421 EXPECT_EQ(reply, 9102);
1422
1423 testService->ClearRemoteDecTimes();
1424
1425 proxy1 = nullptr;
1426 proxy2 = nullptr;
1427 remoteTestService1 = nullptr;
1428 remoteTestService2 = nullptr;
1429 sleep(1);
1430 EXPECT_EQ(testService->GetRemoteDecTimes(), 1);
1431
1432 object = nullptr;
1433 testService = nullptr;
1434
1435 SetCurrentTestCase(DBINDER_TEST_INIT);
1436 }
1437
1438 /*
1439 * @tc.name: DbinderRemoteCall030
1440 * @tc.desc: Verify transferring objects between remote devices and tranfer to anthor process again
1441 * @tc.type: FUNC
1442 * @tc.require: SR000FL75G
1443 */
1444 HWTEST_F(DbinderTest, DbinderRemoteCall030, TestSize.Level3)
1445 {
1446 SetCurrentTestCase(DBINDER_TEST_REMOTE_CALL_011);
1447 DBINDER_LOGI(LOG_LABEL, "");
1448
1449 /*
1450 * @tc.steps: step1.Get a local binder object and a proxy pointing to remote stub.
1451 * @tc.expected: step1.Get both objects successfully.
1452 */
1453 sptr<IRemoteObject> object = manager_->GetSystemAbility(RPC_TEST_SERVICE);
1454 ASSERT_TRUE(object != nullptr);
1455 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1456 ASSERT_TRUE(remoteObject != nullptr);
1457
1458 sptr<IDBinderTestService> remoteTestService = iface_cast<IDBinderTestService>(remoteObject);
1459 ASSERT_TRUE(remoteTestService != nullptr);
1460
1461 /*
1462 * @tc.steps: step2.Transfer the binder object to remote device repeatedly.
1463 * @tc.expected: step2.Remote device receives the object and use it to communicate with server.
1464 */
1465 for (int i = 0; i < MULTIPLEX_TIMES; i++) {
1466 int reply = 0;
1467 int withdrawRes = 0;
1468 int result = remoteTestService->TransProxyObjectAgain(2021, object, DBinderTestServiceProxy::NOT_SAVE,
1469 reply, withdrawRes);
1470 EXPECT_EQ(result, 0);
1471 EXPECT_EQ(reply, 1202);
1472 }
1473
1474 SetCurrentTestCase(DBINDER_TEST_INIT);
1475 }
1476
1477 /*
1478 * @tc.name: DbinderRemoteCall031
1479 * @tc.desc: Test two ways of sending stub object, GetSystemAbility and through message parcel,
1480 * and its reference count
1481 * @tc.type: FUNC
1482 * @tc.require: SR000CQSAB AR000DAPPM
1483 */
1484 HWTEST_F(DbinderTest, DbinderRemoteCall031, TestSize.Level3)
1485 {
1486 SetCurrentTestCase(DBINDER_TEST_TRANS_STUB_001);
1487
1488 /*
1489 * @tc.steps: step1.Get a local stub object and a proxy pointing to remote a remote stub.
1490 * @tc.expected: step1.Get both objects successfully.
1491 */
1492 sptr<IRemoteObject> object = new DBinderTestService();
1493 ASSERT_TRUE(object != nullptr);
1494 EXPECT_EQ(object->GetSptrRefCount(), 1);
1495 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1496 ASSERT_TRUE(remoteObject != nullptr);
1497 // refered by one proxy here, another in samgr process of remote device
1498 EXPECT_EQ(remoteObject->GetObjectRefCount(), 2);
1499 sptr<IDBinderTestService> proxy = iface_cast<IDBinderTestService>(remoteObject);
1500 ASSERT_TRUE(proxy != nullptr);
1501 sptr<IRemoteObject> remoteObject2 = manager_->GetSystemAbility(RPC_TEST_SERVICE2, serverId_);
1502 ASSERT_TRUE(remoteObject2 != nullptr);
1503 EXPECT_EQ(remoteObject2->GetObjectRefCount(), 2);
1504 sptr<IDBinderTestService> proxy2 = iface_cast<IDBinderTestService>(remoteObject2);
1505 ASSERT_TRUE(proxy2 != nullptr);
1506
1507 /*
1508 * @tc.steps: step2.Use the proxy object to transfer stub object
1509 * @tc.expected: step2.Remote call succeeds.
1510 */
1511 int result = proxy->TransStubObjectRefCount(object, IDBinderTestService::SAVE);
1512 EXPECT_EQ(result, 0);
1513 EXPECT_EQ(object->GetSptrRefCount(), 2);
1514
1515 result = proxy2->TransStubObjectRefCount(object, IDBinderTestService::SAVE);
1516 EXPECT_EQ(result, 0);
1517 EXPECT_EQ(object->GetSptrRefCount(), 3);
1518
1519 result = proxy->TransStubObjectRefCount(object, IDBinderTestService::WITHDRAW);
1520 EXPECT_EQ(result, 0);
1521 EXPECT_EQ(object->GetSptrRefCount(), 2);
1522
1523 result = proxy2->TransStubObjectRefCount(object, IDBinderTestService::WITHDRAW);
1524 EXPECT_EQ(result, 0);
1525 EXPECT_EQ(object->GetSptrRefCount(), 1);
1526
1527 SetCurrentTestCase(DBINDER_TEST_INIT);
1528 }
1529
1530 /*
1531 * @tc.name: DbinderRemoteCall032
1532 * @tc.desc: Test sending rpc proxy acquired from GetSystemAbility to another process
1533 * in this device, sending ipc proxy acquired from GetSystemAbility to another device,
1534 * and their reference count respectively.
1535 * @tc.type: FUNC
1536 * @tc.require: SR000CQSAB AR000DAPPM
1537 */
1538 HWTEST_F(DbinderTest, DbinderRemoteCall032, TestSize.Level3)
1539 {
1540 SetCurrentTestCase(DBINDER_TEST_TRANS_STUB_001);
1541
1542 /*
1543 * @tc.steps: step1.Get a local proxy and a remote proxy.
1544 * @tc.expected: step1.Get both proxy successfully.
1545 */
1546 sptr<IRemoteObject> remoteObject = manager_->GetSystemAbility(RPC_TEST_SERVICE, serverId_);
1547 ASSERT_TRUE(remoteObject != nullptr);
1548 // refered by one sptr here, another in samgr process of remote device
1549 EXPECT_EQ(remoteObject->GetObjectRefCount(), 2);
1550 sptr<IDBinderTestService> proxy = iface_cast<IDBinderTestService>(remoteObject);
1551 ASSERT_TRUE(proxy != nullptr);
1552 EXPECT_EQ(remoteObject->GetSptrRefCount(), 2);
1553 EXPECT_EQ(remoteObject->GetObjectRefCount(), 2);
1554
1555 sptr<IRemoteObject> remoteObject2 = manager_->GetSystemAbility(RPC_TEST_SERVICE);
1556 ASSERT_TRUE(remoteObject2 != nullptr);
1557 EXPECT_EQ(remoteObject2->GetObjectRefCount(), 1);
1558 sptr<IDBinderTestService> proxy2 = iface_cast<IDBinderTestService>(remoteObject2);
1559 ASSERT_TRUE(proxy2 != nullptr);
1560 EXPECT_EQ(remoteObject2->GetSptrRefCount(), 2);
1561 EXPECT_EQ(remoteObject2->GetObjectRefCount(), 1);
1562
1563 /*
1564 * @tc.steps: step2.Use the local proxy object to transfer remote proxy object
1565 * @tc.expected: step2.Remote call succeeds.
1566 */
1567 int result = proxy2->TransProxyObjectRefCount(remoteObject, IDBinderTestService::SAVE);
1568 EXPECT_EQ(result, 0);
1569 EXPECT_EQ(remoteObject->GetObjectRefCount(), 3);
1570
1571 result = proxy->TransProxyObjectRefCount(remoteObject2, IDBinderTestService::SAVE);
1572 EXPECT_EQ(result, 0);
1573 EXPECT_EQ(remoteObject2->GetObjectRefCount(), 2);
1574
1575 result = proxy2->TransProxyObjectRefCount(remoteObject, IDBinderTestService::WITHDRAW);
1576 EXPECT_EQ(result, 0);
1577 EXPECT_EQ(remoteObject->GetObjectRefCount(), 2);
1578
1579 result = proxy->TransProxyObjectRefCount(remoteObject2, IDBinderTestService::WITHDRAW);
1580 EXPECT_EQ(result, 0);
1581 EXPECT_EQ(remoteObject2->GetObjectRefCount(), 1);
1582
1583 SetCurrentTestCase(DBINDER_TEST_INIT);
1584 }
1585
main(int argc,char * argv[])1586 int main(int argc, char *argv[])
1587 {
1588 g_pDistributetestEnv = new DistributeTestEnvironment("major.desc");
1589 testing::AddGlobalTestEnvironment(g_pDistributetestEnv);
1590 testing::GTEST_FLAG(output) = "xml:./";
1591 testing::InitGoogleTest(&argc, argv);
1592 return RUN_ALL_TESTS();
1593 }
1594