Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #80, Unit tests read past array bounds and general cleanup #98

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
823 changes: 104 additions & 719 deletions unit-test/sc_app_tests.c

Large diffs are not rendered by default.

1,053 changes: 98 additions & 955 deletions unit-test/sc_atsrq_tests.c

Large diffs are not rendered by default.

1,250 changes: 138 additions & 1,112 deletions unit-test/sc_cmds_tests.c

Large diffs are not rendered by default.

1,139 changes: 163 additions & 976 deletions unit-test/sc_loads_tests.c

Large diffs are not rendered by default.

1,165 changes: 129 additions & 1,036 deletions unit-test/sc_rtsrq_tests.c

Large diffs are not rendered by default.

574 changes: 44 additions & 530 deletions unit-test/sc_state_tests.c

Large diffs are not rendered by default.

105 changes: 15 additions & 90 deletions unit-test/sc_utils_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,206 +29,131 @@
#include "utstubs.h"

/* sc_utils_tests globals */
uint8 call_count_CFE_EVS_SendEvent;

void SC_GetCurrentTime_Test(void)
{
SC_AppData.CurrentTime = 0;

/* Execute the function being tested */
SC_GetCurrentTime();
UtAssert_VOIDCALL(SC_GetCurrentTime());

/* Verify results */
UtAssert_True(SC_AppData.CurrentTime != 0, "SC_AppData.CurrentTime != 0");
}

void SC_GetAtsEntryTime_Test(void)
{
SC_AbsTimeTag_t AbsTimeTag;

SC_AtsEntryHeader_t Entry;
Entry.TimeTag_MS = 0;
Entry.TimeTag_LS = 10;

/* Execute the function being tested */
AbsTimeTag = SC_GetAtsEntryTime(&Entry);

/* Verify results */
UtAssert_True(AbsTimeTag == 10, "AbsTimeTag == 10");
UtAssert_UINT32_EQ(SC_GetAtsEntryTime(&Entry), Entry.TimeTag_LS);
}

void SC_ComputeAbsTime_Test(void)
{
SC_AbsTimeTag_t AbsTimeTag;

SC_AppData.CurrentTime = 0;

/* Execute the function being tested */
AbsTimeTag = SC_ComputeAbsTime(0);

/* Verify results */

/* The CFE_TIME_Add stub increments when status >= 0 */
UtAssert_True(AbsTimeTag == 1, "AbsTimeTag == 1");
/* Execute the function being tested
* Note the CFE_TIME_Add stub increments when status >= 0 */
UtAssert_UINT32_EQ(SC_ComputeAbsTime(0), 1);
}

void SC_CompareAbsTime_Test_True(void)
{
bool Result;

SC_AbsTimeTag_t AbsTimeTag1 = {0};
SC_AbsTimeTag_t AbsTimeTag2 = {0};

UT_SetDeferredRetcode(UT_KEY(CFE_TIME_Compare), 1, CFE_TIME_A_GT_B);

/* Execute the function being tested */
Result = SC_CompareAbsTime(AbsTimeTag1, AbsTimeTag2);

/* Verify results */
UtAssert_True(Result == true, "Result == true");
UtAssert_BOOL_TRUE(SC_CompareAbsTime(AbsTimeTag1, AbsTimeTag2));
}

void SC_CompareAbsTime_Test_False(void)
{
bool Result;

SC_AbsTimeTag_t AbsTimeTag1 = {0};
SC_AbsTimeTag_t AbsTimeTag2 = {0};

UT_SetDeferredRetcode(UT_KEY(CFE_TIME_Compare), 1, -1);

/* Execute the function being tested */
Result = SC_CompareAbsTime(AbsTimeTag1, AbsTimeTag2);

/* Verify results */
UtAssert_True(Result == false, "Result == false");
UtAssert_BOOL_FALSE(SC_CompareAbsTime(AbsTimeTag1, AbsTimeTag2));
}

