1 /*
2 * Copyright (c) 2022-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 "enterprise_device_mgr_stub_mock.h"
17
18 #include <fcntl.h>
19 #include "domain_filter_rule.h"
20 #include "firewall_rule.h"
21 #include "http_proxy.h"
22 #include "iptables_utils.h"
23 #include "os_account_info.h"
24 #include "update_policy_utils.h"
25 #include "usb_device_id.h"
26 #include "usb_interface_type.h"
27
28 namespace OHOS {
29 namespace EDM {
30 const uint32_t APPID_MAX_SIZE = 200;
31 const int32_t BASE_CLASS = 3;
32 const int32_t SUB_CLASS = 1;
33 const int32_t PROTOCOL = 2;
34 const std::string TEST_TARGET_PATH = "/data/service/el1/public/edm/test.txt";
InvokeSendRequestGetEnterpriseInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetEnterpriseInfo(uint32_t code, MessageParcel &data,
36 MessageParcel &reply, MessageOption &option)
37 {
38 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetEnterpriseInfo code :" << code;
39 EntInfo entInfo;
40 code_ = code;
41 reply.WriteInt32(ERR_OK);
42 entInfo.Marshalling(reply);
43 return 0;
44 }
45
InvokeSendRequestEnableAdmin(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)46 int EnterpriseDeviceMgrStubMock::InvokeSendRequestEnableAdmin(uint32_t code, MessageParcel &data, MessageParcel &reply,
47 MessageOption &option)
48 {
49 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestEnableAdmin code :" << code;
50 code_ = code;
51 reply.WriteInt32(ERR_OK);
52 std::vector<std::string> writeArray{"com.edm.test.demo"};
53 reply.WriteInt32(writeArray.size());
54 reply.WriteStringVector(writeArray);
55 return 0;
56 }
57
InvokeSendRequestMapEnableAdminTwoSuc(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 int EnterpriseDeviceMgrStubMock::InvokeSendRequestMapEnableAdminTwoSuc(uint32_t code, MessageParcel &data,
59 MessageParcel &reply, MessageOption &option)
60 {
61 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestMapEnableAdminTwoSuc code :" << code;
62 code_ = code;
63 reply.WriteInt32(ERR_OK);
64 std::vector<std::string> writeArray{"com.edm.test.demo"};
65 reply.WriteStringVector(writeArray);
66
67 std::vector<std::string> writeArray2{"set date time policy"};
68 reply.WriteStringVector(writeArray2);
69 return 0;
70 }
71
InvokeSendRequestSetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)72 int EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy(uint32_t code, MessageParcel &data, MessageParcel &reply,
73 MessageOption &option)
74 {
75 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
76 code_ = code;
77 reply.WriteInt32(ERR_OK);
78 return 0;
79 }
80
InvokeSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)81 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy(uint32_t code, MessageParcel &data, MessageParcel &reply,
82 MessageOption &option)
83 {
84 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
85 code_ = code;
86 reply.WriteInt32(ERR_OK);
87 reply.WriteString(RETURN_STRING);
88 return 0;
89 }
90
InvokeSendRequestGetPolicyForWriteFileToStream(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)91 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyForWriteFileToStream(uint32_t code, MessageParcel &data,
92 MessageParcel &reply, MessageOption &option)
93 {
94 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicyForWriteFileToStream :" << code;
95 int32_t fd = open(TEST_TARGET_PATH.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
96 code_ = code;
97 reply.WriteInt32(ERR_OK);
98 reply.WriteFileDescriptor(fd);
99 return 0;
100 }
101
InvokeSendRequestSetPolicyInstallFail(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)102 int EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicyInstallFail(uint32_t code, MessageParcel &data,
103 MessageParcel &reply, MessageOption &option)
104 {
105 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestSetPolicyInstallFail code :" << code;
106 code_ = code;
107 reply.WriteInt32(EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
108 return 0;
109 }
110
InvokeBoolSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)111 int EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy(uint32_t code, MessageParcel &data,
112 MessageParcel &reply, MessageOption &option)
113 {
114 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
115 code_ = code;
116 reply.WriteInt32(ERR_OK);
117 reply.WriteBool(true);
118 return 0;
119 }
120
InvokeBoolSendRequestGetFirewallRule(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)121 int EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetFirewallRule(uint32_t code, MessageParcel &data,
122 MessageParcel &reply, MessageOption &option)
123 {
124 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeBoolSendRequestGetFirewallRule code :" << code;
125 code_ = code;
126 reply.WriteInt32(ERR_OK);
127 reply.WriteInt32(1);
128 IPTABLES::DomainFilterRule rule{IPTABLES::Action::INVALID, "321", "www.example.com", IPTABLES::Direction::OUTPUT};
129 IPTABLES::DomainFilterRuleParcel ruleParcel{rule};
130 ruleParcel.Marshalling(reply);
131 return 0;
132 }
133
InvokeSendRequestGetDomainFilterRules(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)134 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetDomainFilterRules(uint32_t code, MessageParcel &data,
135 MessageParcel &reply, MessageOption &option)
136 {
137 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetDomainFilterRules code :" << code;
138 code_ = code;
139 reply.WriteInt32(ERR_OK);
140 reply.WriteInt32(1);
141 IPTABLES::FirewallRule rule{IPTABLES::Direction::INVALID, IPTABLES::Action::INVALID, IPTABLES::Protocol::INVALID,
142 "", "", "", "", ""};
143 IPTABLES::FirewallRuleParcel ruleParcel{rule};
144 ruleParcel.Marshalling(reply);
145 return 0;
146 }
147
InvokeIntSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)148 int EnterpriseDeviceMgrStubMock::InvokeIntSendRequestGetPolicy(uint32_t code, MessageParcel &data, MessageParcel &reply,
149 MessageOption &option)
150 {
151 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeIntSendRequestGetPolicy code :" << code;
152 code_ = code;
153 reply.WriteInt32(ERR_OK);
154 reply.WriteInt32(0);
155 return 0;
156 }
157
InvokeArrayStringSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)158 int EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy(uint32_t code, MessageParcel &data,
159 MessageParcel &reply, MessageOption &option)
160 {
161 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
162 code_ = code;
163 reply.WriteInt32(ERR_OK);
164 reply.WriteStringVector(std::vector<std::string>{RETURN_STRING});
165 return 0;
166 }
167
InvokeSendRequestGetErrPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)168 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetErrPolicy(uint32_t code, MessageParcel &data,
169 MessageParcel &reply, MessageOption &option)
170 {
171 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
172 code_ = code;
173 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
174 return 0;
175 }
176
InvokeSendRequestGetPolicyExceedsMax(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)177 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyExceedsMax(uint32_t code, MessageParcel &data,
178 MessageParcel &reply, MessageOption &option)
179 {
180 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
181 code_ = code;
182 reply.WriteInt32(ERR_OK);
183 reply.WriteInt32(APPID_MAX_SIZE + 1);
184 reply.WriteStringVector(std::vector<std::string>{RETURN_STRING});
185 return 0;
186 }
187
InvokeArrayElementSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)188 int EnterpriseDeviceMgrStubMock::InvokeArrayElementSendRequestGetPolicy(uint32_t code, MessageParcel &data,
189 MessageParcel &reply, MessageOption &option)
190 {
191 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
192 code_ = code;
193 reply.WriteInt32(ERR_OK);
194 reply.WriteStringVector(std::vector<std::string>{ELEMENT_STRING});
195 return 0;
196 }
197
InvokeSendRequestParamError(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)198 int EnterpriseDeviceMgrStubMock::InvokeSendRequestParamError(uint32_t code, MessageParcel &data, MessageParcel &reply,
199 MessageOption &option)
200 {
201 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
202 code_ = code;
203 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
204 reply.WriteString(RETURN_STRING);
205 return 0;
206 }
207
InvokeHttpProxySendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)208 int EnterpriseDeviceMgrStubMock::InvokeHttpProxySendRequestGetPolicy(uint32_t code, MessageParcel &data,
209 MessageParcel &reply, MessageOption &option)
210 {
211 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeHttpProxySendRequestGetPolicy code :" << code;
212 code_ = code;
213 OHOS::NetManagerStandard::HttpProxy httpProxy;
214 reply.WriteInt32(ERR_OK);
215 httpProxy.Marshalling(reply);
216 return ERR_OK;
217 }
218
InvokeBluetoothProxySendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)219 int EnterpriseDeviceMgrStubMock::InvokeBluetoothProxySendRequestGetPolicy(uint32_t code, MessageParcel &data,
220 MessageParcel &reply, MessageOption &option)
221 {
222 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeBluetoothProxySendRequestGetPolicy code :" << code;
223 EntInfo entInfo;
224 code_ = code;
225 reply.WriteInt32(ERR_OK);
226 reply.WriteString(RETURN_STRING);
227 reply.WriteInt32(1);
228 reply.WriteInt32(1);
229 return 0;
230 }
231
InvokeAccountProxySendRequestAddOsAccount(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)232 int EnterpriseDeviceMgrStubMock::InvokeAccountProxySendRequestAddOsAccount(uint32_t code, MessageParcel &data,
233 MessageParcel &reply, MessageOption &option)
234 {
235 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeOsAccountProxySendRequestGetPolicy code :" << code;
236 OHOS::AccountSA::OsAccountInfo accountInfo;
237 code_ = code;
238 reply.WriteInt32(ERR_OK);
239 accountInfo.SetLocalName(RETURN_STRING);
240 accountInfo.Marshalling(reply);
241 reply.WriteString(RETURN_STRING);
242 reply.WriteString(RETURN_STRING);
243 return 0;
244 }
245
InvokeAllowedUsbDevicesSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)246 int EnterpriseDeviceMgrStubMock::InvokeAllowedUsbDevicesSendRequestGetPolicy(uint32_t code, MessageParcel &data,
247 MessageParcel &reply, MessageOption &option)
248 {
249 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeAllowedUsbDevicesSendRequestGetPolicy code :" << code;
250 code_ = code;
251 reply.WriteInt32(ERR_OK);
252 std::vector<UsbDeviceId> usbDeviceIds;
253 UsbDeviceId id1;
254 int32_t testVid = 1;
255 int32_t testPid = 9;
256 id1.SetVendorId(testVid);
257 id1.SetProductId(testPid);
258 usbDeviceIds.push_back(id1);
259 reply.WriteInt32(usbDeviceIds.size());
260 std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(), [&](const auto usbDeviceId) {
261 usbDeviceId.Marshalling(reply);
262 });
263 return 0;
264 }
265
InvokeDisallowedUsbDevicesSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)266 int EnterpriseDeviceMgrStubMock::InvokeDisallowedUsbDevicesSendRequestGetPolicy(uint32_t code, MessageParcel &data,
267 MessageParcel &reply, MessageOption &option)
268 {
269 GTEST_LOG_(INFO) <<
270 "mock EnterpriseDeviceMgrStubMock InvokeDisallowedUsbDevicesSendRequestGetPolicy code :" << code;
271 code_ = code;
272 reply.WriteInt32(ERR_OK);
273 std::vector<USB::UsbDeviceType> usbDeviceTypes;
274 USB::UsbDeviceType type;
275 type.baseClass = BASE_CLASS;
276 type.subClass = SUB_CLASS;
277 type.protocol = PROTOCOL;
278 type.isDeviceType = false;
279 usbDeviceTypes.push_back(type);
280 reply.WriteInt32(usbDeviceTypes.size());
281 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
282 usbDeviceType.Marshalling(reply);
283 });
284 return 0;
285 }
286
InvokeSendRequestGetSuperAdmin(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)287 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetSuperAdmin(uint32_t code, MessageParcel &data,
288 MessageParcel &reply, MessageOption &option)
289 {
290 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetSuperAdmin code :" << code;
291 code_ = code;
292 reply.WriteInt32(ERR_OK);
293 reply.WriteString(RETURN_STRING);
294 reply.WriteString(RETURN_STRING);
295 return 0;
296 }
297
InvokeSendRequestGetOTAUpdatePolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)298 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetOTAUpdatePolicy(uint32_t code, MessageParcel &data,
299 MessageParcel &reply, MessageOption &option)
300 {
301 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetOTAUpdatePolicy code :" << code;
302 code_ = code;
303 reply.WriteInt32(ERR_OK);
304 UpdatePolicy updatePolicy;
305 updatePolicy.type = UpdatePolicyType::PROHIBIT;
306 updatePolicy.version = UPGRADE_VERSION;
307 UpdatePolicyUtils::WriteUpdatePolicy(reply, updatePolicy);
308 return 0;
309 }
310
InvokeSendRequestGetUpgradeResult(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)311 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetUpgradeResult(uint32_t code, MessageParcel &data,
312 MessageParcel &reply, MessageOption &option)
313 {
314 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetOTAUpdatePolicy code :" << code;
315 code_ = code;
316 reply.WriteInt32(ERR_OK);
317 UpgradeResult upgradeResult;
318 upgradeResult.status = UpgradeStatus::UPGRADE_FAILURE;
319 upgradeResult.version = UPGRADE_VERSION;
320 upgradeResult.errorCode = UPGRADE_FAILED_CODE;
321 upgradeResult.errorMessage = UPGRADE_FAILED_MESSAGE;
322 UpdatePolicyUtils::WriteUpgradeResult(reply, upgradeResult);
323 return 0;
324 }
325
InvokeSendRequestGetPasswordPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)326 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPasswordPolicy(uint32_t code, MessageParcel &data,
327 MessageParcel &reply, MessageOption &option)
328 {
329 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPasswordPolicy code :" << code;
330 code_ = code;
331 reply.WriteInt32(ERR_OK);
332 reply.WriteString("test_complexityReg");
333 reply.WriteInt64(1);
334 reply.WriteString("test_additionalDescription");
335 return 0;
336 }
337
InvokeSendRequestGetManagedBrowserPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)338 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetManagedBrowserPolicy(uint32_t code, MessageParcel &data,
339 MessageParcel &reply, MessageOption &option)
340 {
341 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetManagedBrowserPolicy code :" << code;
342 code_ = code;
343 reply.WriteInt32(ERR_OK);
344 int32_t size = 200;
345 reply.WriteInt32(size);
346 void* rawData = malloc(size);
347 reply.WriteRawData(rawData, size);
348 return 0;
349 }
350
InvokeSendRequestGetAdmins(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)351 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetAdmins(uint32_t code, MessageParcel &data,
352 MessageParcel &reply, MessageOption &option)
353 {
354 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetAdmins code :" << code;
355 code_ = code;
356 reply.WriteInt32(ERR_OK);
357 int32_t size = 1;
358 reply.WriteUint32(size);
359 std::shared_ptr<AAFwk::Want> want = std::make_shared<AAFwk::Want>();
360 std::string bundleName = "com.edm.test.demo";
361 std::string abilityName = "test.ability";
362 want->SetParam("bundleName", bundleName);
363 want->SetParam("abilityName", abilityName);
364 want->SetParam("adminType", static_cast<int32_t>(AdminType::BYOD));
365 reply.WriteParcelable(want.get());
366 return 0;
367 }
368
InvokeSendRequestCheckAndGetAdminProvisionInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)369 int EnterpriseDeviceMgrStubMock::InvokeSendRequestCheckAndGetAdminProvisionInfo(uint32_t code, MessageParcel &data,
370 MessageParcel &reply, MessageOption &option)
371 {
372 GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetAdmins code :" << code;
373 code_ = code;
374 reply.WriteInt32(ERR_OK);
375 reply.WriteString("com.edm.test.demo");
376 return 0;
377 }
378 } // namespace EDM
379 } // namespace OHOS
380