3 * Copyright (c) 2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements unit tests for CHIP Interaction Model Command Interaction
25 #include <app/InteractionModelEngine.h>
26 #include <core/CHIPCore.h>
27 #include <core/CHIPTLV.h>
28 #include <core/CHIPTLVDebug.hpp>
29 #include <core/CHIPTLVUtilities.hpp>
30 #include <messaging/ExchangeContext.h>
31 #include <messaging/ExchangeMgr.h>
32 #include <messaging/Flags.h>
33 #include <platform/CHIPDeviceLayer.h>
34 #include <support/ErrorStr.h>
35 #include <support/UnitTestRegistration.h>
36 #include <system/SystemPacketBuffer.h>
37 #include <system/TLVPacketBufferBackingStore.h>
38 #include <transport/PASESession.h>
39 #include <transport/SecureSessionMgr.h>
40 #include <transport/raw/UDP.h>
42 #include <nlunit-test.h>
45 static System::Layer gSystemLayer;
46 static SecureSessionMgr gSessionManager;
47 static Messaging::ExchangeManager gExchangeManager;
48 static TransportMgr<Transport::UDP> gTransportManager;
49 static Transport::AdminId gAdminId = 0;
52 class TestCommandInteraction
55 static void TestCommandSender(nlTestSuite * apSuite, void * apContext);
56 static void TestCommandHandler(nlTestSuite * apSuite, void * apContext);
59 static void GenerateCommandData(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload);
62 void TestCommandInteraction::GenerateCommandData(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload)
64 CHIP_ERROR err = CHIP_NO_ERROR;
65 InvokeCommand::Builder invokeCommandBuilder;
66 System::PacketBufferTLVWriter writer;
67 writer.Init(std::move(aPayload));
69 err = invokeCommandBuilder.Init(&writer);
70 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
72 CommandList::Builder commandList = invokeCommandBuilder.CreateCommandListBuilder();
73 NL_TEST_ASSERT(apSuite, invokeCommandBuilder.GetError() == CHIP_NO_ERROR);
75 CommandDataElement::Builder commandDataElementBuilder = commandList.CreateCommandDataElementBuilder();
76 NL_TEST_ASSERT(apSuite, commandList.GetError() == CHIP_NO_ERROR);
77 CommandPath::Builder commandPathBuilder = commandDataElementBuilder.CreateCommandPathBuilder();
78 NL_TEST_ASSERT(apSuite, commandDataElementBuilder.GetError() == CHIP_NO_ERROR);
79 commandPathBuilder.EndpointId(1).ClusterId(3).CommandId(4).EndOfCommandPath();
80 NL_TEST_ASSERT(apSuite, commandPathBuilder.GetError() == CHIP_NO_ERROR);
82 chip::TLV::TLVWriter * pWriter = commandDataElementBuilder.GetWriter();
83 chip::TLV::TLVType dummyType = chip::TLV::kTLVType_NotSpecified;
84 err = pWriter->StartContainer(chip::TLV::ContextTag(CommandDataElement::kCsTag_Data), chip::TLV::kTLVType_Structure, dummyType);
85 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
87 err = pWriter->PutBoolean(chip::TLV::ContextTag(1), true);
88 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
90 err = pWriter->EndContainer(dummyType);
91 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
93 commandDataElementBuilder.EndOfCommandDataElement();
94 NL_TEST_ASSERT(apSuite, commandDataElementBuilder.GetError() == CHIP_NO_ERROR);
96 commandList.EndOfCommandList();
97 NL_TEST_ASSERT(apSuite, commandList.GetError() == CHIP_NO_ERROR);
99 invokeCommandBuilder.EndOfInvokeCommand();
100 NL_TEST_ASSERT(apSuite, invokeCommandBuilder.GetError() == CHIP_NO_ERROR);
102 err = writer.Finalize(&aPayload);
103 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
106 void TestCommandInteraction::TestCommandSender(nlTestSuite * apSuite, void * apContext)
108 CHIP_ERROR err = CHIP_NO_ERROR;
110 app::CommandSender commandSender;
112 System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
113 err = commandSender.Init(&gExchangeManager, nullptr);
114 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
116 err = commandSender.SendCommandRequest(kTestDeviceNodeId, gAdminId);
117 NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NOT_CONNECTED);
119 GenerateCommandData(apSuite, apContext, buf);
121 err = commandSender.ProcessCommandMessage(std::move(buf), Command::CommandRoleId::SenderId);
122 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
124 commandSender.Shutdown();
127 void TestCommandInteraction::TestCommandHandler(nlTestSuite * apSuite, void * apContext)
129 CHIP_ERROR err = CHIP_NO_ERROR;
130 app::CommandHandler commandHandler;
131 System::PacketBufferTLVWriter writer;
132 System::PacketBufferHandle commandDatabuf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
133 err = commandHandler.Init(&chip::gExchangeManager, nullptr);
134 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
136 GenerateCommandData(apSuite, apContext, commandDatabuf);
138 err = commandHandler.ProcessCommandMessage(std::move(commandDatabuf), Command::CommandRoleId::HandlerId);
139 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
147 void InitializeChip(nlTestSuite * apSuite)
149 CHIP_ERROR err = CHIP_NO_ERROR;
150 chip::Optional<chip::Transport::PeerAddress> peer(chip::Transport::Type::kUndefined);
151 chip::Transport::AdminPairingTable admins;
152 chip::Transport::AdminPairingInfo * adminInfo = admins.AssignAdminId(chip::gAdminId, chip::kTestDeviceNodeId);
154 NL_TEST_ASSERT(apSuite, adminInfo != nullptr);
156 err = chip::Platform::MemoryInit();
157 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
159 chip::gSystemLayer.Init(nullptr);
161 err = chip::gSessionManager.Init(chip::kTestDeviceNodeId, &chip::gSystemLayer, &chip::gTransportManager, &admins);
162 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
164 err = chip::gExchangeManager.Init(&chip::gSessionManager);
165 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
167 err = chip::app::InteractionModelEngine::GetInstance()->Init(&chip::gExchangeManager, nullptr);
168 NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
172 const nlTest sTests[] =
174 NL_TEST_DEF("CheckCommandSender", chip::app::TestCommandInteraction::TestCommandSender),
175 NL_TEST_DEF("CheckCommandHandler", chip::app::TestCommandInteraction::TestCommandHandler),
182 int TestCommandInteraction()
185 nlTestSuite theSuite =
187 "CommandInteraction",
194 InitializeChip(&theSuite);
196 nlTestRunner(&theSuite, nullptr);
198 return (nlTestRunnerStats(&theSuite));
201 CHIP_REGISTER_TEST_SUITE(TestCommandInteraction)