2020-12-27 14:20:26 +01:00
|
|
|
#include <fsfw/globalfunctions/arrayprinter.h>
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2020-12-27 14:14:38 +01:00
|
|
|
#include <array>
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-07-18 11:58:55 +02:00
|
|
|
#include "CatchDefinitions.h"
|
2022-07-23 10:34:19 +02:00
|
|
|
#include "SerialLinkedListAdapterPacket.h"
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
TEST_CASE("Serial Linked Packet", "[SerLinkPacket]") {
|
|
|
|
// perform set-up here
|
|
|
|
uint32_t header = 42;
|
|
|
|
std::array<uint8_t, 3> testArray{1, 2, 3};
|
|
|
|
uint32_t tail = 96;
|
|
|
|
size_t packetMaxSize = 256;
|
|
|
|
uint8_t packet[packetMaxSize] = {};
|
|
|
|
size_t packetLen = 0;
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
SECTION("Test Deserialization with Serial Buffer Adapter.") {
|
|
|
|
// This is a serialization of a packet, made "manually".
|
|
|
|
// We generate a packet which store data big-endian by swapping some
|
|
|
|
// values. (like coming from ground).
|
|
|
|
header = EndianConverter::convertBigEndian(header);
|
|
|
|
std::memcpy(packet, &header, sizeof(header));
|
|
|
|
packetLen += sizeof(header);
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
std::copy(testArray.data(), testArray.data() + testArray.size(), packet + packetLen);
|
|
|
|
packetLen += testArray.size();
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
tail = EndianConverter::convertBigEndian(tail);
|
|
|
|
std::memcpy(packet + packetLen, &tail, sizeof(tail));
|
|
|
|
packetLen += sizeof(tail);
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
// arrayprinter::print(packet, packetLen, OutputType::DEC);
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
// This is the buffer which will be filled when testClass.deSerialize
|
|
|
|
// is called.
|
|
|
|
std::array<uint8_t, 3> bufferAdaptee = {};
|
|
|
|
TestPacket testClass(packet, packetLen, bufferAdaptee.data(), bufferAdaptee.size());
|
|
|
|
const uint8_t* readOnlyPointer = packet;
|
|
|
|
// Deserialize big endian packet by setting bigEndian to true.
|
|
|
|
ReturnValue_t result =
|
|
|
|
testClass.deSerialize(&readOnlyPointer, &packetLen, SerializeIF::Endianness::BIG);
|
2022-08-16 01:08:26 +02:00
|
|
|
REQUIRE(result == returnvalue::OK);
|
2022-02-02 10:29:30 +01:00
|
|
|
CHECK(testClass.getHeader() == 42);
|
|
|
|
// Equivalent check.
|
|
|
|
// CHECK(testClass.getBuffer()[0] == 1);
|
|
|
|
CHECK(bufferAdaptee[0] == 1);
|
|
|
|
CHECK(bufferAdaptee[1] == 2);
|
|
|
|
CHECK(bufferAdaptee[2] == 3);
|
|
|
|
CHECK(testClass.getTail() == 96);
|
|
|
|
}
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
SECTION("Test Serialization") {
|
|
|
|
// Same process as performed in setup, this time using the class
|
|
|
|
// instead of doing it manually.
|
|
|
|
TestPacket testClass(header, tail, testArray.data(), testArray.size());
|
|
|
|
size_t serializedSize = 0;
|
|
|
|
uint8_t* packetPointer = packet;
|
|
|
|
// serialize for ground: bigEndian = true.
|
|
|
|
ReturnValue_t result = testClass.serialize(&packetPointer, &serializedSize, packetMaxSize,
|
|
|
|
SerializeIF::Endianness::BIG);
|
2022-08-16 01:08:26 +02:00
|
|
|
REQUIRE(result == returnvalue::OK);
|
2022-02-02 10:29:30 +01:00
|
|
|
// Result should be big endian now.
|
|
|
|
CHECK(packet[3] == 42);
|
|
|
|
CHECK(packet[4] == 1);
|
|
|
|
CHECK(packet[5] == 2);
|
|
|
|
CHECK(packet[6] == 3);
|
|
|
|
CHECK(packet[10] == 96);
|
|
|
|
}
|
2020-10-20 17:11:23 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
// perform tear-down here
|
2020-10-20 17:11:23 +02:00
|
|
|
}
|