diff --git a/modules/cfe_testcase/src/msg_api_test.c b/modules/cfe_testcase/src/msg_api_test.c new file mode 100644 index 000000000..e6fb291d7 --- /dev/null +++ b/modules/cfe_testcase/src/msg_api_test.c @@ -0,0 +1,239 @@ +/************************************************************************* +** +** GSC-18128-1, "Core Flight Executive Version 6.7" +** +** Copyright (c) 2006-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. +** +** File: msg_api_test.c +** +** Purpose: +** Functional test of cFE Message header APIs. +** +*************************************************************************/ + +/* + * Includes + */ + +#include "cfe_test.h" +#include + +/* merged all test into one function to avoid creating new messages for each test */ +void TestMSGApi(void) +{ + UtPrintf("Testing: CFE_MSG_Init, CFE_MSG_GetSize, CFE_MSG_SetSize, CFE_MSG_GetType, " + "CFE_MSG_SetType, CFE_MSG_GetHeaderVersion, CFE_MSG_SetHeaderVersion, CFE_MSG_GetHasSecondaryHeader, " + "CFE_MSG_SetHasSecondaryHeader, CFE_MSG_GetApId, CFE_MSG_SetApId, CFE_MSG_GetSegmentationFlag, " + "CFE_MSG_SetSegmentationFlag, CFE_MSG_GetSequenceCount, CFE_MSG_SetSequenceCount, " + "CFE_MSG_GetNextSequenceCount, CFE_MSG_GetEDSVersion, CFE_MSG_SetEDSVersion, " + "CFE_MSG_GetEndian, CFE_MSG_SetEndian, CFE_MSG_GetPlaybackFlag, CFE_MSG_SetPlaybackFlag, " + "CFE_MSG_GetSubsystem, CFE_MSG_SetSubsystem, CFE_MSG_GetSystem, CFE_MSG_SetSystem, " + "CFE_MSG_GenerateChecksum, CFE_MSG_ValidateChecksum, CFE_MSG_SetFcnCode, CFE_MSG_GetFcnCode, " + "CFE_MSG_GetMsgTime, CFE_MSG_SetMsgTime "); + + /* declare local vars */ + CFE_MSG_CommandHeader_t cmd; + CFE_MSG_Size_t size; + CFE_MSG_Type_t type; + CFE_SB_MsgId_Atom_t msgId; + CFE_MSG_HeaderVersion_t hdrVer; + CFE_MSG_ApId_t appId; + CFE_MSG_SegmentationFlag_t segFlag; + CFE_MSG_SequenceCount_t seqCnt; + CFE_MSG_EDSVersion_t edsVersion; + CFE_MSG_Endian_t endian; + CFE_MSG_PlaybackFlag_t playbackFlag; + CFE_MSG_Subsystem_t subSystem; + CFE_MSG_System_t system; + CFE_MSG_FcnCode_t fcnCode; + CFE_TIME_SysTime_t msgTime; + bool hasSecondaryHdr; + bool isValid; + + memset(&cmd, 0xFF, sizeof(cmd)); + msgId = 0; + + /* test msg-init */ + UtAssert_INT32_EQ(CFE_MSG_Init(NULL, CFE_SB_ValueToMsgId(0), sizeof(cmd)), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_Init(&cmd.Msg, CFE_SB_ValueToMsgId(0), 0), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_Init(&cmd.Msg, CFE_SB_ValueToMsgId(msgId), sizeof(cmd)); + + /* test set-msg-size */ + UtAssert_INT32_EQ(CFE_MSG_SetSize(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetSize(&cmd.Msg, 12u)); + + /* test get-msg-size */ + CFE_UtAssert_SUCCESS(CFE_MSG_GetSize(&cmd.Msg, &size)); + UtAssert_True(size == 12u, "CFE_MSG_SetSize => size = 12u"); + + /* test get-type */ + CFE_UtAssert_SUCCESS(CFE_MSG_GetType(&cmd.Msg, &type)); + /* type has to be ...Type_Cmd because it created within a command */ + UtAssert_INT32_EQ(type, CFE_MSG_Type_Cmd); + + /* test msg set-type */ + CFE_UtAssert_SUCCESS(CFE_MSG_SetType(&cmd.Msg, CFE_MSG_Type_Tlm)); + /* check if set-type works like expected */ + UtAssert_INT32_EQ(CFE_MSG_GetType(&cmd.Msg, &type), CFE_MSG_Type_Tlm); + + /* test get header-version */ + CFE_UtAssert_SUCCESS(CFE_MSG_GetHeaderVersion(&cmd.Msg, &hdrVer)); + UtAssert_INT32_EQ(CFE_MSG_GetHeaderVersion(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + /* test set header-version */ + CFE_UtAssert_SUCCESS(CFE_MSG_SetHeaderVersion(&cmd.Msg, CFE_MISSION_CCSDSVER); + CFE_MSG_GetHeaderVersion(&cmd.Msg, &hdrVer); + UtAssert_True(hdrVer == CFE_MISSION_CCSDSVER, "hdrVer = CFE_MISSION_CCSDSVER"); + UtAssert_INT32_EQ(CFE_MSG_SetHeaderVersion(NULL, hdrVer), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetHeaderVersion(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + /* test get-has-secondary-header */ + CFE_UtAssert_SUCCESS(CFE_MSG_GetHasSecondaryHeader(&cmd.Msg, &hasSecondaryHdr)); + UtAssert_INT32_EQ(CFE_MSG_GetHasSecondaryHeader(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + /* test set-has-scondary-header */ + hasSecondaryHdr = true; + CFE_UtAssert_SUCCESS(CFE_MSG_SetHasSecondaryHeader(&cmd.Msg, hasSecondaryHdr)); + hasSecondaryHdr = false; + CFE_MSG_GetHasSecondaryHeader(&cmd.Msg, &hasSecondaryHdr); + UtAssert_True(hasSecondaryHdr, "CFE_MSG_SetHasSecondaryHeader => CFE_MSG_GetHasSecondaryHeader = true"); + + /* test get-apid */ + CFE_UtAssert_SUCCESS(CFE_MSG_GetApId(&cmd.Msg, &appId)); + UtAssert_INT32_EQ(CFE_MSG_GetApId(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + /* test set-apid */ + CFE_UtAssert_SUCCESS(CFE_MSG_SetApId(&cmd.Msg, CFE_PLATFORM_DEFAULT_APID)); + UtAssert_True(CFE_MSG_GetApId(&cmd.Msg, &appId) == CFE_PLATFORM_DEFAULT_APID, "CFE_MSG_SetApId => apid = CFE_PLATFORM_DEFAULT_APID"); + UtAssert_INT32_EQ(CFE_MSG_SetApId(&cmd.Msg, 0xFFFF), CFE_MSG_BAD_ARGUMENT); + + /* test get/set-segmentation-flag */ + UtAssert_INT32_EQ(CFE_MSG_GetSegmentationFlag(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetSegmentationFlag(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetSegmentationFlag(&cmd.Msg, CFE_MSG_SegFlag_Continue)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetSegmentationFlag(&cmd.Msg, &segFlag)); + UtAssert_True(segFlag == CFE_MSG_SegFlag_Continue, "CFE_MSG_SetSegmentationFlag => segFlag = CFE_MSG_SegFlag_Continue"); + + /* test set/get-sequence-count */ + UtAssert_INT32_EQ(CFE_MSG_GetSequenceCount(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetSequenceCount(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetSequenceCount(&cmd.Msg, 2u)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetSequenceCount(&cmd.Msg, &seqCnt)); + + UtAssert_True(seqCnt == 2u, "CFE_MSG_SetSequenceCount => seqCnt = 2"); + + /* test get-next-sequence-count */ + UtAssert_True(CFE_MSG_GetNextSequenceCount(NULL) == 0, "CFE_MSG_GetNextSequenceCount(NULL) = 0"); + /* 0x3FFF <- bad because defined CFE_MSG_SEQCNT_MASK value but not public */ + UtAssert_True(CFE_MSG_GetNextSequenceCount(0x3FFF + 1) == 0, "CFE_MSG_GetNextSequenceCount(0x3FFF+1) = 0"); + UtAssert_True(CFE_MSG_GetNextSequenceCount(2u) == 3u, "CFE_MSG_GetNextSequenceCount(2u) = 3u"); + + /* test get/set-eds-version */ + UtAssert_INT32_EQ(CFE_MSG_SetEDSVersion(&cmd.msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetEDSVersion(&cmd.msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetEDSVersion(&cmd.Msg, 5u)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetEDSVersion(&cmd.Msg, &edsVersion)); + + UtAssert_True(edsVersion == 5u, "CFE_MSG_SetEDSVersion => edsVersion = 5u"); + + /* test get/set-endian */ + UtAssert_INT32_EQ(CFE_MSG_SetEndian(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetEndian(&cmd.Msg, CFE_MSG_Endian_Invalid), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetEndian(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetEndian(&cmd.Msg, CFE_MSG_Endian_Little)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetEndian(&cmd.Msg, &endian)); + + UtAssert_True(endian == CFE_MSG_Endian_Little, "CFE_MSG_SetEndian => endian = CFE_MSG_Endian_Little"); + + /* test get/set-playback-flag */ + UtAssert_INT32_EQ(CFE_MSG_SetPlaybackFlag(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetPlaybackFlag(&cmd.Msg, CFE_MSG_PlayFlag_Invalid), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetPlaybackFlag(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetPlaybackFlag(&cmd.Msg, CFE_MSG_PlayFlag_Playback)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetPlaybackFlag(&cmd.Msg, &playbackFlag)); + + UtAssert_True(playbackFlag == CFE_MSG_PlayFlag_Playback, "CFE_MSG_SetPlaybackFlag => playbackFlag = CFE_MSG_PlayFlag_Playback"); + + /* test get/set-subsystem */ + UtAssert_INT32_EQ(CFE_MSG_SetSubsystem(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetSubsystem(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetSubsystem(&cmd.Msg, 3u)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetSubsystem(&cmd.Msg, &subSystem)); + + UtAssert_True(subSystem == 3u, "CFE_MSG_SetSubsystem => subSystem = 3u"); + + /* test get/set-system */ + UtAssert_INT32_EQ(CFE_MSG_SetSystem(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetSystem(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetSystem(&cmd.Msg, 3u)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetSystem(&cmd.Msg, &system)); + + UtAssert_True(system == 3u, "CFE_MSG_SetSystem => system = 3u"); + + /* test generate-checksum */ + UtAssert_INT32_EQ(CFE_MSG_GenerateChecksum(NULL), CFE_MSG_BAD_ARGUMENT); + CFE_MSG_SetType(&cmd.Msg, CFE_MSG_Type_Tlm); + UtAssert_INT32_EQ(CFE_MSG_GenerateChecksum(&cmd.Msg), CFE_MSG_BAD_ARGUMENT); + + /* create new cmd message - TODO */ + /* + * CFE_MSG_CommandHeader_t cmd2; + * memset(&cmd2, 0xFF, sizeof(cmd2)); + * CFE_UtAssert_SUCCESS(CFE_MSG_Init(&cmd2.Msg, CFE_SB_ValueToMsgId(1u), sizeof(cmd2)); + * + * UtAssert_UINT32_EQ(CFE_MSG_GenerateChecksum(&cmd2.Msg), ); + * delete cmd2.Msg; + * delete cmd2; + */ + + /* + * CFE_UtAssert_SUCCESS(CFE_MSG_ValidateChecksum(&cmd.Msg, &isValid)); + * UtAssert_True(isValid, "checksum: isValid = true"); + */ + + /* test get/set-fcn-code */ + UtAssert_INT32_EQ(CFE_MSG_SetFcnCode(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetFcnCode(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetFcnCode(&cmd.Msg, 4u)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetFcnCode(&cmd.Msg, &fcnCode)); + + UtAssert_True(fcnCode == 4u, "CFE_MSG_SetFcnCode => fcnCode = 4u"); + + /* test get/set-msg-time */ + UtAssert_INT32_EQ(CFE_MSG_SetMsgTime(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetMsgTime(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + CFE_UtAssert_SUCCESS(CFE_MSG_SetMsgTime(&cmd.Msg, 7u)); + CFE_UtAssert_SUCCESS(CFE_MSG_GetMsgTime(&cmd.Msg, &msgTime)); + + UtAssert_True(msgTime == 7u, "CFE_MSG_SetMsgTime => msgTime = 7u"); +} + +void MSGApiTestSetup(void) +{ + UtTest_Add(TestMSGApi, NULL, NULL, "Test message header apis"); +} \ No newline at end of file diff --git a/modules/msg/fsw/src/cfe_msg_ccsdsext.c b/modules/msg/fsw/src/cfe_msg_ccsdsext.c index c18d3e4cf..ecb0841c7 100644 --- a/modules/msg/fsw/src/cfe_msg_ccsdsext.c +++ b/modules/msg/fsw/src/cfe_msg_ccsdsext.c @@ -90,7 +90,7 @@ CFE_Status_t CFE_MSG_GetEDSVersion(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_EDSV *-----------------------------------------------------------------*/ CFE_Status_t CFE_MSG_SetEDSVersion(CFE_MSG_Message_t *MsgPtr, CFE_MSG_EDSVersion_t Version) { - if (MsgPtr == NULL || (Version > (CFE_MSG_EDSVER_MASK >> CFE_MSG_EDSVER_SHIFT))) + if (MsgPtr == NULL || Version == NULL || (Version > (CFE_MSG_EDSVER_MASK >> CFE_MSG_EDSVER_SHIFT))) { return CFE_MSG_BAD_ARGUMENT; } @@ -253,7 +253,7 @@ CFE_Status_t CFE_MSG_GetSubsystem(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_Subsy *-----------------------------------------------------------------*/ CFE_Status_t CFE_MSG_SetSubsystem(CFE_MSG_Message_t *MsgPtr, CFE_MSG_Subsystem_t Subsystem) { - if (MsgPtr == NULL || ((Subsystem & ~CFE_MSG_SUBSYS_MASK) != 0)) + if (MsgPtr == NULL || Subsystem == NULL || ((Subsystem & ~CFE_MSG_SUBSYS_MASK) != 0)) { return CFE_MSG_BAD_ARGUMENT; } @@ -294,7 +294,7 @@ CFE_Status_t CFE_MSG_GetSystem(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_System_t *-----------------------------------------------------------------*/ CFE_Status_t CFE_MSG_SetSystem(CFE_MSG_Message_t *MsgPtr, CFE_MSG_System_t System) { - if (MsgPtr == NULL) + if (MsgPtr == NULL || System == NULL) { return CFE_MSG_BAD_ARGUMENT; } diff --git a/modules/msg/fsw/src/cfe_msg_ccsdspri.c b/modules/msg/fsw/src/cfe_msg_ccsdspri.c index 86b5392ed..d41e53e24 100644 --- a/modules/msg/fsw/src/cfe_msg_ccsdspri.c +++ b/modules/msg/fsw/src/cfe_msg_ccsdspri.c @@ -96,7 +96,7 @@ CFE_Status_t CFE_MSG_GetHeaderVersion(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_H *-----------------------------------------------------------------*/ CFE_Status_t CFE_MSG_SetHeaderVersion(CFE_MSG_Message_t *MsgPtr, CFE_MSG_HeaderVersion_t Version) { - if (MsgPtr == NULL || (Version > (CFE_MSG_CCSDSVER_MASK >> CFE_MSG_CCSDSVER_SHIFT))) + if (MsgPtr == NULL || Version == NULL || (Version > (CFE_MSG_CCSDSVER_MASK >> CFE_MSG_CCSDSVER_SHIFT))) { return CFE_MSG_BAD_ARGUMENT; } @@ -371,7 +371,7 @@ CFE_Status_t CFE_MSG_GetSequenceCount(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_S *-----------------------------------------------------------------*/ CFE_Status_t CFE_MSG_SetSequenceCount(CFE_MSG_Message_t *MsgPtr, CFE_MSG_SequenceCount_t SeqCnt) { - if (MsgPtr == NULL || ((SeqCnt & ~CFE_MSG_SEQCNT_MASK) != 0)) + if (MsgPtr == NULL || SeqCnt == NULL || ((SeqCnt & ~CFE_MSG_SEQCNT_MASK) != 0)) { return CFE_MSG_BAD_ARGUMENT; } @@ -391,6 +391,12 @@ CFE_Status_t CFE_MSG_SetSequenceCount(CFE_MSG_Message_t *MsgPtr, CFE_MSG_Sequenc *-----------------------------------------------------------------*/ CFE_MSG_SequenceCount_t CFE_MSG_GetNextSequenceCount(CFE_MSG_SequenceCount_t SeqCnt) { + if (SeqCnt == NULL) + { + SeqCnt = 0; + return SeqCnt; + } + SeqCnt++; if (SeqCnt > CFE_MSG_SEQCNT_MASK) @@ -432,7 +438,7 @@ CFE_Status_t CFE_MSG_GetSize(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_Size_t *Si *-----------------------------------------------------------------*/ CFE_Status_t CFE_MSG_SetSize(CFE_MSG_Message_t *MsgPtr, CFE_MSG_Size_t Size) { - if (MsgPtr == NULL || Size < CFE_MSG_SIZE_OFFSET || Size > (0xFFFF + CFE_MSG_SIZE_OFFSET)) + if (MsgPtr == NULL || Size == NULL || Size < CFE_MSG_SIZE_OFFSET || Size > (0xFFFF + CFE_MSG_SIZE_OFFSET)) { return CFE_MSG_BAD_ARGUMENT; } diff --git a/modules/msg/fsw/src/cfe_msg_sechdr_fc.c b/modules/msg/fsw/src/cfe_msg_sechdr_fc.c index 29adbb963..0e00cb10c 100644 --- a/modules/msg/fsw/src/cfe_msg_sechdr_fc.c +++ b/modules/msg/fsw/src/cfe_msg_sechdr_fc.c @@ -76,7 +76,7 @@ CFE_Status_t CFE_MSG_SetFcnCode(CFE_MSG_Message_t *MsgPtr, CFE_MSG_FcnCode_t Fcn bool hassechdr = false; CFE_MSG_CommandHeader_t *cmd = (CFE_MSG_CommandHeader_t *)MsgPtr; - if (MsgPtr == NULL || (FcnCode > CFE_MSG_FC_MASK)) + if (MsgPtr == NULL || FcnCode == NULL || (FcnCode > CFE_MSG_FC_MASK)) { return CFE_MSG_BAD_ARGUMENT; } diff --git a/modules/msg/fsw/src/cfe_msg_sechdr_time.c b/modules/msg/fsw/src/cfe_msg_sechdr_time.c index 6bec344e0..524d8d76a 100644 --- a/modules/msg/fsw/src/cfe_msg_sechdr_time.c +++ b/modules/msg/fsw/src/cfe_msg_sechdr_time.c @@ -43,7 +43,7 @@ CFE_Status_t CFE_MSG_SetMsgTime(CFE_MSG_Message_t *MsgPtr, CFE_TIME_SysTime_t Ne bool hassechdr = false; CFE_MSG_TelemetryHeader_t *tlm = (CFE_MSG_TelemetryHeader_t *)MsgPtr; - if (MsgPtr == NULL) + if (MsgPtr == NULL || NewTime == NULL) { return CFE_MSG_BAD_ARGUMENT; }