Skip to content

Commit 1296223

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Add write roundtrip test (#8169)
* Refactor TestWriteInteraction to use a MessagingContext. This allows us to test more details of the messages being sent. * Add a roundtrip test to TestWriteInteraction. Various fixups needed for that: 1) Make InteractionModelEngine::Shutdown actually remove the object as an unsolicited message handler. 2) Initialize WriteHandler's state properly so it's not random whether we think we have available WriteHandlers. 3) Fix TestWriteInteraction::AddAttributeDataElement to use the right tag (2, not 1) for its data. 4) Stop trying to run AppTests in QEMU, because of linking/compilation issues in that setup.
1 parent 7bf624c commit 1296223

File tree

5 files changed

+128
-48
lines changed

5 files changed

+128
-48
lines changed

scripts/tests/esp32_qemu_tests.sh

+12-4
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ if [ $? -ne 0 ]; then
4242
fi
4343

4444
really_run_suite() {
45-
idf scripts/tools/qemu_run_test.sh src/test_driver/esp32/build/chip "$1" "$2"
45+
idf scripts/tools/qemu_run_test.sh src/test_driver/esp32/build/chip "$@"
4646
}
4747

4848
run_suite() {
4949
if [[ -d "${log_dir}" ]]; then
5050
suite=${1%.a}
5151
suite=${suite#lib}
52-
really_run_suite "$1" "$2" |& tee "$log_dir/$suite.log"
52+
really_run_suite "$@" |& tee "$log_dir/$suite.log"
5353
else
54-
really_run_suite "$1" "$2"
54+
really_run_suite "$@"
5555
fi
5656
}
5757

@@ -61,7 +61,15 @@ run_suite() {
6161
SUITES=(
6262
)
6363

64-
run_suite libAppTests.a
64+
# TODO: libAppTests depends on MessagingTestHelpers, which depends on
65+
# NetworkTestHelpers. That sort of depends on InetTestHelpers or
66+
# equivalent (to provide gSystemLayer, gInet, InitNetwork(),
67+
# ShutdownNetwork()) but there's only a POSIX implementation of that
68+
# last, which does not compile on ESP32. Need to figure out how to
69+
# make that work. See comments below for the transport layer tests,
70+
# which have the same issue.
71+
# run_suite libAppTests.a -lMessagingTestHelpers -lNetworkTestHelpers
72+
6573
run_suite libASN1Tests.a
6674
run_suite libBleLayerTests.a
6775
run_suite libCoreTests.a

src/app/InteractionModelEngine.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ void InteractionModelEngine::Shutdown()
116116
}
117117

118118
mpNextAvailableClusterInfo = nullptr;
119+
120+
mpExchangeMgr->UnregisterUnsolicitedMessageHandlerForProtocol(Protocols::InteractionModel::Id);
119121
}
120122

121123
CHIP_ERROR InteractionModelEngine::NewCommandSender(CommandSender ** const apCommandSender)

src/app/WriteHandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class WriteHandler
106106
InteractionModelDelegate * mpDelegate = nullptr;
107107
WriteResponse::Builder mWriteResponseBuilder;
108108
System::PacketBufferTLVWriter mMessageWriter;
109-
State mState;
109+
State mState = State::Uninitialized;
110110
};
111111
} // namespace app
112112
} // namespace chip

src/app/tests/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ chip_test_suite("tests") {
4141
"${chip_root}/src/app",
4242
"${chip_root}/src/app/util:device_callbacks_manager",
4343
"${chip_root}/src/lib/core",
44+
"${chip_root}/src/messaging/tests:helpers",
4445
"${chip_root}/src/protocols",
46+
"${chip_root}/src/transport/raw/tests:helpers",
4547
"${nlunit_test_root}:nlunit-test",
4648
]
4749
}

src/app/tests/TestWriteInteraction.cpp

+111-43
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <messaging/ExchangeContext.h>
2525
#include <messaging/ExchangeMgr.h>
2626
#include <messaging/Flags.h>
27+
#include <messaging/tests/MessagingContext.h>
2728
#include <platform/CHIPDeviceLayer.h>
2829
#include <protocols/secure_channel/MessageCounterManager.h>
2930
#include <protocols/secure_channel/PASESession.h>
@@ -33,16 +34,17 @@
3334
#include <system/TLVPacketBufferBackingStore.h>
3435
#include <transport/SecureSessionMgr.h>
3536
#include <transport/raw/UDP.h>
37+
#include <transport/raw/tests/NetworkTestHelpers.h>
3638

3739
#include <nlunit-test.h>
3840

