tests: MulticastUdpTransport test suite improvements

refs #3346

Change-Id: I31b272b9b99d97ed317cf534d9e1bc4592bde77d
diff --git a/tests/daemon/face/datagram-transport.t.cpp b/tests/daemon/face/datagram-transport.t.cpp
index 70403d6..96585ae 100644
--- a/tests/daemon/face/datagram-transport.t.cpp
+++ b/tests/daemon/face/datagram-transport.t.cpp
@@ -24,6 +24,7 @@
  */
 
 #include "unicast-udp-transport-fixture.hpp"
+#include "multicast-udp-transport-fixture.hpp"
 
 #include <boost/mpl/vector.hpp>
 
@@ -34,11 +35,14 @@
 BOOST_AUTO_TEST_SUITE(Face)
 BOOST_AUTO_TEST_SUITE(TestDatagramTransport)
 
-typedef boost::mpl::vector<UnicastUdpTransportFixture> DatagramTransportFixtures;
+typedef boost::mpl::vector<UnicastUdpTransportFixture,
+                           MulticastUdpTransportFixture
+                           > DatagramTransportFixtures;
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, DatagramTransportFixtures, T)
 {
-  this->initialize();
+  SKIP_IF_IP_UNAVAILABLE(this->defaultAddr);
+  this->initialize(this->defaultAddr);
 
   auto block1 = ndn::encoding::makeStringBlock(300, "hello");
   this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
@@ -46,13 +50,7 @@
   BOOST_CHECK_EQUAL(this->transport->getCounters().nOutBytes, block1.size());
 
   std::vector<uint8_t> readBuf(block1.size());
-  this->remoteSocket.async_receive(boost::asio::buffer(readBuf),
-    [this] (const boost::system::error_code& error, size_t) {
-      BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
-      this->limitedIo.afterOp();
-    });
-
-  BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+  this->remoteRead(readBuf);
 
   BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.end(), block1.begin(), block1.end());
   BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
