yaffs have been making some changes to yaffs_tester file.
[yaffs2.git] / direct / timothy_tests / dev / yaffs_tester.c
index 5f1ebcf5f278e3512de6a3743e0c2a9a1bf185d5..68353d0e00e9e88ddc94c6abc671a0c27f62d3b4 100644 (file)
@@ -26,17 +26,12 @@ int simulate_power_failure = 0;
 
 buffer message_buffer; /*create  message_buffer */
 
-
-struct handle_regster{
-       int handle[MAX_NUMBER_OF_OPENED_HANDLES];
-       char path[MAX_NUMBER_OF_OPENED_HANDLES][100];
-       int number_of_open_handles;
-};
+char yaffs_test_dir[] ="/yaffs2/test_dir";     /*the path to the directory where all of the testing will take place*/
+char yaffs_mount_dir[]="/yaffs2/";             /*the path to the mount point which yaffs will mount*/
 
 
 int main(int argc, char *argv[]){      
-       char yaffs_test_dir[] ="/yaffs2/test_dir";      /*the path to the directory where all of the testing will take place*/
-       char yaffs_mount_dir[]="/yaffs2/";              /*the path to the mount point which yaffs will mount*/
+       
        
        init(yaffs_test_dir,yaffs_mount_dir,argc,argv);
        test(yaffs_test_dir);
@@ -93,6 +88,11 @@ void init(char *yaffs_test_dir,char *yaffs_mount_dir,int argc, char *argv[]){
        }
        
 }
