From 4ee01e395bd4dc72f1aec1fc0dbb7880af5fef4f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 9 Aug 2022 15:29:22 +0200 Subject: [PATCH] finished fault handler unit tests --- src/fsfw/cfdp/FaultHandlerBase.cpp | 11 ++++- unittests/cfdp/handler/testFaultHandler.cpp | 55 ++++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/fsfw/cfdp/FaultHandlerBase.cpp b/src/fsfw/cfdp/FaultHandlerBase.cpp index 443a1a92..1dbfce0d 100644 --- a/src/fsfw/cfdp/FaultHandlerBase.cpp +++ b/src/fsfw/cfdp/FaultHandlerBase.cpp @@ -19,6 +19,12 @@ bool FaultHandlerBase::setFaultHandler(cfdp::ConditionCode code, cfdp::FaultHand if (not faultHandlerMap.contains(code)) { return false; } + if (handler != FaultHandlerCodes::NOTICE_OF_SUSPENSION and + handler != FaultHandlerCodes::ABANDON_TRANSACTION and + handler != FaultHandlerCodes::NOTICE_OF_CANCELLATION and + handler != FaultHandlerCodes::IGNORE_ERROR) { + return false; + } faultHandlerMap[code] = handler; return true; } @@ -34,8 +40,11 @@ bool FaultHandlerBase::reportFault(cfdp::ConditionCode code) { abandonCb(code); } else if (fh == cfdp::FaultHandlerCodes::NOTICE_OF_CANCELLATION) { noticeOfCancellationCb(code); - } else { + } else if (fh == cfdp::FaultHandlerCodes::NOTICE_OF_SUSPENSION) { noticeOfSuspensionCb(code); + } else { + // Should never happen, but use defensive programming + return false; } return true; } diff --git a/unittests/cfdp/handler/testFaultHandler.cpp b/unittests/cfdp/handler/testFaultHandler.cpp index bea16889..e6fb4547 100644 --- a/unittests/cfdp/handler/testFaultHandler.cpp +++ b/unittests/cfdp/handler/testFaultHandler.cpp @@ -6,6 +6,7 @@ TEST_CASE("CFDP Fault Handler", "[cfdp]") { using namespace cfdp; auto fhMock = CfdpFaultHandlerMock(); cfdp::FaultHandlerCodes fhCode; + SECTION("State") { // Verify initial condition CHECK(fhMock.getFaultHandler(ConditionCode::UNSUPPORTED_CHECKSUM_TYPE, fhCode)); @@ -30,7 +31,57 @@ TEST_CASE("CFDP Fault Handler", "[cfdp]") { CHECK(fhCode == FaultHandlerCodes::IGNORE_ERROR); } - SECTION("Call Handler") { - fhMock.reportFault(ConditionCode::CHECK_LIMIT_REACHED); + SECTION("Call Handler, Ignore Fault") { + auto& info = fhMock.getFhInfo(FaultHandlerCodes::IGNORE_ERROR); + CHECK(fhMock.reportFault(ConditionCode::CHECK_LIMIT_REACHED)); + CHECK(info.callCount == 1); + CHECK(info.condCodes.back() == ConditionCode::CHECK_LIMIT_REACHED); + fhMock.reportFault(ConditionCode::FILE_CHECKSUM_FAILURE); + CHECK(info.callCount == 2); + CHECK(info.condCodes.back() == ConditionCode::FILE_CHECKSUM_FAILURE); + } + + SECTION("Invalid Reported Code") { CHECK(not fhMock.reportFault(ConditionCode::NO_ERROR)); } + + SECTION("Invalid FH code"){ + CHECK(not fhMock.setFaultHandler(ConditionCode::KEEP_ALIVE_LIMIT_REACHED, FaultHandlerCodes::RESERVED)); + CHECK(fhMock.getFaultHandler(ConditionCode::KEEP_ALIVE_LIMIT_REACHED, fhCode)); + CHECK(fhCode == FaultHandlerCodes::IGNORE_ERROR); + CHECK(not fhMock.setFaultHandler(ConditionCode::NO_ERROR, FaultHandlerCodes::IGNORE_ERROR)); + CHECK(not fhMock.getFaultHandler(ConditionCode::NO_ERROR, fhCode)); + } + + SECTION("Set Other Fault Handler") { + CHECK(fhMock.setFaultHandler(ConditionCode::FILE_CHECKSUM_FAILURE, + FaultHandlerCodes::NOTICE_OF_CANCELLATION)); + CHECK(fhMock.setFaultHandler(ConditionCode::INACTIVITY_DETECTED, + FaultHandlerCodes::ABANDON_TRANSACTION)); + CHECK(fhMock.setFaultHandler(ConditionCode::KEEP_ALIVE_LIMIT_REACHED, + FaultHandlerCodes::NOTICE_OF_SUSPENSION)); + auto& ignoreInfo = fhMock.getFhInfo(FaultHandlerCodes::IGNORE_ERROR); + auto& cancellationInfo = fhMock.getFhInfo(FaultHandlerCodes::NOTICE_OF_CANCELLATION); + auto& suspensionInfo = fhMock.getFhInfo(FaultHandlerCodes::NOTICE_OF_SUSPENSION); + auto& abandonInfo = fhMock.getFhInfo(FaultHandlerCodes::ABANDON_TRANSACTION); + + CHECK(fhMock.reportFault(ConditionCode::FILE_CHECKSUM_FAILURE)); + CHECK(cancellationInfo.callCount == 1); + CHECK(cancellationInfo.condCodes.back() == ConditionCode::FILE_CHECKSUM_FAILURE); + CHECK(ignoreInfo.callCount == 0); + CHECK(suspensionInfo.callCount == 0); + CHECK(abandonInfo.callCount == 0); + + CHECK(fhMock.reportFault(ConditionCode::INACTIVITY_DETECTED)); + CHECK(cancellationInfo.callCount == 1); + CHECK(ignoreInfo.callCount == 0); + CHECK(suspensionInfo.callCount == 0); + CHECK(abandonInfo.callCount == 1); + CHECK(abandonInfo.condCodes.back() == ConditionCode::INACTIVITY_DETECTED); + + CHECK(fhMock.reportFault(ConditionCode::KEEP_ALIVE_LIMIT_REACHED)); + CHECK(cancellationInfo.callCount == 1); + CHECK(ignoreInfo.callCount == 0); + CHECK(suspensionInfo.callCount == 1); + CHECK(suspensionInfo.condCodes.back() == ConditionCode::KEEP_ALIVE_LIMIT_REACHED); + CHECK(abandonInfo.callCount == 1); } } \ No newline at end of file