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 "usbrequestwait_fuzzer.h"
17 #include "UsbSubscriberTest.h"
18 #include "hdf_log.h"
19 #include "usbcommonfunction_fuzzer.h"
20 #include "v1_0/iusb_interface.h"
21 
22 using namespace OHOS::HDI::Usb::V1_0;
23 
24 namespace OHOS {
25 constexpr size_t THRESHOLD = 10;
26 constexpr int32_t OFFSET = 4;
27 constexpr int32_t OFFSET_BYTE = 8;
28 namespace USB {
UsbRequestWaitFuzzTest(const uint8_t * data,size_t size)29 bool UsbRequestWaitFuzzTest(const uint8_t *data, size_t size)
30 {
31     (void)size;
32     UsbDev dev;
33     sptr<IUsbInterface> usbInterface = IUsbInterface::Get();
34     int32_t ret = UsbFuzzTestHostModeInit(dev, usbInterface);
35     if (ret != HDF_SUCCESS) {
36         HDF_LOGE("%{public}s: UsbFuzzTestHostModeInit failed", __func__);
37         return false;
38     }
39 
40     ret = usbInterface->RequestWait(dev, reinterpret_cast<std::vector<uint8_t> &>(data),
41         reinterpret_cast<std::vector<uint8_t> &>(std::move(data + OFFSET)),
42         *(reinterpret_cast<int32_t *>(*(data + OFFSET_BYTE))));
43     if (ret == HDF_SUCCESS) {
44         HDF_LOGI("%{public}s: request wait succeed", __func__);
45     }
46 
47     ret = usbInterface->CloseDevice(dev);
48     if (ret != HDF_SUCCESS) {
49         HDF_LOGE("%{public}s: close device failed", __func__);
50         return false;
51     }
52     return true;
53 }
54 } // namespace USB
55 } // namespace OHOS
56 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
58 {
59     if (size < OHOS::THRESHOLD) {
60         return 0;
61     }
62     OHOS::USB::UsbRequestWaitFuzzTest(data, size);
63     return 0;
64 }