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>
44 #include <struct/kfile_mem.h>
46 #include "cfg/cfg_kfile.h"
47 #include <cfg/debug.h>
49 #include <cfg/module.h>
51 // Define logging setting (for cfg/log.h module).
52 #define LOG_LEVEL KFILE_LOG_LEVEL
53 #define LOG_FORMAT KFILE_LOG_FORMAT
56 #include <mware/formatwr.h>
60 MOD_DEFINE(kfile_test);
62 // Size of the "virtual" disk that
64 #define BUF_TEST_LEN 3209
67 uint8_t test_buf[BUF_TEST_LEN];
68 uint8_t test_buf_save[BUF_TEST_LEN];
70 uint8_t test_disk[BUF_TEST_LEN];
74 * Help function to init disk and the buffers.
76 static void init_testBuf(void)
79 kprintf("Init fake buffer..\n");
80 for (int i = 0; i < BUF_TEST_LEN; i++)
83 kprintf("%d ", test_disk[i]);
87 memset(test_buf, 0, sizeof(test_buf));
88 memset(test_buf_save, 0, sizeof(test_buf_save));
92 * KFile read/write subtest.
93 * Try to write/read in the same \a f file location \a size bytes.
94 * \return true if all is ok, false otherwise
95 * \note Restore file position at exit (if no error)
96 * \note Test buffer \a buf must be filled with
97 * the following statement:
102 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
107 if (kfile_write(f, buf, size) != size)
110 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
115 memset(buf, 0, size);
118 * Read file in test buffer
120 if (kfile_read(f, buf, size) != size)
123 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
128 for (size_t i = 0; i < size; i++)
129 if (buf[i] != (i & 0xff))
136 * KFile read/write test.
137 * This function write and read \a test_buf long \a size
139 * \a save_buf can be NULL or a buffer where to save previous file content.
141 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
145 * Part of test buf size that you would write.
146 * This var is used in test 3 to check kfile_write
147 * when writing beyond filesize limit.
149 kfile_off_t len = size / 2;
152 /* Fill test buffer */
153 for (size_t i = 0; i < size; i++)
154 test_buf[i] = (i & 0xff);
157 * If necessary, user can save content,
162 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
163 kfile_read(fd, save_buf, size);
167 LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
172 if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
176 * Test read/write to address 0..size
178 if (!kfile_rwTest(fd, test_buf, size))
181 LOG_INFO("Test 1: ok!\n");
184 * Restore previous read content.
188 kfile_seek(fd, 0, KSM_SEEK_SET);
190 if (kfile_write(fd, save_buf, size) != size)
193 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
198 LOG_INFO("Test 2: write from pos [%ld] to [%ld]\n", fd->size/2 , fd->size/2 + size);
201 * Go to half test size.
203 kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
206 * If necessary, user can save content
211 kfile_read(fd, save_buf, size);
212 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
213 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
217 * Test read/write to address filesize/2 ... filesize/2 + size
219 if (!kfile_rwTest(fd, test_buf, size))
222 LOG_INFO("Test 2: ok!\n");
225 * Restore previous content.
229 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
231 if (kfile_write(fd, save_buf, size) != size)
234 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
240 LOG_INFO("Test 3: write outside of fd->size limit [%ld]\n", fd->size);
243 * Go to the Flash end
245 kfile_seek(fd, -len, KSM_SEEK_END);
248 * If necessary, user can save content,
253 kfile_read(fd, save_buf, len);
254 kfile_seek(fd, -len, KSM_SEEK_CUR);
255 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
259 * Test read/write to address (filesize - size) ... filesize
261 if (kfile_rwTest(fd, test_buf, size))
264 kprintf("Test 3: ok!\n");
267 * Restore previous read content
271 kfile_seek(fd, -len, KSM_SEEK_END);
273 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
276 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
286 LOG_ERR("One kfile_test failed!\n");
294 * Setup all needed for kfile test
296 int kfile_testSetup(void)
298 MOD_INIT(kfile_test);
299 LOG_INFO("Mod init..ok\n");
301 // Init our backend and the buffers
302 kfilemem_init(&mem, test_disk, BUF_TEST_LEN);
308 int kfile_testRun(void)
310 return kfile_testRunGeneric(&mem.fd, test_buf, test_buf_save, BUF_TEST_LEN);
314 * End a dataflash Test.
317 int kfile_testTearDown(void)