• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
19 #include "ipc_debug.h"
20 #include "ipc_skeleton.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "fd_san.h"
25 
26 namespace OHOS {
27 
28 constexpr int PARCEL_MAX_CAPACITY = 200 * 1024;
29 
ConnectService()30 bool TestServiceClient::ConnectService()
31 {
32     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33     if (saMgr == nullptr) {
34         ZLOGE(LABEL, "get registry fail");
35         return false;
36     }
37 
38     sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
39 
40     if (object != nullptr) {
41         ZLOGI(LABEL, "Got test Service object");
42         sptr<IRemoteObject::DeathRecipient> death(new TestDeathRecipient());
43         object->AddDeathRecipient(death.GetRefPtr());
44         testService_ = iface_cast<ITestService>(object);
45     }
46 
47     if (testService_ == nullptr) {
48         ZLOGE(LABEL, "Could not find Test Service!");
49         return false;
50     }
51     return true;
52 }
53 
StartSyncTransaction()54 bool TestServiceClient::StartSyncTransaction()
55 {
56     if (testService_ == nullptr) {
57         ZLOGE(LABEL, "The testService_ object is an empty object");
58         return false;
59     }
60 
61     ZLOGI(LABEL, "StartSyncTransaction");
62     int result = 0;
63     int originalValue = 2019;
64     int reversalValue = 9102;
65     int ret = testService_->TestSyncTransaction(originalValue, result);
66     if (ret != 0) {
67         ZLOGE(LABEL, "TestSyncTransaction function call failed");
68         return false;
69     }
70 
71     if (result != reversalValue) {
72         return false;
73     }
74     return true;
75 }
76 
StartPingService()77 bool TestServiceClient::StartPingService()
78 {
79     if (testService_ == nullptr) {
80         ZLOGE(LABEL, "The testService_ object is an empty object");
81         return false;
82     }
83 
84     ZLOGI(LABEL, "StartPingService");
85     const std::u16string descriptor = ITestService::GetDescriptor();
86     int ret = testService_->TestPingService(descriptor);
87     if (ret != 0) {
88         ZLOGE(LABEL, "TestPingService function call failed");
89         return false;
90     }
91     return true;
92 }
93 
StartGetFooService()94 bool TestServiceClient::StartGetFooService()
95 {
96     if (testService_ == nullptr) {
97         ZLOGE(LABEL, "The testService_ object is an empty object");
98         return false;
99     }
100 
101     ZLOGI(LABEL, "StartGetFooService");
102     sptr<IFoo> foo = testService_->TestGetFooService();
103     if (foo == nullptr) {
104         ZLOGE(LABEL, "TestGetFooService function call failed");
105         return false;
106     }
107     return true;
108 }
109 
StartDumpService()110 bool TestServiceClient::StartDumpService()
111 {
112     if (testService_ == nullptr) {
113         ZLOGE(LABEL, "The testService_ object is an empty object");
114         return false;
115     }
116 
117     ZLOGI(LABEL, "StartDumpService");
118     int ret = testService_->TestDumpService();
119     if (ret != 0) {
120         ZLOGE(LABEL, "TestDumpService function call failed");
121         return false;
122     }
123     return true;
124 }
125 
StartTestFileDescriptor()126 bool TestServiceClient::StartTestFileDescriptor()
127 {
128     if (testService_ == nullptr) {
129         ZLOGE(LABEL, "The testService_ object is an empty object");
130         return false;
131     }
132 
133     ZLOGI(LABEL, "StartTestFileDescriptor");
134     int fd = testService_->TestGetFileDescriptor();
135     if (fd == INVALID_FD) {
136         ZLOGE(LABEL, "TestGetFileDescriptor function call failed");
137         return false;
138     }
139     fdsan_exchange_owner_tag(fd, 0, IPC_FD_TAG);
140     if (write(fd, "client write!\n", strlen("client write!\n")) < 0) {
141         ZLOGE(LABEL, "write fd error");
142         return false;
143     }
144     fdsan_close_with_tag(fd, IPC_FD_TAG);
145     return true;
146 }
147 
StartLoopTest(int maxCount)148 bool TestServiceClient::StartLoopTest(int maxCount)
149 {
150     if (testService_ == nullptr) {
151         ZLOGE(LABEL, "The testService_ object is an empty object");
152         return false;
153     }
154 
155     ZLOGI(LABEL, "StartLoopTest");
156     int count = 0;
157     std::string testString;
158     // start loop test, test times is 10240
159     for (count = 0; count < maxCount; count++) {
160         testString += "0123456789!";
161         int ret = testService_->TestStringTransaction(testString);
162         if (ret > PARCEL_MAX_CAPACITY) {
163             return false;
164         }
165     }
166 
167     return true;
168 }
169 
TestEnableSerialInvokeFlag()170 bool TestServiceClient::TestEnableSerialInvokeFlag()
171 {
172     ZLOGI(LABEL, "TestEnableSerialInvokeFlag");
173     if (testService_ == nullptr) {
174         ZLOGE(LABEL, "The testService_ object is an empty object");
175         return false;
176     }
177     int result = testService_->TestEnableSerialInvokeFlag();
178     if (result != 0) {
179         ZLOGE(LABEL, "TestEnableSerialInvokeFlag function call failed");
180         return false;
181     }
182 
183     return true;
184 }
185 
TestNativeIPCSendRequests(int subCmd)186 bool TestServiceClient::TestNativeIPCSendRequests(int subCmd)
187 {
188     if (testService_ == nullptr) {
189         ZLOGE(LABEL, "The testService_ object is an empty object");
190         return false;
191     }
192 
193     auto remoteProxy = std::make_shared<NativeRemoteProxyTest>(testService_);
194     if (remoteProxy == nullptr) {
195         ZLOGE(LABEL, "Create remote proxy test failed!");
196         return false;
197     }
198 
199     static std::map<int, std::function<int()>> commandMap = {
200         { NATIVE_TEST_CMD_SYNC_ADD,               [remoteProxy]() { return remoteProxy->SyncAdd(); }},
201         { NATIVE_TEST_CMD_ASYNC_ADD,              [remoteProxy]() { return remoteProxy->ASyncAdd(); }},
202         { NATIVE_TEST_CMD_SYNC_ADD_REPEAT,        [remoteProxy]() { return remoteProxy->AddParallel(true); }},
203         { NATIVE_TEST_CMD_ASYNC_ADD_REPEAT,       [remoteProxy]() { return remoteProxy->AddParallel(false); }},
204         { NATIVE_TEST_CMD_SEND_AND_ECHO_BASE,     [remoteProxy]() { return remoteProxy->SendAndEchoBase(); }},
205         { NATIVE_TEST_CMD_SEND_AND_ECHO_SRING,    [remoteProxy]() { return remoteProxy->SendAndEchoString(); }},
206         { NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER,   [remoteProxy]() { return remoteProxy->SendAndEchoBuffer(); }},
207         { NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR,   [remoteProxy]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
208         { NATIVE_TEST_CMD_SEND_ERROR_CODE,        [remoteProxy]() { return remoteProxy->SendErrorCode(); }},
209     };
210     auto it = commandMap.find(subCmd);
211     if (it == commandMap.end()) {
212         ZLOGE(LABEL, "Error sub cmd:%{public}d", subCmd);
213         return false;
214     }
215     if (it->second() != 0) {
216         ZLOGE(LABEL, "Test sub cmd:%{public}d failed!", subCmd);
217         return false;
218     }
219 
220     ZLOGI(LABEL, "Test sub cmd:%{public}d success!", subCmd);
221     return true;
222 }
223 
TestRegisterRemoteStub()224 bool TestServiceClient::TestRegisterRemoteStub()
225 {
226     if (testService_ == nullptr) {
227         ZLOGE(LABEL, "The testService_ object is an empty object");
228         return false;
229     }
230 
231     if (remoteStub_ == nullptr) {
232         remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
233         if (remoteStub_ == nullptr) {
234             ZLOGE(LABEL, "Create remote stub test failed!");
235             return false;
236         }
237     }
238     int ret = remoteStub_->RegisterRemoteStub();
239     if (ret != 0) {
240         ZLOGE(LABEL, "RegisterRemoteStub function call failed");
241         return false;
242     }
243     ZLOGI(LABEL, "RegisterRemoteStub function success");
244     return true;
245 }
246 
TestUnRegisterRemoteStub()247 bool TestServiceClient::TestUnRegisterRemoteStub()
248 {
249     if (testService_ == nullptr) {
250         ZLOGE(LABEL, "The testService_ object is an empty object");
251         return false;
252     }
253 
254     if (remoteStub_ == nullptr) {
255         remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
256         if (remoteStub_ == nullptr) {
257             ZLOGE(LABEL, "create remote stub test failed!");
258             return false;
259         }
260     }
261     int ret = remoteStub_->UnRegisterRemoteStub();
262     if (ret != 0) {
263         ZLOGE(LABEL, "UnRegisterRemoteStub function call failed");
264         return false;
265     }
266     ZLOGI(LABEL, "TestUnRegisterRemoteStub function call success");
267     return true;
268 }
269 
TestSendTooManyRequest()270 bool TestServiceClient::TestSendTooManyRequest()
271 {
272     if (testService_ == nullptr) {
273         ZLOGE(LABEL, "The testService_ object is an empty object");
274         return false;
275     }
276 
277     ZLOGI(LABEL, "TestSendTooManyRequest");
278     int res = 0;
279     int data = 2024;
280     int ret = testService_->TestSendTooManyRequest(data, res);
281     if (ret != 0) {
282         ZLOGE(LABEL, "TestSendTooManyRequest function call failed");
283         return false;
284     }
285     return true;
286 }
287 
TestMultiThreadSendRequest()288 bool TestServiceClient::TestMultiThreadSendRequest()
289 {
290     if (testService_ == nullptr) {
291         ZLOGE(LABEL, "The testService_ object is an empty object");
292         return false;
293     }
294 
295     int res = 0;
296     int value = 2024;
297     int ret = testService_->TestMultiThreadSendRequest(value, res);
298     if (ret != 0) {
299         ZLOGE(LABEL, "TestMultiThreadSendRequest function call failed");
300         return false;
301     }
302     return true;
303 }
304 
TestQueryThreadInvocationState()305 bool TestServiceClient::TestQueryThreadInvocationState()
306 {
307     if (testService_ == nullptr) {
308         ZLOGE(LABEL, "The testService_ object is an empty object");
309         return false;
310     }
311 
312     int ret = testService_->TestQueryThreadInvocationState();
313     if (ret != 0) {
314         ZLOGE(LABEL, "TestQueryThreadInvocationState function call failed");
315         return false;
316     }
317     return true;
318 }
319 } // namespace OHOS
320