Exemple #1
0
/**
 * Initialize the Disk with random content.
 */
int initDisk(void) {
    int hr = SUCCESS;
    int i, j;
    char *randomBuf;

    // initialize disk
    LOG(stdout, "Initialize disk before using it\n");
    FAIL_BRK3(SD_initDisk(), stderr, "Problems initializing disk %d\n", sderrno);
    LOG(stdout, "Write random garbage to disk\n");

    randomBuf = (char *) malloc(sizeof(char) * SD_SECTORSIZE);
    for (i = 0; i < SD_NUMSECTORS; i++) {
        // fill the buffer with random junk
        for (j = 0; j < SD_SECTORSIZE; j++) {
            randomBuf[j] = (char) rand();
        }

        // continue trying to write for number of times
        int numTries = 10;
        while (SD_write(i, (void*) randomBuf) && numTries) {
            fprintf(stdout, "Error %d while writing block %d\n", sderrno, i);
            numTries--;
        }
        FAIL_BRK3((numTries == 0), stderr,
                "Final attempt to write %d block to disk FAILED.", i);

        LOG(stdout, "Block written (%d) \n", i);
    }

    Fail:

    SAFE_FREE(randomBuf);
    return hr;
}
Exemple #2
0
/*
 * Calls SD_write until SD_write doesn't fail
 * because of the random errors.
 */
int
safe_write(int sector, void* buf)
{
    int success = SD_write(sector, buf);
    if (success == -1 && sderrno == E_WRITING_FILE)
        success = safe_write(sector, buf);
    return success;
}
Exemple #3
0
static PT_THREAD( LOG_log(pt_t* pt) )
{
	u32 time;
	u8 is_filtered;
	u8 i;

	PT_BEGIN(pt);

	// systematically unlock the channel
	// because most of time no response is sent
	// when a response is needed, the channel will be locked
	DPT_unlock(&LOG.interf);

	switch ( LOG.state ) {
		case LOG_OFF:
		default:
			// empty the log fifo
			(void)FIFO_get(&LOG.in_fifo, &LOG.fr);

			// loop back for next frame
			PT_RESTART(pt);
			break;

		case LOG_RAM:
#ifdef SAVE_IN_RAM_ENABLED
#endif
			break;

		case LOG_EEPROM:
			// if address is out of range
			if ( LOG.eeprom_addr >= EEPROM_END_ADDR ) {
				// logging is no more possible
				// so quit
				PT_EXIT(pt);
			}
			break;

		case LOG_SDCARD:
			// if address is out of range
			if ( LOG.sdcard_addr >= SDCARD_END_ADDR ) {
				// logging is no more possible
				// so quit
				PT_EXIT(pt);
			}
			break;
	}

	// wait while no frame is present in the fifo
	PT_WAIT_WHILE(pt, KO == FIFO_get(&LOG.in_fifo, &LOG.fr));

	// if it is a log command
	if ( (LOG.fr.cmde == FR_LOG_CMD) && (!LOG.fr.resp) ) {
		// treat it
		LOG_command(&LOG.fr);

		// send the response
		DPT_lock(&LOG.interf);
		PT_WAIT_UNTIL(pt, OK == DPT_tx(&LOG.interf, &LOG.fr));
		DPT_unlock(&LOG.interf);


		// and wait till the next frame
		PT_RESTART(pt);
	}

	// filter the frame according to its origin
	is_filtered = OK;	// by default, every frame is filtered
	for ( i = 0; i < sizeof(LOG.orig_filter); i++ ) {
		// passthrough or frame origin and filter acceptance match
		if ( (LOG.orig_filter[i] == 0x00) || (LOG.orig_filter[i] == LOG.fr.orig) ){
			is_filtered = KO;
			break;
		}
	}

	// if frame is filtered away
	if ( is_filtered ) {
		// lop back for next frame
		PT_RESTART(pt);
	}

	// build the log packet
	LOG.block.index = LOG.index;
	time = TIME_get();
	LOG.block.time[0] = (u8)(time >> 16);
	LOG.block.time[1] = (u8)(time >>  8);
	LOG.block.fr = LOG.fr;

	switch ( LOG.state ) {
		case LOG_OFF:
		default:
			// shall never happen but just in case
			// loop back for next frame
			PT_RESTART(pt);
			break;

		case LOG_RAM:
#ifdef SAVE_IN_RAM_ENABLED
			LOG.ram_buffer[LOG.ram_index] = LOG.block;
			if ( LOG.ram_index < (RAM_BUFFER_SIZE - 1) ) {
				LOG.ram_index++;
			}
#endif
			break;

		case LOG_EEPROM:
			// save it to eeprom
			PT_WAIT_UNTIL(pt, EEP_write(LOG.eeprom_addr, (u8*)&LOG.block, sizeof(log_t)));

			// wait until saving is done
			PT_WAIT_UNTIL(pt, EEP_is_fini());
			break;

		case LOG_SDCARD:
			// save it to sdcard (fill the write buffer)
			PT_WAIT_UNTIL(pt, SD_write(LOG.sdcard_addr, (u8*)&LOG.block, sizeof(log_t)));

			// wait until saving is done

			break;
	}

	// loop back to treat the next frame to log
	PT_RESTART(pt);

	PT_END(pt);
}
Exemple #4
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;
}