Skip to content

Commit

Permalink
tests: subsys/fs/nvs: add test for initializing bad region
Browse files Browse the repository at this point in the history
Signed-off-by: Cory Andrew Mayer <[email protected]>
  • Loading branch information
corymayer committed Dec 20, 2024
1 parent 10760e6 commit 54dc53e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/subsys/fs/nvs/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_LOG=y
CONFIG_NVS_LOG_LEVEL_DBG=y
CONFIG_NVS_INIT_BAD_MEMORY_REGION=y
37 changes: 37 additions & 0 deletions tests/subsys/fs/nvs/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,40 @@ ZTEST_F(nvs, test_nvs_cache_hash_quality)

#endif
}

/*
* Test NVS bad region initialization recovery.
*/
ZTEST_F(nvs, test_nvs_init_bad_memory_region)
{

int err;
uint32_t data;

err = nvs_mount(&fixture->fs);
zassert_true(err == 0, "nvs_mount call failure: %d", err);

/* Write bad ATE to each sector */
for (uint16_t i = 0; i < TEST_SECTOR_COUNT; i++) {
data = 0xdeadbeef;
err = flash_write(fixture->fs.flash_device, fixture->fs.offset +
(fixture->fs.sector_size * (i + 1)) -
sizeof(struct nvs_ate), &data, sizeof(data));
zassert_true(err == 0, "flash_write failed: %d", err);
}

/* Reinitialize the NVS. */
memset(&fixture->fs, 0, sizeof(fixture->fs));
(void)setup();

#ifdef CONFIG_NVS_INIT_BAD_MEMORY_REGION
err = nvs_mount(&fixture->fs);
zassert_true(err == 0, "nvs_mount call failure: %d", err);

/* Ensure that the NVS is able to store new content. */
execute_long_pattern_write(TEST_DATA_ID, &fixture->fs);
#else
err = nvs_mount(&fixture->fs);
zassert_true(err == -EDEADLK, "nvs_mount call ok, expect fail: %d", err);
#endif
}

0 comments on commit 54dc53e

Please sign in to comment.