From 1f29295e93d7fea49fc99a71f66f6c019363a40c Mon Sep 17 00:00:00 2001 From: asterix Date: Thu, 29 Nov 2007 15:11:48 +0000 Subject: [PATCH] Add generic test for kfile interface. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1013 38d2e660-2303-0410-9eaa-f027e97ec537 --- kern/kfile.c | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 kern/kfile.c diff --git a/kern/kfile.c b/kern/kfile.c new file mode 100644 index 00000000..b9934e79 --- /dev/null +++ b/kern/kfile.c @@ -0,0 +1,255 @@ +/** + * \file + * + * + * \brief Virtual KFile I/O interface. + * This module implement a standard fd.seek and a kfile + * test function. + * + * \version $Id$ + * \author Francesco Sacchi + * \author Daniele Basile + * + */ + +#include + + +#if CONFIG_TEST + +/** + * Program memory read/write subtest. + * Try to write/read in the same \a f file location \a _size bytes. + * \return true if all is ok, false otherwise + * \note Restore file position at exit (if no error) + * \note Test buffer \a buf must be filled with + * the following statement: + *
+ * buf[i] = i & 0xff
+ * 
+ */ +static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size) +{ + int32_t size = _size; + + // Write test buffer + if (f->write(f, buf, size) != size) + return false; + f->seek(f, -size, KSM_SEEK_CUR); + + // Reset test buffer + memset(buf, 0, size); + + // Read flash in test buffer + if (f->read(f, buf, size) != size) + return false; + f->seek(f, -size, KSM_SEEK_CUR); + + // Check test result + for (size_t i = 0; i < size; i++) + if (buf[i] != (i & 0xff)) + return false; + + return true; +} + +/** + * Test for program memory read/write. + */ +bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_buf_size) +{ + KFile fd; + int32_t size = _size; + + /* + * Part of test buf size that you would write. + * This var is useded in test 3 to check fd.write + * when write outside size limit. Normaly we want + * perform a write until is space to write, otherwise + * we return. + */ + int32_t len = size/2; + + /* + * Fill in test buffer + */ + for (int i = 0; i < size; i++) + test_buf[i] = (i & 0xff); + + kprintf("Generated test string..\n"); + + /* + * Open fd handler + */ + fd.open(&fd, NULL, 0); + kprintf("Opened fd handler..\n"); + + /* + * If necessary, user could save content, + * for later restore. + */ + if (save_buf != NULL) + { + fd.read(&fd, save_buf, save_buf_size); + kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + } + + /* TEST 1 BEGIN. */ + kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size); + + /* + * Seek to addr 0 + */ + if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0) + goto kfile_test_end; + + kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos); + + /* + * Test flash read/write to address 0..size + */ + if (!Kfile_rwTest(&fd, test_buf, size)) + goto kfile_test_end; + + kprintf("Test 1: ok!\n"); + + /* + * Restore previous read content + */ + if (save_buf != NULL) + { + fd.seek(&fd, 0, KSM_SEEK_SET); + + if (fd.write(&fd, save_buf, save_buf_size) != size) + goto kfile_test_end; + + kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + } + /* TEST 1 END. */ + + /* TEST 2 BEGIN. */ + kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size); + + /* + * Go to half test size. + */ + fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET); + + kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2); + + /* + * If necessary, user could save content, + * for later restore. + */ + if (save_buf != NULL) + { + fd.read(&fd, save_buf, save_buf_size); + fd.seek(&fd, -size, KSM_SEEK_CUR); + kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + } + + /* + * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size + */ + if (!Kfile_rwTest(&fd, test_buf, size)) + goto kfile_test_end; + + kprintf("Test 2: ok!\n"); + + /* + * Restore previous read content + */ + if (save_buf != NULL) + { + fd.seek(&fd, -size, KSM_SEEK_CUR); + + if (fd.write(&fd, save_buf, save_buf_size) != size) + goto kfile_test_end; + + kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + } + + /* TEST 2 END. */ + + /* TEST 3 BEGIN. */ + kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size); + + /* + * Go to the Flash end + */ + fd.seek(&fd, -len, KSM_SEEK_END); + kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len); + + /* + * If necessary, user could save content, + * for later restore. + */ + if (save_buf != NULL) + { + ASSERT(len > save_buf_size); + + fd.read(&fd, save_buf, len); + fd.seek(&fd, -len, KSM_SEEK_CUR); + kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len); + } + + /* + * Test flash read/write to address (FLASHEND - size) ... FLASHEND + */ + if (!Kfile_rwTest(&fd, test_buf, size)) + goto kfile_test_end; + + kprintf("Test 3: ok !\n"); + + /* + * Restore previous read content + */ + if (save_buf != NULL) + { + fd.seek(&fd, -len, KSM_SEEK_END); + + if (fd.write(&fd, save_buf, len) != len) + goto kfile_test_end; + + kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len); + } + + /* TEST 3 END. */ + + fd.close(&fd); + return true; + +kfile_test_end: + fd.close(&fd); + return false; +} + +#endif /* CONFIG_TEST */ \ No newline at end of file -- 2.25.1