comm: Stop SyncLogicHandler from fetching own LSAs

refs: #2005

Change-Id: I717a5258673dd5308fdf6561dbdb626ae4c74e9f
diff --git a/tests/dummy-face.hpp b/tests/dummy-face.hpp
new file mode 100644
index 0000000..a3a9402
--- /dev/null
+++ b/tests/dummy-face.hpp
@@ -0,0 +1,116 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014  University of Memphis,
+ *                     Regents of the University of California
+ *
+ * This file is part of NLSR (Named-data Link State Routing).
+ * See AUTHORS.md for complete list of NLSR authors and contributors.
+ *
+ * NLSR 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.
+ *
+ * NLSR 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
+ * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ **/
+
+#ifndef NLSR_TEST_DUMMY_FACE_HPP
+#define NLSR_TEST_DUMMY_FACE_HPP
+
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/transport/transport.hpp>
+
+namespace ndn {
+
+class DummyTransport : public Transport
+{
+public:
+  void
+  receive(const Block& block)
+  {
+    m_receiveCallback(block);
+  }
+
+  virtual void
+  close()
+  {
+  }
+
+  virtual void
+  pause()
+  {
+  }
+
+  virtual void
+  resume()
+  {
+  }
+
+  virtual void
+  send(const Block& wire)
+  {
+    if (wire.type() == Tlv::Interest) {
+      m_sentInterests->push_back(Interest(wire));
+    }
+    else if (wire.type() == Tlv::Data) {
+      m_sentDatas->push_back(Data(wire));
+    }
+  }
+
+  virtual void
+  send(const Block& header, const Block& payload)
+  {
+    this->send(payload);
+  }
+
+public:
+  std::vector<Interest>* m_sentInterests;
+  std::vector<Data>*     m_sentDatas;
+};
+
+
+/** \brief a Face for unit testing
+ */
+class DummyFace : public Face
+{
+public:
+  explicit
+  DummyFace(shared_ptr<DummyTransport> transport)
+    : Face(transport)
+    , m_transport(transport)
+  {
+    m_transport->m_sentInterests = &m_sentInterests;
+    m_transport->m_sentDatas     = &m_sentDatas;
+  }
+
+  /** \brief cause the Face to receive a packet
+   */
+  template<typename Packet>
+  void
+  receive(const Packet& packet)
+  {
+    m_transport->receive(packet.wireEncode());
+  }
+
+public:
+  std::vector<Interest> m_sentInterests;
+  std::vector<Data>     m_sentDatas;
+
+private:
+  shared_ptr<DummyTransport> m_transport;
+};
+
+inline shared_ptr<DummyFace>
+makeDummyFace()
+{
+  return make_shared<DummyFace>(make_shared<DummyTransport>());
+}
+
+} // namespace ndn
+
+#endif // NLSR_TEST_DUMMY_FACE_HPP
\ No newline at end of file
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index cbb83d9..dc60c90 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -24,6 +24,7 @@
 #define NLSR_TEST_COMMON_HPP
 
 #include <boost/asio.hpp>
