src: use psync::SegmentPublisher and fix proccessing of interests w/ segments

refs: #4784, #4760

Change-Id: Ia017a6f340e1cd35991068453c9da595c453ca1f
diff --git a/tests/publisher/test-segment-publisher.cpp b/tests/publisher/test-segment-publisher.cpp
deleted file mode 100644
index a70e57f..0000000
--- a/tests/publisher/test-segment-publisher.cpp
+++ /dev/null
@@ -1,173 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018,  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 "publisher/segment-publisher.hpp"
-
-#include "../boost-test.hpp"
-#include "../test-common.hpp"
-
-#include <ndn-cxx/encoding/tlv.hpp>
-#include <ndn-cxx/util/dummy-client-face.hpp>
-
-#include <boost/mpl/int.hpp>
-#include <boost/mpl/vector.hpp>
-
-namespace nlsr {
-namespace tests {
-
-using namespace nlsr::test;
-
-template<int64_t N=10000>
-class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace>
-{
-public:
-  TestSegmentPublisher(ndn::util::DummyClientFace& face,
-                       ndn::KeyChain& keyChain,
-                       ndn::security::SigningInfo& signingInfo,
-                       const ndn::time::milliseconds freshnessPeriod)
-    : SegmentPublisher(face, keyChain, signingInfo, freshnessPeriod)
-    , m_totalPayloadLength(0)
-  {
-
-  }
-
-  virtual
-  ~TestSegmentPublisher()
-  {
-  }
-
-  uint16_t
-  getLimit() const
-  {
-    return N;
-  }
-
-  size_t
-  getTotalPayloadLength() const
-  {
-    return m_totalPayloadLength;
-  }
-
-protected:
-
-  virtual size_t
-  generate(ndn::EncodingBuffer& outBuffer)
-  {
-    size_t totalLength = 0;
-    for (int64_t i = 0; i < N; i++) {
-        totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::tlv::Content, i);
-    }
-
-    m_totalPayloadLength += totalLength;
-    return totalLength;
-  }
-
-protected:
-  size_t m_totalPayloadLength;
-};
-
-template<int64_t N>
-class SegmentPublisherFixture : public BaseFixture
-{
-public:
-  SegmentPublisherFixture()
-    : m_face(std::make_shared<ndn::util::DummyClientFace>(m_ioService, m_keyChain))
-    , m_expectedFreshnessPeriod(ndn::time::milliseconds(111))
-    , m_publisher(*m_face, m_keyChain, m_signingInfo, m_expectedFreshnessPeriod)
-    , m_publishingPrefix("/localhost/nfd/SegmentPublisherFixture")
-  {
-  }
-
-  void
-  validate(const ndn::Data& data)
-  {
-    BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod);
-
-    ndn::Block payload = data.getContent();
-
-    m_buffer.appendByteArray(payload.value(), payload.value_size());
-
-    // uint64_t segmentNo = data.getName()[-1].toSegment();
-    if (data.getFinalBlock().value_or(ndn::name::Component("")) != data.getName()[-1]) {
-      return;
-    }
-
-    // wrap data in a single Content TLV for easy parsing
-    m_buffer.prependVarNumber(m_buffer.size());
-    m_buffer.prependVarNumber(ndn::tlv::Content);
-
-    BOOST_TEST_CHECKPOINT("creating parser");
-    ndn::Block parser(m_buffer.buf(), m_buffer.size());
-    BOOST_TEST_CHECKPOINT("parsing aggregated response");
-    parser.parse();
-
-    BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit());
-
-    uint64_t expectedNo = m_publisher.getLimit() - 1;
-    for (ndn::Block::element_const_iterator i = parser.elements_begin();
-         i != parser.elements_end();
-         ++i)
-      {
-        uint64_t number = readNonNegativeInteger(*i);
-        BOOST_REQUIRE_EQUAL(number, expectedNo);
-        --expectedNo;
-      }
-  }
-
-protected:
-  std::shared_ptr<ndn::util::DummyClientFace> m_face;
-  const ndn::time::milliseconds m_expectedFreshnessPeriod;
-  TestSegmentPublisher<N> m_publisher;
-  ndn::EncodingBuffer m_buffer;
-  ndn::security::SigningInfo m_signingInfo;
-  const ndn::Name m_publishingPrefix;
-};
-
-using boost::mpl::int_;
-typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes;
-
-BOOST_AUTO_TEST_SUITE(PublisherTestSegmentPublisher)
-
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>)
-{
-  this->m_publisher.publish(this->m_publishingPrefix);
-  this->m_face->processEvents();
-
-  size_t nSegments = this->m_publisher.getTotalPayloadLength() /
-                     this->m_publisher.getMaxSegmentSize();
-  if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 ||
-      nSegments == 0)
-    ++nSegments;
-
-  BOOST_CHECK_EQUAL(this->m_face->sentData.size(), nSegments);
-  for (const ndn::Data& data : this->m_face->sentData) {
-    this->validate(data);
-  }
-}
-
-BOOST_AUTO_TEST_SUITE_END()
-
-} // namespace tests
-} // namespace nlsr
diff --git a/tests/test-lsdb.cpp b/tests/test-lsdb.cpp
index 8a36113..2f9fefc 100644
--- a/tests/test-lsdb.cpp
+++ b/tests/test-lsdb.cpp
@@ -41,7 +41,7 @@
 {
 public:
   LsdbFixture()
-    : face(m_ioService, m_keyChain)
+    : face(m_ioService, m_keyChain, {true, true})
     , nlsr(m_ioService, m_scheduler, face, m_keyChain)
     , lsdb(nlsr.getLsdb())
     , conf(nlsr.getConfParameter())
@@ -172,6 +172,48 @@
   BOOST_CHECK_EQUAL(interests.size(), 0);
 }
 
+BOOST_AUTO_TEST_CASE(LsdbSegmentedData)
+{
+  // Add a lot of NameLSAs to exceed max packet size
+  ndn::Name lsaKey("/ndn/site/%C1.Router/this-router/NAME");
+
+  NameLsa* nameLsa = lsdb.findNameLsa(lsaKey);
+  uint64_t seqNo = nameLsa->getLsSeqNo();
+
+  ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
+
+  int nPrefixes = 0;
+  while (nameLsa->serialize().size() < ndn::MAX_NDN_PACKET_SIZE) {
+    nameLsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
+  }
+  lsdb.installNameLsa(*nameLsa);
+
+  // Create another Lsdb and expressInterest
+  ndn::util::DummyClientFace face2(m_ioService, m_keyChain, {true, true});
+  face.linkTo(face2);
+  Nlsr nlsr2(m_ioService, m_scheduler, face2, m_keyChain);
+  std::string config = R"CONF(
+              trust-anchor
+                {
+                  type any
+                }
+            )CONF";
+  nlsr2.getValidator().load(config, "config-file-from-string");
+
+  Lsdb& lsdb2(nlsr2.getLsdb());
+
+  advanceClocks(ndn::time::milliseconds(1), 10);
+
+  ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME");
+  interestName.appendNumber(seqNo);
+  // 0 == timeout count
+  lsdb2.expressInterest(interestName, 0);
+
+  advanceClocks(ndn::time::milliseconds(1), 10);
+
+  BOOST_CHECK_EQUAL(lsdb.getNameLsdb().front().getNpl(), lsdb2.getNameLsdb().front().getNpl());
+}
+
 BOOST_AUTO_TEST_CASE(SegmentLsaData)
 {
   ndn::Name lsaKey("/ndn/site/%C1.Router/this-router/NAME");
@@ -189,29 +231,21 @@
 
   std::string expectedDataContent = lsa->serialize();
 
-  ndn::Name interestName("/ndn/NLSR/LSA/site/%C1.Router/this-router/NAME/");
+  ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME/");
   interestName.appendNumber(seqNo);
 
-  ndn::Interest interest(interestName);
-  lsdb.processInterest(ndn::Name(), interest);
-  advanceClocks(ndn::time::milliseconds(1), 10);
-  face.sentData.clear();
+  ndn::util::DummyClientFace face2(m_ioService, m_keyChain, {true, true});
+  face.linkTo(face2);
 
-  lsdb.processInterest(ndn::Name(), interest);
+  auto fetcher = ndn::util::SegmentFetcher::start(face2, ndn::Interest(interestName),
+                                                  ndn::security::v2::getAcceptAllValidator());
+  fetcher->onComplete.connect([&expectedDataContent] (ndn::ConstBufferPtr bufferPtr) {
+                                ndn::Block block(bufferPtr);
+                                BOOST_CHECK_EQUAL(expectedDataContent, readString(block));
+                              });
 
-  advanceClocks(ndn::time::milliseconds(1), 10);
-
-  std::string recvDataContent;
-  for (const ndn::Data& data : face.sentData)
-  {
-    const ndn::Block& nameBlock = data.getContent();
-    std::string nameBlockContent(reinterpret_cast<char const*>(nameBlock.value()),
-                                 nameBlock.value_size());
-
-    recvDataContent += nameBlockContent;
-  }
-
-  BOOST_CHECK_EQUAL(expectedDataContent, recvDataContent);
+  advanceClocks(ndn::time::milliseconds(1), 100);
+  fetcher->stop();
 }
 
 BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData)
