security: Construct KeyChain from configuration file.

Change-Id: Iaddac24e2c4e199fdde83fa1d0067a87e18729c4
Refs: #1532
diff --git a/tests/security/config-file-empty-home/.ndn/client.conf b/tests/security/config-file-empty-home/.ndn/client.conf
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/security/config-file-empty-home/.ndn/client.conf
diff --git a/tests/security/config-file-home/.ndn/client.conf b/tests/security/config-file-home/.ndn/client.conf
new file mode 100644
index 0000000..cc05409
--- /dev/null
+++ b/tests/security/config-file-home/.ndn/client.conf
@@ -0,0 +1,2 @@
+pib=sqlite3
+tpm=file
\ No newline at end of file
diff --git a/tests/security/config-file-malformed-home/.ndn/client.conf b/tests/security/config-file-malformed-home/.ndn/client.conf
new file mode 100644
index 0000000..4ed6728
--- /dev/null
+++ b/tests/security/config-file-malformed-home/.ndn/client.conf
@@ -0,0 +1,2 @@
+pib=lord
+tpm=ring
diff --git a/tests/security/config-file-malformed2-home/.ndn/client.conf b/tests/security/config-file-malformed2-home/.ndn/client.conf
new file mode 100644
index 0000000..3f7795d
--- /dev/null
+++ b/tests/security/config-file-malformed2-home/.ndn/client.conf
@@ -0,0 +1,2 @@
+pib=sqlite3
+tpm=just-wrong
diff --git a/tests/security/test-keychain.cpp b/tests/security/test-keychain.cpp
index 01c9502..214aa9a 100644
--- a/tests/security/test-keychain.cpp
+++ b/tests/security/test-keychain.cpp
@@ -11,18 +11,88 @@
  */
 
 #include "security/key-chain.hpp"
+#include <boost/filesystem.hpp>
 
 #include "boost-test.hpp"
 
 using namespace std;
 
 namespace ndn {
+namespace tests {
 
-BOOST_AUTO_TEST_SUITE(SecurityTestKeyChain)
-
-BOOST_AUTO_TEST_CASE (ExportIdentity)
+class KeychainConfigFileFixture
 {
-  KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
+public:
+  KeychainConfigFileFixture()
+  {
+    if (std::getenv("TEST_HOME"))
+      m_HOME = std::getenv("TEST_HOME");
+  }
+
+  ~KeychainConfigFileFixture()
+  {
+    if (!m_HOME.empty())
+      setenv("TEST_HOME", m_HOME.c_str(), 1);
+    else
+      unsetenv("TEST_HOME");
+  }
+
+protected:
+  std::string m_HOME;
+};
+
+BOOST_FIXTURE_TEST_SUITE(SecurityTestKeyChain, KeychainConfigFileFixture)
+
+BOOST_AUTO_TEST_CASE(ConstructorNormalConfig)
+{
+  using namespace boost::filesystem;
+
+  setenv("TEST_HOME", "tests/security/config-file-home", 1);
+
+  BOOST_REQUIRE_NO_THROW(KeyChain());
+
+  path pibPath(absolute(std::getenv("TEST_HOME")));
+  pibPath /= ".ndn/ndnsec-public-info.db";
+
+  boost::filesystem::remove(pibPath);
+}
+
+BOOST_AUTO_TEST_CASE(ConstructorEmptyConfig)
+{
+  using namespace boost::filesystem;
+
+  setenv("TEST_HOME", "tests/security/config-file-empty-home", 1);
+
+  BOOST_REQUIRE_NO_THROW(KeyChain());
+
+  path pibPath(absolute(std::getenv("TEST_HOME")));
+  pibPath /= ".ndn/ndnsec-public-info.db";
+
+  boost::filesystem::remove(pibPath);
+}
+
+BOOST_AUTO_TEST_CASE(ConstructorMalConfig)
+{
+  using namespace boost::filesystem;
+
+  setenv("TEST_HOME", "tests/security/config-file-malformed-home", 1);
+
+  BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
+}
+
+BOOST_AUTO_TEST_CASE(ConstructorMal2Config)
+{
+  using namespace boost::filesystem;
+
+  setenv("TEST_HOME", "tests/security/config-file-malformed2-home", 1);
+
+  BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
+}
+
+BOOST_AUTO_TEST_CASE(ExportIdentity)
+{
+  BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+  KeyChain keyChain("sqlite3", "file");
 
   Name identity("/TestKeyChain/ExportIdentity/");
   identity.appendVersion();
@@ -62,9 +132,10 @@
   BOOST_REQUIRE(keyChain.doesCertificateExist(certName) == false);
 }
 
-BOOST_AUTO_TEST_CASE (PrepareIdentityCertificate)
+BOOST_AUTO_TEST_CASE(PrepareIdentityCertificate)
 {
-  KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
+  BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+  KeyChain keyChain("sqlite3", "file");
 
   Name identity("/TestKeyChain/PrepareIdentityCertificate/");
   identity.appendVersion();
@@ -73,24 +144,25 @@
   vector<CertificateSubjectDescription> subjectDescription;
   Name lowerIdentity = identity;
   lowerIdentity.append("Lower").appendVersion();
-  Name lowerKeyName = keyChain.generateRSAKeyPair(lowerIdentity, true);
+  Name lowerKeyName = keyChain.generateRsaKeyPair(lowerIdentity, true);
   shared_ptr<IdentityCertificate> idCert
     = keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
-						  time::system_clock::now(),
-						  time::system_clock::now() + time::days(365),
-						  subjectDescription);
+                                                  time::system_clock::now(),
+                                                  time::system_clock::now() + time::days(365),
+                                                  subjectDescription);
   BOOST_CHECK(static_cast<bool>(idCert));
-  BOOST_CHECK(idCert->getName().getPrefix(5) == Name().append(identity).append("KEY").append("Lower"));
+  BOOST_CHECK(idCert->getName().getPrefix(5) ==
+              Name().append(identity).append("KEY").append("Lower"));
 
 
   Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
   anotherIdentity.appendVersion();
-  Name anotherKeyName = keyChain.generateRSAKeyPair(anotherIdentity, true);
+  Name anotherKeyName = keyChain.generateRsaKeyPair(anotherIdentity, true);
   shared_ptr<IdentityCertificate> idCert2
     = keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
-						  time::system_clock::now(),
-						  time::system_clock::now() + time::days(365),
-						  subjectDescription);
+                                                  time::system_clock::now(),
+                                                  time::system_clock::now() + time::days(365),
+                                                  subjectDescription);
   BOOST_CHECK(static_cast<bool>(idCert2));
   BOOST_CHECK(idCert2->getName().getPrefix(5) == Name().append(anotherIdentity).append("KEY"));
 
