Skip to content

Commit f1b7f2e

Browse files
Add WillSendMessage to loopback delegate and make source addresses more plausible
1 parent e3d570b commit f1b7f2e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/messaging/tests/MessagingContext.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class MessagingContext : public PlatformMemoryUser
9494

9595
MessagingContext() :
9696
mInitialized(false), mAliceAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT + 1)),
97-
mBobAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT))
97+
mBobAddress(LoopbackTransport::LoopbackPeer(mAliceAddress))
9898
{}
9999
// TODO Replace VerifyOrDie with Pigweed assert after transition app/tests to Pigweed.
100100
// TODO Currently src/app/icd/server/tests is using MessagingConetext as dependency.

src/transport/raw/tests/NetworkTestHelpers.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class LoopbackTransportDelegate
6464
public:
6565
virtual ~LoopbackTransportDelegate() {}
6666

67+
// Called by the loopback transport when a message is requested to be sent.
68+
// This is called even if the message is subsequently rejected or dropped.
69+
virtual void WillSendMessage(const Transport::PeerAddress & peer, const System::PacketBufferHandle & message) {}
70+
6771
// Called by the loopback transport when it drops one of a configurable number of messages (mDroppedMessageCount) after a
6872
// configurable allowed number of messages (mNumMessagesToAllowBeforeDropping)
6973
virtual void OnMessageDropped() {}
@@ -72,6 +76,18 @@ class LoopbackTransportDelegate
7276
class LoopbackTransport : public Transport::Base
7377
{
7478
public:
79+
// In test scenarios using the loopback transport, we're only ever given
80+
// the address we're sending to, but we don't have any information about
81+
// what our local address is. Assume our fake addresses come in pairs of
82+
// even and odd port numbers, so we can calculate one from the other by
83+
// flipping the LSB of the port number.
84+
static Transport::PeerAddress LoopbackPeer(const Transport::PeerAddress & address)
85+
{
86+
Transport::PeerAddress other(address);
87+
other.SetPort(address.GetPort() ^ 1);
88+
return other;
89+
}
90+
7591
void InitLoopbackTransport(System::Layer * systemLayer)
7692
{
7793
Reset();
@@ -100,14 +116,19 @@ class LoopbackTransport : public Transport::Base
100116
{
101117
auto item = std::move(_this->mPendingMessageQueue.front());
102118
_this->mPendingMessageQueue.pop();
103-
_this->HandleMessageReceived(item.mDestinationAddress, std::move(item.mPendingMessage));
119+
_this->HandleMessageReceived(LoopbackPeer(item.mDestinationAddress), std::move(item.mPendingMessage));
104120
}
105121
}
106122

107123
static constexpr uint32_t kUnlimitedMessageCount = std::numeric_limits<uint32_t>::max();
108124

109125
CHIP_ERROR SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf) override
110126
{
127+
if (mDelegate != nullptr)
128+
{
129+
mDelegate->WillSendMessage(address, msgBuf);
130+
}
131+
111132
if (mNumMessagesToAllowBeforeError == 0)
112133
{
113134
ReturnErrorOnFailure(mMessageSendError);

0 commit comments

Comments
 (0)