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_client.h"
17 #include <unistd.h>
18 #include "ipc_debug.h"
19 #include "ipc_skeleton.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
ConnectService()25 int TestServiceClient::ConnectService()
26 {
27 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
28 if (saMgr == nullptr) {
29 ZLOGE(LABEL, "get registry fail");
30 return -1;
31 }
32
33 sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
34
35 if (object != nullptr) {
36 ZLOGE(LABEL, "Got test Service object");
37 sptr<IRemoteObject::DeathRecipient> death(new TestDeathRecipient());
38 object->AddDeathRecipient(death.GetRefPtr());
39 testService_ = iface_cast<ITestService>(object);
40 }
41
42 if (testService_ == nullptr) {
43 ZLOGE(LABEL, "Could not find Test Service!");
44 return -1;
45 }
46
47 return 0;
48 }
49
StartSyncTransaction()50 void TestServiceClient::StartSyncTransaction()
51 {
52 if (testService_ != nullptr) {
53 ZLOGD(LABEL, "StartSyncTransaction");
54 [[maybe_unused]] int result = 0;
55 testService_->TestSyncTransaction(2019, result);
56 }
57 }
58
StartSyncDelayReply()59 void TestServiceClient::StartSyncDelayReply()
60 {
61 if (testService_ != nullptr) {
62 ZLOGD(LABEL, "StartSyncDelayReply");
63 [[maybe_unused]] int result = 0;
64 testService_->TestSyncTransaction(2019, result, 2);
65 }
66 }
67
StartAsyncTransaction()68 void TestServiceClient::StartAsyncTransaction()
69 {
70 if (testService_ != nullptr) {
71 ZLOGD(LABEL, "StartAsyncTransaction");
72 [[maybe_unused]] int result = 0;
73 testService_->TestAsyncTransaction(2019, result);
74 }
75 }
76
StartPingService()77 void TestServiceClient::StartPingService()
78 {
79 if (testService_ != nullptr) {
80 ZLOGD(LABEL, "StartPingService");
81 const std::u16string descriptor = ITestService::GetDescriptor();
82 testService_->TestPingService(descriptor);
83 }
84 }
85
StartGetFooService()86 void TestServiceClient::StartGetFooService()
87 {
88 if (testService_ != nullptr) {
89 ZLOGD(LABEL, "StartGetFooService");
90 sptr<IFoo> foo = testService_->TestGetFooService();
91 if (foo == nullptr) {
92 ZLOGD(LABEL, "Fail to get foo service");
93 }
94 }
95 }
96
StartDumpService()97 void TestServiceClient::StartDumpService()
98 {
99 if (testService_ != nullptr) {
100 ZLOGD(LABEL, "StartDumpService");
101 testService_->TestDumpService();
102 }
103 }
104
StartAsyncDumpService()105 void TestServiceClient::StartAsyncDumpService()
106 {
107 if (testService_ != nullptr) {
108 ZLOGD(LABEL, "StartAsyncDumpService");
109 testService_->TestAsyncDumpService();
110 }
111 }
112
StartTestFileDescriptor()113 void TestServiceClient::StartTestFileDescriptor()
114 {
115 if (testService_ != nullptr) {
116 ZLOGD(LABEL, "StartTestFileDescriptor");
117 int fd = testService_->TestGetFileDescriptor();
118 if (fd != INVALID_FD) {
119 if (write(fd, "client write!\n", strlen("client write!\n")) < 0) {
120 ZLOGE(LABEL, "write fd error");
121 }
122 close(fd);
123 }
124 }
125 }
126
StartLoopTest(int maxCount)127 int TestServiceClient::StartLoopTest(int maxCount)
128 {
129 if (testService_ != nullptr) {
130 ZLOGD(LABEL, "StartLoopTest");
131 int count = 0;
132 std::string testString;
133 // start loop test, test times is 1000
134 for (count = 0; count < maxCount; count++) {
135 testString += "0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+{}?/[]<>-='|~";
136 testService_->TestStringTransaction(testString);
137 }
138 return count;
139 }
140 return 0;
141 }
142 } // namespace OHOS
143