tests: Add test suite shared identity management fixture

This is one of the two commits for issue #1683.
This one provides an identity management fixture which will be used to replace the existing
global IdentityFixture in the second commit.

Change-Id: I2eecaa2ee73661db328760ac957474791ff59112
Refs: #1683
diff --git a/tests/unit-tests/security/identity-fixture.cpp b/tests/unit-tests/security/identity-fixture.cpp
index 31bd3a2..04589bc 100644
--- a/tests/unit-tests/security/identity-fixture.cpp
+++ b/tests/unit-tests/security/identity-fixture.cpp
@@ -24,19 +24,23 @@
 #include "boost-test.hpp"
 
 namespace ndn {
-
-// OSX KeyChain, when used on a headless server,
-// forbids usage of a private key if that key isn't created by the calling process.
-// Therefore, unit testing must create its own key pair.
+namespace security {
 
 class IdentityFixture
 {
 public:
   IdentityFixture()
   {
+    // initialize KeyChain from TEST_HOME
+    if (std::getenv("TEST_HOME"))
+      m_HOME = std::getenv("TEST_HOME");
+    setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1);
+
+    KeyChain keyChain("sqlite3", "file");
+
     // save the old default identity
     try {
-      m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
+      m_oldDefaultIdentity = keyChain.getDefaultIdentity();
       m_hasOldDefaultIdentity = true;
     }
     catch (SecPublicInfo::Error& e) {
@@ -47,28 +51,36 @@
     m_newIdentity.appendVersion();
 
     // create the new identity and self-signed certificate
-    m_keyChain.createIdentity(m_newIdentity);
+    keyChain.createIdentity(m_newIdentity);
 
     // set the new identity as default identity,
     // and the corresponding certificate becomes the default certificate
-    m_keyChain.setDefaultIdentity(m_newIdentity);
+    keyChain.setDefaultIdentity(m_newIdentity);
   }
 
   ~IdentityFixture()
   {
+    KeyChain keyChain("sqlite3", "file");
+
     // recover the old default setting
     if (m_hasOldDefaultIdentity) {
-      m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
+      keyChain.setDefaultIdentity(m_oldDefaultIdentity);
     }
 
     // remove the temporarily created identity and certificates
     // XXX This has no effect if oldDefaultIdentity doesn't exist.
     //     newIdentity would be kept as default.
-    m_keyChain.deleteIdentity(m_newIdentity);
+    keyChain.deleteIdentity(m_newIdentity);
+
+    if (!m_HOME.empty())
+      setenv("TEST_HOME", m_HOME.c_str(), 1);
+    else
+      unsetenv("TEST_HOME");
   }
 
 private:
-  KeyChain m_keyChain;
+  std::string m_HOME;
+
   bool m_hasOldDefaultIdentity;
   Name m_oldDefaultIdentity;
   Name m_newIdentity;
@@ -76,4 +88,5 @@
 
 BOOST_GLOBAL_FIXTURE(IdentityFixture)
 
+} // namespace security
 } // namespace ndn
diff --git a/tests/unit-tests/security/identity-management-fixture.cpp b/tests/unit-tests/security/identity-management-fixture.cpp
new file mode 100644
index 0000000..72c0cbe
--- /dev/null
+++ b/tests/unit-tests/security/identity-management-fixture.cpp
@@ -0,0 +1,54 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * 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.
+ *
+ * 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 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 ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+
+namespace ndn {
+namespace security {
+
+IdentityManagementFixture::IdentityManagementFixture()
+  : m_keyChain("sqlite3", "file")
+{
+}
+
+IdentityManagementFixture::~IdentityManagementFixture()
+{
+  for (std::vector<Name>::iterator it = m_identities.begin();
+       it != m_identities.end(); it++) {
+    m_keyChain.deleteIdentity(*it);
+  }
+}
+
+bool
+IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params)
+{
+  try {
+    m_keyChain.createIdentity(identity, params);
+    m_identities.push_back(identity);
+    return true;
+  }
+  catch (std::runtime_error& e) {
+    return false;
+  }
+}
+
+} // namespace security
+} // namespace ndn
diff --git a/tests/unit-tests/security/identity-management-fixture.hpp b/tests/unit-tests/security/identity-management-fixture.hpp
new file mode 100644
index 0000000..268d2ea
--- /dev/null
+++ b/tests/unit-tests/security/identity-management-fixture.hpp
@@ -0,0 +1,53 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * 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.
+ *
+ * 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 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 ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "security/key-chain.hpp"
+#include <vector>
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+
+/**
+ * @brief IdentityManagementFixture is a test suite level fixture.
+ * Test cases in the suite can use this fixture to create identities.
+ * Identities added via addIdentity method are automatically deleted
+ * during test teardown.
+ */
+class IdentityManagementFixture
+{
+public:
+  IdentityManagementFixture();
+
+  ~IdentityManagementFixture();
+
+  /// @brief add identity, return true if succeed.
+  bool
+  addIdentity(const Name& identity, const KeyParams& params = KeyChain::DEFAULT_KEY_PARAMS);
+
+protected:
+  KeyChain m_keyChain;
+  std::vector<Name> m_identities;
+};
+
+} // namespace security
+} // namespace ndn
diff --git a/tests/unit-tests/security/test-fixture.cpp b/tests/unit-tests/security/test-fixture.cpp
new file mode 100644
index 0000000..1b0226a
--- /dev/null
+++ b/tests/unit-tests/security/test-fixture.cpp
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * 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.
+ *
+ * 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 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 ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+#include "boost-test.hpp"
+
+namespace ndn {
+
+BOOST_FIXTURE_TEST_SUITE(SecurityTestIdentityManagementFixture, security::IdentityManagementFixture)
+
+BOOST_AUTO_TEST_CASE(Tmp)
+{
+  Name identity("/tmp/identity");
+  BOOST_REQUIRE(addIdentity(identity));
+  Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
+  BOOST_REQUIRE_EQUAL(certName.empty(), false);
+  BOOST_REQUIRE_NO_THROW(m_keyChain.getCertificate(certName));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/tests/unit-tests/transport/test-unix-transport.cpp b/tests/unit-tests/transport/test-unix-transport.cpp
index 6c20302..7812664 100644
--- a/tests/unit-tests/transport/test-unix-transport.cpp
+++ b/tests/unit-tests/transport/test-unix-transport.cpp
@@ -31,12 +31,12 @@
 public:
   UnixTransportFixture()
   {
-    m_HOME = std::getenv("HOME");
+    m_HOME = std::getenv("TEST_HOME");
   }
 
   ~UnixTransportFixture()
   {
-    setenv("HOME", m_HOME.c_str(), 1);
+    setenv("TEST_HOME", m_HOME.c_str(), 1);
     // std::cerr << "restoring home = " << m_HOME << std::endl;
   }
 
@@ -48,7 +48,7 @@
 
 BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameOk)
 {
-  setenv("HOME", "tests/unit-tests/transport/test-homes/ok", 1);
+  setenv("TEST_HOME", "tests/unit-tests/transport/test-homes/ok", 1);
 
   ConfigFile config;
   BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/tmp/test/nfd.sock");
@@ -56,21 +56,24 @@
 
 BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameMissingSocketMissingProtocol)
 {
-  setenv("HOME", "tests/unit-tests/transport/test-homes/missing-unix-socket-missing-protocol", 1);
+  setenv("TEST_HOME",
+         "tests/unit-tests/transport/test-homes/missing-unix-socket-missing-protocol", 1);
   ConfigFile config;
   BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/var/run/nfd.sock");
 }
 
 BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameMissingSocketNdndProtocol)
 {
-  setenv("HOME", "tests/unit-tests/transport/test-homes/missing-unix-socket-with-ndnd-protocol", 1);
+  setenv("TEST_HOME",
+         "tests/unit-tests/transport/test-homes/missing-unix-socket-with-ndnd-protocol", 1);
   ConfigFile config;
   BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/tmp/.ndnd.sock");
 }
 
 BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameMissingSocketWithProtocol)
 {
-  setenv("HOME", "tests/unit-tests/transport/test-homes/missing-unix-socket-with-protocol", 1);
+  setenv("TEST_HOME",
+         "tests/unit-tests/transport/test-homes/missing-unix-socket-with-protocol", 1);
   ConfigFile config;
   BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/var/run/nfd.sock");
 }
