Skip to content

Commit 5efe2e7

Browse files
cecillerestyled-commitsjamesharrow
authored
TC-EEVSE-2.3: Fix check when command returns success, but expects error (project-chip#41687)
* TC-CNET-4.12: Potential fix for connection timeout [WIP] * Another try with srp * some more fixes * fix instance name * Fix EEVSE check for command errors * Fix app * Restyled by autopep8 * Revert "fix instance name" This reverts commit 2d5a46f. * Revert "some more fixes" This reverts commit 04a32b5. * Revert "Another try with srp" This reverts commit ecd3d2c. * Revert "TC-CNET-4.12: Potential fix for connection timeout [WIP]" This reverts commit 67b1b24. * Made the check at a lower part of the SDK and changed to use an existing constant. * Added stronger validation for all send_single_cmd() calls to ensure the expected_status is Success in the success path. * Added comments as proposed in PR review * Added suggestion from PR to improve assert message when it fails. * Restyled by autopep8 --------- Co-authored-by: Restyled.io <[email protected]> Co-authored-by: James Harrow <[email protected]>
1 parent 3eeb701 commit 5efe2e7

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

examples/energy-management-app/energy-management-common/energy-evse/src/ChargingTargetsMemMgr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ CHIP_ERROR ChargingTargetsMemMgr::AllocAndCopy()
9393
// ChargingTargetsMemMgr.h before this method can be called.
9494

9595
VerifyOrDie(mpListOfDays[mChargingTargetSchedulesIdx] == nullptr);
96+
VerifyOrReturnError(mNumDailyChargingTargets <= kEvseTargetsMaxTargetsPerDay, CHIP_ERROR_NO_MEMORY);
9697

9798
if (mNumDailyChargingTargets > 0)
9899
{
@@ -124,6 +125,7 @@ ChargingTargetsMemMgr::AllocAndCopy(const DataModel::List<const Structs::Chargin
124125
VerifyOrDie(mpListOfDays[mChargingTargetSchedulesIdx] == nullptr);
125126

126127
mNumDailyChargingTargets = static_cast<uint16_t>(chargingTargets.size());
128+
VerifyOrReturnError(mNumDailyChargingTargets <= kEvseTargetsMaxTargetsPerDay, CHIP_ERROR_NO_MEMORY);
127129

128130
if (mNumDailyChargingTargets > 0)
129131
{
@@ -161,6 +163,7 @@ ChargingTargetsMemMgr::AllocAndCopy(const DataModel::DecodableList<Structs::Char
161163
ReturnErrorOnFailure(chargingTargets.ComputeSize(&numDailyChargingTargets));
162164

163165
mNumDailyChargingTargets = static_cast<uint16_t>(numDailyChargingTargets);
166+
VerifyOrReturnError(mNumDailyChargingTargets <= kEvseTargetsMaxTargetsPerDay, CHIP_ERROR_NO_MEMORY);
164167

165168
if (mNumDailyChargingTargets > 0)
166169
{

examples/energy-management-app/energy-management-common/energy-evse/src/EnergyEvseDelegateImpl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ Status EnergyEvseDelegate::SetTargets(
368368
VerifyOrReturnError(targets != nullptr, Status::Failure);
369369

370370
CHIP_ERROR err = targets->SetTargets(chargingTargetSchedules);
371+
if (err == CHIP_ERROR_NO_MEMORY)
372+
{
373+
return Status::ResourceExhausted;
374+
}
371375
VerifyOrReturnError(err == CHIP_NO_ERROR, StatusIB(err).mStatus);
372376

373377
/* The Application needs to be told that the Targets have been updated

src/app/clusters/energy-evse-server/energy-evse-server.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ Status Instance::ValidateTargets(
449449
innerIdx++;
450450
}
451451

452+
if (innerIdx > kEvseTargetsMaxTargetsPerDay)
453+
{
454+
ChipLogError(AppServer, "Too many targets in a single ChargingTargetScheduleStruct (%d)", innerIdx);
455+
return Status::ResourceExhausted;
456+
}
457+
452458
if (iterInner.GetStatus() != CHIP_NO_ERROR)
453459
{
454460
return Status::InvalidCommand;

src/python_testing/TC_EEVSE_Utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ async def send_enable_charge_command(self, endpoint: int = None, charge_until: i
8686
endpoint=endpoint,
8787
timedRequestTimeoutMs=timedRequestTimeoutMs)
8888

89+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
90+
asserts.assert_equal(expected_status, Status.Success,
91+
f"Unexpected Success returned when expected {expected_status}")
92+
8993
except InteractionModelError as e:
94+
# The command failed, which might be what we expected, check if it is the expected error
9095
asserts.assert_equal(e.status, expected_status,
9196
"Unexpected error returned")
9297

@@ -106,7 +111,12 @@ async def send_enable_discharge_command(self, endpoint: int = None, discharge_un
106111
endpoint=endpoint,
107112
timedRequestTimeoutMs=timedRequestTimeoutMs)
108113

114+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
115+
asserts.assert_equal(expected_status, Status.Success,
116+
f"Unexpected Success returned when expected {expected_status}")
117+
109118
except InteractionModelError as e:
119+
# The command failed, which might be what we expected, check if it is the expected error
110120
asserts.assert_equal(e.status, expected_status,
111121
"Unexpected error returned")
112122

@@ -116,7 +126,12 @@ async def send_disable_command(self, endpoint: int = None, timedRequestTimeoutMs
116126
endpoint=endpoint,
117127
timedRequestTimeoutMs=timedRequestTimeoutMs)
118128

129+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
130+
asserts.assert_equal(expected_status, Status.Success,
131+
f"Unexpected Success returned when expected {expected_status}")
132+
119133
except InteractionModelError as e:
134+
# The command failed, which might be what we expected, check if it is the expected error
120135
asserts.assert_equal(e.status, expected_status,
121136
"Unexpected error returned")
122137

@@ -127,7 +142,12 @@ async def send_start_diagnostics_command(self, endpoint: int = None, timedReques
127142
endpoint=endpoint,
128143
timedRequestTimeoutMs=timedRequestTimeoutMs)
129144

145+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
146+
asserts.assert_equal(expected_status, Status.Success,
147+
f"Unexpected Success returned when expected {expected_status}")
148+
130149
except InteractionModelError as e:
150+
# The command failed, which might be what we expected, check if it is the expected error
131151
asserts.assert_equal(e.status, expected_status,
132152
"Unexpected error returned")
133153

@@ -138,7 +158,12 @@ async def send_clear_targets_command(self, endpoint: int = None, timedRequestTim
138158
endpoint=endpoint,
139159
timedRequestTimeoutMs=timedRequestTimeoutMs)
140160

161+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
162+
asserts.assert_equal(expected_status, Status.Success,
163+
f"Unexpected Success returned when expected {expected_status}")
164+
141165
except InteractionModelError as e:
166+
# The command failed, which might be what we expected, check if it is the expected error
142167
asserts.assert_equal(e.status, expected_status,
143168
"Unexpected error returned")
144169

@@ -149,7 +174,12 @@ async def send_get_targets_command(self, endpoint: int = None, timedRequestTimeo
149174
endpoint=endpoint,
150175
timedRequestTimeoutMs=timedRequestTimeoutMs)
151176

177+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
178+
asserts.assert_equal(expected_status, Status.Success,
179+
f"Unexpected Success returned when expected {expected_status}")
180+
152181
except InteractionModelError as e:
182+
# The command failed, which might be what we expected, check if it is the expected error
153183
asserts.assert_equal(e.status, expected_status,
154184
"Unexpected error returned")
155185

@@ -165,7 +195,12 @@ async def send_set_targets_command(self, endpoint: int = None,
165195
endpoint=endpoint,
166196
timedRequestTimeoutMs=timedRequestTimeoutMs)
167197

198+
# If the command was expected to fail but it succeeded, check it wasn't meant to fail
199+
asserts.assert_equal(expected_status, Status.Success,
200+
f"Unexpected Success returned when expected {expected_status}")
201+
168202
except InteractionModelError as e:
203+
# The command failed, which might be what we expected, check if it is the expected error
169204
asserts.assert_equal(e.status, expected_status,
170205
"Unexpected error returned")
171206

@@ -298,7 +333,7 @@ def compute_expected_target_time_as_epoch_s(self, minutes_past_midnight):
298333

299334
logger.info(
300335
f"minutesPastMidnight = {minutes_past_midnight} => "
301-
f"{int(minutes_past_midnight/60)}:{int(minutes_past_midnight%60)}"
336+
f"{int(minutes_past_midnight/60)}:{int(minutes_past_midnight % 60)}"
302337
f" Expected target_time = {target_time}")
303338

304339
# Matter Epoch is 1st Jan 2000

0 commit comments

Comments
 (0)