completed baseline PUS TM unittests

This commit is contained in:
Robin Müller 2022-07-22 18:31:00 +02:00
parent b350018cad
commit cb05329dd9
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 32 additions and 0 deletions

View File

@ -12,6 +12,10 @@ class CdsShortTimestamperMock : public TimeStamperIF, public TimeReaderIF {
ReturnValue_t lastDeserializeResult = HasReturnvaluesIF::RETURN_OK; ReturnValue_t lastDeserializeResult = HasReturnvaluesIF::RETURN_OK;
ReturnValue_t lastSerializeResult = HasReturnvaluesIF::RETURN_OK; ReturnValue_t lastSerializeResult = HasReturnvaluesIF::RETURN_OK;
unsigned int getSizeCallCount = 0; unsigned int getSizeCallCount = 0;
bool nextSerFails = false;
ReturnValue_t serFailRetval = HasReturnvaluesIF::RETURN_FAILED;
bool nextDeserFails = false;
ReturnValue_t deserFailRetval = HasReturnvaluesIF::RETURN_FAILED;
std::array<uint8_t, 7> valueToStamp{}; std::array<uint8_t, 7> valueToStamp{};
CdsShortTimestamperMock() = default; CdsShortTimestamperMock() = default;
@ -23,6 +27,9 @@ class CdsShortTimestamperMock : public TimeStamperIF, public TimeReaderIF {
Endianness streamEndianness) const override { Endianness streamEndianness) const override {
auto &thisNonConst = const_cast<CdsShortTimestamperMock &>(*this); auto &thisNonConst = const_cast<CdsShortTimestamperMock &>(*this);
thisNonConst.serializeCallCount += 1; thisNonConst.serializeCallCount += 1;
if (nextSerFails) {
return serFailRetval;
}
if (*size + getSerializedSize() > maxSize) { if (*size + getSerializedSize() > maxSize) {
thisNonConst.lastSerializeResult = SerializeIF::BUFFER_TOO_SHORT; thisNonConst.lastSerializeResult = SerializeIF::BUFFER_TOO_SHORT;
return lastSerializeResult; return lastSerializeResult;
@ -41,6 +48,9 @@ class CdsShortTimestamperMock : public TimeStamperIF, public TimeReaderIF {
ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
Endianness streamEndianness) override { Endianness streamEndianness) override {
deserializeCallCount += 1; deserializeCallCount += 1;
if (nextDeserFails) {
return deserFailRetval;
}
if (*size < 7) { if (*size < 7) {
lastDeserializeResult = SerializeIF::STREAM_TOO_SHORT; lastDeserializeResult = SerializeIF::STREAM_TOO_SHORT;
return lastDeserializeResult; return lastDeserializeResult;
@ -55,7 +65,15 @@ class CdsShortTimestamperMock : public TimeStamperIF, public TimeReaderIF {
void reset() { void reset() {
serializeCallCount = 0; serializeCallCount = 0;
getSizeCallCount = 0; getSizeCallCount = 0;
deserializeCallCount = 0;
nextSerFails = false;
nextDeserFails = false;
lastSerializeResult = HasReturnvaluesIF::RETURN_OK;
lastDeserializeResult = HasReturnvaluesIF::RETURN_OK;
deserFailRetval = HasReturnvaluesIF::RETURN_FAILED;
serFailRetval = HasReturnvaluesIF::RETURN_FAILED;
} }
ReturnValue_t readTimeStamp(const uint8_t *buffer, size_t maxSize) override { ReturnValue_t readTimeStamp(const uint8_t *buffer, size_t maxSize) override {
return deSerialize(&buffer, &maxSize, SerializeIF::Endianness::NETWORK); return deSerialize(&buffer, &maxSize, SerializeIF::Endianness::NETWORK);
} }

View File

@ -91,4 +91,18 @@ TEST_CASE("PUS TM Reader", "[pus-tm-reader]") {
REQUIRE(roData[1] == 2); REQUIRE(roData[1] == 2);
REQUIRE(roData[2] == 3); REQUIRE(roData[2] == 3);
} }
SECTION("Invalid stream lengths") {
REQUIRE(creator.serialize(&dataPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
for (size_t i = 0; i < serLen - 1; i++) {
REQUIRE(reader.setReadOnlyData(buf.data(), i) == SerializeIF::STREAM_TOO_SHORT);
}
}
SECTION("Reading timestamp fails") {
timeStamperAndReader.nextDeserFails = true;
REQUIRE(creator.serialize(&dataPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(reader.setReadOnlyData(buf.data(), serLen) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(reader.parseDataWithCrcCheck() == HasReturnvaluesIF::RETURN_FAILED);
}
} }