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