Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / tests / TestCommandInteraction.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file implements unit tests for CHIP Interaction Model Command Interaction
22  *
23  */
24
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>
41
42 #include <nlunit-test.h>
43
44 namespace chip {
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;
50
51 namespace app {
52 class TestCommandInteraction
53 {
54 public:
55     static void TestCommandSender(nlTestSuite * apSuite, void * apContext);
56     static void TestCommandHandler(nlTestSuite * apSuite, void * apContext);
57
58 private:
59     static void GenerateCommandData(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload);
60 };
61
62 void TestCommandInteraction::GenerateCommandData(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload)
63 {
64     CHIP_ERROR err = CHIP_NO_ERROR;
65     InvokeCommand::Builder invokeCommandBuilder;
66     System::PacketBufferTLVWriter writer;
67     writer.Init(std::move(aPayload));
68
69     err = invokeCommandBuilder.Init(&writer);
70     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
71
72     CommandList::Builder commandList = invokeCommandBuilder.CreateCommandListBuilder();
73     NL_TEST_ASSERT(apSuite, invokeCommandBuilder.GetError() == CHIP_NO_ERROR);
74
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);
81
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);
86
87     err = pWriter->PutBoolean(chip::TLV::ContextTag(1), true);
88     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
89
90     err = pWriter->EndContainer(dummyType);
91     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
92
93     commandDataElementBuilder.EndOfCommandDataElement();
94     NL_TEST_ASSERT(apSuite, commandDataElementBuilder.GetError() == CHIP_NO_ERROR);
95
96     commandList.EndOfCommandList();
97     NL_TEST_ASSERT(apSuite, commandList.GetError() == CHIP_NO_ERROR);
98
99     invokeCommandBuilder.EndOfInvokeCommand();
100     NL_TEST_ASSERT(apSuite, invokeCommandBuilder.GetError() == CHIP_NO_ERROR);
101
102     err = writer.Finalize(&aPayload);
103     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
104 }
105
106 void TestCommandInteraction::TestCommandSender(nlTestSuite * apSuite, void * apContext)
107 {
108     CHIP_ERROR err = CHIP_NO_ERROR;
109
110     app::CommandSender commandSender;
111
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);
115
116     err = commandSender.SendCommandRequest(kTestDeviceNodeId, gAdminId);
117     NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NOT_CONNECTED);
118
119     GenerateCommandData(apSuite, apContext, buf);
120
121     err = commandSender.ProcessCommandMessage(std::move(buf), Command::CommandRoleId::SenderId);
122     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
123
124     commandSender.Shutdown();
125 }
126
127 void TestCommandInteraction::TestCommandHandler(nlTestSuite * apSuite, void * apContext)
128 {
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);
135
136     GenerateCommandData(apSuite, apContext, commandDatabuf);
137
138     err = commandHandler.ProcessCommandMessage(std::move(commandDatabuf), Command::CommandRoleId::HandlerId);
139     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
140 }
141
142 } // namespace app
143 } // namespace chip
144
145 namespace {
146
147 void InitializeChip(nlTestSuite * apSuite)
148 {
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);
153
154     NL_TEST_ASSERT(apSuite, adminInfo != nullptr);
155
156     err = chip::Platform::MemoryInit();
157     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
158
159     chip::gSystemLayer.Init(nullptr);
160
161     err = chip::gSessionManager.Init(chip::kTestDeviceNodeId, &chip::gSystemLayer, &chip::gTransportManager, &admins);
162     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
163
164     err = chip::gExchangeManager.Init(&chip::gSessionManager);
165     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
166
167     err = chip::app::InteractionModelEngine::GetInstance()->Init(&chip::gExchangeManager, nullptr);
168     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
169 }
170
171 // clang-format off
172 const nlTest sTests[] =
173 {
174     NL_TEST_DEF("CheckCommandSender", chip::app::TestCommandInteraction::TestCommandSender),
175     NL_TEST_DEF("CheckCommandHandler", chip::app::TestCommandInteraction::TestCommandHandler),
176     NL_TEST_SENTINEL()
177 };
178 // clang-format on
179
180 } // namespace
181
182 int TestCommandInteraction()
183 {
184     // clang-format off
185     nlTestSuite theSuite =
186         {
187         "CommandInteraction",
188         &sTests[0],
189         nullptr,
190         nullptr
191     };
192     // clang-format on
193
194     InitializeChip(&theSuite);
195
196     nlTestRunner(&theSuite, nullptr);
197
198     return (nlTestRunnerStats(&theSuite));
199 }
200
201 CHIP_REGISTER_TEST_SUITE(TestCommandInteraction)