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