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