Skip to content

Commit

Permalink
tests: refactor EthernetFixture
Browse files Browse the repository at this point in the history
Also, use BOOST_TEST_INFO_SCOPE in a few places.

Change-Id: I113f326207825949ed9d3d22ade0654976d49b76
  • Loading branch information
Pesa committed Oct 19, 2023
1 parent c5a9f81 commit d7083a5
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 163 deletions.
18 changes: 8 additions & 10 deletions tests/daemon/face/ethernet-factory.t.cpp
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 @@ -49,22 +49,20 @@ class EthernetFactoryFixture : public EthernetFixture
{
std::set<std::string> uris;
std::transform(netifs.begin(), netifs.end(), std::inserter(uris, uris.end()),
[] (const auto& netif) {
return FaceUri::fromDev(netif->getName()).toString();
});
[] (const auto& netif) { return FaceUri::fromDev(netif->getName()).toString(); });
return uris;
}

std::vector<const Face*>
listEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
{
return this->listFacesByScheme("ether", linkType);
return listFacesByScheme("ether", linkType);
}

size_t
countEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
{
return this->listEtherMcastFaces(linkType).size();
return listEtherMcastFaces(linkType).size();
}
};

Expand Down Expand Up @@ -303,10 +301,9 @@ BOOST_AUTO_TEST_CASE(Blacklist)
auto etherMcastFaces = this->listEtherMcastFaces();
BOOST_CHECK_EQUAL(etherMcastFaces.size(), netifs.size() - 1);
BOOST_CHECK(std::none_of(etherMcastFaces.begin(), etherMcastFaces.end(),
[ifname] (const nfd::Face* face) {
return face->getLocalUri() == FaceUri::fromDev(ifname);
}
));
[uri = FaceUri::fromDev(ifname)] (const auto* face) {
return face->getLocalUri() == uri;
}));
}

BOOST_AUTO_TEST_CASE(Omitted)
Expand Down Expand Up @@ -438,6 +435,7 @@ BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
BOOST_AUTO_TEST_CASE(GetChannels)
{
BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);

SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);

factory.createChannel(netifs.front(), 1_min);
Expand Down
86 changes: 9 additions & 77 deletions tests/daemon/face/ethernet-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 All @@ -27,17 +27,12 @@
#define NFD_TESTS_DAEMON_FACE_ETHERNET_FIXTURE_HPP

#include "face/multicast-ethernet-transport.hpp"
#include "face/unicast-ethernet-transport.hpp"

#include "tests/daemon/limited-io.hpp"
#include "tests/daemon/global-io-fixture.hpp"
#include "test-netif.hpp"