+#include <boost/test/unit_test.hpp>
 #include <ndn-cxx/util/scheduler.hpp>
 
 namespace nlsr {
diff --git a/tests/test-conf-file-processor.cpp b/tests/test-conf-file-processor.cpp
index 44987de..d05e39c 100644
--- a/tests/test-conf-file-processor.cpp
+++ b/tests/test-conf-file-processor.cpp
@@ -22,6 +22,7 @@
  **/
 
 #include "test-common.hpp"
+#include "dummy-face.hpp"
 
 #include <fstream>
 #include "conf-file-processor.hpp"
@@ -31,11 +32,16 @@
 namespace nlsr {
 namespace test {
 
+using ndn::DummyFace;
+using ndn::shared_ptr;
+
 BOOST_FIXTURE_TEST_SUITE(TestConfFileProcessor, BaseFixture)
 
 BOOST_AUTO_TEST_CASE(ConfFileProcessorSample)
 {
-  Nlsr nlsr1(g_ioService, g_scheduler);
+  shared_ptr<DummyFace> face = ndn::makeDummyFace();
+
+  Nlsr nlsr1(g_ioService, g_scheduler, ndn::ref(*face));
 
   const std::string CONFIG =
      "general\n"
diff --git a/tests/test-lsdb.cpp b/tests/test-lsdb.cpp
index 84493d2..1caefcf 100644
--- a/tests/test-lsdb.cpp
+++ b/tests/test-lsdb.cpp
@@ -22,6 +22,7 @@
  **/
 
 #include "test-common.hpp"
+#include "dummy-face.hpp"
 
 #include "lsdb.hpp"
 #include "nlsr.hpp"
@@ -31,16 +32,49 @@
 #include <ndn-cxx/util/time.hpp>
 
 namespace nlsr {
-
 namespace test {
 
-BOOST_FIXTURE_TEST_SUITE(TestLsdb, BaseFixture)
+using ndn::DummyFace;
+using ndn::shared_ptr;
+
+class LsdbFixture : public BaseFixture
+{
+public:
+  LsdbFixture()
+    : face(ndn::makeDummyFace())
+    , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
+    , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter())
+    , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
+    , REGISTER_VERB("register")
+  {
+  }
+
+  void extractParameters(ndn::Interest& interest, ndn::Name::Component& verb,
+                         ndn::nfd::ControlParameters& extractedParameters)
+  {
+    const ndn::Name& name = interest.getName();
+    verb = name[REGISTER_COMMAND_PREFIX.size()];
+    const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1];
+
+    ndn::Block rawParameters = parameterComponent.blockFromValue();
+    extractedParameters.wireDecode(rawParameters);
+  }
+
+public:
+  shared_ptr<DummyFace> face;
+  Nlsr nlsr;
+  SyncLogicHandler sync;
+
+  ndn::Name REGISTER_COMMAND_PREFIX;
+  ndn::Name::Component REGISTER_VERB;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
 
 BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
 {
   INIT_LOGGERS("/tmp", "DEBUG");
 
-  Nlsr nlsr1(g_ioService, g_scheduler);
   ndn::time::system_clock::TimePoint testTimePoint =  ndn::time::system_clock::now();
   NamePrefixList npl1;
 
@@ -51,12 +85,12 @@
   npl1.insert(s1);
   npl1.insert(s2);
 
-//For NameLsa lsType is name.
-//12 is seqNo, randomly generated.
-//1800 is the default life time.
+  //For NameLsa lsType is name.
+  //12 is seqNo, randomly generated.
+  //1800 is the default life time.
   NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1);
 
-  Lsdb lsdb1(nlsr1, g_scheduler);
+  Lsdb lsdb1(nlsr, g_scheduler);
 
   lsdb1.installNameLsa(nlsa1);
   lsdb1.writeNameLsdbLog();
@@ -68,6 +102,42 @@
   BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), std::string("name")), false);
 }
 
+BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild)
+{
+  ConfParameter& conf = nlsr.getConfParameter();
+  conf.setNetwork("/ndn");
+  conf.setSiteName("/site");
+  conf.setRouterName("/%C1.router/this-router");
+
+  nlsr.initialize();
+
+  Lsdb& lsdb = nlsr.getLsdb();
+  face->processEvents(ndn::time::milliseconds(1));
+  face->m_sentInterests.clear();
+
+  // Should register Sync prefix
+  lsdb.buildAndInstallOwnAdjLsa();
+  face->processEvents(ndn::time::milliseconds(1));
+
+  std::vector<ndn::Interest>& interests = face->m_sentInterests;
+
+  BOOST_REQUIRE(interests.size() > 0);
+
+  ndn::nfd::ControlParameters extractedParameters;
+  ndn::Name::Component verb;
+  extractParameters(interests[0], verb, extractedParameters);
+
+  BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
+  BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix());
+
+  // Should not register Sync prefix
+  face->m_sentInterests.clear();
+  lsdb.buildAndInstallOwnAdjLsa();
+  face->processEvents(ndn::time::milliseconds(1));
+
+  BOOST_CHECK_EQUAL(interests.size(), 0);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } //namespace test
diff --git a/tests/test-sync-logic-handler.cpp b/tests/test-sync-logic-handler.cpp
new file mode 100644
index 0000000..00839b1
--- /dev/null
+++ b/tests/test-sync-logic-handler.cpp
@@ -0,0 +1,172 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014  University of Memphis,
+ *                     Regents of the University of California
+ *
+ * This file is part of NLSR (Named-data Link State Routing).
+ * See AUTHORS.md for complete list of NLSR authors and contributors.
+ *
+ * NLSR 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.
+ *
+ * NLSR 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
+ * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ **/
+
+#include "test-common.hpp"
+#include "dummy-face.hpp"
+
+#include "nlsr.hpp"
+#include "communication/sync-logic-handler.hpp"
+
+namespace nlsr {
+namespace test {
+
+using ndn::DummyFace;
+using ndn::shared_ptr;
+
+class SyncLogicFixture : public BaseFixture
+{
+public:
+  SyncLogicFixture()
+    : face(ndn::makeDummyFace())
+    , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
+    , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter())
+    , CONFIG_NETWORK("/ndn")
+    , CONFIG_SITE("/site")
+    , CONFIG_ROUTER_NAME("/%C1.Router/this-router")
+  {
+    nlsr.getConfParameter().setNetwork(CONFIG_NETWORK);
+    nlsr.getConfParameter().setSiteName(CONFIG_SITE);
+    nlsr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
+    nlsr.getConfParameter().buildRouterPrefix();
+  }
+
+  void
+  receiveUpdate(std::string prefix, uint64_t seqNo)
+  {
+    Sync::MissingDataInfo info = {prefix, 0, seqNo};
+
+    std::vector<Sync::MissingDataInfo> updates;
+    updates.push_back(info);
+
+    face->processEvents(ndn::time::milliseconds(1));
+    face->m_sentInterests.clear();
+
+    sync.onNsyncUpdate(updates, NULL);
+
+    face->processEvents(ndn::time::milliseconds(1));
+  }
+
+public:
+  shared_ptr<DummyFace> face;
+  Nlsr nlsr;
+  SyncLogicHandler sync;
+
+  const std::string CONFIG_NETWORK;
+  const std::string CONFIG_SITE;
+  const std::string CONFIG_ROUTER_NAME;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
+
+BOOST_AUTO_TEST_CASE(UpdateForOther)
+{
+  std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
+                           CONFIG_SITE + "/%C1.Router/other-router/";
+
+  receiveUpdate(updateName, 1);
+
+  std::vector<ndn::Interest>& interests = face->m_sentInterests;
+  BOOST_REQUIRE_EQUAL(interests.size(), 3);
+
+  std::vector<ndn::Interest>::iterator it = interests.begin();
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "name/");
+
+  ++it;
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "adjacency/");
+
+  ++it;
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "coordinate/");
+}
+
+BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
+{
+  std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
+                           CONFIG_SITE + CONFIG_ROUTER_NAME;
+
+  receiveUpdate(updateName, 1);
+
+  std::vector<ndn::Interest>& interests = face->m_sentInterests;
+  BOOST_CHECK_EQUAL(interests.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(MalformedUpdate)
+{
+  std::string updateName = CONFIG_SITE + nlsr.getConfParameter().getLsaPrefix().toUri() +
+                           CONFIG_ROUTER_NAME;
+
+  std::vector<ndn::Interest>& interests = face->m_sentInterests;
+  BOOST_CHECK_EQUAL(interests.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(SequenceNumber)
+{
+  std::string originRouter = CONFIG_NETWORK + CONFIG_SITE + "/%C1.Router/other-router/";
+
+  Lsdb& lsdb = nlsr.getLsdb();
+
+  // Install Name LSA
+  NamePrefixList nameList;
+  NameLsa lsa(originRouter, "name", 999, ndn::time::system_clock::TimePoint::max(), nameList);
+  lsdb.installNameLsa(lsa);
+
+  // Install Adj LSA
+  AdjacencyList adjList;
+  AdjLsa adjLsa(originRouter, "adjacency", 1000, ndn::time::system_clock::TimePoint::max(),
+                3 , adjList);
+  lsdb.installAdjLsa(adjLsa);
+
+  // Install Cor LSA
+  CoordinateLsa corLsa(originRouter, "coordinate", 1000, ndn::time::system_clock::TimePoint::max(),
+                       0,0);
+  lsdb.installCoordinateLsa(corLsa);
+
+  std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
+                           CONFIG_SITE + "/%C1.Router/other-router/";
+
+  // Lower NameLSA sequence number
+  uint64_t lowerSeqNo = static_cast<uint64_t>(998) << 40;
+  receiveUpdate(updateName, lowerSeqNo);
+
+  std::vector<ndn::Interest>& interests = face->m_sentInterests;
+  BOOST_REQUIRE_EQUAL(interests.size(), 0);
+
+  // Same NameLSA sequence number
+  uint64_t sameSeqNo = static_cast<uint64_t>(999) << 40;
+  receiveUpdate(updateName, sameSeqNo);
+
+  interests = face->m_sentInterests;
+  BOOST_REQUIRE_EQUAL(interests.size(), 0);
+
+  // Higher NameLSA sequence number
+  uint64_t higherSeqNo = static_cast<uint64_t>(1000) << 40;
+  receiveUpdate(updateName, higherSeqNo);
+
+  interests = face->m_sentInterests;
+  BOOST_REQUIRE_EQUAL(interests.size(), 1);
+
+  std::vector<ndn::Interest>::iterator it = interests.begin();
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "name/");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace test
+} // namespace nlsr