yaffs Commiting some more yaffs tests showing bugs in yaffs.
[yaffs2.git] / direct / timothy_tests / running_out_of_handles_error / yaffs_tester.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Timothy Manning <timothy@yaffs.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 /*
15  * yaffs_tester.c designed to stress test yaffs2 direct.
16  */
17
18
19 #include "yaffs_tester.h"
20
21
22
23 int random_seed;
24 int simulate_power_failure = 0;
25
26
27 buffer message_buffer;  /*create  message_buffer */
28
29
30
31 int main(int argc, char *argv[]){       
32         char yaffs_test_dir[] ="/yaffs2/test_dir";      /*the path to the directory where all of the testing will take place*/
33         char yaffs_mount_dir[]="/yaffs2/";              /*the path to the mount point which yaffs will mount*/
34         
35         init(yaffs_test_dir,yaffs_mount_dir,argc,argv);
36         test(yaffs_test_dir);
37         yaffs_unmount(yaffs_mount_dir);
38         return 0;
39 }
40
41
42
43 void init(char *yaffs_test_dir,char *yaffs_mount_dir,int argc, char *argv[]){
44         char output=0;
45         int x=0;
46         int seed=-1;
47         FILE *log_handle;
48         /*these variables are already set to zero, but it is better not to take chances*/
49         message_buffer.head=0;                           
50         message_buffer.tail=0;
51
52
53         log_handle=fopen(LOG_FILE,"w");
54         if (log_handle!=NULL){
55                 fputs("log file for yaffs tester\n",log_handle);
56                 fclose(log_handle);
57         }
58         add_to_buffer(&message_buffer,"welcome to the yaffs tester",MESSAGE_LEVEL_BASIC_TASKS,PRINT);/* print boot up message*/ 
59         yaffs_start_up();
60         yaffs_mount(yaffs_mount_dir);
61         for (x=0;x<argc;x++){
62 //              add_to_buffer(&message_buffer,"argv ",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
63 //              add_to_buffer(&message_buffer,argv[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
64                 if (strcmp("-seed",argv[x])==0){                        /*warning only compares the length of the strings, quick fix*/
65                         seed= atoi(argv[x+1]);
66                         /*add_to_buffer(&message_buffer,"setting seed to ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
67                         append_int_to_buffer(&message_buffer,seed,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);                    
68                         append_to_buffer(&message_buffer,"\n",MESSAGE_LEVEL_BASIC_TASKS,PRINT);*/
69                 }
70         }
71         if (seed==-1){
72                 seed=time(NULL);
73                 srand(seed); 
74         }
75         else {
76         srand(seed);
77         }
78         add_to_buffer(&message_buffer,"setting seed to ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
79         append_int_to_buffer(&message_buffer,seed,MESSAGE_LEVEL_BASIC_TASKS,PRINT);/* print boot up message*/   
80
81         if (yaffs_access(yaffs_test_dir,0))     /* if the test folder does not exist then create it */
82         {
83                 add_to_buffer(&message_buffer,"creating dir: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
84                 append_to_buffer(&message_buffer,yaffs_test_dir,MESSAGE_LEVEL_BASIC_TASKS,PRINT);       
85                 output=yaffs_mkdir(yaffs_test_dir,S_IREAD | S_IWRITE);
86                 yaffs_check_for_errors(output, &message_buffer,"could not create dir","created dir\n\n");
87         }
88         
89 }
90 void join_paths(char *path1,char *path2,char *new_path ){
91
92 /*      printf("strlen path1:%d\n",strlen(path1));
93         printf("strlen path2:%d\n",strlen(path2));
94         printf("path1; %s\n",path1);
95 */
96         add_to_buffer(&message_buffer, "joining paths:",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
97         append_to_buffer(&message_buffer,path1,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
98         append_to_buffer(&message_buffer, " and ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
99         append_to_buffer(&message_buffer, path2,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
100         if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
101                 /*paths are compatiable. concatanate them. note -2 is because of \0*/  
102                 strcat(new_path,path1);
103                 strcat(new_path,path2);         
104                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
105                 //strcpy(new_path,strcat(path1,path2)); 
106                 //return new_path;
107         }       
108         else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
109                 /*paths are compatiable. concatanate them*/  
110                 strcat(new_path,path1);
111                 strcat(new_path,path2);         
112                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
113                 //strcpy(new_path,strcat(path1,path2)); 
114                 //return new_path;
115         }
116         else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
117                         /*need to add a "/". */  
118                 strcat(new_path,path1);
119                 strcat(new_path,"/");
120                 strcat(new_path,path2);
121                 //strcpy(new_path,strcat(path1,strcat("/\0",path2)));
122
123 #if 0
124                 copy_array(path1,new_path,0,0);
125                 copy_array('\0',new_path,0,(sizeof(path1)/sizeof(char)));
126                 copy_array(path2,new_path,0,(sizeof(path1)/sizeof(char))+1);
127  old method now trying to use copy_array
128                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))+1];
129                 for (x=0;x<=(sizeof(path1)/sizeof(char))-1;x++){ 
130                         new_path[x]=path1[x];
131                 }
132                 new_path[x+1]='/';
133                 for (x=(sizeof(path1)/sizeof(char)) ,i=0 ;i<=(sizeof(path2)/sizeof(char));x++,i++){ 
134                         new_path[x]=path2[i]; 
135                 }
136 #endif
137
138                 //return new_path;
139         }
140         else if ((path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]=='/') {
141                 /*need to remove a "/". */
142                 /*yaffs does not mind the extra slash. */
143                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))-1];
144                 
145                 strcat(new_path,path1);
146                 strcat(new_path,path2);
147                 //strcpy(new_path,strcat(path1,strncat("",path2,(sizeof(path1)/sizeof(char))-1))); 
148                 //return new_path;
149         } 
150         else{
151                 //error 
152                 //return -1;
153         }
154 }
155
156
157
158 void test(char*yaffs_test_dir){
159         char output=0;
160         char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
161         char path[MAX_FILE_NAME_SIZE];
162         join_paths(yaffs_test_dir,name,path);
163         while(1)
164         {
165                 path[0]='\0';// this should clear the path
166                 generate_random_string(name);
167                 join_paths(yaffs_test_dir,name,path);
168                 add_to_buffer(&message_buffer,"trying to open file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
169                 append_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
170                 output=yaffs_open(path,O_CREAT | O_TRUNC| O_RDWR, S_IREAD | S_IWRITE);
171                 yaffs_check_for_errors(output, &message_buffer,"failed to open file","opened file");
172         }
173 }
174 void  generate_random_string(char *ptr){
175         unsigned int x;
176         unsigned int length=((rand() %MAX_FILE_NAME_SIZE)+1);   /*creates a int with the number of charecters been between 1 and 51*/           
177         char letter='\0';
178         //printf("generating string\n");
179         //printf("string length is %d\n",length);
180         for (x=0; x <= (length-2) &&length>2 ; x++)
181         {
182                 //printf("x=%d\n",x);   
183                 /* keep generating a charecter until the charecter is legal*/
184                 while((letter=='\0' )||(letter=='/')||(letter=='\\')){
185                         letter=(rand() % 126-32)+32;    /*generate a number between 32 and 126 and uses it as a charecter (letter) */
186                 }       
187                 ptr[x]=letter;
188                 //printf("charecter generated is %c\n",ptr[x]);
189         }
190         ptr[x+1]='\0';  /*adds NULL charecter to turn it into a string*/
191         
192 }
193