fsfw/unittests/tmtcpacket/testCcsdsReader.cpp

71 lines
2.7 KiB
C++
Raw Permalink Normal View History

2022-07-21 14:40:00 +02:00
#include <array>
2022-07-18 16:21:00 +02:00
#include <catch2/catch_test_macros.hpp>
2022-07-21 14:40:00 +02:00
#include "fsfw/tmtcpacket/ccsds/SpacePacketCreator.h"
2022-07-20 11:43:16 +02:00
#include "fsfw/tmtcpacket/ccsds/SpacePacketReader.h"
2022-07-18 16:21:00 +02:00
2022-07-21 17:48:11 +02:00
#define FULL_PACKET_LEN 29
2022-07-21 14:40:00 +02:00
TEST_CASE("CCSDS Reader", "[ccsds-reader]") {
auto params = SpacePacketParams(PacketId(ccsds::PacketType::TC, true, 0x02),
PacketSeqCtrl(ccsds::SequenceFlags::FIRST_SEGMENT, 0x34), 0x16);
SpacePacketCreator base = SpacePacketCreator(params);
// This is enough to hold 0x16 (22) bytes + 6 (SP header length) + 1 as defined as the full packet
// length derived from the length field
2022-07-21 17:48:11 +02:00
std::array<uint8_t, FULL_PACKET_LEN> buf{};
2022-07-21 14:40:00 +02:00
uint8_t* bufPtr = buf.data();
size_t serLen = 0;
SpacePacketReader reader;
2022-07-21 17:48:11 +02:00
auto checkReader = [&](SpacePacketReader& reader) {
2022-07-21 14:40:00 +02:00
REQUIRE(reader.getPacketDataLen() == 0x16);
2022-07-21 17:48:11 +02:00
REQUIRE(reader.getBufSize() == SpacePacketIF::getHeaderLen());
2022-07-21 14:40:00 +02:00
REQUIRE(reader.getFullData() == buf.data());
REQUIRE(reader.getFullPacketLen() == 0x16 + SpacePacketReader::getHeaderLen() + 1);
REQUIRE(reader.getPacketIdRaw() == 0x1802);
REQUIRE(reader.getSequenceFlags() == ccsds::SequenceFlags::FIRST_SEGMENT);
REQUIRE(reader.getSequenceCount() == 0x34);
REQUIRE(reader.getPacketSeqCtrlRaw() == 0x4034);
REQUIRE(reader.hasSecHeader());
REQUIRE(reader.getApid() == 0x02);
REQUIRE(not reader.isNull());
// We only serialized the 6 bytes of the header, so the packer data should be invalid
REQUIRE(reader.getPacketData() == nullptr);
2022-07-21 17:48:11 +02:00
};
SECTION("Empty Reader") {
REQUIRE(SpacePacketIF::getHeaderLen() == 6);
REQUIRE(reader.isNull());
2022-08-16 01:08:26 +02:00
REQUIRE(reader.checkSize() == returnvalue::FAILED);
2022-07-21 17:48:11 +02:00
}
SECTION("Basic Read") {
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) ==
2022-08-16 01:08:26 +02:00
returnvalue::OK);
2022-07-21 17:48:11 +02:00
SECTION("Setter") {
reader.setReadOnlyData(buf.data(), SpacePacketIF::getHeaderLen());
checkReader(reader);
}
SECTION("Direct Construction") {
SpacePacketReader secondReader(buf.data(), serLen);
checkReader(secondReader);
}
2022-07-21 14:40:00 +02:00
}
SECTION("Read with additional data") {
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) ==
2022-08-16 01:08:26 +02:00
returnvalue::OK);
REQUIRE(reader.setReadOnlyData(buf.data(), buf.size()) == returnvalue::OK);
2022-07-21 17:48:11 +02:00
REQUIRE(reader.getBufSize() == buf.size());
REQUIRE(reader.getFullPacketLen() == FULL_PACKET_LEN);
2022-07-21 14:40:00 +02:00
REQUIRE(reader.getPacketData() == buf.data() + SpacePacketIF::getHeaderLen());
}
SECTION("Invalid Size") {
for (size_t i = 0; i < 5; i++) {
REQUIRE(reader.setReadOnlyData(buf.data(), i) == SerializeIF::STREAM_TOO_SHORT);
2022-08-01 17:16:37 +02:00
REQUIRE(reader.isNull());
2022-07-21 14:40:00 +02:00
REQUIRE(reader.getPacketData() == nullptr);
}
}
}