test_partition_manager.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "catch.hpp"
  14. #include <algorithm>
  15. #include <cstring>
  16. #include "nvs_test_api.h"
  17. #include "nvs_handle_simple.hpp"
  18. #include "nvs_partition_manager.hpp"
  19. #include "spi_flash_emulation.h"
  20. #include "nvs_test_api.h"
  21. #include "test_fixtures.hpp"
  22. using namespace nvs;
  23. TEST_CASE("Partition manager initializes storage", "[partition_mgr]")
  24. {
  25. const uint32_t NVS_FLASH_SECTOR = 6;
  26. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  27. PartitionEmulationFixture f(0, 10, "test");
  28. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  29. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  30. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(f.part.get_partition_name()) == ESP_OK);
  31. }
  32. TEST_CASE("Partition manager de-initializes storage", "[partition_mgr]")
  33. {
  34. const uint32_t NVS_FLASH_SECTOR = 6;
  35. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  36. PartitionEmulationFixture f(0, 10, "test");
  37. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  38. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  39. CHECK(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  40. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") == nullptr);
  41. }
  42. TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]")
  43. {
  44. const uint32_t NVS_FLASH_SECTOR = 6;
  45. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  46. SpiFlashEmulator emu(10);
  47. PartitionEmulation part_0(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test1");
  48. PartitionEmulation part_1(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test2");
  49. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&part_0, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  50. == ESP_OK);
  51. // TODO: why does this work, actually? same sectors used as above
  52. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&part_1, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  53. == ESP_OK);
  54. Storage *storage1 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test1");
  55. REQUIRE(storage1 != nullptr);
  56. Storage *storage2 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test2");
  57. REQUIRE(storage2 != nullptr);
  58. CHECK(storage1 != storage2);
  59. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_0.get_partition_name()) == ESP_OK);
  60. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_1.get_partition_name()) == ESP_OK);
  61. }
  62. TEST_CASE("Partition manager invalidates handle on partition de-init", "[partition_mgr]")
  63. {
  64. const uint32_t NVS_FLASH_SECTOR = 6;
  65. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  66. PartitionEmulationFixture f(0, 10, "test");
  67. REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  68. == ESP_OK);
  69. NVSHandleSimple *handle;
  70. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  71. CHECK(handle->erase_all() == ESP_OK);
  72. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  73. CHECK(handle->erase_all() == ESP_ERR_NVS_INVALID_HANDLE);
  74. delete handle;
  75. }