test_nvs_handle.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <string>
  22. using namespace std;
  23. using namespace nvs;
  24. TEST_CASE("NVSHandleSimple closes its reference in PartitionManager", "[partition_mgr]")
  25. {
  26. const uint32_t NVS_FLASH_SECTOR = 6;
  27. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  28. SpiFlashEmulator emu(10);
  29. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  30. == ESP_OK);
  31. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  32. NVSHandleSimple *handle;
  33. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  34. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  35. delete handle;
  36. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  37. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  38. }
  39. TEST_CASE("NVSHandleSimple multiple open and closes with PartitionManager", "[partition_mgr]")
  40. {
  41. const uint32_t NVS_FLASH_SECTOR = 6;
  42. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  43. SpiFlashEmulator emu(10);
  44. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  45. == ESP_OK);
  46. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  47. NVSHandleSimple *handle1;
  48. NVSHandleSimple *handle2;
  49. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle1) == ESP_OK);
  50. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  51. REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, &handle2) == ESP_OK);
  52. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 2);
  53. delete handle1;
  54. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  55. delete handle2;
  56. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  57. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  58. }
  59. TEST_CASE("nvshandle readonly fails", "[partition_mgr]")
  60. {
  61. SpiFlashEmulator emu(10);
  62. NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME);
  63. NVSHandleSimple *handle_1;
  64. NVSHandleSimple *handle_2;
  65. const uint32_t NVS_FLASH_SECTOR = 6;
  66. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  67. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  68. CHECK(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  69. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  70. // first, creating namespace...
  71. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle_1) == ESP_OK);
  72. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  73. delete handle_1;
  74. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  75. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READONLY, &handle_2) == ESP_OK);
  76. CHECK(handle_2->set_item("key", 47) == ESP_ERR_NVS_READ_ONLY);
  77. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
  78. delete handle_2;
  79. CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
  80. // without deinit it affects "nvs api tests"
  81. CHECK(nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  82. }
  83. TEST_CASE("NVSHandleSimple set/get char", "[partition_mgr]")
  84. {
  85. enum class TestEnum : char {
  86. FOO = -1,
  87. BEER,
  88. BAR
  89. };
  90. SpiFlashEmulator emu(10);
  91. const uint32_t NVS_FLASH_SECTOR = 6;
  92. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  93. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  94. REQUIRE(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  95. == ESP_OK);
  96. NVSHandleSimple *handle;
  97. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  98. char test_e = 'a';
  99. char test_e_read = 'z';
  100. CHECK(handle->set_item("key", test_e) == ESP_OK);
  101. CHECK(handle->get_item("key", test_e_read) == ESP_OK);
  102. CHECK(test_e == test_e_read);
  103. delete handle;
  104. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  105. }
  106. TEST_CASE("NVSHandleSimple correctly sets/gets int enum", "[partition_mgr]")
  107. {
  108. enum class TestEnum : int {
  109. FOO,
  110. BAR
  111. };
  112. SpiFlashEmulator emu(10);
  113. const uint32_t NVS_FLASH_SECTOR = 6;
  114. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  115. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  116. REQUIRE(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  117. == ESP_OK);
  118. NVSHandleSimple *handle;
  119. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  120. TestEnum test_e = TestEnum::BAR;
  121. TestEnum test_e_read = TestEnum::FOO;
  122. CHECK(handle->set_item("key", test_e) == ESP_OK);
  123. CHECK(handle->get_item("key", test_e_read) == ESP_OK);
  124. CHECK(test_e == test_e_read);
  125. delete handle;
  126. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  127. }
  128. TEST_CASE("NVSHandleSimple correctly sets/gets int enum with negative values", "[partition_mgr]")
  129. {
  130. enum class TestEnum : int {
  131. FOO = -1,
  132. BEER,
  133. BAR
  134. };
  135. SpiFlashEmulator emu(10);
  136. const uint32_t NVS_FLASH_SECTOR = 6;
  137. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  138. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  139. REQUIRE(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  140. == ESP_OK);
  141. NVSHandleSimple *handle;
  142. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  143. TestEnum test_e = TestEnum::FOO;
  144. TestEnum test_e_read = TestEnum::BEER;
  145. CHECK(handle->set_item("key", test_e) == ESP_OK);
  146. CHECK(handle->get_item("key", test_e_read) == ESP_OK);
  147. CHECK(test_e == test_e_read);
  148. delete handle;
  149. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  150. }
  151. TEST_CASE("NVSHandleSimple correctly sets/gets uint8_t enum", "[partition_mgr]")
  152. {
  153. enum class TestEnum : uint8_t {
  154. FOO,
  155. BAR
  156. };
  157. SpiFlashEmulator emu(10);
  158. const uint32_t NVS_FLASH_SECTOR = 6;
  159. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  160. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  161. REQUIRE(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  162. == ESP_OK);
  163. NVSHandleSimple *handle;
  164. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  165. TestEnum test_e = TestEnum::BAR;
  166. TestEnum test_e_read = TestEnum::FOO;
  167. CHECK(handle->set_item("key", test_e) == ESP_OK);
  168. CHECK(handle->get_item("key", test_e_read) == ESP_OK);
  169. CHECK(test_e == test_e_read);
  170. delete handle;
  171. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  172. }
  173. TEST_CASE("NVSHandleSimple correctly sets/gets char enum", "[partition_mgr]")
  174. {
  175. enum class TestEnum : char {
  176. FOO = -1,
  177. BEER,
  178. BAR
  179. };
  180. SpiFlashEmulator emu(10);
  181. const uint32_t NVS_FLASH_SECTOR = 6;
  182. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  183. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  184. REQUIRE(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  185. == ESP_OK);
  186. NVSHandleSimple *handle;
  187. REQUIRE(NVSPartitionManager::get_instance()->open_handle(NVS_DEFAULT_PART_NAME, "ns_1", NVS_READWRITE, &handle) == ESP_OK);
  188. TestEnum test_e = TestEnum::BAR;
  189. TestEnum test_e_read = TestEnum::FOO;
  190. CHECK(handle->set_item("key", test_e) == ESP_OK);
  191. CHECK(handle->get_item("key", test_e_read) == ESP_OK);
  192. CHECK(test_e == test_e_read);
  193. delete handle;
  194. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  195. }