• 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 <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 
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     if (write(fd, "client write!\n", strlen("client write!\n")) < 0) {
140         ZLOGE(LABEL, "write fd error");
141         return false;
142     }
143     close(fd);
144     return true;
145 }
146 
StartLoopTest(int maxCount)147 bool TestServiceClient::StartLoopTest(int maxCount)
148 {
149     if (testService_ == nullptr) {
150         ZLOGE(LABEL, "The testService_ object is an empty object");
151         return false;
152     }
153 
154     ZLOGI(LABEL, "StartLoopTest");
155     int count = 0;
156     std::string testString;
157     // start loop test, test times is 10240
158     for (count = 0; count < maxCount; count++) {
159         testString += "0123456789!";
160         int ret = testService_->TestStringTransaction(testString);
161         if (ret > PARCEL_MAX_CAPACITY) {
162             return false;
163         }
164     }
165 
166     return true;
167 }
168 
TestEnableSerialInvokeFlag()169 bool TestServiceClient::TestEnableSerialInvokeFlag()
170 {
171     ZLOGI(LABEL, "TestEnableSerialInvokeFlag");
172     if (testService_ == nullptr) {
173         ZLOGE(LABEL, "The testService_ object is an empty object");
174         return false;
175     }
176     int result = testService_->TestEnableSerialInvokeFlag();
177     if (result != 0) {
178         ZLOGE(LABEL, "TestEnableSerialInvokeFlag function call failed");
179         return false;
180     }
181 
182     return true;
183 }
184 
TestNativeIPCSendRequests(int subCmd)185 bool TestServiceClient::TestNativeIPCSendRequests(int subCmd)
186 {
187     if (testService_ == nullptr) {
188         ZLOGE(LABEL, "The testService_ object is an empty object");
189         return false;
190     }
191 
192     auto remoteProxy = std::make_shared<NativeRemoteProxyTest>(testService_);
193     if (remoteProxy == nullptr) {
194         ZLOGE(LABEL, "Create remote proxy test failed!");
195         return false;
196     }
197 
198     static std::map<int, std::function<int()>> commandMap = {
199         { NATIVE_TEST_CMD_SYNC_ADD,               [remoteProxy]() { return remoteProxy->SyncAdd(); }},
200         { NATIVE_TEST_CMD_ASYNC_ADD,              [remoteProxy]() { return remoteProxy->ASyncAdd(); }},
201         { NATIVE_TEST_CMD_SYNC_ADD_REPEAT,        [remoteProxy]() { return remoteProxy->AddParallel(true); }},
202         { NATIVE_TEST_CMD_ASYNC_ADD_REPEAT,       [remoteProxy]() { return remoteProxy->AddParallel(false); }},
203         { NATIVE_TEST_CMD_SEND_AND_ECHO_BASE,     [remoteProxy]() { return remoteProxy->SendAndEchoBase(); }},
204         { NATIVE_TEST_CMD_SEND_AND_ECHO_SRING,    [remoteProxy]() { return remoteProxy->SendAndEchoString(); }},
205         { NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER,   [remoteProxy]() { return remoteProxy->SendAndEchoBuffer(); }},
206         { NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR,   [remoteProxy]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
207         { NATIVE_TEST_CMD_SEND_ERROR_CODE,        [remoteProxy]() { return remoteProxy->SendErrorCode(); }},
208     };
209     auto it = commandMap.find(subCmd);
210     if (it == commandMap.end()) {
211         ZLOGE(LABEL, "Error sub cmd:%{public}d", subCmd);
212         return false;
213     }
214     if (it->second() != 0) {
215         ZLOGE(LABEL, "Test sub cmd:%{public}d failed!", subCmd);
216         return false;
217     }
218 
219     ZLOGI(LABEL, "Test sub cmd:%{public}d success!", subCmd);
220     return true;
221 }
222 
TestRegisterRemoteStub()223 bool TestServiceClient::TestRegisterRemoteStub()
224 {
225     if (testService_ == nullptr) {
226         ZLOGE(LABEL, "The testService_ object is an empty object");
227         return false;
228     }
229 
230     if (remoteStub_ == nullptr) {
231         remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
232         if (remoteStub_ == nullptr) {
233             ZLOGE(LABEL, "Create remote stub test failed!");
234             return false;
235         }
236     }
237     int ret = remoteStub_->RegisterRemoteStub();
238     if (ret != 0) {
239         ZLOGE(LABEL, "RegisterRemoteStub function call failed");
240         return false;
241     }
242     ZLOGI(LABEL, "RegisterRemoteStub function success");
243     return true;
244 }
245 
TestUnRegisterRemoteStub()246 bool TestServiceClient::TestUnRegisterRemoteStub()
247 {
248     if (testService_ == nullptr) {
249         ZLOGE(LABEL, "The testService_ object is an empty object");
250         return false;
251     }
252 
253     if (remoteStub_ == nullptr) {
254         remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
255         if (remoteStub_ == nullptr) {
256             ZLOGE(LABEL, "create remote stub test failed!");
257             return false;
258         }
259     }
260     int ret = remoteStub_->UnRegisterRemoteStub();
261     if (ret != 0) {
262         ZLOGE(LABEL, "UnRegisterRemoteStub function call failed");
263         return false;
264     }
265     ZLOGI(LABEL, "TestUnRegisterRemoteStub function call success");
266     return true;
267 }
268 
TestSendTooManyRequest()269 bool TestServiceClient::TestSendTooManyRequest()
270 {
271     if (testService_ == nullptr) {
272         ZLOGE(LABEL, "The testService_ object is an empty object");
273         return false;
274     }
275 
276     ZLOGI(LABEL, "TestSendTooManyRequest");
277     int res = 0;
278     int data = 2024;
279     int ret = testService_->TestSendTooManyRequest(data, res);
280     if (ret != 0) {
281         ZLOGE(LABEL, "TestSendTooManyRequest function call failed");
282         return false;
283     }
284     return true;
285 }
286 
TestMultiThreadSendRequest()287 bool TestServiceClient::TestMultiThreadSendRequest()
288 {
289     if (testService_ == nullptr) {
290         ZLOGE(LABEL, "The testService_ object is an empty object");
291         return false;
292     }
293 
294     int res = 0;
295     int value = 2024;
296     int ret = testService_->TestMultiThreadSendRequest(value, res);
297     if (ret != 0) {
298         ZLOGE(LABEL, "TestMultiThreadSendRequest function call failed");
299         return false;
300     }
301     return true;
302 }
303 
304 } // namespace OHOS
305