From 5e3239898e348770ad083c6dd40f6b294ba5b5ba Mon Sep 17 00:00:00 2001
From: Grigoris Pavlakis <grigpavl@ece.auth.gr>
Date: Sun, 18 Aug 2019 22:07:54 +0300
Subject: [PATCH] Fix an off-by-one in the safeguard's bit index

Other changes: Add a couple more tests
---
 src/Services/Parameter.cpp         |  2 +-
 test/Services/ParameterService.cpp | 56 +++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/src/Services/Parameter.cpp b/src/Services/Parameter.cpp
index 7c825324..fae89171 100644
--- a/src/Services/Parameter.cpp
+++ b/src/Services/Parameter.cpp
@@ -17,7 +17,7 @@ Parameter::Parameter(uint8_t newPtc, uint8_t newPfc, ValueType initialValue, Upd
 
 void Parameter::setCurrentValue(ValueType newVal) {
 	// set the value only if the parameter can be updated manually
-	if (flags[2]) {
+	if (flags[1]) {
 		currentValue = newVal;
 	}
 }
diff --git a/test/Services/ParameterService.cpp b/test/Services/ParameterService.cpp
index 65bb50b3..25188e30 100644
--- a/test/Services/ParameterService.cpp
+++ b/test/Services/ParameterService.cpp
@@ -10,8 +10,7 @@ void foo(ValueType* bar) {  // sample function
 }
 
 /* test ideas:
-* parameter setting while flag is active
-* requesting only invalid parameter IDs
+*
 *
 */
 
@@ -49,9 +48,29 @@ TEST_CASE("Parameter Service - General") {
 }
 
 TEST_CASE("Parameter Report Subservice") {
-//	SECTION("All requested parameters invalid") {
-//
-//	}
+	SECTION("All requested parameters invalid") {
+		Message request = Message(20, 1, Message::TC, 1);
+		request.appendUint16(3);
+		request.appendUint16(54432);
+		request.appendUint16(60000);
+		request.appendUint16(65535);
+
+		MessageParser::execute(request);
+		CHECK(ServiceTests::get(0).serviceType == 1);
+		CHECK(ServiceTests::get(0).messageType == 4);
+		CHECK(ServiceTests::get(1).serviceType == 1);
+		CHECK(ServiceTests::get(1).messageType == 4);
+		CHECK(ServiceTests::get(2).serviceType == 1);
+		CHECK(ServiceTests::get(2).messageType == 4);
+
+		Message report = ServiceTests::get(3);
+		CHECK(report.serviceType == 20);
+		CHECK(report.messageType == 2);
+		CHECK(report.readUint16() == 0);  // the message shall be empty
+
+		ServiceTests::reset();
+		Services.reset();
+	}
 
 	SECTION("Faulty instruction handling") {
 		Parameter param0 = Parameter(3, 14);
@@ -137,4 +156,31 @@ TEST_CASE("Parameter Setting Subservice") {
 		ServiceTests::reset();
 		Services.reset();
 	}
+
+	SECTION("Attempt to set parameter with no manual update availability") {
+		Parameter param1 = Parameter(1, 7, 12);
+		pserv.addNewParameter(1, param1, "100");
+
+		Message setRequest = Message(20, 3, Message::TC, 1);
+		setRequest.appendUint16(1);
+		setRequest.appendUint16(1);
+		setRequest.appendUint32(0xBAAAAAAD);
+
+		MessageParser::execute(setRequest);
+
+		Message infoRequest = Message(20, 1, Message::TC, 1);
+		infoRequest.appendUint16(1);
+		infoRequest.appendUint16(1);
+
+		MessageParser::execute(infoRequest);
+
+		Message report = ServiceTests::get(0);
+
+		CHECK(report.readUint16() == 1);
+		CHECK(report.readUint16() == 1);
+		CHECK_FALSE(report.readUint32() == 0xBAAAAAAD);
+
+		ServiceTests::reset();
+		Services.reset();
+	}
 }
-- 
GitLab