From 3f41af76c53c1f208d34d07bc0e3a0922f6e1dd9 Mon Sep 17 00:00:00 2001 From: jdfiguer Date: Wed, 31 Jan 2024 15:11:34 -0500 Subject: [PATCH] Fix #95, Replaces conditionally compiled code with runtime conditional logic --- fsw/inc/ds_extern_typedefs.h | 2 - fsw/src/ds_app.c | 1 + fsw/src/ds_app.h | 2 + fsw/src/ds_file.c | 237 +++++++++++++++++------------------ fsw/src/ds_file.h | 2 - fsw/src/ds_table.c | 16 +-- fsw/tables/ds_file_tbl.c | 32 ----- unit-test/ds_cmds_tests.c | 5 +- unit-test/ds_file_tests.c | 152 +++++++++++----------- unit-test/ds_table_tests.c | 9 +- 10 files changed, 208 insertions(+), 250 deletions(-) diff --git a/fsw/inc/ds_extern_typedefs.h b/fsw/inc/ds_extern_typedefs.h index ab5ddcb..d3b1fee 100644 --- a/fsw/inc/ds_extern_typedefs.h +++ b/fsw/inc/ds_extern_typedefs.h @@ -84,9 +84,7 @@ typedef struct */ typedef struct { -#if (DS_MOVE_FILES == true) char Movename[DS_PATHNAME_BUFSIZE]; /**< \brief Move files to this dir after close */ -#endif char Pathname[DS_PATHNAME_BUFSIZE]; /**< \brief Path portion of filename */ char Basename[DS_BASENAME_BUFSIZE]; /**< \brief Base portion of filename */ char Extension[DS_EXTENSION_BUFSIZE]; /**< \brief Extension portion of filename */ diff --git a/fsw/src/ds_app.c b/fsw/src/ds_app.c index 9f7b74b..9bd982b 100644 --- a/fsw/src/ds_app.c +++ b/fsw/src/ds_app.c @@ -184,6 +184,7 @@ CFE_Status_t DS_AppInitialize(void) memset(&DS_AppData, 0, sizeof(DS_AppData)); DS_AppData.AppEnableState = DS_DEF_ENABLE_STATE; + DS_AppData.EnableMoveFiles = DS_MOVE_FILES; /* ** Mark files as closed diff --git a/fsw/src/ds_app.h b/fsw/src/ds_app.h index 94c731a..488e189 100644 --- a/fsw/src/ds_app.h +++ b/fsw/src/ds_app.h @@ -104,6 +104,8 @@ typedef struct DS_HashLink_t HashLinks[DS_PACKETS_IN_FILTER_TABLE]; /**< \brief Hash table linked list elements */ DS_HashLink_t *HashTable[DS_HASH_TABLE_ENTRIES]; /**< \brief Each hash table entry is a linked list */ + + uint8 EnableMoveFiles; /**< \brief Whether to move files to downlink directory after close */ } DS_AppData_t; /** \brief DS global data structure reference */ diff --git a/fsw/src/ds_file.c b/fsw/src/ds_file.c index f6fd76b..fb9f31c 100644 --- a/fsw/src/ds_file.c +++ b/fsw/src/ds_file.c @@ -376,73 +376,73 @@ void DS_FileWriteData(int32 FileIndex, const void *FileData, uint32 DataLength) void DS_FileWriteHeader(int32 FileIndex) { -#if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) - - DS_DestFileEntry_t *DestFile = &DS_AppData.DestFileTblPtr->File[FileIndex]; - DS_AppFileStatus_t *FileStatus = &DS_AppData.FileStatus[FileIndex]; - CFE_FS_Header_t CFE_FS_Header; - DS_FileHeader_t DS_FileHeader; - int32 Result; - - /* - ** Initialize selected parts of the cFE file header... - */ - CFE_FS_InitHeader(&CFE_FS_Header, DS_FILE_HDR_DESCRIPTION, DS_FILE_HDR_SUBTYPE); - - /* - ** Let cFE finish the init and write the primary header... - */ - Result = CFE_FS_WriteHeader(FileStatus->FileHandle, &CFE_FS_Header); - - if (Result == sizeof(CFE_FS_Header_t)) + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) { - /* - ** Success - update file size and data rate counters... - */ - DS_AppData.FileWriteCounter++; - - FileStatus->FileSize += sizeof(CFE_FS_Header_t); - FileStatus->FileGrowth += sizeof(CFE_FS_Header_t); + DS_DestFileEntry_t *DestFile = &DS_AppData.DestFileTblPtr->File[FileIndex]; + DS_AppFileStatus_t *FileStatus = &DS_AppData.FileStatus[FileIndex]; + CFE_FS_Header_t CFE_FS_Header; + DS_FileHeader_t DS_FileHeader; + int32 Result; /* - ** Initialize the DS file header... + ** Initialize selected parts of the cFE file header... */ - memset(&DS_FileHeader, 0, sizeof(DS_FileHeader)); - DS_FileHeader.FileTableIndex = FileIndex; - DS_FileHeader.FileNameType = DestFile->FileNameType; - strncpy(DS_FileHeader.FileName, FileStatus->FileName, sizeof(DS_FileHeader.FileName)); + CFE_FS_InitHeader(&CFE_FS_Header, DS_FILE_HDR_DESCRIPTION, DS_FILE_HDR_SUBTYPE); /* - ** Manually write the secondary header... + ** Let cFE finish the init and write the primary header... */ - Result = OS_write(FileStatus->FileHandle, &DS_FileHeader, sizeof(DS_FileHeader_t)); + Result = CFE_FS_WriteHeader(FileStatus->FileHandle, &CFE_FS_Header); - if (Result == sizeof(DS_FileHeader_t)) + if (Result == sizeof(CFE_FS_Header_t)) { /* ** Success - update file size and data rate counters... */ DS_AppData.FileWriteCounter++; - FileStatus->FileSize += sizeof(DS_FileHeader_t); - FileStatus->FileGrowth += sizeof(DS_FileHeader_t); + FileStatus->FileSize += sizeof(CFE_FS_Header_t); + FileStatus->FileGrowth += sizeof(CFE_FS_Header_t); + + /* + ** Initialize the DS file header... + */ + memset(&DS_FileHeader, 0, sizeof(DS_FileHeader)); + DS_FileHeader.FileTableIndex = FileIndex; + DS_FileHeader.FileNameType = DestFile->FileNameType; + strncpy(DS_FileHeader.FileName, FileStatus->FileName, sizeof(DS_FileHeader.FileName)); + + /* + ** Manually write the secondary header... + */ + Result = OS_write(FileStatus->FileHandle, &DS_FileHeader, sizeof(DS_FileHeader_t)); + + if (Result == sizeof(DS_FileHeader_t)) + { + /* + ** Success - update file size and data rate counters... + */ + DS_AppData.FileWriteCounter++; + + FileStatus->FileSize += sizeof(DS_FileHeader_t); + FileStatus->FileGrowth += sizeof(DS_FileHeader_t); + } + else + { + /* + ** Error - send event, close file and disable destination... + */ + DS_FileWriteError(FileIndex, sizeof(DS_FileHeader_t), Result); + } } else { /* ** Error - send event, close file and disable destination... */ - DS_FileWriteError(FileIndex, sizeof(DS_FileHeader_t), Result); + DS_FileWriteError(FileIndex, sizeof(CFE_FS_Header_t), Result); } } - else - { - /* - ** Error - send event, close file and disable destination... - */ - DS_FileWriteError(FileIndex, sizeof(CFE_FS_Header_t), Result); - } -#endif } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -761,35 +761,36 @@ void DS_FileCreateSequence(char *Buffer, uint32 Type, uint32 Count) void DS_FileUpdateHeader(int32 FileIndex) { -#if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) - /* - ** Update CFE specific header fields... - */ - DS_AppFileStatus_t *FileStatus = &DS_AppData.FileStatus[FileIndex]; - CFE_TIME_SysTime_t CurrentTime = CFE_TIME_GetTime(); - int32 Result; - - Result = OS_lseek(FileStatus->FileHandle, sizeof(CFE_FS_Header_t), OS_SEEK_SET); - - if (Result == sizeof(CFE_FS_Header_t)) + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) { - /* update file close time */ - Result = OS_write(FileStatus->FileHandle, &CurrentTime, sizeof(CFE_TIME_SysTime_t)); + /* + ** Update CFE specific header fields... + */ + DS_AppFileStatus_t *FileStatus = &DS_AppData.FileStatus[FileIndex]; + CFE_TIME_SysTime_t CurrentTime = CFE_TIME_GetTime(); + int32 Result; - if (Result == sizeof(CFE_TIME_SysTime_t)) + Result = OS_lseek(FileStatus->FileHandle, sizeof(CFE_FS_Header_t), OS_SEEK_SET); + + if (Result == sizeof(CFE_FS_Header_t)) { - DS_AppData.FileUpdateCounter++; + /* update file close time */ + Result = OS_write(FileStatus->FileHandle, &CurrentTime, sizeof(CFE_TIME_SysTime_t)); + + if (Result == sizeof(CFE_TIME_SysTime_t)) + { + DS_AppData.FileUpdateCounter++; + } + else + { + DS_AppData.FileUpdateErrCounter++; + } } else { DS_AppData.FileUpdateErrCounter++; } } - else - { - DS_AppData.FileUpdateErrCounter++; - } -#endif } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -800,68 +801,74 @@ void DS_FileUpdateHeader(int32 FileIndex) void DS_FileCloseDest(int32 FileIndex) { DS_AppFileStatus_t *FileStatus = &DS_AppData.FileStatus[FileIndex]; - -#if (DS_MOVE_FILES == true) - /* - ** Move file from working directory to downlink directory... - */ - int32 OS_result; - int32 PathLength; - char *FileName; - char PathName[DS_TOTAL_FNAME_BUFSIZE]; + int32 OS_result; + int32 PathLength; + char * FileName; + char PathName[DS_TOTAL_FNAME_BUFSIZE]; /* ** First, close the file... */ OS_close(FileStatus->FileHandle); - /* - ** Move file only if table has a downlink directory name... - */ - if (DS_AppData.DestFileTblPtr->File[FileIndex].Movename[0] != '\0') + if (DS_AppData.EnableMoveFiles == DS_ENABLED) { /* - ** Make sure directory name does not end with slash character... + ** Move file only if table has a downlink directory name... */ - CFE_SB_MessageStringGet(PathName, DS_AppData.DestFileTblPtr->File[FileIndex].Movename, NULL, sizeof(PathName), - sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); - PathLength = strlen(PathName); - if (PathName[PathLength - 1] == '/') + if (DS_AppData.DestFileTblPtr->File[FileIndex].Movename[0] != '\0') { - PathName[PathLength - 1] = '\0'; - PathLength--; - } - - /* - ** Get a pointer to slash character before the filename... - */ - FileName = strrchr(FileStatus->FileName, '/'); + /* + ** Make sure directory name does not end with slash character... + */ + CFE_SB_MessageStringGet(PathName, DS_AppData.DestFileTblPtr->File[FileIndex].Movename, NULL, + sizeof(PathName), sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); + PathLength = strlen(PathName); + if (PathName[PathLength - 1] == '/') + { + PathName[PathLength - 1] = '\0'; + PathLength--; + } - if (FileName != NULL) - { /* - ** Verify that directory name plus filename is not too large... + ** Get a pointer to slash character before the filename... */ - if ((PathLength + strlen(FileName)) < DS_TOTAL_FNAME_BUFSIZE) + FileName = strrchr(FileStatus->FileName, '/'); + + if (FileName != NULL) { /* - ** Append the filename (with slash) to the directory name... + ** Verify that directory name plus filename is not too large... */ - strcat(PathName, FileName); + if ((PathLength + strlen(FileName)) < DS_TOTAL_FNAME_BUFSIZE) + { + /* + ** Append the filename (with slash) to the directory name... + */ + strcat(PathName, FileName); - /* - ** Use OS function to move/rename the file... - */ - OS_result = OS_mv(FileStatus->FileName, PathName); + /* + ** Use OS function to move/rename the file... + */ + OS_result = OS_mv(FileStatus->FileName, PathName); - if (OS_result != OS_SUCCESS) + if (OS_result != OS_SUCCESS) + { + /* + ** Error - send event but leave destination enabled... + */ + CFE_EVS_SendEvent(DS_MOVE_FILE_ERR_EID, CFE_EVS_EventType_ERROR, + "FILE MOVE error: src = '%s', tgt = '%s', result = %d", FileStatus->FileName, + PathName, (int)OS_result); + } + } + else { /* ** Error - send event but leave destination enabled... */ CFE_EVS_SendEvent(DS_MOVE_FILE_ERR_EID, CFE_EVS_EventType_ERROR, - "FILE MOVE error: src = '%s', tgt = '%s', result = %d", FileStatus->FileName, - PathName, (int)OS_result); + "FILE MOVE error: dir name = '%s', filename = '%s'", PathName, FileName); } } else @@ -870,27 +877,13 @@ void DS_FileCloseDest(int32 FileIndex) ** Error - send event but leave destination enabled... */ CFE_EVS_SendEvent(DS_MOVE_FILE_ERR_EID, CFE_EVS_EventType_ERROR, - "FILE MOVE error: dir name = '%s', filename = '%s'", PathName, FileName); + "FILE MOVE error: dir name = '%s', filename = 'NULL'", PathName); } - } - else - { - /* - ** Error - send event but leave destination enabled... - */ - CFE_EVS_SendEvent(DS_MOVE_FILE_ERR_EID, CFE_EVS_EventType_ERROR, - "FILE MOVE error: dir name = '%s', filename = 'NULL'", PathName); - } - /* Update the path name for reporting */ - strncpy(FileStatus->FileName, PathName, sizeof(FileStatus->FileName)); + /* Update the path name for reporting */ + strncpy(FileStatus->FileName, PathName, sizeof(FileStatus->FileName)); + } } -#else - /* - ** Close the file... - */ - OS_close(FileStatus->FileHandle); -#endif /* ** Transmit file information telemetry... diff --git a/fsw/src/ds_file.h b/fsw/src/ds_file.h index c26c9c5..c92e102 100644 --- a/fsw/src/ds_file.h +++ b/fsw/src/ds_file.h @@ -35,7 +35,6 @@ /* */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) /** * \brief DS File Header (follows cFE file header at start of file) */ @@ -49,7 +48,6 @@ typedef struct char FileName[DS_TOTAL_FNAME_BUFSIZE]; /**< \brief On-board filename */ } DS_FileHeader_t; -#endif /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ diff --git a/fsw/src/ds_table.c b/fsw/src/ds_table.c index 2e9d3db..9643e9b 100644 --- a/fsw/src/ds_table.c +++ b/fsw/src/ds_table.c @@ -52,9 +52,10 @@ CFE_Status_t DS_TableInit(void) bool NeedToLoadFilterTable = false; uint16 TableRegisterFlags = CFE_TBL_OPT_SNGL_BUFFER | CFE_TBL_OPT_LOAD_DUMP; -#if (DS_MAKE_TABLES_CRITICAL == 1) - TableRegisterFlags |= CFE_TBL_OPT_CRITICAL; -#endif + if (DS_MAKE_TABLES_CRITICAL == 1) + { + TableRegisterFlags |= CFE_TBL_OPT_CRITICAL; + } /* ** If registration fails for either table then the DS app will @@ -909,10 +910,11 @@ CFE_Status_t DS_TableCreateCDS(void) DS_AppData.FileStatus[i].FileCount = DataStoreBuffer[i]; } -#if (DS_CDS_ENABLE_STATE == 1) - /* Only restore enable/disable state if configured */ - DS_AppData.AppEnableState = (uint8)DataStoreBuffer[DS_DEST_FILE_CNT]; -#endif + if (DS_CDS_ENABLE_STATE == 1) + { + /* Only restore enable/disable state if configured */ + DS_AppData.AppEnableState = (uint8)DataStoreBuffer[DS_DEST_FILE_CNT]; + } } } diff --git a/fsw/tables/ds_file_tbl.c b/fsw/tables/ds_file_tbl.c index 78b990f..e841609 100644 --- a/fsw/tables/ds_file_tbl.c +++ b/fsw/tables/ds_file_tbl.c @@ -74,9 +74,7 @@ DS_DestFileTable_t DS_DestFileTable = { { /* File Index 00 -- event packets only */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ "set_by_cmd_b4_enable", /* .Basename = */ "events", /* .Extension = */ ".dat", @@ -89,9 +87,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 01 -- application housekeeping packets */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ "set_by_cmd_b4_enable", /* .Basename = */ "app", /* .Extension = */ ".hk", @@ -104,9 +100,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 02 -- application telemetry packets */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ "set_by_cmd_b4_enable", /* .Basename = */ "app", /* .Extension = */ ".tlm", @@ -119,9 +113,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 03 -- hardware telemetry packets */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ "set_by_cmd_b4_enable", /* .Basename = */ "hw", /* .Extension = */ "tlm", @@ -134,9 +126,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 04 -- cFE housekeeping packets */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ "set_by_cmd_b4_enable", /* .Basename = */ "cfe", /* .Extension = */ "hk", @@ -149,9 +139,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 05 -- cFE telemetry packets */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ "set_by_cmd_b4_enable", /* .Basename = */ "cfe", /* .Extension = */ "tlm", @@ -164,9 +152,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 06 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -179,9 +165,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 07 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -194,9 +178,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 08 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -209,9 +191,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 09 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -224,9 +204,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 10 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -239,9 +217,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 11 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -254,9 +230,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 12 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -269,9 +243,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 13 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -284,9 +256,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 14 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, @@ -299,9 +269,7 @@ DS_DestFileTable_t DS_DestFileTable = { }, /* File Index 15 */ { -#if (DS_MOVE_FILES == true) /* .Movename = */ DS_EMPTY_STRING, -#endif /* .Pathname = */ DS_EMPTY_STRING, /* .Basename = */ DS_EMPTY_STRING, /* .Extension = */ DS_EMPTY_STRING, diff --git a/unit-test/ds_cmds_tests.c b/unit-test/ds_cmds_tests.c index 3b0fc9c..376a601 100644 --- a/unit-test/ds_cmds_tests.c +++ b/unit-test/ds_cmds_tests.c @@ -991,7 +991,7 @@ void DS_SetDestSizeCmd_Test_FileTableNotLoaded(void) UT_SetDefaultReturnValue(UT_KEY(DS_TableVerifySize), true); /* Execute the function being tested */ - UtAssert_VOIDCALL(DS_SetDestSizeCmd(&UT_CmdBuf.Buf)); + UtAssert_VOIDCALL(DS_SetDestSizeCmd(&UT_CmdBuf.Buf)); /* Verify results */ UtAssert_UINT32_EQ(DS_AppData.CmdRejectedCounter, 1); @@ -1270,15 +1270,14 @@ void DS_CloseFileCmd_Test_InvalidFileTableIndex(void) void DS_CloseAllCmd_Test_Nominal(void) { uint32 i; + DS_AppData.EnableMoveFiles = DS_ENABLED; for (i = 1; i < DS_DEST_FILE_CNT; i++) { DS_AppData.FileStatus[i].FileHandle = OS_OBJECT_ID_UNDEFINED; } -#if (DS_MOVE_FILES == true) strncpy(DS_AppData.DestFileTblPtr->File[0].Movename, "", DS_PATHNAME_BUFSIZE); -#endif /* Execute the function being tested */ UtAssert_VOIDCALL(DS_CloseAllCmd(&UT_CmdBuf.Buf)); diff --git a/unit-test/ds_file_tests.c b/unit-test/ds_file_tests.c index baece77..a350d06 100644 --- a/unit-test/ds_file_tests.c +++ b/unit-test/ds_file_tests.c @@ -269,10 +269,9 @@ void DS_FileSetupWrite_Test_FileHandleClosed(void) DS_AppData.FileStatus[FileIndex].FileCount = 0; DS_AppData.FileStatus[FileIndex].FileSize = 3; - /* Fail writing the header so the data won't write */ - UT_SetDefaultReturnValue(UT_KEY(CFE_FS_WriteHeader), -1); + /* Fail creating the destination file so the file handle remains closed*/ + UT_SetDefaultReturnValue(UT_KEY(OS_OpenCreate), OS_ERROR); - /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileSetupWrite(FileIndex, &UT_CmdBuf.Buf)); /* Verify results */ @@ -283,8 +282,9 @@ void DS_FileSetupWrite_Test_FileHandleClosed(void) void DS_FileSetupWrite_Test_MaxFileSizeExceeded(void) { - int32 FileIndex = 0; - size_t forced_Size = sizeof(DS_NoopCmd_t); + int32 FileIndex = 0; + size_t forced_Size = sizeof(DS_NoopCmd_t); + DS_AppData.EnableMoveFiles = DS_ENABLED; UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &forced_Size, sizeof(forced_Size), false); @@ -297,11 +297,8 @@ void DS_FileSetupWrite_Test_MaxFileSizeExceeded(void) UT_DS_SetDestFileEntry(&DS_AppData.DestFileTblPtr->File[FileIndex]); strncpy(DS_AppData.FileStatus[FileIndex].FileName, "directory1/", sizeof(DS_AppData.FileStatus[FileIndex].FileName)); - -#if (DS_MOVE_FILES == true) strncpy(DS_AppData.DestFileTblPtr->File[FileIndex].Movename, "directory2/movename/", sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); -#endif /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileSetupWrite(FileIndex, &UT_CmdBuf.Buf)); @@ -364,7 +361,6 @@ void DS_FileWriteData_Test_Error(void) UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, DS_WRITE_FILE_ERR_EID); } -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileWriteHeader_Test_PlatformConfigCFE_Nominal(void) { int32 FileIndex = 0; @@ -381,9 +377,7 @@ void DS_FileWriteHeader_Test_PlatformConfigCFE_Nominal(void) UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileSize, sizeof(CFE_FS_Header_t) + sizeof(DS_FileHeader_t)); UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileGrowth, sizeof(CFE_FS_Header_t) + sizeof(DS_FileHeader_t)); } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileWriteHeader_Test_PrimaryHeaderError(void) { int32 FileIndex = 0; @@ -403,9 +397,7 @@ void DS_FileWriteHeader_Test_PrimaryHeaderError(void) UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1); /* Generates 1 event message we don't care about in this test */ } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileWriteHeader_Test_SecondaryHeaderError(void) { int32 FileIndex = 0; @@ -427,9 +419,7 @@ void DS_FileWriteHeader_Test_SecondaryHeaderError(void) UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1); /* Generates 1 event message we don't care about in this test */ } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileWriteError_Test(void) { int32 FileIndex = 0; @@ -454,7 +444,6 @@ void DS_FileWriteError_Test(void) UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR); UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, DS_WRITE_FILE_ERR_EID); } -#endif void DS_FileCreateDest_Test_Nominal(void) { @@ -475,11 +464,14 @@ void DS_FileCreateDest_Test_Nominal(void) UtAssert_VOIDCALL(DS_FileCreateDest(FileIndex)); /* Verify results */ -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE - UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 3); -#else - UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 1); -#endif + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) + { + UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 3); + } + else + { + UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 1); + } /* the file handle should have been reset and should not be closed */ UtAssert_BOOL_FALSE(OS_ObjectIdEqual(DS_AppData.FileStatus[FileIndex].FileHandle, DS_UT_OBJID_1)); @@ -510,7 +502,7 @@ void DS_FileCreateDest_Test_NominalRollover(void) strncpy(DS_AppData.FileStatus[FileIndex].FileName, "filename", sizeof(DS_AppData.FileStatus[FileIndex].FileName)); DS_AppData.FileStatus[FileIndex].FileCount = DS_MAX_SEQUENCE_COUNT; - DS_AppData.FileStatus[FileIndex].FileHandle = DS_UT_OBJID_1; + DS_AppData.FileStatus[FileIndex].FileHandle = OS_OBJECT_ID_UNDEFINED; /* Set to fail the condition "if (Result < 0)" */ UT_SetDefaultReturnValue(UT_KEY(OS_OpenCreate), OS_SUCCESS); @@ -521,11 +513,14 @@ void DS_FileCreateDest_Test_NominalRollover(void) UtAssert_VOIDCALL(DS_FileCreateDest(FileIndex)); /* Verify results */ -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE - UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 3); -#else - UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 1); -#endif + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) + { + UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 3); + } + else + { + UtAssert_INT32_EQ(DS_AppData.FileWriteCounter, 1); + } /* the file handle should have been reset and should not be closed */ UtAssert_BOOL_FALSE(OS_ObjectIdEqual(DS_AppData.FileStatus[FileIndex].FileHandle, DS_UT_OBJID_1)); @@ -577,7 +572,10 @@ void DS_FileCreateDest_Test_ClosedFileHandle(void) DS_AppData.DestFileTblPtr->File[FileIndex].Movename[0] = '\0'; /* Set to fail header write, which will call OS_close and clear the handle */ - UT_SetDefaultReturnValue(UT_KEY(CFE_FS_WriteHeader), -1); + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) + { + UT_SetDefaultReturnValue(UT_KEY(CFE_FS_WriteHeader), -1); + } /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileCreateDest(FileIndex)); @@ -782,7 +780,6 @@ void DS_FileCreateName_Test_ExtensionZero(void) UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileCreateSequence_Test_ByCount(void) { const uint32 Count = 1; @@ -802,9 +799,7 @@ void DS_FileCreateSequence_Test_ByCount(void) UtAssert_STRINGBUF_EQ(Sequence, sizeof(Sequence), StrCompare, sizeof(StrCompare)); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileCreateSequence_Test_ByTime(void) { int32 FileIndex = 0; @@ -831,9 +826,7 @@ void DS_FileCreateSequence_Test_ByTime(void) UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileCreateSequence_Test_BadFilenameType(void) { int32 FileIndex = 0; @@ -853,9 +846,7 @@ void DS_FileCreateSequence_Test_BadFilenameType(void) UtAssert_UINT32_EQ(strncmp(Sequence, "", DS_TOTAL_FNAME_BUFSIZE), 0); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileUpdateHeader_Test_PlatformConfigCFE_Nominal(void) { int32 FileIndex = 0; @@ -867,9 +858,7 @@ void DS_FileUpdateHeader_Test_PlatformConfigCFE_Nominal(void) UtAssert_UINT32_EQ(DS_AppData.FileUpdateCounter, 1); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileUpdateHeader_Test_WriteError(void) { int32 FileIndex = 0; @@ -884,9 +873,7 @@ void DS_FileUpdateHeader_Test_WriteError(void) UtAssert_UINT32_EQ(DS_AppData.FileUpdateErrCounter, 1); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE void DS_FileUpdateHeader_Test_PlatformConfigCFE_SeekError(void) { int32 FileIndex = 0; @@ -901,9 +888,7 @@ void DS_FileUpdateHeader_Test_PlatformConfigCFE_SeekError(void) UtAssert_UINT32_EQ(DS_AppData.FileUpdateErrCounter, 1); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if (DS_MOVE_FILES == true) void DS_FileCloseDest_Test_PlatformConfigMoveFiles_Nominal(void) { int32 FileIndex = 0; @@ -915,6 +900,7 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_Nominal(void) sizeof(DS_AppData.FileStatus[FileIndex].FileName)); strncpy(DS_AppData.DestFileTblPtr->File[FileIndex].Movename, "directory2/movename/", sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); + DS_AppData.EnableMoveFiles = DS_ENABLED; /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileCloseDest(FileIndex)); @@ -926,9 +912,7 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_Nominal(void) UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileName[0], 0); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif -#if (DS_MOVE_FILES == true) void DS_FileCloseDest_Test_PlatformConfigMoveFiles_MoveError(void) { int32 FileIndex = 0; @@ -940,6 +924,7 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_MoveError(void) sizeof(DS_AppData.FileStatus[FileIndex].FileName)); strncpy(DS_AppData.DestFileTblPtr->File[FileIndex].Movename, "directory2/movename/", sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); + DS_AppData.EnableMoveFiles = DS_ENABLED; /* Set to generate error message DS_MOVE_FILE_ERR_EID */ UT_SetDefaultReturnValue(UT_KEY(OS_mv), -1); @@ -954,23 +939,22 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_MoveError(void) UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1); } -#endif -#if (DS_MOVE_FILES == true) void DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameTooLarge(void) { - int32 FileIndex = 0; + int32 FileIndex = 0; const char DirName[] = "directory1/"; - + /* Set up the handle */ OS_OpenCreate(&DS_AppData.FileStatus[FileIndex].FileHandle, NULL, 0, 0); - + size_t DirNameLen = sizeof(DirName); strncpy(DS_AppData.FileStatus[FileIndex].FileName, DirName, DirNameLen); - memset(&DS_AppData.FileStatus[FileIndex].FileName[DirNameLen-1], 'f', DS_TOTAL_FNAME_BUFSIZE - DirNameLen); - DS_AppData.FileStatus[FileIndex].FileName[DS_TOTAL_FNAME_BUFSIZE-1] = '\0'; + memset(&DS_AppData.FileStatus[FileIndex].FileName[DirNameLen - 1], 'f', DS_TOTAL_FNAME_BUFSIZE - DirNameLen); + DS_AppData.FileStatus[FileIndex].FileName[DS_TOTAL_FNAME_BUFSIZE - 1] = '\0'; strncpy(DS_AppData.DestFileTblPtr->File[FileIndex].Movename, "directory2/movename/", sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); + DS_AppData.EnableMoveFiles = DS_ENABLED; /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileCloseDest(FileIndex)); @@ -982,9 +966,7 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameTooLarge(void) UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1); } -#endif -#if (DS_MOVE_FILES == true) void DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameNull(void) { int32 FileIndex = 0; @@ -994,6 +976,7 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameNull(void) strncpy(DS_AppData.DestFileTblPtr->File[FileIndex].Movename, "directory2/movename", sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); + DS_AppData.EnableMoveFiles = DS_ENABLED; /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileCloseDest(FileIndex)); @@ -1007,27 +990,43 @@ void DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameNull(void) UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1); } -#endif -#if (DS_MOVE_FILES == false) -void DS_FileCloseDest_Test_MoveFilesFalse(void) +void DS_FileCloseDest_Test_PlatformConfigMoveFiles_MovenameNull(void) { int32 FileIndex = 0; /* Set up the handle */ OS_OpenCreate(&DS_AppData.FileStatus[FileIndex].FileHandle, NULL, 0, 0); + strncpy(DS_AppData.DestFileTblPtr->File[FileIndex].Movename, "", + sizeof(DS_AppData.DestFileTblPtr->File[FileIndex].Movename)); + DS_AppData.EnableMoveFiles = DS_ENABLED; + /* Execute the function being tested */ UtAssert_VOIDCALL(DS_FileCloseDest(FileIndex)); /* Verify results */ - UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileHandle, OS_OBJECT_ID_UNDEFINED); + UtAssert_STUB_COUNT(CFE_SB_MessageStringGet, 0); +} + +void DS_FileCloseDest_Test_PlatformConfigMoveFiles_DisableMoveFiles(void) +{ + int32 FileIndex = 0; + + /* Set up the handle */ + OS_OpenCreate(&DS_AppData.FileStatus[FileIndex].FileHandle, NULL, 0, 0); + DS_AppData.EnableMoveFiles = DS_DISABLED; + + /* Execute the function being tested */ + UtAssert_VOIDCALL(DS_FileCloseDest(FileIndex)); + + /* Verify results */ + UtAssert_BOOL_FALSE(OS_ObjectIdDefined(DS_AppData.FileStatus[FileIndex].FileHandle)); UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileAge, 0); UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileSize, 0); UtAssert_UINT32_EQ(DS_AppData.FileStatus[FileIndex].FileName[0], 0); UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); } -#endif void DS_FileTestAge_Test_Nominal(void) { @@ -1283,24 +1282,24 @@ void UtTest_Setup(void) UT_DS_TEST_ADD(DS_FileWriteData_Test_Nominal); UT_DS_TEST_ADD(DS_FileWriteData_Test_Error); -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE - UT_DS_TEST_ADD(DS_FileWriteHeader_Test_PlatformConfigCFE_Nominal); - UT_DS_TEST_ADD(DS_FileWriteHeader_Test_PrimaryHeaderError); - UT_DS_TEST_ADD(DS_FileWriteHeader_Test_SecondaryHeaderError); -#endif + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) + { + UT_DS_TEST_ADD(DS_FileWriteHeader_Test_PlatformConfigCFE_Nominal); + UT_DS_TEST_ADD(DS_FileWriteHeader_Test_PrimaryHeaderError); + UT_DS_TEST_ADD(DS_FileWriteHeader_Test_SecondaryHeaderError); + } -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE - UT_DS_TEST_ADD(DS_FileWriteError_Test); -#endif + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) + { + UT_DS_TEST_ADD(DS_FileWriteError_Test); + } UT_DS_TEST_ADD(DS_FileCreateDest_Test_Nominal); UT_DS_TEST_ADD(DS_FileCreateDest_Test_StringTerminate); UT_DS_TEST_ADD(DS_FileCreateDest_Test_NominalRollover); UT_DS_TEST_ADD(DS_FileCreateDest_Test_Error); -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE UT_DS_TEST_ADD(DS_FileCreateDest_Test_ClosedFileHandle); -#endif UT_DS_TEST_ADD(DS_FileCreateName_Test_Nominal); UT_DS_TEST_ADD(DS_FileCreateName_Test_NominalWithSeparator); @@ -1311,26 +1310,23 @@ void UtTest_Setup(void) UT_DS_TEST_ADD(DS_FileCreateName_Test_PathBaseSeqExtTooLarge); UT_DS_TEST_ADD(DS_FileCreateName_Test_ExtensionZero); -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE UT_DS_TEST_ADD(DS_FileCreateSequence_Test_ByCount); UT_DS_TEST_ADD(DS_FileCreateSequence_Test_ByTime); UT_DS_TEST_ADD(DS_FileCreateSequence_Test_BadFilenameType); -#endif -#if DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE - UT_DS_TEST_ADD(DS_FileUpdateHeader_Test_PlatformConfigCFE_Nominal); - UT_DS_TEST_ADD(DS_FileUpdateHeader_Test_WriteError); - UT_DS_TEST_ADD(DS_FileUpdateHeader_Test_PlatformConfigCFE_SeekError); -#endif + if (DS_FILE_HEADER_TYPE == DS_FILE_HEADER_CFE) + { + UT_DS_TEST_ADD(DS_FileUpdateHeader_Test_PlatformConfigCFE_Nominal); + UT_DS_TEST_ADD(DS_FileUpdateHeader_Test_WriteError); + UT_DS_TEST_ADD(DS_FileUpdateHeader_Test_PlatformConfigCFE_SeekError); + } -#if (DS_MOVE_FILES == true) UT_DS_TEST_ADD(DS_FileCloseDest_Test_PlatformConfigMoveFiles_Nominal); UT_DS_TEST_ADD(DS_FileCloseDest_Test_PlatformConfigMoveFiles_MoveError); UT_DS_TEST_ADD(DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameTooLarge); UT_DS_TEST_ADD(DS_FileCloseDest_Test_PlatformConfigMoveFiles_FilenameNull); -#else - UT_DS_TEST_ADD(DS_FileCloseDest_Test_MoveFilesFalse); -#endif + UT_DS_TEST_ADD(DS_FileCloseDest_Test_PlatformConfigMoveFiles_MovenameNull); + UT_DS_TEST_ADD(DS_FileCloseDest_Test_PlatformConfigMoveFiles_DisableMoveFiles); UT_DS_TEST_ADD(DS_FileTestAge_Test_Nominal); UT_DS_TEST_ADD(DS_FileTestAge_Test_ExceedMaxAge); diff --git a/unit-test/ds_table_tests.c b/unit-test/ds_table_tests.c index 7b58da5..f0eee13 100644 --- a/unit-test/ds_table_tests.c +++ b/unit-test/ds_table_tests.c @@ -1394,10 +1394,11 @@ void DS_TableCreateCDS_Test_PreExistingCDSArea(void) UtAssert_UINT32_EQ(DS_AppData.FileStatus[DS_DEST_FILE_CNT / 2].FileCount, 0); UtAssert_UINT32_EQ(DS_AppData.FileStatus[DS_DEST_FILE_CNT - 1].FileCount, 0); -#if (DS_CDS_ENABLE_STATE == 1) - /* only test if configured */ - UtAssert_UINT32_EQ(DS_AppData.AppEnableState, 0); -#endif + if (DS_CDS_ENABLE_STATE == 1) + { + /* only test if configured */ + UtAssert_UINT32_EQ(DS_AppData.AppEnableState, 0); + } UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0); }