1bcb037eaf0254c170f1b523f84648590ff22047
[bertos.git] / bertos / fs / battfs_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, 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief BattFS Test.
33  *
34  * \version $Id$
35  * \author Francesco Sacchi <batt@develer.com>
36  */
37
38 #include <fs/battfs.h>
39
40 #include <cfg/debug.h>
41 #include <cfg/test.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #define FILE_SIZE 32768
48 #define PAGE_SIZE 128
49 #define PAGE_COUNT FILE_SIZE / PAGE_SIZE
50
51 #if UNIT_TEST
52
53 FILE *fp;
54 const char test_filename[]="battfs_disk.bin";
55
56 static uint8_t page_buffer[PAGE_SIZE];
57
58 static bool disk_open(struct BattFsSuper *d)
59 {
60         fp = fopen(test_filename, "r+b");
61         ASSERT(fp);
62         fseek(fp, 0, SEEK_END);
63         d->page_size = PAGE_SIZE;
64         d->page_count = ftell(fp) / d->page_size;
65         d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
66         //TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
67         return (fp && d->page_array);
68 }
69
70 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
71 {
72         TRACEMSG("page:%d, addr:%d, size:%d", page, addr, size);
73         fseek(fp, page * d->page_size + addr, SEEK_SET);
74         return fread(buf, 1, size, fp);
75 }
76
77 static size_t disk_buffer_write(struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t size)
78 {
79         TRACEMSG("addr:%d, size:%d", addr, size);
80         ASSERT(addr + size <= d->page_size);
81         memcpy(&page_buffer[addr], buf, size);
82
83         return size;
84 }
85
86 static bool disk_page_load(struct BattFsSuper *d, pgcnt_t page)
87 {
88         TRACEMSG("page:%d", page);
89         fseek(fp, page * d->page_size, SEEK_SET);
90         return fread(page_buffer, 1, d->page_size, fp) == d->page_size;
91 }
92
93 static bool disk_page_save(struct BattFsSuper *d, pgcnt_t page)
94 {
95         TRACEMSG("page:%d", page);
96         fseek(fp, page * d->page_size, SEEK_SET);
97         return fwrite(page_buffer, 1, d->page_size, fp) == d->page_size;
98 }
99
100 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
101 {
102         //TRACEMSG("page:%d\n", page);
103         fseek(fp, page * d->page_size, SEEK_SET);
104
105         for (int i = 0; i < d->page_size; i++)
106                 if (fputc(0xff, fp) == EOF)
107                         return false;
108         return true;
109 }
110
111 static bool disk_close(struct BattFsSuper *d)
112 {
113         //TRACE;
114         free(d->page_array);
115         return (fclose(fp) != EOF);
116 }
117
118 static void testCheck(BattFsSuper *disk, pgcnt_t *reference)
119 {
120         ASSERT(battfs_init(disk));
121
122         for (int i = 0; i < disk->page_count; i++)
123         {
124                 if (disk->page_array[i] != reference[i])
125                 {
126                         kprintf("Error at addr %d: page_array read", i);
127                         for (pgcnt_t i = 0; i < disk->page_count; i++)
128                         {
129                                 if (!(i % 16))
130                                         kputchar('\n');
131                                 kprintf("%04d ", disk->page_array[i]);
132                         }
133                         kputchar('\n');
134                         kprintf("Expected:");
135                         for (pgcnt_t i = 0; i < disk->page_count; i++)
136                         {
137                                 if (!(i % 16))
138                                         kputchar('\n');
139                                 kprintf("%04d ", reference[i]);
140                         }
141                         kputchar('\n');
142                         battfs_close(disk);
143                         exit(2);
144                 }
145         }
146         battfs_close(disk);
147 }
148
149 static void test1(BattFsSuper *disk)
150 {
151         pgcnt_t ref[PAGE_COUNT];
152         kprintf("Test1: disk new\n");
153
154         FILE *fpt = fopen(test_filename, "w+");
155
156         for (int i = 0; i < FILE_SIZE; i++)
157                 fputc(0xff, fpt);
158         fclose(fpt);
159         for (int i = 0; i < PAGE_COUNT; i++)
160                 ref[i] = i;
161
162         testCheck(disk, ref);
163         kprintf("Test1: passed\n");
164 }
165
166 static void test2(BattFsSuper *disk)
167 {
168         pgcnt_t ref[PAGE_COUNT];
169         kprintf("Test2: disk full with 1 contiguos file\n");
170
171
172         fp = fopen(test_filename, "w+");
173
174         for (int i = 0; i < PAGE_COUNT; i++)
175         {
176                 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
177                 ref[i] = i;
178         }
179         fclose(fp);
180
181         testCheck(disk, ref);
182         kprintf("Test2: passed\n");
183 }
184
185
186 static void test3(BattFsSuper *disk)
187 {
188         pgcnt_t ref[PAGE_COUNT];
189         kprintf("Test3: disk half full with 1 contiguos file, rest unformatted\n");
190
191
192         fp = fopen(test_filename, "w+");
193
194         for (int i = 0; i < PAGE_COUNT / 2; i++)
195         {
196                 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
197                 ref[i] = i;
198         }
199         fseek(fp, FILE_SIZE / 2, SEEK_SET);
200         for (int i = FILE_SIZE / 2; i < FILE_SIZE; i++)
201                 fputc(0xff, fp);
202         fclose(fp);
203
204         for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
205         {
206                 ref[i] = i;
207         }
208
209
210         testCheck(disk, ref);
211         kprintf("Test3: passed\n");
212 }
213
214 #if 0
215 static void test4(BattFsSuper *disk)
216 {
217         pgcnt_t ref[PAGE_COUNT];
218         kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
219
220
221         fp = fopen(test_filename, "w+");
222
223         for (int i = 0; i < PAGE_COUNT / 2; i++)
224         {
225                 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
226                 ref[i] = i;
227         }
228         for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
229         {
230                 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
231                 ref[i] = i;
232         }
233         fclose(fp);
234
235
236         testCheck(disk, ref);
237         kprintf("Test4: passed\n");
238 }
239
240 static void test5(BattFsSuper *disk)
241 {
242         pgcnt_t ref[PAGE_COUNT];
243         kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
244
245
246         fp = fopen(test_filename, "w+");
247
248         for (int i = 0; i < FILE_SIZE; i++)
249                 fputc(0xff, fp);
250
251         for (int i = 0; i < PAGE_COUNT / 3; i++)
252         {
253                 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
254                 ref[i] = i;
255         }
256         for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
257         {
258                 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
259                 ref[i + PAGE_COUNT / 3 + 1] = i;
260         }
261         fclose(fp);
262
263         for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
264                 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
265
266         testCheck(disk, ref);
267         kprintf("Test5: passed\n");
268 }
269 #endif
270
271 static void test6(BattFsSuper *disk)
272 {
273         pgcnt_t ref[4];
274         kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
275
276
277         fp = fopen(test_filename, "w+");
278         // page, inode, seq, fill, pgoff
279         battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
280         battfs_writeTestBlock(disk, 1, 0, 0, 0, 1);
281         battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
282         disk->erase(disk, 3);
283
284
285         fclose(fp);
286         ref[0] = 0;
287         ref[1] = 2;
288         ref[2] = 1;
289         ref[3] = 3;
290
291         testCheck(disk, ref);
292         kprintf("Test6: passed\n");
293 }
294
295 static void test7(BattFsSuper *disk)
296 {
297         pgcnt_t ref[4];
298         kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
299
300
301         fp = fopen(test_filename, "w+");
302         // page, inode, seq, fill, pgoff
303         battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
304         battfs_writeTestBlock(disk, 1, 0, 1, 0, 1);
305         battfs_writeTestBlock(disk, 2, 0, 0, 0, 1);
306         disk->erase(disk, 3);
307
308         fclose(fp);
309         ref[0] = 0;
310         ref[1] = 1;
311         ref[2] = 2;
312         ref[3] = 3;
313
314         testCheck(disk, ref);
315         kprintf("Test7: passed\n");
316 }
317
318 static void test8(BattFsSuper *disk)
319 {
320         pgcnt_t ref[4];
321         kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
322
323
324         fp = fopen(test_filename, "w+");
325
326         // page, inode, seq, fill, pgoff
327         disk->erase(disk, 0);
328         battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
329         battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
330         battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
331
332
333         fclose(fp);
334         ref[0] = 1;
335         ref[1] = 2;
336         ref[2] = 0;
337         ref[3] = 3;
338
339         testCheck(disk, ref);
340         kprintf("Test8: passed\n");
341 }
342
343 static void test9(BattFsSuper *disk)
344 {
345         pgcnt_t ref[8];
346         kprintf("Test9: 2 file with old seq num, 2 free block\n");
347
348
349         fp = fopen(test_filename, "w+");
350
351         // page, inode, seq, fill, pgoff
352         disk->erase(disk, 0);
353         battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
354         battfs_writeTestBlock(disk, 2, 0, 3, 0, 1);
355         battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
356         disk->erase(disk, 4);
357         battfs_writeTestBlock(disk, 5, 4, 0, 0, 0);
358         battfs_writeTestBlock(disk, 6, 4, 1, 0, 1);
359         battfs_writeTestBlock(disk, 7, 4, 0, 0, 1);
360
361
362         fclose(fp);
363         ref[0] = 1;
364         ref[1] = 2;
365         ref[2] = 5;
366         ref[3] = 6;
367         ref[4] = 0;
368         ref[5] = 3;
369         ref[6] = 4;
370         ref[7] = 7;
371
372         testCheck(disk, ref);
373         kprintf("Test9: passed\n");
374 }
375
376 static void test10(BattFsSuper *disk)
377 {
378         BattFs fd1;
379         BattFs fd2;
380         kprintf("Test10: open file test, inode 0 and inode 4\n");
381
382         fp = fopen(test_filename, "w+");
383
384         int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
385         unsigned int INODE = 0;
386         unsigned int INODE2 = 4;
387         unsigned int INEXISTENT_INODE = 123;
388         unsigned int MODE = 0;
389
390         // page, inode, seq, fill, pgoff
391         disk->erase(disk, 0);
392         battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
393         battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
394         battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
395         disk->erase(disk, 4);
396         battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
397         battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
398         battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
399
400         fclose(fp);
401
402         ASSERT(battfs_init(disk));
403         ASSERT(!battfs_fileExists(disk, INEXISTENT_INODE));
404
405         ASSERT(battfs_fileExists(disk, INODE));
406         ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
407         ASSERT(fd1.fd.size == PAGE_FILL * 2);
408         ASSERT(fd1.fd.seek_pos == 0);
409         ASSERT(fd1.mode == MODE);
410         ASSERT(fd1.inode == INODE);
411         ASSERT(fd1.start == &disk->page_array[0]);
412         ASSERT(fd1.disk == disk);
413         ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
414
415         ASSERT(kfile_reopen(&fd1.fd) == &fd1.fd);
416         ASSERT(fd1.fd.size == PAGE_FILL * 2);
417         ASSERT(fd1.fd.seek_pos == 0);
418         ASSERT(fd1.mode == MODE);
419         ASSERT(fd1.inode == INODE);
420         ASSERT(fd1.start == &disk->page_array[0]);
421         ASSERT(fd1.disk == disk);
422         ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
423
424         ASSERT(battfs_fileExists(disk, INODE2));
425         ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
426         ASSERT(fd2.fd.size == PAGE_FILL * 2);
427         ASSERT(fd2.fd.seek_pos == 0);
428         ASSERT(fd2.mode == MODE);
429         ASSERT(fd2.inode == INODE2);
430         ASSERT(fd2.start == &disk->page_array[2]);
431         ASSERT(fd2.disk == disk);
432         ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
433
434         ASSERT(kfile_close(&fd1.fd) == 0);
435         ASSERT(kfile_close(&fd2.fd) == 0);
436         ASSERT(LIST_EMPTY(&disk->file_opened_list));
437         ASSERT(battfs_close(disk));
438
439         kprintf("Test10: passed\n");
440 }
441
442 static void test11(BattFsSuper *disk)
443 {
444         BattFs fd1;
445         uint8_t buf[16];
446
447         kprintf("Test11: read file test\n");
448
449         fp = fopen(test_filename, "w+");
450
451         unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
452         unsigned int INODE = 0;
453         unsigned int INODE2 = 4;
454         unsigned int MODE = 0;
455
456         disk->erase(disk, 0);
457         battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
458         battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
459         battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
460         disk->erase(disk, 4);
461         battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
462         battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
463         battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
464
465         fclose(fp);
466
467         ASSERT(battfs_init(disk));
468         ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
469         ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
470         ASSERT(fd1.fd.seek_pos == sizeof(buf));
471         for (size_t i = 0; i < sizeof(buf); i++)
472                 ASSERT(buf[i] == 0xff);
473
474         ASSERT(kfile_close(&fd1.fd) == 0);
475         ASSERT(battfs_close(disk));
476
477         kprintf("Test11: passed\n");
478 }
479
480 static void test12(BattFsSuper *disk)
481 {
482         BattFs fd1;
483
484         kprintf("Test12: read file test across page boundary and seek test\n");
485
486         fp = fopen(test_filename, "w+");
487
488         const unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
489         unsigned int INODE = 0;
490         unsigned int MODE = 0;
491         uint8_t buf[PAGE_FILL + BATTFS_HEADER_LEN / 2];
492
493         disk->erase(disk, 0);
494         battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
495         battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
496         battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
497         disk->erase(disk, 4);
498         battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2);
499         battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3);
500         battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3);
501
502         fclose(fp);
503
504         ASSERT(battfs_init(disk));
505         ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
506
507         ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
508         ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf));
509         for (size_t i = 0; i < sizeof(buf); i++)
510                 ASSERT(buf[i] == 0xff);
511
512         ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
513         ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 2);
514         for (size_t i = 0; i < sizeof(buf); i++)
515                 ASSERT(buf[i] == 0xff);
516
517         ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
518         ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 3);
519         for (size_t i = 0; i < sizeof(buf); i++)
520                 ASSERT(buf[i] == 0xff);
521
522         ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == PAGE_FILL * 4 - sizeof(buf) * 3);
523         ASSERT(fd1.fd.seek_pos == (kfile_off_t)fd1.fd.size);
524         for (size_t i = 0; i < PAGE_FILL * 4 - sizeof(buf) * 3; i++)
525                 ASSERT(buf[i] == 0xff);
526
527         ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_SET) == 0);
528         ASSERT(fd1.fd.seek_pos == 0);
529
530         ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_END) == (kfile_off_t)fd1.fd.size);
531         ASSERT(fd1.fd.seek_pos = (kfile_off_t)fd1.fd.size);
532
533         ASSERT(kfile_close(&fd1.fd) == 0);
534         ASSERT(battfs_close(disk));
535
536         kprintf("Test12: passed\n");
537 }
538
539
540 int battfs_testRun(void)
541 {
542         BattFsSuper disk;
543
544         disk.open = disk_open;
545         disk.read = disk_page_read;
546         disk.load = disk_page_load;
547         disk.bufferWrite = disk_buffer_write;
548         disk.save = disk_page_save;
549         disk.erase = disk_page_erase;
550         disk.close = disk_close;
551         test1(&disk);
552         test2(&disk);
553         test3(&disk);
554         //test4(&disk);
555         //test5(&disk);
556         test6(&disk);
557         test7(&disk);
558         test8(&disk);
559         test9(&disk);
560         test10(&disk);
561         test11(&disk);
562         test12(&disk);
563         kprintf("All tests passed!\n");
564
565         return 0;
566 }
567
568 int battfs_testSetup(void)
569 {
570         return 0;
571 }
572
573 int battfs_testTearDown(void)
574 {
575         return 0;
576 }
577
578 TEST_MAIN(battfs)
579
580 #include <fs/battfs.c>
581 #include <kern/kfile.c>
582 #include <drv/kdebug.c>
583 #include <mware/formatwr.c>
584 #include <mware/hex.c>
585
586 #endif // _TEST