00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <sys/types.h>
00005 #include <sys/stat.h>
00006 #include <unistd.h>
00007 #include "CUnit.h"
00008 #include "TestDB.h"
00009 #include "Basic.h"
00010 #include "fat.h"
00011 #include "format.h"
00012 #include "load_fat.h"
00013
00021 int success_init(void) { return 0; }
00023 int success_clean(void) { return 0; }
00024
00034 void run_formatter (char *fname, int bsize, int nblock) {
00035 int ret, len, i, nbytes;
00036 struct stat st;
00037 FILE *fs;
00038 struct fat_ctrl f_ctrl;
00039 struct boot_sector b_sec;
00040 struct dir_entry d_entry;
00041 unsigned int *fat_check;
00042 char blk_elem;
00043
00044
00045
00046
00047
00048
00049 fs = fopen(fname, "w");
00050 CU_ASSERT_PTR_NOT_NULL_FATAL(fs);
00051
00052
00053
00054
00055
00056 ret = fat_format(fs, bsize, nblock);
00057 CU_ASSERT_EQUAL(0, ret);
00058 fclose(fs);
00059
00060
00061
00062
00063 len = sizeof(struct boot_sector);
00064 len = len + sizeof(unsigned int) * nblock;
00065 len = len + bsize * nblock;
00066 stat(fname, &st);
00067 CU_TEST_FATAL(len == st.st_size);
00068
00069
00070
00071
00072
00073 fs = fopen(fname, "r+");
00074 ret = mount_fat(fs, &f_ctrl);
00075 CU_ASSERT_EQUAL_FATAL(0, ret);
00076
00077
00078
00079
00080 b_sec = f_ctrl.b_sector;
00081 CU_TEST_FATAL(b_sec.fs_type == FAT_TYPE);
00082 CU_TEST_FATAL(b_sec.block_size == bsize);
00083 CU_TEST_FATAL(b_sec.num_block == nblock);
00084
00085
00086
00087
00088 CU_ASSERT_EQUAL_FATAL(f_ctrl.blk_base, sizeof(struct boot_sector) + (sizeof(unsigned int) * b_sec.num_block));
00089
00090
00091
00092
00093
00094
00095 fat_check = f_ctrl.fat_ptr;
00096 for (i=0; i<b_sec.num_block; i++)
00097 CU_ASSERT_EQUAL_FATAL(fat_check[i], i==ROOT_IDX ? LAST_BLOCK : BLOCK_FREE);
00098
00099
00100
00101
00102
00103
00104 fseek(fs, f_ctrl.blk_base, SEEK_SET);
00105 nbytes = ROOT_IDX*b_sec.block_size;
00106 for (i=0; i<nbytes; i++) {
00107 ret = fread(&blk_elem, sizeof(char), 1, fs);
00108 CU_ASSERT_EQUAL_FATAL(1, ret);
00109 CU_ASSERT_EQUAL_FATAL(blk_elem, INIT_BLOCK);
00110 }
00111
00112
00113
00114
00115
00116 ret = fread(&d_entry, sizeof(d_entry), 1, fs);
00117 CU_ASSERT_EQUAL_FATAL(1, ret);
00118 CU_ASSERT_EQUAL_FATAL(d_entry.used, DIR_ENTRY_BUSY);
00119 CU_ASSERT_STRING_EQUAL_FATAL(d_entry.name, ".");
00120 CU_ASSERT_EQUAL_FATAL(d_entry.attr, SUB_ENTRY);
00121 CU_ASSERT_EQUAL_FATAL(d_entry.index, 0);
00122 CU_ASSERT_EQUAL_FATAL(d_entry.len, 2 * sizeof(d_entry));
00123
00124 ret = fread(&d_entry, sizeof(d_entry), 1, fs);
00125 CU_ASSERT_EQUAL_FATAL(1, ret);
00126 CU_ASSERT_EQUAL_FATAL(d_entry.used, DIR_ENTRY_BUSY);
00127 CU_ASSERT_STRING_EQUAL_FATAL(d_entry.name, "..");
00128 CU_ASSERT_EQUAL_FATAL(d_entry.attr, SUB_ENTRY);
00129 CU_ASSERT_EQUAL_FATAL(d_entry.index, 0);
00130 CU_ASSERT_EQUAL_FATAL(d_entry.len, 2 * sizeof(d_entry));
00131
00132
00133
00134
00135
00136
00137 len = 2 * sizeof(d_entry) + nbytes;
00138 nbytes = b_sec.num_block*b_sec.block_size;
00139 for (i=len; i<nbytes; i++) {
00140 ret = fread(&blk_elem, sizeof(char), 1, fs);
00141 CU_ASSERT_EQUAL_FATAL(1, ret);
00142 CU_ASSERT_EQUAL_FATAL(blk_elem, INIT_BLOCK);
00143 }
00144 }
00145
00152 void format_128 (void)
00153 {
00154 run_formatter("./DataFile/FAT_128", 128, 100);
00155 }
00156
00163 void format_1K (void)
00164 {
00165 run_formatter("./DataFile/FAT_1K", 1024, 10);
00166 }
00167
00174 void format_2K (void)
00175 {
00176 run_formatter("./DataFile/FAT_2K", 1024*2, 10);
00177 }
00178
00185 void format_4K (void)
00186 {
00187 run_formatter("./DataFile/FAT_4K", 1024*4, 10);
00188 }
00189
00191 void add_suite_format(void)
00192 {
00193 CU_pSuite format_suite = NULL;
00194
00195 format_suite = CU_add_suite("Format Operation", success_init, success_clean);
00196 CU_add_test(format_suite, "FAT format/mount Test (128 Bytes)", format_128);
00197 CU_add_test(format_suite, "FAT format/mount Test (1K Bytes)", format_1K);
00198 CU_add_test(format_suite, "FAT format/mount Test (2K Bytes)", format_2K);
00199 CU_add_test(format_suite, "FAT format/mount Test (4K Bytes)", format_4K);
00200 }
00201