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 "edm_plugin_fuzzer.h"
17 
18 #include "common_fuzzer.h"
19 #include "edm_ipc_interface_code.h"
20 #include "func_code.h"
21 
22 namespace OHOS {
23 namespace EDM {
24 constexpr size_t MIN_SIZE = 4;
25 constexpr uint32_t MAX_EDM_INTERFACE_CODE = 1000;
26 
27 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)28 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
29 {
30     if (data == nullptr) {
31         return 0;
32     }
33 
34     if (size < MIN_SIZE) {
35         return 0;
36     }
37     uint32_t operateType = (CommonFuzzer::GetU32Data(data)) % 3;
38     uint32_t code = (CommonFuzzer::GetU32Data(data)) % 60 + MAX_EDM_INTERFACE_CODE;
39     if (code == EdmInterfaceCode::RESET_FACTORY || code == EdmInterfaceCode::SHUTDOWN ||
40         code == EdmInterfaceCode::REBOOT || code == EdmInterfaceCode::USB_READ_ONLY ||
41         code == EdmInterfaceCode::DISABLED_HDC || code == EdmInterfaceCode::DISABLE_USB ||
42         code == EdmInterfaceCode::SET_WIFI_PROFILE) {
43         return 0;
44     }
45     code = POLICY_FUNC_CODE(operateType, code);
46     MessageParcel parcel;
47     CommonFuzzer::SetParcelContent(parcel, data, size);
48     CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
49 
50     return 0;
51 }
52 } // namespace EDM
53 } // namespace OHOS