Skip to content

Commit b951c6c

Browse files
Cleanup and fix batch command tests in TestCommandInteraction (#32288)
* Have unit test build batch command using CommandSender * Restyled by clang-format * Fix CI * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent e45c93a commit b951c6c

File tree

1 file changed

+96
-105
lines changed

1 file changed

+96
-105
lines changed

src/app/tests/TestCommandInteraction.cpp

+96-105
Original file line numberDiff line numberDiff line change
@@ -1576,36 +1576,31 @@ void TestCommandInteraction::TestCommandHandlerRejectMultipleIdenticalCommands(n
15761576
CHIP_ERROR err = CHIP_NO_ERROR;
15771577

15781578
isCommandDispatched = false;
1579-
mockCommandSenderDelegate.ResetCounter();
1580-
app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager());
1579+
mockCommandSenderExtendedDelegate.ResetCounter();
1580+
PendingResponseTrackerImpl pendingResponseTracker;
1581+
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
1582+
&pendingResponseTracker);
15811583

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

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

1588-
// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
1589-
for (int i = 0; i < 2; i++)
1590-
{
1591-
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
1592-
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
1593-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
1594-
CommandPathIB::Builder & path = invokeRequest.CreatePath();
1595-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
1596-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(commandPathParams));
1597-
NL_TEST_ASSERT(apSuite,
1598-
CHIP_NO_ERROR ==
1599-
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
1600-
TLV::kTLVType_Structure,
1601-
commandSender.mDataElementContainerType));
1602-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1603-
NL_TEST_ASSERT(apSuite,
1604-
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
1605-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
1606-
}
1591+
for (uint16_t i = 0; i < 2; i++)
1592+
{
1593+
app::CommandSender::PrepareCommandParameters prepareCommandParams;
1594+
prepareCommandParams.SetStartDataStruct(true);
1595+
prepareCommandParams.SetCommandRef(i);
1596+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(commandPathParams, prepareCommandParams));
1597+
NL_TEST_ASSERT(apSuite,
1598+
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
16071599

1608-
commandSender.MoveToState(app::CommandSender::State::AddedCommand);
1600+
app::CommandSender::FinishCommandParameters finishCommandParams;
1601+
finishCommandParams.SetEndDataStruct(true);
1602+
finishCommandParams.SetCommandRef(i);
1603+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.FinishCommand(finishCommandParams));
16091604
}
16101605

16111606
err = commandSender.SendCommandRequest(ctx.GetSessionBobToAlice());
@@ -1615,60 +1610,62 @@ void TestCommandInteraction::TestCommandHandlerRejectMultipleIdenticalCommands(n
16151610
ctx.DrainAndServiceIO();
16161611

16171612
NL_TEST_ASSERT(apSuite,
1618-
mockCommandSenderDelegate.onResponseCalledTimes == 0 && mockCommandSenderDelegate.onFinalCalledTimes == 1 &&
1619-
mockCommandSenderDelegate.onErrorCalledTimes == 1);
1613+
mockCommandSenderExtendedDelegate.onResponseCalledTimes == 0 &&
1614+
mockCommandSenderExtendedDelegate.onFinalCalledTimes == 1 &&
1615+
mockCommandSenderExtendedDelegate.onErrorCalledTimes == 1);
16201616
NL_TEST_ASSERT(apSuite, !chip::isCommandDispatched);
16211617

16221618
NL_TEST_ASSERT(apSuite, GetNumActiveHandlerObjects() == 0);
16231619
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
16241620
}
16251621

1622+
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
1623+
16261624
void TestCommandInteraction::TestCommandHandlerRejectsMultipleCommandsWithIdenticalCommandRef(nlTestSuite * apSuite,
16271625
void * apContext)
16281626
{
16291627
TestContext & ctx = *static_cast<TestContext *>(apContext);
16301628
CHIP_ERROR err = CHIP_NO_ERROR;
16311629

16321630
isCommandDispatched = false;
1633-
mockCommandSenderDelegate.ResetCounter();
1631+
mockCommandSenderExtendedDelegate.ResetCounter();
1632+
PendingResponseTrackerImpl pendingResponseTracker;
1633+
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
1634+
&pendingResponseTracker);
16341635

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

