Add Update Handler class

Change-Id: I465297bfa3b8c4c8e6e7f7cd028b2d4afeb4e768
Refs: #3598
diff --git a/tests/unit-tests/dummy-parser-fixture.hpp b/tests/unit-tests/dummy-parser-fixture.hpp
new file mode 100644
index 0000000..434cfde
--- /dev/null
+++ b/tests/unit-tests/dummy-parser-fixture.hpp
@@ -0,0 +1,102 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent 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 Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#ifndef NDN_TESTS_UNIT_TESTS_DUMMY_PARSER_HPP
+#define NDN_TESTS_UNIT_TESTS_DUMMY_PARSER_HPP
+
+#include <ndn-cxx/data.hpp>
+
+#include <vector>
+
+namespace ndn {
+namespace ntorrent {
+namespace tests {
+
+using std::vector;
+
+class DummyParser {
+public:
+  DummyParser()
+  {
+  }
+
+  ~DummyParser()
+  {
+  }
+
+  static shared_ptr<Data>
+  createDataPacket(const Name& packetName, const std::vector<Name>& vec)
+  {
+    shared_ptr<Data> data = make_shared<Data>(packetName);
+
+    EncodingEstimator estimator;
+    size_t estimatedSize = encodeContent(estimator, vec);
+
+    EncodingBuffer buffer(estimatedSize, 0);
+    encodeContent(buffer, vec);
+
+    data->setContentType(tlv::ContentType_Blob);
+    data->setContent(buffer.block());
+
+    return data;
+  }
+
+  static shared_ptr<vector<Name>>
+  decodeContent(const Block& content)
+  {
+    shared_ptr<vector<Name>> nameVec = make_shared<vector<Name>>();
+    content.parse();
+    // Decode the names (do not worry about the order)
+    for (auto element = content.elements_begin(); element != content.elements_end(); element++) {
+      element->parse();
+      Name name(*element);
+      nameVec->push_back(name);
+    }
+    return nameVec;
+  }
+private:
+  template<encoding::Tag TAG>
+  static size_t
+  encodeContent(EncodingImpl<TAG>& encoder, const std::vector<Name>& vec)
+  {
+    // Content ::= CONTENT-TYPE TLV-LENGTH
+    //             RoutableName+
+
+    // RoutableName ::= NAME-TYPE TLV-LENGTH
+    //                  Name
+
+    size_t totalLength = 0;
+    for (const auto& element : vec) {
+      size_t nameLength = 0;
+      nameLength += element.wireEncode(encoder);
+      totalLength += nameLength;
+    }
+    totalLength += encoder.prependVarNumber(totalLength);
+    totalLength += encoder.prependVarNumber(tlv::Content);
+    return totalLength;
+  }
+};
+
+} // namespace tests
+} // namespace ntorrent
+} // namespace ndn
+
+#endif // NDN_TESTS_UNIT_TESTS_DUMMY_PARSER_HPP
diff --git a/tests/unit-tests/torrent-manager.t.cpp b/tests/unit-tests/torrent-manager.t.cpp
index eba4ae4..cf009f1 100644
--- a/tests/unit-tests/torrent-manager.t.cpp
+++ b/tests/unit-tests/torrent-manager.t.cpp
@@ -21,6 +21,7 @@
 
 #include "boost-test.hpp"
 
+#include "dummy-parser-fixture.hpp"
 #include "torrent-manager.hpp"
 #include "torrent-file.hpp"
 #include "unit-test-time-fixture.hpp"
