diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index 159d0f4482132dfcf2970b6b428db76b16dea5cf..547bc1b37da7476c6ede27ae8e08dba120812468 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -2959,6 +2959,7 @@ int32_t UsbService::AddSerialRight(uint32_t tokenId, int32_t portId) } // LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::GetDeviceVidPidSerialNumber(int32_t portId, std::string& deviceName, std::string& strDesc) { USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: Start", __func__); @@ -2975,6 +2976,7 @@ int32_t UsbService::GetDeviceVidPidSerialNumber(int32_t portId, std::string& dev } return isMatched; } +// LCOV_EXCL_STOP // LCOV_EXCL_START void UsbService::UpdateDeviceVidPidMap(std::vector& serialPortList) diff --git a/test/fuzztest/usbmgraddserialright_fuzzer/corpus/init b/test/fuzztest/usbmgraddserialright_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/usbmgraddserialright_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/usbmgraddserialright_fuzzer/project.xml b/test/fuzztest/usbmgraddserialright_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..20dc766db73742058c8300227a37ba63703fc683 --- /dev/null +++ b/test/fuzztest/usbmgraddserialright_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/test/fuzztest/usbmgraddserialright_fuzzer/usbmgraddserialright_fuzzer.cpp b/test/fuzztest/usbmgraddserialright_fuzzer/usbmgraddserialright_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4e40244731d90c5b10b49d351b0dfae3276094c --- /dev/null +++ b/test/fuzztest/usbmgraddserialright_fuzzer/usbmgraddserialright_fuzzer.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "usbmgraddserialright_fuzzer.h" + +#include "usb_srv_client.h" +#include "usb_errors.h" + +namespace OHOS { +namespace USB { + bool UsbMgrAddSerialRightFuzzTest(const uint8_t* data, size_t size) + { + auto &usbSrvClient = UsbSrvClient::GetInstance(); + if (data == nullptr || size < sizeof(int32_t) + sizeof(uint32_t)) { + return false; + } + int32_t ret = usbSrvClient.AddSerialRight(*reinterpret_cast(data), + *reinterpret_cast(data + sizeof(uint32_t))); + if (ret == UEC_OK) { + return false; + } + return true; + } +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::USB::UsbMgrAddSerialRightFuzzTest(data, size); + return 0; +} + diff --git a/test/fuzztest/usbmgraddserialright_fuzzer/usbmgraddserialright_fuzzer.h b/test/fuzztest/usbmgraddserialright_fuzzer/usbmgraddserialright_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..29566224b53ed2f49aa4b17d87800f1f1ab54c83 --- /dev/null +++ b/test/fuzztest/usbmgraddserialright_fuzzer/usbmgraddserialright_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USBMGRADDSERIALRIGHT_FUZZER_H +#define USBMGRADDSERIALRIGHT_FUZZER_H + +#define FUZZ_PROJECT_NAME "usbmgraddserialright_fuzzer" + +#endif // USBMGRADDSERIALRIGHT_FUZZER_H diff --git a/test/fuzztest/usbmgrserialsetattribute_fuzzer/usbmgrserialsetattribute_fuzzer.cpp b/test/fuzztest/usbmgrserialsetattribute_fuzzer/usbmgrserialsetattribute_fuzzer.cpp index 779730d78b6c90eaa2f72a433ab514518fec251a..86408c24f4f639a96b5435cba3ae77be59ca6564 100644 --- a/test/fuzztest/usbmgrserialsetattribute_fuzzer/usbmgrserialsetattribute_fuzzer.cpp +++ b/test/fuzztest/usbmgrserialsetattribute_fuzzer/usbmgrserialsetattribute_fuzzer.cpp @@ -23,17 +23,17 @@ constexpr int32_t OK = 0; } using OHOS::USB::UsbSrvClient; namespace OHOS { +const uint32_t OFFSET = 4; namespace SERIAL { bool UsbMgrSerialSetAttributeFuzzTest(const uint8_t* data, size_t size) { - if (data == nullptr || size < sizeof(int32_t)) { + if (data == nullptr || size < sizeof(UsbSerialAttr) + OFFSET) { return false; } auto &usbSrvClient = UsbSrvClient::GetInstance(); - struct UsbSerialAttr info; int32_t portId = *reinterpret_cast(data); - if (usbSrvClient.SerialGetAttribute(portId, info) != OK) { + if (usbSrvClient.SerialSetAttribute(portId, reinterpret_cast(std::move(data + OFFSET))) != OK) { return false; }