1638-
size_t numberOfCommandsToSend = 2;
1640+
uint16_t numberOfCommandsToSend = 2;
16391641
{
16401642
CommandPathParams requestCommandPaths[] = {
16411643
MakeTestCommandPath(kTestCommandIdWithData),
16421644
MakeTestCommandPath(kTestCommandIdCommandSpecificResponse),
16431645
};
16441646

1645-
commandSender.AllocateBuffer();
1647+
uint16_t hardcodedCommandRef = 0;
16461648

1647-
// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
1648-
for (size_t i = 0; i < numberOfCommandsToSend; i++)
1649+
for (uint16_t i = 0; i < numberOfCommandsToSend; i++)
16491650
{
1650-
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
1651-
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
1652-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
1653-
CommandPathIB::Builder & path = invokeRequest.CreatePath();
1654-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
1655-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(requestCommandPaths[i]));
1651+
app::CommandSender::PrepareCommandParameters prepareCommandParams;
1652+
prepareCommandParams.SetStartDataStruct(true);
1653+
prepareCommandParams.SetCommandRef(i);
1654+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(requestCommandPaths[i], prepareCommandParams));
16561655
NL_TEST_ASSERT(apSuite,
1657-
CHIP_NO_ERROR ==
1658-
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
1659-
TLV::kTLVType_Structure,
1660-
commandSender.mDataElementContainerType));
1661-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1662-
NL_TEST_ASSERT(apSuite,
1663-
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
1664-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.Ref(1));
1665-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
1656+
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1657+
// TODO fix this comment
1658+
// We are taking advantage of the fact that the commandRef was set into finishCommandParams during PrepareCommand
1659+
// But setting it to a different value here, we are overriding what it was supposed to be.
1660+
app::CommandSender::FinishCommandParameters finishCommandParams;
1661+
finishCommandParams.SetEndDataStruct(true);
1662+
finishCommandParams.SetCommandRef(hardcodedCommandRef);
1663+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.TestOnlyFinishCommand(finishCommandParams));
16661664
}
1667-
1668-
commandSender.MoveToState(app::CommandSender::State::AddedCommand);
16691665
}
16701666

1671-
CommandHandler commandHandler(&mockCommandHandlerDelegate);
1667+
BasicCommandPathRegistry<4> mBasicCommandPathRegistry;
1668+
CommandHandler commandHandler(kCommandHandlerTestOnlyMarker, &mockCommandHandlerDelegate, &mBasicCommandPathRegistry);
16721669
TestExchangeDelegate delegate;
16731670
auto exchange = ctx.NewExchangeToAlice(&delegate, false);
16741671
commandHandler.mResponseSender.SetExchangeContext(exchange);
@@ -1696,53 +1693,48 @@ void TestCommandInteraction::TestCommandHandlerRejectsMultipleCommandsWithIdenti
16961693
exchange->Close();
16971694
}
16981695