@@ -43,18 +44,14 @@
 namespace fs = boost::filesystem;
 
 class TestTorrentManager : public TorrentManager {
- public:
-  TestTorrentManager(const ndn::Name&   torrentFileName,
-                     const std::string& filePath)
-  : TorrentManager(torrentFileName, filePath)
-  {
-  }
-
+public:
   TestTorrentManager(const ndn::Name&                 torrentFileName,
                      const std::string&               filePath,
                      std::shared_ptr<DummyClientFace> face)
   : TorrentManager(torrentFileName, filePath, face)
+  , m_face(face)
   {
+    m_keyChain = make_shared<KeyChain>();
   }
 
   std::vector<TorrentFile> torrentSegments() const {
@@ -111,6 +108,18 @@
   bool writeFileManifest(const FileManifest& manifest, const std::string& path) {
     return TorrentManager::writeFileManifest(manifest, path);
   }
+
+  void sendRoutablePrefixResponse() {
+    // Create a data packet containing one name as content
+    shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+                                                       { Name("ucla") });
+    m_keyChain->sign(*d);
+    m_face->receive(*d);
+  }
+
+private:
+  shared_ptr<KeyChain> m_keyChain;
+  shared_ptr<DummyClientFace> m_face;
 };
 
 class FaceFixture : public UnitTestTimeFixture
@@ -138,7 +147,6 @@
     : FaceFixture(false)
   {
   }
-
 };
 
 BOOST_FIXTURE_TEST_SUITE(TestTorrentManagerInitialize, FaceFixture)
@@ -202,8 +210,12 @@
     TestTorrentManager manager(initialSegmentName,
                                filePath,
                                face);
+
     manager.Initialize();
 
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     // Check that the torrent segments and file manifests match (content and order)
     BOOST_CHECK(manager.torrentSegments() == torrentSegments);
     BOOST_CHECK(manager.fileManifests()   == manifests);
@@ -222,8 +234,13 @@
 BOOST_AUTO_TEST_CASE(CheckInitializeEmpty)
 {
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=02c737fd4c6e7de4b4825b089f39700c2dfa8fd2bb2b91f09201e357c4463253",
-                             "tests/testdata/");
+                             "tests/testdata/", face);
+
   manager.Initialize();
+
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   BOOST_CHECK(manager.torrentSegments() == vector<TorrentFile>());
   BOOST_CHECK(manager.fileManifests()   == vector<FileManifest>());
 }
@@ -276,8 +293,12 @@
     TestTorrentManager manager(initialSegmentName,
                                filePath,
                                face);
+
     manager.Initialize();
 
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     // Check that the torrent segments and file manifests match (content and order)
     BOOST_CHECK(manager.torrentSegments() == torrentSegments);
     BOOST_CHECK(manager.fileManifests()   == vector<FileManifest>());
@@ -342,12 +363,17 @@
       boost::filesystem::create_directory(filename.parent_path());
       io::save(m, filename.string());
     }
+
     // Initialize and verify
     TestTorrentManager manager(initialSegmentName,
                                filePath,
                                face);
+
     manager.Initialize();
 
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     // Check that the torrent segments and file manifests match (content and order)
     BOOST_CHECK(manager.torrentSegments() == torrentSegments);
     BOOST_CHECK(manager.fileManifests()   == manifests);
