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

Have a single loopback transport implementation for tests. #8045

Merged
Show file tree
Hide file tree
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
18 changes: 2 additions & 16 deletions src/channel/tests/TestChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <support/CodeUtils.h>
#include <transport/SecureSessionMgr.h>
#include <transport/TransportMgr.h>
#include <transport/raw/tests/NetworkTestHelpers.h>

#include <nlbyteorder.h>
#include <nlunit-test.h>
Expand All @@ -51,22 +52,7 @@ using TestContext = chip::Test::MessagingContext;

TestContext sContext;

class LoopbackTransport : public Transport::Base
{
public:
/// Transports are required to have a constructor that takes exactly one argument
CHIP_ERROR Init(const char * unused) { return CHIP_NO_ERROR; }

CHIP_ERROR SendMessage(const PacketHeader & header, const PeerAddress & address, System::PacketBufferHandle && msgBuf) override
{
HandleMessageReceived(header, address, std::move(msgBuf));
return CHIP_NO_ERROR;
}

bool CanSendToPeer(const PeerAddress & address) override { return true; }
};

TransportMgr<LoopbackTransport> gTransportMgr;
TransportMgr<Test::LoopbackTransport> gTransportMgr;

class MockAppDelegate : public ExchangeDelegate
{
Expand Down
18 changes: 2 additions & 16 deletions src/messaging/tests/TestExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <support/CodeUtils.h>
#include <transport/SecureSessionMgr.h>
#include <transport/TransportMgr.h>
#include <transport/raw/tests/NetworkTestHelpers.h>

#include <nlbyteorder.h>
#include <nlunit-test.h>
Expand All @@ -57,22 +58,7 @@ enum : uint8_t

TestContext sContext;

class LoopbackTransport : public Transport::Base
{
public:
/// Transports are required to have a constructor that takes exactly one argument
CHIP_ERROR Init(const char * unused) { return CHIP_NO_ERROR; }

CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) override
{
HandleMessageReceived(address, std::move(msgBuf));
return CHIP_NO_ERROR;
}

bool CanSendToPeer(const PeerAddress & address) override { return true; }
};

TransportMgr<LoopbackTransport> gTransportMgr;
TransportMgr<Test::LoopbackTransport> gTransportMgr;

class MockAppDelegate : public ExchangeDelegate
{
Expand Down
83 changes: 28 additions & 55 deletions src/messaging/tests/TestReliableMessageProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <messaging/ExchangeMgr.h>
#include <messaging/Flags.h>
#include <messaging/tests/MessagingContext.h>
#include <transport/raw/tests/NetworkTestHelpers.h>

namespace {

Expand All @@ -57,36 +58,8 @@ TestContext sContext;

const char PAYLOAD[] = "Hello!";

class OutgoingTransport : public Transport::Base
{
public:
CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) override
{
mSendMessageCount++;

if (mNumMessagesToDrop == 0)
{
System::PacketBufferHandle receivedMessage = msgBuf.CloneData();
HandleMessageReceived(address, std::move(receivedMessage));
}
else
{
mNumMessagesToDrop--;
mDroppedMessageCount++;
}

return CHIP_NO_ERROR;
}

bool CanSendToPeer(const PeerAddress & address) override { return true; }

uint32_t mNumMessagesToDrop = 0;
uint32_t mDroppedMessageCount = 0;
uint32_t mSendMessageCount = 0;
};

TransportMgrBase gTransportMgr;
OutgoingTransport gLoopback;
Test::LoopbackTransport gLoopback;

class MockAppDelegate : public ExchangeDelegate
{
Expand Down Expand Up @@ -288,7 +261,7 @@ void CheckResendApplicationMessage(nlTestSuite * inSuite, void * inContext)
});

// Let's drop the initial message
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 2;
gLoopback.mDroppedMessageCount = 0;

Expand All @@ -308,7 +281,7 @@ void CheckResendApplicationMessage(nlTestSuite * inSuite, void * inContext)
ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_NO_ERROR);

// Ensure the retransmit message was dropped, and is still there in the retransmit table
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 0);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1);
Expand All @@ -318,7 +291,7 @@ void CheckResendApplicationMessage(nlTestSuite * inSuite, void * inContext)
ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_NO_ERROR);

