Skip to content

Commit

Permalink
TestUDP: use ephemeral port (#9692)
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost authored Sep 14, 2021
1 parent 181ab66 commit b384bde
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/transport/raw/UDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ CHIP_ERROR UDP::Init(UdpListenParameters & params)

mState = State::kInitialized;

ChipLogDetail(Inet, "UDP::Init bound to port=%d", mUDPEndPoint->GetBoundPort());

exit:
if (err != CHIP_NO_ERROR)
{
Expand All @@ -75,6 +77,12 @@ CHIP_ERROR UDP::Init(UdpListenParameters & params)
return err;
}

uint16_t UDP::GetBoundPort()
{
VerifyOrDie(mUDPEndPoint != nullptr);
return mUDPEndPoint->GetBoundPort();
}

void UDP::Close()
{
if (mUDPEndPoint)
Expand Down
2 changes: 2 additions & 0 deletions src/transport/raw/UDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class DLL_EXPORT UDP : public Base
*/
CHIP_ERROR Init(UdpListenParameters & params);

uint16_t GetBoundPort();

/**
* Close the open endpoint without destroying the object
*/
Expand Down
6 changes: 3 additions & 3 deletions src/transport/raw/tests/TestUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void CheckSimpleInitTest(nlTestSuite * inSuite, void * inContext, Inet::IPAddres

Transport::UDP udp;

CHIP_ERROR err = udp.Init(Transport::UdpListenParameters(&ctx.GetInetLayer()).SetAddressType(type));
CHIP_ERROR err = udp.Init(Transport::UdpListenParameters(&ctx.GetInetLayer()).SetAddressType(type).SetListenPort(0));

NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ void CheckMessageTest(nlTestSuite * inSuite, void * inContext, const IPAddress &

Transport::UDP udp;

err = udp.Init(Transport::UdpListenParameters(&ctx.GetInetLayer()).SetAddressType(addr.Type()));
err = udp.Init(Transport::UdpListenParameters(&ctx.GetInetLayer()).SetAddressType(addr.Type()).SetListenPort(0));
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

MockTransportMgrDelegate gMockTransportMgrDelegate(inSuite);
Expand All @@ -139,7 +139,7 @@ void CheckMessageTest(nlTestSuite * inSuite, void * inContext, const IPAddress &
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Should be able to send a message to itself by just calling send.
err = udp.SendMessage(Transport::PeerAddress::UDP(addr), std::move(buffer));
err = udp.SendMessage(Transport::PeerAddress::UDP(addr, udp.GetBoundPort()), std::move(buffer));
if (err == System::MapErrorPOSIX(EADDRNOTAVAIL))
{
// TODO(#2698): the underlying system does not support IPV6. This early return
Expand Down

0 comments on commit b384bde

Please sign in to comment.