From 629ebd62cf241075437ccb6b46bdd85a76db5864 Mon Sep 17 00:00:00 2001 From: benlau Date: Sun, 17 Aug 2025 14:35:54 +0800 Subject: [PATCH] fix cfi check fail crash and add dt/fuzz Signed-off-by: benlau --- ethernet/bundle.json | 3 + ethernet/eth_client/BUILD.gn | 1 + ethernet/eth_interfaces/hdi_service/BUILD.gn | 7 +- .../hdi_service/libethernet_hdi_service.map | 23 ++++ .../hdi_service/service_common/hdi_wpa_hal.c | 2 +- .../hdi_service/service_common/hdi_wpa_hal.h | 2 +- .../service_common/wpa_common_cmd.c | 2 +- ethernet/test/BUILD.gn | 26 ++++ ethernet/test/fuzztest/BUILD.gn | 67 ++++++++++ ethernet/test/fuzztest/corpus/init | 16 +++ ethernet/test/fuzztest/ethernet_fuzzer.cpp | 125 ++++++++++++++++++ ethernet/test/fuzztest/project.xml | 25 ++++ ethernet/test/unittest/BUILD.gn | 67 ++++++++++ ethernet/test/unittest/eth_client_test.cpp | 110 +++++++++++++++ .../test/unittest/eth_hdi_services_c_test.cpp | 112 ++++++++++++++++ 15 files changed, 581 insertions(+), 7 deletions(-) create mode 100644 ethernet/eth_interfaces/hdi_service/libethernet_hdi_service.map create mode 100644 ethernet/test/BUILD.gn create mode 100644 ethernet/test/fuzztest/BUILD.gn create mode 100644 ethernet/test/fuzztest/corpus/init create mode 100644 ethernet/test/fuzztest/ethernet_fuzzer.cpp create mode 100644 ethernet/test/fuzztest/project.xml create mode 100644 ethernet/test/unittest/BUILD.gn create mode 100644 ethernet/test/unittest/eth_client_test.cpp create mode 100644 ethernet/test/unittest/eth_hdi_services_c_test.cpp diff --git a/ethernet/bundle.json b/ethernet/bundle.json index d6b82cd808..33aa0d8f95 100644 --- a/ethernet/bundle.json +++ b/ethernet/bundle.json @@ -43,6 +43,9 @@ "header_base": "//drivers/peripheral/ethernet/eth_client/include" } } + ], + "test": [ + "//drivers/peripheral/ethernet/test:hdf_test_ethernet" ] } } diff --git a/ethernet/eth_client/BUILD.gn b/ethernet/eth_client/BUILD.gn index bd4675bb4b..b00fc0cc9d 100644 --- a/ethernet/eth_client/BUILD.gn +++ b/ethernet/eth_client/BUILD.gn @@ -39,6 +39,7 @@ if (defined(ohos_lite)) { "-fno-strict-aliasing", "-Wno-format", "-Wno-format-extra-args", + "-fstack-protector-strong", ] subsystem_name = "hdf" diff --git a/ethernet/eth_interfaces/hdi_service/BUILD.gn b/ethernet/eth_interfaces/hdi_service/BUILD.gn index d8c3788c90..53a8df3c09 100644 --- a/ethernet/eth_interfaces/hdi_service/BUILD.gn +++ b/ethernet/eth_interfaces/hdi_service/BUILD.gn @@ -46,10 +46,7 @@ if (is_standard_system) { "-fsigned-char", "-fno-common", "-fno-strict-aliasing", - "-DCONFIG_CTRL_IFACE", - "-DCONFIG_P2P", - "-DCONFIG_WEP", - "-DCONFIG_WPS", + "-fstack-protector-strong", ] cflags += [ "-fdata-sections", @@ -75,6 +72,7 @@ if (is_standard_system) { install_images = [ chipset_base_dir, ] + version_script = "libethernet_hdi_service.map" subsystem_name = "hdf" part_name = "drivers_peripheral_ethernet" } @@ -103,6 +101,7 @@ if (is_standard_system) { "-fsigned-char", "-fno-common", "-fno-strict-aliasing", + "-fstack-protector-strong", ] cflags += [ "-fdata-sections", diff --git a/ethernet/eth_interfaces/hdi_service/libethernet_hdi_service.map b/ethernet/eth_interfaces/hdi_service/libethernet_hdi_service.map new file mode 100644 index 0000000000..30945a5bc5 --- /dev/null +++ b/ethernet/eth_interfaces/hdi_service/libethernet_hdi_service.map @@ -0,0 +1,23 @@ +# Copyright (c) 2025 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. + +1.0 { + global: + EthernetImplGetInstance; + HdfEthernetStubDriver; + HdfEthernetDriverInit; + HdfEthernetDriverBind; + HdfEthernetDriverRelease; + local: + *; +}; diff --git a/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.c b/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.c index c948bf0d8d..30c9d5448e 100644 --- a/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.c +++ b/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.c @@ -161,7 +161,7 @@ static void WpaCliClose(EthWpaInstance *p) return; } -static int WpaCliTerminate(void) +static int WpaCliTerminate(EthWpaInstance *p) { HDF_LOGI("enter WpaCliTerminate."); char cmd[WPA_CMD_BUF_LEN] = {0}; diff --git a/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.h b/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.h index 2e1fa0e40f..9ddf1b976a 100644 --- a/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.h +++ b/ethernet/eth_interfaces/hdi_service/service_common/hdi_wpa_hal.h @@ -40,7 +40,7 @@ struct EthWpaInstance { pthread_t tid; int (*wpaCliConnect)(EthWpaInstance *p); void (*wpaCliClose)(EthWpaInstance *p); - int (*wpaCliTerminate)(); + int (*wpaCliTerminate)(EthWpaInstance *p); int (*wpaCliCmdSetNetwork)(EthWpaInstance *p, const char *ifName, const char *name, const char *value); int (*wpaCliCmdStaShellCmd)(EthWpaInstance *p, const char *ifName, const char *params); }; diff --git a/ethernet/eth_interfaces/hdi_service/service_common/wpa_common_cmd.c b/ethernet/eth_interfaces/hdi_service/service_common/wpa_common_cmd.c index c4ef40c6a8..c6534c012b 100644 --- a/ethernet/eth_interfaces/hdi_service/service_common/wpa_common_cmd.c +++ b/ethernet/eth_interfaces/hdi_service/service_common/wpa_common_cmd.c @@ -225,7 +225,7 @@ static int32_t StopWpaSupplicant(void) HDF_LOGE("%{public}s: Get wpa global interface failed!", __func__); return HDF_FAILURE; } - int ret = pWpaInstance->wpaCliTerminate(); + int ret = pWpaInstance->wpaCliTerminate(pWpaInstance); HDF_LOGI("%{public}s: wpaCliTerminate ret = %{public}d", __func__, ret); return HDF_SUCCESS; } diff --git a/ethernet/test/BUILD.gn b/ethernet/test/BUILD.gn new file mode 100644 index 0000000000..a61c4dc6ad --- /dev/null +++ b/ethernet/test/BUILD.gn @@ -0,0 +1,26 @@ +# Copyright (c) 2025 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. + +if (is_standard_system) { + import("//build/ohos.gni") +} + +group("hdf_test_ethernet") { + testonly = true + if (defined(global_parts_info) && defined(global_parts_info.hdf_drivers_peripheral_ethernet)) { + deps = [ + "fuzztest:hdf_ethernet_fuzztest", + "unittest:hdf_ethernet_unittest", + ] + } +} diff --git a/ethernet/test/fuzztest/BUILD.gn b/ethernet/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000..027d27ab93 --- /dev/null +++ b/ethernet/test/fuzztest/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (c) 2025 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") +import("//third_party/wpa_supplicant/wpa_supplicant-2.9_standard/wpa.gni") +import("../../../../hdf_core/adapter/uhdf2/uhdf.gni") + +ETHERNET_ROOT_DIR = "../../.." + +ohos_fuzztest("EthernetFuzzTest") { + module_out_path = "drivers_peripheral_ethernet/drivers_peripheral_ethernet" + + include_dirs = [ + "$ETHERNET_ROOT_DIR/eth_interfaces/hdi_service/service_common", + "$ETHERNET_ROOT_DIR/eth_client/include", + "//third_party/wpa_supplicant/wpa_supplicant-2.9_standard", + "//third_party/wpa_supplicant/wpa_supplicant-2.9_standard/src", + "//third_party/wpa_supplicant/wpa_supplicant-2.9_standard/src/utils", + "//third_party/wpa_supplicant/wpa_supplicant-2.9_standard/wpa_supplicant", + "//third_party/wpa_supplicant/wpa_supplicant-2.9_standard/wpa_supplicant_lib", + ] + + defines = [ "__OHOS__USER__" ] + + external_deps = [ + "c_utils:utils", + "drivers_interface_ethernet:libethernet_proxy_1.0", + "drivers_interface_ethernet:libethernet_stub_1.0", + "hdf_core:libhdf_host", + "hdf_core:libhdf_ipc_adapter", + "hdf_core:libhdf_utils", + "hdf_core:libhdi", + "hdf_core:libpub_utils", + "hilog:libhilog", + "wpa_supplicant:wpa", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-fno-strict-aliasing", + "-fstack-protector-strong", + ] + + public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ] + + sources = [ + "ethernet_fuzzer.cpp", + ] +} + +group("hdf_ethernet_fuzztest") { + testonly = true + deps = [ ":EthernetFuzzTest" ] +} diff --git a/ethernet/test/fuzztest/corpus/init b/ethernet/test/fuzztest/corpus/init new file mode 100644 index 0000000000..498ab51bd4 --- /dev/null +++ b/ethernet/test/fuzztest/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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 diff --git a/ethernet/test/fuzztest/ethernet_fuzzer.cpp b/ethernet/test/fuzztest/ethernet_fuzzer.cpp new file mode 100644 index 0000000000..9d975e70d2 --- /dev/null +++ b/ethernet/test/fuzztest/ethernet_fuzzer.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2025 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 +#include +#include +#include + +#include "devmgr_hdi.h" +#include "hdf_remote_service.h" +#include "hdf_log.h" +#include "securec.h" +#include "servmgr_hdi.h" +#include "v1_0/iethernet.h" +#include "v1_0/iethernet_callback.h" + +#define FUZZ_PROJECT_NAME "ethernet_fuzzer" + +namespace OHOS { +namespace ETHERNET { + +static constexpr size_t THRESHOLD = 10; +static constexpr const char *ETH_SERVICE_NAME = "ethernet_service"; +static constexpr const char *ETH_IFNAME = "eth0"; +static struct IEthernet *g_ethInstance = nullptr; +static struct HDIDeviceManager *g_devMgr = nullptr; +static struct IEthernetCallback *g_wpaCallbackObj = nullptr; + +static void FuzzEthernetRegisterEapEventCallback() +{ + if (g_ethInstance == nullptr) { + HDF_LOGE("%{public}s: g_ethInstance is null", __FUNCTION__); + return; + } + g_ethInstance->RegisterEapEventCallback(g_ethInstance, g_wpaCallbackObj, ETH_IFNAME); + HDF_LOGI("%{public}s: success", __FUNCTION__); +} + +static void FuzzEthernetUnregisterEapEventCallback() +{ + if (g_ethInstance == nullptr) { + HDF_LOGE("%{public}s: g_ethInstance is null", __FUNCTION__); + return; + } + g_ethInstance->UnregisterEapEventCallback(g_ethInstance, g_wpaCallbackObj, ETH_IFNAME); + HDF_LOGI("%{public}s: success", __FUNCTION__); +} + +static void FuzzEthernetEapShellCmd() +{ + if (g_ethInstance == nullptr) { + HDF_LOGE("%{public}s: g_ethInstance is null", __FUNCTION__); + return; + } + std::string testCmd = "TEST"; + g_ethInstance->EapShellCmd(g_ethInstance, ETH_IFNAME, testCmd.c_str()); + HDF_LOGI("%{public}s: success", __FUNCTION__); +} + +static void ExecEthernetFuzzTest() +{ + FuzzEthernetRegisterEapEventCallback(); + FuzzEthernetUnregisterEapEventCallback(); + FuzzEthernetEapShellCmd(); +} + +static bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size) +{ + bool result = false; + g_devMgr = HDIDeviceManagerGet(); + if (g_devMgr == nullptr) { + HDF_LOGE("%{public}s : g_devMgr is null", __FUNCTION__); + return result; + } + int32_t rc = g_devMgr->LoadDevice(g_devMgr, ETH_SERVICE_NAME); + if (rc != HDF_SUCCESS) { + HDF_LOGE("%{public}s : LoadDevice fail", __FUNCTION__); + return result; + } + g_ethInstance = IEthernetGetInstance(ETH_SERVICE_NAME, true); + if (g_ethInstance == nullptr) { + HDF_LOGE("%{public}s : IEthernetGetInstance fail", __FUNCTION__); + return result; + } + int32_t ret = g_ethInstance->StartEap(g_ethInstance, ETH_IFNAME); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s : StartEap failed!", __FUNCTION__); + return result; + } + ExecEthernetFuzzTest(); + ret = g_ethInstance->StopEap(g_ethInstance, ETH_IFNAME); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s : StopEap failed!", __FUNCTION__); + result = false; + } + IEthernetReleaseInstance(ETH_SERVICE_NAME, g_ethInstance, true); + g_devMgr->UnloadDevice(g_devMgr, ETH_SERVICE_NAME); + HDIDeviceManagerRelease(g_devMgr); + g_devMgr = nullptr; + g_ethInstance = nullptr; + return result; +} +} // namespace ETHERNET +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + if (size >= OHOS::ETHERNET::THRESHOLD) { + OHOS::ETHERNET::DoSomethingInterestingWithMyAPI(data, size); + } + return 0; +} diff --git a/ethernet/test/fuzztest/project.xml b/ethernet/test/fuzztest/project.xml new file mode 100644 index 0000000000..fc8254d8f1 --- /dev/null +++ b/ethernet/test/fuzztest/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/ethernet/test/unittest/BUILD.gn b/ethernet/test/unittest/BUILD.gn new file mode 100644 index 0000000000..6eace860a1 --- /dev/null +++ b/ethernet/test/unittest/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (c) 2025 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") +import("../../../../hdf_core/adapter/uhdf2/uhdf.gni") + +ETHERNET_ROOT_DIR = "../../.." + +ohos_unittest("EthernetUnitTest") { + module_out_path = "drivers_peripheral_ethernet/drivers_peripheral_ethernet" + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + "-fstack-protector-strong", + ] + cflags_cc = [ "--coverage" ] + ldflags = [ "--coverage" ] + + include_dirs = [ + "$ETHERNET_ROOT_DIR/eth_interfaces/hdi_service/service_common", + "$ETHERNET_ROOT_DIR/eth_client/include", + ] + + sources = [ + "eth_client_test.cpp", + "eth_hdi_services_c_test.cpp", + ] + + deps = [ + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "../../eth_client:ethernet_hdi_client", + ] + + external_deps = [ + "c_utils:utils", + "drivers_interface_ethernet:libethernet_proxy_1.0", + "drivers_interface_ethernet:libethernet_stub_1.0", + "hdf_core:libhdf_host", + "hdf_core:libhdf_ipc_adapter", + "hdf_core:libhdf_utils", + "hdf_core:libhdi", + "hdf_core:libpub_utils", + "hilog:libhilog", + "wpa_supplicant:wpa", + ] +} + +group("hdf_ethernet_unittest") { + testonly = true + deps = [ ":EthernetUnitTest" ] +} diff --git a/ethernet/test/unittest/eth_client_test.cpp b/ethernet/test/unittest/eth_client_test.cpp new file mode 100644 index 0000000000..6cb241940a --- /dev/null +++ b/ethernet/test/unittest/eth_client_test.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2025 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 +#include + +#include "devmgr_hdi.h" +#include "ethernet_eap_client.h" +#include "hdf_remote_service.h" +#include "hdf_log.h" +#include "securec.h" +#include "servmgr_hdi.h" +#include "v1_0/iethernet.h" + +using namespace testing::ext; + +namespace HdiTest { + +static constexpr const char *IFNAME = "eth0"; +static constexpr const char *ETH_SERVICE_NAME = "ethernet_service"; +static struct IEthernet *g_ethInstance = nullptr; +static struct IEthernetCallback *g_ethCallback = nullptr; +static struct HDIDeviceManager *g_devMgr = nullptr; + +class EthClientTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void EthClientTest::SetUpTestCase() +{ + g_devMgr = HDIDeviceManagerGet(); + if (g_devMgr == nullptr) { + return; + } + int32_t rc = g_devMgr->LoadDevice(g_devMgr, ETH_SERVICE_NAME); + g_ethInstance = IEthernetGetInstance(ETH_SERVICE_NAME, false); + if (g_ethInstance == nullptr) { + return; + } + rc = g_ethInstance->StartEap(g_ethInstance, IFNAME); + g_ethCallback = (struct IEthernetCallback *)OsalMemCalloc(sizeof(struct IEthernetCallback)); +} + +void EthClientTest::TearDownTestCase() +{ + if (g_ethInstance != nullptr) { + g_ethInstance->StopEap(g_ethInstance, IFNAME); + IEthernetReleaseInstance(ETH_SERVICE_NAME, g_ethInstance, false); + } + if (g_ethCallback != nullptr) { + OsalMemFree(g_ethCallback); + } + if (g_devMgr != nullptr) { + g_devMgr->UnloadDevice(g_devMgr, ETH_SERVICE_NAME); + HDIDeviceManagerRelease(g_devMgr); + } + g_devMgr = nullptr; + g_ethCallback = nullptr; + g_ethInstance = nullptr; +} + +void EthClientTest::SetUp() +{ +} + +void EthClientTest::TearDown() +{ +} + +HWTEST_F(EthClientTest, EthEapClientEventReportTest001, TestSize.Level1) +{ + std::string data = "TEST"; + EthEapClientEventReport(IFNAME, data.c_str()); + EXPECT_NE(data.size(), 0); +} + +HWTEST_F(EthClientTest, EthEapClientRegisterCallbackTest001, TestSize.Level1) +{ + int32_t rc = EthEapClientRegisterCallback(g_ethCallback, IFNAME); + EXPECT_NE(rc, HDF_ERR_INVALID_PARAM); +} + +HWTEST_F(EthClientTest, EthEapClientUnregisterCallbackTest001, TestSize.Level1) +{ + int32_t rc = EthEapClientUnregisterCallback(g_ethCallback, IFNAME); + EXPECT_NE(rc, HDF_ERR_INVALID_PARAM); +} + +HWTEST_F(EthClientTest, EthEapClientReleaseCallbackTest001, TestSize.Level1) +{ + EthEapClientReleaseCallback(); + EXPECT_NE(g_ethInstance, nullptr); +} + +} diff --git a/ethernet/test/unittest/eth_hdi_services_c_test.cpp b/ethernet/test/unittest/eth_hdi_services_c_test.cpp new file mode 100644 index 0000000000..4c36ea59c8 --- /dev/null +++ b/ethernet/test/unittest/eth_hdi_services_c_test.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2025 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 +#include + +#include "devmgr_hdi.h" +#include "hdf_remote_service.h" +#include "hdf_log.h" +#include "securec.h" +#include "v1_0/iethernet.h" +#include "servmgr_hdi.h" + +using namespace testing::ext; + +namespace HdiTest { + +static constexpr const char *IFNAME = "eth0"; +static constexpr const char *ETH_SERVICE_NAME = "ethernet_service"; +static struct IEthernet *g_ethInstance = nullptr; +static struct IEthernetCallback *g_ethCallback = nullptr; +static struct HDIDeviceManager *g_devMgr = nullptr; + +class EthHdiServiceCTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void EthHdiServiceCTest::SetUpTestCase() +{ + g_devMgr = HDIDeviceManagerGet(); + if (g_devMgr == nullptr) { + return; + } + int32_t rc = g_devMgr->LoadDevice(g_devMgr, ETH_SERVICE_NAME); + g_ethInstance = IEthernetGetInstance(ETH_SERVICE_NAME, false); + if (g_ethInstance == nullptr) { + return; + } + rc = g_ethInstance->StartEap(g_ethInstance, IFNAME); + g_ethCallback = (struct IEthernetCallback *)OsalMemCalloc(sizeof(struct IEthernetCallback)); +} + +void EthHdiServiceCTest::TearDownTestCase() +{ + if (g_ethInstance != nullptr) { + g_ethInstance->StopEap(g_ethInstance, IFNAME); + IEthernetReleaseInstance(ETH_SERVICE_NAME, g_ethInstance, false); + } + if (g_ethCallback != nullptr) { + OsalMemFree(g_ethCallback); + } + if (g_devMgr != nullptr) { + g_devMgr->UnloadDevice(g_devMgr, ETH_SERVICE_NAME); + HDIDeviceManagerRelease(g_devMgr); + } + g_devMgr = nullptr; + g_ethCallback = nullptr; + g_ethInstance = nullptr; +} + +void EthHdiServiceCTest::SetUp() +{ +} + +void EthHdiServiceCTest::TearDown() +{ +} + +HWTEST_F(EthHdiServiceCTest, RegisterEventCallbackTest_001, TestSize.Level1) +{ + if (g_ethInstance == nullptr) { + return; + } + int32_t rc = g_ethInstance->RegisterEapEventCallback(g_ethInstance, g_ethCallback, IFNAME); + EXPECT_EQ(rc, HDF_SUCCESS); +} + +HWTEST_F(EthHdiServiceCTest, UnregisterEventCallbackTest_001, TestSize.Level1) +{ + if (g_ethInstance == nullptr) { + return; + } + int32_t rc = g_ethInstance->UnregisterEapEventCallback(g_ethInstance, g_ethCallback, IFNAME); + EXPECT_EQ(rc, HDF_SUCCESS); +} + +HWTEST_F(EthHdiServiceCTest, EapShellCmdTest_001, TestSize.Level1) +{ + if (g_ethInstance == nullptr) { + return; + } + const char *cmd = "test cmd"; + int32_t rc = g_ethInstance->EapShellCmd(g_ethInstance, IFNAME, cmd); + EXPECT_EQ(rc, HDF_SUCCESS); +} + +}; -- Gitee