2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Timothy Manning <timothy@yaffs.net>
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.
14 #include "mirror_tests.h"
17 int simulate_power_failure = 0;
19 int num_of_random_tests=1;
23 typedef struct test_list_temp2{
25 int (*test_pointer)(arg_temp *args_struct);
28 typedef struct test_temp2 {
30 test_list_temp test_list[];
33 test_temp yaffs_tests={
35 {{"yaffs_test_open",yaffs_test_open},
36 {"yaffs_test_truncate",yaffs_test_truncate},
37 {"yaffs_test_unlink",yaffs_test_unlink},
38 {"yaffs_test_write",yaffs_test_write}
42 test_temp linux_tests={
44 {{"linux_test_open",linux_test_open},
45 {"linux_test_truncate",linux_test_truncate},
46 {"linux_test_unlink",linux_test_unlink},
47 {"linux_test_write",linux_test_write}
52 int main(int argc, char *argv[])
56 // yaffs_tests.num_of_tests=(sizeof(yaffs_tests)/sizeof(test_temp));
57 // linux_tests.num_of_tests=(sizeof(linux_tests)/sizeof(test_temp));
60 print_message(1,"running mirror_tests\n");
61 sprintf(message,"linux_root_path: %s\n",linux_struct.root_path);
62 print_message(3,message);
63 sprintf(message,"yaffs_root_path: %s\n",yaffs_struct.root_path);
64 print_message(3,message);
65 sprintf(message,"linux_num_of_tests: %d\n",linux_tests.num_of_tests);
66 print_message(3,message);
67 sprintf(message,"yaffs_num_of_tests: %d\n",yaffs_tests.num_of_tests);
68 print_message(3,message);
70 run_random_test(num_of_random_tests);
71 //compare_linux_and_yaffs();
72 yaffs_unmount("yaffs2");
76 void init(int argc, char *argv[])
83 x=(unsigned)time(NULL);
84 sprintf(message,"seeding srand with: %d\n",x);
85 print_message(2,message);
88 linux_struct.type_of_test =LINUX;
89 yaffs_struct.type_of_test =YAFFS;
91 sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
92 print_message(3,message);
93 strcpy(dir,getcwd(dir,200));
96 printf("dir: %s\n",dir);
97 strcpy(linux_struct.root_path,dir);
98 strcpy(yaffs_struct.root_path,"yaffs2/test/");
101 for (x=0;x<argc;x++){
102 if (strcmp(argv[x],"-h")==0){
103 printf("mirror_tests help\n");
104 printf("arguments:\n");
105 printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
106 printf("\t-linux_path [PATH] //sets the path for linux.\n");
107 printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
108 printf("\t-v //verbose mode everything is printed\n");
109 printf("\t-q //quiet mode nothing is printed.\n");
110 printf("\t-n [number] //sets the number of random tests to run.\n");
111 printf("\t-s [number] //seeds rand with the number\n");
112 printf("\t-t [number] //sets yaffs_trace to the number\n");
113 printf("\t-clean //removes emfile and test dir\n");
115 } else if (strcmp(argv[x],"-yaffs_path")==0){
116 strcpy(yaffs_struct.root_path, argv[x+1]);
117 } else if (strcmp(argv[x],"-linux_path")==0){
118 strcpy(linux_struct.root_path, argv[x+1]);
119 } else if (strcmp(argv[x],"-p")==0){
120 set_print_level(atoi(argv[x+1]));
121 } else if (strcmp(argv[x],"-v")==0){
123 } else if (strcmp(argv[x],"-q")==0){
125 } else if (strcmp(argv[x],"-n")==0){
126 num_of_random_tests=atoi(argv[x+1]);
127 } else if (strcmp(argv[x],"-s")==0){
128 srand(atoi(argv[x+1]));
129 } else if (strcmp(argv[x],"-t")==0){
130 yaffs_set_trace(atoi(argv[x+1]));
131 } else if (strcmp(argv[x],"-clean")==0){
138 print_message(2,"\nmounting yaffs\n");
139 x=yaffs_mount("yaffs2");
141 print_message(3,"failed to mount yaffs\n");
144 print_message(3,"mounted yaffs\n");
148 print_message(3,"\nmaking linux test dir\n");
149 x=mkdir(linux_struct.root_path,0777);
151 print_message(1,"failed to make dir\n");
154 print_message(3,"made dir\n");
157 print_message(3,"\nmaking yaffs test dir\n");
158 x=yaffs_mkdir(yaffs_struct.root_path,0777);
160 print_message(1,"failed to make dir\n");
163 print_message(3,"made dir\n");
167 int run_random_test(int num_of_random_tests)
173 int num_of_tests_before_check=1;
175 arg_temp args_struct;
176 for (y=0;(y*num_of_tests_before_check)<num_of_random_tests;y++){
177 for (x=0;x<num_of_tests_before_check && (x+(y*num_of_tests_before_check)<num_of_random_tests);x++) {
180 test_id = select_test_id(yaffs_tests.num_of_tests);
181 sprintf(message,"running test_id %d\n",test_id);
182 print_message(3,message);
183 generate_random_numbers(&args_struct);
184 run_yaffs_test(test_id, &args_struct);
186 //check_mode(&args_struct);
188 run_linux_test(test_id, &args_struct);
190 if (get_print_level()>=4){
195 if ((abs(yaffs_get_error())!=abs(errno)) &&
196 (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
197 (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)&&
198 (abs(yaffs_get_error())!=EINVAL && abs(errno) != EBADF)
200 print_message(2,"\ndifference in returned errors######################################\n");
203 if (get_exit_on_error()){
208 //check_mode(&args_struct);
209 compare_linux_and_yaffs(&args_struct);
210 //check_mode(&args_struct);
213 //compare_linux_and_yaffs(&args_struct);
216 int select_test_id(int test_len)
219 //printf("test_len = %d\n",test_len);
220 id=(rand() % test_len );
221 //printf("id %d\n",id);
226 int check_mode(arg_temp *args_struct)
232 struct yaffs_stat yaffs_stat_struct;
233 join_paths(yaffs_struct.root_path,args_struct->string1, path );
234 sprintf(message,"\ntrying to stat to: %s\n",path);
235 print_message(3,message);
236 output=yaffs_stat(path,&yaffs_stat_struct);
238 sprintf(message,"failed to stat the file\n");
239 print_message(3,message);
242 sprintf(message,"stated the file\n");
243 print_message(3,message);
244 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
245 print_message(3,message);
246 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
247 print_message(3,message);
252 int check_mode_file(char *path)
257 struct yaffs_stat yaffs_stat_struct;
259 sprintf(message,"\ntrying to stat to: %s\n",path);
260 print_message(3,message);
261 output=yaffs_stat(path,&yaffs_stat_struct);
263 sprintf(message,"failed to stat the file\n");
264 print_message(3,message);
267 sprintf(message,"stated the file\n");
268 print_message(3,message);
269 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
270 print_message(3,message);
271 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
272 print_message(3,message);
277 int compare_linux_and_yaffs(arg_temp *args_struct)
284 int number_of_files_in_yaffs=0;
285 int number_of_files_in_linux=0;
288 struct yaffs_stat yaffs_stat_struct;
289 struct stat linux_stat_struct;
290 yaffs_DIR *yaffs_open_dir;
291 yaffs_dirent *yaffs_current_file;
294 struct dirent *linux_current_file;
300 print_message(2,"\n\n\n comparing folders\n");
301 // check_mode_file("yaffs2/test/YY");
302 //find out the number of files in the directory
303 yaffs_open_dir = yaffs_opendir(yaffs_struct.root_path);
304 if (yaffs_open_dir != NULL) {
305 for (x=0;NULL!=yaffs_readdir(yaffs_open_dir);x++){}
306 number_of_files_in_yaffs=x;
307 sprintf(message,"number of files in yaffs dir= %d\n",number_of_files_in_yaffs);
308 print_message(2,message);
309 yaffs_rewinddir(yaffs_open_dir);
311 print_message(3,"failed to open yaffs test dir\n");
314 char yaffs_file_list[x][200];
316 //check_mode_file("yaffs2/test/YY");
317 //copy file names into array
318 if (yaffs_open_dir !=NULL){
319 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
320 for (x=0 ;NULL!=yaffs_current_file;x++)
323 if (NULL!=yaffs_current_file){
324 strcpy(yaffs_file_list[x],yaffs_current_file->d_name);
326 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
328 yaffs_closedir(yaffs_open_dir);
330 print_message(3,"failed to populate yaffs test list\n");
334 //find out the number of files in the directory
335 linux_open_dir = opendir(linux_struct.root_path);
336 if (linux_open_dir!=NULL){
337 for (x=0;NULL!=readdir(linux_open_dir);x++){}
338 number_of_files_in_linux=x-2;
339 sprintf(message,"number of files in linux dir= %d\n",(number_of_files_in_linux));
340 print_message(2,message);
341 //the -2 is because linux shows 2 extra files which are automaticly created.
343 rewinddir(linux_open_dir);
345 print_message(3,"failed to open linux test dir\n");
349 char linux_file_list[x-2][200];
350 //check_mode_file("yaffs2/test/YY");
351 //copy file names into array
352 if (linux_open_dir!=NULL){
353 linux_current_file =readdir(linux_open_dir);
354 for (x=0, y=0 ;NULL!=linux_current_file;x++)
357 if (NULL!=linux_current_file){
358 strcpy(message,linux_current_file->d_name);
359 print_message(7,"opened file: ");
360 print_message(7,message);
361 print_message(7,"\n");
363 if (NULL!=linux_current_file &&
364 0!=strcmp(message,".")&&
365 0!=strcmp(message,"..")){
366 strcpy(file_name,linux_current_file->d_name);
367 //sprintf("file opened: %s\n",linux_current_file->d_name);
368 //print_message(3,message);
369 print_message(7,"added file to list\n");
370 strcpy(linux_file_list[y],file_name);
372 sprintf(message,"file added to list: %s\n",linux_file_list[x]);
373 print_message(7,message);
375 linux_current_file =readdir(linux_open_dir);
377 closedir(linux_open_dir);
379 print_message(3,"failed to populate linux test dir\n");
383 //match the files in both folders
384 for (x=0;x<number_of_files_in_yaffs;x++){
385 sprintf(message,"\nsearching for yaffs file: %s\n",yaffs_file_list[x]);
386 print_message(3,message);
387 for (y=0;y<number_of_files_in_linux;y++){
388 sprintf(message,"comparing to linux file: %s\n",linux_file_list[y]);
389 print_message(7,message);
391 if (0==strcmp(yaffs_file_list[x],linux_file_list[y])){
392 sprintf(message,"file matched: %s\n",linux_file_list[y]);
393 print_message(3,message);
394 //check that the modes of both files are the same
395 join_paths(yaffs_struct.root_path,yaffs_file_list[x],y_path);
396 join_paths(linux_struct.root_path,linux_file_list[y],l_path);
397 if (yaffs_stat(y_path,&yaffs_stat_struct)>=0&&
398 stat(l_path,&linux_stat_struct)>=0){
399 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
400 print_message(3,message);
401 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
402 print_message(3,message);
403 sprintf(message," linux file mode is %d\n",(linux_stat_struct.st_mode & (S_IREAD|S_IWRITE)));
404 print_message(3,message);
405 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(linux_stat_struct.st_mode & S_IREAD),(linux_stat_struct.st_mode & S_IWRITE));
406 print_message(3,message);
407 if ((yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE))==
408 ( linux_stat_struct.st_mode & (S_IREAD|S_IWRITE))){
409 print_message(2,"file modes match\n");
411 print_message(2,"file modes do not match\n");
414 linux_file_list[y][0]=NULL;
415 yaffs_file_list[x][0]=NULL;
417 print_message(2,"failed to stat one of the files\n");
430 //print remaining files
431 for (x=0;x<number_of_files_in_linux;x++){
432 if (linux_file_list[x][0]!=NULL){
433 sprintf(message,"unmatched file in linux: %s\n",linux_file_list[x]);
434 print_message(2,message);
438 for (x=0;x<number_of_files_in_yaffs;x++){
439 if (yaffs_file_list[x][0]!=NULL){
440 sprintf(message,"unmatched file in yaffs: %s\n",yaffs_file_list[x]);
441 print_message(2,message);
445 if (exit_bool==1&& get_exit_on_error()==1){
446 print_message(2,"exiting program\n");
449 //printf("file_name %s\n", yaffs_current_file->d_name);
450 // generate_array_of_objects_in_yaffs();
451 // generate_array_of_objects_in_linux();
452 //first do a check to see if both sides have the same objects on both sides.
453 //then stat all of the files and compare size and mode
454 //read the text of each file and compare them.
456 //show the diffrences by printing them.
460 void generate_random_numbers(arg_temp *args_struct)
463 args_struct->char1= (rand() % 255);
464 args_struct->char2= (rand() % 255);
465 args_struct->int1= (rand() % 100000);
466 args_struct->int2= (rand() % 100000);
467 generate_random_string(string,50);
468 strcpy(args_struct->string1, string);
469 generate_random_string(string,50);
470 strcpy(args_struct->string2, string);
473 void run_yaffs_test(int id,arg_temp *args_struct)
477 print_message(3,"\n");
478 //printf("id = %d\n",id);
479 sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
480 print_message(3,message);
481 output=yaffs_tests.test_list[id].test_pointer(args_struct);
483 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
484 print_message(3,message);
486 print_message(3,"test_passed\n");
490 void run_linux_test(int id,arg_temp *args_struct)
494 print_message(3,"\n");
495 //printf("id = %d\n",id);
496 sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
497 print_message(3,message);
498 output=linux_tests.test_list[id].test_pointer(args_struct);
500 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
501 print_message(3,message);
503 print_message(3,"test_passed\n");
514 struct dirent *linux_current_file;
518 strcat(string,"/emfile-2k-0");
519 sprintf(message,"\n\nunlinking emfile at this path: %s\n",string);
520 print_message(3,message);
524 linux_open_dir = opendir(linux_struct.root_path);
526 for (x=0 ;NULL!=linux_current_file ;x++)
528 linux_current_file =readdir(linux_open_dir);
529 if (NULL!=linux_current_file){
531 strcpy(file,linux_struct.root_path);
532 strcat(file,linux_current_file->d_name);
533 sprintf(message,"unlinking file %d\n",linux_current_file->d_name);
534 print_message(3,message);
538 closedir(linux_open_dir);
539 rmdir(linux_struct.root_path);