Skip to content

Commit

Permalink
[nasa/nos3#176] Baseline unit-tests from NASA/sample_app to start from;
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucas9 committed Jul 23, 2024
1 parent bf8dd3d commit a3cbee2
Show file tree
Hide file tree
Showing 17 changed files with 1,571 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fsw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
69 changes: 69 additions & 0 deletions fsw/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
128 changes: 128 additions & 0 deletions fsw/unit-test/common/eventcheck.c
Original file line number Diff line number Diff line change
@@ -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);
}
64 changes: 64 additions & 0 deletions fsw/unit-test/common/eventcheck.h
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions fsw/unit-test/common/setup.c
Original file line number Diff line number Diff line change
@@ -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) {}
45 changes: 45 additions & 0 deletions fsw/unit-test/common/setup.h
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit a3cbee2

Please sign in to comment.