From dba3f9960ef56db30011f9ff73bf6a3aa5e07549 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 9 Aug 2022 14:55:08 +0200 Subject: [PATCH] add CFDP fault handler mock --- src/fsfw/cfdp/FaultHandlerBase.cpp | 23 +++++------ src/fsfw/cfdp/FaultHandlerBase.h | 10 +++-- src/fsfw/cfdp/mib.h | 1 - src/fsfw/cfdp/pdu/PduConfig.h | 4 +- unittests/cfdp/CMakeLists.txt | 22 +++-------- unittests/cfdp/handler/CMakeLists.txt | 1 + .../cfdp/{ => handler}/testDestHandler.cpp | 0 .../cfdp/{ => handler}/testDistributor.cpp | 0 unittests/cfdp/pdu/CMakeLists.txt | 13 +++++++ unittests/cfdp/{ => pdu}/testAckPdu.cpp | 0 unittests/cfdp/{ => pdu}/testCfdpHeader.cpp | 0 unittests/cfdp/{ => pdu}/testEofPdu.cpp | 0 unittests/cfdp/{ => pdu}/testFileData.cpp | 0 .../cfdp/{ => pdu}/testFileDirective.cpp | 0 unittests/cfdp/{ => pdu}/testFinishedPdu.cpp | 0 unittests/cfdp/{ => pdu}/testKeepAlivePdu.cpp | 0 unittests/cfdp/{ => pdu}/testMetadataPdu.cpp | 3 +- unittests/cfdp/{ => pdu}/testNakPdu.cpp | 0 unittests/cfdp/{ => pdu}/testPromptPdu.cpp | 0 unittests/mocks/CMakeLists.txt | 3 +- unittests/mocks/CfdpFaultHandlerMock.cpp | 38 +++++++++++++++++++ unittests/mocks/CfdpFaultHandlerMock.h | 32 ++++++++++++++++ 22 files changed, 113 insertions(+), 37 deletions(-) create mode 100644 unittests/cfdp/handler/CMakeLists.txt rename unittests/cfdp/{ => handler}/testDestHandler.cpp (100%) rename unittests/cfdp/{ => handler}/testDistributor.cpp (100%) create mode 100644 unittests/cfdp/pdu/CMakeLists.txt rename unittests/cfdp/{ => pdu}/testAckPdu.cpp (100%) rename unittests/cfdp/{ => pdu}/testCfdpHeader.cpp (100%) rename unittests/cfdp/{ => pdu}/testEofPdu.cpp (100%) rename unittests/cfdp/{ => pdu}/testFileData.cpp (100%) rename unittests/cfdp/{ => pdu}/testFileDirective.cpp (100%) rename unittests/cfdp/{ => pdu}/testFinishedPdu.cpp (100%) rename unittests/cfdp/{ => pdu}/testKeepAlivePdu.cpp (100%) rename unittests/cfdp/{ => pdu}/testMetadataPdu.cpp (99%) rename unittests/cfdp/{ => pdu}/testNakPdu.cpp (100%) rename unittests/cfdp/{ => pdu}/testPromptPdu.cpp (100%) create mode 100644 unittests/mocks/CfdpFaultHandlerMock.cpp create mode 100644 unittests/mocks/CfdpFaultHandlerMock.h diff --git a/src/fsfw/cfdp/FaultHandlerBase.cpp b/src/fsfw/cfdp/FaultHandlerBase.cpp index 06edabd6..91c3afe4 100644 --- a/src/fsfw/cfdp/FaultHandlerBase.cpp +++ b/src/fsfw/cfdp/FaultHandlerBase.cpp @@ -1,19 +1,21 @@ #include "FaultHandlerBase.h" -CfdpFaultHandlerBase::CfdpFaultHandlerBase() = default; +namespace cfdp { -bool CfdpFaultHandlerBase::getFaultHandler( - cfdp::ConditionCode code, cfdp::FaultHandlerCodes& handler) const { +FaultHandlerBase::FaultHandlerBase() = default; +FaultHandlerBase::~FaultHandlerBase() = default; + +bool FaultHandlerBase::getFaultHandler(cfdp::ConditionCode code, + cfdp::FaultHandlerCodes& handler) const { auto iter = faultHandlerMap.find(code); - if(iter == faultHandlerMap.end()) { + if (iter == faultHandlerMap.end()) { return false; } handler = iter->second; return true; } -bool CfdpFaultHandlerBase::setFaultHandler(cfdp::ConditionCode code, - cfdp::FaultHandlerCodes handler) { +bool FaultHandlerBase::setFaultHandler(cfdp::ConditionCode code, cfdp::FaultHandlerCodes handler) { if (not faultHandlerMap.contains(code)) { return false; } @@ -21,21 +23,20 @@ bool CfdpFaultHandlerBase::setFaultHandler(cfdp::ConditionCode code, return true; } -bool CfdpFaultHandlerBase::faultCallback(cfdp::ConditionCode code) { +bool FaultHandlerBase::faultCallback(cfdp::ConditionCode code) { if (not faultHandlerMap.contains(code)) { return false; } cfdp::FaultHandlerCodes fh = faultHandlerMap[code]; if (fh == cfdp::FaultHandlerCodes::IGNORE_ERROR) { ignoreCb(code); - } else if(fh == cfdp::FaultHandlerCodes::ABANDON_TRANSACTION) { + } else if (fh == cfdp::FaultHandlerCodes::ABANDON_TRANSACTION) { abandonCb(code); - } else if(fh == cfdp::FaultHandlerCodes::NOTICE_OF_CANCELLATION) { + } else if (fh == cfdp::FaultHandlerCodes::NOTICE_OF_CANCELLATION) { noticeOfCancellationCb(code); } else { noticeOfSuspensionCb(code); } return true; } - -CfdpFaultHandlerBase::~CfdpFaultHandlerBase() = default; +} // namespace cfdp diff --git a/src/fsfw/cfdp/FaultHandlerBase.h b/src/fsfw/cfdp/FaultHandlerBase.h index fc18f1c7..6a4abc8b 100644 --- a/src/fsfw/cfdp/FaultHandlerBase.h +++ b/src/fsfw/cfdp/FaultHandlerBase.h @@ -5,10 +5,12 @@ #include "definitions.h" -class CfdpFaultHandlerBase { +namespace cfdp { + +class FaultHandlerBase { public: - virtual ~CfdpFaultHandlerBase(); - CfdpFaultHandlerBase(); + virtual ~FaultHandlerBase(); + FaultHandlerBase(); /** * Get the fault handler code for the given condition code @@ -46,4 +48,6 @@ class CfdpFaultHandlerBase { cfdp::FaultHandlerCodes::IGNORE_ERROR}}; }; +} // namespace cfdp + #endif // FSFW_CFDP_FAULTHANDLERBASE_H diff --git a/src/fsfw/cfdp/mib.h b/src/fsfw/cfdp/mib.h index 3a8c96d5..c40034d4 100644 --- a/src/fsfw/cfdp/mib.h +++ b/src/fsfw/cfdp/mib.h @@ -15,5 +15,4 @@ struct LocalEntityCfg { IndicationCfg indicCfg; }; - #endif // FSFW_CFDP_MIB_H diff --git a/src/fsfw/cfdp/pdu/PduConfig.h b/src/fsfw/cfdp/pdu/PduConfig.h index a008f18d..48f3993b 100644 --- a/src/fsfw/cfdp/pdu/PduConfig.h +++ b/src/fsfw/cfdp/pdu/PduConfig.h @@ -1,8 +1,8 @@ #ifndef FSFW_SRC_FSFW_CFDP_PDU_PDUCONFIG_H_ #define FSFW_SRC_FSFW_CFDP_PDU_PDUCONFIG_H_ -#include "fsfw/cfdp/definitions.h" #include "VarLenField.h" +#include "fsfw/cfdp/definitions.h" namespace cfdp { @@ -22,7 +22,7 @@ struct TransactionSeqNum : public VarLenField { TransactionSeqNum(cfdp::WidthInBytes width, size_t seqNum) : VarLenField(width, seqNum) {} }; -} +} // namespace cfdp class PduConfig { public: diff --git a/unittests/cfdp/CMakeLists.txt b/unittests/cfdp/CMakeLists.txt index ac4672fa..4a574af2 100644 --- a/unittests/cfdp/CMakeLists.txt +++ b/unittests/cfdp/CMakeLists.txt @@ -1,17 +1,5 @@ -target_sources( - ${FSFW_TEST_TGT} - PRIVATE testCfdp.cpp - testTlvsLvs.cpp - testAckPdu.cpp - testEofPdu.cpp - testNakPdu.cpp - testFinishedPdu.cpp - testPromptPdu.cpp - testKeepAlivePdu.cpp - testMetadataPdu.cpp - testFileData.cpp - testCfdpHeader.cpp - testFileDirective.cpp - testDistributor.cpp - testDestHandler.cpp - testSourceHandler.cpp) +target_sources(${FSFW_TEST_TGT} PRIVATE testCfdp.cpp testTlvsLvs.cpp + testSourceHandler.cpp) + +add_subdirectory(handler) +add_subdirectory(pdu) diff --git a/unittests/cfdp/handler/CMakeLists.txt b/unittests/cfdp/handler/CMakeLists.txt new file mode 100644 index 00000000..a209b995 --- /dev/null +++ b/unittests/cfdp/handler/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(${FSFW_TEST_TGT} PRIVATE testDistributor.cpp testDestHandler.cpp) diff --git a/unittests/cfdp/testDestHandler.cpp b/unittests/cfdp/handler/testDestHandler.cpp similarity index 100% rename from unittests/cfdp/testDestHandler.cpp rename to unittests/cfdp/handler/testDestHandler.cpp diff --git a/unittests/cfdp/testDistributor.cpp b/unittests/cfdp/handler/testDistributor.cpp similarity index 100% rename from unittests/cfdp/testDistributor.cpp rename to unittests/cfdp/handler/testDistributor.cpp diff --git a/unittests/cfdp/pdu/CMakeLists.txt b/unittests/cfdp/pdu/CMakeLists.txt new file mode 100644 index 00000000..02981217 --- /dev/null +++ b/unittests/cfdp/pdu/CMakeLists.txt @@ -0,0 +1,13 @@ +target_sources( + ${FSFW_TEST_TGT} + PRIVATE testAckPdu.cpp + testAckPdu.cpp + testEofPdu.cpp + testNakPdu.cpp + testFinishedPdu.cpp + testPromptPdu.cpp + testKeepAlivePdu.cpp + testMetadataPdu.cpp + testFileData.cpp + testCfdpHeader.cpp + testFileDirective.cpp) diff --git a/unittests/cfdp/testAckPdu.cpp b/unittests/cfdp/pdu/testAckPdu.cpp similarity index 100% rename from unittests/cfdp/testAckPdu.cpp rename to unittests/cfdp/pdu/testAckPdu.cpp diff --git a/unittests/cfdp/testCfdpHeader.cpp b/unittests/cfdp/pdu/testCfdpHeader.cpp similarity index 100% rename from unittests/cfdp/testCfdpHeader.cpp rename to unittests/cfdp/pdu/testCfdpHeader.cpp diff --git a/unittests/cfdp/testEofPdu.cpp b/unittests/cfdp/pdu/testEofPdu.cpp similarity index 100% rename from unittests/cfdp/testEofPdu.cpp rename to unittests/cfdp/pdu/testEofPdu.cpp diff --git a/unittests/cfdp/testFileData.cpp b/unittests/cfdp/pdu/testFileData.cpp similarity index 100% rename from unittests/cfdp/testFileData.cpp rename to unittests/cfdp/pdu/testFileData.cpp diff --git a/unittests/cfdp/testFileDirective.cpp b/unittests/cfdp/pdu/testFileDirective.cpp similarity index 100% rename from unittests/cfdp/testFileDirective.cpp rename to unittests/cfdp/pdu/testFileDirective.cpp diff --git a/unittests/cfdp/testFinishedPdu.cpp b/unittests/cfdp/pdu/testFinishedPdu.cpp similarity index 100% rename from unittests/cfdp/testFinishedPdu.cpp rename to unittests/cfdp/pdu/testFinishedPdu.cpp diff --git a/unittests/cfdp/testKeepAlivePdu.cpp b/unittests/cfdp/pdu/testKeepAlivePdu.cpp similarity index 100% rename from unittests/cfdp/testKeepAlivePdu.cpp rename to unittests/cfdp/pdu/testKeepAlivePdu.cpp diff --git a/unittests/cfdp/testMetadataPdu.cpp b/unittests/cfdp/pdu/testMetadataPdu.cpp similarity index 99% rename from unittests/cfdp/testMetadataPdu.cpp rename to unittests/cfdp/pdu/testMetadataPdu.cpp index 4ea15451..dec3b479 100644 --- a/unittests/cfdp/testMetadataPdu.cpp +++ b/unittests/cfdp/pdu/testMetadataPdu.cpp @@ -1,5 +1,3 @@ -#include - #include #include #include @@ -7,6 +5,7 @@ #include "fsfw/cfdp/pdu/MetadataPduCreator.h" #include "fsfw/cfdp/pdu/MetadataPduReader.h" #include "fsfw/cfdp/tlv/FilestoreResponseTlv.h" +#include "fsfw/cfdp/tlv/MessageToUserTlv.h" #include "fsfw/globalfunctions/arrayprinter.h" TEST_CASE("Metadata PDU", "[cfdp][pdu]") { diff --git a/unittests/cfdp/testNakPdu.cpp b/unittests/cfdp/pdu/testNakPdu.cpp similarity index 100% rename from unittests/cfdp/testNakPdu.cpp rename to unittests/cfdp/pdu/testNakPdu.cpp diff --git a/unittests/cfdp/testPromptPdu.cpp b/unittests/cfdp/pdu/testPromptPdu.cpp similarity index 100% rename from unittests/cfdp/testPromptPdu.cpp rename to unittests/cfdp/pdu/testPromptPdu.cpp diff --git a/unittests/mocks/CMakeLists.txt b/unittests/mocks/CMakeLists.txt index bc5649de..0ee7bf1e 100644 --- a/unittests/mocks/CMakeLists.txt +++ b/unittests/mocks/CMakeLists.txt @@ -14,4 +14,5 @@ target_sources( PusDistributorMock.cpp CcsdsCheckerMock.cpp AcceptsTcMock.cpp - StorageManagerMock.cpp) + StorageManagerMock.cpp + CfdpFaultHandlerMock.cpp) diff --git a/unittests/mocks/CfdpFaultHandlerMock.cpp b/unittests/mocks/CfdpFaultHandlerMock.cpp new file mode 100644 index 00000000..3f7c8896 --- /dev/null +++ b/unittests/mocks/CfdpFaultHandlerMock.cpp @@ -0,0 +1,38 @@ +#include "CfdpFaultHandlerMock.h" + +void CfdpFaultHandlerMock::noticeOfSuspensionCb(cfdp::ConditionCode code) { + auto& info = fhInfoMap.at(cfdp::FaultHandlerCodes::NOTICE_OF_SUSPENSION); + info.callCount++; + info.condCodes.push(code); +} + +void CfdpFaultHandlerMock::noticeOfCancellationCb(cfdp::ConditionCode code) { + auto& info = fhInfoMap.at(cfdp::FaultHandlerCodes::NOTICE_OF_CANCELLATION); + info.callCount++; + info.condCodes.push(code); +} + +void CfdpFaultHandlerMock::abandonCb(cfdp::ConditionCode code) { + auto& info = fhInfoMap.at(cfdp::FaultHandlerCodes::ABANDON_TRANSACTION); + info.callCount++; + info.condCodes.push(code); +} + +void CfdpFaultHandlerMock::ignoreCb(cfdp::ConditionCode code) { + auto& info = fhInfoMap.at(cfdp::FaultHandlerCodes::IGNORE_ERROR); + info.callCount++; + info.condCodes.push(code); +} + +CfdpFaultHandlerMock::FaultInfo& CfdpFaultHandlerMock::getFhInfo(cfdp::FaultHandlerCodes fhCode) { + return fhInfoMap.at(fhCode); +} + +void CfdpFaultHandlerMock::reset() { fhInfoMap.clear(); } + +bool CfdpFaultHandlerMock::faultCbWasCalled() const { + return std::any_of(fhInfoMap.begin(), fhInfoMap.end(), + [](const std::pair& pair) { + return pair.second.callCount > 0; + }); +} diff --git a/unittests/mocks/CfdpFaultHandlerMock.h b/unittests/mocks/CfdpFaultHandlerMock.h new file mode 100644 index 00000000..8003798e --- /dev/null +++ b/unittests/mocks/CfdpFaultHandlerMock.h @@ -0,0 +1,32 @@ +#ifndef FSFW_TESTS_CFDPFAULTHANDLERMOCK_H +#define FSFW_TESTS_CFDPFAULTHANDLERMOCK_H + +#include +#include + +#include "fsfw/cfdp/FaultHandlerBase.h" + +class CfdpFaultHandlerMock : public cfdp::FaultHandlerBase { + public: + struct FaultInfo { + size_t callCount = 0; + std::queue condCodes; + }; + + void noticeOfSuspensionCb(cfdp::ConditionCode code) override; + void noticeOfCancellationCb(cfdp::ConditionCode code) override; + void abandonCb(cfdp::ConditionCode code) override; + void ignoreCb(cfdp::ConditionCode code) override; + + FaultInfo& getFhInfo(cfdp::FaultHandlerCodes fhCode); + bool faultCbWasCalled() const; + void reset(); + + private: + std::map fhInfoMap = { + std::pair{cfdp::FaultHandlerCodes::IGNORE_ERROR, FaultInfo()}, + std::pair{cfdp::FaultHandlerCodes::NOTICE_OF_CANCELLATION, FaultInfo()}, + std::pair{cfdp::FaultHandlerCodes::NOTICE_OF_SUSPENSION, FaultInfo()}, + std::pair{cfdp::FaultHandlerCodes::ABANDON_TRANSACTION, FaultInfo()}}; +}; +#endif // FSFW_TESTS_CFDPFAULTHANDLERMOCK_H