Merge "tests: update test files with latest ndn-cxx and fix a bug in database-fixture"
diff --git a/tests/database-fixture.hpp b/tests/database-fixture.hpp
index ffe00b0..c7e04bb 100644
--- a/tests/database-fixture.hpp
+++ b/tests/database-fixture.hpp
@@ -1,34 +1,27 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2019, Regents of the University of California,
- *                          Arizona Board of Regents,
- *                          Colorado State University,
- *                          University Pierre & Marie Curie, Sorbonne University,
- *                          Washington University in St. Louis,
- *                          Beijing Institute of Technology,
- *                          The University of Memphis.
+/*
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
- * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
- * is a part of ndncert, a certificate management system based on NDN.
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
- * ndncert 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.
+ * ndn-cxx library 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.
  *
- * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * ndn-cxx library 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.
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  *
- * You should have received copies of the GNU General Public License along with
- * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
  *
- * See AUTHORS.md for complete list of ndncert authors and contributors.
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
 #ifndef NDNCERT_TESTS_DATABASE_FIXTURE_HPP
 #define NDNCERT_TESTS_DATABASE_FIXTURE_HPP
 
-#include "test-common.hpp"
 #include "identity-management-fixture.hpp"
 #include <boost/filesystem.hpp>
 
@@ -41,7 +34,14 @@
 public:
   DatabaseFixture()
   {
-    dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
+    auto parentDir = boost::filesystem::path(getenv("TEST_HOME"));
+    if (!boost::filesystem::exists(parentDir)) {
+      boost::filesystem::create_directory(parentDir);
+    }
+    dbDir = parentDir / ".ndncert";
+    if (!boost::filesystem::exists(dbDir)) {
+      boost::filesystem::create_directory(dbDir);
+    }
   }
 
   ~DatabaseFixture()
diff --git a/tests/identity-management-fixture.cpp b/tests/identity-management-fixture.cpp
index a152f73..e26b30c 100644
--- a/tests/identity-management-fixture.cpp
+++ b/tests/identity-management-fixture.cpp
@@ -1,32 +1,26 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2019, Regents of the University of California,
- *                          Arizona Board of Regents,
- *                          Colorado State University,
- *                          University Pierre & Marie Curie, Sorbonne University,
- *                          Washington University in St. Louis,
- *                          Beijing Institute of Technology,
- *                          The University of Memphis.
+/*
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
- * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
- * is a part of ndncert, a certificate management system based on NDN.
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
- * ndncert 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.
+ * ndn-cxx library 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.
  *
- * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * ndn-cxx library 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.
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  *
- * You should have received copies of the GNU General Public License along with
- * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
  *
- * See AUTHORS.md for complete list of ndncert authors and contributors.
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
 #include "identity-management-fixture.hpp"
-
+#include <ndn-cxx/security/v2/additional-description.hpp>
 #include <ndn-cxx/util/io.hpp>
 #include <boost/filesystem.hpp>
 
@@ -134,4 +128,4 @@
 
 } // namespace tests
 } // namespace ndncert
-} // namespace ndn
+} // namespace ndn
\ No newline at end of file
diff --git a/tests/identity-management-fixture.hpp b/tests/identity-management-fixture.hpp
index d287fc9..c83bf30 100644
--- a/tests/identity-management-fixture.hpp
+++ b/tests/identity-management-fixture.hpp
@@ -1,38 +1,33 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2019, Regents of the University of California,
- *                          Arizona Board of Regents,
- *                          Colorado State University,
- *                          University Pierre & Marie Curie, Sorbonne University,
- *                          Washington University in St. Louis,
- *                          Beijing Institute of Technology,
- *                          The University of Memphis.
+/*
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
- * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
- * is a part of ndncert, a certificate management system based on NDN.
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
- * ndncert 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.
+ * ndn-cxx library 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.
  *
- * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * ndn-cxx library 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.
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  *
- * You should have received copies of the GNU General Public License along with
- * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
  *
- * See AUTHORS.md for complete list of ndncert authors and contributors.
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-#ifndef NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
-#define NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+#ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+#define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
 
-#include "test-common.hpp"
 #include "test-home-fixture.hpp"
+#include "unit-test-time-fixture.hpp"
+#include "boost-test.hpp"
 #include <ndn-cxx/security/v2/key-chain.hpp>
-#include <ndn-cxx/security/v2/additional-description.hpp>
 #include <ndn-cxx/security/signing-helpers.hpp>
+#include <vector>
 
 namespace ndn {
 namespace ndncert {
@@ -51,7 +46,6 @@
   std::set<std::string> m_certFiles;
 };
 
-
 /**
  * @brief A test suite level fixture to help with identity management
  *
@@ -103,9 +97,6 @@
   KeyChain m_keyChain;
 };
 
-/** \brief convenience base class for inheriting from both UnitTestTimeFixture
- *         and IdentityManagementV2Fixture
- */
 class IdentityManagementTimeFixture : public UnitTestTimeFixture
                                     , public IdentityManagementFixture
 {
@@ -115,4 +106,4 @@
 } // namespace ndncert
 } // namespace ndn
 