void SC_VerifyCmdLength_Test_Nominal(void)
{
SC_NoArgsCmd_t CmdPacket;
bool Result;
CFE_SB_MsgId_t TestMsgId = CFE_SB_ValueToMsgId(SC_CMD_MID);
CFE_MSG_FcnCode_t FcnCode = SC_NOOP_CC;
size_t MsgSize = sizeof(CmdPacket);

SC_InitTables();

UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(SC_AppendAtsCmd_t), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &MsgSize, sizeof(MsgSize), false);

/* Execute the function being tested */
Result = SC_VerifyCmdLength(&CmdPacket.CmdHeader.Msg, sizeof(CmdPacket));
UtAssert_BOOL_TRUE(SC_VerifyCmdLength(&CmdPacket.CmdHeader.Msg, sizeof(CmdPacket)));

/* Verify results */
UtAssert_True(Result == true, "Result == true");
UtAssert_True(SC_OperData.HkPacket.CmdErrCtr == 0, "SC_OperData.HkPacket.CmdErrCtr == 0");

call_count_CFE_EVS_SendEvent = UT_GetStubCount(UT_KEY(CFE_EVS_SendEvent));

UtAssert_True(call_count_CFE_EVS_SendEvent == 0, "CFE_EVS_SendEvent was called %u time(s), expected 0",
call_count_CFE_EVS_SendEvent);
UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0);
}

void SC_VerifyCmdLength_Test_LenError(void)
{
SC_NoArgsCmd_t CmdPacket;
bool Result;
CFE_SB_MsgId_t TestMsgId = CFE_SB_ValueToMsgId(SC_CMD_MID);
CFE_MSG_FcnCode_t FcnCode = SC_NOOP_CC;
size_t MsgSize = sizeof(CmdPacket);
int32 strCmpResult;
char ExpectedEventString[CFE_MISSION_EVS_MAX_MESSAGE_LENGTH];

snprintf(ExpectedEventString, CFE_MISSION_EVS_MAX_MESSAGE_LENGTH,
"Invalid msg length: ID = 0x%%08lX, CC = %%d, Len = %%d, Expected = %%d");

SC_InitTables();

UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(SC_AppendAtsCmd_t), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &MsgSize, sizeof(MsgSize), false);

/* Execute the function being tested */
Result = SC_VerifyCmdLength(&CmdPacket.CmdHeader.Msg, 999);
UtAssert_BOOL_FALSE(SC_VerifyCmdLength(&CmdPacket.CmdHeader.Msg, 999));

/* Verify results */
UtAssert_True(Result == false, "Result == false");
UtAssert_True(SC_OperData.HkPacket.CmdCtr == 0, "SC_OperData.HkPacket.CmdCtr == 0");
UtAssert_True(SC_OperData.HkPacket.CmdErrCtr == 1, "SC_OperData.HkPacket.CmdErrCtr == 1");

UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, SC_LEN_ERR_EID);
UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR);

strCmpResult = strncmp(ExpectedEventString, context_CFE_EVS_SendEvent[0].Spec, CFE_MISSION_EVS_MAX_MESSAGE_LENGTH);

UtAssert_True(strCmpResult == 0, "Event string matched expected result, '%s'", context_CFE_EVS_SendEvent[0].Spec);

call_count_CFE_EVS_SendEvent = UT_GetStubCount(UT_KEY(CFE_EVS_SendEvent));

UtAssert_True(call_count_CFE_EVS_SendEvent == 1, "CFE_EVS_SendEvent was called %u time(s), expected 1",
call_count_CFE_EVS_SendEvent);
UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1);
}

