24
24
#include < messaging/ExchangeContext.h>
25
25
#include < messaging/ExchangeMgr.h>
26
26
#include < messaging/Flags.h>
27
+ #include < messaging/tests/MessagingContext.h>
27
28
#include < platform/CHIPDeviceLayer.h>
28
29
#include < protocols/secure_channel/MessageCounterManager.h>
29
30
#include < protocols/secure_channel/PASESession.h>
33
34
#include < system/TLVPacketBufferBackingStore.h>
34
35
#include < transport/SecureSessionMgr.h>
35
36
#include < transport/raw/UDP.h>
37
+ #include < transport/raw/tests/NetworkTestHelpers.h>
36
38
37
39
#include < nlunit-test.h>
38
40
39
41
namespace {
40
- chip::System::Layer gSystemLayer ;
41
- chip::SecureSessionMgr gSessionManager ;
42
- chip::Messaging::ExchangeManager gExchangeManager ;
43
- chip::TransportMgr<chip::Transport::UDP> gTransportManager ;
44
- chip::secure_channel::MessageCounterManager gMessageCounterManager ;
45
- chip::Transport::AdminId gAdminId = 0 ;
42
+ chip::TransportMgrBase gTransportManager ;
43
+ chip::Test::LoopbackTransport gLoopback ;
44
+
45
+ using TestContext = chip::Test::MessagingContext ;
46
+ TestContext sContext ;
47
+
46
48
} // namespace
47
49
namespace chip {
48
50
namespace app {
@@ -51,6 +53,7 @@ class TestWriteInteraction
51
53
public:
52
54
static void TestWriteClient (nlTestSuite * apSuite, void * apContext);
53
55
static void TestWriteHandler (nlTestSuite * apSuite, void * apContext);
56
+ static void TestWriteRoundtrip (nlTestSuite * apSuite, void * apContext);
54
57
55
58
private:
56
59
static void AddAttributeDataElement (nlTestSuite * apSuite, void * apContext, WriteClient & aWriteClient);
@@ -86,7 +89,7 @@ void TestWriteInteraction::AddAttributeDataElement(nlTestSuite * apSuite, void *
86
89
87
90
chip::TLV::TLVWriter * writer = aWriteClient.GetAttributeDataElementTLVWriter ();
88
91
89
- err = writer->PutBoolean (chip::TLV::ContextTag (1 ), true );
92
+ err = writer->PutBoolean (chip::TLV::ContextTag (chip::app::AttributeDataElement:: kCsTag_Data ), true );
90
93
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
91
94
92
95
err = aWriteClient.FinishAttribute ();
@@ -198,18 +201,21 @@ void TestWriteInteraction::GenerateWriteResponse(nlTestSuite * apSuite, void * a
198
201
199
202
void TestWriteInteraction::TestWriteClient (nlTestSuite * apSuite, void * apContext)
200
203
{
204
+ TestContext & ctx = *static_cast <TestContext *>(apContext);
205
+
201
206
CHIP_ERROR err = CHIP_NO_ERROR;
202
207
203
208
app::WriteClient writeClient;
204
209
205
210
chip::app::InteractionModelDelegate delegate;
206
211
System::PacketBufferHandle buf = System::PacketBufferHandle::New (System::PacketBuffer::kMaxSize );
207
- err = writeClient.Init (&gExchangeManager , &delegate);
212
+ err = writeClient.Init (&ctx. GetExchangeManager () , &delegate);
208
213
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
209
214
AddAttributeDataElement (apSuite, apContext, writeClient);
210
215
211
- err = writeClient.SendWriteRequest (kTestDeviceNodeId , gAdminId , nullptr );
212
- NL_TEST_ASSERT (apSuite, err == CHIP_ERROR_NOT_CONNECTED);
216
+ SecureSessionHandle session = ctx.GetSessionLocalToPeer ();
217
+ err = writeClient.SendWriteRequest (ctx.GetDestinationNodeId (), ctx.GetAdminId (), &session);
218
+ NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
213
219
214
220
GenerateWriteResponse (apSuite, apContext, buf);
215
221
@@ -221,6 +227,8 @@ void TestWriteInteraction::TestWriteClient(nlTestSuite * apSuite, void * apConte
221
227
222
228
void TestWriteInteraction::TestWriteHandler (nlTestSuite * apSuite, void * apContext)
223
229
{
230
+ TestContext & ctx = *static_cast <TestContext *>(apContext);
231
+
224
232
CHIP_ERROR err = CHIP_NO_ERROR;
225
233
226
234
app::WriteHandler writeHandler;
@@ -236,43 +244,72 @@ void TestWriteInteraction::TestWriteHandler(nlTestSuite * apSuite, void * apCont
236
244
237
245
AddAttributeStatus (apSuite, apContext, writeHandler);
238
246
239
- writeHandler.mpExchangeCtx = gExchangeManager .NewContext ({ 0 , 0 , 0 }, nullptr );
240
247
TestExchangeDelegate delegate;
241
- writeHandler.mpExchangeCtx -> SetDelegate (&delegate);
242
- err = writeHandler.SendWriteResponse ();
243
- NL_TEST_ASSERT (apSuite, err == CHIP_ERROR_NOT_CONNECTED );
248
+ writeHandler.mpExchangeCtx = ctx. NewExchangeToLocal (&delegate);
249
+ err = writeHandler.SendWriteResponse ();
250
+ NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR );
244
251
245
252
writeHandler.Shutdown ();
246
253
}
247
- } // namespace app
248
- } // namespace chip
249
254
250
- namespace {
255
+ CHIP_ERROR WriteSingleClusterData (ClusterInfo & aClusterInfo, TLV::TLVReader & aReader, WriteHandler * aWriteHandler)
256
+ {
257
+ return aWriteHandler->AddAttributeStatusCode (
258
+ AttributePathParams (aClusterInfo.mNodeId , aClusterInfo.mEndpointId , aClusterInfo.mClusterId , aClusterInfo.mFieldId ,
259
+ aClusterInfo.mListIndex , AttributePathParams::Flags::kFieldIdValid ),
260
+ Protocols::SecureChannel::GeneralStatusCode::kSuccess , Protocols::SecureChannel::Id,
261
+ Protocols::InteractionModel::ProtocolCode::Success);
262
+ }
251
263
252
- void InitializeChip (nlTestSuite * apSuite)
264
+ class RoundtripDelegate : public chip ::app::InteractionModelDelegate
253
265
{
254
- CHIP_ERROR err = CHIP_NO_ERROR;
255
- chip::Optional<chip::Transport::PeerAddress> peer (chip::Transport::Type::kUndefined );
256
- chip::Transport::AdminPairingTable admins;
257
- chip::Transport::AdminPairingInfo * adminInfo = admins.AssignAdminId (gAdminId , chip::kTestDeviceNodeId );
266
+ public:
267
+ CHIP_ERROR WriteResponseStatus (const WriteClient * apWriteClient,
268
+ const Protocols::SecureChannel::GeneralStatusCode aGeneralCode, const uint32_t aProtocolId,
269
+ const uint16_t aProtocolCode, AttributePathParams & aAttributePathParams,
270
+ uint8_t aCommandIndex) override
271
+ {
272
+ mGotResponse = true ;
273
+ return CHIP_NO_ERROR;
274
+ }
258
275
259
- NL_TEST_ASSERT (apSuite, adminInfo != nullptr );
276
+ bool mGotResponse = false ;
277
+ };
260
278
261
- err = chip::Platform::MemoryInit ();
262
- NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
279
+ void TestWriteInteraction::TestWriteRoundtrip (nlTestSuite * apSuite, void * apContext)
280
+ {
281
+ TestContext & ctx = *static_cast <TestContext *>(apContext);
263
282
264
- gSystemLayer . Init ( nullptr ) ;
283
+ CHIP_ERROR err = CHIP_NO_ERROR ;
265
284
266
- err = gSessionManager .Init (chip::kTestDeviceNodeId , &gSystemLayer , &gTransportManager , &admins, &gMessageCounterManager );
285
+ RoundtripDelegate delegate;
286
+ auto * engine = chip::app::InteractionModelEngine::GetInstance ();
287
+ err = engine->Init (&ctx.GetExchangeManager (), &delegate);
267
288
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
268
289
269
- err = gExchangeManager .Init (&gSessionManager );
290
+ app::WriteClient * writeClient;
291
+ err = engine->NewWriteClient (&writeClient);
270
292
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
271
293
272
- err = gMessageCounterManager .Init (&gExchangeManager );
294
+ System::PacketBufferHandle buf = System::PacketBufferHandle::New (System::PacketBuffer::kMaxSize );
295
+ AddAttributeDataElement (apSuite, apContext, *writeClient);
296
+
297
+ NL_TEST_ASSERT (apSuite, !delegate.mGotResponse );
298
+
299
+ SecureSessionHandle session = ctx.GetSessionLocalToPeer ();
300
+ err = writeClient->SendWriteRequest (ctx.GetDestinationNodeId (), ctx.GetAdminId (), &session);
273
301
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
302
+
303
+ NL_TEST_ASSERT (apSuite, delegate.mGotResponse );
304
+
305
+ engine->Shutdown ();
274
306
}
275
307
308
+ } // namespace app
309
+ } // namespace chip
310
+
311
+ namespace {
312
+
276
313
/* *
277
314
* Test Suite. It lists all the test functions.
278
315
*/
@@ -282,28 +319,59 @@ const nlTest sTests[] =
282
319
{
283
320
NL_TEST_DEF (" CheckWriteClient" , chip::app::TestWriteInteraction::TestWriteClient),
284
321
NL_TEST_DEF (" CheckWriteHandler" , chip::app::TestWriteInteraction::TestWriteHandler),
322
+ NL_TEST_DEF (" CheckWriteRoundtrip" , chip::app::TestWriteInteraction::TestWriteRoundtrip),
285
323
NL_TEST_SENTINEL ()
286
324
};
287
325
// clang-format on
288
- } // namespace
289
326
290
- int TestWriteInteraction ()
327
+ int Initialize (void * aContext);
328
+ int Finalize (void * aContext);
329
+
330
+ // clang-format off
331
+ nlTestSuite sSuite =
332
+ {
333
+ " TestWriteInteraction" ,
334
+ &sTests [0 ],
335
+ Initialize,
336
+ Finalize
337
+ };
338
+ // clang-format on
339
+
340
+ int Initialize (void * aContext)
341
+ {
342
+ CHIP_ERROR err = chip::Platform::MemoryInit ();
343
+ if (err != CHIP_NO_ERROR)
344
+ {
345
+ return FAILURE;
346
+ }
347
+
348
+ gTransportManager .Init (&gLoopback );
349
+
350
+ auto * ctx = static_cast <TestContext *>(aContext);
351
+ err = ctx->Init (&sSuite , &gTransportManager );
352
+ if (err != CHIP_NO_ERROR)
353
+ {
354
+ return FAILURE;
355
+ }
356
+
357
+ gTransportManager .SetSecureSessionMgr (&ctx->GetSecureSessionManager ());
358
+ return SUCCESS;
359
+ }
360
+
361
+ int Finalize (void * aContext)
291
362
{
292
- // clang-format off
293
- nlTestSuite theSuite =
294
- {
295
- " TestWriteInteraction" ,
296
- &sTests [0 ],
297
- nullptr ,
298
- nullptr
299
- };
300
- // clang-format on
363
+ CHIP_ERROR err = reinterpret_cast <TestContext *>(aContext)->Shutdown ();
364
+ chip::Platform::MemoryShutdown ();
365
+ return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE;
366
+ }
301
367
302
- InitializeChip (&theSuite);
368
+ } // namespace
303
369
304
- nlTestRunner (&theSuite, nullptr );
370
+ int TestWriteInteraction ()
371
+ {
372
+ nlTestRunner (&sSuite , &sContext );
305
373
306
- return (nlTestRunnerStats (&theSuite ));
374
+ return (nlTestRunnerStats (&sSuite ));
307
375
}
308
376
309
377
CHIP_REGISTER_TEST_SUITE (TestWriteInteraction)
0 commit comments