From f18a935e78653bda26b86b342a4972d770692cef Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Tue, 16 Jun 2020 16:04:59 -0400 Subject: [PATCH] Fix #778, Add CFE_Assert library and test module Provides ability to load UT assert as a CFE app, and an example of using this to test some basic CFE ES functions. --- cmake/arch_build.cmake | 1 - cmake/mission_defaults.cmake | 7 ++ modules/cfe_assert/CMakeLists.txt | 12 ++ modules/cfe_assert/inc/cfe_assert.h | 63 +++++++++++ modules/cfe_assert/src/cfe_assert_io.c | 137 +++++++++++++++++++++++ modules/cfe_assert/src/cfe_assert_main.c | 135 ++++++++++++++++++++++ modules/cfe_test/CMakeLists.txt | 8 ++ modules/cfe_test/src/cfe_test.c | 45 ++++++++ modules/cfe_test/src/cfe_test.h | 45 ++++++++ modules/cfe_test/src/es_test.c | 46 ++++++++ 10 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 modules/cfe_assert/CMakeLists.txt create mode 100644 modules/cfe_assert/inc/cfe_assert.h create mode 100644 modules/cfe_assert/src/cfe_assert_io.c create mode 100644 modules/cfe_assert/src/cfe_assert_main.c create mode 100644 modules/cfe_test/CMakeLists.txt create mode 100644 modules/cfe_test/src/cfe_test.c create mode 100644 modules/cfe_test/src/cfe_test.h create mode 100644 modules/cfe_test/src/es_test.c diff --git a/cmake/arch_build.cmake b/cmake/arch_build.cmake index db99d03e9..d3b98d4d3 100644 --- a/cmake/arch_build.cmake +++ b/cmake/arch_build.cmake @@ -215,7 +215,6 @@ function(add_unit_test_exe UT_NAME UT_SRCS) add_test(${UT_NAME} ${UT_NAME}) endfunction(add_unit_test_exe) - ################################################################## # # FUNCTION: cfe_exec_do_install diff --git a/cmake/mission_defaults.cmake b/cmake/mission_defaults.cmake index 8f49d54d5..59cf835a4 100644 --- a/cmake/mission_defaults.cmake +++ b/cmake/mission_defaults.cmake @@ -45,3 +45,10 @@ set(MISSION_MODULE_SEARCH_PATH set(cfe-core_SEARCH_PATH "cfe/fsw") set(osal_SEARCH_PATH ".") set(psp_SEARCH_PATH ".") + +# If ENABLE_UNIT_TEST is enabled, then include the cfe_assert library in +# all targets. This can still be overridden in targets.cmake. +if (ENABLE_UNIT_TESTS) + list(APPEND MISSION_GLOBAL_APPLIST cfe_assert cfe_test) +endif (ENABLE_UNIT_TESTS) + diff --git a/modules/cfe_assert/CMakeLists.txt b/modules/cfe_assert/CMakeLists.txt new file mode 100644 index 000000000..1b4602170 --- /dev/null +++ b/modules/cfe_assert/CMakeLists.txt @@ -0,0 +1,12 @@ +project(CFE_ASSERT C) + +include_directories("${CFE_ASSERT_SOURCE_DIR}/inc") +include_directories("${UT_ASSERT_SOURCE_DIR}/inc") + +# Create the app module +add_cfe_app(cfe_assert + src/cfe_assert_io.c + src/cfe_assert_main.c + $ +) + diff --git a/modules/cfe_assert/inc/cfe_assert.h b/modules/cfe_assert/inc/cfe_assert.h new file mode 100644 index 000000000..8dde70a4f --- /dev/null +++ b/modules/cfe_assert/inc/cfe_assert.h @@ -0,0 +1,63 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-2019 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: cfe_assert.h +** +** Purpose: +** Specification for the CFE assert (UT assert wrapper) functions. +** +*************************************************************************/ +#ifndef cfe_assert_h_ +#define cfe_assert_h_ + +/************************************************************************ +** Includes +*************************************************************************/ +#include + +/************************************************************************ +** Type Definitions +*************************************************************************/ + +/************************************************************************* +** Exported Functions +*************************************************************************/ + +/************************************************************************/ +/** \brief Application Entry Point Function +** +** \par Description +** This function should be specified in the cfe_es_startup.scr file +** as part of starting this application. +** +** \par Assumptions, External Events, and Notes: +** None +** +** \return Execution status, see \ref CFEReturnCodes +** +** +*************************************************************************/ +void CFE_Assert_AppMain(void); + +#endif /* cfe_assert_h_ */ + +/************************/ +/* End of File Comment */ +/************************/ diff --git a/modules/cfe_assert/src/cfe_assert_io.c b/modules/cfe_assert/src/cfe_assert_io.c new file mode 100644 index 000000000..85fe44932 --- /dev/null +++ b/modules/cfe_assert/src/cfe_assert_io.c @@ -0,0 +1,137 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-2019 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: cfe_assert_io.c +** +** Purpose: +** Implementation of the CFE assert (UT assert wrapper) functions. +** +*************************************************************************/ + +#include +#include +#include +#include + +#include + +#include "utbsp.h" +#include "uttest.h" + +/* +** Local Variables +*/ +typedef struct +{ + uint32 CurrVerbosity; +} BSP_UT_GlobalData_t; + +BSP_UT_GlobalData_t BSP_UT_Global; + +void UT_BSP_Setup(void) +{ + BSP_UT_Global.CurrVerbosity = (2 << UTASSERT_CASETYPE_PASS) - 1; + UT_BSP_DoText(UTASSERT_CASETYPE_BEGIN, "CFE FUNCTIONAL TEST"); +} + +void UT_BSP_StartTestSegment(uint32 SegmentNumber, const char *SegmentName) +{ + char ReportBuffer[128]; + + snprintf(ReportBuffer, sizeof(ReportBuffer), "%02u %s", (unsigned int)SegmentNumber, SegmentName); + UT_BSP_DoText(UTASSERT_CASETYPE_BEGIN, ReportBuffer); +} + +void UT_BSP_DoText(uint8 MessageType, const char *OutputMessage) +{ + const char *Prefix; + uint32 MsgEnabled = BSP_UT_Global.CurrVerbosity >> MessageType; + + if (MsgEnabled & 1) + { + switch (MessageType) + { + case UTASSERT_CASETYPE_ABORT: + Prefix = "ABORT"; + break; + case UTASSERT_CASETYPE_FAILURE: + Prefix = "FAIL"; + break; + case UTASSERT_CASETYPE_MIR: + Prefix = "MIR"; + break; + case UTASSERT_CASETYPE_TSF: + Prefix = "TSF"; + break; + case UTASSERT_CASETYPE_TTF: + Prefix = "TTF"; + break; + case UTASSERT_CASETYPE_NA: + Prefix = "N/A"; + break; + case UTASSERT_CASETYPE_BEGIN: + Prefix = "BEGIN"; + break; + case UTASSERT_CASETYPE_END: + Prefix = "END"; + break; + case UTASSERT_CASETYPE_PASS: + Prefix = "PASS"; + break; + case UTASSERT_CASETYPE_INFO: + Prefix = "INFO"; + break; + case UTASSERT_CASETYPE_DEBUG: + Prefix = "DEBUG"; + break; + default: + Prefix = "OTHER"; + break; + } + + CFE_ES_WriteToSysLog("[%5s] %s\n", Prefix, OutputMessage); + } + + /* + * If any ABORT (major failure) message is thrown, + * then call a BSP-provided routine to stop the test and possibly dump a core + */ + if (MessageType == UTASSERT_CASETYPE_ABORT) + { + OS_TaskExit(); + } +} + +void UT_BSP_EndTest(const UtAssert_TestCounter_t *TestCounters) +{ + /* + * Only output a "summary" if there is more than one test Segment. + * Otherwise it is a duplicate of the report already given. + */ + if (TestCounters->TestSegmentCount > 1) + { + UtAssert_DoTestSegmentReport("SUMMARY", TestCounters); + } + + CFE_ES_WriteToSysLog("TEST COMPLETE: %u tests Segment(s) executed\n\n", + (unsigned int)TestCounters->TestSegmentCount); + + OS_TaskExit(); +} diff --git a/modules/cfe_assert/src/cfe_assert_main.c b/modules/cfe_assert/src/cfe_assert_main.c new file mode 100644 index 000000000..d488be8ec --- /dev/null +++ b/modules/cfe_assert/src/cfe_assert_main.c @@ -0,0 +1,135 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-2019 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: cfe_assert_main.c +** +** Purpose: +** Implementation of the CFE assert (UT assert wrapper) functions. +** +*************************************************************************/ + +/* + * Includes + */ + +#include + +#include "cfe_assert.h" + +#include "uttest.h" +#include "utbsp.h" + +/* + * The maximum amount of time that the application will delay to + * wait for other apps to complete startup before running the tests. + * + * The value is in milliseconds. Normally this shouldn't be more than + * a second or two for apps to all reach their respective main loop(s). + */ +#define CFE_ASSERT_MAX_STARTUP_WAIT 30000 + +/* + * Small Extra delay before starting tests. + * + * This is not strictly necessary, but it does give a bit of time for other apps + * to settle their final syslog writes/events such that they will not be intermixed + * with test messages in the syslog. + * + * The value is in milliseconds. + */ +#define CFE_ASSERT_START_DELAY 4000 + + +/* + * Entry point for this application + */ +void CFE_Assert_AppMain(void) +{ + int32 rc; + uint32 RunStatus; + + /* + ** Register the app with Executive services + */ + rc = CFE_ES_RegisterApp(); + if (rc != CFE_SUCCESS) + { + CFE_ES_WriteToSysLog("Error in CFE_ES_RegisterApp(): %08lx\n", (unsigned long)rc); + return; + } + + UtTest_EarlyInit(); + UT_BSP_Setup(); + + /* + * Start a test case for all startup logic. + * + * Test libs may use assert statements within their init function and these + * will be reported as a "startup" test case. + */ + UtAssert_BeginTest("CFE-STARTUP"); + + /* + * Delay until the system reaches "operational" state -- this is when all libs have initialized + * and all apps have reached their RunLoop. + */ + rc = CFE_ES_WaitForSystemState(CFE_ES_SystemState_OPERATIONAL, CFE_ASSERT_MAX_STARTUP_WAIT); + if (rc != CFE_SUCCESS) + { + CFE_ES_WriteToSysLog("Error while waiting for OPERATIONAL state: %08lx\n", (unsigned long)rc); + return; + } + + /* + * Startup Phase has ended. + */ + UtAssert_EndTest(); + + /* + * Note - in a normal app this would be a while loop, + * but is just an "if" right now as it only runs once. + * + * A future enhancement to this app might be to create an SB pipe + * and execute tests based on the receipt of a command to do so. + * + * For now, it just works in a one-shot mode to run tests that were + * registered during startup, then it self-exits. + */ + RunStatus = CFE_ES_RunStatus_APP_RUN; + if(CFE_ES_RunLoop(&RunStatus)) + { + OS_TaskDelay(CFE_ASSERT_START_DELAY); + + /* + * Run all registered test cases. + */ + UtTest_Run(); + + /* + * Exit the main task. + */ + RunStatus = CFE_ES_RunStatus_APP_EXIT; + } + + + CFE_ES_ExitApp(RunStatus); +} + + diff --git a/modules/cfe_test/CMakeLists.txt b/modules/cfe_test/CMakeLists.txt new file mode 100644 index 000000000..4f6353434 --- /dev/null +++ b/modules/cfe_test/CMakeLists.txt @@ -0,0 +1,8 @@ +include_directories("${CFE_ASSERT_SOURCE_DIR}/inc") +include_directories("${UT_ASSERT_SOURCE_DIR}/inc") + +# Create the app module +add_cfe_app(cfe_test + src/cfe_test.c + src/es_test.c +) diff --git a/modules/cfe_test/src/cfe_test.c b/modules/cfe_test/src/cfe_test.c new file mode 100644 index 000000000..fe5842928 --- /dev/null +++ b/modules/cfe_test/src/cfe_test.c @@ -0,0 +1,45 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-2019 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: cfe_test.c +** +** Purpose: +** Initialization routine for CFE functional test +** Demonstration of how to register and use the UT assert functions. +** +*************************************************************************/ + +/* + * Includes + */ + +#include "cfe_test.h" + +/* + * Initialization function + * Register this test routine with CFE Assert + */ +int32 CFE_Test_Init(int32 LibId) +{ + UtTest_Add(ES_Test_AppId, NULL, NULL, "ES AppID"); + return CFE_SUCCESS; +} + + diff --git a/modules/cfe_test/src/cfe_test.h b/modules/cfe_test/src/cfe_test.h new file mode 100644 index 000000000..77acd1e8a --- /dev/null +++ b/modules/cfe_test/src/cfe_test.h @@ -0,0 +1,45 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-2019 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: cfe_test.c +** +** Purpose: +** Initialization routine for CFE functional test +** Demonstration of how to register and use the UT assert functions. +** +*************************************************************************/ + +#ifndef CFE_TEST_H +#define CFE_TEST_H + +/* + * Includes + */ + +#include + +#include "uttest.h" +#include "utassert.h" + +void ES_Test_AppId(void); +int32 CFE_Test_Init(int32 LibId); + + +#endif /* CFE_TEST_H */ diff --git a/modules/cfe_test/src/es_test.c b/modules/cfe_test/src/es_test.c new file mode 100644 index 000000000..632b084aa --- /dev/null +++ b/modules/cfe_test/src/es_test.c @@ -0,0 +1,46 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-2019 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: es_test.c +** +** Purpose: +** Functional test of basic ES APIs +** +** Demonstration of how to register and use the UT assert functions. +** +*************************************************************************/ + +/* + * Includes + */ + +#include "cfe_test.h" + + +void ES_Test_AppId(void) +{ + uint32 AppId; + char AppNameBuf[OS_MAX_API_NAME+4]; + + UtAssert_INT32_EQ(CFE_ES_GetAppID(&AppId), CFE_SUCCESS); + UtAssert_INT32_EQ(CFE_ES_GetAppName(AppNameBuf, AppId, sizeof(AppNameBuf)), CFE_SUCCESS); + UtAssert_StrCmp(AppNameBuf, "ASSERT_APP", "CFE_ES_GetAppName() returned ASSERT_APP"); +} +