From c8c72c77449c24711d441dfe40ecc46843c828f2 Mon Sep 17 00:00:00 2001 From: kongr45gpen <electrovesta@gmail.com> Date: Tue, 13 Aug 2019 14:32:28 +0300 Subject: [PATCH] Use the new ServiceTests functions for tests that expect errors Closes #19 --- src/Helpers/TimeAndDate.cpp | 4 ++-- src/Message.cpp | 4 ++-- src/MessageParser.cpp | 19 ++++++++++--------- test/ErrorHandler.cpp | 5 +++++ test/Helpers/TimeAndDate.cpp | 4 ++++ test/Helpers/TimeHelper.cpp | 14 +++++++++----- test/Services/FunctionManagementService.cpp | 2 +- test/Services/ParameterService.cpp | 6 ++++++ test/TestPlatform.cpp | 3 +-- 9 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/Helpers/TimeAndDate.cpp b/src/Helpers/TimeAndDate.cpp index b531d227..29903d17 100644 --- a/src/Helpers/TimeAndDate.cpp +++ b/src/Helpers/TimeAndDate.cpp @@ -15,8 +15,8 @@ TimeAndDate::TimeAndDate(uint16_t year, uint8_t month, uint8_t day, uint8_t hour ASSERT_INTERNAL(2019 <= year, ErrorHandler::InternalErrorType::InvalidDate); ASSERT_INTERNAL((1 <= month) && (month <= 12), ErrorHandler::InternalErrorType::InvalidDate); ASSERT_INTERNAL((1 <= day) && (day <= 31), ErrorHandler::InternalErrorType::InvalidDate); - ASSERT_INTERNAL(hour <= 24, ErrorHandler::InternalErrorType::InvalidDate); - ASSERT_INTERNAL(minute <= 60, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(hour < 24, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(minute < 60, ErrorHandler::InternalErrorType::InvalidDate); ASSERT_INTERNAL(second <= 60, ErrorHandler::InternalErrorType::InvalidDate); this->year = year; diff --git a/src/Message.cpp b/src/Message.cpp index b1dc6999..8661d6eb 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -168,9 +168,9 @@ void Message::appendMessage(const Message& message, uint16_t size) { } void Message::appendString(const etl::istring& string) { - ASSERT_INTERNAL(dataSize + string.size() < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + ASSERT_INTERNAL(dataSize + string.size() <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); // TODO: Do we need to keep this check? How does etl::string handle it? - ASSERT_INTERNAL(string.size() < string.capacity(), ErrorHandler::StringTooLarge); + ASSERT_INTERNAL(string.size() <= string.capacity(), ErrorHandler::StringTooLarge); memcpy(data + dataSize, string.data(), string.size()); diff --git a/src/MessageParser.cpp b/src/MessageParser.cpp index de6ba757..6b639752 100644 --- a/src/MessageParser.cpp +++ b/src/MessageParser.cpp @@ -47,14 +47,14 @@ Message MessageParser::parse(uint8_t* data, uint32_t length) { // Individual fields of the CCSDS Space Packet primary header uint8_t versionNumber = data[0] >> 5; Message::PacketType packetType = ((data[0] & 0x10) == 0) ? Message::TM : Message::TC; - uint8_t secondaryHeaderFlag = data[0] & static_cast<uint8_t>(0x08); + bool secondaryHeaderFlag = (data[0] & 0x08u) != 0U; uint16_t APID = packetHeaderIdentification & static_cast<uint16_t>(0x07ff); auto sequenceFlags = static_cast<uint8_t>(packetSequenceControl >> 14); uint16_t packetSequenceCount = packetSequenceControl & (~ 0xc000u); // keep last 14 bits // Returning an internal error, since the Message is not available yet ASSERT_INTERNAL(versionNumber == 0U, ErrorHandler::UnacceptablePacket); - ASSERT_INTERNAL(secondaryHeaderFlag == 1U, ErrorHandler::UnacceptablePacket); + ASSERT_INTERNAL(secondaryHeaderFlag, ErrorHandler::UnacceptablePacket); ASSERT_INTERNAL(sequenceFlags == 0x3U, ErrorHandler::UnacceptablePacket); ASSERT_INTERNAL(packetDataLength == (length - 6U), ErrorHandler::UnacceptablePacket); @@ -78,7 +78,7 @@ void MessageParser::parseECSSTCHeader(const uint8_t* data, uint16_t length, Mess uint8_t serviceType = data[1]; uint8_t messageType = data[2]; - ErrorHandler::assertRequest(pusVersion == 2U, message, ErrorHandler::UnacceptableMessage); + ErrorHandler::assertRequest(pusVersion == 2u, message, ErrorHandler::UnacceptableMessage); // Remove the length of the header length -= 5; @@ -108,18 +108,19 @@ Message MessageParser::parseECSSTC(uint8_t* data) { String<CCSDS_MAX_MESSAGE_SIZE> MessageParser::composeECSS(const Message& message, uint16_t size) { uint8_t header[5]; - header[0] = ECSS_PUS_VERSION << 4u; // Assign the pusVersion = 2 - header[1] = message.serviceType; - header[2] = message.messageType; + if (message.packetType == Message::TC) { - header[0] = ECSS_PUS_VERSION << 4U; // Assign the pusVersion = 2 + header[0] = ECSS_PUS_VERSION << 4u; // Assign the pusVersion = 2 header[1] = message.serviceType; header[2] = message.messageType; header[3] = 0; header[4] = 0; } else { - header[3] = static_cast<uint8_t>(message.messageTypeCounter >> 8U); - header[4] = static_cast<uint8_t>(message.messageTypeCounter & 0xffU); + header[0] = ECSS_PUS_VERSION << 4u; // Assign the pusVersion = 2 + header[1] = message.serviceType; + header[2] = message.messageType; + header[3] = static_cast<uint8_t>(message.messageTypeCounter >> 8u); + header[4] = static_cast<uint8_t>(message.messageTypeCounter & 0xffu); } String<CCSDS_MAX_MESSAGE_SIZE> dataString(header, 5); diff --git a/test/ErrorHandler.cpp b/test/ErrorHandler.cpp index c0dc1393..38f89722 100644 --- a/test/ErrorHandler.cpp +++ b/test/ErrorHandler.cpp @@ -7,6 +7,7 @@ TEST_CASE("Error: Failed Acceptance", "[errors]") { ErrorHandler::reportError(failedMessage, ErrorHandler::MessageTooShort); REQUIRE(ServiceTests::hasOneMessage()); + CHECK(ServiceTests::thrownError(ErrorHandler::MessageTooShort)); Message report = ServiceTests::get(0); // Check that a TM[1,2] message was returned @@ -29,6 +30,7 @@ TEST_CASE("Error: Failed Execution Start", "[errors]") { ErrorHandler::reportError(failedMessage, ErrorHandler::UnknownExecutionStartError); REQUIRE(ServiceTests::hasOneMessage()); + CHECK(ServiceTests::thrownError(ErrorHandler::UnknownExecutionStartError)); Message report = ServiceTests::get(0); // Check that a TM[1,3] message was returned @@ -51,6 +53,7 @@ TEST_CASE("Error: Failed Execution Progress", "[errors]") { ErrorHandler::reportProgressError(failedMessage, ErrorHandler::UnknownExecutionProgressError, 0); REQUIRE(ServiceTests::hasOneMessage()); + CHECK(ServiceTests::thrownError(ErrorHandler::UnknownExecutionProgressError)); Message report = ServiceTests::get(0); // Check that a TM[1,6] message was returned @@ -74,6 +77,7 @@ TEST_CASE("Error: Failed Execution Completion", "[errors]") { ErrorHandler::reportError(failedMessage, ErrorHandler::UnknownExecutionCompletionError); REQUIRE(ServiceTests::hasOneMessage()); + CHECK(ServiceTests::thrownError(ErrorHandler::UnknownExecutionCompletionError)); Message report = ServiceTests::get(0); // Check that a TM[1,8] message was returned @@ -96,6 +100,7 @@ TEST_CASE("Error: Failed Routing", "[errors]") { ErrorHandler::reportError(failedMessage, ErrorHandler::UnknownRoutingError); REQUIRE(ServiceTests::hasOneMessage()); + CHECK(ServiceTests::thrownError(ErrorHandler::UnknownRoutingError)); Message report = ServiceTests::get(0); // Check that a TM[1,8] message was returned diff --git a/test/Helpers/TimeAndDate.cpp b/test/Helpers/TimeAndDate.cpp index f5bc35fe..13866f29 100644 --- a/test/Helpers/TimeAndDate.cpp +++ b/test/Helpers/TimeAndDate.cpp @@ -1,5 +1,6 @@ #include "catch2/catch.hpp" #include "Helpers/TimeAndDate.hpp" +#include "../Services/ServiceTests.hpp" TEST_CASE("Date comparison", "[operands]") { SECTION("Invalid date") { @@ -9,6 +10,9 @@ TEST_CASE("Date comparison", "[operands]") { TimeAndDate InvalidDate3(2030, 2, 2, 74, 5, 6); // error in hour TimeAndDate InvalidDate4(2030, 2, 2, 4, 75, 6); // error in minute TimeAndDate InvalidDate5(2030, 2, 2, 4, 5, 76); // error in seconds + + CHECK(ServiceTests::countErrors() == 6); + CHECK(ServiceTests::thrownError(ErrorHandler::InvalidDate)); } SECTION("Different year") { diff --git a/test/Helpers/TimeHelper.cpp b/test/Helpers/TimeHelper.cpp index 00577f65..cca057be 100644 --- a/test/Helpers/TimeHelper.cpp +++ b/test/Helpers/TimeHelper.cpp @@ -1,5 +1,6 @@ #include "catch2/catch.hpp" #include "Helpers/TimeHelper.hpp" +#include "../Services/ServiceTests.hpp" TEST_CASE("Time format implementation for CDS", "[CDS]") { SECTION("Invalid date") { @@ -16,7 +17,7 @@ TEST_CASE("Time format implementation for CDS", "[CDS]") { TimeHelper::utcToSeconds(TimeInfo); // invalid month - TimeInfo.year = 2018; + TimeInfo.year = 2019; TimeInfo.month = 60; TimeInfo.day = 10; TimeInfo.hour = 10; @@ -26,7 +27,7 @@ TEST_CASE("Time format implementation for CDS", "[CDS]") { TimeHelper::utcToSeconds(TimeInfo); // invalid day - TimeInfo.year = 2018; + TimeInfo.year = 2019; TimeInfo.month = 4; TimeInfo.day = 35; TimeInfo.hour = 10; @@ -36,7 +37,7 @@ TEST_CASE("Time format implementation for CDS", "[CDS]") { TimeHelper::utcToSeconds(TimeInfo); // invalid hour - TimeInfo.year = 2018; + TimeInfo.year = 2019; TimeInfo.month = 4; TimeInfo.day = 10; TimeInfo.hour = 100; @@ -46,7 +47,7 @@ TEST_CASE("Time format implementation for CDS", "[CDS]") { TimeHelper::utcToSeconds(TimeInfo); // invalid minute - TimeInfo.year = 2018; + TimeInfo.year = 2019; TimeInfo.month = 4; TimeInfo.day = 10; TimeInfo.hour = 10; @@ -56,7 +57,7 @@ TEST_CASE("Time format implementation for CDS", "[CDS]") { TimeHelper::utcToSeconds(TimeInfo); // invalid second - TimeInfo.year = 2018; + TimeInfo.year = 2019; TimeInfo.month = 4; TimeInfo.day = 10; TimeInfo.hour = 10; @@ -64,6 +65,9 @@ TEST_CASE("Time format implementation for CDS", "[CDS]") { TimeInfo.second = 122; TimeHelper::utcToSeconds(TimeInfo); + + CHECK(ServiceTests::countErrors() == 6); + CHECK(ServiceTests::thrownError(ErrorHandler::InvalidDate)); } SECTION("Convert UTC date to elapsed seconds since Unix epoch") { diff --git a/test/Services/FunctionManagementService.cpp b/test/Services/FunctionManagementService.cpp index 92cd24c4..a4893a1a 100644 --- a/test/Services/FunctionManagementService.cpp +++ b/test/Services/FunctionManagementService.cpp @@ -56,7 +56,7 @@ TEST_CASE("ST[08] - Call Tests") { CHECK(ServiceTests::get(0).messageType == 4); CHECK(ServiceTests::get(0).serviceType == 1); - CHECK(ServiceTests::countErrors() == 2); + CHECK(ServiceTests::countErrors() == 1); CHECK(globalVariable == 10); } } diff --git a/test/Services/ParameterService.cpp b/test/Services/ParameterService.cpp index d21f32bd..6153f6c5 100644 --- a/test/Services/ParameterService.cpp +++ b/test/Services/ParameterService.cpp @@ -25,6 +25,9 @@ TEST_CASE("Parameter Report Subservice") { CHECK_FALSE(report.readUint16() == 34672); // fail if faulty ID is present in report report.readUint32(); // ignore the carried settings } + + CHECK(ServiceTests::countErrors() == 1); + CHECK(ServiceTests::thrownError(ErrorHandler::UnknownExecutionStartError)); } // **WARNING** @@ -93,5 +96,8 @@ TEST_CASE("Parameter Setting Subservice") { CHECK(before.readUint16() == after.readUint16()); // check if all IDs are present CHECK_FALSE(after.readUint32() == 0xBAAAAAAD); // fail if any settings are BAAAAAAD :P } + + CHECK(ServiceTests::countErrors() == 3); + CHECK(ServiceTests::thrownError(ErrorHandler::UnknownExecutionStartError)); } } diff --git a/test/TestPlatform.cpp b/test/TestPlatform.cpp index beb76de1..6833d3ed 100644 --- a/test/TestPlatform.cpp +++ b/test/TestPlatform.cpp @@ -48,8 +48,7 @@ struct ServiceTestsListener : Catch::TestEventListenerBase { // An Error was thrown with this Message. If you expected this to happen, please call a // corresponding assertion function from ServiceTests to silence this message. - // TODO: Uncomment the following line as soon as Issue #19 is closed - // CHECK(ServiceTests::hasNoErrors()); + CHECK(ServiceTests::hasNoErrors()); } } -- GitLab