📄 fileapi.c
字号:
STTBX_Print (("Error closing file (1)\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_WriteFile (FileHandle, "d", 1, &DataWritten)) { STTBX_Print (("STAVFS_WriteFile should fail writing to a closed file handle\n")); Result = TEST_FAILED; } /* reopen for read only and confirm we can't then write */ if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_READ_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error reopening file\n")); Result = TEST_FAILED; } else { if (ST_NO_ERROR == STAVFS_WriteFile (FileHandle, "e", 1, &DataWritten)) { STTBX_Print (("STAVFS_WriteFile should fail on a handle without write access\n")); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file (2)\n")); Result = TEST_FAILED; } } /* delete the file (not actually necessary whilst it's STD_FILE_NAME) */ if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME)) { STTBX_Print (("Error deleting file\n")); Result = TEST_FAILED; } } if (TEST_PASSED != ClosePartition("Dev0", DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } } return (Result);}/******************************************************************************Function Name : ReadFile Description : API tests for STAVFS_ReadFile Parameters :******************************************************************************/static TestResult_t ReadFile(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_Handle_t DiskHandle; STAVFS_FileHandle_t FileHandle; U64 FileSize; U32 DataWritten, DataRead; U8 Buf[10]; I64_SetValue (STD_FILE_SIZE, 0, FileSize); /* Bad file handles (stale one attempted after we've closed a file) */ if (ST_NO_ERROR == STAVFS_ReadFile (STAVFS_NULL_FILE_HANDLE, Buf, 1, &DataRead)) { STTBX_Print (("STAVFS_ReadFile should fail reading from 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_ReadFile ((void *) 20, Buf, 1, &DataRead)) { STTBX_Print (("STAVFS_ReadFile should fail with a file handle that has" " never been opened (inside the range used)\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_ReadFile ((void *) 5678, Buf, 1, &DataRead)) { STTBX_Print (("STAVFS_ReadFile 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 | STAVFS_READ_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error opening file\n")); Result = TEST_FAILED; } else { /* Write some data so it can be read back */ if (ST_NO_ERROR == STAVFS_WriteFile (STAVFS_NULL_FILE_HANDLE, "1234567890", 10, &DataWritten)) { STTBX_Print (("Error writing file\n")); Result = TEST_FAILED; if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file\n")); } } else { if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, "1234567890", 10, &DataWritten)) { STTBX_Print (("Error writing file\n")); Result = TEST_FAILED; } /* NULL buffer/DataRead */ if (ST_NO_ERROR == STAVFS_ReadFile (FileHandle, NULL, 1, &DataRead)) { STTBX_Print (("STAVFS_ReadFile should fail with NULL buffer\n")); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_ReadFile(FileHandle, Buf, 1, NULL)) { STTBX_Print (("STAVFS_ReadFile should work with NULL DataRead\n")); Result = TEST_FAILED; } /* close the file we opened, and check we can no longer read from it */ if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file (1)\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_ReadFile (FileHandle, Buf, 1, &DataRead)) { STTBX_Print (("STAVFS_ReadFile should fail reading from a closed file handle\n")); Result = TEST_FAILED; } /* reopen for write only and confirm we can't then read */ if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_WRITE_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error reopening file\n")); Result = TEST_FAILED; } else { if (ST_NO_ERROR == STAVFS_ReadFile (FileHandle, Buf, 1, &DataRead)) { STTBX_Print (("STAVFS_ReadFile should fail on a handle without read access\n")); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file (2)\n")); Result = TEST_FAILED; } } } /* delete the file (not actually necessary whilst it's STD_FILE_NAME) */ if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME)) { STTBX_Print (("Error deleting file\n")); Result = TEST_FAILED; } } if (TEST_PASSED != ClosePartition("Dev0", DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } } return (Result);}/******************************************************************************Function Name : CloseFile Description : API tests for STAVFS_CloseFile Parameters :******************************************************************************/static TestResult_t CloseFile(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_Handle_t DiskHandle; STAVFS_FileHandle_t FileHandle; U64 FileSize; I64_SetValue (STD_FILE_SIZE, 0, FileSize); /* Bad file handles (stale one attempted after we've closed a file) */ if (ST_NO_ERROR == STAVFS_CloseFile (STAVFS_NULL_FILE_HANDLE)) { STTBX_Print (("STAVFS_CloseFile should fail for 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_CloseFile ((void *) 20)) { STTBX_Print (("STAVFS_CloseFile should fail with a file handle that has" " never been opened (inside the range used)\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_CloseFile ((void *) 5678)) { STTBX_Print (("STAVFS_CloseFile 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 | STAVFS_READ_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error opening file\n")); Result = TEST_FAILED; } else { /* close the file we opened, and check we can only do so once */ if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_CloseFile (FileHandle)) { STTBX_Print (("STAVFS_CloseFile should fail for a file that is already closed\n")); Result = TEST_FAILED; } /* delete the file (not actually necessary whilst it's STD_FILE_NAME) */ if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME)) { STTBX_Print (("Error deleting file\n")); Result = TEST_FAILED; } } if (TEST_PASSED != ClosePartition("Dev0", DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } } return (Result);}/******************************************************************************Function Name : SeekFile Description : API tests for STAVFS_SeekFile Parameters :******************************************************************************/static TestResult_t SeekFile(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_Handle_t DiskHandle; STAVFS_FileHandle_t FileHandle; U64 FileSize; S64 Pos; I64_SetValue (STD_FILE_SIZE, 0, FileSize); /* Bad file handles (stale one attempted after we've closed a file) */ I64_SetValue (0, 0, Pos); if (ST_NO_ERROR == STAVFS_SeekFile (STAVFS_NULL_FILE_HANDLE, Pos, STAVFS_START, TRUE)) { STTBX_Print (("STAVFS_SeekFile should fail for 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_SeekFile ((void *) 20, Pos, STAVFS_START, TRUE)) { STTBX_Print (("STAVFS_SeekFile should fail with a file handle that has" " never been opened (inside the range used)\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_SeekFile ((void *) 5678, Pos, STAVFS_START, TRUE)) { STTBX_Print (("STAVFS_SeekFile 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 { /* attempt to read-seek on this handle (not opened for read) */ if (ST_NO_ERROR == STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, FALSE)) { STTBX_Print (("STAVFS_SeekFile should fail read-seek in a file not opened for read\n")); Result = TEST_FAILED; } /* ... seekable positions ... */ /* try some invalid mode values */ if (ST_NO_ERROR == STAVFS_SeekFile (FileHandle, Pos, -1, TRUE) || ST_NO_ERROR == STAVFS_SeekFile (FileHandle, Pos, 4, TRUE)) { STTBX_Print (("STAVFS_SeekFile should fail for invalid seek mode\n")); Result = TEST_FAILED; } /* ... 2 for BOOL WritePosition ... */ /* close the file we opened, and confirm we can't then seek */ if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file\n")); Result = TEST_FAILED; } if (ST_NO_ERROR == STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, TRUE)) { STTBX_Print (("STAVFS_SeekFile should fail for a closed file handle\n")); Result = TEST_FAILED; } /* reopen for read only and confirm we can't then write-seek */ if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, STD_FILE_NAME, STAVFS_READ_MODE, FileSize, &FileHandle)) { STTBX_Print (("Error reopening file\n")); Result = TEST_FAILED; } else { if (ST_NO_ERROR == STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, TRUE)) { STTBX_Print (("STAVFS_SeekFile should fail write-seek" " in a file not opened for write\n")); Result = TEST_FAILED; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -