test_nvs_cxx_api.cpp 6.7 KB

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