test_nvs_storage.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <cstring>
  15. #include "nvs_test_api.h"
  16. #include "nvs_storage.hpp"
  17. #include "nvs_partition_manager.hpp"
  18. #include "spi_flash_emulation.h"
  19. #include <iostream>
  20. using namespace std;
  21. using namespace nvs;
  22. TEST_CASE("Storage iterator recognizes blob with VerOffset::VER_1_OFFSET", "[nvs_storage]")
  23. {
  24. const uint32_t NVS_FLASH_SECTOR = 6;
  25. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  26. SpiFlashEmulator emu(10);
  27. REQUIRE(NVSPartitionManager::get_instance()->init_custom("test", NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
  28. == ESP_OK);
  29. uint8_t blob [] = {0x0, 0x1, 0x2, 0x3};
  30. uint8_t blob_new [] = {0x3, 0x2, 0x1, 0x0};
  31. Storage *storage = NVSPartitionManager::get_instance()->lookup_storage_from_name("test");
  32. uint8_t ns_index;
  33. storage->createOrOpenNamespace("test_ns", true, ns_index);
  34. CHECK(storage->writeItem(ns_index, ItemType::BLOB, "test_blob", blob, sizeof(blob)) == ESP_OK);
  35. // changing provokes a blob with version offset 1 (VerOffset::VER_1_OFFSET)
  36. CHECK(storage->writeItem(ns_index, ItemType::BLOB, "test_blob", blob_new, sizeof(blob_new)) == ESP_OK);
  37. nvs_opaque_iterator_t it;
  38. it.storage = storage;
  39. it.type = NVS_TYPE_ANY;
  40. // Central check: does the iterator recognize the blob with version 1?
  41. REQUIRE(storage->findEntry(&it, "test_ns"));
  42. CHECK(string(it.entry_info.namespace_name) == string("test_ns"));
  43. CHECK(string(it.entry_info.key) == string("test_blob"));
  44. CHECK(it.entry_info.type == NVS_TYPE_BLOB);
  45. REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  46. }