1 /*
2 * Copyright (c) 2024 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_sa_proxy_cache_proxy.h"
17 #include "safwk_log.h"
18
19 namespace OHOS {
GetStringFunc(const std::string & in_str,std::string & ret_str)20 ErrCode TestSaProxyCacheProxy::GetStringFunc(
21 const std::string& in_str,
22 std::string& ret_str)
23 {
24 MessageParcel data;
25 MessageParcel reply;
26 MessageOption option(MessageOption::TF_SYNC);
27
28 if (!data.WriteInterfaceToken(GetDescriptor())) {
29 return ERR_INVALID_VALUE;
30 }
31
32 if (!data.WriteString16(Str8ToStr16(in_str))) {
33 return ERR_INVALID_DATA;
34 }
35
36 bool hitCache = ApiCacheManager::GetInstance().PreSendRequest(GetDescriptor(),
37 COMMAND_GET_STRING_FUNC, data, reply);
38 if (hitCache == true) {
39 ErrCode errCode = reply.ReadInt32();
40 if (FAILED(errCode)) {
41 return errCode;
42 }
43 ret_str = Str16ToStr8(reply.ReadString16());
44 return ERR_OK;
45 }
46
47 sptr<IRemoteObject> remote = Remote();
48 if (remote == nullptr) {
49 return ERR_INVALID_DATA;
50 }
51
52 TestSendRequestTimes++;
53 int32_t result = remote->SendRequest(COMMAND_GET_STRING_FUNC, data, reply, option);
54 if (FAILED(result)) {
55 return result;
56 }
57 ErrCode errCode = reply.ReadInt32();
58 if (FAILED(errCode)) {
59 return errCode;
60 }
61
62 ApiCacheManager::GetInstance().PostSendRequest(GetDescriptor(), COMMAND_GET_STRING_FUNC, data, reply);
63 ret_str = Str16ToStr8(reply.ReadString16());
64 return ERR_OK;
65 }
66
GetDoubleFunc(int32_t number,double & ret_number)67 ErrCode TestSaProxyCacheProxy::GetDoubleFunc(
68 int32_t number,
69 double& ret_number)
70 {
71 MessageParcel data;
72 MessageParcel reply;
73 MessageOption option(MessageOption::TF_SYNC);
74
75 if (!data.WriteInterfaceToken(GetDescriptor())) {
76 return ERR_INVALID_VALUE;
77 }
78
79 if (!data.WriteInt32(number)) {
80 return ERR_INVALID_DATA;
81 }
82
83 HILOGD(TAG, "testsaproxycache");
84 bool hitCache = ApiCacheManager::GetInstance().PreSendRequest(GetDescriptor(),
85 COMMAND_GET_DOUBLE_FUNC, data, reply);
86 if (hitCache == true) {
87 HILOGD(TAG, "hit");
88 ErrCode errCode = reply.ReadInt32();
89 if (FAILED(errCode)) {
90 return errCode;
91 }
92 ret_number = reply.ReadDouble();
93 return ERR_OK;
94 }
95
96 sptr<IRemoteObject> remote = Remote();
97 if (remote == nullptr) {
98 return ERR_INVALID_DATA;
99 }
100
101 TestSendRequestTimes++;
102 int32_t result = remote->SendRequest(COMMAND_GET_DOUBLE_FUNC, data, reply, option);
103 if (FAILED(result)) {
104 HILOGD(TAG, "result err");
105 return result;
106 }
107 ErrCode errCode = reply.ReadInt32();
108 if (FAILED(errCode)) {
109 HILOGD(TAG, "errCode failed");
110 return errCode;
111 }
112
113 ApiCacheManager::GetInstance().PostSendRequest(GetDescriptor(), COMMAND_GET_DOUBLE_FUNC, data, reply);
114 ret_number = reply.ReadDouble();
115 HILOGD(TAG, "ret_number %lf", ret_number);
116 return ERR_OK;
117 }
118
ProxyReadVector(MessageParcel & reply,std::vector<int8_t> & ret_vec)119 ErrCode TestSaProxyCacheProxy::ProxyReadVector(MessageParcel& reply, std::vector<int8_t>& ret_vec)
120 {
121 int32_t ret_vecSize = reply.ReadInt32();
122 if (ret_vecSize > VECTOR_MAX_SIZE) {
123 return ERR_INVALID_DATA;
124 }
125
126 for (int32_t i = 0; i < ret_vecSize; ++i) {
127 int8_t value = (int8_t)reply.ReadInt32();
128 ret_vec.push_back(value);
129 }
130 return ERR_OK;
131 }
132
GetVectorFunc(const std::vector<bool> & in_vec,std::vector<int8_t> & ret_vec)133 ErrCode TestSaProxyCacheProxy::GetVectorFunc(const std::vector<bool>& in_vec, std::vector<int8_t>& ret_vec)
134 {
135 MessageParcel data;
136 MessageParcel reply;
137 MessageOption option(MessageOption::TF_SYNC);
138
139 if (!data.WriteInterfaceToken(GetDescriptor())) {
140 return ERR_INVALID_VALUE;
141 }
142
143 if (in_vec.size() > static_cast<size_t>(VECTOR_MAX_SIZE)) {
144 return ERR_INVALID_DATA;
145 }
146 data.WriteInt32(in_vec.size());
147 for (auto it = in_vec.begin(); it != in_vec.end(); ++it) {
148 if (!data.WriteInt32((*it) ? 1 : 0)) {
149 return ERR_INVALID_DATA;
150 }
151 }
152
153 bool hitCache = ApiCacheManager::GetInstance().PreSendRequest(GetDescriptor(),
154 COMMAND_GET_VECTOR_FUNC, data, reply);
155 if (hitCache == true) {
156 ErrCode errCode = reply.ReadInt32();
157 if (FAILED(errCode)) {
158 return errCode;
159 }
160 return ProxyReadVector(reply, ret_vec);
161 }
162
163 sptr<IRemoteObject> remote = Remote();
164 if (remote == nullptr) {
165 return ERR_INVALID_DATA;
166 }
167
168 TestSendRequestTimes++;
169 int32_t result = remote->SendRequest(COMMAND_GET_VECTOR_FUNC, data, reply, option);
170 if (FAILED(result)) {
171 return result;
172 }
173 ErrCode errCode = reply.ReadInt32();
174 if (FAILED(errCode)) {
175 return errCode;
176 }
177
178 ApiCacheManager::GetInstance().PostSendRequest(GetDescriptor(), COMMAND_GET_VECTOR_FUNC, data, reply);
179 return ProxyReadVector(reply, ret_vec);
180 }
181
TestGetIpcSendRequestTimes()182 uint32_t TestSaProxyCacheProxy::TestGetIpcSendRequestTimes()
183 {
184 return TestSendRequestTimes;
185 }
186
GetSaPid(int32_t & pid)187 ErrCode TestSaProxyCacheProxy::GetSaPid(
188 int32_t& pid)
189 {
190 MessageParcel data;
191 MessageParcel reply;
192 MessageOption option(MessageOption::TF_SYNC);
193
194 if (!data.WriteInterfaceToken(GetDescriptor())) {
195 return ERR_INVALID_VALUE;
196 }
197
198 sptr<IRemoteObject> remote = Remote();
199 if (remote == nullptr) {
200 return ERR_INVALID_DATA;
201 }
202
203 int32_t result = remote->SendRequest(COMMAND_GET_SA_PID, data, reply, option);
204 if (FAILED(result)) {
205 return result;
206 }
207 ErrCode errCode = reply.ReadInt32();
208 if (FAILED(errCode)) {
209 return errCode;
210 }
211
212 pid = reply.ReadInt32();
213 return ERR_OK;
214 }
215
216 } // namespace OHOS
217