1696+
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
1697+
16991698
void TestCommandInteraction::TestCommandHandlerRejectMultipleCommandsWhenHandlerOnlySupportsOne(nlTestSuite * apSuite,
17001699
void * apContext)
17011700
{
17021701
TestContext & ctx = *static_cast<TestContext *>(apContext);
17031702
CHIP_ERROR err = CHIP_NO_ERROR;
17041703

17051704
isCommandDispatched = false;
1706-
mockCommandSenderDelegate.ResetCounter();
17071705

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

1711-
size_t numberOfCommandsToSend = 2;
1711+
app::CommandSender::ConfigParameters configParameters;
1712+
configParameters.SetRemoteMaxPathsPerInvoke(2);
1713+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.SetCommandSenderConfig(configParameters));
1714+
1715+
uint16_t numberOfCommandsToSend = 2;
17121716
{
17131717
CommandPathParams requestCommandPaths[] = {
17141718
MakeTestCommandPath(kTestCommandIdWithData),
17151719
MakeTestCommandPath(kTestCommandIdCommandSpecificResponse),
17161720
};
17171721

1718-
commandSender.AllocateBuffer();
1719-
1720-
// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
1721-
for (size_t i = 0; i < numberOfCommandsToSend; i++)
1722+
for (uint16_t i = 0; i < numberOfCommandsToSend; i++)
17221723
{
1723-
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
1724-
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
1725-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
1726-
CommandPathIB::Builder & path = invokeRequest.CreatePath();
1727-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
1728-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(requestCommandPaths[i]));
1724+
app::CommandSender::PrepareCommandParameters prepareCommandParams;
1725+
prepareCommandParams.SetStartDataStruct(true);
1726+
prepareCommandParams.SetCommandRef(i);
1727+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(requestCommandPaths[i], prepareCommandParams));
17291728
NL_TEST_ASSERT(apSuite,
1730-
CHIP_NO_ERROR ==
1731-
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
1732-
TLV::kTLVType_Structure,
1733-
commandSender.mDataElementContainerType));
1734-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1735-
NL_TEST_ASSERT(apSuite,
1736-
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
1737-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.Ref(static_cast<uint16_t>(i)));
1738-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
1729+
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1730+
app::CommandSender::FinishCommandParameters finishCommandParams;
1731+
finishCommandParams.SetEndDataStruct(true);
1732+
finishCommandParams.SetCommandRef(i);
1733+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.FinishCommand(finishCommandParams));
17391734
}
1740-
1741-
commandSender.MoveToState(app::CommandSender::State::AddedCommand);
17421735
}
17431736

1744-
BasicCommandPathRegistry<4> mBasicCommandPathRegistry;
1745-
CommandHandler commandHandler(kCommandHandlerTestOnlyMarker, &mockCommandHandlerDelegate, &mBasicCommandPathRegistry);
1737+
CommandHandler commandHandler(&mockCommandHandlerDelegate);
17461738
TestExchangeDelegate delegate;
17471739
auto exchange = ctx.NewExchangeToAlice(&delegate, false);
17481740
commandHandler.mResponseSender.SetExchangeContext(exchange);
@@ -1757,9 +1749,9 @@ void TestCommandInteraction::TestCommandHandlerRejectMultipleCommandsWhenHandler
17571749
commandDispatchedCount = 0;
17581750

17591751
InteractionModel::Status status = commandHandler.ProcessInvokeRequest(std::move(commandDatabuf), false);
1760-
NL_TEST_ASSERT(apSuite, status == InteractionModel::Status::Success);
1752+
NL_TEST_ASSERT(apSuite, status == InteractionModel::Status::InvalidAction);
17611753

1762-
NL_TEST_ASSERT(apSuite, commandDispatchedCount == 2);
1754+
NL_TEST_ASSERT(apSuite, commandDispatchedCount == 0);
17631755

17641756
//
17651757
// Ordinarily, the ExchangeContext will close itself on a responder exchange when unwinding back from an
@@ -1777,39 +1769,35 @@ void TestCommandInteraction::TestCommandHandlerAcceptMultipleCommands(nlTestSuit
17771769
CHIP_ERROR err = CHIP_NO_ERROR;
17781770

17791771
isCommandDispatched = false;
1780-
mockCommandSenderDelegate.ResetCounter();
17811772

1782-
// Using commandSender to help build afterward we take the buffer to feed into standalone CommandHandler
1783-
app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager());
1773+
mockCommandSenderExtendedDelegate.ResetCounter();
1774+
PendingResponseTrackerImpl pendingResponseTracker;
1775+
app::CommandSender commandSender(kCommandSenderTestOnlyMarker, &mockCommandSenderExtendedDelegate, &ctx.GetExchangeManager(),
1776+
&pendingResponseTracker);
1777+
1778+
app::CommandSender::ConfigParameters configParameters;
1779+
configParameters.SetRemoteMaxPathsPerInvoke(2);
1780+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.SetCommandSenderConfig(configParameters));
17841781

