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.
37 * \author Francesco Sacchi <batt@develer.com>
38 * \author Daniele Basile <asterix@develer.com>
43 #include <struct/kfile_mem.h>
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 static uint8_t test_buf[BUF_TEST_LEN];
67 static uint8_t test_buf_save[BUF_TEST_LEN];
69 static uint8_t test_disk[BUF_TEST_LEN];
73 * Help function to init disk and the buffers.
75 static void init_testBuf(void)
78 kprintf("Init fake buffer..\n");
79 for (int i = 0; i < BUF_TEST_LEN; i++)
82 kprintf("%d ", test_disk[i]);
86 memset(test_buf, 0, sizeof(test_buf));
87 memset(test_buf_save, 0, sizeof(test_buf_save));
91 * KFile read/write subtest.
92 * Try to write/read in the same \a f file location \a size bytes.
93 * \return true if all is ok, false otherwise
94 * \note Restore file position at exit (if no error)
95 * \note Test buffer \a buf must be filled with
96 * the following statement:
101 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
106 if (kfile_write(f, buf, size) != size)
108 LOG_ERR("error writing buf");
112 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
117 memset(buf, 0, size);
120 * Read file in test buffer
122 if (kfile_read(f, buf, size) != size)
124 LOG_ERR("error reading buf");
129 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
134 for (size_t i = 0; i < size; i++)
136 if (buf[i] != (i & 0xff))
138 LOG_ERR("error comparing at index [%d] read [%02x] expected [%02x]\n", i, buf[i], i);
147 * KFile read/write test.
148 * This function write and read \a test_buf long \a size
150 * \a save_buf can be NULL or a buffer where to save previous file content.
152 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
156 * Part of test buf size that you would write.
157 * This var is used in test 3 to check kfile_write
158 * when writing beyond filesize limit.
160 kfile_off_t len = size / 2;
163 /* Fill test buffer */
164 for (size_t i = 0; i < size; i++)
165 test_buf[i] = (i & 0xff);
168 * If necessary, user can save content,
173 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
174 kfile_read(fd, save_buf, size);
178 LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
183 if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
187 * Test read/write to address 0..size
189 if (!kfile_rwTest(fd, test_buf, size))
192 LOG_INFO("Test 1: ok!\n");
195 * Restore previous read content.
199 kfile_seek(fd, 0, KSM_SEEK_SET);
201 if (kfile_write(fd, save_buf, size) != size)
204 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
209 LOG_INFO("Test 2: write from pos [%ld] to [%ld]\n", fd->size/2 , fd->size/2 + size);
212 * Go to half test size.
214 kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
217 * If necessary, user can save content
222 kfile_read(fd, save_buf, size);
223 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
224 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
228 * Test read/write to address filesize/2 ... filesize/2 + size
230 if (!kfile_rwTest(fd, test_buf, size))
233 LOG_INFO("Test 2: ok!\n");
236 * Restore previous content.
240 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
242 if (kfile_write(fd, save_buf, size) != size)
245 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
251 LOG_INFO("Test 3: write outside of fd->size limit [%ld]\n", fd->size);
254 * Go to the Flash end
256 kfile_seek(fd, -len, KSM_SEEK_END);
259 * If necessary, user can save content,
264 kfile_read(fd, save_buf, len);
265 kfile_seek(fd, -len, KSM_SEEK_CUR);
266 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
270 * Test read/write to address (filesize - size) ... filesize
272 if (kfile_rwTest(fd, test_buf, size))
275 kprintf("Test 3: ok!\n");
278 * Restore previous read content
282 kfile_seek(fd, -len, KSM_SEEK_END);
284 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
287 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
297 LOG_ERR("One kfile_test failed!\n");
305 * Setup all needed for kfile test
307 int kfile_testSetup(void)
309 MOD_INIT(kfile_test);
310 LOG_INFO("Mod init..ok\n");
312 // Init our backend and the buffers
313 kfilemem_init(&mem, test_disk, BUF_TEST_LEN);
319 int kfile_testRun(void)
321 return kfile_testRunGeneric(&mem.fd, test_buf, test_buf_save, BUF_TEST_LEN);
325 * End a dataflash Test.
328 int kfile_testTearDown(void)