Skip to content

Commit

Permalink
tests/mtd_flashpage: add test for AUX slot
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 18, 2022
1 parent dfe7b5d commit a17d320
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/mtd_flashpage/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ include ../Makefile.tests_common
USEMODULE += mtd_flashpage
USEMODULE += embunit

USEMODULE += mtd_write_page
# define an auxiliary slot on the internal flash
SLOT_AUX_LEN := 0x8000

include $(RIOTBASE)/Makefile.include
77 changes: 77 additions & 0 deletions tests/mtd_flashpage/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,90 @@ static void test_mtd_write_read(void)
#endif
}

#if CONFIG_SLOT_AUX_LEN
static bool mem_is_all_set(const uint8_t *buf, uint8_t c, size_t n)
{
for (const uint8_t *end = buf + n; buf != end; ++buf) {
if (*buf != c) {
return false;
}
}

return true;
}

static void test_mtd_aux_slot(void)
{
mtd_dev_t *dev = mtd_aux;

mtd_init(dev);

uint32_t sector = dev->sector_count - 2;

uint8_t buffer[FLASHPAGE_SIZE];

uint32_t page_0 = dev->pages_per_sector * sector;
uint32_t page_1 = dev->pages_per_sector * (sector + 1);
uint32_t page_size = dev->page_size;

/* write dummy data to sectors */
memset(buffer, 0x23, dev->page_size);
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev, buffer, page_0, 0, page_size), 0);
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev, buffer, page_1, 0, page_size), 0);

/* erase two sectors and check if they have been erased */
TEST_ASSERT_EQUAL_INT(mtd_erase_sector(dev, sector, 2), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_0, 0, page_size), 0);
TEST_ASSERT(mem_is_all_set(buffer, 0xFF, page_size) || mem_is_all_set(buffer, 0x00, page_size));
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_1, 0, page_size), 0);
TEST_ASSERT(mem_is_all_set(buffer, 0xFF, page_size) || mem_is_all_set(buffer, 0x00, page_size));

/* write test data & read it back */
const char test_str[] = "0123456789";
uint32_t offset = 5;

TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev, test_str, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);

/* write across page boundary */
offset = page_size - sizeof(test_str) / 2;
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev, test_str, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);

/* write across sector boundary */
offset = page_size - sizeof(test_str) / 2
+ (dev->pages_per_sector - 1) * page_size;
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev, test_str, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);

/* overwrite first test string, rely on MTD for read-modify-write */
const char test_str_2[] = "Hello World!";
offset = 5;
TEST_ASSERT_EQUAL_INT(mtd_write_page(dev, test_str_2, page_0, offset, sizeof(test_str_2)), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_0, offset, sizeof(test_str_2)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str_2, buffer, sizeof(test_str_2)), 0);

/* test write_page across sectors */
offset = dev->pages_per_sector * dev->page_size - 2;
TEST_ASSERT_EQUAL_INT(mtd_write_page(dev, test_str, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);
}
#endif

Test *tests_mtd_flashpage_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_mtd_init),
new_TestFixture(test_mtd_erase),
new_TestFixture(test_mtd_write_erase),
new_TestFixture(test_mtd_write_read),
#if CONFIG_SLOT_AUX_LEN
new_TestFixture(test_mtd_aux_slot),
#endif
};

EMB_UNIT_TESTCALLER(mtd_flashpage_tests, setup, teardown, fixtures);
Expand Down

0 comments on commit a17d320

Please sign in to comment.