1 /*
2 * Copyright (c) 2022 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 "usbinterrupttransferread_fuzzer.h"
17 #include "UsbSubscriberTest.h"
18 #include "hdf_log.h"
19 #include "securec.h"
20 #include "usbcommonfunction_fuzzer.h"
21 #include "v1_0/iusb_interface.h"
22
23 using namespace OHOS::HDI::Usb::V1_0;
24
25 namespace OHOS {
26 namespace USB {
UsbInterruptTransferReadFuzzTest(const uint8_t * data,size_t size)27 bool UsbInterruptTransferReadFuzzTest(const uint8_t *data, size_t size)
28 {
29 (void)size;
30 UsbDev dev;
31 sptr<IUsbInterface> usbInterface = IUsbInterface::Get();
32 int32_t ret = UsbFuzzTestHostModeInit(dev, usbInterface);
33 if (ret != HDF_SUCCESS) {
34 HDF_LOGE("%{public}s: UsbFuzzTestHostModeInit failed", __func__);
35 return false;
36 }
37
38 UsbPipe pipe;
39 if (memcpy_s((void *)&pipe, sizeof(pipe), data, sizeof(pipe)) != EOK) {
40 HDF_LOGE("%{public}s: memcpy_s failed", __func__);
41 return false;
42 }
43
44 int32_t timeout = *(reinterpret_cast<int32_t *>(*data));
45 ret = usbInterface->InterruptTransferRead(dev, pipe, timeout, reinterpret_cast<std::vector<uint8_t> &>(data));
46 if (ret == HDF_SUCCESS) {
47 HDF_LOGI("%{public}s: interrupt transfer read succeed", __func__);
48 }
49
50 ret = usbInterface->CloseDevice(dev);
51 if (ret != HDF_SUCCESS) {
52 HDF_LOGE("%{public}s: close device failed", __func__);
53 return false;
54 }
55 return true;
56 }
57 } // namespace USB
58 } // namespace OHOS
59
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
61 {
62 OHOS::USB::UsbInterruptTransferReadFuzzTest(data, size);
63 return 0;
64 }