tests: merge DummyFace::LinkService with DummyReceiveLinkService

Change-Id: I0b45d80055085278bbe028ef95898d68c6e3f824
diff --git a/tests/daemon/face/dummy-face.cpp b/tests/daemon/face/dummy-face.cpp
index 367afed..9e7e0b6 100644
--- a/tests/daemon/face/dummy-face.cpp
+++ b/tests/daemon/face/dummy-face.cpp
@@ -24,109 +24,54 @@
  */
 
 #include "dummy-face.hpp"
+#include "dummy-link-service.hpp"
 #include "dummy-transport.hpp"
 
 namespace nfd {
 namespace face {
 namespace tests {
 
-class DummyFace::LinkService : public face::LinkService
-{
-public:
-  void
-  receiveInterest(const Interest& interest)
-  {
-    this->face::LinkService::receiveInterest(interest);
-  }
-
-  void
-  receiveData(const Data& data)
-  {
-    this->face::LinkService::receiveData(data);
-  }
-
-  void
-  receiveNack(const lp::Nack& nack)
-  {
-    this->face::LinkService::receiveNack(nack);
-  }
-
-  signal::Signal<LinkService, uint32_t> afterSend;
-
-private:
-  void
-  doSendInterest(const Interest& interest) final
-  {
-    this->sentInterests.push_back(interest);
-    this->afterSend(tlv::Interest);
-  }
-
-  void
-  doSendData(const Data& data) final
-  {
-    this->sentData.push_back(data);
-    this->afterSend(tlv::Data);
-  }
-
-  void
-  doSendNack(const lp::Nack& nack) final
-  {
-    this->sentNacks.push_back(nack);
-    this->afterSend(lp::tlv::Nack);
-  }
-
-  void
-  doReceivePacket(Transport::Packet&&) final
-  {
-    BOOST_ASSERT(false);
-  }
-
-public:
-  std::vector<Interest> sentInterests;
-  std::vector<Data> sentData;
-  std::vector<lp::Nack> sentNacks;
-};
-
 DummyFace::DummyFace(const std::string& localUri, const std::string& remoteUri,
                      ndn::nfd::FaceScope scope, ndn::nfd::FacePersistency persistency,
                      ndn::nfd::LinkType linkType)
-  : Face(make_unique<LinkService>(),
+  : Face(make_unique<DummyLinkService>(),
          make_unique<DummyTransport>(localUri, remoteUri, scope, persistency, linkType))
-  , afterSend(this->getLinkServiceInternal()->afterSend)
-  , sentInterests(this->getLinkServiceInternal()->sentInterests)
-  , sentData(this->getLinkServiceInternal()->sentData)
-  , sentNacks(this->getLinkServiceInternal()->sentNacks)
+  , afterSend(getDummyLinkService()->afterSend)
+  , sentInterests(getDummyLinkService()->sentInterests)
+  , sentData(getDummyLinkService()->sentData)
+  , sentNacks(getDummyLinkService()->sentNacks)
 {
+  getDummyLinkService()->setPacketLogging(LogSentPackets);
 }
 
 void
 DummyFace::setState(FaceState state)
 {
-  static_cast<DummyTransport*>(this->getTransport())->setState(state);
+  static_cast<DummyTransport*>(getTransport())->setState(state);
 }
 
 void
 DummyFace::receiveInterest(const Interest& interest)
 {
-  this->getLinkServiceInternal()->receiveInterest(interest);
+  getDummyLinkService()->receiveInterest(interest);
 }
 
 void
 DummyFace::receiveData(const Data& data)
 {
-  this->getLinkServiceInternal()->receiveData(data);
+  getDummyLinkService()->receiveData(data);
 }
 
 void
 DummyFace::receiveNack(const lp::Nack& nack)
 {
-  this->getLinkServiceInternal()->receiveNack(nack);
+  getDummyLinkService()->receiveNack(nack);
 }
 
-DummyFace::LinkService*
-DummyFace::getLinkServiceInternal()
+DummyLinkService*
+DummyFace::getDummyLinkService() const
 {
-  return static_cast<LinkService*>(this->getLinkService());
+  return static_cast<DummyLinkService*>(getLinkService());
 }
 
 } // namespace tests
diff --git a/tests/daemon/face/dummy-face.hpp b/tests/daemon/face/dummy-face.hpp
index 93bcd48..387206a 100644
--- a/tests/daemon/face/dummy-face.hpp
+++ b/tests/daemon/face/dummy-face.hpp
@@ -32,7 +32,9 @@
 namespace face {
 namespace tests {
 
-/** \brief a Face for unit testing
+class DummyLinkService;
+
+/** \brief A Face for unit testing.
  *
  *  The DummyFace has no underlying transport, but allows observing outgoing packets
  *  and injecting incoming packets at network layer.
@@ -45,8 +47,7 @@
 class DummyFace : public Face
 {
 public:
-  class LinkService;
-
+  explicit
   DummyFace(const std::string& localUri = "dummy://", const std::string& remoteUri = "dummy://",
             ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL,
             ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
@@ -73,17 +74,17 @@
   void
   receiveNack(const lp::Nack& nack);
 
-  /** \brief signals after any network-layer packet is sent
+  /** \brief Emitted after a network-layer packet is sent.
    *
-   *  The network-layer packet type is indicated as an argument,
-   *  which is either of tlv::Interest, tlv::Data, or lp::tlv::Nack.
-   *  The callback may retrieve the packet from sentInterests.back(), sentData.back(), or sentNacks.back().
+   *  The packet type is reported via the argument, whose value will be one of
+   *  tlv::Interest, tlv::Data, or lp::tlv::Nack. Signal handlers may retrieve
+   *  the packet via `sentInterests.back()`, `sentData.back()`, or `sentNacks.back()`.
    */
-  signal::Signal<LinkService, uint32_t>& afterSend;
+  signal::Signal<DummyLinkService, uint32_t>& afterSend;
 
 private:
-  LinkService*
-  getLinkServiceInternal();
+  DummyLinkService*
+  getDummyLinkService() const;
 
 public:
   std::vector<Interest>& sentInterests;
diff --git a/tests/daemon/face/dummy-receive-link-service.hpp b/tests/daemon/face/dummy-link-service.cpp
similarity index 65%
rename from tests/daemon/face/dummy-receive-link-service.hpp
rename to tests/daemon/face/dummy-link-service.cpp
index 56d72ce..2c92a52 100644
--- a/tests/daemon/face/dummy-receive-link-service.hpp
+++ b/tests/daemon/face/dummy-link-service.cpp
@@ -23,51 +23,46 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NFD_TESTS_DAEMON_FACE_DUMMY_RECEIVE_LINK_SERVICE_HPP
-#define NFD_TESTS_DAEMON_FACE_DUMMY_RECEIVE_LINK_SERVICE_HPP
-
-#include "face/link-service.hpp"
+#include "dummy-link-service.hpp"
 
 namespace nfd {
 namespace face {
 namespace tests {
 
-/** \brief A dummy LinkService that logs all received packets, for Transport testing.
- *  \warning This LinkService does not allow sending.
- */
-class DummyReceiveLinkService final : public LinkService
+void
+DummyLinkService::doSendInterest(const Interest& interest)
 {
-private:
-  void
-  doSendInterest(const Interest&) final
-  {
-    BOOST_ASSERT(false);
-  }
+  if (m_loggingFlags & LogSentInterests)
+    sentInterests.push_back(interest);
 
-  void
-  doSendData(const Data&) final
-  {
-    BOOST_ASSERT(false);
-  }
+  afterSend(tlv::Interest);
+}
 
-  void
-  doSendNack(const lp::Nack&) final
-  {
-    BOOST_ASSERT(false);
-  }
+void
+DummyLinkService::doSendData(const Data& data)
+{
+  if (m_loggingFlags & LogSentData)
+    sentData.push_back(data);
 
-  void
-  doReceivePacket(Transport::Packet&& packet) final
-  {
+  afterSend(tlv::Data);
+}
+
+void
+DummyLinkService::doSendNack(const lp::Nack& nack)
+{
+  if (m_loggingFlags & LogSentNacks)
+    sentNacks.push_back(nack);
+
+  afterSend(lp::tlv::Nack);
+}
+
+void
+DummyLinkService::doReceivePacket(Transport::Packet&& packet)
+{
+  if (m_loggingFlags & LogReceivedPackets)
     receivedPackets.push_back(std::move(packet));
-  }
-
-public:
-  std::vector<Transport::Packet> receivedPackets;
-};
+}
 
 } // namespace tests
 } // namespace face
 } // namespace nfd
-
-#endif // NFD_TESTS_DAEMON_FACE_DUMMY_RECEIVE_LINK_SERVICE_HPP
diff --git a/tests/daemon/face/dummy-link-service.hpp b/tests/daemon/face/dummy-link-service.hpp
new file mode 100644
index 0000000..3717336
--- /dev/null
+++ b/tests/daemon/face/dummy-link-service.hpp
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  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_DUMMY_LINK_SERVICE_HPP
+#define NFD_TESTS_DAEMON_FACE_DUMMY_LINK_SERVICE_HPP
+
+#include "face/link-service.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+enum PacketLoggingFlags : unsigned {
+  LogNothing          = 0,      ///< disable packet logging
+  LogSentInterests    = 1 << 0, ///< log sent Interest packets
+  LogSentData         = 1 << 1, ///< log sent Data packets
+  LogSentNacks        = 1 << 2, ///< log sent Nack packets
+  LogSentPackets      = LogSentInterests | LogSentData | LogSentNacks, ///< log all sent packets
+  LogReceivedPackets  = 1 << 3, ///< log all received packets (as Transport::Packet)
+  LogAllPackets       = LogSentPackets | LogReceivedPackets, ///< log all sent and received packets
+};
+
+/** \brief A dummy LinkService that logs all sent and received packets.
+ */
+class DummyLinkService final : public LinkService
+{
+public:
+  /** \brief Emitted after a network-layer packet is sent through this link service.
+   *
+   *  The packet type is reported via the argument, whose value will be one of
+   *  tlv::Interest, tlv::Data, or lp::tlv::Nack. Signal handlers may retrieve
+   *  the packet via `sentInterests.back()`, `sentData.back()`, or `sentNacks.back()`.
+   */
+  signal::Signal<DummyLinkService, uint32_t> afterSend;
+
+  using LinkService::receiveInterest;
+  using LinkService::receiveData;
+  using LinkService::receiveNack;
+
+  void
+  setPacketLogging(std::underlying_type_t<PacketLoggingFlags> flags)
+  {
+    m_loggingFlags = static_cast<PacketLoggingFlags>(flags);
+  }
+
+private:
+  void
+  doSendInterest(const Interest& interest) final;
+
+  void
+  doSendData(const Data& data) final;
+
+  void
+  doSendNack(const lp::Nack& nack) final;
+
+  void
+  doReceivePacket(Transport::Packet&& packet) final;
+
+public:
+  std::vector<Interest> sentInterests;
+  std::vector<Data> sentData;
+  std::vector<lp::Nack> sentNacks;
+  std::vector<Transport::Packet> receivedPackets;
+
+private:
+  PacketLoggingFlags m_loggingFlags = LogAllPackets;
+};
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_TESTS_DAEMON_FACE_DUMMY_LINK_SERVICE_HPP
diff --git a/tests/daemon/face/multicast-udp-transport-fixture.hpp b/tests/daemon/face/multicast-udp-transport-fixture.hpp
index 626c37a..cdcb26a 100644
--- a/tests/daemon/face/multicast-udp-transport-fixture.hpp
+++ b/tests/daemon/face/multicast-udp-transport-fixture.hpp
@@ -31,7 +31,7 @@
 
 #include "tests/test-common.hpp"
 #include "tests/daemon/limited-io.hpp"
-#include "dummy-receive-link-service.hpp"
+#include "tests/daemon/face/dummy-link-service.hpp"
 
 namespace nfd {
 namespace face {
@@ -78,12 +78,11 @@
     MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address);
     MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), nullptr, true);
 
-    face = make_unique<Face>(
-             make_unique<DummyReceiveLinkService>(),
-             make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
-                                                ndn::nfd::LINK_TYPE_MULTI_ACCESS));
+    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<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+    receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
 
     BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
   }