@@ -228,12 +262,11 @@
     lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes));
   }
 
-  ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
+  ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
   interestName.appendNumber(seqNo);
 
-  const ndn::ConstBufferPtr bufferPtr = std::make_shared<ndn::Buffer>(lsa.serialize().c_str(),
-                                                                 lsa.serialize().size());
-  lsdb.afterFetchLsa(bufferPtr, interestName);
+  ndn::Block block = ndn::encoding::makeStringBlock(ndn::tlv::Content, lsa.serialize());
+  lsdb.afterFetchLsa(block.getBuffer(), interestName);
 
   NameLsa* foundLsa = lsdb.findNameLsa(lsa.getKey());
   BOOST_REQUIRE(foundLsa != nullptr);
diff --git a/tests/test-statistics.cpp b/tests/test-statistics.cpp
index 51797e5..3e46c80 100644
--- a/tests/test-statistics.cpp
+++ b/tests/test-statistics.cpp
@@ -196,7 +196,7 @@
  */
 BOOST_AUTO_TEST_CASE(LsdbSendLsaInterest)
 {
-  const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
+  const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/router/");
   uint32_t seqNo = 1;
 
   // Adjacency LSA
@@ -240,7 +240,7 @@
 
   lsdb.installAdjLsa(*adjLsa);
 
-  const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/this-router/");
+  const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/");
 
   // Receive Adjacency LSA Interest
   receiveInterestAndCheckSentStats(interestPrefix,
@@ -302,37 +302,37 @@
   ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
 
   // adjacency lsa
-  ndn::Name adjInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/ADJACENCY/");
+  ndn::Name adjInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/ADJACENCY/");
   adjInterest.appendNumber(seqNo);
   AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, nlsr.getAdjacencyList());
   lsdb.installAdjLsa(aLsa);
 
-  const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.serialize().c_str(),
-                                                                    aLsa.serialize().size());
-  lsdb.afterFetchLsa(aBuffer, adjInterest);
+  ndn::Block block = ndn::encoding::makeStringBlock(ndn::tlv::Content, aLsa.serialize());
+
+  lsdb.afterFetchLsa(block.getBuffer(), adjInterest);
   BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
 
   // coordinate lsa
-  ndn::Name coordInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/COORDINATE/");
+  ndn::Name coordInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/COORDINATE/");
   coordInterest.appendNumber(seqNo);
   std::vector<double> angles = {20.0, 30.0};
   CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
   lsdb.installCoordinateLsa(cLsa);
 
-  const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.serialize().c_str(),
-                                                                   cLsa.serialize().size());
-  lsdb.afterFetchLsa(cBuffer, coordInterest);
+  block = ndn::encoding::makeStringBlock(ndn::tlv::Content, cLsa.serialize());
+
+  lsdb.afterFetchLsa(block.getBuffer(), coordInterest);
   BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
 
   // name lsa
-  ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
+  ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
   interestName.appendNumber(seqNo);
   NameLsa nLsa(routerName, seqNo, MAX_TIME, nlsr.getNamePrefixList());
   lsdb.installNameLsa(nLsa);
 
-  const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.serialize().c_str(),
-                                                                   nLsa.serialize().size());
-  lsdb.afterFetchLsa(nBuffer, interestName);
+  block = ndn::encoding::makeStringBlock(ndn::tlv::Content, nLsa.serialize());
+
+  lsdb.afterFetchLsa(block.getBuffer(), interestName);
   BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
 
   // 3 lsa data types should be received