Exemplo n.º 1
0
/**
 * Tests having multiple files openned and written at the same time
 */
int multipleOpenFilesTest() {
    int hr = SUCCESS;
    int fd, fd2, fd3, fsize = SD_SECTORSIZE;
    char *fname1 = "simult1";
    char *fname2 = "simult2";
    char *fname3 = "simult3";
    char *buffer = malloc(fsize);
    initBuffer(buffer, fsize);

    FAIL_BRK4(initAndLoadDisk());
    FAIL_BRK4(initFS());

    // Openning the files
    fd = sfs_fopen(fname1);
    FAIL_BRK3((fd == -1), stdout, "Error: fopen for (%s) failed\n", fname1);

    fd2 = sfs_fopen(fname2);
    FAIL_BRK3((fd2 == -1), stdout, "Error: fopen for (%s) failed\n", fname2);

    fd3 = sfs_fopen(fname3);
    FAIL_BRK3((fd3 == -1), stdout, "Error: fopen for (%s) failed\n", fname3);

    // Writing in a different order
    FAIL_BRK3((sfs_fwrite(fd2, buffer, fsize) != fsize), stdout,
            "Error: Write failed\n");
    FAIL_BRK3((sfs_fwrite(fd, buffer, fsize) != fsize), stdout,
            "Error: Write failed\n");
    FAIL_BRK3((sfs_fwrite(fd3, buffer, fsize) != fsize), stdout,
            "Error: Write failed\n");

    // Closing the files in different order
    FAIL_BRK3(sfs_fclose(fd3), stdout, "Error: Closing the file failed\n");
    FAIL_BRK3(sfs_fclose(fd2), stdout, "Error: Closing the file failed\n");
    FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Closing the file failed\n");

    FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: ls failed\n");

    // reload the disk and verify
    FAIL_BRK4(saveAndCloseDisk());
    FAIL_BRK4(initAndLoadDisk());
    FAIL_BRK4(verifyFile(fname1, buffer, fsize));
    FAIL_BRK4(verifyFile(fname2, buffer, fsize));
    FAIL_BRK4(verifyFile(fname3, buffer, fsize));

    Fail:

    SAFE_FREE(buffer);

    saveAndCloseDisk();
    PRINT_RESULTS("Multiple Open Files Test");
    return hr;
}
Exemplo n.º 2
0
/**
 * creates a number of nested folders
 */
int nestedFolders(char *dirBaseName, int num) {
    int hr = SUCCESS;
    int i;
    char *dirName = malloc(strlen(dirBaseName) + 32);

    for (i = 0; i < num; i++) {
        sprintf(dirName, "%s%04d", dirBaseName, i);
        FAIL_BRK3(sfs_mkdir(dirName), stdout,
                "Error: Creating folder (%s) failed\n", dirName);

        FAIL_BRK3(sfs_fcd(dirName), stdout,
                "Error: Changing folder to (%s) failed\n", dirName);
    }

    // going back to /
    FAIL_BRK3(sfs_fcd("/"), stdout, "Error: Going to root failed\n");

    // cd down one by one
    for (i = 0; i < num; i++) {
        sprintf(dirName, "dir%04d", i);
        FAIL_BRK3(sfs_fcd(dirName), stdout,
                "Error: Changing folder to (%s) failed\n", dirName);
    }

    // cd back up one by one
    for (i = 0; i < num; i++) {
        FAIL_BRK3(sfs_fcd(".."), stdout,
                "Error: Changing folder to (..) failed\n");
        if (i % 10 == 0) {
            FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: ls failed ...");
        }
    }

    // cd down one by one
    for (i = 0; i < num; i++) {
        sprintf(dirName, "dir%04d", i);
        FAIL_BRK3(sfs_fcd(dirName), stdout,
                "Error: Changing folder to (%s) failed\n", dirName);
    }

    Fail:

    SAFE_FREE(dirName);
    return hr;
}
Exemplo n.º 3
0
/* The main testing program
*/
    int
