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