From ab4b6c843e3b76ed50d9ed0b2ab59875f2057daa Mon Sep 17 00:00:00 2001 From: Avi Weiss Date: Fri, 7 Jun 2024 07:15:08 +0200 Subject: [PATCH] Add first test - should fail --- fsw/src/hs_cmds.c | 112 +++++++++++++++++++++++++++++--------- unit-test/hs_cmds_tests.c | 43 +++++++++++++++ 2 files changed, 130 insertions(+), 25 deletions(-) diff --git a/fsw/src/hs_cmds.c b/fsw/src/hs_cmds.c index b545bd6..d137eed 100644 --- a/fsw/src/hs_cmds.c +++ b/fsw/src/hs_cmds.c @@ -237,9 +237,19 @@ void HS_ResetCounters(void) CFE_Status_t HS_EnableAppMonCmd(const HS_EnableAppMonCmd_t *BufPtr) { HS_AppData.CmdCount++; - HS_AppMonStatusRefresh(); - HS_AppData.CurrentAppMonState = HS_STATE_ENABLED; - CFE_EVS_SendEvent(HS_ENABLE_APPMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Application Monitoring Enabled"); + + if (HS_AppData.CurrentAppMonState == HS_STATE_ENABLED) + { + CFE_EVS_SendEvent(HS_ENABLE_APPMON_INF_EID, CFE_EVS_EventType_INFORMATION, + "Application Monitoring is *already* Enabled"); + } + else + { + HS_AppMonStatusRefresh(); + HS_AppData.CurrentAppMonState = HS_STATE_ENABLED; + CFE_EVS_SendEvent(HS_ENABLE_APPMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Application Monitoring Enabled"); + } + return CFE_SUCCESS; } @@ -251,8 +261,18 @@ CFE_Status_t HS_EnableAppMonCmd(const HS_EnableAppMonCmd_t *BufPtr) CFE_Status_t HS_DisableAppMonCmd(const HS_DisableAppMonCmd_t *BufPtr) { HS_AppData.CmdCount++; - HS_AppData.CurrentAppMonState = HS_STATE_DISABLED; - CFE_EVS_SendEvent(HS_DISABLE_APPMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Application Monitoring Disabled"); + + if (HS_AppData.CurrentAppMonState == HS_STATE_DISABLED) + { + CFE_EVS_SendEvent(HS_DISABLE_APPMON_INF_EID, CFE_EVS_EventType_INFORMATION, + "Application Monitoring is *already* Disabled"); + } + else + { + HS_AppData.CurrentAppMonState = HS_STATE_DISABLED; + CFE_EVS_SendEvent(HS_DISABLE_APPMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Application Monitoring Disabled"); + } + return CFE_SUCCESS; } @@ -265,10 +285,12 @@ CFE_Status_t HS_EnableEventMonCmd(const HS_EnableEventMonCmd_t *BufPtr) { CFE_Status_t Status = CFE_SUCCESS; - /* - ** Subscribe to Event Messages if currently disabled - */ - if (HS_AppData.CurrentEventMonState == HS_STATE_DISABLED) + if (HS_AppData.CurrentEventMonState == HS_STATE_ENABLED) + { + CFE_EVS_SendEvent(HS_ENABLE_EVENTMON_INF_EID, CFE_EVS_EventType_INFORMATION, + "Event Monitoring is *already* Enabled"); + } + else { Status = CFE_SB_SubscribeEx(CFE_SB_ValueToMsgId(CFE_EVS_LONG_EVENT_MSG_MID), HS_AppData.EventPipe, CFE_SB_DEFAULT_QOS, HS_EVENT_PIPE_DEPTH); @@ -277,6 +299,9 @@ CFE_Status_t HS_EnableEventMonCmd(const HS_EnableEventMonCmd_t *BufPtr) Status = CFE_SB_SubscribeEx(CFE_SB_ValueToMsgId(CFE_EVS_SHORT_EVENT_MSG_MID), HS_AppData.EventPipe, CFE_SB_DEFAULT_QOS, HS_EVENT_PIPE_DEPTH); + HS_AppData.CurrentEventMonState = HS_STATE_ENABLED; + CFE_EVS_SendEvent(HS_ENABLE_EVENTMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Event Monitoring Enabled"); + if (Status != CFE_SUCCESS) { CFE_EVS_SendEvent(HS_EVENTMON_SHORT_SUB_EID, CFE_EVS_EventType_ERROR, @@ -295,8 +320,6 @@ CFE_Status_t HS_EnableEventMonCmd(const HS_EnableEventMonCmd_t *BufPtr) if (Status == CFE_SUCCESS) { HS_AppData.CmdCount++; - HS_AppData.CurrentEventMonState = HS_STATE_ENABLED; - CFE_EVS_SendEvent(HS_ENABLE_EVENTMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Event Monitoring Enabled"); } else { @@ -315,10 +338,12 @@ CFE_Status_t HS_DisableEventMonCmd(const HS_DisableEventMonCmd_t *BufPtr) { CFE_Status_t Status = CFE_SUCCESS; - /* - ** Unsubscribe from Event Messages if currently enabled - */ - if (HS_AppData.CurrentEventMonState == HS_STATE_ENABLED) + if (HS_AppData.CurrentEventMonState == HS_STATE_DISABLED) + { + CFE_EVS_SendEvent(HS_DISABLE_EVENTMON_INF_EID, CFE_EVS_EventType_INFORMATION, + "Event Monitoring is *already* Dnabled"); + } + else { Status = CFE_SB_Unsubscribe(CFE_SB_ValueToMsgId(CFE_EVS_LONG_EVENT_MSG_MID), HS_AppData.EventPipe); @@ -326,6 +351,9 @@ CFE_Status_t HS_DisableEventMonCmd(const HS_DisableEventMonCmd_t *BufPtr) { Status = CFE_SB_Unsubscribe(CFE_SB_ValueToMsgId(CFE_EVS_SHORT_EVENT_MSG_MID), HS_AppData.EventPipe); + HS_AppData.CurrentEventMonState = HS_STATE_DISABLED; + CFE_EVS_SendEvent(HS_DISABLE_EVENTMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Event Monitoring Disabled"); + if (Status != CFE_SUCCESS) { CFE_EVS_SendEvent(HS_EVENTMON_SHORT_UNSUB_EID, CFE_EVS_EventType_ERROR, @@ -344,8 +372,6 @@ CFE_Status_t HS_DisableEventMonCmd(const HS_DisableEventMonCmd_t *BufPtr) if (Status == CFE_SUCCESS) { HS_AppData.CmdCount++; - HS_AppData.CurrentEventMonState = HS_STATE_DISABLED; - CFE_EVS_SendEvent(HS_DISABLE_EVENTMON_INF_EID, CFE_EVS_EventType_INFORMATION, "Event Monitoring Disabled"); } else { @@ -363,8 +389,17 @@ CFE_Status_t HS_DisableEventMonCmd(const HS_DisableEventMonCmd_t *BufPtr) CFE_Status_t HS_EnableAlivenessCmd(const HS_EnableAlivenessCmd_t *BufPtr) { HS_AppData.CmdCount++; - HS_AppData.CurrentAlivenessState = HS_STATE_ENABLED; - CFE_EVS_SendEvent(HS_ENABLE_ALIVENESS_INF_EID, CFE_EVS_EventType_INFORMATION, "Aliveness Indicator Enabled"); + + if (HS_AppData.CurrentAlivenessState == HS_STATE_ENABLED) + { + CFE_EVS_SendEvent(HS_ENABLE_ALIVENESS_INF_EID, CFE_EVS_EventType_INFORMATION, + "Aliveness Indicator *already* Enabled"); + } + else + { + HS_AppData.CurrentAlivenessState = HS_STATE_ENABLED; + CFE_EVS_SendEvent(HS_ENABLE_ALIVENESS_INF_EID, CFE_EVS_EventType_INFORMATION, "Aliveness Indicator Enabled"); + } return CFE_SUCCESS; } @@ -377,8 +412,17 @@ CFE_Status_t HS_EnableAlivenessCmd(const HS_EnableAlivenessCmd_t *BufPtr) CFE_Status_t HS_DisableAlivenessCmd(const HS_DisableAlivenessCmd_t *BufPtr) { HS_AppData.CmdCount++; - HS_AppData.CurrentAlivenessState = HS_STATE_DISABLED; - CFE_EVS_SendEvent(HS_DISABLE_ALIVENESS_INF_EID, CFE_EVS_EventType_INFORMATION, "Aliveness Indicator Disabled"); + + if (HS_AppData.CurrentAlivenessState == HS_STATE_DISABLED) + { + CFE_EVS_SendEvent(HS_DISABLE_ALIVENESS_INF_EID, CFE_EVS_EventType_INFORMATION, + "Aliveness Indicator *already* Disabled"); + } + else + { + HS_AppData.CurrentAlivenessState = HS_STATE_DISABLED; + CFE_EVS_SendEvent(HS_DISABLE_ALIVENESS_INF_EID, CFE_EVS_EventType_INFORMATION, "Aliveness Indicator Disabled"); + } return CFE_SUCCESS; } @@ -391,8 +435,17 @@ CFE_Status_t HS_DisableAlivenessCmd(const HS_DisableAlivenessCmd_t *BufPtr) CFE_Status_t HS_EnableCpuHogCmd(const HS_EnableCpuHogCmd_t *BufPtr) { HS_AppData.CmdCount++; - HS_AppData.CurrentCPUHogState = HS_STATE_ENABLED; - CFE_EVS_SendEvent(HS_ENABLE_CPUHOG_INF_EID, CFE_EVS_EventType_INFORMATION, "CPU Hogging Indicator Enabled"); + + if (HS_AppData.CurrentCPUHogState == HS_STATE_ENABLED) + { + CFE_EVS_SendEvent(HS_ENABLE_CPUHOG_INF_EID, CFE_EVS_EventType_INFORMATION, + "CPU Hogging Indicator *already* Enabled"); + } + else + { + HS_AppData.CurrentCPUHogState = HS_STATE_ENABLED; + CFE_EVS_SendEvent(HS_ENABLE_CPUHOG_INF_EID, CFE_EVS_EventType_INFORMATION, "CPU Hogging Indicator Enabled"); + } return CFE_SUCCESS; } @@ -405,8 +458,17 @@ CFE_Status_t HS_EnableCpuHogCmd(const HS_EnableCpuHogCmd_t *BufPtr) CFE_Status_t HS_DisableCpuHogCmd(const HS_DisableCpuHogCmd_t *BufPtr) { HS_AppData.CmdCount++; - HS_AppData.CurrentCPUHogState = HS_STATE_DISABLED; - CFE_EVS_SendEvent(HS_DISABLE_CPUHOG_INF_EID, CFE_EVS_EventType_INFORMATION, "CPU Hogging Indicator Disabled"); + + if (HS_AppData.CurrentCPUHogState == HS_STATE_DISABLED) + { + CFE_EVS_SendEvent(HS_DISABLE_CPUHOG_INF_EID, CFE_EVS_EventType_INFORMATION, + "CPU Hogging Indicator *already* Disabled"); + } + else + { + HS_AppData.CurrentCPUHogState = HS_STATE_DISABLED; + CFE_EVS_SendEvent(HS_DISABLE_CPUHOG_INF_EID, CFE_EVS_EventType_INFORMATION, "CPU Hogging Indicator Disabled"); + } return CFE_SUCCESS; } diff --git a/unit-test/hs_cmds_tests.c b/unit-test/hs_cmds_tests.c index e799b21..4b38d5d 100644 --- a/unit-test/hs_cmds_tests.c +++ b/unit-test/hs_cmds_tests.c @@ -1106,6 +1106,49 @@ void HS_EnableAppMonCmd_Test(void) call_count_CFE_EVS_SendEvent); } +void HS_EnableAppMonCmd_Test_AlreadyEnabled(void) +{ + CFE_SB_MsgId_t TestMsgId; + CFE_MSG_FcnCode_t FcnCode; + size_t MsgSize; + HS_AMTEntry_t AMTable[HS_MAX_MONITORED_APPS]; + int32 strCmpResult; + char ExpectedEventString[2][CFE_MISSION_EVS_MAX_MESSAGE_LENGTH]; + snprintf(ExpectedEventString[0], CFE_MISSION_EVS_MAX_MESSAGE_LENGTH, "Application Monitoring is *already* Enabled"); + + memset(AMTable, 0, sizeof(AMTable)); + + TestMsgId = CFE_SB_ValueToMsgId(HS_CMD_MID); + FcnCode = HS_ENABLE_APP_MON_CC; + MsgSize = sizeof(UT_CmdBuf.EnableAppMonCmd); + 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), &MsgSize, sizeof(MsgSize), false); + + HS_AppData.AMTablePtr = AMTable; + + /* Execute the function being tested */ + HS_EnableAppMonCmd(&UT_CmdBuf.EnableAppMonCmd); + + /* Verify results */ + UtAssert_True(HS_AppData.CmdCount == 1, "HS_AppData.CmdCount == 1"); + + UtAssert_True(HS_AppData.CurrentAppMonState == HS_STATE_ENABLED, + "HS_AppData.CurrentAppMonState == HS_STATE_ENABLED"); + + UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventID, HS_ENABLE_APPMON_INF_EID); + UtAssert_INT32_EQ(context_CFE_EVS_SendEvent[0].EventType, CFE_EVS_EventType_INFORMATION); + + strCmpResult = + strncmp(ExpectedEventString[0], 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); +} + void HS_DisableAppMonCmd_Test(void) { CFE_SB_MsgId_t TestMsgId;