4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief Test suite for virtual KFile I/O interface.
35 * This module implements a test for some generic I/O interfaces for kfile.
38 * \author Francesco Sacchi <batt@develer.com>
39 * \author Daniele Basile <asterix@develer.com>
45 #include "cfg/cfg_kfile.h"
46 #include <cfg/debug.h>
48 #include <cfg/module.h>
50 // Define logging setting (for cfg/log.h module).
51 #define LOG_LEVEL KFILE_LOG_LEVEL
52 #define LOG_FORMAT KFILE_LOG_FORMAT
55 #include <mware/formatwr.h>
59 MOD_DEFINE(kfile_test);
61 // Size of the "virtual" disk that
63 #define BUF_TEST_LEN 3209
66 typedef uint8_t fake_t;
67 fake_t test_buf[BUF_TEST_LEN];
68 fake_t test_buf_save[BUF_TEST_LEN];
69 fake_t test_disk[BUF_TEST_LEN];
74 * Beckend to use kfile structure on pc.
76 static int fake_close(KFile *fd)
82 static size_t fake_read(KFile *fd, void *buf, size_t size)
84 fake_t *dest = (fake_t *)buf;
87 rd_len = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
89 memcpy(dest, test_disk, size);
90 fd->seek_pos += rd_len;
92 LOG_INFO("Read: real[%ld] expected[%ld]\n", rd_len, size);
97 static size_t fake_write(KFile *fd, const void *buf, size_t size)
99 fake_t *src = (fake_t *)buf;
102 wr_len = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
104 memcpy(test_disk, src, wr_len);
105 fd->seek_pos += wr_len;
107 LOG_INFO("Write: real[%ld] expected[%ld]\n", wr_len, size);
112 int fake_flush(KFile *fd)
119 void fake_kfileInit(void)
121 // Setup data flash programming functions.
122 fd.reopen = kfile_genericReopen;
123 fd.close = fake_close;
125 fd.write = fake_write;
126 fd.seek = kfile_genericSeek;
127 fd.flush = fake_flush;
130 fd.size = BUF_TEST_LEN;
135 * Help function to init disk and the buffers.
137 static void init_testBuf(void)
141 kprintf("Init fake buffer..\n");
142 for (int i = 0; i < BUF_TEST_LEN; i++)
144 test_disk[i] = random();
145 kprintf("%d ", test_disk[i]);
149 memset(test_buf, 0, sizeof(test_buf));
150 memset(test_buf_save, 0, sizeof(test_buf_save));
154 * KFile read/write subtest.
155 * Try to write/read in the same \a f file location \a size bytes.
156 * \return true if all is ok, false otherwise
157 * \note Restore file position at exit (if no error)
158 * \note Test buffer \a buf must be filled with
159 * the following statement:
164 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
169 if (kfile_write(f, buf, size) != size)
172 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
177 memset(buf, 0, size);
180 * Read file in test buffer
182 if (kfile_read(f, buf, size) != size)
185 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
190 for (size_t i = 0; i < size; i++)
191 if (buf[i] != (i & 0xff))
198 * KFile read/write test.
199 * This function write and read \a test_buf long \a size
201 * \a save_buf can be NULL or a buffer where to save previous file content.
203 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
207 * Part of test buf size that you would write.
208 * This var is used in test 3 to check kfile_write
209 * when writing beyond filesize limit.
211 kfile_off_t len = size / 2;
214 /* Fill test buffer */
215 for (size_t i = 0; i < size; i++)
216 test_buf[i] = (i & 0xff);
219 * If necessary, user can save content,
224 LOG_INFO("Saved content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
225 kfile_read(fd, save_buf, size);
229 LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
234 if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
238 * Test read/write to address 0..size
240 if (!kfile_rwTest(fd, test_buf, size))
243 LOG_INFO("Test 1: ok!\n");
246 * Restore previous read content.
250 kfile_seek(fd, 0, KSM_SEEK_SET);
252 if (kfile_write(fd, save_buf, size) != size)
255 LOG_INFO("Restore content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
260 LOG_INFO("Test 2: write from pos [%d] to [%ld]\n", fd->size/2 , fd->size/2 + size);
263 * Go to half test size.
265 kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
268 * If necessary, user can save content
273 kfile_read(fd, save_buf, size);
274 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
275 LOG_INFO("Saved content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
279 * Test read/write to address filesize/2 ... filesize/2 + size
281 if (!kfile_rwTest(fd, test_buf, size))
284 LOG_INFO("Test 2: ok!\n");
287 * Restore previous content.
291 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
293 if (kfile_write(fd, save_buf, size) != size)
296 LOG_INFO("Restore content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
302 LOG_INFO("Test 3: write outside of fd->size limit [%d]\n", fd->size);
303 LOG_INFO("This test should FAIL!, you must see an assertion fail message.\n");
306 * Go to the Flash end
308 kfile_seek(fd, -len, KSM_SEEK_END);
311 * If necessary, user can save content,
316 kfile_read(fd, save_buf, len);
317 kfile_seek(fd, -len, KSM_SEEK_CUR);
318 LOG_INFO("Saved content..form [%d] to [%d]\n", fd->seek_pos, fd->seek_pos + len);
322 * Test read/write to address (filesize - size) ... filesize
324 if (kfile_rwTest(fd, test_buf, size))
327 kprintf("Test 3: ok!\n");
330 * Restore previous read content
334 kfile_seek(fd, -len, KSM_SEEK_END);
336 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
339 LOG_INFO("Restore content..form [%d] to [%d]\n", fd->seek_pos, fd->seek_pos + len);
349 LOG_ERR("One kfile_test failed!\n");
357 * Setup all needed for kfile test
359 int kfile_testSetup(void)
361 MOD_INIT(kfile_test);
362 LOG_INFO("Mod init..ok\n");
364 // Init our backend and the buffers
371 int kfile_testRun(void)
373 return kfile_testRunGeneric(&fd, test_buf, test_buf_save, BUF_TEST_LEN);
377 * End a dataflash Test.
380 int kfile_testTearDown(void)