test_partition_manager.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "catch.hpp"
  7. #include <algorithm>
  8. #include <cstring>
  9. #include "nvs_test_api.h"
  10. #include "nvs_handle_simple.hpp"
  11. #include "nvs_partition_manager.hpp"
  12. #include "spi_flash_emulation.h"
  13. #include "nvs_test_api.h"
  14. #include "test_fixtures.hpp"
  15. using namespace nvs;
  16. TEST_CASE("Partition manager initializes storage", "[partition_mgr]")
  17. {
  18. const uint32_t NVS_FLASH_SECTOR = 6;
  19. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  20. PartitionEmulationFixture f(0, 10, "test");
  21. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  22. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  23. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(f.part.get_partition_name()) == ESP_OK);
  24. }
  25. TEST_CASE("Partition manager de-initializes storage", "[partition_mgr]")
  26. {
  27. const uint32_t NVS_FLASH_SECTOR = 6;
  28. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  29. PartitionEmulationFixture f(0, 10, "test");
  30. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  31. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  32. CHECK(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  33. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") == nullptr);
  34. }
  35. TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]")
  36. {
  37. const uint32_t NVS_FLASH_SECTOR = 6;
  38. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  39. SpiFlashEmulator emu(10);
  40. PartitionEmulation part_0(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test1");
  41. PartitionEmulation part_1(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test2");
  42. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&part_0, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  43. == ESP_OK);
  44. // TODO: why does this work, actually? same sectors used as above
  45. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&part_1, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  46. == ESP_OK);
  47. Storage *storage1 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test1");
  48. REQUIRE(storage1 != nullptr);
  49. Storage *storage2 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test2");
  50. REQUIRE(storage2 != nullptr);
  51. CHECK(storage1 != storage2);
  52. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_0.get_partition_name()) == ESP_OK);
  53. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_1.get_partition_name()) == ESP_OK);
  54. }
  55. TEST_CASE("Partition manager open fails on null handle", "[partition_mgr]")
  56. {
  57. const uint32_t NVS_FLASH_SECTOR = 6;
  58. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  59. PartitionEmulationFixture f(0, 10, "test");
  60. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  61. == ESP_OK);
  62. CHECK(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, nullptr)
  63. == ESP_ERR_INVALID_ARG);
  64. NVSPartitionManager::get_instance()->deinit_partition("test");
  65. }
  66. TEST_CASE("Partition manager invalidates handle on partition de-init", "[partition_mgr]")
  67. {
  68. const uint32_t NVS_FLASH_SECTOR = 6;
  69. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  70. PartitionEmulationFixture f(0, 10, "test");
  71. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  72. == ESP_OK);
  73. NVSHandleSimple *handle;
  74. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  75. CHECK(handle->erase_all() == ESP_OK);
  76. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  77. CHECK(handle->erase_all() == ESP_ERR_NVS_INVALID_HANDLE);
  78. delete handle;
  79. }