@@ -388,8 +414,12 @@
 
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=946b92641d2b87bf4f5913930be20e3789ff5fb5d72739614f93f677d90fbd9d",
                              filePath, face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   // Test download torrent file segments
   uint32_t counter = 0;
   manager.downloadTorrentFile(filePath + "torrent_files", [&counter, &torrentSegments]
@@ -436,8 +466,12 @@
 
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=946b92641d2b87bf4f5913930be20e3789ff5fb5d72739614f93f677d90fbd9d",
                              filePath, face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   // Test download manifest segments -- 2 files (the first one segment, the second multiple)
   int counter = 0;
   manager.download_file_manifest(manifests[0].getFullName(), filePath + "manifests",
@@ -494,8 +528,12 @@
   std::string filePath = ".appdata/foo/";
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=946b92641d2b87bf4f5913930be20e3789ff5fb5d72739614f93f677d90fbd9d",
                              filePath, face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   Name dataName("/test/ucla");
 
   // Download data successfully
@@ -537,8 +575,12 @@
   std::string filePath = ".appdata/foo/";
   TestTorrentManager manager("NTORRENT/test/torrent-file/sha256digest",
                              filePath, face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   TorrentFile t1(Name("NTORRENT/test/torrent-file/sha256digest"),
                  Name("NTORRENT/test/torrent-file/1/sha256digest"), Name("/test"),
                  { Name("/manifest1") });
@@ -576,11 +618,16 @@
 BOOST_AUTO_TEST_CASE(TestFindTorrentFileSegmentToDownload2)
 {
   std::string filePath = ".appdata/foo/";
-  TestTorrentManager manager("/test/0/sha256digest",
+  TestTorrentManager manager("NTORRENT/test/torrent-file/0/sha256digest",
                              filePath, face);
+
   manager.Initialize();
 
-  BOOST_CHECK_EQUAL(manager.findTorrentFileSegmentToDownload()->toUri(), "/test/0/sha256digest");
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
+  BOOST_CHECK_EQUAL(manager.findTorrentFileSegmentToDownload()->toUri(),
+                    "/NTORRENT/test/torrent-file/0/sha256digest");
 
   fs::remove_all(filePath);
   fs::remove_all(".appdata");
@@ -631,8 +678,12 @@
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=a8a2e98cd943d895b8c4b12a208343bcf9344ce85a6376dc6f5754fe8f4a573e",
                              filePath,
                              face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   // Set the file state
   std::vector<bool> v1 = {true};
   manager.setFileState(manifests[0].getFullName(), make_shared<fs::fstream>(), v1);
@@ -675,10 +726,14 @@
 BOOST_AUTO_TEST_CASE(TestFindManifestSegmentToDownload1)
 {
   std::string filePath = ".appdata/foo/";
-  TestTorrentManager manager("NTORRENT/test/sha256digest",
+  TestTorrentManager manager("NTORRENT/test/torrent-file/sha256digest",
                              filePath, face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   Name n1(Name("NTORRENT/test/file0"));
   n1.appendSequenceNumber(0);
 
@@ -786,8 +841,12 @@
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=a8a2e98cd943d895b8c4b12a208343bcf9344ce85a6376dc6f5754fe8f4a573e",
                              filePath,
                              face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   // Set the file state
   std::vector<bool> v1 = {true};
   manager.setFileState(manifests[0].getFullName(), make_shared<fs::fstream>(), v1);
@@ -885,8 +944,12 @@
   TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=a8a2e98cd943d895b8c4b12a208343bcf9344ce85a6376dc6f5754fe8f4a573e",
                              filePath,
                              face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   // Set the file state
   std::vector<bool> v1 = {true};
   manager.setFileState(manifests[0].getFullName(), make_shared<fs::fstream>(), v1);
@@ -973,7 +1036,12 @@
     TestTorrentManager manager(initialSegmentName,
                                filePath,
                                face);
+
     manager.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     size_t nData = 0;
     BOOST_CHECK_EQUAL(0, face->sentData.size());
     // request all the torrent segments
@@ -1093,8 +1161,12 @@
   TestTorrentManager manager(initialSegmentName,
                              filePath,
                              face);
+
   manager.Initialize();
 
+  advanceClocks(time::milliseconds(1), 10);
+  manager.sendRoutablePrefixResponse();
+
   // insert the other entities
   data.insert(data.end(), torrentSegments.begin(), torrentSegments.end());
   data.insert(data.end(), manifests.begin(), manifests.end());
@@ -1189,7 +1261,12 @@
     TestTorrentManager manager(initialSegmentName,
                                filePath,
                                face);
+
     manager.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     // check that initially there is no data on disk
     for (auto m : manager.fileManifests()) {
       auto fileState = manager.fileState(m.getFullName());
@@ -1273,7 +1350,12 @@
     TestTorrentManager manager(initialSegmentName,
                                filePath,
                                face);
+
     manager.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     std::string dirPath = ".appdata/foo/";
     std::string torrentPath = dirPath + "torrent_files/";
     BOOST_CHECK(manager.torrentSegments().empty());
@@ -1285,13 +1367,22 @@
     TestTorrentManager manager2(initialSegmentName,
                                 filePath,
                                 face);
+
     manager2.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager2.sendRoutablePrefixResponse();
+
     BOOST_CHECK(manager2.torrentSegments() == torrentSegments);
 
     // start anew
     fs::remove_all(torrentPath);
     fs::create_directories(torrentPath);
     manager.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     BOOST_CHECK(manager.torrentSegments().empty());
 
     // check that there is no dependence on the order of torrent segments
@@ -1350,7 +1441,12 @@
     TestTorrentManager manager(initialSegmentName,
                               filePath,
                               face);
+
     manager.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     for (const auto& t : torrentSegments) {
       manager.writeTorrentSegment(t, torrentPath);
     }
@@ -1366,12 +1462,20 @@
                                 face);
 
     manager2.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager2.sendRoutablePrefixResponse();
+
     BOOST_CHECK(manager2.fileManifests() == manifests);
 
     // start anew
     fs::remove_all(manifestPath);
     fs::create_directories(manifestPath);
     manager.Initialize();
+
+    advanceClocks(time::milliseconds(1), 10);
+    manager.sendRoutablePrefixResponse();
+
     BOOST_CHECK(manager.fileManifests().empty());
 
     // check that there is no dependence on the order of torrent segments
diff --git a/tests/unit-tests/update-handler.t.cpp b/tests/unit-tests/update-handler.t.cpp
new file mode 100644
index 0000000..22624b6
--- /dev/null
+++ b/tests/unit-tests/update-handler.t.cpp
@@ -0,0 +1,277 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent 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 Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#include "boost-test.hpp"
+#include "update-handler.hpp"
+#include "unit-test-time-fixture.hpp"
+#include "dummy-parser-fixture.hpp"
+
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/util/io.hpp>
+
+namespace ndn {
+namespace ntorrent {
+namespace tests {
+
+using util::DummyClientFace;
+using std::vector;
+
+class TestUpdateHandler : public UpdateHandler {
+public:
+  TestUpdateHandler(Name torrentName, shared_ptr<KeyChain> keyChain,
+                    shared_ptr<StatsTable> statsTable, shared_ptr<Face> face)
+  : UpdateHandler(torrentName, keyChain, statsTable, face)
+  {
+  }
+
+  ~TestUpdateHandler()
+  {
+  }
+
+  Name
+  getOwnRoutablePrefix()
+  {
+    return UpdateHandler::getOwnRoutablePrefix();
+  }
+};
+
+class FaceFixture : public UnitTestTimeFixture
+{
+public:
+  explicit
+  FaceFixture()
+  : face1(util::makeDummyClientFace(io, { true, true }))
+  , face2(util::makeDummyClientFace(io, { true, true }))
+  {
+  }
+
+public:
+  shared_ptr<DummyClientFace> face1;
+  shared_ptr<DummyClientFace> face2;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestUpdateHandlerClass, FaceFixture)
+
+BOOST_AUTO_TEST_CASE(TestInitialSetup)
+{
+  shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
+
+  shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
+
+  TestUpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
+  advanceClocks(time::milliseconds(1), 10);
+  // Create a data packet containing one name as content
+  shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+                                                      { Name("ucla") });
+  keyChain->sign(*d);
+  face1->receive(*d);
+
+  BOOST_CHECK_EQUAL(handler1.getOwnRoutablePrefix().toUri(), "/ucla");
+
+  shared_ptr<StatsTable> table2 = make_shared<StatsTable>(Name("linux15.01"));
+  TestUpdateHandler handler2(Name("linux15.01"), keyChain, table2, face2);
+  advanceClocks(time::milliseconds(1), 10);
+  // Create a data packet containing one name as content
+  d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+                                     { Name("arizona") });
+  keyChain->sign(*d);
+  face2->receive(*d);
+
+  BOOST_CHECK_EQUAL(handler2.getOwnRoutablePrefix().toUri(), "/arizona");
+}
+
+BOOST_AUTO_TEST_CASE(TestAliveInterestExchange)
+{
+  shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
+  table1->insert(Name("isp1"));
+  table1->insert(Name("isp2"));
+  table1->insert(Name("isp3"));
+
+  shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
+
+  TestUpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
+  advanceClocks(time::milliseconds(1), 10);
+  // Create a data packet containing one name as content
+  shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+                                                      { Name("ucla") });
+  keyChain->sign(*d);
+  face1->receive(*d);
+
+  shared_ptr<StatsTable> table2 = make_shared<StatsTable>(Name("linux15.01"));
+  table2->insert(Name("ucla"));
+  TestUpdateHandler handler2(Name("linux15.01"), keyChain, table2, face2);
+  advanceClocks(time::milliseconds(1), 10);
+  // Create a data packet containing one name as content
+  d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+                                     { Name("arizona") });
+  keyChain->sign(*d);
+  face2->receive(*d);
+
+  handler2.sendAliveInterest(table2->begin());
+
+  advanceClocks(time::milliseconds(1), 40);
+  Interest interest(Name("/NTORRENT/linux15.01/ALIVE/test"));
+  face1->receive(interest);
+
+  advanceClocks(time::milliseconds(1), 10);
+  std::vector<Data> dataVec = face1->sentData;
+
+  BOOST_CHECK_EQUAL(dataVec.size(), 1);
+  BOOST_CHECK_EQUAL(dataVec[0].getName().toUri(), "/NTORRENT/linux15.01/ALIVE/test");
+
+  auto block = dataVec[0].getContent();
+  shared_ptr<vector<Name>> nameVec = DummyParser::decodeContent(block);
+  auto it = nameVec->begin();
+
+  BOOST_CHECK_EQUAL(it->toUri(), "/test");
+  ++it;
+  BOOST_CHECK_EQUAL(it->toUri(), "/isp3");
+  ++it;
+  BOOST_CHECK_EQUAL(it->toUri(), "/isp2");
+  ++it;
+  BOOST_CHECK_EQUAL(it->toUri(), "/isp1");
+
+  BOOST_CHECK_EQUAL((table1->end() - 1)->getRecordName().toUri(), "/test");
+
+  d = DummyParser::createDataPacket(Name("/NTORRENT/linux15.01/ALIVE/arizona"),
+                                     { Name("isp1"), Name("isp2"), Name("isp3") });
+  keyChain->sign(*d);
+
+  advanceClocks(time::milliseconds(1), 40);
+  face2->receive(*d);
+
+  auto i = table2->begin();
+  ++i;
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
+  ++i;
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
+  ++i;
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
+
+  table1->erase(Name("isp1"));
+  table1->erase(Name("isp2"));
+
+  table1->insert(Name("isp4"));
+  table1->insert(Name("isp5"));
+  table1->insert(Name("isp6"));
+  table1->insert(Name("isp7"));
+  table1->insert(Name("isp8"));
+  table1->insert(Name("isp9"));
+
+  handler2.sendAliveInterest(table2->begin());
+
+  advanceClocks(time::milliseconds(1), 40);
+  Interest interest2(Name("/NTORRENT/linux15.01/ALIVE/arizona"));
+  face1->receive(interest2);
+
+  advanceClocks(time::milliseconds(1), 10);
+
+  dataVec = face1->sentData;
+  BOOST_CHECK_EQUAL(dataVec.size(), 2);
+
+  auto iter = dataVec.begin() + 1;
+  advanceClocks(time::milliseconds(1), 30);
+  face2->receive(*iter);
+
+  i = table2->begin();
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/ucla");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/test");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp4");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp5");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp6");
+  BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+  BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+  ++i;
+
+  BOOST_CHECK(i == table2->end());
+}
+
+BOOST_AUTO_TEST_CASE(TestNeedsUpdate)
+{
+  shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
+  table1->insert(Name("isp1"));
+  table1->insert(Name("isp2"));
+  table1->insert(Name("isp3"));
+
+  shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
+
+  UpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
+
+  BOOST_CHECK(handler1.needsUpdate());
+
+  auto i = table1->begin() + 1;
+  i->incrementSentInterests();
+  i->incrementSentInterests();
+  i->incrementReceivedData();
+
+  BOOST_CHECK(handler1.needsUpdate());
+
+  table1->insert(Name("isp4"));
+  table1->insert(Name("isp5"));
+
+  BOOST_CHECK(!handler1.needsUpdate());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ntorrent
+} // namespace ndn