log: Retouch documentation; Rearrenge level logic; Rename LOG_VERBOSITY to LOG_FORMAT...
[bertos.git] / bertos / kern / kfile_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Test suite for virtual KFile I/O interface.
34  *
35  * This module implements a test for some generic I/O interfaces for kfile.
36  *
37  * \version $Id$
38  * \author Francesco Sacchi <batt@develer.com>
39  * \author Daniele Basile <asterix@develer.com>
40  */
41
42
43 #include "kfile.h"
44
45 #include "cfg/cfg_kfile.h"
46 #include <cfg/debug.h>
47 #include <cfg/module.h>
48
49 // Define logging setting (for cfg/log.h module).
50 #define LOG_LEVEL   KFILE_LOG_LEVEL
51 #define LOG_FORMAT  KFILE_LOG_FORMAT
52 #include <cfg/log.h>
53
54 #include <mware/formatwr.h>
55
56 #include <string.h>
57
58
59 MOD_DEFINE(kfile_test);
60
61
62 /**
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:
69  * <pre>
70  * buf[i] = i & 0xff
71  * </pre>
72  */
73 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
74 {
75         /*
76          * Write test buffer
77          */
78         if (kfile_write(f, buf, size) != size)
79                 return false;
80
81         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
82
83         /*
84          * Reset test buffer
85          */
86         memset(buf, 0, size);
87
88         /*
89          * Read file in test buffer
90          */
91         if (kfile_read(f, buf, size) != size)
92                 return false;
93         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
94
95         /*
96          * Check test result
97          */
98         for (size_t i = 0; i < size; i++)
99                 if (buf[i] != (i & 0xff))
100                         return false;
101
102         return true;
103 }
104
105 /**
106  * Setup all needed for kfile test
107  */
108 int kfile_testSetUp(void)
109 {
110         MOD_INIT(kfile_test);
111         LOG_INFO("Mod init..ok\n");
112
113         return 0;
114 }
115
116
117 /**
118  * KFile read/write test.
119  * This function write and read \a test_buf long \a size
120  * on \a fd handler.
121  * \a save_buf can be NULL or a buffer where to save previous file content.
122  */
123 int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
124 {
125
126         /*
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.
130          */
131         kfile_off_t len = size / 2;
132
133
134         /* Fill test buffer */
135         for (size_t i = 0; i < size; i++)
136                 test_buf[i] = (i & 0xff);
137
138         /*
139          * If necessary, user can save content,
140          * for later restore.
141          */
142         if (save_buf)
143         {
144                 kfile_read(fd, save_buf, size);
145                 LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
146         }
147
148         /* TEST 1 BEGIN. */
149         LOG_INFO("Test 1: write from pos 0 to [%lu]\n", size);
150
151         /*
152          * Seek to addr 0.
153          */
154         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
155                 goto kfile_test_end;
156
157         /*
158          * Test read/write to address 0..size
159          */
160         if (!kfile_rwTest(fd, test_buf, size))
161                 goto kfile_test_end;
162
163         LOG_INFO("Test 1: ok!\n");
164
165         /*
166          * Restore previous read content.
167          */
168         if (save_buf)
169         {
170                 kfile_seek(fd, 0, KSM_SEEK_SET);
171
172                 if (kfile_write(fd, save_buf, size) != size)
173                         goto kfile_test_end;
174
175                 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
176         }
177         /* TEST 1 END. */
178
179         /* TEST 2 BEGIN. */
180         LOG_INFO("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size);
181
182         /*
183          * Go to half test size.
184          */
185         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
186
187         /*
188          * If necessary, user can save content
189          * for later restore.
190          */
191         if (save_buf)
192         {
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);
196         }
197
198         /*
199          * Test read/write to address filesize/2 ... filesize/2 + size
200          */
201         if (!kfile_rwTest(fd, test_buf, size))
202                 goto kfile_test_end;
203
204         LOG_INFO("Test 2: ok!\n");
205
206         /*
207          * Restore previous content.
208          */
209         if (save_buf)
210         {
211                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
212
213                 if (kfile_write(fd, save_buf, size) != size)
214                         goto kfile_test_end;
215
216                 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
217         }
218
219         /* TEST 2 END. */
220
221         /* TEST 3 BEGIN. */
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");
224
225         /*
226          * Go to the Flash end
227          */
228         kfile_seek(fd, -len, KSM_SEEK_END);
229
230         /*
231          * If necessary, user can save content,
232          * for later restore.
233          */
234         if (save_buf)
235         {
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);
239         }
240
241         /*
242          * Test read/write to address (filesize - size) ... filesize
243          */
244         if (kfile_rwTest(fd, test_buf, size))
245                 goto kfile_test_end;
246
247         kprintf("Test 3: ok!\n");
248
249         /*
250          * Restore previous read content
251          */
252         if (save_buf)
253         {
254                 kfile_seek(fd, -len, KSM_SEEK_END);
255
256                 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
257                         goto kfile_test_end;
258
259                 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
260         }
261
262         /* TEST 3 END. */
263
264         kfile_close(fd);
265         return 0;
266
267 kfile_test_end:
268         kfile_close(fd);
269         LOG_ERR("One kfile_test fail!\n");
270         return EOF;
271 }
272
273 /**
274  * End a dataflash Test.
275  * (Unused)
276  */
277 int kfile_testTearDown(void)
278 {
279         /*    */
280         return 0;
281 }