void SC_VerifyCmdLength_Test_LenErrorNotMID(void)
{
SC_NoArgsCmd_t CmdPacket;
bool Result;
CFE_SB_MsgId_t TestMsgId = CFE_SB_ValueToMsgId(SC_SEND_HK_MID);
CFE_MSG_FcnCode_t FcnCode = 0;
size_t MsgSize = sizeof(CmdPacket);
int32 strCmpResult;
char ExpectedEventString[CFE_MISSION_EVS_MAX_MESSAGE_LENGTH];

snprintf(ExpectedEventString, CFE_MISSION_EVS_MAX_MESSAGE_LENGTH,
"Invalid msg length: ID = 0x%%08lX, CC = %%d, Len = %%d, Expected = %%d");

SC_InitTables();

UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(SC_AppendAtsCmd_t), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &MsgSize, sizeof(MsgSize), false);

/* Execute the function being tested */
Result = SC_VerifyCmdLength(&CmdPacket.CmdHeader.Msg, 999);
UtAssert_BOOL_FALSE(SC_VerifyCmdLength(&CmdPacket.CmdHeader.Msg, 999));

/* Verify results */
UtAssert_True(Result == false, "Result == false");
UtAssert_True(SC_OperData.HkPacket.CmdCtr == 0, "SC_OperData.HkPacket.CmdCtr == 0");

UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, SC_LEN_ERR_EID);
UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR);

strCmpResult = strncmp(ExpectedEventString, context_CFE_EVS_SendEvent[0].Spec, CFE_MISSION_EVS_MAX_MESSAGE_LENGTH);

UtAssert_True(strCmpResult == 0, "Event string matched expected result, '%s'", context_CFE_EVS_SendEvent[0].Spec);

call_count_CFE_EVS_SendEvent = UT_GetStubCount(UT_KEY(CFE_EVS_SendEvent));

UtAssert_True(call_count_CFE_EVS_SendEvent == 1, "CFE_EVS_SendEvent was called %u time(s), expected 1",
call_count_CFE_EVS_SendEvent);
UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 1);
}

void SC_ToggleAtsIndex_Test(void)
{
uint16 Result;

SC_AtpControlBlock_t AtsCtrlBlck;

SC_OperData.AtsCtrlBlckAddr = &AtsCtrlBlck;

SC_OperData.AtsCtrlBlckAddr->AtsNumber = 1;

Result = SC_ToggleAtsIndex();

UtAssert_True(Result == 1, "Result == 1");
UtAssert_UINT16_EQ(SC_ToggleAtsIndex(), 1);

SC_OperData.AtsCtrlBlckAddr->AtsNumber = 2;

Result = SC_ToggleAtsIndex();

UtAssert_True(Result == 0, "Result == 0");
UtAssert_UINT16_EQ(SC_ToggleAtsIndex(), 0);
}

void UtTest_Setup(void)
Expand Down
49 changes: 46 additions & 3 deletions unit-test/utilities/sc_test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@
#define UT_MAX_SENDEVENT_DEPTH 4
CFE_EVS_SendEvent_context_t context_CFE_EVS_SendEvent[UT_MAX_SENDEVENT_DEPTH];
CFE_ES_WriteToSysLog_context_t context_CFE_ES_WriteToSysLog;
SC_RtsInfoEntry_t RtsInfoTbl[SC_NUMBER_OF_RTS];

UT_CmdBuf_t UT_CmdBuf;

/* Table buffers */
uint32 AtsTblAddr[SC_NUMBER_OF_ATS][SC_ATS_BUFF_SIZE32];
uint32 AppendTblAddr[SC_APPEND_BUFF_SIZE32];
uint32 RtsTblAddr[SC_NUMBER_OF_RTS][SC_RTS_BUFF_SIZE32];
SC_AtsInfoTable_t AtsInfoTblAddr[SC_NUMBER_OF_ATS];
SC_RtsInfoEntry_t RtsInfoTblAddr[SC_NUMBER_OF_RTS];
SC_RtpControlBlock_t RtsCtrlBlckAddr;
SC_AtpControlBlock_t AtsCtrlBlckAddr;
uint32 AtsCmdStatusTblAddr[SC_NUMBER_OF_ATS][SC_MAX_ATS_CMDS];

