d87dff51fc6fc7a8ac1cf2730b24c0f211ce5a03
[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         for (x=0;x<num_of_random_tests;x++){
68                 run_random_test();
69         }
70         compare_linux_and_yaffs();
71         yaffs_unmount("yaffs2");
72         return 0;
73 }
74
75 void init(int argc, char *argv[])
76 {
77         char dir[200];
78         dir[0]='\0';
79         int x=-1;
80         char message[100];
81
82         srand((unsigned)time(NULL));
83         yaffs_set_trace(0);
84         linux_struct.type_of_test =LINUX;
85         yaffs_struct.type_of_test =YAFFS;
86
87         sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
88         print_message(3,message);
89         strcpy(dir,getcwd(dir,200));
90
91         strcat(dir,"/test/");
92         printf("dir: %s\n",dir);
93         strcpy(linux_struct.root_path,dir);
94         strcpy(yaffs_struct.root_path,"yaffs2/test/");  
95
96
97         for (x=0;x<argc;x++){
98                 if (strcmp(argv[x],"-h")==0){
99                         printf("mirror_tests help\n");
100                         printf("arguments:\n");
101                         printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
102                         printf("\t-linux_path [PATH] //sets the path for linux.\n");
103                         printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
104                         printf("\t-v //verbose mode everything is printed\n");
105                         printf("\t-q //quiet mode nothing is printed.\n");
106                         printf("\t-n [number] //sets the number of random tests to run.\n");
107                         printf("\t-s [number] //seeds rand with the number\n");
108                         printf("\t-t [number] //sets yaffs_trace to the number\n");
109                         printf("\t-c //removes emfile and test dir\n");
110                         exit(0);
111                 } else if (strcmp(argv[x],"-yaffs_path")==0){
112                         strcpy(yaffs_struct.root_path, argv[x+1]);
113                 } else if (strcmp(argv[x],"-linux_path")==0){
114                         strcpy(linux_struct.root_path, argv[x+1]);
115                 } else if (strcmp(argv[x],"-p")==0){
116                         set_print_level(atoi(argv[x+1]));
117                 } else if (strcmp(argv[x],"-v")==0){
118                         set_print_level(5);
119                 } else if (strcmp(argv[x],"-q")==0){
120                         set_print_level(-1);
121                 } else if (strcmp(argv[x],"-n")==0){
122                         num_of_random_tests=atoi(argv[x+1]);
123                 } else if (strcmp(argv[x],"-s")==0){
124                         srand(atoi(argv[x+1]));
125                 } else if (strcmp(argv[x],"-t")==0){
126                         yaffs_set_trace(atoi(argv[x+1]));
127                 } else if (strcmp(argv[x],"-c")==0){
128                         clean_dir();
129                         exit(0);
130                 }
131         }
132         clean_dir();
133         yaffs_start_up();
134         print_message(2,"\nmounting yaffs\n");
135         x=yaffs_mount("yaffs2");
136         if (x<0) {
137                 print_message(3,"failed to mount yaffs\n");
138                 get_error_yaffs();
139         } else {
140                 print_message(3,"mounted yaffs\n");
141         }
142
143
144         print_message(3,"\nmaking linux test dir\n");
145         x=mkdir(linux_struct.root_path,0777);
146         if (x<0) {
147                 print_message(1,"failed to make dir\n");
148                 get_error_linux();
149         } else {
150                 print_message(3,"made dir\n");
151         }
152
153         print_message(3,"\nmaking yaffs test dir\n");
154         x=yaffs_mkdir(yaffs_struct.root_path,0777);
155         if (x<0) {
156                 print_message(1,"failed to make dir\n");
157                 get_error_yaffs();
158         } else {
159                 print_message(3,"made dir\n");
160         }
161 }
162
163 int run_random_test(void)
164 {
165         int x=-1;
166         int id=0;
167         int test_id=-1;
168         int num_of_tests=1;
169         char message[100];
170         arg_temp args_struct;
171         for (x=0;x<num_of_tests;x++) {
172                 errno=0;
173                 yaffs_set_error(0);
174                 test_id = select_test_id(yaffs_tests.num_of_tests);
175                 sprintf(message,"running test_id %d\n",test_id);
176                 print_message(3,message);
177                 generate_random_numbers(&args_struct);
178                 run_yaffs_test(test_id, &args_struct);
179                 run_linux_test(test_id, &args_struct);
180                 if      ((abs(yaffs_get_error())!=abs(errno)) &&
181                         (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
182                         (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)
183                         ){
184                         print_message(2,"\ndifference in returned errors######################################\n");
185                         get_error_yaffs();
186                         get_error_linux();
187                         if (get_exit_on_error()){ 
188                                 exit(0);
189                         }
190                 }
191         }
192         compare_linux_and_yaffs(); 
193 }
194
195 int select_test_id(int test_len)
196 {
197         int id=0;
198         //printf("test_len = %d\n",test_len);
199         id=(rand() % test_len );
200         //printf("id %d\n",id);
201         return id;
202
203 }
204
205 int compare_linux_and_yaffs(void)
206 {
207         int x=0;
208         yaffs_DIR *yaffs_open_dir;
209         yaffs_dirent *yaffs_current_file;
210         
211         DIR *linux_open_dir;
212         struct dirent *linux_current_file;
213
214         yaffs_open_dir = yaffs_opendir(yaffs_struct.root_path);
215         
216         for (x=0;NULL!=yaffs_readdir(yaffs_open_dir);x++){};
217         printf("number of files in yaffs dir= %d\n",x);
218         
219         char yaffs_file_list[x][100];
220         yaffs_rewinddir(yaffs_open_dir);
221
222         for (x=0 ;NULL!=yaffs_current_file;x++)
223         {
224                 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
225                 if (NULL!=yaffs_current_file){
226                         strcpy(yaffs_file_list[x],yaffs_current_file->d_name);
227                 }
228         }
229
230         linux_open_dir = opendir(linux_struct.root_path);
231
232         for (x=0;NULL!=readdir(linux_open_dir);x++){};
233         printf("number of files in linux dir= %d\n",(x-2));     //the -2 is because linux shows 2 extra files which are automaticly created. 
234         
235         //printf("file_name %s\n", yaffs_current_file->d_name);
236 //      generate_array_of_objects_in_yaffs(); 
237 //      generate_array_of_objects_in_linux();
238         //first do a check to see if both sides have the same objects on both sides. 
239         //then stat all of the files and compare size and mode
240         //read the text of each file and compare them.
241         
242         //show the diffrences by printing them. 
243
244 }
245
246 void generate_random_numbers(arg_temp *args_struct)
247 {
248         char string[51];
249         args_struct->char1= (rand() % 255);
250         args_struct->char2= (rand() % 255);
251         args_struct->int1= (rand() % 100000);
252         args_struct->int2= (rand() % 100000);
253         generate_random_string(string,50);
254         strcpy(args_struct->string1, string);
255         generate_random_string(string,50);
256         strcpy(args_struct->string2, string);
257 }
258
259 void run_yaffs_test(int id,arg_temp *args_struct)
260 {
261         char message[200];
262         int output =0;
263         print_message(3,"\n");
264         //printf("id = %d\n",id);
265         sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
266         print_message(3,message);
267         output=yaffs_tests.test_list[id].test_pointer(args_struct);
268         if (output<0) {
269                 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
270                 print_message(3,message);
271         } else {
272                 print_message(3,"test_passed\n");
273         }
274 }
275
276 void run_linux_test(int id,arg_temp *args_struct)
277 {
278         char message[200];
279         int output =0;
280         print_message(3,"\n");
281         //printf("id = %d\n",id);
282         sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
283         print_message(3,message);
284         output=linux_tests.test_list[id].test_pointer(args_struct);
285         if (output<0) {
286                 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
287                 print_message(3,message);
288         } else {
289                 print_message(3,"test_passed\n");
290         }
291 }
292
293 void get_error_yaffs(void)
294 {
295         int error_code=0;
296         char message[30];
297         message[0]='\0';
298
299         error_code=yaffs_get_error();
300         sprintf(message,"yaffs_error code %d\n",error_code);
301         print_message(1,message);
302         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
303         print_message(1,message);
304 }
305
306 void get_error_linux(void)
307 {
308         int error_code=0;
309         char message[30];
310         message[0]='\0';
311
312         error_code=errno;
313         sprintf(message,"linux_error code %d\n",error_code);
314         print_message(1,message);
315         strcpy(message,"error code");
316         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
317         //perror(message);      
318         print_message(1,message);
319 }
320
321 void clean_dir(void)
322 {
323         char string[200]; 
324         char file[200];
325         char message[200];
326         DIR *linux_open_dir;
327         struct dirent *linux_current_file;
328         int x=0;
329         
330         getcwd(string,200);
331         strcat(string,"/emfile-2k-0");
332         sprintf(message,"\n\nunlinking emfile at this path: %s\n",string);
333         print_message(3,message);
334         unlink(string);
335         
336
337         linux_open_dir = opendir(linux_struct.root_path);
338         if (linux_open_dir){
339                 for (x=0 ;NULL!=linux_current_file   ;x++)
340                 {
341                         linux_current_file =readdir(linux_open_dir);
342                         if (NULL!=linux_current_file){
343                                 
344                                 strcpy(file,linux_struct.root_path);
345                                 strcat(file,linux_current_file->d_name);
346                                 sprintf(message,"unlinking file %d\n",linux_current_file->d_name);
347                                 print_message(3,message);
348                                 unlink(file);
349                         }
350                 }
351                 unlink(linux_struct.root_path);
352         }
353         
354 }