tests+mgmt: Import common unit test code NFD

This commit introduces BaseFixture, UnitTestTimeFixture,
IdentityManagementFixture, and IdentityManagementTimeFixture.
In addition to that, the command line of the unit test binary (for boost
libraries before 1.62) are extended to support multi-format output.

This commit also addresses a strange bug in the management tool that was
causing spurious segmentation faults during unit tests.

Change-Id: Ib34b4007773f0b35d9ed919eebbdc4e0d413fa1e
diff --git a/tests/unit/clients/iterative-query-controller.cpp b/tests/unit/clients/iterative-query-controller.cpp
index a34375b..93bbb4e 100644
--- a/tests/unit/clients/iterative-query-controller.cpp
+++ b/tests/unit/clients/iterative-query-controller.cpp
@@ -19,13 +19,9 @@
 
 #include "clients/iterative-query-controller.hpp"
 #include "daemon/name-server.hpp"
-#include "logger.hpp"
-#include "../database-test-data.hpp"
-#include "../../boost-test.hpp"
 
-#include <ndn-cxx/util/dummy-client-face.hpp>
-
-#include <boost/asio.hpp>
+#include "test-common.hpp"
+#include "unit/database-test-data.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/clients/query.cpp b/tests/unit/clients/query.cpp
index 6fdca35..40fdfc3 100644
--- a/tests/unit/clients/query.cpp
+++ b/tests/unit/clients/query.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -19,7 +19,7 @@
 
 #include "clients/query.hpp"
 
-#include "../../boost-test.hpp"
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
@@ -69,6 +69,6 @@
 
 BOOST_AUTO_TEST_SUITE_END()
 
-}// namespace tests
-}// namespace ndns
+} // namespace tests
+} // namespace ndns
 } // namespace ndn
diff --git a/tests/unit/clients/response.cpp b/tests/unit/clients/response.cpp
index 7332c35..5950063 100644
--- a/tests/unit/clients/response.cpp
+++ b/tests/unit/clients/response.cpp
@@ -18,20 +18,17 @@
  */
 
 #include "clients/response.hpp"
