security: Separate TPM locator modification and database resetting in PIB

Change-Id: I80c2805b6e1204b95d59a090a6a04e1ee62fb6e5
Refs: #3203
diff --git a/tests/unit-tests/security/pib/identity.t.cpp b/tests/unit-tests/security/pib/identity.t.cpp
index 5c9a229..3704d92 100644
--- a/tests/unit-tests/security/pib/identity.t.cpp
+++ b/tests/unit-tests/security/pib/identity.t.cpp
@@ -52,8 +52,7 @@
   else
     BOOST_CHECK(true);
 
-  auto pibImpl = make_shared<PibMemory>();
-  id = Identity(id1, pibImpl, true);
+  id = Identity(id1, make_shared<PibMemory>(), true);
 
   BOOST_CHECK_EQUAL(static_cast<bool>(id), true);
   BOOST_CHECK_EQUAL(!id, false);
@@ -66,9 +65,7 @@
 
 BOOST_AUTO_TEST_CASE(KeyOperations)
 {
-  auto pibImpl = make_shared<PibMemory>();
-
-  Identity identity1(id1, pibImpl, true);
+  Identity identity1(id1, make_shared<PibMemory>(), true);
 
   // Key does not exist, throw Error
   BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
diff --git a/tests/unit-tests/security/pib/key.t.cpp b/tests/unit-tests/security/pib/key.t.cpp
index 6a543f6..9391f53 100644
--- a/tests/unit-tests/security/pib/key.t.cpp
+++ b/tests/unit-tests/security/pib/key.t.cpp
@@ -78,10 +78,7 @@
   BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
 
   const auto& defaultCert = key11.getDefaultCertificate();
-  BOOST_CHECK_EQUAL_COLLECTIONS(defaultCert.wireEncode().wire(),
-                                defaultCert.wireEncode().wire() + defaultCert.wireEncode().size(),
-                                id1Key1Cert1.wireEncode().wire(),
-                                id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
+  BOOST_CHECK(defaultCert.wireEncode() == id1Key1Cert1.wireEncode());
 
   key11.removeCertificate(id1Key1Cert1.getName());
   BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
diff --git a/tests/unit-tests/security/pib/pib-impl.t.cpp b/tests/unit-tests/security/pib/pib-impl.t.cpp
index ccf54df..610d657 100644
--- a/tests/unit-tests/security/pib/pib-impl.t.cpp
+++ b/tests/unit-tests/security/pib/pib-impl.t.cpp
@@ -43,192 +43,225 @@
 
 using pib::Pib;
 
-class PibMemoryWrapper
+class PibMemoryFixture : public PibDataFixture
 {
 public:
-  PibMemory impl;
+  PibMemory pib;
 };
 
-class PibSqlite3Wrapper
+class PibSqlite3Fixture : public PibDataFixture
 {
 public:
-  PibSqlite3Wrapper()
+  PibSqlite3Fixture()
     : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
-    , impl(tmpPath.c_str())
+    , pib(tmpPath.c_str())
   {
   }
 
-  ~PibSqlite3Wrapper()
+  ~PibSqlite3Fixture()
   {
     boost::filesystem::remove_all(tmpPath);
   }
 
 public:
   boost::filesystem::path tmpPath;
-  PibSqlite3 impl;
+  PibSqlite3 pib;
 };
 
-typedef boost::mpl::list<PibMemoryWrapper,
-                         PibSqlite3Wrapper> PibImpls;
+typedef boost::mpl::list<PibMemoryFixture,
+                         PibSqlite3Fixture> PibImpls;
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture)
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
 {
-  T wrapper;
-  PibImpl& pibImpl = wrapper.impl;
+  // Basic getting and setting
+  BOOST_CHECK_NO_THROW(this->pib.getTpmLocator());
 
-  // no default setting, throw Error
-  BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
+  BOOST_CHECK_NO_THROW(this->pib.setTpmLocator("tpmLocator"));
+  BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
 
-  // check id1, which should not exist
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
+  // Add cert, and do not change TPM locator
+  this->pib.addCertificate(this->id1Key1Cert1);
+  BOOST_CHECK(this->pib.hasIdentity(this->id1));
+  BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
+  BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
 
-  // add id1, should be default
-  pibImpl.addIdentity(id1);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
-  BOOST_CHECK_NO_THROW(pibImpl.getDefaultIdentity());
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
+  // Set TPM locator to the same value, nothing should change
+  this->pib.setTpmLocator("tpmLocator");
+  BOOST_CHECK(this->pib.hasIdentity(this->id1));
+  BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
+  BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
 
-  // add id2, should not be default
-  pibImpl.addIdentity(id2);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), true);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
-
-  // set id2 explicitly as default
-  pibImpl.setDefaultIdentity(id2);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2);
-
-  // remove id2, should not have default identity
-  pibImpl.removeIdentity(id2);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), false);
-  BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
-
-  // add id2 again, should be default
-  pibImpl.addIdentity(id2);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2);
-
-  // get all identities, should contain id1 and id2
-  std::set<Name> idNames = pibImpl.getIdentities();
-  BOOST_CHECK_EQUAL(idNames.size(), 2);
-  BOOST_CHECK_EQUAL(idNames.count(id1), 1);
-  BOOST_CHECK_EQUAL(idNames.count(id2), 1);
+  // Change TPM locator (contents of PIB should not change)
+  this->pib.setTpmLocator("newTpmLocator");
+  BOOST_CHECK(this->pib.hasIdentity(this->id1));
+  BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
+  BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
 }
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture)
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T)
 {
-  T wrapper;
-  PibImpl& pibImpl = wrapper.impl;
-
   // no default setting, throw Error
-  BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
+  BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
+
+  // check id1, which should not exist
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
+
+  // add id1, should be default
+  this->pib.addIdentity(this->id1);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
+  BOOST_CHECK_NO_THROW(this->pib.getDefaultIdentity());
+  BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
+
+  // add id2, should not be default
+  this->pib.addIdentity(this->id2);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), true);
+  BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
+
+  // set id2 explicitly as default
+  this->pib.setDefaultIdentity(this->id2);
+  BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
+
+  // remove id2, should not have default identity
+  this->pib.removeIdentity(this->id2);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), false);
+  BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
+
+  // add id2 again, should be default
+  this->pib.addIdentity(this->id2);
+  BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
+
+  // get all identities, should contain id1 and id2
+  std::set<Name> idNames = this->pib.getIdentities();
+  BOOST_CHECK_EQUAL(idNames.size(), 2);
+  BOOST_CHECK_EQUAL(idNames.count(this->id1), 1);
+  BOOST_CHECK_EQUAL(idNames.count(this->id2), 1);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(ClearIdentities, T, PibImpls, T)
+{
+  this->pib.setTpmLocator("tpmLocator");
+
+  // Add id, key, and cert
+  this->pib.addCertificate(this->id1Key1Cert1);
+  BOOST_CHECK(this->pib.hasIdentity(this->id1));
+  BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
+  BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
+
+  // Clear identities
+  this->pib.clearIdentities();
+  BOOST_CHECK_EQUAL(this->pib.getIdentities().size(), 0);
+  BOOST_CHECK_EQUAL(this->pib.getKeysOfIdentity(this->id1).size(), 0);
+  BOOST_CHECK_EQUAL(this->pib.getCertificatesOfKey(this->id1Key1Name).size(), 0);
+  BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, T)
+{
+  // no default setting, throw Error
+  BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
 
   // check id1Key1, should not exist, neither should id1.
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), false);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
+  BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
 
   // add id1Key1, should be default, id1 should be added implicitly
