fsfw/unittests/cfdp/handler/testDistributor.cpp

99 lines
4.3 KiB
C++
Raw Normal View History

2022-08-08 12:34:54 +02:00
#include <catch2/catch_test_macros.hpp>
2022-08-08 17:53:42 +02:00
#include "fsfw/cfdp/CfdpDistributor.h"
#include "fsfw/cfdp/pdu/MetadataPduCreator.h"
2022-08-10 09:39:57 +02:00
#include "fsfw/cfdp/tlv/StringLv.h"
2022-08-08 17:53:42 +02:00
#include "fsfw/storagemanager/LocalPool.h"
2022-08-09 13:04:23 +02:00
#include "fsfw/tcdistribution/definitions.h"
2022-08-08 17:53:42 +02:00
#include "mocks/AcceptsTcMock.h"
#include "mocks/MessageQueueMock.h"
2022-08-09 12:50:50 +02:00
#include "mocks/StorageManagerMock.h"
2022-08-08 17:53:42 +02:00
2022-08-08 12:34:54 +02:00
TEST_CASE("CFDP Distributor", "[cfdp][distributor]") {
2022-08-08 17:53:42 +02:00
LocalPool::LocalPoolConfig cfg = {{5, 32}, {2, 64}};
2022-08-09 12:50:50 +02:00
StorageManagerMock pool(objects::NO_OBJECT, cfg);
2022-08-08 17:53:42 +02:00
auto queue = MessageQueueMock(1);
CfdpDistribCfg distribCfg(1, pool, &queue);
auto distributor = CfdpDistributor(distribCfg);
2022-08-08 18:29:32 +02:00
auto obswEntityId = cfdp::EntityId(UnsignedByteField<uint16_t>(2));
auto groundEntityId = cfdp::EntityId(UnsignedByteField<uint16_t>(1));
2022-08-09 12:13:10 +02:00
MessageQueueId_t receiverQueueId = 3;
auto tcAcceptor = AcceptsTcMock("CFDP Receiver", 0, receiverQueueId);
2022-08-09 13:04:23 +02:00
// Set up Metadata PDU for generate test data.
2022-08-08 18:29:32 +02:00
cfdp::FileSize fileSize(12);
const cfdp::EntityId& sourceId(groundEntityId);
const cfdp::EntityId& destId(obswEntityId);
cfdp::TransactionSeqNum seqNum(UnsignedByteField<uint16_t>(12));
2022-09-15 18:41:15 +02:00
auto pduConf = PduConfig(sourceId, destId, cfdp::TransmissionMode::UNACKNOWLEDGED, seqNum);
2022-08-08 18:29:32 +02:00
std::string sourceFileString = "hello.txt";
2022-08-10 09:39:57 +02:00
cfdp::StringLv sourceFileName(sourceFileString);
2022-08-08 18:29:32 +02:00
std::string destFileString = "hello2.txt";
2022-08-10 09:39:57 +02:00
cfdp::StringLv destFileName(destFileString);
2022-09-15 18:41:15 +02:00
MetadataInfo metadataInfo(false, cfdp::ChecksumType::CRC_32, fileSize, sourceFileName,
2022-08-08 18:29:32 +02:00
destFileName);
MetadataPduCreator creator(pduConf, metadataInfo);
uint8_t* dataPtr = nullptr;
2022-08-08 17:53:42 +02:00
SECTION("State") {
2022-08-22 16:35:53 +02:00
CHECK(distributor.initialize() == returnvalue::OK);
2022-08-08 17:53:42 +02:00
CHECK(std::strcmp(distributor.getName(), "CFDP Distributor") == 0);
CHECK(distributor.getIdentifier() == 0);
CHECK(distributor.getRequestQueue() == queue.getId());
}
2022-08-08 12:34:54 +02:00
SECTION("Packet Forwarding") {
2022-08-22 16:35:53 +02:00
CHECK(distributor.initialize() == returnvalue::OK);
CHECK(distributor.registerTcDestination(obswEntityId, tcAcceptor) == returnvalue::OK);
2022-08-08 18:29:32 +02:00
size_t serLen = 0;
store_address_t storeId;
2022-08-09 13:04:23 +02:00
CHECK(pool.LocalPool::getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-08 18:29:32 +02:00
REQUIRE(creator.SerializeIF::serializeBe(dataPtr, serLen, creator.getSerializedSize()) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-08 18:29:32 +02:00
TmTcMessage msg(storeId);
queue.addReceivedMessage(msg);
2022-08-22 16:35:53 +02:00
CHECK(distributor.performOperation(0) == returnvalue::OK);
2022-08-09 12:13:10 +02:00
CHECK(queue.wasMessageSent());
CHECK(queue.numberOfSentMessages() == 1);
// The packet is forwarded, with no need to delete the data
CHECK(pool.hasDataAtId(storeId));
TmTcMessage sentMsg;
2022-08-22 16:35:53 +02:00
CHECK(queue.getNextSentMessage(receiverQueueId, sentMsg) == returnvalue::OK);
2022-08-09 12:13:10 +02:00
CHECK(sentMsg.getStorageId() == storeId);
2022-08-08 17:53:42 +02:00
}
2022-08-09 13:04:23 +02:00
SECTION("No Destination found") {
2022-08-22 16:35:53 +02:00
CHECK(distributor.initialize() == returnvalue::OK);
2022-08-09 13:04:23 +02:00
size_t serLen = 0;
store_address_t storeId;
CHECK(pool.LocalPool::getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-09 13:04:23 +02:00
REQUIRE(creator.SerializeIF::serializeBe(dataPtr, serLen, creator.getSerializedSize()) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-09 13:04:23 +02:00
TmTcMessage msg(storeId);
queue.addReceivedMessage(msg);
CHECK(distributor.performOperation(0) == tmtcdistrib::NO_DESTINATION_FOUND);
}
SECTION("Getting data fails") {
pool.nextModifyDataCallFails.first = true;
pool.nextModifyDataCallFails.second = StorageManagerIF::DATA_DOES_NOT_EXIST;
size_t serLen = 0;
store_address_t storeId;
2022-08-22 16:35:53 +02:00
CHECK(distributor.registerTcDestination(obswEntityId, tcAcceptor) == returnvalue::OK);
2022-08-09 13:04:23 +02:00
CHECK(pool.LocalPool::getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-09 13:04:23 +02:00
REQUIRE(creator.SerializeIF::serializeBe(dataPtr, serLen, creator.getSerializedSize()) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-09 13:04:23 +02:00
TmTcMessage msg(storeId);
queue.addReceivedMessage(msg);
CHECK(distributor.performOperation(0) == StorageManagerIF::DATA_DOES_NOT_EXIST);
}
SECTION("Duplicate registration") {
2022-08-22 16:35:53 +02:00
CHECK(distributor.initialize() == returnvalue::OK);
CHECK(distributor.registerTcDestination(obswEntityId, tcAcceptor) == returnvalue::OK);
CHECK(distributor.registerTcDestination(obswEntityId, tcAcceptor) == returnvalue::FAILED);
2022-08-09 13:04:23 +02:00
}
2022-08-08 12:34:54 +02:00
}