face: improve WebSocketChannel test suite

Change-Id: I7b3dfa930713331e6911a57624727163b8a7b3fe
Refs: #3373
diff --git a/tests/daemon/face/websocket-channel.t.cpp b/tests/daemon/face/websocket-channel.t.cpp
index 9e10239..afa3629 100644
--- a/tests/daemon/face/websocket-channel.t.cpp
+++ b/tests/daemon/face/websocket-channel.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2015,  Regents of the University of California,
+ * Copyright (c) 2014-2016,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -24,18 +24,293 @@
  */
 
 #include "face/websocket-channel.hpp"
+#include "face/websocket-transport.hpp"
 
+#include "tests/limited-io.hpp"
 #include "tests/test-common.hpp"
 
 namespace nfd {
 namespace tests {
 
 BOOST_AUTO_TEST_SUITE(Face)
-BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, BaseFixture)
 
-// TODO add a test case to accept multiple incoming connections on a channel
+using nfd::Face;
+namespace ip = boost::asio::ip;
 
-// TODO add a test case to check a failed face is removed from the channel
+class WebSocketChannelFixture : public BaseFixture
+{
+protected:
+  WebSocketChannelFixture()
+    : clientShouldPong(true)
+    , m_nextPort(20070)
+  {
+  }
+
+  unique_ptr<WebSocketChannel>
+  makeChannel(const ip::address& addr, uint16_t port = 0)
+  {
+    if (port == 0)
+      port = m_nextPort++;
+
+    return make_unique<WebSocketChannel>(websocket::Endpoint(addr, port));
+  }
+
+  void
+  listen(const ip::address& addr,
+         const time::milliseconds& pingInterval = time::seconds(10),
+         const time::milliseconds& pongTimeout = time::seconds(1))
+  {
+    listenerEp = websocket::Endpoint(addr, 20030);
+    listenerChannel = makeChannel(addr, 20030);
+    listenerChannel->setPingInterval(pingInterval);
+    listenerChannel->setPongTimeout(pongTimeout);
+    listenerChannel->listen(bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1));
+  }
+
+  void
+  clientConnect(websocket::Client& client)
+  {
+    client.clear_access_channels(websocketpp::log::alevel::all);
+    client.clear_error_channels(websocketpp::log::elevel::all);
+
+    client.init_asio(&g_io);
+    client.set_open_handler(bind(&WebSocketChannelFixture::clientHandleOpen, this, _1));
+    client.set_message_handler(bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2));
+    client.set_ping_handler(bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2));
+
+    std::string uri = "ws://" + listenerEp.address().to_string() + ":" + to_string(listenerEp.port());
+    websocketpp::lib::error_code ec;
+    websocket::Client::connection_ptr con = client.get_connection(uri, ec);
+    BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code());
+
+    client.connect(con);
+  }
+
+  void
+  initialize(const ip::address& addr,
+             const time::milliseconds& pingInterval = time::seconds(10),
+             const time::milliseconds& pongTimeout = time::seconds(1))
+  {
+    listen(addr, pingInterval, pongTimeout);
+    clientConnect(client);
+    BOOST_REQUIRE_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
+                        time::seconds(1)), LimitedIo::EXCEED_OPS);
+    BOOST_REQUIRE_EQUAL(listenerChannel->size(), 1);
+  }
+
+  void
+  clientSendInterest(const Interest& interest)
+  {
+    const Block& payload = interest.wireEncode();
+    client.send(clientHandle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
+  }
+
+private:
+  void
+  listenerOnFaceCreated(const shared_ptr<Face>& newFace)
+  {
+    BOOST_REQUIRE(newFace != nullptr);
+    newFace->afterReceiveInterest.connect(bind(&WebSocketChannelFixture::faceAfterReceiveInterest, this, _1));
+    connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
+    listenerFaces.push_back(newFace);
+    limitedIo.afterOp();
+  }
+
+  void
+  faceAfterReceiveInterest(const Interest& interest)
+  {
+    faceReceivedInterests.push_back(interest);
+    limitedIo.afterOp();
+  }
+
+  void
+  clientHandleOpen(websocketpp::connection_hdl hdl)
+  {
+    clientHandle = hdl;
+    limitedIo.afterOp();
+  }
+
+  void
+  clientHandleMessage(websocketpp::connection_hdl, websocket::Client::message_ptr msg)
+  {
+    clientReceivedMessages.push_back(msg->get_payload());
+    limitedIo.afterOp();
+  }
+
+  bool
+  clientHandlePing(websocketpp::connection_hdl, std::string)
+  {
+    auto now = time::steady_clock::now();
+    if (m_prevPingRecvTime != time::steady_clock::TimePoint()) {
+      measuredPingInterval = now - m_prevPingRecvTime;
+    }
+    m_prevPingRecvTime = now;
+
+    limitedIo.afterOp();
+    return clientShouldPong;
+  }
+
+protected:
+  LimitedIo limitedIo;
+
+  websocket::Endpoint listenerEp;
+  unique_ptr<WebSocketChannel> listenerChannel;
+  std::vector<shared_ptr<Face>> listenerFaces;
+  std::vector<Interest> faceReceivedInterests;
+
+  websocket::Client client;
+  websocketpp::connection_hdl clientHandle;
+  std::vector<std::string> clientReceivedMessages;
+  time::steady_clock::Duration measuredPingInterval;
+  bool clientShouldPong; // set clientShouldPong false to disable the pong response,
+                         // which will cause timeout in listenerChannel
+
+private:
+  uint16_t m_nextPort;
+  time::steady_clock::TimePoint m_prevPingRecvTime;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture)
+
+BOOST_AUTO_TEST_CASE(Uri)
+{
+  websocket::Endpoint ep(ip::address_v4::loopback(), 20070);
+  auto channel = makeChannel(ep.address(), ep.port());
+  BOOST_CHECK_EQUAL(channel->getUri(), FaceUri("ws://127.0.0.1:20070"));
+}
+
+BOOST_AUTO_TEST_CASE(Listen)
+{
+  auto channel = makeChannel(ip::address_v4());
+  BOOST_CHECK_EQUAL(channel->isListening(), false);
+
+  channel->listen(nullptr);
+  BOOST_CHECK_EQUAL(channel->isListening(), true);
+
+  // listen() is idempotent
+  BOOST_CHECK_NO_THROW(channel->listen(nullptr));
+  BOOST_CHECK_EQUAL(channel->isListening(), true);
+}
+
+BOOST_AUTO_TEST_CASE(MultipleAccepts)
+{
+  listen(ip::address_v4::loopback());
+
+  BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
+  BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
+
+  websocket::Client client1;
+  clientConnect(client1);
+  BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+  BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
+
+  websocket::Client client2;
+  websocket::Client client3;
+  clientConnect(client2);
+  clientConnect(client3);
+  BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+  BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
+
+  // check face persistency
+  for (const auto& face : listenerFaces) {
+    BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+  }
+}
+
+BOOST_AUTO_TEST_CASE(Send)
+{
+  initialize(ip::address_v4::loopback());
+  auto transport = listenerFaces.front()->getTransport();
+
+  Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
+  transport->send(face::Transport::Packet(Block(pkt1)));
+  BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+  Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
+  transport->send(face::Transport::Packet(Block(pkt2)));
+  BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+  BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2);
+  BOOST_CHECK_EQUAL_COLLECTIONS(
+    reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()),
+    reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(),
+    pkt1.begin(), pkt1.end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(
+    reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()),
+    reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(),
+    pkt2.begin(), pkt2.end());
+}
+
+BOOST_AUTO_TEST_CASE(Receive)
+{
+  initialize(ip::address_v4::loopback());
+
+  // use network-layer packets here, otherwise GenericLinkService
+  // won't recognize the packet type and will discard it
+  auto interest1 = makeInterest("ndn:/TpnzGvW9R");
+  auto interest2 = makeInterest("ndn:/QWiIMfj5sL");
+
+  clientSendInterest(*interest1);
+  BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+  clientSendInterest(*interest2);
+  BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+
+  BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2);
+  BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName());
+  BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName());
+}
+
+BOOST_AUTO_TEST_CASE(FaceClosure)
+{
+  initialize(ip::address_v4::loopback());
+
+  listenerFaces.front()->close();
+  BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(RemoteClose)
+{
+  initialize(ip::address_v4::loopback());
+
+  client.close(clientHandle, websocketpp::close::status::going_away, "");
+  BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+  BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(SetPingInterval)
+{
+  auto pingInterval = time::milliseconds(300);
+  initialize(ip::address_v4::loopback(), pingInterval, time::milliseconds(1000));
+
+  BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing
+                    time::seconds(1)), LimitedIo::EXCEED_OPS);
+  BOOST_CHECK_LE(measuredPingInterval, pingInterval * 1.1);
+  BOOST_CHECK_GE(measuredPingInterval, pingInterval * 0.9);
+}
+
+BOOST_AUTO_TEST_CASE(SetPongTimeOut)
+{
+  initialize(ip::address_v4::loopback(), time::milliseconds(500), time::milliseconds(300));
+
+  clientShouldPong = false;
+  BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal
+                    time::seconds(2)), LimitedIo::EXCEED_OPS);
+  BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
+
+  auto transport = static_cast<face::WebSocketTransport*>(listenerFaces.front()->getTransport());
+  BOOST_CHECK(transport->getState() == face::TransportState::FAILED ||
+              transport->getState() == face::TransportState::CLOSED);
+  BOOST_CHECK_EQUAL(transport->getCounters().nOutPings, 1);
+  BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0);
+}
 
 BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel
 BOOST_AUTO_TEST_SUITE_END() // Face