diff --git a/tests/unit-tests/util/test-config-file.cpp b/tests/unit-tests/util/test-config-file.cpp
index 9a0b680..06316bb 100644
--- a/tests/unit-tests/util/test-config-file.cpp
+++ b/tests/unit-tests/util/test-config-file.cpp
@@ -33,12 +33,12 @@
 public:
   ConfigFileFixture()
   {
-    m_HOME = std::getenv("HOME");
+    m_HOME = std::getenv("TEST_HOME");
   }
 
   ~ConfigFileFixture()
   {
-    setenv("HOME", m_HOME.c_str(), 1);
+    setenv("TEST_HOME", m_HOME.c_str(), 1);
     // std::cerr << "restoring home = " << m_HOME << std::endl;
   }
 
@@ -51,11 +51,11 @@
 BOOST_AUTO_TEST_CASE(TestParse)
 {
   using namespace boost::filesystem;
-  // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
+  // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
 
-  setenv("HOME", "tests/unit-tests/util/config-file-home", 1);
+  setenv("TEST_HOME", "tests/unit-tests/util/config-file-home", 1);
 
-  path homePath(absolute(std::getenv("HOME")));
+  path homePath(absolute(std::getenv("TEST_HOME")));
   homePath /= ".ndn/client.conf";
 
   try
@@ -68,7 +68,7 @@
       BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
       BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
     }
-  catch(const std::runtime_error& error)
+  catch (const std::runtime_error& error)
     {
       BOOST_FAIL("Unexpected exception: " << error.what());
     }
@@ -76,14 +76,14 @@
 
 BOOST_AUTO_TEST_CASE(EmptyPathParse)
 {
-  // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
+  // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
 
-  setenv("HOME", "tests/unit-tests/util/does/not/exist", 1);
+  setenv("TEST_HOME", "tests/unit-tests/util/does/not/exist", 1);
   try
     {
       ConfigFile config;
     }
-  catch(const std::runtime_error& error)
+  catch (const std::runtime_error& error)
     {
       BOOST_FAIL("Unexpected exception: " << error.what());
     }
@@ -92,16 +92,16 @@
 BOOST_AUTO_TEST_CASE(MalformedParse)
 {
   using namespace boost::filesystem;
-  // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
+  // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
 
-  setenv("HOME", "tests/unit-tests/util/config-file-malformed-home", 1);
+  setenv("TEST_HOME", "tests/unit-tests/util/config-file-malformed-home", 1);
 
   bool fileWasMalformed = false;
   try
     {
       ConfigFile config;
     }
-  catch(const ConfigFile::Error& error)
+  catch (const ConfigFile::Error& error)
     {
       fileWasMalformed = true;
     }