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

[nasa/nos3#176] Unit Tests #14

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[nasa/nos3#176] Added hwlib stubs to CMakeLists for unit-tests and re…
…solved errors in current tests;
  • Loading branch information
jlucas9 committed Jul 31, 2024
commit 75e69a935ebc69fe0543dcce0fa82a792c4401f9
2 changes: 2 additions & 0 deletions fsw/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# inclusion of source files that are normally private
include_directories(${PROJECT_SOURCE_DIR}/fsw/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
include_directories(${hwlib_MISSION_DIR}/fsw/public_inc)


# Add a coverage test executable called "sample-ALL" that
Expand All @@ -35,6 +36,7 @@ add_cfe_coverage_test(sample ALL
"coveragetest/coveragetest_sample_app.c"
"../src/sample_app.c"
"../src/sample_device.c"
"../../../../fsw/apps/hwlib/fsw/stubs/libuart.c"
)

# The sample uses library functions provided by sample_lib so must be linked
Expand Down
77 changes: 18 additions & 59 deletions fsw/unit-test/coveragetest/coveragetest_sample_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void Test_SAMPLE_AppMain(void)
*/
UT_SetDeferredRetcode(UT_KEY(CFE_ES_RunLoop), 1, true);
UT_SetDeferredRetcode(UT_KEY(CFE_SB_ReceiveBuffer), 1, CFE_SB_PIPE_RD_ERR);
UT_CheckEvent_Setup(&EventTest, SAMPLE_PIPE_ERR_EID, "SAMPLE: SB Pipe Read Error");
UT_CheckEvent_Setup(&EventTest, SAMPLE_PIPE_ERR_EID, "SAMPLE: SB Pipe Read Error = %d");

/*
* Invoke again
Expand Down Expand Up @@ -252,19 +252,12 @@ void Test_SAMPLE_AppInit(void)

UT_SetDeferredRetcode(UT_KEY(CFE_SB_CreatePipe), 1, CFE_SB_BAD_ARGUMENT);
UT_TEST_FUNCTION_RC(SAMPLE_AppInit(), CFE_SB_BAD_ARGUMENT);
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_ES_WriteToSysLog)) == 2, "CFE_ES_WriteToSysLog() called");

UT_SetDeferredRetcode(UT_KEY(CFE_SB_Subscribe), 1, CFE_SB_BAD_ARGUMENT);
UT_TEST_FUNCTION_RC(SAMPLE_AppInit(), CFE_SB_BAD_ARGUMENT);
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_ES_WriteToSysLog)) == 3, "CFE_ES_WriteToSysLog() called");

UT_SetDeferredRetcode(UT_KEY(CFE_SB_Subscribe), 2, CFE_SB_BAD_ARGUMENT);
UT_TEST_FUNCTION_RC(SAMPLE_AppInit(), CFE_SB_BAD_ARGUMENT);
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_ES_WriteToSysLog)) == 4, "CFE_ES_WriteToSysLog() called");

UT_SetDeferredRetcode(UT_KEY(CFE_TBL_Register), 1, CFE_TBL_ERR_INVALID_OPTIONS);
UT_TEST_FUNCTION_RC(SAMPLE_AppInit(), CFE_TBL_ERR_INVALID_OPTIONS);
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_ES_WriteToSysLog)) == 5, "CFE_ES_WriteToSysLog() called");
}

void Test_SAMPLE_ProcessCommandPacket(void)
Expand All @@ -285,7 +278,7 @@ void Test_SAMPLE_ProcessCommandPacket(void)
UT_CheckEvent_t EventTest;

memset(&TestMsg, 0, sizeof(TestMsg));
UT_CheckEvent_Setup(&EventTest, SAMPLE_PROCESS_CMD_ERR_EID, "SAMPLE: Invalid command packet, MID = 0x%x");
UT_CheckEvent_Setup(&EventTest, SAMPLE_PROCESS_CMD_ERR_EID, NULL);

/*
* The CFE_MSG_GetMsgId() stub uses a data buffer to hold the
Expand All @@ -298,19 +291,14 @@ void Test_SAMPLE_ProcessCommandPacket(void)
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &MsgSize, sizeof(MsgSize), false);
SAMPLE_ProcessCommandPacket();

TestMsgId = CFE_SB_ValueToMsgId(SAMPLE_REQ_HK_TLM);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false);
SAMPLE_ProcessCommandPacket();
UtAssert_True(EventTest.MatchCount == 0, "SAMPLE_CMD_ERR_EID not generated (%u)",
(unsigned int)EventTest.MatchCount);

/* invalid message id */
TestMsgId = CFE_SB_INVALID_MSG_ID;
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
SAMPLE_ProcessCommandPacket();

/*
* Confirm that the event was generated only _once_
*/
UtAssert_True(EventTest.MatchCount == 1, "SAMPLE_CMD_ERR_EID generated (%u)",
(unsigned int)EventTest.MatchCount);
}
Expand All @@ -321,6 +309,7 @@ void Test_SAMPLE_ProcessGroundCommand(void)
* Test Case For:
* void SAMPLE_ProcessGroundCommand
*/
CFE_SB_MsgId_t TestMsgId = CFE_SB_ValueToMsgId(SAMPLE_CMD_MID);
CFE_MSG_FcnCode_t FcnCode;
size_t Size;

Expand All @@ -329,8 +318,7 @@ void Test_SAMPLE_ProcessGroundCommand(void)
{
CFE_SB_Buffer_t SBBuf;
SAMPLE_NoArgs_cmd_t Noop;
SAMPLE_NoArgs_cmd_t Reset;
SAMPLE_NoArgs_cmd_t Process;
SAMPLE_NoArgs_cmd_t Reset;
} TestMsg;
UT_CheckEvent_t EventTest;

Expand All @@ -348,36 +336,32 @@ void Test_SAMPLE_ProcessGroundCommand(void)
/* test dispatch of NOOP */
FcnCode = SAMPLE_NOOP_CC;
Size = sizeof(TestMsg.Noop);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false);
UT_CheckEvent_Setup(&EventTest, SAMPLE_CMD_NOOP_INF_EID, NULL);

SAMPLE_ProcessGroundCommand();

UtAssert_True(EventTest.MatchCount == 1, "SAMPLE_CMD_NOOP_INF_EID generated (%u)",
(unsigned int)EventTest.MatchCount);

/* test dispatch of RESET */
FcnCode = SAMPLE_RESET_COUNTERS_CC;
Size = sizeof(TestMsg.Reset);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false);
UT_CheckEvent_Setup(&EventTest, SAMPLE_CMD_RESET_INF_EID, NULL);