3941
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+
4648
} // namespace
4749
namespace chip {
4850
namespace app {
@@ -51,6 +53,7 @@ class TestWriteInteraction
5153
public:
5254
static void TestWriteClient(nlTestSuite * apSuite, void * apContext);
5355
static void TestWriteHandler(nlTestSuite * apSuite, void * apContext);
56+
static void TestWriteRoundtrip(nlTestSuite * apSuite, void * apContext);
5457

5558
private:
5659
static void AddAttributeDataElement(nlTestSuite * apSuite, void * apContext, WriteClient & aWriteClient);
@@ -86,7 +89,7 @@ void TestWriteInteraction::AddAttributeDataElement(nlTestSuite * apSuite, void *
8689

8790
chip::TLV::TLVWriter * writer = aWriteClient.GetAttributeDataElementTLVWriter();
8891

89-
err = writer->PutBoolean(chip::TLV::ContextTag(1), true);
92+
err = writer->PutBoolean(chip::TLV::ContextTag(chip::app::AttributeDataElement::kCsTag_Data), true);
9093
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
9194

9295
err = aWriteClient.FinishAttribute();
@@ -198,18 +201,21 @@ void TestWriteInteraction::GenerateWriteResponse(nlTestSuite * apSuite, void * a
198201

199202
void TestWriteInteraction::TestWriteClient(nlTestSuite * apSuite, void * apContext)
200203
{
204+
TestContext & ctx = *static_cast<TestContext *>(apContext);
205+
201206
CHIP_ERROR err = CHIP_NO_ERROR;
202207

203208
app::WriteClient writeClient;
204209

205210
chip::app::InteractionModelDelegate delegate;
206211
System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
207-
err = writeClient.Init(&gExchangeManager, &delegate);
212+
err = writeClient.Init(&ctx.GetExchangeManager(), &delegate);
208213
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
209214
AddAttributeDataElement(apSuite, apContext, writeClient);
210215

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);
213219

214220
GenerateWriteResponse(apSuite, apContext, buf);
215221

@@ -221,6 +227,8 @@ void TestWriteInteraction::TestWriteClient(nlTestSuite * apSuite, void * apConte
221227

222228
void TestWriteInteraction::TestWriteHandler(nlTestSuite * apSuite, void * apContext)
223229
{
230+
TestContext & ctx = *static_cast<TestContext *>(apContext);
231+
224232
CHIP_ERROR err = CHIP_NO_ERROR;
225233

226234
app::WriteHandler writeHandler;
@@ -236,43 +244,72 @@ void TestWriteInteraction::TestWriteHandler(nlTestSuite * apSuite, void * apCont
236244

237245
AddAttributeStatus(apSuite, apContext, writeHandler);
238246

239-
writeHandler.mpExchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr);
240247
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);
244251

245252
writeHandler.Shutdown();
246253
}
247-
} // namespace app
248-
} // namespace chip
249254

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+
}
251263

252-
void InitializeChip(nlTestSuite * apSuite)
264+
class RoundtripDelegate : public chip::app::InteractionModelDelegate
253265
{
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+
}
258275

259-
NL_TEST_ASSERT(apSuite, adminInfo != nullptr);
276+
bool mGotResponse = false;
277+
};
260278

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);
263282

264-
gSystemLayer.Init(nullptr);
283+
CHIP_ERROR err = CHIP_NO_ERROR;
265284

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);
267288
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
268289

269-
err = gExchangeManager.Init(&gSessionManager);
290+
app::WriteClient * writeClient;
291+
err = engine->NewWriteClient(&writeClient);
270292
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
271293

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);
273301
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
302+
303+
NL_TEST_ASSERT(apSuite, delegate.mGotResponse);
304+
305+
engine->Shutdown();
274306
}
275307

308+
} // namespace app
309+
} // namespace chip
310+
311+
namespace {
312+
276313
/**
277314
* Test Suite. It lists all the test functions.
278315
*/
@@ -282,28 +319,59 @@ const nlTest sTests[] =
282319
{
283320
NL_TEST_DEF("CheckWriteClient", chip::app::TestWriteInteraction::TestWriteClient),
284321
NL_TEST_DEF("CheckWriteHandler", chip::app::TestWriteInteraction::TestWriteHandler),
322+
NL_TEST_DEF("CheckWriteRoundtrip", chip::app::TestWriteInteraction::TestWriteRoundtrip),
285323
NL_TEST_SENTINEL()
286324
};
287325
// clang-format on
288-
} // namespace
289326

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)
291362
{
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+
}
301367

302-
InitializeChip(&theSuite);
368+
} // namespace
303369

304-
nlTestRunner(&theSuite, nullptr);
370+
int TestWriteInteraction()
371+
{
372+
nlTestRunner(&sSuite, &sContext);
305373

306-
return (nlTestRunnerStats(&theSuite));
374+
return (nlTestRunnerStats(&sSuite));
307375
}
308376

309377
CHIP_REGISTER_TEST_SUITE(TestWriteInteraction)

0 commit comments

Comments
 (0)