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 #7, update code coverage for md_app_tests.c #25

Merged
merged 1 commit into from
Jul 8, 2022
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
4 changes: 2 additions & 2 deletions .github/workflows/unit-test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
name: Run unit test and coverage
uses: nasa/cFS/.github/workflows/unit-test-coverage.yml@main
with:
max-missed-branches: 7
max-missed-lines: 11
max-missed-branches: 3
max-missed-lines: 5
10 changes: 7 additions & 3 deletions unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ include_directories(stubs)
# Accomplish this by cycling through all the app's source files, there must be
# a *_tests file for each
foreach(SRCFILE ${APP_SRC_FILES})
# Get the base sourcefile name as a module name without path or the

# Get the base sourcefile name as a module name without path or the
# extension, this will be used as the base name of the unit test file.
get_filename_component(UNIT_NAME "${SRCFILE}" NAME_WE)

# Use the module name to make the test name by adding _tests to the end
set(TESTS_NAME "${UNIT_NAME}_tests")

# Make the test sourcefile name with unit test path and extension
set(TESTS_SOURCE_FILE "${PROJECT_SOURCE_DIR}/unit-test/${TESTS_NAME}.c")

Expand All @@ -46,4 +46,8 @@ foreach(SRCFILE ${APP_SRC_FILES})
# Add dependency to utilities and internal stubs
add_cfe_coverage_dependency(md "${UNIT_NAME}" md_internal)

# Include overrides for unit under test
target_include_directories(coverage-md-${UNIT_NAME}-object BEFORE PRIVATE
stubs/override_inc
)
endforeach()
84 changes: 30 additions & 54 deletions unit-test/md_app_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,6 @@ void MD_InitTableServices_Test_TblRegisterCriticalError(void)

void MD_InitTableServices_Test_TblNameError(void)
{
int32 Result;
int32 strCmpResult;
char ExpectedEventString[2][CFE_MISSION_EVS_MAX_MESSAGE_LENGTH];

Expand All @@ -1219,24 +1218,12 @@ void MD_InitTableServices_Test_TblNameError(void)
snprintf(ExpectedEventString[1], CFE_MISSION_EVS_MAX_MESSAGE_LENGTH,
"Dwell Tables Recovered: %%d, Dwell Tables Initialized: %%d");

/* Set to satisfy condition "Status == CFE_TBL_INFO_RECOVERED_TBL" */
UT_SetDeferredRetcode(UT_KEY(CFE_TBL_Register), 1, CFE_TBL_INFO_RECOVERED_TBL);

UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_GetAddress), CFE_TBL_INFO_UPDATED);

UT_SetDeferredRetcode(UT_KEY(CFE_TBL_Load), 1, CFE_SUCCESS);

/* Set MD_LoadTablePtr = &MD_DWELL_TBL_TEST_GlobalLoadTable */
UT_SetHookFunction(UT_KEY(CFE_TBL_GetAddress), &MD_DWELL_TBL_TEST_CFE_TBL_GetAddressHook, NULL);

/* Set to generate snprintf error MD_INIT_TBL_NAME_ERR_EID */
UT_SetDeferredRetcode(UT_KEY(stub_snprintf), 1, -1);

/* Execute the function being tested */
Result = MD_InitTableServices();

/* Verify results */
UtAssert_True(Result == -1, "Result == -1");
UtAssert_UINT32_EQ(MD_InitTableServices(), -1);

UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, MD_INIT_TBL_NAME_ERR_EID);
UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR);
Expand All @@ -1254,16 +1241,12 @@ void MD_InitTableServices_Test_TblNameError(void)

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

call_count_CFE_EVS_SendEvent = UT_GetStubCount(UT_KEY(CFE_EVS_SendEvent));

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

} /* end MD_InitTableServices_Test_TblNameError */

