Refactor kfile_test to use the new kfile_mem module.
[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                 return false;
109
110         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
111
112         /*
113          * Reset test buffer
114          */
115         memset(buf, 0, size);
116
117         /*
118          * Read file in test buffer
119          */
120         if (kfile_read(f, buf, size) != size)
121                 return false;
122
123         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
124
125         /*
126          * Check test result
127          */
128         for (size_t i = 0; i < size; i++)
129                 if (buf[i] != (i & 0xff))
130                         return false;
131
132         return true;
133 }
134
135 /**
136  * KFile read/write test.
137  * This function write and read \a test_buf long \a size
138  * on \a fd handler.
139  * \a save_buf can be NULL or a buffer where to save previous file content.
140  */
141 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
142 {
143
144         /*
145          * Part of test buf size that you would write.
146          * This var is used in test 3 to check kfile_write
147          * when writing beyond filesize limit.
148          */
149         kfile_off_t len = size / 2;
150
151
152         /* Fill test buffer */
153         for (size_t i = 0; i < size; i++)
154                 test_buf[i] = (i & 0xff);
155
156         /*
157          * If necessary, user can save content,
158          * for later restore.
159          */
160         if (save_buf)
161         {
162                 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
163                 kfile_read(fd, save_buf, size);
164         }
165
166         /* TEST 1 BEGIN. */
167         LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
168
169         /*
170          * Seek to addr 0.
171          */
172         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
173                 goto kfile_test_end;
174
175         /*
176          * Test read/write to address 0..size
177          */
178         if (!kfile_rwTest(fd, test_buf, size))
179                 goto kfile_test_end;
180
181         LOG_INFO("Test 1: ok!\n");
182
183         /*
184          * Restore previous read content.
185          */
186         if (save_buf)
187         {
188                 kfile_seek(fd, 0, KSM_SEEK_SET);
189
190                 if (kfile_write(fd, save_buf, size) != size)
191                         goto kfile_test_end;
192
193                 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
194         }
195         /* TEST 1 END. */
196
197         /* TEST 2 BEGIN. */
198         LOG_INFO("Test 2: write from pos [%ld] to [%ld]\n", fd->size/2 , fd->size/2 + size);
199
200         /*
201          * Go to half test size.
202          */
203         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
204
205         /*
206          * If necessary, user can save content
207          * for later restore.
208          */
209         if (save_buf)
210         {
211                 kfile_read(fd, save_buf, size);
212                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
213                 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
214         }
215
216         /*
217          * Test read/write to address filesize/2 ... filesize/2 + size
218          */
219         if (!kfile_rwTest(fd, test_buf, size))
220                 goto kfile_test_end;
221
222         LOG_INFO("Test 2: ok!\n");
223
224         /*
225          * Restore previous content.
226          */
227         if (save_buf)
228         {
229                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
230
231                 if (kfile_write(fd, save_buf, size) != size)
232                         goto kfile_test_end;
233
234                 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
235         }
236
237         /* TEST 2 END. */
238
239         /* TEST 3 BEGIN. */
240         LOG_INFO("Test 3: write outside of fd->size limit [%ld]\n", fd->size);
241
242         /*
243          * Go to the Flash end
244          */
245         kfile_seek(fd, -len, KSM_SEEK_END);
246
247         /*
248          * If necessary, user can save content,
249          * for later restore.
250          */
251         if (save_buf)
252         {
253                 kfile_read(fd, save_buf, len);
254                 kfile_seek(fd, -len, KSM_SEEK_CUR);
255                 LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
256         }
257
258         /*
259          * Test read/write to address (filesize - size) ... filesize
260          */
261         if (kfile_rwTest(fd, test_buf, size))
262                 goto kfile_test_end;
263
264         kprintf("Test 3: ok!\n");
265
266         /*
267          * Restore previous read content
268          */
269         if (save_buf)
270         {
271                 kfile_seek(fd, -len, KSM_SEEK_END);
272
273                 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
274                         goto kfile_test_end;
275
276                 LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
277         }
278
279         /* TEST 3 END. */
280
281         kfile_close(fd);
282         return 0;
283
284 kfile_test_end:
285         kfile_close(fd);
286         LOG_ERR("One kfile_test failed!\n");
287         return EOF;
288 }
289
290
291
292
293 /**
294  * Setup all needed for kfile test
295  */
296 int kfile_testSetup(void)
297 {
298         MOD_INIT(kfile_test);
299         LOG_INFO("Mod init..ok\n");
300
301                 // Init our backend and the buffers
302                 kfilemem_init(&mem, test_disk, BUF_TEST_LEN);
303                 init_testBuf();
304
305         return 0;
306 }
307
308 int kfile_testRun(void)
309 {
310         return kfile_testRunGeneric(&mem.fd, test_buf, test_buf_save, BUF_TEST_LEN);
311 }
312
313 /**
314  * End a dataflash Test.
315  * (Unused)
316  */
317 int kfile_testTearDown(void)
318 {
319         return 0;
320 }
321
322 TEST_MAIN(kfile);
323