Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2337, add msg verify capability #2338

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions modules/core_api/fsw/inc/cfe_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,30 @@ CFE_Status_t CFE_MSG_GetTypeFromMsgId(CFE_SB_MsgId_t MsgId, CFE_MSG_Type_t *Type

/**\}*/

/** \defgroup CFEAPIMSGMsgVerify cFE Message Checking APIs
* \{
*/
/*****************************************************************************/
/**
* \brief Checks message headers against expected values
*
* \par Description
* This routine validates that any error-control field(s) in the message header
* matches the expected value.
*
* The specific function of this API is entirely dependent on the header fields
* and may be a no-op if no error checking is implemented. In that case, it
* will always output "true".
*
* \param[in] MsgPtr Message Pointer @nonnull
* \param[out] VerifyStatus Output variable to be set to verification result @nonnull
*
* \return Execution status, see \ref CFEReturnCodes
* \retval #CFE_SUCCESS \copybrief CFE_SUCCESS
* \retval #CFE_MSG_BAD_ARGUMENT \copybrief CFE_MSG_BAD_ARGUMENT
*/
CFE_Status_t CFE_MSG_Verify(const CFE_MSG_Message_t *MsgPtr, bool *VerifyStatus);

/**\}*/

#endif /* CFE_MSG_H */
25 changes: 13 additions & 12 deletions modules/msg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@
# Add the basic set of files which are always built
# Defined as absolute so this list can also be used to build unit tests
set(${DEP}_SRC
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_ccsdspri.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_init.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_msgid_shared.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_sechdr_checksum.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_sechdr_fc.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_sechdr_time.c
fsw/src/cfe_msg_ccsdspri.c
fsw/src/cfe_msg_init.c
fsw/src/cfe_msg_verify.c
fsw/src/cfe_msg_msgid_shared.c
fsw/src/cfe_msg_sechdr_checksum.c
fsw/src/cfe_msg_sechdr_fc.c
fsw/src/cfe_msg_sechdr_time.c
)

# Source selection for if CCSDS extended header is included, and MsgId version use
if (MISSION_INCLUDE_CCSDSEXT_HEADER)
message(STATUS "CCSDS primary and extended header included in message header")
list(APPEND ${DEP}_SRC
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_ccsdsext.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_initdefaulthdr_priext.c)
fsw/src/cfe_msg_ccsdsext.c
fsw/src/cfe_msg_initdefaulthdr_priext.c)
if (MISSION_MSGID_V2) # MsgId v2 or v1 can be used with extended headers
message(STATUS "Message Id version 2 in use (MsgId V2)")
list(APPEND ${DEP}_SRC
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_msgid_v2.c)
fsw/src/cfe_msg_msgid_v2.c)
else (MISSION_MSGID_V2)
message(STATUS "Message Id version 1 in use (MsgId V1)")
list(APPEND ${DEP}_SRC
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_msgid_v1.c)
fsw/src/cfe_msg_msgid_v1.c)
endif (MISSION_MSGID_V2)
else (MISSION_INCLUDE_CCSDSEXT_HEADER)
message(STATUS "CCSDS primary header included in message header (not including CCSDS extended header)")
message(STATUS "Message Id version 1 in use (MsgId V1)")
list(APPEND ${DEP}_SRC
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_initdefaulthdr_pri.c
${CMAKE_CURRENT_SOURCE_DIR}/fsw/src/cfe_msg_msgid_v1.c)
fsw/src/cfe_msg_initdefaulthdr_pri.c
fsw/src/cfe_msg_msgid_v1.c)
if (MISSION_MSGID_V2)
message(FATAL_ERROR "Message Id (MsgId) version 2 can only be used if MISSION_INCLUDE_CCSDSEXT_HEADER is set")
endif (MISSION_MSGID_V2)
Expand Down
48 changes: 48 additions & 0 deletions modules/msg/fsw/src/cfe_msg_verify.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/************************************************************************
* 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.
************************************************************************/

#include "cfe_msg.h"
#include "cfe_msg_priv.h"
#include "cfe_msg_defaults.h"
#include "cfe_time.h"

/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_MSG_Verify(const CFE_MSG_Message_t *MsgPtr, bool *VerifyStatus)
{
if (MsgPtr == NULL || VerifyStatus == NULL)
{
return CFE_MSG_BAD_ARGUMENT;
}

/*
* In the default implementation, there is not anything to check here.
* Only commands have a checksum, but the value of that checksum was historically
* not enforced by CFE.
*
* This is mainly a hook for user expansion, in case a custom implementation
* has message verification capability.
*/
*VerifyStatus = true;

return CFE_SUCCESS;
}
38 changes: 16 additions & 22 deletions modules/msg/ut-coverage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
#
##################################################################

# Unit test object library sources, options, and includes
add_library(ut_${DEP}_objs OBJECT ${${DEP}_SRC})
target_compile_options(ut_${DEP}_objs PRIVATE ${UT_COVERAGE_COMPILE_FLAGS})
target_include_directories(ut_${DEP}_objs PRIVATE
$<TARGET_PROPERTY:${DEP},INCLUDE_DIRECTORIES>)
set(UNIT_SRCS)
foreach(SRC ${${DEP}_SRC})
get_filename_component(UNITNAME "${SRC}" NAME)
list(APPEND UNIT_SRCS "../${SRC}")
endforeach()