// Ensure the retransmit message was NOT dropped, and the retransmit table is empty, as we should have gotten an ack
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 3);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount >= 3);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0);

Expand Down Expand Up @@ -353,7 +326,7 @@ void CheckCloseExchangeAndResendApplicationMessage(nlTestSuite * inSuite, void *
});

// Let's drop the initial message
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 2;
gLoopback.mDroppedMessageCount = 0;

Expand All @@ -374,7 +347,7 @@ void CheckCloseExchangeAndResendApplicationMessage(nlTestSuite * inSuite, void *
ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_NO_ERROR);

// Ensure the retransmit message was dropped, and is still there in the retransmit table
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 0);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1);
Expand All @@ -384,7 +357,7 @@ void CheckCloseExchangeAndResendApplicationMessage(nlTestSuite * inSuite, void *
ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_NO_ERROR);

// Ensure the retransmit message was NOT dropped, and the retransmit table is empty, as we should have gotten an ack
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 3);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount >= 3);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0);

Expand Down Expand Up @@ -422,7 +395,7 @@ void CheckFailedMessageRetainOnSend(nlTestSuite * inSuite, void * inContext)
mockSender.mMessageDispatch.mRetainMessageOnSend = false;

// Let's drop the initial message
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 1;
gLoopback.mDroppedMessageCount = 0;

Expand Down Expand Up @@ -478,7 +451,7 @@ void CheckResendApplicationMessageWithPeerExchange(nlTestSuite * inSuite, void *
});

// Let's drop the initial message
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 1;
gLoopback.mDroppedMessageCount = 0;

Expand All @@ -500,7 +473,7 @@ void CheckResendApplicationMessageWithPeerExchange(nlTestSuite * inSuite, void *
ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_NO_ERROR);

// Ensure the retransmit message was not dropped, and is no longer in the retransmit table
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 2);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount >= 2);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 1);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0);
NL_TEST_ASSERT(inSuite, mockReceiver.IsOnMessageReceivedCalled);
Expand Down Expand Up @@ -545,7 +518,7 @@ void CheckDuplicateMessageClosedExchange(nlTestSuite * inSuite, void * inContext
});

// Let's not drop the message. Expectation is that it is received by the peer, but the ack is dropped
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 0;
gLoopback.mDroppedMessageCount = 0;

Expand All @@ -562,7 +535,7 @@ void CheckDuplicateMessageClosedExchange(nlTestSuite * inSuite, void * inContext

// Ensure the message was sent
// The ack was dropped, and message was added to the retransmit table
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1);

Expand All @@ -578,7 +551,7 @@ void CheckDuplicateMessageClosedExchange(nlTestSuite * inSuite, void * inContext

// Ensure the retransmit message was sent and the ack was sent
// and retransmit table was cleared
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0);

Expand Down Expand Up @@ -628,7 +601,7 @@ void CheckResendSessionEstablishmentMessageWithPeerExchange(nlTestSuite * inSuit
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Let's drop the initial message
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 1;
gLoopback.mDroppedMessageCount = 0;

Expand All @@ -650,7 +623,7 @@ void CheckResendSessionEstablishmentMessageWithPeerExchange(nlTestSuite * inSuit
ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_NO_ERROR);

// Ensure the retransmit message was not dropped, and is no longer in the retransmit table
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 2);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount >= 2);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 1);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0);
NL_TEST_ASSERT(inSuite, mockReceiver.IsOnMessageReceivedCalled);
Expand Down Expand Up @@ -705,7 +678,7 @@ void CheckDuplicateMessage(nlTestSuite * inSuite, void * inContext)
});

// Let's not drop the message. Expectation is that it is received by the peer, but the ack is dropped
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 0;
gLoopback.mDroppedMessageCount = 0;

Expand All @@ -723,7 +696,7 @@ void CheckDuplicateMessage(nlTestSuite * inSuite, void * inContext)

// Ensure the message was sent
// The ack was dropped, and message was added to the retransmit table
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1);

Expand All @@ -740,7 +713,7 @@ void CheckDuplicateMessage(nlTestSuite * inSuite, void * inContext)

// Ensure the retransmit message was sent and the ack was sent
// and retransmit table was cleared
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);
NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0);

