diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..f43b1e5148c470a9e166396b71f684c42739a585 --- /dev/null +++ b/test/fuzztest/BUILD.gn @@ -0,0 +1,24 @@ +# 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 + +import("//build/config/features.gni") +import("//build/test.gni") + +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + "bus_extension_fuzzer:bus_extension_fuzzer", + ] +} diff --git a/test/fuzztest/bus_extension_fuzzer/BUILD.gn b/test/fuzztest/bus_extension_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..5a1a6cd2e035f8dbc200b4359b7c1ded41b91390 --- /dev/null +++ b/test/fuzztest/bus_extension_fuzzer/BUILD.gn @@ -0,0 +1,24 @@ +# 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 + +import("//build/config/features.gni") +import("//build/test.gni") + +group("bus_extension_fuzzer") { + testonly = true + deps = [] + + deps += [ + "usbdriverinfo_fuzzer:UsbDriverInfoFuzzTest", + ] +} diff --git a/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/BUILD.gn b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1311487b714db007b03a644183e08e1b613da4e7 --- /dev/null +++ b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/BUILD.gn @@ -0,0 +1,41 @@ +# 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. +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//drivers/external_device_manager/extdevmgr.gni") + +module_output_path = "${part_name}/fuzztest" +ohos_fuzztest("UsbDriverInfoFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//drivers/external_device_manager/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer" + + sources = [ + "usbdriverinfo_fuzzer.cpp", + ] + include_dirs = [ + + ] + deps = [ + "${part_path}/framework/bus_extension:bus_extension", + ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "bundle_framework:appexecfwk_base", + ] + defines = [ ] + configs = [ + "${utils_path}:utils_config", + ] +} diff --git a/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/corpus/init b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/bus_extension_fuzzer/usbdriverinfo_fuzzer/project.xml b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..d37d5e342f7208f586de9ef17485551381dc16b6 --- /dev/null +++ b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 20 + + 2048 + + \ No newline at end of file diff --git a/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/usbdriverinfo_fuzzer.cpp b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/usbdriverinfo_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de3fed7cc3e87dfcaf6ed4c04f6e759f13ef2a22 --- /dev/null +++ b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/usbdriverinfo_fuzzer.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2023 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 "iostream" +#include "string" +#include "securec.h" +#include "ibus_extension.h" +#include "usb_driver_info.h" +#include "hilog_wrapper.h" +#include "usbdriverinfo_fuzzer.h" +namespace OHOS { +namespace ExtDevMgr { +using namespace std; + +bool IBusGetFuzzer(const uint8_t *data, size_t size) +{ + string str(reinterpret_cast(data)); + auto bus = IBusExtension::GetInstance(str); + return true; +} + +bool DriverInfoUnSerializeFuzzer(const uint8_t *data, size_t size) +{ + string drvInfoStr(reinterpret_cast(data)); + if (drvInfoStr.size() == 0) { + return false; + } + + DriverInfo devInfo; + devInfo.UnSerialize(drvInfoStr); + UsbDriverInfo usbDevInfo; + usbDevInfo.UnSerialize(drvInfoStr); + return true; +} +bool ParseDriverInfoTest(const uint8_t *data, size_t size) +{ + auto bus = IBusExtension::GetInstance("USB"); + string str(reinterpret_cast(data)); + Metadata vids; + Metadata pids; + vids.name = "vid"; + vids.value = str; + pids.name = "pid"; + pids.value = str; + vector metadata = {vids, pids}; + auto ret = bus->ParseDriverInfo(metadata); + return true; +} + +using TestFuncDef = bool (*)(const uint8_t *data, size_t size); + +TestFuncDef g_allTestFunc[] = { + IBusGetFuzzer, + DriverInfoUnSerializeFuzzer, + ParseDriverInfoTest, +}; + +bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size) +{ + if (size < sizeof(int)) { + return false; + } + int index = *(reinterpret_cast(rawData)); + rawData += sizeof(int); + size -= sizeof(int); + int funcCount = sizeof(g_allTestFunc) / sizeof(g_allTestFunc[0]); + + auto func = g_allTestFunc[index % funcCount]; + if (func != nullptr) { + auto ret = func(rawData, size); + return ret; + } + return false; +} +} // namespace ExtDevMgr +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + OHOS::ExtDevMgr::DoSomethingInterestingWithMyAPI(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/usbdriverinfo_fuzzer.h b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/usbdriverinfo_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..71f3d1e00d0485ea76b8867a4251b15b892427cf --- /dev/null +++ b/test/fuzztest/bus_extension_fuzzer/usbdriverinfo_fuzzer/usbdriverinfo_fuzzer.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 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 USB_DRIVER_INFO_FUZZER +#define USB_DRIVER_INFO_FUZZER +#define FUZZ_PROJECT_NAME "usbdriverinfo_fuzzer" + +#endif \ No newline at end of file