diff --git a/tests/daemon/face/tcp-transport-fixture.hpp b/tests/daemon/face/tcp-transport-fixture.hpp
index dd77a95..77208e5 100644
--- a/tests/daemon/face/tcp-transport-fixture.hpp
+++ b/tests/daemon/face/tcp-transport-fixture.hpp
@@ -31,7 +31,7 @@
 
 #include "tests/test-common.hpp"
 #include "tests/daemon/limited-io.hpp"
-#include "dummy-receive-link-service.hpp"
+#include "tests/daemon/face/dummy-link-service.hpp"
 
 namespace nfd {
 namespace face {
@@ -98,10 +98,10 @@
       scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
     }
 
-    face = make_unique<Face>(make_unique<DummyReceiveLinkService>(),
+    face = make_unique<Face>(make_unique<DummyLinkService>(),
                              make_unique<TcpTransport>(std::move(sock), persistency, scope));
     transport = static_cast<TcpTransport*>(face->getTransport());
-    receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+    receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
 
     BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
   }
diff --git a/tests/daemon/face/transport.t.cpp b/tests/daemon/face/transport.t.cpp
index fbe3ee5..348348d 100644
--- a/tests/daemon/face/transport.t.cpp
+++ b/tests/daemon/face/transport.t.cpp
@@ -28,8 +28,8 @@
 
 #include "tests/test-common.hpp"
 #include "tests/daemon/global-io-fixture.hpp"
-#include "dummy-receive-link-service.hpp"
-#include "dummy-transport.hpp"
+#include "tests/daemon/face/dummy-link-service.hpp"
+#include "tests/daemon/face/dummy-transport.hpp"
 
 #include <boost/mpl/fold.hpp>
 #include <boost/mpl/int.hpp>
@@ -193,10 +193,10 @@
   void
   initialize(unique_ptr<DummyTransport> t = make_unique<DummyTransport>())
   {
-    this->face = make_unique<nfd::Face>(make_unique<DummyReceiveLinkService>(), std::move(t));
+    this->face = make_unique<nfd::Face>(make_unique<DummyLinkService>(), std::move(t));
     this->transport = static_cast<DummyTransport*>(face->getTransport());
     this->sentPackets = &this->transport->sentPackets;
-    this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+    this->receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
   }
 
 protected:
diff --git a/tests/daemon/face/unicast-udp-transport-fixture.hpp b/tests/daemon/face/unicast-udp-transport-fixture.hpp
index dcd99ef..f9c8728 100644
--- a/tests/daemon/face/unicast-udp-transport-fixture.hpp
+++ b/tests/daemon/face/unicast-udp-transport-fixture.hpp
@@ -31,7 +31,7 @@
 
 #include "tests/test-common.hpp"
 #include "tests/daemon/limited-io.hpp"
-#include "dummy-receive-link-service.hpp"
+#include "tests/daemon/face/dummy-link-service.hpp"
 
 namespace nfd {
 namespace face {
@@ -61,11 +61,10 @@
 
     remoteConnect(address);
 
-    face = make_unique<Face>(
-             make_unique<DummyReceiveLinkService>(),
-             make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
+    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<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+    receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
 
     BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
   }
diff --git a/tests/daemon/face/unix-stream-transport-fixture.hpp b/tests/daemon/face/unix-stream-transport-fixture.hpp
index 74c1b45..17202f6 100644
--- a/tests/daemon/face/unix-stream-transport-fixture.hpp
+++ b/tests/daemon/face/unix-stream-transport-fixture.hpp
@@ -31,7 +31,7 @@
 
 #include "tests/test-common.hpp"
 #include "tests/daemon/limited-io.hpp"
-#include "dummy-receive-link-service.hpp"
+#include "tests/daemon/face/dummy-link-service.hpp"
 
 #include <boost/filesystem.hpp>
 
@@ -112,10 +112,10 @@
     BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
 
     localEp = sock.local_endpoint();
-    face = make_unique<Face>(make_unique<DummyReceiveLinkService>(),
+    face = make_unique<Face>(make_unique<DummyLinkService>(),
                              make_unique<UnixStreamTransport>(std::move(sock)));
     transport = static_cast<UnixStreamTransport*>(face->getTransport());
-    receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+    receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
 
     BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
   }
diff --git a/tests/daemon/face/websocket-transport-fixture.hpp b/tests/daemon/face/websocket-transport-fixture.hpp
index 07763cd..bcf9cf9 100644
--- a/tests/daemon/face/websocket-transport-fixture.hpp
+++ b/tests/daemon/face/websocket-transport-fixture.hpp
@@ -31,7 +31,7 @@
 
 #include "tests/test-common.hpp"
 #include "tests/daemon/limited-io.hpp"
-#include "dummy-receive-link-service.hpp"
+#include "tests/daemon/face/dummy-link-service.hpp"
 
 namespace nfd {
 namespace face {
@@ -109,11 +109,10 @@
     BOOST_REQUIRE_EQUAL(limitedIo.run(2, // serverHandleOpen, clientHandleOpen
                                       1_s), LimitedIo::EXCEED_OPS);
 
-    face = make_unique<Face>(
-             make_unique<DummyReceiveLinkService>(),
-             make_unique<WebSocketTransport>(serverHdl, std::ref(server), pingInterval));
+    face = make_unique<Face>(make_unique<DummyLinkService>(),
+                             make_unique<WebSocketTransport>(serverHdl, std::ref(server), pingInterval));
     transport = static_cast<WebSocketTransport*>(face->getTransport());
-    serverReceivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+    serverReceivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
 
     BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
   }