fsfw/unittests/cfdp/handler/testDestHandler.cpp

222 lines
9.4 KiB
C++
Raw Permalink Normal View History

2022-09-14 19:29:43 +02:00
#include <etl/crc32.h>
#include <catch2/catch_test_macros.hpp>
#include <random>
#include <utility>
2024-05-24 14:29:42 +02:00
#include "OwnedPduPacket.h"
#include "cfdp/PduSenderMock.h"
2022-09-14 19:29:43 +02:00
#include "fsfw/cfdp.h"
#include "fsfw/cfdp/pdu/EofPduCreator.h"
#include "fsfw/cfdp/pdu/FileDataCreator.h"
#include "fsfw/cfdp/pdu/MetadataPduCreator.h"
#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"
2022-09-14 19:29:43 +02:00
TEST_CASE("CFDP Dest Handler", "[cfdp]") {
using namespace cfdp;
using namespace returnvalue;
MessageQueueId_t destQueueId = 2;
AcceptsTmMock tmReceiver(destQueueId);
MessageQueueMock mqMock(destQueueId, MessageQueueIF::NO_QUEUE);
2024-05-24 14:29:42 +02:00
auto localId = EntityId(UnsignedByteField<uint16_t>(2));
auto remoteId = EntityId(UnsignedByteField<uint16_t>(3));
2022-09-14 19:29:43 +02:00
FaultHandlerMock fhMock;
LocalEntityCfg localEntityCfg(localId, IndicationCfg(), fhMock);
FilesystemMock fsMock;
UserMock userMock(fsMock);
RemoteConfigTableMock remoteCfgTableMock;
LostSegmentsList<128> lostSegmentsList;
2024-05-24 14:29:42 +02:00
DestHandlerParams dp(localEntityCfg, userMock, remoteCfgTableMock, lostSegmentsList);
2022-09-14 19:29:43 +02:00
EventReportingProxyMock eventReporterMock;
LocalPool::LocalPoolConfig storeCfg = {{10, 32}, {10, 64}, {10, 128}, {10, 1024}};
2024-05-24 14:29:42 +02:00
PduSenderMock senderMock;
FsfwParams fp(&eventReporterMock);
2022-09-14 19:29:43 +02:00
RemoteEntityCfg cfg(remoteId);
remoteCfgTableMock.addRemoteConfig(cfg);
2024-05-24 14:29:42 +02:00
std::array<uint8_t, 4096> pduBuf{};
2022-09-14 19:29:43 +02:00
size_t serLen = 0;
store_address_t storeId;
PduConfig conf;
2024-05-24 14:29:42 +02:00
auto destHandler = DestHandler(senderMock, 4096, dp, fp);
2022-09-14 19:29:43 +02:00
2023-07-19 13:44:52 +02:00
auto metadataPreparation = [&](Fss cfdpFileSize, ChecksumType checksumType) {
2024-05-24 14:29:42 +02:00
const std::string srcNameString = "hello.txt";
const std::string destNameString = "hello-cpy.txt";
2022-09-14 19:29:43 +02:00
StringLv srcName(srcNameString);
StringLv destName(destNameString);
2023-07-27 14:55:46 +02:00
MetadataGenericInfo info(false, checksumType, std::move(cfdpFileSize));
2024-05-24 14:29:42 +02:00
const TransactionSeqNum seqNum(UnsignedByteField<uint16_t>(1));
2022-09-14 19:29:43 +02:00
conf.sourceId = remoteId;
conf.destId = localId;
2022-09-15 18:44:00 +02:00
conf.mode = TransmissionMode::UNACKNOWLEDGED;
2022-09-14 19:29:43 +02:00
conf.seqNum = seqNum;
2024-05-24 14:29:42 +02:00
const MetadataPduCreator metadataCreator(conf, info, srcName, destName, nullptr, 0);
REQUIRE(metadataCreator.serialize(pduBuf.data(), serLen, metadataCreator.getSerializedSize()) ==
OK);
return OwnedPduPacket(metadataCreator.getPduType(), metadataCreator.getDirectiveCode(),
pduBuf.data(), serLen);
2022-09-14 19:29:43 +02:00
};
auto metadataCheck = [&](const cfdp::DestHandler::FsmResult& res, const char* sourceName,
const char* destName, size_t fileLen) {
REQUIRE(res.result == OK);
REQUIRE(res.errors == 0);
REQUIRE(userMock.metadataRecvd.size() == 1);
auto& idMetadataPair = userMock.metadataRecvd.back();
REQUIRE(idMetadataPair.first == destHandler.getTransactionId());
REQUIRE(idMetadataPair.second.sourceId.getValue() == 3);
2023-07-27 14:55:46 +02:00
REQUIRE(idMetadataPair.second.fileSize.getSize(nullptr) == fileLen);
2022-09-14 19:29:43 +02:00
REQUIRE(strcmp(idMetadataPair.second.destFileName, destName) == 0);
REQUIRE(strcmp(idMetadataPair.second.sourceFileName, sourceName) == 0);
userMock.metadataRecvd.pop();
REQUIRE(fsMock.fileMap.find(destName) != fsMock.fileMap.end());
REQUIRE(res.result == OK);
2023-07-14 15:56:38 +02:00
REQUIRE(res.state == CfdpState::BUSY_CLASS_1_NACKED);
2022-09-14 19:29:43 +02:00
REQUIRE(res.step == DestHandler::TransactionStep::RECEIVING_FILE_DATA_PDUS);
};
2023-07-19 13:44:52 +02:00
auto eofPreparation = [&](Fss cfdpFileSize, uint32_t crc) {
2022-09-14 19:29:43 +02:00
EofInfo eofInfo(cfdp::ConditionCode::NO_ERROR, crc, std::move(cfdpFileSize));
EofPduCreator eofCreator(conf, eofInfo);
2024-05-24 14:29:42 +02:00
REQUIRE(eofCreator.serialize(pduBuf.data(), serLen, eofCreator.getSerializedSize()) == OK);
OwnedPduPacket packet(eofCreator.getPduType(), eofCreator.getDirectiveCode(), pduBuf.data(),
serLen);
return packet;
2022-09-14 19:29:43 +02:00
};
auto eofCheck = [&](const cfdp::DestHandler::FsmResult& res, const TransactionId& id) {
REQUIRE(res.result == OK);
2023-07-14 15:56:38 +02:00
REQUIRE(res.state == CfdpState::IDLE);
2022-09-14 19:29:43 +02:00
REQUIRE(res.errors == 0);
REQUIRE(res.step == DestHandler::TransactionStep::IDLE);
2023-08-14 13:40:25 +02:00
REQUIRE(userMock.eofRecvdRecvd.size() == 1);
auto& eofId = userMock.eofRecvdRecvd.back();
2022-09-14 19:29:43 +02:00
CHECK(eofId == id);
REQUIRE(userMock.finishedRecvd.size() == 1);
auto& idParamPair = userMock.finishedRecvd.back();
CHECK(idParamPair.first == id);
CHECK(idParamPair.second.condCode == ConditionCode::NO_ERROR);
};
auto fileDataPduCheck = [&](const cfdp::DestHandler::FsmResult& res,
const std::vector<store_address_t>& idsToCheck) {
REQUIRE(res.result == OK);
2023-07-14 15:56:38 +02:00
REQUIRE(res.state == CfdpState::BUSY_CLASS_1_NACKED);
2022-09-14 19:29:43 +02:00
REQUIRE(res.step == DestHandler::TransactionStep::RECEIVING_FILE_DATA_PDUS);
};
SECTION("State") {
2023-07-14 15:56:38 +02:00
CHECK(destHandler.getCfdpState() == CfdpState::IDLE);
2022-09-14 19:29:43 +02:00
CHECK(destHandler.getTransactionStep() == DestHandler::TransactionStep::IDLE);
}
SECTION("Idle State Machine Iteration") {
2024-05-24 14:29:42 +02:00
auto [packetsSent, result, step, state, errors, errorCodes] =
destHandler.stateMachineNoPacket();
CHECK(result == OK);
CHECK(errors == 0);
2023-07-14 15:56:38 +02:00
CHECK(destHandler.getCfdpState() == CfdpState::IDLE);
2022-09-14 19:29:43 +02:00
CHECK(destHandler.getTransactionStep() == DestHandler::TransactionStep::IDLE);
}
SECTION("Empty File Transfer") {
2024-05-24 14:29:42 +02:00
const DestHandler::FsmResult& res = destHandler.stateMachineNoPacket();
2022-09-14 19:29:43 +02:00
CHECK(res.result == OK);
2023-07-19 13:44:52 +02:00
Fss cfdpFileSize(0);
2024-05-24 14:29:42 +02:00
auto packet = metadataPreparation(cfdpFileSize, ChecksumType::NULL_CHECKSUM);
destHandler.stateMachine(packet);
2022-09-14 19:29:43 +02:00
metadataCheck(res, "hello.txt", "hello-cpy.txt", 0);
2024-05-24 14:29:42 +02:00
destHandler.stateMachine(packet);
// REQUIRE(res.callStatus == CallStatus::CALL_AFTER_DELAY);
2022-09-14 19:29:43 +02:00
auto transactionId = destHandler.getTransactionId();
2024-05-24 14:29:42 +02:00
auto ownedEofPacket = eofPreparation(cfdpFileSize, 0);
2022-09-14 19:29:43 +02:00
// After EOF, operation is done because no closure was requested
2024-05-24 14:29:42 +02:00
destHandler.stateMachine(ownedEofPacket);
2022-09-14 19:29:43 +02:00
eofCheck(res, transactionId);
}
SECTION("Small File Transfer") {
2024-05-24 14:29:42 +02:00
const DestHandler::FsmResult& res = destHandler.stateMachineNoPacket();
2022-09-14 19:29:43 +02:00
CHECK(res.result == OK);
std::string fileData = "hello test data";
etl::crc32 crcCalc;
crcCalc.add(fileData.begin(), fileData.end());
uint32_t crc32 = crcCalc.value();
2023-07-19 13:44:52 +02:00
Fss cfdpFileSize(fileData.size());
2024-05-24 14:29:42 +02:00
auto metadataPacket = metadataPreparation(cfdpFileSize, ChecksumType::CRC_32);
destHandler.stateMachine(metadataPacket);
2022-09-14 19:29:43 +02:00
metadataCheck(res, "hello.txt", "hello-cpy.txt", fileData.size());
2024-05-24 14:29:42 +02:00
destHandler.stateMachineNoPacket();
2022-09-14 19:29:43 +02:00
auto transactionId = destHandler.getTransactionId();
2023-07-19 13:44:52 +02:00
Fss offset(0);
2022-09-14 19:29:43 +02:00
FileDataInfo fdPduInfo(offset, reinterpret_cast<const uint8_t*>(fileData.data()),
fileData.size());
FileDataCreator fdPduCreator(conf, fdPduInfo);
2024-05-24 14:29:42 +02:00
REQUIRE(fdPduCreator.serialize(pduBuf.data(), serLen, fdPduCreator.getSerializedSize()) == OK);
OwnedPduPacket fdPdu(fdPduCreator.getPduType(), std::nullopt, pduBuf.data(), serLen);
destHandler.stateMachine(fdPdu);
2022-09-14 19:29:43 +02:00
fileDataPduCheck(res, {storeId});
2024-05-24 14:29:42 +02:00
auto eofPacket = eofPreparation(cfdpFileSize, crc32);
2022-09-14 19:29:43 +02:00
// After EOF, operation is done because no closure was requested
2024-05-24 14:29:42 +02:00
destHandler.stateMachine(eofPacket);
2022-09-14 19:29:43 +02:00
eofCheck(res, transactionId);
}
SECTION("Segmented File Transfer") {
2024-05-24 14:29:42 +02:00
const DestHandler::FsmResult& res = destHandler.stateMachineNoPacket();
2022-09-14 19:29:43 +02:00
CHECK(res.result == OK);
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distU8(0, 255);
std::array<uint8_t, 1024> largerFileData{};
for (auto& val : largerFileData) {
val = distU8(rng);
}
etl::crc32 crcCalc;
crcCalc.add(largerFileData.begin(), largerFileData.end());
uint32_t crc32 = crcCalc.value();
2023-07-19 13:44:52 +02:00
Fss cfdpFileSize(largerFileData.size());
2024-05-24 14:29:42 +02:00
auto metaPacket = metadataPreparation(cfdpFileSize, ChecksumType::CRC_32);
destHandler.stateMachine(metaPacket);
2022-09-14 19:29:43 +02:00
metadataCheck(res, "hello.txt", "hello-cpy.txt", largerFileData.size());
2024-05-24 14:29:42 +02:00
destHandler.stateMachineNoPacket();
2022-09-14 19:29:43 +02:00
auto transactionId = destHandler.getTransactionId();
std::vector<store_address_t> idsToCheck;
{
2023-07-19 13:44:52 +02:00
Fss offset(0);
2024-05-24 14:29:42 +02:00
FileDataInfo fdPduInfo(offset, (largerFileData.data()), largerFileData.size() / 2);
2022-09-14 19:29:43 +02:00
FileDataCreator fdPduCreator(conf, fdPduInfo);
2024-05-24 14:29:42 +02:00
REQUIRE(fdPduCreator.serialize(pduBuf.data(), serLen, fdPduCreator.getSerializedSize()) ==
OK);
2022-09-14 19:29:43 +02:00
idsToCheck.push_back(storeId);
2024-05-24 14:29:42 +02:00
OwnedPduPacket fdPdu(fdPduCreator.getPduType(), std::nullopt, pduBuf.data(), serLen);
destHandler.stateMachine(fdPdu);
2022-09-14 19:29:43 +02:00
}
{
2023-07-19 13:44:52 +02:00
Fss offset(512);
2024-05-24 14:29:42 +02:00
FileDataInfo fdPduInfo(offset, largerFileData.data() + 512, largerFileData.size() / 2);
2022-09-14 19:29:43 +02:00
FileDataCreator fdPduCreator(conf, fdPduInfo);
2024-05-24 14:29:42 +02:00
REQUIRE(fdPduCreator.serialize(pduBuf.data(), serLen, fdPduCreator.getSerializedSize()) ==
OK);
2022-09-14 19:29:43 +02:00
idsToCheck.push_back(storeId);
2024-05-24 14:29:42 +02:00
OwnedPduPacket fdPdu(fdPduCreator.getPduType(), std::nullopt, pduBuf.data(), serLen);
destHandler.stateMachine(fdPdu);
2022-09-14 19:29:43 +02:00
}
2024-05-24 14:29:42 +02:00
destHandler.stateMachineNoPacket();
2022-09-14 19:29:43 +02:00
fileDataPduCheck(res, idsToCheck);
2024-05-24 14:29:42 +02:00
auto eofPacket = eofPreparation(cfdpFileSize, crc32);
2022-09-14 19:29:43 +02:00
// After EOF, operation is done because no closure was requested
2024-05-24 14:29:42 +02:00
destHandler.stateMachine(eofPacket);
2022-09-14 19:29:43 +02:00
eofCheck(res, transactionId);
}
}