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 "power_policy_plugin_fuzzer.h"
17
18 #include <system_ability_definition.h>
19
20 #include "common_fuzzer.h"
21 #include "edm_ipc_interface_code.h"
22 #include "ienterprise_device_mgr.h"
23 #include "func_code.h"
24 #include "power_policy.h"
25 #define private public
26 #include "power_policy_plugin.h"
27 #undef private
28 #include "message_parcel.h"
29 #include "utils.h"
30
31 namespace OHOS {
32 namespace EDM {
33 constexpr size_t MIN_SIZE = 24;
34 constexpr int32_t WITHOUT_USERID = 0;
35 constexpr uint32_t MAX_POWER_POLICY_ACTION_ENUM = 5;
36
37 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
39 {
40 if (data == nullptr) {
41 return 0;
42 }
43 if (size < MIN_SIZE) {
44 return 0;
45 }
46 int32_t pos = 0;
47 int32_t stringSize = size / 8;
48 for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
49 operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
50 uint32_t code = EdmInterfaceCode::USB_READ_ONLY;
51 code = POLICY_FUNC_CODE(operateType, code);
52
53 AppExecFwk::ElementName admin;
54 admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
55 admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
56 MessageParcel parcel;
57 parcel.WriteInterfaceToken(IEnterpriseDeviceMgrIdl::GetDescriptor());
58 parcel.WriteInt32(WITHOUT_USERID);
59 if (operateType) {
60 parcel.WriteParcelable(&admin);
61 uint32_t powerScene = CommonFuzzer::GetU32Data(data);
62 parcel.WriteUint32(powerScene);
63 uint32_t powerPolicyAction = CommonFuzzer::GetU32Data(data) % MAX_POWER_POLICY_ACTION_ENUM;
64 parcel.WriteUint32(powerPolicyAction);
65 uint32_t delayTime = CommonFuzzer::GetU32Data(data);
66 parcel.WriteUint32(delayTime);
67 } else {
68 parcel.WriteString("");
69 parcel.WriteInt32(0);
70 parcel.WriteParcelable(&admin);
71 }
72 CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
73 }
74
75 PowerPolicyPlugin plugin;
76 std::string policyData = CommonFuzzer::GetString(data, pos, stringSize, size);
77 MessageParcel parcel;
78 parcel.WriteInt32(CommonFuzzer::GetU32Data(data));
79 MessageParcel reply;
80 int32_t userId = CommonFuzzer::GetU32Data(data);
81 plugin.OnGetPolicy(policyData, parcel, reply, userId);
82
83 std::string policyKey = CommonFuzzer::GetString(data, pos, stringSize, size);
84 PowerPolicy powerPolicy;
85 bool isSetPolicy = CommonFuzzer::GetU32Data(data) % 2;
86 plugin.DealPowerSuspendPolicy(policyKey, powerPolicy, isSetPolicy);
87 return 0;
88 }
89 } // namespace EDM
90 } // namespace OHOS