2 * Test code for the "direct" interface.
13 void dumpDir(const char *dname);
17 void copy_in_a_file(char *yaffsName,char *inName)
20 unsigned char buffer[100];
22 inh = open(inName,O_RDONLY);
23 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
25 while((ni = read(inh,buffer,100)) > 0)
27 no = yaffs_write(outh,buffer,ni);
30 printf("problem writing yaffs file\n");
39 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
43 unsigned char buffer[100];
45 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
47 memset(buffer,bval,100);
54 yaffs_write(outh,buffer,i);
56 } while (sizeOfFile > 0);
63 void make_pattern_file(char *fn,int size)
68 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
69 yaffs_lseek(outh,size-1,SEEK_SET);
70 yaffs_write(outh,"A",1);
72 for(i = 0; i < size; i+=256)
75 yaffs_lseek(outh,i,SEEK_SET);
76 yaffs_write(outh,&marker,sizeof(marker));
82 int check_pattern_file(char *fn)
90 h = yaffs_open(fn, O_RDWR,0);
91 size = yaffs_lseek(h,0,SEEK_END);
93 for(i = 0; i < size; i+=256)
95 yaffs_lseek(h,i,SEEK_SET);
96 yaffs_read(h,&marker,sizeof(marker));
100 printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
101 fn,size,i,marker,~i);
112 int dump_file_data(char *fn)
121 h = yaffs_open(fn, O_RDWR,0);
125 while(yaffs_read(h,&b,1)> 0)
142 void dump_file(const char *fn)
148 h = yaffs_open(fn,O_RDONLY,0);
151 printf("*****\nDump file %s does not exist\n",fn);
155 size = yaffs_lseek(h,0,SEEK_SET);
156 printf("*****\nDump file %s size %d\n",fn,size);
157 for(i = 0; i < size; i++)
164 void create_file_of_size(const char *fn,int syze)
171 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
173 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
175 while (iterations > 0)
177 sprintf(xx,"%s %8d",fn,iterations);
178 yaffs_write(h,xx,strlen(xx));
184 void verify_file_of_size(const char *fn,int syze)
193 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
195 h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
197 while (iterations > 0)
199 sprintf(xx,"%s %8d",fn,iterations);
206 printf("=====>>>>> verification of file %s failed near position %d\n",fn,yaffs_lseek(h,0,SEEK_CUR));
213 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
221 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
223 iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
224 while (iterations > 0)
226 yaffs_write(h,fn,strlen(fn));
230 yaffs_truncate(h,reSyze);
232 yaffs_lseek(h,0,SEEK_SET);
233 iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
234 while (iterations > 0)
236 yaffs_write(h,fn,strlen(fn));
244 void do_some_file_stuff(const char *path)
249 sprintf(fn,"%s/%s",path,"f1");
250 create_file_of_size(fn,10000);
252 sprintf(fn,"%s/%s",path,"fdel");
253 create_file_of_size(fn,10000);
256 sprintf(fn,"%s/%s",path,"f2");
258 create_resized_file_of_size(fn,10000,3000,4000);
261 void yaffs_backward_scan_test(const char *path)
269 do_some_file_stuff(path);
271 sprintf(fn,"%s/ddd",path);
275 do_some_file_stuff(fn);
285 void yaffs_device_flush_test(const char *path)
295 do_some_file_stuff(path);
297 // Open and add some data to a few files
298 for(i = 0; i < 10; i++) {
300 sprintf(fn,"%s/ff%d",path,i);
302 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
303 yaffs_write(h,xxzz,2000);
304 yaffs_write(h,xxzz,2000);
313 void short_scan_test(const char *path, int fsize, int niterations)
318 sprintf(fn,"%s/%s",path,"f1");
321 for(i = 0; i < niterations; i++)
323 printf("\n*****************\nIteration %d\n",i);
325 printf("\nmount: Directory look-up of %s\n",path);
327 make_a_file(fn,1,fsize);
334 void scan_pattern_test(const char *path, int fsize, int niterations)
341 sprintf(fn[0],"%s/%s",path,"f0");
342 sprintf(fn[1],"%s/%s",path,"f1");
343 sprintf(fn[2],"%s/%s",path,"f2");
347 for(i = 0; i < niterations; i++)
349 printf("\n*****************\nIteration %d\n",i);
351 printf("\nmount: Directory look-up of %s\n",path);
353 for(j = 0; j < 3; j++)
355 result = dump_file_data(fn[j]);
356 result = check_pattern_file(fn[j]);
357 make_pattern_file(fn[j],fsize);
358 result = dump_file_data(fn[j]);
359 result = check_pattern_file(fn[j]);
365 void fill_disk(char *path,int nfiles)
374 for(n = 0; n < nfiles; n++)
376 sprintf(str,"%s/%d",path,n);
378 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
380 printf("writing file %s handle %d ",str, h);
382 while ((result = yaffs_write(h,xx,600)) == 600)
384 f = yaffs_freespace(path);
386 result = yaffs_close(h);
387 printf(" close %d\n",result);
391 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
397 for(i = 0; i < ncycles; i++)
399 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
400 fill_disk(path,nfiles);
402 for(j = 0; j < nfiles; j++)
404 sprintf(str,"%s/%d",path,j);
405 result = yaffs_unlink(str);
406 printf("unlinking file %s, result %d\n",str,result);
412 void fill_files(char *path,int flags, int maxIterations,int siz)
422 sprintf(str,"%s/%d",path,i);
423 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
428 for(j = 0; j < siz; j++)
430 yaffs_write(h,str,1);
439 } while(h >= 0 && i < maxIterations);
445 sprintf(str,"%s/%d",path,i);
446 printf("unlink %s\n",str);
448 } while(yaffs_unlink(str) >= 0);
452 void leave_unlinked_file(char *path,int maxIterations,int siz)
461 sprintf(str,"%s/%d",path,i);
462 printf("create %s\n",str);
463 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
469 } while(h < 0 && i < maxIterations);
473 for(i = 0; i < siz; i++)
475 yaffs_write(h,str,1);
479 printf("Leaving file %s open\n",str);
483 void dumpDirFollow(const char *dname)
490 d = yaffs_opendir(dname);
494 printf("opendir failed\n");
498 while((de = yaffs_readdir(d)) != NULL)
500 sprintf(str,"%s/%s",dname,de->d_name);
504 printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
505 switch(s.st_mode & S_IFMT)
507 case S_IFREG: printf("data file"); break;
508 case S_IFDIR: printf("directory"); break;
509 case S_IFLNK: printf("symlink -->");
510 if(yaffs_readlink(str,str,100) < 0)
513 printf("\"%s\"",str);
515 default: printf("unknown"); break;
525 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
530 void dump_directory_tree_worker(const char *dname,int recursive)
537 d = yaffs_opendir(dname);
541 printf("opendir failed\n");
545 while((de = yaffs_readdir(d)) != NULL)
547 sprintf(str,"%s/%s",dname,de->d_name);
551 printf("%s inode %d obj %x length %d mode %X ",str,s.st_ino,de->d_dont_use,(int)s.st_size,s.st_mode);
552 switch(s.st_mode & S_IFMT)
554 case S_IFREG: printf("data file"); break;
555 case S_IFDIR: printf("directory"); break;
556 case S_IFLNK: printf("symlink -->");
557 if(yaffs_readlink(str,str,100) < 0)
560 printf("\"%s\"",str);
562 default: printf("unknown"); break;
567 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
568 dump_directory_tree_worker(str,1);
577 static void dump_directory_tree(const char *dname)
579 dump_directory_tree_worker(dname,1);
581 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
584 void dumpDir(const char *dname)
585 { dump_directory_tree_worker(dname,0);
587 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
591 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
595 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
597 fd = yaffs_open(path,tflags,0);
599 if((fd >= 0) != (expectedResult > 0))
601 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
605 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
614 int long_test(int argc, char *argv[])
625 struct yaffs_stat ystat;
629 yaffs_mount("/boot");
630 yaffs_mount("/data");
631 yaffs_mount("/flash");
634 printf("\nDirectory look-up of /boot\n");
636 printf("\nDirectory look-up of /data\n");
638 printf("\nDirectory look-up of /flash\n");
641 //leave_unlinked_file("/flash",20000,0);
642 //leave_unlinked_file("/data",20000,0);
644 leave_unlinked_file("/ram",20,0);
647 f = yaffs_open("/boot/b1", O_RDONLY,0);
649 printf("open /boot/b1 readonly, f=%d\n",f);
651 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
653 printf("open /boot/b1 O_CREAT, f=%d\n",f);
656 r = yaffs_write(f,"hello",1);
657 printf("write %d attempted to write to a read-only file\n",r);
661 printf("close %d\n",r);
663 f = yaffs_open("/boot/b1", O_RDWR,0);
665 printf("open /boot/b1 O_RDWR,f=%d\n",f);
668 r = yaffs_write(f,"hello",2);
669 printf("write %d attempted to write to a writeable file\n",r);
670 r = yaffs_write(f,"world",3);
671 printf("write %d attempted to write to a writeable file\n",r);
673 r= yaffs_lseek(f,0,SEEK_END);
674 printf("seek end %d\n",r);
676 r = yaffs_read(f,buffer,10);
677 printf("read %d \"%s\"\n",r,buffer);
678 r= yaffs_lseek(f,0,SEEK_SET);
679 printf("seek set %d\n",r);
681 r = yaffs_read(f,buffer,10);
682 printf("read %d \"%s\"\n",r,buffer);
684 r = yaffs_read(f,buffer,10);
685 printf("read %d \"%s\"\n",r,buffer);
687 // Check values reading at end.
688 // A read past end of file should return 0 for 0 bytes read.
690 r= yaffs_lseek(f,0,SEEK_END);
691 r = yaffs_read(f,buffer,10);
692 printf("read at end returned %d\n",r);
693 r= yaffs_lseek(f,500,SEEK_END);
694 r = yaffs_read(f,buffer,10);
695 printf("read past end returned %d\n",r);
699 printf("close %d\n",r);
701 copy_in_a_file("/boot/yyfile","xxx");
703 // Create a file with a long name
705 copy_in_a_file("/boot/file with a long name","xxx");
708 printf("\nDirectory look-up of /boot\n");
712 r = yaffs_stat("/boot/file with a long name",&ystat);
716 r = yaffs_rename("/boot/file with a long name","/boot/r1");
718 printf("\nDirectory look-up of /boot\n");
722 r = yaffs_unlink("/boot/r1");
724 printf("\nDirectory look-up of /boot\n");
729 r = yaffs_mkdir("/boot/directory1",0);
731 printf("\nDirectory look-up of /boot\n");
733 printf("\nDirectory look-up of /boot/directory1\n");
734 dumpDir("/boot/directory1");
736 // add a file to the directory
737 copy_in_a_file("/boot/directory1/file with a long name","xxx");
739 printf("\nDirectory look-up of /boot\n");
741 printf("\nDirectory look-up of /boot/directory1\n");
742 dumpDir("/boot/directory1");
744 // Attempt to delete directory (should fail)
746 r = yaffs_rmdir("/boot/directory1");
748 printf("\nDirectory look-up of /boot\n");
750 printf("\nDirectory look-up of /boot/directory1\n");
751 dumpDir("/boot/directory1");
753 // Delete file first, then rmdir should work
754 r = yaffs_unlink("/boot/directory1/file with a long name");
755 r = yaffs_rmdir("/boot/directory1");
758 printf("\nDirectory look-up of /boot\n");
760 printf("\nDirectory look-up of /boot/directory1\n");
761 dumpDir("/boot/directory1");
764 fill_disk_and_delete("/boot",20,20);
766 printf("\nDirectory look-up of /boot\n");
770 yaffs_symlink("yyfile","/boot/slink");
772 yaffs_readlink("/boot/slink",str,100);
773 printf("symlink alias is %s\n",str);
778 printf("\nDirectory look-up of /boot\n");
780 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
781 dumpDirFollow("/boot");
782 printf("\nDirectory look-up of /boot/directory1\n");
783 dumpDir("/boot/directory1");
785 h = yaffs_open("/boot/slink",O_RDWR,0);
787 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
791 yaffs_unlink("/boot/slink");
794 printf("\nDirectory look-up of /boot\n");
799 yaffs_stat("/boot/yyfile",&ystat);
800 temp_mode = ystat.st_mode;
802 yaffs_chmod("/boot/yyfile",0x55555);
803 printf("\nDirectory look-up of /boot\n");
806 yaffs_chmod("/boot/yyfile",temp_mode);
807 printf("\nDirectory look-up of /boot\n");
810 // Permission checks...
811 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
812 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
813 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
815 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
816 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
817 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
819 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
820 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
821 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
823 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
824 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
825 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
827 yaffs_chmod("/boot/yyfile",temp_mode);
829 //create a zero-length file and unlink it (test for scan bug)
831 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
834 yaffs_unlink("/boot/zlf");
837 yaffs_DumpDevStruct("/boot");
839 fill_disk_and_delete("/boot",20,20);
841 yaffs_DumpDevStruct("/boot");
843 fill_files("/boot",1,10000,0);
844 fill_files("/boot",1,10000,5000);
845 fill_files("/boot",2,10000,0);
846 fill_files("/boot",2,10000,5000);
848 leave_unlinked_file("/data",20000,0);
849 leave_unlinked_file("/data",20000,5000);
850 leave_unlinked_file("/data",20000,5000);
851 leave_unlinked_file("/data",20000,5000);
852 leave_unlinked_file("/data",20000,5000);
853 leave_unlinked_file("/data",20000,5000);
855 yaffs_DumpDevStruct("/boot");
856 yaffs_DumpDevStruct("/data");
864 int huge_directory_test_on_path(char *path)
884 struct yaffs_stat ystat;
890 // Create a large number of files
892 for(i = 0; i < 2000; i++)
894 sprintf(str,"%s/%d",path,i);
896 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
902 d = yaffs_opendir(path);
905 while((de = yaffs_readdir(d)) != NULL) {
906 if (total >lastTotal+100*9*1024||(i & 1023)==0){
907 printf("files = %d, total = %d\n",i, total);
911 sprintf(str,"%s/%s",path,de->d_name);
913 switch(s.st_mode & S_IFMT){
915 //printf("data file");
927 int yaffs_scan_test(const char *path)
932 void rename_over_test(const char *mountpt)
938 sprintf(a,"%s/a",mountpt);
939 sprintf(b,"%s/b",mountpt);
943 yaffs_mount(mountpt);
944 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
946 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
948 yaffs_rename(a,b); // rename over
949 yaffs_rename(b,a); // rename back again (not renaimng over)
950 yaffs_rename(a,b); // rename back again (not renaimng over)
953 yaffs_unmount(mountpt);
957 int resize_stress_test(const char *path)
972 sprintf(aname,"%s%s",path,"/a");
973 sprintf(bname,"%s%s",path,"/b");
975 memset(abuffer,'a',1000);
976 memset(bbuffer,'b',1000);
978 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
979 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
981 printf(" %s %d %s %d\n",aname,a,bname,b);
985 for(j = 0; j < 100; j++)
987 yaffs_lseek(a,0,SEEK_END);
990 for(i = 0; i <20000; i++)
992 //r = yaffs_lseek(b,i,SEEK_SET);
993 //r = yaffs_write(b,bbuffer,1000);
998 int syz = yaffs_lseek(a,0,SEEK_END);
1001 if(syz < 0) syz = 0;
1002 yaffs_truncate(a,syz);
1008 r = yaffs_lseek(a,i * 500,SEEK_SET);
1009 r = yaffs_write(a,abuffer,1000);
1021 int resize_stress_test_no_grow_complex(const char *path,int iters)
1036 sprintf(aname,"%s%s",path,"/a");
1037 sprintf(bname,"%s%s",path,"/b");
1039 memset(abuffer,'a',1000);
1040 memset(bbuffer,'b',1000);
1042 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1043 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1045 printf(" %s %d %s %d\n",aname,a,bname,b);
1049 for(j = 0; j < iters; j++)
1051 yaffs_lseek(a,0,SEEK_END);
1054 for(i = 0; i <20000; i++)
1056 //r = yaffs_lseek(b,i,SEEK_SET);
1057 //r = yaffs_write(b,bbuffer,1000);
1062 int syz = yaffs_lseek(a,0,SEEK_END);
1068 if(syz < 0) syz = 0;
1069 yaffs_truncate(a,syz);
1070 syz = yaffs_lseek(a,0,SEEK_END);
1071 printf("shrink to %d\n",syz);
1079 r = yaffs_lseek(a,500,SEEK_END);
1080 r = yaffs_write(a,abuffer,1000);
1086 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1094 int resize_stress_test_no_grow(const char *path,int iters)
1109 sprintf(aname,"%s%s",path,"/a");
1110 sprintf(bname,"%s%s",path,"/b");
1112 memset(abuffer,'a',1000);
1113 memset(bbuffer,'b',1000);
1115 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1116 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1118 printf(" %s %d %s %d\n",aname,a,bname,b);
1122 for(j = 0; j < iters; j++)
1124 yaffs_lseek(a,0,SEEK_END);
1127 for(i = 0; i <20000; i++)
1129 //r = yaffs_lseek(b,i,SEEK_SET);
1130 //r = yaffs_write(b,bbuffer,1000);
1135 int syz = yaffs_lseek(a,0,SEEK_END);
1141 if(syz < 0) syz = 0;
1142 yaffs_truncate(a,syz);
1143 syz = yaffs_lseek(a,0,SEEK_END);
1144 printf("shrink to %d\n",syz);
1152 r = yaffs_lseek(a,-500,SEEK_END);
1153 r = yaffs_write(a,abuffer,1000);
1159 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1167 int directory_rename_test(void)
1172 yaffs_mount("/ram");
1173 yaffs_mkdir("/ram/a",0);
1174 yaffs_mkdir("/ram/a/b",0);
1175 yaffs_mkdir("/ram/c",0);
1177 printf("\nDirectory look-up of /ram\n");
1180 dumpDir("/ram/a/b");
1182 printf("Do rename (should fail)\n");
1184 r = yaffs_rename("/ram/a","/ram/a/b/d");
1185 printf("\nDirectory look-up of /ram\n");
1188 dumpDir("/ram/a/b");
1190 printf("Do rename (should not fail)\n");
1192 r = yaffs_rename("/ram/c","/ram/a/b/d");
1193 printf("\nDirectory look-up of /ram\n");
1196 dumpDir("/ram/a/b");
1203 int cache_read_test(void)
1207 int sizeOfFiles = 500000;
1212 yaffs_mount("/boot");
1214 make_a_file("/boot/a",'a',sizeOfFiles);
1215 make_a_file("/boot/b",'b',sizeOfFiles);
1217 a = yaffs_open("/boot/a",O_RDONLY,0);
1218 b = yaffs_open("/boot/b",O_RDONLY,0);
1219 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1223 if (i > 100) i = 100;
1225 yaffs_read(a,buffer,i);
1226 yaffs_read(b,buffer,i);
1227 yaffs_write(c,buffer,i);
1228 } while(sizeOfFiles > 0);
1236 int cache_bypass_bug_test(void)
1238 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1239 // resulting in erroneous reads after writes.
1240 // This bug has been fixed.
1247 memset(buffer1,0,sizeof(buffer1));
1248 memset(buffer2,0,sizeof(buffer2));
1252 yaffs_mount("/boot");
1254 // Create a file of 2000 bytes.
1255 make_a_file("/boot/a",'X',2000);
1257 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1259 // Write a short sequence to the file.
1260 // This will go into the cache.
1261 yaffs_lseek(a,0,SEEK_SET);
1262 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1264 // Read a short sequence from the file.
1265 // This will come from the cache.
1266 yaffs_lseek(a,0,SEEK_SET);
1267 yaffs_read(a,buffer1,30);
1269 // Read a page size sequence from the file.
1270 yaffs_lseek(a,0,SEEK_SET);
1271 yaffs_read(a,buffer2,512);
1273 printf("buffer 1 %s\n",buffer1);
1274 printf("buffer 2 %s\n",buffer2);
1276 if(strncmp(buffer1,buffer2,20))
1278 printf("Cache bypass bug detected!!!!!\n");
1286 int free_space_check(void)
1291 yaffs_mount("/boot");
1292 fill_disk("/boot/",2);
1293 f = yaffs_freespace("/boot");
1295 printf("%d free when disk full\n",f);
1299 int truncate_test(void)
1309 yaffs_mount("/boot");
1311 yaffs_unlink("/boot/trunctest");
1313 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1315 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1317 yaffs_truncate(a,3);
1318 l= yaffs_lseek(a,0,SEEK_END);
1320 printf("truncated length is %d\n",l);
1322 yaffs_lseek(a,5,SEEK_SET);
1323 yaffs_write(a,"1",1);
1325 yaffs_lseek(a,0,SEEK_SET);
1327 r = yaffs_read(a,y,10);
1329 printf("read %d bytes:",r);
1331 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1343 void fill_disk_test(const char *mountpt)
1348 for(i = 0; i < 5; i++)
1350 yaffs_mount(mountpt);
1351 fill_disk_and_delete(mountpt,100,i+1);
1352 yaffs_unmount(mountpt);
1359 void lookup_test(const char *mountpt)
1369 struct yaffs_stat s;
1374 yaffs_mount(mountpt);
1376 d = yaffs_opendir(mountpt);
1380 printf("opendir failed\n");
1385 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1387 printf("unlinking %s\n",de->d_name);
1388 yaffs_unlink(de->d_name);
1391 printf("%d files deleted\n",i);
1395 for(i = 0; i < 2000; i++){
1396 sprintf(a,"%s/%d",mountpt,i);
1397 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1402 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1404 printf("%d %s\n",i,de->d_name);
1407 printf("%d files listed\n\n\n",i);
1414 for(i = 0; i < 2000; i++){
1415 sprintf(a,"%s/%d",mountpt,i);
1420 yaffs_unmount(mountpt);
1424 void link_test(const char *mountpt)
1436 sprintf(a,"%s/aaa",mountpt);
1437 sprintf(b,"%s/bbb",mountpt);
1438 sprintf(c,"%s/ccc",mountpt);
1442 yaffs_mount(mountpt);
1445 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1446 for(i = 0; i < 100; i++)
1447 yaffs_write(h,a,100);
1460 yaffs_unmount(mountpt);
1461 yaffs_mount(mountpt);
1463 printf("link test done\n");
1467 void freespace_test(const char *mountpt)
1478 sprintf(a,"%s/aaa",mountpt);
1482 yaffs_mount(mountpt);
1484 f0 = yaffs_freespace(mountpt);
1486 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1488 for(i = 0; i < 100; i++)
1489 yaffs_write(h,a,100);
1493 f1 = yaffs_freespace(mountpt);
1497 f2 = yaffs_freespace(mountpt);
1500 yaffs_unmount(mountpt);
1501 yaffs_mount(mountpt);
1503 f3 = yaffs_freespace(mountpt);
1505 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1510 void simple_rw_test(const char *mountpt)
1519 sprintf(a,"%s/aaa",mountpt);
1523 yaffs_mount(mountpt);
1527 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1529 for(i = 100000;i < 200000; i++){
1530 result = yaffs_write(h,&i,sizeof(i));
1534 printf("write error\n");
1541 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1544 yaffs_lseek(h,0,SEEK_SET);
1546 for(i = 100000; i < 200000; i++){
1547 result = yaffs_read(h,&x,sizeof(x));
1549 if(result != 4 || x != i){
1550 printf("read error %d %x %x\n",i,result,x);
1554 printf("Simple rw test passed\n");
1561 void scan_deleted_files_test(const char *mountpt)
1573 sprintf(sub,"%s/sdir",mountpt);
1576 for(j = 0; j < 10; j++)
1578 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1579 yaffs_mount(mountpt);
1583 p = (j & 0) ? mountpt: sub;
1585 for(i = 0; i < 100; i++)
1587 sprintf(fn,"%s/%d",p,i);
1591 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1592 for(k = 0; k < 1000; k++)
1593 yaffs_write(h,fn,100);
1600 for(i = 0; i < 10; i++)
1602 sprintf(fn,"%s/%d",p,i);
1610 yaffs_unmount(mountpt);
1619 void write_10k(int h)
1622 const char *s="0123456789";
1623 for(i = 0; i < 1000; i++)
1624 yaffs_write(h,s,10);
1627 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1633 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1635 for(i = 0; i < 100000; i+= 10000)
1640 offs = yaffs_lseek(h1,0,SEEK_CUR);
1643 printf("Could not write file\n");
1647 for(i = 0; i < 100000; i+= 10000)
1652 offs = yaffs_lseek(h1,0,SEEK_CUR);
1655 printf("Could not write file\n");
1659 yaffs_unlink(fdel1);
1664 void verify_200k_file(const char *fn)
1669 const char *s="0123456789";
1672 h1 = yaffs_open(fn, O_RDONLY, 0);
1674 for(i = 0; i < 200000 && errCount < 10; i+= 10)
1676 yaffs_read(h1,x,10);
1677 if(strncmp(x,s,10) != 0)
1679 printf("File %s verification failed at %d\n",fn,i);
1684 printf("Too many errors... aborted\n");
1691 void check_resize_gc_bug(const char *mountpt)
1700 sprintf(a,"%s/a",mountpt);
1701 sprintf(b,"%s/b",mountpt);
1702 sprintf(c,"%s/c",mountpt);
1708 yaffs_mount(mountpt);
1712 for(i = 0; i < 50; i++)
1714 printf("A\n");write_200k_file(a,"",c);
1715 printf("B\n");verify_200k_file(a);
1716 printf("C\n");write_200k_file(b,a,c);
1717 printf("D\n");verify_200k_file(b);
1718 yaffs_unmount(mountpt);
1719 yaffs_mount(mountpt);
1720 printf("E\n");verify_200k_file(a);
1721 printf("F\n");verify_200k_file(b);
1727 void multi_mount_test(const char *mountpt,int nmounts)
1737 sprintf(a,"%s/a",mountpt);
1741 for(i = 0; i < nmounts; i++){
1747 static char xx[1000];
1749 printf("############### Iteration %d Start\n",i);
1750 if(1 || i == 0 || i == 5)
1751 yaffs_mount(mountpt);
1753 dump_directory_tree(mountpt);
1758 sprintf(xx,"%s/0",a);
1759 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1761 sprintf(xx,"%s/1",a);
1762 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1764 for(j = 0; j < 200; j++){
1765 yaffs_write(h0,xx,1000);
1766 yaffs_write(h1,xx,1000);
1769 len0 = yaffs_lseek(h0,0,SEEK_END);
1770 len1 = yaffs_lseek(h1,0,SEEK_END);
1772 yaffs_lseek(h0,0,SEEK_SET);
1773 yaffs_lseek(h1,0,SEEK_SET);
1775 for(j = 0; j < 200; j++){
1776 yaffs_read(h0,xx,1000);
1777 yaffs_read(h1,xx,1000);
1781 yaffs_truncate(h0,0);
1785 printf("########### %d\n",i);
1786 dump_directory_tree(mountpt);
1788 if(1 || i == 4 || i == nmounts -1)
1789 yaffs_unmount(mountpt);
1794 void small_mount_test(const char *mountpt,int nmounts)
1810 sprintf(a,"%s/a",mountpt);
1816 for(i = 0; i < nmounts; i++){
1818 static char xx[1000];
1820 printf("############### Iteration %d Start\n",i);
1821 if(1 || i == 0 || i == 5)
1822 yaffs_mount(mountpt);
1824 dump_directory_tree(mountpt);
1828 sprintf(xx,"%s/0",a);
1831 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1832 for(j = 0; j < 130; j++)
1833 yaffs_write(h0,xx,1000);
1837 h0 = yaffs_open(xx,O_RDONLY,0);
1839 sprintf(xx,"%s/1",a);
1840 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1842 while((nread = yaffs_read(h0,xx,1000)) > 0)
1843 yaffs_write(h1,xx,nread);
1846 len0 = yaffs_lseek(h0,0,SEEK_END);
1847 len1 = yaffs_lseek(h1,0,SEEK_END);
1849 yaffs_lseek(h0,0,SEEK_SET);
1850 yaffs_lseek(h1,0,SEEK_SET);
1852 for(j = 0; j < 200; j++){
1853 yaffs_read(h0,xx,1000);
1854 yaffs_read(h1,xx,1000);
1860 printf("########### %d\n",i);
1861 dump_directory_tree(mountpt);
1863 if(1 || i == 4 || i == nmounts -1)
1864 yaffs_unmount(mountpt);
1871 void small_overwrite_test(const char *mountpt,int nmounts)
1887 sprintf(a,"%s/a",mountpt);
1893 for(i = 0; i < nmounts; i++){
1895 static char xx[8000];
1897 printf("############### Iteration %d Start\n",i);
1899 yaffs_mount(mountpt);
1901 dump_directory_tree(mountpt);
1905 sprintf(xx,"%s/0",a);
1906 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1907 sprintf(xx,"%s/1",a);
1908 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1910 for(j = 0; j < 1000000; j+=1000){
1911 yaffs_truncate(h0,j);
1912 yaffs_lseek(h0,j,SEEK_SET);
1913 yaffs_write(h0,xx,7000);
1914 yaffs_write(h1,xx,7000);
1922 printf("########### %d\n",i);
1923 dump_directory_tree(mountpt);
1926 yaffs_unmount(mountpt);
1931 void yaffs_touch(const char *fn)
1933 yaffs_chmod(fn, S_IREAD | S_IWRITE);
1936 void checkpoint_fill_test(const char *mountpt,int nmounts)
1947 sprintf(a,"%s/a",mountpt);
1954 for(i = 0; i < nmounts; i++){
1955 printf("############### Iteration %d Start\n",i);
1956 yaffs_mount(mountpt);
1957 dump_directory_tree(mountpt);
1960 sprintf(b,"%s/zz",a);
1962 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
1965 while(yaffs_write(h,c,50) == 50){}
1969 for(j = 0; j < 2; j++){
1970 printf("touch %d\n",j);
1972 yaffs_unmount(mountpt);
1973 yaffs_mount(mountpt);
1976 dump_directory_tree(mountpt);
1977 yaffs_unmount(mountpt);
1982 int make_file2(const char *name1, const char *name2,int syz)
1992 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1994 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1996 while(syz > 0 && n > 0){
1997 i = (syz > 2500) ? 2500 : syz;
1998 n = yaffs_write(h1,xx,i);
1999 n = yaffs_write(h2,xx,i);
2008 extern void SetCheckpointReservedBlocks(int n);
2010 void checkpoint_upgrade_test(const char *mountpt,int nmounts)
2022 sprintf(a,"%s/a",mountpt);
2027 printf("Create start condition\n");
2029 SetCheckpointReservedBlocks(0);
2030 yaffs_mount(mountpt);
2032 sprintf(b,"%s/zz",a);
2033 sprintf(c,"%s/xx",a);
2034 make_file2(b,c,2000000);
2035 sprintf(d,"%s/aa",a);
2036 make_file2(d,NULL,500000000);
2037 dump_directory_tree(mountpt);
2039 printf("Umount/mount attempt full\n");
2040 yaffs_unmount(mountpt);
2042 SetCheckpointReservedBlocks(10);
2043 yaffs_mount(mountpt);
2045 printf("unlink small file\n");
2047 dump_directory_tree(mountpt);
2049 printf("Umount/mount attempt\n");
2050 yaffs_unmount(mountpt);
2051 yaffs_mount(mountpt);
2053 for(j = 0; j < 500; j++){
2054 printf("***** touch %d\n",j);
2055 dump_directory_tree(mountpt);
2057 yaffs_unmount(mountpt);
2058 yaffs_mount(mountpt);
2061 for(j = 0; j < 500; j++){
2062 printf("***** touch %d\n",j);
2063 dump_directory_tree(mountpt);
2065 yaffs_unmount(mountpt);
2066 yaffs_mount(mountpt);
2070 void huge_array_test(const char *mountpt,int n)
2082 sprintf(a,"mount point %s",mountpt);
2088 yaffs_mount(mountpt);
2093 printf("\n\n START run\n\n");
2094 while(yaffs_freespace(mountpt) > 25000000){
2095 sprintf(a,"%s/file%d",mountpt,fnum);
2097 printf("create file %s\n",a);
2098 create_file_of_size(a,10000000);
2099 printf("verifying file %s\n",a);
2100 verify_file_of_size(a,10000000);
2103 printf("\n\n verification/deletion\n\n");
2105 for(i = 0; i < fnum; i++){
2106 sprintf(a,"%s/file%d",mountpt,i);
2107 printf("verifying file %s\n",a);
2108 verify_file_of_size(a,10000000);
2109 printf("deleting file %s\n",a);
2112 printf("\n\n done \n\n");
2120 int main(int argc, char *argv[])
2122 //return long_test(argc,argv);
2124 //return cache_read_test();
2126 //resize_stress_test_no_grow("/flash",20);
2128 //huge_directory_test_on_path("/ram2k");
2130 //yaffs_backward_scan_test("/flash/flash");
2131 // yaffs_device_flush_test("/flash/flash");
2134 //scan_pattern_test("/flash",10000,10);
2135 //short_scan_test("/flash/flash",40000,200);
2136 //small_mount_test("/flash/flash",1000);
2137 small_overwrite_test("/flash/flash",1000);
2138 //checkpoint_fill_test("/flash/flash",20);
2139 //checkpoint_upgrade_test("/flash/flash",20);
2140 // huge_array_test("/flash/flash",10);
2145 //long_test_on_path("/ram2k");
2146 // long_test_on_path("/flash");
2147 //simple_rw_test("/flash/flash");
2148 //fill_disk_test("/flash/flash");
2149 // rename_over_test("/flash");
2150 //lookup_test("/flash");
2151 //freespace_test("/flash/flash");
2153 //link_test("/flash/flash");
2158 // cache_bypass_bug_test();
2160 //free_space_check();
2162 //check_resize_gc_bug("/flash");