@@ -60,7 +58,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, DatagramTransportFixtures, T)
 {
-  this->initialize();
+  SKIP_IF_IP_UNAVAILABLE(this->defaultAddr);
+  this->initialize(this->defaultAddr);
 
   Block pkt = ndn::encoding::makeStringBlock(300, "hello");
   ndn::Buffer buf(pkt.begin(), pkt.end());
@@ -74,7 +73,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveIncomplete, T, DatagramTransportFixtures, T)
 {
-  this->initialize();
+  SKIP_IF_IP_UNAVAILABLE(this->defaultAddr);
+  this->initialize(this->defaultAddr);
 
   std::vector<uint8_t> buf{
     0x05, 0x03, 0x00, 0x01,
@@ -89,7 +89,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTrailingGarbage, T, DatagramTransportFixtures, T)
 {
-  this->initialize();
+  SKIP_IF_IP_UNAVAILABLE(this->defaultAddr);
+  this->initialize(this->defaultAddr);
 
   Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
   Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
@@ -107,7 +108,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, DatagramTransportFixtures, T)
 {
-  this->initialize();
+  SKIP_IF_IP_UNAVAILABLE(this->defaultAddr);
+  this->initialize(this->defaultAddr);
 
   std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
   Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
@@ -135,7 +137,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, DatagramTransportFixtures, T)
 {
-  this->initialize();
+  SKIP_IF_IP_UNAVAILABLE(this->defaultAddr);
+  this->initialize(this->defaultAddr);
 
   this->transport->close();
   BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSING);
diff --git a/tests/daemon/face/get-available-interface-ip.hpp b/tests/daemon/face/get-available-interface-ip.hpp
index 3a98632..e7088a8 100644
--- a/tests/daemon/face/get-available-interface-ip.hpp
+++ b/tests/daemon/face/get-available-interface-ip.hpp
@@ -42,19 +42,22 @@
 
 /** \brief get a non-local IP address from any available network interface
  *  \tparam A the address type, either boost::asio::ip::address_v4 or boost::asio::ip::address_v6
+ *  \param needMulticast true if the address must be chosen from a multicast-capable interface
  *  \return an IP address
  *  \retval default-constructed A, if no address is available
  */
 template<typename A>
 A
-getAvailableInterfaceIp();
+getAvailableInterfaceIp(bool needMulticast = false);
 
 template<>
 inline boost::asio::ip::address_v4
-getAvailableInterfaceIp()
+getAvailableInterfaceIp(bool needMulticast)
 {
   for (const auto& interface : listNetworkInterfaces()) {
-    if (interface.isUp() && !interface.isLoopback()) {
+    if (interface.isUp() &&
+        !interface.isLoopback() &&
+        (!needMulticast || interface.isMulticastCapable())) {
       for (const auto& address : interface.ipv4Addresses) {
         if (!address.is_unspecified() &&
             !address.is_loopback()) {
@@ -63,16 +66,17 @@
       }
     }
   }
-
   return {};
 }
 
 template<>
 inline boost::asio::ip::address_v6
-getAvailableInterfaceIp()
+getAvailableInterfaceIp(bool needMulticast)
 {
   for (const auto& interface : listNetworkInterfaces()) {
-    if (interface.isUp() && !interface.isLoopback()) {
+    if (interface.isUp() &&
+        !interface.isLoopback() &&
+        (!needMulticast || interface.isMulticastCapable())) {
       for (const auto& address : interface.ipv6Addresses) {
         if (!address.is_unspecified() &&
             !address.is_link_local() && // see #1428
@@ -82,7 +86,6 @@
       }
     }
   }
-
   return {};
 }
 
diff --git a/tests/daemon/face/multicast-udp-transport-fixture.hpp b/tests/daemon/face/multicast-udp-transport-fixture.hpp
new file mode 100644
index 0000000..639c828
--- /dev/null
+++ b/tests/daemon/face/multicast-udp-transport-fixture.hpp
@@ -0,0 +1,133 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  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_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP
+#define NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP
+
+#include "face/multicast-udp-transport.hpp"
+#include "face/face.hpp"
+
+#include "dummy-receive-link-service.hpp"
+#include "get-available-interface-ip.hpp"
+#include "tests/limited-io.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+using namespace nfd::tests;
+namespace ip = boost::asio::ip;
+using ip::udp;
+
+class MulticastUdpTransportFixture : public BaseFixture
+{
+protected:
+  MulticastUdpTransportFixture()
+    : transport(nullptr)
+    , multicastEp(ip::address::from_string("230.15.19.47"), 7070)
+    , defaultAddr(getAvailableInterfaceIp<ip::address_v4>(true))
+    , receivedPackets(nullptr)
+    , remoteSockRx(g_io)
+    , remoteSockTx(g_io)
+  {
+  }
+
+  void
+  initialize(ip::address_v4 address)
+  {
+    openMulticastSockets(remoteSockRx, remoteSockTx, multicastEp.port());
+
+    udp::socket sockRx(g_io);
+    udp::socket sockTx(g_io);
+    localEp = udp::endpoint(address, 7001);
+    openMulticastSockets(sockRx, sockTx, localEp.port());
+
+    face = make_unique<Face>(
+             make_unique<DummyReceiveLinkService>(),
+             make_unique<MulticastUdpTransport>(localEp, multicastEp, std::move(sockRx), std::move(sockTx)));
+    transport = static_cast<MulticastUdpTransport*>(face->getTransport());
+    receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+
+    BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
+  }
+
+  void
+  openMulticastSockets(udp::socket& rx, udp::socket& tx, uint16_t port)
+  {
+    rx.open(udp::v4());
+    rx.set_option(udp::socket::reuse_address(true));
+    rx.bind(udp::endpoint(multicastEp.address(), port));
+    rx.set_option(ip::multicast::join_group(multicastEp.address()));
+
+    tx.open(udp::v4());
+    tx.set_option(udp::socket::reuse_address(true));
+    tx.set_option(ip::multicast::enable_loopback(true));
+    tx.bind(udp::endpoint(ip::address_v4::any(), port));
+  }
+
+  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) {
+        if (needToCheck) {
+          BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
+        }
+        limitedIo.afterOp();
+      });
+    BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+  }
+
+  void
+  remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
+  {
+    remoteSockTx.async_send_to(boost::asio::buffer(buf), udp::endpoint(multicastEp.address(), 7001),
+      [needToCheck] (const boost::system::error_code& error, size_t) {
+        if (needToCheck) {
+          BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
+        }
+      });
+    limitedIo.defer(time::seconds(1));
+  }
+
+protected:
+  LimitedIo limitedIo;
+  MulticastUdpTransport* transport;
+  udp::endpoint localEp;
+  udp::endpoint multicastEp;
+  const ip::address_v4 defaultAddr;
+  std::vector<Transport::Packet>* receivedPackets;
+
+private:
+  unique_ptr<Face> face;
+  udp::socket remoteSockRx;
+  udp::socket remoteSockTx;
+};
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP
diff --git a/tests/daemon/face/multicast-udp-transport.t.cpp b/tests/daemon/face/multicast-udp-transport.t.cpp
index 545d4b8..ac1e33c 100644
--- a/tests/daemon/face/multicast-udp-transport.t.cpp
+++ b/tests/daemon/face/multicast-udp-transport.t.cpp
@@ -23,43 +23,82 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "face/multicast-udp-transport.hpp"
 #include "transport-test-common.hpp"
 
+#include "multicast-udp-transport-fixture.hpp"
+
 namespace nfd {
 namespace face {
 namespace tests {
 
-using namespace nfd::tests;
-namespace ip = boost::asio::ip;
-using ip::udp;
-
 BOOST_AUTO_TEST_SUITE(Face)
-BOOST_FIXTURE_TEST_SUITE(TestMulticastUdpTransport, BaseFixture)
+BOOST_FIXTURE_TEST_SUITE(TestMulticastUdpTransport, MulticastUdpTransportFixture)
 
-BOOST_AUTO_TEST_CASE(StaticPropertiesIpv4)
+BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv4)
 {
-  udp::socket sockRx(g_io);
-  sockRx.open(udp::v4());
-  sockRx.set_option(udp::socket::reuse_address(true));
-  sockRx.bind(udp::endpoint(ip::address::from_string("127.0.0.1"), 7001));
+  SKIP_IF_IP_UNAVAILABLE(defaultAddr);
+  initialize(defaultAddr);
 
-  udp::socket sockTx(g_io);
-  sockTx.open(udp::v4());
-  sockTx.set_option(udp::socket::reuse_address(true));
-  sockTx.bind(udp::endpoint(udp::v4(), 7001));
+  checkStaticPropertiesInitialized(*transport);
 
-  MulticastUdpTransport transport(sockRx.local_endpoint(),
-                                  udp::endpoint(ip::address::from_string("230.15.19.47"), 7001),
-                                  std::move(sockRx), std::move(sockTx));
-  checkStaticPropertiesInitialized(transport);
+  BOOST_CHECK_EQUAL(transport->getLocalUri(),
+                    FaceUri("udp4://" + defaultAddr.to_string() + ":" + to_string(localEp.port())));
+  BOOST_CHECK_EQUAL(transport->getRemoteUri(),
+                    FaceUri("udp4://" + multicastEp.address().to_string() + ":" + to_string(multicastEp.port())));
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_MULTI_ACCESS);
+  BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 60 - 8);
+}
 
-  BOOST_CHECK_EQUAL(transport.getLocalUri(), FaceUri("udp4://127.0.0.1:7001"));
-  BOOST_CHECK_EQUAL(transport.getRemoteUri(), FaceUri("udp4://230.15.19.47:7001"));
-  BOOST_CHECK_EQUAL(transport.getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
-  BOOST_CHECK_EQUAL(transport.getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
-  BOOST_CHECK_EQUAL(transport.getLinkType(), ndn::nfd::LINK_TYPE_MULTI_ACCESS);
-  BOOST_CHECK_EQUAL(transport.getMtu(), 65535 - 60 - 8);
+BOOST_AUTO_TEST_CASE(ReceiveMultipleRemoteEndpoints)
+{
+  SKIP_IF_IP_UNAVAILABLE(defaultAddr);
+  initialize(defaultAddr);
+
+  // remoteSockRx2 unnecessary for this test case - only remoteSockTx2 is needed
+  udp::socket remoteSockTx2(g_io);
+  remoteSockTx2.open(udp::v4());
+  remoteSockTx2.set_option(udp::socket::reuse_address(true));
+  remoteSockTx2.set_option(ip::multicast::enable_loopback(true));
+  remoteSockTx2.bind(udp::endpoint(ip::address_v4::any(), 7071));
+
+  Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
+  ndn::Buffer buf1(pkt1.begin(), pkt1.end());
+  remoteWrite(buf1);
+
+  Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
+  ndn::Buffer buf2(pkt2.begin(), pkt2.end());
+  remoteWrite(buf2);
+
+  BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 2);
+  BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, buf1.size() + buf2.size());
+  BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
+
+  BOOST_REQUIRE_EQUAL(receivedPackets->size(), 2);
+  BOOST_CHECK_EQUAL(receivedPackets->at(0).remoteEndpoint,
+                    receivedPackets->at(1).remoteEndpoint);
+
+  udp::endpoint destEp(multicastEp.address(), localEp.port());
+  remoteSockTx2.async_send_to(boost::asio::buffer(buf1), destEp,
+    [] (const boost::system::error_code& error, size_t) {
+      BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
+    });
+  remoteSockTx2.async_send_to(boost::asio::buffer(buf2), destEp,
+    [] (const boost::system::error_code& error, size_t) {
+      BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
+    });
+  limitedIo.defer(time::seconds(1));
+
+  BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 4);
+  BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, 2 * buf1.size() + 2 * buf2.size());
+  BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
+
+  BOOST_REQUIRE_EQUAL(receivedPackets->size(), 4);
+  BOOST_CHECK_EQUAL(receivedPackets->at(2).remoteEndpoint,
+                    receivedPackets->at(3).remoteEndpoint);
+  BOOST_CHECK_NE(receivedPackets->at(0).remoteEndpoint,
+                 receivedPackets->at(2).remoteEndpoint);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestMulticastUdpTransport
diff --git a/tests/daemon/face/stream-transport.t.cpp b/tests/daemon/face/stream-transport.t.cpp
index 89470de..587c818 100644
--- a/tests/daemon/face/stream-transport.t.cpp
+++ b/tests/daemon/face/stream-transport.t.cpp
@@ -35,7 +35,9 @@
 BOOST_AUTO_TEST_SUITE(Face)
 BOOST_AUTO_TEST_SUITE(TestStreamTransport)
 
-typedef boost::mpl::vector<TcpTransportFixture, UnixStreamTransportFixture> StreamTransportFixtures;
+typedef boost::mpl::vector<TcpTransportFixture,
+                           UnixStreamTransportFixture
+                           > StreamTransportFixtures;
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, StreamTransportFixtures, T)
 {
diff --git a/tests/daemon/face/unicast-udp-transport-fixture.hpp b/tests/daemon/face/unicast-udp-transport-fixture.hpp
index 1ec37b0..01698cb 100644
--- a/tests/daemon/face/unicast-udp-transport-fixture.hpp
+++ b/tests/daemon/face/unicast-udp-transport-fixture.hpp
@@ -46,6 +46,7 @@
   UnicastUdpTransportFixture()
     : transport(nullptr)
     , remoteSocket(g_io)
+    , defaultAddr(ip::address_v4::loopback())
     , receivedPackets(nullptr)
   {
   }
@@ -80,6 +81,19 @@
   }
 
   void
+  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) {
+        if (needToCheck) {
+          BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
+        }
+        limitedIo.afterOp();
+      });
+    BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+  }
+
+  void
   remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
   {
     remoteSocket.async_send(boost::asio::buffer(buf),
@@ -96,6 +110,7 @@
   UnicastUdpTransport* transport;
   udp::endpoint localEp;
   udp::socket remoteSocket;
+  const ip::address defaultAddr;
   std::vector<Transport::Packet>* receivedPackets;
 
 private: