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