2022-09-06 14:33:01 +02:00
|
|
|
#include <etl/crc32.h>
|
|
|
|
|
2022-08-08 12:41:05 +02:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2022-09-06 16:14:39 +02:00
|
|
|
#include <random>
|
2022-09-06 16:33:38 +02:00
|
|
|
#include <utility>
|
2022-08-08 12:41:05 +02:00
|
|
|
|
2022-08-09 15:46:42 +02:00
|
|
|
#include "fsfw/cfdp.h"
|
2022-09-06 11:56:05 +02:00
|
|
|
#include "fsfw/cfdp/pdu/EofPduCreator.h"
|
2022-09-06 15:45:51 +02:00
|
|
|
#include "fsfw/cfdp/pdu/FileDataCreator.h"
|
2022-09-06 13:16:00 +02:00
|
|
|
#include "fsfw/cfdp/pdu/MetadataPduCreator.h"
|
2022-09-05 11:19:23 +02:00
|
|
|
#include "mocks/AcceptsTmMock.h"
|
2022-09-05 11:37:41 +02:00
|
|
|
#include "mocks/EventReportingProxyMock.h"
|
2022-08-17 17:09:39 +02:00
|
|
|
#include "mocks/FilesystemMock.h"
|
2022-09-05 11:19:23 +02:00
|
|
|
#include "mocks/MessageQueueMock.h"
|
2022-09-05 11:37:41 +02:00
|
|
|
#include "mocks/StorageManagerMock.h"
|
2022-08-09 19:00:47 +02:00
|
|
|
#include "mocks/cfdp/FaultHandlerMock.h"
|
2022-08-17 17:09:39 +02:00
|
|
|
#include "mocks/cfdp/RemoteConfigTableMock.h"
|
2022-08-10 11:09:07 +02:00
|
|
|
#include "mocks/cfdp/UserMock.h"
|
2022-08-10 15:03:53 +02:00
|
|
|
|
2022-08-09 15:46:42 +02:00
|
|
|
TEST_CASE("CFDP Dest Handler", "[cfdp]") {
|
|
|
|
using namespace cfdp;
|
2022-09-05 11:37:41 +02:00
|
|
|
using namespace returnvalue;
|
2022-09-05 11:19:23 +02:00
|
|
|
MessageQueueId_t destQueueId = 2;
|
|
|
|
AcceptsTmMock tmReceiver(destQueueId);
|
|
|
|
MessageQueueMock mqMock(destQueueId);
|
2022-08-09 16:36:35 +02:00
|
|
|
EntityId localId = EntityId(UnsignedByteField<uint16_t>(2));
|
2022-09-05 14:20:01 +02:00
|
|
|
EntityId remoteId = EntityId(UnsignedByteField<uint16_t>(3));
|
2022-09-06 13:16:00 +02:00
|
|
|
FaultHandlerMock fhMock;
|
|
|
|
LocalEntityCfg localEntityCfg(localId, IndicationCfg(), fhMock);
|
|
|
|
FilesystemMock fsMock;
|
|
|
|
UserMock userMock(fsMock);
|
|
|
|
RemoteConfigTableMock remoteCfgTableMock;
|
2022-09-05 11:19:23 +02:00
|
|
|
PacketInfoList<64> packetInfoList;
|
|
|
|
LostSegmentsList<128> lostSegmentsList;
|
|
|
|
DestHandlerParams dp(localEntityCfg, userMock, remoteCfgTableMock, packetInfoList,
|
|
|
|
lostSegmentsList);
|
2022-09-05 11:37:41 +02:00
|
|
|
EventReportingProxyMock eventReporterMock;
|
|
|
|
LocalPool::LocalPoolConfig storeCfg = {{10, 32}, {10, 64}, {10, 128}, {10, 1024}};
|
|
|
|
StorageManagerMock tcStore(2, storeCfg);
|
|
|
|
StorageManagerMock tmStore(3, storeCfg);
|
2022-09-07 18:20:38 +02:00
|
|
|
FsfwParams fp(tmReceiver, &mqMock, &eventReporterMock);
|
2022-09-05 17:20:29 +02:00
|
|
|
RemoteEntityCfg cfg;
|
|
|
|
cfg.remoteId = remoteId;
|
|
|
|
remoteCfgTableMock.addRemoteConfig(cfg);
|
2022-09-05 11:37:41 +02:00
|
|
|
fp.tcStore = &tcStore;
|
|
|
|
fp.tmStore = &tmStore;
|
2022-09-06 14:33:01 +02:00
|
|
|
uint8_t* buf = nullptr;
|
|
|
|
size_t serLen = 0;
|
2022-09-06 15:45:51 +02:00
|
|
|
store_address_t storeId;
|
|
|
|
PduConfig conf;
|
2022-09-05 11:37:41 +02:00
|
|
|
auto destHandler = DestHandler(dp, fp);
|
|
|
|
CHECK(destHandler.initialize() == OK);
|
2022-08-09 15:46:42 +02:00
|
|
|
|
2022-09-06 15:45:51 +02:00
|
|
|
auto metadataPreparation = [&](FileSize cfdpFileSize, ChecksumTypes checksumType) {
|
2022-09-05 14:20:01 +02:00
|
|
|
std::string srcNameString = "hello.txt";
|
|
|
|
std::string destNameString = "hello-cpy.txt";
|
|
|
|
StringLv srcName(srcNameString);
|
|
|
|
StringLv destName(destNameString);
|
2022-09-06 15:45:51 +02:00
|
|
|
MetadataInfo info(false, checksumType, cfdpFileSize, srcName, destName);
|
2022-09-06 13:45:43 +02:00
|
|
|
TransactionSeqNum seqNum(UnsignedByteField<uint16_t>(1));
|
2022-09-06 14:33:01 +02:00
|
|
|
conf.sourceId = remoteId;
|
|
|
|
conf.destId = localId;
|
|
|
|
conf.mode = TransmissionModes::UNACKNOWLEDGED;
|
|
|
|
conf.seqNum = seqNum;
|
2022-09-06 11:56:05 +02:00
|
|
|
MetadataPduCreator metadataCreator(conf, info);
|
2022-09-06 14:33:01 +02:00
|
|
|
REQUIRE(tcStore.getFreeElement(&storeId, metadataCreator.getSerializedSize(), &buf) == OK);
|
|
|
|
REQUIRE(metadataCreator.serialize(buf, serLen, metadataCreator.getSerializedSize()) == OK);
|
2022-09-06 15:45:51 +02:00
|
|
|
PacketInfo packetInfo(metadataCreator.getPduType(), storeId,
|
|
|
|
metadataCreator.getDirectiveCode());
|
2022-09-05 14:20:01 +02:00
|
|
|
packetInfoList.push_back(packetInfo);
|
2022-09-06 14:33:01 +02:00
|
|
|
};
|
|
|
|
|
2022-09-06 15:45:51 +02:00
|
|
|
auto metadataCheck = [&](const cfdp::DestHandler::FsmResult& res, const char* sourceName,
|
|
|
|
const char* destName, size_t fileLen) {
|
2022-09-06 11:56:05 +02:00
|
|
|
REQUIRE(res.result == OK);
|
|
|
|
REQUIRE(res.callStatus == CallStatus::CALL_AGAIN);
|
2022-09-06 16:14:39 +02:00
|
|
|
REQUIRE(res.errors == 0);
|
2022-09-06 11:56:05 +02:00
|
|
|
// Assert that the packet was deleted after handling
|
|
|
|
REQUIRE(not tcStore.hasDataAtId(storeId));
|
2022-09-06 13:45:43 +02:00
|
|
|
REQUIRE(packetInfoList.empty());
|
2022-09-06 13:16:00 +02:00
|
|
|
REQUIRE(userMock.metadataRecvd.size() == 1);
|
2022-09-06 14:06:22 +02:00
|
|
|
auto& idMetadataPair = userMock.metadataRecvd.back();
|
|
|
|
REQUIRE(idMetadataPair.first == destHandler.getTransactionId());
|
|
|
|
REQUIRE(idMetadataPair.second.sourceId.getValue() == 3);
|
2022-09-06 15:45:51 +02:00
|
|
|
REQUIRE(idMetadataPair.second.fileSize == fileLen);
|
2022-09-06 14:33:01 +02:00
|
|
|
REQUIRE(strcmp(idMetadataPair.second.destFileName, destName) == 0);
|
|
|
|
REQUIRE(strcmp(idMetadataPair.second.sourceFileName, sourceName) == 0);
|
2022-09-06 13:45:43 +02:00
|
|
|
userMock.metadataRecvd.pop();
|
2022-09-06 14:33:01 +02:00
|
|
|
REQUIRE(fsMock.fileMap.find(destName) != fsMock.fileMap.end());
|
2022-09-06 11:56:05 +02:00
|
|
|
REQUIRE(res.result == OK);
|
|
|
|
REQUIRE(res.state == CfdpStates::BUSY_CLASS_1_NACKED);
|
|
|
|
REQUIRE(res.step == DestHandler::TransactionStep::RECEIVING_FILE_DATA_PDUS);
|
2022-09-06 14:33:01 +02:00
|
|
|
};
|
|
|
|
|
2022-09-06 15:45:51 +02:00
|
|
|
auto eofPreparation = [&](FileSize cfdpFileSize, uint32_t crc) {
|
|
|
|
EofInfo eofInfo(cfdp::ConditionCode::NO_ERROR, crc, std::move(cfdpFileSize));
|
|
|
|
EofPduCreator eofCreator(conf, eofInfo);
|
|
|
|
REQUIRE(tcStore.getFreeElement(&storeId, eofCreator.getSerializedSize(), &buf) == OK);
|
|
|
|
REQUIRE(eofCreator.serialize(buf, serLen, eofCreator.getSerializedSize()) == OK);
|
|
|
|
PacketInfo packetInfo(eofCreator.getPduType(), storeId, eofCreator.getDirectiveCode());
|
|
|
|
packetInfoList.push_back(packetInfo);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto eofCheck = [&](const cfdp::DestHandler::FsmResult& res, const TransactionId& id) {
|
|
|
|
REQUIRE(res.result == OK);
|
|
|
|
REQUIRE(res.state == CfdpStates::IDLE);
|
2022-09-06 16:14:39 +02:00
|
|
|
REQUIRE(res.errors == 0);
|
2022-09-06 15:45:51 +02:00
|
|
|
REQUIRE(res.step == DestHandler::TransactionStep::IDLE);
|
|
|
|
// Assert that the packet was deleted after handling
|
|
|
|
REQUIRE(not tcStore.hasDataAtId(storeId));
|
|
|
|
REQUIRE(packetInfoList.empty());
|
|
|
|
REQUIRE(userMock.eofsRevd.size() == 1);
|
|
|
|
auto& eofId = userMock.eofsRevd.back();
|
|
|
|
CHECK(eofId == id);
|
|
|
|
REQUIRE(userMock.finishedRecvd.size() == 1);
|
|
|
|
auto& idParamPair = userMock.finishedRecvd.back();
|
|
|
|
CHECK(idParamPair.first == id);
|
|
|
|
CHECK(idParamPair.second.condCode == ConditionCode::NO_ERROR);
|
|
|
|
};
|
|
|
|
|
2022-09-06 16:33:38 +02:00
|
|
|
auto fileDataPduCheck = [&](const cfdp::DestHandler::FsmResult& res,
|
|
|
|
const std::vector<store_address_t>& idsToCheck) {
|
2022-09-06 16:14:39 +02:00
|
|
|
REQUIRE(res.result == OK);
|
|
|
|
REQUIRE(res.state == CfdpStates::BUSY_CLASS_1_NACKED);
|
|
|
|
REQUIRE(res.step == DestHandler::TransactionStep::RECEIVING_FILE_DATA_PDUS);
|
2022-09-06 16:33:38 +02:00
|
|
|
for (const auto id : idsToCheck) {
|
|
|
|
REQUIRE(not tcStore.hasDataAtId(id));
|
|
|
|
}
|
2022-09-06 16:14:39 +02:00
|
|
|
REQUIRE(packetInfoList.empty());
|
|
|
|
};
|
|
|
|
|
2022-09-06 14:33:01 +02:00
|
|
|
SECTION("State") {
|
|
|
|
CHECK(destHandler.getCfdpState() == CfdpStates::IDLE);
|
|
|
|
CHECK(destHandler.getTransactionStep() == DestHandler::TransactionStep::IDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Idle State Machine Iteration") {
|
|
|
|
auto res = destHandler.performStateMachine();
|
|
|
|
CHECK(res.result == OK);
|
|
|
|
CHECK(res.callStatus == CallStatus::CALL_AFTER_DELAY);
|
|
|
|
CHECK(res.errors == 0);
|
|
|
|
CHECK(destHandler.getCfdpState() == CfdpStates::IDLE);
|
|
|
|
CHECK(destHandler.getTransactionStep() == DestHandler::TransactionStep::IDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Empty File Transfer") {
|
|
|
|
const DestHandler::FsmResult& res = destHandler.performStateMachine();
|
|
|
|
CHECK(res.result == OK);
|
|
|
|
FileSize cfdpFileSize(0);
|
2022-09-06 15:45:51 +02:00
|
|
|
metadataPreparation(cfdpFileSize, ChecksumTypes::NULL_CHECKSUM);
|
2022-09-06 14:33:01 +02:00
|
|
|
destHandler.performStateMachine();
|
2022-09-06 15:45:51 +02:00
|
|
|
metadataCheck(res, "hello.txt", "hello-cpy.txt", 0);
|
2022-09-06 14:33:01 +02:00
|
|
|
destHandler.performStateMachine();
|
|
|
|
REQUIRE(res.callStatus == CallStatus::CALL_AFTER_DELAY);
|
2022-09-06 13:45:43 +02:00
|
|
|
auto transactionId = destHandler.getTransactionId();
|
2022-09-06 15:45:51 +02:00
|
|
|
eofPreparation(cfdpFileSize, 0);
|
2022-09-06 13:45:43 +02:00
|
|
|
// After EOF, operation is done because no closure was requested
|
|
|
|
destHandler.performStateMachine();
|
2022-09-06 15:45:51 +02:00
|
|
|
eofCheck(res, transactionId);
|
2022-09-05 11:40:22 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 14:33:01 +02:00
|
|
|
SECTION("Small File Transfer") {
|
|
|
|
const DestHandler::FsmResult& res = destHandler.performStateMachine();
|
|
|
|
CHECK(res.result == OK);
|
|
|
|
std::string fileData = "hello test data";
|
|
|
|
etl::crc32 crcCalc;
|
|
|
|
crcCalc.add(fileData.begin(), fileData.end());
|
|
|
|
uint32_t crc32 = crcCalc.value();
|
2022-09-06 15:45:51 +02:00
|
|
|
FileSize cfdpFileSize(fileData.size());
|
|
|
|
metadataPreparation(cfdpFileSize, ChecksumTypes::CRC_32);
|
2022-09-06 14:33:01 +02:00
|
|
|
destHandler.performStateMachine();
|
2022-09-06 15:45:51 +02:00
|
|
|
metadataCheck(res, "hello.txt", "hello-cpy.txt", fileData.size());
|
2022-09-06 14:33:01 +02:00
|
|
|
destHandler.performStateMachine();
|
|
|
|
REQUIRE(res.callStatus == CallStatus::CALL_AFTER_DELAY);
|
2022-09-06 15:45:51 +02:00
|
|
|
auto transactionId = destHandler.getTransactionId();
|
|
|
|
FileSize offset(0);
|
|
|
|
FileDataInfo fdPduInfo(offset, reinterpret_cast<const uint8_t*>(fileData.data()),
|
|
|
|
fileData.size());
|
|
|
|
FileDataCreator fdPduCreator(conf, fdPduInfo);
|
|
|
|
REQUIRE(tcStore.getFreeElement(&storeId, fdPduCreator.getSerializedSize(), &buf) == OK);
|
|
|
|
REQUIRE(fdPduCreator.serialize(buf, serLen, fdPduCreator.getSerializedSize()) == OK);
|
|
|
|
PacketInfo packetInfo(fdPduCreator.getPduType(), storeId, std::nullopt);
|
|
|
|
packetInfoList.push_back(packetInfo);
|
|
|
|
destHandler.performStateMachine();
|
2022-09-06 16:33:38 +02:00
|
|
|
fileDataPduCheck(res, {storeId});
|
2022-09-06 15:45:51 +02:00
|
|
|
eofPreparation(cfdpFileSize, crc32);
|
|
|
|
// After EOF, operation is done because no closure was requested
|
|
|
|
destHandler.performStateMachine();
|
|
|
|
eofCheck(res, transactionId);
|
2022-09-06 14:33:01 +02:00
|
|
|
}
|
2022-09-05 14:20:01 +02:00
|
|
|
|
2022-09-06 16:14:39 +02:00
|
|
|
SECTION("Segmented File Transfer") {
|
|
|
|
const DestHandler::FsmResult& res = destHandler.performStateMachine();
|
|
|
|
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{};
|
2022-09-06 16:33:38 +02:00
|
|
|
for (auto& val : largerFileData) {
|
2022-09-06 16:14:39 +02:00
|
|
|
val = distU8(rng);
|
|
|
|
}
|
|
|
|
etl::crc32 crcCalc;
|
|
|
|
crcCalc.add(largerFileData.begin(), largerFileData.end());
|
|
|
|
uint32_t crc32 = crcCalc.value();
|
|
|
|
FileSize cfdpFileSize(largerFileData.size());
|
|
|
|
metadataPreparation(cfdpFileSize, ChecksumTypes::CRC_32);
|
|
|
|
destHandler.performStateMachine();
|
|
|
|
metadataCheck(res, "hello.txt", "hello-cpy.txt", largerFileData.size());
|
|
|
|
destHandler.performStateMachine();
|
|
|
|
REQUIRE(res.callStatus == CallStatus::CALL_AFTER_DELAY);
|
|
|
|
auto transactionId = destHandler.getTransactionId();
|
2022-09-06 16:33:38 +02:00
|
|
|
|
|
|
|
std::vector<store_address_t> idsToCheck;
|
|
|
|
{
|
|
|
|
FileSize offset(0);
|
|
|
|
FileDataInfo fdPduInfo(offset, reinterpret_cast<const uint8_t*>(largerFileData.data()),
|
|
|
|
largerFileData.size() / 2);
|
|
|
|
FileDataCreator fdPduCreator(conf, fdPduInfo);
|
|
|
|
REQUIRE(tcStore.getFreeElement(&storeId, fdPduCreator.getSerializedSize(), &buf) == OK);
|
|
|
|
REQUIRE(fdPduCreator.serialize(buf, serLen, fdPduCreator.getSerializedSize()) == OK);
|
|
|
|
PacketInfo packetInfo(fdPduCreator.getPduType(), storeId, std::nullopt);
|
|
|
|
idsToCheck.push_back(storeId);
|
|
|
|
packetInfoList.push_back(packetInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
FileSize offset(512);
|
|
|
|
FileDataInfo fdPduInfo(offset, reinterpret_cast<const uint8_t*>(largerFileData.data() + 512),
|
|
|
|
largerFileData.size() / 2);
|
|
|
|
FileDataCreator fdPduCreator(conf, fdPduInfo);
|
|
|
|
REQUIRE(tcStore.getFreeElement(&storeId, fdPduCreator.getSerializedSize(), &buf) == OK);
|
|
|
|
REQUIRE(fdPduCreator.serialize(buf, serLen, fdPduCreator.getSerializedSize()) == OK);
|
|
|
|
PacketInfo packetInfo(fdPduCreator.getPduType(), storeId, std::nullopt);
|
|
|
|
idsToCheck.push_back(storeId);
|
|
|
|
packetInfoList.push_back(packetInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
destHandler.performStateMachine();
|
|
|
|
fileDataPduCheck(res, idsToCheck);
|
|
|
|
eofPreparation(cfdpFileSize, crc32);
|
|
|
|
// After EOF, operation is done because no closure was requested
|
|
|
|
destHandler.performStateMachine();
|
|
|
|
eofCheck(res, transactionId);
|
2022-09-06 16:14:39 +02:00
|
|
|
}
|
2022-08-09 15:46:42 +02:00
|
|
|
}
|