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 "usbopendevice_fuzzer.h"
17#include "hdf_log.h"
18#include "securec.h"
19#include "usbcommonfunction_fuzzer.h"
20#include "v1_0/iusb_interface.h"
21
22using namespace OHOS::HDI::Usb::V1_0;
23
24namespace OHOS {
25namespace USB {
26bool UsbOpenDeviceFuzzTest(const uint8_t *data, size_t size)
27{
28    (void)size;
29    sptr<IUsbInterface> usbInterface = IUsbInterface::Get();
30    int32_t ret = usbInterface->SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_HOST, DEFAULT_ROLE_HOST);
31    sleep(SLEEP_TIME);
32    if (ret != HDF_SUCCESS) {
33        HDF_LOGE("%{public}s: set port role as host failed", __func__);
34        return false;
35    }
36
37    UsbDev dev;
38    if (memcpy_s((void *)&dev, sizeof(dev), data, sizeof(dev)) != EOK) {
39        HDF_LOGE("%{public}s: memcpy_s failed", __func__);
40        return false;
41    }
42
43    ret = usbInterface->OpenDevice(dev);
44    if (ret == HDF_SUCCESS) {
45        HDF_LOGI("%{public}s: open device succeed", __func__);
46        ret = usbInterface->CloseDevice(dev);
47        if (ret == HDF_SUCCESS) {
48            HDF_LOGI("%{public}s: close device succeed", __func__);
49        }
50    }
51
52    return true;
53}
54} // namespace USB
55} // namespace OHOS
56
57extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
58{
59    OHOS::USB::UsbOpenDeviceFuzzTest(data, size);
60    return 0;
61}