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.h"
17
18 #include <fcntl.h>
19 #include <iostream>
20 #include <unistd.h>
21
22 #include "ipc_skeleton.h"
23 #include "ipc_debug.h"
24 #include "ipc_payload_statistics.h"
25 #include "string_ex.h"
26 #include "if_system_ability_manager.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 using namespace OHOS::HiviewDFX;
32
Reverse(int x)33 static int Reverse(int x)
34 {
35 int result = 0;
36 int decimal = 10; // decimal value.
37
38 while (x != 0) {
39 result = result * decimal + x % decimal;
40 x = x / decimal;
41 }
42
43 return result;
44 }
45
Instantiate(bool isEnableSerialInvokeFlag)46 int TestService::Instantiate(bool isEnableSerialInvokeFlag)
47 {
48 ZLOGD(LABEL, "%{public}s call in", __func__);
49 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50 if (saMgr == nullptr) {
51 ZLOGE(LABEL, "%{public}s:fail to get Registry", __func__);
52 return -ENODEV;
53 }
54
55 sptr<IRemoteObject> newInstance = new TestService(isEnableSerialInvokeFlag);
56 IPCObjectStub *stub = reinterpret_cast<IPCObjectStub *>(newInstance.GetRefPtr());
57 stub->SetRequestSidFlag(true);
58
59 #ifdef IPCSERVERTESTEXTRA
60 int result = saMgr->AddSystemAbility(IPC_EXTRA_TEST_SERVICE, newInstance);
61 ZLOGD(LABEL, "%{public}s: IPC_EXTRA_TEST_SERVICE result = %{public}d", __func__, result);
62 #else
63 int result = saMgr->AddSystemAbility(IPC_TEST_SERVICE, newInstance);
64 ZLOGD(LABEL, "%{public}s: IPC_TEST_SERVICE result = %{public}d", __func__, result);
65 #endif
66
67 ZLOGD(LABEL, "TestService: strong = %d", newInstance->GetSptrRefCount());
68 return result;
69 }
70
TestService(bool serialInvokeFlag)71 TestService::TestService(bool serialInvokeFlag) : TestServiceStub(serialInvokeFlag), testFd_(INVALID_FD)
72 {
73 }
74
~TestService()75 TestService::~TestService()
76 {
77 if (testFd_ != INVALID_FD) {
78 close(testFd_);
79 }
80 }
81
TestSyncTransaction(int data,int & rep,int delayTime)82 int TestService::TestSyncTransaction(int data, int &rep, int delayTime)
83 {
84 rep = Reverse(data);
85
86 if (delayTime > 0) {
87 sleep((uint32_t)delayTime);
88 }
89
90 std::string sid = IPCSkeleton::GetCallingSid();
91 ZLOGI(LABEL, "TestServiceStub:read from client data = %{public}d, Caller sid = %{public}s", data, sid.c_str());
92
93 return ERR_NONE;
94 }
95
TestAsyncTransaction(int data,int timeout)96 int TestService::TestAsyncTransaction(int data, int timeout)
97 {
98 ZLOGE(LABEL, "TestServiceStub:read from client data = %{public}d", data);
99
100 if (timeout > 0) {
101 sleep((uint32_t)timeout);
102 }
103
104 return Reverse(data);
105 }
106
TestAsyncCallbackTrans(int data,int & reply,int timeout)107 int TestService::TestAsyncCallbackTrans(int data, int &reply, int timeout)
108 {
109 if (timeout > 0) {
110 timeout = 0;
111 }
112
113 return Reverse(data);
114 }
115
TestZtraceTransaction(std::string & send,std::string & receive,int len)116 int TestService::TestZtraceTransaction(std::string &send, std::string &receive, int len)
117 {
118 receive = send;
119 transform(receive.begin(), receive.end(), receive.begin(), ::tolower);
120 return 0;
121 }
122
TestPingService(const std::u16string & serviceName)123 int TestService::TestPingService(const std::u16string &serviceName)
124 {
125 std::u16string localServiceName = GetObjectDescriptor();
126 if (localServiceName.compare(serviceName) != 0) {
127 ZLOGE(LABEL, "local name is ""%s, passing is %s",
128 Str16ToStr8(localServiceName).c_str(), Str16ToStr8(serviceName).c_str());
129 return -1;
130 }
131
132 return 0;
133 }
134
TestGetFooService()135 sptr<IFoo> TestService::TestGetFooService()
136 {
137 return new FooStub();
138 }
139
TestGetFileDescriptor()140 int TestService::TestGetFileDescriptor()
141 {
142 if (testFd_ != INVALID_FD) {
143 close(testFd_);
144 }
145
146 testFd_ = open("/data/test.txt",
147 O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
148
149 if (testFd_ == INVALID_FD) {
150 ZLOGE(LABEL, "%s(%d):open failed.", __func__, __LINE__);
151 return !INVALID_FD;
152 }
153
154 ssize_t writeLen = write(testFd_, "Sever write!\n", strlen("Sever write!\n"));
155 if (writeLen < 0) {
156 ZLOGE(LABEL, "%s(%d): server write file failed.", __func__, __LINE__);
157 close(testFd_);
158 return INVALID_FD;
159 } else {
160 ZLOGD(LABEL, "%s(%d): server write file success.", __func__, __LINE__);
161 }
162
163 return testFd_;
164 }
165
TestStringTransaction(const std::string & data)166 int TestService::TestStringTransaction(const std::string &data)
167 {
168 return data.size();
169 }
170
TestDumpService()171 void TestService::TestDumpService()
172 {
173 // use for proxy only.
174 }
175
TestAsyncDumpService()176 void TestService::TestAsyncDumpService()
177 {
178 // use for proxy only.
179 }
180
TestRawDataTransaction(int length,int & reply)181 int TestService::TestRawDataTransaction(int length, int &reply)
182 {
183 (void)length;
184 (void)reply;
185 return 0;
186 }
187
TestRawDataReply(int length)188 int TestService::TestRawDataReply(int length)
189 {
190 return 0;
191 }
192
TestCallingUidPid()193 int TestService::TestCallingUidPid()
194 {
195 return 0;
196 }
197
TestAccessTokenID(int32_t ftoken_expected)198 int TestService::TestAccessTokenID(int32_t ftoken_expected)
199 {
200 (void)ftoken_expected;
201 return 0;
202 }
203
TestAccessTokenID64(uint64_t token_expected,uint64_t ftoken_expected)204 int TestService::TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected)
205 {
206 (void)token_expected;
207 (void)ftoken_expected;
208 return 0;
209 }
210
TestMessageParcelAppend(MessageParcel & dst,MessageParcel & src)211 int TestService::TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src)
212 {
213 (void)dst;
214 (void)src;
215 return 0;
216 }
217
TestMessageParcelAppendWithIpc(MessageParcel & dst,MessageParcel & src,MessageParcel & reply,bool withObject)218 int TestService::TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
219 MessageParcel &reply, bool withObject)
220 {
221 (void)dst;
222 (void)src;
223 (void)reply;
224 (void)withObject;
225 return 0;
226 }
227
TestFlushAsyncCalls(int count,int length)228 int TestService::TestFlushAsyncCalls(int count, int length)
229 {
230 return 0;
231 }
232
TestMultipleProcesses(int data,int & rep,int delayTime)233 int TestService::TestMultipleProcesses(int data, int &rep, int delayTime)
234 {
235 (void)data;
236 (void)rep;
237 (void)delayTime;
238 return 0;
239 }
240
Dump(int fd,const std::vector<std::u16string> & args)241 int TestService::Dump(int fd, const std::vector<std::u16string> &args)
242 {
243 ssize_t writeCount = 0;
244 if (fd > 0) {
245 std::u16string argsParam = args.front();
246 std::string context;
247 context.append(Str16ToStr8(argsParam));
248 context.append(1, '\r');
249 writeCount = write(fd, context.data(), context.size());
250 }
251
252 if (!IPCPayloadStatistics::GetStatisticsStatus()) {
253 IPCPayloadStatistics::StartStatistics();
254 }
255
256 std::cout << " ---------------------------------------- "<< std::endl;
257 std::cout << " TotalCount = " << IPCPayloadStatistics::GetTotalCount();
258 std::cout << " TotalCost = " << IPCPayloadStatistics::GetTotalCost() << std::endl;
259 std::vector<int32_t> pidVec = IPCPayloadStatistics::GetPids();
260 for (int32_t &val : pidVec) {
261 std::cout << " Pid = " << val << std::endl;
262 std::cout << " PidCount = " << IPCPayloadStatistics::GetCount(val)
263 << " PidCost = " << IPCPayloadStatistics::GetCost(val) << std::endl;
264 std::vector<IPCInterfaceInfo> infoVec = IPCPayloadStatistics::GetDescriptorCodes(val);
265 for (auto &info : infoVec) {
266 std::cout << " desc = " << Str16ToStr8(info.desc) << " code = " << info.code;
267 std::cout << " DescCount = " << IPCPayloadStatistics::GetDescriptorCodeCount(val, info.desc, info.code);
268 IPCPayloadCost payloadCost = IPCPayloadStatistics::GetDescriptorCodeCost(val, info.desc, info.code);
269 std::cout << " DescTotalCost = " << payloadCost.totalCost;
270 std::cout << " DescMaxCost = " << payloadCost.maxCost;
271 std::cout << " DescMinCost = " << payloadCost.minCost;
272 std::cout << " DescAverCost = " << payloadCost.averCost << std::endl;
273 }
274 }
275 std::cout << " ---------------------------------------- "<< std::endl;
276
277 return writeCount > 0 ? ERR_NONE : ERR_TRANSACTION_FAILED;
278 }
279
TestAshmem(sptr<Ashmem> ashmem,int32_t contentSize)280 std::u16string TestService::TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize)
281 {
282 (void)contentSize;
283 return u"";
284 }
285
TestNestingSend(int sendCode,int & replyCode)286 int TestService::TestNestingSend(int sendCode, int &replyCode)
287 {
288 (void)sendCode;
289 return 0;
290 }
291
TestEnableSerialInvokeFlag()292 int TestService::TestEnableSerialInvokeFlag()
293 {
294 return 0;
295 }
296
TestRegisterRemoteStub(const char * descriptor,const sptr<IRemoteObject> object)297 int TestService::TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object)
298 {
299 if (descriptor == nullptr || strlen(descriptor) < 1 || object == nullptr) {
300 return -1;
301 }
302 std::lock_guard<std::mutex> lockGuard(remoteObjectsMutex_);
303 remoteObjects_.emplace(descriptor, object);
304 return 0;
305 }
306
TestUnRegisterRemoteStub(const char * descriptor)307 int TestService::TestUnRegisterRemoteStub(const char *descriptor)
308 {
309 if (descriptor == nullptr || strlen(descriptor) < 1) {
310 return -1;
311 }
312 std::lock_guard<std::mutex> lockGuard(remoteObjectsMutex_);
313 remoteObjects_.erase(descriptor);
314 return 0;
315 }
316
TestQueryRemoteProxy(const char * descriptor)317 sptr<IRemoteObject> TestService::TestQueryRemoteProxy(const char *descriptor)
318 {
319 if (descriptor == nullptr || strlen(descriptor) < 1) {
320 return nullptr;
321 }
322 auto data = remoteObjects_.find(descriptor);
323 if (data != remoteObjects_.end()) {
324 return data->second;
325 }
326 return nullptr;
327 }
328
TestSendTooManyRequest(int data,int & reply)329 int TestService::TestSendTooManyRequest(int data, int &reply)
330 {
331 (void)data;
332 (void)reply;
333 return 0;
334 }
TestMultiThreadSendRequest(int data,int & reply)335 int TestService::TestMultiThreadSendRequest(int data, int &reply)
336 {
337 (void)data;
338 (void)reply;
339 return 0;
340 }
341 } // namespace OHOS
342