fsfw/unittests/cfdp/pdu/testFinishedPdu.cpp

190 lines
8.6 KiB
C++
Raw Normal View History

2022-02-02 10:29:30 +01:00
#include <array>
#include <catch2/catch_test_macros.hpp>
2022-09-05 11:03:25 +02:00
#include "fsfw/cfdp/pdu/FinishedPduCreator.h"
#include "fsfw/cfdp/pdu/FinishedPduReader.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/globalfunctions/arrayprinter.h"
2022-08-03 13:23:49 +02:00
TEST_CASE("Finished 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> fnBuffer = {};
uint8_t* buffer = fnBuffer.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
cfdp::Lv emptyFsMsg;
FinishedInfo info(cfdp::ConditionCode::INACTIVITY_DETECTED,
2022-08-09 21:03:56 +02:00
cfdp::FileDeliveryCode::DATA_INCOMPLETE,
cfdp::FileDeliveryStatus::DISCARDED_DELIBERATELY);
2022-02-02 10:29:30 +01:00
SECTION("Serialize") {
2022-09-05 11:03:25 +02:00
FinishPduCreator serializer(pduConf, info);
2022-02-02 10:29:30 +01:00
result = serializer.serialize(&buffer, &sz, fnBuffer.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.getSerializedSize() == 12);
REQUIRE(((fnBuffer[1] << 8) | fnBuffer[2]) == 2);
2022-09-15 18:41:15 +02:00
REQUIRE(fnBuffer[10] == cfdp::FileDirective::FINISH);
2022-02-02 10:29:30 +01:00
REQUIRE(((fnBuffer[sz - 1] >> 4) & 0x0f) == cfdp::ConditionCode::INACTIVITY_DETECTED);
2022-08-09 21:03:56 +02:00
REQUIRE(((fnBuffer[sz - 1] >> 2) & 0x01) == cfdp::FileDeliveryCode::DATA_INCOMPLETE);
REQUIRE((fnBuffer[sz - 1] & 0b11) == cfdp::FileDeliveryStatus::DISCARDED_DELIBERATELY);
2022-02-02 10:29:30 +01:00
REQUIRE(sz == 12);
// Add a filestore response
std::string firstName = "hello.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv firstNameLv(firstName);
2022-02-02 10:29:30 +01:00
FilestoreResponseTlv response(cfdp::FilestoreActionCode::DELETE_FILE,
cfdp::FSR_APPEND_FILE_1_NOT_EXISTS, firstNameLv, nullptr);
FilestoreResponseTlv* responsePtr = &response;
REQUIRE(response.getSerializedSize() == 14);
size_t len = 1;
info.setFilestoreResponsesArray(&responsePtr, &len, &len);
serializer.updateDirectiveFieldLen();
REQUIRE(serializer.getSerializedSize() == 12 + 14);
sz = 0;
buffer = fnBuffer.data();
result = serializer.serialize(&buffer, &sz, fnBuffer.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.getSerializedSize() == 12 + 14);
REQUIRE(serializer.getPduDataFieldLen() == 16);
// Add two filestore responses and a fault location parameter
std::string secondName = "hello2.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv secondNameLv(secondName);
2022-02-02 10:29:30 +01:00
FilestoreResponseTlv response2(cfdp::FilestoreActionCode::DENY_FILE, cfdp::FSR_SUCCESS,
secondNameLv, nullptr);
REQUIRE(response2.getSerializedSize() == 15);
len = 2;
std::array<FilestoreResponseTlv*, 2> responses{&response, &response2};
info.setFilestoreResponsesArray(responses.data(), &len, &len);
serializer.updateDirectiveFieldLen();
EntityIdTlv faultLoc(destId);
REQUIRE(faultLoc.getSerializedSize() == 4);
info.setFaultLocation(&faultLoc);
serializer.updateDirectiveFieldLen();
sz = 0;
buffer = fnBuffer.data();
result = serializer.serialize(&buffer, &sz, fnBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
info.setConditionCode(cfdp::ConditionCode::FILESTORE_REJECTION);
REQUIRE(serializer.getSerializedSize() == 12 + 14 + 15 + 4);
REQUIRE(sz == 12 + 14 + 15 + 4);
2022-08-09 21:03:56 +02:00
info.setFileStatus(cfdp::FileDeliveryStatus::DISCARDED_FILESTORE_REJECTION);
REQUIRE(info.getFileStatus() == cfdp::FileDeliveryStatus::DISCARDED_FILESTORE_REJECTION);
info.setDeliveryCode(cfdp::FileDeliveryCode::DATA_INCOMPLETE);
REQUIRE(info.getDeliveryCode() == cfdp::FileDeliveryCode::DATA_INCOMPLETE);
2022-02-02 10:29:30 +01:00
for (size_t maxSz = 0; maxSz < 45; maxSz++) {
sz = 0;
buffer = fnBuffer.data();
result = serializer.serialize(&buffer, &sz, maxSz, SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result != returnvalue::OK);
}
2022-02-02 10:29:30 +01:00
}
SECTION("Deserialize") {
FinishedInfo emptyInfo;
2022-09-05 11:03:25 +02:00
FinishPduCreator serializer(pduConf, info);
2022-02-02 10:29:30 +01:00
result = serializer.serialize(&buffer, &sz, fnBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-09-05 11:03:25 +02:00
FinishPduReader deserializer(fnBuffer.data(), fnBuffer.size(), emptyInfo);
2022-02-02 10:29:30 +01:00
result = deserializer.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-08-09 21:03:56 +02:00
REQUIRE(emptyInfo.getFileStatus() == cfdp::FileDeliveryStatus::DISCARDED_DELIBERATELY);
2022-02-02 10:29:30 +01:00
REQUIRE(emptyInfo.getConditionCode() == cfdp::ConditionCode::INACTIVITY_DETECTED);
2022-08-09 21:03:56 +02:00
REQUIRE(emptyInfo.getDeliveryCode() == cfdp::FileDeliveryCode::DATA_INCOMPLETE);
2022-02-02 10:29:30 +01:00
// Add a filestore response
sz = 0;
buffer = fnBuffer.data();
std::string firstName = "hello.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv firstNameLv(firstName);
2022-02-02 10:29:30 +01:00
FilestoreResponseTlv response(cfdp::FilestoreActionCode::DELETE_FILE, cfdp::FSR_NOT_PERFORMED,
firstNameLv, nullptr);
FilestoreResponseTlv* responsePtr = &response;
size_t len = 1;
info.setFilestoreResponsesArray(&responsePtr, &len, &len);
serializer.updateDirectiveFieldLen();
REQUIRE(serializer.getPduDataFieldLen() == 16);
result = serializer.serialize(&buffer, &sz, fnBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
FilestoreResponseTlv emptyResponse(firstNameLv, nullptr);
responsePtr = &emptyResponse;
emptyInfo.setFilestoreResponsesArray(&responsePtr, nullptr, &len);
2022-09-05 11:03:25 +02:00
FinishPduReader deserializer2(fnBuffer.data(), fnBuffer.size(), emptyInfo);
2022-02-02 10:29:30 +01:00
result = deserializer2.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(emptyInfo.getFsResponsesLen() == 1);
FilestoreResponseTlv** responseArray = nullptr;
emptyInfo.getFilestoreResonses(&responseArray, nullptr, nullptr);
REQUIRE(responseArray[0]->getActionCode() == cfdp::FilestoreActionCode::DELETE_FILE);
REQUIRE(responseArray[0]->getStatusCode() == cfdp::FSR_NOT_PERFORMED);
auto& fileNameRef = responseArray[0]->getFirstFileName();
size_t stringSize = 0;
const char* string = reinterpret_cast<const char*>(fileNameRef.getValue(&stringSize));
std::string firstFileName(string, stringSize);
REQUIRE(firstFileName == "hello.txt");
// Add two filestore responses and a fault location parameter
std::string secondName = "hello2.txt";
2022-08-10 10:34:02 +02:00
cfdp::StringLv secondNameLv(secondName);
2022-02-02 10:29:30 +01:00
FilestoreResponseTlv response2(cfdp::FilestoreActionCode::DENY_FILE, cfdp::FSR_SUCCESS,
secondNameLv, nullptr);
REQUIRE(response2.getSerializedSize() == 15);
len = 2;
std::array<FilestoreResponseTlv*, 2> responses{&response, &response2};
info.setFilestoreResponsesArray(responses.data(), &len, &len);
serializer.updateDirectiveFieldLen();
EntityIdTlv faultLoc(destId);
info.setFaultLocation(&faultLoc);
serializer.updateDirectiveFieldLen();
sz = 0;
buffer = fnBuffer.data();
result = serializer.serialize(&buffer, &sz, fnBuffer.size(), SerializeIF::Endianness::NETWORK);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
EntityId emptyId;
EntityIdTlv emptyFaultLoc(emptyId);
emptyInfo.setFaultLocation(&emptyFaultLoc);
response.setFilestoreMessage(&emptyFsMsg);
emptyInfo.setFilestoreResponsesArray(responses.data(), &len, &len);
response2.setFilestoreMessage(&emptyFsMsg);
2022-09-05 11:03:25 +02:00
FinishPduReader deserializer3(fnBuffer.data(), fnBuffer.size(), emptyInfo);
2022-02-02 10:29:30 +01:00
result = deserializer3.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-02-02 10:29:30 +01:00
auto& infoRef = deserializer3.getInfo();
REQUIRE(deserializer3.getWholePduSize() == 45);
size_t invalidMaxLen = 1;
emptyInfo.setFilestoreResponsesArray(responses.data(), &len, &invalidMaxLen);
result = deserializer3.parseData();
REQUIRE(result == cfdp::FINISHED_CANT_PARSE_FS_RESPONSES);
emptyInfo.setFilestoreResponsesArray(nullptr, nullptr, nullptr);
result = deserializer3.parseData();
REQUIRE(result == cfdp::FINISHED_CANT_PARSE_FS_RESPONSES);
// Clear condition code
auto tmp = fnBuffer[11];
fnBuffer[11] = fnBuffer[11] & ~0xf0;
fnBuffer[11] = fnBuffer[11] | (cfdp::ConditionCode::NO_ERROR << 4);
emptyInfo.setFilestoreResponsesArray(responses.data(), &len, &len);
result = deserializer3.parseData();
REQUIRE(result == cfdp::INVALID_TLV_TYPE);
fnBuffer[11] = tmp;
// Invalid TLV type, should be entity ID
2022-09-15 18:41:15 +02:00
fnBuffer[sz - 4] = cfdp::TlvType::FILESTORE_REQUEST;
2022-02-02 10:29:30 +01:00
result = deserializer3.parseData();
REQUIRE(result == cfdp::INVALID_TLV_TYPE);
for (size_t maxSz = 0; maxSz < 45; maxSz++) {
2022-09-05 11:03:25 +02:00
FinishPduReader faultyDeser(fnBuffer.data(), maxSz, emptyInfo);
2022-02-02 10:29:30 +01:00
result = faultyDeser.parseData();
2022-08-16 01:08:26 +02:00
REQUIRE(result != returnvalue::OK);
}
2022-02-02 10:29:30 +01:00
}
}