2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Timothy Manning <timothy@yaffs.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include "mirror_tests.h"
16 int simulate_power_failure = 0;
18 int num_of_random_tests=1;
22 typedef struct test_list_temp2{
24 int (*test_pointer)(arg_temp *args_struct);
27 typedef struct test_temp2 {
29 test_list_temp test_list[];
32 test_temp yaffs_tests={
34 {{"yaffs_test_open",yaffs_test_open},
35 {"yaffs_test_truncate",yaffs_test_truncate},
36 {"yaffs_test_unlink",yaffs_test_unlink},
37 {"yaffs_test_write",yaffs_test_write}
41 test_temp linux_tests={
43 {{"linux_test_open",linux_test_open},
44 {"linux_test_truncate",linux_test_truncate},
45 {"linux_test_unlink",linux_test_unlink},
46 {"linux_test_write",linux_test_write}
50 const struct option long_options[]={
52 {"yaffs_path", 1,NULL,'y'},
53 {"linux_path", 1,NULL,'l'},
54 {"print_level", 1,NULL,'p'},
55 {"quiet", 0,NULL,'q'},
56 {"number", 1,NULL,'n'},
58 {"trace", 1,NULL,'t'},
59 {"clean", 0,NULL,'c'},
60 {"verbose", 0,NULL,'v'}
63 const char short_options[]="hy:l:p:qn:s:t:cv";
66 int main(int argc, char *argv[])
70 // yaffs_tests.num_of_tests=(sizeof(yaffs_tests)/sizeof(test_temp));
71 // linux_tests.num_of_tests=(sizeof(linux_tests)/sizeof(test_temp));
74 print_message(1,"running mirror_tests\n");
75 sprintf(message,"linux_root_path: %s\n",linux_struct.root_path);
76 print_message(3,message);
77 sprintf(message,"yaffs_root_path: %s\n",yaffs_struct.root_path);
78 print_message(3,message);
79 sprintf(message,"linux_num_of_tests: %d\n",linux_tests.num_of_tests);
80 print_message(3,message);
81 sprintf(message,"yaffs_num_of_tests: %d\n",yaffs_tests.num_of_tests);
82 print_message(3,message);
84 run_random_test(num_of_random_tests);
85 //compare_linux_and_yaffs();
86 yaffs_unmount("yaffs2");
90 void init(int argc, char *argv[])
98 x=(unsigned)time(NULL);
99 sprintf(message,"seeding srand with: %d\n",x);
100 print_message(2,message);
103 linux_struct.type_of_test =LINUX;
104 yaffs_struct.type_of_test =YAFFS;
106 sprintf(message,"current absolute path is: %s\n",getcwd(dir,200));
107 print_message(3,message);
108 strcpy(dir,getcwd(dir,200));
110 strcat(dir,"/test/");
111 printf("dir: %s\n",dir);
112 strcpy(linux_struct.root_path,dir);
113 strcpy(yaffs_struct.root_path,"yaffs2/test/");
117 new_option=getopt_long(argc,argv,short_options,long_options,NULL);
118 if (new_option=='h'){
119 printf("mirror_tests help\n");
120 printf("arguments:\n");
121 printf("\t-yaffs_path [PATH] //sets the path for yaffs.\n");
122 printf("\t-linux_path [PATH] //sets the path for linux.\n");
123 printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
124 printf("\t-v //verbose mode everything is printed\n");
125 printf("\t-q //quiet mode nothing is printed.\n");
126 printf("\t-n [number] //sets the number of random tests to run.\n");
127 printf("\t-s [number] //seeds rand with the number\n");
128 printf("\t-t [number] //sets yaffs_trace to the number\n");
129 printf("\t-clean //removes emfile and test dir\n");
131 } else if (new_option=='y'){
132 strcpy(yaffs_struct.root_path, optarg);
133 } else if (new_option=='l'){
134 strcpy(linux_struct.root_path, optarg);
135 } else if (new_option=='p'){
136 set_print_level(atoi(optarg));
137 } else if (new_option=='v'){
139 } else if (new_option=='q'){
141 } else if (new_option=='n'){
142 num_of_random_tests=atoi(optarg);
143 } else if (new_option=='s'){
144 srand(atoi(argv[x+1]));
145 } else if (new_option=='t'){
146 yaffs_set_trace(atoi(optarg));
147 } else if (new_option=='c'){
150 } else if (new_option==-1){
152 } else if (new_option=='?') {
153 printf("bad argument\n");
157 } while(new_option!=-1);
160 print_message(2,"\nmounting yaffs\n");
161 x=yaffs_mount("yaffs2");
163 print_message(3,"failed to mount yaffs\n");
166 print_message(3,"mounted yaffs\n");
170 print_message(3,"\nmaking linux test dir\n");
171 x=mkdir(linux_struct.root_path,0777);
173 print_message(1,"failed to make dir\n");
176 print_message(3,"made dir\n");
179 print_message(3,"\nmaking yaffs test dir\n");
180 x=yaffs_mkdir(yaffs_struct.root_path,0777);
182 print_message(1,"failed to make dir\n");
185 print_message(3,"made dir\n");
189 int run_random_test(int num_of_random_tests)
195 int num_of_tests_before_check=1;
197 arg_temp args_struct;
198 for (y=0;(y*num_of_tests_before_check)<num_of_random_tests;y++){
199 for (x=0;x<num_of_tests_before_check && (x+(y*num_of_tests_before_check)<num_of_random_tests);x++) {
202 test_id = select_test_id(yaffs_tests.num_of_tests);
203 sprintf(message,"running test_id %d\n",test_id);
204 print_message(3,message);
205 generate_random_numbers(&args_struct);
206 run_yaffs_test(test_id, &args_struct);
208 //check_mode(&args_struct);
210 run_linux_test(test_id, &args_struct);
212 if (get_print_level()>=4){
217 if ((abs(yaffs_get_error())!=abs(errno)) &&
218 (abs(yaffs_get_error())!=EISDIR && abs(errno) != 0) &&
219 (abs(yaffs_get_error())!=ENOENT && abs(errno) != EACCES)&&
220 (abs(yaffs_get_error())!=EINVAL && abs(errno) != EBADF)
222 print_message(2,"\ndifference in returned errors######################################\n");
225 if (get_exit_on_error()){
230 //check_mode(&args_struct);
231 compare_linux_and_yaffs(&args_struct);
232 //check_mode(&args_struct);
235 compare_linux_and_yaffs(&args_struct);
238 int select_test_id(int test_len)
241 //printf("test_len = %d\n",test_len);
242 id=(rand() % test_len );
243 //printf("id %d\n",id);
248 int check_mode(arg_temp *args_struct)
254 struct yaffs_stat yaffs_stat_struct;
255 join_paths(yaffs_struct.root_path,args_struct->string1, path );
256 sprintf(message,"\ntrying to stat to: %s\n",path);
257 print_message(3,message);
258 output=yaffs_stat(path,&yaffs_stat_struct);
260 sprintf(message,"failed to stat the file\n");
261 print_message(3,message);
264 sprintf(message,"stated the file\n");
265 print_message(3,message);
266 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
267 print_message(3,message);
268 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
269 print_message(3,message);
274 int check_mode_file(char *path)
279 struct yaffs_stat yaffs_stat_struct;
281 sprintf(message,"\ntrying to stat to: %s\n",path);
282 print_message(3,message);
283 output=yaffs_stat(path,&yaffs_stat_struct);
285 sprintf(message,"failed to stat the file\n");
286 print_message(3,message);
289 sprintf(message,"stated the file\n");
290 print_message(3,message);
291 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
292 print_message(3,message);
293 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));
294 print_message(3,message);
299 int compare_linux_and_yaffs(arg_temp *args_struct)
306 int number_of_files_in_yaffs=0;
307 int number_of_files_in_linux=0;
309 char **yaffs_file_list=NULL;
310 char **linux_file_list=NULL;
312 struct yaffs_stat yaffs_stat_struct;
313 struct stat linux_stat_struct;
314 yaffs_DIR *yaffs_open_dir;
315 struct yaffs_dirent *yaffs_current_file;
318 struct dirent *linux_current_file;
324 print_message(2,"\n\n\n comparing folders\n");
325 // check_mode_file("yaffs2/test/YY");
326 //find out the number of files in the directory
327 yaffs_open_dir = yaffs_opendir(yaffs_struct.root_path);
328 if (yaffs_open_dir) {
329 for (x=0;yaffs_readdir(yaffs_open_dir);x++){}
330 number_of_files_in_yaffs=x;
331 sprintf(message,"number of files in yaffs dir= %d\n",number_of_files_in_yaffs);
332 print_message(2,message);
333 yaffs_rewinddir(yaffs_open_dir);
335 print_message(3,"failed to open yaffs test dir\n");
338 yaffs_file_list= (char **)malloc(x*sizeof(char*));
339 for (x=0;x<number_of_files_in_yaffs;x++) {
340 yaffs_file_list[x]=malloc(200);
343 //check_mode_file("yaffs2/test/YY");
344 //copy file names into array
346 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
347 for (x=0 ;yaffs_current_file;x++)
349 printf("x= %d \n",x);
350 printf("yaffs_current_file->d_name = %s\n",yaffs_current_file->d_name);
351 if (yaffs_current_file){
352 strcpy(yaffs_file_list[x],yaffs_current_file->d_name);
355 yaffs_current_file =yaffs_readdir(yaffs_open_dir);
357 yaffs_closedir(yaffs_open_dir);
359 print_message(3,"failed to populate yaffs test list\n");
363 //find out the number of files in the directory
364 linux_open_dir = opendir(linux_struct.root_path);
366 for (x=0;readdir(linux_open_dir);x++){}
367 number_of_files_in_linux=(x-2);
368 sprintf(message,"number of files in linux dir= %d\n",(number_of_files_in_linux));
369 print_message(2,message);
370 //the -2 is because linux shows 2 extra files which are automaticly created.
372 rewinddir(linux_open_dir);
374 print_message(3,"failed to open linux test dir\n");
378 linux_file_list= (char **)malloc(number_of_files_in_linux*sizeof(char*));
380 for (x=0;x<number_of_files_in_linux;x++) {
381 linux_file_list[x]=malloc(200);
384 //check_mode_file("yaffs2/test/YY");
385 //copy file names into array
387 linux_current_file =readdir(linux_open_dir);
388 for (x=0, y=0 ;linux_current_file;x++)
391 if (linux_current_file){
392 strcpy(message,linux_current_file->d_name);
393 print_message(7,"opened file: ");
394 print_message(7,message);
395 print_message(7,"\n");
397 if (linux_current_file &&
398 0!=strcmp(message,".")&&
399 0!=strcmp(message,"..")){
400 strcpy(file_name,linux_current_file->d_name);
401 //sprintf("file opened: %s\n",linux_current_file->d_name);
402 //print_message(3,message);
403 print_message(7,"added file to list\n");
404 strcpy(linux_file_list[y],file_name);
405 sprintf(message,"file added to list: %s\n",linux_file_list[y]);
406 print_message(7,message);
409 linux_current_file =readdir(linux_open_dir);
411 closedir(linux_open_dir);
413 print_message(3,"failed to populate linux test dir\n");
417 //match the files in both folders
418 for (x=0;x<number_of_files_in_yaffs;x++){
419 sprintf(message,"\nsearching for yaffs file: %s\n",yaffs_file_list[x]);
420 print_message(3,message);
421 for (y=0;y<number_of_files_in_linux;y++){
422 sprintf(message,"comparing to linux file: %s\n",linux_file_list[y]);
423 print_message(7,message);
425 if (0==strcmp(yaffs_file_list[x],linux_file_list[y])){
426 sprintf(message,"file matched: %s\n",linux_file_list[y]);
427 print_message(3,message);
428 //check that the modes of both files are the same
429 join_paths(yaffs_struct.root_path,yaffs_file_list[x],y_path);
430 join_paths(linux_struct.root_path,linux_file_list[y],l_path);
431 if (yaffs_stat(y_path,&yaffs_stat_struct)>=0&&
432 stat(l_path,&linux_stat_struct)>=0){
433 sprintf(message," yaffs file mode is %d\n",(yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE)));
434 print_message(3,message);
435 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(yaffs_stat_struct.st_mode & S_IREAD),(yaffs_stat_struct.st_mode & S_IWRITE));
436 print_message(3,message);
437 sprintf(message," linux file mode is %d\n",(linux_stat_struct.st_mode & (S_IREAD|S_IWRITE)));
438 print_message(3,message);
439 sprintf(message,"mode S_IREAD %d S_IWRITE %d\n",(linux_stat_struct.st_mode & S_IREAD),(linux_stat_struct.st_mode & S_IWRITE));
440 print_message(3,message);
441 if ((yaffs_stat_struct.st_mode & (S_IREAD| S_IWRITE))==
442 ( linux_stat_struct.st_mode & (S_IREAD|S_IWRITE))){
443 print_message(2,"file modes match\n");
445 print_message(2,"file modes do not match\n");
448 linux_file_list[y][0]=NULL;
449 yaffs_file_list[x][0]=NULL;
451 print_message(2,"failed to stat one of the files\n");
464 //print remaining files
465 for (x=0;x<number_of_files_in_linux;x++){
466 if (linux_file_list[x][0]){
467 sprintf(message,"unmatched file in linux: %s\n",linux_file_list[x]);
468 print_message(2,message);
472 for (x=0;x<number_of_files_in_yaffs;x++){
473 if (yaffs_file_list[x][0]){
474 sprintf(message,"unmatched file in yaffs: %s\n",yaffs_file_list[x]);
475 print_message(2,message);
479 if (exit_bool==1&& get_exit_on_error()==1){
480 print_message(2,"exiting program\n");
484 for (x=0;x<number_of_files_in_yaffs;x++) {
485 free(yaffs_file_list[x]);
487 free(yaffs_file_list);
489 for (x=0;x<number_of_files_in_linux;x++) {
490 free(linux_file_list[x]);
492 free(linux_file_list);
495 //printf("file_name %s\n", yaffs_current_file->d_name);
496 // generate_array_of_objects_in_yaffs();
497 // generate_array_of_objects_in_linux();
498 //first do a check to see if both sides have the same objects on both sides.
499 //then stat all of the files and compare size and mode
500 //read the text of each file and compare them.
502 //show the diffrences by printing them.
507 void generate_random_numbers(arg_temp *args_struct)
510 args_struct->char1= (rand() % 255);
511 args_struct->char2= (rand() % 255);
512 args_struct->int1= (rand() % 100000);
513 args_struct->int2= (rand() % 100000);
514 generate_random_string(string,50);
515 strcpy(args_struct->string1, string);
516 generate_random_string(string,50);
517 strcpy(args_struct->string2, string);
520 void run_yaffs_test(int id,arg_temp *args_struct)
524 print_message(3,"\n");
525 //printf("id = %d\n",id);
526 sprintf(message,"running_test %s\n",yaffs_tests.test_list[id].test_name);
527 print_message(3,message);
528 output=yaffs_tests.test_list[id].test_pointer(args_struct);
530 sprintf(message,"test_failed %s\n",yaffs_tests.test_list[id].test_name);
531 print_message(3,message);
533 print_message(3,"test_passed\n");
537 void run_linux_test(int id,arg_temp *args_struct)
541 print_message(3,"\n");
542 //printf("id = %d\n",id);
543 sprintf(message,"running_test %s\n",linux_tests.test_list[id].test_name);
544 print_message(3,message);
545 output=linux_tests.test_list[id].test_pointer(args_struct);
547 sprintf(message,"test_failed %s\n",linux_tests.test_list[id].test_name);
548 print_message(3,message);
550 print_message(3,"test_passed\n");
561 struct dirent *linux_current_file;
565 strcat(string,"/emfile-2k-0");
566 sprintf(message,"\n\nunlinking emfile at this path: %s\n",string);
567 print_message(3,message);
571 linux_open_dir = opendir(linux_struct.root_path);
577 linux_current_file =readdir(linux_open_dir);
578 if (NULL!=linux_current_file){
580 strcpy(file,linux_struct.root_path);
581 strcat(file,linux_current_file->d_name);
582 sprintf(message,"unlinking file %s\n",file);
583 print_message(3,message);
584 print_message(3,"chmoding file\n");
585 output=chmod(file,(S_IRUSR|S_IWUSR));
589 print_message(3,"unlinking file\n");
595 } while(linux_current_file);
596 closedir(linux_open_dir);
597 rmdir(linux_struct.root_path);