fsfw/unittests/cfdp/testOtherTlvs.cpp

138 lines
5.6 KiB
C++
Raw Permalink Normal View History

2022-08-10 09:39:57 +02:00
#include <fsfw/cfdp/tlv/EntityIdTlv.h>
#include <fsfw/cfdp/tlv/FaultHandlerOverrideTlv.h>
#include <fsfw/cfdp/tlv/FilestoreRequestTlv.h>
#include <fsfw/cfdp/tlv/FilestoreResponseTlv.h>
#include <fsfw/cfdp/tlv/FlowLabelTlv.h>
#include <fsfw/cfdp/tlv/MessageToUserTlv.h>
#include <array>
#include <catch2/catch_test_macros.hpp>
#include <string>
#include "fsfw/cfdp/pdu/PduConfig.h"
#include "fsfw/cfdp/tlv/Lv.h"
#include "fsfw/cfdp/tlv/Tlv.h"
#include "fsfw/globalfunctions/arrayprinter.h"
2022-08-10 13:05:07 +02:00
TEST_CASE("CFDP Other TLVs", "[cfdp][tlv]") {
2022-08-10 09:39:57 +02:00
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("Filestore Response TLV") {
std::string name = "hello.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv firstName(name);
2022-08-10 09:39:57 +02:00
std::string name2 = "hello2.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv secondName(name2);
2022-08-10 09:39:57 +02:00
std::string msg = "12345";
cfdp::Lv fsMsg(reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
FilestoreResponseTlv response(cfdp::FilestoreActionCode::APPEND_FILE, cfdp::FSR_SUCCESS,
firstName, &fsMsg);
response.setSecondFileName(&secondName);
REQUIRE(response.getLengthField() == 10 + 11 + 6 + 1);
REQUIRE(response.getSerializedSize() == response.getLengthField() + 2);
cfdp::Tlv rawResponse;
std::array<uint8_t, 128> serBuf = {};
result = response.convertToTlv(rawResponse, serBuf.data(), serBuf.size(),
SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(rawResponse.getType() == cfdp::TlvType::FILESTORE_RESPONSE);
2022-08-10 10:34:02 +02:00
cfdp::StringLv emptyMsg;
cfdp::StringLv emptySecondName;
2022-08-10 09:39:57 +02:00
FilestoreResponseTlv emptyTlv(firstName, &emptyMsg);
emptyTlv.setSecondFileName(&emptySecondName);
result = emptyTlv.deSerialize(rawResponse, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
REQUIRE(emptyTlv.getActionCode() == cfdp::FilestoreActionCode::APPEND_FILE);
REQUIRE(emptyTlv.getStatusCode() == cfdp::FSR_SUCCESS);
size_t firstNameLen = 0;
const char* firstNamePtr =
reinterpret_cast<const char*>(emptyTlv.getFirstFileName().getValue(&firstNameLen));
auto helloString = std::string(firstNamePtr, firstNameLen);
REQUIRE(helloString == "hello.txt");
}
SECTION("Filestore Request TLV") {
std::string name = "hello.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv firstName(name);
2022-08-10 09:39:57 +02:00
std::string name2 = "hello2.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv secondName(name2);
2022-08-10 09:39:57 +02:00
FilestoreRequestTlv request(cfdp::FilestoreActionCode::APPEND_FILE, firstName);
// second name not set yet
REQUIRE(request.getLengthField() == 10 + 1);
REQUIRE(request.getSerializedSize() == request.getLengthField() + 2);
std::array<uint8_t, 128> serBuf = {};
uint8_t* ptr = serBuf.data();
size_t sz = 0;
result = request.serialize(&ptr, &sz, serBuf.size(), SerializeIF::Endianness::NETWORK);
REQUIRE(result == cfdp::FILESTORE_REQUIRES_SECOND_FILE);
ptr = serBuf.data();
sz = 0;
request.setSecondFileName(&secondName);
size_t expectedSz = request.getLengthField();
REQUIRE(expectedSz == 10 + 11 + 1);
REQUIRE(request.getSerializedSize() == expectedSz + 2);
result = request.serialize(&ptr, &sz, serBuf.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(sz == expectedSz + 2);
FilestoreRequestTlv emptyRequest(firstName);
emptyRequest.setSecondFileName(&secondName);
const uint8_t* constptr = serBuf.data();
result = emptyRequest.deSerialize(&constptr, &sz, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
cfdp::Tlv rawRequest;
ptr = serBuf.data();
sz = 0;
result = request.convertToTlv(rawRequest, serBuf.data(), serBuf.size(),
SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(rawRequest.getType() == cfdp::TlvType::FILESTORE_REQUEST);
2022-08-10 09:39:57 +02:00
emptyRequest.setActionCode(cfdp::FilestoreActionCode::DELETE_FILE);
result = emptyRequest.deSerialize(rawRequest, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-15 18:41:15 +02:00
REQUIRE(emptyRequest.getType() == cfdp::TlvType::FILESTORE_REQUEST);
2022-08-10 09:39:57 +02:00
REQUIRE(emptyRequest.getActionCode() == cfdp::FilestoreActionCode::APPEND_FILE);
}
SECTION("Other") {
MessageToUserTlv emptyTlv;
uint8_t flowLabel = 1;
FlowLabelTlv flowLabelTlv(&flowLabel, 1);
FaultHandlerOverrideTlv faultOverrideTlv(cfdp::ConditionCode::FILESTORE_REJECTION,
2022-09-15 18:41:15 +02:00
cfdp::FaultHandlerCode::NOTICE_OF_CANCELLATION);
2022-08-10 09:39:57 +02:00
size_t sz = 0;
result =
faultOverrideTlv.serialize(&serPtr, &sz, rawBuf.size(), SerializeIF::Endianness::NETWORK);
REQUIRE(faultOverrideTlv.getSerializedSize() == 3);
REQUIRE(sz == 3);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
FaultHandlerOverrideTlv emptyOverrideTlv;
result = emptyOverrideTlv.deSerialize(&deserPtr, &sz, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
EntityId entId(cfdp::WidthInBytes::TWO_BYTES, 0x42);
EntityId emptyId;
EntityIdTlv idTlv(emptyId);
serPtr = rawBuf.data();
result = idTlv.serialize(&serPtr, &deserSize, rawBuf.size(), SerializeIF::Endianness::NETWORK);
2022-09-15 18:41:15 +02:00
cfdp::Tlv rawTlv(cfdp::TlvType::ENTITY_ID, rawBuf.data() + 2, 2);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
deserPtr = rawBuf.data();
result = idTlv.deSerialize(rawTlv, SerializeIF::Endianness::NETWORK);
2022-08-22 16:35:53 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-10 09:39:57 +02:00
}
}