@@ -98,27 +170,27 @@
   Name wrongKeyName1;
   shared_ptr<IdentityCertificate> idCert3
     = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity,
-						  time::system_clock::now(),
-						  time::system_clock::now() + time::days(365),
-						  subjectDescription);
+                                                  time::system_clock::now(),
+                                                  time::system_clock::now() + time::days(365),
+                                                  subjectDescription);
   BOOST_CHECK(!static_cast<bool>(idCert3));
 
 
   Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate");
   shared_ptr<IdentityCertificate> idCert4
     = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity,
-						  time::system_clock::now(),
-						  time::system_clock::now() + time::days(365),
-						  subjectDescription);
+                                                  time::system_clock::now(),
+                                                  time::system_clock::now() + time::days(365),
+                                                  subjectDescription);
   BOOST_CHECK(!static_cast<bool>(idCert4));
 
 
   Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
   shared_ptr<IdentityCertificate> idCert5
     = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
-						  time::system_clock::now(),
-						  time::system_clock::now() + time::days(365),
-						  subjectDescription);
+                                                  time::system_clock::now(),
+                                                  time::system_clock::now() + time::days(365),
+                                                  subjectDescription);
   BOOST_CHECK(!static_cast<bool>(idCert5));
 
   keyChain.deleteIdentity(identity);
@@ -128,4 +200,5 @@
 
 BOOST_AUTO_TEST_SUITE_END()
 
+} // namespace tests
 } // namespace ndn
diff --git a/tests/security/test-sec-public-info-sqlite3.cpp b/tests/security/test-sec-public-info-sqlite3.cpp
index ae7135b..6215762 100644
--- a/tests/security/test-sec-public-info-sqlite3.cpp
+++ b/tests/security/test-sec-public-info-sqlite3.cpp
@@ -10,6 +10,7 @@
  * ndn-cxx library copyright, permissions, and redistribution restrictions.
  */
 
+#include "security/sec-public-info-sqlite3.hpp"
 #include "security/key-chain.hpp"
 #include "util/time.hpp"
 
@@ -20,9 +21,10 @@
 
 BOOST_AUTO_TEST_SUITE(SecurityTestSecPublicInfoSqlite3)
 
-BOOST_AUTO_TEST_CASE (Delete)
+BOOST_AUTO_TEST_CASE(Delete)
 {
-  KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
+  BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+  KeyChain keyChain("sqlite3", "file");
 
   Name identity("/TestSecPublicInfoSqlite3/Delete");
   identity.appendVersion();
@@ -32,7 +34,7 @@
 
   Name keyName1 = IdentityCertificate::certificateNameToPublicKeyName(certName1);
   Name keyName2;
-  BOOST_REQUIRE_NO_THROW(keyName2 = keyChain.generateRSAKeyPairAsDefault(identity));
+  BOOST_REQUIRE_NO_THROW(keyName2 = keyChain.generateRsaKeyPairAsDefault(identity));
 
   shared_ptr<IdentityCertificate> cert2;
   BOOST_REQUIRE_NO_THROW(cert2 = keyChain.selfSign(keyName2));
@@ -40,7 +42,7 @@
   BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert2));
 
   Name keyName3;
-  BOOST_REQUIRE_NO_THROW(keyName3 = keyChain.generateRSAKeyPairAsDefault(identity));
+  BOOST_REQUIRE_NO_THROW(keyName3 = keyChain.generateRsaKeyPairAsDefault(identity));
 
   shared_ptr<IdentityCertificate> cert3;
   BOOST_REQUIRE_NO_THROW(cert3 = keyChain.selfSign(keyName3));
