📄 toplevel.c
字号:
/****************************************************************************** File Name : toplevel.c Description : Unit test for the top level API calls.******************************************************************************//* Includes ---------------------------------------------------------------- */#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include "wrapper.h"#include "stavfs.h"#include "avdevice.h"/* Private Types ----------------------------------------------------------- *//* Private Constants ------------------------------------------------------- *//* count of partitions we attempt to create and use */#define CNT_PARTITIONS TABLE_LEN(TestPartitionDefs)/* count of files used in DiskSpace test */#define CNT_FILES (3)/* Private Variables ------------------------------------------------------- *//* Private Macros ---------------------------------------------------------- *//* Private Function Prototypes --------------------------------------------- */ST_ErrorCode_t STAVFS_ResetPartition (STAVFS_Handle_t Handle);static TestResult_t Format (int TestNo, TestVariant_t Variant);static TestResult_t Init (int TestNo, TestVariant_t Variant);static TestResult_t Revision (int TestNo, TestVariant_t Variant);static TestResult_t Open (int TestNo, TestVariant_t Variant);static TestResult_t DiskSpace (int TestNo, TestVariant_t Variant);static TestResult_t PartitionNumbers (int TestNo, TestVariant_t Variant);static TestResult_t Resetting (int TestNo, TestVariant_t Variant);static TestResult_t Closing (int TestNo, TestVariant_t Variant);/* Functions --------------------------------------------------------------- *//******************************************************************************Function Name : TopLevel Description : TopLevel test section function. Parameters :******************************************************************************/BOOL TopLevel (parse_t * pars_p, char *result_sym_p){ char *Description = "Unit Test top level API calls"; TestCall_t TestList[] = { Format, /* Test 001 */ Init, /* Test 002 */ Init, /* Test 003 */ Revision, /* Test 004 */ Open, /* Test 005 */ DiskSpace, /* Test 006 */ PartitionNumbers, /* Test 007 */ Resetting, /* Test 008 */ Closing, /* Test 009 */ }; GenericTest (pars_p, result_sym_p, Description, TestList, TABLE_LEN(TestList)-1, TEST_VARIANT_A); return (FALSE);}/******************************************************************************Function Name : Format Description : Format the disk partitions Parameters :******************************************************************************/static TestResult_t Format(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_InitParams_t InitParams; STAVFS_TermParams_t TermParams; U64 Position, Size; int i; strcpy(InitParams.EVTName, EVT_DEVICE_NAME); strcpy(InitParams.ATAPIName, ATAPI_DEVICE_NAME); InitParams.Flags = 0; InitParams.MemoryPartition = system_partition; InitParams.Protocol = 1; InitParams.UnitNumber = 0; /* InitParams.PartitionNumber set below */ TermParams.Flags = 0; if (ST_NO_ERROR != STAVFS_CreateEmptyPartitionTable(EVT_DEVICE_NAME, ATAPI_DEVICE_NAME, 0, 1)) { STTBX_Print(("Error clearing partition table\n")); Result = TEST_FAILED; } for (i = 0; i < TABLE_LEN(TestPartitionDefs); ++i) { InitParams.PartitionNumber = i; if (ST_NO_ERROR != STAVFS_Init("Dev1", &InitParams)) { STTBX_Print(("Error initialising partition %i\n", i)); Result = TEST_FAILED; } else { I64_SetValue(TestPartitionDefs[i].Size, 0, Size); I64_SetValue(TestPartitionDefs[i].Position, 0, Position); if (ST_NO_ERROR != STAVFS_FormatPartition("Dev1", Size, Position, TestPartitionDefs[i].SizeOfCluster)) { STTBX_Print(("Error formating partition %i\n", i)); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_Term("Dev1", &TermParams)) { STTBX_Print(("Error terminating partition %i\n", i)); Result = TEST_FAILED; } } } return(Result);}/******************************************************************************Function Name : Init Description : Test the driver initialisation 1) Test for multiple initialisation. 2) Test for re-initialisation Parameters :******************************************************************************/static TestResult_t Init(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_InitParams_t InitParams; strcpy(InitParams.EVTName, EVT_DEVICE_NAME); strcpy(InitParams.ATAPIName, ATAPI_DEVICE_NAME); InitParams.Flags = 0; InitParams.MemoryPartition = system_partition; InitParams.Protocol = 1; InitParams.UnitNumber = 0; InitParams.PartitionNumber = 0; if (ST_NO_ERROR != STAVFS_Init("Dev1", &InitParams)) { STTBX_Print(("First initialisation failed\n")); Result = TEST_FAILED; } else { STAVFS_TermParams_t TermParams; TermParams.Flags = 0; if (TestNo == 1) { /* Test 001 - Multiple initialisation */ if (ST_ERROR_ALREADY_INITIALIZED != STAVFS_Init("Dev2", &InitParams)) { STTBX_Print(("Second initialisation did not give the correct error\n")); Result = TEST_FAILED; STAVFS_Term("Dev2", &TermParams); } } if (TestNo == 2) { /* Test 002 - re-initialisation */ if (ST_NO_ERROR != STAVFS_Term("Dev1", &TermParams)) { STTBX_Print(("First termination failed\n")); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_Init("Dev1", &InitParams)) { STTBX_Print(("Second initialisation failed\n")); Result = TEST_FAILED; STAVFS_Term("Dev1", &TermParams); } } if (ST_NO_ERROR != STAVFS_Term("Dev1", &TermParams)) { STTBX_Print(("Second termination failed\n")); Result = TEST_FAILED; } } return(Result);}/******************************************************************************Function Name : Revision Description : Check that a meaningful string is returned Parameters :******************************************************************************/static TestResult_t Revision(int TestNo, TestVariant_t Variant){ int a; int b; int c; if ((NULL != STAVFS_GetRevision ()) && (3 == sscanf(STAVFS_GetRevision (), "%i.%i.%i", &a, &b, &c)) && (0 <= a) && (a <= 512) && (0 <= b) && (b <= 512) && (0 <= c) && (c <= 512)) { return(TEST_PASSED); } return(TEST_FAILED);}/******************************************************************************Function Name : Open Description : Test openning and closing the device. Parameters :******************************************************************************/static TestResult_t Open(int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_InitParams_t InitParams; STAVFS_OpenParams_t OpenParams; STAVFS_TermParams_t TermParams; ST_DeviceName_t DevNames[CNT_PARTITIONS] = {"Dev1", "Dev2", "Dev3", "Dev4"}; STAVFS_Handle_t Handles[CNT_PARTITIONS]; int PartitionIdx = 0; /* index of next partition to set up */ BOOL NextInited = FALSE; /* Set to TRUE when an Open fails; indicating that Term(DevNames[PartitionIdx]) should be called. Otherwise, attempt to Close and Term-inate from 0 to PartitionIdx, exclusive */ strcpy(InitParams.EVTName, EVT_DEVICE_NAME); strcpy(InitParams.ATAPIName, ATAPI_DEVICE_NAME); InitParams.Flags = 0; InitParams.MemoryPartition = system_partition; InitParams.Protocol = 1; InitParams.UnitNumber = 0; InitParams.PartitionNumber = 0; OpenParams.Flags = 0; TermParams.Flags = 0; if (ST_ERROR_UNKNOWN_DEVICE != STAVFS_Open (DevNames[0], &OpenParams, &Handles[0])) { STTBX_Print(("Opening an uninitialised device does not give the correct error\n")); Result = TEST_FAILED; } else if (ST_NO_ERROR != STAVFS_Init (DevNames[0], &InitParams)) { STTBX_Print(("First initialisation failed\n")); Result = TEST_FAILED; } else { if (ST_NO_ERROR != STAVFS_Open (DevNames[0], &OpenParams, &Handles[0])) { STTBX_Print(("Opening a volume on the device failed\n")); NextInited = TRUE; /* Term but don't try to Close Dev1 */ Result = TEST_FAILED; } else if (ST_NO_ERROR == STAVFS_Open (DevNames[0], &OpenParams, &Handles[1])) { STTBX_Print(("Succeeded opening a partition that is already open\n")); ++PartitionIdx; /* still try to Close the first handle (as well as Term-inate Dev1) */ Result = TEST_FAILED; } else { for (PartitionIdx = 1; PartitionIdx < CNT_PARTITIONS; ++PartitionIdx) { InitParams.PartitionNumber = PartitionIdx; if (ST_NO_ERROR != STAVFS_Init (DevNames[PartitionIdx], &InitParams)) { STTBX_Print(("Failed initialising partition %i\n", PartitionIdx)); Result = TEST_FAILED; break; } if (ST_NO_ERROR != STAVFS_Open (DevNames[PartitionIdx], &OpenParams, &Handles[PartitionIdx])) { STTBX_Print(("Failed opening partition %i\n", PartitionIdx)); NextInited = TRUE; /* Term but don't try to Close this device */ Result = TEST_FAILED; break; } } } /* PartitionIdx is now one above the index of the last partition sucessfully init-ed and opened (0 if we failed to open partition 0). We now try to close and terminate each in turn. But first, did we Init (but not open) the next one? */ if (NextInited == TRUE && PartitionIdx < CNT_PARTITIONS) { if (ST_NO_ERROR != STAVFS_Term (DevNames[PartitionIdx], &TermParams)) { STTBX_Print(("Failed terminating partition %i\n", PartitionIdx)); Result = TEST_FAILED; } } while (--PartitionIdx >= 0) { /* close and terminate partition with index PartitionIdx */ if (ST_NO_ERROR != STAVFS_Close (Handles[PartitionIdx])) { STTBX_Print(("Failed closing partition %i\n", PartitionIdx)); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_Term (DevNames[PartitionIdx], &TermParams)) { STTBX_Print(("Failed terminating partition %i\n", PartitionIdx)); Result = TEST_FAILED; } } } return (Result);}/******************************************************************************Function Name : DiskSpace Description : Test the 'get disk space available' function. Parameters :******************************************************************************/static TestResult_t DiskSpace (int TestNo, TestVariant_t Variant){ TestResult_t Result = TEST_PASSED; STAVFS_InitParams_t InitParams; STAVFS_OpenParams_t OpenParams; STAVFS_Handle_t Handle; STAVFS_TermParams_t TermParams; TermParams.Flags = 0; strcpy (InitParams.EVTName, EVT_DEVICE_NAME); strcpy (InitParams.ATAPIName, ATAPI_DEVICE_NAME); InitParams.Flags = 0; InitParams.MemoryPartition = system_partition; InitParams.Protocol = 1; InitParams.UnitNumber = 0; InitParams.PartitionNumber = 0; OpenParams.Flags = 0; TermParams.Flags = 0; if (ST_NO_ERROR != STAVFS_Init ("Dev1", &InitParams)) { STTBX_Print (("First initialisation failed\n")); Result = TEST_FAILED; } else { if (ST_NO_ERROR != STAVFS_Open ("Dev1", &OpenParams, &Handle)) { STTBX_Print (("Opening a volume on the device failed\n")); Result = TEST_FAILED; } else { char FNames[CNT_FILES][] = {"fred0", "fred1", "fred2"}; STAVFS_FileHandle_t FHandles[CNT_FILES]; U64 FileSize; U64 Size1; U64 Size2; U64 Diff; U16 ClusterSize; int i; if (ST_NO_ERROR != STAVFS_GetFreeDiskSpace (Handle, &Size1)) { STTBX_Print (("Error getting free space (1)\n")); Result = TEST_FAILED; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -