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