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)
109 LOG_ERR("error writing buf");
113 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
118 memset(buf, 0, size);
121 * Read file in test buffer
123 if (kfile_read(f, buf, size) != size)
125 LOG_ERR("error reading buf");
130 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
135 for (size_t i = 0; i < size; i++)
137 if (buf[i] != (i & 0xff))
139 LOG_ERR("error comparing at index [%d] read [%02x] expected [%02x]\n", i, buf[i], i);
148 * KFile read/write test.
149 * This function write and read \a test_buf long \a size
151 * \a save_buf can be NULL or a buffer where to save previous file content.
153 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
157 * Part of test buf size that you would write.
158 * This var is used in test 3 to check kfile_write
159 * when writing beyond filesize limit.
161 kfile_off_t len = size / 2;
164 /* Fill test buffer */
165 for (size_t i = 0; i < size; i++)
166 test_buf[i] = (i & 0xff);
169 * If necessary, user can save content,
174 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
175 kfile_read(fd, save_buf, size);
179 LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
184 if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
188 * Test read/write to address 0..size
190 if (!kfile_rwTest(fd, test_buf, size))
193 LOG_INFO("Test 1: ok!\n");
196 * Restore previous read content.
200 kfile_seek(fd, 0, KSM_SEEK_SET);
202 if (kfile_write(fd, save_buf, size) != size)
205 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
210 LOG_INFO("Test 2: write from pos [%ld] to [%ld]\n", fd->size/2 , fd->size/2 + size);
213 * Go to half test size.
215 kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
218 * If necessary, user can save content
223 kfile_read(fd, save_buf, size);
224 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
225 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
229 * Test read/write to address filesize/2 ... filesize/2 + size
231 if (!kfile_rwTest(fd, test_buf, size))
234 LOG_INFO("Test 2: ok!\n");
237 * Restore previous content.
241 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
243 if (kfile_write(fd, save_buf, size) != size)
246 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
252 LOG_INFO("Test 3: write outside of fd->size limit [%ld]\n", fd->size);
255 * Go to the Flash end
257 kfile_seek(fd, -len, KSM_SEEK_END);
260 * If necessary, user can save content,
265 kfile_read(fd, save_buf, len);
266 kfile_seek(fd, -len, KSM_SEEK_CUR);
267 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
271 * Test read/write to address (filesize - size) ... filesize
273 if (kfile_rwTest(fd, test_buf, size))
276 kprintf("Test 3: ok!\n");
279 * Restore previous read content
283 kfile_seek(fd, -len, KSM_SEEK_END);
285 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
288 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
298 LOG_ERR("One kfile_test failed!\n");
306 * Setup all needed for kfile test
308 int kfile_testSetup(void)
310 MOD_INIT(kfile_test);
311 LOG_INFO("Mod init..ok\n");
313 // Init our backend and the buffers
314 kfilemem_init(&mem, test_disk, BUF_TEST_LEN);
320 int kfile_testRun(void)
322 return kfile_testRunGeneric(&mem.fd, test_buf, test_buf_save, BUF_TEST_LEN);
326 * End a dataflash Test.
329 int kfile_testTearDown(void)