tests: expand test coverage of EthernetFactory and UdpFactory
Change-Id: I469ac8682502ca7a0e360703b462bfb119eaedf1
diff --git a/tests/daemon/face/ethernet-factory.t.cpp b/tests/daemon/face/ethernet-factory.t.cpp
index 0ee4562..d09c7df 100644
--- a/tests/daemon/face/ethernet-factory.t.cpp
+++ b/tests/daemon/face/ethernet-factory.t.cpp
@@ -39,11 +39,6 @@
, public FaceSystemFactoryFixture<EthernetFactory>
{
protected:
- EthernetFactoryFixture()
- {
- this->copyRealNetifsToNetmon();
- }
-
std::set<std::string>
listUrisOfAvailableNetifs() const
{
@@ -59,17 +54,37 @@
return listFacesByScheme("ether", linkType);
}
- size_t
- countEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
+ shared_ptr<ndn::net::NetworkInterface>
+ makeFakeNetif()
{
- return listEtherMcastFaces(linkType).size();
+ static int counter = 0;
+ ++counter;
+
+ auto netif = netmon->makeNetworkInterface();
+ netif->setIndex(1000 + counter);
+ netif->setName("ethdummy" + std::to_string(counter));
+ netif->setType(ndn::net::InterfaceType::ETHERNET);
+ netif->setFlags(IFF_MULTICAST | IFF_UP);
+ netif->setState(ndn::net::InterfaceState::RUNNING);
+ netif->setMtu(1500);
+ netif->setEthernetAddress(ethernet::Address{0x00, 0x00, 0x5e, 0x00, 0x53, 0x5e});
+ return netif;
+ }
+};
+
+class EthernetFactoryFixtureWithRealNetifs : public EthernetFactoryFixture
+{
+protected:
+ EthernetFactoryFixtureWithRealNetifs()
+ {
+ copyRealNetifsToNetmon();
}
};
BOOST_AUTO_TEST_SUITE(Face)
-BOOST_FIXTURE_TEST_SUITE(TestEthernetFactory, EthernetFactoryFixture)
+BOOST_AUTO_TEST_SUITE(TestEthernetFactory)
-BOOST_AUTO_TEST_SUITE(ProcessConfig)
+BOOST_FIXTURE_TEST_SUITE(ProcessConfig, EthernetFactoryFixtureWithRealNetifs)
BOOST_AUTO_TEST_CASE(Defaults)
{
@@ -79,9 +94,6 @@
face_system
{
ether
- {
- mcast no
- }
}
)CONFIG";
@@ -92,10 +104,10 @@
auto channels = factory.getChannels();
BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
[] (const auto& ch) { return ch->isListening(); }));
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), netifs.size());
}
-BOOST_AUTO_TEST_CASE(DisableListen)
+BOOST_AUTO_TEST_CASE(DisableListenAndMcast)
{
SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
@@ -118,13 +130,11 @@
auto channels = factory.getChannels();
BOOST_CHECK(std::none_of(channels.begin(), channels.end(),
[] (const auto& ch) { return ch->isListening(); }));
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
}
BOOST_AUTO_TEST_CASE(McastNormal)
{
- SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
-
const std::string CONFIG = R"CONFIG(
face_system
{
@@ -148,8 +158,9 @@
parseConfig(CONFIG, true);
parseConfig(CONFIG, false);
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size());
- for (const auto& face : this->listEtherMcastFaces()) {
+ auto etherMcastFaces = this->listEtherMcastFaces();
+ BOOST_CHECK_EQUAL(etherMcastFaces.size(), netifs.size());
+ for (const auto* face : etherMcastFaces) {
BOOST_REQUIRE(face->getChannel().lock());
// not universal, but for Ethernet, local URI of a mcast face matches URI of the associated channel
BOOST_CHECK_EQUAL(face->getLocalUri(), face->getChannel().lock()->getUri());
@@ -180,17 +191,17 @@
)CONFIG";
parseConfig(CONFIG_WITHOUT_MCAST, false);
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
parseConfig(CONFIG_WITH_MCAST, false);
g_io.poll();
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size());
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), netifs.size());
parseConfig(CONFIG_WITHOUT_MCAST, false);
g_io.poll();
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
}
BOOST_AUTO_TEST_CASE(McastAdHoc)
@@ -203,15 +214,14 @@
ether
{
listen no
- mcast yes
mcast_ad_hoc yes
}
}
)CONFIG";
parseConfig(CONFIG, false);
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(ndn::nfd::LINK_TYPE_MULTI_ACCESS), 0);
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size());
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces(ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifs.size());
}
BOOST_AUTO_TEST_CASE(ChangeMcastGroup)
@@ -238,6 +248,7 @@
)CONFIG";
parseConfig(CONFIG1, false);
+ g_io.poll();
auto etherMcastFaces = this->listEtherMcastFaces();
BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size());
BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(),
@@ -317,7 +328,7 @@
parseConfig(CONFIG, true);
parseConfig(CONFIG, false);
- BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
}
BOOST_AUTO_TEST_CASE(BadListen)
@@ -432,7 +443,7 @@
BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
-BOOST_AUTO_TEST_CASE(GetChannels)
+BOOST_FIXTURE_TEST_CASE(GetChannels, EthernetFactoryFixtureWithRealNetifs)
{
BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
@@ -442,22 +453,23 @@
checkChannelListEqual(factory, {FaceUri::fromDev(netifs.front()->getName()).toString()});
}
-BOOST_AUTO_TEST_CASE(CreateChannel)
+BOOST_FIXTURE_TEST_CASE(CreateChannel, EthernetFactoryFixtureWithRealNetifs)
{
SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
auto channel1 = factory.createChannel(netifs.front(), 1_min);
auto channel1a = factory.createChannel(netifs.front(), 5_min);
BOOST_CHECK_EQUAL(channel1, channel1a);
- BOOST_CHECK_EQUAL(channel1->getUri().toString(), "dev://" + netifs.front()->getName());
+ checkChannelListEqual(factory, {FaceUri::fromDev(netifs.front()->getName()).toString()});
SKIP_IF_ETHERNET_NETIF_COUNT_LT(2);
auto channel2 = factory.createChannel(netifs.back(), 1_min);
BOOST_CHECK_NE(channel1, channel2);
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
}
-BOOST_AUTO_TEST_CASE(CreateFace)
+BOOST_FIXTURE_TEST_CASE(CreateFace, EthernetFactoryFixtureWithRealNetifs)
{
createFace(factory,
FaceUri("ether://[00:00:5e:00:53:5e]"),
@@ -505,7 +517,7 @@
{CreateFaceExpectedResult::SUCCESS, 0, ""});
}
-BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
+BOOST_FIXTURE_TEST_CASE(CreateFaceInvalidRequest, EthernetFactoryFixture)
{
createFace(factory,
FaceUri("ether://[00:00:5e:00:53:5e]"),
@@ -541,8 +553,119 @@
{ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, true, false, false},
{CreateFaceExpectedResult::FAILURE, 406,
"Local fields can only be enabled on faces with local scope"});
+
+ createFace(factory,
+ FaceUri("ether://[00:00:5e:00:53:5e]"),
+ FaceUri("dev://eth0"),
+ {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, 42, false, false, false},
+ {CreateFaceExpectedResult::FAILURE, 406,
+ "Override MTU cannot be less than 64"});
}
+BOOST_FIXTURE_TEST_SUITE(OnInterfaceAdded, EthernetFactoryFixture)
+
+BOOST_AUTO_TEST_CASE(EligibleForChannelAndMcast)
+{
+ parseConfig(R"CONFIG(
+ face_system
+ {
+ ether
+ }
+ )CONFIG", false);
+ g_io.poll();
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+
+ // Add a fake interface that satisfies both unicast and multicast criteria.
+ netmon->addInterface(this->makeFakeNetif());
+ // The channel fails to listen because the interface is fake.
+ // However, it's not removed from the channel list (issue #4400).
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 1);
+ // Multicast face creation fails because the interface is fake.
+ // This test is to ensure that the factory handles failures gracefully.
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+
+ SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
+
+ // Now add a real interface: both channel and multicast face should be created successfully.
+ netmon->addInterface(const_pointer_cast<ndn::net::NetworkInterface>(netifs.front()));
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 2);
+ auto etherMcastFaces = this->listEtherMcastFaces();
+ BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), 1);
+ BOOST_CHECK_EQUAL(etherMcastFaces.front()->getLocalUri(), FaceUri::fromDev(netifs.front()->getName()));
+}
+
+BOOST_AUTO_TEST_CASE(EligibleForChannelOnly)
+{
+ SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
+
+ parseConfig(R"CONFIG(
+ face_system
+ {
+ ether
+ {
+ listen no
+ }
+ }
+ )CONFIG", false);
+ g_io.poll();
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+
+ // Add an interface that satisfies only the unicast criteria.
+ auto netif = const_pointer_cast<ndn::net::NetworkInterface>(netifs.front());
+ netif->setFlags(netif->getFlags() & ~IFF_MULTICAST);
+ netmon->addInterface(netif);
+ checkChannelListEqual(factory, {FaceUri::fromDev(netifs.front()->getName()).toString()});
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(Ineligible)
+{
+ parseConfig(R"CONFIG(
+ face_system
+ {
+ ether
+ }
+ )CONFIG", false);
+ g_io.poll();
+
+ // netif is down
+ auto netif = this->makeFakeNetif();
+ netif->setFlags(netif->getFlags() & ~IFF_UP);
+ netmon->addInterface(netif);
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+
+ // incompatible netif type
+ netif = this->makeFakeNetif();
+ netif->setType(ndn::net::InterfaceType::LOOPBACK);
+ netmon->addInterface(netif);
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+
+ // invalid Ethernet address
+ netif = this->makeFakeNetif();
+ netif->setEthernetAddress(ethernet::Address{});
+ netmon->addInterface(netif);
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(Disabled)
+{
+ SKIP_IF_ETHERNET_NETIF_COUNT_LT(1);
+
+ parseConfig("", false);
+ g_io.poll();
+
+ netmon->addInterface(const_pointer_cast<ndn::net::NetworkInterface>(netifs.front()));
+ BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
+ BOOST_CHECK_EQUAL(this->listEtherMcastFaces().size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // OnInterfaceAdded
+
BOOST_AUTO_TEST_SUITE_END() // TestEthernetFactory
BOOST_AUTO_TEST_SUITE_END() // Face