set (ut_${DEP}_tests
msg_UT.c
test_msg_not.c
test_msg_pri_not.c
test_cfe_msg_init.c
test_cfe_msg_verify.c
test_cfe_msg_ccsdspri.c
test_cfe_msg_msgid_shared.c
test_cfe_msg_checksum.c
test_cfe_msg_fc.c
test_cfe_msg_time.c
$<TARGET_OBJECTS:ut_${DEP}_objs>)
test_cfe_msg_time.c)

# Add extended header tests if appropriate
if (MISSION_INCLUDE_CCSDSEXT_HEADER)
Expand All @@ -44,21 +44,15 @@ else (MISSION_MSGID_V2)
test_cfe_msg_msgid_v1.c)
endif (MISSION_MSGID_V2)

# Add executable
add_executable(${DEP}_UT ${ut_${DEP}_tests})

# Add include to get private defaults
target_include_directories(${DEP}_UT PRIVATE ../fsw/src)
add_cfe_coverage_test(${DEP} ALL
"msg_UT.c;${ut_${DEP}_tests}" # This list needs to be passed as a single argument
"${UNIT_SRCS}"
)

# Also add the UT_COVERAGE_LINK_FLAGS to the link command
# This should enable coverage analysis on platforms that support this
target_link_libraries(${DEP}_UT
${UT_COVERAGE_LINK_FLAGS}
ut_core_private_stubs
ut_core_api_stubs
ut_assert)
# This permits UT test cases to directly access private headers in the fsw/src dir
target_include_directories(coverage-${DEP}-ALL-testrunner PRIVATE
../fsw/src
)

add_test(${DEP}_UT ${DEP}_UT)
foreach(TGT ${INSTALL_TARGET_LIST})
install(TARGETS ${DEP}_UT DESTINATION ${TGT}/${UT_INSTALL_SUBDIR})
endforeach()
target_link_libraries(coverage-${DEP}-ALL-testrunner ut_core_private_stubs)
2 changes: 2 additions & 0 deletions modules/msg/ut-coverage/msg_UT.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "test_cfe_msg_init.h"
#include "test_cfe_msg_ccsdspri.h"
#include "test_cfe_msg_ccsdsext.h"
#include "test_cfe_msg_verify.h"
#include "test_cfe_msg_msgid_shared.h"
#include "test_cfe_msg_msgid.h"
#include "test_cfe_msg_fc.h"
Expand All @@ -46,6 +47,7 @@ void UtTest_Setup(void)
Test_MSG_CCSDSPri();
Test_MSG_CCSDSExt();
Test_MSG_MsgId_Shared();
UT_ADD_TEST(Test_MSG_Verify);
UT_ADD_TEST(Test_MSG_MsgId);
UT_ADD_TEST(Test_MSG_Checksum);
UT_ADD_TEST(Test_MSG_FcnCode);
Expand Down
59 changes: 59 additions & 0 deletions modules/msg/ut-coverage/test_cfe_msg_verify.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/************************************************************************
* 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.
************************************************************************/

/*
* Test message verify
*/

/*
* Includes
*/
#include "utassert.h"
#include "ut_support.h"
#include "cfe_msg.h"
#include "test_cfe_msg_verify.h"
#include "cfe_error.h"
#include "cfe_msg_defaults.h"

#include <string.h>

/*
* Test MSG Verify
*/
void Test_MSG_Verify(void)

Check notice

Code scanning / CodeQL

Long function without assertion

All functions of more than 10 lines should have at least one assertion.
{
union
{
CFE_MSG_Message_t msg;
CFE_MSG_CommandHeader_t cmd;
CFE_MSG_TelemetryHeader_t tlm;
} LocalBuf;
bool Result;

memset(&LocalBuf, 0, sizeof(LocalBuf));
Result = false;

/* bad buffer */
UtAssert_INT32_EQ(CFE_MSG_Verify(NULL, &Result), CFE_MSG_BAD_ARGUMENT);
UtAssert_INT32_EQ(CFE_MSG_Verify(&LocalBuf.msg, NULL), CFE_MSG_BAD_ARGUMENT);

/* nominal */
Result = false;
CFE_UtAssert_SUCCESS(CFE_MSG_Verify(&LocalBuf.msg, &Result));
UtAssert_BOOL_TRUE(Result);
}
36 changes: 36 additions & 0 deletions modules/msg/ut-coverage/test_cfe_msg_verify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/************************************************************************
* 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
*
* cfe_msg_verify test header
*/
#ifndef TEST_CFE_MSG_VERIFY_H
#define TEST_CFE_MSG_VERIFY_H

/*
* Includes
*/

/*
* Functions
*/
void Test_MSG_Verify(void);

#endif /* TEST_CFE_MSG_VERIFY_H */