fsfw/unittests/cfdp/testTlv.cpp

128 lines
5.3 KiB
C++
Raw Normal View History

2022-08-10 09:39:57 +02:00
#include <array>
#include <catch2/catch_test_macros.hpp>
#include "fsfw/cfdp.h"
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-08-10 09:39:57 +02:00
auto tlv = Tlv(TlvTypes::ENTITY_ID, tlvBuf.data(), deserSize);
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);
REQUIRE(rawBuf[0] == TlvTypes::ENTITY_ID);
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") {
auto tlv = Tlv(TlvTypes::ENTITY_ID, rawBuf.data(), deserSize);
// Set new value
sourceId.setValue(cfdp::WidthInBytes::FOUR_BYTES, 12);
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-08-10 09:39:57 +02:00
REQUIRE(rawBuf[0] == TlvTypes::ENTITY_ID);
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") {
auto tlvInvalid = Tlv(cfdp::TlvTypes::INVALID_TLV, rawBuf.data(), 0);
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
tlvInvalid = Tlv(cfdp::TlvTypes::ENTITY_ID, nullptr, 3);
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") {
Tlv zeroLenField(TlvTypes::FAULT_HANDLER, nullptr, 0);
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-08-10 09:39:57 +02:00
REQUIRE(rawBuf[0] == TlvTypes::FAULT_HANDLER);
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);
auto tlvSerialization = Tlv(TlvTypes::ENTITY_ID, tlvRawBuf.data(), deserSize);
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);
REQUIRE(tlv.getType() == TlvTypes::ENTITY_ID);
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
rawBuf[0] = TlvTypes::INVALID_TLV;
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
Tlv zeroLenField(TlvTypes::FAULT_HANDLER, nullptr, 0);
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);
}
}