/*
* Function Definitions
*/
Expand Down Expand Up @@ -75,20 +84,54 @@ void UT_Handler_CFE_ES_WriteToSysLog(void *UserObj, UT_EntryKey_t FuncKey, const
context_CFE_ES_WriteToSysLog.Spec[CFE_MISSION_EVS_MAX_MESSAGE_LENGTH - 1] = '\0';
}

void SC_Test_SetTableAddrs(void)
{
uint8 i;

/* Set table addresses */
for (i = 0; i < SC_NUMBER_OF_ATS; i++)
{
SC_OperData.AtsTblAddr[i] = AtsTblAddr[i];
SC_OperData.AtsCmdStatusTblAddr[i] = AtsCmdStatusTblAddr[i];
}
for (i = 0; i < SC_NUMBER_OF_RTS; i++)
{
SC_OperData.RtsTblAddr[i] = RtsTblAddr[i];
}
SC_OperData.AppendTblAddr = AppendTblAddr;
SC_OperData.AtsInfoTblAddr = AtsInfoTblAddr;
SC_OperData.RtsInfoTblAddr = RtsInfoTblAddr;
SC_OperData.RtsCtrlBlckAddr = &RtsCtrlBlckAddr;
SC_OperData.AtsCtrlBlckAddr = &AtsCtrlBlckAddr;
}

void SC_Test_Setup(void)
{

/* initialize test environment to default state for every test */
UT_ResetState(0);

/* Clear app data */
memset(&SC_OperData, 0, sizeof(SC_OperData));
memset(&SC_AppData, 0, sizeof(SC_AppData));
memset(RtsInfoTbl, 0, sizeof(RtsInfoTbl));

/* Clear table buffers */
memset(&AtsTblAddr, 0, sizeof(AtsTblAddr));
memset(&AppendTblAddr, 0, sizeof(AppendTblAddr));
memset(&RtsTblAddr, 0, sizeof(RtsTblAddr));
memset(&AtsInfoTblAddr, 0, sizeof(AtsInfoTblAddr));
memset(&RtsInfoTblAddr, 0, sizeof(RtsInfoTblAddr));
memset(&RtsCtrlBlckAddr, 0, sizeof(RtsCtrlBlckAddr));
memset(&AtsCtrlBlckAddr, 0, sizeof(AtsCtrlBlckAddr));
memset(&AtsCmdStatusTblAddr, 0, sizeof(AtsCmdStatusTblAddr));

/* Clear unit test buffers */
memset(context_CFE_EVS_SendEvent, 0, sizeof(context_CFE_EVS_SendEvent));
memset(&context_CFE_ES_WriteToSysLog, 0, sizeof(context_CFE_ES_WriteToSysLog));
memset(&UT_CmdBuf, 0, sizeof(UT_CmdBuf));

SC_OperData.RtsInfoTblAddr = &RtsInfoTbl[0];
/* Set table addresses */
SC_Test_SetTableAddrs();

/* Register custom handlers */
UT_SetVaHandlerFunction(UT_KEY(CFE_EVS_SendEvent), UT_Handler_CFE_EVS_SendEvent, NULL);
Expand Down
6 changes: 3 additions & 3 deletions unit-test/utilities/sc_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
#include "cfe_msgids.h"
#include "cfe_tbl_msg.h"

extern SC_AppData_t SC_AppData;
extern SC_OperData_t SC_OperData;
extern SC_RtsInfoEntry_t RtsInfoTbl[SC_NUMBER_OF_RTS];
extern SC_AppData_t SC_AppData;
extern SC_OperData_t SC_OperData;

/*
* Global context structures
Expand Down Expand Up @@ -80,6 +79,7 @@ extern UT_CmdBuf_t UT_CmdBuf;
* Function Definitions
*/

void SC_Test_SetTableAddrs(void);
void SC_Test_Setup(void);
void SC_Test_TearDown(void);

Expand Down