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