Skip to content

Commit

Permalink
Merge branch 'feature/storage_host_test_nvs' into 'master'
Browse files Browse the repository at this point in the history
nvs_flash: Migrate host tests to CMake build system, use Linux partition API

Closes IDF-4697

See merge request espressif/esp-idf!20671
  • Loading branch information
rrtandler committed Nov 16, 2022
2 parents c381fee + ca7f073 commit aad600c
Show file tree
Hide file tree
Showing 28 changed files with 2,999 additions and 2,777 deletions.
7 changes: 7 additions & 0 deletions .gitlab/ci/host-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ test_nvs_on_host:
- cd components/nvs_flash/test_nvs_host
- make test

test_nvs_on_host_cmake:
extends: .host_test_template
script:
- cd ${IDF_PATH}/components/nvs_flash/host_test/nvs_host_test
- idf.py build
- build/nvs_host_test.elf

test_nvs_coverage:
extends:
- .host_test_template
Expand Down
8 changes: 8 additions & 0 deletions components/nvs_flash/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ idf_component_register(SRCS "${srcs}"
REQUIRES "esp_partition"
PRIV_REQUIRES spi_flash
INCLUDE_DIRS "include"
"../spi_flash/include"
PRIV_INCLUDE_DIRS "private_include")

# If we use the linux target, we need to redirect the crc functions to the linux
Expand All @@ -28,6 +29,13 @@ if(${target} STREQUAL "linux")
target_compile_options(${COMPONENT_LIB} PUBLIC "-DLINUX_TARGET")
target_compile_options(${COMPONENT_LIB} PUBLIC --coverage)
target_link_libraries(${COMPONENT_LIB} PUBLIC --coverage)

find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()
endif()

if(CONFIG_NVS_ENCRYPTION)
Expand Down
21 changes: 6 additions & 15 deletions components/nvs_flash/host_test/fixtures/test_fixtures.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE 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.
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "nvs_partition.hpp"
#include "nvs.h"
Expand All @@ -29,8 +21,7 @@ extern "C" {

struct FixtureException : std::exception {
FixtureException(const std::string& msg) : msg(msg) { }

const char *what() {
const char *what() const noexcept {
return msg.c_str();
}

Expand Down
12 changes: 12 additions & 0 deletions components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)
# Freertos is included via common components. However, CATCH isn't compatible with the FreeRTOS component yet, hence
# using the FreeRTOS mock component.
# target.
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")

project(nvs_host_test)

add_dependencies(nvs_host_test.elf partition-table)
2 changes: 2 additions & 0 deletions components/nvs_flash/host_test/nvs_host_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| Supported Targets | Linux |
| ----------------- | ----- |
14 changes: 14 additions & 0 deletions components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
idf_component_register(SRCS "main.cpp"
"test_nvs.cpp"
"test_partition_manager.cpp"
"test_nvs_cxx_api.cpp"
"test_nvs_handle.cpp"
"test_nvs_initialization.cpp"
"test_nvs_storage.cpp"
INCLUDE_DIRS
"../../../src"
"../../../private_include"
"../../../../mbedtls/mbedtls/include"
"../../../../../tools/catch"
WHOLE_ARCHIVE
REQUIRES nvs_flash)
7 changes: 7 additions & 0 deletions components/nvs_flash/host_test/nvs_host_test/main/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nvs_partition.hpp"
#include "esp_private/partition_linux.h"
#include "nvs.h"

class PartitionEmulationFixture {
public:
PartitionEmulationFixture( uint32_t start_sector = 0,
uint32_t sector_size = 1,
const char *partition_name = NVS_DEFAULT_PART_NAME) :
esp_partition()
{

if (esp_partition_file_mmap((const uint8_t **) &p_part_desc_addr_start) != ESP_OK) {
throw ("Failed to initialize esp_partition_file_mmap");
}

esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE;
esp_partition.size = sector_size * SPI_FLASH_SEC_SIZE;
esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
strncpy(esp_partition.label, partition_name, PART_NAME_MAX_SIZE);
p_part = new nvs::NVSPartition(&esp_partition);
}

~PartitionEmulationFixture()
{
delete p_part;
esp_partition_file_munmap();
}

nvs::NVSPartition *part()
{
return p_part;
}

nvs::NVSPartition *p_part;
esp_partition_t esp_partition;
uint8_t *p_part_desc_addr_start;
};
Loading

0 comments on commit aad600c

Please sign in to comment.