4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2007, 2008 Develer S.r.l. (http://www.develer.com/)
34 * \version $Id: demo.c 18242 2007-10-08 17:35:23Z marco $
35 * \author Francesco Sacchi <batt@develer.com>
38 #include <fs/battfs.h>
40 #include <cfg/debug.h>
47 #define FILE_SIZE 32768
49 #define PAGE_COUNT FILE_SIZE / PAGE_SIZE
52 const char test_filename[]="battfs_disk.bin";
55 static bool disk_open(struct BattFsSuper *d)
57 fp = fopen(test_filename, "r+b");
59 fseek(fp, 0, SEEK_END);
60 d->page_size = PAGE_SIZE;
61 d->page_count = ftell(fp) / d->page_size;
62 d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
63 //TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
64 return (fp && d->page_array);
67 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
69 //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
70 fseek(fp, page * d->page_size + addr, SEEK_SET);
71 return fread(buf, 1, size, fp);
74 static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size)
76 //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
77 fseek(fp, page * d->page_size + addr, SEEK_SET);
78 return fwrite(buf, 1, size, fp);
81 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
83 //TRACEMSG("page:%d\n", page);
84 fseek(fp, page * d->page_size, SEEK_SET);
86 for (int i = 0; i < d->page_size; i++)
87 if (fputc(0xff, fp) == EOF)
92 static bool disk_close(struct BattFsSuper *d)
96 return (fclose(fp) != EOF);
99 static void testCheck(BattFsSuper *disk, pgcnt_t *reference)
101 ASSERT(battfs_init(disk));
103 for (int i = 0; i < disk->page_count; i++)
105 if (disk->page_array[i] != reference[i])
107 kprintf("Error at addr %d: page_array read", i);
108 for (pgcnt_t i = 0; i < disk->page_count; i++)
112 kprintf("%04d ", disk->page_array[i]);
115 kprintf("Expected:");
116 for (pgcnt_t i = 0; i < disk->page_count; i++)
120 kprintf("%04d ", reference[i]);
130 static void test1(BattFsSuper *disk)
132 pgcnt_t ref[PAGE_COUNT];
133 kprintf("Test1: disk new\n");
135 FILE *fpt = fopen(test_filename, "w+");
137 for (int i = 0; i < FILE_SIZE; i++)
140 for (int i = 0; i < PAGE_COUNT; i++)
141 ref[i] = PAGE_COUNT - i - 1;
143 testCheck(disk, ref);
144 kprintf("Test1: passed\n");
147 static void test2(BattFsSuper *disk)
149 pgcnt_t ref[PAGE_COUNT];
150 kprintf("Test2: disk full with 1 contiguos file\n");
153 fp = fopen(test_filename, "w+");
155 for (int i = 0; i < PAGE_COUNT; i++)
157 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
162 testCheck(disk, ref);
163 kprintf("Test2: passed\n");
167 static void test3(BattFsSuper *disk)
169 pgcnt_t ref[PAGE_COUNT];
170 kprintf("Test3: disk half full with 1 contiguos file, rest unformatted\n");
173 fp = fopen(test_filename, "w+");
175 for (int i = 0; i < PAGE_COUNT / 2; i++)
177 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
180 fseek(fp, FILE_SIZE / 2, SEEK_SET);
181 for (int i = FILE_SIZE / 2; i < FILE_SIZE; i++)
185 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
187 ref[i] = PAGE_COUNT + PAGE_COUNT / 2 - i - 1;
191 testCheck(disk, ref);
192 kprintf("Test3: passed\n");
195 static void test4(BattFsSuper *disk)
197 pgcnt_t ref[PAGE_COUNT];
198 kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
201 fp = fopen(test_filename, "w+");
203 for (int i = 0; i < PAGE_COUNT / 2; i++)
205 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
208 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
210 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
216 testCheck(disk, ref);
217 kprintf("Test4: passed\n");
220 static void test5(BattFsSuper *disk)
222 pgcnt_t ref[PAGE_COUNT];
223 kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
226 fp = fopen(test_filename, "w+");
228 for (int i = 0; i < FILE_SIZE; i++)
231 for (int i = 0; i < PAGE_COUNT / 3; i++)
233 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
236 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
238 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
239 ref[i + PAGE_COUNT / 3 + 1] = i;
243 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
244 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
246 testCheck(disk, ref);
247 kprintf("Test5: passed\n");
250 static void test6(BattFsSuper *disk)
253 kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
256 fp = fopen(test_filename, "w+");
258 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
259 battfs_writeTestBlock(disk, 1, 0, 0, 0, 1, MARK_PAGE_VALID);
260 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
261 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
269 testCheck(disk, ref);
270 kprintf("Test6: passed\n");
273 static void test7(BattFsSuper *disk)
276 kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
279 fp = fopen(test_filename, "w+");
281 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
282 battfs_writeTestBlock(disk, 1, 0, 1, 0, 1, MARK_PAGE_VALID);
283 battfs_writeTestBlock(disk, 2, 0, 0, 0, 1, MARK_PAGE_VALID);
284 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
292 testCheck(disk, ref);
293 kprintf("Test7: passed\n");
296 static void test8(BattFsSuper *disk)
299 kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
302 fp = fopen(test_filename, "w+");
304 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
305 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
306 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
307 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
316 testCheck(disk, ref);
317 kprintf("Test8: passed\n");
320 static void test9(BattFsSuper *disk)
323 kprintf("Test9: 2 file with old seq num, 2 free block\n");
326 fp = fopen(test_filename, "w+");
328 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
329 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
330 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1, MARK_PAGE_VALID);
331 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
332 battfs_writeTestBlock(disk, 4, 0, 0, 0, 0, 1236);
333 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0, MARK_PAGE_VALID);
334 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1, MARK_PAGE_VALID);
335 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1, MARK_PAGE_VALID);
348 testCheck(disk, ref);
349 kprintf("Test9: passed\n");
352 static void test10(BattFsSuper *disk)
356 kprintf("Test10: open file test, inode 0 and inode 4\n");
358 fp = fopen(test_filename, "w+");
360 unsigned int PAGE_FILL = 116;
361 unsigned int INODE = 0;
362 unsigned int INODE2 = 4;
363 unsigned int INEXISTENT_INODE = 123;
364 unsigned int MODE = 0;
366 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
367 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
368 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
369 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
370 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
371 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
372 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
373 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
377 ASSERT(battfs_init(disk));
378 ASSERT(!battfs_fileExists(disk, INEXISTENT_INODE));
380 ASSERT(battfs_fileExists(disk, INODE));
381 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
382 ASSERT(fd1.fd.size == PAGE_FILL * 2);
383 ASSERT(fd1.fd.seek_pos == 0);
384 ASSERT(fd1.mode == MODE);
385 ASSERT(fd1.inode == INODE);
386 ASSERT(fd1.start == &disk->page_array[0]);
387 ASSERT(fd1.disk == disk);
388 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
390 ASSERT(kfile_reopen(&fd1.fd) == &fd1.fd);
391 ASSERT(fd1.fd.size == PAGE_FILL * 2);
392 ASSERT(fd1.fd.seek_pos == 0);
393 ASSERT(fd1.mode == MODE);
394 ASSERT(fd1.inode == INODE);
395 ASSERT(fd1.start == &disk->page_array[0]);
396 ASSERT(fd1.disk == disk);
397 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
399 ASSERT(battfs_fileExists(disk, INODE2));
400 ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
401 ASSERT(fd2.fd.size == PAGE_FILL * 2);
402 ASSERT(fd2.fd.seek_pos == 0);
403 ASSERT(fd2.mode == MODE);
404 ASSERT(fd2.inode == INODE2);
405 ASSERT(fd2.start == &disk->page_array[2]);
406 ASSERT(fd2.disk == disk);
407 ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
409 ASSERT(kfile_close(&fd1.fd) == 0);
410 ASSERT(kfile_close(&fd2.fd) == 0);
411 ASSERT(LIST_EMPTY(&disk->file_opened_list));
412 ASSERT(battfs_close(disk));
414 kprintf("Test10: passed\n");
417 static void test11(BattFsSuper *disk)
422 kprintf("Test11: read file test\n");
424 fp = fopen(test_filename, "w+");
426 unsigned int PAGE_FILL = 116;
427 unsigned int INODE = 0;
428 unsigned int INODE2 = 4;
429 unsigned int MODE = 0;
431 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
432 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
433 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
434 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
435 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
436 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
437 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
438 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
442 ASSERT(battfs_init(disk));
443 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
444 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
445 ASSERT(fd1.fd.seek_pos == sizeof(buf));
446 for (size_t i = 0; i < sizeof(buf); i++)
449 ASSERT(kfile_close(&fd1.fd) == 0);
450 ASSERT(battfs_close(disk));
452 kprintf("Test11: passed\n");
455 static void test12(BattFsSuper *disk)
459 kprintf("Test12: read file test across page boundary and seek test\n");
461 fp = fopen(test_filename, "w+");
463 const unsigned int PAGE_FILL = 116;
464 unsigned int INODE = 0;
465 unsigned int MODE = 0;
466 uint8_t buf[PAGE_FILL + 10];
468 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
469 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
470 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
471 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
472 battfs_writeTestBlock(disk, 4, INODE, 0, PAGE_FILL, 0, 1236);
473 battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2, MARK_PAGE_VALID);
474 battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3, MARK_PAGE_VALID);
475 battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3, MARK_PAGE_VALID);
479 ASSERT(battfs_init(disk));
480 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
482 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
483 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf));
484 for (size_t i = 0; i < sizeof(buf); i++)
487 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
488 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 2);
489 for (size_t i = 0; i < sizeof(buf); i++)
492 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
493 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 3);
494 for (size_t i = 0; i < sizeof(buf); i++)
497 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == 86);
498 ASSERT(fd1.fd.seek_pos == (kfile_off_t)fd1.fd.size);
499 for (size_t i = 0; i < 86; i++)
502 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_SET) == 0);
503 ASSERT(fd1.fd.seek_pos == 0);
505 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_END) == (kfile_off_t)fd1.fd.size);
506 ASSERT(fd1.fd.seek_pos = (kfile_off_t)fd1.fd.size);
508 ASSERT(kfile_close(&fd1.fd) == 0);
509 ASSERT(battfs_close(disk));
511 kprintf("Test12: passed\n");
515 int battfs_testRun(void)
519 disk.open = disk_open;
520 disk.read = disk_page_read;
521 disk.write = disk_page_write;
522 disk.erase = disk_page_erase;
523 disk.close = disk_close;
536 kprintf("All tests passed!\n");
543 int battfs_testSetup(void)
548 int battfs_testTearDown(void)
555 #include "fs/battfs.c"
556 #include "drv/kdebug.c"
557 #include "mware/formatwr.c"
558 #include "mware/hex.c"
563 return battfs_testRun();