Merge branch 'bugfix/IDFGH-2910_v4.0' into 'release/v4.0'

NVS: bugfix - iterator skipping version 1 blobs (v4.0)

See merge request espressif/esp-idf!8193
This commit is contained in:
Ivan Grokhotkov 2020-04-09 05:45:21 +08:00
commit ed3ea95cc2
4 changed files with 65 additions and 1 deletions

View file

@ -763,7 +763,9 @@ inline bool isIterableItem(Item& item)
inline bool isMultipageBlob(Item& item)
{
return (item.datatype == ItemType::BLOB_DATA && item.chunkIndex != 0);
return (item.datatype == ItemType::BLOB_DATA &&
!(item.chunkIndex == static_cast<uint8_t>(VerOffset::VER_0_OFFSET)
|| item.chunkIndex == static_cast<uint8_t>(VerOffset::VER_1_OFFSET)));
}
bool Storage::nextEntry(nvs_opaque_iterator_t* it)

View file

@ -42,6 +42,12 @@ enum class ItemType : uint8_t {
ANY = NVS_TYPE_ANY
};
/**
* Used to recognize transient states of a blob. Once a blob is modified, new chunks with the new data are written
* with a new version. The version is saved in the highest bit of Item::chunkIndex as well as in
* Item::blobIndex::chunkStart.
* If a chunk is modified and hence re-written, the version swaps: 0x0 -> 0x80 or 0x80 -> 0x0.
*/
enum class VerOffset: uint8_t {
VER_0_OFFSET = 0x0,
VER_1_OFFSET = 0x80,

View file

@ -18,6 +18,7 @@ SOURCE_FILES = \
test_spi_flash_emulation.cpp \
test_intrusive_list.cpp \
test_nvs.cpp \
test_nvs_storage.cpp \
crc.cpp \
main.cpp

View file

@ -0,0 +1,55 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "catch.hpp"
#include "nvs_test_api.h"
#include "nvs.h"
#include "spi_flash_emulation.h"
#include <iostream>
using namespace std;
TEST_CASE("Storage iterator recognizes blob with VerOffset::VER_1_OFFSET", "[nvs_storage]")
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
SpiFlashEmulator emu(10);
CHECK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK);
nvs_handle_t handle_1;
uint8_t blob [] = {0x0, 0x1, 0x2, 0x3};
uint8_t blob_new [] = {0x3, 0x2, 0x1, 0x0};
// first, creating namespace...
CHECK(nvs_open("test_ns", NVS_READWRITE, &handle_1) == ESP_OK);
CHECK(nvs_set_blob(handle_1, "key", blob, sizeof(blob)) == ESP_OK);
// changing provokes a blob with version offset 1 (VerOffset::VER_1_OFFSET)
CHECK(nvs_set_blob(handle_1, "key", blob_new, sizeof(blob_new)) == ESP_OK);
nvs_iterator_t it = nvs_entry_find(NVS_DEFAULT_PART_NAME, NULL, NVS_TYPE_ANY);
REQUIRE(it);
nvs_entry_info_t info;
nvs_entry_info(it, &info);
CHECK(string(info.namespace_name) == "test_ns");
CHECK(string(info.key) == "key");
CHECK(info.type == NVS_TYPE_BLOB);
nvs_close(handle_1);
// without deinit it affects "nvs api tests"
CHECK(nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME) == ESP_OK);
}