fsfw/unittests/cfdp/pdu/testMetadataPdu.cpp

180 lines
7.4 KiB
C++
Raw Normal View History

2022-02-02 10:29:30 +01:00
#include <array>
#include <catch2/catch_test_macros.hpp>
2022-08-08 18:36:10 +02:00
#include <iostream>
2022-02-02 10:29:30 +01:00
2022-08-08 17:53:42 +02:00
#include "fsfw/cfdp/pdu/MetadataPduCreator.h"
#include "fsfw/cfdp/pdu/MetadataPduReader.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/cfdp/tlv/FilestoreResponseTlv.h"
2022-08-09 14:55:08 +02:00
#include "fsfw/cfdp/tlv/MessageToUserTlv.h"
#include "fsfw/globalfunctions/arrayprinter.h"
2022-08-03 13:23:49 +02:00
TEST_CASE("Metadata PDU", "[cfdp][pdu]") {
2022-02-02 10:29:30 +01:00
using namespace cfdp;
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
std::array<uint8_t, 256> mdBuffer = {};
uint8_t* buffer = mdBuffer.data();
size_t sz = 0;
EntityId destId(WidthInBytes::TWO_BYTES, 2);
TransactionSeqNum seqNum(WidthInBytes::TWO_BYTES, 15);
EntityId sourceId(WidthInBytes::TWO_BYTES, 1);
2022-09-15 18:41:15 +02:00
PduConfig pduConf(sourceId, destId, TransmissionMode::ACKNOWLEDGED, seqNum);
2022-02-02 10:29:30 +01:00
std::string firstFileName = "hello.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv sourceFileName(firstFileName);
2022-08-23 19:37:30 +02:00
cfdp::StringLv destFileName;
2023-07-19 13:44:52 +02:00
Fss fileSize(35);
2022-09-15 18:41:15 +02:00
MetadataInfo info(false, ChecksumType::MODULAR, fileSize, sourceFileName, destFileName);
2022-02-02 10:29:30 +01:00
FilestoreResponseTlv response(FilestoreActionCode::CREATE_DIRECTORY, FSR_CREATE_NOT_ALLOWED,
sourceFileName, nullptr);
std::array<uint8_t, 3> msg = {0x41, 0x42, 0x43};
cfdp::Tlv responseTlv;
std::array<uint8_t, 64> responseBuf = {};
uint8_t* responseBufPtr = responseBuf.data();
response.convertToTlv(responseTlv, buffer, responseBuf.size(), SerializeIF::Endianness::MACHINE);
MessageToUserTlv msgToUser(msg.data(), msg.size());
std::array<Tlv*, 2> options{&responseTlv, &msgToUser};
REQUIRE(options[0]->getSerializedSize() == 2 + 1 + 10 + 1);
REQUIRE(options[1]->getSerializedSize() == 5);
2022-02-02 10:29:30 +01:00
SECTION("Serialize") {
2022-08-08 17:53:42 +02:00
MetadataPduCreator serializer(pduConf, info);
2022-02-02 10:29:30 +01:00
result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(serializer.getWholePduSize() == 27);
REQUIRE(info.getSourceFileName().getSerializedSize() == 10);
REQUIRE(info.getDestFileName().getSerializedSize() == 1);
REQUIRE(info.getSerializedSize() == 16);
REQUIRE((mdBuffer[1] << 8 | mdBuffer[2]) == 17);
2022-09-15 18:41:15 +02:00
REQUIRE(mdBuffer[10] == FileDirective::METADATA);
2022-02-02 10:29:30 +01:00
// no closure requested and checksum type is modular => 0x00
REQUIRE(mdBuffer[11] == 0x00);
uint32_t fileSizeRaw = 0;
result = SerializeAdapter::deSerialize(&fileSizeRaw, mdBuffer.data() + 12, nullptr,
SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(fileSizeRaw == 35);
REQUIRE(mdBuffer[16] == 9);
REQUIRE(mdBuffer[17] == 'h');
REQUIRE(mdBuffer[18] == 'e');
REQUIRE(mdBuffer[19] == 'l');
REQUIRE(mdBuffer[20] == 'l');
REQUIRE(mdBuffer[21] == 'o');
REQUIRE(mdBuffer[22] == '.');
REQUIRE(mdBuffer[23] == 't');
REQUIRE(mdBuffer[24] == 'x');
REQUIRE(mdBuffer[25] == 't');
REQUIRE(mdBuffer[26] == 0);
2022-02-02 10:29:30 +01:00
std::string otherFileName = "hello2.txt";
2022-08-23 19:37:30 +02:00
cfdp::StringLv otherFileNameLv(otherFileName.data(), otherFileName.size());
2022-02-02 10:29:30 +01:00
info.setSourceFileName(otherFileNameLv);
size_t sizeOfOptions = options.size();
2022-08-23 19:37:30 +02:00
info.setOptionsArray(options.data(), sizeOfOptions, sizeOfOptions);
2022-02-02 10:29:30 +01:00
REQUIRE(info.getMaxOptionsLen() == 2);
info.setMaxOptionsLen(3);
REQUIRE(info.getMaxOptionsLen() == 3);
2022-09-15 18:41:15 +02:00
info.setChecksumType(cfdp::ChecksumType::CRC_32C);
2022-02-02 10:29:30 +01:00
info.setClosureRequested(true);
uint8_t* buffer = mdBuffer.data();
size_t sz = 0;
serializer.updateDirectiveFieldLen();
2022-02-02 10:29:30 +01:00
result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE((mdBuffer[1] << 8 | mdBuffer[2]) == 37);
2022-09-15 18:41:15 +02:00
auto checksumType = static_cast<cfdp::ChecksumType>(mdBuffer[11] & 0x0f);
REQUIRE(checksumType == cfdp::ChecksumType::CRC_32C);
2022-02-02 10:29:30 +01:00
bool closureRequested = mdBuffer[11] >> 6 & 0x01;
REQUIRE(closureRequested == true);
// The size of the two options is 19. Summing up:
// - 11 bytes of source file name
// - 1 byte for dest file name
// - 4 for FSS
// - 1 leading byte.
// - 1 byte for PDU type
// PDU header has 10 bytes.
// I am not going to check the options raw content, those are part of the dedicated
// TLV unittests
REQUIRE(sz == 10 + 37);
for (size_t maxSz = 0; maxSz < sz; maxSz++) {
uint8_t* buffer = mdBuffer.data();
size_t sz = 0;
result = serializer.serialize(&buffer, &sz, maxSz, SerializeIF::Endianness::NETWORK);
REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT);
}
2022-02-02 10:29:30 +01:00
for (size_t initSz = 1; initSz < 47; initSz++) {
uint8_t* buffer = mdBuffer.data();
size_t sz = initSz;
result = serializer.serialize(&buffer, &sz, 46, SerializeIF::Endianness::NETWORK);
REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT);
}
info.setDestFileName(destFileName);
}
2022-02-02 10:29:30 +01:00
SECTION("Deserialize") {
2022-08-08 17:53:42 +02:00
MetadataPduCreator serializer(pduConf, info);
2022-02-02 10:29:30 +01:00
result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-08 17:53:42 +02:00
MetadataPduReader deserializer(mdBuffer.data(), mdBuffer.size(), info);
2022-02-02 10:29:30 +01:00
result = deserializer.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
size_t fullSize = deserializer.getWholePduSize();
for (size_t maxSz = 0; maxSz < fullSize; maxSz++) {
2022-08-08 17:53:42 +02:00
MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info);
2022-02-02 10:29:30 +01:00
result = invalidSzDeser.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result != returnvalue::OK);
2022-02-02 10:29:30 +01:00
}
size_t sizeOfOptions = options.size();
size_t maxSize = 4;
2022-08-23 19:37:30 +02:00
info.setOptionsArray(options.data(), sizeOfOptions, maxSize);
2022-02-02 10:29:30 +01:00
REQUIRE(info.getOptionsLen() == 2);
2022-09-15 18:41:15 +02:00
info.setChecksumType(cfdp::ChecksumType::CRC_32C);
2022-02-02 10:29:30 +01:00
info.setClosureRequested(true);
uint8_t* buffer = mdBuffer.data();
size_t sz = 0;
serializer.updateDirectiveFieldLen();
2022-02-02 10:29:30 +01:00
info.setSourceFileName(sourceFileName);
result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-08 17:53:42 +02:00
MetadataPduReader deserializer2(mdBuffer.data(), mdBuffer.size(), info);
2022-02-02 10:29:30 +01:00
result = deserializer2.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(options[0]->getType() == cfdp::TlvType::FILESTORE_RESPONSE);
2022-02-02 10:29:30 +01:00
REQUIRE(options[0]->getSerializedSize() == 14);
2022-09-15 18:41:15 +02:00
REQUIRE(options[1]->getType() == cfdp::TlvType::MSG_TO_USER);
2022-02-02 10:29:30 +01:00
REQUIRE(options[1]->getSerializedSize() == 5);
2022-02-02 10:29:30 +01:00
for (size_t invalidFieldLen = 0; invalidFieldLen < 36; invalidFieldLen++) {
mdBuffer[1] = (invalidFieldLen >> 8) & 0xff;
mdBuffer[2] = invalidFieldLen & 0xff;
result = deserializer2.parseData();
if (invalidFieldLen == 17) {
REQUIRE(info.getOptionsLen() == 0);
}
if (invalidFieldLen == 31) {
REQUIRE(info.getOptionsLen() == 1);
}
// This is the precise length where there are no options or one option
if (invalidFieldLen != 17 and invalidFieldLen != 31) {
2022-08-16 01:08:26 +02:00
REQUIRE(result != returnvalue::OK);
2022-02-02 10:29:30 +01:00
}
}
mdBuffer[1] = (36 >> 8) & 0xff;
mdBuffer[2] = 36 & 0xff;
2022-08-23 19:37:30 +02:00
info.setOptionsArray(nullptr, std::nullopt, std::nullopt);
2022-02-02 10:29:30 +01:00
REQUIRE(deserializer2.parseData() == cfdp::METADATA_CANT_PARSE_OPTIONS);
2022-08-23 19:37:30 +02:00
info.setOptionsArray(options.data(), sizeOfOptions, std::nullopt);
2022-02-02 10:29:30 +01:00
for (size_t maxSz = 0; maxSz < 46; maxSz++) {
2022-08-08 17:53:42 +02:00
MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info);
2022-08-08 18:36:10 +02:00
if (not invalidSzDeser.isNull()) {
result = invalidSzDeser.parseData();
REQUIRE(result == SerializeIF::STREAM_TOO_SHORT);
}
}
2022-02-02 10:29:30 +01:00
}
}