Skip to content

Commit

Permalink
tests: randomize port used in multicast transport tests
Browse files Browse the repository at this point in the history
Change-Id: I6030317ad10cf6f3c81f29c2a3898b41a1982c0e
  • Loading branch information
Pesa committed Nov 3, 2023
1 parent 05bd6c2 commit ef6a528
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 68 deletions.
5 changes: 3 additions & 2 deletions daemon/face/udp-protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ computeMtu(const Endpoint& localEndpoint);
inline Endpoint
getDefaultMulticastGroup()
{
return {boost::asio::ip::make_address_v4(0xE00017AA), 56363};
return {boost::asio::ip::address_v4(0xE00017AA), 56363};
}

/**
Expand All @@ -55,7 +55,8 @@ getDefaultMulticastGroup()
inline Endpoint
getDefaultMulticastGroupV6()
{
return {boost::asio::ip::make_address_v6("FF02::1234"), 56363};
return {boost::asio::ip::address_v6({0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34}), 56363};
}

} // namespace nfd::udp
Expand Down
41 changes: 22 additions & 19 deletions tests/daemon/face/multicast-udp-transport-fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "tests/daemon/face/dummy-link-service.hpp"
#include "tests/daemon/face/transport-test-common.hpp"

#include <ndn-cxx/util/random.hpp>

namespace nfd::tests {

namespace ip = boost::asio::ip;
Expand All @@ -49,39 +51,39 @@ class MulticastUdpTransportFixture : public GlobalIoFixture
ip::address mcastAddr;
if (address.is_v4()) {
// the administratively scoped group 224.0.0.254 is reserved for experimentation (RFC 4727)
mcastAddr = ip::make_address_v4(0xE00000FE);
mcastAddr = ip::make_address_v4("224.0.0.254");
}
else {
// the group FF0X::114 is reserved for experimentation at all scope levels (RFC 4727)
auto v6Addr = ip::make_address_v6("FF01::114");
v6Addr.scope_id(netif->getIndex());
mcastAddr = v6Addr;
}
mcastEp = udp::endpoint(mcastAddr, 7373);
remoteMcastEp = udp::endpoint(mcastAddr, 8383);
mcastEp = udp::endpoint(mcastAddr, m_randomPort(ndn::random::getRandomNumberEngine()));
m_remoteMcastEp = udp::endpoint(mcastAddr, m_randomPort(ndn::random::getRandomNumberEngine()));

MulticastUdpTransport::openRxSocket(remoteSockRx, mcastEp, address);
MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(address, 0), &*netif, true);
MulticastUdpTransport::openRxSocket(m_remoteSockRx, mcastEp, address);
MulticastUdpTransport::openTxSocket(m_remoteSockTx, udp::endpoint(address, 0), &*netif, true);

udp::socket sockRx(g_io);
udp::socket sockTx(g_io);
MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address);
MulticastUdpTransport::openRxSocket(sockRx, m_remoteMcastEp, address);
MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), &*netif, true);

face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
ndn::nfd::LINK_TYPE_MULTI_ACCESS));
transport = static_cast<MulticastUdpTransport*>(face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
m_face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
ndn::nfd::LINK_TYPE_MULTI_ACCESS));
transport = static_cast<MulticastUdpTransport*>(m_face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets;

BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
}

void
remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
{
remoteSockRx.async_receive(boost::asio::buffer(buf),
[this, needToCheck] (const boost::system::error_code& error, size_t) {
m_remoteSockRx.async_receive(boost::asio::buffer(buf),
[this, needToCheck] (const auto& error, size_t) {
if (needToCheck) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
}
Expand All @@ -93,14 +95,14 @@ class MulticastUdpTransportFixture : public GlobalIoFixture
void
remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
{
sendToGroup(remoteSockTx, buf, needToCheck);
sendToGroup(m_remoteSockTx, buf, needToCheck);
limitedIo.defer(1_s);
}

void
sendToGroup(udp::socket& sock, const std::vector<uint8_t>& buf, bool needToCheck = true) const
{
sock.async_send_to(boost::asio::buffer(buf), remoteMcastEp,
sock.async_send_to(boost::asio::buffer(buf), m_remoteMcastEp,
[needToCheck] (const auto& error, size_t) {
if (needToCheck) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
Expand All @@ -116,10 +118,11 @@ class MulticastUdpTransportFixture : public GlobalIoFixture
std::vector<RxPacket>* receivedPackets = nullptr;

private:
unique_ptr<Face> face;
udp::endpoint remoteMcastEp;
udp::socket remoteSockRx{g_io};
udp::socket remoteSockTx{g_io};
std::uniform_int_distribution<uint16_t> m_randomPort{10000};
udp::endpoint m_remoteMcastEp;
udp::socket m_remoteSockRx{g_io};
udp::socket m_remoteSockTx{g_io};
unique_ptr<Face> m_face;
};

} // namespace nfd::tests
Expand Down
30 changes: 15 additions & 15 deletions tests/daemon/face/tcp-transport-fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class TcpTransportFixture : public GlobalIoFixture
void
startAccept(const tcp::endpoint& remoteEp)
{
BOOST_REQUIRE(!acceptor.is_open());
acceptor.open(remoteEp.protocol());
acceptor.set_option(boost::asio::socket_base::reuse_address(true));
acceptor.bind(remoteEp);
acceptor.listen(1);
acceptor.async_accept(remoteSocket, [this] (const boost::system::error_code& error) {
BOOST_REQUIRE(!m_acceptor.is_open());
m_acceptor.open(remoteEp.protocol());
m_acceptor.set_option(boost::asio::socket_base::reuse_address(true));
m_acceptor.bind(remoteEp);
m_acceptor.listen(1);
m_acceptor.async_accept(remoteSocket, [this] (const auto& error) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
limitedIo.afterOp();
});
Expand All @@ -60,8 +60,8 @@ class TcpTransportFixture : public GlobalIoFixture
void
stopAccept()
{
BOOST_REQUIRE(acceptor.is_open());
acceptor.close();
BOOST_REQUIRE(m_acceptor.is_open());
m_acceptor.close();
}

void
Expand All @@ -72,7 +72,7 @@ class TcpTransportFixture : public GlobalIoFixture
startAccept(remoteEp);

tcp::socket sock(g_io);
sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
sock.async_connect(remoteEp, [this] (const auto& error) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
limitedIo.afterOp();
});
Expand All @@ -89,10 +89,10 @@ class TcpTransportFixture : public GlobalIoFixture
scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
}

face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<TcpTransport>(std::move(sock), persistency, scope));
transport = static_cast<TcpTransport*>(face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
m_face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<TcpTransport>(std::move(sock), persistency, scope));
transport = static_cast<TcpTransport*>(m_face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets;

BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
}
Expand All @@ -117,8 +117,8 @@ class TcpTransportFixture : public GlobalIoFixture
std::vector<RxPacket>* receivedPackets = nullptr;

private:
tcp::acceptor acceptor{g_io};
unique_ptr<Face> face;
tcp::acceptor m_acceptor{g_io};
unique_ptr<Face> m_face;
};

} // namespace nfd::tests
Expand Down
12 changes: 6 additions & 6 deletions tests/daemon/face/unicast-udp-transport-fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class UnicastUdpTransportFixture : public GlobalIoFixture

remoteConnect(address);

face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
transport = static_cast<UnicastUdpTransport*>(face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
m_face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
transport = static_cast<UnicastUdpTransport*>(m_face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets;

BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
}
Expand All @@ -75,7 +75,7 @@ class UnicastUdpTransportFixture : public GlobalIoFixture
remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
{
remoteSocket.async_receive(boost::asio::buffer(buf),
[this, needToCheck] (const boost::system::error_code& error, size_t) {
[this, needToCheck] (const auto& error, size_t) {
if (needToCheck) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ class UnicastUdpTransportFixture : public GlobalIoFixture
std::vector<RxPacket>* receivedPackets = nullptr;

private:
unique_ptr<Face> face;
unique_ptr<Face> m_face;
};

} // namespace nfd::tests
Expand Down
32 changes: 12 additions & 20 deletions tests/daemon/face/unix-stream-transport-fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ class AcceptorWithCleanup : public unix_stream::acceptor
class UnixStreamTransportFixture : public GlobalIoFixture
{
protected:
UnixStreamTransportFixture()
: transport(nullptr)
, remoteSocket(g_io)
, receivedPackets(nullptr)
, acceptor(g_io)
{
}

[[nodiscard]] std::tuple<bool, std::string>
checkPreconditions() const
{
Expand All @@ -98,24 +90,24 @@ class UnixStreamTransportFixture : public GlobalIoFixture
initialize()
{
unix_stream::socket sock(g_io);
acceptor.async_accept(sock, [this] (const boost::system::error_code& error) {
m_acceptor.async_accept(sock, [this] (const auto& error) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
limitedIo.afterOp();
});

unix_stream::endpoint remoteEp(acceptor.local_endpoint());
remoteSocket.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
unix_stream::endpoint remoteEp(m_acceptor.local_endpoint());
remoteSocket.async_connect(remoteEp, [this] (const auto& error) {
BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
limitedIo.afterOp();
});

BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);

localEp = sock.local_endpoint();
face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<UnixStreamTransport>(std::move(sock)));
transport = static_cast<UnixStreamTransport*>(face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
m_face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<UnixStreamTransport>(std::move(sock)));
transport = static_cast<UnixStreamTransport*>(m_face->getTransport());
receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets;

BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
}
Expand All @@ -134,14 +126,14 @@ class UnixStreamTransportFixture : public GlobalIoFixture

protected:
LimitedIo limitedIo;
UnixStreamTransport* transport;
UnixStreamTransport* transport = nullptr;
unix_stream::endpoint localEp;
unix_stream::socket remoteSocket;
std::vector<RxPacket>* receivedPackets;
unix_stream::socket remoteSocket{g_io};
std::vector<RxPacket>* receivedPackets = nullptr;

private:
AcceptorWithCleanup acceptor;
unique_ptr<Face> face;
AcceptorWithCleanup m_acceptor{g_io};
unique_ptr<Face> m_face;
};

} // namespace nfd::tests
Expand Down
12 changes: 6 additions & 6 deletions tests/daemon/face/websocket-transport-fixture.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -103,10 +103,10 @@ class WebSocketTransportFixture : public GlobalIoFixture
BOOST_REQUIRE_EQUAL(limitedIo.run(2, // serverHandleOpen, clientHandleOpen
1_s), LimitedIo::EXCEED_OPS);

face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<WebSocketTransport>(serverHdl, server, pingInterval));
transport = static_cast<WebSocketTransport*>(face->getTransport());
serverReceivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
m_face = make_unique<Face>(make_unique<DummyLinkService>(),
make_unique<WebSocketTransport>(serverHdl, server, pingInterval));
transport = static_cast<WebSocketTransport*>(m_face->getTransport());
serverReceivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets;

BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
}
Expand Down Expand Up @@ -205,7 +205,7 @@ class WebSocketTransportFixture : public GlobalIoFixture
std::vector<std::string> clientReceivedMessages;

private:
unique_ptr<Face> face;
unique_ptr<Face> m_face;
};

} // namespace nfd::tests
Expand Down

0 comments on commit ef6a528

Please sign in to comment.