face: UnixStream{Face,Channel,ChannelFactory} implementation.

refs: #1174

Change-Id: I14d1e357c96a02995355e7970323fe40847580aa
diff --git a/daemon/face/tcp-channel-factory.hpp b/daemon/face/tcp-channel-factory.hpp
index 1a26738..171c24f 100644
--- a/daemon/face/tcp-channel-factory.hpp
+++ b/daemon/face/tcp-channel-factory.hpp
@@ -20,9 +20,13 @@
    */
   struct Error : public ChannelFactory<tcp::Endpoint, TcpChannel>::Error
   {
-    Error(const std::string& what) : ChannelFactory<tcp::Endpoint, TcpChannel>::Error(what) {}
+    Error(const std::string& what)
+      : ChannelFactory<tcp::Endpoint, TcpChannel>::Error(what)
+    {
+    }
   };
 
+  explicit
   TcpChannelFactory(boost::asio::io_service& ioService);
 
   /**
@@ -58,6 +62,7 @@
   shared_ptr<TcpChannel>
   create(const std::string& localHost, const std::string& localPort);
 
+private:
   /**
    * \brief Look up TcpChannel using specified local endpoint
    *
@@ -68,7 +73,7 @@
    */
   shared_ptr<TcpChannel>
   find(const tcp::Endpoint& localEndpoint);
-  
+
 private:
   boost::asio::io_service& m_ioService;
 };
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index e94747f..4cca5c5 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -11,12 +11,12 @@
 #include "core/monotonic_deadline_timer.hpp"
 #include "tcp-face.hpp"
 
