fsfw/unittests/cfdp/pdu/testAckPdu.cpp

101 lines
4.4 KiB
C++
Raw Permalink Normal View History

2022-02-02 10:29:30 +01:00
#include <array>
#include <catch2/catch_test_macros.hpp>
2022-09-08 12:07:16 +02:00
#include "fsfw/cfdp/pdu/AckPduCreator.h"
#include "fsfw/cfdp/pdu/AckPduReader.h"
#include "fsfw/globalfunctions/arrayprinter.h"
2022-08-08 12:34:54 +02:00
TEST_CASE("ACK PDU", "[cfdp][pdu]") {
2022-02-02 10:29:30 +01:00
using namespace cfdp;
2022-08-03 13:15:49 +02:00
ReturnValue_t result;
2022-02-02 10:29:30 +01:00
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);
2022-09-15 18:41:15 +02:00
auto pduConf = PduConfig(sourceId, destId, TransmissionMode::ACKNOWLEDGED, seqNum);
AckInfo ackInfo(FileDirective::EOF_DIRECTIVE, ConditionCode::NO_ERROR,
2022-02-02 10:29:30 +01:00
AckTransactionStatus::ACTIVE);
2022-09-08 12:07:16 +02:00
auto ackSerializer = AckPduCreator(ackInfo, pduConf);
2022-02-02 10:29:30 +01:00
result = ackSerializer.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
SECTION("Serialize") {
2022-09-15 18:41:15 +02:00
REQUIRE(buf[sz - 3] == cfdp::FileDirective::ACK);
REQUIRE((buf[sz - 2] >> 4) == FileDirective::EOF_DIRECTIVE);
2022-08-03 13:15:49 +02:00
REQUIRE((buf[sz - 2] & 0x0f) == 0);
REQUIRE(buf[sz - 1] == AckTransactionStatus::ACTIVE);
2022-09-15 18:41:15 +02:00
ackInfo.setAckedDirective(FileDirective::FINISH);
2022-02-02 10:29:30 +01:00
ackInfo.setAckedConditionCode(ConditionCode::FILESTORE_REJECTION);
ackInfo.setTransactionStatus(AckTransactionStatus::TERMINATED);
2022-09-08 12:07:16 +02:00
auto ackSerializer2 = AckPduCreator(ackInfo, pduConf);
2022-02-02 10:29:30 +01:00
bufptr = buf.data();
sz = 0;
result = ackSerializer2.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(buf[sz - 3] == cfdp::FileDirective::ACK);
REQUIRE((buf[sz - 2] >> 4) == FileDirective::FINISH);
2022-08-03 13:15:49 +02:00
REQUIRE((buf[sz - 2] & 0x0f) == 0b0001);
REQUIRE((buf[sz - 1] >> 4) == ConditionCode::FILESTORE_REJECTION);
REQUIRE((buf[sz - 1] & 0b11) == AckTransactionStatus::TERMINATED);
2022-02-02 10:29:30 +01:00
bufptr = buf.data();
sz = 0;
2022-09-15 18:41:15 +02:00
ackInfo.setAckedDirective(FileDirective::KEEP_ALIVE);
2022-09-08 12:07:16 +02:00
auto ackSerializer3 = AckPduCreator(ackInfo, pduConf);
2022-02-02 10:29:30 +01:00
result = ackSerializer3.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
// Invalid file directive
2022-08-16 01:08:26 +02:00
REQUIRE(result != returnvalue::OK);
2022-09-15 18:41:15 +02:00
ackInfo.setAckedDirective(FileDirective::FINISH);
2022-02-02 10:29:30 +01:00
// buffer too small
result = ackSerializer.serialize(&bufptr, &sz, 8, SerializeIF::Endianness::NETWORK);
REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT);
}
2022-02-02 10:29:30 +01:00
SECTION("Deserialize") {
2022-08-03 13:15:49 +02:00
AckInfo ackInfo2;
2022-09-08 12:07:16 +02:00
auto reader = AckPduReader(buf.data(), sz, ackInfo2);
2022-02-02 10:29:30 +01:00
result = reader.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(ackInfo2.getAckedDirective() == FileDirective::EOF_DIRECTIVE);
2022-08-03 13:15:49 +02:00
REQUIRE(ackInfo2.getAckedConditionCode() == ConditionCode::NO_ERROR);
REQUIRE(ackInfo2.getDirectiveSubtypeCode() == 0);
REQUIRE(ackInfo2.getTransactionStatus() == AckTransactionStatus::ACTIVE);
2022-09-15 18:41:15 +02:00
AckInfo newInfo = AckInfo(FileDirective::FINISH, ConditionCode::FILESTORE_REJECTION,
2022-02-02 10:29:30 +01:00
AckTransactionStatus::TERMINATED);
2022-09-08 12:07:16 +02:00
auto ackSerializer2 = AckPduCreator(newInfo, pduConf);
2022-02-02 10:29:30 +01:00
bufptr = buf.data();
sz = 0;
result = ackSerializer2.serialize(&bufptr, &sz, maxsz, SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-08 12:07:16 +02:00
auto reader2 = AckPduReader(buf.data(), sz, ackInfo2);
2022-02-02 10:29:30 +01:00
result = reader2.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(ackInfo2.getAckedDirective() == FileDirective::FINISH);
2022-08-03 13:15:49 +02:00
REQUIRE(ackInfo2.getAckedConditionCode() == ConditionCode::FILESTORE_REJECTION);
REQUIRE(ackInfo2.getDirectiveSubtypeCode() == 0b0001);
REQUIRE(ackInfo2.getTransactionStatus() == AckTransactionStatus::TERMINATED);
2022-02-02 10:29:30 +01:00
uint8_t prevVal = buf[sz - 2];
2022-09-15 18:41:15 +02:00
buf[sz - 2] = FileDirective::INVALID_DIRECTIVE << 4;
2022-02-02 10:29:30 +01:00
result = reader2.parseData();
REQUIRE(result == cfdp::INVALID_ACK_DIRECTIVE_FIELDS);
2022-09-15 18:41:15 +02:00
buf[sz - 2] = FileDirective::FINISH << 4 | 0b1111;
2022-02-02 10:29:30 +01:00
result = reader2.parseData();
REQUIRE(result == cfdp::INVALID_ACK_DIRECTIVE_FIELDS);
buf[sz - 2] = prevVal;
2022-09-15 18:41:15 +02:00
buf[sz - 3] = cfdp::FileDirective::INVALID_DIRECTIVE;
2022-02-02 10:29:30 +01:00
result = reader2.parseData();
2022-09-08 11:53:06 +02:00
REQUIRE(result == cfdp::INVALID_DIRECTIVE_FIELD);
2022-09-15 18:41:15 +02:00
buf[sz - 3] = cfdp::FileDirective::ACK;
2022-09-08 12:07:16 +02:00
auto maxSizeTooSmall = AckPduReader(buf.data(), sz - 2, ackInfo2);
2022-02-02 10:29:30 +01:00
result = maxSizeTooSmall.parseData();
REQUIRE(result == SerializeIF::STREAM_TOO_SHORT);
}
}