fsfw/unittests/cfdp/pdu/testCfdpHeader.cpp

317 lines
15 KiB
C++
Raw Normal View History

2022-08-03 13:23:49 +02:00
#include <array>
#include <catch2/catch_test_macros.hpp>
2022-08-03 15:22:39 +02:00
#include "fsfw/cfdp/pdu/HeaderCreator.h"
#include "fsfw/cfdp/pdu/HeaderReader.h"
2022-08-22 16:35:53 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2022-08-03 13:23:49 +02:00
TEST_CASE("CFDP Header", "[cfdp]") {
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 =
PduConfig(sourceId, destId, cfdp::TransmissionModes::ACKNOWLEDGED, seqNum, false);
uint8_t* serTarget = serBuf.data();
const uint8_t* deserTarget = serTarget;
size_t serSize = 0;
2022-08-09 11:17:57 +02:00
auto headerSerializer = HeaderCreator(pduConf, cfdp::PduType::FILE_DIRECTIVE, 0);
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Header State") {
2022-08-03 13:23:49 +02:00
REQUIRE(seqNum.getSerializedSize() == 1);
REQUIRE(headerSerializer.getPduDataFieldLen() == 0);
REQUIRE(headerSerializer.getSerializedSize() == 7);
REQUIRE(headerSerializer.getWholePduSize() == 7);
REQUIRE(headerSerializer.getCrcFlag() == false);
REQUIRE(headerSerializer.getDirection() == cfdp::Direction::TOWARDS_RECEIVER);
REQUIRE(headerSerializer.getLargeFileFlag() == false);
REQUIRE(headerSerializer.getLenEntityIds() == 1);
REQUIRE(headerSerializer.getLenSeqNum() == 1);
REQUIRE(headerSerializer.getPduType() == cfdp::PduType::FILE_DIRECTIVE);
REQUIRE(headerSerializer.getSegmentMetadataFlag() == cfdp::SegmentMetadataFlag::NOT_PRESENT);
REQUIRE(headerSerializer.getSegmentationControl() == false);
REQUIRE(headerSerializer.getTransmissionMode() == cfdp::TransmissionModes::ACKNOWLEDGED);
cfdp::TransactionSeqNum seqNumLocal;
headerSerializer.getTransactionSeqNum(seqNumLocal);
REQUIRE(seqNumLocal.getWidth() == cfdp::WidthInBytes::ONE_BYTE);
REQUIRE(seqNumLocal.getValue() == 2);
cfdp::EntityId sourceDestId;
headerSerializer.getSourceId(sourceDestId);
REQUIRE(sourceDestId.getWidth() == cfdp::WidthInBytes::ONE_BYTE);
REQUIRE(sourceDestId.getValue() == 0);
headerSerializer.getDestId(sourceDestId);
REQUIRE(sourceDestId.getWidth() == cfdp::WidthInBytes::ONE_BYTE);
REQUIRE(sourceDestId.getValue() == 1);
2022-08-09 11:17:57 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Deserialization fails") {
const uint8_t** dummyPtr = nullptr;
REQUIRE(headerSerializer.deSerialize(dummyPtr, &serSize, SerializeIF::Endianness::NETWORK) ==
2022-08-22 16:35:53 +02:00
returnvalue::FAILED);
2022-08-09 11:17:57 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Serialization fails") {
REQUIRE(headerSerializer.serialize(nullptr, &serSize, serBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::FAILED);
2022-08-09 11:17:57 +02:00
}
SECTION("Buffer Too Short") {
2022-08-03 13:23:49 +02:00
for (uint8_t idx = 0; idx < 7; idx++) {
result = headerSerializer.serialize(&serTarget, &serSize, idx, SerializeIF::Endianness::BIG);
REQUIRE(result == static_cast<int>(SerializeIF::BUFFER_TOO_SHORT));
}
2022-08-09 11:17:57 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Set Data Field Len") {
2022-08-03 13:23:49 +02:00
// Set PDU data field len
headerSerializer.setPduDataFieldLen(0x0ff0);
REQUIRE(headerSerializer.getPduDataFieldLen() == 0x0ff0);
REQUIRE(headerSerializer.getSerializedSize() == 7);
REQUIRE(headerSerializer.getWholePduSize() == 7 + 0x0ff0);
serTarget = serBuf.data();
serSize = 0;
result = headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
SerializeIF::Endianness::BIG);
REQUIRE(serBuf[1] == 0x0f);
REQUIRE(serBuf[2] == 0xf0);
2022-08-09 11:17:57 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Serialize with Fields Flipped") {
2022-08-03 13:23:49 +02:00
pduConf.crcFlag = true;
pduConf.largeFile = true;
pduConf.direction = cfdp::Direction::TOWARDS_SENDER;
pduConf.mode = cfdp::TransmissionModes::UNACKNOWLEDGED;
headerSerializer.setSegmentationControl(
cfdp::SegmentationControl::RECORD_BOUNDARIES_PRESERVATION);
headerSerializer.setPduType(cfdp::PduType::FILE_DATA);
headerSerializer.setSegmentMetadataFlag(cfdp::SegmentMetadataFlag::PRESENT);
serTarget = serBuf.data();
serSize = 0;
2022-08-09 11:17:57 +02:00
SECTION("Regular") {
// Everything except version bit flipped to one now
REQUIRE(headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-08-09 11:17:57 +02:00
CHECK(serBuf[0] == 0x3f);
CHECK(serBuf[3] == 0x99);
REQUIRE(headerSerializer.getCrcFlag() == true);
REQUIRE(headerSerializer.getDirection() == cfdp::Direction::TOWARDS_SENDER);
REQUIRE(headerSerializer.getLargeFileFlag() == true);
REQUIRE(headerSerializer.getLenEntityIds() == 1);
REQUIRE(headerSerializer.getLenSeqNum() == 1);
REQUIRE(headerSerializer.getPduType() == cfdp::PduType::FILE_DATA);
REQUIRE(headerSerializer.getSegmentMetadataFlag() == cfdp::SegmentMetadataFlag::PRESENT);
REQUIRE(headerSerializer.getTransmissionMode() == cfdp::TransmissionModes::UNACKNOWLEDGED);
REQUIRE(headerSerializer.getSegmentationControl() == true);
}
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Other variable sized fields") {
pduConf.seqNum.setValue(cfdp::WidthInBytes::TWO_BYTES, 0x0fff);
pduConf.sourceId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0xff00ff00);
pduConf.destId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0x00ff00ff);
REQUIRE(pduConf.sourceId.getSerializedSize() == 4);
REQUIRE(headerSerializer.getSerializedSize() == 14);
REQUIRE(headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-08-09 11:17:57 +02:00
REQUIRE(headerSerializer.getCrcFlag() == true);
REQUIRE(headerSerializer.getDirection() == cfdp::Direction::TOWARDS_SENDER);
REQUIRE(headerSerializer.getLargeFileFlag() == true);
REQUIRE(headerSerializer.getLenEntityIds() == 4);
REQUIRE(headerSerializer.getLenSeqNum() == 2);
REQUIRE(headerSerializer.getPduType() == cfdp::PduType::FILE_DATA);
REQUIRE(headerSerializer.getSegmentMetadataFlag() == cfdp::SegmentMetadataFlag::PRESENT);
REQUIRE(headerSerializer.getTransmissionMode() == cfdp::TransmissionModes::UNACKNOWLEDGED);
REQUIRE(headerSerializer.getSegmentationControl() == true);
// Last three bits are 2 now (length of seq number) and bit 1 to bit 3 is 4 (len entity IDs)
REQUIRE(serBuf[3] == 0b11001010);
uint32_t entityId = 0;
size_t deSerSize = 0;
SerializeAdapter::deSerialize(&entityId, serBuf.data() + 4, &deSerSize,
SerializeIF::Endianness::NETWORK);
CHECK(deSerSize == 4);
CHECK(entityId == 0xff00ff00);
uint16_t seqNumRaw = 0;
SerializeAdapter::deSerialize(&seqNumRaw, serBuf.data() + 8, &deSerSize,
SerializeIF::Endianness::NETWORK);
CHECK(deSerSize == 2);
CHECK(seqNumRaw == 0x0fff);
SerializeAdapter::deSerialize(&entityId, serBuf.data() + 10, &deSerSize,
SerializeIF::Endianness::NETWORK);
CHECK(deSerSize == 4);
CHECK(entityId == 0x00ff00ff);
}
SECTION("Buffer Too Short") {
pduConf.seqNum.setValue(cfdp::WidthInBytes::TWO_BYTES, 0x0fff);
pduConf.sourceId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0xff00ff00);
pduConf.destId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0x00ff00ff);
for (uint8_t idx = 0; idx < 14; idx++) {
REQUIRE(
headerSerializer.serialize(&serTarget, &serSize, idx, SerializeIF::Endianness::BIG) ==
SerializeIF::BUFFER_TOO_SHORT);
}
2022-08-03 13:23:49 +02:00
}
2022-08-09 11:17:57 +02:00
}
2022-08-03 13:23:49 +02:00
2022-08-09 11:17:57 +02:00
SECTION("Invalid Variable Sized Fields") {
2022-08-03 13:23:49 +02:00
result = pduConf.sourceId.setValue(cfdp::WidthInBytes::ONE_BYTE, 0xfff);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::FAILED);
2022-08-03 13:23:49 +02:00
result = pduConf.sourceId.setValue(cfdp::WidthInBytes::TWO_BYTES, 0xfffff);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::FAILED);
2022-08-03 13:23:49 +02:00
result = pduConf.sourceId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0xfffffffff);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::FAILED);
2022-08-09 11:17:57 +02:00
}
SECTION("Header Serialization") {
result = headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
SerializeIF::Endianness::BIG);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-09 11:17:57 +02:00
REQUIRE(serSize == 7);
// Only version bits are set
REQUIRE(serBuf[0] == 0b00100000);
// PDU data field length is 0
REQUIRE(serBuf[1] == 0);
REQUIRE(serBuf[2] == 0);
// Entity and Transaction Sequence number are 1 byte large
REQUIRE(serBuf[3] == 0b00010001);
// Source ID
REQUIRE(serBuf[4] == 0);
// Transaction Seq Number
REQUIRE(serBuf[5] == 2);
// Dest ID
REQUIRE(serBuf[6] == 1);
2022-08-03 13:23:49 +02:00
uint8_t oneByteSourceId = 32;
serTarget = &oneByteSourceId;
size_t deserLen = 1;
pduConf.sourceId.deSerialize(cfdp::WidthInBytes::ONE_BYTE,
const_cast<const uint8_t**>(&serTarget), &deserLen,
SerializeIF::Endianness::MACHINE);
REQUIRE(pduConf.sourceId.getValue() == 32);
uint16_t twoByteSourceId = 0xf0f0;
serTarget = reinterpret_cast<uint8_t*>(&twoByteSourceId);
deserLen = 2;
pduConf.sourceId.deSerialize(cfdp::WidthInBytes::TWO_BYTES,
const_cast<const uint8_t**>(&serTarget), &deserLen,
SerializeIF::Endianness::MACHINE);
REQUIRE(pduConf.sourceId.getValue() == 0xf0f0);
uint32_t fourByteSourceId = 0xf0f0f0f0;
serTarget = reinterpret_cast<uint8_t*>(&fourByteSourceId);
deserLen = 4;
pduConf.sourceId.deSerialize(cfdp::WidthInBytes::FOUR_BYTES,
const_cast<const uint8_t**>(&serTarget), &deserLen,
SerializeIF::Endianness::MACHINE);
REQUIRE(pduConf.sourceId.getValue() == 0xf0f0f0f0);
pduConf.sourceId.setValue(cfdp::WidthInBytes::ONE_BYTE, 1);
serTarget = serBuf.data();
serSize = 1;
result = pduConf.sourceId.serialize(&serTarget, &serSize, 1, SerializeIF::Endianness::MACHINE);
REQUIRE(result == static_cast<int>(SerializeIF::BUFFER_TOO_SHORT));
}
SECTION("Header Deserialization") {
2022-08-09 11:17:57 +02:00
REQUIRE(headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-08-03 13:23:49 +02:00
REQUIRE(serBuf[1] == 0);
REQUIRE(serBuf[2] == 0);
// Entity and Transaction Sequence number are 1 byte large
REQUIRE(serBuf[3] == 0b00010001);
REQUIRE(serSize == 7);
// Deser call not strictly necessary
2022-08-03 15:22:39 +02:00
auto headerDeser = HeaderReader(serBuf.data(), serBuf.size());
2022-08-03 13:23:49 +02:00
ReturnValue_t serResult = headerDeser.parseData();
2022-08-22 16:35:53 +02:00
REQUIRE(serResult == returnvalue::OK);
2022-08-03 13:23:49 +02:00
REQUIRE(headerDeser.getPduDataFieldLen() == 0);
REQUIRE(headerDeser.getHeaderSize() == 7);
REQUIRE(headerDeser.getWholePduSize() == 7);
REQUIRE(headerDeser.getCrcFlag() == false);
REQUIRE(headerDeser.getDirection() == cfdp::Direction::TOWARDS_RECEIVER);
REQUIRE(headerDeser.getLargeFileFlag() == false);
REQUIRE(headerDeser.getLenEntityIds() == 1);
REQUIRE(headerDeser.getLenSeqNum() == 1);
REQUIRE(headerDeser.getPduType() == cfdp::PduType::FILE_DIRECTIVE);
REQUIRE(headerDeser.getSegmentMetadataFlag() == cfdp::SegmentMetadataFlag::NOT_PRESENT);
REQUIRE(headerDeser.getSegmentationControl() == false);
REQUIRE(headerDeser.getTransmissionMode() == cfdp::TransmissionModes::ACKNOWLEDGED);
pduConf.crcFlag = true;
pduConf.largeFile = true;
pduConf.direction = cfdp::Direction::TOWARDS_SENDER;
pduConf.mode = cfdp::TransmissionModes::UNACKNOWLEDGED;
headerSerializer.setSegmentationControl(
cfdp::SegmentationControl::RECORD_BOUNDARIES_PRESERVATION);
headerSerializer.setPduType(cfdp::PduType::FILE_DATA);
headerSerializer.setSegmentMetadataFlag(cfdp::SegmentMetadataFlag::PRESENT);
result = pduConf.seqNum.setValue(cfdp::WidthInBytes::TWO_BYTES, 0x0fff);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
result = pduConf.sourceId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0xff00ff00);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
result = pduConf.destId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 0x00ff00ff);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
serTarget = serBuf.data();
serSize = 0;
result = headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
SerializeIF::Endianness::BIG);
2022-08-03 15:22:39 +02:00
headerDeser = HeaderReader(serBuf.data(), serBuf.size());
2022-08-03 13:23:49 +02:00
result = headerDeser.parseData();
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
// Everything except version bit flipped to one now
REQUIRE(serBuf[0] == 0x3f);
REQUIRE(serBuf[3] == 0b11001010);
REQUIRE(headerDeser.getWholePduSize() == 14);
REQUIRE(headerDeser.getCrcFlag() == true);
REQUIRE(headerDeser.getDirection() == cfdp::Direction::TOWARDS_SENDER);
REQUIRE(headerDeser.getLargeFileFlag() == true);
REQUIRE(headerDeser.getLenEntityIds() == 4);
REQUIRE(headerDeser.getLenSeqNum() == 2);
REQUIRE(headerDeser.getPduType() == cfdp::PduType::FILE_DATA);
REQUIRE(headerDeser.getSegmentMetadataFlag() == cfdp::SegmentMetadataFlag::PRESENT);
REQUIRE(headerDeser.getSegmentationControl() == true);
REQUIRE(headerDeser.getTransmissionMode() == cfdp::TransmissionModes::UNACKNOWLEDGED);
cfdp::TransactionSeqNum seqNumLocal;
headerDeser.getTransactionSeqNum(seqNumLocal);
REQUIRE(seqNumLocal.getWidth() == cfdp::WidthInBytes::TWO_BYTES);
REQUIRE(seqNumLocal.getValue() == 0x0fff);
cfdp::EntityId sourceDestId;
headerDeser.getSourceId(sourceDestId);
REQUIRE(sourceDestId.getWidth() == cfdp::WidthInBytes::FOUR_BYTES);
REQUIRE(sourceDestId.getValue() == 0xff00ff00);
headerDeser.getDestId(sourceDestId);
REQUIRE(sourceDestId.getWidth() == cfdp::WidthInBytes::FOUR_BYTES);
REQUIRE(sourceDestId.getValue() == 0x00ff00ff);
size_t deSerSize = headerDeser.getWholePduSize();
serTarget = serBuf.data();
const auto** serTargetConst = const_cast<const uint8_t**>(&serTarget);
result = headerDeser.parseData();
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
2022-08-22 16:35:53 +02:00
CHECK(headerDeser.setData(nullptr, -1) != returnvalue::OK);
2022-08-09 11:03:22 +02:00
REQUIRE(headerDeser.getHeaderSize() == 14);
2022-08-03 13:23:49 +02:00
headerDeser.setData(serBuf.data(), serBuf.size());
serTarget = serBuf.data();
serSize = 0;
pduConf.sourceId.setValue(cfdp::WidthInBytes::ONE_BYTE, 22);
pduConf.destId.setValue(cfdp::WidthInBytes::ONE_BYTE, 48);
result = headerSerializer.serialize(&serTarget, &serSize, serBuf.size(),
SerializeIF::Endianness::BIG);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-03 13:23:49 +02:00
REQUIRE(headerDeser.getWholePduSize() == 8);
headerDeser.setData(serBuf.data(), serBuf.size());
headerDeser.getSourceId(sourceDestId);
REQUIRE(sourceDestId.getWidth() == cfdp::WidthInBytes::ONE_BYTE);
REQUIRE(sourceDestId.getValue() == 22);
}
}