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 "usbbulktransferread_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 constexpr size_t THRESHOLD = 10;
27 constexpr int32_t OFFSET = 4;
28 constexpr int32_t OFFSET_BYTE = 8;
29 namespace USB {
UsbBulkTransferReadFuzzTest(const uint8_t * data,size_t size)30 bool UsbBulkTransferReadFuzzTest(const uint8_t *data, size_t size)
31 {
32 (void)size;
33 UsbDev dev;
34 sptr<IUsbInterface> usbInterface = IUsbInterface::Get();
35 int32_t ret = UsbFuzzTestHostModeInit(dev, usbInterface);
36 if (ret != HDF_SUCCESS) {
37 HDF_LOGE("%{public}s: UsbFuzzTestHostModeInit failed", __func__);
38 return false;
39 }
40
41 UsbPipe pipe;
42 if (memcpy_s((void *)&pipe, sizeof(pipe), data, sizeof(pipe)) != EOK) {
43 HDF_LOGE("%{public}s: memcpy_s failed", __func__);
44 return false;
45 }
46 int32_t timeout = *(reinterpret_cast<int32_t *>(*(data + OFFSET)));
47 ret = usbInterface->BulkTransferRead(
48 dev, pipe, timeout, reinterpret_cast<std::vector<uint8_t> &>(std::move(data + OFFSET_BYTE)));
49 if (ret == HDF_SUCCESS) {
50 HDF_LOGI("%{public}s: bulk transfer read succeed", __func__);
51 }
52
53 ret = usbInterface->CloseDevice(dev);
54 if (ret != HDF_SUCCESS) {
55 HDF_LOGE("%{public}s: close device failed", __func__);
56 return false;
57 }
58 return true;
59 }
60 } // namespace USB
61 } // namespace OHOS
62
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65 if (size < OHOS::THRESHOLD) {
66 return 0;
67 }
68 OHOS::USB::UsbBulkTransferReadFuzzTest(data, size);
69 return 0;
70 }
71