Skip to content

Commit c3dbf38

Browse files
author
Joakim Nohlgård
committed
unittests: add unit test for mtd-vfs
1 parent ee8cf6e commit c3dbf38

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
USEMODULE += mtd
2+
USEMODULE += vfs

tests/unittests/tests-mtd/tests-mtd.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "mtd.h"
2020
#include "board.h"
2121

22+
#if MODULE_VFS
23+
#include <fcntl.h>
24+
#include "vfs.h"
25+
#endif
26+
2227
/* Define MTD_0 in board.h to use the board mtd if any */
2328
#ifdef MTD_0
2429
#define _dev MTD_0
@@ -237,6 +242,34 @@ static void test_mtd_write_read_flash(void)
237242
TEST_ASSERT_EQUAL_INT(0, memcmp(buf_empty, buf_read + sizeof(buf_expected), sizeof(buf_empty)));
238243
}
239244

245+
#if MODULE_VFS
246+
static void test_mtd_vfs(void)
247+
{
248+
int fd;
249+
fd = vfs_bind(VFS_ANY_FD, O_RDWR, &mtd_vfs_ops, &_dev);
250+
const char buf[] = "mnopqrst";
251+
uint8_t buf_empty[] = {0xff, 0xff, 0xff};
252+
char buf_read[sizeof(buf) + sizeof(buf_empty)];
253+
memset(buf_read, 0, sizeof(buf_read));
254+
255+
int ret = vfs_lseek(fd, sizeof(buf_empty), SEEK_SET);
256+
TEST_ASSERT_EQUAL_INT(sizeof(buf_empty), ret);
257+
ret = vfs_write(fd, buf, sizeof(buf));
258+
TEST_ASSERT_EQUAL_INT(sizeof(buf), ret);
259+
ret = vfs_lseek(fd, 0, SEEK_SET);
260+
TEST_ASSERT_EQUAL_INT(0, ret);
261+
ret = vfs_read(fd, buf_read, sizeof(buf_read));
262+
TEST_ASSERT_EQUAL_INT(sizeof(buf_read), ret);
263+
TEST_ASSERT_EQUAL_INT(0, memcmp(buf_empty, buf_read, sizeof(buf_empty)));
264+
TEST_ASSERT_EQUAL_INT(0, memcmp(buf, buf_read + sizeof(buf_empty), sizeof(buf)));
265+
266+
ret = vfs_lseek(fd, 0, SEEK_END);
267+
TEST_ASSERT(ret > 0);
268+
ret = vfs_write(fd, buf, sizeof(buf));
269+
/* Attempted to write past the device memory */
270+
TEST_ASSERT(ret < 0);
271+
}
272+
#endif
240273

241274
Test *tests_mtd_tests(void)
242275
{
@@ -246,6 +279,9 @@ Test *tests_mtd_tests(void)
246279
new_TestFixture(test_mtd_write_erase),
247280
new_TestFixture(test_mtd_write_read),
248281
new_TestFixture(test_mtd_write_read_flash),
282+
#if MODULE_VFS
283+
new_TestFixture(test_mtd_vfs),
284+
#endif
249285
};
250286

251287
EMB_UNIT_TESTCALLER(mtd_tests, setup_teardown, setup_teardown, fixtures);

0 commit comments

Comments
 (0)