tests[sync]: Fix test cases

Change-Id: I44b17e9647b8f9ed9b82381e0201d1d10ce1731c
diff --git a/tests/test-common.cpp b/tests/test-common.cpp
index b9a9464..55b566e 100644
--- a/tests/test-common.cpp
+++ b/tests/test-common.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
  *                          Arizona Board of Regents,
  *                          Colorado State University,
  *                          University Pierre & Marie Curie, Sorbonne University,
@@ -26,6 +26,8 @@
  */
 
 #include "test-common.hpp"
+
+#include <ndn-cxx/util/digest.hpp>
 #include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
 
 namespace ndn {
@@ -121,6 +123,14 @@
   return nack;
 }
 
+ConstBufferPtr
+digestFromFile(const boost::filesystem::path& filename)
+{
+  boost::filesystem::ifstream iff(filename, std::ios::in | std::ios::binary);
+  util::Sha256 digest(iff);
+  return digest.computeDigest();
+}
+
 } // namespace tests
 } // namespace chronoshare
 } // namespace ndn
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index 6802e60..afbdac3 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
  *                          Arizona Board of Regents,
  *                          Colorado State University,
  *                          University Pierre & Marie Curie, Sorbonne University,
@@ -34,6 +34,7 @@
 
 #include <boost/asio/io_service.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/filesystem/fstream.hpp>
 
 #include <ndn-cxx/name.hpp>
 #include <ndn-cxx/data.hpp>
@@ -165,6 +166,11 @@
   packet.setName(name);
 }
 
+/** \brief convert file to digest
+ */
+ndn::ConstBufferPtr
+digestFromFile(const boost::filesystem::path& filename);
+
 } // namespace tests
 } // namespace chronoshare
 } // namespace ndn
diff --git a/tests/unit-tests/sync-core.t.cpp b/tests/unit-tests/sync-core.t.cpp
index 0d55fd1..5fdd5fa 100644
--- a/tests/unit-tests/sync-core.t.cpp
+++ b/tests/unit-tests/sync-core.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
  *
  * This file is part of ChronoShare, a decentralized file sharing application over NDN.
  *
@@ -17,32 +17,44 @@
  *
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
-#include "logging.hpp"
+
 #include "sync-core.hpp"
 
-#include <boost/filesystem.hpp>
-#include <boost/make_shared.hpp>
-#include <boost/test/unit_test.hpp>
+#include "test-common.hpp"
 
