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)
170 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
172 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
174 while (iterations > 0)
176 yaffs_write(h,fn,strlen(fn));
182 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
190 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
192 iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
193 while (iterations > 0)
195 yaffs_write(h,fn,strlen(fn));
199 yaffs_truncate(h,reSyze);
201 yaffs_lseek(h,0,SEEK_SET);
202 iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
203 while (iterations > 0)
205 yaffs_write(h,fn,strlen(fn));
213 void do_some_file_stuff(const char *path)
218 sprintf(fn,"%s/%s",path,"f1");
219 create_file_of_size(fn,10000);
221 sprintf(fn,"%s/%s",path,"fdel");
222 create_file_of_size(fn,10000);
225 sprintf(fn,"%s/%s",path,"f2");
227 create_resized_file_of_size(fn,10000,3000,4000);
230 void yaffs_backward_scan_test(const char *path)
238 do_some_file_stuff(path);
240 sprintf(fn,"%s/ddd",path);
244 do_some_file_stuff(fn);
254 void yaffs_device_flush_test(const char *path)
264 do_some_file_stuff(path);
266 // Open and add some data to a few files
267 for(i = 0; i < 10; i++) {
269 sprintf(fn,"%s/ff%d",path,i);
271 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
272 yaffs_write(h,xxzz,2000);
273 yaffs_write(h,xxzz,2000);
282 void short_scan_test(const char *path, int fsize, int niterations)
287 sprintf(fn,"%s/%s",path,"f1");
290 for(i = 0; i < niterations; i++)
292 printf("\n*****************\nIteration %d\n",i);
294 printf("\nmount: Directory look-up of %s\n",path);
296 make_a_file(fn,1,fsize);
303 void scan_pattern_test(const char *path, int fsize, int niterations)
310 sprintf(fn[0],"%s/%s",path,"f0");
311 sprintf(fn[1],"%s/%s",path,"f1");
312 sprintf(fn[2],"%s/%s",path,"f2");
316 for(i = 0; i < niterations; i++)
318 printf("\n*****************\nIteration %d\n",i);
320 printf("\nmount: Directory look-up of %s\n",path);
322 for(j = 0; j < 3; j++)
324 result = dump_file_data(fn[j]);
325 result = check_pattern_file(fn[j]);
326 make_pattern_file(fn[j],fsize);
327 result = dump_file_data(fn[j]);
328 result = check_pattern_file(fn[j]);
334 void fill_disk(char *path,int nfiles)
343 for(n = 0; n < nfiles; n++)
345 sprintf(str,"%s/%d",path,n);
347 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
349 printf("writing file %s handle %d ",str, h);
351 while ((result = yaffs_write(h,xx,600)) == 600)
353 f = yaffs_freespace(path);
355 result = yaffs_close(h);
356 printf(" close %d\n",result);
360 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
366 for(i = 0; i < ncycles; i++)
368 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
369 fill_disk(path,nfiles);
371 for(j = 0; j < nfiles; j++)
373 sprintf(str,"%s/%d",path,j);
374 result = yaffs_unlink(str);
375 printf("unlinking file %s, result %d\n",str,result);
381 void fill_files(char *path,int flags, int maxIterations,int siz)
391 sprintf(str,"%s/%d",path,i);
392 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
397 for(j = 0; j < siz; j++)
399 yaffs_write(h,str,1);
408 } while(h >= 0 && i < maxIterations);
414 sprintf(str,"%s/%d",path,i);
415 printf("unlink %s\n",str);
417 } while(yaffs_unlink(str) >= 0);
421 void leave_unlinked_file(char *path,int maxIterations,int siz)
430 sprintf(str,"%s/%d",path,i);
431 printf("create %s\n",str);
432 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
438 } while(h < 0 && i < maxIterations);
442 for(i = 0; i < siz; i++)
444 yaffs_write(h,str,1);
448 printf("Leaving file %s open\n",str);
452 void dumpDirFollow(const char *dname)
459 d = yaffs_opendir(dname);
463 printf("opendir failed\n");
467 while((de = yaffs_readdir(d)) != NULL)
469 sprintf(str,"%s/%s",dname,de->d_name);
473 printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
474 switch(s.st_mode & S_IFMT)
476 case S_IFREG: printf("data file"); break;
477 case S_IFDIR: printf("directory"); break;
478 case S_IFLNK: printf("symlink -->");
479 if(yaffs_readlink(str,str,100) < 0)
482 printf("\"%s\"",str);
484 default: printf("unknown"); break;
494 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
499 void dump_directory_tree_worker(const char *dname,int recursive)
506 d = yaffs_opendir(dname);
510 printf("opendir failed\n");
514 while((de = yaffs_readdir(d)) != NULL)
516 sprintf(str,"%s/%s",dname,de->d_name);
520 printf("%s length %d mode %X ",str,(int)s.st_size,s.st_mode);
521 switch(s.st_mode & S_IFMT)
523 case S_IFREG: printf("data file"); break;
524 case S_IFDIR: printf("directory"); break;
525 case S_IFLNK: printf("symlink -->");
526 if(yaffs_readlink(str,str,100) < 0)
529 printf("\"%s\"",str);
531 default: printf("unknown"); break;
536 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
537 dump_directory_tree_worker(str,1);
546 static void dump_directory_tree(const char *dname)
548 dump_directory_tree_worker(dname,1);
550 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
553 void dumpDir(const char *dname)
554 { dump_directory_tree_worker(dname,0);
556 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
560 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
564 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
566 fd = yaffs_open(path,tflags,0);
568 if((fd >= 0) != (expectedResult > 0))
570 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
574 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
583 int long_test(int argc, char *argv[])
594 struct yaffs_stat ystat;
598 yaffs_mount("/boot");
599 yaffs_mount("/data");
600 yaffs_mount("/flash");
603 printf("\nDirectory look-up of /boot\n");
605 printf("\nDirectory look-up of /data\n");
607 printf("\nDirectory look-up of /flash\n");
610 //leave_unlinked_file("/flash",20000,0);
611 //leave_unlinked_file("/data",20000,0);
613 leave_unlinked_file("/ram",20,0);
616 f = yaffs_open("/boot/b1", O_RDONLY,0);
618 printf("open /boot/b1 readonly, f=%d\n",f);
620 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
622 printf("open /boot/b1 O_CREAT, f=%d\n",f);
625 r = yaffs_write(f,"hello",1);
626 printf("write %d attempted to write to a read-only file\n",r);
630 printf("close %d\n",r);
632 f = yaffs_open("/boot/b1", O_RDWR,0);
634 printf("open /boot/b1 O_RDWR,f=%d\n",f);
637 r = yaffs_write(f,"hello",2);
638 printf("write %d attempted to write to a writeable file\n",r);
639 r = yaffs_write(f,"world",3);
640 printf("write %d attempted to write to a writeable file\n",r);
642 r= yaffs_lseek(f,0,SEEK_END);
643 printf("seek end %d\n",r);
645 r = yaffs_read(f,buffer,10);
646 printf("read %d \"%s\"\n",r,buffer);
647 r= yaffs_lseek(f,0,SEEK_SET);
648 printf("seek set %d\n",r);
650 r = yaffs_read(f,buffer,10);
651 printf("read %d \"%s\"\n",r,buffer);
653 r = yaffs_read(f,buffer,10);
654 printf("read %d \"%s\"\n",r,buffer);
656 // Check values reading at end.
657 // A read past end of file should return 0 for 0 bytes read.
659 r= yaffs_lseek(f,0,SEEK_END);
660 r = yaffs_read(f,buffer,10);
661 printf("read at end returned %d\n",r);
662 r= yaffs_lseek(f,500,SEEK_END);
663 r = yaffs_read(f,buffer,10);
664 printf("read past end returned %d\n",r);
668 printf("close %d\n",r);
670 copy_in_a_file("/boot/yyfile","xxx");
672 // Create a file with a long name
674 copy_in_a_file("/boot/file with a long name","xxx");
677 printf("\nDirectory look-up of /boot\n");
681 r = yaffs_stat("/boot/file with a long name",&ystat);
685 r = yaffs_rename("/boot/file with a long name","/boot/r1");
687 printf("\nDirectory look-up of /boot\n");
691 r = yaffs_unlink("/boot/r1");
693 printf("\nDirectory look-up of /boot\n");
698 r = yaffs_mkdir("/boot/directory1",0);
700 printf("\nDirectory look-up of /boot\n");
702 printf("\nDirectory look-up of /boot/directory1\n");
703 dumpDir("/boot/directory1");
705 // add a file to the directory
706 copy_in_a_file("/boot/directory1/file with a long name","xxx");
708 printf("\nDirectory look-up of /boot\n");
710 printf("\nDirectory look-up of /boot/directory1\n");
711 dumpDir("/boot/directory1");
713 // Attempt to delete directory (should fail)
715 r = yaffs_rmdir("/boot/directory1");
717 printf("\nDirectory look-up of /boot\n");
719 printf("\nDirectory look-up of /boot/directory1\n");
720 dumpDir("/boot/directory1");
722 // Delete file first, then rmdir should work
723 r = yaffs_unlink("/boot/directory1/file with a long name");
724 r = yaffs_rmdir("/boot/directory1");
727 printf("\nDirectory look-up of /boot\n");
729 printf("\nDirectory look-up of /boot/directory1\n");
730 dumpDir("/boot/directory1");
733 fill_disk_and_delete("/boot",20,20);
735 printf("\nDirectory look-up of /boot\n");
739 yaffs_symlink("yyfile","/boot/slink");
741 yaffs_readlink("/boot/slink",str,100);
742 printf("symlink alias is %s\n",str);
747 printf("\nDirectory look-up of /boot\n");
749 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
750 dumpDirFollow("/boot");
751 printf("\nDirectory look-up of /boot/directory1\n");
752 dumpDir("/boot/directory1");
754 h = yaffs_open("/boot/slink",O_RDWR,0);
756 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
760 yaffs_unlink("/boot/slink");
763 printf("\nDirectory look-up of /boot\n");
768 yaffs_stat("/boot/yyfile",&ystat);
769 temp_mode = ystat.st_mode;
771 yaffs_chmod("/boot/yyfile",0x55555);
772 printf("\nDirectory look-up of /boot\n");
775 yaffs_chmod("/boot/yyfile",temp_mode);
776 printf("\nDirectory look-up of /boot\n");
779 // Permission checks...
780 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
781 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
782 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
784 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
785 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
786 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
788 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
789 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
790 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
792 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
793 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
794 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
796 yaffs_chmod("/boot/yyfile",temp_mode);
798 //create a zero-length file and unlink it (test for scan bug)
800 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
803 yaffs_unlink("/boot/zlf");
806 yaffs_DumpDevStruct("/boot");
808 fill_disk_and_delete("/boot",20,20);
810 yaffs_DumpDevStruct("/boot");
812 fill_files("/boot",1,10000,0);
813 fill_files("/boot",1,10000,5000);
814 fill_files("/boot",2,10000,0);
815 fill_files("/boot",2,10000,5000);
817 leave_unlinked_file("/data",20000,0);
818 leave_unlinked_file("/data",20000,5000);
819 leave_unlinked_file("/data",20000,5000);
820 leave_unlinked_file("/data",20000,5000);
821 leave_unlinked_file("/data",20000,5000);
822 leave_unlinked_file("/data",20000,5000);
824 yaffs_DumpDevStruct("/boot");
825 yaffs_DumpDevStruct("/data");
833 int huge_directory_test_on_path(char *path)
853 struct yaffs_stat ystat;
859 // Create a large number of files
861 for(i = 0; i < 2000; i++)
863 sprintf(str,"%s/%d",path,i);
865 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
871 d = yaffs_opendir(path);
874 while((de = yaffs_readdir(d)) != NULL) {
875 if (total >lastTotal+100*9*1024||(i & 1023)==0){
876 printf("files = %d, total = %d\n",i, total);
880 sprintf(str,"%s/%s",path,de->d_name);
882 switch(s.st_mode & S_IFMT){
884 //printf("data file");
896 int yaffs_scan_test(const char *path)
901 void rename_over_test(const char *mountpt)
907 sprintf(a,"%s/a",mountpt);
908 sprintf(b,"%s/b",mountpt);
912 yaffs_mount(mountpt);
913 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
915 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
917 yaffs_rename(a,b); // rename over
918 yaffs_rename(b,a); // rename back again (not renaimng over)
919 yaffs_rename(a,b); // rename back again (not renaimng over)
922 yaffs_unmount(mountpt);
926 int resize_stress_test(const char *path)
941 sprintf(aname,"%s%s",path,"/a");
942 sprintf(bname,"%s%s",path,"/b");
944 memset(abuffer,'a',1000);
945 memset(bbuffer,'b',1000);
947 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
948 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
950 printf(" %s %d %s %d\n",aname,a,bname,b);
954 for(j = 0; j < 100; j++)
956 yaffs_lseek(a,0,SEEK_END);
959 for(i = 0; i <20000; i++)
961 //r = yaffs_lseek(b,i,SEEK_SET);
962 //r = yaffs_write(b,bbuffer,1000);
967 int syz = yaffs_lseek(a,0,SEEK_END);
971 yaffs_truncate(a,syz);
977 r = yaffs_lseek(a,i * 500,SEEK_SET);
978 r = yaffs_write(a,abuffer,1000);
990 int resize_stress_test_no_grow_complex(const char *path,int iters)
1005 sprintf(aname,"%s%s",path,"/a");
1006 sprintf(bname,"%s%s",path,"/b");
1008 memset(abuffer,'a',1000);
1009 memset(bbuffer,'b',1000);
1011 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1012 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1014 printf(" %s %d %s %d\n",aname,a,bname,b);
1018 for(j = 0; j < iters; j++)
1020 yaffs_lseek(a,0,SEEK_END);
1023 for(i = 0; i <20000; i++)
1025 //r = yaffs_lseek(b,i,SEEK_SET);
1026 //r = yaffs_write(b,bbuffer,1000);
1031 int syz = yaffs_lseek(a,0,SEEK_END);
1037 if(syz < 0) syz = 0;
1038 yaffs_truncate(a,syz);
1039 syz = yaffs_lseek(a,0,SEEK_END);
1040 printf("shrink to %d\n",syz);
1048 r = yaffs_lseek(a,500,SEEK_END);
1049 r = yaffs_write(a,abuffer,1000);
1055 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1063 int resize_stress_test_no_grow(const char *path,int iters)
1078 sprintf(aname,"%s%s",path,"/a");
1079 sprintf(bname,"%s%s",path,"/b");
1081 memset(abuffer,'a',1000);
1082 memset(bbuffer,'b',1000);
1084 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1085 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1087 printf(" %s %d %s %d\n",aname,a,bname,b);
1091 for(j = 0; j < iters; j++)
1093 yaffs_lseek(a,0,SEEK_END);
1096 for(i = 0; i <20000; i++)
1098 //r = yaffs_lseek(b,i,SEEK_SET);
1099 //r = yaffs_write(b,bbuffer,1000);
1104 int syz = yaffs_lseek(a,0,SEEK_END);
1110 if(syz < 0) syz = 0;
1111 yaffs_truncate(a,syz);
1112 syz = yaffs_lseek(a,0,SEEK_END);
1113 printf("shrink to %d\n",syz);
1121 r = yaffs_lseek(a,-500,SEEK_END);
1122 r = yaffs_write(a,abuffer,1000);
1128 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1136 int directory_rename_test(void)
1141 yaffs_mount("/ram");
1142 yaffs_mkdir("/ram/a",0);
1143 yaffs_mkdir("/ram/a/b",0);
1144 yaffs_mkdir("/ram/c",0);
1146 printf("\nDirectory look-up of /ram\n");
1149 dumpDir("/ram/a/b");
1151 printf("Do rename (should fail)\n");
1153 r = yaffs_rename("/ram/a","/ram/a/b/d");
1154 printf("\nDirectory look-up of /ram\n");
1157 dumpDir("/ram/a/b");
1159 printf("Do rename (should not fail)\n");
1161 r = yaffs_rename("/ram/c","/ram/a/b/d");
1162 printf("\nDirectory look-up of /ram\n");
1165 dumpDir("/ram/a/b");
1172 int cache_read_test(void)
1176 int sizeOfFiles = 500000;
1181 yaffs_mount("/boot");
1183 make_a_file("/boot/a",'a',sizeOfFiles);
1184 make_a_file("/boot/b",'b',sizeOfFiles);
1186 a = yaffs_open("/boot/a",O_RDONLY,0);
1187 b = yaffs_open("/boot/b",O_RDONLY,0);
1188 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1192 if (i > 100) i = 100;
1194 yaffs_read(a,buffer,i);
1195 yaffs_read(b,buffer,i);
1196 yaffs_write(c,buffer,i);
1197 } while(sizeOfFiles > 0);
1205 int cache_bypass_bug_test(void)
1207 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1208 // resulting in erroneous reads after writes.
1209 // This bug has been fixed.
1216 memset(buffer1,0,sizeof(buffer1));
1217 memset(buffer2,0,sizeof(buffer2));
1221 yaffs_mount("/boot");
1223 // Create a file of 2000 bytes.
1224 make_a_file("/boot/a",'X',2000);
1226 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1228 // Write a short sequence to the file.
1229 // This will go into the cache.
1230 yaffs_lseek(a,0,SEEK_SET);
1231 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1233 // Read a short sequence from the file.
1234 // This will come from the cache.
1235 yaffs_lseek(a,0,SEEK_SET);
1236 yaffs_read(a,buffer1,30);
1238 // Read a page size sequence from the file.
1239 yaffs_lseek(a,0,SEEK_SET);
1240 yaffs_read(a,buffer2,512);
1242 printf("buffer 1 %s\n",buffer1);
1243 printf("buffer 2 %s\n",buffer2);
1245 if(strncmp(buffer1,buffer2,20))
1247 printf("Cache bypass bug detected!!!!!\n");
1255 int free_space_check(void)
1260 yaffs_mount("/boot");
1261 fill_disk("/boot/",2);
1262 f = yaffs_freespace("/boot");
1264 printf("%d free when disk full\n",f);
1268 int truncate_test(void)
1278 yaffs_mount("/boot");
1280 yaffs_unlink("/boot/trunctest");
1282 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1284 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1286 yaffs_truncate(a,3);
1287 l= yaffs_lseek(a,0,SEEK_END);
1289 printf("truncated length is %d\n",l);
1291 yaffs_lseek(a,5,SEEK_SET);
1292 yaffs_write(a,"1",1);
1294 yaffs_lseek(a,0,SEEK_SET);
1296 r = yaffs_read(a,y,10);
1298 printf("read %d bytes:",r);
1300 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1312 void fill_disk_test(const char *mountpt)
1317 for(i = 0; i < 5; i++)
1319 yaffs_mount(mountpt);
1320 fill_disk_and_delete(mountpt,100,i+1);
1321 yaffs_unmount(mountpt);
1328 void lookup_test(const char *mountpt)
1338 struct yaffs_stat s;
1343 yaffs_mount(mountpt);
1345 d = yaffs_opendir(mountpt);
1349 printf("opendir failed\n");
1354 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1356 printf("unlinking %s\n",de->d_name);
1357 yaffs_unlink(de->d_name);
1360 printf("%d files deleted\n",i);
1364 for(i = 0; i < 2000; i++){
1365 sprintf(a,"%s/%d",mountpt,i);
1366 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1371 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1373 printf("%d %s\n",i,de->d_name);
1376 printf("%d files listed\n\n\n",i);
1383 for(i = 0; i < 2000; i++){
1384 sprintf(a,"%s/%d",mountpt,i);
1389 yaffs_unmount(mountpt);
1393 void link_test(const char *mountpt)
1405 sprintf(a,"%s/aaa",mountpt);
1406 sprintf(b,"%s/bbb",mountpt);
1407 sprintf(c,"%s/ccc",mountpt);
1411 yaffs_mount(mountpt);
1414 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1415 for(i = 0; i < 100; i++)
1416 yaffs_write(h,a,100);
1429 yaffs_unmount(mountpt);
1430 yaffs_mount(mountpt);
1432 printf("link test done\n");
1436 void freespace_test(const char *mountpt)
1447 sprintf(a,"%s/aaa",mountpt);
1451 yaffs_mount(mountpt);
1453 f0 = yaffs_freespace(mountpt);
1455 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1457 for(i = 0; i < 100; i++)
1458 yaffs_write(h,a,100);
1462 f1 = yaffs_freespace(mountpt);
1466 f2 = yaffs_freespace(mountpt);
1469 yaffs_unmount(mountpt);
1470 yaffs_mount(mountpt);
1472 f3 = yaffs_freespace(mountpt);
1474 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1479 void simple_rw_test(const char *mountpt)
1488 sprintf(a,"%s/aaa",mountpt);
1492 yaffs_mount(mountpt);
1496 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1498 for(i = 100000;i < 200000; i++){
1499 result = yaffs_write(h,&i,sizeof(i));
1503 printf("write error\n");
1510 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1513 yaffs_lseek(h,0,SEEK_SET);
1515 for(i = 100000; i < 200000; i++){
1516 result = yaffs_read(h,&x,sizeof(x));
1518 if(result != 4 || x != i){
1519 printf("read error %d %x %x\n",i,result,x);
1523 printf("Simple rw test passed\n");
1530 void scan_deleted_files_test(const char *mountpt)
1542 sprintf(sub,"%s/sdir",mountpt);
1545 for(j = 0; j < 10; j++)
1547 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1548 yaffs_mount(mountpt);
1552 p = (j & 0) ? mountpt: sub;
1554 for(i = 0; i < 100; i++)
1556 sprintf(fn,"%s/%d",p,i);
1560 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1561 for(k = 0; k < 1000; k++)
1562 yaffs_write(h,fn,100);
1569 for(i = 0; i < 10; i++)
1571 sprintf(fn,"%s/%d",p,i);
1579 yaffs_unmount(mountpt);
1588 void write_10k(int h)
1591 const char *s="0123456789";
1592 for(i = 0; i < 1000; i++)
1593 yaffs_write(h,s,10);
1596 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1602 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1604 for(i = 0; i < 100000; i+= 10000)
1609 offs = yaffs_lseek(h1,0,SEEK_CUR);
1612 printf("Could not write file\n");
1616 for(i = 0; i < 100000; i+= 10000)
1621 offs = yaffs_lseek(h1,0,SEEK_CUR);
1624 printf("Could not write file\n");
1628 yaffs_unlink(fdel1);
1633 void verify_200k_file(const char *fn)
1638 const char *s="0123456789";
1641 h1 = yaffs_open(fn, O_RDONLY, 0);
1643 for(i = 0; i < 200000 && errCount < 10; i+= 10)
1645 yaffs_read(h1,x,10);
1646 if(strncmp(x,s,10) != 0)
1648 printf("File %s verification failed at %d\n",fn,i);
1653 printf("Too many errors... aborted\n");
1660 void check_resize_gc_bug(const char *mountpt)
1669 sprintf(a,"%s/a",mountpt);
1670 sprintf(b,"%s/b",mountpt);
1671 sprintf(c,"%s/c",mountpt);
1677 yaffs_mount(mountpt);
1681 for(i = 0; i < 50; i++)
1683 printf("A\n");write_200k_file(a,"",c);
1684 printf("B\n");verify_200k_file(a);
1685 printf("C\n");write_200k_file(b,a,c);
1686 printf("D\n");verify_200k_file(b);
1687 yaffs_unmount(mountpt);
1688 yaffs_mount(mountpt);
1689 printf("E\n");verify_200k_file(a);
1690 printf("F\n");verify_200k_file(b);
1696 void multi_mount_test(const char *mountpt,int nmounts)
1706 sprintf(a,"%s/a",mountpt);
1713 for(i = 0; i < nmounts; i++){
1714 printf("############### Iteration %d Start\n",i);
1715 yaffs_mount(mountpt);
1716 dump_directory_tree(mountpt);
1718 for(j = 0; j < i; j++){
1719 sprintf(b,"%s/%d",a,j);
1720 verify_200k_file(b);
1722 sprintf(b,"%s/%d",a,i);
1724 write_200k_file(b,"","");
1726 printf("######## Iteration %d Start\n",i);
1727 dump_directory_tree(mountpt);
1729 yaffs_unmount(mountpt);
1734 int main(int argc, char *argv[])
1736 //return long_test(argc,argv);
1738 //return cache_read_test();
1740 //resize_stress_test_no_grow("/flash",20);
1742 //huge_directory_test_on_path("/ram2k");
1744 //yaffs_backward_scan_test("/flash/flash");
1745 // yaffs_device_flush_test("/flash/flash");
1748 //scan_pattern_test("/flash",10000,10);
1749 //short_scan_test("/flash/flash",40000,200);
1750 multi_mount_test("/flash/flash",20);
1754 //long_test_on_path("/ram2k");
1755 // long_test_on_path("/flash");
1756 //simple_rw_test("/flash/flash");
1757 //fill_disk_test("/flash/flash");
1758 // rename_over_test("/flash");
1759 //lookup_test("/flash");
1760 //freespace_test("/flash/flash");
1762 //link_test("/flash/flash");
1767 // cache_bypass_bug_test();
1769 //free_space_check();
1771 //check_resize_gc_bug("/flash");