continuing tests

This commit is contained in:
Robin Müller 2021-09-09 01:06:54 +02:00
parent b5063117f6
commit 35b53e9a17
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
3 changed files with 84 additions and 47 deletions

View File

@ -22,7 +22,7 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
if(not escapeStxEtx) {
destStream[0] = DLE_CHAR;
}
destStream[0] = STX_CHAR;
destStream[1] = STX_CHAR;
}
if(escapeStxEtx) {
@ -220,6 +220,9 @@ ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream,
uint8_t nextByte;
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)) {
if (sourceStream[encodedIndex] == DLE_CHAR) {
if(encodedIndex + 1 >= sourceStreamLen) {
return DECODING_ERROR;
}
nextByte = sourceStream[encodedIndex + 1];
if(nextByte == STX_CHAR) {
*readLen = ++encodedIndex;
@ -247,3 +250,6 @@ ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream,
return DECODING_ERROR;
}
void DleEncoder::setEscapeMode(bool escapeStxEtx) {
this->escapeStxEtx = escapeStxEtx;
}

View File

@ -37,6 +37,9 @@ public:
* @param escapeCr In escaped mode, escape all CR occurrences as well
*/
DleEncoder(bool escapeStxEtx = true, bool escapeCr = false);
void setEscapeMode(bool escapeStxEtx);
virtual ~DleEncoder();
static constexpr uint8_t INTERFACE_ID = CLASS_ID::DLE_ENCODER;

View File

@ -4,58 +4,82 @@
#include <array>
const std::array<uint8_t, 5> TEST_ARRAY_0 = { 0 };
const std::array<uint8_t, 3> TEST_ARRAY_1 = { 0, DleEncoder::DLE_CHAR, 5};
const std::array<uint8_t, 3> TEST_ARRAY_2 = { 0, DleEncoder::STX_CHAR, 5};
const std::array<uint8_t, 3> TEST_ARRAY_3 = { 0, DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR};
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_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_3_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::CARRIAGE_RETURN,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR
};
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
};
TEST_CASE("DleEncoder" , "[DleEncoder]") {
DleEncoder dleEncoder;
std::array<uint8_t, 32> buffer;
size_t encodedLen = 0;
size_t readLen = 0;
size_t decodedLen = 0;
auto testLambdaEncode = [&](DleEncoder& encoder, const std::vector<uint8_t>& vecToEncode,
const std::vector<uint8_t>& expectedVec) {
ReturnValue_t result = encoder.encode(vecToEncode.data(), vecToEncode.size(),
buffer.data(), buffer.size(), &encodedLen);
REQUIRE(result == retval::CATCH_OK);
for(size_t idx = 0; idx < expectedVec.size(); idx++) {
REQUIRE(buffer[idx] == expectedVec[idx]);
}
REQUIRE(encodedLen == expectedVec.size());
};
auto testLambdaDecode = [&](DleEncoder& encoder, const std::vector<uint8_t>& testVecEncoded,
const std::vector<uint8_t>& expectedVec) {
ReturnValue_t result = encoder.decode(testVecEncoded.data(),
testVecEncoded.size(),
&readLen, buffer.data(), buffer.size(), &decodedLen);
REQUIRE(result == retval::CATCH_OK);
REQUIRE(readLen == testVecEncoded.size());
REQUIRE(decodedLen == expectedVec.size());
for(size_t idx = 0; idx < decodedLen; idx++) {
REQUIRE(buffer[idx] == expectedVec[idx]);
}
};
SECTION("Encoding") {
size_t encodedLen = 0;
ReturnValue_t result = dleEncoder.encode(TEST_ARRAY_0.data(), TEST_ARRAY_0.size(),
buffer.data(), buffer.size(), &encodedLen);
REQUIRE(result == retval::CATCH_OK);
std::vector<uint8_t> expected = {DleEncoder::STX_CHAR, 0, 0, 0, 0, 0,
DleEncoder::ETX_CHAR};
for(size_t idx = 0; idx < expected.size(); idx++) {
REQUIRE(buffer[idx] == expected[idx]);
}
REQUIRE(encodedLen == 7);
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);
result = dleEncoder.encode(TEST_ARRAY_1.data(), TEST_ARRAY_1.size(),
buffer.data(), buffer.size(), &encodedLen);
REQUIRE(result == retval::CATCH_OK);
expected = std::vector<uint8_t>{DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR,
DleEncoder::DLE_CHAR, 5, DleEncoder::ETX_CHAR};
for(size_t idx = 0; idx < expected.size(); idx++) {
REQUIRE(buffer[idx] == expected[idx]);
}
REQUIRE(encodedLen == expected.size());
dleEncoder.setEscapeMode(false);
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
result = dleEncoder.encode(TEST_ARRAY_2.data(), TEST_ARRAY_2.size(),
buffer.data(), buffer.size(), &encodedLen);
REQUIRE(result == retval::CATCH_OK);
expected = std::vector<uint8_t>{DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR,
DleEncoder::STX_CHAR + 0x40, 5, DleEncoder::ETX_CHAR};
for(size_t idx = 0; idx < expected.size(); idx++) {
REQUIRE(buffer[idx] == expected[idx]);
}
REQUIRE(encodedLen == expected.size());
result = dleEncoder.encode(TEST_ARRAY_3.data(), TEST_ARRAY_3.size(),
buffer.data(), buffer.size(), &encodedLen);
REQUIRE(result == retval::CATCH_OK);
expected = std::vector<uint8_t>{DleEncoder::STX_CHAR, 0, DleEncoder::CARRIAGE_RETURN,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR};
for(size_t idx = 0; idx < expected.size(); idx++) {
REQUIRE(buffer[idx] == expected[idx]);
}
REQUIRE(encodedLen == expected.size());
result = dleEncoder.encode(TEST_ARRAY_3.data(), TEST_ARRAY_3.size(),
ReturnValue_t result = dleEncoder.encode(TEST_ARRAY_3.data(), TEST_ARRAY_3.size(),
buffer.data(), 0, &encodedLen);
REQUIRE(result == DleEncoder::STREAM_TOO_SHORT);
result = dleEncoder.encode(TEST_ARRAY_1.data(), TEST_ARRAY_1.size(),
@ -64,6 +88,10 @@ TEST_CASE("DleEncoder" , "[DleEncoder]") {
}
SECTION("Decoding") {
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);
}
}