-using namespace std;
-using namespace Ndnx;
-using namespace boost;
-using namespace boost::filesystem;
+namespace ndn {
+namespace chronoshare {
+namespace tests {
 
-INIT_LOGGER("Test.SyncCore");
+namespace fs = boost::filesystem;
 
-BOOST_AUTO_TEST_SUITE(SyncCoreTests)
+INIT_LOGGER("Test.SyncCore")
+
+class TestSyncCoreFixture : public IdentityManagementTimeFixture
+{
+public:
+  void
+  advanceClocks()
+  {
+    for (int i = 0; i < 100; ++i) {
+      usleep(10000);
+      IdentityManagementTimeFixture::advanceClocks(time::milliseconds(10), 1);
+    }
+  }
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestSyncCore, TestSyncCoreFixture)
 
 void
 callback(const SyncStateMsgPtr& msg)
 {
+  _LOG_DEBUG("Callback I'm called!!!!");
   BOOST_CHECK(msg->state_size() > 0);
   int size = msg->state_size();
   int index = 0;
   while (index < size) {
     SyncState state = msg->state(index);
     BOOST_CHECK(state.has_old_seq());
-    BOOST_CHECK(state.old_seq() >= 0);
     if (state.seq() != 0) {
       BOOST_CHECK(state.old_seq() != state.seq());
     }
@@ -50,71 +62,63 @@
   }
 }
 
-void
-checkRoots(const HashPtr& root1, const HashPtr& root2)
+BOOST_AUTO_TEST_CASE(TwoNodes)
 {
-  BOOST_CHECK_EQUAL(*root1, *root2);
-}
-
-BOOST_AUTO_TEST_CASE(SyncCoreTest)
-{
-  INIT_LOGGERS();
-
-  string dir = "./SyncCoreTest";
-  // clean the test dir
-  path d(dir);
-  if (exists(d)) {
-    remove_all(d);
+  fs::path tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH) / "SyncCoreTest";
+  if (exists(tmpdir)) {
+    remove_all(tmpdir);
   }
 
-  string dir1 = "./SyncCoreTest/1";
-  string dir2 = "./SyncCoreTest/2";
-  Name user1("/joker");
-  Name loc1("/gotham1");
-  Name user2("/darkknight");
-  Name loc2("/gotham2");
-  Name syncPrefix("/broadcast/darkknight");
-  NdnxWrapperPtr c1(new NdnxWrapper());
-  NdnxWrapperPtr c2(new NdnxWrapper());
-  SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
-  SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
+  std::string dir1 = (tmpdir / "1").string();
+  std::string dir2 = (tmpdir / "2").string();
+  Name user1("/shuai");
+  Name loc1("/locator1");
+  Name user2("/loli");
+  Name loc2("/locator2");
+  Name syncPrefix("/broadcast/arslan");
 
-  SyncCore* core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1);
-  usleep(10000);
-  SyncCore* core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2);
+  shared_ptr<Face> c1 = make_shared<Face>(this->m_io);
+  auto log1 = make_shared<SyncLog>(dir1, user1);
+  auto core1 = make_shared<SyncCore>(*c1, log1, user1, loc1, syncPrefix, bind(&callback, _1));
 
-  sleep(1);
-  checkRoots(core1->root(), core2->root());
+  shared_ptr<Face> c2 = make_shared<Face>(this->m_io);
+  auto log2 = make_shared<SyncLog>(dir2, user2);
+  auto core2 = make_shared<SyncCore>(*c2, log2, user2, loc2, syncPrefix, bind(&callback, _1));
 
-  // _LOG_TRACE ("\n\n\n\n\n\n----------\n");
+  // @TODO: Implement test using the dummy forwarder and disable dependency on real time
+
+  this->advanceClocks();
+
+  BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
+
+  // _LOG_TRACE("\n\n\n\n\n\n----------\n");
 
   core1->updateLocalState(1);
-  usleep(100000);
-  checkRoots(core1->root(), core2->root());
+  this->advanceClocks();
+  BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
   BOOST_CHECK_EQUAL(core2->seq(user1), 1);
   BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
 
   core1->updateLocalState(5);
-  usleep(100000);
-  checkRoots(core1->root(), core2->root());
+  this->advanceClocks();
+  BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
   BOOST_CHECK_EQUAL(core2->seq(user1), 5);
   BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
 
   core2->updateLocalState(10);
-  usleep(100000);
-  checkRoots(core1->root(), core2->root());
+  this->advanceClocks();
+  BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
   BOOST_CHECK_EQUAL(core1->seq(user2), 10);
   BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
 
   // simple simultaneous data generation
-  // _LOG_TRACE ("\n\n\n\n\n\n----------Simultaneous\n");
-  _LOG_TRACE("Simultaneous");
+  _LOG_TRACE("\n\n\n\n\n\n----------Simultaneous\n");
 
   core1->updateLocalState(11);
-  usleep(100);
+  this->advanceClocks();
   core2->updateLocalState(15);
-  usleep(2000000);
-  checkRoots(core1->root(), core2->root());
+  this->advanceClocks();
+  BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
   BOOST_CHECK_EQUAL(core1->seq(user2), 15);
   BOOST_CHECK_EQUAL(core2->seq(user1), 11);
 
@@ -122,11 +126,10 @@
   BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
   BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
   BOOST_CHECK_EQUAL(log2->LookupLocator(user2), loc2);
-
-  // clean the test dir
-  if (exists(d)) {
-    remove_all(d);
-  }
 }
 
 BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace chronoshare
+} // namespace ndn
diff --git a/tests/unit-tests/sync-log.t.cpp b/tests/unit-tests/sync-log.t.cpp
index 9e9ebb0..bf5801e 100644
--- a/tests/unit-tests/sync-log.t.cpp
+++ b/tests/unit-tests/sync-log.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
  *
  * This file is part of ChronoShare, a decentralized file sharing application over NDN.
  *
@@ -18,103 +18,103 @@
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
 
-#include <boost/lexical_cast.hpp>
-#include <boost/test/unit_test.hpp>
+#include "sync-log.hpp"
 
-#include "action-log.hpp"
-#include "logging.hpp"
-#include <boost/filesystem.hpp>
-#include <ccnx-name.hpp>
-#include <iostream>
-#include <unistd.h>
+#include "test-common.hpp"
 
