#include #include #include "filesystem.h" #define FILESIZE 51200 #define OPENFAILED 1 #define WRITEFAILED 2 #define READFAILED 3 #define READCHECKERROR 4 #define CLOSEFAILED 5 #define FILESOPENERROR 6 #define MKDIRERROR 7 #define SEEKFAILED 8 #define READWRITEFAILED 9 #define DELETEFAILED 10 #define PERMERROR 11 #define RENAMEERROR 12 #define READDIRERROR 13 #define OPENDIRERROR 14 FILE * output; int testrw(void); int openclose(void); int permcheck(void); void errorroutine(int errornumber) { char outputfile[20]; strcpy (outputfile, "testing.txt"); /* output = stdout; */ if ((output = fopen(outputfile, "a")) == NULL) { printf("Error in output.\n"); fflush(stdout); exit (69); } switch (errornumber) { case OPENFAILED: fprintf (output, "An error occurred in opening a file.\n"); break; case OPENDIRERROR: fprintf(output, "An error occurred in opendir.\n"); break; case READDIRERROR: fprintf(output,"An error occurred in f_readdir\n"); break; case WRITEFAILED: fprintf (output, "An error occurred in writing to a file.\n"); break; case READFAILED: fprintf (output, "An error occurred in reading a file.\n"); break; case READCHECKERROR: fprintf (output, "The validity of data writen is in question.\n"); break; case FILESOPENERROR: fprintf(output, "Should not be able to open more than 10 files.\n"); break; case CLOSEFAILED: fprintf (output, "An error occurred in closing a file.\n"); break; case SEEKFAILED: fprintf (output, "An error occurred in seeking.\n"); break; case MKDIRERROR: fprintf (output, "An error occurred in creating a directory\n"); break; case READWRITEFAILED: fprintf (output, "An error occurred in seeking.\n"); break; case DELETEFAILED: fprintf (output, "An error occurred in deleting a file or directory.\n"); break; case PERMERROR: fprintf (output, "An error occurred changing a file or directory permissions.\n"); break; case RENAMEERROR: fprintf (output, "An error occurred renaming a file or directory.\n"); break; default: fprintf (output, "Error in error routine.\n"); } fclose(output); } void errorprint(const char * errormessage, /* args */ ... ) { char outputfile[20]; va_list idontknow; va_start (idontknow, errormessage); strcpy (outputfile, "testing.txt"); /* output = stdout; */ if ((output = fopen(outputfile,"a")) == NULL) { printf("Writing testing.txt\n"); fflush(stdout); exit (69); } fprintf(output, errormessage, va_arg(idontknow, int) ); fclose(output); } int f_test(void) { int error = 0; int total = 0; unsigned long int numaccess,total_distance_moved; extern unsigned long int GetNumofAccesses(), GetNumofMovements(); errorprint("Testing your file system.\n"); errorprint("Initializing ...."); error = f_initialize(); if (error < 0) { errorprint("could not initialize file system\n"); total += 1; } errorprint("OK\n"); /* create tmp dir for testing */ error = f_mkdir("/tmp"); if (error < 0) { errorroutine(MKDIRERROR); total += 1; } else errorprint("Making /tmp.\n"); error = testrw(); if (error != 0) { errorroutine(error); total += 1; } else errorprint("Testing of f_read f_write OK.\n"); error = openclose(); if (error != 0) { errorroutine(error); total += 1; } else errorprint("Testing of f_open and f_close OK.\n"); error = permcheck(); if (error != 0) { errorroutine(error); total += 1; } else errorprint("Testing of permissions OK.\n"); return total; } int testrw(void) { /* Test reading and writing large files */ int filed, filed1; char fluf[FILESIZE]; char test[FILESIZE]; int error, x; errorprint("Opening /tmp/big\n"); filed = f_open ("/tmp/big", 1,"rw"); if (filed < 0) return OPENFAILED; errorprint("Opening /new/big -- should fail\n"); filed1 = f_open("/new/big",1,"rw"); if (filed1 >= 0) return OPENFAILED; /** should not be able to open /new/big since /new does not exist **/ for (x = 0; x < FILESIZE; x++) sprintf (fluf, "%d", x); error = f_write (filed, fluf,FILESIZE); if (error < 0) return WRITEFAILED; error = f_close(filed); if (error < 0) return CLOSEFAILED; else errorprint("Closing /tmp/big file.\n"); errorprint("Opening /tmp/big file.\n"); filed = f_open ("/tmp/big", 0,"rw"); if (filed < 0) return OPENFAILED; error = f_read (filed, test,FILESIZE); if (error < 0) return READFAILED; error = strncmp(test, fluf, FILESIZE); if (error != 0) return READCHECKERROR; errorprint("OK.\n"); /** Check to see if a file can be opened multiple times **/ errorprint("Opening /tmp/big file a second time.\n"); filed1 = f_open("/tmp/big",0,"r"); if (filed1 < 0) return OPENFAILED; errorprint("Testing f_seek....."); error = f_seek(filed1,25); if (error < 0) return(SEEKFAILED); if (error != 0) return SEEKFAILED; errorprint("OK.\n"); f_close(filed1); f_close(filed); return 0; } int openclose(void) { int error; int fdtable[10]; int loop; char tmp[FILESIZE]; char test[FILESIZE]; char name[FILESIZE]; char pathname[FILESIZE]; int filesindir; int dired; strcpy (test, "Just some info."); /* create ten files and put some info in * seek to begining and read then test for * correctness */ errorprint("Creating /test\n"); error = f_mkdir("/test"); if (error < 0) { errorprint("Could not create /test\n"); return(MKDIRERROR); } else errorprint("created directory /test\n"); errorprint("Opening /test using f_opendir\n"); dired = f_opendir("/test"); if (error < 0) { errorprint("Could not open directory /test\n"); return(OPENDIRERROR); } errorprint("Writing to /test -- should fail!\n"); error = f_write(dired,test,strlen(test)+1); if (error >= 0) { errorprint("Should not be able to write to a directory\n"); return(WRITEFAILED); } errorprint("Reading /test using f_read -- should fail!\n"); error = f_read(dired,test,strlen(test)+1); if (error >= 0) { errorprint("Should not be able to read a directory with f_read\n"); return(READFAILED); } for (loop = 0; loop < 10; loop++) { sprintf(name, "/test/%d", loop); errorprint("Opening file /test/%d.\t", loop); error = f_open(name, 1,"rw"); if (error < 0) return OPENFAILED; else fdtable[loop] = error; errorprint("Writing to file /test/%d.\t", loop); error = f_write (fdtable[loop], test,strlen(test)+1); if (error < 0) return WRITEFAILED; errorprint("Seeking in file /test/%d.\n", loop); error = f_seek (fdtable[loop], 0); if (error < 0) return SEEKFAILED; strcpy (tmp, " "); errorprint("Reading file /test/%d.\t", loop); error = f_read (fdtable[loop], tmp,strlen(test)+1); if (error < 0) return READFAILED; error = strcmp (tmp, test); if (error != 0) { errorprint("Check validity of information writen.\n"); return READWRITEFAILED; } else errorprint("Valid information read.\n"); } /* close all open files */ for (loop = 0; loop < 10; loop++) { errorprint("Closing file /test/%d ", loop); error = f_close(fdtable[loop]); if (error < 0) { errorprint("FAILED\n"); errorprint("Check your close routines.\n"); /* Try to continue return CLOSEFAILED; */ } else errorprint("SUCCESS\n"); } errorprint("attempting to delete /test -- should fail!\n"); error = f_delete("/test"); if (error == 0) { errorprint("Should not be able to delete non-empty directory /test!!\n"); return(DELETEFAILED); } /* Delete all files and directory */ filesindir = 1; errorprint("Testing f_opendir and f_readdir...Deleting all files in /test\n"); dired = f_opendir("/test"); while (filesindir != 0) { strcpy(pathname,"/test/"); filesindir = f_readdir(dired,name); if (filesindir < 0) { errorprint("could not read entries in /test \n"); return(READDIRERROR); } if (filesindir == 0) { errorprint("last entry read from directory /test \n"); break; } if (( strcmp(name,"..") != 0) && (strcmp(name,".") != 0)) { strcat(pathname,name); errorprint("Deleting file %s \n ",pathname); error = f_delete(pathname); if (error < 0) { errorprint("ERROR.\n"); return DELETEFAILED; } errorprint("OK.\n"); } } errorprint("Deleting /test\n"); error = f_delete("/test"); if (error < 0) { errorprint("should have been able to delete /test\n"); errorprint("/test should be empty unless error in readdir\n"); return(DELETEFAILED); } else errorprint("Deleted /test \n"); return 0; } int permcheck(void) { int error; int filed; /* make /tmp/big write protected */ errorprint("Write protecting /tmp \n"); error = f_perm ("/tmp"); if (error < 0) { errorroutine (PERMERROR); return PERMERROR; } /* try and delete /tmp/big; should not work */ errorprint("Trying to delete /tmp/big\n"); error = f_delete ("/tmp/big"); if (error == 0) { errorroutine (PERMERROR); return PERMERROR; } else errorprint("Could not delete /tmp/big --- GOOD \n"); /* make /tmp unprotected */ errorprint("Toggling permissions for /tmp\n"); error = f_perm ("/tmp"); if (error < 0) { errorroutine (PERMERROR); return PERMERROR; } /* make file to test rename */ errorprint("Creating /tmp/little \n"); filed = f_open ("/tmp/little", 1,"rw"); if (filed < 0) { errorroutine (OPENFAILED); return OPENFAILED; } errorprint("Closing /tmp/little \n"); error = f_close (filed); if (error < 0) { errorroutine (CLOSEFAILED); return CLOSEFAILED; } /* Test renaming a file to an existing file */ errorprint("Renaming /tmp/little to existing file\n"); error = f_rename ("/tmp/little", "/tmp/big"); if (error == 0) { errorroutine (RENAMEERROR); return RENAMEERROR; } else errorprint("Did not work. --- GOOD\n"); /* delete /tmp/big */ errorprint("Deleting /tmp/big\n"); error = f_delete ("/tmp/big"); if (error < 0) { errorroutine (PERMERROR); return PERMERROR; } /* Try renaming an existing file */ errorprint("Renaming /tmp/little to /tmp/big\n"); error = f_rename ("/tmp/little", "/tmp/big"); if (error < 0) { errorroutine (RENAMEERROR); return RENAMEERROR; } return 0; }