1 /*
2 * Copyright (c) 2025 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 "usbgetdevices_fuzzer.h"
17 #include "hdf_log.h"
18 #include "securec.h"
19 #include "v1_1/iusb_ddk.h"
20
21 using namespace OHOS::HDI::Usb::Ddk;
22
23 namespace OHOS {
24 namespace USB {
UsbGetDevicesTest(const uint8_t * data,size_t size)25 bool UsbGetDevicesTest(const uint8_t *data, size_t size)
26 {
27 sptr<V1_1::IUsbDdk> usbDdk = V1_1::IUsbDdk::Get();
28 if (usbDdk == nullptr) {
29 return false;
30 }
31
32 size_t numElements = size / sizeof(uint64_t);
33 std::vector<uint64_t> devices;
34 devices.reserve(numElements);
35
36 for (size_t i = 0; i < numElements; ++i) {
37 uint64_t value;
38 std::copy(data + i * sizeof(uint64_t),
39 data + (i + 1) * sizeof(uint64_t),
40 reinterpret_cast<uint8_t*>(&value));
41 devices.push_back(value);
42 }
43
44 int ret = usbDdk->GetDevices(devices);
45 if (ret != HDF_SUCCESS) {
46 return false;
47 }
48 HDF_LOGI("%{public}s: get devices succeed", __func__);
49 return true;
50 }
51 } // namespace USB
52 } // namespace OHOS
53
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
55 {
56 OHOS::USB::UsbGetDevicesTest(data, size);
57 return 0;
58 }