Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and fix batch command tests in TestCommandInteraction #32288

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 96 additions & 105 deletions src/app/tests/TestCommandInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1534,36 +1534,31 @@ void TestCommandInteraction::TestCommandHandlerRejectMultipleIdenticalCommands(n
CHIP_ERROR err = CHIP_NO_ERROR;

isCommandDispatched = false;
mockCommandSenderDelegate.ResetCounter();
app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager());
mockCommandSenderExtendedDelegate.ResetCounter();
PendingResponseTrackerImpl pendingResponseTracker;
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
&pendingResponseTracker);

{
// Command ID is not important here, since the command handler should reject the commands without handling it.
auto commandPathParams = MakeTestCommandPath(kTestCommandIdCommandSpecificResponse);
app::CommandSender::ConfigParameters configParameters;
configParameters.SetRemoteMaxPathsPerInvoke(2);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.SetCommandSenderConfig(configParameters));

commandSender.AllocateBuffer();
// Command ID is not important here, since the command handler should reject the commands without handling it.
auto commandPathParams = MakeTestCommandPath(kTestCommandIdCommandSpecificResponse);

// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
for (int i = 0; i < 2; i++)
{
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
CommandPathIB::Builder & path = invokeRequest.CreatePath();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(commandPathParams));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR ==
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
TLV::kTLVType_Structure,
commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
}
for (uint16_t i = 0; i < 2; i++)
{
app::CommandSender::PrepareCommandParameters prepareCommandParams;
prepareCommandParams.SetStartDataStruct(true);
prepareCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(commandPathParams, prepareCommandParams));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));

commandSender.MoveToState(app::CommandSender::State::AddedCommand);
app::CommandSender::FinishCommandParameters finishCommandParams;
finishCommandParams.SetEndDataStruct(true);
finishCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.FinishCommand(finishCommandParams));
}

err = commandSender.SendCommandRequest(ctx.GetSessionBobToAlice());
Expand All @@ -1573,60 +1568,62 @@ void TestCommandInteraction::TestCommandHandlerRejectMultipleIdenticalCommands(n
ctx.DrainAndServiceIO();

NL_TEST_ASSERT(apSuite,
mockCommandSenderDelegate.onResponseCalledTimes == 0 && mockCommandSenderDelegate.onFinalCalledTimes == 1 &&
mockCommandSenderDelegate.onErrorCalledTimes == 1);
mockCommandSenderExtendedDelegate.onResponseCalledTimes == 0 &&
mockCommandSenderExtendedDelegate.onFinalCalledTimes == 1 &&
mockCommandSenderExtendedDelegate.onErrorCalledTimes == 1);
NL_TEST_ASSERT(apSuite, !chip::isCommandDispatched);

NL_TEST_ASSERT(apSuite, GetNumActiveHandlerObjects() == 0);
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
}

#if CONFIG_BUILD_FOR_HOST_UNIT_TEST

void TestCommandInteraction::TestCommandHandlerRejectsMultipleCommandsWithIdenticalCommandRef(nlTestSuite * apSuite,
void * apContext)
{
TestContext & ctx = *static_cast<TestContext *>(apContext);
CHIP_ERROR err = CHIP_NO_ERROR;

isCommandDispatched = false;
mockCommandSenderDelegate.ResetCounter();
mockCommandSenderExtendedDelegate.ResetCounter();
PendingResponseTrackerImpl pendingResponseTracker;
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
&pendingResponseTracker);

// Using commandSender to help build afterward we take the buffer to feed into standalone CommandHandler
app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager());
app::CommandSender::ConfigParameters configParameters;
configParameters.SetRemoteMaxPathsPerInvoke(2);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.SetCommandSenderConfig(configParameters));

