test_nvs_storage.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "nvs_test_api.h"
  15. #include "nvs.h"
  16. #include "spi_flash_emulation.h"
  17. #include <iostream>
  18. using namespace std;
  19. TEST_CASE("Storage iterator recognizes blob with VerOffset::VER_1_OFFSET", "[nvs_storage]")
  20. {
  21. const uint32_t NVS_FLASH_SECTOR = 6;
  22. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  23. SpiFlashEmulator emu(10);
  24. CHECK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
  25. nvs_handle_t handle_1;
  26. uint8_t blob [] = {0x0, 0x1, 0x2, 0x3};
  27. uint8_t blob_new [] = {0x3, 0x2, 0x1, 0x0};
  28. // first, creating namespace...
  29. CHECK(nvs_open("test_ns", NVS_READWRITE, &handle_1) == ESP_OK);
  30. CHECK(nvs_set_blob(handle_1, "key", blob, sizeof(blob)) == ESP_OK);
  31. // changing provokes a blob with version offset 1 (VerOffset::VER_1_OFFSET)
  32. CHECK(nvs_set_blob(handle_1, "key", blob_new, sizeof(blob_new)) == ESP_OK);
  33. nvs_iterator_t it = nvs_entry_find(NVS_DEFAULT_PART_NAME, NULL, NVS_TYPE_ANY);
  34. REQUIRE(it);
  35. nvs_entry_info_t info;
  36. nvs_entry_info(it, &info);
  37. CHECK(string(info.namespace_name) == "test_ns");
  38. CHECK(string(info.key) == "key");
  39. CHECK(info.type == NVS_TYPE_BLOB);
  40. nvs_close(handle_1);
  41. // without deinit it affects "nvs api tests"
  42. CHECK(nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
  43. }