diff --git a/CMakeLists.txt b/CMakeLists.txt index 9329ae3ae696c5cdabb54b5cb29177a6f1155458..17504fdae3f08774795d5a3808265d9ea742b19f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ IF (EXISTS "${PROJECT_SOURCE_DIR}/lib/Catch2/CMakeLists.txt") add_executable(tests $<TARGET_OBJECTS:common> ${test_main_SRC} - ${test_SRC}) + ${test_SRC} test/Services/EventReportService.cpp) target_link_libraries(tests Catch2::Catch2) ENDIF() diff --git a/src/Services/EventReportService.cpp b/src/Services/EventReportService.cpp index 450636167e9170f9b9d1f757c75cbf37026cb0b7..bb3b68e365ae93c2d097b078b672c6ec298b64b1 100644 --- a/src/Services/EventReportService.cpp +++ b/src/Services/EventReportService.cpp @@ -1,14 +1,6 @@ #include "Services/EventReportService.hpp" #include "Message.hpp" -/** - * Note for the funtions informativeEventReport, lowSeverityReport, mediumSeverityReport, - * highSeverityReport: I could use - * for (int i=0; i<length; i++ { - * report.appendByte(data[i]); - * } - * instead of reinterpret_cast as it is a dangerous cast - */ void EventReportService::informativeEventReport(uint16_t eventID, const uint8_t *data, uint8_t length) { diff --git a/test/Services/EventReportService.cpp b/test/Services/EventReportService.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d5fa1414b49842475d828b4b971c0cd32f59007 --- /dev/null +++ b/test/Services/EventReportService.cpp @@ -0,0 +1,85 @@ +#include <catch2/catch.hpp> +#include <Services/EventReportService.hpp> +#include <Message.hpp> +#include "ServiceTests.hpp" +#include <cstring> + +/* + * @todo Change the reinterpret_cast + */ +TEST_CASE("Informative Event Report TM[5,1]","[service][st05]") { + EventReportService eventReportService; + const unsigned char eventReportData[]="HelloWorld"; + char checkString[255]; + eventReportService.informativeEventReport(1, eventReportData,10); + REQUIRE(ServiceTests::hasOneMessage()); + + Message report = ServiceTests::get(0); + // Checks for the data-members of the report Message created + CHECK(report.serviceType == 5); + CHECK(report.messageType == 1); + CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1) + REQUIRE(report.dataSize == 12); + // Check for the value that is stored in <<data>> array(data-member of object response) + CHECK(report.readEnum16() == 1); + report.readString(checkString,10); + CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0); +} + +TEST_CASE("Loww Severity Anomaly Report TM[5,2]","[service][st05]") { + EventReportService eventReportService; + const unsigned char eventReportData[]="HelloWorld"; + char checkString[255]; + eventReportService.lowSeverityAnomalyReport(2, eventReportData,10); + REQUIRE(ServiceTests::hasOneMessage()); + + Message report = ServiceTests::get(0); + // Checks for the data-members of the report Message created + CHECK(report.serviceType == 5); + CHECK(report.messageType == 2); + CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1) + REQUIRE(report.dataSize == 12); + // Check for the value that is stored in <<data>> array(data-member of object response) + CHECK(report.readEnum16() == 2); + report.readString(checkString,10); + CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0); +} + +TEST_CASE("Medium Severity Anomaly Report TM[5,3]","[service][st05]") { + EventReportService eventReportService; + const unsigned char eventReportData[]="HelloWorld"; + char checkString[255]; + eventReportService.mediumSeverityAnomalyReport(3, eventReportData,10); + REQUIRE(ServiceTests::hasOneMessage()); + + Message report = ServiceTests::get(0); + // Checks for the data-members of the report Message created + CHECK(report.serviceType == 5); + CHECK(report.messageType == 3); + CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1) + REQUIRE(report.dataSize == 12); + // Check for the value that is stored in <<data>> array(data-member of object response) + CHECK(report.readEnum16() == 3); + report.readString(checkString,10); + CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0); +} + +TEST_CASE("High Severity Anomaly Report TM[5,4]","[service][st05]") { + EventReportService eventReportService; + const unsigned char eventReportData[]="HelloWorld"; + char checkString[255]; + eventReportService.highSeverityAnomalyReport(4, eventReportData,10); + REQUIRE(ServiceTests::hasOneMessage()); + + Message report = ServiceTests::get(0); + // Checks for the data-members of the report Message created + CHECK(report.serviceType == 5); + CHECK(report.messageType == 4); + CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1) + REQUIRE(report.dataSize == 12); + // Check for the value that is stored in <<data>> array(data-member of object response) + CHECK(report.readEnum16() == 4); + report.readString(checkString,10); + CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0); +} +