b01ba4469c3dfdd5f43359d4f707b602005abe01
[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  * This module implements a test for some generic I/O interfaces for kfile.
35  *
36  * \version $Id$
37  * \author Francesco Sacchi <batt@develer.com>
38  * \author Daniele Basile <asterix@develer.com>
39  *
40  */
41
42
43 #include "kfile.h"
44
45 #include <cfg/cfg_kfile.h>
46 #include <cfg/debug.h>
47
48 #include <mware/formatwr.h>
49
50 #include <string.h>
51
52 #warning TODO:Refactor this module..
53
54 /**
55  * KFile read/write subtest.
56  * Try to write/read in the same \a f file location \a size bytes.
57  * \return true if all is ok, false otherwise
58  * \note Restore file position at exit (if no error)
59  * \note Test buffer \a buf must be filled with
60  * the following statement:
61  * <pre>
62  * buf[i] = i & 0xff
63  * </pre>
64  */
65 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
66 {
67         /*
68          * Write test buffer
69          */
70         if (kfile_write(f, buf, size) != size)
71                 return false;
72
73         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
74
75         /*
76          * Reset test buffer
77          */
78         memset(buf, 0, size);
79
80         /*
81          * Read file in test buffer
82          */
83         if (kfile_read(f, buf, size) != size)
84                 return false;
85         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
86
87         /*
88          * Check test result
89          */
90         for (size_t i = 0; i < size; i++)
91                 if (buf[i] != (i & 0xff))
92                         return false;
93
94         return true;
95 }
96
97 /**
98  * KFile read/write test.
99  * This function write and read \a test_buf long \a size
100  * on \a fd handler.
101  * \a save_buf can be NULL or a buffer where to save previous file content.
102  */
103 bool kfile_test(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
104 {
105         /*
106          * Part of test buf size that you would write.
107          * This var is used in test 3 to check kfile_write
108          * when writing beyond filesize limit.
109          */
110         kfile_off_t len = size / 2;
111
112
113         /* Fill test buffer */
114         for (size_t i = 0; i < size; i++)
115                 test_buf[i] = (i & 0xff);
116
117         /*
118          * If necessary, user can save content,
119          * for later restore.
120          */
121         if (save_buf)
122         {
123                 kfile_read(fd, save_buf, size);
124                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
125         }
126
127         /* TEST 1 BEGIN. */
128         kprintf("Test 1: write from pos 0 to [%lu]\n", size);
129
130         /*
131          * Seek to addr 0.
132          */
133         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
134                 goto kfile_test_end;
135
136         /*
137          * Test read/write to address 0..size
138          */
139         if (!kfile_rwTest(fd, test_buf, size))
140                 goto kfile_test_end;
141
142         kprintf("Test 1: ok!\n");
143
144         /*
145          * Restore previous read content.
146          */
147         if (save_buf)
148         {
149                 kfile_seek(fd, 0, KSM_SEEK_SET);
150
151                 if (kfile_write(fd, save_buf, size) != size)
152                         goto kfile_test_end;
153
154                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
155         }
156         /* TEST 1 END. */
157
158         /* TEST 2 BEGIN. */
159         kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size);
160
161         /*
162          * Go to half test size.
163          */
164         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
165
166         /*
167          * If necessary, user can save content
168          * for later restore.
169          */
170         if (save_buf)
171         {
172                 kfile_read(fd, save_buf, size);
173                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
174                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
175         }
176
177         /*
178          * Test read/write to address filesize/2 ... filesize/2 + size
179          */
180         if (!kfile_rwTest(fd, test_buf, size))
181                 goto kfile_test_end;
182
183         kprintf("Test 2: ok!\n");
184
185         /*
186          * Restore previous content.
187          */
188         if (save_buf)
189         {
190                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
191
192                 if (kfile_write(fd, save_buf, size) != size)
193                         goto kfile_test_end;
194
195                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
196         }
197
198         /* TEST 2 END. */
199
200         /* TEST 3 BEGIN. */
201         kprintf("Test 3: write outside of fd->size limit [%lu]\n", fd->size);
202         kprintf("This test should FAIL!, you must see an assertion fail message.\n");
203
204         /*
205          * Go to the Flash end
206          */
207         kfile_seek(fd, -len, KSM_SEEK_END);
208
209         /*
210          * If necessary, user can save content,
211          * for later restore.
212          */
213         if (save_buf)
214         {
215                 kfile_read(fd, save_buf, len);
216                 kfile_seek(fd, -len, KSM_SEEK_CUR);
217                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
218         }
219
220         /*
221          * Test read/write to address (filesize - size) ... filesize
222          */
223         if (kfile_rwTest(fd, test_buf, size))
224                 goto kfile_test_end;
225
226         kprintf("Test 3: ok!\n");
227
228         /*
229          * Restore previous read content
230          */
231         if (save_buf)
232         {
233                 kfile_seek(fd, -len, KSM_SEEK_END);
234
235                 if (kfile_write(fd, save_buf, len) != len)
236                         goto kfile_test_end;
237
238                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
239         }
240
241         /* TEST 3 END. */
242
243         kfile_close(fd);
244         return true;
245
246 kfile_test_end:
247         kfile_close(fd);
248         return false;
249 }
250
251
252