NVS Flash: prevent erasing initialized partition
Closes https://github.com/espressif/esp-idf/issues/4755 Closes https://github.com/espressif/esp-idf/issues/2777 * nvs_flash_erase_partition() checks whether the parition in question is initialized already and will return an error if so * reflect changes in the documentation
This commit is contained in:
@@ -18,6 +18,24 @@
|
||||
|
||||
static const char* TAG = "test_nvs";
|
||||
|
||||
TEST_CASE("flash erase fails on initialized partition", "[nvs]")
|
||||
{
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_LOGW(TAG, "nvs_flash_init failed (0x%x), erasing partition and retrying", err);
|
||||
const esp_partition_t* nvs_partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
|
||||
assert(nvs_partition && "partition table must have an NVS partition");
|
||||
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK( err );
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_NVS_INVALID_STATE, nvs_flash_erase());
|
||||
|
||||
nvs_flash_deinit();
|
||||
}
|
||||
|
||||
// test could have different output on host tests
|
||||
TEST_CASE("nvs deinit with open handle", "[nvs]")
|
||||
{
|
||||
@@ -121,8 +139,8 @@ TEST_CASE("calculate used and free space", "[nvs]")
|
||||
// erase if have any namespace
|
||||
TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
|
||||
if(stat1.namespace_count != 0) {
|
||||
TEST_ESP_OK(nvs_flash_erase());
|
||||
TEST_ESP_OK(nvs_flash_deinit());
|
||||
TEST_ESP_OK(nvs_flash_erase());
|
||||
TEST_ESP_OK(nvs_flash_init());
|
||||
}
|
||||
|
||||
@@ -235,8 +253,8 @@ TEST_CASE("calculate used and free space", "[nvs]")
|
||||
|
||||
nvs_close(handle_3);
|
||||
|
||||
TEST_ESP_OK(nvs_flash_erase());
|
||||
TEST_ESP_OK(nvs_flash_deinit());
|
||||
TEST_ESP_OK(nvs_flash_erase());
|
||||
}
|
||||
|
||||
TEST_CASE("check for memory leaks in nvs_set_blob", "[nvs]")
|
||||
|
||||
Reference in New Issue
Block a user