main(int argc, char **argv)
{
    int i, j, k;
    int chunksize;
    char *buffer;
    char fixedbuf[1024];
    int fds[MAX_FD];
    char *names[MAX_FD];
    int filesize[MAX_FD];
    int nopen;                    /* Number of files simultaneously open */
    int ncreate;                  /* Number of files created in directory */
    int error_count = 0;
    int tmp;

    mksfs(1);                     /* Initialize the file system. */

    /* First we open two files and attempt to write data to them.
    */
    for (i = 0; i < 2; i++) {
        names[i] = rand_name();
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            fprintf(stderr, "ERROR: creating first test file %s\n", names[i]);
            error_count++;
        }
        tmp = sfs_fopen(names[i]);
        if (tmp >= 0 && tmp != fds[i]) {
            fprintf(stderr, "ERROR: file %s was opened twice\n", names[i]);
            error_count++;
        }
        filesize[i] = (rand() % (MAX_BYTES-MIN_BYTES)) + MIN_BYTES;
    }

    for (i = 0; i < 2; i++) {
        for (j = i + 1; j < 2; j++) {
            if (fds[i] == fds[j]) {
                fprintf(stderr, "Warning: the file descriptors probably shouldn't be the same?\n");
            }
        }
    }

    printf("Two files created with zero length:\n");
    sfs_ls();
    printf("\n");

    for (i = 0; i < 2; i++) {
        for (j = 0; j < filesize[i]; j += chunksize) {
            if ((filesize[i] - j) < 10) {
                chunksize = filesize[i] - j;
            }
            else {
                chunksize = (rand() % (filesize[i] - j)) + 1;
            }

            if ((buffer = malloc(chunksize)) == NULL) {
                fprintf(stderr, "ABORT: Out of memory!\n");
                exit(-1);
            }
            for (k = 0; k < chunksize; k++) {
                buffer[k] = (char) (j+k);
            }
            printf("file: %d, chunksize: %d\n", i, chunksize);
            sfs_ls();
            sfs_fwrite(fds[i], buffer, chunksize);
	    free(buffer);
        }
    }

    sfs_fclose(fds[1]);

    printf("File %s now has length %d and %s now has length %d:\n",
            names[0], filesize[0], names[1], filesize[1]);
    sfs_ls();

    fds[1] = sfs_fopen(names[1]);

    printf("before doing reads\n\n");

    for (i = 0; i < 2; i++) {
        for (j = 0; j < filesize[i]; j += chunksize) {
            if ((filesize[i] - j) < 10) {
                chunksize = filesize[i] - j;
            }
            else {
                chunksize = (rand() % (filesize[i] - j)) + 1;
            }
            if ((buffer = malloc(chunksize)) == NULL) {
                fprintf(stderr, "ABORT: Out of memory!\n");
                exit(-1);
            }
            printf("before sfs_read\n\n");
            sfs_fread(fds[i], buffer, chunksize);
            printf("After sfs_read\n\n");
            /*
            for (k = 0; k < chunksize; k++) {
                if (buffer[k] != (char)(j+k)) {
                    fprintf(stderr, "ERROR: data error at offset %d in file %s (%d,%d)\n",
                            j+k, names[i], buffer[k], (char)(j+k));
                    error_count++;
                    break;
                }
            }
            */
            free(buffer);
            printf("After doing reads %d\n\n", i);
        }
    }
    printf("After doing reads\n\n");

    for (i = 0; i < 2; i++) {
        sfs_fclose(fds[i]);
        if (sfs_remove(names[i]) != 0) {
            fprintf(stderr, "ERROR: deleting file %s\n", names[i]);
            error_count++;
        }
        printf("After deleting file %s:\n", names[i]);
        sfs_ls();
    }

    /* Now try to close and delete the closed and deleted files. Don't
     * care about the return codes, really, but just want to make sure
     * this doesn't cause a problem.
     */
    for (i = 0; i < 2; i++) {
        sfs_fclose(fds[i]);
        if (sfs_remove(names[i]) == 0) {
            fprintf(stderr, "Warning: deleting already deleted file %s\n", names[i]);
        }
        //free(names[i]);
        names[i] = NULL;
    }

    /* Now just try to open up a bunch of files.
    */
    ncreate = 0;
    for (i = 0; i < MAX_FD; i++) {
        names[i] = rand_name();
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            break;
        }
        sfs_fclose(fds[i]);
        ncreate++;
    }

    printf("Created %d files in the root directory\n", ncreate);

    nopen = 0;
    for (i = 0; i < ncreate; i++) {
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            break;
        }
        nopen++;
    }
    printf("Simultaneously opened %d files\n", nopen);

    for (i = 0; i < nopen; i++) {
        sfs_fwrite(fds[i], test_str, strlen(test_str));
        sfs_fclose(fds[i]);
    }

    /* Re-open in reverse order */
    for (i = nopen-1; i >= 0; i--) {
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            fprintf(stderr, "ERROR: can't re-open file %s\n", names[i]);
        }
    }

    /* Now test the file contents.
    */
    for (j = 0; j < strlen(test_str); j++) {
        for (i = 0; i < nopen; i++) {
            char ch;

            sfs_fread(fds[i], &ch, 1);
            if (ch != test_str[j]) {
                fprintf(stderr, "ERROR: Read wrong byte from %s at %d (%d,%d)\n", 
                        names[i], j, ch, test_str[j]);
                error_count++;
                break;
            }
        }
    }

    /* Now close all of the open file handles.
    */
    for (i = 0; i < nopen; i++) {
        sfs_fclose(fds[i]);
    }

    /* Now we try to re-initialize the system.
    */
    mksfs(0);

    for (i = 0; i < nopen; i++) {
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] >= 0) {
            sfs_fread(fds[i], fixedbuf, sizeof(fixedbuf));

            for (j = 0; j < strlen(test_str); j++) {
                if (test_str[j] != fixedbuf[j]) {
                    fprintf(stderr, "ERROR: Wrong byte in %s at %d (%d,%d)\n", 
                            names[i], j, fixedbuf[j], test_str[j]);
                    error_count++;
                    break;
                }
            }

            sfs_fclose(fds[i]);
        }
    }

    for (i = 0; i < ncreate; i++) {
        sfs_remove(names[i]);
        free(names[i]);
        names[i] = NULL;
    }

    //-------- The following part tests sfs_fseek

    printf("Tests sfs_fseek\n");

    // Prepare a file
    char* f_name = rand_name();
    int f_id = sfs_fopen(f_name);
    buffer = malloc(100);

    for(i = 0; i < 10; i++)
    {
        sfs_fwrite(f_id, "0123456789", 10);
    }

    // sfs_fwrite shouldn't change read pointer
    for(i = 0; i < 10; i++)
    {
        sfs_fread(f_id, buffer, 10);
        if(0 != strncmp(buffer, "0123456789", 10))
        {
            fprintf(stderr, "ERROR: should read '0123456789'\n");
            error_count++;
        }
    }

    // sys_seek changes read pointer
    for(i = 0; i < 100; i += 7)
    {
        sfs_fseek(f_id, i);
        sfs_fread(f_id, buffer, 1);
        if (buffer[0] - 48 != i % 10) {
            fprintf(stderr, "ERROR: postion %d shoud be %c\n", i, i % 10);
            error_count++;
        }
    }

    sfs_fseek(f_id, 80);
    sfs_fwrite(f_id, "9876543210", 10);
    sfs_fseek(f_id, 85);
    sfs_fread(f_id, buffer, 10);
    if(0 != strncmp(buffer, "4321001234", 10))
    {
        fprintf(stderr, "ERROR: should read '4321001234'\n");
        error_count++;
    }

    //free(buffer);

    fprintf(stderr, "Test program exiting with %d errors\n", error_count);
    return (error_count);
}
Exemplo n.º 4
0
int main(int argc, char *argv[]) {
    
    if ( argc == 2 ) {
	port = strtol(argv[1], &endptr, 0);
	if ( *endptr ) {
	    fprintf(stderr, "Invalid port number.\n");
	    exit(EXIT_FAILURE);
	}
    }
    else if ( argc < 2 ) {
            port = ecPort;
    }

    else {
	fprintf(stderr, "Invalid arguments\n");
	exit(EXIT_FAILURE);
    }

    fileSystemInit();
    connection_setup();

    if ( (connS = accept(listS, NULL, NULL) ) < 0 ) {
	    fprintf(stderr, "Error!!\n");
	    exit(EXIT_FAILURE);
    }
    header = (char *)malloc(2 * sizeof(int));
    elreadmos = (char *)malloc(10000);
    myLStable = (char *)malloc(100);
    while ( 1 ) {
	recieveing(connS, recEI, 2 * sizeof(int));
	memcpy(&recvReq, recEI, sizeof(int));
	memcpy(&recvSize, recEI +sizeof(int), sizeof(int));
	switch (recvReq) {

		case 1:
			printf("root directory\n");
			LStableAdd = sfs_ls();
			strcpy(myLStable, LStableAdd);
			size = strlen(myLStable);
			DataSend = (char *)malloc(size);
			memcpy(header, &lsCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			memcpy(DataSend, myLStable, size);
			Sending(connS, header, (2 * sizeof(int)));
			Sending(connS, DataSend, size);
			free(DataSend);
			break;
		case 2:
			printf("open file\n");
			recData = (char *)malloc(recvSize);
			recieveing(connS, recData, recvSize);
			memcpy(openReq, recData, recvSize);
			openReq[recvSize] = 0;
			remID = sfs_open(openReq);
			DataSend = (char *)malloc(sizeof(int));
			size = sizeof(int);
			memcpy(header, &opCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int));
			memcpy(DataSend, &remID, size);
			Sending(connS, header, (2 * sizeof(int)));
			Sending(connS, DataSend, size);
			free(recData);
			free(DataSend);
			break;
		case 3:
			printf("close file\n");
			recData = (char *)malloc(recvSize);
			recieveing(connS, recData, recvSize);
			memcpy(&elremote, recData, sizeof(int));
			sfs_close(elremote);
			memcpy(header, &closeCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			Sending(connS, header, (2 * sizeof(int)));
                        free(recData);
			break;
		case 4:
			printf("write file\n");
			recData = (char *)malloc(recvSize);
			elWritBuf = (char *)malloc(recvSize);
			recieveing(connS, recData, recvSize - 8);
			memcpy(elWritBuf, recData, recvSize -8);
			elWritBuf[recvSize - 8] = 0;
			recieveing(connS, recData, sizeof(int));
			memcpy(&remID, recData, sizeof(int));
			recieveing(connS, recData, sizeof(int));
			memcpy(&my_len, recData, sizeof(int));
                        sfs_write(remID, elWritBuf, my_len);
			size = 0;
                        memcpy(header, &writCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			Sending(connS, header, (2 * sizeof(int)));
                        free(recData);
			free(elWritBuf);
                        break;

		case 5:

			printf("read file\n");
			recData = (char *)malloc(recvSize);
                        recieveing(connS, recData, sizeof(int));
			memcpy(&remID, recData, sizeof(int));
			recieveing(connS, recData, sizeof(int));
			memcpy(&my_len, recData, sizeof(int));
                        sfs_read(remID, elreadmos, my_len);
                        size = strlen(elreadmos);
                        memcpy(header, &readCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int));
			Sending(connS, header, (2 * sizeof(int)));
                        DataSend = (char *)malloc(size);
			memcpy(DataSend, elreadmos, size);
			DataSend[size] = 0;
			puts(DataSend);
                        Sending(connS, DataSend, size);
                        break;

		case 6:
			printf("remove file\n");
			recData = (char *)malloc(recvSize);
                        recieveing(connS, recData, recvSize);
			memcpy(remReq, recData, recvSize);
			remReq[recvSize] = 0;
			printf("file to remove = %s\n",remReq);
                        if (sfs_remove(remReq) == -1){
				size = strlen("Error: file doesnot exist\n");
				DataSend = (char *)malloc(size);
				strcpy(DataSend, "Error: file doesnt exist\n");
			}

			else{
				size = strlen("file removed successfully\n");
				DataSend = (char *)malloc(size);
				strcpy(DataSend, "file remove successfull\n");

			}
                        memcpy(header, &remCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			Sending(connS, header, (2 * sizeof(int)));
                        Sending(connS, DataSend, size);
                        free(recData);
			free(DataSend);
                        break;
               default:
			printf("Invalid input\n");
                        break;
	}

    }

}
Exemplo n.º 5
0
/**
 * Tests sfs_rm functionality.
 */
int removeTest() {
    int hr = SUCCESS;
    char dirName[16], fileName[16];
    int i, numDirs = 100, numFiles = 100, fsize = SD_SECTORSIZE * 1.8;
    char *buffer = malloc(fsize * sizeof(char));

    // test setup
    FAIL_BRK4(initAndLoadDisk());
    FAIL_BRK4(initFS());

    FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");

    // make some files
    for (i = 0; i < numFiles; i++) {
        sprintf(fileName, "file%04d", i);

        initBuffer(buffer, fsize);
        FAIL_BRK4(createSmallFile(fileName, buffer, fsize));
    }

    // make some folders
    for (i = 0; i < numDirs; i++) {
        sprintf(dirName, "dir%04d", i);
        FAIL_BRK4(createFolder(dirName));
    }

    FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");

    // delete the even folders and files
    for (i = 0; i < numFiles; i += 2) {
        sprintf(fileName, "file%04d", i);
        FAIL_BRK3(sfs_rm(fileName), stdout,
                "Error: deleting file (%s) failed\n", fileName);

        sprintf(dirName, "dir%04d", i);
        FAIL_BRK3(sfs_rm(dirName), stdout,
                "Error: deleting folder (%s) failed\n", dirName);
    }

    FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");

    // delete the rest of the files and folders
    for (i = 1; i < numFiles; i += 2) {
        sprintf(fileName, "file%04d", i);
        FAIL_BRK3(sfs_rm(fileName), stdout,
                "Error: deleting file (%s) failed\n", fileName);

        sprintf(dirName, "dir%04d", i);
        FAIL_BRK3(sfs_rm(dirName), stdout,
                "Error: deleting folder (%s) failed\n", dirName);
    }

    FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");

    Fail:

    SAFE_FREE(buffer);
    saveAndCloseDisk();
    PRINT_RESULTS("Remove Test");
    return hr;
}
Exemplo n.º 6
0
/**
 * This is a place where you need to put your test. Please insert your test code here and
 * change the name of the test to be representative of what you are testing. You can use this test
 * during the development period to test your code. When you do the final submission here you have to
 * implement a test that tests some interesting functionality of the sfs.
 * @return SUCCESS if the test is passed successfully FAIL otherwise.
 */
int customTest() {
    int hr = SUCCESS;
	int i, j, fd, fd1, fd2, lseek2;
    char *randomBuf;//buffer contain junk data, size: SD_SECTORSIZE
	randomBuf = (char *) malloc(sizeof(char) * SD_SECTORSIZE);

	// For later tests
	char* asciidata = (char*)malloc(257 * sizeof(char)); // 2
	char* morealphabet = (char*)malloc(7*sizeof(char));
	morealphabet[0] = 't';
	morealphabet[1] = 'u';
	morealphabet[2] = 'v';
	morealphabet[3] = 'w';
	morealphabet[4] = 'x';
	morealphabet[5] = 'y';
	morealphabet[6] = 'z';
	char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
	char* fd1read = (char*)malloc(26*sizeof(char));

	// normal test from testfs
    // initialize disk
    FAIL_BRK4(SD_initDisk());
    for (i = 0; i < SD_NUMSECTORS; i++) {
        for (j = 0; j < SD_SECTORSIZE; j++) {
            randomBuf[j] = (char) rand();
        }
		while (SD_write(i, (void*) randomBuf));
    }
	FAIL_BRK4(SD_loadDisk(gsDiskFName));
	refreshDisk();

    // initialize fs, sfs_mkfs
    FAIL_BRK4(initFS());
	refreshDisk();

	// initialize test sfs_mkfs, when mkfs, nothing should appear again.
    FAIL_BRK4(initFS());
	FAIL_BRK4(sfs_mkdir("foo"));
	refreshDisk();
	FAIL_BRK4(sfs_fcd("foo"));
	FAIL_BRK4(sfs_mkdir("bar"));
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((sfs_fcd("foo") != -1));
	FAIL_BRK4((sfs_fcd("bar") != -1));
	FAIL_BRK4(sfs_fcd("/"));
	FAIL_BRK4((sfs_fcd("foo") != -1));
	FAIL_BRK4((sfs_fcd("bar") != -1));
	FAIL_BRK4(sfs_mkdir("foo"));
	refreshDisk();
	FAIL_BRK4(sfs_fcd("foo"));
	FAIL_BRK4((sfs_fcd("bar") != -1));

	//normal test . and ..
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((sfs_mkdir(".") != -1));
	FAIL_BRK4(sfs_mkdir(".foo."));
	FAIL_BRK4(sfs_mkdir(".foo"));
	FAIL_BRK4((sfs_mkdir("..") != -1));
	FAIL_BRK4(sfs_mkdir("..foo"));
	FAIL_BRK4(sfs_mkdir("..foo.."));
	FAIL_BRK4((sfs_mkdir("/") != -1));
	FAIL_BRK4(sfs_mkdir("..."));
	FAIL_BRK4(sfs_mkdir("...."));
	FAIL_BRK4((sfs_mkdir("./") != -1));
	FAIL_BRK4((sfs_mkdir("/.") != -1));
	FAIL_BRK4((sfs_mkdir("./.") != -1));

	// ascii code test, make a file containing all ascii code chars to make sure implementation does not use an EOF char for size
	FAIL_BRK4(initFS());
	refreshDisk();
	//257 chars, possible ascii 0 -> 255 + 255 at the end to make sure we test
	// create data

	for (i = 0; i < 256; i++) {
		asciidata[i] = i; // table sequentially
	}
	// now put 255 in again at the end
	asciidata[256] = 255;
	// now fwrite into the a file and fread and compare
	FAIL_BRK4(createSmallFile("asciitable", asciidata, 257));
	refreshDisk();
	FAIL_BRK4(verifyFile("asciitable", asciidata, 257));

	// This test will open the same file twice, write more onto it using one of the file descriptors to lengthen it, and read both to make sure they still match (should test to make sure size is with inodes and not the table
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4(createSmallFile("alphabet", "abcdefghijklmnopqrst", 20));
	// Open twice
	FAIL_BRK4((fd1 = sfs_fopen("alphabet")) == -1);
	FAIL_BRK4((fd2 = sfs_fopen("alphabet")) == -1);
	// lseek to end and write to second one
	FAIL_BRK4((lseek2 = sfs_lseek(fd2, 19)) == -1);
	FAIL_BRK4((sfs_fwrite(fd2, morealphabet, 7)) == -1);
	// Now verify we can read from fd1 and it should be the full alphabet since we wrote to fd2 which is the same file
	FAIL_BRK4((sfs_fread(fd1, fd1read, 26)) == -1);
	FAIL_BRK4((strncmp(alphabet, fd1read, 26)) != 0); // here is the comparison of strings
	FAIL_BRK4((sfs_fclose(fd1)) == -1);
	FAIL_BRK4((sfs_fclose(fd2)) == -1);

	//test dir takes more that one sector

	//test for file
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1);
	FAIL_BRK4(sfs_fclose(fd));
	FAIL_BRK4((sfs_fcd("foo.bin") != -1));

	//test same name
	FAIL_BRK4(initFS());
	refreshDisk();

	FAIL_BRK4(sfs_mkdir("foo.bin"));
	FAIL_BRK4((fd = sfs_fopen("foo.bin")) != -1);

	FAIL_BRK4((fd = sfs_fopen("bar.bin")) == -1);
	FAIL_BRK4((sfs_mkdir("bar.bin") != -1));

	//test initFS will erase the file descriptor
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1);
	FAIL_BRK4(initFS());
	FAIL_BRK4((sfs_fclose(fd) != -1));

	//test create nothing
	FAIL_BRK4(initFS());
	refreshDisk();

	FAIL_BRK4((sfs_mkdir("") != -1));
	FAIL_BRK4((fd = sfs_fopen("")) != -1);

	//test open . and ..
	FAIL_BRK4(initFS());
	refreshDisk();

	FAIL_BRK4((fd = sfs_fopen("/")) != -1);
	FAIL_BRK4((fd = sfs_fopen(".")) != -1);
	FAIL_BRK4((fd = sfs_fopen("..")) != -1);

	FAIL_BRK4(sfs_mkdir("foo"));
	FAIL_BRK4((fd = sfs_fopen("foo")) != -1);
	FAIL_BRK4(sfs_fcd("foo"));

	FAIL_BRK4((fd = sfs_fopen("/")) != -1);
	FAIL_BRK4((fd = sfs_fopen(".")) != -1);
	FAIL_BRK4((fd = sfs_fopen("..")) != -1);

	FAIL_BRK4(sfs_fcd("/"));
	FAIL_BRK4((fd = sfs_fopen("/.")) != -1);
	FAIL_BRK4((fd = sfs_fopen("./")) != -1);
	FAIL_BRK4((fd = sfs_fopen("./.")) != -1);
	FAIL_BRK4((fd = sfs_fopen("/..")) != -1);
	FAIL_BRK4((fd = sfs_fopen("../")) != -1);
	FAIL_BRK4((fd = sfs_fopen("../..")) != -1);
	FAIL_BRK4((fd = sfs_fopen("//")) != -1);
	FAIL_BRK4((fd = sfs_fopen(".foo")) == -1);
	FAIL_BRK4((fd = sfs_fopen(".foo.")) == -1);
	FAIL_BRK4((fd = sfs_fopen("..foo")) == -1);
	FAIL_BRK4((fd = sfs_fopen("..foo..")) == -1);
	FAIL_BRK4((fd = sfs_fopen("...")) == -1);
	FAIL_BRK4((fd = sfs_fopen(".....")) == -1);
	
	// test the malloc not been released
	FAIL_BRK4(sfs_mkfs());
	refreshDisk();
	int thememmax;
	struct rusage ru;
	getrusage(RUSAGE_SELF, &ru);
	thememmax=ru.ru_maxrss;
	long int k;
	for(k = 0; k < 100000; ++k)
	{
		FAIL_BRK4(sfs_mkfs());
		FAIL_BRK4(sfs_mkdir("foo"));
		FAIL_BRK4(sfs_fcd("foo"));
		FAIL_BRK4(sfs_ls(f_ls));
		FAIL_BRK4((fd = sfs_fopen("bar")) == -1);
		FAIL_BRK4((sfs_fwrite(fd, randomBuf, SD_SECTORSIZE)) == -1);
		FAIL_BRK4((sfs_lseek(fd, 1)) == -1);
		FAIL_BRK4((sfs_fread(fd, randomBuf, SD_SECTORSIZE - 1)) == -1);
		FAIL_BRK4(sfs_fclose(fd));
	}
	getrusage(RUSAGE_SELF, &ru);
	FAIL_BRK4(100 * thememmax <= ru.ru_maxrss);
/*	
    FAIL_BRK4(createFolder("bar"));
	FAIL_BRK4(sfs_fcd("bar"));
	FAIL_BRK4(createFolder("foo"));	
	FAIL_BRK4(sfs_fcd("foo"));
	FAIL_BRK4(sfs_fcd("/bar/foo"));
//	FAIL_BRK3((sfs_fcd("//bar/foo") != -1), stdout, "Error: sfs_fcd() failed\n");
	FAIL_BRK4(sfs_fcd("/../bar/foo"));
	FAIL_BRK4(sfs_fcd("/../bar/foo/"));
	FAIL_BRK4(sfs_fcd("/"));
	FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
	FAIL_BRK4(sfs_fcd("/bar"));
	FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
	
	//int fopentmp;
	//FAIL_BRK3((fopentmp = sfs_fopen("test.txt") != -1), stdout, "Error: Allowing read from unopened file\n");
	//FAIL_BRK3((sfs_fread(fopentmp, buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n");

    // test if fcd does not allow going to places that does not exist
    FAIL_BRK3((sfs_fcd("bla") != -1), stdout,
            "Error: Allowing cd to folder that does not exist\n");
    FAIL_BRK3((sfs_fcd("x") != -1), stdout,
            "Error: Allowing cd to folder that does not exist\n");
    FAIL_BRK3((sfs_fcd("x/y/x/z") != -1), stdout,
            "Error: Allowing cd to folder that does not exist\n");
*/

    Fail:

    //clean up code goes here
	SAFE_FREE(asciidata);
	SAFE_FREE(morealphabet);
	SAFE_FREE(fd1read);
	SAFE_FREE(randomBuf);
    saveAndCloseDisk();
    PRINT_RESULTS("customTest!");
    return hr;
}
Exemplo n.º 7
0
/* The main testing program
*/
    int
