Fix copyright
[yaffs2.git] / direct / test-framework / timothy_tests / stress_tester / yaffs_tester.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Timothy Manning <timothy@yaffs.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * yaffs_tester.c designed to stress test yaffs2 direct.
15  */
16
17
18 #include "yaffs_tester.h"
19
20
21
22 int random_seed;
23 int simulate_power_failure = 0;
24
25
26 buffer message_buffer;  /*create  message_buffer */
27
28 char yaffs_test_dir[] ="/yaffs2/test_dir";      /*the path to the directory where all of the testing will take place*/
29 char yaffs_mount_dir[]="/yaffs2/";              /*the path to the mount point which yaffs will mount*/
30
31
32 int main(int argc, char *argv[]){       
33         
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
91 void quit_program(){
92         yaffs_unmount(yaffs_mount_dir);
93         exit(1);
94 }
95 void join_paths(char *path1,char *path2,char *new_path ){
96
97 /*      printf("strlen path1:%d\n",strlen(path1));
98         printf("strlen path2:%d\n",strlen(path2));
99         printf("path1; %s\n",path1);
100 */
101         add_to_buffer(&message_buffer, "joining paths:",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
102         append_to_buffer(&message_buffer,path1,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
103         append_to_buffer(&message_buffer, " and ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
104         append_to_buffer(&message_buffer, path2,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
105         if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
106                 /*paths are compatiable. concatanate them. note -2 is because of \0*/  
107                 strcat(new_path,path1);
108                 strcat(new_path,path2);         
109                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
110                 //strcpy(new_path,strcat(path1,path2)); 
111                 //return new_path;
112         }       
113         else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
114                 /*paths are compatiable. concatanate them*/  
115                 strcat(new_path,path1);
116                 strcat(new_path,path2);         
117                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
118                 //strcpy(new_path,strcat(path1,path2)); 
119                 //return new_path;
120         }
121         else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
122                         /*need to add a "/". */  
123                 strcat(new_path,path1);
124                 strcat(new_path,"/");
125                 strcat(new_path,path2);
126                 //strcpy(new_path,strcat(path1,strcat("/\0",path2)));
127
128 #if 0
129                 copy_array(path1,new_path,0,0);
130                 copy_array('\0',new_path,0,(sizeof(path1)/sizeof(char)));
131                 copy_array(path2,new_path,0,(sizeof(path1)/sizeof(char))+1);
132  old method now trying to use copy_array
133                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))+1];
134                 for (x=0;x<=(sizeof(path1)/sizeof(char))-1;x++){ 
135                         new_path[x]=path1[x];
136                 }
137                 new_path[x+1]='/';
138                 for (x=(sizeof(path1)/sizeof(char)) ,i=0 ;i<=(sizeof(path2)/sizeof(char));x++,i++){ 
139                         new_path[x]=path2[i]; 
140                 }
141 #endif
142
143                 //return new_path;
144         }
145         else if ((path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]=='/') {
146                 /*need to remove a "/". */
147                 /*yaffs does not mind the extra slash. */
148                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))-1];
149                 
150                 strcat(new_path,path1);
151                 strcat(new_path,path2);
152                 //strcpy(new_path,strcat(path1,strncat("",path2,(sizeof(path1)/sizeof(char))-1))); 
153                 //return new_path;
154         } 
155         else{
156                 //error 
157                 //return -1;
158         }
159 }
160
161 void open_random_file(char *yaffs_test_dir, handle_regster *P_open_handles_array){
162         int output=0;
163         int x=0;
164         char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
165         char path[MAX_FILE_NAME_SIZE+strlen(yaffs_test_dir)];
166         path[0]='\0';// this should clear the path
167         add_to_buffer(&message_buffer,"\n\number of opened handles: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
168         append_int_to_buffer(&message_buffer,P_open_handles_array->number_of_open_handles,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
169         if (P_open_handles_array->number_of_open_handles<MAX_NUMBER_OF_OPENED_HANDLES)
170         {
171                 generate_random_string(name,MAX_FILE_NAME_SIZE);
172                 printf("before %d %d %d\n",strlen(yaffs_test_dir),strlen(name),strlen(path));
173                 join_paths(yaffs_test_dir,name,path);
174                 printf("after %d %d %d\n",strlen(yaffs_test_dir),strlen(name),strlen(path));
175                 add_to_buffer(&message_buffer,"trying to open file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
176                 append_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
177                 if (yaffs_access(path,0)==0){
178                         stat_file(path);
179                 }
180                 else {
181                         add_to_buffer(&message_buffer,"file does not exists, creating file",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
182                 }
183
184                 output=yaffs_open(path,O_CREAT | O_TRUNC| O_RDWR, S_IREAD | S_IWRITE);
185                 x=0;
186                 for (x=0;P_open_handles_array->handle[x]!=-3 && x<MAX_NUMBER_OF_OPENED_HANDLES;x++){}   /*find an empty handle*/         
187
188                 add_to_buffer(&message_buffer,"handle array id ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
189                 append_int_to_buffer(&message_buffer,x,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
190                         
191                 //for (x=0;handle_pointers[x]!=NULL;x++){}
192                 P_open_handles_array->handle[x]=output;
193                 P_open_handles_array->path[x][0]='\0';
194                 strcat(P_open_handles_array->path[x],path);
195                 add_to_buffer(&message_buffer,"yaffs handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
196                 append_int_to_buffer(&message_buffer,output,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
197                 add_to_buffer(&message_buffer,"stored handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
198         
199                 //yaffs_open will return a null pointer if it cannot open a file. check for errors will not work.                       
200                 yaffs_check_for_errors(output, &message_buffer,"failed to open file","opened file");
201                 
202                 P_open_handles_array->number_of_open_handles++; 
203         }
204         else  close_random_file(P_open_handles_array);
205  
206 }
207
208 void write_to_random_file(handle_regster *P_open_handles_array){
209         int number_of_lines_of_text=0;
210         int length=100;
211         char text[length+1];
212         text[0]='\0';
213         int seek=0;
214         int x=0;
215         int output=0;
216         if (P_open_handles_array->number_of_open_handles>0){
217                 
218                 while (P_open_handles_array->handle[x]==-3){            /*find a random open handle*/
219                         x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
220                 }
221                 add_to_buffer(&message_buffer,"\n\ntrying to write to ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
222                 append_to_buffer(&message_buffer,P_open_handles_array->path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
223                 
224                 stat_file(P_open_handles_array->path[x]);
225                 number_of_lines_of_text=rand() %1000;
226                 add_to_buffer(&message_buffer,"writing  ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
227                 append_int_to_buffer(&message_buffer,number_of_lines_of_text,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
228                 append_to_buffer(&message_buffer," lines of text",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
229
230                 
231                 for (;number_of_lines_of_text>0;number_of_lines_of_text--)
232                 {
233                         generate_random_string(text,length);
234                         seek=rand()%1000;
235                         add_to_buffer(&message_buffer,"trying to seek to  ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
236                         append_int_to_buffer(&message_buffer,seek,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
237                         output=yaffs_lseek(P_open_handles_array->handle[x],seek,SEEK_SET);
238                         yaffs_check_for_errors(output, &message_buffer,"failed to seek","seeked file");
239                         add_to_buffer(&message_buffer,"trying to write to file",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
240                         output=yaffs_write(P_open_handles_array->handle[x], text, strlen(text));
241                         yaffs_check_for_errors(output, &message_buffer,"failed to write text","wrote text");
242                                         
243                 }       
244          }
245 }
246
247 void truncate_random_file(handle_regster *P_open_handles_array){
248         int x=0;
249         int truncate_size=0;
250         int output=0;
251         
252         if (P_open_handles_array->number_of_open_handles>0){    
253                 add_to_buffer(&message_buffer,"\n\ntruncate function ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
254                 while (P_open_handles_array->handle[x]==-3){            /*find a random open handle*/
255                         x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
256                 }
257                 add_to_buffer(&message_buffer,"trying to truncate ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
258                 append_to_buffer(&message_buffer,P_open_handles_array->path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
259                 
260                 stat_file(P_open_handles_array->path[x]);
261                 truncate_size=rand() %10000;
262                 output=yaffs_ftruncate(P_open_handles_array->handle[x], truncate_size);
263                 yaffs_check_for_errors(output, &message_buffer,"failed to truncate file","truncated file");
264         }
265 }
266
267 void close_random_file(handle_regster *P_open_handles_array){
268         /*run out of space on the handle pointer array*/        
269         /*make space*/
270         int x=0;
271         int output=0;
272         int start=0;
273         printf("trying to clear handle");
274         if (P_open_handles_array->number_of_open_handles>0){
275                 start=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
276
277                 for (x=start;(x+1>start ||x<start);){
278                         x++;                    
279                         if (x>MAX_NUMBER_OF_OPENED_HANDLES-1) x=0;
280                         if (P_open_handles_array->handle[x] !=-3 ){
281                                 //the handle is open, so try to close it.
282                                 break;
283                         }
284
285                 }
286                 if (P_open_handles_array->handle[x]!=-3)
287                 {
288                         add_to_buffer(&message_buffer,"\n\ntrying to close file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
289                         append_to_buffer(&message_buffer,P_open_handles_array->path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
290                         add_to_buffer(&message_buffer,"file handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
291                         append_int_to_buffer(&message_buffer,P_open_handles_array->handle[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
292         
293                         stat_file(P_open_handles_array->path[x]);
294                         output=yaffs_close(P_open_handles_array->handle[x]);
295
296                         if (output==-1) yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
297                         else {
298                                 yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
299                                 P_open_handles_array->handle[x]=-3;
300                                 P_open_handles_array->path[x][0]='\0';
301                                 P_open_handles_array->number_of_open_handles--;
302                         }
303                 }
304                 else {
305                         add_to_buffer(&message_buffer,"\n\ntried to close file but could not find a open file ",MESSAGE_LEVEL_BASIC_TASKS,PRINT);       
306                 }       
307         }
308 }
309
310 void stat_file(char *path){
311         int output=0;
312         struct yaffs_stat stat;
313         if (yaffs_access(path,0)==0){
314                 add_to_buffer(&message_buffer,"file exists, trying to stat: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
315                 add_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
316                 output=yaffs_lstat(path,&stat);
317                 yaffs_check_for_errors(output, &message_buffer,"failed to stat file","statted file");
318                 //stat.st_ino,(int)stat.st_size,stat.st_mode
319                 add_to_buffer(&message_buffer,"yaffs inode: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
320                 append_int_to_buffer(&message_buffer,stat.st_ino,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
321                 append_to_buffer(&message_buffer," file size: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
322                 append_int_to_buffer(&message_buffer,(int)stat.st_size,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);                       
323                 append_to_buffer(&message_buffer," file mode: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
324                 append_int_to_buffer(&message_buffer,stat.st_mode,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
325         }
326         else{
327                 add_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
328                 append_to_buffer(&message_buffer," does not exist,could not stat",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
329         }
330 }
331
332 void test(char*yaffs_test_dir){
333         struct yaffs_stat stat;
334         int output=0;
335         //char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
336         //char path[MAX_FILE_NAME_SIZE];
337         handle_regster open_handles_array;
338         //int handle_pointers[MAX_NUMBER_OF_OPENED_HANDLES];
339         //int number_of_opened_handles=0;
340         int x=0;
341         
342                 
343         open_handles_array.number_of_open_handles=0;
344         for (x=0;x<MAX_NUMBER_OF_OPENED_HANDLES;x++){
345                 open_handles_array.handle[x]=-3;
346                 open_handles_array.path[x][0]='\0';
347
348         }
349
350         while(1)
351         {
352
353                 x=rand() % 3;
354                 printf("running test: %d",x);
355                 switch(x){
356                         case 0 :open_random_file(yaffs_test_dir,&open_handles_array);break;
357                         case 3 :write_to_random_file(&open_handles_array);break;
358 //                      case 1 :close_random_file(&open_handles_array);break;
359                         case 2 :truncate_random_file(&open_handles_array);break;
360                 }
361         }
362 }
363 void  generate_random_string(char *ptr,int length_of_str){
364         unsigned int x;
365         unsigned int length=((rand() %length_of_str)+1);        /*creates a int with the number of charecters been between 1 and 51*/           
366         char letter='\0';
367
368         //printf("generating string\n");
369         //printf("string length is %d\n",length);
370         for (x=0; x <= (length-2) &&length>2 ; x++)
371         {
372                 //printf("x=%d\n",x);   
373                 /* keep generating a charecter until the charecter is legal*/
374                 while((letter=='\0' )||(letter=='/')||(letter=='\\')){
375                         letter=(rand() % 126-32)+32;    /*generate a number between 32 and 126 and uses it as a charecter (letter) */
376                 }       
377                 ptr[x]=letter;
378                 //printf("charecter generated is %c\n",ptr[x]);
379         }
380         ptr[x+1]='\0';  /*adds NULL charecter to turn it into a string*/
381         
382 }
383