size_t numberOfCommandsToSend = 2;
uint16_t numberOfCommandsToSend = 2;
{
CommandPathParams requestCommandPaths[] = {
MakeTestCommandPath(kTestCommandIdWithData),
MakeTestCommandPath(kTestCommandIdCommandSpecificResponse),
};

commandSender.AllocateBuffer();
uint16_t hardcodedCommandRef = 0;

// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
for (size_t i = 0; i < numberOfCommandsToSend; i++)
for (uint16_t i = 0; i < numberOfCommandsToSend; i++)
{
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
CommandPathIB::Builder & path = invokeRequest.CreatePath();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(requestCommandPaths[i]));
app::CommandSender::PrepareCommandParameters prepareCommandParams;
prepareCommandParams.SetStartDataStruct(true);
prepareCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(requestCommandPaths[i], prepareCommandParams));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR ==
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
TLV::kTLVType_Structure,
commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.Ref(1));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
// TODO fix this comment
// We are taking advantage of the fact that the commandRef was set into finishCommandParams during PrepareCommand
// But setting it to a different value here, we are overriding what it was supposed to be.
app::CommandSender::FinishCommandParameters finishCommandParams;
finishCommandParams.SetEndDataStruct(true);
finishCommandParams.SetCommandRef(hardcodedCommandRef);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.TestOnlyFinishCommand(finishCommandParams));
}

commandSender.MoveToState(app::CommandSender::State::AddedCommand);
}

CommandHandler commandHandler(&mockCommandHandlerDelegate);
BasicCommandPathRegistry<4> mBasicCommandPathRegistry;
CommandHandler commandHandler(kCommandHandlerTestOnlyMarker, &mockCommandHandlerDelegate, &mBasicCommandPathRegistry);
TestExchangeDelegate delegate;
auto exchange = ctx.NewExchangeToAlice(&delegate, false);
commandHandler.mResponseSender.SetExchangeContext(exchange);
Expand Down Expand Up @@ -1654,53 +1651,48 @@ void TestCommandInteraction::TestCommandHandlerRejectsMultipleCommandsWithIdenti
exchange->Close();
}

#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST

void TestCommandInteraction::TestCommandHandlerRejectMultipleCommandsWhenHandlerOnlySupportsOne(nlTestSuite * apSuite,
void * apContext)
{
TestContext & ctx = *static_cast<TestContext *>(apContext);
CHIP_ERROR err = CHIP_NO_ERROR;

isCommandDispatched = false;
mockCommandSenderDelegate.ResetCounter();

// Using commandSender to help build afterward we take the buffer to feed into standalone CommandHandler
app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager());
mockCommandSenderExtendedDelegate.ResetCounter();
PendingResponseTrackerImpl pendingResponseTracker;
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
&pendingResponseTracker);

size_t numberOfCommandsToSend = 2;
app::CommandSender::ConfigParameters configParameters;
configParameters.SetRemoteMaxPathsPerInvoke(2);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.SetCommandSenderConfig(configParameters));

uint16_t numberOfCommandsToSend = 2;
{
CommandPathParams requestCommandPaths[] = {
MakeTestCommandPath(kTestCommandIdWithData),
MakeTestCommandPath(kTestCommandIdCommandSpecificResponse),
};

commandSender.AllocateBuffer();

// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
for (size_t i = 0; i < numberOfCommandsToSend; i++)
for (uint16_t i = 0; i < numberOfCommandsToSend; i++)
{
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
CommandPathIB::Builder & path = invokeRequest.CreatePath();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(requestCommandPaths[i]));
app::CommandSender::PrepareCommandParameters prepareCommandParams;
prepareCommandParams.SetStartDataStruct(true);
prepareCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(requestCommandPaths[i], prepareCommandParams));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR ==
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
TLV::kTLVType_Structure,
commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.Ref(static_cast<uint16_t>(i)));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
app::CommandSender::FinishCommandParameters finishCommandParams;
finishCommandParams.SetEndDataStruct(true);
finishCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.FinishCommand(finishCommandParams));
}

commandSender.MoveToState(app::CommandSender::State::AddedCommand);
}

BasicCommandPathRegistry<4> mBasicCommandPathRegistry;
CommandHandler commandHandler(kCommandHandlerTestOnlyMarker, &mockCommandHandlerDelegate, &mBasicCommandPathRegistry);
CommandHandler commandHandler(&mockCommandHandlerDelegate);
TestExchangeDelegate delegate;
auto exchange = ctx.NewExchangeToAlice(&delegate, false);
commandHandler.mResponseSender.SetExchangeContext(exchange);
Expand All @@ -1715,9 +1707,9 @@ void TestCommandInteraction::TestCommandHandlerRejectMultipleCommandsWhenHandler
commandDispatchedCount = 0;