SAMPLE_ProcessGroundCommand();

UtAssert_True(EventTest.MatchCount == 1, "SAMPLE_CMD_RESET_INF_EID generated (%u)",
(unsigned int)EventTest.MatchCount);

/* test an invalid CC */
FcnCode = 1000;
FcnCode = 99;
Size = sizeof(TestMsg.Noop);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false);
UT_CheckEvent_Setup(&EventTest, SAMPLE_CMD_ERR_EID, "Invalid ground command code: CC = %d");
UT_CheckEvent_Setup(&EventTest, SAMPLE_CMD_ERR_EID, NULL);
SAMPLE_ProcessGroundCommand();

/*
* Confirm that the event was generated only _once_
*/
UtAssert_True(EventTest.MatchCount == 1, "SAMPLE_CMD_ERR_EID generated (%u)",
(unsigned int)EventTest.MatchCount);
}
Expand All @@ -386,7 +370,7 @@ void Test_SAMPLE_ReportHousekeeping(void)
{
/*
* Test Case For:
* void SAMPLE_ReportHousekeeping( const CFE_SB_CmdHdr_t *Msg )
* void SAMPLE_ReportHousekeeping()
*/
CFE_MSG_Message_t *MsgSend;
CFE_MSG_Message_t *MsgTimestamp;
Expand All @@ -402,7 +386,7 @@ void Test_SAMPLE_ReportHousekeeping(void)
UT_SetDataBuffer(UT_KEY(CFE_SB_TimeStampMsg), &MsgTimestamp, sizeof(MsgTimestamp), false);

/* Call unit under test, NULL pointer confirms command access is through APIs */
SAMPLE_ProcessCommandPacket();
SAMPLE_ReportHousekeeping();

/* Confirm message sent*/
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_SB_TransmitMsg)) == 1, "CFE_SB_TransmitMsg() called once");
Expand All @@ -412,30 +396,6 @@ void Test_SAMPLE_ReportHousekeeping(void)
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_SB_TimeStampMsg)) == 1, "CFE_SB_TimeStampMsg() called once");
UtAssert_True(MsgTimestamp == &SAMPLE_AppData.HkTelemetryPkt.TlmHeader.Msg,
"CFE_SB_TimeStampMsg() address matches expected");

/*
* Confirm that the CFE_TBL_Manage() call was done
*/
UtAssert_True(UT_GetStubCount(UT_KEY(CFE_TBL_Manage)) == 1, "CFE_TBL_Manage() called");
}

void Test_SAMPLE_ResetCounters(void)
{
/*
* Test Case For:
* void SAMPLE_ResetCounters( void )
*/
UT_CheckEvent_t EventTest;

UT_CheckEvent_Setup(&EventTest, SAMPLE_CMD_RESET_INF_EID, "SAMPLE: RESET command");

SAMPLE_ResetCounters();

/*
* Confirm that the event was generated
*/
UtAssert_True(EventTest.MatchCount == 1, "SAMPLE_CMD_RESET_INF_EID generated (%u)",
(unsigned int)EventTest.MatchCount);
}

void Test_SAMPLE_VerifyCmdLength(void)
Expand All @@ -447,14 +407,13 @@ void Test_SAMPLE_VerifyCmdLength(void)
UT_CheckEvent_t EventTest;
size_t size = 1;
CFE_MSG_FcnCode_t fcncode = 2;
CFE_SB_MsgId_t msgid = CFE_SB_ValueToMsgId(3);
CFE_SB_MsgId_t msgid = CFE_SB_ValueToMsgId(SAMPLE_CMD_MID);

/*
* test a match case
*/
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &size, sizeof(size), false);
UT_CheckEvent_Setup(&EventTest, SAMPLE_LEN_ERR_EID,
"Invalid Msg length: ID = 0x%X, CC = %u, Len = %u, Expected = %u");
UT_CheckEvent_Setup(&EventTest, SAMPLE_LEN_ERR_EID, NULL);

SAMPLE_VerifyCmdLength(NULL, size);

Expand All @@ -470,6 +429,7 @@ void Test_SAMPLE_VerifyCmdLength(void)
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &size, sizeof(size), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &msgid, sizeof(msgid), false);
UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &fcncode, sizeof(fcncode), false);
UT_CheckEvent_Setup(&EventTest, SAMPLE_LEN_ERR_EID, NULL);
SAMPLE_VerifyCmdLength(NULL, size + 1);

/*
Expand Down Expand Up @@ -502,6 +462,5 @@ void UtTest_Setup(void)
ADD_TEST(SAMPLE_ProcessCommandPacket);
ADD_TEST(SAMPLE_ProcessGroundCommand);
ADD_TEST(SAMPLE_ReportHousekeeping);
ADD_TEST(SAMPLE_ResetCounters);
ADD_TEST(SAMPLE_VerifyCmdLength);
}