Add generic test for kfile interface.
[bertos.git] / kern / kfile.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 Virtual KFile I/O interface.
34  * This module implement a standard fd.seek and a kfile
35  * test function.
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
46 #if CONFIG_TEST
47
48 /**
49  * Program memory read/write subtest.
50  * Try to write/read in the same \a f file location \a _size bytes.
51  * \return true if all is ok, false otherwise
52  * \note Restore file position at exit (if no error)
53  * \note Test buffer \a buf must be filled with
54  * the following statement:
55  * <pre>
56  * buf[i] = i & 0xff
57  * </pre>
58  */
59 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
60 {
61         int32_t size = _size;
62
63         // Write test buffer
64         if (f->write(f, buf, size) != size)
65                 return false;
66         f->seek(f, -size, KSM_SEEK_CUR);
67
68         // Reset test buffer
69         memset(buf, 0, size);
70
71         // Read flash in test buffer
72         if (f->read(f, buf, size) != size)
73                 return false;
74         f->seek(f, -size, KSM_SEEK_CUR);
75
76         // Check test result
77         for (size_t i = 0; i < size; i++)
78                 if (buf[i] != (i & 0xff))
79                         return false;
80
81         return true;
82 }
83
84 /**
85  * Test for program memory read/write.
86  */
87 bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_buf_size)
88 {
89         KFile fd;
90         int32_t size = _size;
91
92         /*
93          * Part of test buf size that you would write.
94          * This var is useded in test 3 to check fd.write
95          * when write outside size limit. Normaly we want
96          * perform a write until is space to write, otherwise
97          * we return.
98          */
99         int32_t len = size/2;
100
101         /*
102          * Fill in test buffer
103          */
104         for (int i = 0; i < size; i++)
105                 test_buf[i] = (i & 0xff);
106
107         kprintf("Generated test string..\n");
108
109         /*
110          * Open fd handler
111          */
112         fd.open(&fd, NULL, 0);
113         kprintf("Opened fd handler..\n");
114
115         /*
116          * If necessary, user could save content,
117          * for later restore.
118          */
119         if (save_buf != NULL)
120         {
121                 fd.read(&fd, save_buf, save_buf_size);
122                 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
123         }
124
125         /* TEST 1 BEGIN. */
126         kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size);
127
128         /*
129          * Seek to addr 0
130          */
131         if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0)
132                 goto kfile_test_end;
133
134         kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
135
136         /*
137          * Test flash 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 != NULL)
148         {
149                 fd.seek(&fd, 0, KSM_SEEK_SET);
150
151                 if (fd.write(&fd, save_buf, 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 + save_buf_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 , size);
160
161         /*
162          * Go to half test size.
163          */
164         fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
165
166         kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
167
168         /*
169          * If necessary, user could save content,
170          * for later restore.
171          */
172         if (save_buf != NULL)
173         {
174                 fd.read(&fd, save_buf, save_buf_size);
175                 fd.seek(&fd, -size, KSM_SEEK_CUR);
176                 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
177         }
178
179         /*
180          * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
181          */
182         if (!Kfile_rwTest(&fd, test_buf, size))
183                 goto kfile_test_end;
184
185         kprintf("Test 2: ok!\n");
186
187         /*
188          * Restore previous read content
189          */
190         if (save_buf != NULL)
191         {
192                 fd.seek(&fd, -size, KSM_SEEK_CUR);
193
194                 if (fd.write(&fd, save_buf, save_buf_size) != size)
195                         goto kfile_test_end;
196
197                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
198         }
199
200         /* TEST 2 END. */
201
202         /* TEST 3 BEGIN. */
203         kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
204
205         /*
206          * Go to the Flash end
207          */
208         fd.seek(&fd, -len, KSM_SEEK_END);
209         kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
210
211         /*
212          * If necessary, user could save content,
213          * for later restore.
214          */
215         if (save_buf != NULL)
216         {
217                 ASSERT(len > save_buf_size);
218
219                 fd.read(&fd, save_buf, len);
220                 fd.seek(&fd, -len, KSM_SEEK_CUR);
221                 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
222         }
223
224         /*
225          * Test flash read/write to address (FLASHEND - size) ... FLASHEND
226          */
227         if (!Kfile_rwTest(&fd, test_buf, size))
228                 goto kfile_test_end;
229
230         kprintf("Test 3: ok !\n");
231
232         /*
233          * Restore previous read content
234          */
235         if (save_buf != NULL)
236         {
237                 fd.seek(&fd, -len, KSM_SEEK_END);
238
239                 if (fd.write(&fd, save_buf, len) != len)
240                         goto kfile_test_end;
241
242                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
243         }
244
245         /* TEST 3 END. */
246
247         fd.close(&fd);
248         return true;
249
250 kfile_test_end:
251         fd.close(&fd);
252         return false;
253 }
254
255 #endif /* CONFIG_TEST */