Camelcase the testSetUp functions.
[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         kfile_testSetUp();
127
128         /*
129          * Part of test buf size that you would write.
130          * This var is used in test 3 to check kfile_write
131          * when writing beyond filesize limit.
132          */
133         kfile_off_t len = size / 2;
134
135
136         /* Fill test buffer */
137         for (size_t i = 0; i < size; i++)
138                 test_buf[i] = (i & 0xff);
139
140         /*
141          * If necessary, user can save content,
142          * for later restore.
143          */
144         if (save_buf)
145         {
146                 kfile_read(fd, save_buf, size);
147                 LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
148         }
149
150         /* TEST 1 BEGIN. */
151         LOG_INFO("Test 1: write from pos 0 to [%lu]\n", size);
152
153         /*
154          * Seek to addr 0.
155          */
156         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
157                 goto kfile_test_end;
158
159         /*
160          * Test read/write to address 0..size
161          */
162         if (!kfile_rwTest(fd, test_buf, size))
163                 goto kfile_test_end;
164
165         LOG_INFO("Test 1: ok!\n");
166
167         /*
168          * Restore previous read content.
169          */
170         if (save_buf)
171         {
172                 kfile_seek(fd, 0, KSM_SEEK_SET);
173
174                 if (kfile_write(fd, save_buf, size) != size)
175                         goto kfile_test_end;
176
177                 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
178         }
179         /* TEST 1 END. */
180
181         /* TEST 2 BEGIN. */
182         LOG_INFO("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size);
183
184         /*
185          * Go to half test size.
186          */
187         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
188
189         /*
190          * If necessary, user can save content
191          * for later restore.
192          */
193         if (save_buf)
194         {
195                 kfile_read(fd, save_buf, size);
196                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
197                 LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
198         }
199
200         /*
201          * Test read/write to address filesize/2 ... filesize/2 + size
202          */
203         if (!kfile_rwTest(fd, test_buf, size))
204                 goto kfile_test_end;
205
206         LOG_INFO("Test 2: ok!\n");
207
208         /*
209          * Restore previous content.
210          */
211         if (save_buf)
212         {
213                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
214
215                 if (kfile_write(fd, save_buf, size) != size)
216                         goto kfile_test_end;
217
218                 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
219         }
220
221         /* TEST 2 END. */
222
223         /* TEST 3 BEGIN. */
224         LOG_INFO("Test 3: write outside of fd->size limit [%lu]\n", fd->size);
225         LOG_INFO("This test should FAIL!, you must see an assertion fail message.\n");
226
227         /*
228          * Go to the Flash end
229          */
230         kfile_seek(fd, -len, KSM_SEEK_END);
231
232         /*
233          * If necessary, user can save content,
234          * for later restore.
235          */
236         if (save_buf)
237         {
238                 kfile_read(fd, save_buf, len);
239                 kfile_seek(fd, -len, KSM_SEEK_CUR);
240                 LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
241         }
242
243         /*
244          * Test read/write to address (filesize - size) ... filesize
245          */
246         if (kfile_rwTest(fd, test_buf, size))
247                 goto kfile_test_end;
248
249         kprintf("Test 3: ok!\n");
250
251         /*
252          * Restore previous read content
253          */
254         if (save_buf)
255         {
256                 kfile_seek(fd, -len, KSM_SEEK_END);
257
258                 if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
259                         goto kfile_test_end;
260
261                 LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
262         }
263
264         /* TEST 3 END. */
265
266         kfile_close(fd);
267         return 0;
268
269 kfile_test_end:
270         kfile_close(fd);
271         LOG_ERR("One kfile_test fail!\n");
272         return EOF;
273 }
274
275 /**
276  * End a dataflash Test.
277  * (Unused)
278  */
279 int kfile_testTearDown(void)
280 {
281         /*    */
282         return 0;
283 }