2 * Test code for the "direct" interface.
13 void copy_in_a_file(char *yaffsName,char *inName)
16 unsigned char buffer[100];
18 inh = open(inName,O_RDONLY);
19 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
21 while((ni = read(inh,buffer,100)) > 0)
23 no = yaffs_write(outh,buffer,ni);
26 printf("problem writing yaffs file\n");
35 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
39 unsigned char buffer[100];
41 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
43 memset(buffer,bval,100);
50 yaffs_write(outh,buffer,i);
52 } while (sizeOfFile > 0);
62 void fill_disk(char *path,int nfiles)
70 for(n = 0; n < nfiles; n++)
72 sprintf(str,"%s/%d",path,n);
74 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
76 printf("writing file %s handle %d ",str, h);
78 while ((result = yaffs_write(h,xx,600)) == 600)
82 result = yaffs_close(h);
83 printf(" close %d\n",result);
87 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
93 for(i = 0; i < ncycles; i++)
95 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
96 fill_disk(path,nfiles);
98 for(j = 0; j < nfiles; j++)
100 sprintf(str,"%s/%d",path,j);
101 result = yaffs_unlink(str);
102 printf("unlinking file %s, result %d\n",str,result);
108 void fill_files(char *path,int flags, int maxIterations,int siz)
118 sprintf(str,"%s/%d",path,i);
119 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
124 for(j = 0; j < siz; j++)
126 yaffs_write(h,str,1);
135 } while(h >= 0 && i < maxIterations);
141 sprintf(str,"%s/%d",path,i);
142 printf("unlink %s\n",str);
144 } while(yaffs_unlink(str) >= 0);
148 void leave_unlinked_file(char *path,int maxIterations,int siz)
157 sprintf(str,"%s/%d",path,i);
158 printf("create %s\n",str);
159 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
165 } while(h < 0 && i < maxIterations);
169 for(i = 0; i < siz; i++)
171 yaffs_write(h,str,1);
175 printf("Leaving file %s open\n",str);
179 void dumpDirFollow(const char *dname)
186 d = yaffs_opendir(dname);
190 printf("opendir failed\n");
194 while((de = yaffs_readdir(d)) != NULL)
196 sprintf(str,"%s/%s",dname,de->d_name);
200 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
201 switch(s.st_mode & S_IFMT)
203 case S_IFREG: printf("data file"); break;
204 case S_IFDIR: printf("directory"); break;
205 case S_IFLNK: printf("symlink -->");
206 if(yaffs_readlink(str,str,100) < 0)
209 printf("\"%s\"",str);
211 default: printf("unknown"); break;
221 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
224 void dumpDir(const char *dname)
231 d = yaffs_opendir(dname);
235 printf("opendir failed\n");
239 while((de = yaffs_readdir(d)) != NULL)
241 sprintf(str,"%s/%s",dname,de->d_name);
245 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
246 switch(s.st_mode & S_IFMT)
248 case S_IFREG: printf("data file"); break;
249 case S_IFDIR: printf("directory"); break;
250 case S_IFLNK: printf("symlink -->");
251 if(yaffs_readlink(str,str,100) < 0)
254 printf("\"%s\"",str);
256 default: printf("unknown"); break;
266 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
271 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
275 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
277 fd = yaffs_open(path,tflags,0);
279 if((fd >= 0) != (expectedResult > 0))
281 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
285 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
294 int long_test(int argc, char *argv[])
305 struct yaffs_stat ystat;
309 yaffs_mount("/boot");
310 yaffs_mount("/data");
311 yaffs_mount("/flash");
314 printf("\nDirectory look-up of /boot\n");
316 printf("\nDirectory look-up of /data\n");
318 printf("\nDirectory look-up of /flash\n");
321 //leave_unlinked_file("/flash",20000,0);
322 //leave_unlinked_file("/data",20000,0);
324 leave_unlinked_file("/ram",20,0);
327 f = yaffs_open("/boot/b1", O_RDONLY,0);
329 printf("open /boot/b1 readonly, f=%d\n",f);
331 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
333 printf("open /boot/b1 O_CREAT, f=%d\n",f);
336 r = yaffs_write(f,"hello",1);
337 printf("write %d attempted to write to a read-only file\n",r);
341 printf("close %d\n",r);
343 f = yaffs_open("/boot/b1", O_RDWR,0);
345 printf("open /boot/b1 O_RDWR,f=%d\n",f);
348 r = yaffs_write(f,"hello",2);
349 printf("write %d attempted to write to a writeable file\n",r);
350 r = yaffs_write(f,"world",3);
351 printf("write %d attempted to write to a writeable file\n",r);
353 r= yaffs_lseek(f,0,SEEK_END);
354 printf("seek end %d\n",r);
356 r = yaffs_read(f,buffer,10);
357 printf("read %d \"%s\"\n",r,buffer);
358 r= yaffs_lseek(f,0,SEEK_SET);
359 printf("seek set %d\n",r);
361 r = yaffs_read(f,buffer,10);
362 printf("read %d \"%s\"\n",r,buffer);
364 r = yaffs_read(f,buffer,10);
365 printf("read %d \"%s\"\n",r,buffer);
367 // Check values reading at end.
368 // A read past end of file should return 0 for 0 bytes read.
370 r= yaffs_lseek(f,0,SEEK_END);
371 r = yaffs_read(f,buffer,10);
372 printf("read at end returned %d\n",r);
373 r= yaffs_lseek(f,500,SEEK_END);
374 r = yaffs_read(f,buffer,10);
375 printf("read past end returned %d\n",r);
379 printf("close %d\n",r);
381 copy_in_a_file("/boot/yyfile","xxx");
383 // Create a file with a long name
385 copy_in_a_file("/boot/file with a long name","xxx");
388 printf("\nDirectory look-up of /boot\n");
392 r = yaffs_stat("/boot/file with a long name",&ystat);
396 r = yaffs_rename("/boot/file with a long name","/boot/r1");
398 printf("\nDirectory look-up of /boot\n");
402 r = yaffs_unlink("/boot/r1");
404 printf("\nDirectory look-up of /boot\n");
409 r = yaffs_mkdir("/boot/directory1",0);
411 printf("\nDirectory look-up of /boot\n");
413 printf("\nDirectory look-up of /boot/directory1\n");
414 dumpDir("/boot/directory1");
416 // add a file to the directory
417 copy_in_a_file("/boot/directory1/file with a long name","xxx");
419 printf("\nDirectory look-up of /boot\n");
421 printf("\nDirectory look-up of /boot/directory1\n");
422 dumpDir("/boot/directory1");
424 // Attempt to delete directory (should fail)
426 r = yaffs_rmdir("/boot/directory1");
428 printf("\nDirectory look-up of /boot\n");
430 printf("\nDirectory look-up of /boot/directory1\n");
431 dumpDir("/boot/directory1");
433 // Delete file first, then rmdir should work
434 r = yaffs_unlink("/boot/directory1/file with a long name");
435 r = yaffs_rmdir("/boot/directory1");
438 printf("\nDirectory look-up of /boot\n");
440 printf("\nDirectory look-up of /boot/directory1\n");
441 dumpDir("/boot/directory1");
444 fill_disk_and_delete("/boot",20,20);
446 printf("\nDirectory look-up of /boot\n");
450 yaffs_symlink("yyfile","/boot/slink");
452 yaffs_readlink("/boot/slink",str,100);
453 printf("symlink alias is %s\n",str);
458 printf("\nDirectory look-up of /boot\n");
460 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
461 dumpDirFollow("/boot");
462 printf("\nDirectory look-up of /boot/directory1\n");
463 dumpDir("/boot/directory1");
465 h = yaffs_open("/boot/slink",O_RDWR,0);
467 printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
471 yaffs_unlink("/boot/slink");
474 printf("\nDirectory look-up of /boot\n");
479 yaffs_stat("/boot/yyfile",&ystat);
480 temp_mode = ystat.st_mode;
482 yaffs_chmod("/boot/yyfile",0x55555);
483 printf("\nDirectory look-up of /boot\n");
486 yaffs_chmod("/boot/yyfile",temp_mode);
487 printf("\nDirectory look-up of /boot\n");
490 // Permission checks...
491 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
492 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
493 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
495 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
496 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
497 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
499 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
500 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
501 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
503 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
504 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
505 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
507 yaffs_chmod("/boot/yyfile",temp_mode);
509 //create a zero-length file and unlink it (test for scan bug)
511 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
514 yaffs_unlink("/boot/zlf");
517 yaffs_DumpDevStruct("/boot");
519 fill_disk_and_delete("/boot",20,20);
521 yaffs_DumpDevStruct("/boot");
523 fill_files("/boot",1,10000,0);
524 fill_files("/boot",1,10000,5000);
525 fill_files("/boot",2,10000,0);
526 fill_files("/boot",2,10000,5000);
528 leave_unlinked_file("/data",20000,0);
529 leave_unlinked_file("/data",20000,5000);
530 leave_unlinked_file("/data",20000,5000);
531 leave_unlinked_file("/data",20000,5000);
532 leave_unlinked_file("/data",20000,5000);
533 leave_unlinked_file("/data",20000,5000);
535 yaffs_DumpDevStruct("/boot");
536 yaffs_DumpDevStruct("/data");
546 int directory_rename_test(void)
552 yaffs_mkdir("/ram/a",0);
553 yaffs_mkdir("/ram/a/b",0);
554 yaffs_mkdir("/ram/c",0);
556 printf("\nDirectory look-up of /ram\n");
561 printf("Do rename (should fail)\n");
563 r = yaffs_rename("/ram/a","/ram/a/b/d");
564 printf("\nDirectory look-up of /ram\n");
569 printf("Do rename (should not fail)\n");
571 r = yaffs_rename("/ram/c","/ram/a/b/d");
572 printf("\nDirectory look-up of /ram\n");
582 int cache_read_test(void)
586 int sizeOfFiles = 500000;
591 yaffs_mount("/boot");
593 make_a_file("/boot/a",'a',sizeOfFiles);
594 make_a_file("/boot/b",'b',sizeOfFiles);
596 a = yaffs_open("/boot/a",O_RDONLY,0);
597 b = yaffs_open("/boot/b",O_RDONLY,0);
598 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
602 if (i > 100) i = 100;
604 yaffs_read(a,buffer,i);
605 yaffs_read(b,buffer,i);
606 yaffs_write(c,buffer,i);
607 } while(sizeOfFiles > 0);
615 int cache_bypass_bug_test(void)
617 // This test reporoduces a bug whereby YAFFS caching is buypassed
618 // resulting in erroneous reads after writes.
624 memset(buffer1,0,sizeof(buffer1));
625 memset(buffer2,0,sizeof(buffer2));
629 yaffs_mount("/boot");
631 // Create a file of 2000 bytes.
632 make_a_file("/boot/a",'X',2000);
634 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
636 // Write a short sequence to the file.
637 // This will go into the cache.
638 yaffs_lseek(a,0,SEEK_SET);
639 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
641 // Read a short sequence from the file.
642 // This will come from the cache.
643 yaffs_lseek(a,0,SEEK_SET);
644 yaffs_read(a,buffer1,30);
646 // Read a page size sequence from the file.
647 yaffs_lseek(a,0,SEEK_SET);
648 yaffs_read(a,buffer2,512);
650 printf("buffer 1 %s\n",buffer1);
651 printf("buffer 2 %s\n",buffer2);
653 if(strncmp(buffer1,buffer2,20))
655 printf("Cache bypass bug detected!!!!!\n");
664 int main(int argc, char *argv[])
666 //return long_test(argc,argv);
668 //return cache_read_test();
670 return cache_bypass_bug_test();