test_nvs_handle.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <iostream>
  21. using namespace std;
  22. using namespace nvs;
  23. TEST_CASE("NVSHandleSimple closes its reference in PartitionManager", "[partition_mgr]")
  24. {
  25. const uint32_t NVS_FLASH_SECTOR = 6;
  26. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  27. SpiFlashEmulator emu(10);
  28. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  29. == ESP_OK);
  30. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  31. NVSHandleSimple *handle;
  32. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  33. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  34. delete handle;
  35. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  36. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  37. }
  38. TEST_CASE("NVSHandleSimple multiple open and closes with PartitionManager", "[partition_mgr]")
  39. {
  40. const uint32_t NVS_FLASH_SECTOR = 6;
  41. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  42. SpiFlashEmulator emu(10);
  43. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  44. == ESP_OK);
  45. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  46. NVSHandleSimple *handle1;
  47. NVSHandleSimple *handle2;
  48. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle1) == ESP_OK);
  49. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  50. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle2) == ESP_OK);
  51. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 2);
  52. delete handle1;
  53. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  54. delete handle2;
  55. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  56. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  57. }
  58. TEST_CASE("nvshandle readonly fails", "[partition_mgr]")
  59. {
  60. SpiFlashEmulator emu(10);
  61. NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME);
  62. NVSHandleSimple *handle_1;
  63. NVSHandleSimple *handle_2;
  64. const uint32_t NVS_FLASH_SECTOR = 6;
  65. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  66. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  67. CHECK(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  68. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  69. // first, creating namespace...
  70. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle_1) == ESP_OK);
  71. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  72. delete handle_1;
  73. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  74. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READONLY, &handle_2) == ESP_OK);
  75. CHECK(handle_2->set_item("key", 47) == ESP_ERR_NVS_READ_ONLY);
  76. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  77. delete handle_2;
  78. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  79. // without deinit it affects "nvs api tests"
  80. CHECK(nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  81. }