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 "usbserialsetflowcontrol_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "hdf_log.h"
21 #include "usbserialcommonfunction_fuzzer.h"
22
23 using namespace OHOS::HDI::Usb::UsbSerialDdk::V1_0;
24
25 namespace OHOS {
26 constexpr size_t THRESHOLD = 10;
27
28 namespace USBSerial {
29 constexpr uint32_t DEFAULT_BAUD_RATE = 9600;
30
GetFlowControl(const uint8_t * data,size_t size)31 static uint32_t GetFlowControl(const uint8_t* data, size_t size)
32 {
33 if (data == nullptr || size < sizeof(uint32_t)) {
34 HDF_LOGE("Invalid input data or insufficient size.");
35 return DEFAULT_BAUD_RATE;
36 }
37
38 uint32_t baudRate = static_cast<uint32_t>(data[0]) | (static_cast<uint32_t>(data[1]) << SECOND_MOVE_LEN) |
39 (static_cast<uint32_t>(data[SECOND_BIT]) << FIRST_MOVE_LEN) |
40 (static_cast<uint32_t>(data[THIRD_BIT]) << ZERO_MOVE_LEN);
41
42 return baudRate;
43 }
44
UsbSerialSetBaudRateFuzzTest(const uint8_t * data,size_t size)45 bool UsbSerialSetBaudRateFuzzTest(const uint8_t *data, size_t size)
46 {
47 sptr<IUsbSerialDdk> usbSerialInterface = IUsbSerialDdk::Get();
48 int32_t ret = usbSerialInterface->Init();
49 if (ret != HDF_SUCCESS) {
50 HDF_LOGE("%s: init failed, ret = %d", __func__, ret);
51 return false;
52 }
53
54 UsbSerialDeviceHandle* device = nullptr;
55 ret = UsbSerialFuzzTestHostModeInit(usbSerialInterface, device);
56 if (ret != HDF_SUCCESS) {
57 HDF_LOGE("%s: UsbSerial open device failed, ret = %d", __func__, ret);
58 return false;
59 }
60
61 enum UsbSerialFlowControl flowControl = static_cast<UsbSerialFlowControl>(GetFlowControl(data, size));
62 ret = usbSerialInterface->SetFlowControl(*device, flowControl);
63 if (ret != HDF_SUCCESS) {
64 usbSerialInterface->Release();
65 HDF_LOGE("%{public}s: SetBaudRate failed", __func__);
66 return false;
67 }
68
69 ret = usbSerialInterface->Close(*device);
70 if (ret != HDF_SUCCESS) {
71 HDF_LOGE("%{public}s: close device failed", __func__);
72 return false;
73 }
74
75 ret = usbSerialInterface->Release();
76 if (ret != HDF_SUCCESS) {
77 HDF_LOGE("%{public}s: release failed", __func__);
78 return false;
79 }
80 return true;
81 }
82 } // namespace USBSerial
83 } // namespace OHOS
84
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87 if (size < OHOS::THRESHOLD) {
88 return 0;
89 }
90 OHOS::USBSerial::UsbSerialSetBaudRateFuzzTest(data, size);
91 return 0;
92 }