410243e6bb97bc73b00c7ab71d2daf534c7b5553
[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 #include <struct/kfile_mem.h>
45
46 #include "cfg/cfg_kfile.h"
47 #include <cfg/debug.h>
48 #include <cfg/test.h>
49 #include <cfg/module.h>
50
51 // Define logging setting (for cfg/log.h module).
52 #define LOG_LEVEL   KFILE_LOG_LEVEL
53 #define LOG_FORMAT  KFILE_LOG_FORMAT
54 #include <cfg/log.h>
55
56 #include <mware/formatwr.h>
57
58 #include <string.h>
59
60 MOD_DEFINE(kfile_test);
61
62 // Size of the "virtual" disk that
63 // we want to test.
64 #define BUF_TEST_LEN     3209
65
66 // Buffer for test
67 uint8_t test_buf[BUF_TEST_LEN];
68 uint8_t test_buf_save[BUF_TEST_LEN];
69
70 uint8_t test_disk[BUF_TEST_LEN];
71 KFileMem mem;
72
73 /*
74  * Help function to init disk and the buffers.
75  */
76 static void init_testBuf(void)
77 {
78
79         kprintf("Init fake buffer..\n");
80         for (int i = 0; i < BUF_TEST_LEN; i++)
81         {
82                 test_disk[i] = i;
83                 kprintf("%d ", test_disk[i]);
84         }
85         kprintf("\nend\n");
86
87         memset(test_buf, 0, sizeof(test_buf));
88         memset(test_buf_save, 0, sizeof(test_buf_save));
89 }
90
91 /**
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:
98  * <pre>
99  * buf[i] = i & 0xff
100  * </pre>
101  */
102 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
103 {
104         /*
105          * Write test buffer
106          */
107         if (kfile_write(f, buf, size) != size)
108         {
109                 LOG_ERR("error writing buf");
110                 return false;
111         }
112
113         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
114
115         /*
116          * Reset test buffer
117          */
118         memset(buf, 0, size);
119
120         /*
121          * Read file in test buffer
122          */
123         if (kfile_read(f, buf, size) != size)
124         {
125                 LOG_ERR("error reading buf");
126                 return false;
127         }
128
129
130         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
131
132         /*
133          * Check test result
134          */
135         for (size_t i = 0; i < size; i++)
136         {
137                 if (buf[i] != (i & 0xff))
138                 {
139                         LOG_ERR("error comparing at index [%d] read [%02x] expected [%02x]\n", i, buf[i], i);
140                         return false;
141                 }
142         }
143
144         return true;
145 }
146
147 /**
148  * KFile read/write test.
149  * This function write and read \a test_buf long \a size
150  * on \a fd handler.
151  * \a save_buf can be NULL or a buffer where to save previous file content.
152  */
153 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
154 {
155
156         /*
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.
160          */
161         kfile_off_t len = size / 2;
162
163
164         /* Fill test buffer */
165         for (size_t i = 0; i < size; i++)
166                 test_buf[i] = (i & 0xff);
167
168         /*
169          * If necessary, user can save content,
170          * for later restore.
171          */
172         if (save_buf)
173         {
174                 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
175                 kfile_read(fd, save_buf, size);
176         }
177
178         /* TEST 1 BEGIN. */
179         LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
180
181         /*
182          * Seek to addr 0.
183          */
184         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
185                 goto kfile_test_end;
186
187         /*
188          * Test read/write to address 0..size
189          */
190         if (!kfile_rwTest(fd, test_buf, size))
191                 goto kfile_test_end;
192
193         LOG_INFO("Test 1: ok!\n");
194
195         /*
196          * Restore previous read content.
197          */
198         if (save_buf)
199         {
200                 kfile_seek(fd, 0, KSM_SEEK_SET);
201
202                 if (kfile_write(fd, save_buf, size) != size)
203                         goto kfile_test_end;
204
205                 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
206         }
207         /* TEST 1 END. */
208
209         /* TEST 2 BEGIN. */
210         LOG_INFO("Test 2: write from pos [%ld] to [%ld]\n", fd->size/2 , fd->size/2 + size);
211
212         /*
213          * Go to half test size.
214          */
215         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
216
217         /*
218          * If necessary, user can save content
219          * for later restore.
220          */
221         if (save_buf)
222         {
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);
226         }
227
228         /*
229          * Test read/write to address filesize/2 ... filesize/2 + size
230          */
231         if (!kfile_rwTest(fd, test_buf, size))
232                 goto kfile_test_end;
233
234         LOG_INFO("Test 2: ok!\n");
235
236         /*
237          * Restore previous content.
238          */
239         if (save_buf)
240         {
241                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
242
243                 if (kfile_write(fd, save_buf, size) != size)
244                         goto kfile_test_end;
245
246                 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
247         }
248
249         /* TEST 2 END. */
250
251         /* TEST 3 BEGIN. */
252         LOG_INFO("Test 3: write outside of fd->size limit [%ld]\n", fd->size);
253
254         /*
255          * Go to the Flash end
256          */
257         kfile_seek(fd, -len, KSM_SEEK_END);
258
259         /*
260          * If necessary, user can save content,
261          * for later restore.
262          */
263         if (save_buf)
264         {
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);
268         }
269
270         /*
271          * Test read/write to address (filesize - size) ... filesize
272          */
273         if (kfile_rwTest(fd, test_buf, size))
274                 goto kfile_test_end;
275
276         kprintf("Test 3: ok!\n");
277
278         /*
279          * Restore previous read content
280          */
281         if (save_buf)
282         {
283                 kfile_seek(fd, -len, KSM_SEEK_END);
284
285                 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
286                         goto kfile_test_end;
287
288                 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
289         }
290
291         /* TEST 3 END. */
292
293         kfile_close(fd);
294         return 0;
295
296 kfile_test_end:
297         kfile_close(fd);
298         LOG_ERR("One kfile_test failed!\n");
299         return EOF;
300 }
301
302
303
304
305 /**
306  * Setup all needed for kfile test
307  */
308 int kfile_testSetup(void)
309 {
310         MOD_INIT(kfile_test);
311         LOG_INFO("Mod init..ok\n");
312
313                 // Init our backend and the buffers
314                 kfilemem_init(&mem, test_disk, BUF_TEST_LEN);
315                 init_testBuf();
316
317         return 0;
318 }
319
320 int kfile_testRun(void)
321 {
322         return kfile_testRunGeneric(&mem.fd, test_buf, test_buf_save, BUF_TEST_LEN);
323 }
324
325 /**
326  * End a dataflash Test.
327  * (Unused)
328  */
329 int kfile_testTearDown(void)
330 {
331         return 0;
332 }
333
334 TEST_MAIN(kfile);
335