fsfw/tests/src/fsfw_tests/unit/cfdp/testAckPdu.cpp
Robin Mueller 5907f8ee9d
Added CFDP packet stack
This PR adds the packet stack for the CCSDS File Delivery Protocol.
It also refactors the existing TMTC infastructure to allow sending
of CFDP packets to the CCSDS handlers.

This includes the whole PDU (Protocol Data Unit) stack:

- File Data PDUs

and all file directive PDUs

- ACK PDU
- NAK PDU
- Metadata PDU
- Finished PDU
- Prompt PDU
- Keep Alive PDU
- EOF PDU

The PR includes a full set of unittests for the packet stack
with a coverage of 90+ %.

The refactoring of the existing TMTC infastructure includes non-ideal
solutions like diamond inheritance.
Avoiding this solution would require refactoring the packet stack.
This would be a good idea anyway because the existing stack is tightly
coupled to the FSFW, making reuse more difficult if only the stack is
planned to be used without the store functionalities etc.

The PDU implementation provided here is only weakly coupled to the FSFW,
only using components like returnvalues or the Serialization modules.
There are dedicated serializers and deserializers, which also helps in
creating small focused modules which are easy to test.

Some of the modules here were provied by Matthias Tompert.
2021-12-03 15:37:49 +01:00

102 lines
4.8 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "fsfw/cfdp/pdu/AckPduDeserializer.h"
#include "fsfw/cfdp/pdu/AckPduSerializer.h"
#include "fsfw/globalfunctions/arrayprinter.h"
#include <array>
TEST_CASE( "ACK PDU" , "[AckPdu]") {
using namespace cfdp;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
std::array<uint8_t, 256> buf = {};
uint8_t* bufptr = buf.data();
size_t maxsz = buf.size();
size_t sz = 0;
auto seqNum = TransactionSeqNum(WidthInBytes::TWO_BYTES, 15);
auto sourceId = EntityId(WidthInBytes::TWO_BYTES, 1);
auto destId = EntityId(WidthInBytes::TWO_BYTES, 2);
auto pduConf = PduConfig(TransmissionModes::ACKNOWLEDGED, seqNum, sourceId, destId);
AckInfo ackInfo(FileDirectives::EOF_DIRECTIVE, ConditionCode::NO_ERROR, AckTransactionStatus::ACTIVE);
auto ackSerializer = AckPduSerializer(ackInfo, pduConf);
result = ackSerializer.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
SECTION("Serialize") {
REQUIRE(buf.data()[sz - 3] == cfdp::FileDirectives::ACK);
REQUIRE((buf.data()[sz - 2] >> 4) == FileDirectives::EOF_DIRECTIVE);
REQUIRE((buf.data()[sz - 2] & 0x0f) == 0);
REQUIRE(buf.data()[sz - 1] == AckTransactionStatus::ACTIVE);
ackInfo.setAckedDirective(FileDirectives::FINISH);
ackInfo.setAckedConditionCode(ConditionCode::FILESTORE_REJECTION);
ackInfo.setTransactionStatus(AckTransactionStatus::TERMINATED);
auto ackSerializer2 = AckPduSerializer(ackInfo, pduConf);
bufptr = buf.data();
sz = 0;
result = ackSerializer2.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
REQUIRE(buf.data()[sz - 3] == cfdp::FileDirectives::ACK);
REQUIRE((buf.data()[sz - 2] >> 4) == FileDirectives::FINISH);
REQUIRE((buf.data()[sz - 2] & 0x0f) == 0b0001);
REQUIRE((buf.data()[sz - 1] >> 4) == ConditionCode::FILESTORE_REJECTION);
REQUIRE((buf.data()[sz - 1] & 0b11) == AckTransactionStatus::TERMINATED);
bufptr = buf.data();
sz = 0;
ackInfo.setAckedDirective(FileDirectives::KEEP_ALIVE);
auto ackSerializer3 = AckPduSerializer(ackInfo, pduConf);
result = ackSerializer3.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
// Invalid file directive
REQUIRE(result != HasReturnvaluesIF::RETURN_OK);
ackInfo.setAckedDirective(FileDirectives::FINISH);
// buffer too small
result = ackSerializer.serialize(&bufptr, &sz, 8, SerializeIF::Endianness::NETWORK);
REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT);
}
SECTION("Deserialize") {
AckInfo ackInfo;
auto reader = AckPduDeserializer(buf.data(), sz, ackInfo);
result = reader.parseData();
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
REQUIRE(ackInfo.getAckedDirective() == FileDirectives::EOF_DIRECTIVE);
REQUIRE(ackInfo.getAckedConditionCode() == ConditionCode::NO_ERROR);
REQUIRE(ackInfo.getDirectiveSubtypeCode() == 0);
REQUIRE(ackInfo.getTransactionStatus() == AckTransactionStatus::ACTIVE);
AckInfo newInfo = AckInfo(FileDirectives::FINISH,
ConditionCode::FILESTORE_REJECTION, AckTransactionStatus::TERMINATED);
auto ackSerializer2 = AckPduSerializer(newInfo, pduConf);
bufptr = buf.data();
sz = 0;
result = ackSerializer2.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
auto reader2 = AckPduDeserializer(buf.data(), sz, ackInfo);
result = reader2.parseData();
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
REQUIRE(ackInfo.getAckedDirective() == FileDirectives::FINISH);
REQUIRE(ackInfo.getAckedConditionCode() == ConditionCode::FILESTORE_REJECTION);
REQUIRE(ackInfo.getDirectiveSubtypeCode() == 0b0001);
REQUIRE(ackInfo.getTransactionStatus() == AckTransactionStatus::TERMINATED);
uint8_t prevVal = buf[sz - 2];
buf[sz - 2] = FileDirectives::INVALID_DIRECTIVE << 4;
result = reader2.parseData();
REQUIRE(result == cfdp::INVALID_ACK_DIRECTIVE_FIELDS);
buf[sz - 2] = FileDirectives::FINISH << 4 | 0b1111;
result = reader2.parseData();
REQUIRE(result == cfdp::INVALID_ACK_DIRECTIVE_FIELDS);
buf[sz - 2] = prevVal;
buf[sz - 3] = cfdp::FileDirectives::INVALID_DIRECTIVE;
result = reader2.parseData();
REQUIRE(result == cfdp::INVALID_DIRECTIVE_FIELDS);
buf[sz - 3] = cfdp::FileDirectives::ACK;
auto maxSizeTooSmall = AckPduDeserializer(buf.data(), sz - 2, ackInfo);
result = maxSizeTooSmall.parseData();
REQUIRE(result == SerializeIF::STREAM_TOO_SHORT);
}
}