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