From 498abbcaa5e42538f0efa5d422d4cc36467cf9be Mon Sep 17 00:00:00 2001
From: Grigoris Pavlakis <grigpavl@ece.auth.gr>
Date: Fri, 23 Nov 2018 02:05:25 +0200
Subject: [PATCH] Start work on proper test cases

---
 CMakeLists.txt                     |  8 +++--
 inc/Services/ParameterService.hpp  |  3 ++
 src/Services/ParameterService.cpp  | 13 ++++-----
 src/main.cpp                       |  7 +++--
 test/Services/ParameterService.cpp | 47 ++++++++++++++++++++++++++++++
 5 files changed, 64 insertions(+), 14 deletions(-)
 create mode 100644 test/Services/ParameterService.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4ee04669..6844fc4a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,13 +15,15 @@ add_custom_target(check
 
 # Specify the .cpp files for the executables
 add_executable(ecss_services src/main.cpp src/Message.cpp src/Service.cpp
-        src/Services/TestService.cpp src/Services/RequestVerificationService.cpp src/Services/ParameterService.cpp)
+        src/Services/TestService.cpp src/Services/RequestVerificationService.cpp
+        src/Services/ParameterService.cpp)
 
 IF (EXISTS "${PROJECT_SOURCE_DIR}/lib/Catch2/CMakeLists.txt")
     add_subdirectory(lib/Catch2)
     add_executable(tests src/Message.cpp src/Services/TestService.cpp
-            src/Services/RequestVerificationService.cpp test/tests.cpp
+            src/Services/RequestVerificationService.cpp src/Services/ParameterService.cpp
+            test/tests.cpp
             test/Message.cpp test/TestPlatform.cpp test/Services/TestService.cpp
-            test/Services/RequestVerificationService.cpp)
+            test/Services/RequestVerificationService.cpp test/Services/ParameterService.cpp)
     target_link_libraries(tests Catch2::Catch2)
 ENDIF ()
diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp
index f7f2a04e..0992043f 100644
--- a/inc/Services/ParameterService.hpp
+++ b/inc/Services/ParameterService.hpp
@@ -2,6 +2,7 @@
 #define ECSS_SERVICES_PARAMETERSERVICE_HPP
 
 #include "Service.hpp"
+// #include "Services/RequestVerificationService.hpp"
 
 #define CONFIGLENGTH 5
 
@@ -42,6 +43,8 @@ class ParameterService : public Service {
 	Parameter paramsList[CONFIGLENGTH];
 	// CONFIGLENGTH is just a dummy number for now, this should be statically set
 
+	// RequestVerificationService rvs;  // request verification service for error reporting
+
 public:
 	ParameterService();
 
diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp
index 9875f01a..49e1de6b 100644
--- a/src/Services/ParameterService.cpp
+++ b/src/Services/ParameterService.cpp
@@ -9,7 +9,7 @@
 
 #endif
 
-uint16_t numOfValidIds(Message idMsg);
+uint16_t numOfValidIds(Message idMsg);  //count the valid ids in a given TC[20, 1]
 
 ParameterService::ParameterService() {
 #ifdef DEMOMODE
@@ -48,7 +48,7 @@ Message ParameterService::reportParameterIds(Message paramIds) {
 	 * @return A TM[20, 2] packet containing the valid parameter IDs and their settings.
 	 * @return Empty TM[20, 2] packet on wrong type.
 	 *
-	 * @todo Generate failure notifs where needed when ST[06] is ready
+	 * @todo Generate failure notifs where needed when ST[01] is ready
 	 *
 	 * NOTES:
 	 * Method for valid ID counting is a hack (clones the message and figures out the number
@@ -71,14 +71,11 @@ Message ParameterService::reportParameterIds(Message paramIds) {
 
 			if (currId < CONFIGLENGTH) {  // check to prevent out-of-bounds access due to invalid id
 
-				reqParam.appendUint16(currId);      // append it to the new packet
+				reqParam.appendUint16(currId);
 				reqParam.appendUint32(paramsList[currId].settingData);
-				// right after that append the settings
 			} else {
 
-				// generate failed execution notification
-				// (depends on execution reporting subservice ST[06])
-
+				// generate failure of execution notification for ST[06]
 				continue;       //ignore the invalid ID
 			}
 		}
@@ -115,7 +112,7 @@ void ParameterService::setParameterIds(Message newParamValues) {
 				paramsList[currId].settingData = newParamValues.readUint32();
 			} else {
 
-				// generate failure of execution with ST[06]
+				// generate failure of execution notification for ST[06]
 				continue;       // ignore the invalid ID
 			}
 		}
diff --git a/src/main.cpp b/src/main.cpp
index f11acf21..0cfdc6a1 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,7 +1,7 @@
 #include <iostream>
-#include <Services/TestService.hpp>
+#include "Services/TestService.hpp"
 #include "Services/ParameterService.hpp"
-#include <Services/RequestVerificationService.hpp>
+#include "Services/RequestVerificationService.hpp"
 #include "Message.hpp"
 
 int main() {
@@ -30,6 +30,7 @@ int main() {
 	receivedPacket.appendUint16(7);
 	testService.onBoardConnection(receivedPacket);
 
+	/*
 	//ST[20] test
 	ParameterService paramService;
 
@@ -71,7 +72,7 @@ int main() {
 		std::cout << "Parameter ID: " << std::dec << returnedPacket.readUint16() << std::endl
 		          << "Parameter value: " << std::dec << returnedPacket.readUint32() << std::endl;
 
-	}
+	}*/
 
 // ST[01] test
 	// parameters take random values and works as expected
diff --git a/test/Services/ParameterService.cpp b/test/Services/ParameterService.cpp
new file mode 100644
index 00000000..c87cd773
--- /dev/null
+++ b/test/Services/ParameterService.cpp
@@ -0,0 +1,47 @@
+#include "catch2/catch.hpp"
+#include "Services/ParameterService.hpp"
+#include "Message.hpp"
+
+#define CATCH_CONFIG_MAIN
+
+TEST_CASE("Parameter Report Subservice") {
+
+	ParameterService pserv;
+
+	SECTION("Faulty Instruction Ignorance Test") {
+
+		//TODO: Find a better criterion than checking the first 16 bits
+
+		Message request(20, 1, Message::TC, 1);
+		Message report(20, 2, Message::TM, 1);
+
+		request.appendUint16(2);      // number of requested IDs
+		request.appendUint16(34672);  // faulty ID in this context
+		request.appendUint16(3);      // valid
+
+		report = pserv.reportParameterIds(request);
+		request.readPosition = 0;
+
+		uint16_t repIdCount = report.readUint16();
+		uint16_t reqIdCount = request.readUint16();
+
+		REQUIRE(repIdCount < reqIdCount);
+		// in case there are ignored IDs the number of IDs in the report
+		// will be smaller than the original
+	}
+
+	SECTION("Wrong Message Type Handling Test") {
+
+		Message falseRequest(15, 3, Message::TM, 1);   //a totally wrong message
+		Message response = pserv.reportParameterIds(falseRequest);
+		CHECK(response.messageType == 2);
+		CHECK(response.serviceType == 20);
+		CHECK(response.packetType == Message::TM);
+		CHECK(response.readPosition == 0);   // if empty, this should't change from 0
+	}
+}
+
+TEST_CASE("Parameter Setting Subservice") {
+
+
+}
\ No newline at end of file
-- 
GitLab