namespace nfd::tests {

using face::EthernetTransport;
using face::MulticastEthernetTransport;
using face::UnicastEthernetTransport;

/**
* \brief Fixture providing a list of EthernetTransport-capable network interfaces.
*/
Expand All @@ -47,79 +42,25 @@ class EthernetFixture : public virtual GlobalIoFixture
EthernetFixture()
{
for (const auto& netif : collectNetworkInterfaces()) {
if (!netif->isLoopback() && netif->isUp()) {
// similar filtering logic to EthernetFactory::applyMcastConfigToNetif()
if (netif->isUp() && !netif->isLoopback() && netif->canMulticast()) {
try {
MulticastEthernetTransport t(*netif, ethernet::getBroadcastAddress(),
ndn::nfd::LINK_TYPE_MULTI_ACCESS);
face::MulticastEthernetTransport t(*netif, ethernet::getBroadcastAddress(),
ndn::nfd::LINK_TYPE_MULTI_ACCESS);
netifs.push_back(netif);
}
catch (const EthernetTransport::Error&) {
catch (const face::EthernetTransport::Error&) {
// ignore
}
}
}
if (!netifs.empty()) {
defaultNetif = const_pointer_cast<ndn::net::NetworkInterface>(netifs.front());
}
}

/** \brief Returns the first running interface.
*/
shared_ptr<ndn::net::NetworkInterface>
getRunningNetif() const
{
for (const auto& netif : netifs) {
if (netif->getState() == ndn::net::InterfaceState::RUNNING) {
return const_pointer_cast<ndn::net::NetworkInterface>(netif);
}
}

return nullptr;
}

/** \brief Create a UnicastEthernetTransport.
*/
void
initializeUnicast(shared_ptr<ndn::net::NetworkInterface> netif = nullptr,
ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
ethernet::Address remoteAddr = {0x00, 0x00, 0x5e, 0x00, 0x53, 0x5e})
{
if (!netif) {
netif = defaultNetif;
}

localEp = netif->getName();
remoteEp = remoteAddr;
transport = make_unique<UnicastEthernetTransport>(*netif, remoteEp, persistency, 2_s);
}

/** \brief Create a MulticastEthernetTransport.
*/
void
initializeMulticast(shared_ptr<ndn::net::NetworkInterface> netif = nullptr,
ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS,
ethernet::Address mcastGroup = {0x01, 0x00, 0x5e, 0x90, 0x10, 0x5e})
{
if (!netif) {
netif = defaultNetif;
}

localEp = netif->getName();
remoteEp = mcastGroup;
transport = make_unique<MulticastEthernetTransport>(*netif, remoteEp, linkType);
}

protected:
LimitedIo limitedIo;

/** \brief EthernetTransport-capable network interfaces.
/**
* \brief EthernetTransport-capable network interfaces.
*/
std::vector<shared_ptr<const ndn::net::NetworkInterface>> netifs;

shared_ptr<ndn::net::NetworkInterface> defaultNetif;
unique_ptr<EthernetTransport> transport;
std::string localEp;
ethernet::Address remoteEp;
};

} // namespace nfd::tests
Expand All @@ -133,13 +74,4 @@ class EthernetFixture : public virtual GlobalIoFixture
} \
} while (false)

#define SKIP_IF_NO_RUNNING_ETHERNET_NETIF() \
do { \
if (!this->getRunningNetif()) { \
BOOST_WARN_MESSAGE(false, "skipping assertions that require a running " \
"EthernetTransport-capable network interface"); \
return; \
} \
} while (false)

#endif // NFD_TESTS_DAEMON_FACE_ETHERNET_FIXTURE_HPP
115 changes: 115 additions & 0 deletions tests/daemon/face/ethernet-transport-fixture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef NFD_TESTS_DAEMON_FACE_ETHERNET_TRANSPORT_FIXTURE_HPP
#define NFD_TESTS_DAEMON_FACE_ETHERNET_TRANSPORT_FIXTURE_HPP

#include "face/multicast-ethernet-transport.hpp"
#include "face/unicast-ethernet-transport.hpp"

#include "tests/daemon/limited-io.hpp"
#include "ethernet-fixture.hpp"

namespace nfd::tests {

class EthernetTransportFixture : public EthernetFixture
{
protected:
EthernetTransportFixture()
{
if (!netifs.empty()) {
defaultNetif = const_pointer_cast<ndn::net::NetworkInterface>(netifs.front());
}
}

/**
* \brief Returns the first running interface.
*/
shared_ptr<ndn::net::NetworkInterface>
getRunningNetif() const
{
for (const auto& netif : netifs) {
if (netif->getState() == ndn::net::InterfaceState::RUNNING) {
return const_pointer_cast<ndn::net::NetworkInterface>(netif);
}
}

return nullptr;
}

/**
* \brief Create a UnicastEthernetTransport.
*/
void
initializeUnicast(shared_ptr<ndn::net::NetworkInterface> netif = nullptr,
ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
ethernet::Address remoteAddr = {0x00, 0x00, 0x5e, 0x00, 0x53, 0x5e})
{
if (!netif) {
netif = defaultNetif;
}

localEp = netif->getName();
remoteEp = remoteAddr;
transport = make_unique<face::UnicastEthernetTransport>(*netif, remoteEp, persistency, 2_s);
}

/**
* \brief Create a MulticastEthernetTransport.
*/
void
initializeMulticast(shared_ptr<ndn::net::NetworkInterface> netif = nullptr,
ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS,
ethernet::Address mcastGroup = {0x01, 0x00, 0x5e, 0x90, 0x10, 0x5e})
{
if (!netif) {
netif = defaultNetif;
}

localEp = netif->getName();
remoteEp = mcastGroup;
transport = make_unique<face::MulticastEthernetTransport>(*netif, remoteEp, linkType);
}

protected:
LimitedIo limitedIo;
shared_ptr<ndn::net::NetworkInterface> defaultNetif;
unique_ptr<face::EthernetTransport> transport;
std::string localEp;
ethernet::Address remoteEp;
};

} // namespace nfd::tests

