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