yaffs Fixed some more bugs in direct/timothy_tests/mirror_tests
[yaffs2.git] / direct / timothy_tests / mirror_tests / mirror_tests.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 #include "mirror_tests.h"
15
16 int random_seed;
17 int simulate_power_failure = 0;
18
19 int num_of_random_tests=1;
20
21
22
23 typedef struct test_list_temp2{
24         char *test_name;
25         int (*test_pointer)(arg_temp *args_struct);
26 }test_list_temp;
27
28 typedef struct test_temp2 {
29         int num_of_tests;
30         test_list_temp test_list[];
31 }test_temp;
32
33 test_temp yaffs_tests={
34         3,
35         {{"yaffs_test_open",yaffs_test_open},
36         {"yaffs_test_truncate",yaffs_test_truncate},
37         {"yaffs_test_unlink",yaffs_test_unlink}
38         }
39 };
40
41 test_temp linux_tests={
42         3,
43         {{"linux_test_open",linux_test_open},
44         {"linux_test_truncate",linux_test_truncate},
45         {"linux_test_unlink",linux_test_unlink}
46         }
47 };
48
49
50 int main(int argc, char *argv[])
51 {
52         char message[100];
53         int x;
54 //      yaffs_tests.num_of_tests=(sizeof(yaffs_tests)/sizeof(test_temp));
55 //      linux_tests.num_of_tests=(sizeof(linux_tests)/sizeof(test_temp));
56
57         init(argc,argv);
58         print_message(1,"running mirror_tests\n");
59         sprintf(message,"linux_root_path: %s\n",linux_struct.root_path);
60         print_message(3,message);
61         sprintf(message,"yaffs_root_path: %s\n",yaffs_struct.root_path);
62         print_message(3,message);
63         sprintf(message,"linux_num_of_tests: %d\n",linux_tests.num_of_tests);
64         print_message(3,message);
65         sprintf(message,"yaffs_num_of_tests: %d\n",yaffs_tests.num_of_tests);
66         print_message(3,message);
67
68         run_random_test(num_of_random_tests);
69         compare_linux_and_yaffs();
70         yaffs_unmount("yaffs2");
71         return 0;
72 }
73
74 void init(int argc, char *argv[])
75 {
76         char dir[200];
77         dir[0]='\0';
78         int x=-1;
79         char message[100];
80
81         srand((unsigned)time(NULL));
82         yaffs_set_trace(0);
83         linux_struct.type_of_test =LINUX;
84         yaffs_struct.type_of_test =YAFFS;
85
86         sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
87         print_message(3,message);
88         strcpy(dir,getcwd(dir,200));
89
90         strcat(dir,"/test/");
91         printf("dir: %s\n",dir);
92         strcpy(linux_struct.root_path,dir);
93         strcpy(yaffs_struct.root_path,"yaffs2/test/");  
94
95
96         for (x=0;x<argc;x++){
97                 if (strcmp(argv[x],"-h")==0){
98                         printf("mirror_tests help\n");
99                         printf("arguments:\n");
100                         printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
101                         printf("\t-linux_path [PATH] //sets the path for linux.\n");
102                         printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
103                         printf("\t-v //verbose mode everything is printed\n");
104                         printf("\t-q //quiet mode nothing is printed.\n");
105                         printf("\t-n [number] //sets the number of random tests to run.\n");
106                         printf("\t-s [number] //seeds rand with the number\n");
107                         printf("\t-t [number] //sets yaffs_trace to the number\n");
108                         printf("\t-clean //removes emfile and test dir\n");
109                         exit(0);
110                 } else if (strcmp(argv[x],"-yaffs_path")==0){
111                         strcpy(yaffs_struct.root_path, argv[x+1]);
112                 } else if (strcmp(argv[x],"-linux_path")==0){
113                         strcpy(linux_struct.root_path, argv[x+1]);
114                 } else if (strcmp(argv[x],"-p")==0){
115                         set_print_level(atoi(argv[x+1]));
116                 } else if (strcmp(argv[x],"-v")==0){
117                         set_print_level(5);
118                 } else if (strcmp(argv[x],"-q")==0){
119                         set_print_level(-1);
120                 } else if (strcmp(argv[x],"-n")==0){
121                         num_of_random_tests=atoi(argv[x+1]);
122                 } else if (strcmp(argv[x],"-s")==0){
123                         srand(atoi(argv[x+1]));
124                 } else if (strcmp(argv[x],"-t")==0){
125                         yaffs_set_trace(atoi(argv[x+1]));
126                 } else if (strcmp(argv[x],"-clean")==0){
127                         clean_dir();
128                         exit(0);
129                 }
130         }
131         clean_dir();
132         yaffs_start_up();
133         print_message(2,"\nmounting yaffs\n");
134         x=yaffs_mount("yaffs2");
135         if (x<0) {
136                 print_message(3,"failed to mount yaffs\n");
137                 get_error_yaffs();
138         } else {
139                 print_message(3,"mounted yaffs\n");
140         }
141
142
143         print_message(3,"\nmaking linux test dir\n");
144         x=mkdir(linux_struct.root_path,0777);
145         if (x<0) {
146                 print_message(1,"failed to make dir\n");
147                 get_error_linux();
148         } else {
149                 print_message(3,"made dir\n");
150         }
151
152         print_message(3,"\nmaking yaffs test dir\n");
153         x=yaffs_mkdir(yaffs_struct.root_path,0777);
154         if (x<0) {
155                 print_message(1,"failed to make dir\n");
156                 get_error_yaffs();
157         } else {
158                 print_message(3,"made dir\n");
159         }
160 }
161
162 int run_random_test(int num_of_random_tests)
163 {
164         int y=0;
165         int x=-1;
166         int id=0;
167         int test_id=-1;
168         int num_of_tests_before_check=10;
169         char message[200];
170         arg_temp args_struct;
171         for (y=0;(y*num_of_tests_before_check)<num_of_random_tests;y++){
172                 for (x=0;x<num_of_tests_before_check && (x+(y*num_of_tests_before_check)<num_of_random_tests);x++) {
173                         errno=0;
174                         yaffs_set_error(0);
175                         test_id = select_test_id(yaffs_tests.num_of_tests);
176                         sprintf(message,"running test_id %d\n",test_id);
177                         print_message(3,message);
178                         generate_random_numbers(&args_struct);
179                         run_yaffs_test(test_id, &args_struct);
180                         run_linux_test(test_id, &args_struct);
181                         if      ((abs(yaffs_get_error())!=abs(errno)) &&
182                                 (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
183                                 (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)
184                                 ){
185                                 print_message(2,"\ndifference in returned errors######################################\n");
186                                 get_error_yaffs();
187                                 get_error_linux();
188                                 if (get_exit_on_error()){ 
189                                         exit(0);
190                                 }
191                         }
192                 }       
193                 compare_linux_and_yaffs();
194         } 
195 }
196
197 int select_test_id(int test_len)
198 {
199         int id=0;
200         //printf("test_len = %d\n",test_len);
201         id=(rand() % test_len );
202         //printf("id %d\n",id);
203         return id;
204
205 }
206
207 int compare_linux_and_yaffs(void)
208 {
209         int x=0,y=0;
210         int exit_bool=0;
211         int number_of_files_in_yaffs=0;
212         int number_of_files_in_linux=0;
213         char message[200];
214
215         yaffs_DIR *yaffs_open_dir;
216         yaffs_dirent *yaffs_current_file;
217         
218         DIR *linux_open_dir;
219         struct dirent *linux_current_file;
220         
221         print_message(2,"\n\n comparing folders\n");
222
223
224         yaffs_open_dir = yaffs_opendir(yaffs_struct.root_path);
225         if (yaffs_open_dir) {
226                 for (x=0;NULL!=yaffs_readdir(yaffs_open_dir);x++){}
227                 number_of_files_in_yaffs=x;
228                 sprintf(message,"number of files in yaffs dir= %d\n",number_of_files_in_yaffs);
229                 print_message(2,message);
230                 yaffs_rewinddir(yaffs_open_dir);
231         } else {
232                 print_message(3,"failed to open yaffs test dir\n");
233         }
234         char yaffs_file_list[x][200];
235
236
237         if (yaffs_open_dir){
238         
239                 for (x=0 ;NULL!=yaffs_current_file;x++)
240                 {
241                         yaffs_current_file =yaffs_readdir(yaffs_open_dir);
242                         if (NULL!=yaffs_current_file){
243                                 strcpy(yaffs_file_list[x],yaffs_current_file->d_name);
244                         }
245                 }
246                 yaffs_closedir(yaffs_open_dir);
247         } else {
248                 print_message(3,"failed to populate yaffs test list\n");
249         }
250
251         linux_open_dir = opendir(linux_struct.root_path);
252         if (linux_open_dir){
253                 for (x=0;NULL!=readdir(linux_open_dir);x++){}
254                 number_of_files_in_linux=x-2;
255                 sprintf(message,"number of files in linux dir= %d\n",(number_of_files_in_linux));
256                 print_message(2,message);
257                 //the -2 is because linux shows 2 extra files which are automaticly created. 
258         
259                 rewinddir(linux_open_dir);
260         } else {
261                 print_message(3,"failed to open linux test dir\n");
262         }
263         char linux_file_list[x-2][200];
264         if (linux_open_dir){
265                 for (x=0, y=0 ;NULL!=linux_current_file;x++)
266                 {
267                         linux_current_file =readdir(linux_open_dir);
268                         if (NULL!=linux_current_file){
269                                 strcpy(message,linux_current_file->d_name);
270                                 print_message(3,"opened file: ");
271                                 print_message(3,message);
272                                 print_message(3,"\n");
273                         }
274                         if (NULL!=linux_current_file && 
275                                 0!=strcmp(message,".")&&
276                                 0!=strcmp(message,"..")){
277                         //      strcpy(message,linux_current_file->d_name);
278                                 //sprintf("file opened: %s\n",linux_current_file->d_name);
279                                 //print_message(3,message);
280                                 print_message(3,"added file to list\n");
281                                 strcpy(linux_file_list[y],message);
282                                 y++;
283                                 sprintf(message,"file added to list: %s\n",linux_file_list[x]);
284                                 print_message(3,message);
285                         }
286                 }
287                 closedir(linux_open_dir);
288         } else {
289                 print_message(3,"failed to populate linux test dir\n");
290         }
291         
292
293
294         for (x=0;x<number_of_files_in_yaffs;x++){
295                 sprintf(message,"searching for yaffs file: %s\n",yaffs_file_list[x]);
296                 print_message(3,message);
297                 for (y=0;y<number_of_files_in_linux;y++){
298                         sprintf(message,"comparing to linux file: %s\n",linux_file_list[x]);
299                         print_message(3,message);
300
301                         if (0==strcmp(yaffs_file_list[x],linux_file_list[y])){
302                                 sprintf(message,"file matched: %s\n",linux_file_list[y]);
303                                 print_message(3,message);
304                                 linux_file_list[y][0]=NULL;
305                                 yaffs_file_list[x][0]=NULL;
306                                 break;
307                         }
308                 }
309         }
310
311         //print remaining files
312         for (x=0;x<number_of_files_in_linux;x++){
313                 if (linux_file_list[x][0]!=NULL){
314                         sprintf(message,"unmatched file in linux: %s\n",linux_file_list[x]);
315                         print_message(2,message);
316                         exit_bool=1;
317                 }
318         }
319         for (x=0;x<number_of_files_in_yaffs;x++){
320                 if (yaffs_file_list[x][0]!=NULL){
321                         sprintf(message,"unmatched file in yaffs: %s\n",yaffs_file_list[x]);
322                         print_message(2,message);
323                         exit_bool=1;
324                 }
325         }
326         if (exit_bool==1){
327                 print_message(2,"exiting program\n");
328                 exit(0);
329         }
330
331         //printf("file_name %s\n", yaffs_current_file->d_name);
332 //      generate_array_of_objects_in_yaffs(); 
333 //      generate_array_of_objects_in_linux();
334         //first do a check to see if both sides have the same objects on both sides. 
335         //then stat all of the files and compare size and mode
336         //read the text of each file and compare them.
337         
338         //show the diffrences by printing them. 
339
340 }
341
342 void generate_random_numbers(arg_temp *args_struct)
343 {
344         char string[51];
345         args_struct->char1= (rand() % 255);
346         args_struct->char2= (rand() % 255);
347         args_struct->int1= (rand() % 100000);
348         args_struct->int2= (rand() % 100000);
349         generate_random_string(string,50);
350         strcpy(args_struct->string1, string);
351         generate_random_string(string,50);
352         strcpy(args_struct->string2, string);
353 }
354
355 void run_yaffs_test(int id,arg_temp *args_struct)
356 {
357         char message[200];
358         int output =0;
359         print_message(3,"\n");
360         //printf("id = %d\n",id);
361         sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
362         print_message(3,message);
363         output=yaffs_tests.test_list[id].test_pointer(args_struct);
364         if (output<0) {
365                 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
366                 print_message(3,message);
367         } else {
368                 print_message(3,"test_passed\n");
369         }
370 }
371
372 void run_linux_test(int id,arg_temp *args_struct)
373 {
374         char message[200];
375         int output =0;
376         print_message(3,"\n");
377         //printf("id = %d\n",id);
378         sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
379         print_message(3,message);
380         output=linux_tests.test_list[id].test_pointer(args_struct);
381         if (output<0) {
382                 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
383                 print_message(3,message);
384         } else {
385                 print_message(3,"test_passed\n");
386         }
387 }
388
389 void get_error_yaffs(void)
390 {
391         int error_code=0;
392         char message[30];
393         message[0]='\0';
394
395         error_code=yaffs_get_error();
396         sprintf(message,"yaffs_error code %d\n",error_code);
397         print_message(1,message);
398         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
399         print_message(1,message);
400 }
401
402 void get_error_linux(void)
403 {
404         int error_code=0;
405         char message[30];
406         message[0]='\0';
407
408         error_code=errno;
409         sprintf(message,"linux_error code %d\n",error_code);
410         print_message(1,message);
411         strcpy(message,"error code");
412         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
413         //perror(message);      
414         print_message(1,message);
415 }
416
417 void clean_dir(void)
418 {
419         char string[200]; 
420         char file[200];
421         char message[200];
422         DIR *linux_open_dir;
423         struct dirent *linux_current_file;
424         int x=0;
425         
426         getcwd(string,200);
427         strcat(string,"/emfile-2k-0");
428         sprintf(message,"\n\nunlinking emfile at this path: %s\n",string);
429         print_message(3,message);
430         unlink(string);
431         
432
433         linux_open_dir = opendir(linux_struct.root_path);
434         if (linux_open_dir){
435                 for (x=0 ;NULL!=linux_current_file   ;x++)
436                 {
437                         linux_current_file =readdir(linux_open_dir);
438                         if (NULL!=linux_current_file){
439                                 
440                                 strcpy(file,linux_struct.root_path);
441                                 strcat(file,linux_current_file->d_name);
442                                 sprintf(message,"unlinking file %d\n",linux_current_file->d_name);
443                                 print_message(3,message);
444                                 unlink(file);
445                         }
446                 }
447                 closedir(linux_open_dir);
448                 rmdir(linux_struct.root_path);
449         }
450         
451 }