diff --git a/wifi/bundle.json b/wifi/bundle.json index c5411b742..2445df166 100644 --- a/wifi/bundle.json +++ b/wifi/bundle.json @@ -103,6 +103,7 @@ } ], "test": [ + "//foundation/communication/wifi/wifi/test/fuzztest/wifi_manage:fuzztest", "//foundation/communication/wifi/wifi/test/fuzztest/wifi_sta:fuzztest", "//foundation/communication/wifi/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_scan:unittest", "//foundation/communication/wifi/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_sta:unittest", diff --git a/wifi/test/fuzztest/wifi_manage/BUILD.gn b/wifi/test/fuzztest/wifi_manage/BUILD.gn new file mode 100644 index 000000000..2890eb501 --- /dev/null +++ b/wifi/test/fuzztest/wifi_manage/BUILD.gn @@ -0,0 +1,22 @@ +# 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/test.gni") +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + "wifip2pstub_fuzzer:WifiP2pStubFuzzTest", + ] +} diff --git a/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/BUILD.gn b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/BUILD.gn new file mode 100644 index 000000000..f4cb598d8 --- /dev/null +++ b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/BUILD.gn @@ -0,0 +1,59 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/communication/wifi/wifi/wifi.gni") +module_output_path = "wifi/wifi_manage" + +##############################fuzztest########################################## +ohos_fuzztest("WifiP2pStubFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "$WIFI_ROOT_DIR/test/fuzztest/wifi_manage/wifip2pstub_fuzzer" + + include_dirs = [ + "$WIFI_ROOT_DIR/interfaces/inner_api", + "$WIFI_ROOT_DIR/frameworks/native/include", + "$WIFI_ROOT_DIR/frameworks/native/interfaces", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage", + "$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper", + "$WIFI_ROOT_DIR/test/fuzztest/fuzz_common_func", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "wifip2pstub_fuzzer.cpp" ] + + deps = [ + "$WIFI_ROOT_DIR/frameworks/native:wifi_sdk", + "$WIFI_ROOT_DIR/service/wifi_standard/wifi_manage:wifi_manager_service", + "$WIFI_ROOT_DIR/service/wifi_standard/wifi_manage/wifi_scan:wifi_scan_service", + "$WIFI_ROOT_DIR/service/wifi_standard/wifi_manage:wifi_p2p_ability", + "$WIFI_ROOT_DIR/service/wifi_standard/wifi_manage:wifi_p2p_servce_impl", + "$WIFI_ROOT_DIR/service/wifi_standard/wifi_toolkit:wifi_toolkit", + "$WIFI_ROOT_DIR/utils:wifi_utils, + ] + + external_deps = [ + "c_utils:utils", + "ipc:ipc_single", + ] + + part_name = "wifi" + subsystem_name = "communication" +} diff --git a/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/corpus/init b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/corpus/init new file mode 100644 index 000000000..ece6c3f66 --- /dev/null +++ b/wifi/test/fuzztest/wifi_manage/wifip2pstub_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/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/project.xml b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/project.xml new file mode 100644 index 000000000..5e3b584f3 --- /dev/null +++ b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/wifip2pstub_fuzzer.cpp b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/wifip2pstub_fuzzer.cpp new file mode 100644 index 000000000..a2e99fe6d --- /dev/null +++ b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/wifip2pstub_fuzzer.cpp @@ -0,0 +1,297 @@ +/* + * 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 "wifip2pstub_fuzzer.h" + +#include +#include + +#include "wifi_p2p_stub.h" +#include "message_parcel.h" +#include "securec.h" +#include "define.h" + +namespace OHOS { +namespace Wifi { +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; +const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IWifi.IWifiP2pService"; +std::shared_ptr pWifiP2pStub = std::make_shared(); + +class WifiP2pStubTest : public WifiP2pStub { +public: + WifiP2pStubTest() = default; + virtual ~WifiP2pStubTest() = default; + ErrCode MonitorCfgChange() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode EnableP2p() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode DisableP2p() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode DiscoverDevices() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode StopDiscoverDevices() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode DiscoverServices() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode StopDiscoverServices() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode RequestService(const WifiP2pDevice &device, const WifiP2pServiceRequest &request) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode PutLocalP2pService(const WifiP2pServiceInfo &srvInfo) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode DeleteLocalP2pService(const WifiP2pServiceInfo &srvInfo) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode StartP2pListen(int period, int interval) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode StopP2pListen() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode CreateGroup(const WifiP2pConfig &config) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode RemoveGroup() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode DeleteGroup(const WifiP2pGroupInfo &group) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode P2pConnect(const WifiP2pConfig &config) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode P2pCancelConnect() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode QueryP2pLinkedInfo(WifiP2pLinkedInfo& linkedInfo) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode GetCurrentGroup(WifiP2pGroupInfo &group) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode GetP2pEnableStatus(int &status) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode GetP2pDiscoverStatus(int &status) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode GetP2pDiscoverStatus(int &status) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode QueryP2pDevices(std::vector &devices) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode QueryP2pLocalDevice(WifiP2pDevice &device) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode QueryP2pGroups(std::vector &groups) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode QueryP2pServices(std::vector &services) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode RegisterCallBack(const sptr &callback, const std::vector &event) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode GetSupportedFeatures(long &features) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode SetP2pDeviceName(const std::string &deviceName) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dSharedlinkIncrease() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dSharedlinkDecrease() override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dCreateGroup(const int frequency, FreqType type) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dConnect(const Hid2dConnectConfig& config) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dReleaseIPAddr(const std::string& ifName) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dGetRecommendChannel(const RecommendChannelRequest& request, + RecommendChannelResponse& response) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dGetChannelListFor5G(std::vector& vecChannelList) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType, + char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType, + char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen) override + { + return WIFI_OPT_SUCCESS; + } + + ErrCode Hid2dSetUpperScene(const std::string& ifName, const Hid2dUpperScene& scene) override + { + return WIFI_OPT_SUCCESS; + } + + bool IsRemoteDied() override + { + return WIFI_OPT_SUCCESS; + } +}; + +void OnEnableP2pTest(const char* data, size_t size) +{ + MessageParcel datas; + datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + pWifiP2pStub->OnRemoteRequest(WIFI_SVR_CMD_P2P_ENABLE, datas, reply, option); +} + +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + OnEnableP2pTest(); + return true; +} + +}; +}; + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + if (size < OHOS::Wifi::U32_AT_SIZE) { + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::Wifi::FOO_MAX_LEN) { + return 0; + } + + OHOS::Wifi::DoSomethingInterestingWithMyAPI(ch, size); + return 0; +} + + diff --git a/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/wifip2pstub_fuzzer.h b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/wifip2pstub_fuzzer.h new file mode 100644 index 000000000..b8de7ffa3 --- /dev/null +++ b/wifi/test/fuzztest/wifi_manage/wifip2pstub_fuzzer/wifip2pstub_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 WIFI_FUZZ_P2P_STUB_H +#define WIFI_FUZZ_P2P_STUB_H + +#define FUZZ_PROJECT_NAME "wifip2pstub_fuzzer" + +#endif