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 <iostream>
18 #include <unistd.h>
19 #include <map>
20 #include "ipc_debug.h"
21 #include "ipc_skeleton.h"
22 #include "if_system_ability_manager.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS {
27 
ConnectService()28 int TestServiceClient::ConnectService()
29 {
30     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
31     if (saMgr == nullptr) {
32         ZLOGE(LABEL, "get registry fail");
33         return -1;
34     }
35 
36     sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
37 
38     if (object != nullptr) {
39         ZLOGD(LABEL, "Got test Service object");
40         sptr<IRemoteObject::DeathRecipient> death(new TestDeathRecipient());
41         object->AddDeathRecipient(death.GetRefPtr());
42         testService_ = iface_cast<ITestService>(object);
43     }
44 
45     if (testService_ == nullptr) {
46         ZLOGE(LABEL, "Could not find Test Service!");
47         return -1;
48     }
49     return 0;
50 }
51 
StartSyncTransaction()52 void TestServiceClient::StartSyncTransaction()
53 {
54     if (testService_ != nullptr) {
55         ZLOGD(LABEL, "StartSyncTransaction");
56         [[maybe_unused]] int result = 0;
57         testService_->TestSyncTransaction(2019, result);
58     }
59 }
60 
StartSyncDelayReply()61 void TestServiceClient::StartSyncDelayReply()
62 {
63     if (testService_ != nullptr) {
64         ZLOGD(LABEL, "StartSyncDelayReply");
65         [[maybe_unused]] int result = 0;
66         testService_->TestSyncTransaction(2019, result, 2);
67     }
68 }
69 
StartAsyncTransaction()70 void TestServiceClient::StartAsyncTransaction()
71 {
72     if (testService_ != nullptr) {
73         ZLOGD(LABEL, "StartAsyncTransaction");
74         [[maybe_unused]] int result = 0;
75         testService_->TestAsyncTransaction(2019, result);
76     }
77 }
78 
StartPingService()79 void TestServiceClient::StartPingService()
80 {
81     if (testService_ != nullptr) {
82         ZLOGD(LABEL, "StartPingService");
83         const std::u16string descriptor = ITestService::GetDescriptor();
84         testService_->TestPingService(descriptor);
85     }
86 }
87 
StartGetFooService()88 void TestServiceClient::StartGetFooService()
89 {
90     if (testService_ != nullptr) {
91         ZLOGD(LABEL, "StartGetFooService");
92         sptr<IFoo> foo = testService_->TestGetFooService();
93         if (foo == nullptr) {
94             ZLOGD(LABEL, "Fail to get foo service");
95         }
96     }
97 }
98 
StartDumpService()99 void TestServiceClient::StartDumpService()
100 {
101     if (testService_ != nullptr) {
102         ZLOGD(LABEL, "StartDumpService");
103         testService_->TestDumpService();
104     }
105 }
106 
StartAsyncDumpService()107 void TestServiceClient::StartAsyncDumpService()
108 {
109     if (testService_ != nullptr) {
110         ZLOGD(LABEL, "StartAsyncDumpService");
111         testService_->TestAsyncDumpService();
112     }
113 }
114 
StartTestFileDescriptor()115 void TestServiceClient::StartTestFileDescriptor()
116 {
117     if (testService_ != nullptr) {
118         ZLOGD(LABEL, "StartTestFileDescriptor");
119         int fd = testService_->TestGetFileDescriptor();
120         if (fd != INVALID_FD) {
121             if (write(fd, "client write!\n", strlen("client write!\n")) < 0) {
122                 ZLOGE(LABEL, "write fd error");
123             }
124             close(fd);
125         }
126     }
127 }
128 
StartLoopTest(int maxCount)129 int TestServiceClient::StartLoopTest(int maxCount)
130 {
131     if (testService_ != nullptr) {
132         ZLOGD(LABEL, "StartLoopTest");
133         int count = 0;
134         std::string testString;
135         // start loop test, test times is 1000
136         for (count = 0; count < maxCount; count++) {
137             testString += "0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+{}?/[]<>-='|~";
138             testService_->TestStringTransaction(testString);
139         }
140         return count;
141     }
142     return 0;
143 }
144 
TestEnableSerialInvokeFlag()145 void TestServiceClient::TestEnableSerialInvokeFlag()
146 {
147     ZLOGD(LABEL, "TestEnableSerialInvokeFlag");
148     if (testService_ == nullptr) {
149         ZLOGE(LABEL, "Member variable testService_ Is a null pointer");
150         return;
151     }
152     int result = testService_->TestEnableSerialInvokeFlag();
153     if (result != 0) {
154         std::cout << "TestServiceClient::TestEnableSerialInvokeFlag function call failed" << std::endl;
155         return;
156     }
157     std::cout << "TestServiceClient::TestEnableSerialInvokeFlag function call successful" << std::endl;
158 }
159 
TestNativeIPCSendRequests(int subCmd)160 void TestServiceClient::TestNativeIPCSendRequests(int subCmd)
161 {
162     auto remoteProxy = std::make_shared<NativeRemoteProxyTest>(testService_);
163     if (remoteProxy == nullptr) {
164         ZLOGE(LABEL, "create remote proxy test failed!");
165         return;
166     }
167     static std::map<int, std::function<int()>> commandMap = {
168         { NATIVE_TEST_CMD_SYNC_ADD, [&]() { return remoteProxy->SyncAdd(); }},
169         { NATIVE_TEST_CMD_ASYNC_ADD, [&]() { return remoteProxy->ASyncAdd(); }},
170         { NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(true); }},
171         { NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(false); }},
172         { NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [&]() { return remoteProxy->SendAndEchoBase(); }},
173         { NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [&]() { return remoteProxy->SendAndEchoString(); }},
174         { NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [&]() { return remoteProxy->SendAndEchoBuffer(); }},
175         { NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [&]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
176         { NATIVE_TEST_CMD_SEND_ERROR_CODE, [&]() { return remoteProxy->SendErrorCode(); }},
177     };
178     auto it = commandMap.find(subCmd);
179     if (it != commandMap.end()) {
180         if (it->second() != 0) {
181             ZLOGE(LABEL, "Test sub cmd:%{public}d failed!", subCmd);
182         } else {
183             ZLOGD(LABEL, "Test sub cmd:%{public}d success!", subCmd);
184         }
185     } else {
186         ZLOGD(LABEL, "error sub cmd:%{public}d", subCmd);
187         return;
188     }
189 }
190 
TestRegisterRemoteStub()191 void TestServiceClient::TestRegisterRemoteStub()
192 {
193     if (remoteStub_ == nullptr) {
194         remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
195         if (remoteStub_ == nullptr) {
196             ZLOGE(LABEL, "create remote stub test failed!");
197             return;
198         }
199     }
200     int ret = remoteStub_->RegisterRemoteStub();
201     if (ret != 0) {
202         ZLOGE(LABEL, "function call failed");
203         return;
204     }
205     ZLOGD(LABEL, "function call success");
206 }
207 
TestUnRegisterRemoteStub()208 void TestServiceClient::TestUnRegisterRemoteStub()
209 {
210     if (remoteStub_ == nullptr) {
211         remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
212         if (remoteStub_ == nullptr) {
213             ZLOGE(LABEL, "create remote stub test failed!");
214             return;
215         }
216     }
217     int ret = remoteStub_->UnRegisterRemoteStub();
218     if (ret != 0) {
219         ZLOGE(LABEL, "function call failed");
220         return;
221     }
222     ZLOGD(LABEL, "function call success");
223 }
224 
TestSendTooManyRequest()225 void TestServiceClient::TestSendTooManyRequest()
226 {
227     if (testService_ != nullptr) {
228         ZLOGD(LABEL, "TestSendTooManyRequest");
229         int ret = 0;
230         int data = 2024;
231         testService_->TestSendTooManyRequest(data, ret);
232     }
233 }
234 
TestMultiThreadSendRequest()235 void TestServiceClient::TestMultiThreadSendRequest()
236 {
237     if (testService_ != nullptr) {
238         ZLOGD(LABEL, "TestMultiThreadSendRequest");
239         int ret = 0;
240         int value = 2024;
241         testService_->TestMultiThreadSendRequest(value, ret);
242     }
243 }
244 
245 } // namespace OHOS
246