InteractionModel::Status status = commandHandler.ProcessInvokeRequest(std::move(commandDatabuf), false);
NL_TEST_ASSERT(apSuite, status == InteractionModel::Status::Success);
NL_TEST_ASSERT(apSuite, status == InteractionModel::Status::InvalidAction);

NL_TEST_ASSERT(apSuite, commandDispatchedCount == 2);
NL_TEST_ASSERT(apSuite, commandDispatchedCount == 0);

//
// Ordinarily, the ExchangeContext will close itself on a responder exchange when unwinding back from an
Expand All @@ -1735,39 +1727,35 @@ void TestCommandInteraction::TestCommandHandlerAcceptMultipleCommands(nlTestSuit
CHIP_ERROR err = CHIP_NO_ERROR;

isCommandDispatched = false;
mockCommandSenderDelegate.ResetCounter();

// Using commandSender to help build afterward we take the buffer to feed into standalone CommandHandler
app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager());
mockCommandSenderExtendedDelegate.ResetCounter();
PendingResponseTrackerImpl pendingResponseTracker;
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
&pendingResponseTracker);

app::CommandSender::ConfigParameters configParameters;
configParameters.SetRemoteMaxPathsPerInvoke(2);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.SetCommandSenderConfig(configParameters));

size_t numberOfCommandsToSend = 2;
uint16_t numberOfCommandsToSend = 2;
{
CommandPathParams requestCommandPaths[] = {
MakeTestCommandPath(kTestCommandIdWithData),
MakeTestCommandPath(kTestCommandIdCommandSpecificResponse),
};

commandSender.AllocateBuffer();

// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
for (size_t i = 0; i < numberOfCommandsToSend; i++)
for (uint16_t i = 0; i < numberOfCommandsToSend; i++)
{
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
CommandPathIB::Builder & path = invokeRequest.CreatePath();
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(requestCommandPaths[i]));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR ==
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
TLV::kTLVType_Structure,
commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
app::CommandSender::PrepareCommandParameters prepareCommandParams;
prepareCommandParams.SetStartDataStruct(true);
prepareCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(requestCommandPaths[i], prepareCommandParams));
NL_TEST_ASSERT(apSuite,
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.Ref(static_cast<uint16_t>(i)));
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
app::CommandSender::FinishCommandParameters finishCommandParams;
finishCommandParams.SetEndDataStruct(true);
finishCommandParams.SetCommandRef(i);
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.FinishCommand(finishCommandParams));
}

commandSender.MoveToState(app::CommandSender::State::AddedCommand);
Expand All @@ -1784,6 +1772,7 @@ void TestCommandInteraction::TestCommandHandlerAcceptMultipleCommands(nlTestSuit
err = commandSender.Finalize(commandDatabuf);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

sendResponse = true;
mockCommandHandlerDelegate.ResetCounter();
commandDispatchedCount = 0;

Expand Down Expand Up @@ -1938,7 +1927,9 @@ const nlTest sTests[] =
NL_TEST_DEF("TestCommandHandlerWithProcessReceivedNotExistCommand", chip::app::TestCommandInteraction::TestCommandHandlerWithProcessReceivedNotExistCommand),
NL_TEST_DEF("TestCommandHandlerWithProcessReceivedEmptyDataMsg", chip::app::TestCommandInteraction::TestCommandHandlerWithProcessReceivedEmptyDataMsg),
NL_TEST_DEF("TestCommandHandlerRejectMultipleIdenticalCommands", chip::app::TestCommandInteraction::TestCommandHandlerRejectMultipleIdenticalCommands),
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
NL_TEST_DEF("TestCommandHandlerRejectsMultipleCommandsWithIdenticalCommandRef", chip::app::TestCommandInteraction::TestCommandHandlerRejectsMultipleCommandsWithIdenticalCommandRef),
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
NL_TEST_DEF("TestCommandHandlerRejectMultipleCommandsWhenHandlerOnlySupportsOne", chip::app::TestCommandInteraction::TestCommandHandlerRejectMultipleCommandsWhenHandlerOnlySupportsOne),
NL_TEST_DEF("TestCommandHandlerAcceptMultipleCommands", chip::app::TestCommandInteraction::TestCommandHandlerAcceptMultipleCommands),
NL_TEST_DEF("TestCommandHandler_FillUpInvokeResponseMessageWhereSecondResponseIsStatusResponse", chip::app::TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhereSecondResponseIsStatusResponse),
Expand Down
Loading