tests: randomize port used in multicast transport tests
Change-Id: I6030317ad10cf6f3c81f29c2a3898b41a1982c0e
diff --git a/tests/daemon/face/multicast-udp-transport-fixture.hpp b/tests/daemon/face/multicast-udp-transport-fixture.hpp
index 40c79ee..7f126e9 100644
--- a/tests/daemon/face/multicast-udp-transport-fixture.hpp
+++ b/tests/daemon/face/multicast-udp-transport-fixture.hpp
@@ -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;
@@ -49,7 +51,7 @@
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)
@@ -57,22 +59,22 @@
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);
}
@@ -80,8 +82,8 @@
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);
}
@@ -93,14 +95,14 @@
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);
@@ -116,10 +118,11 @@
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
diff --git a/tests/daemon/face/tcp-transport-fixture.hpp b/tests/daemon/face/tcp-transport-fixture.hpp
index 01114e1..688028e 100644
--- a/tests/daemon/face/tcp-transport-fixture.hpp
+++ b/tests/daemon/face/tcp-transport-fixture.hpp
@@ -46,12 +46,12 @@
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();
});
@@ -60,8 +60,8 @@
void
stopAccept()
{
- BOOST_REQUIRE(acceptor.is_open());
- acceptor.close();
+ BOOST_REQUIRE(m_acceptor.is_open());
+ m_acceptor.close();
}
void
@@ -72,7 +72,7 @@
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();
});
@@ -89,10 +89,10 @@
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);
}
@@ -117,8 +117,8 @@
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
diff --git a/tests/daemon/face/unicast-udp-transport-fixture.hpp b/tests/daemon/face/unicast-udp-transport-fixture.hpp
index 24d7e26..474d520 100644
--- a/tests/daemon/face/unicast-udp-transport-fixture.hpp
+++ b/tests/daemon/face/unicast-udp-transport-fixture.hpp
@@ -53,10 +53,10 @@
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);
}
@@ -75,7 +75,7 @@
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);
}
@@ -104,7 +104,7 @@
std::vector<RxPacket>* receivedPackets = nullptr;
private:
- unique_ptr<Face> face;
+ unique_ptr<Face> m_face;
};
} // namespace nfd::tests
diff --git a/tests/daemon/face/unix-stream-transport-fixture.hpp b/tests/daemon/face/unix-stream-transport-fixture.hpp
index 2590cda..43b1ad0 100644
--- a/tests/daemon/face/unix-stream-transport-fixture.hpp
+++ b/tests/daemon/face/unix-stream-transport-fixture.hpp
@@ -80,14 +80,6 @@
class UnixStreamTransportFixture : public GlobalIoFixture
{
protected:
- UnixStreamTransportFixture()
- : transport(nullptr)
- , remoteSocket(g_io)
- , receivedPackets(nullptr)
- , acceptor(g_io)
- {
- }
-
[[nodiscard]] std::tuple<bool, std::string>
checkPreconditions() const
{
@@ -98,13 +90,13 @@
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();
});
@@ -112,10 +104,10 @@
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);
}
@@ -134,14 +126,14 @@
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
diff --git a/tests/daemon/face/websocket-transport-fixture.hpp b/tests/daemon/face/websocket-transport-fixture.hpp
index c6f25da..ce13198 100644
--- a/tests/daemon/face/websocket-transport-fixture.hpp
+++ b/tests/daemon/face/websocket-transport-fixture.hpp
@@ -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,
@@ -103,10 +103,10 @@
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);
}
@@ -205,7 +205,7 @@
std::vector<std::string> clientReceivedMessages;
private:
- unique_ptr<Face> face;
+ unique_ptr<Face> m_face;
};
} // namespace nfd::tests