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