From ede130ba0fa2f97e5045206fbfa745c9b14a4f9b Mon Sep 17 00:00:00 2001 From: Cory Andrew Mayer Date: Wed, 18 Dec 2024 15:37:11 -0500 Subject: [PATCH] tests: subsys/fs/nvs: add test for initializing bad region Signed-off-by: Cory Andrew Mayer --- tests/subsys/fs/nvs/prj.conf | 1 + tests/subsys/fs/nvs/src/main.c | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tests/subsys/fs/nvs/prj.conf b/tests/subsys/fs/nvs/prj.conf index 75740e3df33328a..c046194738a786e 100644 --- a/tests/subsys/fs/nvs/prj.conf +++ b/tests/subsys/fs/nvs/prj.conf @@ -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 diff --git a/tests/subsys/fs/nvs/src/main.c b/tests/subsys/fs/nvs/src/main.c index 85aaf9f1b79f38f..0b088648b068334 100644 --- a/tests/subsys/fs/nvs/src/main.c +++ b/tests/subsys/fs/nvs/src/main.c @@ -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) - + 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 +}