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 "usbserialsetbaudrate_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
GetBaudRateFromData(const uint8_t * data,size_t size)31 static uint32_t GetBaudRateFromData(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 return baudRate;
42 }
43
UsbSerialSetBaudRateFuzzTest(const uint8_t * data,size_t size)44 bool UsbSerialSetBaudRateFuzzTest(const uint8_t *data, size_t size)
45 {
46 sptr<IUsbSerialDdk> usbSerialInterface = IUsbSerialDdk::Get();
47 int32_t ret = usbSerialInterface->Init();
48 if (ret != HDF_SUCCESS) {
49 HDF_LOGE("%s: init failed, ret = %d", __func__, ret);
50 return false;
51 }
52
53 UsbSerialDeviceHandle* device = nullptr;
54 ret = UsbSerialFuzzTestHostModeInit(usbSerialInterface, device);
55 if (ret != HDF_SUCCESS) {
56 HDF_LOGE("%s: UsbSerial open device failed, ret = %d", __func__, ret);
57 return false;
58 }
59
60 uint32_t baudRate = GetBaudRateFromData(data, size);
61 ret = usbSerialInterface->SetBaudRate(*device, baudRate);
62 if (ret != HDF_SUCCESS) {
63 HDF_LOGE("%{public}s: SetBaudRate failed", __func__);
64 return false;
65 }
66
67 ret = usbSerialInterface->Close(*device);
68 if (ret != HDF_SUCCESS) {
69 HDF_LOGE("%{public}s: close device failed", __func__);
70 usbSerialInterface->Release();
71 return false;
72 }
73
74 ret = usbSerialInterface->Release();
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%{public}s: release failed", __func__);
77 return false;
78 }
79 return true;
80 }
81 } // namespace USBSerial
82 } // namespace OHOS
83
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86 if (size < OHOS::THRESHOLD) {
87 return 0;
88 }
89 OHOS::USBSerial::UsbSerialSetBaudRateFuzzTest(data, size);
90 return 0;
91 }