fsfw/unittests/cfdp/testLvs.cpp

121 lines
4.7 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"
2022-09-14 19:15:52 +02:00
#include "fsfw/cfdp/VarLenFields.h"
2022-08-10 09:39:57 +02:00
2022-08-10 13:05:07 +02:00
TEST_CASE("CFDP LV", "[cfdp][lv]") {
2022-08-10 09:39:57 +02:00
using namespace cfdp;
2022-08-22 16:35:53 +02:00
ReturnValue_t result = returnvalue::OK;
2022-08-10 09:39:57 +02:00
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("LV Serialization") {
std::array<uint8_t, 8> lvRawBuf{};
serPtr = lvRawBuf.data();
REQUIRE(sourceId.serialize(&serPtr, &deserSize, lvRawBuf.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);
auto lv = cfdp::Lv(lvRawBuf.data(), 2);
REQUIRE(lv.getSerializedSize() == 3);
serPtr = rawBuf.data();
deserSize = 0;
REQUIRE(lv.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 == 3);
REQUIRE(rawBuf[0] == 2);
uint16_t sourceIdRaw = 0;
REQUIRE(SerializeAdapter::deSerialize(&sourceIdRaw, rawBuf.data() + 1, &deserSize,
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(sourceIdRaw == 0x0ff0);
}
2023-07-27 14:55:46 +02:00
SECTION("Move LV") {
std::array<uint8_t, 8> lvRawBuf{};
serPtr = lvRawBuf.data();
REQUIRE(sourceId.serialize(&serPtr, &deserSize, lvRawBuf.size(),
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
auto lv = cfdp::Lv(lvRawBuf.data(), 2);
auto lvMovedCopy = cfdp::Lv(std::move(lv));
REQUIRE(lvMovedCopy.getSerializedSize() == 3);
}
2022-08-10 09:39:57 +02:00
SECTION("Empty Serialization") {
auto lvEmpty = Lv();
REQUIRE(lvEmpty.getSerializedSize() == 1);
serPtr = rawBuf.data();
deserSize = 0;
result =
lvEmpty.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(deserSize == 1);
deserPtr = rawBuf.data();
result = lvEmpty.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::BIG);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(lvEmpty.getSerializedSize() == 1);
}
SECTION("Uninit LV") {
std::array<uint8_t, 8> lvRawBuf{};
serPtr = lvRawBuf.data();
REQUIRE(sourceId.serialize(&serPtr, &deserSize, lvRawBuf.size(),
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::NETWORK) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
auto lv = cfdp::Lv(lvRawBuf.data(), 2);
serPtr = rawBuf.data();
deserSize = 0;
result = lv.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
Lv uninitLv;
deserPtr = rawBuf.data();
deserSize = 3;
result = uninitLv.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::BIG);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(uninitLv.getSerializedSize() == 3);
const uint8_t* storedValue = uninitLv.getValue(nullptr);
uint16_t sourceIdRaw = 0;
REQUIRE(SerializeAdapter::deSerialize(&sourceIdRaw, storedValue, &deserSize,
2022-08-22 16:35:53 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(sourceIdRaw == 0x0ff0);
}
SECTION("Invalid Input") {
Lv uninitLv;
REQUIRE(uninitLv.deSerialize(nullptr, nullptr, SerializeIF::Endianness::BIG) ==
2022-08-22 16:35:53 +02:00
returnvalue::FAILED);
2022-08-10 09:39:57 +02:00
serPtr = rawBuf.data();
deserSize = 0;
REQUIRE(uninitLv.serialize(&serPtr, &deserSize, 0, SerializeIF::Endianness::BIG) ==
SerializeIF::BUFFER_TOO_SHORT);
REQUIRE(uninitLv.serialize(nullptr, nullptr, 12, SerializeIF::Endianness::BIG));
deserSize = 0;
REQUIRE(uninitLv.deSerialize(&deserPtr, &deserSize, SerializeIF::Endianness::BIG) ==
SerializeIF::STREAM_TOO_SHORT);
}
2022-08-10 09:45:24 +02:00
SECTION("String LV String") {
2022-08-10 09:39:57 +02:00
std::string filename = "hello.txt";
StringLv sourceFileName(filename);
REQUIRE(sourceFileName.getSerializedSize() == 1 + filename.size());
2022-08-22 16:35:53 +02:00
REQUIRE(sourceFileName.serializeBe(rawBuf.data(), deserSize, rawBuf.size()) == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(rawBuf[0] == filename.size());
std::string filenameFromRaw(reinterpret_cast<const char*>(rawBuf.data() + 1), filename.size());
REQUIRE(filenameFromRaw == filename);
}
2022-08-10 09:45:24 +02:00
SECTION("String LV Const Char") {
const char filename[] = "hello.txt";
StringLv sourceFileName(filename, sizeof(filename) - 1);
REQUIRE(sourceFileName.getSerializedSize() == 1 + sizeof(filename) - 1);
2022-08-22 16:35:53 +02:00
REQUIRE(sourceFileName.serializeBe(rawBuf.data(), deserSize, rawBuf.size()) == returnvalue::OK);
2022-08-10 09:45:24 +02:00
REQUIRE(rawBuf[0] == sizeof(filename) - 1);
rawBuf[deserSize] = '\0';
const char* filenameFromRaw = reinterpret_cast<const char*>(rawBuf.data() + 1);
REQUIRE(std::strcmp(filename, filenameFromRaw) == 0);
}
2022-08-10 09:39:57 +02:00
}