test_nvs_cxx_api.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. TEST_CASE("NVSHandleSimple CXX api open invalid arguments", "[nvs cxx]")
  23. {
  24. const uint32_t NVS_FLASH_SECTOR = 6;
  25. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  26. SpiFlashEmulator emu(10);
  27. esp_err_t result;
  28. shared_ptr<nvs::NVSHandle> handle;
  29. REQUIRE(nvs::NVSPartitionManager::get_instance()->
  30. init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  31. handle = nvs::open_nvs_handle_from_partition(nullptr, "ns_1", NVS_READWRITE, &result);
  32. CHECK(result == ESP_ERR_INVALID_ARG);
  33. CHECK(!handle);
  34. handle = nvs::open_nvs_handle_from_partition("test", nullptr, NVS_READWRITE, &result);
  35. CHECK(result == ESP_ERR_INVALID_ARG);
  36. CHECK(!handle);
  37. nvs::NVSPartitionManager::get_instance()->deinit_partition("test");
  38. }
  39. TEST_CASE("NVSHandleSimple CXX api open partition uninitialized", "[nvs cxx]")
  40. {
  41. SpiFlashEmulator emu(10);
  42. esp_err_t result;
  43. shared_ptr<nvs::NVSHandle> handle;
  44. handle = nvs::open_nvs_handle_from_partition("test", "ns_1", NVS_READWRITE, &result);
  45. bool result_expected = result == ESP_ERR_NVS_NOT_INITIALIZED || result == ESP_ERR_NVS_PART_NOT_FOUND;
  46. CHECK(result_expected);
  47. CHECK(!handle);
  48. }
  49. TEST_CASE("NVSHandleSimple CXX api open successful", "[nvs cxx]")
  50. {
  51. const uint32_t NVS_FLASH_SECTOR = 6;
  52. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  53. SpiFlashEmulator emu(10);
  54. esp_err_t result;
  55. shared_ptr<nvs::NVSHandle> handle;
  56. REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  57. == ESP_OK);
  58. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  59. handle = nvs::open_nvs_handle_from_partition("test", "ns_1", NVS_READWRITE, &result);
  60. CHECK(result == ESP_OK);
  61. CHECK(handle);
  62. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 1);
  63. handle.reset();
  64. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  65. nvs::NVSPartitionManager::get_instance()->deinit_partition("test");
  66. }
  67. TEST_CASE("NVSHandleSimple CXX api open default part successful", "[nvs cxx]")
  68. {
  69. const uint32_t NVS_FLASH_SECTOR = 6;
  70. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  71. SpiFlashEmulator emu(10);
  72. esp_err_t result;
  73. shared_ptr<nvs::NVSHandle> handle;
  74. REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom("nvs", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  75. == ESP_OK);
  76. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  77. handle = nvs::open_nvs_handle("ns_1", NVS_READWRITE, &result);
  78. CHECK(result == ESP_OK);
  79. CHECK(handle);
  80. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 1);
  81. handle.reset();
  82. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  83. nvs::NVSPartitionManager::get_instance()->deinit_partition("nvs");
  84. }
  85. TEST_CASE("NVSHandleSimple CXX api open default part ns NULL", "[nvs cxx]")
  86. {
  87. const uint32_t NVS_FLASH_SECTOR = 6;
  88. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  89. SpiFlashEmulator emu(10);
  90. esp_err_t result;
  91. shared_ptr<nvs::NVSHandle> handle;
  92. REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom("nvs", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  93. == ESP_OK);
  94. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  95. handle = nvs::open_nvs_handle(nullptr, NVS_READWRITE, &result);
  96. CHECK(result == ESP_ERR_INVALID_ARG);
  97. CHECK(!handle);
  98. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  99. nvs::NVSPartitionManager::get_instance()->deinit_partition("nvs");
  100. }
  101. TEST_CASE("NVSHandleSimple CXX api read/write string", "[nvs cxx]")
  102. {
  103. const uint32_t NVS_FLASH_SECTOR = 6;
  104. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  105. SpiFlashEmulator emu(10);
  106. char read_buffer [256];
  107. esp_err_t result;
  108. shared_ptr<nvs::NVSHandle> handle;
  109. REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom("nvs", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  110. == ESP_OK);
  111. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  112. handle = nvs::open_nvs_handle("test_ns", NVS_READWRITE, &result);
  113. CHECK(result == ESP_OK);
  114. REQUIRE(handle);
  115. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 1);
  116. CHECK(handle->set_string("test", "test string") == ESP_OK);
  117. CHECK(handle->commit() == ESP_OK);
  118. CHECK(handle->get_string("test", read_buffer, sizeof(read_buffer)) == ESP_OK);
  119. CHECK(string(read_buffer) == "test string");
  120. nvs::NVSPartitionManager::get_instance()->deinit_partition("nvs");
  121. }
  122. TEST_CASE("NVSHandleSimple CXX api read/write blob", "[nvs cxx]")
  123. {
  124. const uint32_t NVS_FLASH_SECTOR = 6;
  125. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  126. SpiFlashEmulator emu(10);
  127. const char blob [6] = {15, 16, 17, 18, 19};
  128. char read_blob[6] = {0};
  129. esp_err_t result;
  130. shared_ptr<nvs::NVSHandle> handle;
  131. REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom("nvs", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  132. == ESP_OK);
  133. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 0);
  134. handle = nvs::open_nvs_handle("test_ns", NVS_READWRITE, &result);
  135. CHECK(result == ESP_OK);
  136. REQUIRE(handle);
  137. CHECK(nvs::NVSPartitionManager::get_instance()->open_handles_size() == 1);
  138. CHECK(handle->set_blob("test", blob, sizeof(blob)) == ESP_OK);
  139. CHECK(handle->commit() == ESP_OK);
  140. CHECK(handle->get_blob("test", read_blob, sizeof(read_blob)) == ESP_OK);
  141. CHECK(vector<char>(blob, blob + sizeof(blob)) == vector<char>(read_blob, read_blob + sizeof(read_blob)));
  142. nvs::NVSPartitionManager::get_instance()->deinit_partition("nvs");
  143. }