fsfw/unittests/cfdp/testTlv.cpp

129 lines
5.3 KiB
C++
Raw Permalink Normal View History

2022-08-10 09:39:57 +02:00
#include <array>
#include <catch2/catch_test_macros.hpp>
#include "fsfw/cfdp.h"
2022-09-14 19:15:52 +02:00
#include "fsfw/cfdp/VarLenFields.h"
2022-08-10 09:39:57 +02:00
TEST_CASE("CFDP TLV", "[cfdp][tlv]") {
using namespace cfdp;
ReturnValue_t result;
std::array<uint8_t, 255> rawBuf{};
uint8_t* serPtr = rawBuf.data();
const uint8_t* deserPtr = rawBuf.data();
size_t deserSize = 0;
cfdp::EntityId sourceId = EntityId(cfdp::WidthInBytes::TWO_BYTES, 0x0ff0);
SECTION("Entity ID Serialization") {
REQUIRE(sourceId.serialize(&serPtr, &deserSize, rawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(deserSize == 2);
}
SECTION("TLV Serialization") {
std::array<uint8_t, 8> tlvBuf{};
2022-08-22 16:35:53 +02:00
REQUIRE(sourceId.serializeBe(tlvBuf.data(), deserSize, tlvBuf.size()) == returnvalue::OK);
2022-09-15 18:41:15 +02:00
auto tlv = Tlv(TlvType::ENTITY_ID, tlvBuf.data(), deserSize);
2022-08-10 09:39:57 +02:00
REQUIRE(tlv.getSerializedSize() == 4);
REQUIRE(tlv.getLengthField() == 2);
deserSize = 0;
REQUIRE(tlv.serialize(&serPtr, &deserSize, rawBuf.size(), SerializeIF::Endianness::NETWORK) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(deserSize == 4);
2022-09-15 18:41:15 +02:00
REQUIRE(rawBuf[0] == TlvType::ENTITY_ID);
2022-08-10 09:39:57 +02:00
REQUIRE(rawBuf[1] == 2);
uint16_t entityId = 0;
REQUIRE(SerializeAdapter::deSerialize(&entityId, rawBuf.data() + 2, &deserSize,
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(entityId == 0x0ff0);
}
SECTION("TLV Other Value") {
2022-09-15 18:41:15 +02:00
auto tlv = Tlv(TlvType::ENTITY_ID, rawBuf.data(), deserSize);
2022-08-10 09:39:57 +02:00
// Set new value
sourceId.setValueAndWidth(cfdp::WidthInBytes::FOUR_BYTES, 12);
2022-08-10 09:39:57 +02:00
REQUIRE(sourceId.serialize(&serPtr, &deserSize, rawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
tlv.setValue(rawBuf.data(), cfdp::WidthInBytes::FOUR_BYTES);
serPtr = rawBuf.data();
deserSize = 0;
result = tlv.serialize(&serPtr, &deserSize, rawBuf.size(), SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(rawBuf[0] == TlvType::ENTITY_ID);
2022-08-10 09:39:57 +02:00
REQUIRE(rawBuf[1] == 4);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
}
SECTION("TLV Invalid") {
2022-09-15 18:41:15 +02:00
auto tlvInvalid = Tlv(cfdp::TlvType::INVALID_TLV, rawBuf.data(), 0);
2022-08-10 09:39:57 +02:00
REQUIRE(tlvInvalid.serialize(&serPtr, &deserSize, rawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) != returnvalue::OK);
2022-09-15 18:41:15 +02:00
tlvInvalid = Tlv(cfdp::TlvType::ENTITY_ID, nullptr, 3);
2022-08-10 09:39:57 +02:00
REQUIRE(tlvInvalid.serialize(&serPtr, &deserSize, rawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) != returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(tlvInvalid.serialize(&serPtr, &deserSize, 0, SerializeIF::Endianness::NETWORK) !=
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(tlvInvalid.getSerializedSize() == 0);
REQUIRE(tlvInvalid.serialize(nullptr, nullptr, 0, SerializeIF::Endianness::NETWORK) !=
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-10 09:39:57 +02:00
}
SECTION("TLV Zero Length Field") {
2022-09-15 18:41:15 +02:00
Tlv zeroLenField(TlvType::FAULT_HANDLER, nullptr, 0);
2022-08-10 09:39:57 +02:00
REQUIRE(zeroLenField.getSerializedSize() == 2);
serPtr = rawBuf.data();
deserSize = 0;
REQUIRE(zeroLenField.serialize(&serPtr, &deserSize, rawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(rawBuf[0] == TlvType::FAULT_HANDLER);
2022-08-10 09:39:57 +02:00
REQUIRE(rawBuf[1] == 0);
}
SECTION("TLV Deserialization") {
// Serialization was tested before, generate raw data now
std::array<uint8_t, 8> tlvRawBuf{};
serPtr = tlvRawBuf.data();
result =
sourceId.serialize(&serPtr, &deserSize, tlvRawBuf.size(), SerializeIF::Endianness::NETWORK);
2022-09-15 18:41:15 +02:00
auto tlvSerialization = Tlv(TlvType::ENTITY_ID, tlvRawBuf.data(), deserSize);
2022-08-10 09:39:57 +02:00
serPtr = rawBuf.data();
deserSize = 0;
result = tlvSerialization.serialize(&serPtr, &deserSize, rawBuf.size(),
SerializeIF::Endianness::NETWORK);
Tlv tlv;
deserPtr = rawBuf.data();
result = tlv.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(tlv.getSerializedSize() == 4);
2022-09-15 18:41:15 +02:00
REQUIRE(tlv.getType() == TlvType::ENTITY_ID);
2022-08-10 09:39:57 +02:00
deserPtr = tlv.getValue();
uint16_t entityId = 0;
deserSize = 0;
SerializeAdapter::deSerialize(&entityId, deserPtr, &deserSize,
SerializeIF::Endianness::NETWORK);
REQUIRE(entityId == 0x0ff0);
2022-08-22 16:35:53 +02:00
REQUIRE(tlv.deSerialize(nullptr, nullptr, SerializeIF::Endianness::NETWORK) != returnvalue::OK);
2022-08-10 09:39:57 +02:00
deserPtr = rawBuf.data();
deserSize = 0;
REQUIRE(tlv.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::NETWORK) ==
SerializeIF::STREAM_TOO_SHORT);
// Set invalid TLV
2022-09-15 18:41:15 +02:00
rawBuf[0] = TlvType::INVALID_TLV;
2022-08-10 09:39:57 +02:00
deserSize = 4;
REQUIRE(tlv.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::NETWORK) !=
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-10 09:39:57 +02:00
2022-09-15 18:41:15 +02:00
Tlv zeroLenField(TlvType::FAULT_HANDLER, nullptr, 0);
2022-08-10 09:39:57 +02:00
serPtr = rawBuf.data();
deserSize = 0;
REQUIRE(zeroLenField.serialize(&serPtr, &deserSize, rawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
deserPtr = rawBuf.data();
result = zeroLenField.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(zeroLenField.getSerializedSize() == 2);
REQUIRE(deserSize == 0);
}
}