Serve other routers' certificate and LSA

refs #4448, #4430

Change-Id: I3a9a9ae613770774be06effe26dfb866ea49f05a
diff --git a/tests/test-lsa-segment-storage.cpp b/tests/test-lsa-segment-storage.cpp
new file mode 100644
index 0000000..de0ee13
--- /dev/null
+++ b/tests/test-lsa-segment-storage.cpp
@@ -0,0 +1,154 @@
+/* -*- 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 "lsa-segment-storage.hpp"
+#include "test-common.hpp"
+#include "nlsr.hpp"
+#include "name-prefix-list.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace nlsr {
+namespace test {
+
+class LsaSegmentStorageFixture : public UnitTestTimeFixture
+{
+public:
+  LsaSegmentStorageFixture()
+    : face(m_ioService, m_keyChain)
+    , nlsr(m_ioService, m_scheduler, face, m_keyChain)
+    , lsdb(nlsr.getLsdb())
+    , lsaStorage(lsdb.getLsaStorage())
+  {
+  }
+
+  static shared_ptr<ndn::Data>
+  makeLsaSegment(const ndn::Name& baseName, uint64_t segmentNo, bool isFinal)
+  {
+    const uint8_t buffer[] = "Hello, world!";
+
+    ndn::Name lsaDataName(baseName);
+    lsaDataName.appendSegment(segmentNo);
+    auto lsaSegment = make_shared<ndn::Data>(ndn::Name(lsaDataName));
+    lsaSegment->setContent(buffer, sizeof(buffer));
+    if (isFinal) {
+      lsaSegment->setFinalBlockId(lsaSegment->getName()[-1]);
+    }
+
+    return signData(lsaSegment);
+  }
+
+  void
+  receiveLsaInterest(const ndn::Name& baseInterestName, uint64_t segmentNo,
+                     bool isSegmentZero)
+  {
+    if (isSegmentZero) {
+      lsdb.processInterest(ndn::Name(), ndn::Interest(baseInterestName));
+    }
+    else {
+      ndn::Name nextInterestName(baseInterestName);
+      nextInterestName.appendSegment(segmentNo);
+      lsdb.processInterest(ndn::Name(), ndn::Interest(nextInterestName));
+      advanceClocks(ndn::time::milliseconds(1), 10);
+    }
+  }
+
+public:
+  ndn::util::DummyClientFace face;
+  Nlsr nlsr;
+  Lsdb& lsdb;
+  LsaSegmentStorage& lsaStorage;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+  ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
+  lsaInterestName.appendNumber(12);
+
+  ndn::Name lsaDataName(lsaInterestName);
+  lsaDataName.appendVersion();
+
+  for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
+    auto lsaData = makeLsaSegment(lsaDataName, segmentNo, segmentNo == 4);
+    lsaStorage.afterFetcherSignalEmitted(*lsaData);
+  }
+
+  // receive interest for other-router's LSA that is stored in this router's storage
+  for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
+    receiveLsaInterest(lsaInterestName, segmentNo, segmentNo == 0);
+  }
+
+  // 4 data segments should be sent in response to 4 interests
+  BOOST_CHECK_EQUAL(face.sentData.size(), 4);
+}
+
+BOOST_AUTO_TEST_CASE(DeleteOldLsa)
+{
+  ndn::Name lsaDataName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
+  uint64_t segmentNo = 0;
+
+  uint64_t oldSeqNo = 12;
+  ndn::Name oldLsaDataName(lsaDataName);
+  oldLsaDataName.appendNumber(oldSeqNo);
+  oldLsaDataName.appendVersion();
+
+  auto oldLsaData = makeLsaSegment(oldLsaDataName, segmentNo, true);
+  lsaStorage.afterFetcherSignalEmitted(*oldLsaData);
+  advanceClocks(ndn::time::milliseconds(1), 10);
+
+  uint64_t newSeqNo = 13;
+  ndn::Name newLsaDataName(lsaDataName);
+  newLsaDataName.appendNumber(newSeqNo);
+  newLsaDataName.appendVersion();
+
+  auto newLsaData = makeLsaSegment(newLsaDataName, segmentNo, true);
+  lsaStorage.afterFetcherSignalEmitted(*newLsaData);
+  advanceClocks(ndn::time::milliseconds(1), 10);
+
+  ndn::Name lsaInterestName(lsaDataName);
+
+  ndn::Name oldLsaInterestName(lsaInterestName);
+  oldLsaInterestName.appendNumber(oldSeqNo);
+  receiveLsaInterest(oldLsaInterestName, segmentNo, true);
+
+  advanceClocks(ndn::time::milliseconds(1), 10);
+
+  BOOST_CHECK_EQUAL(face.sentData.size(), 0);
+
+  ndn::Name newLsaInterestName(lsaInterestName);
+  newLsaInterestName.appendNumber(newSeqNo);
+  receiveLsaInterest(newLsaInterestName, segmentNo, true);
+
+  advanceClocks(ndn::time::milliseconds(1), 10);
+
+  BOOST_CHECK_EQUAL(face.sentData.size(), 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage
+
+} // namespace test
+} // namespace nlsr
diff --git a/tests/test-lsdb.cpp b/tests/test-lsdb.cpp
index 197d5cd..8a36113 100644
--- a/tests/test-lsdb.cpp
+++ b/tests/test-lsdb.cpp
@@ -20,6 +20,7 @@
  **/
 
 #include "lsdb.hpp"
