lab5 added.
[mit-jos.git] / fs / test.c
blob29d18542bbfd81ab44de0ba107b49eb54f3dd855
2 #include <inc/x86.h>
3 #include <inc/string.h>
5 #include "fs.h"
8 int
9 strecmp(char *a, char *b)
11 while (*b)
12 if (*a++ != *b++)
13 return 1;
14 return 0;
17 static char *msg = "This is the NEW message of the day!\n\n";
19 void
20 fs_test(void)
22 struct File *f;
23 int r;
24 char *blk;
25 uint32_t *bits;
27 // back up bitmap
28 if ((r = sys_page_alloc(0, (void*) PGSIZE, PTE_P|PTE_U|PTE_W)) < 0)
29 panic("sys_page_alloc: %e", r);
30 bits = (uint32_t*) PGSIZE;
31 memmove(bits, bitmap, PGSIZE);
32 // allocate block
33 if ((r = alloc_block()) < 0)
34 panic("alloc_block: %e", r);
35 // check that block was free
36 assert(bits[r/32] & (1 << (r%32)));
37 // and is not free any more
38 assert(!(bitmap[r/32] & (1 << (r%32))));
39 cprintf("alloc_block is good\n");
41 if ((r = file_open("/not-found", &f)) < 0 && r != -E_NOT_FOUND)
42 panic("file_open /not-found: %e", r);
43 else if (r == 0)
44 panic("file_open /not-found succeeded!");
45 if ((r = file_open("/newmotd", &f)) < 0)
46 panic("file_open /newmotd: %e", r);
47 cprintf("file_open is good\n");
49 if ((r = file_get_block(f, 0, &blk)) < 0)
50 panic("file_get_block: %e", r);
51 if (strecmp(blk, msg) != 0)
52 panic("file_get_block returned wrong data");
53 cprintf("file_get_block is good\n");
55 *(volatile char*)blk = *(volatile char*)blk;
56 assert((vpt[VPN(blk)] & PTE_D));
57 file_flush(f);
58 assert(!(vpt[VPN(blk)] & PTE_D));
59 cprintf("file_flush is good\n");
61 if ((r = file_set_size(f, 0)) < 0)
62 panic("file_set_size: %e", r);
63 assert(f->f_direct[0] == 0);
64 assert(!(vpt[VPN(f)] & PTE_D));
65 cprintf("file_truncate is good\n");
67 if ((r = file_set_size(f, strlen(msg))) < 0)
68 panic("file_set_size 2: %e", r);
69 assert(!(vpt[VPN(f)] & PTE_D));
70 if ((r = file_get_block(f, 0, &blk)) < 0)
71 panic("file_get_block 2: %e", r);
72 strcpy(blk, msg);
73 assert((vpt[VPN(blk)] & PTE_D));
74 file_flush(f);
75 assert(!(vpt[VPN(blk)] & PTE_D));
76 file_close(f);
77 assert(!(vpt[VPN(f)] & PTE_D));
78 cprintf("file rewrite is good\n");