#define SKIP_IF_NO_RUNNING_ETHERNET_NETIF() \
do { \
if (!this->getRunningNetif()) { \
BOOST_WARN_MESSAGE(false, "skipping assertions that require a running " \
"EthernetTransport-capable network interface"); \
return; \
} \
} while (false)

#endif // NFD_TESTS_DAEMON_FACE_ETHERNET_TRANSPORT_FIXTURE_HPP
6 changes: 3 additions & 3 deletions tests/daemon/face/multicast-ethernet-transport.t.cpp
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 All @@ -23,7 +23,7 @@
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/

#include "ethernet-fixture.hpp"
#include "ethernet-transport-fixture.hpp"

#include "common/global.hpp"

Expand All @@ -34,7 +34,7 @@ namespace nfd::tests {
using namespace nfd::face;

BOOST_AUTO_TEST_SUITE(Face)
BOOST_FIXTURE_TEST_SUITE(TestMulticastEthernetTransport, EthernetFixture)
BOOST_FIXTURE_TEST_SUITE(TestMulticastEthernetTransport, EthernetTransportFixture)

BOOST_AUTO_TEST_CASE(StaticProperties)
{
Expand Down
28 changes: 15 additions & 13 deletions tests/daemon/face/transport-test-common.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 @@ -32,25 +32,27 @@

namespace nfd::tests {

/** \brief Check that a transport has all its static properties set after initialization
/**
* \brief Check that a transport has all its static properties set after initialization.
*
* This check shall be inserted to the StaticProperties test case for each transport,
* in addition to checking the values of properties.
* When a new static property is defined, this test case shall be updated.
* Thus, if a transport forgets to set a static property, this check would fail.
* This check shall be inserted to the StaticProperties test case for each transport,
* in addition to checking the values of properties.
* When a new static property is defined, this test case shall be updated.
* Thus, if a transport forgets to set a static property, this check would fail.
*/
inline void
checkStaticPropertiesInitialized(const face::Transport& transport)
{
BOOST_CHECK(!transport.getLocalUri().getScheme().empty());
BOOST_CHECK(!transport.getRemoteUri().getScheme().empty());
BOOST_CHECK_NE(transport.getScope(), ndn::nfd::FACE_SCOPE_NONE);
BOOST_CHECK_NE(transport.getPersistency(), ndn::nfd::FACE_PERSISTENCY_NONE);
BOOST_CHECK_NE(transport.getLinkType(), ndn::nfd::LINK_TYPE_NONE);
BOOST_CHECK_NE(transport.getMtu(), face::MTU_INVALID);
BOOST_TEST(transport.getLocalUri().getScheme().empty() == false);
BOOST_TEST(transport.getRemoteUri().getScheme().empty() == false);
BOOST_TEST(transport.getScope() != ndn::nfd::FACE_SCOPE_NONE);
BOOST_TEST(transport.getPersistency() != ndn::nfd::FACE_PERSISTENCY_NONE);
BOOST_TEST(transport.getLinkType() != ndn::nfd::LINK_TYPE_NONE);
BOOST_TEST(transport.getMtu() != face::MTU_INVALID);
}

/** \brief Generic wrapper for transport fixtures that require an IP address
/**
* \brief Generic wrapper for transport fixtures that require an IP address.
*/
template<typename TransportFixtureBase,
AddressFamily AF = AddressFamily::Any,
Expand Down
Loading

0 comments on commit d7083a5

Please sign in to comment.