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
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
#include "cfdp/PduSenderMock.h"
|
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"
|
2023-08-03 14:34:21 +02:00
|
|
|
#include "fsfw/tmtcservices/TmTcMessage.h"
|
2023-07-19 23:22:55 +02:00
|
|
|
#include "fsfw/util/SeqCountProvider.h"
|
2024-11-07 12:26:00 +01:00
|
|
|
#include "mock/AcceptsTmMock.h"
|
|
|
|
#include "mock/EventReportingProxyMock.h"
|
|
|
|
#include "mock/FilesystemMock.h"
|
|
|
|
#include "mock/MessageQueueMock.h"
|
|
|
|
#include "mock/StorageManagerMock.h"
|
|
|
|
#include "mock/cfdp/FaultHandlerMock.h"
|
|
|
|
#include "mock/cfdp/RemoteConfigTableMock.h"
|
|
|
|
#include "mock/cfdp/UserMock.h"
|
2023-07-17 11:37:20 +02:00
|
|
|
|
|
|
|
TEST_CASE("CFDP Source Handler", "[cfdp]") {
|
|
|
|
using namespace cfdp;
|
|
|
|
using namespace returnvalue;
|
2023-07-27 17:15:01 +02:00
|
|
|
using namespace std::filesystem;
|
2024-05-24 14:29:42 +02:00
|
|
|
constexpr size_t MAX_FILE_SEGMENT_SIZE = 255;
|
2023-08-14 11:26:57 +02:00
|
|
|
|
2023-07-17 11:37:20 +02:00
|
|
|
MessageQueueId_t destQueueId = 2;
|
|
|
|
AcceptsTmMock tmReceiver(destQueueId);
|
2024-05-24 14:29:42 +02:00
|
|
|
// MessageQueueMock mqMock(destQueueId);
|
|
|
|
auto localId = EntityId(UnsignedByteField<uint16_t>(2));
|
|
|
|
auto 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);
|
2023-07-19 23:22:55 +02:00
|
|
|
SeqCountProviderU16 seqCountProvider;
|
|
|
|
SourceHandlerParams dp(localEntityCfg, userMock, seqCountProvider);
|
2023-07-17 11:37:20 +02:00
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
PduSenderMock pduSender;
|
2023-07-17 11:37:20 +02:00
|
|
|
EventReportingProxyMock eventReporterMock;
|
|
|
|
LocalPool::LocalPoolConfig storeCfg = {{10, 32}, {10, 64}, {10, 128}, {10, 1024}};
|
2024-05-24 14:29:42 +02:00
|
|
|
FsfwParams fp(&eventReporterMock);
|
|
|
|
auto sourceHandler = SourceHandler(pduSender, 4096, 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";
|
2024-05-24 14:29:42 +02:00
|
|
|
fsMock.createFile(srcFileName.c_str());
|
|
|
|
cfdp::StringLv srcNameLv(srcFileName.c_str(), srcFileName.length());
|
2023-08-03 17:43:03 +02:00
|
|
|
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
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
auto onePduSentCheck = [&](const SourceHandler::FsmResult& fsmResult) {
|
2023-08-14 12:19:55 +02:00
|
|
|
CHECK(fsmResult.errors == 0);
|
2023-08-03 17:43:03 +02:00
|
|
|
CHECK(fsmResult.packetsSent == 1);
|
2023-08-04 15:17:53 +02:00
|
|
|
};
|
2023-08-14 21:01:29 +02:00
|
|
|
auto genericMetadataCheck = [&](const SourceHandler::FsmResult& fsmResult,
|
|
|
|
size_t expectedFileSize, uint16_t expectedSeqNum) {
|
2023-08-14 12:19:55 +02:00
|
|
|
CHECK(fsmResult.errors == 0);
|
2024-05-24 14:29:42 +02:00
|
|
|
auto optNextPacket = pduSender.getNextSentPacket();
|
|
|
|
CHECK(optNextPacket.has_value());
|
|
|
|
const auto& [pduType, fileDirective, rawPdu] = *optNextPacket;
|
|
|
|
CHECK(rawPdu.size() == 55);
|
2023-08-03 17:43:03 +02:00
|
|
|
MetadataGenericInfo metadataInfo;
|
2024-05-24 14:29:42 +02:00
|
|
|
MetadataPduReader metadataReader(rawPdu.data(), rawPdu.size(), metadataInfo, nullptr, 0);
|
2023-08-03 17:43:03 +02:00
|
|
|
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());
|
|
|
|
};
|
2023-08-14 21:01:29 +02:00
|
|
|
auto genericEofCheck = [&](const SourceHandler::FsmResult& fsmResult, size_t expectedFileSize,
|
2023-08-14 13:40:25 +02:00
|
|
|
uint32_t expectedChecksum, uint16_t expectedSeqNum) {
|
2024-05-24 14:29:42 +02:00
|
|
|
onePduSentCheck(fsmResult);
|
2023-08-03 17:43:03 +02:00
|
|
|
TmTcMessage tmtcMessage;
|
2024-05-24 14:29:42 +02:00
|
|
|
auto optNextPacket = pduSender.getNextSentPacket();
|
|
|
|
CHECK(optNextPacket.has_value());
|
|
|
|
const auto& [pduType, fileDirective, rawPdu] = *optNextPacket;
|
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
|
2024-05-24 14:29:42 +02:00
|
|
|
CHECK(rawPdu.size() == 20);
|
2023-08-03 17:43:03 +02:00
|
|
|
EofInfo eofInfo;
|
2024-05-24 14:29:42 +02:00
|
|
|
EofPduReader eofReader(rawPdu.data(), rawPdu.size(), eofInfo);
|
2023-08-03 17:43:03 +02:00
|
|
|
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);
|
2023-08-03 17:43:03 +02:00
|
|
|
CHECK(eofInfo.getChecksum() == expectedChecksum);
|
|
|
|
CHECK(eofInfo.getConditionCode() == ConditionCode::NO_ERROR);
|
|
|
|
CHECK(eofInfo.getFileSize().value() == expectedFileSize);
|
|
|
|
};
|
2023-08-14 21:01:29 +02:00
|
|
|
auto genericNoticeOfCompletionCheck = [&](const SourceHandler::FsmResult& fsmResult,
|
2024-05-24 14:29:42 +02:00
|
|
|
const uint16_t expectedSeqNum) {
|
2023-08-14 21:01:29 +02:00
|
|
|
CHECK(userMock.finishedRecvd.size() == 1);
|
|
|
|
CHECK(userMock.finishedRecvd.back().first ==
|
|
|
|
TransactionId(localId, TransactionSeqNum(cfdp::WidthInBytes::TWO_BYTES, expectedSeqNum)));
|
|
|
|
CHECK(sourceHandler.getStep() == SourceHandler::TransactionStep::IDLE);
|
|
|
|
CHECK(sourceHandler.getState() == CfdpState::IDLE);
|
|
|
|
};
|
|
|
|
|
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;
|
2024-05-24 14:29:42 +02:00
|
|
|
const SourceHandler::FsmResult& fsmResult = sourceHandler.stateMachineNoPacket();
|
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
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
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-14 21:01:29 +02:00
|
|
|
// Verify notice of completion.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
2023-08-14 21:01:29 +02:00
|
|
|
genericNoticeOfCompletionCheck(fsmResult, 0);
|
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;
|
2024-05-24 14:29:42 +02:00
|
|
|
fsMock.createFile(srcFileName.c_str());
|
2023-08-03 17:43:03 +02:00
|
|
|
std::string fileContent = "hello world\n";
|
|
|
|
size_t expectedFileSize = fileContent.size();
|
2024-05-24 14:29:42 +02:00
|
|
|
fsMock.writeToFile(srcFileName.c_str(), 0, reinterpret_cast<const uint8_t*>(fileContent.data()),
|
|
|
|
expectedFileSize);
|
2023-08-03 17:43:03 +02:00
|
|
|
CHECK(sourceHandler.transactionStart(putRequest, cfg) == OK);
|
2024-05-24 14:29:42 +02:00
|
|
|
const SourceHandler::FsmResult& fsmResult = sourceHandler.stateMachineNoPacket();
|
2023-08-03 17:43:03 +02:00
|
|
|
|
|
|
|
// 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.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
|
|
|
|
|
|
|
onePduSentCheck(fsmResult);
|
|
|
|
auto optNextPacket = pduSender.getNextSentPacket();
|
|
|
|
CHECK(optNextPacket.has_value());
|
|
|
|
const auto& [pduType, fileDirective, rawPdu] = *optNextPacket;
|
2023-08-04 15:17:53 +02:00
|
|
|
FileDataInfo fdInfo;
|
2024-05-24 14:29:42 +02:00
|
|
|
FileDataReader fdReader(rawPdu.data(), rawPdu.size(), fdInfo);
|
2023-08-04 15:17:53 +02:00
|
|
|
// 10 byte PDU header, 4 byte offset, 12 bytes file data.
|
2024-05-24 14:29:42 +02:00
|
|
|
CHECK(rawPdu.size() == 26);
|
2023-08-04 15:17:53 +02:00
|
|
|
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);
|
2023-08-03 17:43:03 +02:00
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
2023-08-04 15:17:53 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-14 21:01:29 +02:00
|
|
|
// Verify notice of completion.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
2023-08-14 21:01:29 +02:00
|
|
|
genericNoticeOfCompletionCheck(fsmResult, expectedSeqNum);
|
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();
|
2024-05-24 14:29:42 +02:00
|
|
|
fsMock.createFile(srcFileName.c_str());
|
|
|
|
fsMock.writeToFile(srcFileName.c_str(), 0, largerFileData.data(), largerFileData.size());
|
2023-08-14 11:26:57 +02:00
|
|
|
CHECK(sourceHandler.transactionStart(putRequest, cfg) == OK);
|
2024-05-24 14:29:42 +02:00
|
|
|
const SourceHandler::FsmResult& fsmResult = sourceHandler.stateMachineNoPacket();
|
2023-08-14 11:26:57 +02:00
|
|
|
// 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.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
|
|
|
|
|
|
|
onePduSentCheck(fsmResult);
|
|
|
|
auto optNextPacket = pduSender.getNextSentPacket();
|
|
|
|
CHECK(optNextPacket.has_value());
|
|
|
|
const auto& [pduType, fileDirective, rawPdu] = *optNextPacket;
|
2023-08-14 11:26:57 +02:00
|
|
|
FileDataInfo fdInfo;
|
|
|
|
{
|
2024-05-24 14:29:42 +02:00
|
|
|
// auto accessor = onePduSentCheck(fsmResult, tmtcMessage, &pduPtr);
|
|
|
|
FileDataReader fdReader(rawPdu.data(), rawPdu.size(), fdInfo);
|
2023-08-14 11:26:57 +02:00
|
|
|
// 10 byte PDU header, 4 byte offset, 255 byte file data
|
2024-05-24 14:29:42 +02:00
|
|
|
CHECK(rawPdu.size() == 269);
|
2023-08-14 11:26:57 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check second file data PDU.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
2023-08-14 21:04:22 +02:00
|
|
|
{
|
2024-05-24 14:29:42 +02:00
|
|
|
optNextPacket = pduSender.getNextSentPacket();
|
|
|
|
CHECK(optNextPacket.has_value());
|
|
|
|
const auto& [pduType, fileDirective, rawPdu] = *optNextPacket;
|
|
|
|
FileDataReader fdReader(rawPdu.data(), rawPdu.size(), fdInfo);
|
2023-08-14 21:04:22 +02:00
|
|
|
// 10 byte PDU header, 4 byte offset, remaining file data (400 - 255 == 145).
|
2024-05-24 14:29:42 +02:00
|
|
|
CHECK(rawPdu.size() == 10 + 4 + largerFileData.size() - MAX_FILE_SEGMENT_SIZE);
|
2023-08-14 21:04:22 +02:00
|
|
|
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 11:26:57 +02:00
|
|
|
}
|
2023-08-14 12:15:35 +02:00
|
|
|
|
|
|
|
// Check EOF and verify checksum.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
2023-08-14 12:15:35 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-14 21:01:29 +02:00
|
|
|
// Verify notice of completion.
|
2024-05-24 14:29:42 +02:00
|
|
|
sourceHandler.stateMachineNoPacket();
|
2023-08-14 21:01:29 +02:00
|
|
|
genericNoticeOfCompletionCheck(fsmResult, expectedSeqNum);
|
2023-08-14 11:26:57 +02:00
|
|
|
}
|
2023-07-17 11:37:20 +02:00
|
|
|
}
|