Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[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         2,
35         {{"yaffs_test_open",yaffs_test_open},
36         {"yaffs_test_truncate",yaffs_test_truncate}
37         }
38 };
39
40 test_temp linux_tests={
41         2,
42         {{"linux_test_open",linux_test_open},
43         {"linux_test_truncate",linux_test_truncate}
44         }
45 };
46
47
48 int main(int argc, char *argv[])
49 {
50         char message[100];
51         int x;
52 //      yaffs_tests.num_of_tests=(sizeof(yaffs_tests)/sizeof(test_temp));
53 //      linux_tests.num_of_tests=(sizeof(linux_tests)/sizeof(test_temp));
54
55         init(argc,argv);
56         print_message(1,"running mirror_tests\n");
57         sprintf(message,"linux_root_path: %s\n",linux_struct.root_path);
58         print_message(3,message);
59         sprintf(message,"yaffs_root_path: %s\n",yaffs_struct.root_path);
60         print_message(3,message);
61         sprintf(message,"linux_num_of_tests: %d\n",linux_tests.num_of_tests);
62         print_message(3,message);
63         sprintf(message,"yaffs_num_of_tests: %d\n",yaffs_tests.num_of_tests);
64         print_message(3,message);
65         for (x=0;x<num_of_random_tests;x++){
66                 run_random_test();
67         }
68         yaffs_unmount("yaffs2");
69         return 0;
70 }
71
72 void init(int argc, char *argv[])
73 {
74         char dir[200];
75         dir[0]='\0';
76         int x=-1;
77         char message[100];
78         
79         srand((unsigned)time(NULL));
80         linux_struct.type_of_test =LINUX;
81         yaffs_struct.type_of_test =YAFFS;
82
83         sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
84         print_message(3,message);
85         strcpy(dir,getcwd(dir,200));
86
87         strcat(dir,"/test/");
88         printf("dir: %s\n",dir);
89         strcpy(linux_struct.root_path,dir);
90         strcpy(yaffs_struct.root_path,"yaffs2/test/");  
91
92
93         for (x=0;x<argc;x++){
94                 if (strcmp(argv[x],"-h")==0){
95                         printf("mirror_tests help\n");
96                         printf("arguments:\n");
97                         printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
98                         printf("\t-linux_path [PATH] //sets the path for linux.\n");
99                         printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
100                         printf("\t-v //verbose mode everything is printed\n");
101                         printf("\t-q //quiet mode nothing is printed.\n");
102                         printf("\t-n [number] //sets the number of random tests to run.\n");
103                         printf("\t-s [number] //seeds rand with the number\n");
104                         exit(0);
105                 } else if (strcmp(argv[x],"-yaffs_path")==0){
106                         strcpy(yaffs_struct.root_path, argv[x+1]);
107                 } else if (strcmp(argv[x],"-linux_path")==0){
108                         strcpy(linux_struct.root_path, argv[x+1]);
109                 } else if (strcmp(argv[x],"-p")==0){
110                         set_print_level(atoi(argv[x+1]));
111                 } else if (strcmp(argv[x],"-v")==0){
112                         set_print_level(5);
113                 } else if (strcmp(argv[x],"-q")==0){
114                         set_print_level(-1);
115                 } else if (strcmp(argv[x],"-n")==0){
116                         num_of_random_tests=atoi(argv[x+1]);
117                 } else if (strcmp(argv[x],"-n")==0){
118                         //srand(atoi(argv[x+1]));
119                 }
120         }
121
122         yaffs_start_up();
123         print_message(message,"\nmounting yaffs\n");
124         x=yaffs_mount("yaffs2");
125         if (x<0) {
126                 print_message(3,"failed to mount yaffs\n");
127                 get_error_yaffs();
128         } else {
129                 print_message(3,"mounted yaffs\n");
130         }
131
132
133         print_message(3,"\nmaking linux test dir\n");
134         x=mkdir(linux_struct.root_path,0777);
135         if (x<0) {
136                 print_message(1,"failed to make dir\n");
137                 get_error_linux();
138         } else {
139                 print_message(3,"made dir\n");
140         }
141
142         print_message(3,"\nmaking yaffs test dir\n");
143         x=yaffs_mkdir(yaffs_struct.root_path,0777);
144         if (x<0) {
145                 print_message(1,"failed to make dir\n");
146                 get_error_yaffs();
147         } else {
148                 print_message(3,"made dir\n");
149         }
150 }
151
152 int run_random_test(void)
153 {
154         int x=-1;
155         int id=0;
156         int test_id=-1;
157         int num_of_tests=1;
158         char message[100];
159         arg_temp args_struct;
160         for (x=0;x<num_of_tests;x++) {
161                 errno=0;
162                 yaffs_set_error(0);
163                 test_id = select_test_id(yaffs_tests.num_of_tests);
164                 sprintf(message,"running test_id %d\n",test_id);
165                 print_message(3,message);
166                 generate_random_numbers(&args_struct);
167                 run_yaffs_test(test_id, &args_struct);
168                 run_linux_test(test_id, &args_struct);
169                 if      ((abs(yaffs_get_error())!=abs(errno)) &&
170                         (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
171                         (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)
172                         ){
173                         print_message(2,"\ndiffrence in returned errors######################################\n");
174                         get_error_yaffs();
175                         get_error_linux();
176                         if (get_exit_on_error()){ 
177                                 exit(0);
178                         }
179                 }
180         }
181         compare_linux_and_yaffs(); 
182 }
183
184 int select_test_id(int test_len)
185 {
186         int id=0;
187         //printf("test_len = %d\n",test_len);
188         id=(rand() % test_len );
189         //printf("id %d\n",id);
190         return id;
191
192 }
193
194 int compare_linux_and_yaffs(void)
195 {
196 //      generate_array_of_objects_in_yaffs(); 
197 //      generate_array_of_objects_in_linux();
198         //first do a check to see if both sides have the same objects on both sides. 
199         //then stat all of the files and compare size and mode
200         //read the text of each file and compare them.
201         
202         //show the diffrences by printing them. 
203
204 }
205
206 void generate_random_numbers(arg_temp *args_struct)
207 {
208         char string[51];
209         args_struct->char1= (rand() % 255);
210         args_struct->char2= (rand() % 255);
211         args_struct->int1= (rand() % 100000);
212         args_struct->int2= (rand() % 100000);
213         generate_random_string(string,50);
214         strcpy(args_struct->string1, string);
215         generate_random_string(string,50);
216         strcpy(args_struct->string2, string);
217 }
218
219 void run_yaffs_test(int id,arg_temp *args_struct)
220 {
221         char message[200];
222         int output =0;
223         print_message(3,"\n");
224         //printf("id = %d\n",id);
225         sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
226         print_message(3,message);
227         output=yaffs_tests.test_list[id].test_pointer(args_struct);
228         if (output<0) {
229                 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
230                 print_message(3,message);
231         } else {
232                 print_message(3,"test_passed\n");
233         }
234 }
235
236 void run_linux_test(int id,arg_temp *args_struct)
237 {
238         char message[200];
239         int output =0;
240         print_message(3,"\n");
241         //printf("id = %d\n",id);
242         sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
243         print_message(3,message);
244         output=linux_tests.test_list[id].test_pointer(args_struct);
245         if (output<0) {
246                 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
247                 print_message(3,message);
248         } else {
249                 print_message(3,"test_passed\n");
250         }
251 }
252
253 void get_error_yaffs(void)
254 {
255         int error_code=0;
256         char message[30];
257         message[0]='\0';
258
259         error_code=yaffs_get_error();
260         sprintf(message,"yaffs_error code %d\n",error_code);
261         print_message(1,message);
262         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
263         print_message(1,message);
264 }
265
266 void get_error_linux(void)
267 {
268         int error_code=0;
269         char message[30];
270         message[0]='\0';
271
272         error_code=errno;
273         sprintf(message,"linux_error code %d\n",error_code);
274         print_message(1,message);
275         strcpy(message,"error code");
276         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
277         //perror(message);      
278         print_message(1,message);
279 }
280