-using namespace std;
-using namespace boost;
-using namespace Ndnx;
+namespace ndn {
+namespace chronoshare {
+namespace tests {
+
 namespace fs = boost::filesystem;
 
-BOOST_AUTO_TEST_SUITE(TestSyncLog)
+INIT_LOGGER("Test.SyncLog")
 
+BOOST_FIXTURE_TEST_SUITE(TestSyncLog, IdentityManagementTimeFixture)
 
 BOOST_AUTO_TEST_CASE(BasicDatabaseTest)
 {
-  INIT_LOGGERS();
+  fs::path tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH);
+  if (exists(tmpdir)) {
+    remove_all(tmpdir);
+  }
 
-  fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%");
-  SyncLog db(tmpdir, Name("/alex"));
+  SyncLog db(tmpdir, Name("/lijing"));
 
-  HashPtr hash = db.RememberStateInStateLog();
+  ndn::ConstBufferPtr hash = db.RememberStateInStateLog();
   // should be empty
-  BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
-                    "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
 
-  db.UpdateDeviceSeqNo(Name("/alex"), 1);
+  BOOST_CHECK_EQUAL(toHex(*hash),
+                    "94D988A90C6A3D0F74624368BE65E5369DDDDB3444841FAD4EF41F674B937F26");
+
+  db.UpdateDeviceSeqNo(Name("/lijing"), 1);
   hash = db.RememberStateInStateLog();
 
-  BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
-                    "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
+  BOOST_CHECK_EQUAL(toHex(*hash),
+                    "91A849EEDE75ACD56AE1BCB99E92D8FB28757683BC387DBB0E59C3108FCF4F18");
 
-  db.UpdateDeviceSeqNo(Name("/alex"), 2);
+  db.UpdateDeviceSeqNo(Name("/lijing"), 2);
   hash = db.RememberStateInStateLog();
-  BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
-                    "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
+  BOOST_CHECK_EQUAL(toHex(*hash),
+                    "D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282");
 
-  db.UpdateDeviceSeqNo(Name("/alex"), 2);
+  db.UpdateDeviceSeqNo(Name("/lijing"), 2);
   hash = db.RememberStateInStateLog();
-  BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
-                    "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
+  BOOST_CHECK_EQUAL(toHex(*hash),
+                    "D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282");
 
-  db.UpdateDeviceSeqNo(Name("/alex"), 1);
+  db.UpdateDeviceSeqNo(Name("/lijing"), 1);
   hash = db.RememberStateInStateLog();
-  BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
-                    "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
+  BOOST_CHECK_EQUAL(toHex(*hash),
+                    "D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282");
 
-  db.UpdateLocator(Name("/alex"), Name("/hawaii"));
+  db.UpdateLocator(Name("/lijing"), Name("/hawaii"));
 
-  BOOST_CHECK_EQUAL(db.LookupLocator(Name("/alex")), Name("/hawaii"));
+  BOOST_CHECK_EQUAL(db.LookupLocator(Name("/lijing")), Name("/hawaii"));
 
-  SyncStateMsgPtr msg =
-    db.FindStateDifferences("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
+  SyncStateMsgPtr msg = db.FindStateDifferences("00", "95284D3132A7A88B85C5141CA63EFA68B7A7DAF37315DEF69E296A0C24692833");
   BOOST_CHECK_EQUAL(msg->state_size(), 0);
 
   msg = db.FindStateDifferences("00",
-                                "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
+                                "D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282");
   BOOST_CHECK_EQUAL(msg->state_size(), 1);
   BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE);
   BOOST_CHECK_EQUAL(msg->state(0).seq(), 2);
 
-  msg = db.FindStateDifferences("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c",
+  msg = db.FindStateDifferences("D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282",
                                 "00");
   BOOST_CHECK_EQUAL(msg->state_size(), 1);
   BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::DELETE);
 
-  msg = db.FindStateDifferences("7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405",
-                                "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
+  msg = db.FindStateDifferences("94D988A90C6A3D0F74624368BE65E5369DDDDB3444841FAD4EF41F674B937F26",
+                                "D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282");
   BOOST_CHECK_EQUAL(msg->state_size(), 1);
   BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE);
   BOOST_CHECK_EQUAL(msg->state(0).seq(), 2);
 
-  msg = db.FindStateDifferences("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c",
-                                "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
+  msg = db.FindStateDifferences("D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282",
+                                "94D988A90C6A3D0F74624368BE65E5369DDDDB3444841FAD4EF41F674B937F26");
   BOOST_CHECK_EQUAL(msg->state_size(), 1);
   BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE);
   BOOST_CHECK_EQUAL(msg->state(0).seq(), 0);
 
-  db.UpdateDeviceSeqNo(Name("/bob"), 1);
+  db.UpdateDeviceSeqNo(Name("/shuai"), 1);
   hash = db.RememberStateInStateLog();
-  BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
-                    "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
+  BOOST_CHECK_EQUAL(toHex(*hash),
+                    "602FF1878FC394B90E4A0E90C7409EA4B8EE8AA40169801D62F838470551DB7C");
 
   msg = db.FindStateDifferences("00",
-                                "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
+                                "602FF1878FC394B90E4A0E90C7409EA4B8EE8AA40169801D62F838470551DB7C");
   BOOST_CHECK_EQUAL(msg->state_size(), 2);
   BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE);
   BOOST_CHECK_EQUAL(msg->state(0).seq(), 2);
 
   BOOST_CHECK_EQUAL(msg->state(1).type(), SyncState::UPDATE);
   BOOST_CHECK_EQUAL(msg->state(1).seq(), 1);
-
-  remove_all(tmpdir);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace chronoshare
+} // namespace ndn
diff --git a/tests/unit-tests/protobuf.t.cpp b/tests/unit-tests/sync-state.pb.t.cpp
similarity index 69%
rename from tests/unit-tests/protobuf.t.cpp
rename to tests/unit-tests/sync-state.pb.t.cpp
index 6bf6f23..b0ce112 100644
--- a/tests/unit-tests/protobuf.t.cpp
+++ b/tests/unit-tests/sync-state.pb.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
  *
  * This file is part of ChronoShare, a decentralized file sharing application over NDN.
  *
@@ -17,26 +17,27 @@
  *
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
-#include "ccnx-common.hpp"
+
+#include "sync-state.pb.h"
 #include "sync-core.hpp"
+
 #include <boost/iostreams/device/back_inserter.hpp>
 #include <boost/iostreams/filter/gzip.hpp>
 #include <boost/iostreams/filtering_stream.hpp>
-#include <boost/make_shared.hpp>
-#include <boost/make_shared.hpp>
-#include <boost/range/iterator_range.hpp>
-#include <boost/test/unit_test.hpp>
 
-using namespace Ndnx;
-using namespace std;
-using namespace boost;
+#include "test-common.hpp"
 
-BOOST_AUTO_TEST_SUITE(ProtobufTests)
+namespace ndn {
+namespace chronoshare {
+namespace tests {
 
+INIT_LOGGER("Test.protobuf")
 
-BOOST_AUTO_TEST_CASE(TestGzipProtobuf)
+BOOST_AUTO_TEST_SUITE(TestSyncStatePb)
+
+BOOST_AUTO_TEST_CASE(Serialize)
 {
-  SyncStateMsgPtr msg = make_shared<SyncStateMsg>();
+  auto msg = make_shared<SyncStateMsg>();
 
   SyncState* state = msg->add_state();
   state->set_type(SyncState::UPDATE);
@@ -45,22 +46,26 @@
   state->set_locator(&x[0], sizeof(x));
   state->set_name(&x[0], sizeof(x));
 
-  BytesPtr bb = serializeMsg<SyncStateMsg>(*msg);
+  ndn::ConstBufferPtr bb = serializeMsg<SyncStateMsg>(*msg);
 
-  BytesPtr cb = serializeGZipMsg<SyncStateMsg>(*msg);
-  BOOST_CHECK(cb->size() < bb->size());
-  cout << cb->size() << ", " << bb->size() << endl;
+  ndn::ConstBufferPtr cb = serializeGZipMsg<SyncStateMsg>(*msg);
+  BOOST_CHECK_LT(cb->size(), bb->size());
 
   SyncStateMsgPtr msg1 = deserializeGZipMsg<SyncStateMsg>(*cb);
 
-  BOOST_REQUIRE(msg1->state_size() == 1);
+  BOOST_REQUIRE_EQUAL(msg1->state_size(), 1);
 
   SyncState state1 = msg1->state(0);
   BOOST_CHECK_EQUAL(state->seq(), state1.seq());
   BOOST_CHECK_EQUAL(state->type(), state1.type());
-  string sx(x, 100);
+
+  std::string sx(x, 100);
   BOOST_CHECK_EQUAL(sx, state1.name());
   BOOST_CHECK_EQUAL(sx, state1.locator());
 }
 
 BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace chronoshare
+} // namespace ndn
diff --git a/tests/wscript b/tests/wscript
index 5d667b5..b47447b 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -8,7 +8,7 @@
     if not bld.env['WITH_TESTS']:
         return
 
-    Logs.error("Most unit tests are temporary disabled")
+    Logs.error("Many unit tests are temporary disabled")
 
     bld(features='cxx',
         target='unit-tests-main',
@@ -21,8 +21,10 @@
     unit_tests = bld.program(
             target='../unit-tests',
             features='cxx cxxprogram',
-            source=bld.path.ant_glob(['**/*.cpp'], excl=['main.cpp',
-                                                         'unit-tests/**/*.cpp']),
+            source=bld.path.ant_glob(['*.cpp',
+                                      'unit-tests/sync-*.t.cpp',
+                                      ],
+                                     excl=['main.cpp']),
             use='unit-tests-main core-objects chronoshare',
             install_path=None,
             defines='UNIT_TEST_CONFIG_PATH=\"%s/tmp-files/\"' % (bld.bldnode),