-  pibImpl.addKey(id1, id1Key1Name, id1Key1.buf(), id1Key1.size());
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), true);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
-  const Buffer& keyBits = pibImpl.getKeyBits(id1Key1Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.begin(), keyBits.end(), id1Key1.begin(), id1Key1.end());
-  BOOST_CHECK_NO_THROW(pibImpl.getDefaultKeyOfIdentity(id1));
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name);
+  this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.buf(), this->id1Key1.size());
+  BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
+  const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
+  BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.begin(), keyBits.end(), this->id1Key1.begin(), this->id1Key1.end());
+  BOOST_CHECK_NO_THROW(this->pib.getDefaultKeyOfIdentity(this->id1));
+  BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
 
   // add id1Key2, should not be default
-  pibImpl.addKey(id1, id1Key2Name, id1Key2.buf(), id1Key2.size());
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key2Name), true);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name);
+  this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.buf(), this->id1Key2.size());
+  BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true);
+  BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
 
   // set id1Key2 explicitly as default
-  pibImpl.setDefaultKeyOfIdentity(id1, id1Key2Name);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name);
+  this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name);
+  BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
 
   // set a non-existing key as default, throw Error
-  BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, Name("/non-existing")),
+  BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")),
                     Pib::Error);
 
   // remove id1Key2, should not have default key
