fsfw/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp

175 lines
8.3 KiB
C++
Raw Normal View History

2021-09-08 23:33:10 +02:00
#include "fsfw/globalfunctions/DleEncoder.h"
#include "fsfw_tests/unit/CatchDefinitions.h"
#include "catch2/catch_test_macros.hpp"
#include <array>
2021-09-09 01:06:54 +02:00
const std::vector<uint8_t> TEST_ARRAY_0 = { 0, 0, 0, 0, 0 };
const std::vector<uint8_t> TEST_ARRAY_1 = { 0, DleEncoder::DLE_CHAR, 5};
const std::vector<uint8_t> TEST_ARRAY_2 = { 0, DleEncoder::STX_CHAR, 5};
const std::vector<uint8_t> TEST_ARRAY_3 = { 0, DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR};
const std::vector<uint8_t> TEST_ARRAY_4 = { DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR,
DleEncoder::STX_CHAR };
const std::vector<uint8_t> TEST_ARRAY_0_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, 0, 0, 0, 0, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_0_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0, 0, 0, 0, 0,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_1_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR, 5, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_1_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
5, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
2021-09-09 01:06:54 +02:00
const std::vector<uint8_t> TEST_ARRAY_2_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR + 0x40,
5, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_2_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0,
DleEncoder::STX_CHAR, 5, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
2021-09-09 01:06:54 +02:00
const std::vector<uint8_t> TEST_ARRAY_3_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::CARRIAGE_RETURN,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR
2021-09-09 01:06:54 +02:00
};
const std::vector<uint8_t> TEST_ARRAY_3_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0,
DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR, DleEncoder::DLE_CHAR,
DleEncoder::ETX_CHAR
};
2021-09-09 01:06:54 +02:00
const std::vector<uint8_t> TEST_ARRAY_4_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::DLE_CHAR,
DleEncoder::STX_CHAR + 0x40, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_4_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
DleEncoder::ETX_CHAR, DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
2021-09-08 23:33:10 +02:00
2021-09-09 01:06:54 +02:00
TEST_CASE("DleEncoder" , "[DleEncoder]") {
2021-09-08 23:33:10 +02:00
DleEncoder dleEncoder;
2021-09-09 10:47:54 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
2021-09-08 23:33:10 +02:00
std::array<uint8_t, 32> buffer;
2021-09-09 01:06:54 +02:00
size_t encodedLen = 0;
size_t readLen = 0;
size_t decodedLen = 0;
2021-09-08 23:33:10 +02:00
2021-09-09 01:06:54 +02:00
auto testLambdaEncode = [&](DleEncoder& encoder, const std::vector<uint8_t>& vecToEncode,
const std::vector<uint8_t>& expectedVec) {
2021-09-09 10:47:54 +02:00
result = encoder.encode(vecToEncode.data(), vecToEncode.size(),
2021-09-08 23:33:10 +02:00
buffer.data(), buffer.size(), &encodedLen);
REQUIRE(result == retval::CATCH_OK);
2021-09-09 01:06:54 +02:00
for(size_t idx = 0; idx < expectedVec.size(); idx++) {
REQUIRE(buffer[idx] == expectedVec[idx]);
2021-09-08 23:33:10 +02:00
}
2021-09-09 01:06:54 +02:00
REQUIRE(encodedLen == expectedVec.size());
};
2021-09-08 23:33:10 +02:00
2021-09-09 01:06:54 +02:00
auto testLambdaDecode = [&](DleEncoder& encoder, const std::vector<uint8_t>& testVecEncoded,
const std::vector<uint8_t>& expectedVec) {
2021-09-09 10:47:54 +02:00
result = encoder.decode(testVecEncoded.data(),
2021-09-09 01:06:54 +02:00
testVecEncoded.size(),
&readLen, buffer.data(), buffer.size(), &decodedLen);
2021-09-08 23:33:10 +02:00
REQUIRE(result == retval::CATCH_OK);
2021-09-09 01:06:54 +02:00
REQUIRE(readLen == testVecEncoded.size());
REQUIRE(decodedLen == expectedVec.size());
for(size_t idx = 0; idx < decodedLen; idx++) {
REQUIRE(buffer[idx] == expectedVec[idx]);
2021-09-08 23:33:10 +02:00
}
2021-09-09 01:06:54 +02:00
};
2021-09-08 23:33:10 +02:00
2021-09-09 01:06:54 +02:00
SECTION("Encoding") {
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_ESCAPED);
2021-09-09 10:47:54 +02:00
auto testFaultyEncoding = [&](const std::vector<uint8_t>& vecToEncode,
const std::vector<uint8_t>& expectedVec) {
for(size_t faultyDestSize = 0; faultyDestSize < expectedVec.size(); faultyDestSize ++) {
if(faultyDestSize == 8) {
}
result = dleEncoder.encode(vecToEncode.data(), vecToEncode.size(),
buffer.data(), faultyDestSize, &encodedLen);
REQUIRE(result == DleEncoder::STREAM_TOO_SHORT);
}
};
testFaultyEncoding(TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_ESCAPED);
testFaultyEncoding(TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_ESCAPED);
testFaultyEncoding(TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_ESCAPED);
testFaultyEncoding(TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_ESCAPED);
testFaultyEncoding(TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_ESCAPED);
dleEncoder.setEscapeMode(false);
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_NON_ESCAPED);
2021-09-09 10:47:54 +02:00
testFaultyEncoding(TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
testFaultyEncoding(TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_NON_ESCAPED);
testFaultyEncoding(TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_NON_ESCAPED);
testFaultyEncoding(TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_NON_ESCAPED);
testFaultyEncoding(TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_NON_ESCAPED);
dleEncoder.setEscapeMode(true);
2021-09-08 23:33:10 +02:00
}
SECTION("Decoding") {
2021-09-09 01:06:54 +02:00
testLambdaDecode(dleEncoder, TEST_ARRAY_0_ENCODED_ESCAPED, TEST_ARRAY_0);
testLambdaDecode(dleEncoder, TEST_ARRAY_1_ENCODED_ESCAPED, TEST_ARRAY_1);
testLambdaDecode(dleEncoder, TEST_ARRAY_2_ENCODED_ESCAPED, TEST_ARRAY_2);
testLambdaDecode(dleEncoder, TEST_ARRAY_3_ENCODED_ESCAPED, TEST_ARRAY_3);
testLambdaDecode(dleEncoder, TEST_ARRAY_4_ENCODED_ESCAPED, TEST_ARRAY_4);
2021-09-09 10:47:54 +02:00
auto testFaultyDecoding = [&](const std::vector<uint8_t>& vecToDecode,
const std::vector<uint8_t>& expectedVec) {
for(size_t faultyDestSizes = 0;
faultyDestSizes < expectedVec.size();
faultyDestSizes ++) {
result = dleEncoder.decode(vecToDecode.data(),
vecToDecode.size(), &readLen,
buffer.data(), faultyDestSizes, &decodedLen);
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
}
};
testFaultyDecoding(TEST_ARRAY_0_ENCODED_ESCAPED, TEST_ARRAY_0);
testFaultyDecoding(TEST_ARRAY_1_ENCODED_ESCAPED, TEST_ARRAY_1);
testFaultyDecoding(TEST_ARRAY_2_ENCODED_ESCAPED, TEST_ARRAY_2);
testFaultyDecoding(TEST_ARRAY_3_ENCODED_ESCAPED, TEST_ARRAY_3);
testFaultyDecoding(TEST_ARRAY_4_ENCODED_ESCAPED, TEST_ARRAY_4);
dleEncoder.setEscapeMode(false);
testLambdaDecode(dleEncoder, TEST_ARRAY_0_ENCODED_NON_ESCAPED, TEST_ARRAY_0);
testLambdaDecode(dleEncoder, TEST_ARRAY_1_ENCODED_NON_ESCAPED, TEST_ARRAY_1);
testLambdaDecode(dleEncoder, TEST_ARRAY_2_ENCODED_NON_ESCAPED, TEST_ARRAY_2);
testLambdaDecode(dleEncoder, TEST_ARRAY_3_ENCODED_NON_ESCAPED, TEST_ARRAY_3);
testLambdaDecode(dleEncoder, TEST_ARRAY_4_ENCODED_NON_ESCAPED, TEST_ARRAY_4);
testFaultyDecoding(TEST_ARRAY_0_ENCODED_NON_ESCAPED, TEST_ARRAY_0);
testFaultyDecoding(TEST_ARRAY_1_ENCODED_NON_ESCAPED, TEST_ARRAY_1);
testFaultyDecoding(TEST_ARRAY_2_ENCODED_NON_ESCAPED, TEST_ARRAY_2);
testFaultyDecoding(TEST_ARRAY_3_ENCODED_NON_ESCAPED, TEST_ARRAY_3);
testFaultyDecoding(TEST_ARRAY_4_ENCODED_NON_ESCAPED, TEST_ARRAY_4);
dleEncoder.setEscapeMode(true);
2021-09-08 23:33:10 +02:00
}
}