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/test_dpp.c b/components/wpa_supplicant/test/test_dpp.c deleted file mode 100644 index d9eb32732b..0000000000 --- a/components/wpa_supplicant/test/test_dpp.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include "unity.h" -#include -#include "utils/common.h" -#include "utils/includes.h" -#include "crypto/crypto.h" -#include "../src/common/defs.h" -#include "../src/common/dpp.h" - -#ifdef CONFIG_ESP_WIFI_TESTING_OPTIONS -struct dpp_global { - void *msg_ctx; - struct dl_list bootstrap; /* struct dpp_bootstrap_info */ - struct dl_list configurator; /* struct dpp_configurator */ -}; - -extern u8 dpp_protocol_key_override[600]; -extern size_t dpp_protocol_key_override_len; -extern u8 dpp_nonce_override[DPP_MAX_NONCE_LEN]; -extern size_t dpp_nonce_override_len; -#define MAX_FRAME_SIZE 1200 - -TEST_CASE("Test vectors DPP responder p256", "[wpa_dpp]") -{ - /* Global variables */ - char command[1200] = {0}; - const u8 *frame; - int len = 0; - struct dpp_authentication *auth_instance = NULL; - u8 auth[MAX_FRAME_SIZE] = {0}; - char prefix[] = "30310201010420"; - char postfix[] = "a00a06082a8648ce3d030107"; - size_t hex_len; - int ret = 0; - int id; - - /* DPP global config initialization */ - struct dpp_global_config dpp_conf; - memset(&dpp_conf, 0, sizeof(dpp_conf)); - struct dpp_global *dpp = dpp_global_init(&dpp_conf); - - /* bootstrap generation test */ - ESP_LOGI("DPP Test", "bootstrap generation test"); - { - char key[1000] = {0}; - const char *uri; - - char private_bootstrap_key[] = "54ce181a98525f217216f59b245f60e9df30ac7f6b26c939418cfc3c42d1afa0"; - char bootstrap_info[] = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgACCcWFqRtN+f0loEUgGIXDnMXPrjl92u2pV97Ff6DjUD8=;;"; - - sprintf(key, "%s%s%s", prefix, private_bootstrap_key, postfix); - - sprintf(command, "type=qrcode key=%s", key); - id = dpp_bootstrap_gen(dpp, command); - uri = dpp_bootstrap_get_uri(dpp, id); - printf("uri is =%s\n", uri); - printf("is be =%s\n", bootstrap_info); - TEST_ASSERT((strcmp(uri, bootstrap_info) == 0)); - } - ESP_LOGI("DPP Test", "bootstap generation passed"); - ESP_LOGI("DPP Test", "Overwrite Protocol key, responder nounce"); - { - char protocol_key[] = "f798ed2e19286f6a6efe210b1863badb99af2a14b497634dbfd2a97394fb5aa5"; - char nounce[] = "3d0cfb011ca916d796f7029ff0b43393"; - - /* Overwrite protocol key */ - memset(command, 0, 1200); - sprintf(command, "%s%s%s", prefix, protocol_key, postfix); - - hex_len = os_strlen(command); - ret = 0; - - if (hex_len > 2 * sizeof(dpp_protocol_key_override)) - ret = -1; - else if (hexstr2bin(command, dpp_protocol_key_override, - hex_len / 2)) - ret = -1; - else - dpp_protocol_key_override_len = hex_len / 2; - - TEST_ASSERT(ret == 0); - - /* Overwrite nounce */ - hex_len = os_strlen(nounce); - - if (hex_len > 2 * sizeof(dpp_nonce_override)) - ret = -1; - else if (hexstr2bin(nounce, dpp_nonce_override, hex_len / 2)) - ret = -1; - else - dpp_nonce_override_len = hex_len / 2; - - TEST_ASSERT(ret == 0); - - } - ESP_LOGI("DPP Test", "Overwritten Protocol key, responder nounce.. "); - ESP_LOGI("DPP Test", "Enqueue Auth request"); - { - char auth_req[] = "d00012001ac459c40d649f8664c1b8771ac459c40d6400120409506f9a1a010002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d011020005d467a09760292fc15d31792b0a5b050db8bf6ad807d71b2d93f4d1c2e65d8810310400050a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61ae14f35a5a858bccad90d126c46594c49ef82655e78888e15a32d916ac217249118100200510104102900868f478fc599ac3fa8152b975eff8be4e71b189dbefbc3185b1d7f3864e896f913cba3d9601326f278"; - - char auth_resp[] = "d00012349f8664c1b8771ac459c40d649f8664c1b87712340409506f9a1a0101001001000002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d091040005e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a902d4b030669fb9eff8b0a79fa7c1a172ac2a92c626256963f9274dc90682c81e504107500da553cdf80da3e27054c5e1f809ac303c63948b9bb5690ad12f357d75dfbc362cbae89e472dd6851925534024310aff5ae403831e98a7efc7deb9516164329c227039ae73c509147d156ae085f56c242bf7decc1f3b68d81697c6197453cb6faff7b062f7861073148052db539895bc6583d08b4aa"; - u8 *tmp; - - hex_len = os_strlen(auth_req); - if (hex_len > 2 * MAX_FRAME_SIZE) - ret = -1; - else if (hexstr2bin(auth_req, auth, hex_len / 2)) - ret = -1; - else - len = hex_len / 2; - frame = auth; - frame += 26; - len -= 26; - auth_instance = dpp_auth_req_rx(NULL, 1, 0 , NULL, - dpp_bootstrap_get_id(dpp, id), 2412, frame, frame+6, len-6); - - /* auth response u8 */ - hex_len = os_strlen(auth_resp); - if (hex_len > 2 * MAX_FRAME_SIZE) - ret = -1; - else if (hexstr2bin(auth_resp, auth, hex_len / 2)) - ret = -1; - else - len = hex_len / 2; - tmp = auth; - tmp += 26; - len -= 26; - - frame = wpabuf_head_u8(auth_instance->resp_msg); - len = wpabuf_len(auth_instance->resp_msg); - - TEST_ASSERT(memcmp(frame + 28, tmp + 26, len - 26) == 0); - } - ESP_LOGI("DPP Test", "Auth request parsing passed"); - ESP_LOGI("DPP Test", "Enqueue Auth confirm parsing passed"); - { - char auth_confirm[] = "d00012341ac459c40d649f8664c1b8771ac459c40d6412340409506f9a1a0102001001000002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d0410340054e07e62c74526dfd97e029dc781e0771e573ebc73c94227b5de8350fc6a1974b40f54c9fe1a1c9288a91fce4ee6c1f2ff069741"; - hex_len = os_strlen(auth_confirm); - os_memset(auth, 0, 1200); - if (hex_len > 2 * MAX_FRAME_SIZE) - ret = -1; - else if (hexstr2bin(auth_confirm, auth, hex_len / 2)) - ret = -1; - else - len = hex_len / 2; - frame = auth; - frame = auth + 26; - len = len - 26; - dpp_auth_conf_rx(auth_instance, frame, frame+6, len-6); - TEST_ASSERT(auth_instance->auth_success == 1); - } - ESP_LOGI("DPP Test", "Auth confirm parsing passed"); - /* deinit for memory passing */ - { - dpp_auth_deinit(auth_instance); - dpp_global_deinit(dpp); - } - ESP_LOGI("DPP Test", "Test case passed"); -} -#endif diff --git a/components/wpa_supplicant/test/test_eloop.c b/components/wpa_supplicant/test/test_eloop.c deleted file mode 100644 index 7cf73bb5e4..0000000000 --- a/components/wpa_supplicant/test/test_eloop.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -#include "string.h" -#include -#include "esp_system.h" -#include "unity.h" -#include "esp_system.h" -#include "esp_event.h" -#include "esp_wifi_types.h" -#include "utils/common.h" -#include "utils/eloop.h" -#include "common/ieee802_11_defs.h" -#include "../esp_supplicant/src/esp_wifi_driver.h" -#include "esp_log.h" -#include "test_utils.h" -#include "memory_checks.h" -#include - -#if SOC_WIFI_SUPPORTED -uint32_t timeouts_usec[6] = { 10000, 1000, 10000, 5000, 15000, 1000 }; -uint32_t timeouts_sec[6] = { 10, 1, 10, 5, 15, 1 }; -int executed_order[6]; -int t; -struct os_reltime ts; - - -/* there is only single instance of esp_timer so no need of protection */ -void callback(void *a, void *b) -{ - int *i = a; - struct os_time age, now; - - os_get_reltime(&now); - os_time_sub(&now, &ts, &age); - - int32_t ms_diff = (age.sec - timeouts_sec[*i]) * 1000 + - (age.usec - timeouts_usec[*i]) / 1000; - - /* let's give 50 ms offset for this small block */ - if (ms_diff > 50) { - executed_order[t] = -1; - } else { - executed_order[t] = *i; - } - t++; - - ESP_LOGI("Eloop Test", "timer[%d] ran after %" PRId32 " msec of scheduled time", - *i, ms_diff); - -} - -extern const wifi_osi_funcs_t *wifi_funcs; -/* Check if eloop runs its timers correctly & in correct order */ -TEST_CASE("Test eloop timers run", "[eloop]") -{ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - cfg.nvs_enable = false; - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - TEST_ESP_OK(esp_wifi_stop()); - TEST_ESP_OK(esp_wifi_deinit()); - /* Reset memory stats since some is leaked during the first initialization */ - test_utils_record_free_mem(); - - int execution_order[6] = {1, 5, 3, 0, 2, 4}; - int index[6] = {0,1,2,3,4,5}; - t = 0; - - /* We need pptask to run eloop, wifi init will do that */ - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - os_get_reltime(&ts); - for (int i = 0; i < 6; i++) { - eloop_register_timeout(timeouts_sec[i], timeouts_usec[i], - callback, &index[i], NULL); - } - - /* wait for all timers to run */ - os_sleep(20, 0); - /* check the execution order, this will also check whether they were fired at correct time */ - TEST_ASSERT(memcmp(execution_order, executed_order, 6*sizeof(int)) == 0); - TEST_ESP_OK(esp_wifi_stop()); - TEST_ESP_OK(esp_wifi_deinit()); - os_sleep(3, 0); -} -#endif //SOC_WIFI_SUPPORTED diff --git a/components/wpa_supplicant/test/test_fast_pbkdf2.c b/components/wpa_supplicant/test/test_fast_pbkdf2.c deleted file mode 100644 index 02055c045e..0000000000 --- a/components/wpa_supplicant/test/test_fast_pbkdf2.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -#include "string.h" -#include -#include "unity.h" -#include "utils/common.h" -#include "mbedtls/pkcs5.h" -#include "crypto/sha1.h" - -#if SOC_WIFI_SUPPORTED - -#define PMK_LEN 32 - -TEST_CASE("Test pbkdf2", "[crypto-pbkdf2]") -{ - uint8_t PMK[PMK_LEN]; - uint8_t ssid_len; - uint8_t passphrase_len; - uint8_t ssid[MAX_SSID_LEN]; - uint8_t passphrase[MAX_PASSPHRASE_LEN]; - uint8_t expected_pmk1[PMK_LEN] = - {0xe7, 0x90, 0xd0, 0x65, 0x67, 0xf0, 0xbf, 0xca, 0xca, 0x10, 0x88, 0x0b, 0x85, 0xb2, 0x33, 0xe5, - 0xe1, 0xd5, 0xe5, 0xb8, 0xd0, 0xfd, 0x94, 0x60, 0x56, 0x95, 0x5e, 0x41, 0x5a, 0x7f, 0xfa, 0xfa}; - - uint8_t expected_pmk[PMK_LEN]; - - /* Compare Fast PBKDF output with expected output*/ - pbkdf2_sha1("espressif", (uint8_t *)"espressif", strlen("espressif"), 4096, PMK, PMK_LEN); - TEST_ASSERT(memcmp(PMK, expected_pmk1, PMK_LEN) == 0); - - /* Compare fast PBKDF output with mbedtls pbkdf2 function's output */ - pbkdf2_sha1("espressif2", (uint8_t *)"espressif2", strlen("espressif2"), 4096, PMK, PMK_LEN); - mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, (const unsigned char *) "espressif2", - strlen("espressif2") , (const unsigned char *)"espressif2", - strlen("espressif2"), 4096, PMK_LEN, expected_pmk); - TEST_ASSERT(memcmp(PMK, expected_pmk, PMK_LEN) == 0); - - /* Calculate PMK using random ssid and passphrase and compare */ - os_memset(ssid, 0, MAX_SSID_LEN); - os_memset(passphrase, 0, MAX_PASSPHRASE_LEN); - ssid_len = os_random(); - ssid_len %= MAX_SSID_LEN; - - os_get_random(ssid, ssid_len); - - passphrase_len = os_random(); - passphrase_len %= MAX_PASSPHRASE_LEN; - - os_get_random(passphrase, passphrase_len); - pbkdf2_sha1((char *)passphrase, ssid, ssid_len, 4096, PMK, PMK_LEN); - mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, (const unsigned char *) passphrase, - strlen((char *)passphrase) , (const unsigned char *)ssid, - ssid_len, 4096, PMK_LEN, expected_pmk); - - /* Dump values if fails */ - if (memcmp(PMK, expected_pmk, PMK_LEN) != 0) { - ESP_LOG_BUFFER_HEXDUMP("passphrase", passphrase, passphrase_len, ESP_LOG_INFO); - ESP_LOG_BUFFER_HEXDUMP("ssid", ssid, ssid_len, ESP_LOG_INFO); - ESP_LOG_BUFFER_HEXDUMP("PMK", PMK, PMK_LEN, ESP_LOG_INFO); - ESP_LOG_BUFFER_HEXDUMP("expected_pmk", expected_pmk, PMK_LEN, ESP_LOG_INFO); - } - TEST_ASSERT(memcmp(PMK, expected_pmk, PMK_LEN) == 0); -} - -#endif /* SOC_WIFI_SUPPORTED */ 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 99% rename from components/wpa_supplicant/test/test_crypto.c rename to components/wpa_supplicant/test_apps/main/test_crypto.c index d0460d5293..e7b5a2159b 100644 --- a/components/wpa_supplicant/test/test_crypto.c +++ b/components/wpa_supplicant/test_apps/main/test_crypto.c @@ -16,11 +16,13 @@ #include "mbedtls/ecp.h" #include "test_utils.h" +#include "test_wpa_supplicant_common.h" typedef struct crypto_bignum crypto_bignum; TEST_CASE("Test crypto lib bignum apis", "[wpa_crypto]") { + set_leak_threshold(250); { uint8_t buf[32], buf2[32]; @@ -331,6 +333,7 @@ static inline void ecp_mpi_load( mbedtls_mpi *X, const mbedtls_mpi_uint *p, size TEST_CASE("Test crypto lib ECC apis", "[wpa_crypto]") { + set_leak_threshold(600); static const mbedtls_mpi_uint secp256r1_gx[] = { BYTES_TO_T_UINT_8( 0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4 ), diff --git a/components/wpa_supplicant/test_apps/main/test_dpp.c b/components/wpa_supplicant/test_apps/main/test_dpp.c new file mode 100644 index 0000000000..b75cbbd915 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/test_dpp.c @@ -0,0 +1,178 @@ +/* + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include "unity.h" +#include +#include "utils/common.h" +#include "utils/includes.h" +#include "crypto/crypto.h" +#include "common/defs.h" +#include "common/dpp.h" +#include "sdkconfig.h" +#include "test_wpa_supplicant_common.h" + +#ifdef CONFIG_ESP_WIFI_TESTING_OPTIONS +struct dpp_global { + void *msg_ctx; + struct dl_list bootstrap; /* struct dpp_bootstrap_info */ + struct dl_list configurator; /* struct dpp_configurator */ +}; + +extern u8 dpp_protocol_key_override[600]; +extern size_t dpp_protocol_key_override_len; +extern u8 dpp_nonce_override[DPP_MAX_NONCE_LEN]; +extern size_t dpp_nonce_override_len; +#define MAX_FRAME_SIZE 1200 + +TEST_CASE("Test vectors DPP responder p256", "[wpa_dpp]") +{ + set_leak_threshold(120); + /* Global variables */ + char command[1200] = {0}; + const u8 *frame; + int len = 0; + struct dpp_authentication *auth_instance = NULL; + u8 auth[MAX_FRAME_SIZE] = {0}; + char prefix[] = "30310201010420"; + char postfix[] = "a00a06082a8648ce3d030107"; + size_t hex_len; + int ret = 0; + int id; + + /* DPP global config initialization */ + struct dpp_global_config dpp_conf; + memset(&dpp_conf, 0, sizeof(dpp_conf)); + struct dpp_global *dpp = dpp_global_init(&dpp_conf); + + /* bootstrap generation test */ + ESP_LOGI("DPP Test", "bootstrap generation test"); + { + char key[1000] = {0}; + const char *uri; + + char private_bootstrap_key[] = "54ce181a98525f217216f59b245f60e9df30ac7f6b26c939418cfc3c42d1afa0"; + char bootstrap_info[] = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgACCcWFqRtN+f0loEUgGIXDnMXPrjl92u2pV97Ff6DjUD8=;;"; + + sprintf(key, "%s%s%s", prefix, private_bootstrap_key, postfix); + + sprintf(command, "type=qrcode key=%s", key); + id = dpp_bootstrap_gen(dpp, command); + uri = dpp_bootstrap_get_uri(dpp, id); + printf("uri is =%s\n", uri); + printf("is be =%s\n", bootstrap_info); + TEST_ASSERT((strcmp(uri, bootstrap_info) == 0)); + } + ESP_LOGI("DPP Test", "bootstap generation passed"); + ESP_LOGI("DPP Test", "Overwrite Protocol key, responder nounce"); + { + char protocol_key[] = "f798ed2e19286f6a6efe210b1863badb99af2a14b497634dbfd2a97394fb5aa5"; + char nounce[] = "3d0cfb011ca916d796f7029ff0b43393"; + + /* Overwrite protocol key */ + memset(command, 0, 1200); + sprintf(command, "%s%s%s", prefix, protocol_key, postfix); + + hex_len = os_strlen(command); + ret = 0; + + if (hex_len > 2 * sizeof(dpp_protocol_key_override)) { + ret = -1; + } else if (hexstr2bin(command, dpp_protocol_key_override, + hex_len / 2)) { + ret = -1; + } else { + dpp_protocol_key_override_len = hex_len / 2; + } + + TEST_ASSERT(ret == 0); + + /* Overwrite nounce */ + hex_len = os_strlen(nounce); + + if (hex_len > 2 * sizeof(dpp_nonce_override)) { + ret = -1; + } else if (hexstr2bin(nounce, dpp_nonce_override, hex_len / 2)) { + ret = -1; + } else { + dpp_nonce_override_len = hex_len / 2; + } + + TEST_ASSERT(ret == 0); + + } + ESP_LOGI("DPP Test", "Overwritten Protocol key, responder nounce.. "); + ESP_LOGI("DPP Test", "Enqueue Auth request"); + { + char auth_req[] = "d00012001ac459c40d649f8664c1b8771ac459c40d6400120409506f9a1a010002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d011020005d467a09760292fc15d31792b0a5b050db8bf6ad807d71b2d93f4d1c2e65d8810310400050a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61ae14f35a5a858bccad90d126c46594c49ef82655e78888e15a32d916ac217249118100200510104102900868f478fc599ac3fa8152b975eff8be4e71b189dbefbc3185b1d7f3864e896f913cba3d9601326f278"; + + char auth_resp[] = "d00012349f8664c1b8771ac459c40d649f8664c1b87712340409506f9a1a0101001001000002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d091040005e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a902d4b030669fb9eff8b0a79fa7c1a172ac2a92c626256963f9274dc90682c81e504107500da553cdf80da3e27054c5e1f809ac303c63948b9bb5690ad12f357d75dfbc362cbae89e472dd6851925534024310aff5ae403831e98a7efc7deb9516164329c227039ae73c509147d156ae085f56c242bf7decc1f3b68d81697c6197453cb6faff7b062f7861073148052db539895bc6583d08b4aa"; + u8 *tmp; + + hex_len = os_strlen(auth_req); + if (hex_len > 2 * MAX_FRAME_SIZE) { + ret = -1; + } else if (hexstr2bin(auth_req, auth, hex_len / 2)) { + ret = -1; + } else { + len = hex_len / 2; + } + frame = auth; + frame += 26; + len -= 26; + auth_instance = dpp_auth_req_rx(NULL, 1, 0, NULL, + dpp_bootstrap_get_id(dpp, id), 2412, frame, frame + 6, len - 6); + + /* auth response u8 */ + hex_len = os_strlen(auth_resp); + if (hex_len > 2 * MAX_FRAME_SIZE) { + ret = -1; + } else if (hexstr2bin(auth_resp, auth, hex_len / 2)) { + ret = -1; + } else { + len = hex_len / 2; + } + tmp = auth; + tmp += 26; + len -= 26; + + frame = wpabuf_head_u8(auth_instance->resp_msg); + len = wpabuf_len(auth_instance->resp_msg); + + TEST_ASSERT(memcmp(frame + 28, tmp + 26, len - 26) == 0); + } + ESP_LOGI("DPP Test", "Auth request parsing passed"); + ESP_LOGI("DPP Test", "Enqueue Auth confirm parsing passed"); + { + char auth_confirm[] = "d00012341ac459c40d649f8664c1b8771ac459c40d6412340409506f9a1a0102001001000002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d0410340054e07e62c74526dfd97e029dc781e0771e573ebc73c94227b5de8350fc6a1974b40f54c9fe1a1c9288a91fce4ee6c1f2ff069741"; + hex_len = os_strlen(auth_confirm); + os_memset(auth, 0, 1200); + if (hex_len > 2 * MAX_FRAME_SIZE) { + ret = -1; + } else if (hexstr2bin(auth_confirm, auth, hex_len / 2)) { + ret = -1; + } else { + len = hex_len / 2; + } + frame = auth; + frame = auth + 26; + len = len - 26; + dpp_auth_conf_rx(auth_instance, frame, frame + 6, len - 6); + TEST_ASSERT(auth_instance->auth_success == 1); + } + ESP_LOGI("DPP Test", "Auth confirm parsing passed"); + /* deinit for memory passing */ + { + dpp_auth_deinit(auth_instance); + dpp_global_deinit(dpp); + } + ESP_LOGI("DPP Test", "Test case passed"); +} +#endif diff --git a/components/wpa_supplicant/test_apps/main/test_eloop.c b/components/wpa_supplicant/test_apps/main/test_eloop.c new file mode 100644 index 0000000000..9553c50bb6 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/test_eloop.c @@ -0,0 +1,86 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include "esp_system.h" +#include "esp_log.h" +#include "esp_system.h" +#include "esp_event.h" +#include "esp_wifi_types.h" +#include "unity.h" +#include "utils/common.h" +#include "utils/eloop.h" +#include "common/ieee802_11_defs.h" +#include "esp_wifi_driver.h" +#include "memory_checks.h" +#include "test_wpa_supplicant_common.h" + +static uint32_t timeouts_usec[6] = { 10000, 1000, 10000, 5000, 15000, 1000 }; +static uint32_t timeouts_sec[6] = { 10, 1, 10, 5, 15, 1 }; +static int executed_order[6]; +static int t; +static struct os_reltime ts; + + +/* there is only single instance of esp_timer so no need of protection */ +static void callback(void *a, void *b) +{ + int *i = a; + struct os_time age, now; + + os_get_reltime(&now); + os_time_sub(&now, &ts, &age); + + int32_t ms_diff = (age.sec - timeouts_sec[*i]) * 1000 + + (age.usec - timeouts_usec[*i]) / 1000; + + /* let's give 50 ms offset for this small block */ + if (ms_diff > 50) { + executed_order[t] = -1; + } else { + executed_order[t] = *i; + } + t++; + + ESP_LOGI("Eloop Test", "timer[%d] ran after %" PRId32 " msec of scheduled time", + *i, ms_diff); + +} + +extern const wifi_osi_funcs_t *wifi_funcs; +/* Check if eloop runs its timers correctly & in correct order */ +TEST_CASE("Test eloop timers run", "[eloop]") +{ + set_leak_threshold(800); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + cfg.nvs_enable = false; + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + TEST_ESP_OK(esp_wifi_stop()); + TEST_ESP_OK(esp_wifi_deinit()); + /* Reset memory stats since some is leaked during the first initialization */ + test_utils_record_free_mem(); + + int execution_order[6] = {1, 5, 3, 0, 2, 4}; + int index[6] = {0, 1, 2, 3, 4, 5}; + t = 0; + + /* We need pptask to run eloop, wifi init will do that */ + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + os_get_reltime(&ts); + for (int i = 0; i < 6; i++) { + eloop_register_timeout(timeouts_sec[i], timeouts_usec[i], + callback, &index[i], NULL); + } + + /* wait for all timers to run */ + os_sleep(20, 0); + /* check the execution order, this will also check whether they were fired at correct time */ + TEST_ASSERT(memcmp(execution_order, executed_order, 6 * sizeof(int)) == 0); + TEST_ESP_OK(esp_wifi_stop()); + TEST_ESP_OK(esp_wifi_deinit()); + os_sleep(3, 0); +} diff --git a/components/wpa_supplicant/test_apps/main/test_fast_pbkdf2.c b/components/wpa_supplicant/test_apps/main/test_fast_pbkdf2.c new file mode 100644 index 0000000000..6e94c3e403 --- /dev/null +++ b/components/wpa_supplicant/test_apps/main/test_fast_pbkdf2.c @@ -0,0 +1,68 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "string.h" +#include +#include "unity.h" +#include "utils/common.h" +#include "mbedtls/pkcs5.h" +#include "crypto/sha1.h" +#include "test_wpa_supplicant_common.h" + +#define PMK_LEN 32 + +TEST_CASE("Test pbkdf2", "[crypto-pbkdf2]") +{ + set_leak_threshold(120); + + uint8_t PMK[PMK_LEN]; + uint8_t ssid_len; + uint8_t passphrase_len; + uint8_t ssid[MAX_SSID_LEN]; + uint8_t passphrase[MAX_PASSPHRASE_LEN]; + uint8_t expected_pmk1[PMK_LEN] = { + 0xe7, 0x90, 0xd0, 0x65, 0x67, 0xf0, 0xbf, 0xca, 0xca, 0x10, 0x88, 0x0b, 0x85, 0xb2, 0x33, 0xe5, + 0xe1, 0xd5, 0xe5, 0xb8, 0xd0, 0xfd, 0x94, 0x60, 0x56, 0x95, 0x5e, 0x41, 0x5a, 0x7f, 0xfa, 0xfa + }; + + uint8_t expected_pmk[PMK_LEN]; + + /* Compare Fast PBKDF output with expected output*/ + pbkdf2_sha1("espressif", (uint8_t *)"espressif", strlen("espressif"), 4096, PMK, PMK_LEN); + TEST_ASSERT(memcmp(PMK, expected_pmk1, PMK_LEN) == 0); + + /* Compare fast PBKDF output with mbedtls pbkdf2 function's output */ + pbkdf2_sha1("espressif2", (uint8_t *)"espressif2", strlen("espressif2"), 4096, PMK, PMK_LEN); + mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, (const unsigned char *) "espressif2", + strlen("espressif2"), (const unsigned char *)"espressif2", + strlen("espressif2"), 4096, PMK_LEN, expected_pmk); + TEST_ASSERT(memcmp(PMK, expected_pmk, PMK_LEN) == 0); + + /* Calculate PMK using random ssid and passphrase and compare */ + os_memset(ssid, 0, MAX_SSID_LEN); + os_memset(passphrase, 0, MAX_PASSPHRASE_LEN); + ssid_len = os_random(); + ssid_len %= MAX_SSID_LEN; + + os_get_random(ssid, ssid_len); + + passphrase_len = os_random(); + passphrase_len %= MAX_PASSPHRASE_LEN; + + os_get_random(passphrase, passphrase_len); + pbkdf2_sha1((char *)passphrase, ssid, ssid_len, 4096, PMK, PMK_LEN); + mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, (const unsigned char *) passphrase, + strlen((char *)passphrase), (const unsigned char *)ssid, + ssid_len, 4096, PMK_LEN, expected_pmk); + + /* Dump values if fails */ + if (memcmp(PMK, expected_pmk, PMK_LEN) != 0) { + ESP_LOG_BUFFER_HEXDUMP("passphrase", passphrase, passphrase_len, ESP_LOG_INFO); + ESP_LOG_BUFFER_HEXDUMP("ssid", ssid, ssid_len, ESP_LOG_INFO); + ESP_LOG_BUFFER_HEXDUMP("PMK", PMK, PMK_LEN, ESP_LOG_INFO); + ESP_LOG_BUFFER_HEXDUMP("expected_pmk", expected_pmk, PMK_LEN, ESP_LOG_INFO); + } + TEST_ASSERT(memcmp(PMK, expected_pmk, PMK_LEN) == 0); +} diff --git a/components/wpa_supplicant/test/test_offchannel.c b/components/wpa_supplicant/test_apps/main/test_offchannel.c similarity index 93% rename from components/wpa_supplicant/test/test_offchannel.c rename to components/wpa_supplicant/test_apps/main/test_offchannel.c index 126b2844fe..9e5ef6f5bc 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 * @@ -10,20 +10,23 @@ * CONDITIONS OF ANY KIND, either express or implied. */ -#include "string.h" +#include #include #include "esp_system.h" -#include "unity.h" +#include "esp_log.h" #include "esp_system.h" #include "esp_event.h" -#include "esp_wifi_types.h" -#include "utils/common.h" -#include "common/ieee802_11_defs.h" -#include "../esp_supplicant/src/esp_wifi_driver.h" -#include "esp_log.h" -#include "test_utils.h" -#include "memory_checks.h" +#include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" +#include "unity.h" +#include "utils/common.h" +#include "esp_wifi_types.h" +#include "esp_wifi_driver.h" +#include "memory_checks.h" +#include "common/ieee802_11_defs.h" +#include "test_utils.h" +#include "test_wpa_supplicant_common.h" +#include "sdkconfig.h" #define WIFI_START_EVENT 0x00000001 #define WIFI_ROC_DONE_EVENT 0x00000002 @@ -32,9 +35,9 @@ #define TEST_LISTEN_CHANNEL 6 -/* No runners */ -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3, ESP32C2, ESP32C6, ESP32H2) -//IDF-5046 +/* No runners; IDF-5046 */ +#if CONFIG_IDF_TARGET_ESP32 + static const char *TAG = "test_offchan"; esp_netif_t *wifi_netif; static EventGroupHandle_t wifi_event; @@ -127,7 +130,7 @@ void esp_send_action_frame(uint8_t *dest_mac, const uint8_t *buf, uint32_t len, uint8_t channel, uint32_t wait_time_ms) { wifi_action_tx_req_t *req = os_zalloc(sizeof(*req) + len);; - TEST_ASSERT( req != NULL); + TEST_ASSERT(req != NULL); req->ifx = WIFI_IF_STA; memcpy(req->dest_mac, dest_mac, ETH_ALEN); @@ -144,14 +147,13 @@ void esp_send_action_frame(uint8_t *dest_mac, const uint8_t *buf, uint32_t len, os_free(req); } - /* Test that foreground Scan doesn't pre-empt ROC & vice versa */ TEST_CASE("Test scan and ROC simultaneously", "[Offchan]") { wifi_action_rx_cb_t rx_cb = dummy_rx_action; EventBits_t bits; - test_case_uses_tcpip(); + set_leak_threshold(6000); start_wifi_as_sta(); xEventGroupWaitBits(wifi_event, WIFI_START_EVENT, 1, 0, 5000 / portTICK_PERIOD_MS); @@ -180,7 +182,7 @@ static void test_wifi_offchan_tx(void) char mac_str[19]; uint8_t mac[6]; - test_case_uses_tcpip(); + set_leak_threshold(6000); start_wifi_as_sta(); xEventGroupWaitBits(wifi_event, WIFI_START_EVENT, 1, 0, 5000 / portTICK_PERIOD_MS); @@ -218,7 +220,7 @@ static void test_wifi_roc(void) EventBits_t bits; uint8_t mac[6]; - test_case_uses_tcpip(); + set_leak_threshold(6000); start_wifi_as_sta(); xEventGroupWaitBits(wifi_event, WIFI_START_EVENT, 1, 0, 5000 / portTICK_PERIOD_MS); @@ -241,6 +243,6 @@ static void test_wifi_roc(void) } } -TEST_CASE_MULTIPLE_DEVICES("test ROC and Offchannel Action Frame Tx", "[Offchan][test_env=UT_T2_1][timeout=90]", test_wifi_roc, test_wifi_offchan_tx); +TEST_CASE_MULTIPLE_DEVICES("test ROC and Offchannel Action Frame Tx", "[Offchan][test_env=wifi_two_dut][timeout=90]", test_wifi_roc, test_wifi_offchan_tx); -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(...) +#endif //CONFIG_IDF_TARGET_ESP32 diff --git a/components/wpa_supplicant/test/test_sae.c b/components/wpa_supplicant/test_apps/main/test_sae.c similarity index 92% rename from components/wpa_supplicant/test/test_sae.c rename to components/wpa_supplicant/test_apps/main/test_sae.c index 3c14d14c4b..d2cef1572e 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 */ @@ -16,20 +16,21 @@ #include "utils/common.h" #include "utils/includes.h" #include "crypto/crypto.h" -#include "../src/common/sae.h" +#include "common/sae.h" #include "utils/wpabuf.h" #include "test_utils.h" -#if !CONFIG_IDF_TARGET_ESP32H2 // IDF-6781 -typedef struct crypto_bignum crypto_bignum; +#include "test_wpa_supplicant_common.h" +typedef struct crypto_bignum crypto_bignum; static struct wpabuf *wpabuf_alloc2(size_t len) { struct wpabuf *buf = (struct wpabuf *)os_zalloc(sizeof(struct wpabuf) + len); - if (buf == NULL) + if (buf == NULL) { return NULL; + } buf->size = len; - buf->buf = (u8 *) (buf + 1); + buf->buf = (u8 *)(buf + 1); return buf; } @@ -39,13 +40,15 @@ static struct wpabuf *wpabuf_alloc2(size_t len) * */ void wpabuf_free2(struct wpabuf *buf) { - if (buf == NULL) + if (buf == NULL) { return; + } os_free(buf); } TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") { + set_leak_threshold(120); ESP_LOGI("SAE Test", "### Beginning SAE init and deinit ###"); { /* Test init and deinit*/ @@ -188,10 +191,9 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") ESP_LOG_BUFFER_HEXDUMP("SAE: Predefined PMKID ", pmkid, SAE_PMKID_LEN, ESP_LOG_INFO); TEST_ASSERT(os_memcmp(pmkid, sae.pmkid, SAE_PMKID_LEN) == 0); - pt_info = sae_derive_pt(pt_groups, - (const u8 *) ssid, os_strlen(ssid), - (const u8 *) pw, os_strlen(pw), pwid); + (const u8 *) ssid, os_strlen(ssid), + (const u8 *) pw, os_strlen(pw), pwid); TEST_ASSERT(pt_info != NULL); @@ -240,13 +242,13 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") buf = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf != NULL); + TEST_ASSERT(buf != NULL); sae_write_commit(&sae, buf, NULL, NULL);// No anti-clogging token /* Parsing commit created by self will be detected as reflection attack*/ TEST_ASSERT(sae_parse_commit(&sae, - wpabuf_mhead(buf), buf->used, NULL, 0, default_groups, 0) == SAE_SILENTLY_DISCARD); + wpabuf_mhead(buf), buf->used, NULL, 0, default_groups, 0) == SAE_SILENTLY_DISCARD); wpabuf_free2(buf); sae_clear_temp_data(&sae); @@ -281,14 +283,13 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") /* STA1 creates commit msg buffer*/ buf1 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf1 != NULL); + TEST_ASSERT(buf1 != NULL); sae_write_commit(&sae1, buf1, NULL, NULL);// No anti-clogging token - ESP_LOG_BUFFER_HEXDUMP("SAE: Commit1", wpabuf_mhead_u8(buf1), wpabuf_len(buf1), ESP_LOG_INFO); - + ESP_LOG_BUFFER_HEXDUMP("SAE: Commit1", wpabuf_mhead_u8(buf1), wpabuf_len(buf1), ESP_LOG_INFO); /* STA2 creates commit msg buffer*/ buf2 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf2 != NULL); + TEST_ASSERT(buf2 != NULL); sae_write_commit(&sae2, buf2, NULL, NULL);// No anti-clogging token ESP_LOG_BUFFER_HEXDUMP("SAE: Commit2", wpabuf_mhead_u8(buf2), wpabuf_len(buf2), ESP_LOG_INFO); @@ -297,11 +298,11 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") /* STA1 parses STA2 commit*/ TEST_ASSERT(sae_parse_commit(&sae1, - wpabuf_mhead(buf2), buf2->used, NULL, 0, default_groups, 0) == 0); + wpabuf_mhead(buf2), buf2->used, NULL, 0, default_groups, 0) == 0); /* STA2 parses STA1 commit*/ TEST_ASSERT(sae_parse_commit(&sae2, - wpabuf_mhead(buf1), buf1->used, NULL, 0, default_groups, 0) == 0); + wpabuf_mhead(buf1), buf1->used, NULL, 0, default_groups, 0) == 0); /* STA1 processes commit*/ TEST_ASSERT(sae_process_commit(&sae1) == 0); @@ -311,13 +312,13 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") /* STA1 creates confirm msg buffer*/ buf3 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf3 != NULL); + TEST_ASSERT(buf3 != NULL); sae_write_confirm(&sae1, buf3); ESP_LOG_BUFFER_HEXDUMP("SAE: Confirm1", wpabuf_mhead_u8(buf3), wpabuf_len(buf3), ESP_LOG_INFO); /* STA2 creates confirm msg buffer*/ buf4 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf3 != NULL); + TEST_ASSERT(buf3 != NULL); sae_write_confirm(&sae2, buf4); ESP_LOG_BUFFER_HEXDUMP("SAE: Confirm2", wpabuf_mhead_u8(buf4), wpabuf_len(buf4), ESP_LOG_INFO); @@ -369,12 +370,12 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") /* STA1 creates commit msg buffer*/ buf1 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf1 != NULL); + TEST_ASSERT(buf1 != NULL); sae_write_commit(&sae1, buf1, NULL, NULL);// No anti-clogging token /* STA2 creates commit msg buffer*/ buf2 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf2 != NULL); + TEST_ASSERT(buf2 != NULL); sae_write_commit(&sae2, buf2, NULL, NULL);// No anti-clogging token sae1.state = SAE_COMMITTED; @@ -382,11 +383,11 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") /* STA1 parses STA2 commit*/ TEST_ASSERT(sae_parse_commit(&sae1, - wpabuf_mhead(buf2), buf2->used, NULL, 0, default_groups, 0) == 0); + wpabuf_mhead(buf2), buf2->used, NULL, 0, default_groups, 0) == 0); /* STA2 parses STA1 commit*/ TEST_ASSERT(sae_parse_commit(&sae2, - wpabuf_mhead(buf1), buf1->used, NULL, 0, default_groups, 0) == 0); + wpabuf_mhead(buf1), buf1->used, NULL, 0, default_groups, 0) == 0); /* STA1 processes commit*/ TEST_ASSERT(sae_process_commit(&sae1) == 0); @@ -396,12 +397,12 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") /* STA1 creates confirm msg buffer*/ buf3 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf3 != NULL); + TEST_ASSERT(buf3 != NULL); sae_write_confirm(&sae1, buf3); /* STA2 creates confirm msg buffer*/ buf4 = wpabuf_alloc2(SAE_COMMIT_MAX_LEN); - TEST_ASSERT( buf3 != NULL); + TEST_ASSERT(buf3 != NULL); sae_write_confirm(&sae2, buf4); /* STA1 checks confirm from STA2 and the check fails*/ @@ -423,5 +424,5 @@ TEST_CASE("Test SAE functionality with ECC group", "[wpa3_sae]") ESP_LOGI("SAE Test", "=========== Complete ============"); } -#endif + #endif /* CONFIG_WPA3_SAE */ 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