Skip to content

Commit 3328381

Browse files
yunhanw-googlepull[bot]
authored andcommitted
Add state check for IM Invoke command (#7023)
1 parent 7f65f81 commit 3328381

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

src/app/Command.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ void Command::Shutdown()
146146
CHIP_ERROR Command::PrepareCommand(const CommandPathParams * const apCommandPathParams, bool aIsStatus)
147147
{
148148
CHIP_ERROR err = CHIP_NO_ERROR;
149-
150-
CommandDataElement::Builder commandDataElement =
151-
mInvokeCommandBuilder.GetCommandListBuilder().CreateCommandDataElementBuilder();
152-
err = commandDataElement.GetError();
149+
CommandDataElement::Builder commandDataElement;
150+
VerifyOrExit(mState == CommandState::Initialized || mState == CommandState::AddCommand, err = CHIP_ERROR_INCORRECT_STATE);
151+
commandDataElement = mInvokeCommandBuilder.GetCommandListBuilder().CreateCommandDataElementBuilder();
152+
err = commandDataElement.GetError();
153153
SuccessOrExit(err);
154154

155155
if (apCommandPathParams != nullptr)

src/app/Command.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ class Command
132132
InteractionModelDelegate * mpDelegate = nullptr;
133133
chip::System::PacketBufferHandle mCommandMessageBuf;
134134
uint8_t mCommandIndex = 0;
135+
CommandState mState = CommandState::Uninitialized;
135136

136137
private:
137138
friend class TestCommandInteraction;
138-
CommandState mState = CommandState::Uninitialized;
139139
TLV::TLVType mDataElementContainerType = TLV::kTLVType_NotSpecified;
140140
chip::System::PacketBufferTLVWriter mCommandMessageWriter;
141141
};

src/app/CommandHandler.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ CHIP_ERROR CommandHandler::SendCommandResponse()
5757
{
5858
CHIP_ERROR err = CHIP_NO_ERROR;
5959

60+
VerifyOrExit(mState == CommandState::AddCommand, err = CHIP_ERROR_INCORRECT_STATE);
61+
6062
err = FinalizeCommandsMessage();
6163
SuccessOrExit(err);
6264

src/app/CommandSender.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId
3838
{
3939
CHIP_ERROR err = CHIP_NO_ERROR;
4040

41+
VerifyOrExit(mState == CommandState::AddCommand, err = CHIP_ERROR_INCORRECT_STATE);
42+
4143
err = FinalizeCommandsMessage();
4244
SuccessOrExit(err);
4345

src/app/tests/TestCommandInteraction.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aC
6565
class TestCommandInteraction
6666
{
6767
public:
68+
static void TestCommandSenderWithWrongState(nlTestSuite * apSuite, void * apContext);
69+
static void TestCommandHandlerWithWrongState(nlTestSuite * apSuite, void * apContext);
6870
static void TestCommandSenderWithSendCommand(nlTestSuite * apSuite, void * apContext);
6971
static void TestCommandHandlerWithSendEmptyCommand(nlTestSuite * apSuite, void * apContext);
7072
static void TestCommandSenderWithProcessReceivedMsg(nlTestSuite * apSuite, void * apContext);
@@ -172,6 +174,53 @@ void TestCommandInteraction::AddCommandDataElement(nlTestSuite * apSuite, void *
172174
}
173175
}
174176

177+
void TestCommandInteraction::TestCommandSenderWithWrongState(nlTestSuite * apSuite, void * apContext)
178+
{
179+
CHIP_ERROR err = CHIP_NO_ERROR;
180+
chip::app::CommandPathParams commandPathParams = { 1, // Endpoint
181+
2, // GroupId
182+
3, // ClusterId
183+
4, // CommandId
184+
(chip::app::CommandPathFlags::kEndpointIdValid) };
185+
app::CommandSender commandSender;
186+
187+
err = commandSender.PrepareCommand(&commandPathParams);
188+
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_INCORRECT_STATE);
189+
190+
commandSender.Shutdown();
191+
192+
System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
193+
err = commandSender.Init(&gExchangeManager, nullptr);
194+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
195+
196+
err = commandSender.SendCommandRequest(kTestDeviceNodeId, gAdminId);
197+
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_INCORRECT_STATE);
198+
}
199+
200+
void TestCommandInteraction::TestCommandHandlerWithWrongState(nlTestSuite * apSuite, void * apContext)
201+
{
202+
CHIP_ERROR err = CHIP_NO_ERROR;
203+
chip::app::CommandPathParams commandPathParams = { 1, // Endpoint
204+
2, // GroupId
205+
3, // ClusterId
206+
4, // CommandId
207+
(chip::app::CommandPathFlags::kEndpointIdValid) };
208+
app::CommandHandler commandHandler;
209+
210+
err = commandHandler.PrepareCommand(&commandPathParams);
211+
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_INCORRECT_STATE);
212+
commandHandler.Shutdown();
213+
214+
err = commandHandler.Init(&chip::gExchangeManager, nullptr);
215+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
216+
commandHandler.mpExchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr);
217+
TestExchangeDelegate delegate;
218+
commandHandler.mpExchangeCtx->SetDelegate(&delegate);
219+
err = commandHandler.SendCommandResponse();
220+
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_INCORRECT_STATE);
221+
commandHandler.Shutdown();
222+
}
223+
175224
void TestCommandInteraction::TestCommandSenderWithSendCommand(nlTestSuite * apSuite, void * apContext)
176225
{
177226
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -330,6 +379,8 @@ void InitializeChip(nlTestSuite * apSuite)
330379
// clang-format off
331380
const nlTest sTests[] =
332381
{
382+
NL_TEST_DEF("TestCommandSenderWithWrongState", chip::app::TestCommandInteraction::TestCommandSenderWithWrongState),
383+
NL_TEST_DEF("TestCommandHandlerWithWrongState", chip::app::TestCommandInteraction::TestCommandHandlerWithWrongState),
333384
NL_TEST_DEF("TestCommandSenderWithSendCommand", chip::app::TestCommandInteraction::TestCommandSenderWithSendCommand),
334385
NL_TEST_DEF("TestCommandHandlerWithSendEmptyCommand", chip::app::TestCommandInteraction::TestCommandHandlerWithSendEmptyCommand),
335386
NL_TEST_DEF("TestCommandSenderWithProcessReceivedMsg", chip::app::TestCommandInteraction::TestCommandSenderWithProcessReceivedMsg),

0 commit comments

Comments
 (0)