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"};
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.WriteInt32(1);
165     reply.WriteStringVector(std::vector<std::string>{RETURN_STRING});
166     return 0;
167 }
168 
InvokeSendRequestGetErrPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)169 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetErrPolicy(uint32_t code, MessageParcel &data,
170     MessageParcel &reply, MessageOption &option)
171 {
172     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
173     code_ = code;
174     reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
175     return 0;
176 }
177 
InvokeSendRequestGetPolicyExceedsMax(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)178 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyExceedsMax(uint32_t code, MessageParcel &data,
179     MessageParcel &reply, MessageOption &option)
180 {
181     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
182     code_ = code;
183     reply.WriteInt32(ERR_OK);
184     reply.WriteInt32(APPID_MAX_SIZE + 1);
185     reply.WriteStringVector(std::vector<std::string>{RETURN_STRING});
186     return 0;
187 }
188 
InvokeArrayElementSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)189 int EnterpriseDeviceMgrStubMock::InvokeArrayElementSendRequestGetPolicy(uint32_t code, MessageParcel &data,
190     MessageParcel &reply, MessageOption &option)
191 {
192     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
193     code_ = code;
194     reply.WriteInt32(ERR_OK);
195     reply.WriteStringVector(std::vector<std::string>{ELEMENT_STRING});
196     return 0;
197 }
198 
InvokeSendRequestParamError(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)199 int EnterpriseDeviceMgrStubMock::InvokeSendRequestParamError(uint32_t code, MessageParcel &data, MessageParcel &reply,
200     MessageOption &option)
201 {
202     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPolicy code :" << code;
203     code_ = code;
204     reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
205     reply.WriteString(RETURN_STRING);
206     return 0;
207 }
208 
InvokeHttpProxySendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)209 int EnterpriseDeviceMgrStubMock::InvokeHttpProxySendRequestGetPolicy(uint32_t code, MessageParcel &data,
210     MessageParcel &reply, MessageOption &option)
211 {
212     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeHttpProxySendRequestGetPolicy code :" << code;
213     code_ = code;
214     OHOS::NetManagerStandard::HttpProxy httpProxy;
215     reply.WriteInt32(ERR_OK);
216     httpProxy.Marshalling(reply);
217     return ERR_OK;
218 }
219 
InvokeBluetoothProxySendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)220 int EnterpriseDeviceMgrStubMock::InvokeBluetoothProxySendRequestGetPolicy(uint32_t code, MessageParcel &data,
221     MessageParcel &reply, MessageOption &option)
222 {
223     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeBluetoothProxySendRequestGetPolicy code :" << code;
224     EntInfo entInfo;
225     code_ = code;
226     reply.WriteInt32(ERR_OK);
227     reply.WriteString(RETURN_STRING);
228     reply.WriteInt32(1);
229     reply.WriteInt32(1);
230     return 0;
231 }
232 
InvokeAccountProxySendRequestAddOsAccount(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)233 int EnterpriseDeviceMgrStubMock::InvokeAccountProxySendRequestAddOsAccount(uint32_t code, MessageParcel &data,
234     MessageParcel &reply, MessageOption &option)
235 {
236     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeOsAccountProxySendRequestGetPolicy code :" << code;
237     OHOS::AccountSA::OsAccountInfo accountInfo;
238     code_ = code;
239     reply.WriteInt32(ERR_OK);
240     accountInfo.SetLocalName(RETURN_STRING);
241     accountInfo.Marshalling(reply);
242     reply.WriteString(RETURN_STRING);
243     reply.WriteString(RETURN_STRING);
244     return 0;
245 }
246 
InvokeAllowedUsbDevicesSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)247 int EnterpriseDeviceMgrStubMock::InvokeAllowedUsbDevicesSendRequestGetPolicy(uint32_t code, MessageParcel &data,
248     MessageParcel &reply, MessageOption &option)
249 {
250     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeAllowedUsbDevicesSendRequestGetPolicy code :" << code;
251     code_ = code;
252     reply.WriteInt32(ERR_OK);
253     std::vector<UsbDeviceId> usbDeviceIds;
254     UsbDeviceId id1;
255     int32_t testVid = 1;
256     int32_t testPid = 9;
257     id1.SetVendorId(testVid);
258     id1.SetProductId(testPid);
259     usbDeviceIds.push_back(id1);
260     reply.WriteInt32(usbDeviceIds.size());
261     std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(), [&](const auto usbDeviceId) {
262         usbDeviceId.Marshalling(reply);
263     });
264     return 0;
265 }
266 
InvokeDisallowedUsbDevicesSendRequestGetPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)267 int EnterpriseDeviceMgrStubMock::InvokeDisallowedUsbDevicesSendRequestGetPolicy(uint32_t code, MessageParcel &data,
268     MessageParcel &reply, MessageOption &option)
269 {
270     GTEST_LOG_(INFO) <<
271         "mock EnterpriseDeviceMgrStubMock InvokeDisallowedUsbDevicesSendRequestGetPolicy code :" << code;
272     code_ = code;
273     reply.WriteInt32(ERR_OK);
274     std::vector<USB::UsbDeviceType> usbDeviceTypes;
275     USB::UsbDeviceType type;
276     type.baseClass = BASE_CLASS;
277     type.subClass = SUB_CLASS;
278     type.protocol = PROTOCOL;
279     type.isDeviceType = false;
280     usbDeviceTypes.push_back(type);
281     reply.WriteInt32(usbDeviceTypes.size());
282     std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
283         usbDeviceType.Marshalling(reply);
284     });
285     return 0;
286 }
287 
InvokeSendRequestGetSuperAdmin(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)288 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetSuperAdmin(uint32_t code, MessageParcel &data,
289     MessageParcel &reply, MessageOption &option)
290 {
291     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetSuperAdmin code :" << code;
292     code_ = code;
293     reply.WriteInt32(ERR_OK);
294     reply.WriteString(RETURN_STRING);
295     reply.WriteString(RETURN_STRING);
296     return 0;
297 }
298 
InvokeSendRequestGetOTAUpdatePolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)299 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetOTAUpdatePolicy(uint32_t code, MessageParcel &data,
300     MessageParcel &reply, MessageOption &option)
301 {
302     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetOTAUpdatePolicy code :" << code;
303     code_ = code;
304     reply.WriteInt32(ERR_OK);
305     UpdatePolicy updatePolicy;
306     updatePolicy.type = UpdatePolicyType::PROHIBIT;
307     updatePolicy.version = UPGRADE_VERSION;
308     UpdatePolicyUtils::WriteUpdatePolicy(reply, updatePolicy);
309     return 0;
310 }
311 
InvokeSendRequestGetUpgradeResult(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)312 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetUpgradeResult(uint32_t code, MessageParcel &data,
313     MessageParcel &reply, MessageOption &option)
314 {
315     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetOTAUpdatePolicy code :" << code;
316     code_ = code;
317     reply.WriteInt32(ERR_OK);
318     UpgradeResult upgradeResult;
319     upgradeResult.status = UpgradeStatus::UPGRADE_FAILURE;
320     upgradeResult.version = UPGRADE_VERSION;
321     upgradeResult.errorCode = UPGRADE_FAILED_CODE;
322     upgradeResult.errorMessage = UPGRADE_FAILED_MESSAGE;
323     UpdatePolicyUtils::WriteUpgradeResult(reply, upgradeResult);
324     return 0;
325 }
326 
InvokeSendRequestGetPasswordPolicy(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)327 int EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPasswordPolicy(uint32_t code, MessageParcel &data,
328     MessageParcel &reply, MessageOption &option)
329 {
330     GTEST_LOG_(INFO) << "mock EnterpriseDeviceMgrStubMock InvokeSendRequestGetPasswordPolicy code :" << code;
331     code_ = code;
332     reply.WriteInt32(ERR_OK);
333     reply.WriteString("test_complexityReg");
334     reply.WriteInt64(1);
335     reply.WriteString("test_additionalDescription");
336     return 0;
337 }
338 } // namespace EDM
339 } // namespace OHOS
340