test_nvs_storage.cpp 2.2 KB

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