test_nvs_handle.cpp 9.3 KB

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