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