-#endif // NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
diff --git a/tests/test-home-fixture.hpp b/tests/test-home-fixture.hpp
index 775e38a..858cf23 100644
--- a/tests/test-home-fixture.hpp
+++ b/tests/test-home-fixture.hpp
@@ -22,12 +22,10 @@
 #ifndef NDN_TESTS_TEST_HOME_FIXTURE_HPP
 #define NDN_TESTS_TEST_HOME_FIXTURE_HPP
 
-#include "ndn-cxx/security/v2/key-chain.hpp"
-
+#include <ndn-cxx/security/v2/key-chain.hpp>
 #include <cstdlib>
 #include <fstream>
 #include <initializer_list>
-
 #include <boost/algorithm/string.hpp>
 #include <boost/filesystem.hpp>
 
@@ -36,69 +34,16 @@
 namespace tests {
 
 /**
- * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
- *
- * Note that the specified PATH will be removed after fixture is destroyed.
- * **Do not specify non-temporary paths.**
+ * @brief TestHomeFixture to set TEST_HOME variable and allow config file creation
  */
 template<class Path>
-class PibDirFixture
-{
-public:
-  PibDirFixture()
-    : m_pibDir(Path().PATH)
-  {
-    if (std::getenv("NDN_CLIENT_PIB") != nullptr) {
-      m_oldPib = std::getenv("NDN_CLIENT_PIB");
-    }
-    if (std::getenv("NDN_CLIENT_TPM") != nullptr) {
-      m_oldTpm = std::getenv("NDN_CLIENT_TPM");
-    }
-
-    /// @todo Consider change to an in-memory PIB/TPM
-    setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
-    setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
-  }
-
-  ~PibDirFixture()
-  {
-    if (!m_oldPib.empty()) {
-      setenv("NDN_CLIENT_PIB", m_oldPib.data(), true);
-    }
-    else {
-      unsetenv("NDN_CLIENT_PIB");
-    }
-
-    if (!m_oldTpm.empty()) {
-      setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true);
-    }
-    else {
-      unsetenv("NDN_CLIENT_TPM");
-    }
-
-    boost::filesystem::remove_all(m_pibDir);
-    // const_cast<std::string&>(security::v2::KeyChain::getDefaultPibLocator()).clear();
-    // const_cast<std::string&>(security::v2::KeyChain::getDefaultTpmLocator()).clear();
-  }
-
-protected:
-  const std::string m_pibDir;
-
-private:
-  std::string m_oldPib;
-  std::string m_oldTpm;
-};
-
-/**
- * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation
- */
-template<class Path>
-class TestHomeFixture : public PibDirFixture<Path>
+class TestHomeFixture
 {
 public:
   TestHomeFixture()
+    : m_testHomeDir(Path().PATH)
   {
-    setenv("TEST_HOME", this->m_pibDir.c_str(), true);
+    setenv("TEST_HOME", m_testHomeDir.c_str(), true);
   }
 
   ~TestHomeFixture()
@@ -109,13 +54,16 @@
   void
   createClientConf(std::initializer_list<std::string> lines) const
   {
-    boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn");
-    std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str());
+    boost::filesystem::create_directories(boost::filesystem::path(m_testHomeDir) / ".ndn");
+    std::ofstream of((boost::filesystem::path(m_testHomeDir) / ".ndn" / "client.conf").c_str());
     for (auto line : lines) {
-      boost::replace_all(line, "%PATH%", this->m_pibDir);
+      boost::replace_all(line, "%PATH%", m_testHomeDir);
       of << line << std::endl;
     }
   }
+
+protected:
+  std::string m_testHomeDir;
 };
 
 struct DefaultPibDir
