diff --git a/.gitlab/ci/host-test.yml b/.gitlab/ci/host-test.yml index c646f0c235..b383c77b0a 100644 --- a/.gitlab/ci/host-test.yml +++ b/.gitlab/ci/host-test.yml @@ -34,22 +34,16 @@ check_public_headers: - IDF_TARGET=esp32p4 python tools/ci/check_public_headers.py --jobs 4 --prefix riscv32-esp-elf- - IDF_TARGET=esp32c61 python tools/ci/check_public_headers.py --jobs 4 --prefix riscv32-esp-elf- -test_nvs_on_host: - extends: .host_test_template - script: - - cd components/nvs_flash/test_nvs_host - - make test - test_nvs_coverage: extends: - .host_test_template - .rules:labels:nvs_coverage artifacts: paths: - - components/nvs_flash/test_nvs_host/coverage_report + - components/nvs_flash/host_test/nvs_host_test/coverage_report script: - - cd components/nvs_flash/test_nvs_host - - make coverage_report + - cd components/nvs_flash/host_test/nvs_host_test + - idf.py build coverage # the 'long' host tests take approx 11 hours on our current runners. Adding some margin here for possible CPU contention timeout: 18 hours diff --git a/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt b/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt index 658d311f49..4234d2bcd0 100644 --- a/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt +++ b/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt @@ -5,4 +5,19 @@ set(COMPONENTS main) # This test app doesn't require FreeRTOS, using mock instead list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") +idf_build_set_property(COMPILE_DEFINITIONS "NO_DEBUG_STORAGE" APPEND) project(nvs_host_test) + +add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/coverage_report/index.html" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + COMMAND gcovr --root $ENV{IDF_PATH}/components/nvs_flash --html-details + --exclude ${CMAKE_CURRENT_SOURCE_DIR}/managed_components/* + -o ${CMAKE_CURRENT_BINARY_DIR}/coverage_report/index.html ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generate coverage report" + ) + +add_custom_target(coverage + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + DEPENDS "coverage_report/index.html" + ) diff --git a/components/nvs_flash/host_test/nvs_host_test/README.md b/components/nvs_flash/host_test/nvs_host_test/README.md index 37c142df16..d7915b0122 100644 --- a/components/nvs_flash/host_test/nvs_host_test/README.md +++ b/components/nvs_flash/host_test/nvs_host_test/README.md @@ -1,2 +1,31 @@ | Supported Targets | Linux | | ----------------- | ----- | + +# To build +Navigate to the host test folder and run the IDF build command for linux target + +``` +cd $IDF_PATH +. ./export.sh +cd components/nvs_flash/host_test/nvs_host_test +idf.py --preview set-target linux +idf.py build +``` + +# To run tests locally +Navigate to the IDF root directory and run the binary from there. This ensures all calls to relative paths from test cases are resolved correctly. +This is limitation of ci pipeline as it was not possible to specify the working directory of the host tests. + +``` +cd $IDF_PATH +./components/nvs_flash/host_test/nvs_host_test/build/nvs_host_test.elf +``` + +# To create coverage report +After running the tests, the coverage report can be generated following way. + +``` +cd components/nvs_flash/host_test/nvs_host_test +idf.py build coverage +open ./build/coverage_report/index.html +``` \ No newline at end of file diff --git a/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt b/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt index 27959af8ee..d7260af485 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt +++ b/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt @@ -12,6 +12,9 @@ idf_component_register(SRCS "test_nvs.cpp" REQUIRES nvs_flash PRIV_REQUIRES spi_flash) +target_compile_options(${COMPONENT_LIB} PUBLIC --coverage) +target_link_libraries(${COMPONENT_LIB} PUBLIC --coverage) + if(CMAKE_C_COMPILER_ID MATCHES "Clang") target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++20) endif() diff --git a/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp b/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp index afc3dc5a74..431e889203 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp +++ b/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp @@ -3742,7 +3742,7 @@ TEST_CASE("check and read data from partition generated via manufacturing utilit } } -TEST_CASE("nvs multiple write with same key but different types", "[nvs][xxx]") +TEST_CASE("nvs multiple write with same key but different types", "[nvs]") { PartitionEmulationFixture f(0, 10); diff --git a/components/nvs_flash/test_nvs_host/Makefile b/components/nvs_flash/test_nvs_host/Makefile deleted file mode 100644 index 3ddb65185f..0000000000 --- a/components/nvs_flash/test_nvs_host/Makefile +++ /dev/null @@ -1,113 +0,0 @@ -TEST_PROGRAM=test_nvs -all: $(TEST_PROGRAM) - -SOURCE_FILES = \ - $(addprefix ../src/, \ - nvs_types.cpp \ - nvs_api.cpp \ - nvs_page.cpp \ - nvs_pagemanager.cpp \ - nvs_storage.cpp \ - nvs_item_hash_list.cpp \ - nvs_handle_simple.cpp \ - nvs_handle_locked.cpp \ - nvs_partition_manager.cpp \ - nvs_partition.cpp \ - nvs_encrypted_partition.cpp \ - nvs_cxx_api.cpp \ - nvs_platform.cpp \ - ) \ - spi_flash_emulation.cpp \ - test_compressed_enum_table.cpp \ - test_spi_flash_emulation.cpp \ - test_intrusive_list.cpp \ - test_nvs.cpp \ - test_nvs_partition.cpp \ - test_partition_manager.cpp \ - test_partition_linux.cpp \ - main.cpp - -SOURCE_FILES_C = ../../esp_rom/linux/esp_rom_crc.c esp_err_check_mock.c - -ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1) -COMPILER := clang -else -COMPILER := gcc -endif - -CPPFLAGS += -I../private_include -I../include -I../src -I../../heap/include -I../../esp_rom/include -I../../esp_rom/include/linux -I../../esp_rom/linux/include/linux -I../../log/include -I./ -I../../esp_common/include -I../../esp32/include -I ../../mbedtls/mbedtls/include -I ../../spi_flash/include -I ../../esp_partition/include -I ../../hal/include -I ../../xtensa/include -I ../../soc/linux/include -I ../../../tools/catch -fprofile-arcs -ftest-coverage -g2 -ggdb -CFLAGS += -fprofile-arcs -ftest-coverage -DLINUX_TARGET -DLINUX_HOST_LEGACY_TEST -CXXFLAGS += -std=c++11 -Wall -Werror -DLINUX_TARGET -DLINUX_HOST_LEGACY_TEST -LDFLAGS += -lstdc++ -Wall -fprofile-arcs -ftest-coverage - -ifeq ($(shell uname -s),Linux) -LDFLAGS += -lbsd -endif - -ifeq ($(COMPILER),clang) -CFLAGS += -fsanitize=address -CXXFLAGS += -fsanitize=address -LDFLAGS += -fsanitize=address -endif - -OBJ_FILES = $(SOURCE_FILES:.cpp=.o) -OBJ_FILES_C = $(SOURCE_FILES_C:.c=.o) - -COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) -MBEDTLS_LIB := ../../mbedtls/mbedtls/library/libmbedcrypto.a - -$(OBJ_FILES): %.o: %.cpp -$(OBJ_FILES_C): %.c: %.c - -$(MBEDTLS_LIB): - $(MAKE) -C ../../mbedtls/mbedtls/ lib - -$(TEST_PROGRAM): $(OBJ_FILES) $(OBJ_FILES_C) $(MBEDTLS_LIB) | clean-coverage - g++ -o $@ $^ $(LDFLAGS) - -$(OUTPUT_DIR): - mkdir -p $(OUTPUT_DIR) - -test: $(TEST_PROGRAM) - ./$(TEST_PROGRAM) -d yes exclude:[long] - -long-test: $(TEST_PROGRAM) - ./$(TEST_PROGRAM) -d yes - -$(COVERAGE_FILES): $(TEST_PROGRAM) long-test - -coverage.info: $(COVERAGE_FILES) - find ../src/ -name "*.gcno" -exec gcov -r -pb {} + - lcov --capture --directory ../src --no-external --output-file coverage.info - -coverage_report: coverage.info - genhtml coverage.info --output-directory coverage_report - @echo "Coverage report is in coverage_report/index.html" - -clean-coverage: - rm -f $(COVERAGE_FILES) *.gcov - rm -rf coverage_report/ - rm -f coverage.info - -clean: clean-coverage - $(MAKE) -C ../../mbedtls/mbedtls/ clean - rm -f $(OBJ_FILES) $(OBJ_FILES_C) $(TEST_PROGRAM) - rm -f ../nvs_partition_generator/partition_single_page.bin - rm -f ../nvs_partition_generator/partition_multipage_blob.bin - rm -f ../nvs_partition_generator/partition_encrypted.bin - rm -f ../nvs_partition_generator/partition_encrypted_hmac.bin - rm -f ../nvs_partition_generator/partition_encrypted_using_keygen.bin - rm -f ../nvs_partition_generator/partition_encrypted_using_keyfile.bin - rm -f ../nvs_partition_generator/partition_encrypted_using_keygen_hmac.bin - rm -f ../nvs_partition_generator/partition_decrypted.bin - rm -f ../nvs_partition_generator/partition_decrypted_hmac.bin - rm -f ../nvs_partition_generator/partition_encoded.bin - rm -f ../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin - rm -f ../nvs_partition_generator/Test-1-partition-encrypted.bin - rm -f ../nvs_partition_generator/Test-1-partition.bin - rm -f ../../../tools/mass_mfg/samples/sample_values_multipage_blob_created.csv - rm -f ../../../tools/mass_mfg/samples/sample_values_singlepage_blob_created.csv - - - -.PHONY: clean clean-coverage all test long-test diff --git a/components/nvs_flash/test_nvs_host/README.md b/components/nvs_flash/test_nvs_host/README.md deleted file mode 100644 index bad5b08cd8..0000000000 --- a/components/nvs_flash/test_nvs_host/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Build - -```bash -make -j 6 -``` - -# Run -* Run particular test case: -```bash -./test_nvs "" - -``` -* Run all quick tests: -```bash -./test_nvs -d yes exclude:[long] -``` - -* Run all tests (takes several hours) -```bash -./test_nvs -d yes -``` - diff --git a/components/nvs_flash/test_nvs_host/main.cpp b/components/nvs_flash/test_nvs_host/main.cpp deleted file mode 100644 index 0c7c351f43..0000000000 --- a/components/nvs_flash/test_nvs_host/main.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "catch.hpp" diff --git a/components/nvs_flash/test_nvs_host/sdkconfig.h b/components/nvs_flash/test_nvs_host/sdkconfig.h deleted file mode 100644 index 7a49e28578..0000000000 --- a/components/nvs_flash/test_nvs_host/sdkconfig.h +++ /dev/null @@ -1,6 +0,0 @@ -#define CONFIG_NVS_ENCRYPTION 1 -#define CONFIG_LOG_DEFAULT_LEVEL 3 -#define CONFIG_LOG_MAXIMUM_LEVEL 3 -#define CONFIG_LOG_TIMESTAMP_SOURCE_RTOS 1 -#define CONFIG_IDF_TARGET_LINUX 1 -#define CONFIG_NVS_ASSERT_ERROR_CHECK 1 diff --git a/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp b/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp deleted file mode 100644 index fc153edf30..0000000000 --- a/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "esp_partition.h" -#include "spi_flash_emulation.h" -#include "spi_flash_mmap.h" - -static SpiFlashEmulator* s_emulator = nullptr; - -void spi_flash_emulator_set(SpiFlashEmulator* e) -{ - s_emulator = e; -} - -esp_err_t esp_partition_erase_range(const esp_partition_t* partition, - size_t offset, size_t size) -{ - if (!s_emulator) { - return ESP_ERR_FLASH_OP_TIMEOUT; - } - - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - if (size % sec_size != 0) { - return ESP_ERR_INVALID_SIZE; - } - - if (offset % sec_size != 0) { - return ESP_ERR_INVALID_ARG; - } - - size_t start_sector = offset / sec_size; - size_t num_sectors = size / sec_size; - for (size_t sector = start_sector; sector < (start_sector + num_sectors); sector++) { - if (!s_emulator->erase(sector)) { - return ESP_ERR_FLASH_OP_FAIL; - } - } - - return ESP_OK; -} - -esp_err_t esp_partition_read(const esp_partition_t* partition, - size_t src_offset, void* dst, size_t size) -{ - if (!s_emulator) { - return ESP_ERR_FLASH_OP_TIMEOUT; - } - - if (!s_emulator->read(reinterpret_cast(dst), src_offset, size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; -} - -esp_err_t esp_partition_read_raw(const esp_partition_t* partition, - size_t src_offset, void* dst, size_t size) -{ - if (!s_emulator) { - return ESP_ERR_FLASH_OP_TIMEOUT; - } - - if (!s_emulator->read(reinterpret_cast(dst), src_offset, size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; -} - -esp_err_t esp_partition_write(const esp_partition_t* partition, - size_t dst_offset, const void* src, size_t size) -{ - if (!s_emulator) { - return ESP_ERR_FLASH_OP_TIMEOUT; - } - - if (!s_emulator->write(dst_offset, reinterpret_cast(src), size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; -} - -esp_err_t esp_partition_write_raw(const esp_partition_t* partition, - size_t dst_offset, const void* src, size_t size) -{ - if (!s_emulator) { - return ESP_ERR_FLASH_OP_TIMEOUT; - } - - if (!s_emulator->write(dst_offset, reinterpret_cast(src), size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; -} - -// timing data for ESP8266, 160MHz CPU frequency, 80MHz flash frequency -// all values in microseconds -// values are for block sizes starting at 4 bytes and going up to 4096 bytes -static size_t readTimes[] = {7, 5, 6, 7, 11, 18, 32, 60, 118, 231, 459}; -static size_t writeTimes[] = {19, 23, 35, 57, 106, 205, 417, 814, 1622, 3200, 6367}; -static size_t blockEraseTime = 37142; - - -static size_t timeInterp(uint32_t bytes, size_t* lut) -{ - const int lut_size = sizeof(readTimes)/sizeof(readTimes[0]); - int lz = __builtin_clz(bytes / 4); - int log_size = 32 - lz; - size_t x2 = 1 << (log_size + 2); - size_t y2 = lut[std::min(log_size, lut_size - 1)]; - size_t x1 = 1 << (log_size + 1); - size_t y1 = lut[log_size - 1]; - return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1; -} - -size_t SpiFlashEmulator::getReadOpTime(uint32_t bytes) -{ - return timeInterp(bytes, readTimes); -} - -size_t SpiFlashEmulator::getWriteOpTime(uint32_t bytes) -{ - return timeInterp(bytes, writeTimes); -} - -size_t SpiFlashEmulator::getEraseOpTime() -{ - return blockEraseTime; -} diff --git a/components/nvs_flash/test_nvs_host/spi_flash_emulation.h b/components/nvs_flash/test_nvs_host/spi_flash_emulation.h deleted file mode 100644 index 13db5ef17e..0000000000 --- a/components/nvs_flash/test_nvs_host/spi_flash_emulation.h +++ /dev/null @@ -1,245 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#ifndef spi_flash_emulation_h -#define spi_flash_emulation_h - -#include -#include -#include -#include -#include "esp_partition.h" -#include "catch.hpp" - -class SpiFlashEmulator; - -void spi_flash_emulator_set(SpiFlashEmulator*); - -class SpiFlashEmulator -{ -public: - SpiFlashEmulator(size_t sectorCount) : mUpperSectorBound(sectorCount) - { - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - mData.resize(sectorCount * sec_size / 4, 0xffffffff); - mEraseCnt.resize(sectorCount); - spi_flash_emulator_set(this); - } - - SpiFlashEmulator(const char *filename) - { - load(filename); - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - // At least one page should be free, hence we create mData of size of 2 sectors. - mData.resize(mData.size() + sec_size / 4, 0xffffffff); - mUpperSectorBound = mData.size() * 4 / sec_size; - spi_flash_emulator_set(this); - } - - ~SpiFlashEmulator() - { - spi_flash_emulator_set(nullptr); - } - - bool read(uint32_t* dest, size_t srcAddr, size_t size) const - { - if (srcAddr % 4 != 0 || - size % 4 != 0 || - srcAddr + size > mData.size() * 4) { - return false; - } - - copy(begin(mData) + srcAddr / 4, begin(mData) + (srcAddr + size) / 4, dest); - - ++mReadOps; - mReadBytes += size; - mTotalTime += getReadOpTime(static_cast(size)); - return true; - } - - bool write(size_t dstAddr, const uint32_t* src, size_t size) - { - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - uint32_t sectorNumber = dstAddr/sec_size; - if (sectorNumber < mLowerSectorBound || sectorNumber >= mUpperSectorBound) { - WARN("invalid flash operation detected: erase sector=" << sectorNumber); - return false; - } - - if (dstAddr % 4 != 0 || - size % 4 != 0 || - dstAddr + size > mData.size() * 4) { - return false; - } - - for (size_t i = 0; i < size / 4; ++i) { - if (mFailCountdown != SIZE_MAX && mFailCountdown-- == 0) { - return false; - } - - uint32_t sv = src[i]; - size_t pos = dstAddr / 4 + i; - uint32_t& dv = mData[pos]; - - if (((~dv) & sv) != 0) { // are we trying to set some 0 bits to 1? - WARN("invalid flash operation detected: dst=" << dstAddr << " size=" << size << " i=" << i); - return false; - } - - dv = sv; - } - ++mWriteOps; - mWriteBytes += size; - mTotalTime += getWriteOpTime(static_cast(size)); - return true; - } - - bool erase(size_t sectorNumber) - { - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - size_t offset = sectorNumber * sec_size / 4; - if (offset > mData.size()) { - return false; - } - - if (sectorNumber < mLowerSectorBound || sectorNumber >= mUpperSectorBound) { - WARN("invalid flash operation detected: erase sector=" << sectorNumber); - return false; - } - - if (mFailCountdown != SIZE_MAX && mFailCountdown-- == 0) { - return false; - } - - std::fill_n(begin(mData) + offset, sec_size / 4, 0xffffffff); - - ++mEraseOps; - mEraseCnt[sectorNumber]++; - mTotalTime += getEraseOpTime(); - return true; - } - - void randomize(uint32_t seed) - { - std::random_device rd; - std::mt19937 gen(rd()); - gen.seed(seed); - std::generate_n(mData.data(), mData.size(), gen); - } - - size_t size() const - { - return mData.size() * 4; - } - - const uint32_t* words() const - { - return mData.data(); - } - - const uint8_t* bytes() const - { - return reinterpret_cast(mData.data()); - } - - void load(const char* filename) - { - const uint32_t sector_size = esp_partition_get_main_flash_sector_size(); - size_t sec_size = sector_size; - FILE* f = fopen(filename, "rb"); - fseek(f, 0, SEEK_END); - off_t size = ftell(f); - assert(size % sec_size == 0); - mData.resize(size / sizeof(uint32_t)); - fseek(f, 0, SEEK_SET); - auto s = fread(mData.data(), sec_size, size / sec_size, f); - assert(s == static_cast(size / sec_size)); - fclose(f); - } - - void save(const char* filename) - { - const uint32_t sector_size = esp_partition_get_main_flash_sector_size(); - size_t sec_size = sector_size; - FILE* f = fopen(filename, "wb"); - auto n_sectors = mData.size() * sizeof(uint32_t) / sec_size; - auto s = fwrite(mData.data(), sec_size, n_sectors, f); - assert(s == n_sectors); - fclose(f); - } - - void clearStats() - { - mReadBytes = 0; - mWriteBytes = 0; - mEraseOps = 0; - mReadOps = 0; - mWriteOps = 0; - mTotalTime = 0; - } - - size_t getReadOps() const - { - return mReadOps; - } - size_t getWriteOps() const - { - return mWriteOps; - } - size_t getEraseOps() const - { - return mEraseOps; - } - size_t getReadBytes() const - { - return mReadBytes; - } - size_t getWriteBytes() const - { - return mWriteBytes; - } - size_t getTotalTime() const - { - return mTotalTime; - } - - void setBounds(uint32_t lowerSector, uint32_t upperSector) { - mLowerSectorBound = lowerSector; - mUpperSectorBound = upperSector; - } - - void failAfter(uint32_t count) { - mFailCountdown = count; - } - - size_t getSectorEraseCount(uint32_t sector) const { - return mEraseCnt[sector]; - } - -protected: - static size_t getReadOpTime(uint32_t bytes); - static size_t getWriteOpTime(uint32_t bytes); - static size_t getEraseOpTime(); - - - std::vector mData; - std::vector mEraseCnt; - - mutable size_t mReadOps = 0; - mutable size_t mWriteOps = 0; - mutable size_t mReadBytes = 0; - mutable size_t mWriteBytes = 0; - mutable size_t mEraseOps = 0; - mutable size_t mTotalTime = 0; - size_t mLowerSectorBound = 0; - size_t mUpperSectorBound = 0; - - size_t mFailCountdown = SIZE_MAX; - -}; - - - -#endif /* spi_flash_emulation_h */ diff --git a/components/nvs_flash/test_nvs_host/test_compressed_enum_table.cpp b/components/nvs_flash/test_nvs_host/test_compressed_enum_table.cpp deleted file mode 100644 index 8e9781637f..0000000000 --- a/components/nvs_flash/test_nvs_host/test_compressed_enum_table.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "catch.hpp" -#include "compressed_enum_table.hpp" -#include - -TEST_CASE("test if CompressedEnumTable works as expected", "[enumtable]") -{ - - enum class TEnum1 : uint32_t { - ZERO = 0, - ONE = 1, - TWO = 2, - THREE = 3, - }; - CompressedEnumTable table; - memset(table.data(), 0xff, table.byteSize()); - TEnum1 tmp; - for (size_t i = 0; i < table.count(); ++i) { - CHECK(table.get(i, &tmp) == ESP_OK); - CHECK(tmp == TEnum1::THREE); - } - - CHECK(table.set(0, TEnum1::ONE) == ESP_OK); - CHECK(table.set(1, TEnum1::TWO) == ESP_OK); - CHECK(table.set(2, TEnum1::ZERO) == ESP_OK); - CHECK(table.set(3, TEnum1::ONE) == ESP_OK); - CHECK(table.set(4, TEnum1::TWO) == ESP_OK); - CHECK(table.set(5, TEnum1::ZERO) == ESP_OK); - CHECK(table.set(6, TEnum1::ONE) == ESP_OK); - CHECK(table.set(7, TEnum1::TWO) == ESP_OK); - CHECK(table.set(8, TEnum1::ZERO) == ESP_OK); - CHECK(table.set(9, TEnum1::ZERO) == ESP_OK); - CHECK(table.set(10, TEnum1::ONE) == ESP_OK); - CHECK(table.set(11, TEnum1::TWO) == ESP_OK); -// table.set(12, ... - CHECK(table.set(13, TEnum1::ZERO) == ESP_OK); - CHECK(table.set(14, TEnum1::ONE) == ESP_OK); - CHECK(table.set(15, TEnum1::TWO) == ESP_OK); - - // b10010011100100001001001001001001 - // h 9 3 9 0 9 2 4 9 - - CHECK(table.data()[0] == 0x93909249); -} diff --git a/components/nvs_flash/test_nvs_host/test_fixtures.hpp b/components/nvs_flash/test_nvs_host/test_fixtures.hpp deleted file mode 100644 index 4ca5720dc5..0000000000 --- a/components/nvs_flash/test_nvs_host/test_fixtures.hpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "nvs_partition.hpp" -#include "nvs_encrypted_partition.hpp" -#include "spi_flash_emulation.h" -#include "spi_flash_mmap.h" -#include "nvs.h" - -class PartitionEmulation : public nvs::Partition { -public: - PartitionEmulation(SpiFlashEmulator *spi_flash_emulator, - uint32_t address, - uint32_t size, - const char *partition_name = NVS_DEFAULT_PART_NAME) - : partition_name(partition_name), flash_emu(spi_flash_emulator), address(address), size(size) - { - assert(partition_name); - assert(flash_emu); - assert(size); - readonly = false; - } - - const char *get_partition_name() override - { - return partition_name; - } - - esp_err_t read_raw(size_t src_offset, void* dst, size_t size) override - { - if (!flash_emu->read(reinterpret_cast(dst), src_offset, size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; - } - - esp_err_t read(size_t src_offset, void* dst, size_t size) override - { - if (!flash_emu->read(reinterpret_cast(dst), src_offset, size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; - } - - esp_err_t write_raw(size_t dst_offset, const void* src, size_t size) override - { - if (!flash_emu->write(dst_offset, reinterpret_cast(src), size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; - } - - esp_err_t write(size_t dst_offset, const void* src, size_t size) override - { - if (!flash_emu->write(dst_offset, reinterpret_cast(src), size)) { - return ESP_ERR_FLASH_OP_FAIL; - } - - return ESP_OK; - } - - esp_err_t erase_range(size_t dst_offset, size_t size) override - { - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - if (size % sec_size != 0) { - return ESP_ERR_INVALID_SIZE; - } - - if (dst_offset % sec_size != 0) { - return ESP_ERR_INVALID_ARG; - } - - size_t start_sector = dst_offset / sec_size; - size_t num_sectors = size / sec_size; - for (size_t sector = start_sector; sector < (start_sector + num_sectors); sector++) { - if (!flash_emu->erase(sector)) { - return ESP_ERR_FLASH_OP_FAIL; - } - } - - return ESP_OK; - } - - uint32_t get_address() override - { - return address; - } - - uint32_t get_size() override - { - return size; - } - - bool get_readonly() override - { - return readonly; - } - -private: - const char *partition_name; - - SpiFlashEmulator *flash_emu; - - uint32_t address; - - uint32_t size; - - bool readonly; -}; - -struct PartitionEmulationFixture { - PartitionEmulationFixture(uint32_t start_sector = 0, - uint32_t sector_size = 1, - const char *partition_name = NVS_DEFAULT_PART_NAME) - : emu(start_sector + sector_size), - part(&emu, start_sector * esp_partition_get_main_flash_sector_size(), sector_size * esp_partition_get_main_flash_sector_size(), partition_name) { - } - - ~PartitionEmulationFixture() { } - - SpiFlashEmulator emu; - - PartitionEmulation part; -}; - -struct EncryptedPartitionFixture { - EncryptedPartitionFixture(nvs_sec_cfg_t *cfg, - uint32_t start_sector = 0, - uint32_t sector_size = 1, - const char *partition_name = NVS_DEFAULT_PART_NAME) - : esp_partition(), emu(start_sector + sector_size), - part(&esp_partition) { - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - esp_partition.address = start_sector * sec_size; - esp_partition.size = sector_size * sec_size; - strncpy(esp_partition.label, partition_name, PART_NAME_MAX_SIZE); - assert(part.init(cfg) == ESP_OK); - } - - ~EncryptedPartitionFixture() { } - - esp_partition_t esp_partition; - - SpiFlashEmulator emu; - - nvs::NVSEncryptedPartition part; -}; diff --git a/components/nvs_flash/test_nvs_host/test_intrusive_list.cpp b/components/nvs_flash/test_nvs_host/test_intrusive_list.cpp deleted file mode 100644 index dea63f3c33..0000000000 --- a/components/nvs_flash/test_nvs_host/test_intrusive_list.cpp +++ /dev/null @@ -1,212 +0,0 @@ -// 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. -#include "catch.hpp" -#include -#include -#include "intrusive_list.h" - -struct TestNode : public intrusive_list_node { - TestNode(const char* name_ = "", int num_ = 0) : num(num_) - { - strncpy(name, name_, sizeof(name) - 1); - name[sizeof(name) - 1] = 0; - } - char name[32]; - int num; -}; - -typedef intrusive_list TestList; - - -TEST_CASE("can add items to the list", "[list]") -{ - TestList list; - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - list.push_back(&n1); - REQUIRE(list.begin()->num == 1); - REQUIRE(list.front().num == 1); - REQUIRE(list.back().num == 1); - list.push_front(&n2); - REQUIRE(list.begin()->num == 2); - REQUIRE(list.front().num == 2); - REQUIRE(list.back().num == 1); - - list.insert(list.begin(), &n3); - REQUIRE(list.begin()->num == 3); - REQUIRE(list.front().num == 3); - REQUIRE(list.back().num == 1); - - - auto second = ++list.begin(); - REQUIRE(second->num == 2); - - second++; - REQUIRE(second->num == 1); -} - -TEST_CASE("can iterate over items", "[list]") -{ - TestList list; - - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - int val = 1; - for (auto it = std::begin(list); it != std::end(list); ++it) { - REQUIRE(it->num == val); - ++val; - } -} - -TEST_CASE("iterator's prefix and postfix increments and decrements behave as expected", "[list]") -{ - TestList list; - - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - auto it = std::begin(list); - REQUIRE((++it)->num == 2); - REQUIRE(it++->num == 2); - REQUIRE((--it)->num == 2); - REQUIRE(it--->num == 2); -} - -TEST_CASE("can pop_front from the list", "[list]") -{ - TestList list; - - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - list.pop_front(); - list.pop_front(); - list.pop_front(); - - REQUIRE(std::begin(list) == std::end(list)); -} - - -TEST_CASE("can erase first item in the list", "[list]") -{ - TestList list; - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - list.erase(std::begin(list)); - - REQUIRE(list.front().num == 2); - REQUIRE(list.back().num == 3); -} - - -TEST_CASE("can erase last item in the list", "[list]") -{ - TestList list; - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - list.erase(&list.back()); - - REQUIRE(list.front().num == 1); - REQUIRE(list.back().num == 2); -} - -TEST_CASE("can erase item in the middle of the list", "[list]") -{ - TestList list; - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - list.erase(++std::begin(list)); - - REQUIRE(list.front().num == 1); - REQUIRE(list.back().num == 3); -} - -TEST_CASE("can erase all items in the list", "[list]") -{ - TestList list; - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - - list.push_back(&n1); - list.push_back(&n2); - list.push_back(&n3); - - list.erase(std::begin(list)); - list.erase(std::begin(list)); - list.erase(std::begin(list)); - REQUIRE(std::begin(list) == std::end(list)); -} - - -TEST_CASE("can erase all items in the list using clear method", "[list]") -{ - TestList list; - TestNode n1("one", 1); - TestNode n2("two", 2); - TestNode n3("three", 3); - TestNode n4("four", 4); - TestNode n5("five", 5); - TestNode n6("six", 6); - - - list.push_back(&n1); - list.push_back(&n2); - list.insert(++list.begin(), &n3); - list.insert(++list.begin(), &n4); - list.push_front(&n5); - list.insert(list.begin(), &n6); - - list.clear(); - - REQUIRE(std::begin(list) == std::end(list)); -} diff --git a/components/nvs_flash/test_nvs_host/test_nvs.cpp b/components/nvs_flash/test_nvs_host/test_nvs.cpp deleted file mode 100644 index 06d312e483..0000000000 --- a/components/nvs_flash/test_nvs_host/test_nvs.cpp +++ /dev/null @@ -1,1251 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "catch.hpp" -#include "nvs.hpp" -#include "nvs_test_api.h" -#include "sdkconfig.h" -#include "spi_flash_emulation.h" -#include "nvs_partition_manager.hpp" -#include "nvs_partition.hpp" -#include "mbedtls/aes.h" -#include "mbedtls/md.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include "test_fixtures.hpp" - -#define TEST_ESP_ERR(rc, res) CHECK((rc) == (res)) -#define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK) - -stringstream s_perf; - -bool memeq(void *a, size_t a_len, void *b, size_t b_len) -{ - if (a_len != b_len) { - return false; - } - return memcmp(a, b, a_len) == 0; -} - -static void check_nvs_part_gen_args(SpiFlashEmulator *spi_flash_emulator, - char const *part_name, - int size, - char const *filename, - bool is_encr, - nvs_sec_cfg_t *xts_cfg) -{ - nvs_handle_t handle; - - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - esp_partition_t esp_part; - esp_part.encrypted = false; // we're not testing generic flash encryption here, only the legacy NVS encryption - esp_part.address = 0; - esp_part.size = size * sec_size; - strncpy(esp_part.label, part_name, PART_NAME_MAX_SIZE); - unique_ptr part; - - if (is_encr) { - nvs::NVSEncryptedPartition *enc_part = new (std::nothrow) nvs::NVSEncryptedPartition(&esp_part); - REQUIRE(enc_part != nullptr); - TEST_ESP_OK(enc_part->init(xts_cfg)); - part.reset(enc_part); - } else { - part.reset(new PartitionEmulation(spi_flash_emulator, 0, size, part_name)); - } - - TEST_ESP_OK( nvs::NVSPartitionManager::get_instance()->init_custom(part.get(), 0, size) ); - - TEST_ESP_OK( nvs_open_from_partition(part_name, "dummyNamespace", NVS_READONLY, &handle)); - uint8_t u8v; - TEST_ESP_OK( nvs_get_u8(handle, "dummyU8Key", &u8v)); - CHECK(u8v == 127); - int8_t i8v; - TEST_ESP_OK( nvs_get_i8(handle, "dummyI8Key", &i8v)); - CHECK(i8v == -128); - uint16_t u16v; - TEST_ESP_OK( nvs_get_u16(handle, "dummyU16Key", &u16v)); - CHECK(u16v == 32768); - uint32_t u32v; - TEST_ESP_OK( nvs_get_u32(handle, "dummyU32Key", &u32v)); - CHECK(u32v == 4294967295); - int32_t i32v; - TEST_ESP_OK( nvs_get_i32(handle, "dummyI32Key", &i32v)); - CHECK(i32v == -2147483648); - - char string_buf[256]; - const char test_str[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" - "Fusce quis risus justo.\n" - "Suspendisse egestas in nisi sit amet auctor.\n" - "Pellentesque rhoncus dictum sodales.\n" - "In justo erat, viverra at interdum eget, interdum vel dui."; - size_t str_len = sizeof(test_str); - TEST_ESP_OK( nvs_get_str(handle, "dummyStringKey", string_buf, &str_len)); - CHECK(strncmp(string_buf, test_str, str_len) == 0); - - char buf[64] = {0}; - uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef}; - size_t buflen = 64; - int j; - TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen)); - CHECK(memeq(buf, buflen, hexdata, sizeof(hexdata))); - - uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'}; - TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen)); - CHECK(memeq(buf, buflen, base64data, sizeof(base64data))); - - buflen = 64; - uint8_t hexfiledata[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; - TEST_ESP_OK( nvs_get_blob(handle, "hexFileKey", buf, &buflen)); - CHECK(memeq(buf, buflen, hexfiledata, sizeof(hexfiledata))); - - buflen = 64; - const char strfiledata[64] = "abcdefghijklmnopqrstuvwxyz"; - TEST_ESP_OK( nvs_get_str(handle, "stringFileKey", buf, &buflen)); - CHECK(strcmp(buf, strfiledata) == 0); - - char bin_data[5200]; - size_t bin_len = sizeof(bin_data); - char binfiledata[5200]; - ifstream file; - file.open(filename); - file.read(binfiledata,5200); - size_t binfile_len = file.gcount(); - TEST_ESP_OK( nvs_get_blob(handle, "binFileKey", bin_data, &bin_len)); - CHECK(memeq(bin_data, bin_len, binfiledata, binfile_len)); - - file.close(); - - nvs_close(handle); - - TEST_ESP_OK(nvs_flash_deinit_partition(part_name)); -} - -static void check_nvs_part_gen_args_mfg(SpiFlashEmulator *spi_flash_emulator, - char const *part_name, - int size, - char const *filename, - bool is_encr, - nvs_sec_cfg_t *xts_cfg) -{ - nvs_handle_t handle; - - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - esp_partition_t esp_part; - esp_part.encrypted = false; // we're not testing generic flash encryption here, only the legacy NVS encryption - esp_part.address = 0; - esp_part.size = size * sec_size; - strncpy(esp_part.label, part_name, PART_NAME_MAX_SIZE); - unique_ptr part; - - if (is_encr) { - nvs::NVSEncryptedPartition *enc_part = new (std::nothrow) nvs::NVSEncryptedPartition(&esp_part); - REQUIRE(enc_part != nullptr); - TEST_ESP_OK(enc_part->init(xts_cfg)); - part.reset(enc_part); - } else { - part.reset(new PartitionEmulation(spi_flash_emulator, 0, size, part_name)); - } - - TEST_ESP_OK( nvs::NVSPartitionManager::get_instance()->init_custom(part.get(), 0, size) ); - - TEST_ESP_OK( nvs_open_from_partition(part_name, "dummyNamespace", NVS_READONLY, &handle)); - uint8_t u8v; - TEST_ESP_OK( nvs_get_u8(handle, "dummyU8Key", &u8v)); - CHECK(u8v == 127); - int8_t i8v; - TEST_ESP_OK( nvs_get_i8(handle, "dummyI8Key", &i8v)); - CHECK(i8v == -128); - uint16_t u16v; - TEST_ESP_OK( nvs_get_u16(handle, "dummyU16Key", &u16v)); - CHECK(u16v == 32768); - uint32_t u32v; - TEST_ESP_OK( nvs_get_u32(handle, "dummyU32Key", &u32v)); - CHECK(u32v == 4294967295); - int32_t i32v; - TEST_ESP_OK( nvs_get_i32(handle, "dummyI32Key", &i32v)); - CHECK(i32v == -2147483648); - - char buf[64] = {0}; - size_t buflen = 64; - TEST_ESP_OK( nvs_get_str(handle, "dummyStringKey", buf, &buflen)); - CHECK(strncmp(buf, "0A:0B:0C:0D:0E:0F", buflen) == 0); - - uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef}; - buflen = 64; - int j; - TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen)); - CHECK(memeq(buf, buflen, hexdata, sizeof(hexdata))); - - uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'}; - TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen)); - CHECK(memeq(buf, buflen, base64data, sizeof(base64data))); - - buflen = 64; - uint8_t hexfiledata[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; - TEST_ESP_OK( nvs_get_blob(handle, "hexFileKey", buf, &buflen)); - CHECK(memeq(buf, buflen, hexfiledata, sizeof(hexfiledata))); - - buflen = 64; - const char strfiledata[64] = "abcdefghijklmnopqrstuvwxyz"; - TEST_ESP_OK( nvs_get_str(handle, "stringFileKey", buf, &buflen)); - CHECK(strcmp(buf, strfiledata) == 0); - - char bin_data[5200]; - size_t bin_len = sizeof(bin_data); - char binfiledata[5200]; - ifstream file; - file.open(filename); - file.read(binfiledata,5200); - size_t binfile_len = file.gcount(); - TEST_ESP_OK( nvs_get_blob(handle, "binFileKey", bin_data, &bin_len)); - CHECK(memeq(bin_data, bin_len, binfiledata, binfile_len)); - - file.close(); - - nvs_close(handle); - - TEST_ESP_OK(nvs_flash_deinit_partition(part_name)); -} - -#if CONFIG_NVS_ENCRYPTION -TEST_CASE("check underlying xts code for 32-byte size sector encryption", "[nvs]") -{ - auto toHex = [](char ch) { - if(ch >= '0' && ch <= '9') - return ch - '0'; - else if(ch >= 'a' && ch <= 'f') - return ch - 'a' + 10; - else if(ch >= 'A' && ch <= 'F') - return ch - 'A' + 10; - else - return 0; - }; - - auto toHexByte = [toHex](char* c) { - return 16 * toHex(c[0]) + toHex(c[1]); - }; - - auto toHexStream = [toHexByte](char* src, uint8_t* dest) { - uint32_t cnt =0; - char* p = src; - while(*p != '\0' && *(p + 1) != '\0') - { - dest[cnt++] = toHexByte(p); p += 2; - } - }; - - uint8_t eky_hex[2 * NVS_KEY_SIZE]; - uint8_t ptxt_hex[nvs::Page::ENTRY_SIZE], ctxt_hex[nvs::Page::ENTRY_SIZE], ba_hex[16]; - mbedtls_aes_xts_context ectx[1]; - mbedtls_aes_xts_context dctx[1]; - - char eky[][2 * NVS_KEY_SIZE + 1] = { - "0000000000000000000000000000000000000000000000000000000000000000", - "1111111111111111111111111111111111111111111111111111111111111111" - }; - char tky[][2 * NVS_KEY_SIZE + 1] = { - "0000000000000000000000000000000000000000000000000000000000000000", - "2222222222222222222222222222222222222222222222222222222222222222" - }; - char blk_addr[][2*16 + 1] = { - "00000000000000000000000000000000", - "33333333330000000000000000000000" - }; - - char ptxt[][2 * nvs::Page::ENTRY_SIZE + 1] = { - "0000000000000000000000000000000000000000000000000000000000000000", - "4444444444444444444444444444444444444444444444444444444444444444" - }; - char ctxt[][2 * nvs::Page::ENTRY_SIZE + 1] = { - "d456b4fc2e620bba6ffbed27b956c9543454dd49ebd8d8ee6f94b65cbe158f73", - "e622334f184bbce129a25b2ac76b3d92abf98e22df5bdd15af471f3db8946a85" - }; - - mbedtls_aes_xts_init(ectx); - mbedtls_aes_xts_init(dctx); - - for(uint8_t cnt = 0; cnt < sizeof(eky)/sizeof(eky[0]); cnt++) { - toHexStream(eky[cnt], eky_hex); - toHexStream(tky[cnt], &eky_hex[NVS_KEY_SIZE]); - toHexStream(ptxt[cnt], ptxt_hex); - toHexStream(ctxt[cnt], ctxt_hex); - toHexStream(blk_addr[cnt], ba_hex); - - CHECK(!mbedtls_aes_xts_setkey_enc(ectx, eky_hex, 2 * NVS_KEY_SIZE * 8)); - CHECK(!mbedtls_aes_xts_setkey_enc(dctx, eky_hex, 2 * NVS_KEY_SIZE * 8)); - - CHECK(!mbedtls_aes_crypt_xts(ectx, MBEDTLS_AES_ENCRYPT, nvs::Page::ENTRY_SIZE, ba_hex, ptxt_hex, ptxt_hex)); - - CHECK(!memcmp(ptxt_hex, ctxt_hex, nvs::Page::ENTRY_SIZE)); - } -} - -TEST_CASE("test nvs apis with encryption enabled", "[nvs]") -{ - nvs_handle_t handle_1; - const uint32_t NVS_FLASH_SECTOR = 6; - const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; - - nvs_sec_cfg_t xts_cfg; - for(int count = 0; count < NVS_KEY_SIZE; count++) { - xts_cfg.eky[count] = 0x11; - xts_cfg.tky[count] = 0x22; - } - EncryptedPartitionFixture fixture(&xts_cfg, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN); - fixture.emu.randomize(100); - fixture.emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN); - - for (uint16_t i = NVS_FLASH_SECTOR; i - init_custom(&fixture.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)); - - TEST_ESP_ERR(nvs_open("namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND); - - TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle_1)); - TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x12345678)); - TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x23456789)); - - nvs_handle_t handle_2; - TEST_ESP_OK(nvs_open("namespace2", NVS_READWRITE, &handle_2)); - TEST_ESP_OK(nvs_set_i32(handle_2, "foo", 0x3456789a)); - const char* str = "value 0123456789abcdef0123456789abcdef"; - TEST_ESP_OK(nvs_set_str(handle_2, "key", str)); - - int32_t v1; - TEST_ESP_OK(nvs_get_i32(handle_1, "foo", &v1)); - CHECK(0x23456789 == v1); - - int32_t v2; - TEST_ESP_OK(nvs_get_i32(handle_2, "foo", &v2)); - CHECK(0x3456789a == v2); - - char buf[strlen(str) + 1]; - size_t buf_len = sizeof(buf); - - size_t buf_len_needed; - TEST_ESP_OK(nvs_get_str(handle_2, "key", NULL, &buf_len_needed)); - CHECK(buf_len_needed == buf_len); - - size_t buf_len_short = buf_len - 1; - TEST_ESP_ERR(ESP_ERR_NVS_INVALID_LENGTH, nvs_get_str(handle_2, "key", buf, &buf_len_short)); - CHECK(buf_len_short == buf_len); - - size_t buf_len_long = buf_len + 1; - TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len_long)); - CHECK(buf_len_long == buf_len); - - TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len)); - - CHECK(0 == strcmp(buf, str)); - nvs_close(handle_1); - nvs_close(handle_2); - TEST_ESP_OK(nvs_flash_deinit()); -} - -TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled", "[nvs_part_gen]") -{ - int status; - int childpid = fork(); - if (childpid == 0) { - exit(execlp("cp", " cp", - "-rf", - "../nvs_partition_generator/testdata", - ".", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../nvs_partition_generator/sample_multipage_blob.csv", - "partition_encrypted.bin", - "0x4000", - "--inputkey", - "../nvs_partition_generator/testdata/sample_encryption_keys.bin", - "--outdir", - "../nvs_partition_generator", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - } - } - - SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted.bin"); - - nvs_sec_cfg_t cfg; - for (int count = 0; count < NVS_KEY_SIZE; count++) { - cfg.eky[count] = 0x11; - cfg.tky[count] = 0x22; - } - - check_nvs_part_gen_args(&emu, NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("rm", " rm", - "-rf", - "testdata", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - -} - -TEST_CASE("test decrypt functionality for encrypted data", "[nvs_part_gen]") -{ - - //retrieving the temporary test data - int status = system("cp -rf ../nvs_partition_generator/testdata ."); - CHECK(status == 0); - - //encoding data from sample_multipage_blob.csv - status = system("python ../nvs_partition_generator/nvs_partition_gen.py generate ../nvs_partition_generator/sample_multipage_blob.csv partition_encoded.bin 0x5000 --outdir ../nvs_partition_generator"); - CHECK(status == 0); - - //encrypting data from sample_multipage_blob.csv - status = system("python ../nvs_partition_generator/nvs_partition_gen.py encrypt ../nvs_partition_generator/sample_multipage_blob.csv partition_encrypted.bin 0x5000 --inputkey ../nvs_partition_generator/testdata/sample_encryption_keys.bin --outdir ../nvs_partition_generator"); - CHECK(status == 0); - - //encrypting data from sample_multipage_blob.csv (hmac-based scheme) - status = system("python ../nvs_partition_generator/nvs_partition_gen.py encrypt ../nvs_partition_generator/sample_multipage_blob.csv partition_encrypted_hmac.bin 0x5000 --keygen --key_protect_hmac --kp_hmac_inputkey ../nvs_partition_generator/testdata/sample_hmac_key.bin --outdir ../nvs_partition_generator"); - CHECK(status == 0); - - //decrypting data from partition_encrypted.bin - status = system("python ../nvs_partition_generator/nvs_partition_gen.py decrypt ../nvs_partition_generator/partition_encrypted.bin ../nvs_partition_generator/testdata/sample_encryption_keys.bin ../nvs_partition_generator/partition_decrypted.bin"); - CHECK(status == 0); - - status = system("diff ../nvs_partition_generator/partition_decrypted.bin ../nvs_partition_generator/partition_encoded.bin"); - CHECK(status == 0); - - //decrypting data from partition_encrypted_hmac.bin - status = system("python ../nvs_partition_generator/nvs_partition_gen.py decrypt ../nvs_partition_generator/partition_encrypted_hmac.bin ../nvs_partition_generator/testdata/sample_encryption_keys_hmac.bin ../nvs_partition_generator/partition_decrypted_hmac.bin"); - CHECK(status == 0); - - status = system("diff ../nvs_partition_generator/partition_decrypted_hmac.bin ../nvs_partition_generator/partition_encoded.bin"); - CHECK(status == 0); - - CHECK(WEXITSTATUS(status) == 0); - - - //cleaning up the temporary test data - status = system("rm -rf testdata"); - CHECK(status == 0); - -} - -TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled using keygen", "[nvs_part_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("cp", " cp", - "-rf", - "../nvs_partition_generator/testdata", - ".", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - - if (childpid == 0) { - exit(execlp("rm", " rm", - "-rf", - "../nvs_partition_generator/keys", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../nvs_partition_generator/sample_multipage_blob.csv", - "partition_encrypted_using_keygen.bin", - "0x4000", - "--keygen", - "--outdir", - "../nvs_partition_generator", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - } - } - - - DIR *dir; - struct dirent *file; - char *filename; - char *files; - char *file_ext; - - dir = opendir("../nvs_partition_generator/keys"); - while ((file = readdir(dir)) != NULL) { - filename = file->d_name; - files = strrchr(filename, '.'); - if (files != NULL) { - file_ext = files + 1; - if (strncmp(file_ext, "bin", 3) == 0) { - break; - } - } - } - - std::string encr_file = std::string("../nvs_partition_generator/keys/") + std::string(filename); - SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted_using_keygen.bin"); - - char buffer[64]; - FILE *fp; - - fp = fopen(encr_file.c_str(), "rb"); - fread(buffer, sizeof(buffer), 1, fp); - - fclose(fp); - - nvs_sec_cfg_t cfg; - - for (int count = 0; count < NVS_KEY_SIZE; count++) { - cfg.eky[count] = buffer[count] & 255; - cfg.tky[count] = buffer[count + 32] & 255; - } - - check_nvs_part_gen_args(&emu, NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg); - -} - -TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled using inputkey", "[nvs_part_gen]") -{ - int childpid = fork(); - int status; - - DIR *dir; - struct dirent *file; - char *filename; - char *files; - char *file_ext; - - dir = opendir("../nvs_partition_generator/keys"); - while ((file = readdir(dir)) != NULL) { - filename = file->d_name; - files = strrchr(filename, '.'); - if (files != NULL) { - file_ext = files + 1; - if (strncmp(file_ext, "bin", 3) == 0) { - break; - } - } - } - - std::string encr_file = std::string("../nvs_partition_generator/keys/") + std::string(filename); - - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../nvs_partition_generator/sample_multipage_blob.csv", - "partition_encrypted_using_keyfile.bin", - "0x4000", - "--inputkey", - encr_file.c_str(), - "--outdir", - "../nvs_partition_generator", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - } - - SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted_using_keyfile.bin"); - - char buffer[64]; - FILE *fp; - - fp = fopen(encr_file.c_str(), "rb"); - fread(buffer, sizeof(buffer), 1, fp); - - fclose(fp); - - nvs_sec_cfg_t cfg; - - for (int count = 0; count < NVS_KEY_SIZE; count++) { - cfg.eky[count] = buffer[count] & 255; - cfg.tky[count] = buffer[count + 32] & 255; - } - - check_nvs_part_gen_args(&emu, NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("rm", " rm", - "-rf", - "../nvs_partition_generator/keys", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - - if (childpid == 0) { - exit(execlp("rm", " rm", - "-rf", - "testdata", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - } - } - -} - -static void compute_nvs_keys_with_hmac(nvs_sec_cfg_t *cfg, void *hmac_key) -{ - unsigned char key_bytes[32] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, - 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, - 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20 }; - if (hmac_key != NULL){ - memcpy(key_bytes, hmac_key, 32); - } - - unsigned char ekey_seed[32], tkey_seed[32]; - - for (unsigned int i = 0; i < sizeof(ekey_seed); i+=4) { - ekey_seed[i] = 0x5A; - ekey_seed[i + 1] = 0x5A; - ekey_seed[i + 2] = 0xBE; - ekey_seed[i + 3] = 0xAE; - } - - for (unsigned int i = 0; i < sizeof(tkey_seed); i+=4) { - tkey_seed[i] = 0xA5; - tkey_seed[i + 1] = 0xA5; - tkey_seed[i + 2] = 0xDE; - tkey_seed[i + 3] = 0xCE; - } - - const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256; - - mbedtls_md_context_t ctx; - mbedtls_md_init(&ctx); - - const mbedtls_md_info_t *info = mbedtls_md_info_from_type(alg); - mbedtls_md_setup(&ctx, info, 1); - mbedtls_md_hmac_starts(&ctx, key_bytes, sizeof(key_bytes)); - - mbedtls_md_hmac_update(&ctx, ekey_seed, sizeof(ekey_seed)); - mbedtls_md_hmac_finish(&ctx, cfg->eky); - - mbedtls_md_hmac_reset(&ctx); - mbedtls_md_hmac_update(&ctx, tkey_seed, sizeof(tkey_seed)); - mbedtls_md_hmac_finish(&ctx, cfg->tky); - - assert(memcmp(cfg->eky, cfg->tky, NVS_KEY_SIZE)); - - mbedtls_md_free(&ctx); -} - -TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled using keygen (user-provided HMAC-key)", "[nvs_part_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("cp", " cp", - "-rf", - "../nvs_partition_generator/testdata", - ".", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - - if (childpid == 0) { - exit(execlp("rm", " rm", - "-rf", - "../nvs_partition_generator/keys", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../nvs_partition_generator/sample_multipage_blob.csv", - "partition_encrypted_using_keygen_hmac.bin", - "0x4000", - "--keygen", - "--key_protect_hmac", - "--kp_hmac_inputkey", - "../nvs_partition_generator/testdata/sample_hmac_key.bin", - "--outdir", - "../nvs_partition_generator", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - } - } - - SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted_using_keygen_hmac.bin"); - - nvs_sec_cfg_t cfg; - compute_nvs_keys_with_hmac(&cfg, NULL); - - check_nvs_part_gen_args(&emu, NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg); - -} - -TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled using keygen (dynamically generated HMAC-key)", "[nvs_part_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("cp", " cp", - "-rf", - "../nvs_partition_generator/testdata", - ".", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - - if (childpid == 0) { - exit(execlp("rm", " rm", - "-rf", - "../nvs_partition_generator/keys", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../nvs_partition_generator/sample_multipage_blob.csv", - "partition_encrypted_using_keygen_hmac.bin", - "0x4000", - "--keygen", - "--key_protect_hmac", - "--kp_hmac_keygen", - "--outdir", - "../nvs_partition_generator", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - } - } - - - DIR *dir; - struct dirent *file; - char *filename; - char *files; - char *file_ext; - char *hmac_key_file; - - dir = opendir("../nvs_partition_generator/keys"); - while ((file = readdir(dir)) != NULL) { - filename = file->d_name; - file_ext = NULL; - files = strrchr(filename, '.'); - if (files != NULL) { - file_ext = files + 1; - if (strncmp(file_ext, "bin", 3) != 0) { - continue; - } - } - if (strstr(filename, "hmac") != NULL) { - hmac_key_file = filename; - } - } - - std::string hmac_key_path = std::string("../nvs_partition_generator/keys/") + std::string(hmac_key_file); - SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted_using_keygen_hmac.bin"); - - char hmac_key_buf[32]; - FILE *fp; - fp = fopen(hmac_key_path.c_str(), "rb"); - fread(hmac_key_buf, sizeof(hmac_key_buf), 1, fp); - fclose(fp); - - nvs_sec_cfg_t cfg; - compute_nvs_keys_with_hmac(&cfg, hmac_key_buf); - - check_nvs_part_gen_args(&emu, NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg); - -} - -TEST_CASE("check and read data from partition generated via manufacturing utility with encryption enabled using sample inputkey", "[mfg_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf ../../../tools/mass_mfg/host_test | \ - cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \ - cp -rf ../nvs_partition_generator/testdata . | \ - mkdir -p ../../../tools/mass_mfg/host_test", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../../../tools/mass_mfg/mfg_gen.py", - "generate", - "../../../tools/mass_mfg/samples/sample_config.csv", - "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv", - "Test", - "0x4000", - "--outdir", - "../../../tools/mass_mfg/host_test", - "--version", - "2", - "--inputkey", - "mfg_testdata/sample_encryption_keys.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../../../tools/mass_mfg/host_test/csv/Test-1.csv", - "../nvs_partition_generator/Test-1-partition-encrypted.bin", - "0x4000", - "--version", - "2", - "--inputkey", - "testdata/sample_encryption_keys.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - - } - - } - - SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin"); - - nvs_sec_cfg_t cfg; - for (int count = 0; count < NVS_KEY_SIZE; count++) { - cfg.eky[count] = 0x11; - cfg.tky[count] = 0x22; - } - - check_nvs_part_gen_args_mfg(&emu1, NVS_DEFAULT_PART_NAME, 4, "mfg_testdata/sample_multipage_blob.bin", true, &cfg); - - SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition-encrypted.bin"); - - check_nvs_part_gen_args_mfg(&emu2, NVS_DEFAULT_PART_NAME, 4, "testdata/sample_multipage_blob.bin", true, &cfg); - - - childpid = fork(); - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf ../../../tools/mass_mfg/host_test | \ - rm -rf mfg_testdata | \ - rm -rf testdata", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - -} - -TEST_CASE("check and read data from partition generated via manufacturing utility with encryption enabled using new generated key", "[mfg_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf ../../../tools/mass_mfg/host_test | \ - cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \ - cp -rf ../nvs_partition_generator/testdata . | \ - mkdir -p ../../../tools/mass_mfg/host_test", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../../../tools/mass_mfg/mfg_gen.py", - "generate-key", - "--outdir", - "../../../tools/mass_mfg/host_test", - "--keyfile", - "encr_keys_host_test.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../../../tools/mass_mfg/mfg_gen.py", - "generate", - "../../../tools/mass_mfg/samples/sample_config.csv", - "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv", - "Test", - "0x4000", - "--outdir", - "../../../tools/mass_mfg/host_test", - "--version", - "2", - "--inputkey", - "../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../../../tools/mass_mfg/host_test/csv/Test-1.csv", - "../nvs_partition_generator/Test-1-partition-encrypted.bin", - "0x4000", - "--version", - "2", - "--inputkey", - "../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - - } - - } - - } - - SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin"); - - char buffer[64]; - FILE *fp; - - fp = fopen("../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin", "rb"); - fread(buffer, sizeof(buffer), 1, fp); - - fclose(fp); - - nvs_sec_cfg_t cfg; - - for (int count = 0; count < NVS_KEY_SIZE; count++) { - cfg.eky[count] = buffer[count] & 255; - cfg.tky[count] = buffer[count + 32] & 255; - } - - check_nvs_part_gen_args_mfg(&emu1, NVS_DEFAULT_PART_NAME, 4, "mfg_testdata/sample_multipage_blob.bin", true, &cfg); - - SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition-encrypted.bin"); - - check_nvs_part_gen_args_mfg(&emu2, NVS_DEFAULT_PART_NAME, 4, "testdata/sample_multipage_blob.bin", true, &cfg); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf keys | \ - rm -rf mfg_testdata | \ - rm -rf testdata | \ - rm -rf ../../../tools/mass_mfg/host_test", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - -} - -TEST_CASE("check and read data from partition generated via manufacturing utility with encryption enabled using new generated key (user-provided HMAC-key)", "[mfg_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf ../../../tools/mass_mfg/host_test | \ - cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \ - cp -rf ../nvs_partition_generator/testdata . | \ - mkdir -p ../../../tools/mass_mfg/host_test", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../../../tools/mass_mfg/mfg_gen.py", - "generate", - "../../../tools/mass_mfg/samples/sample_config.csv", - "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv", - "Test", - "0x4000", - "--version", - "2", - "--keygen", - "--key_protect_hmac", - "--kp_hmac_inputkey", - "mfg_testdata/sample_hmac_key.bin", - "--outdir", - "../../../tools/mass_mfg/host_test",NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../../../tools/mass_mfg/host_test/csv/Test-1.csv", - "../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin", - "0x4000", - "--version", - "2", - "--keygen", - "--key_protect_hmac", - "--kp_hmac_inputkey", - "mfg_testdata/sample_hmac_key.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - - } - - } - - SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin"); - - nvs_sec_cfg_t cfg; - compute_nvs_keys_with_hmac(&cfg, NULL); - - check_nvs_part_gen_args_mfg(&emu1, NVS_DEFAULT_PART_NAME, 4, "mfg_testdata/sample_multipage_blob.bin", true, &cfg); - - SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin"); - - check_nvs_part_gen_args_mfg(&emu2, NVS_DEFAULT_PART_NAME, 4, "testdata/sample_multipage_blob.bin", true, &cfg); - - - childpid = fork(); - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf ../../../tools/mass_mfg/host_test | \ - rm -rf mfg_testdata | \ - rm -rf testdata", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - -} - -TEST_CASE("check and read data from partition generated via manufacturing utility with encryption enabled using new generated key (dynamically generated HMAC-key)", "[mfg_gen]") -{ - int childpid = fork(); - int status; - - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf ../../../tools/mass_mfg/host_test | \ - cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \ - cp -rf ../nvs_partition_generator/testdata . | \ - mkdir -p ../../../tools/mass_mfg/host_test", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../../../tools/mass_mfg/mfg_gen.py", - "generate-key", - "--outdir", - "../../../tools/mass_mfg/host_test", - "--key_protect_hmac", - "--kp_hmac_keygen", - "--kp_hmac_keyfile", - "hmac_key_host_test.bin", - "--keyfile", - "encr_keys_host_test.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../../../tools/mass_mfg/mfg_gen.py", - "generate", - "../../../tools/mass_mfg/samples/sample_config.csv", - "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv", - "Test", - "0x4000", - "--outdir", - "../../../tools/mass_mfg/host_test", - "--version", - "2", - "--inputkey", - "../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("python", "python", - "../nvs_partition_generator/nvs_partition_gen.py", - "encrypt", - "../../../tools/mass_mfg/host_test/csv/Test-1.csv", - "../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin", - "0x4000", - "--version", - "2", - "--inputkey", - "../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin", NULL)); - - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - - } - - } - - } - - - SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin"); - - char hmac_key_buf[32]; - FILE *fp; - - fp = fopen("../../../tools/mass_mfg/host_test/keys/hmac_key_host_test.bin", "rb"); - fread(hmac_key_buf, sizeof(hmac_key_buf), 1, fp); - - fclose(fp); - - nvs_sec_cfg_t cfg; - compute_nvs_keys_with_hmac(&cfg, hmac_key_buf); - - check_nvs_part_gen_args_mfg(&emu1, NVS_DEFAULT_PART_NAME, 4, "mfg_testdata/sample_multipage_blob.bin", true, &cfg); - - SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin"); - - check_nvs_part_gen_args_mfg(&emu2, NVS_DEFAULT_PART_NAME, 4, "testdata/sample_multipage_blob.bin", true, &cfg); - - childpid = fork(); - if (childpid == 0) { - exit(execlp("bash", " bash", - "-c", - "rm -rf keys | \ - rm -rf mfg_testdata | \ - rm -rf testdata | \ - rm -rf ../../../tools/mass_mfg/host_test", NULL)); - } else { - CHECK(childpid > 0); - waitpid(childpid, &status, 0); - CHECK(WEXITSTATUS(status) == 0); - - } - -} - -#endif diff --git a/components/nvs_flash/test_nvs_host/test_nvs_partition.cpp b/components/nvs_flash/test_nvs_host/test_nvs_partition.cpp deleted file mode 100644 index d394f525aa..0000000000 --- a/components/nvs_flash/test_nvs_host/test_nvs_partition.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "catch.hpp" -#include -#include -#include "nvs_test_api.h" -#include "nvs_handle_simple.hpp" -#include "nvs_partition.hpp" -#include "spi_flash_emulation.h" -#include "test_fixtures.hpp" -#include - -TEST_CASE("encrypted partition read size must be item size", "[nvs]") -{ - char foo [32] = { }; - nvs_sec_cfg_t xts_cfg; - for (int count = 0; count < NVS_KEY_SIZE; count++) { - xts_cfg.eky[count] = 0x11; - xts_cfg.tky[count] = 0x22; - } - EncryptedPartitionFixture fix(&xts_cfg); - - CHECK(fix.part.read(0, foo, sizeof (foo) - 1) == ESP_ERR_INVALID_SIZE); -} - -TEST_CASE("encrypted partition write size must be mod item size", "[nvs]") -{ - char foo [64] = { }; - nvs_sec_cfg_t xts_cfg; - for (int count = 0; count < NVS_KEY_SIZE; count++) { - xts_cfg.eky[count] = 0x11; - xts_cfg.tky[count] = 0x22; - } - EncryptedPartitionFixture fix(&xts_cfg); - - CHECK(fix.part.write(0, foo, sizeof (foo) - 1) == ESP_ERR_INVALID_SIZE); - CHECK(fix.part.write(0, foo, sizeof (foo) / 2) == ESP_OK); - CHECK(fix.part.write(sizeof(foo) / 2, foo, sizeof (foo)) == ESP_OK); -} diff --git a/components/nvs_flash/test_nvs_host/test_partition_manager.cpp b/components/nvs_flash/test_nvs_host/test_partition_manager.cpp deleted file mode 100644 index cc86e5182c..0000000000 --- a/components/nvs_flash/test_nvs_host/test_partition_manager.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "catch.hpp" -#include -#include -#include "nvs_test_api.h" -#include "nvs_handle_simple.hpp" -#include "nvs_partition_manager.hpp" -#include "spi_flash_emulation.h" -#include "nvs_test_api.h" -#include "test_fixtures.hpp" -#include "esp_partition.h" - -/* -TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]") -{ - const uint32_t NVS_FLASH_SECTOR = 6; - const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; - SpiFlashEmulator emu(10); - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - PartitionEmulation part_0(&emu, NVS_FLASH_SECTOR * sec_size, NVS_FLASH_SECTOR_COUNT_MIN * sec_size, "test1"); - PartitionEmulation part_1(&emu, NVS_FLASH_SECTOR * sec_size, NVS_FLASH_SECTOR_COUNT_MIN * sec_size, "test2"); - - REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom(&part_0, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) - == ESP_OK); - // TODO: why does this work, actually? same sectors used as above - REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom(&part_1, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) - == ESP_OK); - nvs::Storage *storage1 = nvs::NVSPartitionManager::get_instance()->lookup_storage_from_name("test1"); - REQUIRE(storage1 != nullptr); - nvs::Storage *storage2 = nvs::NVSPartitionManager::get_instance()->lookup_storage_from_name("test2"); - REQUIRE(storage2 != nullptr); - - CHECK(storage1 != storage2); - REQUIRE(nvs::NVSPartitionManager::get_instance()->deinit_partition(part_0.get_partition_name()) == ESP_OK); - REQUIRE(nvs::NVSPartitionManager::get_instance()->deinit_partition(part_1.get_partition_name()) == ESP_OK); -} -*/ diff --git a/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp b/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp deleted file mode 100644 index 46fae9f3c0..0000000000 --- a/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "catch.hpp" -#include "esp_partition.h" -#include "spi_flash_emulation.h" -#include "spi_flash_mmap.h" -#include - -template -bool range_empty_n(Tit it_begin, size_t n) -{ - return std::all_of(it_begin, it_begin + n, bind(std::equal_to(), std::placeholders::_1, 0xffffffff)); -} - -struct FlashEmuFixture { - FlashEmuFixture(size_t sectors) : esp_part(), emu(sectors) { } - - esp_partition_t esp_part; - SpiFlashEmulator emu; -}; - -TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]") -{ - FlashEmuFixture f(4); - - uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - uint8_t sector[sec_size]; - - for (int i = 0; i < 4; ++i) { - CHECK(esp_partition_read(&f.esp_part, 0, sector, sizeof(sector)) == ESP_OK); - for (auto v: sector) { - CHECK(v == 0xff); - } - } -} - -TEST_CASE("invalid writes are checked", "[spi_flash_emu]") -{ - FlashEmuFixture f(1); - - uint32_t val = 0; - CHECK(esp_partition_write(&f.esp_part, 0, &val, 4) == ESP_OK); - val = 1; - CHECK(esp_partition_write(&f.esp_part, 0, &val, 4) == ESP_ERR_FLASH_OP_FAIL); -} - - -TEST_CASE("out of bounds writes fail", "[spi_flash_emu]") -{ - FlashEmuFixture f(4); - uint32_t vals[8]; - std::fill_n(vals, 8, 0); - CHECK(esp_partition_write(&f.esp_part, 0, &vals, sizeof(vals)) == ESP_OK); - - CHECK(esp_partition_write(&f.esp_part, 4*4096 - sizeof(vals), &vals, sizeof(vals)) == ESP_OK); - - CHECK(esp_partition_write(&f.esp_part, 4*4096 - sizeof(vals) + 4, &vals, sizeof(vals)) == ESP_ERR_FLASH_OP_FAIL); -} - -TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]") -{ - FlashEmuFixture f(4); - uint32_t val1 = 0xab00cd12; - CHECK(esp_partition_write(&f.esp_part, 0, &val1, sizeof(val1)) == ESP_OK); - uint32_t val2 = 0x5678efab; - CHECK(esp_partition_write(&f.esp_part, 4096 - 4, &val2, sizeof(val2)) == ESP_OK); - - CHECK(f.emu.words()[0] == val1); - CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2)); - CHECK(f.emu.words()[4096 / 4 - 1] == val2); - - uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - CHECK(esp_partition_erase_range(&f.esp_part, 0, sec_size) == ESP_OK); - - CHECK(f.emu.words()[0] == 0xffffffff); - CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2)); - CHECK(f.emu.words()[4096 / 4 - 1] == 0xffffffff); -} - -TEST_CASE("EMU raw read function works", "[spi_flash_emu]") -{ - FlashEmuFixture f(4); - uint32_t value = 0xdeadbeef; - uint32_t read_value = 0; - CHECK(esp_partition_write(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK); - - CHECK(esp_partition_read_raw(&f.esp_part, 0, &read_value, sizeof(read_value)) == ESP_OK); - - CHECK(read_value == 0xdeadbeef); -} - -TEST_CASE("EMU raw write function works", "[spi_flash_emu]") -{ - FlashEmuFixture f(4); - uint32_t value = 0xdeadbeef; - uint32_t read_value = 0; - CHECK(esp_partition_write_raw(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK); - - CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(read_value)) == ESP_OK); - - CHECK(read_value == 0xdeadbeef); -} - -TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_flash_emu]") -{ - FlashEmuFixture f(1); - uint8_t data[512]; - esp_partition_read(&f.esp_part, 0, data, 4); - CHECK(f.emu.getTotalTime() == 7); - CHECK(f.emu.getReadOps() == 1); - CHECK(f.emu.getReadBytes() == 4); - f.emu.clearStats(); - esp_partition_read(&f.esp_part, 0, data, 8); - CHECK(f.emu.getTotalTime() == 5); - CHECK(f.emu.getReadOps() == 1); - CHECK(f.emu.getReadBytes() == 8); - f.emu.clearStats(); - esp_partition_read(&f.esp_part, 0, data, 16); - CHECK(f.emu.getTotalTime() == 6); - CHECK(f.emu.getReadOps() == 1); - CHECK(f.emu.getReadBytes() == 16); - f.emu.clearStats(); - esp_partition_read(&f.esp_part, 0, data, 128); - CHECK(f.emu.getTotalTime() == 18); - CHECK(f.emu.getReadOps() == 1); - CHECK(f.emu.getReadBytes() == 128); - f.emu.clearStats(); - esp_partition_read(&f.esp_part, 0, data, 256); - CHECK(f.emu.getTotalTime() == 32); - f.emu.clearStats(); - esp_partition_read(&f.esp_part, 0, data, (128+256)/2); - CHECK(f.emu.getTotalTime() == (18+32)/2); - f.emu.clearStats(); - - esp_partition_write(&f.esp_part, 0, data, 4); - CHECK(f.emu.getTotalTime() == 19); - CHECK(f.emu.getWriteOps() == 1); - CHECK(f.emu.getWriteBytes() == 4); - f.emu.clearStats(); - CHECK(f.emu.getWriteOps() == 0); - CHECK(f.emu.getWriteBytes() == 0); - esp_partition_write(&f.esp_part, 0, data, 8); - CHECK(f.emu.getTotalTime() == 23); - f.emu.clearStats(); - esp_partition_write(&f.esp_part, 0, data, 16); - CHECK(f.emu.getTotalTime() == 35); - CHECK(f.emu.getWriteOps() == 1); - CHECK(f.emu.getWriteBytes() == 16); - f.emu.clearStats(); - esp_partition_write(&f.esp_part, 0, data, 128); - CHECK(f.emu.getTotalTime() == 205); - f.emu.clearStats(); - esp_partition_write(&f.esp_part, 0, data, 256); - CHECK(f.emu.getTotalTime() == 417); - f.emu.clearStats(); - esp_partition_write(&f.esp_part, 0, data, (128+256)/2); - CHECK(f.emu.getTotalTime() == (205+417)/2); - f.emu.clearStats(); - - const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - esp_partition_erase_range(&f.esp_part, 0, sec_size); - CHECK(f.emu.getEraseOps() == 1); - CHECK(f.emu.getTotalTime() == 37142); -} - -TEST_CASE("data is randomized predictably", "[spi_flash_emu]") -{ - SpiFlashEmulator emu1(3); - emu1.randomize(0x12345678); - - SpiFlashEmulator emu2(3); - emu2.randomize(0x12345678); - - CHECK(std::equal(emu1.bytes(), emu1.bytes() + emu1.size(), emu2.bytes())); -} diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 8ef3d3f7c3..f605b7cd05 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -499,9 +499,6 @@ components/nvs_flash/src/nvs_pagemanager.hpp components/nvs_flash/src/nvs_partition_lookup.cpp components/nvs_flash/src/nvs_partition_lookup.hpp components/nvs_flash/src/nvs_test_api.h -components/nvs_flash/test_nvs_host/main.cpp -components/nvs_flash/test_nvs_host/sdkconfig.h -components/nvs_flash/test_nvs_host/test_intrusive_list.cpp components/protocomm/include/transports/protocomm_console.h components/protocomm/include/transports/protocomm_httpd.h components/riscv/include/riscv/csr.h