From 7c875f106707ffa48f3ca256428a5cf880549217 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 4 Aug 2023 15:17:53 +0200 Subject: [PATCH] unittest for small file transfer done --- src/fsfw/cfdp/handler/DestHandler.cpp | 11 ++--- src/fsfw/cfdp/handler/SourceHandler.cpp | 2 +- src/fsfw/cfdp/pdu/FileDataInfo.cpp | 6 +-- src/fsfw/cfdp/pdu/FileDataInfo.h | 6 +-- unittests/cfdp/handler/testSourceHandler.cpp | 51 +++++++++++++++----- unittests/cfdp/pdu/testFileData.cpp | 3 +- 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/fsfw/cfdp/handler/DestHandler.cpp b/src/fsfw/cfdp/handler/DestHandler.cpp index 83801df7..4814917f 100644 --- a/src/fsfw/cfdp/handler/DestHandler.cpp +++ b/src/fsfw/cfdp/handler/DestHandler.cpp @@ -156,8 +156,7 @@ ReturnValue_t cfdp::DestHandler::handleFileDataPdu(const cfdp::PacketInfo& info) // TODO: This is not a CFDP error. Event and/or warning? return constAccessorPair.first; } - cfdp::Fss offset; - FileDataInfo fdInfo(offset); + FileDataInfo fdInfo; FileDataReader reader(constAccessorPair.second.data(), constAccessorPair.second.size(), fdInfo); ReturnValue_t result = reader.parseData(); if (result != OK) { @@ -166,10 +165,10 @@ ReturnValue_t cfdp::DestHandler::handleFileDataPdu(const cfdp::PacketInfo& info) size_t fileSegmentLen = 0; const uint8_t* fileData = fdInfo.getFileData(&fileSegmentLen); FileOpParams fileOpParams(transactionParams.destName.data(), fileSegmentLen); - fileOpParams.offset = offset.value(); + fileOpParams.offset = fdInfo.getOffset().value(); if (destParams.cfg.indicCfg.fileSegmentRecvIndicRequired) { FileSegmentRecvdParams segParams; - segParams.offset = offset.value(); + segParams.offset = fdInfo.getOffset().value(); segParams.id = transactionParams.transactionId; segParams.length = fileSegmentLen; segParams.recContState = fdInfo.getRecordContinuationState(); @@ -196,8 +195,8 @@ ReturnValue_t cfdp::DestHandler::handleFileDataPdu(const cfdp::PacketInfo& info) transactionParams.deliveryStatus = FileDeliveryStatus::RETAINED_IN_FILESTORE; transactionParams.vfsErrorCount = 0; } - if (offset.value() + fileSegmentLen > transactionParams.progress) { - transactionParams.progress = offset.value() + fileSegmentLen; + if (fdInfo.getOffset().value() + fileSegmentLen > transactionParams.progress) { + transactionParams.progress = fdInfo.getOffset().value() + fileSegmentLen; } return result; } diff --git a/src/fsfw/cfdp/handler/SourceHandler.cpp b/src/fsfw/cfdp/handler/SourceHandler.cpp index 5d6f0929..b35880c9 100644 --- a/src/fsfw/cfdp/handler/SourceHandler.cpp +++ b/src/fsfw/cfdp/handler/SourceHandler.cpp @@ -120,7 +120,7 @@ ReturnValue_t cfdp::SourceHandler::checksumGeneration() { std::array buf{}; etl::crc32 crcCalc; uint64_t currentOffset = 0; - FileOpParams params(transactionParams.destName.data(), transactionParams.fileSize.value()); + FileOpParams params(transactionParams.sourceName.data(), transactionParams.fileSize.value()); while (currentOffset < transactionParams.fileSize.value()) { uint64_t readLen; if (currentOffset + buf.size() > transactionParams.fileSize.value()) { diff --git a/src/fsfw/cfdp/pdu/FileDataInfo.cpp b/src/fsfw/cfdp/pdu/FileDataInfo.cpp index ec12e9da..8248a727 100644 --- a/src/fsfw/cfdp/pdu/FileDataInfo.cpp +++ b/src/fsfw/cfdp/pdu/FileDataInfo.cpp @@ -1,9 +1,9 @@ #include "FileDataInfo.h" -FileDataInfo::FileDataInfo(cfdp::Fss &offset, const uint8_t *fileData, size_t fileSize) - : offset(offset), fileData(fileData), fileSize(fileSize) {} +#include -FileDataInfo::FileDataInfo(cfdp::Fss &offset) : offset(offset) {} +FileDataInfo::FileDataInfo(cfdp::Fss offset, const uint8_t *fileData, size_t fileSize) + : offset(std::move(offset)), fileData(fileData), fileSize(fileSize) {} void FileDataInfo::setSegmentMetadataFlag(bool enable) { if (enable) { diff --git a/src/fsfw/cfdp/pdu/FileDataInfo.h b/src/fsfw/cfdp/pdu/FileDataInfo.h index 2721cda2..b2b975ad 100644 --- a/src/fsfw/cfdp/pdu/FileDataInfo.h +++ b/src/fsfw/cfdp/pdu/FileDataInfo.h @@ -6,8 +6,8 @@ class FileDataInfo { public: - explicit FileDataInfo(cfdp::Fss& offset); - FileDataInfo(cfdp::Fss& offset, const uint8_t* fileData, size_t fileSize); + FileDataInfo() = default; + FileDataInfo(cfdp::Fss offset, const uint8_t* fileData, size_t fileSize); [[nodiscard]] size_t getSerializedSize(bool largeFile = false) const; @@ -33,7 +33,7 @@ class FileDataInfo { private: cfdp::SegmentMetadataFlag segmentMetadataFlag = cfdp::SegmentMetadataFlag::NOT_PRESENT; cfdp::SegmentationControl segCtrl = cfdp::SegmentationControl::NO_RECORD_BOUNDARIES_PRESERVATION; - cfdp::Fss& offset; + cfdp::Fss offset; const uint8_t* fileData = nullptr; size_t fileSize = 0; cfdp::RecordContinuationState recContState = cfdp::RecordContinuationState::NO_START_NO_END; diff --git a/unittests/cfdp/handler/testSourceHandler.cpp b/unittests/cfdp/handler/testSourceHandler.cpp index a2e5e55f..e7f62def 100644 --- a/unittests/cfdp/handler/testSourceHandler.cpp +++ b/unittests/cfdp/handler/testSourceHandler.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -6,6 +8,7 @@ #include "fsfw/cfdp/handler/SourceHandler.h" #include "fsfw/cfdp/pdu/EofPduCreator.h" #include "fsfw/cfdp/pdu/EofPduReader.h" +#include "fsfw/cfdp/pdu/FileDataReader.h" #include "fsfw/cfdp/pdu/MetadataPduCreator.h" #include "fsfw/cfdp/pdu/MetadataPduReader.h" #include "fsfw/tmtcservices/TmTcMessage.h" @@ -57,15 +60,20 @@ TEST_CASE("CFDP Source Handler", "[cfdp]") { PutRequest putRequest(id, srcNameLv, destNameLv); CHECK(sourceHandler.initialize() == OK); - auto genericMetadataCheck = [&](SourceHandler::FsmResult& fsmResult, size_t expectedFileSize) { - TmTcMessage tmtcMessage; - const uint8_t* pduPtr; + auto onePduSentCheck = [&](SourceHandler::FsmResult& fsmResult, TmTcMessage& tmtcMessage, + const uint8_t** pduPtr) { CHECK(fsmResult.packetsSent == 1); CHECK(mqMock.numberOfSentMessages() == 1); REQUIRE(mqMock.getNextSentMessage(destQueueId, tmtcMessage) == OK); auto accessor = tmStore.getData(tmtcMessage.getStorageId()); REQUIRE(accessor.first == OK); - pduPtr = accessor.second.data(); + *pduPtr = accessor.second.data(); + return std::move(accessor); + }; + auto genericMetadataCheck = [&](SourceHandler::FsmResult& fsmResult, size_t expectedFileSize) { + TmTcMessage tmtcMessage; + const uint8_t* pduPtr; + auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr); CHECK(accessor.second.size() == 55); MetadataGenericInfo metadataInfo; MetadataPduReader metadataReader(pduPtr, accessor.second.size(), metadataInfo, nullptr, 0); @@ -87,12 +95,7 @@ TEST_CASE("CFDP Source Handler", "[cfdp]") { uint32_t expectedChecksum) { TmTcMessage tmtcMessage; const uint8_t* pduPtr; - CHECK(fsmResult.packetsSent == 1); - CHECK(mqMock.numberOfSentMessages() == 1); - REQUIRE(mqMock.getNextSentMessage(destQueueId, tmtcMessage) == OK); - auto accessor = tmStore.getData(tmtcMessage.getStorageId()); - REQUIRE(accessor.first == OK); - pduPtr = accessor.second.data(); + auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr); // 10 byte PDU header, 1 byte directive field, 1 byte condition code, 4 byte checksum, // 4 byte FSS CHECK(accessor.second.size() == 20); @@ -136,10 +139,32 @@ TEST_CASE("CFDP Source Handler", "[cfdp]") { // Verify metadata PDU was sent. genericMetadataCheck(fsmResult, expectedFileSize); fsmResult = sourceHandler.stateMachine(); - // TODO: Verify one file data PDU was sent. - CHECK(fsmResult.packetsSent == 1); + TmTcMessage tmtcMessage; + const uint8_t* pduPtr; + auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr); + FileDataInfo fdInfo; + FileDataReader fdReader(pduPtr, accessor.second.size(), fdInfo); + // 10 byte PDU header, 4 byte offset, 12 bytes file data. + CHECK(accessor.second.size() == 26); + CHECK(fdReader.parseData() == OK); + CHECK(fdInfo.getOffset().value() == 0); + size_t fileSize = 0; + const uint8_t* fileData = fdInfo.getFileData(&fileSize); + REQUIRE(fileSize == fileContent.size()); + CHECK(fileData != nullptr); + std::string dataReadBack(reinterpret_cast(fileData), fileSize); + CHECK(dataReadBack == fileContent); + mqMock.clearMessages(); - // TODO: Verify one EOF PDU was sent. + fsmResult = sourceHandler.stateMachine(); + + etl::crc32 crcCalc; + crcCalc.add(fileContent.data(), fileContent.data() + fileContent.size()); + // Verify EOF PDU was sent. + genericEofCheck(fsmResult, expectedFileSize, crcCalc.value()); + + CHECK(sourceHandler.getStep() == SourceHandler::TransactionStep::IDLE); + CHECK(sourceHandler.getState() == CfdpState::IDLE); } SECTION("Transfer two segment file") {} diff --git a/unittests/cfdp/pdu/testFileData.cpp b/unittests/cfdp/pdu/testFileData.cpp index 6eed1dd3..80b02603 100644 --- a/unittests/cfdp/pdu/testFileData.cpp +++ b/unittests/cfdp/pdu/testFileData.cpp @@ -107,8 +107,7 @@ TEST_CASE("File Data PDU", "[cfdp][pdu]") { serializer.serialize(&buffer, &sz, fileDataBuffer.size(), SerializeIF::Endianness::NETWORK); REQUIRE(result == returnvalue::OK); - Fss emptyOffset; - FileDataInfo emptyInfo(emptyOffset); + FileDataInfo emptyInfo; FileDataReader deserializer(fileDataBuffer.data(), fileDataBuffer.size(), emptyInfo); result = deserializer.parseData(); REQUIRE(result == returnvalue::OK);