test_partition_manager.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. using namespace nvs;
  22. TEST_CASE("Partition manager initializes storage", "[partition_mgr]")
  23. {
  24. const uint32_t NVS_FLASH_SECTOR = 6;
  25. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  26. SpiFlashEmulator emu(10);
  27. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  28. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  29. }
  30. TEST_CASE("Partition manager de-initializes storage", "[partition_mgr]")
  31. {
  32. const uint32_t NVS_FLASH_SECTOR = 6;
  33. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  34. SpiFlashEmulator emu(10);
  35. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  36. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  37. CHECK(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  38. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") == nullptr);
  39. }
  40. TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]")
  41. {
  42. const uint32_t NVS_FLASH_SECTOR = 6;
  43. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  44. SpiFlashEmulator emu(10);
  45. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test1", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  46. == ESP_OK);
  47. // TODO: why does this work, actually? same sectors used as above
  48. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test2", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  49. == ESP_OK);
  50. Storage *storage1 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test1");
  51. REQUIRE(storage1 != nullptr);
  52. Storage *storage2 = NVSPartitionManager::get_instance()->lookup_storage_from_name("test2");
  53. REQUIRE(storage2 != nullptr);
  54. CHECK(storage1 != storage2);
  55. }
  56. TEST_CASE("Partition manager invalidates handle on partition de-init", "[partition_mgr]")
  57. {
  58. const uint32_t NVS_FLASH_SECTOR = 6;
  59. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  60. SpiFlashEmulator emu(10);
  61. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  62. == ESP_OK);
  63. NVSHandleSimple *handle;
  64. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  65. CHECK(handle->erase_all() == ESP_OK);
  66. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  67. CHECK(handle->erase_all() == ESP_ERR_NVS_INVALID_HANDLE);
  68. delete handle;
  69. }