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