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 #include "message_parcel.h"
26 #include "utils.h"
27 
28 namespace OHOS {
29 namespace EDM {
30 constexpr size_t MIN_SIZE = 24;
31 constexpr int32_t WITHOUT_USERID = 0;
32 constexpr uint32_t MAX_POWER_POLICY_ACTION_ENUM = 5;
33 
34 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)35 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
36 {
37     if (data == nullptr) {
38         return 0;
39     }
40     if (size < MIN_SIZE) {
41         return 0;
42     }
43     int32_t pos = 0;
44     int32_t stringSize = size / 6;
45     for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
46         operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
47         uint32_t code = EdmInterfaceCode::USB_READ_ONLY;
48         code = POLICY_FUNC_CODE(operateType, code);
49 
50         AppExecFwk::ElementName admin;
51         admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
52         admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
53         MessageParcel parcel;
54         parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor());
55         parcel.WriteInt32(WITHOUT_USERID);
56         if (operateType) {
57             parcel.WriteParcelable(&admin);
58             uint32_t powerScene = CommonFuzzer::GetU32Data(data);
59             parcel.WriteUint32(powerScene);
60             uint32_t powerPolicyAction = CommonFuzzer::GetU32Data(data) % MAX_POWER_POLICY_ACTION_ENUM;
61             parcel.WriteUint32(powerPolicyAction);
62             uint32_t delayTime = CommonFuzzer::GetU32Data(data);
63             parcel.WriteUint32(delayTime);
64         } else {
65             parcel.WriteString("");
66             parcel.WriteInt32(0);
67             parcel.WriteParcelable(&admin);
68         }
69         CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
70     }
71     return 0;
72 }
73 } // namespace EDM
74 } // namespace OHOS