fsfw/unittests/cfdp/pdu/testEofPdu.cpp

119 lines
5.0 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-02 14:39:53 +02:00
#include "fsfw/cfdp/pdu/EofPduCreator.h"
#include "fsfw/cfdp/pdu/EofPduReader.h"
#include "fsfw/globalfunctions/arrayprinter.h"
2022-08-03 13:23:49 +02:00
TEST_CASE("EOF PDU", "[cfdp][pdu]") {
2022-02-02 10:29:30 +01:00
using namespace cfdp;
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
std::array<uint8_t, 128> buf = {};
uint8_t* bufPtr = buf.data();
size_t sz = 0;
EntityId destId(WidthInBytes::TWO_BYTES, 2);
EntityIdTlv faultLoc(destId);
2023-07-19 13:44:52 +02:00
Fss fileSize(12);
2022-02-02 10:29:30 +01:00
// We can already set the fault location, it will be ignored
EofInfo eofInfo(cfdp::ConditionCode::NO_ERROR, 5, fileSize, &faultLoc);
TransactionSeqNum seqNum(WidthInBytes::TWO_BYTES, 15);
EntityId sourceId(WidthInBytes::TWO_BYTES, 1);
2022-09-15 18:41:15 +02:00
PduConfig pduConf(sourceId, destId, TransmissionMode::ACKNOWLEDGED, seqNum);
2022-09-02 14:39:53 +02:00
auto eofSerializer = EofPduCreator(pduConf, eofInfo);
2022-02-02 10:29:30 +01:00
SECTION("Serialize") {
result = eofSerializer.serialize(&bufPtr, &sz, buf.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(((buf[1] << 8) | buf[2]) == 10);
uint32_t checksum = 0;
result = SerializeAdapter::deSerialize(&checksum, buf.data() + sz - 8, nullptr,
SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(checksum == 5);
uint32_t fileSizeVal = 0;
result = SerializeAdapter::deSerialize(&fileSizeVal, buf.data() + sz - 4, nullptr,
SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(fileSizeVal == 12);
2022-09-15 18:41:15 +02:00
REQUIRE(buf[sz - 10] == cfdp::FileDirective::EOF_DIRECTIVE);
2022-02-02 10:29:30 +01:00
REQUIRE(buf[sz - 9] == 0x00);
REQUIRE(sz == 20);
2022-02-02 10:29:30 +01:00
eofInfo.setConditionCode(cfdp::ConditionCode::FILESTORE_REJECTION);
eofInfo.setFileSize(0x10ffffff10, true);
pduConf.largeFile = true;
// Should serialize with fault location now
2022-09-02 14:39:53 +02:00
auto serializeWithFaultLocation = EofPduCreator(pduConf, eofInfo);
2022-02-02 10:29:30 +01:00
bufPtr = buf.data();
sz = 0;
result = serializeWithFaultLocation.serialize(&bufPtr, &sz, buf.size(),
SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(sz == 28);
2022-09-15 18:41:15 +02:00
REQUIRE(buf[10] == cfdp::FileDirective::EOF_DIRECTIVE);
2022-02-02 10:29:30 +01:00
REQUIRE(buf[11] >> 4 == cfdp::ConditionCode::FILESTORE_REJECTION);
uint64_t fileSizeLarge = 0;
result = SerializeAdapter::deSerialize(&fileSizeLarge, buf.data() + 16, nullptr,
SerializeIF::Endianness::NETWORK);
REQUIRE(fileSizeLarge == 0x10ffffff10);
2022-09-15 18:41:15 +02:00
REQUIRE(buf[sz - 4] == cfdp::TlvType::ENTITY_ID);
2022-02-02 10:29:30 +01:00
// width of entity ID is 2
REQUIRE(buf[sz - 3] == 2);
uint16_t entityIdRaw = 0;
result = SerializeAdapter::deSerialize(&entityIdRaw, buf.data() + sz - 2, nullptr,
SerializeIF::Endianness::NETWORK);
REQUIRE(entityIdRaw == 2);
bufPtr = buf.data();
sz = 0;
for (size_t idx = 0; idx < 27; idx++) {
result =
serializeWithFaultLocation.serialize(&bufPtr, &sz, idx, SerializeIF::Endianness::NETWORK);
REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT);
bufPtr = buf.data();
sz = 0;
}
2022-02-02 10:29:30 +01:00
eofInfo.setChecksum(16);
eofInfo.setFaultLoc(nullptr);
}
2022-02-02 10:29:30 +01:00
SECTION("Deserialize") {
result = eofSerializer.serialize(&bufPtr, &sz, buf.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
EntityIdTlv tlv(destId);
EofInfo emptyInfo(&tlv);
2022-09-02 14:39:53 +02:00
auto deserializer = EofPduReader(buf.data(), buf.size(), emptyInfo);
2022-02-02 10:29:30 +01:00
result = deserializer.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(emptyInfo.getConditionCode() == cfdp::ConditionCode::NO_ERROR);
REQUIRE(emptyInfo.getChecksum() == 5);
REQUIRE(emptyInfo.getFileSize().getSize() == 12);
2022-02-02 10:29:30 +01:00
eofInfo.setConditionCode(cfdp::ConditionCode::FILESTORE_REJECTION);
eofInfo.setFileSize(0x10ffffff10, true);
pduConf.largeFile = true;
// Should serialize with fault location now
2022-09-02 14:39:53 +02:00
auto serializeWithFaultLocation = EofPduCreator(pduConf, eofInfo);
2022-02-02 10:29:30 +01:00
bufPtr = buf.data();
sz = 0;
result = serializeWithFaultLocation.serialize(&bufPtr, &sz, buf.size(),
SerializeIF::Endianness::NETWORK);
2022-09-02 14:39:53 +02:00
auto deserializer2 = EofPduReader(buf.data(), buf.size(), emptyInfo);
2022-02-02 10:29:30 +01:00
result = deserializer2.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(emptyInfo.getConditionCode() == cfdp::ConditionCode::FILESTORE_REJECTION);
REQUIRE(emptyInfo.getChecksum() == 5);
REQUIRE(emptyInfo.getFileSize().getSize() == 0x10ffffff10);
2022-09-15 18:41:15 +02:00
REQUIRE(emptyInfo.getFaultLoc()->getType() == cfdp::TlvType::ENTITY_ID);
2022-02-02 10:29:30 +01:00
REQUIRE(emptyInfo.getFaultLoc()->getSerializedSize() == 4);
uint16_t destId = emptyInfo.getFaultLoc()->getEntityId().getValue();
REQUIRE(destId == 2);
for (size_t maxSz = 0; maxSz < deserializer2.getWholePduSize() - 1; maxSz++) {
2022-09-02 14:39:53 +02:00
auto invalidDeser = EofPduReader(buf.data(), maxSz, emptyInfo);
2022-02-02 10:29:30 +01:00
result = invalidDeser.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result != returnvalue::OK);
}
2022-02-02 10:29:30 +01:00
}
}