Skip to content

Commit

Permalink
Add a test that exercises receiving an invalid WriteRequest message.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Jan 13, 2025
1 parent 5bd63d5 commit 8b10aad
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

#include <app-common/zap-generated/cluster-objects.h>
#include <app/InteractionModelEngine.h>
#include <app/MessageDef/StatusResponseMessage.h>
#include <app/reporting/tests/MockReportScheduler.h>
#include <app/tests/AppTestContext.h>
#include <app/tests/test-interaction-model-api.h>
#include <credentials/GroupDataProviderImpl.h>
#include <crypto/CHIPCryptoPAL.h>
#include <crypto/DefaultSessionKeystore.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/ErrorStr.h>
Expand Down Expand Up @@ -85,6 +87,7 @@ class TestWriteInteraction : public chip::Test::AppContext
void TestWriteClient();
void TestWriteClientGroup();
void TestWriteHandlerReceiveInvalidMessage();
void TestWriteHandlerReceiveEmptyWriteRequest();
void TestWriteInvalidMessage1();
void TestWriteInvalidMessage2();
void TestWriteInvalidMessage3();
Expand All @@ -101,9 +104,30 @@ class TestWriteInteraction : public chip::Test::AppContext

class TestExchangeDelegate : public Messaging::ExchangeDelegate
{
public:
Protocols::InteractionModel::Status mLastStatus = Protocols::InteractionModel::Status::Success;
bool mLastStatusParsedSuccessfully = false;

private:
CHIP_ERROR UpdateLastStatus(System::PacketBufferHandle && payload)
{
StatusResponseMessage::Parser response;
System::PacketBufferTLVReader reader;
reader.Init(std::move(payload));
ReturnErrorOnFailure(response.Init(reader));
StatusIB status;
ReturnErrorOnFailure(response.GetStatus(mLastStatus));
return CHIP_NO_ERROR;
}

CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
System::PacketBufferHandle && payload) override
{
if (payloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
{
CHIP_ERROR err = UpdateLastStatus(std::move(payload));
mLastStatusParsedSuccessfully = (err == CHIP_NO_ERROR);
}
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -628,6 +652,52 @@ TEST_F(TestWriteInteraction, TestWriteHandlerInvalidateFabric)

#endif

// This test sends an invalid (because there is no application payload at all)
// Write Request message and makes sure that a correct Status Response is
// received.
TEST_F_FROM_FIXTURE(TestWriteInteraction, TestWriteHandlerReceiveEmptyWriteRequest)
{
Messaging::ReliableMessageMgr * rm = GetExchangeManager().GetReliableMessageMgr();
// Shouldn't have anything in the retransmit table when starting the test.
EXPECT_EQ(rm->TestGetCountRetransTable(), 0);

TestWriteClientCallback writeCallback;
auto * engine = chip::app::InteractionModelEngine::GetInstance();
EXPECT_EQ(engine->Init(&GetExchangeManager(), &GetFabricTable(), app::reporting::GetDefaultReportScheduler()), CHIP_NO_ERROR);

GetLoopback().mDroppedMessageCount = 0;
GetLoopback().mSentMessageCount = 0;
GetLoopback().mNumMessagesToDrop = 0;

// Just send an empty message claiming to be a write request.
System::PacketBufferHandle emptyMessage = System::PacketBufferHandle::New(Crypto::CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES);

TestExchangeDelegate delegate;
delegate.mLastStatusParsedSuccessfully = false;

auto exchange = NewExchangeToAlice(&delegate);
EXPECT_NE(exchange, nullptr);

EXPECT_EQ(exchange->SendMessage(Protocols::InteractionModel::MsgType::WriteRequest, std::move(emptyMessage),
Messaging::SendMessageFlags::kExpectResponse),
CHIP_NO_ERROR);

DrainAndServiceIO();

EXPECT_EQ(InteractionModelEngine::GetInstance()->GetNumActiveWriteHandlers(), 0u);
EXPECT_EQ(GetLoopback().mSentMessageCount, 3u); // Request, response, ack
EXPECT_EQ(GetLoopback().mDroppedMessageCount, 0u);

EXPECT_TRUE(delegate.mLastStatusParsedSuccessfully);
EXPECT_EQ(delegate.mLastStatus, Protocols::InteractionModel::Status::InvalidAction);

engine->Shutdown();
ExpireSessionAliceToBob();
ExpireSessionBobToAlice();
CreateSessionAliceToBob();
CreateSessionBobToAlice();
}

// Write Client sends a write request, receives an unexpected message type, sends a status response to that.
TEST_F_FROM_FIXTURE(TestWriteInteraction, TestWriteInvalidMessage1)
{
Expand Down

0 comments on commit 8b10aad

Please sign in to comment.