diff --git a/features/storage/TESTS/kvstore/general_tests_phase_1/main.cpp b/features/storage/TESTS/kvstore/general_tests_phase_1/main.cpp index 3fc9e08bf7..80aef883f5 100644 --- a/features/storage/TESTS/kvstore/general_tests_phase_1/main.cpp +++ b/features/storage/TESTS/kvstore/general_tests_phase_1/main.cpp @@ -78,6 +78,12 @@ static inline uint32_t align_up(uint32_t val, uint32_t size) //init the blockdevice static void kvstore_init() { + // This directly corresponds to the pages allocated for each of the SecureStore block devices + // For the others it may not match exactly to the space that is used, but it is expected to + // be a close enough approximation to act as a guideline for how much of the block device we + // need to erase in order to ensure a stable initial condition. + const size_t PAGES_ESTIMATE = 40; + int res; size_t program_size, erase_size, ul_bd_size, rbp_bd_size; BlockDevice *sec_bd; @@ -85,6 +91,11 @@ static void kvstore_init() res = bd->init(); TEST_ASSERT_EQUAL_ERROR_CODE(0, res); int erase_val = bd->get_erase_value(); + // Clear out any stale data that might be left from a previous test. + // Multiply by 2 because SecureStore requires two underlying block devices of this size + size_t bytes_to_erase = align_up(2 * PAGES_ESTIMATE * bd->get_program_size(), bd->get_erase_size()); + + bd->erase(0, bytes_to_erase); res = bd->deinit(); TEST_ASSERT_EQUAL_ERROR_CODE(0, res); @@ -121,9 +132,8 @@ static void kvstore_init() erase_size = sec_bd->get_erase_size(); // We must be able to hold at least 10 small keys (20 program sectors) and master record + internal data // but minimum of 2 erase sectors, so that the garbage collection way work - ul_bd_size = align_up(program_size * 40, erase_size * 2); - rbp_bd_size = align_up(program_size * 40, erase_size * 2); - + ul_bd_size = align_up(program_size * PAGES_ESTIMATE, erase_size * 2); + rbp_bd_size = align_up(program_size * PAGES_ESTIMATE, erase_size * 2); TEST_ASSERT((ul_bd_size + rbp_bd_size) < sec_bd->size()); res = sec_bd->deinit(); diff --git a/features/storage/TESTS/kvstore/general_tests_phase_2/main.cpp b/features/storage/TESTS/kvstore/general_tests_phase_2/main.cpp index 71b01a6838..a7ce55b427 100644 --- a/features/storage/TESTS/kvstore/general_tests_phase_2/main.cpp +++ b/features/storage/TESTS/kvstore/general_tests_phase_2/main.cpp @@ -78,6 +78,12 @@ static inline uint32_t align_up(uint32_t val, uint32_t size) //init the blockdevice static void kvstore_init() { + // This directly corresponds to the pages allocated for each of the SecureStore block devices + // For the others it may not match exactly to the space that is used, but it is expected to + // be a close enough approximation to act as a guideline for how much of the block device we + // need to erase in order to ensure a stable initial condition. + const size_t PAGES_ESTIMATE = 40; + int res; size_t program_size, erase_size, ul_bd_size, rbp_bd_size; BlockDevice *sec_bd; @@ -85,6 +91,11 @@ static void kvstore_init() res = bd->init(); TEST_ASSERT_EQUAL_ERROR_CODE(0, res); int erase_val = bd->get_erase_value(); + // Clear out any stale data that might be left from a previous test + // Multiply by 2 because SecureStore requires two underlying block devices of this size + size_t bytes_to_erase = align_up(2 * PAGES_ESTIMATE * bd->get_program_size(), bd->get_erase_size()); + + bd->erase(0, bytes_to_erase); res = bd->deinit(); TEST_ASSERT_EQUAL_ERROR_CODE(0, res); @@ -121,8 +132,8 @@ static void kvstore_init() erase_size = sec_bd->get_erase_size(); // We must be able to hold at least 10 small keys (20 program sectors) and master record + internal data // but minimum of 2 erase sectors, so that the garbage collection way work - ul_bd_size = align_up(program_size * 40, erase_size * 2); - rbp_bd_size = align_up(program_size * 40, erase_size * 2); + ul_bd_size = align_up(program_size * PAGES_ESTIMATE, erase_size * 2); + rbp_bd_size = align_up(program_size * PAGES_ESTIMATE, erase_size * 2); res = sec_bd->deinit(); TEST_ASSERT_EQUAL_ERROR_CODE(MBED_SUCCESS, res);