diff --git a/tests/security/test-sec-tpm-file.cpp b/tests/security/test-sec-tpm-file.cpp
index 1bf895e..05f1afc 100644
--- a/tests/security/test-sec-tpm-file.cpp
+++ b/tests/security/test-sec-tpm-file.cpp
@@ -10,10 +10,12 @@
  * ndn-cxx library copyright, permissions, and redistribution restrictions.
  */
 
+#include "security/sec-tpm-file.hpp"
 #include "security/key-chain.hpp"
-#include "util/time.hpp"
 #include "security/cryptopp.hpp"
 
+#include "util/time.hpp"
+
 #include "boost-test.hpp"
 
 using namespace std;
diff --git a/tests/security/test-sec-tpm-osx.cpp b/tests/security/test-sec-tpm-osx.cpp
index e8aa9a8..0da4233 100644
--- a/tests/security/test-sec-tpm-osx.cpp
+++ b/tests/security/test-sec-tpm-osx.cpp
@@ -10,10 +10,11 @@
  * ndn-cxx library copyright, permissions, and redistribution restrictions.
  */
 
-#include "security/key-chain.hpp"
-#include "util/time.hpp"
+#include "security/sec-tpm-osx.hpp"
 #include "security/cryptopp.hpp"
 
+#include "util/time.hpp"
+
 #include "boost-test.hpp"
 
 using namespace std;
diff --git a/tests/security/test-signature-sha256.cpp b/tests/security/test-signature-sha256.cpp
index 77e5bae..611ac2e 100644
--- a/tests/security/test-signature-sha256.cpp
+++ b/tests/security/test-signature-sha256.cpp
@@ -45,8 +45,10 @@
   char content[5] = "1234";
   testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
 
-  KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keychain;
-  keychain.signWithSha256(testData);
+  BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+  KeyChain keyChain("sqlite3", "file");
+
+  keyChain.signWithSha256(testData);
 
   testData.wireEncode();
 
diff --git a/tests/security/test-signed-interest.cpp b/tests/security/test-signed-interest.cpp
index 4fd92e1..62cec61 100644
--- a/tests/security/test-signed-interest.cpp
+++ b/tests/security/test-signed-interest.cpp
@@ -23,9 +23,10 @@
 
 BOOST_AUTO_TEST_SUITE(SecurityTestSignedInterest)
 
-BOOST_AUTO_TEST_CASE (SignedInterest)
+BOOST_AUTO_TEST_CASE(SignedInterest)
 {
-  KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
+  BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+  KeyChain keyChain("sqlite3", "file");
 
   Name identityName("/TestSignedInterest/SignVerify");
   identityName.appendVersion();
@@ -95,8 +96,8 @@
     make_shared<Interest>("/TestCommandInterest/Validation/Command1");
   generator.generateWithIdentity(*commandInterest1, identity);
   validator.validate(*commandInterest1,
-  		     bind(&CommandInterestFixture::validated, this, _1),
-  		     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
+                     bind(&CommandInterestFixture::validated, this, _1),
+                     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
 
   BOOST_CHECK_EQUAL(m_validity, true);
 
@@ -115,8 +116,8 @@
 
   keyChain.signByIdentity(*commandInterest2, identity);
   validator.validate(*commandInterest2,
-  		     bind(&CommandInterestFixture::validated, this, _1),
-  		     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
+                     bind(&CommandInterestFixture::validated, this, _1),
+                     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
 
   BOOST_CHECK_EQUAL(m_validity, false);
 
@@ -129,8 +130,8 @@
     make_shared<Interest>("/TestCommandInterest/Validation/Command3");
   generator.generateWithIdentity(*commandInterest3, identity2);
   validator.validate(*commandInterest3,
-  		     bind(&CommandInterestFixture::validated, this, _1),
-  		     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
+                     bind(&CommandInterestFixture::validated, this, _1),
+                     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
 
   BOOST_CHECK_EQUAL(m_validity, false);
 
@@ -139,8 +140,8 @@
     make_shared<Interest>("/TestCommandInterest/Validation2/Command");
   generator.generateWithIdentity(*commandInterest4, identity);
   validator.validate(*commandInterest4,
-  		     bind(&CommandInterestFixture::validated, this, _1),
-  		     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
+                     bind(&CommandInterestFixture::validated, this, _1),
+                     bind(&CommandInterestFixture::validationFailed, this, _1, _2));
 
   BOOST_CHECK_EQUAL(m_validity, false);
 
diff --git a/tests/security/test-validator.cpp b/tests/security/test-validator.cpp
index 219fbc4..47ab79a 100644
--- a/tests/security/test-validator.cpp
+++ b/tests/security/test-validator.cpp
@@ -32,9 +32,10 @@
   BOOST_CHECK(false);
 }
 
-BOOST_AUTO_TEST_CASE (Null)
+BOOST_AUTO_TEST_CASE(Null)
 {
-  KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
+  BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+  KeyChain keyChain("sqlite3", "file");
 
   Name identity("/TestValidator/Null");
   identity.appendVersion();