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>
47 #include <cfg/module.h>
49 // Define logging setting (for cfg/log.h module).
50 #define LOG_LEVEL KFILE_LOG_LEVEL
51 #define LOG_FORMAT KFILE_LOG_FORMAT
54 #include <mware/formatwr.h>
59 MOD_DEFINE(kfile_test);
63 * KFile read/write subtest.
64 * Try to write/read in the same \a f file location \a size bytes.
65 * \return true if all is ok, false otherwise
66 * \note Restore file position at exit (if no error)
67 * \note Test buffer \a buf must be filled with
68 * the following statement:
73 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
78 if (kfile_write(f, buf, size) != size)
81 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
89 * Read file in test buffer
91 if (kfile_read(f, buf, size) != size)
94 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
99 for (size_t i = 0; i < size; i++)
100 if (buf[i] != (i & 0xff))
106 * Setup all needed for kfile test
108 int kfile_testSetUp(void)
110 MOD_INIT(kfile_test);
111 LOG_INFO("Mod init..ok\n");
118 * KFile read/write test.
119 * This function write and read \a test_buf long \a size
121 * \a save_buf can be NULL or a buffer where to save previous file content.
123 int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
127 * Part of test buf size that you would write.
128 * This var is used in test 3 to check kfile_write
129 * when writing beyond filesize limit.
131 kfile_off_t len = size / 2;
134 /* Fill test buffer */
135 for (size_t i = 0; i < size; i++)
136 test_buf[i] = (i & 0xff);
139 * If necessary, user can save content,
144 kfile_read(fd, save_buf, size);
145 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
149 LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
154 if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
158 * Test read/write to address 0..size
160 if (!kfile_rwTest(fd, test_buf, size))
163 LOG_INFO("Test 1: ok!\n");
166 * Restore previous read content.
170 kfile_seek(fd, 0, KSM_SEEK_SET);
172 if (kfile_write(fd, save_buf, size) != size)
175 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
180 LOG_INFO("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size);
183 * Go to half test size.
185 kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
188 * If necessary, user can save content
193 kfile_read(fd, save_buf, size);
194 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
195 LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
199 * Test read/write to address filesize/2 ... filesize/2 + size
201 if (!kfile_rwTest(fd, test_buf, size))
204 LOG_INFO("Test 2: ok!\n");
207 * Restore previous content.
211 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
213 if (kfile_write(fd, save_buf, size) != size)
216 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
222 LOG_INFO("Test 3: write outside of fd->size limit [%lu]\n", fd->size);
223 LOG_INFO("This test should FAIL!, you must see an assertion fail message.\n");
226 * Go to the Flash end
228 kfile_seek(fd, -len, KSM_SEEK_END);
231 * If necessary, user can save content,
236 kfile_read(fd, save_buf, len);
237 kfile_seek(fd, -len, KSM_SEEK_CUR);
238 LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
242 * Test read/write to address (filesize - size) ... filesize
244 if (kfile_rwTest(fd, test_buf, size))
247 kprintf("Test 3: ok!\n");
250 * Restore previous read content
254 kfile_seek(fd, -len, KSM_SEEK_END);
256 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
259 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
269 LOG_ERR("One kfile_test failed!\n");
274 * End a dataflash Test.
277 int kfile_testTearDown(void)