/** * Try to call unlink on the read only file system. **/ void unlink_rdonlyfs(){ const char* testName = "unlink_rdonlyfs"; const char* expectedError = "Read-only file system"; char* error = 0; struct stat statbuf; errno = 0; int ret = unlink( "compressed_readonly/basic"); int savedErrno = errno; if( ret != -1 ){ printFail( testName, "Should not have been able to call unlink on the read only file system." ); return; } if( savedErrno != 30 || strcmp( error = strerror( savedErrno ), expectedError ) != 0 ){ printFail( testName, "The error was not correct." ); return; } // Be sure the file is still there. if( stat( "compressed_readonly/basic", &statbuf ) != 0 ){ printFail( testName, "The file is gone." ); return; } printPass( testName ); return; }
/** * Unlink a file and be sure it isn't there. **/ void unlink_file(){ const char* testName = "unlink_file"; int theFile = -1; int ret = 0; struct stat statbuf; theFile = open( testName, O_RDWR | O_CREAT, S_IRWXU ); write( theFile, gData1, strlen( gData1 ) ); close( theFile ); errno = 0; ret = stat( testName, &statbuf ); if( (ret == 0) && (errno == 0) ){ ret = unlink( testName ); if( (ret == 0) && (errno == 0) ){ ret = stat( testName, &statbuf ); if( (ret == -1) && (errno == ENOENT) ){ printPass( testName ); }else{ printFail( testName, "Expected ret = -1 and errno = ENOENT" ); } }else{ printFail( testName, "Unlink() failed." ); } }else{ printFail( testName, "Failed to create the test file" ); } }
/** * This is a lot like getdirentries(). **/ void readdir_oneFile(){ const char* testName = "readdir_oneFile"; const char* dirName = "/readdir_oneFile"; const char* fileName = "/readdir_oneFile/foo.txt"; DIR* dirPtr = 0; const char* expectedResult = "foo.txt"; struct dirent* dirEntry = 0; // Make a directory with a file. mkdir( dirName, 0777 ); int theFile = open( fileName, O_CREAT | O_RDWR ); write( theFile, gData1, 5 ); close( theFile ); errno = 0; dirPtr = opendir( dirName ); if( (errno == 0) && (dirPtr > 0) ){ dirEntry = readdir( dirPtr ); if( (dirEntry > 0) && (errno == 0) ){ if( strcmp( dirEntry->d_name, expectedResult ) == 0 ){ printPass( testName ); }else{ printFail( testName, "File name was not correct" ); } }else{ printFail( testName, "Call to readdir() failed" ); } }else{ printFail( testName, "Unable to open the directory" ); } }
/** * Unlink a file and create a new one with the same name (typical use case). **/ void unlink_recreate(){ const char* testName = "unlink_recreate"; int theFile = -1; int ret = 0; struct stat statbuf; theFile = open( testName, O_RDWR | O_CREAT, S_IRWXU ); write( theFile, gData1, strlen( gData1 ) ); close( theFile ); unlink( testName ); if( stat( testName, &statbuf ) == -1 ){ theFile = open( testName, O_RDWR | O_CREAT, S_IRWXU ); write( theFile, gData1, strlen( gData1 ) ); close( theFile ); if( stat( testName, &statbuf ) == 0 ){ printPass( testName ); }else{ printFail( testName, "File was not recreated" ); } }else{ printFail( testName, "File was not removed" ); } }
/** * Create a file, write, read. **/ void fflush_basic(){ const char* testName = "fflush_basic"; int ret = -1; int theFile = -1; FILE* pFile = 0; char bytesRead[ gBufLarge ]; memset( bytesRead, 0, gBufLarge ); // Write. pFile = fopen( testName, "w+" ); fwrite( gData1, sizeof( char ), strlen( gData1 ), pFile ); // Read what was written. theFile = open( testName, O_RDONLY ); read( theFile, bytesRead, strlen( gData1 ) ); close( theFile ); if( strlen( bytesRead ) == 0 ){ // Flush. errno = 0; ret = fflush( pFile ); if( (ret == 0) && (errno == 0) ){ // Read what was written. theFile = open( testName, O_RDONLY ); read( theFile, bytesRead, strlen( gData1 ) ); close( theFile ); if( strcmp( bytesRead, gData1 ) == 0 ){ printPass( testName ); }else{ printFail( testName, "Flushed data was not correct" ); } }else{ printFail( testName, "Expected ret == 0 and errno == 0" ); } }else{ printFail( testName, "Looks like flushing already happened, so we cannot continue with this test" ); } fclose( pFile ); }
/** * Basic test for SEEK_CUR. **/ void fseek_cur(){ const char* testName = "fseek_cur"; const char* bytesExpected = "abc0e"; char bytesRead[ gBufSmall ]; int ret = -1; memset( bytesRead, 0, gBufSmall ); int theFile = open( testName, O_RDWR | O_CREAT, S_IRWXU ); write( theFile, gData1, strlen( gData1 ) ); close( theFile ); FILE* filePtr = fopen( testName, "r+" ); // Write 2 bytes, then fseek ahead 1 more. fwrite( gData1, 1, 2, filePtr ); errno = 0; ret = fseek( filePtr, 1, SEEK_CUR ); if( (ret == 0) && (errno == 0) ){ // Write a character and verify. fwrite( gData2, 1, 1, filePtr ); fflush( filePtr ); theFile = open( testName, O_RDONLY ); read( theFile, &bytesRead, strlen( gData1 ) + 1 ); close( theFile ); if( strcmp( bytesRead, bytesExpected ) == 0 ){ printPass( testName ); }else{ //printf( "expected: %s, actual: %s\n", bytesExpected, bytesRead ); printFail( testName, "Data did not match" ); } }else{ printFail( testName, "Expected 0" ); } close( theFile ); }
/** * Unlink a file that does not exist. **/ void unlink_enoent(){ const char* testName = "unlink_enoent"; errno = 0; int ret = unlink( testName ); if( (ret == -1) && (errno == ENOENT) ){ printPass( testName ); }else{ printFail( testName, "Unlink should have failed" ); } }
/** * Call fstat() on a file that's larger than a block. **/ void fstat_largeFile(){ const char* testName = "fstat_largeFile"; char aBlock[ gBufBlockSize + 1 ]; struct stat statbuf; int ret = 0; // Write one more byte than a block. memset( aBlock, 'a', gBufBlockSize ); aBlock[ gBufBlockSize ] = 0; int theFile = open( testName, O_CREAT | O_RDWR ); write( theFile, aBlock, gBufBlockSize + 1 ); errno = 0; ret = fstat( theFile, &statbuf ); if( (errno == 0) && (ret == 0) ){ if( statbuf.st_mode == S_IFREG ){ if( statbuf.st_nlink == 1 ){ if( statbuf.st_blksize == gBufBlockSize ){ if( statbuf.st_blocks == 2 ){ printPass( testName ); }else{ printFail( testName, "Expected st_blocks = 2" ); } }else{ printFail( testName, "Expected st_blksize = 4096" ); } }else{ printFail( testName, "Expected st_nlink = 1" ); } }else{ printFail( testName, "Expected st_mode = S_IFREG" ); } }else{ printFail( testName, "Expected errno == 0 and ret == 0" ); } close( theFile ); }
/** * Basic test for SEEK_END. **/ void fseek_end(){ const char* testName = "fseek_end"; const char* bytesExpected = "abcd0"; char bytesRead[ gBufSmall ]; int ret = -1; memset( bytesRead, 0, gBufSmall ); int theFile = open( testName, O_RDWR | O_CREAT, S_IRWXU ); write( theFile, gData1, strlen( gData1 ) ); close( theFile ); FILE* filePtr = fopen( testName, "r+" ); errno = 0; // Seek ret = fseek( filePtr, -1, SEEK_END ); if( (ret == 0) && (errno == 0) ){ // Write a character and verify. fwrite( gData2, 1, 1, filePtr ); fflush( filePtr ); theFile = open( testName, O_RDONLY ); read( theFile, &bytesRead, strlen( gData1 ) ); close( theFile ); if( strcmp( bytesRead, bytesExpected ) == 0 ){ printPass( testName ); }else{ printFail( testName, "Data did not match" ); } }else{ printFail( testName, "Expected 0" ); } fclose( filePtr ); }
void printFailResult() { extern lua_State *L; char buf[128] = {0}; char *station = (char *)getTableElement(L, "con", "STATION"); printItemHead( station ); sprintf(buf, "%s TEST FAILED.", station); printFail(buf); drawButtonRetest(); drawButtonPoweroff(); }
/** * Trigger ebadf **/ void fstat_ebadf(){ const char* testName = "stat_ebadf"; struct stat statbuf; int ret = -1; errno = 0; ret = fstat( -1, &statbuf ); if( (errno == EBADF) && (ret == -1) ){ printPass( testName ); }else{ printFail( testName, "Expected EBADF" ); } }
/** * Trigger enoent. **/ void stat_enoent(){ const char* testName = "stat_enoent"; struct stat statbuf; int ret = -1; errno = 0; ret = stat( "stat_enoent_does_not_exist", &statbuf ); if( (errno == ENOENT) && (ret == -1) ){ printPass( testName ); }else{ printFail( testName, "Expected ENOENT" ); } }
/** * Call fstat() on a dir. **/ void fstat_dir(){ const char* testName = "fstat_dir"; const char* dirName = "/fstat_dir"; struct stat statbuf; int ret = 0; mkdir( dirName, 0777 ); int theDir = open( dirName, O_RDONLY ); errno = 0; ret = fstat( theDir, &statbuf ); if( (errno == 0) && (ret == 0) ){ if( statbuf.st_mode == S_IFDIR ){ if( statbuf.st_nlink == 1 ){ if( statbuf.st_blksize == 4096 ){ if( statbuf.st_blocks == 0 ){ printPass( testName ); }else{ printFail( testName, "Expected st_blocks = 0" ); } }else{ printFail( testName, "Expected st_blksize = 4096" ); } }else{ printFail( testName, "Expected st_nlink = 1" ); } }else{ printFail( testName, "Expected st_mode = S_IFDIR" ); } }else{ printFail( testName, "Expected errno == 0 and ret == 0" ); } close( theDir ); }
/** * Close a file and try to reuse the fildes by closing it. **/ void close_twice(){ const char* testName = "close_twice"; int theFile = -1; int flags = -1; int closeRet = 0; // Create a file. flags = O_RDWR | O_CREAT; theFile = open( testName, flags ); closeRet = close( theFile ); if( closeRet == 0 ){ errno = 0; closeRet = close( theFile ); if( (errno == EBADF) && (closeRet == -1) ){ printPass( testName ); }else{ printFail( testName, "Expected EBADF and a return value of -1" ); } }else{ printFail( testName, "Expected return value of 0" ); } }
/** * Open a file several times and close just some of them. * This is also a general test for multiple file descriptors * for the same file. **/ void close_multipleRefs(){ const char* testName = "close_multipleRefs"; int theFileCREAT = -1; int theFileWRONLY = -1; int theFileRDONLY = -1; int ret = 0; char bytesRead[ gBufSmall ]; char bytesExpected[ gBufSmall ]; memset( bytesRead, 0, gBufSmall ); memset( bytesExpected, 0, gBufSmall ); strcpy( bytesExpected, gData1 ); // Create a file. theFileCREAT = open( testName, O_CREAT ); theFileWRONLY = open( testName, O_WRONLY ); theFileRDONLY = open( testName, O_RDONLY ); ret = close( theFileCREAT ); if( ret == 0 ){ ret = write( theFileWRONLY, gData1, strlen( gData1 ) ); if( ret == strlen( gData1 ) ){ ret = close( theFileWRONLY ); if( ret == 0 ){ ret = read( theFileRDONLY, bytesRead, strlen( gData1 ) ); if( ret == strlen( gData1 ) ){ ret = close( theFileRDONLY ); if( ret == 0 ){ // Now further access should fail. ret = close( theFileCREAT ); if( (ret == -1) && (errno == EBADF) ){ printPass( testName ); }else{ printFail( testName, "Expected final return of -1 and errno of EBADF" ); } }else{ printFail( testName, "Third close was not successful" ); } }else{ printFail( testName, "Read was not successful" ); } }else{ printFail( testName, "Second close was not successful" ); } }else{ printFail( testName, "Write was not successful" ); } }else{ printFail( testName, "First close was not successful" ); } // Just in case... close( theFileWRONLY ); close( theFileRDONLY ); }
/** * Trigger ebadf. **/ void fflush_ebadf(){ const char* testName = "fflush_ebadf"; FILE* pFile; // Write. pFile = fopen( testName, "w+" ); fclose( pFile ); errno = 0; int ret = fflush( pFile ); if( (errno == EBADF) && (ret == EOF) ){ printPass( testName ); }else{ printFail( testName, "Expected EBADF and EOF" ); } }
int main() { /* initialize bare minimum game variables */ int seed = 101; // seed for game init int numPlayers = 2; // number of players int kCards[10] = {adventurer, council_room, feast, // kingdom cards gardens, mine, remodel, smithy, village, baron, great_hall}; struct gameState gState; int r = initializeGame(numPlayers, kCards, seed, &gState); // initialize a new game int s; // exit status for function /* extra parameters specific to this function */ int currentPlayer; int card; /* game state variables used by this function */ int num; /* counter variables */ // test over all players for(currentPlayer = 0; currentPlayer < numPlayers; currentPlayer++){ // test over different cards for(card = 0; card < 20; card += 3){ // test over some quantities of the chosen card for(num = 0; num <= 9; num += 3){ memset(&gState, 23, sizeof(struct gameState)); // clear the game state r = initializeGame(numPlayers, kCards, seed, &gState); // initialize the game // set add'l game variables initCards(&gState, num, card, currentPlayer); // call the function under test s = fullDeckCount(currentPlayer, card, &gState); if(s == num){ printPass(); } else printFail(); printTest("correct total of cards returned", num, s); } } } return 0; }
/** * "A component of the path prefix is not a directory." **/ void unlink_enotdir(){ const char* testName = "unlink_enotdir"; const char* dir1Name = "unlink_enotdir"; const char* fileName = "unlink_enotdir/foo"; const char* dir2Name = "unlink_enotdir/foo/foodir"; mkdir( dir1Name, 0777 ); int theFile = open( fileName, O_RDWR | O_CREAT ); close( theFile ); errno = 0; int ret = unlink( dir2Name ); if( (ret == -1) && (errno == ENOTDIR) ){ printPass( testName ); }else{ printFail( testName, "Expected ret == -1 and errno == ENOTDIR. Fails due to ALC-400. (Fixed)" ); } }
/** * Close a file and try to reuse the fildes. **/ void close_basic(){ const char* testName = "close_basic"; int theFile = -1; int flags = -1; int writeRet = 0; // Create a file. flags = O_RDWR | O_CREAT; theFile = open( testName, flags ); close( theFile ); // Write errno = 0; writeRet = write( theFile, gData1, strlen( gData1 ) ); if( (errno == EBADF) && (writeRet == -1) ){ printPass( testName ); }else{ printFail( testName, "Expected EBADF and a return value of -1" ); } }
/** * Trigger enotdir **/ void stat_enotdir(){ const char* testName = "stat_enotdir"; const char* dir1Name = "/stat_enotdir"; const char* fileName = "/stat_enotdir/file"; const char* dir2Name = "/stat_enotdir/file/subdir"; struct stat statbuf; mkdir( dir1Name, 0777 ); int theFile = open( fileName, O_RDWR | O_CREAT ); close( theFile ); errno = 0; int ret = stat( dir2Name, &statbuf ); if( (errno == ENOTDIR) && (ret == -1) ){ printPass( testName ); }else{ printFail( testName, "Expected ENOTDIR and -1. Fails due to ALC-410. (Fixed)" ); } }
/** * Call stat() on a file. We do this in lots * of places but let's have a defined place for it. * Note that whether we support st_dev and st_ino is uncertain. **/ void stat_file(){ const char* testName = "stat_file"; char aBlock[ gBufBlockSize ]; struct stat statbuf; int ret = 0; // Write 4095 a's followed by a null. memset( aBlock, 'a', gBufBlockSize - 1 ); aBlock[ gBufBlockSize - 1 ] = 0; int theFile = open( testName, O_CREAT | O_RDWR ); write( theFile, aBlock, gBufBlockSize ); close( theFile ); errno = 0; ret = stat( testName, &statbuf ); if( (errno == 0) && (ret == 0) ){ if( statbuf.st_mode == S_IFREG ){ if( statbuf.st_nlink == 1 ){ if( statbuf.st_blksize == gBufBlockSize ){ if( statbuf.st_blocks == 1 ){ if( statbuf.st_size == gBufBlockSize ){ printPass( testName ); }else{ printFail( testName, "Expected size = 4096" ); } }else{ printFail( testName, "Expected st_blocks = 1" ); } }else{ printFail( testName, "Expected st_blksize = 4096" ); } }else{ printFail( testName, "Expected st_nlink = 1" ); } }else{ printFail( testName, "Expected st_mode = S_IFREG" ); } }else{ printFail( testName, "Expected errno == 0 and ret == 0" ); } }
/** * Call stat() on a dir. **/ void stat_dir(){ const char* testName = "stat_dir"; const char* dirName = "/stat_dir"; struct stat statbuf; int ret = 0; mkdir( dirName, 0777 ); errno = 0; ret = stat( testName, &statbuf ); if( (errno == 0) && (ret == 0) ){ if( statbuf.st_mode == S_IFDIR ){ if( statbuf.st_nlink == 1 ){ if( statbuf.st_blksize == 4096 ){ if( statbuf.st_blocks == 0 ){ if( statbuf.st_size == 0 ){ printPass( testName ); }else{ printFail( testName, "Expected size = 0" ); } }else{ printFail( testName, "Expected st_blocks = 0" ); } }else{ printFail( testName, "Expected st_blksize = 4096" ); } }else{ printFail( testName, "Expected st_nlink = 1" ); } }else{ printFail( testName, "Expected st_mode = S_IFDIR" ); } }else{ printFail( testName, "Expected errno == 0 and ret == 0" ); } }
int go_thread(void *data) { extern SDL_Window *window; extern int flow_count; extern lua_State *L; extern char *radios[]; extern int ITEM_NUMBER; extern SDL_Widget *button_ensure; extern int all_pass; int r; char buf[128] = {0}; char run_file[1024] = {0}; char log_file[1024] = {0}; char *path, *tmp; sprintf(buf, "%s test", radios[flow_count]); printItemHead(buf); // record log tmp = (char *)getTableElement(L, "self", "STAT"); sprintf(buf, "%slog", tmp); path = (char *)getTableElement(L, "logs", buf); sprintf( log_file, "%s", path); // sprintf( buf, "%s test start", radios[flow_count]); // recordlog( log_file, buf); path = (char *)getTableElement(L, "con", "PATH"); sprintf( run_file, "%sfunction/%s/run.lua",path,radios[flow_count]); printf( "go-run.lua: %s\n", run_file); // printf("Current dir: %s\n", getcwd(NULL, 0)); // chdir(radios[flow_count]); // printf("Current dir: %s\n", getcwd(NULL, 0)); // r = run(L, "run.lua"); r = run(L, run_file); // chdir(".."); // printf("Current dir: %s\n", getcwd(NULL, 0)); if (r == 0) { printPass("This item test PASS."); if (flow_count >= ITEM_NUMBER - 1) { all_pass = 1; } sprintf( buf, "%s test PASS!", radios[flow_count]); } else { printFail("This item test FAIL."); sprintf( buf, "%s test FAILED!", radios[flow_count]); } recordlog( log_file, buf); if ( r == 0 ) { drawButtonEnsure(); SDL_Delay(500); flow_count++; refreshWin(); if (getTableBooleanElement(L, "con", "AUTOTEST")) SDL_SignalEmit( button_ensure, "clicked", NULL); } else { if (getTableBooleanElement(L, "con", "RETEST_IF_FAIL")) drawButtonRetest(); if (getTableBooleanElement(L, "con", "GJ_IF_FAIL")) drawButtonPoweroff(); else { drawButtonEnsure(); flow_count++; } refreshWin(); } }
int printFail_4lua(lua_State *L) { printFail( (char *)getStr(L) ); return 0; }
/** * Read several files and directories. **/ void readdir_filesAndDirs(){ const char* testName = "/readdir_filesAndDirs"; const char* mainDirName = "/readdir_filesAndDirs/main"; const char* subdir1Name = "/readdir_filesAndDirs/main/dir1"; const char* subdir2Name = "/readdir_filesAndDirs/main/directory 2"; const char* subdir3Name = "/readdir_filesAndDirs/main/dir1/dir3"; // This should not get returned. const char* file1Name = "/readdir_filesAndDirs/main/file1"; const char* file2Name = "/readdir_filesAndDirs/main/file number 2"; const char* expected1 = "dir1"; const char* expected2 = "directory 2"; const char* expected3 = "file1"; const char* expected4 = "file number 2"; const char* ignore1 = "."; const char* ignore2 = ".."; int found1, found2, found3, found4 = 0; int theFile = 0; DIR* dirPtr = 0; struct dirent* dirEntry; int numItems = 0; // Make a directory with files and directories. mkdir( testName, 0777 ); mkdir( mainDirName, 0777 ); mkdir( subdir1Name, 0777 ); mkdir( subdir2Name, 0777 ); mkdir( subdir3Name, 0777 ); theFile = open( file1Name, O_CREAT | O_RDWR, S_IRWXU ); write( theFile, gData1, 5 ); close( theFile ); theFile = open( file2Name, O_CREAT | O_RDWR, S_IRWXU ); write( theFile, gData1, 5 ); close( theFile ); // Open the directory and get info. about it. dirPtr = opendir( mainDirName ); while( dirEntry = readdir( dirPtr ) ){ if( (strcmp( dirEntry->d_name, ignore1 ) == 0) || (strcmp( dirEntry->d_name, ignore2 ) == 0) ){ //printf( "Ignoring %s\n", dirEntry->d_name ); }else{ ++numItems; // Check values. A dirent only has d_ino (not supported) and d_name. if( strcmp( dirEntry->d_name, expected1 ) == 0 ){ found1 = 1; }else if( strcmp( dirEntry->d_name, expected2 ) == 0 ){ found2 = 1; }else if( strcmp( dirEntry->d_name, expected3 ) == 0 ){ found3 = 1; }else if( strcmp( dirEntry->d_name, expected4 ) == 0 ){ found4 = 1; } } } if( numItems == 4 ){ if( found1 && found2 && found3 && found4 ){ printPass( testName ); }else{ printFail( testName, "Missing an item" ); } }else{ printFail( testName, "Found wrong number of items" ); } closedir( dirPtr ); }
int main() { /* initialize bare minimum game variables */ int seed = 101; // seed for game init int numPlayers = 2; // number of players int kCards[10] = {adventurer, council_room, feast, // kingdom cards gardens, mine, remodel, smithy, village, baron, great_hall}; struct gameState gState; int r = initializeGame(numPlayers, kCards, seed, &gState); // initialize a new game int s; // exit status for function /* extra parameters specific to this function */ int currentPlayer; /* game state variables used by this function */ int card; int coins; int handCount; /* counter variables */ // test over all players for(currentPlayer = 0; currentPlayer < numPlayers; currentPlayer++){ // test over some choices of card to buy for(card = council_room; card < smithy; card++){ // test over some initial hand counts for(handCount = 0; handCount < 3; handCount ++){ memset(&gState, 23, sizeof(struct gameState)); // clear the game state r = initializeGame(numPlayers, kCards, seed, &gState); // initialize the game // set add'l game variables initCopper(&gState, currentPlayer, handCount); // initialze player's hand gState.coins = 0; // and 0 coins gState.discardCount[currentPlayer] = 0; // and 0 discards updateCoins(currentPlayer, &gState, 0); // and update for correct # of coins // get # of coins before playing card coins = gState.coins; // call the function under test s = cardFeast(&gState, currentPlayer, card); // tests // check for the purchased card in discard pile if(findCard(&gState, currentPlayer, card) == 1){ printPass(); } else printFail(); printTest("presence of purchased card in discard", 1, findCard(&gState, currentPlayer, card), currentPlayer); // check # of coins updateCoins(currentPlayer, &gState, 0); if(coins == gState.coins){ printPass(); } else printFail(); printTest("# of coins in game state", coins, gState.coins, currentPlayer); } } } return 0; }