-#include <ndn-cxx/security/key-chain.hpp>
-#include "../../boost-test.hpp"
+
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
 namespace tests {
 
-
-
-BOOST_AUTO_TEST_SUITE(Response)
+BOOST_FIXTURE_TEST_SUITE(Response, IdentityManagementFixture)
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
-  KeyChain keyChain("sqlite3", "file");
   Name hint;
   Name zone("/net");
   name::Component qType = ndns::label::NDNS_ITERATIVE_QUERY;
@@ -54,7 +51,7 @@
 
   //const Block& block = r.wireEncode();
   shared_ptr<Data> data = r.toData();
-  // keyChain.sign(*data);
+  // m_keyChain.sign(*data);
 
   ndns::Response r2;
   BOOST_CHECK_EQUAL(r2.fromData(hint, zone, *data), true);
@@ -75,7 +72,7 @@
   BOOST_CHECK_NE(r2, r4);
 
   data = r4.toData();
-  // keyChain.sign(*data);
+  // m_keyChain.sign(*data);
 
   ndns::Response r5(zone, qType);
 
diff --git a/tests/unit/daemon/db-mgr.cpp b/tests/unit/daemon/db-mgr.cpp
index 4b628b3..09fdc9f 100644
--- a/tests/unit/daemon/db-mgr.cpp
+++ b/tests/unit/daemon/db-mgr.cpp
@@ -17,14 +17,11 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "config.hpp"
 #include "daemon/db-mgr.hpp"
-#include "logger.hpp"
 
-#include "../../boost-test.hpp"
-#include <boost/filesystem.hpp>
+#include <algorithm>
 
-#include <algorithm>    // std::sort
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/daemon/name-server.cpp b/tests/unit/daemon/name-server.cpp
index 6a9be97..820dbd2 100644
--- a/tests/unit/daemon/name-server.cpp
+++ b/tests/unit/daemon/name-server.cpp
@@ -17,18 +17,13 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "daemon/db-mgr.hpp"
 #include "daemon/name-server.hpp"
+#include "daemon/db-mgr.hpp"
 #include "clients/response.hpp"
 #include "clients/query.hpp"
-#include "logger.hpp"
 
-#include "../database-test-data.hpp"
-#include "../../boost-test.hpp"
-
-#include <boost/filesystem.hpp>
-#include <ndn-cxx/face.hpp>
-#include <ndn-cxx/util/dummy-client-face.hpp>
+#include "test-common.hpp"
+#include "unit/database-test-data.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/daemon/rrset.cpp b/tests/unit/daemon/rrset.cpp
index ddcff15..c71fe0b 100644
--- a/tests/unit/daemon/rrset.cpp
+++ b/tests/unit/daemon/rrset.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,9 +18,8 @@
  */
 
 #include "daemon/rrset.hpp"
-#include "../../boost-test.hpp"
 
-#include <ndn-cxx/name.hpp>
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/daemon/zone.cpp b/tests/unit/daemon/zone.cpp
index 3d60cd2..ae13d36 100644
--- a/tests/unit/daemon/zone.cpp
+++ b/tests/unit/daemon/zone.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,9 +18,8 @@
  */
 
 #include "daemon/zone.hpp"
-#include "../../boost-test.hpp"
 
-#include <ndn-cxx/name.hpp>
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/database-test-data.cpp b/tests/unit/database-test-data.cpp
index 077d09b..90f2587 100644
--- a/tests/unit/database-test-data.cpp
+++ b/tests/unit/database-test-data.cpp
@@ -18,13 +18,11 @@
  */
 
 #include "database-test-data.hpp"
-#include "logger.hpp"
-
-#include <boost/filesystem.hpp>
 
 namespace ndn {
 namespace ndns {
 namespace tests {
+
 NDNS_LOG_INIT("TestFakeData")
 
 const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
@@ -40,7 +38,6 @@
 
 DbTestData::DbTestData()
   : m_session(TEST_DATABASE.string())
-  , m_keyChain("sqlite3", "file")
 {
   NDNS_LOG_TRACE("start creating test data");
 
diff --git a/tests/unit/database-test-data.hpp b/tests/unit/database-test-data.hpp
index 27f7aaa..104dd5d 100644
--- a/tests/unit/database-test-data.hpp
+++ b/tests/unit/database-test-data.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -25,16 +25,13 @@
 #include "clients/query.hpp"
 #include "validator.hpp"
 
-#include "../boost-test.hpp"
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/util/dummy-client-face.hpp>
-#include <boost/filesystem.hpp>
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
 namespace tests {
 
-class DbTestData
+class DbTestData : public IdentityManagementFixture
 {
 public:
   static const boost::filesystem::path TEST_DATABASE;
@@ -66,7 +63,6 @@
   Zone m_net;
   Zone m_ndnsim;
   DbMgr m_session;
-  KeyChain m_keyChain;
 };
 
 } // namespace tests
diff --git a/tests/unit/logger.cpp b/tests/unit/logger.cpp
index c2491ad..e7ba277 100644
--- a/tests/unit/logger.cpp
+++ b/tests/unit/logger.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,14 +18,11 @@
  */
 
 #include "logger.hpp"
-#include "../boost-test.hpp"
-#include "config.hpp"
-
-#include <fstream>
 
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string/classification.hpp>
-#include <boost/filesystem.hpp>
+
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/mgmt/management-tool.cpp b/tests/unit/mgmt/management-tool.cpp
index 459e9eb..b61ac86 100644
--- a/tests/unit/mgmt/management-tool.cpp
+++ b/tests/unit/mgmt/management-tool.cpp
@@ -17,29 +17,21 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "../../src/mgmt/management-tool.hpp"
-
-#include "../../boost-test.hpp"
-#include <boost/test/output_test_stream.hpp>
-using boost::test_tools::output_test_stream;
+#include "mgmt/management-tool.hpp"
 
 #include "ndns-enum.hpp"
 #include "ndns-label.hpp"
 #include "ndns-tlv.hpp"
 
-#include <vector>
-#include <iostream>
-#include <fstream>
-#include <string>
-
-#include <boost/filesystem.hpp>
 #include <boost/algorithm/string/replace.hpp>
 
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/security/validator.hpp>
 #include <ndn-cxx/util/io.hpp>
 #include <ndn-cxx/util/regex.hpp>
 
+#include "test-common.hpp"
+
+using boost::test_tools::output_test_stream;
+
 namespace ndn {
 namespace ndns {
 namespace tests {
@@ -50,7 +42,6 @@
 static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs";
 static const Name FAKE_ROOT("/fake-root/123456789");
 
-
 /**
  * @brief Recursive copy a directory using Boost Filesystem
  *
@@ -102,7 +93,7 @@
 };
 
 
-class ManagementToolFixture : public TestHome
+class ManagementToolFixture : public TestHome, public IdentityManagementFixture
 {
 public:
   class Error : public std::runtime_error
@@ -126,8 +117,7 @@
   };
 
   ManagementToolFixture()
-    : m_keyChain("sqlite3", "file")
-    , m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
+    : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
     , m_dbMgr(TEST_DATABASE.string().c_str())
 
     , rootKsk("/KEY/ksk-1416974006376/ID-CERT/%FD%00%00%01I%EA%3Bx%BD")
@@ -204,7 +194,6 @@
 
 public:
   PreviousStateCleaner cleaner; // must be first variable
-  ndn::KeyChain m_keyChain;
   ndns::ManagementTool m_tool;
   ndns::DbMgr m_dbMgr;
 
diff --git a/tests/unit/ndns-label.cpp b/tests/unit/ndns-label.cpp
index 09bb182..719d786 100644
--- a/tests/unit/ndns-label.cpp
+++ b/tests/unit/ndns-label.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,7 +18,8 @@
  */
 
 #include "ndns-label.hpp"
-#include "../boost-test.hpp"
+
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
diff --git a/tests/unit/validator.cpp b/tests/unit/validator.cpp
index b5fcfeb..ff9547f 100644
--- a/tests/unit/validator.cpp
+++ b/tests/unit/validator.cpp
@@ -18,10 +18,8 @@
  */
 
 #include "validator.hpp"
-#include "../boost-test.hpp"
-#include <ndn-cxx/util/dummy-client-face.hpp>
-#include <ndn-cxx/security/key-chain.hpp>
-#include <boost/asio.hpp>
+
+#include "test-common.hpp"
 
 namespace ndn {
 namespace ndns {
@@ -31,7 +29,7 @@
 
 BOOST_AUTO_TEST_SUITE(Validator)
 
-class Fixture
+class Fixture : public IdentityManagementFixture
 {
 public:
   Fixture()
@@ -39,15 +37,9 @@
     , m_testId2("/test02/ndn")
     , m_testId3("/test02/ndn/edu")
     , m_randomId("/test03")
-    , m_keyChain("sqlite3", "file")
     , m_version(name::Component::fromVersion(0))
     , m_face(m_keyChain, {false, true})
   {
-    m_keyChain.deleteIdentity(m_testId1);
-    m_keyChain.deleteIdentity(m_testId2);
-    m_keyChain.deleteIdentity(m_testId3);
-    m_keyChain.deleteIdentity(m_randomId);
-
     m_randomDsk = createRoot(m_randomId); // generate a root cert
 
     m_dsk1 = createRoot(m_testId1); // replace to root cert
@@ -67,10 +59,6 @@
   {
     m_face.getIoService().stop();
     m_face.shutdown();
-    m_keyChain.deleteIdentity(m_testId1);
-    m_keyChain.deleteIdentity(m_testId2);
-    m_keyChain.deleteIdentity(m_testId3);
-    m_keyChain.deleteIdentity(m_randomId);
   }
 
   const Name
@@ -146,8 +134,6 @@
 
   Name m_rootCert;
 
-  KeyChain m_keyChain;
-
   Name m_dsk1;
   Name m_dsk2;
   Name m_dsk3;
@@ -167,8 +153,10 @@
   // validator must be created after root key is saved to the target
   ndns::Validator validator(m_face, TEST_CONFIG_PATH "/" "validator.conf");
 
-  Name dataName(m_testId3);
-  dataName.append("NDNS")
+  Name dataName;
+  dataName
+    .append(m_testId3)
+    .append("NDNS")
     .append("rrLabel")
     .append("rrType")
     .appendVersion();
@@ -190,9 +178,10 @@
 
   BOOST_CHECK_EQUAL(hasValidated, true);
 
-
-  dataName = m_testId2;
-  dataName.append("KEY")
+  dataName = Name();
+  dataName
+    .append(m_testId2)
+    .append("KEY")
     .append("rrLabel")
     .append("ID-CERT")
     .appendVersion();
@@ -214,9 +203,10 @@
   // cannot pass verification due to key's owner's name is longer than data owner's
   BOOST_CHECK_EQUAL(hasValidated, true);
 
-
-  dataName = m_testId3;
-  dataName.append("KEY")
+  dataName = Name();
+  dataName
+    .append(m_testId3)
+    .append("KEY")
     .append("rrLabel")
     .append("ID-CERT")
     .appendVersion();
@@ -238,8 +228,10 @@
   // cannot pass due to self-sign cert is used
   BOOST_CHECK_EQUAL(hasValidated, true);
 
-  dataName = m_testId2;
-  dataName.append("KEY")
+  dataName = Name();
+  dataName
+    .append(m_testId2)
+    .append("KEY")
     .append("rrLabel")
     .append("ID-CERT")
     .appendVersion();