open_suite.c

Vai alla documentazione di questo file.
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 <mcheck.h>
00008 #include "CUnit.h"
00009 #include "TestDB.h"
00010 #include "Basic.h"
00011 #include "fat.h"
00012 #include "format.h"
00013 #include "load_fat.h"
00014 #include "list.h"
00015 #include "open_fat.h"
00016 #include "open_suite.h"
00017 
00025 int open_init(void) {  return 0; }
00026 
00028 int open_clean(void) {    return 0; }
00029 
00042 void simple_open (char *fname, int block_size, unsigned int num_block)
00043 {
00044     FILE *fs;
00045     struct fat_ctrl f_ctrl;
00046     struct dir_entry root_info;
00047     unsigned int len;
00048     int  ret;
00049     struct stat st;
00050     char *list;
00051     char ex_list[ ] = ".\t..\tfile1";
00052 
00053     /*
00054      * Formattazione file
00055      */
00056     if ( ( fs = fopen(fname, "w") ) == NULL ) {
00057         perror("simpleopen");
00058         exit (EXIT_FAILURE);
00059     }
00060     fat_format(fs, block_size, num_block);
00061     fclose(fs);
00062     /* caricamento  */
00063     if ( ( fs = fopen(fname, "r+") ) == NULL) {
00064         perror("simpleopen");
00065         exit (EXIT_FAILURE);
00066     }
00067     ret = mount_fat(fs, &f_ctrl);
00068     CU_ASSERT_EQUAL_FATAL(ret, 0);
00069 
00070     /*
00071      * Cerca di creare il file "file1" e chiude il file
00072      */
00073     ret = fat_open(fs, &f_ctrl, "/file1");
00074     CU_ASSERT_EQUAL_FATAL(ret, 0);
00075     fclose(fs);
00076     /* cerca di fare il mount del filesystem .*/
00077     if ( ( fs = fopen(fname, "r+") ) == NULL) {
00078         perror("simpleopen");
00079         exit (EXIT_FAILURE);
00080     }
00081     ret = mount_fat(fs, &f_ctrl);
00082     CU_ASSERT_EQUAL_FATAL(ret, 0);
00083 
00084     /* 
00085      * La root directory table deve contenere tre entry
00086      * per ".", ".." e "dir1" rispettivamente.
00087      */
00088     fseek(fs, f_ctrl.blk_base + (ROOT_IDX)*f_ctrl.b_sector.block_size, SEEK_SET);
00089     /* Legge  "." entry */
00090     ret = fread(&root_info, sizeof(struct dir_entry), 1, fs); 
00091     CU_ASSERT_EQUAL_FATAL(ret, 1);
00092     CU_ASSERT_EQUAL_FATAL(root_info.used, DIR_ENTRY_BUSY);
00093     CU_ASSERT_STRING_EQUAL_FATAL(root_info.name, ".");
00094     CU_ASSERT_EQUAL_FATAL(root_info.attr, SUB_ENTRY);
00095     /* Legge  ".." entry */
00096     ret = fread(&root_info, sizeof(struct dir_entry), 1, fs);
00097     CU_ASSERT_EQUAL_FATAL(ret, 1);
00098     CU_ASSERT_EQUAL_FATAL(root_info.used, DIR_ENTRY_BUSY);
00099     CU_ASSERT_STRING_EQUAL_FATAL(root_info.name, "..");
00100     CU_ASSERT_EQUAL_FATAL(root_info.attr, SUB_ENTRY);
00101     /* Legge "file1" entry */
00102     ret = fread(&root_info, sizeof(struct dir_entry), 1, fs); 
00103     CU_ASSERT_EQUAL_FATAL(ret, 1);
00104     CU_ASSERT_EQUAL_FATAL(root_info.used, DIR_ENTRY_BUSY);
00105     CU_ASSERT_STRING_EQUAL_FATAL(root_info.name, "file1");
00106     CU_ASSERT_EQUAL_FATAL(root_info.attr, FILE_ENTRY);
00107 
00108 
00109     /*
00110      * Controlla la lunghezza del file
00111      */
00112     len = sizeof(struct boot_sector);
00113     len = len + sizeof(unsigned int) * f_ctrl.b_sector.num_block;
00114     len = len + f_ctrl.b_sector.block_size * f_ctrl.b_sector.num_block;
00115     stat(fname, &st);
00116     CU_TEST_FATAL(len == st.st_size);
00117 
00118     /*
00119      * Proviamo a listare il nuovo file.
00120      */
00121     ret = fat_ls(fs, &f_ctrl, "/", &list);
00122     CU_ASSERT_EQUAL_FATAL(ret, 0);
00123     CU_ASSERT_STRING_EQUAL_FATAL(list, ex_list);
00124 
00125     /*
00126      * Cleanup
00127      */
00128     fclose(fs);
00129     unlink(fname); 
00130 }
00131 
00136 void simple_open_128 (void)
00137 {
00138     simple_open("DataFile/fat_128", 128, 10);
00139 }
00140 
00145 void simple_open_1K (void)
00146 {
00147     simple_open("DataFile/fat_1K", 1024, 10);
00148 }
00149 
00154 void simple_open_2K (void)
00155 {
00156     simple_open("DataFile/fat_2K", 1024*2, 10);
00157 }
00158 
00163 void simple_open_4K (void)
00164 {
00165     simple_open("DataFile/fat_4K", 1024*4, 10);
00166 }
00167 
00171 void add_suite_open(void)
00172 {
00173     CU_pSuite open_suite = NULL;
00174 
00175     open_suite = CU_add_suite("File creation", open_init, open_clean);
00176 
00177     /* Test creazione file. */ 
00178     CU_add_test(open_suite, "Create a file with block size 128 bytes", simple_open_128); 
00179     CU_add_test(open_suite, "Create a file with block size 1KB", simple_open_1K); 
00180     CU_add_test(open_suite, "Create a file with block size 2KB", simple_open_2K);
00181     CU_add_test(open_suite, "Create a file with block size 4KB", simple_open_4K);  
00182 }
Generato il Fri Jan 28 22:16:31 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3