From e6afe9baf424d54cebcb8a952e9359c948d426d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piet=20G=C3=B6mpel?= Date: Mon, 23 Dec 2024 12:47:19 +0100 Subject: [PATCH 1/2] Fixed bounds check of connectors when validating charging profile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Piet Gömpel --- lib/ocpp/v16/smart_charging.cpp | 2 +- tests/lib/ocpp/v16/test_smart_charging_handler.cpp | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ocpp/v16/smart_charging.cpp b/lib/ocpp/v16/smart_charging.cpp index 918b7f948..e251cf5dd 100644 --- a/lib/ocpp/v16/smart_charging.cpp +++ b/lib/ocpp/v16/smart_charging.cpp @@ -166,7 +166,7 @@ bool SmartChargingHandler::validate_profile( ChargingProfile& profile, const int connector_id, bool ignore_no_transaction, const int profile_max_stack_level, const int max_charging_profiles_installed, const int charging_schedule_max_periods, const std::vector& charging_schedule_allowed_charging_rate_units) { - if ((size_t)connector_id > this->connectors.size() or connector_id < 0 or profile.stackLevel < 0 or + if ((size_t)connector_id >= this->connectors.size() or connector_id < 0 or profile.stackLevel < 0 or profile.stackLevel > profile_max_stack_level) { EVLOG_warning << "INVALID PROFILE - connector_id invalid or invalid stack level"; return false; diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 5bb8a55dc..879093c81 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -209,6 +209,7 @@ class ChargepointTestFixture : public testing::Test { auto database = std::make_unique(database_path / (chargepoint_id + ".db")); std::shared_ptr database_handler = std::make_shared(std::move(database), init_script_path); + addConnector(0); auto handler = new SmartChargingHandler(connectors, database_handler, *configuration); return handler; } @@ -302,12 +303,19 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ConnectorIdGreaterThanConnectors const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); - const int connector_id = INT_MAX; + int connector_id = INT_MAX; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); + + // if we have connectors 0,1,2 this->connectors.size() == 3 and a connector_id of 3 is invalid + connector_id = this->connectors.size(); + sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + ASSERT_FALSE(sut); } /** From 9744cb8f2e2f6c2d6e4e47539c65de30e84d14b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piet=20G=C3=B6mpel?= Date: Mon, 23 Dec 2024 13:05:54 +0100 Subject: [PATCH 2/2] clang format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Piet Gömpel --- tests/lib/ocpp/v16/test_smart_charging_handler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 879093c81..ea5ecf1ae 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -313,8 +313,8 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ConnectorIdGreaterThanConnectors // if we have connectors 0,1,2 this->connectors.size() == 3 and a connector_id of 3 is invalid connector_id = this->connectors.size(); sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); }