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}
51 const struct option long_options[]={
53 {"yaffs_path", 1,NULL,'y'},
54 {"linux_path", 1,NULL,'l'},
55 {"print_level", 1,NULL,'p'},
56 {"quiet", 0,NULL,'q'},
57 {"number", 1,NULL,'n'},
59 {"trace", 1,NULL,'t'},
60 {"clean", 0,NULL,'c'},
61 {"verbose", 0,NULL,'v'}
64 const char short_options[]="hy:l:p:qn:s:t:cv";
67 int main(int argc, char *argv[])
71 // yaffs_tests.num_of_tests=(sizeof(yaffs_tests)/sizeof(test_temp));
72 // linux_tests.num_of_tests=(sizeof(linux_tests)/sizeof(test_temp));
75 print_message(1,"running mirror_tests\n");
76 sprintf(message,"linux_root_path: %s\n",linux_struct.root_path);
77 print_message(3,message);
78 sprintf(message,"yaffs_root_path: %s\n",yaffs_struct.root_path);
79 print_message(3,message);
80 sprintf(message,"linux_num_of_tests: %d\n",linux_tests.num_of_tests);
81 print_message(3,message);
82 sprintf(message,"yaffs_num_of_tests: %d\n",yaffs_tests.num_of_tests);
83 print_message(3,message);
85 run_random_test(num_of_random_tests);
86 //compare_linux_and_yaffs();
87 yaffs_unmount("yaffs2");
91 void init(int argc, char *argv[])
99 x=(unsigned)time(NULL);
100 sprintf(message,"seeding srand with: %d\n",x);
101 print_message(2,message);
104 linux_struct.type_of_test =LINUX;
105 yaffs_struct.type_of_test =YAFFS;
107 sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
108 print_message(3,message);
109 strcpy(dir,getcwd(dir,200));
111 strcat(dir,"/test/");
112 printf("dir: %s\n",dir);
113 strcpy(linux_struct.root_path,dir);
114 strcpy(yaffs_struct.root_path,"yaffs2/test/");
118 new_option=getopt_long(argc,argv,short_options,long_options,NULL);
119 if (new_option=='h'){
120 printf("mirror_tests help\n");
121 printf("arguments:\n");
122 printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
123 printf("\t-linux_path [PATH] //sets the path for linux.\n");
124 printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
125 printf("\t-v //verbose mode everything is printed\n");
126 printf("\t-q //quiet mode nothing is printed.\n");
127 printf("\t-n [number] //sets the number of random tests to run.\n");
128 printf("\t-s [number] //seeds rand with the number\n");
129 printf("\t-t [number] //sets yaffs_trace to the number\n");
130 printf("\t-clean //removes emfile and test dir\n");
132 } else if (new_option=='y'){
133 strcpy(yaffs_struct.root_path, optarg);
134 } else if (new_option=='l'){
135 strcpy(linux_struct.root_path, optarg);
136 } else if (new_option=='p'){
137 set_print_level(atoi(optarg));
138 } else if (new_option=='v'){
140 } else if (new_option=='q'){
142 } else if (new_option=='n'){
143 num_of_random_tests=atoi(optarg);
144 } else if (new_option=='s'){
145 srand(atoi(argv[x+1]));
146 } else if (new_option=='t'){
147 yaffs_set_trace(atoi(optarg));
148 } else if (new_option=='c'){
151 } else if (new_option==-1){
153 } else if (new_option=='?') {
154 printf("bad argument\n");
158 } while(new_option!=-1);
161 print_message(2,"\nmounting yaffs\n");
162 x=yaffs_mount("yaffs2");
164 print_message(3,"failed to mount yaffs\n");
167 print_message(3,"mounted yaffs\n");
171 print_message(3,"\nmaking linux test dir\n");
172 x=mkdir(linux_struct.root_path,0777);
174 print_message(1,"failed to make dir\n");
177 print_message(3,"made dir\n");
180 print_message(3,"\nmaking yaffs test dir\n");
181 x=yaffs_mkdir(yaffs_struct.root_path,0777);
183 print_message(1,"failed to make dir\n");
186 print_message(3,"made dir\n");
190 int run_random_test(int num_of_random_tests)
196 int num_of_tests_before_check=1;
198 arg_temp args_struct;
199 for (y=0;(y*num_of_tests_before_check)<num_of_random_tests;y++){
200 for (x=0;x<num_of_tests_before_check && (x+(y*num_of_tests_before_check)<num_of_random_tests);x++) {
203 test_id = select_test_id(yaffs_tests.num_of_tests);
204 sprintf(message,"running test_id %d\n",test_id);
205 print_message(3,message);
206 generate_random_numbers(&args_struct);
207 run_yaffs_test(test_id, &args_struct);
209 //check_mode(&args_struct);
211 run_linux_test(test_id, &args_struct);
213 if (get_print_level()>=4){
218 if ((abs(yaffs_get_error())!=abs(errno)) &&
219 (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
220 (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)&&
221 (abs(yaffs_get_error())!=EINVAL && abs(errno) != EBADF)
223 print_message(2,"\ndifference in returned errors######################################\n");
226 if (get_exit_on_error()){
231 //check_mode(&args_struct);
232 compare_linux_and_yaffs(&args_struct);
233 //check_mode(&args_struct);
236 compare_linux_and_yaffs(&args_struct);
239 int select_test_id(int test_len)
242 //printf("test_len = %d\n",test_len);
243 id=(rand() % test_len );
244 //printf("id %d\n",id);
249 int check_mode(arg_temp *args_struct)
255 struct yaffs_stat yaffs_stat_struct;
256 join_paths(yaffs_struct.root_path,args_struct->string1, path );
257 sprintf(message,"\ntrying to stat to: %s\n",path);
258 print_message(3,message);
259 output=yaffs_stat(path,&yaffs_stat_struct);
261 sprintf(message,"failed to stat the file\n");
262 print_message(3,message);
265 sprintf(message,"stated the file\n");
266 print_message(3,message);
267 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
268 print_message(3,message);
269 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
270 print_message(3,message);
275 int check_mode_file(char *path)
280 struct yaffs_stat yaffs_stat_struct;
282 sprintf(message,"\ntrying to stat to: %s\n",path);
283 print_message(3,message);
284 output=yaffs_stat(path,&yaffs_stat_struct);
286 sprintf(message,"failed to stat the file\n");
287 print_message(3,message);
290 sprintf(message,"stated the file\n");
291 print_message(3,message);
292 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
293 print_message(3,message);
294 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));
295 print_message(3,message);
300 int compare_linux_and_yaffs(arg_temp *args_struct)
307 int number_of_files_in_yaffs=0;
308 int number_of_files_in_linux=0;
310 char **yaffs_file_list=NULL;
311 char **linux_file_list=NULL;
313 struct yaffs_stat yaffs_stat_struct;
314 struct stat linux_stat_struct;
315 yaffs_DIR *yaffs_open_dir;
316 yaffs_dirent *yaffs_current_file;
319 struct dirent *linux_current_file;
325 print_message(2,"\n\n\n comparing folders\n");
326 // check_mode_file("yaffs2/test/YY");
327 //find out the number of files in the directory
328 yaffs_open_dir = yaffs_opendir(yaffs_struct.root_path);
329 if (yaffs_open_dir) {
330 for (x=0;yaffs_readdir(yaffs_open_dir);x++){}
331 number_of_files_in_yaffs=x;
332 sprintf(message,"number of files in yaffs dir= %d\n",number_of_files_in_yaffs);
333 print_message(2,message);
334 yaffs_rewinddir(yaffs_open_dir);
336 print_message(3,"failed to open yaffs test dir\n");
339 yaffs_file_list= (char **)malloc(x*sizeof(char*));
340 for (x=0;x<number_of_files_in_yaffs;x++) {
341 yaffs_file_list[x]=malloc(200);
344 //check_mode_file("yaffs2/test/YY");
345 //copy file names into array
347 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
348 for (x=0 ;yaffs_current_file;x++)
350 printf("x= %d \n",x);
351 printf("yaffs_current_file->d_name = %s\n",yaffs_current_file->d_name);
352 if (yaffs_current_file){
353 strcpy(yaffs_file_list[x],yaffs_current_file->d_name);
356 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
358 yaffs_closedir(yaffs_open_dir);
360 print_message(3,"failed to populate yaffs test list\n");
364 //find out the number of files in the directory
365 linux_open_dir = opendir(linux_struct.root_path);
367 for (x=0;readdir(linux_open_dir);x++){}
368 number_of_files_in_linux=(x-2);
369 sprintf(message,"number of files in linux dir= %d\n",(number_of_files_in_linux));
370 print_message(2,message);
371 //the -2 is because linux shows 2 extra files which are automaticly created.
373 rewinddir(linux_open_dir);
375 print_message(3,"failed to open linux test dir\n");
379 linux_file_list= (char **)malloc(number_of_files_in_linux*sizeof(char*));
381 for (x=0;x<number_of_files_in_linux;x++) {
382 linux_file_list[x]=malloc(200);
385 //check_mode_file("yaffs2/test/YY");
386 //copy file names into array
388 linux_current_file =readdir(linux_open_dir);
389 for (x=0, y=0 ;linux_current_file;x++)
392 if (linux_current_file){
393 strcpy(message,linux_current_file->d_name);
394 print_message(7,"opened file: ");
395 print_message(7,message);
396 print_message(7,"\n");
398 if (linux_current_file &&
399 0!=strcmp(message,".")&&
400 0!=strcmp(message,"..")){
401 strcpy(file_name,linux_current_file->d_name);
402 //sprintf("file opened: %s\n",linux_current_file->d_name);
403 //print_message(3,message);
404 print_message(7,"added file to list\n");
405 strcpy(linux_file_list[y],file_name);
406 sprintf(message,"file added to list: %s\n",linux_file_list[y]);
407 print_message(7,message);
410 linux_current_file =readdir(linux_open_dir);
412 closedir(linux_open_dir);
414 print_message(3,"failed to populate linux test dir\n");
418 //match the files in both folders
419 for (x=0;x<number_of_files_in_yaffs;x++){
420 sprintf(message,"\nsearching for yaffs file: %s\n",yaffs_file_list[x]);
421 print_message(3,message);
422 for (y=0;y<number_of_files_in_linux;y++){
423 sprintf(message,"comparing to linux file: %s\n",linux_file_list[y]);
424 print_message(7,message);
426 if (0==strcmp(yaffs_file_list[x],linux_file_list[y])){
427 sprintf(message,"file matched: %s\n",linux_file_list[y]);
428 print_message(3,message);
429 //check that the modes of both files are the same
430 join_paths(yaffs_struct.root_path,yaffs_file_list[x],y_path);
431 join_paths(linux_struct.root_path,linux_file_list[y],l_path);
432 if (yaffs_stat(y_path,&yaffs_stat_struct)>=0&&
433 stat(l_path,&linux_stat_struct)>=0){
434 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
435 print_message(3,message);
436 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
437 print_message(3,message);
438 sprintf(message," linux file mode is %d\n",(linux_stat_struct.st_mode & (S_IREAD|S_IWRITE)));
439 print_message(3,message);
440 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(linux_stat_struct.st_mode & S_IREAD),(linux_stat_struct.st_mode & S_IWRITE));
441 print_message(3,message);
442 if ((yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE))==
443 ( linux_stat_struct.st_mode & (S_IREAD|S_IWRITE))){
444 print_message(2,"file modes match\n");
446 print_message(2,"file modes do not match\n");
449 linux_file_list[y][0]=NULL;
450 yaffs_file_list[x][0]=NULL;
452 print_message(2,"failed to stat one of the files\n");
465 //print remaining files
466 for (x=0;x<number_of_files_in_linux;x++){
467 if (linux_file_list[x][0]){
468 sprintf(message,"unmatched file in linux: %s\n",linux_file_list[x]);
469 print_message(2,message);
473 for (x=0;x<number_of_files_in_yaffs;x++){
474 if (yaffs_file_list[x][0]){
475 sprintf(message,"unmatched file in yaffs: %s\n",yaffs_file_list[x]);
476 print_message(2,message);
480 if (exit_bool==1&& get_exit_on_error()==1){
481 print_message(2,"exiting program\n");
485 for (x=0;x<number_of_files_in_yaffs;x++) {
486 free(yaffs_file_list[x]);
488 free(yaffs_file_list);
490 for (x=0;x<number_of_files_in_linux;x++) {
491 free(linux_file_list[x]);
493 free(linux_file_list);
496 //printf("file_name %s\n", yaffs_current_file->d_name);
497 // generate_array_of_objects_in_yaffs();
498 // generate_array_of_objects_in_linux();
499 //first do a check to see if both sides have the same objects on both sides.
500 //then stat all of the files and compare size and mode
501 //read the text of each file and compare them.
503 //show the diffrences by printing them.
508 void generate_random_numbers(arg_temp *args_struct)
511 args_struct->char1= (rand() % 255);
512 args_struct->char2= (rand() % 255);
513 args_struct->int1= (rand() % 100000);
514 args_struct->int2= (rand() % 100000);
515 generate_random_string(string,50);
516 strcpy(args_struct->string1, string);
517 generate_random_string(string,50);
518 strcpy(args_struct->string2, string);
521 void run_yaffs_test(int id,arg_temp *args_struct)
525 print_message(3,"\n");
526 //printf("id = %d\n",id);
527 sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
528 print_message(3,message);
529 output=yaffs_tests.test_list[id].test_pointer(args_struct);
531 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
532 print_message(3,message);
534 print_message(3,"test_passed\n");
538 void run_linux_test(int id,arg_temp *args_struct)
542 print_message(3,"\n");
543 //printf("id = %d\n",id);
544 sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
545 print_message(3,message);
546 output=linux_tests.test_list[id].test_pointer(args_struct);
548 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
549 print_message(3,message);
551 print_message(3,"test_passed\n");
562 struct dirent *linux_current_file;
566 strcat(string,"/emfile-2k-0");
567 sprintf(message,"\n\nunlinking emfile at this path: %s\n",string);
568 print_message(3,message);
572 linux_open_dir = opendir(linux_struct.root_path);
578 linux_current_file =readdir(linux_open_dir);
579 if (NULL!=linux_current_file){
581 strcpy(file,linux_struct.root_path);
582 strcat(file,linux_current_file->d_name);
583 sprintf(message,"unlinking file %s\n",file);
584 print_message(3,message);
585 print_message(3,"chmoding file\n");
586 output=chmod(file,(S_IRUSR|S_IWUSR));
590 print_message(3,"unlinking file\n");
596 } while(linux_current_file);
597 closedir(linux_open_dir);
598 rmdir(linux_struct.root_path);