Expand Down Expand Up @@ -780,7 +753,7 @@ void CheckReceiveAfterStandaloneAck(nlTestSuite * inSuite, void * inContext)
// returned, then a reply is returned. We need to keep both exchanges alive
// for that (so we can send the response from the receiver and so the
// initial sender exchange can get it).
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 0;
gLoopback.mDroppedMessageCount = 0;
mockReceiver.mRetainExchange = true;
Expand All @@ -790,7 +763,7 @@ void CheckReceiveAfterStandaloneAck(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Ensure the message was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// And that it was received.
Expand All @@ -806,7 +779,7 @@ void CheckReceiveAfterStandaloneAck(nlTestSuite * inSuite, void * inContext)
receiverRc->SendStandaloneAckMessage();

// Ensure the ack was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// Ensure that we have not gotten any app-level responses so far.
Expand All @@ -825,7 +798,7 @@ void CheckReceiveAfterStandaloneAck(nlTestSuite * inSuite, void * inContext)
mockReceiver.mExchange->Close();

// Ensure the response was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// Ensure that we have received that response.
Expand Down Expand Up @@ -876,7 +849,7 @@ void CheckNoPiggybackAfterPiggyback(nlTestSuite * inSuite, void * inContext)
// piggybacked. We need to keep both exchanges alive for that (so we can
// send the response from the receiver and so the initial sender exchange
// can get it).
gLoopback.mSendMessageCount = 0;
gLoopback.mSentMessageCount = 0;
gLoopback.mNumMessagesToDrop = 0;
gLoopback.mDroppedMessageCount = 0;
mockReceiver.mRetainExchange = true;
Expand All @@ -886,7 +859,7 @@ void CheckNoPiggybackAfterPiggyback(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Ensure the message was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 1);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// And that it was received.
Expand All @@ -911,7 +884,7 @@ void CheckNoPiggybackAfterPiggyback(nlTestSuite * inSuite, void * inContext)
SendFlags(SendMessageFlags::kExpectResponse).Set(SendMessageFlags::kNoAutoRequestAck));

// Ensure the response was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// Ensure that we have received that response and it had a piggyback ack.
Expand All @@ -933,7 +906,7 @@ void CheckNoPiggybackAfterPiggyback(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Ensure the message was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 3);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// And that it was received.
Expand All @@ -952,7 +925,7 @@ void CheckNoPiggybackAfterPiggyback(nlTestSuite * inSuite, void * inContext)
mockReceiver.mExchange->Close();

// Ensure the response was sent.
NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 4);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 4);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0);

// Ensure that we have received that response and it did not have a piggyback
Expand Down
1 change: 1 addition & 0 deletions src/protocols/secure_channel/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ chip_test_suite("tests") {
"${chip_root}/src/messaging/tests:helpers",
"${chip_root}/src/protocols",
"${chip_root}/src/protocols/secure_channel",
"${chip_root}/src/transport/raw/tests:helpers",
"${nlio_root}:nlio",
"${nlunit_test_root}:nlunit-test",
]
Expand Down
23 changes: 2 additions & 21 deletions src/protocols/secure_channel/tests/TestCASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <support/CodeUtils.h>
#include <support/ScopedBuffer.h>
#include <support/UnitTestRegistration.h>
#include <transport/raw/tests/NetworkTestHelpers.h>

#include "credentials/tests/CHIPCert_test_vectors.h"

Expand All @@ -51,29 +52,9 @@ using namespace chip::Protocols;

using TestContext = chip::Test::MessagingContext;

class LoopbackTransport : public Transport::Base
{
public:
CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) override
{
ReturnErrorOnFailure(mMessageSendError);
mSentMessageCount++;

System::PacketBufferHandle receivedMessage = msgBuf.CloneData();
HandleMessageReceived(address, std::move(receivedMessage));

return CHIP_NO_ERROR;
}

bool CanSendToPeer(const PeerAddress & address) override { return true; }

uint32_t mSentMessageCount = 0;
CHIP_ERROR mMessageSendError = CHIP_NO_ERROR;
};

namespace {
TransportMgrBase gTransportMgr;
LoopbackTransport gLoopback;
Test::LoopbackTransport gLoopback;

OperationalCredentialSet commissionerDevOpCred;
OperationalCredentialSet accessoryDevOpCred;
Expand Down
Loading