fsfw/unittests/cfdp/pdu/testFileDirective.cpp

86 lines
3.1 KiB
C++
Raw Permalink Normal View History

2022-08-03 13:23:49 +02:00
#include <array>
#include <catch2/catch_test_macros.hpp>
2022-08-03 16:00:48 +02:00
#include "fsfw/cfdp/pdu/FileDirectiveCreator.h"
#include "fsfw/cfdp/pdu/FileDirectiveReader.h"
2022-08-03 13:23:49 +02:00
2022-08-09 15:09:43 +02:00
TEST_CASE("CFDP File Directive", "[cfdp][pdu]") {
2022-08-03 13:23:49 +02:00
using namespace cfdp;
std::array<uint8_t, 32> serBuf{};
ReturnValue_t result;
cfdp::TransactionSeqNum seqNum = TransactionSeqNum(cfdp::WidthInBytes::ONE_BYTE, 2);
cfdp::EntityId sourceId = EntityId(cfdp::WidthInBytes::ONE_BYTE, 0);
cfdp::EntityId destId = EntityId(cfdp::WidthInBytes::ONE_BYTE, 1);
PduConfig pduConf =
2022-09-15 18:41:15 +02:00
PduConfig(sourceId, destId, cfdp::TransmissionMode::ACKNOWLEDGED, seqNum, false);
2022-08-03 13:23:49 +02:00
uint8_t* serTarget = serBuf.data();
const uint8_t* deserTarget = serTarget;
size_t serSize = 0;
2022-09-15 18:41:15 +02:00
auto fdSer = FileDirectiveCreator(pduConf, FileDirective::ACK, 4);
2022-08-03 13:23:49 +02:00
2022-08-09 10:59:24 +02:00
SECTION("Serialization") {
2022-08-03 13:23:49 +02:00
REQUIRE(fdSer.getSerializedSize() == 8);
serTarget = serBuf.data();
serSize = 0;
result = fdSer.serialize(&serTarget, &serSize, serBuf.size(), SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
// Only version bits are set
REQUIRE(serBuf[0] == 0b00100000);
// PDU data field length is 5 (4 + Directive code octet)
REQUIRE(serBuf[1] == 0);
REQUIRE(serBuf[2] == 5);
// Entity and Transaction Sequence number are 1 byte large
2023-05-25 15:11:56 +02:00
REQUIRE(serBuf[3] == 0b00000000);
2022-08-03 13:23:49 +02:00
// Source ID
REQUIRE(serBuf[4] == 0);
// Transaction Seq Number
REQUIRE(serBuf[5] == 2);
// Dest ID
REQUIRE(serBuf[6] == 1);
2022-09-15 18:41:15 +02:00
REQUIRE(serBuf[7] == FileDirective::ACK);
2022-08-09 10:59:24 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 10:59:24 +02:00
SECTION("Serialization fails") {
2022-08-03 13:23:49 +02:00
REQUIRE(fdSer.serialize(nullptr, nullptr, 85, SerializeIF::Endianness::NETWORK) ==
2022-08-22 16:35:53 +02:00
returnvalue::FAILED);
2022-08-09 10:59:24 +02:00
}
SECTION("Buffer Too Short") {
2022-08-03 13:23:49 +02:00
for (uint8_t idx = 0; idx < 8; idx++) {
serTarget = serBuf.data();
serSize = 0;
REQUIRE(fdSer.serialize(&serTarget, &serSize, idx, SerializeIF::Endianness::NETWORK) ==
SerializeIF::BUFFER_TOO_SHORT);
}
2022-08-09 10:59:24 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 10:59:24 +02:00
SECTION("Deserialize") {
CHECK(fdSer.serialize(&serTarget, &serSize, serBuf.size(), SerializeIF::Endianness::NETWORK) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-09 10:59:24 +02:00
serTarget = serBuf.data();
REQUIRE(fdSer.deSerialize(&deserTarget, &serSize, SerializeIF::Endianness::NETWORK) ==
2022-08-22 16:35:53 +02:00
returnvalue::FAILED);
2022-08-03 13:23:49 +02:00
deserTarget = serBuf.data();
2022-08-09 10:59:24 +02:00
CHECK(serSize == 8);
2022-08-03 16:00:48 +02:00
auto fdDeser = FileDirectiveReader(deserTarget, serBuf.size());
2022-08-09 11:03:22 +02:00
REQUIRE(fdDeser.isNull());
REQUIRE(not fdDeser);
2022-08-03 13:23:49 +02:00
REQUIRE(fdDeser.getEndianness() == SerializeIF::Endianness::NETWORK);
fdDeser.setEndianness(SerializeIF::Endianness::MACHINE);
REQUIRE(fdDeser.getEndianness() == SerializeIF::Endianness::MACHINE);
fdDeser.setEndianness(SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(fdDeser.parseData() == returnvalue::OK);
2022-08-09 11:03:22 +02:00
REQUIRE(not fdDeser.isNull());
REQUIRE(fdDeser);
2022-09-15 18:41:15 +02:00
REQUIRE(fdDeser.getFileDirective() == FileDirective::ACK);
2022-08-03 13:23:49 +02:00
REQUIRE(fdDeser.getPduDataFieldLen() == 5);
REQUIRE(fdDeser.getHeaderSize() == 8);
2022-09-15 18:41:15 +02:00
REQUIRE(fdDeser.getPduType() == cfdp::PduType::FILE_DIRECTIVE);
2022-08-03 13:23:49 +02:00
serBuf[7] = 0xff;
// Invalid file directive
2022-09-08 11:53:06 +02:00
REQUIRE(fdDeser.parseData() == cfdp::INVALID_DIRECTIVE_FIELD);
2022-08-03 13:23:49 +02:00
}
2023-05-25 15:11:56 +02:00
}