diff --git a/components/nvs_flash/test/CMakeLists.txt b/components/nvs_flash/test/CMakeLists.txt deleted file mode 100644 index 13515aa416..0000000000 --- a/components/nvs_flash/test/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS "." - PRIV_REQUIRES cmock test_utils nvs_flash bootloader_support spi_flash - EMBED_TXTFILES encryption_keys.bin partition_encrypted.bin sample.bin) -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") - -if(CONFIG_NVS_ENCRYPTION) - target_link_libraries(${COMPONENT_LIB} PUBLIC idf::mbedtls) -endif() diff --git a/components/nvs_flash/test_apps/CMakeLists.txt b/components/nvs_flash/test_apps/CMakeLists.txt new file mode 100644 index 0000000000..6fbd6b2ff4 --- /dev/null +++ b/components/nvs_flash/test_apps/CMakeLists.txt @@ -0,0 +1,11 @@ +#This is the project CMakeLists.txt file for the test subproject +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") +set(COMPONENTS main mbedtls) + +list(APPEND SDKCONFIG_DEFAULTS "$ENV{IDF_PATH}/tools/test_apps/configs/sdkconfig.debug_helpers") +list(APPEND SDKCONFIG_DEFAULTS "sdkconfig.defaults") + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(nvs_flash_test) diff --git a/components/nvs_flash/test_apps/README.md b/components/nvs_flash/test_apps/README.md new file mode 100644 index 0000000000..a8b7833fa3 --- /dev/null +++ b/components/nvs_flash/test_apps/README.md @@ -0,0 +1,2 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/nvs_flash/test_apps/main/CMakeLists.txt b/components/nvs_flash/test_apps/main/CMakeLists.txt new file mode 100644 index 0000000000..1553014c85 --- /dev/null +++ b/components/nvs_flash/test_apps/main/CMakeLists.txt @@ -0,0 +1,8 @@ +idf_component_register(SRC_DIRS "." + PRIV_REQUIRES cmock test_utils nvs_flash bootloader_support spi_flash + EMBED_TXTFILES encryption_keys.bin partition_encrypted.bin sample.bin + WHOLE_ARCHIVE) + +if(CONFIG_NVS_ENCRYPTION) + target_link_libraries(${COMPONENT_LIB} PUBLIC idf::mbedtls) +endif() diff --git a/components/nvs_flash/test_apps/main/app_main.c b/components/nvs_flash/test_apps/main/app_main.c new file mode 100644 index 0000000000..2dc22a84d7 --- /dev/null +++ b/components/nvs_flash/test_apps/main/app_main.c @@ -0,0 +1,46 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "unity.h" +#include "memory_checks.h" + +/* setUp runs before every test */ +void setUp(void) +{ + test_utils_record_free_mem(); + test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL); + test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL); + test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_LWIP); +} + +/* tearDown runs after every test */ +void tearDown(void) +{ + /* some FreeRTOS stuff is cleaned up by idle task */ + vTaskDelay(5); + + /* clean up some of the newlib's lazy allocations */ + esp_reent_cleanup(); + + /* check if unit test has caused heap corruption in any heap */ + TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap"); + + test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL), + test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL)); + +} + +static void test_task(void *pvParameters) +{ + vTaskDelay(2); /* Delay a bit to let the main task be deleted */ + unity_run_menu(); +} + +void app_main(void) +{ + xTaskCreatePinnedToCore(test_task, "testTask", CONFIG_UNITY_FREERTOS_STACK_SIZE, NULL, CONFIG_UNITY_FREERTOS_PRIORITY, NULL, CONFIG_UNITY_FREERTOS_CPU); +} diff --git a/components/nvs_flash/test/encryption_keys.bin b/components/nvs_flash/test_apps/main/encryption_keys.bin similarity index 100% rename from components/nvs_flash/test/encryption_keys.bin rename to components/nvs_flash/test_apps/main/encryption_keys.bin diff --git a/components/nvs_flash/test/partition_encrypted.bin b/components/nvs_flash/test_apps/main/partition_encrypted.bin similarity index 100% rename from components/nvs_flash/test/partition_encrypted.bin rename to components/nvs_flash/test_apps/main/partition_encrypted.bin diff --git a/components/nvs_flash/test/sample.bin b/components/nvs_flash/test_apps/main/sample.bin similarity index 100% rename from components/nvs_flash/test/sample.bin rename to components/nvs_flash/test_apps/main/sample.bin diff --git a/components/nvs_flash/test/test_nvs.c b/components/nvs_flash/test_apps/main/test_nvs.c similarity index 97% rename from components/nvs_flash/test/test_nvs.c rename to components/nvs_flash/test_apps/main/test_nvs.c index c2318ebe50..9a7b19b76d 100644 --- a/components/nvs_flash/test/test_nvs.c +++ b/components/nvs_flash/test_apps/main/test_nvs.c @@ -1,17 +1,27 @@ +/* + * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ #include +#include +#include + #include #include -#include +#include #include -#include "unity.h" -#include "nvs.h" -#include "nvs_flash.h" -#include "esp_partition.h" + #include "esp_flash_encrypt.h" #include "esp_log.h" -#include +#include "esp_partition.h" #include "esp_system.h" +#include "nvs.h" +#include "nvs_flash.h" + +#include "unity.h" + #ifdef CONFIG_NVS_ENCRYPTION #include "mbedtls/aes.h" #endif @@ -49,6 +59,8 @@ TEST_CASE("flash erase deinitializes initialized partition", "[nvs]") nvs_flash_deinit(); } +#ifndef CONFIG_NVS_ENCRYPTION +// NOTE: `nvs_flash_init_partition_ptr` does not support NVS encryption TEST_CASE("nvs_flash_init_partition_ptr() works correctly", "[nvs]") { // First, open and write to partition using normal initialization @@ -77,6 +89,7 @@ TEST_CASE("nvs_flash_init_partition_ptr() works correctly", "[nvs]") nvs_flash_deinit(); } +#endif // test could have different output on host tests TEST_CASE("nvs deinit with open handle", "[nvs]") @@ -310,11 +323,11 @@ TEST_CASE("check for memory leaks in nvs_set_blob", "[nvs]") TEST_ESP_OK( nvs_set_blob(my_handle, "key", key, sizeof(key)) ); TEST_ESP_OK( nvs_commit(my_handle) ); nvs_close(my_handle); - printf("%d\n", esp_get_free_heap_size()); + printf("%" PRId32 "\n", esp_get_free_heap_size()); } nvs_flash_deinit(); - printf("%d\n", esp_get_free_heap_size()); + printf("%" PRId32 "\n", esp_get_free_heap_size()); /* heap leaks will be checked in unity_platform.c */ } @@ -496,7 +509,7 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena TEST_ASSERT_TRUE((nvs_key_end - nvs_key_start - 1) == SPI_FLASH_SEC_SIZE); assert(nvs_part && "partition table must have an NVS partition"); - printf("\n nvs_part size:%d\n", nvs_part->size); + printf("\n nvs_part size:%" PRId32 "\n", nvs_part->size); ESP_ERROR_CHECK(esp_partition_erase_range(key_part, 0, key_part->size)); ESP_ERROR_CHECK( esp_partition_erase_range(nvs_part, 0, nvs_part->size) ); diff --git a/components/nvs_flash/test_apps/partitions_nvs_encr_keys_flash_enc.csv b/components/nvs_flash/test_apps/partitions_nvs_encr_keys_flash_enc.csv new file mode 100644 index 0000000000..b56b869612 --- /dev/null +++ b/components/nvs_flash/test_apps/partitions_nvs_encr_keys_flash_enc.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, , 24K, +factory, app, factory, , 1M, +nvs_key, data, nvs_keys, , 4K, encrypted, +custom_nvs, data, nvs, , 24K, diff --git a/components/nvs_flash/test_apps/pytest_nvs_flash.py b/components/nvs_flash/test_apps/pytest_nvs_flash.py new file mode 100644 index 0000000000..ed07f4f729 --- /dev/null +++ b/components/nvs_flash/test_apps/pytest_nvs_flash.py @@ -0,0 +1,26 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from pytest_embedded_idf.dut import IdfDut + + +@pytest.mark.supported_targets +@pytest.mark.generic +@pytest.mark.parametrize('config', ['default'], indirect=True) +def test_nvs_flash(dut: IdfDut) -> None: + dut.run_all_single_board_cases() + + +CONFIGS_NVS_ENCR_KEYS_FLASH_ENC = [ + pytest.param('nvs_encr_keys_flash_enc_esp32', marks=[pytest.mark.esp32]), + pytest.param('nvs_encr_keys_flash_enc_esp32c3', marks=[pytest.mark.esp32c3]), +] + + +@pytest.mark.parametrize('config', CONFIGS_NVS_ENCR_KEYS_FLASH_ENC, indirect=True) +@pytest.mark.flash_encryption +def test_nvs_flash_encr_keys_flash_enc(dut: IdfDut) -> None: + # Erase the nvs_key partition + dut.serial.erase_partition('nvs_key') + dut.run_all_single_board_cases() diff --git a/components/nvs_flash/test_apps/sdkconfig.ci.default b/components/nvs_flash/test_apps/sdkconfig.ci.default new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/nvs_flash/test_apps/sdkconfig.ci.nvs_encr_keys_flash_enc_esp32 b/components/nvs_flash/test_apps/sdkconfig.ci.nvs_encr_keys_flash_enc_esp32 new file mode 100644 index 0000000000..28b989f246 --- /dev/null +++ b/components/nvs_flash/test_apps/sdkconfig.ci.nvs_encr_keys_flash_enc_esp32 @@ -0,0 +1,18 @@ +# Restricting to ESP32 +CONFIG_IDF_TARGET="esp32" + +# Partition Table +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_nvs_encr_keys_flash_enc.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions_nvs_encr_keys_flash_enc.csv" +CONFIG_PARTITION_TABLE_OFFSET=0x9000 + +# Enabling Flash Encryption +CONFIG_SECURE_FLASH_ENC_ENABLED=y +CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=y +CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC=y +CONFIG_SECURE_BOOT_ALLOW_JTAG=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=y +CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED=y diff --git a/components/nvs_flash/test_apps/sdkconfig.ci.nvs_encr_keys_flash_enc_esp32c3 b/components/nvs_flash/test_apps/sdkconfig.ci.nvs_encr_keys_flash_enc_esp32c3 new file mode 100644 index 0000000000..6a986fa54d --- /dev/null +++ b/components/nvs_flash/test_apps/sdkconfig.ci.nvs_encr_keys_flash_enc_esp32c3 @@ -0,0 +1,18 @@ +# Restricting to ESP32C3 +CONFIG_IDF_TARGET="esp32c3" + +# Partition Table +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_nvs_encr_keys_flash_enc.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions_nvs_encr_keys_flash_enc.csv" +CONFIG_PARTITION_TABLE_OFFSET=0x9000 + +# Enabling Flash Encryption +CONFIG_SECURE_FLASH_ENC_ENABLED=y +CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=y +CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC=y +CONFIG_SECURE_BOOT_ALLOW_JTAG=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=y +CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED=y diff --git a/components/nvs_flash/test_apps/sdkconfig.defaults b/components/nvs_flash/test_apps/sdkconfig.defaults new file mode 100644 index 0000000000..a49ce6190c --- /dev/null +++ b/components/nvs_flash/test_apps/sdkconfig.defaults @@ -0,0 +1,6 @@ +# General options for additional checks +CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y +CONFIG_NVS_ASSERT_ERROR_CHECK=y + +CONFIG_ESP_TASK_WDT_EN=y +CONFIG_ESP_TASK_WDT_INIT=n diff --git a/tools/unit-test-app/configs/default_2_c2 b/tools/unit-test-app/configs/default_2_c2 index 0b38204811..a5f8d178dc 100644 --- a/tools/unit-test-app/configs/default_2_c2 +++ b/tools/unit-test-app/configs/default_2_c2 @@ -1,6 +1,6 @@ # This config is split between targets since different component needs to be included CONFIG_IDF_TARGET="esp32c2" -TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc +TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns newlib partition_table sdmmc CONFIG_NEWLIB_TIME_SYSCALL_USE_NONE=n CONFIG_NEWLIB_TIME_SYSCALL_USE_HRT=y CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC=n diff --git a/tools/unit-test-app/configs/default_2_c6 b/tools/unit-test-app/configs/default_2_c6 index 82947f4f7d..a017534958 100644 --- a/tools/unit-test-app/configs/default_2_c6 +++ b/tools/unit-test-app/configs/default_2_c6 @@ -1,3 +1,3 @@ # This config is split between targets since different component needs to be included CONFIG_IDF_TARGET="esp32c6" -TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc +TEST_COMPONENTS=app_trace esp_eth esp_hid esp_phy esp_wifi espcoredump hal lwip mdns newlib partition_table sdmmc diff --git a/tools/unit-test-app/configs/default_2_h2 b/tools/unit-test-app/configs/default_2_h2 index 97d2d59fae..1c2d4dd8ff 100644 --- a/tools/unit-test-app/configs/default_2_h2 +++ b/tools/unit-test-app/configs/default_2_h2 @@ -1,3 +1,3 @@ # This config is split between targets since different component needs to be included CONFIG_IDF_TARGET="esp32h2" -TEST_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc +TEST_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_wifi espcoredump hal lwip mdns newlib partition_table sdmmc diff --git a/tools/unit-test-app/configs/default_3_c2 b/tools/unit-test-app/configs/default_3_c2 index a534f7a34e..24aff8a7fd 100644 --- a/tools/unit-test-app/configs/default_3_c2 +++ b/tools/unit-test-app/configs/default_3_c2 @@ -1,3 +1,3 @@ # This config is split between targets since different component needs to be included CONFIG_IDF_TARGET="esp32c2" -TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc driver soc spi_flash +TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib partition_table sdmmc driver soc spi_flash diff --git a/tools/unit-test-app/configs/default_3_c6 b/tools/unit-test-app/configs/default_3_c6 index 93dddf3dc0..057e5b426f 100644 --- a/tools/unit-test-app/configs/default_3_c6 +++ b/tools/unit-test-app/configs/default_3_c6 @@ -1,3 +1,3 @@ # This config is split between targets since different component needs to be included CONFIG_IDF_TARGET="esp32c6" -TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc driver soc spi_flash +TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib partition_table sdmmc driver soc spi_flash diff --git a/tools/unit-test-app/configs/default_3_h2 b/tools/unit-test-app/configs/default_3_h2 index 5d08b4c849..c3df75b668 100644 --- a/tools/unit-test-app/configs/default_3_h2 +++ b/tools/unit-test-app/configs/default_3_h2 @@ -1,3 +1,3 @@ # This config is split between targets since different component needs to be included CONFIG_IDF_TARGET="esp32h2" -TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib nvs_flash partition_table sdmmc driver soc spi_flash +TEST_EXCLUDE_COMPONENTS=app_trace esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns newlib partition_table sdmmc driver soc spi_flash diff --git a/tools/unit-test-app/sdkconfig.defaults b/tools/unit-test-app/sdkconfig.defaults index 797dea67e2..494608154c 100644 --- a/tools/unit-test-app/sdkconfig.defaults +++ b/tools/unit-test-app/sdkconfig.defaults @@ -20,4 +20,3 @@ CONFIG_EFUSE_VIRTUAL=y CONFIG_SPIRAM_BANKSWITCH_ENABLE=n CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL=y CONFIG_MQTT_TEST_BROKER_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" -CONFIG_NVS_ASSERT_ERROR_CHECK=y