1785-
size_t numberOfCommandsToSend = 2;
1782+
uint16_t numberOfCommandsToSend = 2;
17861783
{
17871784
CommandPathParams requestCommandPaths[] = {
17881785
MakeTestCommandPath(kTestCommandIdWithData),
17891786
MakeTestCommandPath(kTestCommandIdCommandSpecificResponse),
17901787
};
17911788

1792-
commandSender.AllocateBuffer();
1793-
1794-
// TODO(#30453): CommandSender does support sending multiple commands, update this test to use that.
1795-
for (size_t i = 0; i < numberOfCommandsToSend; i++)
1789+
for (uint16_t i = 0; i < numberOfCommandsToSend; i++)
17961790
{
1797-
InvokeRequests::Builder & invokeRequests = commandSender.mInvokeRequestBuilder.GetInvokeRequests();
1798-
CommandDataIB::Builder & invokeRequest = invokeRequests.CreateCommandData();
1799-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequests.GetError());
1800-
CommandPathIB::Builder & path = invokeRequest.CreatePath();
1801-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetError());
1802-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == path.Encode(requestCommandPaths[i]));
1803-
NL_TEST_ASSERT(apSuite,
1804-
CHIP_NO_ERROR ==
1805-
invokeRequest.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
1806-
TLV::kTLVType_Structure,
1807-
commandSender.mDataElementContainerType));
1808-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.GetWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1791+
app::CommandSender::PrepareCommandParameters prepareCommandParams;
1792+
prepareCommandParams.SetStartDataStruct(true);
1793+
prepareCommandParams.SetCommandRef(i);
1794+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.PrepareCommand(requestCommandPaths[i], prepareCommandParams));
18091795
NL_TEST_ASSERT(apSuite,
1810-
CHIP_NO_ERROR == invokeRequest.GetWriter()->EndContainer(commandSender.mDataElementContainerType));
1811-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.Ref(static_cast<uint16_t>(i)));
1812-
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == invokeRequest.EndOfCommandDataIB());
1796+
CHIP_NO_ERROR == commandSender.GetCommandDataIBTLVWriter()->PutBoolean(chip::TLV::ContextTag(1), true));
1797+
app::CommandSender::FinishCommandParameters finishCommandParams;
1798+
finishCommandParams.SetEndDataStruct(true);
1799+
finishCommandParams.SetCommandRef(i);
1800+
NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == commandSender.FinishCommand(finishCommandParams));
18131801
}
18141802

18151803
commandSender.MoveToState(app::CommandSender::State::AddedCommand);
@@ -1826,6 +1814,7 @@ void TestCommandInteraction::TestCommandHandlerAcceptMultipleCommands(nlTestSuit
18261814
err = commandSender.Finalize(commandDatabuf);
18271815
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
18281816

1817+
sendResponse = true;
18291818
mockCommandHandlerDelegate.ResetCounter();
18301819
commandDispatchedCount = 0;
18311820

@@ -1980,7 +1969,9 @@ const nlTest sTests[] =
19801969
NL_TEST_DEF("TestCommandHandlerWithProcessReceivedNotExistCommand", chip::app::TestCommandInteraction::TestCommandHandlerWithProcessReceivedNotExistCommand),
19811970
NL_TEST_DEF("TestCommandHandlerWithProcessReceivedEmptyDataMsg", chip::app::TestCommandInteraction::TestCommandHandlerWithProcessReceivedEmptyDataMsg),
19821971
NL_TEST_DEF("TestCommandHandlerRejectMultipleIdenticalCommands", chip::app::TestCommandInteraction::TestCommandHandlerRejectMultipleIdenticalCommands),
1972+
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
19831973
NL_TEST_DEF("TestCommandHandlerRejectsMultipleCommandsWithIdenticalCommandRef", chip::app::TestCommandInteraction::TestCommandHandlerRejectsMultipleCommandsWithIdenticalCommandRef),
1974+
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
19841975
NL_TEST_DEF("TestCommandHandlerRejectMultipleCommandsWhenHandlerOnlySupportsOne", chip::app::TestCommandInteraction::TestCommandHandlerRejectMultipleCommandsWhenHandlerOnlySupportsOne),
19851976
NL_TEST_DEF("TestCommandHandlerAcceptMultipleCommands", chip::app::TestCommandInteraction::TestCommandHandlerAcceptMultipleCommands),
19861977
NL_TEST_DEF("TestCommandHandler_FillUpInvokeResponseMessageWhereSecondResponseIsStatusResponse", chip::app::TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhereSecondResponseIsStatusResponse),

0 commit comments

Comments
 (0)