diff --git a/tests/test-common.hpp b/tests/unit-test-time-fixture.hpp
similarity index 63%
rename from tests/test-common.hpp
rename to tests/unit-test-time-fixture.hpp
index 11d7feb..f934a12 100644
--- a/tests/test-common.hpp
+++ b/tests/unit-test-time-fixture.hpp
@@ -1,36 +1,29 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2019, Regents of the University of California,
- *                          Arizona Board of Regents,
- *                          Colorado State University,
- *                          University Pierre & Marie Curie, Sorbonne University,
- *                          Washington University in St. Louis,
- *                          Beijing Institute of Technology,
- *                          The University of Memphis.
+/*
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
- * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
- * is a part of ndncert, a certificate management system based on NDN.
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
- * ndncert 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.
+ * ndn-cxx library 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.
  *
- * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * ndn-cxx library 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.
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  *
- * You should have received copies of the GNU General Public License along with
- * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
  *
- * See AUTHORS.md for complete list of ndncert authors and contributors.
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-#ifndef NDNCERT_TESTS_TEST_COMMON_HPP
-#define NDNCERT_TESTS_TEST_COMMON_HPP
+#ifndef NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
+#define NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
 
-#include "boost-test.hpp"
-#include <boost/asio/io_service.hpp>
 #include <ndn-cxx/util/time-unit-test-clock.hpp>
+#include <boost/asio/io_service.hpp>
 
 namespace ndn {
 namespace ndncert {
@@ -95,20 +88,20 @@
         remaining = time::nanoseconds::zero();
       }
 
-      if (m_io.stopped())
-        m_io.reset();
-      m_io.poll();
+      if (io.stopped())
+        io.reset();
+      io.poll();
     }
   }
 
 public:
   shared_ptr<time::UnitTestSteadyClock> steadyClock;
   shared_ptr<time::UnitTestSystemClock> systemClock;
-  boost::asio::io_service m_io;
+  boost::asio::io_service io;
 };
 
 } // namespace tests
 } // namespace ndncert
 } // namespace ndn
 
-#endif // NDNCERT_TESTS_TEST_COMMON_HPP
+#endif // NDN_TESTS_UNIT_UNIT_TEST_TIME_FIXTURE_HPP
diff --git a/tests/unit-tests/ca-module.t.cpp b/tests/unit-tests/ca-module.t.cpp
index a3fa150..b1d8777 100644
--- a/tests/unit-tests/ca-module.t.cpp
+++ b/tests/unit-tests/ca-module.t.cpp
@@ -38,8 +38,8 @@
 
 BOOST_AUTO_TEST_CASE(Initialization)
 {
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   BOOST_CHECK_EQUAL(ca.getCaConf().m_caName.toUri(), "/ndn");
 
   auto identity = addIdentity(Name("/ndn/site2"));
@@ -59,8 +59,8 @@
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   ca.setProbeHandler([&] (const JsonSection& probeInfo) {
       return "example";
     });
@@ -91,8 +91,8 @@
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   ca.setProbeHandler([&] (const JsonSection& probeInfo) {
       return "example";
     });
@@ -124,8 +124,8 @@
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   advanceClocks(time::milliseconds(20), 60);
 
   Interest interest("/ndn/CA/_PROBE");
@@ -153,8 +153,8 @@
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   advanceClocks(time::milliseconds(20), 60);
 
   ClientModule client(m_keyChain);
@@ -193,8 +193,8 @@
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   ca.m_config.m_probe = "email";
   advanceClocks(time::milliseconds(20), 60);
 
@@ -228,8 +228,8 @@
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  util::DummyClientFace face(m_io, {true, true});
-  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  util::DummyClientFace face(io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
   advanceClocks(time::milliseconds(20), 60);
 
   // generate NEW Interest
diff --git a/tests/unit-tests/ca-sqlite.t.cpp b/tests/unit-tests/ca-sqlite.t.cpp
index e1c8495..16dbfe8 100644
--- a/tests/unit-tests/ca-sqlite.t.cpp
+++ b/tests/unit-tests/ca-sqlite.t.cpp
@@ -20,6 +20,7 @@
 
 #include "ca-detail/ca-sqlite.hpp"
 #include "database-fixture.hpp"
+#include <iostream>
 
 namespace ndn {
 namespace ndncert {
diff --git a/tests/unit-tests/crypto-helper.t.cpp b/tests/unit-tests/crypto-helper.t.cpp
index 4576942..385dbbd 100644
--- a/tests/unit-tests/crypto-helper.t.cpp
+++ b/tests/unit-tests/crypto-helper.t.cpp
@@ -19,7 +19,7 @@
  */
 
 #include "crypto-support/crypto-helper.hpp"
-#include "test-common.hpp"
+#include "identity-management-fixture.hpp"
 #include <iostream>
 
 namespace ndn {
diff --git a/tests/unit-tests/dummy-test.t.cpp b/tests/unit-tests/dummy-test.t.cpp
index 70d3f50..0992583 100644
--- a/tests/unit-tests/dummy-test.t.cpp
+++ b/tests/unit-tests/dummy-test.t.cpp
@@ -18,7 +18,8 @@
  * See AUTHORS.md for complete list of ndncert authors and contributors.
  */
 
-#include "test-common.hpp"
+#include "identity-management-fixture.hpp"
+#include "boost-test.hpp"
 
 namespace ndn {
 namespace ndncert {
diff --git a/tests/unit-tests/enc-tlv.t.cpp b/tests/unit-tests/enc-tlv.t.cpp
index 9ba62e7..10e59d9 100644
--- a/tests/unit-tests/enc-tlv.t.cpp
+++ b/tests/unit-tests/enc-tlv.t.cpp
@@ -20,7 +20,7 @@
 
 #include "crypto-support/enc-tlv.hpp"
 #include "crypto-support/crypto-helper.hpp"
-#include "test-common.hpp"
+#include "boost-test.hpp"
 
 namespace ndn {
 namespace ndncert {