+
+void quit_program(){
+       yaffs_unmount(yaffs_mount_dir);
+       exit(1);
+}
 void join_paths(char *path1,char *path2,char *new_path ){
 
 /*     printf("strlen path1:%d\n",strlen(path1));
@@ -159,7 +159,149 @@ void join_paths(char *path1,char *path2,char *new_path ){
        }
 }
 
-void open_file();
+void open_random_file(char *yaffs_test_dir, handle_regster *P_open_handles_array){
+       int output=0;
+       int x=0;
+       char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
+       char path[MAX_FILE_NAME_SIZE+strlen(yaffs_test_dir)];
+       path[0]='\0';// this should clear the path
+       add_to_buffer(&message_buffer,"\n\number of opened handles: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+       append_int_to_buffer(&message_buffer,P_open_handles_array->number_of_open_handles,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+       if (P_open_handles_array->number_of_open_handles<MAX_NUMBER_OF_OPENED_HANDLES)
+       {
+               generate_random_string(name,MAX_FILE_NAME_SIZE);
+               printf("before %d %d %d\n",strlen(yaffs_test_dir),strlen(name),strlen(path));
+               join_paths(yaffs_test_dir,name,path);//bug###################### here
+               printf("after %d %d %d\n",strlen(yaffs_test_dir),strlen(name),strlen(path));
+               add_to_buffer(&message_buffer,"trying to open file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+               if (yaffs_access(path,0)==0){
+                       stat_file(path);
+               }
+               else {
+                       add_to_buffer(&message_buffer,"file does not exists, creating file",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               }
+
+               output=yaffs_open(path,O_CREAT | O_TRUNC| O_RDWR, S_IREAD | S_IWRITE);
+               x=0;
+               for (x=0;P_open_handles_array->handle[x]!=-3 && x<MAX_NUMBER_OF_OPENED_HANDLES;x++){}   /*find an empty handle*/         
+
+               add_to_buffer(&message_buffer,"handle array id ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_int_to_buffer(&message_buffer,x,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+                       
+               //for (x=0;handle_pointers[x]!=NULL;x++){}
+               P_open_handles_array->handle[x]=output;
+               P_open_handles_array->path[x][0]='\0';
+               strcat(P_open_handles_array->path[x],path);
+               add_to_buffer(&message_buffer,"yaffs handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_int_to_buffer(&message_buffer,output,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+               add_to_buffer(&message_buffer,"stored handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+       
+               //yaffs_open will return a null pointer if it cannot open a file. check for errors will not work.                       
+               yaffs_check_for_errors(output, &message_buffer,"failed to open file","opened file");
+               
+               P_open_handles_array->number_of_open_handles++; 
+       }
+       else close_random_file(P_open_handles_array);
+}
+
+void write_to_random_file(handle_regster *P_open_handles_array){
+       int number_of_lines_of_text=0;
+       int length=100;
+       char text[length+1];
+       text[0]='\0';
+       int seek=0;
+       int x=0;
+       int output=0;
+       if (P_open_handles_array->number_of_open_handles>0){
+               
+               while (P_open_handles_array->handle[x]==-3){            /*find a random open handle*/
+                       x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
+               }
+               add_to_buffer(&message_buffer,"\n\ntrying to write to ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_to_buffer(&message_buffer,P_open_handles_array->path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+               
+               stat_file(P_open_handles_array->path[x]);
+               number_of_lines_of_text=rand() %1000;
+               add_to_buffer(&message_buffer,"writing  ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_int_to_buffer(&message_buffer,number_of_lines_of_text,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_to_buffer(&message_buffer," lines of text",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+
+               
+               for (;number_of_lines_of_text>0;number_of_lines_of_text--)
+               {
+                       generate_random_string(text,length);
+                       seek=rand()%1000;
+                       add_to_buffer(&message_buffer,"trying to seek to  ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+                       append_int_to_buffer(&message_buffer,seek,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+                       output=yaffs_lseek(P_open_handles_array->handle[x],seek,SEEK_SET);
+                       yaffs_check_for_errors(output, &message_buffer,"failed to seek","seeked file");
+                       add_to_buffer(&message_buffer,"trying to write to file",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+                       output=yaffs_write(P_open_handles_array->handle[x], text, strlen(text));
+                       yaffs_check_for_errors(output, &message_buffer,"failed to write text","wrote text");
+                                       
+               }       
+        }
+}
+
+void truncate_random_file(handle_regster *P_open_handles_array){
+       int x=0;
+       int truncate_size=0;
+       int output=0;
+       
+       if (P_open_handles_array->number_of_open_handles>0){    
+               add_to_buffer(&message_buffer,"\n\ntruncate function ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               while (P_open_handles_array->handle[x]==-3){            /*find a random open handle*/
+                       x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
+               }
+               add_to_buffer(&message_buffer,"trying to truncate ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+               append_to_buffer(&message_buffer,P_open_handles_array->path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+               
+               stat_file(P_open_handles_array->path[x]);
+               truncate_size=rand() %10000;
+               output=yaffs_ftruncate(P_open_handles_array->handle[x], truncate_size);
+               yaffs_check_for_errors(output, &message_buffer,"failed to truncate file","truncated file");
+       }
+}
+
+void close_random_file(handle_regster *P_open_handles_array){
+       /*run out of space on the handle pointer array*/        
+       /*make space*/
+       int x=0;
+       int output=0;
+       int start=0;
+       if (P_open_handles_array->number_of_open_handles>0){
+               start=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
+               for (x=start;P_open_handles_array->handle[x] !=-3 &&(x+1>start ||x<start);){
+                       x++;                    
+                       if (x>MAX_NUMBER_OF_OPENED_HANDLES-1) x=0;              
+
+               }
+               if (P_open_handles_array->handle[x]!=-3)
+               {
+                       add_to_buffer(&message_buffer,"\n\ntrying to close file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+                       append_to_buffer(&message_buffer,P_open_handles_array->path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+                       add_to_buffer(&message_buffer,"file handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
+                       append_int_to_buffer(&message_buffer,P_open_handles_array->handle[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
+       
+                       stat_file(P_open_handles_array->path[x]);
+                       output=yaffs_close(P_open_handles_array->handle[x]);
+
+                       if (output==-1) yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
+                       else {
+                               yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
+                               P_open_handles_array->handle[x]=-3;
+                               P_open_handles_array->path[x][0]='\0';
+                               P_open_handles_array->number_of_open_handles--;
+                       }
+               }
+               else {
+                       add_to_buffer(&message_buffer,"\n\ntried to close file but could not find a open file ",MESSAGE_LEVEL_BASIC_TASKS,PRINT);       
+               }       
+       }
+}
+
 void stat_file(char *path){
        int output=0;
        struct yaffs_stat stat;
@@ -185,13 +327,13 @@ void stat_file(char *path){
 void test(char*yaffs_test_dir){
        struct yaffs_stat stat;
        int output=0;
-       char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
-       char path[MAX_FILE_NAME_SIZE];
-       struct handle_regster open_handles_array;
+       //char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
+       //char path[MAX_FILE_NAME_SIZE];
+       handle_regster open_handles_array;
        //int handle_pointers[MAX_NUMBER_OF_OPENED_HANDLES];
        //int number_of_opened_handles=0;
        int x=0;
-       join_paths(yaffs_test_dir,name,path);
+       
                
        open_handles_array.number_of_open_handles=0;
        for (x=0;x<MAX_NUMBER_OF_OPENED_HANDLES;x++){
@@ -201,71 +343,18 @@ void test(char*yaffs_test_dir){
        }
        while(1)
        {
-               path[0]='\0';// this should clear the path
-               add_to_buffer(&message_buffer,"number of opened handles: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-               append_int_to_buffer(&message_buffer,open_handles_array.number_of_open_handles,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
-               if (open_handles_array.number_of_open_handles<MAX_NUMBER_OF_OPENED_HANDLES)
-               {
-                       generate_random_string(name);
-                       join_paths(yaffs_test_dir,name,path);
-                       add_to_buffer(&message_buffer,"trying to open file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-                       append_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
-                       if (yaffs_access(path,0)==0){
-                               stat_file(path);
-                       }
-                       else {
-                               add_to_buffer(&message_buffer,"file does not exists, creating file",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-                       }
-
-                       output=yaffs_open(path,O_CREAT | O_TRUNC| O_RDWR, S_IREAD | S_IWRITE);
-                       x=0;
-                       for (x=0;open_handles_array.handle[x]!=-3 && x<MAX_NUMBER_OF_OPENED_HANDLES;x++){               
-                               //x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
-                       }
-
-                       add_to_buffer(&message_buffer,"handle array id ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-                       append_int_to_buffer(&message_buffer,x,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
-                               
-                       //for (x=0;handle_pointers[x]!=NULL;x++){}
-                       open_handles_array.handle[x]=output;
-                       open_handles_array.path[x][0]='\0';
-                       strcat(open_handles_array.path[x],path);
-                       add_to_buffer(&message_buffer,"yaffs handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-                       append_int_to_buffer(&message_buffer,output,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
-                       add_to_buffer(&message_buffer,"stored handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-               
-                       //yaffs_open will return a null pointer if it cannot open a file. check for errors will not work.                       
-                       yaffs_check_for_errors(output, &message_buffer,"failed to open file","opened file");
-                       
-                       open_handles_array.number_of_open_handles++;
-               }
-               else{ /*run out of space on the handle pointer array*/  
-                       /*make space*/
-                       while (open_handles_array.handle[x]==-3){               
-                               x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
-                       }
-                       add_to_buffer(&message_buffer,"trying to close file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-                       append_to_buffer(&message_buffer,open_handles_array.path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
-                       add_to_buffer(&message_buffer,"file handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
-                       append_int_to_buffer(&message_buffer,open_handles_array.handle[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
-
-                       stat_file(open_handles_array.path[x]);
-       
-                       output=yaffs_close(open_handles_array.handle[x]);
-
-                       if (output==-1) yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
-                       else {
-                               yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
-                               open_handles_array.handle[x]=-3;
-                               open_handles_array.path[x][0]='\0';
-                               open_handles_array.number_of_open_handles--;
-                       }
+               x=rand() % 3;
+               switch(x){
+                       case 0 :open_random_file(yaffs_test_dir,&open_handles_array);break;
+                       //case 1 :write_to_random_file(&open_handles_array);break;
+                       case 2 :close_random_file(&open_handles_array);break;
+                       case 3 :truncate_random_file(&open_handles_array);break;
                }
        }
 }
-void  generate_random_string(char *ptr){
+void  generate_random_string(char *ptr,int length_of_str){
        unsigned int x;
-       unsigned int length=((rand() %MAX_FILE_NAME_SIZE)+1);   /*creates a int with the number of charecters been between 1 and 51*/           
+       unsigned int length=((rand() %length_of_str)+1);        /*creates a int with the number of charecters been between 1 and 51*/           
        char letter='\0';
 
        //printf("generating string\n");