142103316Sopenharmony_ci/*
242103316Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
342103316Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442103316Sopenharmony_ci * you may not use this file except in compliance with the License.
542103316Sopenharmony_ci * You may obtain a copy of the License at
642103316Sopenharmony_ci *
742103316Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
842103316Sopenharmony_ci *
942103316Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042103316Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142103316Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242103316Sopenharmony_ci * See the License for the specific language governing permissions and
1342103316Sopenharmony_ci * limitations under the License.
1442103316Sopenharmony_ci */
1542103316Sopenharmony_ci
1642103316Sopenharmony_ci#include "usbmgrattachkerneldriver_fuzzer.h"
1742103316Sopenharmony_ci
1842103316Sopenharmony_ci#include "usb_errors.h"
1942103316Sopenharmony_ci#include "usb_srv_client.h"
2042103316Sopenharmony_ci#include <cstddef>
2142103316Sopenharmony_ci#include <cstdint>
2242103316Sopenharmony_ci#include "usb_service.h"
2342103316Sopenharmony_ci
2442103316Sopenharmony_ciusing namespace OHOS::USB;
2542103316Sopenharmony_ci
2642103316Sopenharmony_cinamespace OHOS {
2742103316Sopenharmony_ciconst uint32_t OFFSET = 4;
2842103316Sopenharmony_ciconstexpr size_t THRESHOLD = 10;
2942103316Sopenharmony_cienum class UsbInterfaceCode {
3042103316Sopenharmony_ci    USB_FUN_HAS_RIGHT = 0,
3142103316Sopenharmony_ci    USB_FUN_REQUEST_RIGHT,
3242103316Sopenharmony_ci    USB_FUN_REMOVE_RIGHT,
3342103316Sopenharmony_ci    USB_FUN_OPEN_DEVICE,
3442103316Sopenharmony_ci    USB_FUN_RESET_DEVICE,
3542103316Sopenharmony_ci    USB_FUN_GET_DEVICE,
3642103316Sopenharmony_ci    USB_FUN_GET_DEVICES,
3742103316Sopenharmony_ci    USB_FUN_GET_CURRENT_FUNCTIONS,
3842103316Sopenharmony_ci    USB_FUN_SET_CURRENT_FUNCTIONS,
3942103316Sopenharmony_ci    USB_FUN_USB_FUNCTIONS_FROM_STRING,
4042103316Sopenharmony_ci    USB_FUN_USB_FUNCTIONS_TO_STRING,
4142103316Sopenharmony_ci    USB_FUN_CLAIM_INTERFACE,
4242103316Sopenharmony_ci    USB_FUN_RELEASE_INTERFACE,
4342103316Sopenharmony_ci    USB_FUN_BULK_TRANSFER_READ,
4442103316Sopenharmony_ci    USB_FUN_BULK_TRANSFER_WRITE,
4542103316Sopenharmony_ci    USB_FUN_CONTROL_TRANSFER,
4642103316Sopenharmony_ci    USB_FUN_USB_CONTROL_TRANSFER,
4742103316Sopenharmony_ci    USB_FUN_SET_ACTIVE_CONFIG,
4842103316Sopenharmony_ci    USB_FUN_GET_ACTIVE_CONFIG,
4942103316Sopenharmony_ci    USB_FUN_SET_INTERFACE,
5042103316Sopenharmony_ci    USB_FUN_GET_PORTS,
5142103316Sopenharmony_ci    USB_FUN_GET_SUPPORTED_MODES,
5242103316Sopenharmony_ci    USB_FUN_SET_PORT_ROLE,
5342103316Sopenharmony_ci    USB_FUN_REQUEST_QUEUE,
5442103316Sopenharmony_ci    USB_FUN_REQUEST_WAIT,
5542103316Sopenharmony_ci    USB_FUN_REQUEST_CANCEL,
5642103316Sopenharmony_ci    USB_FUN_GET_DESCRIPTOR,
5742103316Sopenharmony_ci    USB_FUN_GET_FILEDESCRIPTOR,
5842103316Sopenharmony_ci    USB_FUN_CLOSE_DEVICE,
5942103316Sopenharmony_ci    USB_FUN_BULK_AYSNC_READ,
6042103316Sopenharmony_ci    USB_FUN_BULK_AYSNC_WRITE,
6142103316Sopenharmony_ci    USB_FUN_BULK_AYSNC_CANCEL,
6242103316Sopenharmony_ci    USB_FUN_REG_BULK_CALLBACK,
6342103316Sopenharmony_ci    USB_FUN_UNREG_BULK_CALLBACK,
6442103316Sopenharmony_ci    USB_FUN_ADD_RIGHT,
6542103316Sopenharmony_ci    USB_FUN_DISABLE_GLOBAL_INTERFACE,
6642103316Sopenharmony_ci    USB_FUN_DISABLE_DEVICE,
6742103316Sopenharmony_ci    USB_FUN_DISABLE_INTERFACE_TYPE,
6842103316Sopenharmony_ci    USB_FUN_CLEAR_HALT,
6942103316Sopenharmony_ci    USB_FUN_GET_DEVICE_SPEED,
7042103316Sopenharmony_ci    USB_FUN_GET_DRIVER_ACTIVE_STATUS,
7142103316Sopenharmony_ci    USB_FUN_ADD_ACCESS_RIGHT,
7242103316Sopenharmony_ci    USB_FUN_BULK_TRANSFER_READ_WITH_LENGTH,
7342103316Sopenharmony_ci    USB_FUN_ATTACH_KERNEL_DRIVER,
7442103316Sopenharmony_ci    USB_FUN_DETACH_KERNEL_DRIVER,
7542103316Sopenharmony_ci};
7642103316Sopenharmony_ciconst std::u16string USB_INTERFACE_TOKEN = u"ohos.usb.IUsbSrv";
7742103316Sopenharmony_ci
7842103316Sopenharmony_cibool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
7942103316Sopenharmony_ci{
8042103316Sopenharmony_ci    uint32_t code = static_cast<uint32_t>(UsbInterfaceCode::USB_FUN_ATTACH_KERNEL_DRIVER);
8142103316Sopenharmony_ci
8242103316Sopenharmony_ci    rawData = rawData + OFFSET;
8342103316Sopenharmony_ci    size = size - OFFSET;
8442103316Sopenharmony_ci
8542103316Sopenharmony_ci    MessageParcel data;
8642103316Sopenharmony_ci    data.WriteInterfaceToken(USB_INTERFACE_TOKEN);
8742103316Sopenharmony_ci    data.WriteBuffer(rawData, size);
8842103316Sopenharmony_ci    data.RewindRead(0);
8942103316Sopenharmony_ci    MessageParcel reply;
9042103316Sopenharmony_ci    MessageOption option;
9142103316Sopenharmony_ci    DelayedSpSingleton<UsbService>::GetInstance()->OnRemoteRequest(code, data, reply, option);
9242103316Sopenharmony_ci
9342103316Sopenharmony_ci    return true;
9442103316Sopenharmony_ci}
9542103316Sopenharmony_ci} // OHOS
9642103316Sopenharmony_ci
9742103316Sopenharmony_ci/* Fuzzer entry point */
9842103316Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
9942103316Sopenharmony_ci{
10042103316Sopenharmony_ci    if (size < OHOS::THRESHOLD) {
10142103316Sopenharmony_ci        return 0;
10242103316Sopenharmony_ci    }
10342103316Sopenharmony_ci    /* Run your code on data */
10442103316Sopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
10542103316Sopenharmony_ci    return 0;
10642103316Sopenharmony_ci}
107