From 7428160138e729a064b7f128b1f8d345a46ced28 Mon Sep 17 00:00:00 2001 From: pavll Date: Fri, 6 Aug 2021 17:19:58 +0200 Subject: [PATCH] Fix #1677, Add Message Api Functional Test --- modules/cfe_testcase/CMakeLists.txt | 1 + modules/cfe_testcase/src/cfe_test.c | 1 + modules/cfe_testcase/src/cfe_test.h | 7 + modules/cfe_testcase/src/msg_api_test.c | 287 ++++++++++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 modules/cfe_testcase/src/msg_api_test.c diff --git a/modules/cfe_testcase/CMakeLists.txt b/modules/cfe_testcase/CMakeLists.txt index c5abc909e..9fdc7fa29 100644 --- a/modules/cfe_testcase/CMakeLists.txt +++ b/modules/cfe_testcase/CMakeLists.txt @@ -18,6 +18,7 @@ add_cfe_app(cfe_testcase src/fs_header_test.c src/fs_util_test.c src/message_id_test.c + src/msg_api_test.c src/sb_pipe_mang_test.c src/tbl_content_access_test.c src/tbl_content_mang_test.c diff --git a/modules/cfe_testcase/src/cfe_test.c b/modules/cfe_testcase/src/cfe_test.c index 580074d4f..4fd0ba041 100644 --- a/modules/cfe_testcase/src/cfe_test.c +++ b/modules/cfe_testcase/src/cfe_test.c @@ -71,6 +71,7 @@ void CFE_TestMain(void) FSHeaderTestSetup(); FSUtilTestSetup(); MessageIdTestSetup(); + MsgApiTestSetup(); SBPipeMangSetup(); TBLContentAccessTestSetup(); TBLContentMangTestSetup(); diff --git a/modules/cfe_testcase/src/cfe_test.h b/modules/cfe_testcase/src/cfe_test.h index 09614a2c5..4c72a6fa4 100644 --- a/modules/cfe_testcase/src/cfe_test.h +++ b/modules/cfe_testcase/src/cfe_test.h @@ -86,6 +86,12 @@ extern CFE_FT_Global_t CFE_FT_Global; UtAssert_True(rcact < CFE_SUCCESS, "%s == (%ld) ", #actual, (long)rcact); \ } while (0) +/* Assert if status is CFE_SUCCESS */ +#define cFE_FTAssert_SUCCESS(status) UtAssert_INT32_EQ(status, CFE_SUCCESS) + +/* Log calls to void functions */ +#define cFE_FTAssert_VOIDCALL(func) (func, UtAssert(true, #func, __FILE__, __LINE__)) + bool TimeInRange(CFE_TIME_SysTime_t Time, CFE_TIME_SysTime_t Target, OS_time_t difference); void CFE_TestMain(void); @@ -102,6 +108,7 @@ void EVSSendTestSetup(void); void FSHeaderTestSetup(void); void FSUtilTestSetup(void); void MessageIdTestSetup(void); +void MsgApiTestSetup(void); void SBPipeMangSetup(void); void TBLContentAccessTestSetup(void); void TBLContentMangTestSetup(void); 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..8ed2a1505 --- /dev/null +++ b/modules/cfe_testcase/src/msg_api_test.c @@ -0,0 +1,287 @@ +/************************************************************************* +** +** 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 + +void TestMsgApiBasic(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"); + + /* declare local vars */ + CFE_MSG_CommandHeader_t cmd; + CFE_MSG_Size_t size; + CFE_MSG_Type_t type; + CFE_SB_MsgId_t msgId; + CFE_MSG_HeaderVersion_t hdrVer; + CFE_MSG_ApId_t appId; + + memset(&cmd, 0xFF, sizeof(cmd)); + msgId = CFE_SB_ValueToMsgId(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, msgId, 0), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_Init(&cmd.Msg, msgId, sizeof(cmd))); + + /* test set-msg-size */ + UtAssert_INT32_EQ(CFE_MSG_SetSize(NULL, 12), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetSize(&cmd.Msg, 12)); + + /* test get-msg-size */ + UtAssert_INT32_EQ(CFE_MSG_GetSize(NULL, &size), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_GetSize(&cmd.Msg, &size)); + UtAssert_UINT32_EQ(size, 12); + + /* test get-type */ + UtAssert_INT32_EQ(CFE_MSG_SetType(NULL, CFE_MSG_Type_Cmd), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetType(NULL, &type), CFE_MSG_BAD_ARGUMENT); + cFE_FTAssert_SUCCESS(CFE_MSG_SetType(&cmd.Msg, CFE_MSG_Type_Cmd)); + + cFE_FTAssert_SUCCESS(CFE_MSG_GetType(&cmd.Msg, &type)); + UtAssert_INT32_EQ(type, CFE_MSG_Type_Cmd); + + /* test msg set-type */ + cFE_FTAssert_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 */ + UtAssert_INT32_EQ(CFE_MSG_GetHeaderVersion(NULL, &hdrVer), CFE_MSG_BAD_ARGUMENT); + cFE_FTAssert_SUCCESS(CFE_MSG_GetHeaderVersion(&cmd.Msg, &hdrVer)); + UtAssert_INT32_EQ(CFE_MSG_GetHeaderVersion(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + /* test set header-version */ + UtAssert_INT32_EQ(CFE_MSG_SetHeaderVersion(NULL, hdrVer), CFE_MSG_BAD_ARGUMENT); + cFE_FTAssert_SUCCESS(CFE_MSG_SetHeaderVersion(&cmd.Msg, 0)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetHeaderVersion(&cmd.Msg, &hdrVer)); + UtAssert_True(hdrVer == 0, "hdrVer = 0"); + + /* test get-has-secondary-header and set-has-scondary-header*/ + bool _expected = true; + bool _returned = false; + + UtAssert_INT32_EQ(CFE_MSG_GetHasSecondaryHeader(NULL, &_expected), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetHasSecondaryHeader(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetHasSecondaryHeader(&cmd.Msg, _expected)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetHasSecondaryHeader(&cmd.Msg, &_returned)); + UtAssert_UINT32_EQ(_expected, _returned); + + /* test get-apid */ + UtAssert_INT32_EQ(CFE_MSG_GetApId(NULL, &appId), CFE_MSG_BAD_ARGUMENT); + cFE_FTAssert_SUCCESS(CFE_MSG_GetApId(&cmd.Msg, &appId)); + UtAssert_INT32_EQ(CFE_MSG_GetApId(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + /* test set-apid */ + UtAssert_INT32_EQ(CFE_MSG_GetApId(NULL, 0), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetApId(&cmd.Msg, 0xFFFF), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetApId(&cmd.Msg, 0)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetApId(&cmd.Msg, &appId)); + UtAssert_True(appId == 0, "CFE_MSG_SetApId => apid = 0"); +} + +void TestMsgApiAdvanced1(void) +{ + UtPrintf("Testing: 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"); + + /* declare local vars */ + CFE_MSG_CommandHeader_t cmd; + CFE_SB_MsgId_t msgId; + 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; + + memset(&cmd, 0xFF, sizeof(cmd)); + msgId = CFE_SB_ValueToMsgId(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, msgId, 0), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_Init(&cmd.Msg, msgId, sizeof(cmd))); + + /* test get/set-segmentation-flag */ + UtAssert_INT32_EQ(CFE_MSG_GetSegmentationFlag(NULL, &segFlag), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetSegmentationFlag(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetSegmentationFlag(NULL, CFE_MSG_SegFlag_Continue), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetSegmentationFlag(&cmd.Msg, CFE_MSG_SegFlag_Continue)); + cFE_FTAssert_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_SetSequenceCount(NULL, 2), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetSequenceCount(NULL, &seqCnt), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetSequenceCount(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetSequenceCount(&cmd.Msg, 2)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetSequenceCount(&cmd.Msg, &seqCnt)); + + UtAssert_True(seqCnt == 2, "CFE_MSG_SetSequenceCount => seqCnt = 2"); + + /* test get-next-sequence-count */ + + /* 0x3FFF <- bad because defined CFE_MSG_SEQCNT_MASK value but not public */ + /* UtAssert_True(CFE_MSG_GetNextSequenceCount(0x3FFF) == 0, "CFE_MSG_GetNextSequenceCount(0x3FFF) = 0"); */ + UtAssert_True(CFE_MSG_GetNextSequenceCount(2) == 3, "CFE_MSG_GetNextSequenceCount(2) = 3"); + + /* test get/set-eds-version */ + UtAssert_INT32_EQ(CFE_MSG_SetEDSVersion(NULL, 5), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetEDSVersion(NULL, &edsVersion), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetEDSVersion(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetEDSVersion(&cmd.Msg, 5)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetEDSVersion(&cmd.Msg, &edsVersion)); + + UtAssert_True(edsVersion == 5, "CFE_MSG_SetEDSVersion => edsVersion = 5"); + + /* test get/set-endian */ + CFE_MSG_Endian_t _expectedEndian = CFE_MSG_Endian_Little; + + UtAssert_INT32_EQ(CFE_MSG_SetEndian(NULL, _expectedEndian), 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); + UtAssert_INT32_EQ(CFE_MSG_GetEndian(NULL, &endian), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetEndian(&cmd.Msg, _expectedEndian)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetEndian(&cmd.Msg, &endian)); + + UtAssert_True(endian == _expectedEndian, "CFE_MSG_SetEndian => endian = CFE_MSG_Endian_Little"); + + /* test get/set-playback-flag */ + CFE_MSG_PlaybackFlag_t _expectedPlayFlag = CFE_MSG_PlayFlag_Playback; + + UtAssert_INT32_EQ(CFE_MSG_SetPlaybackFlag(NULL, _expectedPlayFlag), 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); + UtAssert_INT32_EQ(CFE_MSG_GetPlaybackFlag(NULL, &playbackFlag), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetPlaybackFlag(&cmd.Msg, _expectedPlayFlag)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetPlaybackFlag(&cmd.Msg, &playbackFlag)); + + UtAssert_True(playbackFlag == _expectedPlayFlag, + "CFE_MSG_SetPlaybackFlag => playbackFlag = CFE_MSG_PlayFlag_Playback"); +} + +void TestMsgApiAdvanced2(void) +{ + UtPrintf("Testing: CFE_MSG_GetSubsystem, CFE_MSG_SetSubsystem, 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_SB_MsgId_t msgId; + CFE_MSG_Subsystem_t subSystem; + CFE_MSG_FcnCode_t fcnCode; + CFE_TIME_SysTime_t msgTime; + + memset(&cmd, 0xFF, sizeof(cmd)); + msgId = CFE_SB_ValueToMsgId(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, msgId, 0), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_Init(&cmd.Msg, msgId, sizeof(cmd))); + + /* test get/set-subsystem */ + UtAssert_INT32_EQ(CFE_MSG_GetSubsystem(NULL, &subSystem), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetSubsystem(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetSubsystem(NULL, 3), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetSubsystem(&cmd.Msg, 3)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetSubsystem(&cmd.Msg, &subSystem)); + + UtAssert_True(subSystem == 3, "CFE_MSG_SetSubsystem => subSystem = 3"); + + /* test generate-checksum */ + UtAssert_INT32_EQ(CFE_MSG_GenerateChecksum(NULL), CFE_MSG_BAD_ARGUMENT); + cFE_FTAssert_SUCCESS(CFE_MSG_SetType(&cmd.Msg, CFE_MSG_Type_Tlm)); + UtAssert_INT32_EQ(CFE_MSG_GenerateChecksum(&cmd.Msg), CFE_MSG_BAD_ARGUMENT); + cFE_FTAssert_SUCCESS(CFE_MSG_SetType(&cmd.Msg, CFE_MSG_Type_Cmd)); + + /* create new cmd message */ + CFE_MSG_CommandHeader_t cmd2; + bool isValid = true; + + memset(&cmd2, 0, sizeof(cmd2)); + cFE_FTAssert_SUCCESS(CFE_MSG_Init(&cmd2.Msg, CFE_SB_ValueToMsgId(1), sizeof(cmd2))); + + cFE_FTAssert_SUCCESS(CFE_MSG_ValidateChecksum(&cmd2.Msg, &isValid)); + UtAssert_True(!isValid, "Is checksum valid = false"); + cFE_FTAssert_SUCCESS(CFE_MSG_GenerateChecksum(&cmd2.Msg)); + cFE_FTAssert_SUCCESS(CFE_MSG_ValidateChecksum(&cmd2.Msg, &isValid)); + UtAssert_True(isValid, "Checksum is valid"); + + /* test get/set-fcn-code */ + UtAssert_INT32_EQ(CFE_MSG_SetFcnCode(NULL, 4), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetFcnCode(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetFcnCode(NULL, &fcnCode), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetFcnCode(&cmd.Msg, 4)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetFcnCode(&cmd.Msg, &fcnCode)); + + UtAssert_True(fcnCode == 4, "CFE_MSG_SetFcnCode => fcnCode = 4"); + + /* test get/set-msg-time */ + CFE_TIME_SysTime_t currentTime = CFE_TIME_GetTime(); + + UtAssert_INT32_EQ(CFE_MSG_GetMsgTime(NULL, &msgTime), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_GetMsgTime(&cmd.Msg, NULL), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_SetMsgTime(NULL, currentTime), CFE_MSG_BAD_ARGUMENT); + + cFE_FTAssert_SUCCESS(CFE_MSG_SetMsgTime(&cmd.Msg, currentTime)); + cFE_FTAssert_SUCCESS(CFE_MSG_GetMsgTime(&cmd.Msg, &msgTime)); + + UtAssert_UINT32_EQ(CFE_TIME_Compare(msgTime, currentTime), CFE_TIME_EQUAL); +} + +void MsgApiTestSetup(void) +{ + UtTest_Add(TestMsgApiBasic, NULL, NULL, "Test basic message header apis"); + UtTest_Add(TestMsgApiAdvanced1, NULL, NULL, "Test advanced message header apis part 1"); + UtTest_Add(TestMsgApiAdvanced2, NULL, NULL, "Test advanced message header apis part 2"); +}