-namespace tcp {
-typedef boost::asio::ip::tcp::endpoint Endpoint;
-} // namespace tcp
-
 namespace nfd {
 
+namespace tcp {
+  typedef boost::asio::ip::tcp::endpoint Endpoint;
+} // namespace tcp
+
 /**
  * \brief Class implementing TCP-based channel to create faces
  *
diff --git a/daemon/face/tcp-face.cpp b/daemon/face/tcp-face.cpp
index e867483..774a4d0 100644
--- a/daemon/face/tcp-face.cpp
+++ b/daemon/face/tcp-face.cpp
@@ -18,5 +18,4 @@
 {
 }
 
-
 } // namespace nfd
diff --git a/daemon/face/tcp-face.hpp b/daemon/face/tcp-face.hpp
index af45c6c..98593a0 100644
--- a/daemon/face/tcp-face.hpp
+++ b/daemon/face/tcp-face.hpp
@@ -21,6 +21,7 @@
 public:
   typedef boost::asio::ip::tcp protocol;
 
+  explicit
   TcpFace(const shared_ptr<protocol::socket>& socket);
 };
 
diff --git a/daemon/face/unix-stream-channel-factory.cpp b/daemon/face/unix-stream-channel-factory.cpp
new file mode 100644
index 0000000..cc86333
--- /dev/null
+++ b/daemon/face/unix-stream-channel-factory.cpp
@@ -0,0 +1,51 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "unix-stream-channel-factory.hpp"
+
+#if BOOST_VERSION >= 104800
+#include <boost/filesystem.hpp> // for canonical()
+#endif
+
+namespace nfd {
+
+UnixStreamChannelFactory::UnixStreamChannelFactory(boost::asio::io_service& ioService)
+  : m_ioService(ioService)
+{
+}
+
+shared_ptr<UnixStreamChannel>
+UnixStreamChannelFactory::create(const std::string& unixSocketPath)
+{
+#if BOOST_VERSION >= 104800
+  boost::filesystem::path p(unixSocketPath);
+  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
+  unix_stream::Endpoint endpoint(p.string());
+#else
+  unix_stream::Endpoint endpoint(unixSocketPath);
+#endif
+
+  shared_ptr<UnixStreamChannel> channel = find(endpoint);
+  if (channel)
+    return channel;
+
+  channel = make_shared<UnixStreamChannel>(boost::ref(m_ioService),
+                                           boost::cref(endpoint));
+  m_channels[endpoint] = channel;
+  return channel;
+}
+
+shared_ptr<UnixStreamChannel>
+UnixStreamChannelFactory::find(const unix_stream::Endpoint& endpoint)
+{
+  ChannelMap::iterator i = m_channels.find(endpoint);
+  if (i != m_channels.end())
+    return i->second;
+  else
+    return shared_ptr<UnixStreamChannel>();
+}
+
+} // namespace nfd
diff --git a/daemon/face/unix-stream-channel-factory.hpp b/daemon/face/unix-stream-channel-factory.hpp
new file mode 100644
index 0000000..8d0a6e7
--- /dev/null
+++ b/daemon/face/unix-stream-channel-factory.hpp
@@ -0,0 +1,65 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_FACE_UNIX_STREAM_CHANNEL_FACTORY_HPP
+#define NFD_FACE_UNIX_STREAM_CHANNEL_FACTORY_HPP
+
+#include "channel-factory.hpp"
+#include "unix-stream-channel.hpp"
+
+namespace nfd {
+
+class UnixStreamChannelFactory : public ChannelFactory<unix_stream::Endpoint, UnixStreamChannel>
+{
+public:
+  /**
+   * \brief Exception of UnixStreamChannelFactory
+   */
+  struct Error : public ChannelFactory<unix_stream::Endpoint, UnixStreamChannel>::Error
+  {
+    Error(const std::string& what)
+      : ChannelFactory<unix_stream::Endpoint, UnixStreamChannel>::Error(what)
+    {
+    }
+  };
+
+  explicit
+  UnixStreamChannelFactory(boost::asio::io_service& ioService);
+
+  /**
+   * \brief Create stream-oriented Unix channel using specified socket path
+   *
+   * If this method is called twice with the same path, only one channel
+   * will be created.  The second call will just retrieve the existing
+   * channel.
+   *
+   * \returns always a valid pointer to a UnixStreamChannel object,
+   *          an exception will be thrown if the channel cannot be created.
+   *
+   * \throws UnixStreamChannelFactory::Error
+   */
+  shared_ptr<UnixStreamChannel>
+  create(const std::string& unixSocketPath);
+
+private:
+  /**
+   * \brief Look up UnixStreamChannel using specified endpoint
+   *
+   * \returns shared pointer to the existing UnixStreamChannel object
+   *          or empty shared pointer when such channel does not exist
+   *
+   * \throws never
+   */
+  shared_ptr<UnixStreamChannel>
+  find(const unix_stream::Endpoint& endpoint);
+
+private:
+  boost::asio::io_service& m_ioService;
+};
+
+} // namespace nfd
+
+#endif // NFD_FACE_UNIX_STREAM_CHANNEL_FACTORY_HPP
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
new file mode 100644
index 0000000..428d33e
--- /dev/null
+++ b/daemon/face/unix-stream-channel.cpp
@@ -0,0 +1,101 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "unix-stream-channel.hpp"
+
+#include <boost/filesystem.hpp>
+
+namespace nfd {
+
+NFD_LOG_INIT("UnixStreamChannel");
+
+using namespace boost::asio::local;
+
+UnixStreamChannel::UnixStreamChannel(boost::asio::io_service& ioService,
+                                     const unix_stream::Endpoint& endpoint)
+  : m_ioService(ioService)
+  , m_endpoint(endpoint)
+{
+}
+
+UnixStreamChannel::~UnixStreamChannel()
+{
+  if (m_acceptor)
+    {
+      // use the non-throwing variants during destruction
+      // and ignore any errors
+      boost::system::error_code error;
+      m_acceptor->close(error);
+      boost::filesystem::remove(m_endpoint.path(), error);
+    }
+}
+
+void
+UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
+                          const ConnectFailedCallback& onAcceptFailed,
+                          int backlog/* = acceptor::max_connections*/)
+{
+  if (m_acceptor) // already listening
+    return;
+
+  namespace fs = boost::filesystem;
+  fs::path p(m_endpoint.path());
+  if (fs::symlink_status(p).type() == fs::socket_file)
+    {
+      // for safety, remove an already existing file only if it's a socket
+      fs::remove(p);
+    }
+
+  m_acceptor = make_shared<stream_protocol::acceptor>(boost::ref(m_ioService));
+  m_acceptor->open(m_endpoint.protocol());
+  m_acceptor->bind(m_endpoint);
+  m_acceptor->listen(backlog);
+
+  shared_ptr<stream_protocol::socket> clientSocket =
+    make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+  m_acceptor->async_accept(*clientSocket,
+                           bind(&UnixStreamChannel::handleSuccessfulAccept, this, _1,
+                                clientSocket, onFaceCreated, onAcceptFailed));
+}
+
+void
+UnixStreamChannel::createFace(const shared_ptr<stream_protocol::socket>& socket,
+                              const FaceCreatedCallback& onFaceCreated)
+{
+  shared_ptr<UnixStreamFace> face = make_shared<UnixStreamFace>(boost::cref(socket));
+  onFaceCreated(face);
+}
+
+void
+UnixStreamChannel::handleSuccessfulAccept(const boost::system::error_code& error,
+                                          const shared_ptr<stream_protocol::socket>& socket,
+                                          const FaceCreatedCallback& onFaceCreated,
+                                          const ConnectFailedCallback& onAcceptFailed)
+{
+  if (error) {
+    if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
+      return;
+
+    NFD_LOG_DEBUG("Connection failed: "
+                  << error.category().message(error.value()));
+
+    onAcceptFailed("Connection failed: " +
+                   error.category().message(error.value()));
+    return;
+  }
+
+  // prepare accepting the next connection
+  shared_ptr<stream_protocol::socket> clientSocket =
+    make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+  m_acceptor->async_accept(*clientSocket,
+                           bind(&UnixStreamChannel::handleSuccessfulAccept, this, _1,
+                                clientSocket, onFaceCreated, onAcceptFailed));
+
+  NFD_LOG_DEBUG("[" << m_endpoint << "] << Incoming connection");
+  createFace(socket, onFaceCreated);
+}
+
+} // namespace nfd
diff --git a/daemon/face/unix-stream-channel.hpp b/daemon/face/unix-stream-channel.hpp
new file mode 100644
index 0000000..bd0a81e
--- /dev/null
+++ b/daemon/face/unix-stream-channel.hpp
@@ -0,0 +1,84 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_FACE_UNIX_STREAM_CHANNEL_HPP
+#define NFD_FACE_UNIX_STREAM_CHANNEL_HPP
+
+#include "common.hpp"
+#include "unix-stream-face.hpp"
+
+namespace nfd {
+
+namespace unix_stream {
+  typedef boost::asio::local::stream_protocol::endpoint Endpoint;
+} // namespace unix_stream
+
+/**
+ * \brief Class implementing a local channel to create faces
+ *
+ * Channel can create faces as a response to incoming IPC connections
+ * (UnixStreamChannel::listen needs to be called for that to work).
+ */
+class UnixStreamChannel // : protected SessionBasedChannel
+{
+public:
+  /**
+   * \brief Prototype for the callback called when a face is created
+   *        (as a response to an incoming connection)
+   */
+  typedef function<void(const shared_ptr<UnixStreamFace>& newFace)> FaceCreatedCallback;
+
+  /**
+   * \brief Prototype for the callback that is called when a face
+   *        fails to be created
+   */
+  typedef function<void(const std::string& reason)> ConnectFailedCallback;
+
+  /**
+   * \brief Create UnixStream channel for the specified endpoint
+   *
+   * To enable creation of faces upon incoming connections, one
+   * needs to explicitly call UnixStreamChannel::listen method.
+   */
+  UnixStreamChannel(boost::asio::io_service& ioService,
+                    const unix_stream::Endpoint& endpoint);
+
+  ~UnixStreamChannel();
+
+  /**
+   * \brief Enable listening on the local endpoint, accept connections,
+   *        and create faces when a connection is made
+   * \param onFaceCreated  Callback to notify successful creation of the face
+   * \param onAcceptFailed Callback to notify when channel fails (accept call
+   *                       returns an error)
+   * \param backlog        The maximum length of the queue of pending incoming
+   *                       connections
+   */
+  void
+  listen(const FaceCreatedCallback& onFaceCreated,
+         const ConnectFailedCallback& onAcceptFailed,
+         int backlog = boost::asio::local::stream_protocol::acceptor::max_connections);
+
+private:
+  void
+  createFace(const shared_ptr<boost::asio::local::stream_protocol::socket>& socket,
+             const FaceCreatedCallback& onFaceCreated);
+
+  void
+  handleSuccessfulAccept(const boost::system::error_code& error,
+                         const shared_ptr<boost::asio::local::stream_protocol::socket>& socket,
+                         const FaceCreatedCallback& onFaceCreated,
+                         const ConnectFailedCallback& onConnectFailed);
+
+private:
+  boost::asio::io_service& m_ioService;
+  unix_stream::Endpoint m_endpoint;
+  shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor;
+};
+
+} // namespace nfd
+
+#endif // NFD_FACE_UNIX_STREAM_CHANNEL_HPP
diff --git a/daemon/face/unix-stream-face.cpp b/daemon/face/unix-stream-face.cpp
new file mode 100644
index 0000000..de7c4f9
--- /dev/null
+++ b/daemon/face/unix-stream-face.cpp
@@ -0,0 +1,27 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "unix-stream-face.hpp"
+
+namespace nfd {
+
+// The whole purpose of this file is to specialize the logger,
+// otherwise, everything could be put into the header file.
+
+NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, UnixStreamFace::protocol, "UnixStreamFace");
+
+UnixStreamFace::UnixStreamFace(const shared_ptr<UnixStreamFace::protocol::socket>& socket)
+  : StreamFace<protocol>(socket)
+{
+}
+
+bool
+UnixStreamFace::isLocal() const
+{
+  return true;
+}
+
+} // namespace nfd
diff --git a/daemon/face/unix-stream-face.hpp b/daemon/face/unix-stream-face.hpp
new file mode 100644
index 0000000..c04bec2
--- /dev/null
+++ b/daemon/face/unix-stream-face.hpp
@@ -0,0 +1,37 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_FACE_UNIX_STREAM_FACE_HPP
+#define NFD_FACE_UNIX_STREAM_FACE_HPP
+
+#include "stream-face.hpp"
+
+namespace nfd
+{
+
+/**
+ * \brief Implementation of Face abstraction that uses stream-oriented
+ *        Unix domain sockets as underlying transport mechanism
+ */
+class UnixStreamFace : public StreamFace<boost::asio::local::stream_protocol>
+{
+public:
+  typedef boost::asio::local::stream_protocol protocol;
+
+  explicit
+  UnixStreamFace(const shared_ptr<protocol::socket>& socket);
+
+  /** \brief Get whether face is connected to a local app
+   *
+   *  Always true for a UnixStreamFace.
+   */
+  virtual bool
+  isLocal() const;
+};
+
+} // namespace nfd
+
+#endif // NFD_FACE_UNIX_STREAM_FACE_HPP
diff --git a/tests/face/unix-stream.cpp b/tests/face/unix-stream.cpp
new file mode 100644
index 0000000..fee5827
--- /dev/null
+++ b/tests/face/unix-stream.cpp
@@ -0,0 +1,315 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "face/unix-stream-channel-factory.hpp"
+#include "core/scheduler.hpp"
+
+#include <ndn-cpp-dev/security/key-chain.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace boost::asio::local;
+
+namespace nfd {
+
+BOOST_AUTO_TEST_SUITE(FaceUnixStream)
+
+BOOST_AUTO_TEST_CASE(ChannelMap)
+{
+  boost::asio::io_service io;
+  UnixStreamChannelFactory factory(io);
+
+  shared_ptr<UnixStreamChannel> channel1 = factory.create("foo");
+  shared_ptr<UnixStreamChannel> channel1a = factory.create("foo");
+  BOOST_CHECK_EQUAL(channel1, channel1a);
+
+  shared_ptr<UnixStreamChannel> channel2 = factory.create("bar");
+  BOOST_CHECK_NE(channel1, channel2);
+}
+
+class EndToEndFixture
+{
+public:
+  void
+  client_onConnect(const boost::system::error_code& error)
+  {
+    BOOST_CHECK_MESSAGE(!error, error.message());
+
+    this->afterIo();
+  }
+
+  void
+  channel1_onFaceCreated(const shared_ptr<UnixStreamFace>& newFace)
+  {
+    BOOST_CHECK(!static_cast<bool>(m_face1));
+    m_face1 = newFace;
+    m_face1->onReceiveInterest +=
+      bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
+    m_face1->onReceiveData +=
+      bind(&EndToEndFixture::face1_onReceiveData, this, _1);
+
+    this->afterIo();
+  }
+
+  void
+  channel1_onConnectFailed(const std::string& reason)
+  {
+    BOOST_CHECK_MESSAGE(false, reason);
+
+    this->afterIo();
+  }
+  
+  void
+  face1_onReceiveInterest(const Interest& interest)
+  {
+    m_face1_receivedInterests.push_back(interest);
+
+    this->afterIo();
+  }
+  
+  void
+  face1_onReceiveData(const Data& data)
+  {
+    m_face1_receivedDatas.push_back(data);
+
+    this->afterIo();
+  }
+
+  void
+  face2_onReceiveInterest(const Interest& interest)
+  {
+    m_face2_receivedInterests.push_back(interest);
+
+    this->afterIo();
+  }
+  
+  void
+  face2_onReceiveData(const Data& data)
+  {
+    m_face2_receivedDatas.push_back(data);
+
+    this->afterIo();
+  }
+
+  void
+  channel_onFaceCreated(const shared_ptr<UnixStreamFace>& newFace)
+  {
+    m_faces.push_back(newFace);
+
+    this->afterIo();
+  }
+
+  void
+  channel_onConnectFailed(const std::string& reason)
+  {
+    BOOST_CHECK_MESSAGE(false, reason);
+
+    this->afterIo();
+  }
+
+  void
+  abortTestCase(const std::string& message)
+  {
+    m_ioService.stop();
+    BOOST_FAIL(message);
+  }
+
+private:
+  void
+  afterIo()
+  {
+    if (--m_ioRemaining <= 0)
+      m_ioService.stop();
+  }
+
+protected:
+  boost::asio::io_service m_ioService;
+
+  int m_ioRemaining;
+
+  shared_ptr<UnixStreamFace> m_face1;
+  std::vector<Interest> m_face1_receivedInterests;
+  std::vector<Data> m_face1_receivedDatas;
+  shared_ptr<UnixStreamFace> m_face2;
+  std::vector<Interest> m_face2_receivedInterests;
+  std::vector<Data> m_face2_receivedDatas;
+
+  std::list< shared_ptr<UnixStreamFace> > m_faces;
+};
+
+
+BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
+{
+  UnixStreamChannelFactory factory(m_ioService);
+  Scheduler scheduler(m_ioService); // to limit the amount of time the test may take
+
+  EventId abortEvent =
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "UnixStreamChannel error: cannot connect or cannot accept connection"));
+
+  shared_ptr<UnixStreamChannel> channel1 = factory.create("foo");
+  channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated,   this, _1),
+                   bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
+
+  shared_ptr<stream_protocol::socket> client =
+      make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+  client->async_connect(stream_protocol::endpoint("foo"),
+                        bind(&EndToEndFixture::client_onConnect, this, _1));
+
+  m_ioRemaining = 2;
+  m_ioService.run();
+  m_ioService.reset();
+  scheduler.cancelEvent(abortEvent);
+
+  BOOST_REQUIRE(static_cast<bool>(m_face1));
+
+  abortEvent =
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "UnixStreamChannel error: cannot send or receive Interest/Data packets"));
+
+  m_face2 = make_shared<UnixStreamFace>(client);
+  m_face2->onReceiveInterest +=
+    bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
+  m_face2->onReceiveData +=
+    bind(&EndToEndFixture::face2_onReceiveData, this, _1);
+
+  Interest interest1("ndn:/TpnzGvW9R");
+  Data     data1    ("ndn:/KfczhUqVix");
+  data1.setContent(0, 0);
+  Interest interest2("ndn:/QWiIMfj5sL");
+  Data     data2    ("ndn:/XNBV796f");
+  data2.setContent(0, 0);
+
+  ndn::SignatureSha256WithRsa fakeSignature;
+  fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
+
+  // set fake signature on data1 and data2
+  data1.setSignature(fakeSignature);
+  data2.setSignature(fakeSignature);
+
+  m_face1->sendInterest(interest1);
+  m_face1->sendData    (data1    );
+  m_face2->sendInterest(interest2);
+  m_face2->sendData    (data2    );
+
+  m_ioRemaining = 4;
+  m_ioService.run();
+  m_ioService.reset();
+
+  BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
+  BOOST_REQUIRE_EQUAL(m_face1_receivedDatas    .size(), 1);
+  BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
+  BOOST_REQUIRE_EQUAL(m_face2_receivedDatas    .size(), 1);
+
+  BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
+  BOOST_CHECK_EQUAL(m_face1_receivedDatas    [0].getName(), data2.getName());
+  BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
+  BOOST_CHECK_EQUAL(m_face2_receivedDatas    [0].getName(), data1.getName());
+}
+
+BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
+{
+  UnixStreamChannelFactory factory(m_ioService);
+  Scheduler scheduler(m_ioService); // to limit the amount of time the test may take
+
+  EventId abortEvent =
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "UnixStreamChannel error: cannot connect or cannot accept connection"));
+
+  shared_ptr<UnixStreamChannel> channel = factory.create("foo");
+  channel->listen(bind(&EndToEndFixture::channel_onFaceCreated,   this, _1),
+                  bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
+
+  shared_ptr<stream_protocol::socket> client1 =
+      make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+  client1->async_connect(stream_protocol::endpoint("foo"),
+                         bind(&EndToEndFixture::client_onConnect, this, _1));
+
+  m_ioRemaining = 2;
+  m_ioService.run();
+  m_ioService.reset();
+  scheduler.cancelEvent(abortEvent);
+
+  BOOST_CHECK_EQUAL(m_faces.size(), 1);
+
+  abortEvent =
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "UnixStreamChannel error: cannot accept multiple connections"));
+
+  shared_ptr<stream_protocol::socket> client2 =
+      make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+  client2->async_connect(stream_protocol::endpoint("foo"),
+                         bind(&EndToEndFixture::client_onConnect, this, _1));
+
+  m_ioRemaining = 2;
+  m_ioService.run();
+  m_ioService.reset();
+  scheduler.cancelEvent(abortEvent);
+
+  BOOST_CHECK_EQUAL(m_faces.size(), 2);
+
+  // now close one of the faces
+  m_faces.front()->close();
+
+  // we should still be able to send/receive with the other one
+  m_face1 = m_faces.back();
+  m_face1->onReceiveInterest +=
+      bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
+  m_face1->onReceiveData +=
+      bind(&EndToEndFixture::face1_onReceiveData, this, _1);
+
+  m_face2 = make_shared<UnixStreamFace>(client2);
+  m_face2->onReceiveInterest +=
+      bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
+  m_face2->onReceiveData +=
+      bind(&EndToEndFixture::face2_onReceiveData, this, _1);
+
+  abortEvent =
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "UnixStreamChannel error: cannot send or receive Interest/Data packets"));
+
+  Interest interest1("ndn:/TpnzGvW9R");
+  Data     data1    ("ndn:/KfczhUqVix");
+  data1.setContent(0, 0);
+  Interest interest2("ndn:/QWiIMfj5sL");
+  Data     data2    ("ndn:/XNBV796f");
+  data2.setContent(0, 0);
+
+  ndn::SignatureSha256WithRsa fakeSignature;
+  fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
+
+  // set fake signature on data1 and data2
+  data1.setSignature(fakeSignature);
+  data2.setSignature(fakeSignature);
+
+  m_face1->sendInterest(interest1);
+  m_face1->sendData    (data1    );
+  m_face2->sendInterest(interest2);
+  m_face2->sendData    (data2    );
+
+  m_ioRemaining = 4;
+  m_ioService.run();
+  m_ioService.reset();
+
+  BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
+  BOOST_REQUIRE_EQUAL(m_face1_receivedDatas    .size(), 1);
+  BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
+  BOOST_REQUIRE_EQUAL(m_face2_receivedDatas    .size(), 1);
+
+  BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
+  BOOST_CHECK_EQUAL(m_face1_receivedDatas    [0].getName(), data2.getName());
+  BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
+  BOOST_CHECK_EQUAL(m_face2_receivedDatas    [0].getName(), data1.getName());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace nfd