tests: move integrated tests into unit tests

Change-Id: Ifed29e53091ca430582b1c2e7f3514e02d7c8d14
diff --git a/src/logic.cpp b/src/logic.cpp
index 0c5b87a..e0c0501 100644
--- a/src/logic.cpp
+++ b/src/logic.cpp
@@ -121,14 +121,15 @@
 }
 
 void
-Logic::reset()
+Logic::reset(bool isOnInterest)
 {
   m_isInReset = true;
 
   m_state.reset();
   m_log.clear();
 
-  sendResetInterest();
+  if (!isOnInterest)
+    sendResetInterest();
 
   // reset outstanding interest name, so that data for previous interest will be dropped.
   if (m_outstandingInterestId != 0) {
@@ -173,7 +174,7 @@
     sessionName.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
     m_nodeList[userPrefix].sessionName = sessionName;
     m_nodeList[userPrefix].seqNo = 0;
-    reset();
+    reset(false);
   }
 }
 
@@ -193,7 +194,7 @@
         m_defaultSigningId = DEFAULT_NAME;
       }
     }
-    reset();
+    reset(false);
   }
 }
 
@@ -459,7 +460,7 @@
 Logic::processResetInterest(const Interest& interest)
 {
   _LOG_DEBUG_ID(">> Logic::processResetInterest");
-  reset();
+  reset(true);
 }
 
 void
diff --git a/src/logic.hpp b/src/logic.hpp
index e8ea4f6..b7bfbdf 100644
--- a/src/logic.hpp
+++ b/src/logic.hpp
@@ -126,9 +126,13 @@
 
   ~Logic();
 
-  /// @brief Reset the sync tree (and restart synchronization again)
+  /**
+   * @brief Reset the sync tree (and restart synchronization again)
+   *
+   * @param isOnInterest a flag that tells whether the reset is called by reset interest.
+   */
   void
-  reset();
+  reset(bool isOnInterest = false);
 
   /**
    * @brief Set user prefix
diff --git a/src/socket.cpp b/src/socket.cpp
index 695f758..7356f73 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -30,8 +30,8 @@
 
 namespace chronosync {
 
-const ndn::Name Socket::DEFAULT_PREFIX;
 const ndn::Name Socket::DEFAULT_NAME;
+const ndn::Name Socket::DEFAULT_PREFIX;
 const ndn::shared_ptr<ndn::Validator> Socket::DEFAULT_VALIDATOR;
 
 Socket::Socket(const Name& syncPrefix,
@@ -46,12 +46,30 @@
   , m_signingId(signingId)
   , m_validator(validator)
 {
+  if (m_userPrefix != DEFAULT_NAME)
+    m_registeredPrefixList[m_userPrefix] =
+      m_face.setInterestFilter(m_userPrefix,
+                               bind(&Socket::onInterest, this, _1, _2),
+                               [] (const Name& prefix, const std::string& msg) {});
 }
 
 void
 Socket::addSyncNode(const Name& prefix, const Name& signingId)
 {
+  if (prefix == DEFAULT_NAME)
+    return;
+
+  auto itr = m_registeredPrefixList.find(prefix);
+  if (itr != m_registeredPrefixList.end())
+    return;
+
+  if (m_userPrefix == DEFAULT_NAME)
+    m_userPrefix = prefix;
   m_logic.addUserNode(prefix, signingId);
+  m_registeredPrefixList[prefix] =
+    m_face.setInterestFilter(prefix,
+                             bind(&Socket::onInterest, this, _1, _2),
+                             [] (const Name& prefix, const std::string& msg) {});
 }
 
 void
@@ -79,7 +97,7 @@
   else
     m_keyChain.signByIdentity(*data, m_signingId);
 
-  m_face.put(*data);
+  m_ims.insert(*data);
 
   m_logic.updateSeqNo(newSeq, prefix);
 }
@@ -126,6 +144,15 @@
 }
 
 void
+Socket::onInterest(const Name& prefix, const Interest& interest)
+{
+  shared_ptr<const Data>data = m_ims.find(interest);
+  if (static_cast<bool>(data)) {
+    m_face.put(*data);
+  }
+}
+
+void
 Socket::onData(const Interest& interest, Data& data,
                const ndn::OnDataValidated& onValidated,
                const ndn::OnDataValidationFailed& onFailed)
diff --git a/src/socket.hpp b/src/socket.hpp
index 3e09c79..6987003 100644
--- a/src/socket.hpp
+++ b/src/socket.hpp
@@ -26,6 +26,8 @@
 #define CHRONOSYNC_SOCKET_HPP
 
 #include <ndn-cxx/face.hpp>
+#include <ndn-cxx/util/in-memory-storage-persistent.hpp>
+#include <unordered_map>
 
 #include "logic.hpp"
 
@@ -140,6 +142,9 @@
 
 private:
   void
+  onInterest(const Name& prefix, const Interest& interest);
+
+  void
   onData(const Interest& interest, Data& data,
          const ndn::OnDataValidated& dataCallback,
          const ndn::OnDataValidationFailed& failCallback);
@@ -159,6 +164,7 @@
   static const ndn::shared_ptr<ndn::Validator> DEFAULT_VALIDATOR;
 
 private:
+  typedef std::unordered_map<ndn::Name, const ndn::RegisteredPrefixId*> RegisteredPrefixList;
 
   Name m_userPrefix;
   ndn::Face& m_face;
@@ -168,6 +174,9 @@
   ndn::Name m_signingId;
   ndn::KeyChain m_keyChain;
   ndn::shared_ptr<ndn::Validator> m_validator;
+
+  RegisteredPrefixList m_registeredPrefixList;
+  ndn::util::InMemoryStoragePersistent m_ims;
 };
 
 } // namespace chronosync