From 9e3c2cfd9ee017c5520cc837679326e83dc550d2 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 30 Jun 2021 15:57:35 -0400 Subject: [PATCH] Have a single loopback transport implementation for tests. We have multiple, some with different bells and whistles. Should have one shared thing. Unfortunately, some consumers want to drive things off the loopback (like resending reliable messages) that other consumers can't know about because they are at lower layers of the stack. We just use a virtual hook for that for now. --- src/channel/tests/TestChannel.cpp | 18 +--- src/messaging/tests/TestExchangeMgr.cpp | 18 +--- .../tests/TestReliableMessageProtocol.cpp | 83 +++++++------------ src/protocols/secure_channel/tests/BUILD.gn | 1 + .../secure_channel/tests/TestCASESession.cpp | 23 +---- .../tests/TestMessageCounterManager.cpp | 18 +--- .../secure_channel/tests/TestPASESession.cpp | 48 +++-------- src/transport/raw/tests/BUILD.gn | 5 +- src/transport/raw/tests/NetworkTestHelpers.h | 49 +++++++++++ src/transport/tests/TestSecureSessionMgr.cpp | 37 +-------- 10 files changed, 105 insertions(+), 195 deletions(-) diff --git a/src/channel/tests/TestChannel.cpp b/src/channel/tests/TestChannel.cpp index abda1c765349bc..49a6ff1dbd187b 100644 --- a/src/channel/tests/TestChannel.cpp +++ b/src/channel/tests/TestChannel.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -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 gTransportMgr; +TransportMgr gTransportMgr; class MockAppDelegate : public ExchangeDelegate { diff --git a/src/messaging/tests/TestExchangeMgr.cpp b/src/messaging/tests/TestExchangeMgr.cpp index fb26006ed1040b..27b0a0a22c1837 100644 --- a/src/messaging/tests/TestExchangeMgr.cpp +++ b/src/messaging/tests/TestExchangeMgr.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -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 gTransportMgr; +TransportMgr gTransportMgr; class MockAppDelegate : public ExchangeDelegate { diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index b25b1a7bc8aee9..cb1fb628739386 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -42,6 +42,7 @@ #include #include #include +#include namespace { @@ -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 { @@ -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; @@ -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); @@ -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); @@ -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; @@ -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); @@ -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); @@ -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; @@ -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; @@ -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); @@ -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; @@ -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); @@ -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); @@ -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; @@ -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); @@ -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; @@ -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); @@ -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); @@ -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; @@ -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. @@ -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. @@ -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. @@ -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; @@ -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. @@ -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. @@ -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. @@ -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 diff --git a/src/protocols/secure_channel/tests/BUILD.gn b/src/protocols/secure_channel/tests/BUILD.gn index a48aa9a65dacf1..ca0a348033df5a 100644 --- a/src/protocols/secure_channel/tests/BUILD.gn +++ b/src/protocols/secure_channel/tests/BUILD.gn @@ -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", ] diff --git a/src/protocols/secure_channel/tests/TestCASESession.cpp b/src/protocols/secure_channel/tests/TestCASESession.cpp index 9458c12de004ac..19dc1d91ab2cda 100644 --- a/src/protocols/secure_channel/tests/TestCASESession.cpp +++ b/src/protocols/secure_channel/tests/TestCASESession.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include "credentials/tests/CHIPCert_test_vectors.h" @@ -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; diff --git a/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp b/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp index f957d22a5df964..8e12adc713dc52 100644 --- a/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp +++ b/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -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 PeerAddress & address, System::PacketBufferHandle && msgBuf) override - { - HandleMessageReceived(address, std::move(msgBuf)); - return CHIP_NO_ERROR; - } - - bool CanSendToPeer(const PeerAddress & address) override { return true; } -}; - -TransportMgr gTransportMgr; +TransportMgr gTransportMgr; const char PAYLOAD[] = "Hello!"; diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index 98c16b34480185..9d72adb7cce958 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -32,6 +32,7 @@ #include #include #include +#include using namespace chip; using namespace chip::Inet; @@ -53,54 +54,29 @@ static void test_os_sleep_ms(uint64_t millisecs) nanosleep(&sleep_time, nullptr); } -class PASETestLoopbackTransport : public Transport::Base +class PASETestLoopbackTransport : public Test::LoopbackTransport { -public: - CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) override + void MessageDropped() override { - ReturnErrorOnFailure(mMessageSendError); - mSentMessageCount++; - - if (mNumMessagesToDrop == 0) - { - // The msgBuf is also being used for retransmission. So we cannot hand over the same buffer - // to the receive handler. The receive handler modifies the buffer for extracting headers etc. - // So the buffer passed to receive handler cannot be used for retransmission afterwards. - // Let's clone the message, and provide cloned message to the receive handler. - System::PacketBufferHandle receivedMessage = msgBuf.CloneData(); - HandleMessageReceived(address, std::move(receivedMessage)); - } - else + // Trigger a retransmit. + if (mContext != nullptr) { - mNumMessagesToDrop--; - mDroppedMessageCount++; - if (mContext != nullptr) - { - test_os_sleep_ms(65); - ReliableMessageMgr * rm = mContext->GetExchangeManager().GetReliableMessageMgr(); - ReliableMessageMgr::Timeout(&mContext->GetSystemLayer(), rm, CHIP_NO_ERROR); - } + test_os_sleep_ms(65); + ReliableMessageMgr * rm = mContext->GetExchangeManager().GetReliableMessageMgr(); + ReliableMessageMgr::Timeout(&mContext->GetSystemLayer(), rm, CHIP_NO_ERROR); } - - return CHIP_NO_ERROR; } +public: bool CanSendToPeer(const PeerAddress & address) override { return true; } void Reset() { - mNumMessagesToDrop = 0; - mDroppedMessageCount = 0; - mSentMessageCount = 0; - mMessageSendError = CHIP_NO_ERROR; - mContext = nullptr; + Test::LoopbackTransport::Reset(); + mContext = nullptr; } - uint32_t mNumMessagesToDrop = 0; - uint32_t mDroppedMessageCount = 0; - uint32_t mSentMessageCount = 0; - CHIP_ERROR mMessageSendError = CHIP_NO_ERROR; - TestContext * mContext = nullptr; + TestContext * mContext = nullptr; }; TransportMgrBase gTransportMgr; diff --git a/src/transport/raw/tests/BUILD.gn b/src/transport/raw/tests/BUILD.gn index 2fb4ef59e90d5e..e54e9ec28383f0 100644 --- a/src/transport/raw/tests/BUILD.gn +++ b/src/transport/raw/tests/BUILD.gn @@ -30,7 +30,10 @@ static_library("helpers") { cflags = [ "-Wconversion" ] - public_deps = [ "${chip_root}/src/inet/tests:helpers" ] + public_deps = [ + "${chip_root}/src/inet/tests:helpers", + "${chip_root}/src/transport/raw", + ] } chip_test_suite("tests") { diff --git a/src/transport/raw/tests/NetworkTestHelpers.h b/src/transport/raw/tests/NetworkTestHelpers.h index d8449f34a5e865..4ccb3f4d7249df 100644 --- a/src/transport/raw/tests/NetworkTestHelpers.h +++ b/src/transport/raw/tests/NetworkTestHelpers.h @@ -20,7 +20,11 @@ #include #include +#include #include +#include +#include +#include #include #include @@ -56,5 +60,50 @@ class IOContext Inet::InetLayer * mInetLayer = nullptr; }; +class LoopbackTransport : public Transport::Base +{ +public: + /// Transports are required to have a constructor that takes exactly one argument + CHIP_ERROR Init(const char *) { return CHIP_NO_ERROR; } + + CHIP_ERROR SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf) override + { + ReturnErrorOnFailure(mMessageSendError); + mSentMessageCount++; + + if (mNumMessagesToDrop == 0) + { + System::PacketBufferHandle receivedMessage = msgBuf.CloneData(); + HandleMessageReceived(address, std::move(receivedMessage)); + } + else + { + mNumMessagesToDrop--; + mDroppedMessageCount++; + MessageDropped(); + } + + return CHIP_NO_ERROR; + } + + bool CanSendToPeer(const Transport::PeerAddress & address) override { return true; } + + void Reset() + { + mNumMessagesToDrop = 0; + mDroppedMessageCount = 0; + mSentMessageCount = 0; + mMessageSendError = CHIP_NO_ERROR; + } + + // Hook for subclasses to perform custom logic on message drops. + virtual void MessageDropped() {} + + uint32_t mNumMessagesToDrop = 0; + uint32_t mDroppedMessageCount = 0; + uint32_t mSentMessageCount = 0; + CHIP_ERROR mMessageSendError = CHIP_NO_ERROR; +}; + } // namespace Test } // namespace chip diff --git a/src/transport/tests/TestSecureSessionMgr.cpp b/src/transport/tests/TestSecureSessionMgr.cpp index 0974d28d523ed7..967c8454fed295 100644 --- a/src/transport/tests/TestSecureSessionMgr.cpp +++ b/src/transport/tests/TestSecureSessionMgr.cpp @@ -47,6 +47,7 @@ namespace { using namespace chip; using namespace chip::Inet; using namespace chip::Transport; +using namespace chip::Test; using TestContext = chip::Test::IOContext; @@ -58,38 +59,6 @@ constexpr NodeId kDestinationNodeId = 111222333; const char LARGE_PAYLOAD[kMaxAppMessageLen + 1] = "test message"; -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; } -}; - -class OutgoingTransport : 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 - { - System::PacketBufferHandle recvdMsg = msgBuf.CloneData(); - - HandleMessageReceived(address, std::move(recvdMsg)); - return CHIP_NO_ERROR; - } - - bool CanSendToPeer(const PeerAddress & address) override { return true; } -}; - class TestSessMgrCallback : public SecureSessionMgrDelegate { public: @@ -274,7 +243,7 @@ void SendEncryptedPacketTest(nlTestSuite * inSuite, void * inContext) IPAddress::FromString("127.0.0.1", addr); CHIP_ERROR err = CHIP_NO_ERROR; - TransportMgr transportMgr; + TransportMgr transportMgr; SecureSessionMgr secureSessionMgr; secure_channel::MessageCounterManager gMessageCounterManager; @@ -356,7 +325,7 @@ void SendBadEncryptedPacketTest(nlTestSuite * inSuite, void * inContext) IPAddress::FromString("127.0.0.1", addr); CHIP_ERROR err = CHIP_NO_ERROR; - TransportMgr transportMgr; + TransportMgr transportMgr; SecureSessionMgr secureSessionMgr; secure_channel::MessageCounterManager gMessageCounterManager;