face: introduce NullTransport and use it in several places

Refs: #4528
Change-Id: Icc7a77232bca1a700ce1319493c15a85d05d7488
diff --git a/tests/daemon/face/dummy-face.cpp b/tests/daemon/face/dummy-face.cpp
index 5c9895b..367afed 100644
--- a/tests/daemon/face/dummy-face.cpp
+++ b/tests/daemon/face/dummy-face.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -54,29 +54,29 @@
   signal::Signal<LinkService, uint32_t> afterSend;
 
 private:
-  virtual void
-  doSendInterest(const Interest& interest) override
+  void
+  doSendInterest(const Interest& interest) final
   {
     this->sentInterests.push_back(interest);
     this->afterSend(tlv::Interest);
   }
 
-  virtual void
-  doSendData(const Data& data) override
+  void
+  doSendData(const Data& data) final
   {
     this->sentData.push_back(data);
     this->afterSend(tlv::Data);
   }
 
-  virtual void
-  doSendNack(const lp::Nack& nack) override
+  void
+  doSendNack(const lp::Nack& nack) final
   {
     this->sentNacks.push_back(nack);
     this->afterSend(lp::tlv::Nack);
   }
 
-  virtual void
-  doReceivePacket(Transport::Packet&& packet) override
+  void
+  doReceivePacket(Transport::Packet&&) final
   {
     BOOST_ASSERT(false);
   }
@@ -102,7 +102,7 @@
 void
 DummyFace::setState(FaceState state)
 {
-  this->getTransportInternal()->setState(state);
+  static_cast<DummyTransport*>(this->getTransport())->setState(state);
 }
 
 void
@@ -129,12 +129,6 @@
   return static_cast<LinkService*>(this->getLinkService());
 }
 
-DummyTransport*
-DummyFace::getTransportInternal()
-{
-  return static_cast<DummyTransport*>(this->getTransport());
-}
-
 } // namespace tests
 } // namespace face
 } // namespace nfd
diff --git a/tests/daemon/face/dummy-face.hpp b/tests/daemon/face/dummy-face.hpp
index e4c68b3..93bcd48 100644
--- a/tests/daemon/face/dummy-face.hpp
+++ b/tests/daemon/face/dummy-face.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -32,8 +32,6 @@
 namespace face {
 namespace tests {
 
-class DummyTransport;
-
 /** \brief a Face for unit testing
  *
  *  The DummyFace has no underlying transport, but allows observing outgoing packets
@@ -87,9 +85,6 @@
   LinkService*
   getLinkServiceInternal();
 
-  DummyTransport*
-  getTransportInternal();
-
 public:
   std::vector<Interest>& sentInterests;
   std::vector<Data>& sentData;
@@ -102,6 +97,7 @@
 namespace tests {
 using nfd::face::tests::DummyFace;
 } // namespace tests
+
 } // namespace nfd
 
 #endif // NFD_TESTS_DAEMON_FACE_DUMMY_FACE_HPP
diff --git a/tests/daemon/face/dummy-transport.hpp b/tests/daemon/face/dummy-transport.hpp
index 08a7141..1594df8 100644
--- a/tests/daemon/face/dummy-transport.hpp
+++ b/tests/daemon/face/dummy-transport.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -28,47 +28,39 @@
 
 #include "core/common.hpp"
 
-#include "face/transport.hpp"
+#include "face/null-transport.hpp"
 
 namespace nfd {
 namespace face {
 namespace tests {
 
-/** \brief dummy Transport used in unit tests
+/** \brief Dummy Transport type used in unit tests.
+ *
+ *  All packets sent through this transport are stored in `sentPackets`.
+ *  Reception of a packet can be simulated by invoking `receivePacket()`.
+ *  All persistency changes are recorded in `persistencyHistory`.
  */
-class DummyTransport : public Transport
+template<bool CAN_CHANGE_PERSISTENCY>
+class DummyTransportBase : public NullTransport
 {
 public:
-  DummyTransport(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,
-                 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT,
-                 ssize_t mtu = MTU_UNLIMITED,
-                 ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
-    : isClosed(false)
-    , m_sendQueueLength(0)
+  explicit
+  DummyTransportBase(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,
+                     ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT,
+                     ssize_t mtu = MTU_UNLIMITED,
+                     ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
+    : NullTransport(FaceUri(localUri), FaceUri(remoteUri), scope, persistency)
   {
-    this->setLocalUri(FaceUri(localUri));
-    this->setRemoteUri(FaceUri(remoteUri));
-    this->setScope(scope);
-    this->setPersistency(persistency);
     this->setLinkType(linkType);
     this->setMtu(mtu);
     this->setSendQueueCapacity(sendQueueCapacity);
   }
 
-  void
-  setMtu(ssize_t mtu)
-  {
-    this->Transport::setMtu(mtu);
-  }
-
-  void
-  setState(FaceState state)
-  {
-    this->Transport::setState(state);
-  }
+  using NullTransport::setMtu;
+  using NullTransport::setState;
 
   ssize_t
   getSendQueueLength() override
@@ -83,46 +75,41 @@
   }
 
   void
-  receivePacket(Packet&& packet)
-  {
-    this->receive(std::move(packet));
-  }
-
-  void
   receivePacket(Block block)
   {
-    this->receive(Packet(std::move(block)));
+    receive(Packet(std::move(block)));
   }
 
 protected:
   bool
-  canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const override
+  canChangePersistencyToImpl(ndn::nfd::FacePersistency) const override
   {
-    return true;
+    return CAN_CHANGE_PERSISTENCY;
+  }
+
+  void
+  afterChangePersistency(ndn::nfd::FacePersistency old) override
+  {
+    persistencyHistory.push_back(old);
   }
 
 private:
   void
-  doClose() override
-  {
-    isClosed = true;
-    this->setState(TransportState::CLOSED);
-  }
-
-  void
   doSend(Packet&& packet) override
   {
     sentPackets.push_back(std::move(packet));
   }
 
 public:
-  bool isClosed;
+  std::vector<ndn::nfd::FacePersistency> persistencyHistory;
   std::vector<Packet> sentPackets;
 
 private:
-  ssize_t m_sendQueueLength;
+  ssize_t m_sendQueueLength = 0;
 };
 
+using DummyTransport = DummyTransportBase<true>;
+
 } // namespace tests
 } // namespace face
 } // namespace nfd
diff --git a/tests/daemon/face/generic-link-service.t.cpp b/tests/daemon/face/generic-link-service.t.cpp
index cf51e54..64682c0 100644
--- a/tests/daemon/face/generic-link-service.t.cpp
+++ b/tests/daemon/face/generic-link-service.t.cpp
@@ -1362,7 +1362,7 @@
 
   Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
 
-  BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
+  BOOST_CHECK_NO_THROW(transport->receivePacket(std::move(packet)));
 
   BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
   BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
@@ -1380,7 +1380,7 @@
   Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
   BOOST_CHECK_THROW(packet.parse(), tlv::Error);
 
-  BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
+  BOOST_CHECK_NO_THROW(transport->receivePacket(std::move(packet)));
 
   BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
   BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
diff --git a/tests/daemon/face/null-face.t.cpp b/tests/daemon/face/null-face.t.cpp
index 6d34770..d7f7d29 100644
--- a/tests/daemon/face/null-face.t.cpp
+++ b/tests/daemon/face/null-face.t.cpp
@@ -24,6 +24,7 @@
  */
 
 #include "face/null-face.hpp"
+#include "face/null-transport.hpp"
 
 #include "tests/daemon/global-io-fixture.hpp"
 #include "transport-test-common.hpp"
@@ -37,29 +38,65 @@
 BOOST_AUTO_TEST_SUITE(Face)
 BOOST_FIXTURE_TEST_SUITE(TestNullFace, GlobalIoFixture)
 
-using nfd::Face;
-
 BOOST_AUTO_TEST_CASE(StaticProperties)
 {
-  shared_ptr<Face> face = makeNullFace(FaceUri("testnull://hhppt12sy"));
-  checkStaticPropertiesInitialized(*face->getTransport());
+  FaceUri uri("testnull://hhppt12sy");
+  auto face = makeNullFace(uri);
+  auto transport = face->getTransport();
 
-  BOOST_CHECK_EQUAL(face->getLocalUri(), FaceUri("testnull://hhppt12sy"));
-  BOOST_CHECK_EQUAL(face->getRemoteUri(), FaceUri("testnull://hhppt12sy"));
-  BOOST_CHECK_EQUAL(face->getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
+  checkStaticPropertiesInitialized(*transport);
+
+  BOOST_CHECK_EQUAL(transport->getLocalUri(), uri);
+  BOOST_CHECK_EQUAL(transport->getRemoteUri(), uri);
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+  BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
+}
+
+BOOST_AUTO_TEST_CASE(ExpirationTime)
+{
+  auto transport = make_unique<NullTransport>();
+
+  BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
+}
+
+BOOST_AUTO_TEST_CASE(SendQueue)
+{
+  auto transport = make_unique<NullTransport>();
+
+  BOOST_CHECK_EQUAL(transport->getSendQueueCapacity(), QUEUE_UNSUPPORTED);
+  BOOST_CHECK_EQUAL(transport->getSendQueueLength(), QUEUE_UNSUPPORTED);
 }
 
 BOOST_AUTO_TEST_CASE(Send)
 {
-  shared_ptr<Face> face = makeNullFace();
+  auto face = makeNullFace();
+  BOOST_CHECK_EQUAL(face->getState(), FaceState::UP);
 
-  shared_ptr<Interest> interest = makeInterest("/A");
-  BOOST_CHECK_NO_THROW(face->sendInterest(*interest));
+  face->sendInterest(*makeInterest("/A"));
+  BOOST_CHECK_EQUAL(face->getState(), FaceState::UP);
 
-  shared_ptr<Data> data = makeData("/B");
-  BOOST_CHECK_NO_THROW(face->sendData(*data));
+  face->sendData(*makeData("/B"));
+  BOOST_CHECK_EQUAL(face->getState(), FaceState::UP);
+}
 
-  BOOST_CHECK_NO_THROW(face->close());
+BOOST_AUTO_TEST_CASE(PersistencyChange)
+{
+  auto transport = make_unique<NullTransport>();
+
+  BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND), false);
+  BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERSISTENT), false);
+  BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
+}
+
+BOOST_AUTO_TEST_CASE(Close)
+{
+  auto transport = make_unique<NullTransport>();
+  BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
+
+  transport->close();
+  BOOST_CHECK_EQUAL(transport->getState(), TransportState::CLOSED);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestNullFace
diff --git a/tests/daemon/face/transport.t.cpp b/tests/daemon/face/transport.t.cpp
index 747622e..fbe3ee5 100644
--- a/tests/daemon/face/transport.t.cpp
+++ b/tests/daemon/face/transport.t.cpp
@@ -26,10 +26,10 @@
 #include "face/transport.hpp"
 #include "face/face.hpp"
 
+#include "tests/test-common.hpp"
 #include "tests/daemon/global-io-fixture.hpp"
 #include "dummy-receive-link-service.hpp"
 #include "dummy-transport.hpp"
-#include "transport-test-common.hpp"
 
 #include <boost/mpl/fold.hpp>
 #include <boost/mpl/int.hpp>
@@ -50,37 +50,10 @@
 BOOST_AUTO_TEST_SUITE(Face)
 BOOST_AUTO_TEST_SUITE(TestTransport)
 
-BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
-{
-  auto transport = make_unique<DummyTransport>();
-  checkStaticPropertiesInitialized(*transport);
-}
-
-class PersistencyTestTransport : public DummyTransport
-{
-public:
-  PersistencyTestTransport()
-    : DummyTransport("dummy://", "dummy://",
-                     ndn::nfd::FACE_SCOPE_NON_LOCAL,
-                     ndn::nfd::FACE_PERSISTENCY_ON_DEMAND)
-  {
-  }
-
-protected:
-  void
-  afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
-  {
-    persistencyHistory.push_back(oldPersistency);
-  }
-
-public:
-  std::vector<ndn::nfd::FacePersistency> persistencyHistory;
-};
-
 BOOST_AUTO_TEST_CASE(PersistencyChange)
 {
-  auto transport = make_unique<PersistencyTestTransport>();
-  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+  auto transport = make_unique<DummyTransport>();
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
   BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
 
   BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_NONE), false);
@@ -88,13 +61,13 @@
   BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
 
   transport->setPersistency(transport->getPersistency());
-  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
   BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
 
   transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
   BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
   BOOST_REQUIRE_EQUAL(transport->persistencyHistory.size(), 1);
-  BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+  BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
 }
 
 /** \brief a macro to declare a TransportState as a integral constant
@@ -205,13 +178,6 @@
   }
 }
 
-BOOST_AUTO_TEST_CASE(NoExpirationTime)
-{
-  auto transport = make_unique<DummyTransport>();
-
-  BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
-}
-
 class DummyTransportFixture : public GlobalIoFixture
 {
 protected:
@@ -225,9 +191,9 @@
   }
 
   void
-  initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
+  initialize(unique_ptr<DummyTransport> t = make_unique<DummyTransport>())
   {
-    this->face = make_unique<nfd::Face>(make_unique<DummyReceiveLinkService>(), std::move(transport));
+    this->face = make_unique<nfd::Face>(make_unique<DummyReceiveLinkService>(), std::move(t));
     this->transport = static_cast<DummyTransport*>(face->getTransport());
     this->sentPackets = &this->transport->sentPackets;
     this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;