void MD_InitTableServices_Test_TblFileNameError(void)
{
int32 Result;
int32 strCmpResult;
char ExpectedEventString[2][CFE_MISSION_EVS_MAX_MESSAGE_LENGTH];

Expand All @@ -1273,24 +1256,12 @@ void MD_InitTableServices_Test_TblFileNameError(void)
snprintf(ExpectedEventString[1], CFE_MISSION_EVS_MAX_MESSAGE_LENGTH,
"Dwell Tables Recovered: %%d, Dwell Tables Initialized: %%d");

/* Set to satisfy condition "Status == CFE_TBL_INFO_RECOVERED_TBL" */
UT_SetDeferredRetcode(UT_KEY(CFE_TBL_Register), 1, CFE_TBL_INFO_RECOVERED_TBL);

UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_GetAddress), CFE_TBL_INFO_UPDATED);

UT_SetDeferredRetcode(UT_KEY(CFE_TBL_Load), 1, CFE_SUCCESS);

/* Set MD_LoadTablePtr = &MD_DWELL_TBL_TEST_GlobalLoadTable */
UT_SetHookFunction(UT_KEY(CFE_TBL_GetAddress), &MD_DWELL_TBL_TEST_CFE_TBL_GetAddressHook, NULL);

/* Set to generate snprintf MD_INIT_TBL_FILENAME_ERR_EID error */
UT_SetDeferredRetcode(UT_KEY(stub_snprintf), 2, -1);

/* Execute the function being tested */
Result = MD_InitTableServices();

/* Verify results */
UtAssert_True(Result == -1, "Result == -1");
UtAssert_UINT32_EQ(MD_InitTableServices(), -1);

UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, MD_INIT_TBL_FILENAME_ERR_EID);
UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_ERROR);
Expand All @@ -1308,10 +1279,7 @@ void MD_InitTableServices_Test_TblFileNameError(void)

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

call_count_CFE_EVS_SendEvent = UT_GetStubCount(UT_KEY(CFE_EVS_SendEvent));

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

} /* end MD_InitTableServices_Test_TblFileNameError */

Expand Down Expand Up @@ -1887,29 +1855,37 @@ void MD_SearchCmdHndlrTbl_Test_BadCmdCode(void)

} /* end MD_SearchCmdHndlrTbl_Test_BadCmdCode */

void MD_SearchCmdHndlrTbl_Test_BasMsgID(void)
void MD_SearchCmdHndlrTbl_Test_BadMsgID(void)
{
int16 Result;
CFE_SB_MsgId_t MessageID = MD_UT_MID_1;
uint16 CommandCode = MD_RESET_CNTRS_CC;

/* Execute the function being tested */
Result = MD_SearchCmdHndlrTbl(MessageID, CommandCode);

/* Verify results */
UtAssert_True(Result == MD_BAD_MSG_ID, "Result == MD_BAD_MSG_ID");
UtAssert_INT32_EQ(MD_SearchCmdHndlrTbl(MessageID, CommandCode), MD_BAD_MSG_ID);

call_count_CFE_EVS_SendEvent = UT_GetStubCount(UT_KEY(CFE_EVS_SendEvent));
UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0);

UtAssert_True(call_count_CFE_EVS_SendEvent == 0, "CFE_EVS_SendEvent was called %u time(s), expected 0",
call_count_CFE_EVS_SendEvent);
} /* end MD_SearchCmdHndlrTbl_Test_BadMsgID */

void MD_SearchCmdHndlrTbl_Test_LastTableEntry(void)
{
CFE_SB_MsgId_t MessageID = CFE_SB_MSGID_RESERVED;
uint16 CommandCode = MD_RESET_CNTRS_CC;

} /* end MD_SearchCmdHndlrTbl_Test_BasMsgID */
/* Execute the function being tested */
/* Verify results */
UtAssert_INT32_EQ(MD_SearchCmdHndlrTbl(MessageID, CommandCode), MD_BAD_MSG_ID);

UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 0);

} /* end MD_SearchCmdHndlrTbl_Test_LastTableEntry */

