nvs_flash: Migrate UTs to component test-apps

This commit is contained in:
Laukik Hase 2023-03-01 12:51:55 +05:30
parent 5d222db12f
commit 4ac95a33fd
No known key found for this signature in database
GPG Key ID: 11C571361F51A199
22 changed files with 168 additions and 25 deletions

View File

@ -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()

View File

@ -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)

View File

@ -0,0 +1,2 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |

View File

@ -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()

View File

@ -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);
}

View File

@ -1,17 +1,27 @@
/*
* SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#include "unity.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_partition.h"
#include "esp_flash_encrypt.h"
#include "esp_log.h"
#include <string.h>
#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) );

View File

@ -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,
1 # Name, Type, SubType, Offset, Size, Flags
2 nvs, data, nvs, , 24K,
3 factory, app, factory, , 1M,
4 nvs_key, data, nvs_keys, , 4K, encrypted,
5 custom_nvs, data, nvs, , 24K,

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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