nvs: unit tests for new partition API

* also changed while to for loop
This commit is contained in:
Jakob Hasse
2020-05-26 09:28:39 +08:00
parent cb6dcb6fc4
commit f694f92230
3 changed files with 70 additions and 5 deletions

View File

@@ -158,17 +158,13 @@ static esp_err_t close_handles_and_deinit(const char* partition_name)
#endif #endif
/* Clean up handles related to the storage being deinitialized */ /* Clean up handles related to the storage being deinitialized */
auto it = s_nvs_handles.begin(); for (auto it = s_nvs_handles.begin(), next = it; it != s_nvs_handles.end() && next++; it = next) {
auto next = it;
while(it != s_nvs_handles.end()) {
next++;
if (it->mStoragePtr == storage) { if (it->mStoragePtr == storage) {
ESP_LOGD(TAG, "Deleting handle %d (ns=%d) related to partition \"%s\" (missing call to nvs_close?)", ESP_LOGD(TAG, "Deleting handle %d (ns=%d) related to partition \"%s\" (missing call to nvs_close?)",
it->mHandle, it->mNsIndex, partition_name); it->mHandle, it->mNsIndex, partition_name);
s_nvs_handles.erase(it); s_nvs_handles.erase(it);
delete static_cast<HandleEntry*>(it); delete static_cast<HandleEntry*>(it);
} }
it = next;
} }
/* Finally delete the storage itself */ /* Finally delete the storage itself */

View File

@@ -19,6 +19,7 @@ SOURCE_FILES = \
test_intrusive_list.cpp \ test_intrusive_list.cpp \
test_nvs.cpp \ test_nvs.cpp \
test_nvs_storage.cpp \ test_nvs_storage.cpp \
test_nvs_initialization.cpp \
crc.cpp \ crc.cpp \
main.cpp main.cpp

View File

@@ -0,0 +1,68 @@
// 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 "spi_flash_emulation.h"
#include "esp_partition.h"
#include "nvs.h"
#include "nvs_flash.h"
#include <string.h>
#include <iostream>
TEST_CASE("nvs_flash_init_partition_ptr fails due to nullptr arg", "[nvs_custom_part]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
SpiFlashEmulator emu(10);
CHECK(nvs_flash_init_partition_ptr(nullptr) == ESP_ERR_INVALID_ARG);
}
TEST_CASE("nvs_flash_init_partition_ptr inits one partition", "[nvs_custom_part]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
SpiFlashEmulator emu(10);
nvs_handle_t out_handle;
esp_partition_t partition = {};
strcpy(partition.label, "test");
partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
nvs_flash_deinit_partition("test");
CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_ERR_NVS_PART_NOT_FOUND);
CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_OK);
CHECK(nvs_flash_deinit_partition("test") == ESP_OK);
}
TEST_CASE("nvs_flash_init_partition_ptr deinits one partition", "[nvs_custom_part]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
SpiFlashEmulator emu(10);
nvs_handle_t out_handle;
esp_partition_t partition = {};
strcpy(partition.label, "test");
partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
nvs_flash_deinit_partition("test");
CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_OK);
CHECK(nvs_flash_deinit_partition("test") == ESP_OK);
CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_ERR_NVS_PART_NOT_FOUND);
}