142103316Sopenharmony_ci/*
242103316Sopenharmony_ci * Copyright (c) 2022 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 "usbmgrpiperequestwait_fuzzer.h"
1742103316Sopenharmony_ci
1842103316Sopenharmony_ci#include "usb_errors.h"
1942103316Sopenharmony_ci#include "usb_srv_client.h"
2042103316Sopenharmony_ci
2142103316Sopenharmony_cinamespace OHOS {
2242103316Sopenharmony_ciconst uint32_t OFFSET = 4;
2342103316Sopenharmony_ciconst uint32_t OFFSET_BYTE = 8;
2442103316Sopenharmony_ciconstexpr size_t THRESHOLD = 10;
2542103316Sopenharmony_cinamespace USB {
2642103316Sopenharmony_ci    bool UsbMgrPipeRequestWaitFuzzTest(const uint8_t* data, size_t /* size */)
2742103316Sopenharmony_ci    {
2842103316Sopenharmony_ci        std::vector<UsbDevice> devList;
2942103316Sopenharmony_ci        auto &usbSrvClient = UsbSrvClient::GetInstance();
3042103316Sopenharmony_ci        auto ret = usbSrvClient.GetDevices(devList);
3142103316Sopenharmony_ci        if (ret != UEC_OK || devList.empty()) {
3242103316Sopenharmony_ci            USB_HILOGE(MODULE_USB_SERVICE, "get devices failed ret=%{public}d", ret);
3342103316Sopenharmony_ci            return false;
3442103316Sopenharmony_ci        }
3542103316Sopenharmony_ci
3642103316Sopenharmony_ci        USBDevicePipe pipe;
3742103316Sopenharmony_ci        UsbDevice device = devList.front();
3842103316Sopenharmony_ci        usbSrvClient.RequestRight(device.GetName());
3942103316Sopenharmony_ci        ret = usbSrvClient.OpenDevice(device, pipe);
4042103316Sopenharmony_ci        if (ret != UEC_OK) {
4142103316Sopenharmony_ci            USB_HILOGE(MODULE_USB_SERVICE, "open device failed ret=%{public}d", ret);
4242103316Sopenharmony_ci            return false;
4342103316Sopenharmony_ci        }
4442103316Sopenharmony_ci
4542103316Sopenharmony_ci        UsbInterface interface = devList.front().GetConfigs().front().GetInterfaces().front();
4642103316Sopenharmony_ci        ret = usbSrvClient.ClaimInterface(pipe, interface, true);
4742103316Sopenharmony_ci        if (ret != UEC_OK) {
4842103316Sopenharmony_ci            USB_HILOGE(MODULE_USB_SERVICE, "ClaimInterface failed ret=%{public}d", ret);
4942103316Sopenharmony_ci            return false;
5042103316Sopenharmony_ci        }
5142103316Sopenharmony_ci
5242103316Sopenharmony_ci        ret = usbSrvClient.PipeRequestWait(reinterpret_cast<USBDevicePipe &>(data),
5342103316Sopenharmony_ci            *reinterpret_cast<const int64_t *>(std::move(data + OFFSET)),
5442103316Sopenharmony_ci            reinterpret_cast<UsbRequest &>(std::move(data + OFFSET_BYTE)));
5542103316Sopenharmony_ci        if (ret == UEC_OK) {
5642103316Sopenharmony_ci            return false;
5742103316Sopenharmony_ci        }
5842103316Sopenharmony_ci        return true;
5942103316Sopenharmony_ci    }
6042103316Sopenharmony_ci} // USB
6142103316Sopenharmony_ci} // OHOS
6242103316Sopenharmony_ci
6342103316Sopenharmony_ci/* Fuzzer entry point */
6442103316Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
6542103316Sopenharmony_ci{
6642103316Sopenharmony_ci    if (size < OHOS::THRESHOLD) {
6742103316Sopenharmony_ci        return 0;
6842103316Sopenharmony_ci    }
6942103316Sopenharmony_ci    /* Run your code on data */
7042103316Sopenharmony_ci    OHOS::USB::UsbMgrPipeRequestWaitFuzzTest(data, size);
7142103316Sopenharmony_ci    return 0;
7242103316Sopenharmony_ci}
7342103316Sopenharmony_ci
74