📄 fileapi.c
字号:
/****************************************************************************** File Name : fileapi.c Description : API test for file API calls******************************************************************************//* Includes ---------------------------------------------------------------- */#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include <assert.h>#include "wrapper.h" /* from ../utils. Uses ../../src */#include "apicomm.h"#include "stavfs.h"/* Private Types ----------------------------------------------------------- *//* Private Constants ------------------------------------------------------- */#define PUSH_FILES_LIMIT (50) /* maximum number of files to attempt to create for PushMaxOpen/PartitionFiles. The actual ceilings we should hit are currently 32 open file handles (MAX_OPEN_FILES, src/file.h) and 32 files per partition (MAX_FILES, src/device.h) */#define LONG_FILE_NAME "1234567890123456789012345678901234567890123456789012345" /* file name that exactly fits the allowed length (dir.h MAX_FILE_NAME_LEN-1=55) */ #define EXT_LONG_FILE_NAME "123456789012345678901234567890123456789012345678901234567890" /* LONG_FILE_NAME plus spurious character */#define TRUNC_LONG_FILE_NAME "123456789012345678901234567890123456789012345678901234" /* LONG_FILE_NAME minus one character *//* Private Variables ------------------------------------------------------- */U8 Buffer[1000];/* Private Macros ---------------------------------------------------------- *//* Private Function Prototypes --------------------------------------------- */static TestResult_t OpenFile (int TestNo, TestVariant_t Variant);static TestResult_t WriteFile (int TestNo, TestVariant_t Variant);static TestResult_t ReadFile (int TestNo, TestVariant_t Variant);static TestResult_t SeekFile (int TestNo, TestVariant_t Variant);static TestResult_t GetFilePosition (int TestNo, TestVariant_t Variant);static TestResult_t CloseFile (int TestNo, TestVariant_t Variant);static TestResult_t DeleteFile (int TestNo, TestVariant_t Variant);static TestResult_t GetFreeDiskSpace(int TestNo, TestVariant_t Variant);static TestResult_t PushLimits (int TestNo, TestVariant_t Variant);static TestResult_t PushSpace (int TestNo, TestVariant_t Variant);static void TryBadFileOpen (STAVFS_Handle_t DiskHandle, STAVFS_FileHandle_t Dir, char * PathName, STAVFS_OpenMode_t OpenMode, U64 Size, char * Descr, TestResult_t * Result_p);static void PushMaxHandles (STAVFS_Handle_t DiskHandle, TestResult_t * Result_p);static void PushMaxPartitionFiles (STAVFS_Handle_t DiskHandle, TestResult_t * Result_p);static void TryBadDelete (STAVFS_Handle_t DiskHandle, STAVFS_FileHandle_t Dir, char * PathName, char * Descr, TestResult_t * Result_p);static TestResult_t MakeStdFile (STAVFS_Handle_t DiskHandle);static void DelCmpEnd (STAVFS_Handle_t DiskHandle, TestResult_t * Result_p);/* optional extra reporting of GetFreeDiskSpace at strategic points */static TestResult_t RptFreeSpace (STAVFS_Handle_t DiskHandle, BOOL Extras, char * Descr);#define DO_RPT_FREE_SPACE#ifdef DO_RPT_FREE_SPACE#define RPT_FREE_SPACE(DiskHandle, Extras, Descr) RptFreeSpace(DiskHandle, Extras, Descr)#else#define RPT_FREE_SPACE(DiskHandle, Extras, Descr)#endif/* Functions --------------------------------------------------------------- *//******************************************************************************Function Name : FileAPI Description : File api test section function Parameters :******************************************************************************/BOOL FileAPI(parse_t * pars_p, char *result_sym_p){ char *Description = "File API tests"; TestCall_t TestList[] = { OpenFile, /* Test 001 */ WriteFile, /* Test 002 */ ReadFile, /* Test 003 */ CloseFile, /* Test 004 */ SeekFile, /* Test 005 */ GetFilePosition, /* Test 006 */ DeleteFile, /* Test 007 */ GetFreeDiskSpace, /* Test 008 */ PushLimits, /* Test 009 */ PushSpace, /* Test 010 */ }; GenericTest (pars_p, result_sym_p, Description, TestList, TABLE_LEN(TestList)-1, TEST_VARIANT_A); return (FALSE);}/******************************************************************************Function Name : OpenFile Description : API tests for STAVFS_OpenFile Parameters :******************************************************************************/static TestResult_t OpenFile(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_Handle_t DiskHandle; STAVFS_FileHandle_t FileHandle; STAVFS_OpenParams_t OpenParams; /* for reopen near end */ STAVFS_TermParams_t TermParams; /* for Term near end */ U64 FileSize; OpenParams.Flags = 0; TermParams.Flags = 0; I64_SetValue (STD_FILE_SIZE, 0, FileSize); /* Bad device handles (stale ones attempted after we've closed/terminated a device) */ /* Provided only API tests have been run since startup, handle 2 has never been opened */ TryBadFileOpen ((void *) 2, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "with a device handle that has never been opened (inside the range used)", &Result); TryBadFileOpen ((void *) 10000, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "with out-of-range device handle", &Result); /* need an open device for most remaining tests */ if (TEST_PASSED != OpenPartition (0, 0, "Dev0", &DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } else { /* Bad type/mode combinations */ TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, ".", STAVFS_WRITE_MODE, FileSize, "opening root directory for write", &Result); /* (on the current driver, this would lead to creation of a file called '.' - were it not for the fact that we specifically reserve file names '.' and '..' for future use as directory indicators) */ TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, "TestDir", STAVFS_DIRECTORY_FILE | STAVFS_STREAM_FILE, FileSize, "creating a directory that is also a stream file", &Result); /* (assumes "TestDir" does not currently exist. Will currently fail simply because creating directories is not supported) */ TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, "TestDir", STAVFS_DIRECTORY_FILE | STAVFS_CYCLIC_MODE, FileSize, "creating a directory that is also a cyclic file", &Result); /* (assumes "TestDir" does not currently exist. Will currently fail simply because creating directories is not supported) */ /* Bad CurrentDirectory arguments */ TryBadFileOpen (DiskHandle, (void *) 0, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "opening a file with CurrentDirectory ((void*) 0) (inside file table range)", &Result); /* a file handle that won't have been used unless the Push tests have been run */ TryBadFileOpen (DiskHandle, (void *) 20, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "opening a file with CurrentDirectory ((void*) 20) (inside file table range)", &Result); TryBadFileOpen (DiskHandle, (void *) 10000, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "opening a file with CurrentDirectory ((void*) 10000) (outside file table range)", &Result); /* try a valid file handle - that isn't a directory */ if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error opening file\n")); Result = TEST_FAILED; } else { TryBadFileOpen (DiskHandle, FileHandle, "CreateOnNonDir", STAVFS_WRITE_MODE, FileSize, "with a non-directory open file handle specified for CurrentDirectory", &Result); if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file\n")); Result = TEST_FAILED; } } /* Bad PathName argument */ TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, NULL, STAVFS_WRITE_MODE, FileSize, "creating a new file with NULL name", &Result); TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, "", STAVFS_WRITE_MODE, FileSize, "creating a new file with zero length name", &Result); /* The next two check reserving of '/' and '\' for use as path separators in the future. The test for whichever is finally chosen will turn into a probe of the directory- following/creation logic, including such issues as disallowing '//' (zero length directory name) */ TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, "NameWithPath/Separator", STAVFS_WRITE_MODE, FileSize, "creating a new file with a '/' in the name", &Result); TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, "NameWithPath\\Separator", STAVFS_WRITE_MODE, FileSize, "creating a new file with a '\\' in the name", &Result); /* File name one character too long */ TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, EXT_LONG_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "creating a new file with a name exceeding the maximum length", &Result); /* Check correct handling of partition files / open handles limits - postponed to "PushLimits" test executed after the other ones */ /* requested size > free space - see PushSpace test later */ /* NULL pointer for where to put new file handle. Written-out TryBadFileOpen */ if (ST_NO_ERROR == STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, NULL)) { STTBX_Print (("STAVFS_OpenFile should fail with NULL specified for new handle destination\n")); Result = TEST_FAILED; /* Goodness knows how we should Close it explicitly, but it should happen implicitly when we close the partition. We don't delete STD_FILE_NAME. */ } /* close and terminate partition, with "device that was open but is now closed/terminated" tests postponed earlier */ if (ST_NO_ERROR != STAVFS_Close (DiskHandle)) { STTBX_Print (("Failed to close partition\n")); Result = TEST_FAILED; } TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "with a device handle that was open but is now closed", &Result); /* try to reopen before terminate in order to make "was open but now terminated" test a bit more strict */ if (ST_NO_ERROR != STAVFS_Open ("Dev0", &OpenParams, &DiskHandle)) { STTBX_Print (("Failed to reopen partition\n")); Result = TEST_FAILED; } if (TEST_PASSED != STAVFS_Term ("Dev0", &TermParams)) { STTBX_Print (("Failed to terminate partition\n")); Result = TEST_FAILED; } TryBadFileOpen (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, "with a device handle that was open but is now terminated", &Result); } return (Result);}/******************************************************************************Function Name : TryBadFileOpen Description : Helper for FileOpen: attempt a file open that should fail, but if it does succeed close and delete the file Parameters : Partition handle, OpenParams (both for STAVFS_Open), description to complete error message, place to record test failure******************************************************************************/static void TryBadFileOpen(STAVFS_Handle_t DiskHandle, STAVFS_FileHandle_t Dir, char * PathName, STAVFS_OpenMode_t OpenMode, U64 Size, char * Descr, TestResult_t * Result_p){ STAVFS_FileHandle_t FileHandle; if (ST_NO_ERROR == STAVFS_OpenFile (DiskHandle, Dir, PathName, OpenMode, Size, &FileHandle)) { STTBX_Print (("STAVFS_OpenFile should fail %s\n", Descr)); if (Result_p != NULL) { *Result_p = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Failed closing that handle\n")); } if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, Dir, PathName)) { STTBX_Print (("Failed deleting that file\n")); /* (which will be particularly likely if DiskHandle/Dir/PathName are invalid in some way as part of the test)*/ } } }/******************************************************************************Function Name : WriteFile Description : API tests for STAVFS_WriteFile Parameters :******************************************************************************/static TestResult_t WriteFile(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_Handle_t DiskHandle; STAVFS_FileHandle_t FileHandle; U64 FileSize; U32 DataWritten; I64_SetValue (STD_FILE_SIZE, 0, FileSize); /* Bad file handles (stale one attempted after we've closed a file) */ if (ST_NO_ERROR == STAVFS_WriteFile (STAVFS_NULL_FILE_HANDLE, "a", 1, &DataWritten)) { STTBX_Print (("STAVFS_WriteFile should fail writing to STAVFS_NULL_FILE_HANDLE\n")); Result = TEST_FAILED; } /* provided PushMaxHandles has not been run, file handle 20 will never have been used. Either way, it shouldn't be in use at the moment */ if (ST_NO_ERROR == STAVFS_WriteFile ((void *) 20, "b", 1, &DataWritten)) { STTBX_Print (("STAVFS_WriteFile should fail with a file handle that has never been opened" " (inside the range used)\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_WriteFile ((void *) 5678, "b", 1, &DataWritten)) { STTBX_Print (("STAVFS_WriteFile should fail with out-of-range file handle\n")); Result = TEST_FAILED; } /* get an open device and file */ if (TEST_PASSED != OpenPartition (0, 0, "Dev0", &DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } else { if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error opening file\n")); Result = TEST_FAILED; } else { /* NULL data/DataWritten */ if (ST_NO_ERROR == STAVFS_WriteFile (FileHandle, NULL, 1, &DataWritten)) { STTBX_Print (("STAVFS_WriteFile should fail with NULL data\n")); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, "c", 1, NULL)) { STTBX_Print (("Failed to write data when not returning data written\n")); Result = TEST_FAILED; } /* monster write > free space - see PushSpace test later */ /* close the file we opened, and check we can no longer write to it */ if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -