tests: UnicastUdpTransport test suite improvements

Fixed m_hasBeenUsedRecently uninitialized in DatagramTransport

refs #3307

Change-Id: I786a84009d9cf01e0bb211c872d4375f46c747fd
diff --git a/tests/daemon/face/datagram-transport.t.cpp b/tests/daemon/face/datagram-transport.t.cpp
new file mode 100644
index 0000000..70403d6
--- /dev/null
+++ b/tests/daemon/face/datagram-transport.t.cpp
@@ -0,0 +1,156 @@
+/* -*- 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/>.
+ */
+
+#include "unicast-udp-transport-fixture.hpp"
+
+#include <boost/mpl/vector.hpp>
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Face)
+BOOST_AUTO_TEST_SUITE(TestDatagramTransport)
+
+typedef boost::mpl::vector<UnicastUdpTransportFixture> DatagramTransportFixtures;
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Send, T, DatagramTransportFixtures, T)
+{
+  this->initialize();
+
+  auto block1 = ndn::encoding::makeStringBlock(300, "hello");
+  this->transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nOutPackets, 1);
+  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);
+
+  BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.end(), block1.begin(), block1.end());
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveNormal, T, DatagramTransportFixtures, T)
+{
+  this->initialize();
+
+  Block pkt = ndn::encoding::makeStringBlock(300, "hello");
+  ndn::Buffer buf(pkt.begin(), pkt.end());
+  this->remoteWrite(buf);
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, pkt.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveIncomplete, T, DatagramTransportFixtures, T)
+{
+  this->initialize();
+
+  std::vector<uint8_t> buf{
+    0x05, 0x03, 0x00, 0x01,
+  };
+  this->remoteWrite(buf);
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTrailingGarbage, T, DatagramTransportFixtures, T)
+{
+  this->initialize();
+
+  Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
+  Block pkt2 = ndn::encoding::makeStringBlock(301, "world");
+  ndn::Buffer buf(pkt1.size() + pkt2.size());
+  std::copy(pkt1.begin(), pkt1.end(), buf.buf());
+  std::copy(pkt2.begin(), pkt2.end(), buf.buf() + pkt1.size());
+
+  this->remoteWrite(buf);
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ReceiveTooLarge, T, DatagramTransportFixtures, T)
+{
+  this->initialize();
+
+  std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
+  Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
+  ndn::Buffer buf1(pkt1.begin(), pkt1.end());
+  BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
+
+  Block pkt2 = ndn::encoding::makeBinaryBlock(301, bytes.data(), bytes.size());
+  ndn::Buffer buf2(pkt2.begin(), pkt2.end());
+  BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
+
+  this->remoteWrite(buf1); // this should succeed
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+
+  this->remoteWrite(buf2, false); // this should fail
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, DatagramTransportFixtures, T)
+{
+  this->initialize();
+
+  this->transport->close();
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSING);
+
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+    this->limitedIo.afterOp();
+  });
+  BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestDatagramTransport
+BOOST_AUTO_TEST_SUITE_END() // Face
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
diff --git a/tests/daemon/face/stream-transport.t.cpp b/tests/daemon/face/stream-transport.t.cpp
index 24b8b43..89470de 100644
--- a/tests/daemon/face/stream-transport.t.cpp
+++ b/tests/daemon/face/stream-transport.t.cpp
@@ -26,6 +26,8 @@
 #include "tcp-transport-fixture.hpp"
 #include "unix-stream-transport-fixture.hpp"
 
+#include <boost/mpl/vector.hpp>
+
 namespace nfd {
 namespace face {
 namespace tests {
@@ -122,27 +124,67 @@
 {
   this->initialize();
 
-  std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE + 1, 0);
-  Block pkt = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size());
-  ndn::Buffer buf(pkt.begin(), pkt.end());
+  std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
+  Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
+  ndn::Buffer buf1(pkt1.begin(), pkt1.end());
+  BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
 
-  this->remoteWrite(buf, false);
+  Block pkt2 = ndn::encoding::makeBinaryBlock(301, bytes.data(), bytes.size());
+  ndn::Buffer buf2(pkt2.begin(), pkt2.end());
+  BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
 
-  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
-  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
-  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+  this->remoteWrite(buf1); // this should succeed
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+
+  int nStateChanges = 0;
+  this->transport->afterStateChange.connect(
+    [this, &nStateChanges] (TransportState oldState, TransportState newState) {
+      switch (nStateChanges) {
+      case 0:
+        BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+        BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
+        break;
+      case 1:
+        BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
+        BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+        break;
+      default:
+        BOOST_CHECK(false);
+      }
+      nStateChanges++;
+    });
+
+  this->remoteWrite(buf2, false); // this should fail
+
+  BOOST_CHECK_EQUAL(nStateChanges, 2);
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
 }
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
 {
   this->initialize();
 
-  this->transport->close();
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSING);
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
+  });
 
-  this->g_io.poll();
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+  this->transport->close();
+
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+    this->limitedIo.afterOp();
+  });
+
+  BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
 }
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
@@ -158,8 +200,13 @@
   this->remoteSocket.close();
   BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
 
-  this->g_io.poll();
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+    this->limitedIo.afterOp();
+  });
+
+  BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
@@ -167,4 +214,4 @@
 
 } // namespace tests
 } // namespace face
-} // namespace nfd
\ No newline at end of file
+} // namespace nfd
diff --git a/tests/daemon/face/tcp-transport-fixture.hpp b/tests/daemon/face/tcp-transport-fixture.hpp
index 03594a1..38be67b 100644
--- a/tests/daemon/face/tcp-transport-fixture.hpp
+++ b/tests/daemon/face/tcp-transport-fixture.hpp
@@ -30,9 +30,6 @@
 #include "face/lp-face.hpp"
 
 #include "dummy-receive-link-service.hpp"
-#include "get-available-interface-ip.hpp"
-#include "transport-test-common.hpp"
-
 #include "tests/limited-io.hpp"
 
 namespace nfd {
@@ -86,7 +83,7 @@
   }
 
   void
-  remoteWrite(const ndn::Buffer& buf, bool needToCheck = true)
+  remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
   {
     boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
       [needToCheck] (const boost::system::error_code& error, size_t) {
@@ -94,7 +91,7 @@
           BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
         }
       });
-    limitedIo.defer(time::milliseconds(50));
+    limitedIo.defer(time::seconds(1));
   }
 
 protected:
diff --git a/tests/daemon/face/tcp-transport.t.cpp b/tests/daemon/face/tcp-transport.t.cpp
index 3326908..4871e3c 100644
--- a/tests/daemon/face/tcp-transport.t.cpp
+++ b/tests/daemon/face/tcp-transport.t.cpp
@@ -23,6 +23,9 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "get-available-interface-ip.hpp"
+#include "transport-test-common.hpp"
+
 #include "tcp-transport-fixture.hpp"
 
 namespace nfd {
@@ -30,7 +33,6 @@
 namespace tests {
 
 BOOST_AUTO_TEST_SUITE(Face)
-
 BOOST_FIXTURE_TEST_SUITE(TestTcpTransport, TcpTransportFixture)
 
 BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4)
diff --git a/tests/daemon/face/unicast-udp-transport-fixture.hpp b/tests/daemon/face/unicast-udp-transport-fixture.hpp
new file mode 100644
index 0000000..6dd836a
--- /dev/null
+++ b/tests/daemon/face/unicast-udp-transport-fixture.hpp
@@ -0,0 +1,109 @@
+/* -*- 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_UNICAST_UDP_TRANSPORT_FIXTURE_HPP
+#define NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP
+
+#include "face/unicast-udp-transport.hpp"
+#include "face/lp-face.hpp"
+
+#include "dummy-receive-link-service.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 UnicastUdpTransportFixture : public BaseFixture
+{
+protected:
+  UnicastUdpTransportFixture()
+    : transport(nullptr)
+    , remoteSocket(g_io)
+    , receivedPackets(nullptr)
+  {
+  }
+
+  void
+  initialize(ip::address address = ip::address_v4::loopback(),
+             ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
+  {
+    udp::socket sock(g_io);
+    sock.connect(udp::endpoint(address, 7070));
+    localEp = sock.local_endpoint();
+
+    remoteConnect(address);
+
+    face = make_unique<LpFace>(make_unique<DummyReceiveLinkService>(),
+                               make_unique<UnicastUdpTransport>(std::move(sock), persistency,
+                                                                time::seconds(3)));
+    transport = static_cast<UnicastUdpTransport*>(face->getTransport());
+    receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
+
+    BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
+  }
+
+  void
+  remoteConnect(ip::address address = ip::address_v4::loopback())
+  {
+    udp::endpoint remoteEp(address, 7070);
+    remoteSocket.open(remoteEp.protocol());
+    remoteSocket.set_option(udp::socket::reuse_address(true));
+    remoteSocket.bind(remoteEp);
+    remoteSocket.connect(localEp);
+  }
+
+  void
+  remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
+  {
+    remoteSocket.async_send(boost::asio::buffer(buf),
+      [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;
+  UnicastUdpTransport* transport;
+  udp::endpoint localEp;
+  udp::socket remoteSocket;
+  std::vector<Transport::Packet>* receivedPackets;
+
+private:
+  unique_ptr<LpFace> face;
+};
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP
diff --git a/tests/daemon/face/unicast-udp-transport.t.cpp b/tests/daemon/face/unicast-udp-transport.t.cpp
index 90f0a40..0d66fa3 100644
--- a/tests/daemon/face/unicast-udp-transport.t.cpp
+++ b/tests/daemon/face/unicast-udp-transport.t.cpp
@@ -23,48 +23,176 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "face/unicast-udp-transport.hpp"
+#include "get-available-interface-ip.hpp"
 #include "transport-test-common.hpp"
 
+#include "unicast-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(TestUnicastUdpTransport, BaseFixture)
+BOOST_FIXTURE_TEST_SUITE(TestUnicastUdpTransport, UnicastUdpTransportFixture)
 
-BOOST_AUTO_TEST_CASE(StaticPropertiesIpv4)
+BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4)
 {
-  udp::socket sock(g_io, udp::endpoint(ip::address_v4::loopback(), 7001));
-  sock.connect(udp::endpoint(ip::address_v4::loopback(), 7002));
-  UnicastUdpTransport transport(std::move(sock), ndn::nfd::FACE_PERSISTENCY_PERSISTENT, time::seconds(300));
-  checkStaticPropertiesInitialized(transport);
+  initialize();
 
-  BOOST_CHECK_EQUAL(transport.getLocalUri(), FaceUri("udp4://127.0.0.1:7001"));
-  BOOST_CHECK_EQUAL(transport.getRemoteUri(), FaceUri("udp4://127.0.0.1:7002"));
-  BOOST_CHECK_EQUAL(transport.getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local
-  BOOST_CHECK_EQUAL(transport.getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
-  BOOST_CHECK_EQUAL(transport.getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
-  BOOST_CHECK_EQUAL(transport.getMtu(), 65535 - 60 - 8);
+  checkStaticPropertiesInitialized(*transport);
+
+  BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("udp4://127.0.0.1:" + to_string(localEp.port())));
+  BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("udp4://127.0.0.1:7070"));
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+  BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 60 - 8);
 }
 
-BOOST_AUTO_TEST_CASE(StaticPropertiesIpv6)
+BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv6)
 {
-  udp::socket sock(g_io, udp::endpoint(ip::address_v6::loopback(), 7001));
-  sock.connect(udp::endpoint(ip::address_v6::loopback(), 7002));
-  UnicastUdpTransport transport(std::move(sock), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, time::seconds(300));
-  checkStaticPropertiesInitialized(transport);
+  initialize(ip::address_v6::loopback());
 
-  BOOST_CHECK_EQUAL(transport.getLocalUri(), FaceUri("udp6://[::1]:7001"));
-  BOOST_CHECK_EQUAL(transport.getRemoteUri(), FaceUri("udp6://[::1]:7002"));
-  BOOST_CHECK_EQUAL(transport.getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local
-  BOOST_CHECK_EQUAL(transport.getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
-  BOOST_CHECK_EQUAL(transport.getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
-  BOOST_CHECK_EQUAL(transport.getMtu(), 65535 - 8);
+  checkStaticPropertiesInitialized(*transport);
+
+  BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("udp6://[::1]:" + to_string(localEp.port())));
+  BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("udp6://[::1]:7070"));
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+  BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 8);
+}
+
+BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv4)
+{
+  auto address = getAvailableInterfaceIp<ip::address_v4>();
+  SKIP_IF_IP_UNAVAILABLE(address);
+  initialize(address);
+
+  checkStaticPropertiesInitialized(*transport);
+
+  BOOST_CHECK_EQUAL(transport->getLocalUri(),
+                    FaceUri("udp4://" + address.to_string() + ":" + to_string(localEp.port())));
+  BOOST_CHECK_EQUAL(transport->getRemoteUri(),
+                    FaceUri("udp4://" + address.to_string() + ":7070"));
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+  BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 60 - 8);
+}
+
+BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv6)
+{
+  auto address = getAvailableInterfaceIp<ip::address_v6>();
+  SKIP_IF_IP_UNAVAILABLE(address);
+  initialize(address);
+
+  checkStaticPropertiesInitialized(*transport);
+
+  BOOST_CHECK_EQUAL(transport->getLocalUri(),
+                    FaceUri("udp6://[" + address.to_string() + "]:" + to_string(localEp.port())));
+  BOOST_CHECK_EQUAL(transport->getRemoteUri(),
+                    FaceUri("udp6://[" + address.to_string() + "]:7070"));
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+  BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 8);
+}
+
+BOOST_AUTO_TEST_CASE(IdleClose)
+{
+  initialize(ip::address_v4::loopback(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+
+  int nStateChanges = 0;
+  this->transport->afterStateChange.connect(
+    [this, &nStateChanges] (TransportState oldState, TransportState newState) {
+      switch (nStateChanges) {
+      case 0:
+        BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+        BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
+        break;
+      case 1:
+        BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
+        BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+        break;
+      default:
+        BOOST_CHECK(false);
+      }
+      nStateChanges++;
+      this->limitedIo.afterOp();
+    });
+
+  BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(10)), LimitedIo::EXCEED_OPS);
+
+  BOOST_CHECK_EQUAL(nStateChanges, 2);
+}
+
+typedef std::integral_constant<ndn::nfd::FacePersistency, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND> OnDemand;
+typedef std::integral_constant<ndn::nfd::FacePersistency, ndn::nfd::FACE_PERSISTENCY_PERSISTENT> Persistent;
+typedef boost::mpl::vector<OnDemand, Persistent> RemoteClosePersistencies;
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, Persistency, RemoteClosePersistencies)
+{
+  initialize(ip::address_v4::loopback(), Persistency::value);
+
+  transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+    BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
+    this->limitedIo.afterOp();
+  });
+
+  remoteSocket.close();
+  Transport::Packet pkt(ndn::encoding::makeStringBlock(300, "hello"));
+  transport->send(std::move(pkt)); // trigger ICMP error
+  BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+  transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+    this->limitedIo.afterOp();
+  });
+
+  BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+}
+
+BOOST_AUTO_TEST_CASE(RemoteClosePermanent)
+{
+  initialize(ip::address_v4::loopback(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
+
+  remoteSocket.close();
+
+  Block block1 = ndn::encoding::makeStringBlock(300, "hello");
+  transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
+  BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 1);
+  BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, block1.size());
+
+  limitedIo.defer(time::seconds(1));
+  BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
+
+  remoteConnect();
+
+  transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
+  BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
+  BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, 2 * block1.size());
+
+  std::vector<uint8_t> readBuf(block1.size());
+  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(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+  BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.end(), block1.begin(), block1.end());
+
+  Block block2 = ndn::encoding::makeStringBlock(301, "world");
+  ndn::Buffer buf(block2.begin(), block2.end());
+  remoteWrite(buf);
+
+  BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, block2.size());
+  BOOST_CHECK_EQUAL(receivedPackets->size(), 1);
+  BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestUnicastUdpTransport
diff --git a/tests/daemon/face/unix-stream-transport-fixture.hpp b/tests/daemon/face/unix-stream-transport-fixture.hpp
index 86c8af0..38c9b5f 100644
--- a/tests/daemon/face/unix-stream-transport-fixture.hpp
+++ b/tests/daemon/face/unix-stream-transport-fixture.hpp
@@ -30,8 +30,6 @@
 #include "face/lp-face.hpp"
 
 #include "dummy-receive-link-service.hpp"
-#include "transport-test-common.hpp"
-
 #include "tests/limited-io.hpp"
 
 #include <boost/filesystem.hpp>
@@ -124,7 +122,7 @@
           BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
         }
       });
-    limitedIo.defer(time::milliseconds(50));
+    limitedIo.defer(time::seconds(1));
   }
 
 protected:
@@ -143,4 +141,4 @@
 } // namespace face
 } // namespace nfd
 
-#endif // NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP
\ No newline at end of file
+#endif // NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP
diff --git a/tests/daemon/face/unix-stream-transport.t.cpp b/tests/daemon/face/unix-stream-transport.t.cpp
index 2ff73be..8042042 100644
--- a/tests/daemon/face/unix-stream-transport.t.cpp
+++ b/tests/daemon/face/unix-stream-transport.t.cpp
@@ -23,6 +23,8 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "transport-test-common.hpp"
+
 #include "unix-stream-transport-fixture.hpp"
 
 namespace nfd {
@@ -30,7 +32,6 @@
 namespace tests {
 
 BOOST_AUTO_TEST_SUITE(Face)
-
 BOOST_FIXTURE_TEST_SUITE(TestUnixStreamTransport, UnixStreamTransportFixture)
 
 BOOST_AUTO_TEST_CASE(StaticProperties)