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