esp-idf/components/nvs_flash/test_nvs_host/test_partition_manager.cpp
Jakob Hasse aca9ec28b3 NVS: using esp_partition API
* partition api changed from spi_flash* API to
  esp_partition* API and is abstracted as a C++
  interface.
* The old nvs encryption is still possible
* changed default unit test app partition table
* Partitions coming from esp_partition API are
  checked for generic flash encryption. If yes,
  an error is returned since generic flash
  encryption isn't compatible with nvs
  encryption
* esp32, esp32s2 tests don't require nvs_flash
  but mbedtls now

Closes IDF-1340
Closes IDF-858
2020-09-14 10:34:34 +08:00

93 lines
4.0 KiB
C++

// 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 <algorithm>
#include <cstring>
#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"
using namespace nvs;
TEST_CASE("Partition manager initializes storage", "[partition_mgr]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
PartitionEmulationFixture f(0, 10, "test");
REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(f.part.get_partition_name()) == ESP_OK);
}
TEST_CASE("Partition manager de-initializes storage", "[partition_mgr]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
PartitionEmulationFixture f(0, 10, "test");
REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
CHECK(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") == nullptr);
}
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);
PartitionEmulation part_0(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test1");
PartitionEmulation part_1(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test2");
REQUIRE(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(NVSPartitionManager::get_instance()->init_custom(&part_1, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
== ESP_OK);
Storage *storage1 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test1");
REQUIRE(storage1 != nullptr);
Storage *storage2 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test2");
REQUIRE(storage2 != nullptr);
CHECK(storage1 != storage2);
REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_0.get_partition_name()) == ESP_OK);
REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_1.get_partition_name()) == ESP_OK);
}
TEST_CASE("Partition manager invalidates handle on partition de-init", "[partition_mgr]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
PartitionEmulationFixture f(0, 10, "test");
REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
== ESP_OK);
NVSHandleSimple *handle;
REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle) == ESP_OK);
CHECK(handle->erase_all() == ESP_OK);
REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
CHECK(handle->erase_all() == ESP_ERR_NVS_INVALID_HANDLE);
delete handle;
}