-  pibImpl.removeKey(id1Key2Name);
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key2Name), false);
-  BOOST_CHECK_THROW(pibImpl.getKeyBits(id1Key2Name), Pib::Error);
-  BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
+  this->pib.removeKey(this->id1Key2Name);
+  BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), false);
+  BOOST_CHECK_THROW(this->pib.getKeyBits(this->id1Key2Name), Pib::Error);
+  BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
 
   // add id1Key2 back, should be default
-  pibImpl.addKey(id1, id1Key2Name, id1Key2.buf(), id1Key2.size());
-  BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1Key2Name));
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name);
+  this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.buf(), this->id1Key2.size());
+  BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name));
+  BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
 
   // get all the keys: id1Key1 and id1Key2
-  std::set<Name> keyNames = pibImpl.getKeysOfIdentity(id1);
+  std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1);
   BOOST_CHECK_EQUAL(keyNames.size(), 2);
-  BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name), 1);
-  BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name), 1);
+  BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1);
+  BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1);
 
   // remove id1, should remove all the keys
-  pibImpl.removeIdentity(id1);
-  keyNames = pibImpl.getKeysOfIdentity(id1);
+  this->pib.removeIdentity(this->id1);
+  keyNames = this->pib.getKeysOfIdentity(this->id1);
   BOOST_CHECK_EQUAL(keyNames.size(), 0);
 }
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture)
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T)
 {
-  T wrapper;
-  PibImpl& pibImpl = wrapper.impl;
-
   // no default setting, throw Error
-  BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1Key1Name), Pib::Error);
+  BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
 
   // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), false);
+  BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
+  BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
 
   // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
-  pibImpl.addCertificate(id1Key1Cert1);
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), true);
-  const auto& cert = pibImpl.getCertificate(id1Key1Cert1.getName());
-  BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(),
-                                cert.wireEncode().wire() + cert.wireEncode().size(),
-                                id1Key1Cert1.wireEncode().wire(),
-                                id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
-  BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1Key1Name));
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert1);
+  this->pib.addCertificate(this->id1Key1Cert1);
+  BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
+  BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
+  BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
+  const auto& cert = this->pib.getCertificate(this->id1Key1Cert1.getName());
+  BOOST_CHECK(cert.wireEncode() == this->id1Key1Cert1.wireEncode());
+  BOOST_CHECK_NO_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name));
+  BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
 
   // add id1Key1Cert2, should not be default
-  pibImpl.addCertificate(id1Key1Cert2);
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), true);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert1);
+  this->pib.addCertificate(this->id1Key1Cert2);
+  BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), true);
+  BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
 
   // set id1Key1Cert2 explicitly as default
-  pibImpl.setDefaultCertificateOfKey(id1Key1Name, id1Key1Cert2.getName());
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert2);
+  this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName());
+  BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
 
   // set a non-existing cert as default, throw Error
-  BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1Key1Name, Name("/non-existing")),
+  BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")),
                     Pib::Error);
 
   // remove id1Key1Cert2, should not have default cert
-  pibImpl.removeCertificate(id1Key1Cert2.getName());
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), false);
-  BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert2.getName()), Pib::Error);
-  BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1Key1Name), Pib::Error);
+  this->pib.removeCertificate(this->id1Key1Cert2.getName());
+  BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), false);
+  BOOST_CHECK_THROW(this->pib.getCertificate(this->id1Key1Cert2.getName()), Pib::Error);
+  BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
 
   // add id1Key1Cert2, should be default
-  pibImpl.addCertificate(id1Key1Cert2);
-  BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert2);
+  this->pib.addCertificate(this->id1Key1Cert2);
+  BOOST_CHECK_NO_THROW(this->pib.getCertificate(this->id1Key1Cert1.getName()));
+  BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
 
   // get all certificates: id1Key1Cert1 and id1Key1Cert2
