1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "usbcontroltransferreadwithlength_fuzzer.h"
17094332d3Sopenharmony_ci#include "UsbSubscriberTest.h"
18094332d3Sopenharmony_ci#include "hdf_log.h"
19094332d3Sopenharmony_ci#include "securec.h"
20094332d3Sopenharmony_ci#include "usbcommonfunction_fuzzer.h"
21094332d3Sopenharmony_ci#include "v1_1/iusb_interface.h"
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
24094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_1;
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cinamespace OHOS {
27094332d3Sopenharmony_ciconstexpr size_t THRESHOLD = 10;
28094332d3Sopenharmony_ciconstexpr int32_t OFFSET = 4;
29094332d3Sopenharmony_cinamespace USB {
30094332d3Sopenharmony_cibool UsbControlTransferReadwithLengthFuzzTest(const uint8_t *data, size_t size)
31094332d3Sopenharmony_ci{
32094332d3Sopenharmony_ci    (void)size;
33094332d3Sopenharmony_ci    UsbDev dev;
34094332d3Sopenharmony_ci    sptr<OHOS::HDI::Usb::V1_1::IUsbInterface> usbInterface = OHOS::HDI::Usb::V1_1::IUsbInterface::Get();
35094332d3Sopenharmony_ci    int32_t ret = UsbFuzzTestHostModeInit(dev, usbInterface);
36094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
37094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: UsbFuzzTestHostModeInit failed", __func__);
38094332d3Sopenharmony_ci        return false;
39094332d3Sopenharmony_ci    }
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ci    UsbCtrlTransferParams ctrl;
42094332d3Sopenharmony_ci    if (memcpy_s((void *)&ctrl, sizeof(ctrl), data, sizeof(ctrl)) != EOK) {
43094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: memcpy_s failed", __func__);
44094332d3Sopenharmony_ci        return false;
45094332d3Sopenharmony_ci    }
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ci    ret = usbInterface->ControlTransferReadwithLength(
48094332d3Sopenharmony_ci        dev, ctrl, reinterpret_cast<std::vector<uint8_t> &>(std::move(data + OFFSET)));
49094332d3Sopenharmony_ci    if (ret == HDF_SUCCESS) {
50094332d3Sopenharmony_ci        HDF_LOGI("%{public}s: control transfer read succeed", __func__);
51094332d3Sopenharmony_ci    }
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_ci    ret = usbInterface->CloseDevice(dev);
54094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
55094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: close device failed", __func__);
56094332d3Sopenharmony_ci        return false;
57094332d3Sopenharmony_ci    }
58094332d3Sopenharmony_ci    return true;
59094332d3Sopenharmony_ci}
60094332d3Sopenharmony_ci} // namespace USB
61094332d3Sopenharmony_ci} // namespace OHOS
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64094332d3Sopenharmony_ci{
65094332d3Sopenharmony_ci    if (size < OHOS::THRESHOLD) {
66094332d3Sopenharmony_ci        return 0;
67094332d3Sopenharmony_ci    }
68094332d3Sopenharmony_ci    OHOS::USB::UsbControlTransferReadwithLengthFuzzTest(data, size);
69094332d3Sopenharmony_ci    return 0;
70094332d3Sopenharmony_ci}