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