fsfw/unittests/cfdp/handler/testSourceHandler.cpp

269 lines
11 KiB
C++
Raw Normal View History

2023-08-04 15:17:53 +02:00
#include <etl/crc32.h>
2022-09-14 19:29:43 +02:00
#include <catch2/catch_test_macros.hpp>
2023-07-27 17:15:01 +02:00
#include <filesystem>
2023-08-14 11:26:57 +02:00
#include <random>
2022-09-14 19:29:43 +02:00
2023-07-17 11:37:20 +02:00
#include "fsfw/cfdp.h"
2023-07-27 17:15:01 +02:00
#include "fsfw/cfdp/handler/PutRequest.h"
2023-07-17 11:37:20 +02:00
#include "fsfw/cfdp/handler/SourceHandler.h"
#include "fsfw/cfdp/pdu/EofPduCreator.h"
2023-08-03 15:49:23 +02:00
#include "fsfw/cfdp/pdu/EofPduReader.h"
2023-08-04 15:17:53 +02:00
#include "fsfw/cfdp/pdu/FileDataReader.h"
2023-07-17 11:37:20 +02:00
#include "fsfw/cfdp/pdu/MetadataPduCreator.h"
2023-08-03 13:57:55 +02:00
#include "fsfw/cfdp/pdu/MetadataPduReader.h"
#include "fsfw/tmtcservices/TmTcMessage.h"
#include "fsfw/util/SeqCountProvider.h"
2023-07-17 11:37:20 +02:00
#include "mocks/AcceptsTmMock.h"
#include "mocks/EventReportingProxyMock.h"
#include "mocks/FilesystemMock.h"
#include "mocks/MessageQueueMock.h"
#include "mocks/StorageManagerMock.h"
#include "mocks/cfdp/FaultHandlerMock.h"
#include "mocks/cfdp/RemoteConfigTableMock.h"
#include "mocks/cfdp/UserMock.h"
TEST_CASE("CFDP Source Handler", "[cfdp]") {
using namespace cfdp;
using namespace returnvalue;
2023-07-27 17:15:01 +02:00
using namespace std::filesystem;
2023-08-14 11:26:57 +02:00
const size_t MAX_FILE_SEGMENT_SIZE = 255;
2023-07-17 11:37:20 +02:00
MessageQueueId_t destQueueId = 2;
AcceptsTmMock tmReceiver(destQueueId);
MessageQueueMock mqMock(destQueueId);
EntityId localId = EntityId(UnsignedByteField<uint16_t>(2));
2023-08-14 13:33:47 +02:00
EntityId remoteId = EntityId(UnsignedByteField<uint16_t>(5));
2023-07-17 11:37:20 +02:00
FaultHandlerMock fhMock;
LocalEntityCfg localEntityCfg(localId, IndicationCfg(), fhMock);
FilesystemMock fsMock;
UserMock userMock(fsMock);
SeqCountProviderU16 seqCountProvider;
SourceHandlerParams dp(localEntityCfg, userMock, seqCountProvider);
2023-07-17 11:37:20 +02:00
EventReportingProxyMock eventReporterMock;
LocalPool::LocalPoolConfig storeCfg = {{10, 32}, {10, 64}, {10, 128}, {10, 1024}};
StorageManagerMock tcStore(2, storeCfg);
StorageManagerMock tmStore(3, storeCfg);
FsfwParams fp(tmReceiver, &mqMock, &eventReporterMock);
2023-08-03 13:03:58 +02:00
fp.tcStore = &tcStore;
fp.tmStore = &tmStore;
2023-07-17 11:37:20 +02:00
auto sourceHandler = SourceHandler(dp, fp);
2023-08-03 17:43:03 +02:00
RemoteEntityCfg cfg;
2023-08-14 11:26:57 +02:00
cfg.maxFileSegmentLen = MAX_FILE_SEGMENT_SIZE;
2023-08-14 13:33:47 +02:00
cfg.remoteId = remoteId;
2023-08-03 17:43:03 +02:00
std::string srcFileName = "/tmp/cfdp-test.txt";
std::string destFileName = "/tmp/cfdp-test2.txt";
FilesystemParams srcFileNameFs(srcFileName.c_str());
fsMock.createFile(srcFileNameFs);
cfdp::StringLv srcNameLv(srcFileNameFs.path, std::strlen(srcFileNameFs.path));
FilesystemParams destFileNameFs(destFileName.c_str());
cfdp::StringLv destNameLv(destFileNameFs.path, std::strlen(destFileNameFs.path));
2023-08-14 13:33:47 +02:00
PutRequest putRequest(remoteId, srcNameLv, destNameLv);
2023-08-03 13:03:58 +02:00
CHECK(sourceHandler.initialize() == OK);
2023-07-27 17:15:01 +02:00
2023-08-04 15:17:53 +02:00
auto onePduSentCheck = [&](SourceHandler::FsmResult& fsmResult, TmTcMessage& tmtcMessage,
const uint8_t** pduPtr) {
2023-08-14 12:19:55 +02:00
CHECK(fsmResult.errors == 0);
2023-08-03 17:43:03 +02:00
CHECK(fsmResult.packetsSent == 1);
CHECK(mqMock.numberOfSentMessages() == 1);
REQUIRE(mqMock.getNextSentMessage(destQueueId, tmtcMessage) == OK);
auto accessor = tmStore.getData(tmtcMessage.getStorageId());
REQUIRE(accessor.first == OK);
2023-08-04 15:17:53 +02:00
*pduPtr = accessor.second.data();
return std::move(accessor);
};
2023-08-14 13:24:22 +02:00
auto genericMetadataCheck = [&](SourceHandler::FsmResult& fsmResult, size_t expectedFileSize,
uint16_t expectedSeqNum) {
2023-08-14 12:19:55 +02:00
CHECK(fsmResult.errors == 0);
2023-08-04 15:17:53 +02:00
TmTcMessage tmtcMessage;
const uint8_t* pduPtr;
auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr);
2023-08-03 17:43:03 +02:00
CHECK(accessor.second.size() == 55);
MetadataGenericInfo metadataInfo;
MetadataPduReader metadataReader(pduPtr, accessor.second.size(), metadataInfo, nullptr, 0);
REQUIRE(metadataReader.parseData() == OK);
std::string srcNameRead = metadataReader.getSourceFileName().getString();
CHECK(srcNameRead == srcFileName);
2023-08-14 13:24:22 +02:00
TransactionSeqNum seqNum;
metadataReader.getTransactionSeqNum(seqNum);
CHECK(seqNum.getValue() == expectedSeqNum);
2023-08-14 13:33:47 +02:00
CHECK(userMock.transactionIndicRecvd.size() == 1);
CHECK(userMock.transactionIndicRecvd.back() == TransactionId(localId, seqNum));
EntityId srcId;
metadataReader.getSourceId(srcId);
EntityId destId;
metadataReader.getDestId(destId);
CHECK(srcId.getValue() == localId.getValue());
CHECK(destId.getValue() == remoteId.getValue());
2023-08-03 17:43:03 +02:00
std::string destNameRead = metadataReader.getDestFileName().getString();
CHECK(destNameRead == destFileName);
if (expectedFileSize == 0) {
CHECK(metadataInfo.getChecksumType() == ChecksumType::NULL_CHECKSUM);
} else {
CHECK(metadataInfo.getChecksumType() == ChecksumType::CRC_32);
}
CHECK(metadataInfo.getFileSize().value() == expectedFileSize);
CHECK(!metadataInfo.isClosureRequested());
mqMock.clearMessages();
};
auto genericEofCheck = [&](SourceHandler::FsmResult& fsmResult, size_t expectedFileSize,
2023-08-14 13:40:25 +02:00
uint32_t expectedChecksum, uint16_t expectedSeqNum) {
2023-08-14 12:19:55 +02:00
CHECK(fsmResult.errors == 0);
2023-08-03 17:43:03 +02:00
TmTcMessage tmtcMessage;
const uint8_t* pduPtr;
2023-08-04 15:17:53 +02:00
auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr);
2023-08-03 17:43:03 +02:00
// 10 byte PDU header, 1 byte directive field, 1 byte condition code, 4 byte checksum,
// 4 byte FSS
CHECK(accessor.second.size() == 20);
EofInfo eofInfo;
EofPduReader eofReader(pduPtr, accessor.second.size(), eofInfo);
REQUIRE(eofReader.parseData() == OK);
2023-08-14 13:40:25 +02:00
TransactionSeqNum seqNum;
eofReader.getTransactionSeqNum(seqNum);
CHECK(seqNum.getValue() == expectedSeqNum);
2023-08-14 13:45:13 +02:00
auto transactionId = TransactionId(localId, seqNum);
2023-08-14 13:40:25 +02:00
CHECK(userMock.eofSentRecvd.size() == 1);
2023-08-14 13:45:13 +02:00
CHECK(userMock.eofSentRecvd.back() == transactionId);
CHECK(userMock.finishedRecvd.size() == 1);
CHECK(userMock.finishedRecvd.back().first == transactionId);
2023-08-03 17:43:03 +02:00
CHECK(eofInfo.getChecksum() == expectedChecksum);
CHECK(eofInfo.getConditionCode() == ConditionCode::NO_ERROR);
CHECK(eofInfo.getFileSize().value() == expectedFileSize);
};
2023-07-27 17:15:01 +02:00
SECTION("Test Basic") {
CHECK(sourceHandler.getState() == CfdpState::IDLE);
CHECK(sourceHandler.getStep() == SourceHandler::TransactionStep::IDLE);
}
SECTION("Transfer empty file") {
CHECK(sourceHandler.transactionStart(putRequest, cfg) == OK);
2023-08-03 15:30:38 +02:00
2023-08-03 17:44:30 +02:00
size_t expectedFileSize = 0;
2023-08-03 17:43:03 +02:00
SourceHandler::FsmResult& fsmResult = sourceHandler.stateMachine();
2023-08-03 15:49:23 +02:00
// Verify metadata PDU was sent.
2023-08-14 13:24:22 +02:00
genericMetadataCheck(fsmResult, expectedFileSize, 0);
2023-08-03 15:30:38 +02:00
2023-08-03 17:43:03 +02:00
fsmResult = sourceHandler.stateMachine();
2023-08-03 17:44:30 +02:00
// Verify EOF PDU was sent. No file data PDU is sent for an empty file and the checksum is 0.
2023-08-14 13:40:25 +02:00
genericEofCheck(fsmResult, expectedFileSize, 0, 0);
2023-08-03 17:43:03 +02:00
2023-08-03 15:49:23 +02:00
CHECK(sourceHandler.getStep() == SourceHandler::TransactionStep::IDLE);
CHECK(sourceHandler.getState() == CfdpState::IDLE);
2023-07-27 17:15:01 +02:00
}
2023-08-03 15:50:27 +02:00
2023-08-03 17:43:03 +02:00
SECTION("Transfer small file") {
2023-08-14 13:40:25 +02:00
uint16_t expectedSeqNum = 0;
2023-08-03 17:43:03 +02:00
fsMock.createFile(srcFileNameFs);
std::string fileContent = "hello world\n";
size_t expectedFileSize = fileContent.size();
FileOpParams params(srcFileName.c_str(), expectedFileSize);
fsMock.writeToFile(params, reinterpret_cast<const uint8_t*>(fileContent.data()));
CHECK(sourceHandler.transactionStart(putRequest, cfg) == OK);
SourceHandler::FsmResult& fsmResult = sourceHandler.stateMachine();
// Verify metadata PDU was sent.
2023-08-14 13:40:25 +02:00
genericMetadataCheck(fsmResult, expectedFileSize, expectedSeqNum);
2023-08-14 11:26:57 +02:00
// Verify that a small file data PDU was sent.
2023-08-03 17:43:03 +02:00
fsmResult = sourceHandler.stateMachine();
2023-08-04 15:17:53 +02:00
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<const char*>(fileData), fileSize);
CHECK(dataReadBack == fileContent);
mqMock.clearMessages();
2023-08-03 17:43:03 +02:00
2023-08-04 15:17:53 +02:00
fsmResult = sourceHandler.stateMachine();
etl::crc32 crcCalc;
crcCalc.add(fileContent.data(), fileContent.data() + fileContent.size());
// Verify EOF PDU was sent.
2023-08-14 13:40:25 +02:00
genericEofCheck(fsmResult, expectedFileSize, crcCalc.value(), expectedSeqNum);
2023-08-04 15:17:53 +02:00
CHECK(sourceHandler.getStep() == SourceHandler::TransactionStep::IDLE);
CHECK(sourceHandler.getState() == CfdpState::IDLE);
2023-08-03 17:43:03 +02:00
}
2023-08-14 11:26:57 +02:00
SECTION("Transfer two segment file") {
2023-08-14 13:40:25 +02:00
uint16_t expectedSeqNum = 0;
2023-08-14 11:26:57 +02:00
// Create 400 bytes of random data. This should result in two file segments, with one
// having the maximum size.
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distU8(0, 255);
std::array<uint8_t, 400> largerFileData{};
for (auto& val : largerFileData) {
val = distU8(rng);
}
size_t expectedFileSize = largerFileData.size();
fsMock.createFile(srcFileNameFs);
FileOpParams params(srcFileName.c_str(), expectedFileSize);
fsMock.writeToFile(params, reinterpret_cast<const uint8_t*>(largerFileData.data()));
CHECK(sourceHandler.transactionStart(putRequest, cfg) == OK);
SourceHandler::FsmResult& fsmResult = sourceHandler.stateMachine();
// Verify metadata PDU was sent.
2023-08-14 13:40:25 +02:00
genericMetadataCheck(fsmResult, expectedFileSize, expectedSeqNum);
2023-08-14 11:26:57 +02:00
// Check first file data PDU. It should have the maximum file segment size.
fsmResult = sourceHandler.stateMachine();
TmTcMessage tmtcMessage;
const uint8_t* pduPtr;
FileDataInfo fdInfo;
{
auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr);
2023-08-14 11:26:57 +02:00
FileDataReader fdReader(pduPtr, accessor.second.size(), fdInfo);
// 10 byte PDU header, 4 byte offset, 255 byte file data
CHECK(accessor.second.size() == 269);
CHECK(fdReader.parseData() == OK);
CHECK(fdInfo.getOffset().value() == 0);
size_t fileSize = 0;
const uint8_t* fileData = fdInfo.getFileData(&fileSize);
// Maximum file segment size.
REQUIRE(fileSize == MAX_FILE_SEGMENT_SIZE);
for (unsigned i = 0; i < fileSize; i++) {
CHECK(fileData[i] == largerFileData[i]);
}
}
mqMock.clearMessages();
// Check second file data PDU.
fsmResult = sourceHandler.stateMachine();
auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr);
2023-08-14 11:26:57 +02:00
FileDataReader fdReader(pduPtr, accessor.second.size(), fdInfo);
// 10 byte PDU header, 4 byte offset, remaining file data (400 - 255 == 145).
CHECK(accessor.second.size() == 10 + 4 + largerFileData.size() - MAX_FILE_SEGMENT_SIZE);
CHECK(fdReader.parseData() == OK);
CHECK(fdInfo.getOffset().value() == MAX_FILE_SEGMENT_SIZE);
size_t fileDataSize = 0;
const uint8_t* fileData = fdInfo.getFileData(&fileDataSize);
// Maximum file segment size.
REQUIRE(fileDataSize == largerFileData.size() - MAX_FILE_SEGMENT_SIZE);
for (unsigned i = 0; i < fileDataSize; i++) {
CHECK(fileData[i] == largerFileData[MAX_FILE_SEGMENT_SIZE + i]);
}
2023-08-14 12:15:35 +02:00
mqMock.clearMessages();
// Check EOF and verify checksum.
fsmResult = sourceHandler.stateMachine();
etl::crc32 crcCalc;
crcCalc.add(largerFileData.data(), largerFileData.data() + largerFileData.size());
// Verify EOF PDU was sent.
2023-08-14 13:40:25 +02:00
genericEofCheck(fsmResult, expectedFileSize, crcCalc.value(), expectedSeqNum);
2023-08-14 12:15:35 +02:00
CHECK(sourceHandler.getStep() == SourceHandler::TransactionStep::IDLE);
CHECK(sourceHandler.getState() == CfdpState::IDLE);
2023-08-14 11:26:57 +02:00
}
2023-07-17 11:37:20 +02:00
}