tests[content-server]: Rewrite all tests cases

Change-Id: Id3daa7c1ba757f60abe9a460dcaa6772fd92aea8
diff --git a/tests/unit-tests/content-server.t.cpp b/tests/unit-tests/content-server.t.cpp
new file mode 100644
index 0000000..8dca18f
--- /dev/null
+++ b/tests/unit-tests/content-server.t.cpp
@@ -0,0 +1,150 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017, Regents of the University of California.
+ *
+ * This file is part of ChronoShare, a decentralized file sharing application over NDN.
+ *
+ * ChronoShare 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.
+ *
+ * ChronoShare 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 copies of the GNU General Public License along with
+ * ChronoShare, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ChronoShare authors and contributors.
+ */
+
+#include "content-server.hpp"
+#include "action-log.hpp"
+#include "sync-core.hpp"
+
+#include "test-common.hpp"
+#include "dummy-forwarder.hpp"
+
+namespace ndn {
+namespace chronoshare {
+namespace tests {
+
+namespace fs = boost::filesystem;
+
+_LOG_INIT(Test.ContentServer);
+
+class TestContentServerFixture : public IdentityManagementTimeFixture
+{
+public:
+  TestContentServerFixture()
+    : forwarder(m_io, m_keyChain)
+    , face(forwarder.addFace())
+    , appName("test-chronoshare")
+    , deviceName("/device")
+    , shareFolderName("sharefolder")
+    , root("test-server-and-fetch")
+  {
+    cleanDir(root);
+
+    create_directory(root);
+
+    syncLog = make_shared<SyncLog>(root, deviceName);
+    actionLog = std::make_shared<ActionLog>(face, root, syncLog,
+                                            "top-secret", "test-chronoshare",
+                                            ActionLog::OnFileAddedOrChangedCallback(),
+                                            ActionLog::OnFileRemovedCallback());
+
+    actionLog->AddLocalActionUpdate("file.txt",
+                                    *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
+                                    std::time(nullptr), 0755, 10);
+    BOOST_CHECK_EQUAL(syncLog->SeqNo(deviceName), 1);
+
+    ndn::ConstBufferPtr hash = syncLog->RememberStateInStateLog();
+
+    server = make_unique<ContentServer>(face, actionLog, root,
+                                        deviceName, shareFolderName,
+                                        "test-chronoshare", m_keyChain, 5);
+
+    Name localPrefix("/local");
+    Name broadcastPrefix("/multicast");
+
+    server->registerPrefix(localPrefix);
+    server->registerPrefix(broadcastPrefix);
+
+    advanceClocks(time::milliseconds(10), 1000);
+  }
+
+  ~TestContentServerFixture()
+  {
+    cleanDir(root);
+  }
+
+  void
+  onActionData(const Interest& interest, Data& data)
+  {
+    _LOG_DEBUG("on action data, interest Name: " << interest);
+  }
+
+  void
+  onTimeout(const Interest& interest)
+  {
+    _LOG_DEBUG("on timeout, interest Name: " << interest);
+    BOOST_CHECK(false);
+  }
+
+  void cleanDir(fs::path dir) {
+    if (exists(dir)) {
+      remove_all(dir);
+    }
+  }
+
+public:
+  DummyForwarder forwarder;
+  Face& face;
+  shared_ptr<SyncLog> syncLog;
+  shared_ptr<ActionLog> actionLog;
+  unique_ptr<ContentServer> server;
+  Name appName;
+  Name deviceName;
+
+  std::string shareFolderName;
+
+  fs::path root;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestContentServer, TestContentServerFixture)
+
+BOOST_AUTO_TEST_CASE(TestContentServerServe)
+{
+  Interest interest(
+    Name("/local").append(deviceName).append(appName).append("action")
+                  .append(shareFolderName).appendNumber(1));
+
+  _LOG_DEBUG("interest Name: " << interest);
+
+  dynamic_cast<util::DummyClientFace*>(&face)->receive(interest);
+
+  advanceClocks(time::milliseconds(10), 1000);
+
+  BOOST_CHECK_EQUAL(dynamic_cast<util::DummyClientFace*>(&face)->sentData.size(),1);
+
+  Data data = dynamic_cast<util::DummyClientFace*>(&face)->sentData.at(0);
+
+  BOOST_CHECK_EQUAL(data.getName(), "/local/device/test-chronoshare/action/sharefolder/%01");
+
+  shared_ptr<ActionItem> action = deserializeMsg<ActionItem>(
+    Buffer(data.getContent().value(), data.getContent().value_size()));
+
+  BOOST_CHECK_EQUAL(action->action(), 0);
+  BOOST_CHECK_EQUAL(action->filename(), "file.txt");
+  BOOST_CHECK_EQUAL(action->file_hash().size(), 32);
+  BOOST_CHECK_EQUAL(action->version(), 0);
+  BOOST_CHECK_EQUAL(action->has_parent_device_name(), false);
+  BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace chronoshare
+} // namespace ndn
diff --git a/tests/unit-tests/serve-and-fetch.t.cpp b/tests/unit-tests/serve-and-fetch.t.cpp
index da6aa1a..c5ea473 100644
--- a/tests/unit-tests/serve-and-fetch.t.cpp
+++ b/tests/unit-tests/serve-and-fetch.t.cpp
@@ -18,13 +18,12 @@
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
 
-#include "ccnx-common.hpp"
-#include "ccnx-wrapper.hpp"
 #include "content-server.hpp"
 #include "fetch-manager.hpp"
 #include "object-db.hpp"
 #include "object-manager.hpp"
-#include "scheduler.hpp"
+#include "dummy-forwarder.hpp"
+
 #include <boost/filesystem.hpp>
 #include <boost/make_shared.hpp>
 #include <boost/test/unit_test.hpp>
@@ -35,134 +34,143 @@
 #include <ctime>
 #include <stdio.h>
 
-#include "logging.hpp"
+#include "test-common.hpp"
 
-_LOG_INIT(Test.ServerAndFetch);
 
-using namespace Ndnx;
 using namespace std;
 using namespace boost;
 using namespace boost::filesystem;
 
-BOOST_AUTO_TEST_SUITE(TestServeAndFetch)
 
-path root("test-server-and-fetch");
-path filePath = root / "random-file";
-unsigned char magic = 'm';
-int repeat = 1024 * 400;
-mutex mut;
-condition_variable cond;
-bool finished;
-int ack;
+namespace ndn {
+namespace chronoshare {
+namespace tests {
 
-void
-setup()
+_LOG_INIT(Test.ServerAndFetch);
+
+class TestServerAndFetchFixture : public IdentityManagementTimeFixture
 {
-  if (exists(root)) {
-    remove_all(root);
+public:
+  TestServerAndFetchFixture()
+    : forwarder(m_io, m_keyChain)
+    , root("test-server-and-fetch")
+    , filePath(root / "random-file")
+    , character('m')
+    , repeat(1024 * 40)
+    , finished(false)
+    , ack(0)
+  {
+    if (exists(root)) {
+      remove_all(root);
+    }
+
+    create_directory(root);
+
+    // create file
+    FILE* fp = fopen(filePath.string().c_str(), "w");
+    for (int i = 0; i < repeat; i++) {
+      fwrite(&character, 1, sizeof(character), fp);
+    }
+    fclose(fp);
+
+    ack = 0;
+    finished = false;
   }
 
-  create_directory(root);
-
-  // create file
-  FILE* fp = fopen(filePath.string().c_str(), "w");
-  for (int i = 0; i < repeat; i++) {
-    fwrite(&magic, 1, sizeof(magic), fp);
-  }
-  fclose(fp);
-
-  ack = 0;
-  finished = false;
-}
-
-void
-teardown()
-{
-  if (exists(root)) {
-    remove_all(root);
+  ~TestServerAndFetchFixture()
+  {
+    if (exists(root)) {
+      remove_all(root);
+    }
   }
 
-  ack = 0;
-  finished = false;
-}
-
-Name
-simpleMap(const Name& deviceName)
-{
-  return Name("/local");
-}
-
-void
-segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq, PcoPtr pco)
-{
-  ack++;
-  Bytes co = pco->content();
-  int size = co.size();
-  for (int i = 0; i < size; i++) {
-    BOOST_CHECK_EQUAL(co[i], magic);
+  Name
+  simpleMap(const Name& deviceName)
+  {
+    return Name("/local");
   }
-}
 
-void
-finishCallback(Name& deviceName, Name& baseName)
-{
-  BOOST_CHECK_EQUAL(ack, repeat / 1024);
-  unique_lock<mutex> lock(mut);
-  finished = true;
-  cond.notify_one();
-}
+  void
+  segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq,
+                  ndn::shared_ptr<ndn::Data> data)
+  {
+    ack++;
+    ndn::Block block = Data(data->getContent().blockFromValue()).getContent();
+    int size = block.value_size();
+    const uint8_t* co = block.value();
+    for (int i = 0; i < size; i++) {
+      BOOST_CHECK_EQUAL(co[i], character);
+    }
+
+    _LOG_DEBUG("I'm called!!! segmentCallback!! size " << size << " ack times:" << ack);
+  }
+
+  void
+  finishCallback(Name& deviceName, Name& baseName)
+  {
+    BOOST_CHECK_EQUAL(ack, repeat / 1024);
+    finished = true;
+    cond.notify_one();
+  }
+
+public:
+  DummyForwarder forwarder;
+  path root;
+  path filePath;
+
+  unsigned char character;
+  int repeat;
+
+  boost::condition_variable cond;
+  bool finished;
+  int ack;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestServeAndFetch, TestServerAndFetchFixture)
 
 BOOST_AUTO_TEST_CASE(TestServeAndFetch)
 {
-  _LOG_DEBUG("Setting up test environment ...");
-  setup();
-
-  NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
-  usleep(1000);
-  NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
+  Face& face_serve = forwarder.addFace();
+  Face& face_fetch = forwarder.addFace();
 
   Name deviceName("/test/device");
   Name localPrefix("/local");
-  Name broadcastPrefix("/broadcast");
+  Name broadcastPrefix("/multicast");
 
   const string APPNAME = "test-chronoshare";
 
-  time_t start = time(NULL);
+  time_t start = std::time(NULL);
   _LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ...");
+
   // publish file to db
-  ObjectManager om(ndnx_serve, root, APPNAME);
-  tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
-  time_t end = time(NULL);
+  ObjectManager om(face_serve, m_keyChain, root, APPNAME);
+  auto pub = om.localFileToObjects(filePath, deviceName);
+
+  time_t end = std::time(NULL);
   _LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start
                         << " seconds ...");
 
   ActionLogPtr dummyLog;
-  ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
+  ContentServer server(face_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, m_keyChain, 5);
   server.registerPrefix(localPrefix);
   server.registerPrefix(broadcastPrefix);
 
-  FetchManager fm(ccnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
-  HashPtr hash = pub.get<0>();
-  Name baseName = Name("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
+  FetchManager fm(face_fetch, bind(&TestServerAndFetchFixture::simpleMap, this, _1), Name("/"));
+  ConstBufferPtr hash = std::get<0>(pub);
 
-  fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4),
-             bind(finishCallback, _1, _2), 0, pub.get<1>() - 1);
+  Name baseName = Name(deviceName);
+  baseName.append(APPNAME).append("file").appendImplicitSha256Digest(hash);
 
-  unique_lock<mutex> lock(mut);
-  system_time timeout = get_system_time() + posix_time::milliseconds(5000);
-  while (!finished) {
-    if (!cond.timed_wait(lock, timeout)) {
-      BOOST_FAIL("Fetching has not finished after 5 seconds");
-      break;
-    }
-  }
-  ccnx_fetch->shutdown();
-  ccnx_serve->shutdown();
+  fm.Enqueue(deviceName, baseName, bind(&TestServerAndFetchFixture::segmentCallback, this, _1, _2, _3, _4),
+             bind(&TestServerAndFetchFixture::finishCallback, this, _1, _2), 0, std::get<1>(pub) - 1);
+
+  advanceClocks(time::milliseconds(10), 1000);
 
   _LOG_DEBUG("Finish");
-  usleep(100000);
-
-  teardown();
 }
 
 BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace chronoshare
+} // namespace ndn
diff --git a/tests/wscript b/tests/wscript
index affe8fc..13d8ec7 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -27,6 +27,8 @@
                                       'unit-tests/action-log.t.cpp',
                                       'unit-tests/object-*.t.cpp',
                                       'unit-tests/fetch*.t.cpp',
+                                      'unit-tests/serve-and-fetch.t.cpp',
+                                      'unit-tests/content-server.t.cpp',
                                       ],
                                      excl=['main.cpp']),
             use='chronoshare core-objects unit-tests-main',