Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[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         4,
35         {{"yaffs_test_open",yaffs_test_open},
36         {"yaffs_test_truncate",yaffs_test_truncate},
37         {"yaffs_test_unlink",yaffs_test_unlink},
38         {"yaffs_test_write",yaffs_test_write}
39         }
40 };
41
42 test_temp linux_tests={
43         4,
44         {{"linux_test_open",linux_test_open},
45         {"linux_test_truncate",linux_test_truncate},
46         {"linux_test_unlink",linux_test_unlink},
47         {"linux_test_write",linux_test_write}
48         }
49 };
50
51
52 int main(int argc, char *argv[])
53 {
54         char message[100];
55         int x;
56 //      yaffs_tests.num_of_tests=(sizeof(yaffs_tests)/sizeof(test_temp));
57 //      linux_tests.num_of_tests=(sizeof(linux_tests)/sizeof(test_temp));
58
59         init(argc,argv);
60         print_message(1,"running mirror_tests\n");
61         sprintf(message,"linux_root_path: %s\n",linux_struct.root_path);
62         print_message(3,message);
63         sprintf(message,"yaffs_root_path: %s\n",yaffs_struct.root_path);
64         print_message(3,message);
65         sprintf(message,"linux_num_of_tests: %d\n",linux_tests.num_of_tests);
66         print_message(3,message);
67         sprintf(message,"yaffs_num_of_tests: %d\n",yaffs_tests.num_of_tests);
68         print_message(3,message);
69
70         run_random_test(num_of_random_tests);
71         //compare_linux_and_yaffs();
72         yaffs_unmount("yaffs2");
73         return 0;
74 }
75
76 void init(int argc, char *argv[])
77 {
78         char dir[200];
79         dir[0]='\0';
80         int x=-1;
81         char message[100];
82
83         x=(unsigned)time(NULL);
84         sprintf(message,"seeding srand with: %d\n",x);
85         print_message(2,message);
86         srand(x);
87         yaffs_set_trace(0);
88         linux_struct.type_of_test =LINUX;
89         yaffs_struct.type_of_test =YAFFS;
90
91         sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
92         print_message(3,message);
93         strcpy(dir,getcwd(dir,200));
94
95         strcat(dir,"/test/");
96         printf("dir: %s\n",dir);
97         strcpy(linux_struct.root_path,dir);
98         strcpy(yaffs_struct.root_path,"yaffs2/test/");  
99
100
101         for (x=0;x<argc;x++){
102                 if (strcmp(argv[x],"-h")==0){
103                         printf("mirror_tests help\n");
104                         printf("arguments:\n");
105                         printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
106                         printf("\t-linux_path [PATH] //sets the path for linux.\n");
107                         printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
108                         printf("\t-v //verbose mode everything is printed\n");
109                         printf("\t-q //quiet mode nothing is printed.\n");
110                         printf("\t-n [number] //sets the number of random tests to run.\n");
111                         printf("\t-s [number] //seeds rand with the number\n");
112                         printf("\t-t [number] //sets yaffs_trace to the number\n");
113                         printf("\t-clean //removes emfile and test dir\n");
114                         exit(0);
115                 } else if (strcmp(argv[x],"-yaffs_path")==0){
116                         strcpy(yaffs_struct.root_path, argv[x+1]);
117                 } else if (strcmp(argv[x],"-linux_path")==0){
118                         strcpy(linux_struct.root_path, argv[x+1]);
119                 } else if (strcmp(argv[x],"-p")==0){
120                         set_print_level(atoi(argv[x+1]));
121                 } else if (strcmp(argv[x],"-v")==0){
122                         set_print_level(5);
123                 } else if (strcmp(argv[x],"-q")==0){
124                         set_print_level(-1);
125                 } else if (strcmp(argv[x],"-n")==0){
126                         num_of_random_tests=atoi(argv[x+1]);
127                 } else if (strcmp(argv[x],"-s")==0){
128                         srand(atoi(argv[x+1]));
129                 } else if (strcmp(argv[x],"-t")==0){
130                         yaffs_set_trace(atoi(argv[x+1]));
131                 } else if (strcmp(argv[x],"-clean")==0){
132                         clean_dir();
133                         exit(0);
134                 }
135         }
136         clean_dir();
137         yaffs_start_up();
138         print_message(2,"\nmounting yaffs\n");
139         x=yaffs_mount("yaffs2");
140         if (x<0) {
141                 print_message(3,"failed to mount yaffs\n");
142                 get_error_yaffs();
143         } else {
144                 print_message(3,"mounted yaffs\n");
145         }
146
147
148         print_message(3,"\nmaking linux test dir\n");
149         x=mkdir(linux_struct.root_path,0777);
150         if (x<0) {
151                 print_message(1,"failed to make dir\n");
152                 get_error_linux();
153         } else {
154                 print_message(3,"made dir\n");
155         }
156
157         print_message(3,"\nmaking yaffs test dir\n");
158         x=yaffs_mkdir(yaffs_struct.root_path,0777);
159         if (x<0) {
160                 print_message(1,"failed to make dir\n");
161                 get_error_yaffs();
162         } else {
163                 print_message(3,"made dir\n");
164         }
165 }
166
167 int run_random_test(int num_of_random_tests)
168 {
169         int y=0;
170         int x=-1;
171         int id=0;
172         int test_id=-1;
173         int num_of_tests_before_check=1;
174         char message[200];
175         arg_temp args_struct;
176         for (y=0;(y*num_of_tests_before_check)<num_of_random_tests;y++){
177                 for (x=0;x<num_of_tests_before_check && (x+(y*num_of_tests_before_check)<num_of_random_tests);x++) {
178                         errno=0;
179                         yaffs_set_error(0);
180                         test_id = select_test_id(yaffs_tests.num_of_tests);
181                         sprintf(message,"running test_id %d\n",test_id);
182                         print_message(3,message);
183                         generate_random_numbers(&args_struct);
184                         run_yaffs_test(test_id, &args_struct);
185
186                                 //check_mode(&args_struct);
187                         
188                         run_linux_test(test_id, &args_struct);
189
190                         if (get_print_level()>=4){
191                                 get_error_yaffs();
192                                 get_error_linux();
193                         }
194                         
195                         if      ((abs(yaffs_get_error())!=abs(errno)) &&
196                                 (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
197                                 (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)&&
198                                 (abs(yaffs_get_error())!=EINVAL && abs(errno) != EBADF)
199                                 ){
200                                 print_message(2,"\ndifference in returned errors######################################\n");
201                                 get_error_yaffs();
202                                 get_error_linux();
203                                 if (get_exit_on_error()){ 
204                                         exit(0);
205                                 }
206                         }
207                 }
208                 //check_mode(&args_struct);     
209                 compare_linux_and_yaffs(&args_struct);
210                 //check_mode(&args_struct);
211
212         } 
213         compare_linux_and_yaffs(&args_struct);
214 }
215
216 int select_test_id(int test_len)
217 {
218         int id=0;
219         //printf("test_len = %d\n",test_len);
220         id=(rand() % test_len );
221         //printf("id %d\n",id);
222         return id;
223
224 }
225
226 int check_mode(arg_temp *args_struct)
227 {
228         char path[200];
229         char message[200];
230         int output=0;
231
232         struct yaffs_stat yaffs_stat_struct;
233         join_paths(yaffs_struct.root_path,args_struct->string1, path );
234         sprintf(message,"\ntrying to stat to: %s\n",path);
235         print_message(3,message);
236         output=yaffs_stat(path,&yaffs_stat_struct);
237         if (output < 0){
238                 sprintf(message,"failed to stat the file\n");
239                 print_message(3,message);
240                 get_error_yaffs();
241         } else {
242                 sprintf(message,"stated the file\n");
243                 print_message(3,message);
244                 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
245                 print_message(3,message);
246                 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
247                 print_message(3,message);       
248         }
249         return 1;
250 }
251
252 int check_mode_file(char *path)
253 {
254         char message[200];
255         int output=0;
256
257         struct yaffs_stat yaffs_stat_struct;
258
259         sprintf(message,"\ntrying to stat to: %s\n",path);
260         print_message(3,message);
261         output=yaffs_stat(path,&yaffs_stat_struct);
262         if (output < 0){
263                 sprintf(message,"failed to stat the file\n");
264                 print_message(3,message);
265                 get_error_yaffs();
266         } else {
267                 sprintf(message,"stated the file\n");
268                 print_message(3,message);
269                 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
270                 print_message(3,message);
271                 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
272                 print_message(3,message);       
273         }
274         return 1;
275 }
276
277 int compare_linux_and_yaffs(arg_temp *args_struct)
278 {
279         int x=0,y=0;
280         char l_path[200];
281         char y_path[200];
282         char file_name[200];
283         int exit_bool=0;
284         int number_of_files_in_yaffs=0;
285         int number_of_files_in_linux=0;
286         char message[200];
287         char **yaffs_file_list=NULL;
288         char **linux_file_list=NULL;
289
290         struct yaffs_stat yaffs_stat_struct;
291         struct stat linux_stat_struct;
292         yaffs_DIR *yaffs_open_dir;
293         yaffs_dirent *yaffs_current_file;
294         
295         DIR *linux_open_dir;
296         struct dirent *linux_current_file;
297         
298         l_path[0]='\0';
299         y_path[0]='\0';
300         file_name[0]='\0';
301         message[0]='\0';
302         print_message(2,"\n\n\n comparing folders\n");
303 //      check_mode_file("yaffs2/test/YY");
304         //find out the number of files in the directory
305         yaffs_open_dir = yaffs_opendir(yaffs_struct.root_path);
306         if (yaffs_open_dir) {
307                 for (x=0;yaffs_readdir(yaffs_open_dir);x++){}
308                 number_of_files_in_yaffs=x;
309                 sprintf(message,"number of files in yaffs dir= %d\n",number_of_files_in_yaffs);
310                 print_message(2,message);
311                 yaffs_rewinddir(yaffs_open_dir);
312         } else {
313                 print_message(3,"failed to open yaffs test dir\n");
314         }
315         //create array
316         yaffs_file_list= (char **)malloc(x*sizeof(char*));
317         for (x=0;x<number_of_files_in_yaffs;x++) {
318                 yaffs_file_list[x]=malloc(200);
319         }
320
321         //check_mode_file("yaffs2/test/YY");
322         //copy file names into array
323         if (yaffs_open_dir){
324                 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
325                 for (x=0 ;yaffs_current_file;x++)
326                 {
327                         printf("x= %d \n",x);
328                         printf("yaffs_current_file->d_name = %s\n",yaffs_current_file->d_name);
329                         if (yaffs_current_file){
330                                 strcpy(yaffs_file_list[x],yaffs_current_file->d_name);
331
332                         }
333                         yaffs_current_file =yaffs_readdir(yaffs_open_dir);
334                 }
335                 yaffs_closedir(yaffs_open_dir);
336         } else {
337                 print_message(3,"failed to populate yaffs test list\n");
338         }
339
340
341         //find out the number of files in the directory
342         linux_open_dir = opendir(linux_struct.root_path);
343         if (linux_open_dir){
344                 for (x=0;readdir(linux_open_dir);x++){}
345                 number_of_files_in_linux=(x-2);
346                 sprintf(message,"number of files in linux dir= %d\n",(number_of_files_in_linux));
347                 print_message(2,message);
348                 //the -2 is because linux shows 2 extra files which are automaticly created. 
349         
350                 rewinddir(linux_open_dir);
351         } else {
352                 print_message(3,"failed to open linux test dir\n");
353         }
354
355         //create array
356         linux_file_list= (char **)malloc(number_of_files_in_linux*sizeof(char*));
357         
358         for (x=0;x<number_of_files_in_linux;x++) {
359                 linux_file_list[x]=malloc(200);
360         }
361
362         //check_mode_file("yaffs2/test/YY");
363         //copy file names into array
364         if (linux_open_dir){
365                 linux_current_file =readdir(linux_open_dir);
366                 for (x=0, y=0 ;linux_current_file;x++)
367                 {
368
369                         if (linux_current_file){
370                                 strcpy(message,linux_current_file->d_name);
371                                 print_message(7,"opened file: ");
372                                 print_message(7,message);
373                                 print_message(7,"\n");
374                         }
375                         if (linux_current_file && 
376                                 0!=strcmp(message,".")&&
377                                 0!=strcmp(message,"..")){
378                                 strcpy(file_name,linux_current_file->d_name);
379                                 //sprintf("file opened: %s\n",linux_current_file->d_name);
380                                 //print_message(3,message);
381                                 print_message(7,"added file to list\n");
382                                 strcpy(linux_file_list[y],file_name);
383                                 sprintf(message,"file added to list: %s\n",linux_file_list[y]);
384                                 print_message(7,message);
385                                 y++;
386                         }
387                         linux_current_file =readdir(linux_open_dir);
388                 }
389                 closedir(linux_open_dir);
390         } else {
391                 print_message(3,"failed to populate linux test dir\n");
392         }
393         
394
395         //match the files in both folders
396         for (x=0;x<number_of_files_in_yaffs;x++){
397                 sprintf(message,"\nsearching for yaffs file: %s\n",yaffs_file_list[x]);
398                 print_message(3,message);
399                 for (y=0;y<number_of_files_in_linux;y++){
400                         sprintf(message,"comparing to linux file: %s\n",linux_file_list[y]);
401                         print_message(7,message);
402
403                         if (0==strcmp(yaffs_file_list[x],linux_file_list[y])){
404                                 sprintf(message,"file matched: %s\n",linux_file_list[y]);
405                                 print_message(3,message);
406                                 //check that the modes of both files are the same
407                                 join_paths(yaffs_struct.root_path,yaffs_file_list[x],y_path);
408                                 join_paths(linux_struct.root_path,linux_file_list[y],l_path);
409                                 if (yaffs_stat(y_path,&yaffs_stat_struct)>=0&&
410                                 stat(l_path,&linux_stat_struct)>=0){
411                                         sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
412                                         print_message(3,message);
413                                         sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
414                                         print_message(3,message);                               
415                                         sprintf(message," linux file mode is %d\n",(linux_stat_struct.st_mode & (S_IREAD|S_IWRITE)));
416                                         print_message(3,message);
417                                         sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(linux_stat_struct.st_mode & S_IREAD),(linux_stat_struct.st_mode & S_IWRITE));
418                                         print_message(3,message);
419                                         if ((yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE))==
420                                         ( linux_stat_struct.st_mode & (S_IREAD|S_IWRITE))){
421                                                 print_message(2,"file modes match\n");
422                                         } else {
423                                                 print_message(2,"file modes do not match\n");
424                                                 exit_bool=1;
425                                         }
426                                         linux_file_list[y][0]=NULL;
427                                         yaffs_file_list[x][0]=NULL;
428                                 } else {
429                                         print_message(2,"failed to stat one of the files\n");
430                                         get_error_yaffs();
431                                         get_error_linux();
432                                 }
433                                 
434                                 //read file contents
435                                 
436                                 
437                                 break;
438                         }
439                 }
440         }
441
442         //print remaining files
443         for (x=0;x<number_of_files_in_linux;x++){
444                 if (linux_file_list[x][0]){
445                         sprintf(message,"unmatched file in linux: %s\n",linux_file_list[x]);
446                         print_message(2,message);
447                         exit_bool=1;
448                 }
449         }
450         for (x=0;x<number_of_files_in_yaffs;x++){
451                 if (yaffs_file_list[x][0]){
452                         sprintf(message,"unmatched file in yaffs: %s\n",yaffs_file_list[x]);
453                         print_message(2,message);
454                         exit_bool=1;
455                 }
456         }
457         if (exit_bool==1&& get_exit_on_error()==1){
458                 print_message(2,"exiting program\n");
459                 exit(0);
460         }
461
462         for (x=0;x<number_of_files_in_yaffs;x++) {
463                 free(yaffs_file_list[x]);
464         }
465         free(yaffs_file_list);
466
467         for (x=0;x<number_of_files_in_linux;x++) {
468                 free(linux_file_list[x]);
469         }
470         free(linux_file_list);
471
472
473         //printf("file_name %s\n", yaffs_current_file->d_name);
474 //      generate_array_of_objects_in_yaffs(); 
475 //      generate_array_of_objects_in_linux();
476         //first do a check to see if both sides have the same objects on both sides. 
477         //then stat all of the files and compare size and mode
478         //read the text of each file and compare them.
479         
480         //show the diffrences by printing them. 
481         return 1;
482
483 }
484
485 void generate_random_numbers(arg_temp *args_struct)
486 {
487         char string[51];
488         args_struct->char1= (rand() % 255);
489         args_struct->char2= (rand() % 255);
490         args_struct->int1= (rand() % 100000);
491         args_struct->int2= (rand() % 100000);
492         generate_random_string(string,50);
493         strcpy(args_struct->string1, string);
494         generate_random_string(string,50);
495         strcpy(args_struct->string2, string);
496 }
497
498 void run_yaffs_test(int id,arg_temp *args_struct)
499 {
500         char message[200];
501         int output =0;
502         print_message(3,"\n");
503         //printf("id = %d\n",id);
504         sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
505         print_message(3,message);
506         output=yaffs_tests.test_list[id].test_pointer(args_struct);
507         if (output<0) {
508                 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
509                 print_message(3,message);
510         } else {
511                 print_message(3,"test_passed\n");
512         }
513 }
514
515 void run_linux_test(int id,arg_temp *args_struct)
516 {
517         char message[200];
518         int output =0;
519         print_message(3,"\n");
520         //printf("id = %d\n",id);
521         sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
522         print_message(3,message);
523         output=linux_tests.test_list[id].test_pointer(args_struct);
524         if (output<0) {
525                 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
526                 print_message(3,message);
527         } else {
528                 print_message(3,"test_passed\n");
529         }
530 }
531
532
533 void clean_dir(void)
534 {
535         char string[200]; 
536         char file[200];
537         char message[200];
538         DIR *linux_open_dir;
539         struct dirent *linux_current_file;
540         int x=0;
541         
542         getcwd(string,200);
543         strcat(string,"/emfile-2k-0");
544         sprintf(message,"\n\nunlinking emfile at this path: %s\n",string);
545         print_message(3,message);
546         unlink(string);
547         
548
549         linux_open_dir = opendir(linux_struct.root_path);
550         if (linux_open_dir){
551                 for (x=0 ;NULL!=linux_current_file   ;x++)
552                 {
553                         linux_current_file =readdir(linux_open_dir);
554                         if (NULL!=linux_current_file){
555                                 
556                                 strcpy(file,linux_struct.root_path);
557                                 strcat(file,linux_current_file->d_name);
558                                 sprintf(message,"unlinking file %d\n",linux_current_file->d_name);
559                                 print_message(3,message);
560                                 unlink(file);
561                         }
562                 }
563                 closedir(linux_open_dir);
564                 rmdir(linux_struct.root_path);
565         }
566         
567 }