From ff87a0061cdfcfe72cb9e8e95b40282952454f10 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 27 Sep 2023 20:18:51 +0200 Subject: [PATCH] feat(wpa_supplicant): migrate the tests from unit-test-app --- components/wpa_supplicant/test/CMakeLists.txt | 14 ------ .../test_apps/.build-test-rules.yml | 11 +++++ .../wpa_supplicant/test_apps/CMakeLists.txt | 9 ++++ components/wpa_supplicant/test_apps/README.md | 19 ++++++++ .../test_apps/main/CMakeLists.txt | 25 ++++++++++ .../test_apps/main/idf_component.yml | 3 ++ .../{test => test_apps/main}/test_crypto.c | 0 .../{test => test_apps/main}/test_dpp.c | 2 +- .../{test => test_apps/main}/test_eloop.c | 0 .../main}/test_fast_pbkdf2.c | 0 .../main}/test_offchannel.c | 2 +- .../{test => test_apps/main}/test_sae.c | 2 +- .../main/test_wpa_supplicant_common.h | 21 ++++++++ .../test_apps/main/test_wpa_supplicant_main.c | 48 +++++++++++++++++++ .../test_apps/pytest_wpa_supplicant_ut.py | 25 ++++++++++ .../test_apps/sdkconfig.defaults | 5 ++ 16 files changed, 169 insertions(+), 17 deletions(-) delete mode 100644 components/wpa_supplicant/test/CMakeLists.txt create mode 100644 components/wpa_supplicant/test_apps/.build-test-rules.yml create mode 100644 components/wpa_supplicant/test_apps/CMakeLists.txt create mode 100644 components/wpa_supplicant/test_apps/README.md create mode 100644 components/wpa_supplicant/test_apps/main/CMakeLists.txt create mode 100644 components/wpa_supplicant/test_apps/main/idf_component.yml rename components/wpa_supplicant/{test => test_apps/main}/test_crypto.c (100%) rename components/wpa_supplicant/{test => test_apps/main}/test_dpp.c (98%) rename components/wpa_supplicant/{test => test_apps/main}/test_eloop.c (100%) rename components/wpa_supplicant/{test => test_apps/main}/test_fast_pbkdf2.c (100%) rename components/wpa_supplicant/{test => test_apps/main}/test_offchannel.c (99%) rename components/wpa_supplicant/{test => test_apps/main}/test_sae.c (99%) create mode 100644 components/wpa_supplicant/test_apps/main/test_wpa_supplicant_common.h create mode 100644 components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c create mode 100644 components/wpa_supplicant/test_apps/pytest_wpa_supplicant_ut.py create mode 100644 components/wpa_supplicant/test_apps/sdkconfig.defaults diff --git a/components/wpa_supplicant/test/CMakeLists.txt b/components/wpa_supplicant/test/CMakeLists.txt deleted file mode 100644 index 99377e598e..0000000000 --- a/components/wpa_supplicant/test/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS "." "${CMAKE_CURRENT_BINARY_DIR}" - PRIV_INCLUDE_DIRS "../src" "../esp_supplicant/src" - PRIV_REQUIRES cmock esp_common test_utils wpa_supplicant mbedtls esp_wifi esp_event) - -idf_component_get_property(esp_supplicant_dir wpa_supplicant COMPONENT_DIR) - -# Calculate MD5 value of header file esp_wifi_driver.h -file(MD5 ${esp_supplicant_dir}/esp_supplicant/src/esp_wifi_driver.h WIFI_SUPPLICANT_MD5) -string(SUBSTRING "${WIFI_SUPPLICANT_MD5}" 0 7 WIFI_SUPPLICANT_MD5) - -add_definitions(-DWIFI_SUPPLICANT_MD5=\"${WIFI_SUPPLICANT_MD5}\") -add_definitions(-DCONFIG_WPA3_SAE) -add_definitions(-DCONFIG_DPP) diff --git a/components/wpa_supplicant/test_apps/.build-test-rules.yml b/components/wpa_supplicant/test_apps/.build-test-rules.yml new file mode 100644 index 0000000000..6013a2343c --- /dev/null +++ b/components/wpa_supplicant/test_apps/.build-test-rules.yml @@ -0,0 +1,11 @@ +# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps + +components/wpa_supplicant/test_apps: + disable: + - if: SOC_WIFI_SUPPORTED != 1 + depends_components: + - esp_wifi + - wpa_supplicant + - mbedtls + - esp_timer + - esp_event diff --git a/components/wpa_supplicant/test_apps/CMakeLists.txt b/components/wpa_supplicant/test_apps/CMakeLists.txt new file mode 100644 index 0000000000..bbfb3f8b9f --- /dev/null +++ b/components/wpa_supplicant/test_apps/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) +list(PREPEND SDKCONFIG_DEFAULTS + "$ENV{IDF_PATH}/tools/test_apps/configs/sdkconfig.debug_helpers" + "sdkconfig.defaults") + +project(wpa_supplicant_test) diff --git a/components/wpa_supplicant/test_apps/README.md b/components/wpa_supplicant/test_apps/README.md new file mode 100644 index 0000000000..3fa2378f00 --- /dev/null +++ b/components/wpa_supplicant/test_apps/README.md @@ -0,0 +1,19 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | + +# wpa_supplicant unit test + +To build and run this test app, using esp32s3 target for example: + +```bash +idf.py set-target esp32s3 +idf.py build flash monitor +``` + +To run tests using pytest: + +```bash +idf.py set-target esp32s3 +idf.py build +pytest --target=esp32s3 +``` diff --git a/components/wpa_supplicant/test_apps/main/CMakeLists.txt b/components/wpa_supplicant/test_apps/main/CMakeLists.txt new file mode 100644 index 0000000000..5dcdf7b250 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/CMakeLists.txt @@ -0,0 +1,25 @@ +idf_component_register(SRCS + "test_crypto.c" + "test_dpp.c" + "test_eloop.c" + "test_fast_pbkdf2.c" + "test_offchannel.c" + "test_sae.c" + "test_wpa_supplicant_main.c" + PRIV_INCLUDE_DIRS "." + PRIV_REQUIRES wpa_supplicant mbedtls esp_wifi esp_event unity + WHOLE_ARCHIVE) + +idf_component_get_property(esp_supplicant_dir wpa_supplicant COMPONENT_DIR) + +# Calculate MD5 value of header file esp_wifi_driver.h +file(MD5 ${esp_supplicant_dir}/esp_supplicant/src/esp_wifi_driver.h WIFI_SUPPLICANT_MD5) +string(SUBSTRING "${WIFI_SUPPLICANT_MD5}" 0 7 WIFI_SUPPLICANT_MD5) + +# Steal some private include directories from wpa_supplicant +target_include_directories(${COMPONENT_LIB} PRIVATE ${esp_supplicant_dir}/esp_supplicant/src) +target_include_directories(${COMPONENT_LIB} PRIVATE ${esp_supplicant_dir}/src) + +add_definitions(-DWIFI_SUPPLICANT_MD5=\"${WIFI_SUPPLICANT_MD5}\") +add_definitions(-DCONFIG_WPA3_SAE) +add_definitions(-DCONFIG_DPP) diff --git a/components/wpa_supplicant/test_apps/main/idf_component.yml b/components/wpa_supplicant/test_apps/main/idf_component.yml new file mode 100644 index 0000000000..f5001494e2 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + test_utils: + path: ${IDF_PATH}/tools/unit-test-app/components/test_utils diff --git a/components/wpa_supplicant/test/test_crypto.c b/components/wpa_supplicant/test_apps/main/test_crypto.c similarity index 100% rename from components/wpa_supplicant/test/test_crypto.c rename to components/wpa_supplicant/test_apps/main/test_crypto.c diff --git a/components/wpa_supplicant/test/test_dpp.c b/components/wpa_supplicant/test_apps/main/test_dpp.c similarity index 98% rename from components/wpa_supplicant/test/test_dpp.c rename to components/wpa_supplicant/test_apps/main/test_dpp.c index d9eb32732b..af8d2460ad 100644 --- a/components/wpa_supplicant/test/test_dpp.c +++ b/components/wpa_supplicant/test_apps/main/test_dpp.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/wpa_supplicant/test/test_eloop.c b/components/wpa_supplicant/test_apps/main/test_eloop.c similarity index 100% rename from components/wpa_supplicant/test/test_eloop.c rename to components/wpa_supplicant/test_apps/main/test_eloop.c diff --git a/components/wpa_supplicant/test/test_fast_pbkdf2.c b/components/wpa_supplicant/test_apps/main/test_fast_pbkdf2.c similarity index 100% rename from components/wpa_supplicant/test/test_fast_pbkdf2.c rename to components/wpa_supplicant/test_apps/main/test_fast_pbkdf2.c diff --git a/components/wpa_supplicant/test/test_offchannel.c b/components/wpa_supplicant/test_apps/main/test_offchannel.c similarity index 99% rename from components/wpa_supplicant/test/test_offchannel.c rename to components/wpa_supplicant/test_apps/main/test_offchannel.c index 126b2844fe..5ddc0f082f 100644 --- a/components/wpa_supplicant/test/test_offchannel.c +++ b/components/wpa_supplicant/test_apps/main/test_offchannel.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 * diff --git a/components/wpa_supplicant/test/test_sae.c b/components/wpa_supplicant/test_apps/main/test_sae.c similarity index 99% rename from components/wpa_supplicant/test/test_sae.c rename to components/wpa_supplicant/test_apps/main/test_sae.c index 3c14d14c4b..ec13d144fc 100644 --- a/components/wpa_supplicant/test/test_sae.c +++ b/components/wpa_supplicant/test_apps/main/test_sae.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_common.h b/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_common.h new file mode 100644 index 0000000000..a13ab4c255 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_common.h @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Set the leak threshold value for the specific test case + * + * @param threshold Maximum allowed memory leak in bytes + */ +void set_leak_threshold(int threshold); + +#ifdef __cplusplus +} +#endif diff --git a/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c b/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c new file mode 100644 index 0000000000..833d9ba3f0 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c @@ -0,0 +1,48 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "unity.h" +#include "unity_test_runner.h" +#include "esp_heap_caps.h" + +#define TEST_MEMORY_LEAK_THRESHOLD_DEFAULT 0 +static int leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT; +void set_leak_threshold(int threshold) +{ + leak_threshold = -threshold; +} + +static size_t before_free_8bit; +static size_t before_free_32bit; + +static void check_leak(size_t before_free, size_t after_free, const char *type) +{ + ssize_t delta = after_free - before_free; + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d, threshold %d)\n", type, before_free, after_free, delta, leak_threshold); + TEST_ASSERT_MESSAGE(delta > leak_threshold, "memory leak"); +} + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + check_leak(before_free_8bit, after_free_8bit, "8BIT"); + check_leak(before_free_32bit, after_free_32bit, "32BIT"); + + leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT; +} + +void app_main(void) +{ + printf("Running wpa_supplicant component tests\n"); + unity_run_menu(); +} diff --git a/components/wpa_supplicant/test_apps/pytest_wpa_supplicant_ut.py b/components/wpa_supplicant/test_apps/pytest_wpa_supplicant_ut.py new file mode 100644 index 0000000000..f2d647ed70 --- /dev/null +++ b/components/wpa_supplicant/test_apps/pytest_wpa_supplicant_ut.py @@ -0,0 +1,25 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: CC0-1.0 + +import pytest +from idf_unity_tester import CaseTester +from pytest_embedded import Dut + + +@pytest.mark.generic +@pytest.mark.esp32 +@pytest.mark.esp32s2 +@pytest.mark.esp32s3 +@pytest.mark.esp32c3 +@pytest.mark.esp32c2 +@pytest.mark.esp32c6 +def test_wpa_supplicant_ut(dut: Dut) -> None: + dut.run_all_single_board_cases() + + +@pytest.mark.esp32 +@pytest.mark.wifi_two_dut +def test_wpa_supplicant_ut_offchan(case_tester: CaseTester) -> None: + for case in case_tester.test_menu: + if case.attributes.get('test_env') == 'wifi_two_dut': + case_tester.run_multi_dev_case(case=case, reset=True) diff --git a/components/wpa_supplicant/test_apps/sdkconfig.defaults b/components/wpa_supplicant/test_apps/sdkconfig.defaults new file mode 100644 index 0000000000..94128e9034 --- /dev/null +++ b/components/wpa_supplicant/test_apps/sdkconfig.defaults @@ -0,0 +1,5 @@ +CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192 +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n +CONFIG_ESP_WIFI_TESTING_OPTIONS=y +CONFIG_ESP_WIFI_DPP_SUPPORT=y +CONFIG_WPA3_SAE=y