-  std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1Key1Name);
+  std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
   BOOST_CHECK_EQUAL(certNames.size(), 2);
-  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
-  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
+  BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1);
+  BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1);
 
   // remove id1Key1, should remove all the certs
-  pibImpl.removeKey(id1Key1Name);
-  certNames = pibImpl.getCertificatesOfKey(id1Key1Name);
+  this->pib.removeKey(this->id1Key1Name);
+  certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
   BOOST_CHECK_EQUAL(certNames.size(), 0);
 }
 
diff --git a/tests/unit-tests/security/pib/pib-memory.t.cpp b/tests/unit-tests/security/pib/pib-memory.t.cpp
index b8bfb62..89cf198 100644
--- a/tests/unit-tests/security/pib/pib-memory.t.cpp
+++ b/tests/unit-tests/security/pib/pib-memory.t.cpp
@@ -28,18 +28,11 @@
 namespace pib {
 namespace tests {
 
-// most functionalities are tested in pib-impl.t.cpp
 BOOST_AUTO_TEST_SUITE(Security)
 BOOST_AUTO_TEST_SUITE(Pib)
 BOOST_AUTO_TEST_SUITE(TestPibMemory)
 
-BOOST_AUTO_TEST_CASE(TpmLocatorManagement)
-{
-  PibMemory pibImpl;
-
-  BOOST_CHECK_EQUAL(pibImpl.getTpmLocator(), "tpm-memory:");
-  BOOST_CHECK_THROW(pibImpl.setTpmLocator(""), PibImpl::Error);
-}
+// Functionality is tested as part of pib-impl.t.cpp
 
 BOOST_AUTO_TEST_SUITE_END() // TestPibMemory
 BOOST_AUTO_TEST_SUITE_END() // Pib
diff --git a/tests/unit-tests/security/pib/pib-sqlite3.t.cpp b/tests/unit-tests/security/pib/pib-sqlite3.t.cpp
index aec65a1..966926d 100644
--- a/tests/unit-tests/security/pib/pib-sqlite3.t.cpp
+++ b/tests/unit-tests/security/pib/pib-sqlite3.t.cpp
@@ -20,12 +20,9 @@
  */
 
 #include "security/pib/pib-sqlite3.hpp"
-#include "security/pib/pib.hpp"
 
 #include "boost-test.hpp"
 
-#include <boost/filesystem.hpp>
-
 namespace ndn {
 namespace security {
 namespace pib {
@@ -33,95 +30,9 @@
 
 BOOST_AUTO_TEST_SUITE(Security)
 BOOST_AUTO_TEST_SUITE(Pib)
+BOOST_AUTO_TEST_SUITE(TestPibSqlite3)
 
-class PibSqlite3TestFixture
-{
-public:
-  PibSqlite3TestFixture()
-    : m_path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
-    , impl(m_path.c_str())
-  {
-  }
-
-  ~PibSqlite3TestFixture()
-  {
-    boost::filesystem::remove_all(m_path);
-  }
-
-private:
-  boost::filesystem::path m_path;
-
-public:
-  PibSqlite3 impl;
-};
-
-BOOST_FIXTURE_TEST_SUITE(TestPibSqlite3, PibSqlite3TestFixture)
-
-using pib::Pib;
-
-// most functionalities are tested in pib-impl.t.cpp
-
-const uint8_t SELF_SIGNED_ECDSA_CERT[] = {
-  0x06, 0xfd, 0x01, 0x5b, 0x07, 0x33, 0x08, 0x05, 0x65, 0x63, 0x64, 0x73, 0x61, 0x08, 0x03,
-  0x4b, 0x45, 0x59, 0x08, 0x11, 0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x31, 0x36, 0x35, 0x39,
-  0x34, 0x35, 0x35, 0x32, 0x38, 0x32, 0x37, 0x08, 0x07, 0x49, 0x44, 0x2d, 0x43, 0x45, 0x52,
-  0x54, 0x08, 0x09, 0xfd, 0x00, 0x00, 0x01, 0x49, 0xd3, 0x9d, 0x78, 0x00, 0x14, 0x03, 0x18,
-  0x01, 0x02, 0x15, 0xa5, 0x30, 0x81, 0xa2, 0x30, 0x22, 0x18, 0x0f, 0x32, 0x30, 0x31, 0x34,
-  0x31, 0x31, 0x32, 0x31, 0x31, 0x38, 0x32, 0x39, 0x31, 0x32, 0x5a, 0x18, 0x0f, 0x32, 0x30,
-  0x33, 0x34, 0x31, 0x31, 0x31, 0x36, 0x31, 0x38, 0x32, 0x39, 0x31, 0x32, 0x5a, 0x30, 0x21,
-  0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x29, 0x13, 0x18, 0x2f, 0x65, 0x63, 0x64, 0x73, 0x61,
-  0x2f, 0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x31, 0x36, 0x35, 0x39, 0x34, 0x35, 0x35, 0x32,
-  0x38, 0x32, 0x37, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
-  0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04,
-  0x83, 0xe5, 0x81, 0x19, 0xd9, 0xfa, 0x64, 0x40, 0xad, 0x7c, 0x93, 0xfc, 0x15, 0x90, 0x6b,
-  0x38, 0x1e, 0xc5, 0xca, 0xb1, 0x6b, 0x0b, 0x1f, 0x64, 0xbf, 0x48, 0xaa, 0xd0, 0x91, 0x5c,
-  0x24, 0xd6, 0x78, 0x40, 0xfd, 0x95, 0x5d, 0x54, 0x64, 0xe1, 0x2d, 0x0e, 0x98, 0x66, 0x1d,
-  0x7a, 0xb0, 0x61, 0x17, 0x05, 0x26, 0x13, 0x63, 0x25, 0x7c, 0xda, 0x87, 0x11, 0xc9, 0x67,
-  0xcd, 0x12, 0x05, 0xf0, 0x16, 0x2f, 0x1b, 0x01, 0x03, 0x1c, 0x2a, 0x07, 0x28, 0x08, 0x05,
-  0x65, 0x63, 0x64, 0x73, 0x61, 0x08, 0x03, 0x4b, 0x45, 0x59, 0x08, 0x11, 0x6b, 0x73, 0x6b,
-  0x2d, 0x31, 0x34, 0x31, 0x36, 0x35, 0x39, 0x34, 0x35, 0x35, 0x32, 0x38, 0x32, 0x37, 0x08,
-  0x07, 0x49, 0x44, 0x2d, 0x43, 0x45, 0x52, 0x54, 0x17, 0x47, 0x30, 0x45, 0x02, 0x21, 0x00,
-  0x9b, 0xae, 0xf4, 0x87, 0x55, 0xaa, 0x78, 0xbf, 0x00, 0xff, 0x1a, 0xbe, 0x90, 0x46, 0x6e,
-  0xdd, 0xe6, 0x3b, 0x44, 0xfd, 0x41, 0x04, 0x86, 0xcc, 0x6a, 0x8b, 0x5a, 0x25, 0xbb, 0xf1,
-  0x55, 0xcd, 0x02, 0x20, 0x0e, 0x67, 0xd8, 0x86, 0xe8, 0x7c, 0x90, 0x3c, 0x13, 0xfd, 0x36,
-  0x9c, 0xbc, 0xa1, 0xc3, 0x7c, 0xe0, 0x0c, 0x6d, 0x64, 0xac, 0xdb, 0x69, 0x99, 0xde, 0x80,
-  0x35, 0x3f, 0xf4, 0x6a, 0xcd, 0x6f
-};
-
-BOOST_AUTO_TEST_CASE(TpmTest)
-{
-  Block selfSignedCertBlock(SELF_SIGNED_ECDSA_CERT, sizeof(SELF_SIGNED_ECDSA_CERT));
-  v2::Certificate cert;
-  cert.wireDecode(selfSignedCertBlock);
-  Name identity = cert.getIdentity();
-  Name keyName = cert.getKeyName();
-  Name certName = cert.getName();
-
-  // Basic getting and setting
-  BOOST_REQUIRE_THROW(impl.getTpmLocator(), Pib::Error);
-  impl.setTpmLocator("tpmLocator");
-  BOOST_CHECK_EQUAL(impl.getTpmLocator(), "tpmLocator");
-
-  // Add cert, and do not change tpmLocator
-  impl.addCertificate(cert);
-  BOOST_CHECK(impl.hasIdentity(identity));
-  BOOST_CHECK(impl.hasKey(keyName));
-  BOOST_CHECK(impl.hasCertificate(certName));
-
-  // Set tpmLocator with the existing value, nothing should change.
-  impl.setTpmLocator("tpmLocator");
-  BOOST_CHECK(impl.hasIdentity(identity));
-  BOOST_CHECK(impl.hasKey(keyName));
-  BOOST_CHECK(impl.hasCertificate(certName));
-
-  // Change tpmLocator and ensure the pib is reset
-  impl.setTpmLocator("newTpmLocator");
-  BOOST_CHECK_EQUAL(impl.getTpmLocator(), "newTpmLocator");
-
-  BOOST_CHECK_EQUAL(impl.getIdentities().size(), 0);
-  BOOST_CHECK_EQUAL(impl.getKeysOfIdentity(identity).size(), 0);
-  BOOST_CHECK_EQUAL(impl.getCertificatesOfKey(keyName).size(), 0);
-}
+// Functionality is tested as part of pib-impl.t.cpp
 
 BOOST_AUTO_TEST_SUITE_END() // TestPibSqlite3
 BOOST_AUTO_TEST_SUITE_END() // Pib
diff --git a/tests/unit-tests/security/pib/pib.t.cpp b/tests/unit-tests/security/pib/pib.t.cpp
index 97e8568..f947fed 100644
--- a/tests/unit-tests/security/pib/pib.t.cpp
+++ b/tests/unit-tests/security/pib/pib.t.cpp
@@ -34,18 +34,17 @@
 
 BOOST_AUTO_TEST_SUITE(Security)
 BOOST_AUTO_TEST_SUITE(Pib)
-BOOST_FIXTURE_TEST_SUITE(Common, PibDataFixture)
+BOOST_FIXTURE_TEST_SUITE(TestPib, PibDataFixture)
 
 using pib::Pib;
 
 BOOST_AUTO_TEST_CASE(ValidityChecking)
 {
-  auto pibImpl = make_shared<PibMemory>();
-  Pib pib("pib-memory", "", pibImpl);
+  Pib pib("pib-memory", "", make_shared<PibMemory>());
 
   Identity id = pib.addIdentity(id1);
 
-  BOOST_CHECK_EQUAL(bool(id), true);
+  BOOST_CHECK_EQUAL(static_cast<bool>(id), true);
   BOOST_CHECK_EQUAL(!id, false);
 
   if (id)
@@ -56,7 +55,7 @@
   // key
   Key key = id.addKey(id1Key1.buf(), id1Key1.size(), id1Key1Name);
 
-  BOOST_CHECK_EQUAL(bool(key), true);
+  BOOST_CHECK_EQUAL(static_cast<bool>(key), true);
   BOOST_CHECK_EQUAL(!key, false);
 
   if (key)
@@ -65,10 +64,33 @@
     BOOST_CHECK(false);
 }
 
+BOOST_AUTO_TEST_CASE(TpmLocator)
+{
+  Pib pib("pib-memory", "", make_shared<PibMemory>());
+
+  BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:");
+  BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error);
+
+  pib.setTpmLocator("test-tpm-locator");
+  BOOST_CHECK_NO_THROW(pib.getTpmLocator());
+
+  BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
+  pib.addIdentity(id1);
+  BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
+
+  pib.setTpmLocator("another-tpm-locator");
+  BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
+
+  pib.addIdentity(id1);
+  BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
+  pib.reset();
+  BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
+  BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error);
+}
+
 BOOST_AUTO_TEST_CASE(IdentityOperations)
 {
-  auto pibImpl = make_shared<PibMemory>();
-  Pib pib("pib-memory", "", pibImpl);
+  Pib pib("pib-memory", "", make_shared<PibMemory>());
 
   BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
   Identity identity1 = pib.addIdentity(id1);
@@ -85,7 +107,7 @@
   BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
 }
 
-BOOST_AUTO_TEST_SUITE_END() // Common
+BOOST_AUTO_TEST_SUITE_END() // TestPib
 BOOST_AUTO_TEST_SUITE_END() // Pib
 BOOST_AUTO_TEST_SUITE_END() // Security