+
 #include "test-common.hpp"
 #include "nlsr.hpp"
 #include "lsa.hpp"
@@ -173,23 +174,22 @@
 
 BOOST_AUTO_TEST_CASE(SegmentLsaData)
 {
-  ndn::Name router("/ndn/cs/%C1.Router/router1");
-  uint64_t seqNo = 12;
-  NamePrefixList prefixList;
+  ndn::Name lsaKey("/ndn/site/%C1.Router/this-router/NAME");
 
-  NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
+  NameLsa* lsa = lsdb.findNameLsa(lsaKey);
+  uint64_t seqNo = lsa->getLsSeqNo();
 
   ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
 
   int nPrefixes = 0;
-  while (lsa.serialize().size() < ndn::MAX_NDN_PACKET_SIZE) {
-    lsa.addName(ndn::Name(prefix).appendNumber(++nPrefixes));
+  while (lsa->serialize().size() < ndn::MAX_NDN_PACKET_SIZE) {
+    lsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
   }
+  lsdb.installNameLsa(*lsa);
 
-  std::string expectedDataContent = lsa.serialize();
-  lsdb.installNameLsa(lsa);
+  std::string expectedDataContent = lsa->serialize();
 
-  ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
+  ndn::Name interestName("/ndn/NLSR/LSA/site/%C1.Router/this-router/NAME/");
   interestName.appendNumber(seqNo);
 
   ndn::Interest interest(interestName);
diff --git a/tests/test-statistics.cpp b/tests/test-statistics.cpp
index 90a2a67..c74ab38 100644
--- a/tests/test-statistics.cpp
+++ b/tests/test-statistics.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2017,  The University of Memphis,
+ * Copyright (c) 2014-2018,  The University of Memphis,
  *                           Regents of the University of California,
  *                           Arizona Board of Regents.
  *
@@ -222,21 +222,23 @@
  */
 BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
 {
-  std::string routerName("/ndn/site/%C1.Router/router");
-  ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
-  uint32_t seqNo = 1;
-
   // Adjacency LSA
+  lsdb.buildAndInstallOwnAdjLsa();
+
+  ndn::Name adjLsaKey = conf.getRouterPrefix();
+  adjLsaKey.append(std::to_string(Lsa::Type::ADJACENCY));
+
+  AdjLsa* adjLsa = lsdb.findAdjLsa(adjLsaKey);
+  uint32_t seqNo = adjLsa->getLsSeqNo();
+
   Adjacent adjacency("adjacency");
   adjacency.setStatus(Adjacent::STATUS_ACTIVE);
 
-  AdjacencyList adjacencies;
-  adjacencies.insert(adjacency);
+  adjLsa->addAdjacent(adjacency);
 
-  AdjLsa adjLsa(routerName, seqNo, MAX_TIME, 1, adjacencies);
-  lsdb.installAdjLsa(adjLsa);
+  lsdb.installAdjLsa(*adjLsa);
 
-  const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
+  const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/this-router/");
 
   // Receive Adjacency LSA Interest
   receiveInterestAndCheckSentStats(interestPrefix,
@@ -246,10 +248,15 @@
                                    Statistics::PacketType::SENT_ADJ_LSA_DATA);
 
   // Name LSA
-  NamePrefixList prefixes{ndn::Name{"/ndn/name"}};
+  ndn::Name nameLsaKey = conf.getRouterPrefix();
+  nameLsaKey.append(std::to_string(Lsa::Type::NAME));
 
-  NameLsa nameLsa(routerName, seqNo, MAX_TIME, prefixes);
-  lsdb.installNameLsa(nameLsa);
+  NameLsa* nameLsa = lsdb.findNameLsa(nameLsaKey);
+
+  seqNo = nameLsa->getLsSeqNo();
+
+  nameLsa->addName(ndn::Name("/ndn/name"));
+  lsdb.installNameLsa(*nameLsa);
 
   // Receive Name LSA Interest
   receiveInterestAndCheckSentStats(interestPrefix,
@@ -258,10 +265,15 @@
                                    Statistics::PacketType::RCV_NAME_LSA_INTEREST,
                                    Statistics::PacketType::SENT_NAME_LSA_DATA);
 
-  // Coordinate LSA
-  std::vector<double> angles = {20.0, 30.0};
-  CoordinateLsa coordLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
-  lsdb.installCoordinateLsa(coordLsa);
+  // // Coordinate LSA
+  lsdb.buildAndInstallOwnCoordinateLsa();
+  ndn::Name coorLsaKey = conf.getRouterPrefix();
+  coorLsaKey.append(std::to_string(Lsa::Type::COORDINATE));
+
+  CoordinateLsa* coorLsa = lsdb.findCoordinateLsa(coorLsaKey);
+  seqNo = coorLsa->getLsSeqNo();
+  coorLsa->setCorTheta({20.0, 30.0});
+  lsdb.installCoordinateLsa(*coorLsa);
 
   // Receive Adjacency LSA Interest
   receiveInterestAndCheckSentStats(interestPrefix,