main(int argc, char **argv)
{
    int i, j, k;
    int chunksize;
    char *buffer;
    int fds[MAX_FD];
    char *names[MAX_FD];
    int filesize[MAX_FD];
    int error_count = 0;
    int tmp;

    mksfs(1);                     /* Initialize the file system. */

    /* First we open five files and attempt to write data to them.
    */
    for (i = 0; i < 5; i++) {
        names[i] = rand_name();
        fds[i] = sfs_open(names[i]);
        if (fds[i] < 0) {
            fprintf(stderr, "ERROR: creating first test file %s\n", names[i]);
            error_count++;
        }
        tmp = sfs_open(names[i]);
        if (tmp >= 0 && tmp != fds[i]) {
            fprintf(stderr, "ERROR: file %s was opened twice\n", names[i]);
            error_count++;
        }
        filesize[i] = (rand() % (MAX_BYTES-MIN_BYTES)) + MIN_BYTES;
    }

    for (i = 0; i < 5; i++) {
        for (j = i + 1; j < 2; j++) {
            if (fds[i] == fds[j]) {
                fprintf(stderr, "Warning: the file descriptors probably shouldn't be the same?\n");
            }
        }
    }

    printf("Five files created with zero length:\n");
    sfs_ls();
    printf("\n");

    for (i = 0; i < 5; i++) {
        for (j = 0; j < filesize[i]; j += chunksize) {
            if ((filesize[i] - j) < 10) {
                chunksize = filesize[i] - j;
            }
            else {
                chunksize = (rand() % (filesize[i] - j)) + 1;
            }

            if ((buffer = malloc(chunksize)) == NULL) {
                fprintf(stderr, "ABORT: Out of memory!\n");
                exit(-1);
            }
            for (k = 0; k < chunksize; k++) {
                buffer[k] = (char) (j+k);
            }
            sfs_write(fds[i], buffer, chunksize);
            free(buffer);
        }
    }

    for (i = 0; i < 5; i++)
      sfs_close(fds[i]);

    sfs_ls();
	
    for (i = 0; i < 5; i++)
      fds[i] = sfs_open(names[i]);

    printf("Reopened the files again.. the read/write pointers should be set to front\n");

    for (i = 0; i < 5; i++) {
        for (j = 0; j < filesize[i]; j += chunksize) {
            if ((filesize[i] - j) < 10) {
                chunksize = filesize[i] - j;
            }
            else {
                chunksize = (rand() % (filesize[i] - j)) + 1;
            }
            if ((buffer = malloc(chunksize)) == NULL) {
                fprintf(stderr, "ABORT: Out of memory!\n");
                exit(-1);
            }
            sfs_read(fds[i], buffer, chunksize);
            puts(buffer);
            for (k = 0; k < chunksize; k++) {
                if (buffer[k] != (char)(j+k)) {
                    fprintf(stderr, "ERROR: data error at offset %d in file %s (%x,%x)\n",
                            j+k, names[i], buffer[k], (char)(j+k));
                    error_count++;
                    break;
                }
            }
            free(buffer);
        }
    }

    fprintf(stderr, "Test program exiting with %d errors\n", error_count);
    
    return (error_count);
}