From a3cbee21e867304a715aac3ef34f811f51c0f081 Mon Sep 17 00:00:00 2001 From: "Lucas, John P." Date: Tue, 23 Jul 2024 12:03:53 -0400 Subject: [PATCH] [nasa/nos3#176] Baseline unit-tests from NASA/sample_app to start from; --- fsw/CMakeLists.txt | 8 + fsw/unit-test/CMakeLists.txt | 69 +++++ fsw/unit-test/common/eventcheck.c | 128 ++++++++ fsw/unit-test/common/eventcheck.h | 64 ++++ fsw/unit-test/common/setup.c | 55 ++++ fsw/unit-test/common/setup.h | 45 +++ .../coveragetest/coveragetest_sample_app.c | 185 ++++++++++++ .../coveragetest_sample_app_cmds.c | 219 ++++++++++++++ .../coveragetest_sample_app_dispatch.c | 280 ++++++++++++++++++ .../coveragetest_sample_app_eds_dispatch.c | 62 ++++ .../coveragetest_sample_app_utils.c | 97 ++++++ .../sample_app_coveragetest_common.h | 54 ++++ fsw/unit-test/stubs/sample_app_cmds_stubs.c | 106 +++++++ .../stubs/sample_app_dispatch_stubs.c | 67 +++++ fsw/unit-test/stubs/sample_app_global_stubs.c | 27 ++ fsw/unit-test/stubs/sample_app_stubs.c | 51 ++++ fsw/unit-test/stubs/sample_app_utils_stubs.c | 54 ++++ 17 files changed, 1571 insertions(+) create mode 100644 fsw/unit-test/CMakeLists.txt create mode 100644 fsw/unit-test/common/eventcheck.c create mode 100644 fsw/unit-test/common/eventcheck.h create mode 100644 fsw/unit-test/common/setup.c create mode 100644 fsw/unit-test/common/setup.h create mode 100644 fsw/unit-test/coveragetest/coveragetest_sample_app.c create mode 100644 fsw/unit-test/coveragetest/coveragetest_sample_app_cmds.c create mode 100644 fsw/unit-test/coveragetest/coveragetest_sample_app_dispatch.c create mode 100644 fsw/unit-test/coveragetest/coveragetest_sample_app_eds_dispatch.c create mode 100644 fsw/unit-test/coveragetest/coveragetest_sample_app_utils.c create mode 100644 fsw/unit-test/coveragetest/sample_app_coveragetest_common.h create mode 100644 fsw/unit-test/stubs/sample_app_cmds_stubs.c create mode 100644 fsw/unit-test/stubs/sample_app_dispatch_stubs.c create mode 100644 fsw/unit-test/stubs/sample_app_global_stubs.c create mode 100644 fsw/unit-test/stubs/sample_app_stubs.c create mode 100644 fsw/unit-test/stubs/sample_app_utils_stubs.c diff --git a/fsw/CMakeLists.txt b/fsw/CMakeLists.txt index 5f0d80a..67e9693 100644 --- a/fsw/CMakeLists.txt +++ b/fsw/CMakeLists.txt @@ -14,3 +14,11 @@ aux_source_directory(src APP_SRC_FILES) # Create the app module add_cfe_app(sample ${APP_SRC_FILES}) + +# If UT is enabled, then add the tests from the subdirectory +# Note that this is an app, and therefore does not provide +# stub functions, as other entities would not typically make +# direct function calls into this application. +if (ENABLE_UNIT_TESTS) + add_subdirectory(unit-test) +endif (ENABLE_UNIT_TESTS) diff --git a/fsw/unit-test/CMakeLists.txt b/fsw/unit-test/CMakeLists.txt new file mode 100644 index 0000000..28c052e --- /dev/null +++ b/fsw/unit-test/CMakeLists.txt @@ -0,0 +1,69 @@ +################################################################## +# +# Coverage Unit Test build recipe +# +# This CMake file contains the recipe for building the sample unit tests. +# It is invoked from the parent directory when unit tests are enabled. +# +################################################################## + +# +# NOTE on the subdirectory structures here: +# +# - "inc" provides local header files shared between the coveragetest, +# wrappers, and overrides source code units +# - "coveragetest" contains source code for the actual unit test cases +# The primary objective is to get line/path coverage on the FSW +# code units. +# + +# Use the UT assert public API, and allow direct +# inclusion of source files that are normally private +include_directories(../fsw/src) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc) + +add_cfe_coverage_stubs(sample_app + stubs/sample_app_global_stubs.c + stubs/sample_app_stubs.c + stubs/sample_app_cmds_stubs.c + stubs/sample_app_dispatch_stubs.c + stubs/sample_app_utils_stubs.c +) + +add_library(sample_app_ut_common STATIC + common/eventcheck.c + common/setup.c +) + +target_include_directories(sample_app_ut_common PUBLIC common) +target_link_libraries(sample_app_ut_common core_api ut_assert) + + +# Generate a dedicated "testrunner" executable for each test file +# 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 + # 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 "coveragetest_${UNIT_NAME}") + + # Make the test sourcefile name with unit test path and extension + set(TESTS_SOURCE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/coveragetest/${TESTS_NAME}.c") + + # Create the coverage test executable + add_cfe_coverage_test(sample_app "${UNIT_NAME}" "${TESTS_SOURCE_FILE}" "../${SRCFILE}") + add_cfe_coverage_dependency(sample_app "${UNIT_NAME}" sample_app) + target_link_libraries(coverage-sample_app-${UNIT_NAME}-testrunner coverage-sample_app-stubs sample_app_ut_common) + +endforeach() + + + +# The sample_app uses library functions provided by sample_lib so must be linked +# with the sample_lib stub library (this is mainly just an example of how this +# can be done). +add_cfe_coverage_dependency(sample_app sample_app_cmds sample_lib) diff --git a/fsw/unit-test/common/eventcheck.c b/fsw/unit-test/common/eventcheck.c new file mode 100644 index 0000000..5bb23ee --- /dev/null +++ b/fsw/unit-test/common/eventcheck.c @@ -0,0 +1,128 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** File: coveragetest_sample_app.c +** +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ +#include "common_types.h" +#include "cfe_evs.h" + +#include "eventcheck.h" + +#include "utassert.h" +#include "uttest.h" +#include "utstubs.h" + +/* + * An example hook function to check for a specific event. + */ +static int32 UT_CheckEvent_Hook(void *UserObj, int32 StubRetcode, uint32 CallCount, const UT_StubContext_t *Context, + va_list va) +{ + UT_CheckEvent_t *State = UserObj; + uint16 EventId; + const char * Spec; + + /* + * The CFE_EVS_SendEvent stub passes the EventID as the + * first context argument. + */ + if (Context->ArgCount > 0) + { + EventId = UT_Hook_GetArgValueByName(Context, "EventID", uint16); + if (EventId == State->ExpectedEvent) + { + if (State->ExpectedFormat != NULL) + { + Spec = UT_Hook_GetArgValueByName(Context, "Spec", const char *); + if (Spec != NULL) + { + /* + * Example of how to validate the full argument set. + * ------------------------------------------------ + * + * If really desired one can call something like: + * + * char TestText[CFE_MISSION_EVS_MAX_MESSAGE_LENGTH]; + * vsnprintf(TestText, sizeof(TestText), Spec, va); + * + * And then compare the output (TestText) to the expected fully-rendered string. + * + * NOTE: While this can be done, use with discretion - This isn't really + * verifying that the FSW code unit generated the correct event text, + * rather it is validating what the system snprintf() library function + * produces when passed the format string and args. + * + * This type of check has been demonstrated to make tests very fragile, + * because it is influenced by many factors outside the control of the + * test case. + * + * __This derived string is not an actual output of the unit under test__ + */ + if (strcmp(Spec, State->ExpectedFormat) == 0) + { + ++State->MatchCount; + } + } + } + else + { + ++State->MatchCount; + } + } + } + + return 0; +} + +/* + * Helper function to set up for event checking + * This attaches the hook function to CFE_EVS_SendEvent + */ +void UT_CheckEvent_Setup_Impl(UT_CheckEvent_t *Evt, uint16 ExpectedEvent, const char *EventName, + const char *ExpectedFormat) +{ + if (ExpectedFormat == NULL) + { + UtPrintf("CheckEvent will match: %s(%u)", EventName, ExpectedEvent); + } + else + { + UtPrintf("CheckEvent will match: %s(%u), \"%s\"", EventName, ExpectedEvent, ExpectedFormat); + } + memset(Evt, 0, sizeof(*Evt)); + Evt->ExpectedEvent = ExpectedEvent; + Evt->ExpectedFormat = ExpectedFormat; + UT_SetVaHookFunction(UT_KEY(CFE_EVS_SendEvent), UT_CheckEvent_Hook, Evt); +} diff --git a/fsw/unit-test/common/eventcheck.h b/fsw/unit-test/common/eventcheck.h new file mode 100644 index 0000000..07168aa --- /dev/null +++ b/fsw/unit-test/common/eventcheck.h @@ -0,0 +1,64 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +#ifndef EVENTCHECK_H +#define EVENTCHECK_H + +#include "common_types.h" +#include "cfe_evs.h" + +#include "utassert.h" +#include "uttest.h" +#include "utstubs.h" + +/* + * Unit test check event hook information + */ +typedef struct +{ + uint16 ExpectedEvent; + uint32 MatchCount; + const char *ExpectedFormat; +} UT_CheckEvent_t; + +/* Macro to get expected event name */ +#define UT_CHECKEVENT_SETUP(Evt, ExpectedEvent, ExpectedFormat) \ + UT_CheckEvent_Setup_Impl(Evt, ExpectedEvent, #ExpectedEvent, ExpectedFormat) + +/* + * Helper function to set up for event checking + * This attaches the hook function to CFE_EVS_SendEvent + */ +void UT_CheckEvent_Setup_Impl(UT_CheckEvent_t *Evt, uint16 ExpectedEvent, const char *EventName, + const char *ExpectedFormat); + +#endif diff --git a/fsw/unit-test/common/setup.c b/fsw/unit-test/common/setup.c new file mode 100644 index 0000000..9d6011e --- /dev/null +++ b/fsw/unit-test/common/setup.c @@ -0,0 +1,55 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ +#include "common_types.h" + +#include "setup.h" + +#include "utassert.h" +#include "uttest.h" +#include "utstubs.h" + +/* + * Setup function prior to every test + */ +void Sample_UT_Setup(void) +{ + UT_ResetState(0); +} + +/* + * Teardown function after every test + */ +void Sample_UT_TearDown(void) {} diff --git a/fsw/unit-test/common/setup.h b/fsw/unit-test/common/setup.h new file mode 100644 index 0000000..a1ed825 --- /dev/null +++ b/fsw/unit-test/common/setup.h @@ -0,0 +1,45 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +#ifndef SETUP_H +#define SETUP_H + +#include "common_types.h" + +#include "utassert.h" +#include "uttest.h" +#include "utstubs.h" + +void Sample_UT_Setup(void); +void Sample_UT_TearDown(void); + +#endif diff --git a/fsw/unit-test/coveragetest/coveragetest_sample_app.c b/fsw/unit-test/coveragetest/coveragetest_sample_app.c new file mode 100644 index 0000000..b6d31c4 --- /dev/null +++ b/fsw/unit-test/coveragetest/coveragetest_sample_app.c @@ -0,0 +1,185 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** File: coveragetest_sample_app.c +** +** Purpose: +** Coverage Unit Test cases for the Sample Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the Sample application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ +#include "sample_lib.h" /* For SAMPLE_LIB_Function */ +#include "sample_app_coveragetest_common.h" + +/* +********************************************************************************** +** TEST CASE FUNCTIONS +********************************************************************************** +*/ + +void Test_SAMPLE_APP_Main(void) +{ + CFE_SB_MsgId_t MsgId = CFE_SB_INVALID_MSG_ID; + + /* + * Test Case For: + * void SAMPLE_APP_Main( void ) + */ + + UT_CheckEvent_t EventTest; + + /* + * SAMPLE_APP_Main does not return a value, + * but it has several internal decision points + * that need to be exercised here. + * + * First call it in "nominal" mode where all + * dependent calls should be successful by default. + */ + SAMPLE_APP_Main(); + + /* + * Confirm that CFE_ES_ExitApp() was called at the end of execution + */ + UtAssert_STUB_COUNT(CFE_ES_ExitApp, 1); + + /* + * Now set up individual cases for each of the error paths. + * The first is for SAMPLE_APP_Init(). As this is in the same + * code unit, it is not a stub where the return code can be + * easily set. In order to get this to fail, an underlying + * call needs to fail, and the error gets propagated through. + * The call to CFE_EVS_Register is the first opportunity. + * Any identifiable (non-success) return code should work. + */ + UT_SetDeferredRetcode(UT_KEY(CFE_EVS_Register), 1, CFE_EVS_INVALID_PARAMETER); + + /* + * Just call the function again. It does not return + * the value, so there is nothing to test for here directly. + * However, it should show up in the coverage report that + * the SAMPLE_APP_Init() failure path was taken. + */ + SAMPLE_APP_Main(); + + /* + * This can validate that the internal "RunStatus" was + * set to CFE_ES_RunStatus_APP_ERROR, by querying the struct directly. + */ + UtAssert_UINT32_EQ(SAMPLE_APP_Data.RunStatus, CFE_ES_RunStatus_APP_ERROR); + + /* + * Note that CFE_ES_RunLoop returns a boolean value, + * so in order to exercise the internal "while" loop, + * it needs to return TRUE. But this also needs to return + * FALSE in order to get out of the loop, otherwise + * it will stay there infinitely. + * + * The deferred retcode will accomplish this. + */ + UT_SetDeferredRetcode(UT_KEY(CFE_ES_RunLoop), 1, true); + + /* Set up buffer for command processing */ + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &MsgId, sizeof(MsgId), false); + + /* + * Invoke again + */ + SAMPLE_APP_Main(); + + /* + * Confirm that CFE_SB_ReceiveBuffer() (inside the loop) was called + */ + UtAssert_STUB_COUNT(CFE_SB_ReceiveBuffer, 1); + + /* + * Now also make the CFE_SB_ReceiveBuffer call fail, + * to exercise that error path. This sends an + * event which can be checked with a hook function. + */ + 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_APP_PIPE_ERR_EID, "SAMPLE APP: SB Pipe Read Error, App Will Exit"); + + /* + * Invoke again + */ + SAMPLE_APP_Main(); + + /* + * Confirm that the event was generated + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +void Test_SAMPLE_APP_Init(void) +{ + /* + * Test Case For: + * CFE_Status_t SAMPLE_APP_Init( void ) + */ + + /* nominal case should return CFE_SUCCESS */ + UtAssert_INT32_EQ(SAMPLE_APP_Init(), CFE_SUCCESS); + + /* + * Trigger a failure for each of the sub-calls, and confirm a write to syslog for + * failure to register with EVS, and that an event is generated for subsequent error paths. + * Note that the stub counts accumulate, because the status is _not_ reset between test cases. + */ + UT_SetDeferredRetcode(UT_KEY(CFE_EVS_Register), 1, CFE_EVS_INVALID_PARAMETER); + UtAssert_INT32_EQ(SAMPLE_APP_Init(), CFE_EVS_INVALID_PARAMETER); + UtAssert_STUB_COUNT(CFE_ES_WriteToSysLog, 1); + + UT_SetDeferredRetcode(UT_KEY(CFE_SB_CreatePipe), 1, CFE_SB_BAD_ARGUMENT); + UtAssert_INT32_EQ(SAMPLE_APP_Init(), CFE_SB_BAD_ARGUMENT); + UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 2); /* 1 from previous nominal case, 1 from this error path */ + + UT_SetDeferredRetcode(UT_KEY(CFE_SB_Subscribe), 1, CFE_SB_BAD_ARGUMENT); + UtAssert_INT32_EQ(SAMPLE_APP_Init(), CFE_SB_BAD_ARGUMENT); + UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 3); /* 1 additional event sent from this error path */ + + UT_SetDeferredRetcode(UT_KEY(CFE_SB_Subscribe), 2, CFE_SB_BAD_ARGUMENT); + UtAssert_INT32_EQ(SAMPLE_APP_Init(), CFE_SB_BAD_ARGUMENT); + UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 4); /* 1 additional event sent from this error path */ + + UT_SetDeferredRetcode(UT_KEY(CFE_TBL_Register), 1, CFE_TBL_ERR_INVALID_OPTIONS); + UtAssert_INT32_EQ(SAMPLE_APP_Init(), CFE_TBL_ERR_INVALID_OPTIONS); + UtAssert_STUB_COUNT(CFE_EVS_SendEvent, 6); /* 1 from table registration error, 1 from successful init event */ +} + +/* + * Register the test cases to execute with the unit test tool + */ +void UtTest_Setup(void) +{ + ADD_TEST(SAMPLE_APP_Main); + ADD_TEST(SAMPLE_APP_Init); +} diff --git a/fsw/unit-test/coveragetest/coveragetest_sample_app_cmds.c b/fsw/unit-test/coveragetest/coveragetest_sample_app_cmds.c new file mode 100644 index 0000000..036dbc0 --- /dev/null +++ b/fsw/unit-test/coveragetest/coveragetest_sample_app_cmds.c @@ -0,0 +1,219 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** File: coveragetest_sample_app.c +** +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ + +#include "sample_lib.h" /* For SAMPLE_LIB_Function */ +#include "sample_app_coveragetest_common.h" +#include "sample_app.h" +#include "sample_app_dispatch.h" +#include "sample_app_cmds.h" + +/* +********************************************************************************** +** TEST CASE FUNCTIONS +********************************************************************************** +*/ + +void Test_SAMPLE_APP_ReportHousekeeping(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_ReportHousekeeping( const CFE_SB_CmdHdr_t *Msg ) + */ + CFE_MSG_Message_t *MsgSend; + CFE_MSG_Message_t *MsgTimestamp; + + /* Set up to capture send message address */ + UT_SetDataBuffer(UT_KEY(CFE_SB_TransmitMsg), &MsgSend, sizeof(MsgSend), false); + + /* Set up to capture timestamp message address */ + UT_SetDataBuffer(UT_KEY(CFE_SB_TimeStampMsg), &MsgTimestamp, sizeof(MsgTimestamp), false); + + /* Call unit under test, NULL pointer confirms command access is through APIs */ + SAMPLE_APP_SendHkCmd(NULL); + + /* Confirm message sent*/ + UtAssert_STUB_COUNT(CFE_SB_TransmitMsg, 1); + UtAssert_ADDRESS_EQ(MsgSend, &SAMPLE_APP_Data.HkTlm); + + /* Confirm timestamp msg address */ + UtAssert_STUB_COUNT(CFE_SB_TimeStampMsg, 1); + UtAssert_ADDRESS_EQ(MsgTimestamp, &SAMPLE_APP_Data.HkTlm); + + /* + * Confirm that the CFE_TBL_Manage() call was done + */ + UtAssert_STUB_COUNT(CFE_TBL_Manage, 1); +} + +void Test_SAMPLE_APP_NoopCmd(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_NoopCmd( const SAMPLE_APP_Noop_t *Msg ) + */ + SAMPLE_APP_NoopCmd_t TestMsg; + UT_CheckEvent_t EventTest; + + memset(&TestMsg, 0, sizeof(TestMsg)); + + /* test dispatch of NOOP */ + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_NOOP_INF_EID, NULL); + + UtAssert_INT32_EQ(SAMPLE_APP_NoopCmd(&TestMsg), CFE_SUCCESS); + + /* + * Confirm that the event was generated + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +void Test_SAMPLE_APP_ResetCountersCmd(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_ResetCounters( const SAMPLE_APP_ResetCounters_t *Msg ) + */ + SAMPLE_APP_ResetCountersCmd_t TestMsg; + UT_CheckEvent_t EventTest; + + memset(&TestMsg, 0, sizeof(TestMsg)); + + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_RESET_INF_EID, "SAMPLE: RESET command"); + + UtAssert_INT32_EQ(SAMPLE_APP_ResetCountersCmd(&TestMsg), CFE_SUCCESS); + + /* + * Confirm that the event was generated + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +void Test_SAMPLE_APP_ProcessCmd(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_ProcessCmd( const SAMPLE_APP_ProcessCmd_t *Msg ) + */ + SAMPLE_APP_ProcessCmd_t TestMsg; + SAMPLE_APP_ExampleTable_t TestTblData; + void * TblPtr = &TestTblData; + + memset(&TestTblData, 0, sizeof(TestTblData)); + memset(&TestMsg, 0, sizeof(TestMsg)); + + /* Provide some table data for the SAMPLE_APP_Process() function to use */ + TestTblData.Int1 = 40; + TestTblData.Int2 = 50; + UT_SetDataBuffer(UT_KEY(CFE_TBL_GetAddress), &TblPtr, sizeof(TblPtr), false); + UtAssert_INT32_EQ(SAMPLE_APP_ProcessCmd(&TestMsg), CFE_SUCCESS); + + /* + * This only needs to account for the call to CFE_ES_WriteToSysLog() directly + * invoked by the unit under test. Note that in this build environment, the + * SAMPLE_APP_GetCrc() function is a stub. + */ + UtAssert_STUB_COUNT(CFE_ES_WriteToSysLog, 1); + + /* + * Confirm that the CFE_TBL_GetAddress() call was done + */ + UtAssert_STUB_COUNT(CFE_TBL_GetAddress, 1); + + /* + * Confirm that the SAMPLE_LIB_Function() call was done + * NOTE: This stub is provided by the sample_lib library + */ + UtAssert_STUB_COUNT(SAMPLE_LIB_Function, 1); + + /* + * Configure the CFE_TBL_GetAddress function to return an error. + * Exercise the error return path. + * Error at this point should add only one additional call to + * CFE_ES_WriteToSysLog() through the CFE_TBL_GetAddress() error path. + */ + UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_GetAddress), CFE_TBL_ERR_UNREGISTERED); + UtAssert_INT32_EQ(SAMPLE_APP_ProcessCmd(&TestMsg), CFE_TBL_ERR_UNREGISTERED); + UtAssert_STUB_COUNT(CFE_ES_WriteToSysLog, 2); + + /* + * Configure CFE_TBL_ReleaseAddress() to return an error, exercising the + * error return path. + * Confirm two additional calls to CFE_ES_WriteToSysLog() - one + * reporting the table values, and one through the CFE_TBL_ReleaseAddress() + * error path. + */ + UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_GetAddress), CFE_SUCCESS); + UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_ReleaseAddress), CFE_TBL_ERR_NO_ACCESS); + UtAssert_INT32_EQ(SAMPLE_APP_ProcessCmd(&TestMsg), CFE_TBL_ERR_NO_ACCESS); + UtAssert_STUB_COUNT(CFE_ES_WriteToSysLog, 4); +} + +void Test_SAMPLE_APP_DisplayParamCmd(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_DisplayParamCmd( const SAMPLE_APP_DisplayParamCmd_t *Msg ) + */ + SAMPLE_APP_DisplayParamCmd_t TestMsg; + UT_CheckEvent_t EventTest; + + memset(&TestMsg, 0, sizeof(TestMsg)); + + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_VALUE_INF_EID, "SAMPLE_APP: ValU32=%lu, ValI16=%d, ValStr=%s"); + TestMsg.Payload.ValU32 = 10; + TestMsg.Payload.ValI16 = -4; + snprintf(TestMsg.Payload.ValStr, sizeof(TestMsg.Payload.ValStr), "Hello"); + + UtAssert_INT32_EQ(SAMPLE_APP_DisplayParamCmd(&TestMsg), CFE_SUCCESS); + /* + * Confirm that the event was generated + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +/* + * Register the test cases to execute with the unit test tool + */ +void UtTest_Setup(void) +{ + ADD_TEST(SAMPLE_APP_ReportHousekeeping); + ADD_TEST(SAMPLE_APP_NoopCmd); + ADD_TEST(SAMPLE_APP_ResetCountersCmd); + ADD_TEST(SAMPLE_APP_ProcessCmd); + ADD_TEST(SAMPLE_APP_DisplayParamCmd); +} diff --git a/fsw/unit-test/coveragetest/coveragetest_sample_app_dispatch.c b/fsw/unit-test/coveragetest/coveragetest_sample_app_dispatch.c new file mode 100644 index 0000000..a968286 --- /dev/null +++ b/fsw/unit-test/coveragetest/coveragetest_sample_app_dispatch.c @@ -0,0 +1,280 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ + +#include "sample_app_coveragetest_common.h" +#include "sample_app.h" +#include "sample_app_dispatch.h" +#include "sample_app_cmds.h" + +/* +********************************************************************************** +** TEST CASE FUNCTIONS +********************************************************************************** +*/ + +void Test_SAMPLE_APP_TaskPipe(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_TaskPipe + */ + /* a buffer large enough for any command message */ + union + { + CFE_SB_Buffer_t SBBuf; + SAMPLE_APP_NoopCmd_t Noop; + } TestMsg; + CFE_SB_MsgId_t TestMsgId; + CFE_MSG_FcnCode_t FcnCode; + size_t MsgSize; + UT_CheckEvent_t EventTest; + + memset(&TestMsg, 0, sizeof(TestMsg)); + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_MID_ERR_EID, "SAMPLE: invalid command packet,MID = 0x%x"); + + /* + * The CFE_MSG_GetMsgId() stub uses a data buffer to hold the + * message ID values to return. + */ + TestMsgId = CFE_SB_ValueToMsgId(SAMPLE_APP_CMD_MID); + FcnCode = SAMPLE_APP_NOOP_CC; + MsgSize = 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), &MsgSize, sizeof(MsgSize), false); + SAMPLE_APP_TaskPipe(&TestMsg.SBBuf); + + TestMsgId = CFE_SB_ValueToMsgId(SAMPLE_APP_SEND_HK_MID); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false); + SAMPLE_APP_TaskPipe(&TestMsg.SBBuf); + + /* invalid message id */ + TestMsgId = CFE_SB_INVALID_MSG_ID; + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &TestMsgId, sizeof(TestMsgId), false); + SAMPLE_APP_TaskPipe(&TestMsg.SBBuf); + + /* + * Confirm that the event was generated only _once_ + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +void Test_SAMPLE_APP_ProcessGroundCommand(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_ProcessGroundCommand + */ + CFE_MSG_FcnCode_t FcnCode; + size_t Size; + CFE_SB_MsgId_t MsgId = CFE_SB_ValueToMsgId(1); + + /* a buffer large enough for any command message */ + union + { + CFE_SB_Buffer_t SBBuf; + SAMPLE_APP_NoopCmd_t Noop; + SAMPLE_APP_ResetCountersCmd_t Reset; + SAMPLE_APP_ProcessCmd_t Process; + SAMPLE_APP_DisplayParamCmd_t DisplayParam; + } TestMsg; + UT_CheckEvent_t EventTest; + + memset(&TestMsg, 0, sizeof(TestMsg)); + + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_CMD_LEN_ERR_EID, + "Invalid Msg length: ID = 0x%X, CC = %u, Len = %u, Expected = %u"); + + /* + * call with each of the supported command codes + * The CFE_MSG_GetFcnCode stub allows the code to be + * set to whatever is needed. There is no return + * value here and the actual implementation of these + * commands have separate test cases, so this just + * needs to exercise the "switch" statement. + */ + + /* test dispatch of NOOP */ + FcnCode = SAMPLE_APP_NOOP_CC; + Size = sizeof(TestMsg.Noop); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_NoopCmd, 1); + + FcnCode = SAMPLE_APP_NOOP_CC; + Size = sizeof(TestMsg.Noop) - 1; + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &MsgId, sizeof(MsgId), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_NoopCmd, 1); + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); + + /* test dispatch of RESET */ + FcnCode = SAMPLE_APP_RESET_COUNTERS_CC; + Size = sizeof(TestMsg.Reset); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_ResetCountersCmd, 1); + + FcnCode = SAMPLE_APP_RESET_COUNTERS_CC; + Size = sizeof(TestMsg.Reset) - 1; + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &MsgId, sizeof(MsgId), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_ResetCountersCmd, 1); + UtAssert_UINT32_EQ(EventTest.MatchCount, 2); + + /* test dispatch of PROCESS */ + /* note this will end up calling SAMPLE_APP_Process(), and as such it needs to + * avoid dereferencing a table which does not exist. */ + FcnCode = SAMPLE_APP_PROCESS_CC; + Size = sizeof(TestMsg.Process); + UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_GetAddress), CFE_TBL_ERR_UNREGISTERED); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_ProcessCmd, 1); + + FcnCode = SAMPLE_APP_PROCESS_CC; + Size = sizeof(TestMsg.Process) - 1; + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &MsgId, sizeof(MsgId), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_ProcessCmd, 1); + UtAssert_UINT32_EQ(EventTest.MatchCount, 3); + + /* test dispatch of DISPLAY_PARAM */ + FcnCode = SAMPLE_APP_DISPLAY_PARAM_CC; + Size = sizeof(TestMsg.DisplayParam); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_DisplayParamCmd, 1); + + FcnCode = SAMPLE_APP_DISPLAY_PARAM_CC; + Size = sizeof(TestMsg.DisplayParam) - 1; + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &Size, sizeof(Size), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetMsgId), &MsgId, sizeof(MsgId), false); + + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + UtAssert_STUB_COUNT(SAMPLE_APP_DisplayParamCmd, 1); + UtAssert_UINT32_EQ(EventTest.MatchCount, 4); + + /* test an invalid CC */ + FcnCode = 1000; + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetFcnCode), &FcnCode, sizeof(FcnCode), false); + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_CC_ERR_EID, "Invalid ground command code: CC = %d"); + SAMPLE_APP_ProcessGroundCommand(&TestMsg.SBBuf); + + /* + * Confirm that the event was generated only _once_ + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +void Test_SAMPLE_APP_VerifyCmdLength(void) +{ + /* + * Test Case For: + * bool SAMPLE_APP_VerifyCmdLength + */ + UT_CheckEvent_t EventTest; + size_t size = 1; + CFE_MSG_FcnCode_t fcncode = 2; + CFE_SB_MsgId_t msgid = CFE_SB_ValueToMsgId(3); + + /* + * test a match case + */ + UT_SetDataBuffer(UT_KEY(CFE_MSG_GetSize), &size, sizeof(size), false); + UT_CHECKEVENT_SETUP(&EventTest, SAMPLE_APP_CMD_LEN_ERR_EID, + "Invalid Msg length: ID = 0x%X, CC = %u, Len = %u, Expected = %u"); + + SAMPLE_APP_VerifyCmdLength(NULL, size); + + /* + * Confirm that the event was NOT generated + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 0); + + /* + * test a mismatch case + */ + 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); + SAMPLE_APP_VerifyCmdLength(NULL, size + 1); + + /* + * Confirm that the event WAS generated + */ + UtAssert_UINT32_EQ(EventTest.MatchCount, 1); +} + +/* + * Register the test cases to execute with the unit test tool + */ +void UtTest_Setup(void) +{ + ADD_TEST(SAMPLE_APP_TaskPipe); + ADD_TEST(SAMPLE_APP_ProcessGroundCommand); + ADD_TEST(SAMPLE_APP_VerifyCmdLength); +} diff --git a/fsw/unit-test/coveragetest/coveragetest_sample_app_eds_dispatch.c b/fsw/unit-test/coveragetest/coveragetest_sample_app_eds_dispatch.c new file mode 100644 index 0000000..1f06fa9 --- /dev/null +++ b/fsw/unit-test/coveragetest/coveragetest_sample_app_eds_dispatch.c @@ -0,0 +1,62 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** Purpose: +** Coverage Unit Test cases for the SAMPLE Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the SAMPLE application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ + +#include "sample_app_coveragetest_common.h" +#include "sample_app.h" +#include "sample_app_dispatch.h" +#include "sample_app_cmds.h" + +/* +********************************************************************************** +** TEST CASE FUNCTIONS +********************************************************************************** +*/ + +void Test_SAMPLE_APP_TaskPipe(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_TaskPipe + */ +} + +/* + * Register the test cases to execute with the unit test tool + */ +void UtTest_Setup(void) +{ + ADD_TEST(SAMPLE_APP_TaskPipe); +} diff --git a/fsw/unit-test/coveragetest/coveragetest_sample_app_utils.c b/fsw/unit-test/coveragetest/coveragetest_sample_app_utils.c new file mode 100644 index 0000000..6c81011 --- /dev/null +++ b/fsw/unit-test/coveragetest/coveragetest_sample_app_utils.c @@ -0,0 +1,97 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/* +** File: coveragetest_sample_app.c +** +** Purpose: +** Coverage Unit Test cases for the Sample Application +** +** Notes: +** This implements various test cases to exercise all code +** paths through all functions defined in the Sample application. +** +** It is primarily focused at providing examples of the various +** stub configurations, hook functions, and wrapper calls that +** are often needed when coercing certain code paths through +** complex functions. +*/ + +/* + * Includes + */ +#include "sample_lib.h" /* For SAMPLE_LIB_Function */ +#include "sample_app_coveragetest_common.h" + +/* +********************************************************************************** +** TEST CASE FUNCTIONS +********************************************************************************** +*/ + +void Test_SAMPLE_APP_TblValidationFunc(void) +{ + /* + * Test Case For: + * CFE_Status_t SAMPLE_APP_TblValidationFunc( void *TblData ) + */ + SAMPLE_APP_ExampleTable_t TestTblData; + + memset(&TestTblData, 0, sizeof(TestTblData)); + + /* nominal case (0) should succeed */ + UtAssert_INT32_EQ(SAMPLE_APP_TblValidationFunc(&TestTblData), CFE_SUCCESS); + + /* error case should return SAMPLE_APP_TABLE_OUT_OF_RANGE_ERR_CODE */ + TestTblData.Int1 = 1 + SAMPLE_APP_TBL_ELEMENT_1_MAX; + UtAssert_INT32_EQ(SAMPLE_APP_TblValidationFunc(&TestTblData), SAMPLE_APP_TABLE_OUT_OF_RANGE_ERR_CODE); +} + +void Test_SAMPLE_APP_GetCrc(void) +{ + /* + * Test Case For: + * void SAMPLE_APP_GetCrc( const char *TableName ) + */ + + /* + * The only branch point here is CFE_TBL_GetInfo() + * + * Either way this function just does a write to syslog, + * and it is the same in both cases, just with + * a different message. This could actually verify + * the message using a hook function, if desired. + */ + + UT_SetDefaultReturnValue(UT_KEY(CFE_TBL_GetInfo), CFE_TBL_ERR_INVALID_NAME); + SAMPLE_APP_GetCrc("UT"); + UtAssert_STUB_COUNT(CFE_ES_WriteToSysLog, 1); + + UT_ClearDefaultReturnValue(UT_KEY(CFE_TBL_GetInfo)); + SAMPLE_APP_GetCrc("UT"); + UtAssert_STUB_COUNT(CFE_ES_WriteToSysLog, 2); +} + +/* + * Register the test cases to execute with the unit test tool + */ +void UtTest_Setup(void) +{ + ADD_TEST(SAMPLE_APP_TblValidationFunc); + ADD_TEST(SAMPLE_APP_GetCrc); +} diff --git a/fsw/unit-test/coveragetest/sample_app_coveragetest_common.h b/fsw/unit-test/coveragetest/sample_app_coveragetest_common.h new file mode 100644 index 0000000..792ee5b --- /dev/null +++ b/fsw/unit-test/coveragetest/sample_app_coveragetest_common.h @@ -0,0 +1,54 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/** + * @file + * + * Common definitions for all sample_app coverage tests + */ + +#ifndef SAMPLE_APP_COVERAGETEST_COMMON_H +#define SAMPLE_APP_COVERAGETEST_COMMON_H + +/* + * Includes + */ + +#include "utassert.h" +#include "uttest.h" +#include "utstubs.h" + +#include "setup.h" +#include "eventcheck.h" + +#include "cfe.h" +#include "sample_app_eventids.h" +#include "sample_app.h" +#include "sample_app_dispatch.h" +#include "sample_app_cmds.h" +#include "sample_app_utils.h" +#include "sample_app_msgids.h" +#include "sample_app_msg.h" +#include "sample_app_tbl.h" + +/* + * Macro to add a test case to the list of tests to execute + */ +#define ADD_TEST(test) UtTest_Add((Test_##test), Sample_UT_Setup, Sample_UT_TearDown, #test) + +#endif /* SAMPLE_APP_COVERAGETEST_COMMON_H */ diff --git a/fsw/unit-test/stubs/sample_app_cmds_stubs.c b/fsw/unit-test/stubs/sample_app_cmds_stubs.c new file mode 100644 index 0000000..bfa80ce --- /dev/null +++ b/fsw/unit-test/stubs/sample_app_cmds_stubs.c @@ -0,0 +1,106 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/** + * @file + * + * Auto-Generated stub implementations for functions defined in sample_app_cmds header + */ + +#include "sample_app_cmds.h" +#include "utgenstub.h" + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_DisplayParamCmd() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_DisplayParamCmd(const SAMPLE_APP_DisplayParamCmd_t *Msg) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_DisplayParamCmd, CFE_Status_t); + + UT_GenStub_AddParam(SAMPLE_APP_DisplayParamCmd, const SAMPLE_APP_DisplayParamCmd_t *, Msg); + + UT_GenStub_Execute(SAMPLE_APP_DisplayParamCmd, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_DisplayParamCmd, CFE_Status_t); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_NoopCmd() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_NoopCmd(const SAMPLE_APP_NoopCmd_t *Msg) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_NoopCmd, CFE_Status_t); + + UT_GenStub_AddParam(SAMPLE_APP_NoopCmd, const SAMPLE_APP_NoopCmd_t *, Msg); + + UT_GenStub_Execute(SAMPLE_APP_NoopCmd, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_NoopCmd, CFE_Status_t); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_ProcessCmd() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_ProcessCmd(const SAMPLE_APP_ProcessCmd_t *Msg) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_ProcessCmd, CFE_Status_t); + + UT_GenStub_AddParam(SAMPLE_APP_ProcessCmd, const SAMPLE_APP_ProcessCmd_t *, Msg); + + UT_GenStub_Execute(SAMPLE_APP_ProcessCmd, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_ProcessCmd, CFE_Status_t); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_ResetCountersCmd() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_ResetCountersCmd(const SAMPLE_APP_ResetCountersCmd_t *Msg) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_ResetCountersCmd, CFE_Status_t); + + UT_GenStub_AddParam(SAMPLE_APP_ResetCountersCmd, const SAMPLE_APP_ResetCountersCmd_t *, Msg); + + UT_GenStub_Execute(SAMPLE_APP_ResetCountersCmd, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_ResetCountersCmd, CFE_Status_t); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_SendHkCmd() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_SendHkCmd(const SAMPLE_APP_SendHkCmd_t *Msg) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_SendHkCmd, CFE_Status_t); + + UT_GenStub_AddParam(SAMPLE_APP_SendHkCmd, const SAMPLE_APP_SendHkCmd_t *, Msg); + + UT_GenStub_Execute(SAMPLE_APP_SendHkCmd, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_SendHkCmd, CFE_Status_t); +} diff --git a/fsw/unit-test/stubs/sample_app_dispatch_stubs.c b/fsw/unit-test/stubs/sample_app_dispatch_stubs.c new file mode 100644 index 0000000..7d956f4 --- /dev/null +++ b/fsw/unit-test/stubs/sample_app_dispatch_stubs.c @@ -0,0 +1,67 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/** + * @file + * + * Auto-Generated stub implementations for functions defined in sample_app_dispatch header + */ + +#include "sample_app_dispatch.h" +#include "utgenstub.h" + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_ProcessGroundCommand() + * ---------------------------------------------------- + */ +void SAMPLE_APP_ProcessGroundCommand(const CFE_SB_Buffer_t *SBBufPtr) +{ + UT_GenStub_AddParam(SAMPLE_APP_ProcessGroundCommand, const CFE_SB_Buffer_t *, SBBufPtr); + + UT_GenStub_Execute(SAMPLE_APP_ProcessGroundCommand, Basic, NULL); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_TaskPipe() + * ---------------------------------------------------- + */ +void SAMPLE_APP_TaskPipe(const CFE_SB_Buffer_t *SBBufPtr) +{ + UT_GenStub_AddParam(SAMPLE_APP_TaskPipe, const CFE_SB_Buffer_t *, SBBufPtr); + + UT_GenStub_Execute(SAMPLE_APP_TaskPipe, Basic, NULL); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_VerifyCmdLength() + * ---------------------------------------------------- + */ +bool SAMPLE_APP_VerifyCmdLength(const CFE_MSG_Message_t *MsgPtr, size_t ExpectedLength) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_VerifyCmdLength, bool); + + UT_GenStub_AddParam(SAMPLE_APP_VerifyCmdLength, const CFE_MSG_Message_t *, MsgPtr); + UT_GenStub_AddParam(SAMPLE_APP_VerifyCmdLength, size_t, ExpectedLength); + + UT_GenStub_Execute(SAMPLE_APP_VerifyCmdLength, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_VerifyCmdLength, bool); +} diff --git a/fsw/unit-test/stubs/sample_app_global_stubs.c b/fsw/unit-test/stubs/sample_app_global_stubs.c new file mode 100644 index 0000000..c80a9d0 --- /dev/null +++ b/fsw/unit-test/stubs/sample_app_global_stubs.c @@ -0,0 +1,27 @@ +/************************************************************************ + * NASA Docket No. GSC-18,920-1, and identified as “Core Flight + * System (cFS) Health & Safety (HS) Application version 2.4.1” + * + * Copyright (c) 2021 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +#include "sample_app.h" + +/* UT includes */ +#include "uttest.h" +#include "utassert.h" +#include "utstubs.h" + +SAMPLE_APP_Data_t SAMPLE_APP_Data; diff --git a/fsw/unit-test/stubs/sample_app_stubs.c b/fsw/unit-test/stubs/sample_app_stubs.c new file mode 100644 index 0000000..16977fd --- /dev/null +++ b/fsw/unit-test/stubs/sample_app_stubs.c @@ -0,0 +1,51 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/** + * @file + * + * Auto-Generated stub implementations for functions defined in sample_app header + */ + +#include "sample_app.h" +#include "utgenstub.h" + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_Init() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_Init(void) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_Init, CFE_Status_t); + + UT_GenStub_Execute(SAMPLE_APP_Init, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_Init, CFE_Status_t); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_Main() + * ---------------------------------------------------- + */ +void SAMPLE_APP_Main(void) +{ + + UT_GenStub_Execute(SAMPLE_APP_Main, Basic, NULL); +} diff --git a/fsw/unit-test/stubs/sample_app_utils_stubs.c b/fsw/unit-test/stubs/sample_app_utils_stubs.c new file mode 100644 index 0000000..88e52c1 --- /dev/null +++ b/fsw/unit-test/stubs/sample_app_utils_stubs.c @@ -0,0 +1,54 @@ +/************************************************************************ + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” + * + * Copyright (c) 2020 United States Government as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ************************************************************************/ + +/** + * @file + * + * Auto-Generated stub implementations for functions defined in sample_app_utils header + */ + +#include "sample_app_utils.h" +#include "utgenstub.h" + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_GetCrc() + * ---------------------------------------------------- + */ +void SAMPLE_APP_GetCrc(const char *TableName) +{ + UT_GenStub_AddParam(SAMPLE_APP_GetCrc, const char *, TableName); + + UT_GenStub_Execute(SAMPLE_APP_GetCrc, Basic, NULL); +} + +/* + * ---------------------------------------------------- + * Generated stub function for SAMPLE_APP_TblValidationFunc() + * ---------------------------------------------------- + */ +CFE_Status_t SAMPLE_APP_TblValidationFunc(void *TblData) +{ + UT_GenStub_SetupReturnBuffer(SAMPLE_APP_TblValidationFunc, CFE_Status_t); + + UT_GenStub_AddParam(SAMPLE_APP_TblValidationFunc, void *, TblData); + + UT_GenStub_Execute(SAMPLE_APP_TblValidationFunc, Basic, NULL); + + return UT_GenStub_GetReturnValue(SAMPLE_APP_TblValidationFunc, CFE_Status_t); +}