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 "usbmgrgetports_fuzzer.h"
17 #include "usb_srv_client.h"
18 #include "usb_errors.h"
19 namespace OHOS {
20 namespace USB {
UsbMgrGetPortsFuzzTest(const uint8_t * data,size_t size)21 bool UsbMgrGetPortsFuzzTest(const uint8_t* data, size_t size)
22 {
23 unsigned seed = 0;
24 if (size >= sizeof(unsigned)) {
25 errno_t ret = memcpy_s(&seed, sizeof(unsigned), data, sizeof(unsigned));
26 if (ret != UEC_OK) {
27 return false;
28 }
29 srand(seed);
30 }
31 auto &usbSrvClient = UsbSrvClient::GetInstance();
32 std::vector<UsbPort> portlist;
33 portlist.clear();
34 int32_t ret = usbSrvClient.GetPorts(portlist);
35 if (ret == UEC_OK) {
36 if (portlist.empty()) {
37 USB_HILOGW(MODULE_USB_SERVICE, "GetPorts returned empty list");
38 }
39 return true;
40 }
41 if (!portlist.empty()) {
42 USB_HILOGE(MODULE_USB_SERVICE, "GetPorts failed but portlist not empty");
43 }
44 return false;
45 }
46 }
47 }
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)48 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
49 {
50 OHOS::USB::UsbMgrGetPortsFuzzTest(data, size);
51 return 0;
52 }
53
54