Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test that exercises receiving an invalid WriteRequest message. #37053

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Maybe some kind of followup (for all test cases) should be here to move that part to test SetUp/TearDown?

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
Loading