/* -*- 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 "tests/test-common.hpp"

namespace nfd {
namespace tests {

using namespace boost::asio::local;

BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture)

BOOST_AUTO_TEST_CASE(ChannelMap)
{
  UnixStreamChannelFactory factory;

  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 : protected BaseFixture
{
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)
  {
    g_io.stop();
    BOOST_FAIL(message);
  }

private:
  void
  afterIo()
  {
    if (--m_ioRemaining <= 0)
      g_io.stop();
  }

protected:
  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;

  EventId abortEvent =
    scheduler::schedule(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(g_io));
  client->async_connect(stream_protocol::endpoint("foo"),
                        bind(&EndToEndFixture::client_onConnect, this, _1));

  m_ioRemaining = 2;
  g_io.run();
  g_io.reset();
  scheduler::cancel(abortEvent);

  BOOST_REQUIRE(static_cast<bool>(m_face1));

  abortEvent =
    scheduler::schedule(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;
  g_io.run();
  g_io.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;

  EventId abortEvent =
    scheduler::schedule(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(g_io));
  client1->async_connect(stream_protocol::endpoint("foo"),
                         bind(&EndToEndFixture::client_onConnect, this, _1));

  m_ioRemaining = 2;
  g_io.run();
  g_io.reset();
  scheduler::cancel(abortEvent);

  BOOST_CHECK_EQUAL(m_faces.size(), 1);

  abortEvent =
    scheduler::schedule(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(g_io));
  client2->async_connect(stream_protocol::endpoint("foo"),
                         bind(&EndToEndFixture::client_onConnect, this, _1));

  m_ioRemaining = 2;
  g_io.run();
  g_io.reset();
  scheduler::cancel(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::schedule(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;
  g_io.run();
  g_io.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());
}

static inline void
noOp()
{
}

BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
{
  UnixStreamChannelFactory factory;

  EventId abortEvent =
    scheduler::schedule(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(g_io));
  client->async_connect(stream_protocol::endpoint("foo"),
                        bind(&EndToEndFixture::client_onConnect, this, _1));

  m_ioRemaining = 2;
  g_io.run();
  g_io.reset();
  scheduler::cancel(abortEvent);

  BOOST_REQUIRE(static_cast<bool>(m_face1));

  abortEvent =
    scheduler::schedule(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->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID);
  m_face1->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID);

  BOOST_CHECK(m_face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
  BOOST_CHECK(m_face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));

  m_face2->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID);
  m_face2->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID);

  BOOST_CHECK(m_face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
  BOOST_CHECK(m_face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));

  ////////////////////////////////////////////////////////

  interest1.setIncomingFaceId(11);
  interest1.setNextHopFaceId(111);

  m_face1->sendInterest(interest1);

  data1.setIncomingFaceId(22);
  data1.getLocalControlHeader().setNextHopFaceId(222);

  m_face1->sendData    (data1);

  //

  m_ioRemaining = 2;
  g_io.run();
  g_io.reset();

  BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
  BOOST_REQUIRE_EQUAL(m_face2_receivedDatas    .size(), 1);

  // sending allows only IncomingFaceId, receiving allows only NextHopFaceId
  BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
  BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false);

  BOOST_CHECK_EQUAL(m_face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
  BOOST_CHECK_EQUAL(m_face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);

  ////////////////////////////////////////////////////////

  using namespace boost::asio;

  std::vector<const_buffer> interestWithHeader;
  Block iHeader  = interest1.getLocalControlHeader().wireEncode(interest1, true, true);
  Block iPayload = interest1.wireEncode();
  interestWithHeader.push_back(buffer(iHeader.wire(),  iHeader.size()));
  interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size()));

  std::vector<const_buffer> dataWithHeader;
  Block dHeader  = data1.getLocalControlHeader().wireEncode(data1, true, true);
  Block dPayload = data1.wireEncode();
  dataWithHeader.push_back(buffer(dHeader.wire(),  dHeader.size()));
  dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size()));

  //

  client->async_send(interestWithHeader, bind(&noOp));
  client->async_send(dataWithHeader, bind(&noOp));

  m_ioRemaining = 2;
  g_io.run();
  g_io.reset();

  BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1);
  BOOST_REQUIRE_EQUAL(m_face1_receivedDatas    .size(), 1);

  BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false);
  BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true);
  BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getNextHopFaceId(), 111);

  BOOST_CHECK_EQUAL(m_face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false);
  BOOST_CHECK_EQUAL(m_face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace tests
} // namespace nfd
