Refactor kfile test adding the beckend to be compile and run on pc.
[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/test.h>
48 #include <cfg/module.h>
49
50 // Define logging setting (for cfg/log.h module).
51 #define LOG_LEVEL   KFILE_LOG_LEVEL
52 #define LOG_FORMAT  KFILE_LOG_FORMAT
53 #include <cfg/log.h>
54
55 #include <mware/formatwr.h>
56
57 #include <string.h>
58
59 MOD_DEFINE(kfile_test);
60
61 #define BUF_TEST_LEN     3209
62
63 typedef uint8_t fake_t;
64 fake_t test_buf[BUF_TEST_LEN];
65 fake_t test_buf_save[BUF_TEST_LEN];
66 fake_t test_disk[BUF_TEST_LEN];
67
68 KFile fd;
69
70 static int fake_close(KFile *fd)
71 {
72         (void)fd;
73         return 0;
74 }
75
76 static size_t fake_read(KFile *fd, void *buf, size_t size)
77 {
78         fake_t *dest = (fake_t *)buf;
79         size_t rd_len;
80
81         rd_len = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
82
83         memcpy(dest, test_disk, size);
84         fd->seek_pos += rd_len;
85
86         LOG_INFO("Read: real[%ld] expected[%ld]\n", rd_len, size);
87
88         return rd_len;
89 }
90
91 static size_t fake_write(KFile *fd, const void *buf, size_t size)
92 {
93         fake_t *src = (fake_t *)buf;
94         size_t wr_len;
95
96         wr_len = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
97
98         memcpy(test_disk, src, wr_len);
99         fd->seek_pos += wr_len;
100
101         LOG_INFO("Write: real[%ld] expected[%ld]\n", wr_len, size);
102
103         return wr_len;
104 }
105
106 int fake_flush(KFile *fd)
107 {
108         (void)fd;
109
110         return 0;
111 }
112
113 void fake_kfileInit(void)
114 {
115         // Setup data flash programming functions.
116         fd.reopen = kfile_genericReopen;
117         fd.close = fake_close;
118         fd.read = fake_read;
119         fd.write = fake_write;
120         fd.seek = kfile_genericSeek;
121         fd.flush = fake_flush;
122
123         fd.seek_pos = 0;
124         fd.size = BUF_TEST_LEN;
125
126 }
127
128 static void init_testBuf(void)
129 {
130         #include <stdlib.h>
131
132         kprintf("Init fake buffer..\n");
133         for (int i = 0; i < BUF_TEST_LEN; i++)
134         {
135                 test_disk[i] = random();
136                 kprintf("%d ", test_disk[i]);
137         }
138         kprintf("\nend\n");
139
140         memset(test_buf, 0, sizeof(test_buf));
141         memset(test_buf_save, 0, sizeof(test_buf_save));
142 }
143
144 /**
145  * KFile read/write subtest.
146  * Try to write/read in the same \a f file location \a size bytes.
147  * \return true if all is ok, false otherwise
148  * \note Restore file position at exit (if no error)
149  * \note Test buffer \a buf must be filled with
150  * the following statement:
151  * <pre>
152  * buf[i] = i & 0xff
153  * </pre>
154  */
155 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
156 {
157         /*
158          * Write test buffer
159          */
160         if (kfile_write(f, buf, size) != size)
161                 return false;
162
163         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
164
165         /*
166          * Reset test buffer
167          */
168         memset(buf, 0, size);
169
170         /*
171          * Read file in test buffer
172          */
173         if (kfile_read(f, buf, size) != size)
174                 return false;
175
176         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
177
178         /*
179          * Check test result
180          */
181         for (size_t i = 0; i < size; i++)
182                 if (buf[i] != (i & 0xff))
183                         return false;
184
185         return true;
186 }
187
188 /**
189  * KFile read/write test.
190  * This function write and read \a test_buf long \a size
191  * on \a fd handler.
192  * \a save_buf can be NULL or a buffer where to save previous file content.
193  */
194 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
195 {
196
197         /*
198          * Part of test buf size that you would write.
199          * This var is used in test 3 to check kfile_write
200          * when writing beyond filesize limit.
201          */
202         kfile_off_t len = size / 2;
203
204
205         /* Fill test buffer */
206         for (size_t i = 0; i < size; i++)
207                 test_buf[i] = (i & 0xff);
208
209         /*
210          * If necessary, user can save content,
211          * for later restore.
212          */
213         if (save_buf)
214         {
215                 LOG_INFO("Saved content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
216                 kfile_read(fd, save_buf, size);
217         }
218
219         /* TEST 1 BEGIN. */
220         LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
221
222         /*
223          * Seek to addr 0.
224          */
225         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
226                 goto kfile_test_end;
227
228         /*
229          * Test read/write to address 0..size
230          */
231         if (!kfile_rwTest(fd, test_buf, size))
232                 goto kfile_test_end;
233
234         LOG_INFO("Test 1: ok!\n");
235
236         /*
237          * Restore previous read content.
238          */
239         if (save_buf)
240         {
241                 kfile_seek(fd, 0, KSM_SEEK_SET);
242
243                 if (kfile_write(fd, save_buf, size) != size)
244                         goto kfile_test_end;
245
246                 LOG_INFO("Restore content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
247         }
248         /* TEST 1 END. */
249
250         /* TEST 2 BEGIN. */
251         LOG_INFO("Test 2: write from pos [%d] to [%ld]\n", fd->size/2 , fd->size/2 + size);
252
253         /*
254          * Go to half test size.
255          */
256         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
257
258         /*
259          * If necessary, user can save content
260          * for later restore.
261          */
262         if (save_buf)
263         {
264                 kfile_read(fd, save_buf, size);
265                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
266                 LOG_INFO("Saved content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
267         }
268
269         /*
270          * Test read/write to address filesize/2 ... filesize/2 + size
271          */
272         if (!kfile_rwTest(fd, test_buf, size))
273                 goto kfile_test_end;
274
275         LOG_INFO("Test 2: ok!\n");
276
277         /*
278          * Restore previous content.
279          */
280         if (save_buf)
281         {
282                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
283
284                 if (kfile_write(fd, save_buf, size) != size)
285                         goto kfile_test_end;
286
287                 LOG_INFO("Restore content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
288         }
289
290         /* TEST 2 END. */
291
292         /* TEST 3 BEGIN. */
293         LOG_INFO("Test 3: write outside of fd->size limit [%d]\n", fd->size);
294         LOG_INFO("This test should FAIL!, you must see an assertion fail message.\n");
295
296         /*
297          * Go to the Flash end
298          */
299         kfile_seek(fd, -len, KSM_SEEK_END);
300
301         /*
302          * If necessary, user can save content,
303          * for later restore.
304          */
305         if (save_buf)
306         {
307                 kfile_read(fd, save_buf, len);
308                 kfile_seek(fd, -len, KSM_SEEK_CUR);
309                 LOG_INFO("Saved content..form [%d] to [%d]\n", fd->seek_pos, fd->seek_pos + len);
310         }
311
312         /*
313          * Test read/write to address (filesize - size) ... filesize
314          */
315         if (kfile_rwTest(fd, test_buf, size))
316                 goto kfile_test_end;
317
318         kprintf("Test 3: ok!\n");
319
320         /*
321          * Restore previous read content
322          */
323         if (save_buf)
324         {
325                 kfile_seek(fd, -len, KSM_SEEK_END);
326
327                 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
328                         goto kfile_test_end;
329
330                 LOG_INFO("Restore content..form [%d] to [%d]\n", fd->seek_pos, fd->seek_pos + len);
331         }
332
333         /* TEST 3 END. */
334
335         kfile_close(fd);
336         return 0;
337
338 kfile_test_end:
339         kfile_close(fd);
340         LOG_ERR("One kfile_test failed!\n");
341         return EOF;
342 }
343
344
345
346
347 /**
348  * Setup all needed for kfile test
349  */
350 int kfile_testSetup(void)
351 {
352         MOD_INIT(kfile_test);
353         LOG_INFO("Mod init..ok\n");
354
355                 fake_kfileInit();
356                 init_testBuf();
357
358         return 0;
359 }
360
361 int kfile_testRun(void)
362 {
363         return kfile_testRunGeneric(&fd, test_buf, test_buf_save, BUF_TEST_LEN);
364 }
365
366 /**
367  * End a dataflash Test.
368  * (Unused)
369  */
370 int kfile_testTearDown(void)
371 {
372         return 0;
373 }
374
375 TEST_MAIN(kfile);