void UtTest_Setup(void)
{
UtTest_Add(MD_AppMain_Test_AppInitError, MD_Test_Setup, MD_Test_TearDown, "MD_AppMain_Test_AppInitError");
UtTest_Add(MD_AppMain_Test_RcvMsgError, MD_Test_Setup, MD_Test_TearDown, "MD_AppMain_Test_RcvMsgError");
UtTest_Add(MD_AppMain_Test_RcvMsgTimeout, MD_Test_Setup, MD_Test_TearDown, "MD_AppMain_Test_RcvMsgTimeout");
UtTest_Add(MD_AppMain_Test_RcvMsgNullBuffer, MD_Test_Setup, MD_Test_TearDown, "MD_AppMain_Test_RcvMsgNullBuffer");
UtTest_Add(MD_AppMain_Test_WakeupNominal, MD_Test_Setup, MD_Test_TearDown, "MD_AppMain_Test_WakeupNominal");
UtTest_Add(MD_AppMain_Test_WakeupLengthError, MD_Test_Setup, MD_Test_TearDown, "MD_AppMain_Test_WakeupLengthError");
Expand Down Expand Up @@ -1953,13 +1929,10 @@ void UtTest_Setup(void)
"MD_InitTableServices_Test_TblTooLarge");
UtTest_Add(MD_InitTableServices_Test_TblRegisterCriticalError, MD_Test_Setup, MD_Test_TearDown,
"MD_InitTableServices_Test_TblRegisterCriticalError");

/* TODO uncomment when snprintf can successfully be stubbed */
/*
* UtTest_Add(MD_InitTableServices_Test_TblNameError, MD_Test_Setup, MD_Test_TearDown,
* "MD_InitTableServices_Test_TblNameError"); UtTest_Add(MD_InitTableServices_Test_TblFileNameError, MD_Test_Setup,
* MD_Test_TearDown, "MD_InitTableServices_Test_TblFileNameError");
*/
UtTest_Add(MD_InitTableServices_Test_TblNameError, MD_Test_Setup, MD_Test_TearDown,
"MD_InitTableServices_Test_TblNameError");
UtTest_Add(MD_InitTableServices_Test_TblFileNameError, MD_Test_Setup, MD_Test_TearDown,
"MD_InitTableServices_Test_TblFileNameError");

UtTest_Add(MD_ManageDwellTable_Test_ValidationPendingSucceedThenFail, MD_Test_Setup, MD_Test_TearDown,
"MD_ManageDwellTable_Test_ValidationPendingSucceedThenFail");
Expand Down Expand Up @@ -1989,6 +1962,7 @@ void UtTest_Setup(void)
#if MD_SIGNATURE_OPTION == 1
UtTest_Add(MD_ExecRequest_Test_SetSignature, MD_Test_Setup, MD_Test_TearDown, "MD_ExecRequest_Test_SetSignature");
#endif

/* Note: Cannot test default case in ExecRequest, because that would require the pre-defined constant structure
MD_CmdHandlerTbl to have an entry
with a command code other than the handled cases. It does not, except for the last entry, which does not work
Expand All @@ -2003,8 +1977,10 @@ void UtTest_Setup(void)

UtTest_Add(MD_SearchCmdHndlrTbl_Test_BadCmdCode, MD_Test_Setup, MD_Test_TearDown,
"MD_SearchCmdHndlrTbl_Test_BadCmdCode");
UtTest_Add(MD_SearchCmdHndlrTbl_Test_BasMsgID, MD_Test_Setup, MD_Test_TearDown,
"MD_SearchCmdHndlrTbl_Test_BasMsgID");
UtTest_Add(MD_SearchCmdHndlrTbl_Test_BadMsgID, MD_Test_Setup, MD_Test_TearDown,
"MD_SearchCmdHndlrTbl_Test_BadMsgID");
UtTest_Add(MD_SearchCmdHndlrTbl_Test_LastTableEntry, MD_Test_Setup, MD_Test_TearDown,
"MD_SearchCmdHndlrTbl_Test_LastTableEntry");

} /* end UtTest_Setup */

Expand Down