security: adjust description of PibImpl interfaces

1. Intialize default setting when there is no default setting
2. Removal method should be deep removal.

Change-Id: Id30947834a8002b6dc177ac18c6beae2600efa92
Refs: #2896, #2898
diff --git a/src/security/in-memory-pib-impl.cpp b/src/security/in-memory-pib-impl.cpp
deleted file mode 100644
index bf9d299..0000000
--- a/src/security/in-memory-pib-impl.cpp
+++ /dev/null
@@ -1,224 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 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 "in-memory-pib-impl.hpp"
-#include "pib.hpp"
-
-namespace ndn {
-namespace security {
-
-InMemoryPibImpl::InMemoryPibImpl()
-  : m_hasDefaultIdentity(false)
-{
-}
-
-void
-InMemoryPibImpl::setTpmLocator(const std::string& tpmLocator)
-{
-  throw Error("InMemoryPib/Tpm does not need a locator");
-}
-
-std::string
-InMemoryPibImpl::getTpmLocator() const
-{
-  return "tpm-memory:";
-}
-
-bool
-InMemoryPibImpl::hasIdentity(const Name& identity) const
-{
-  return (m_identities.count(identity) > 0);
-}
-
-void
-InMemoryPibImpl::addIdentity(const Name& identity)
-{
-  m_identities.insert(identity);
-}
-
-void
-InMemoryPibImpl::removeIdentity(const Name& identity)
-{
-  m_identities.erase(identity);
-  if (identity == m_defaultIdentity)
-    m_hasDefaultIdentity = false;
-}
-
-std::set<Name>
-InMemoryPibImpl::getIdentities() const
-{
-  return m_identities;
-}
-
-void
-InMemoryPibImpl::setDefaultIdentity(const Name& identityName)
-{
-  addIdentity(identityName);
-  m_defaultIdentity = identityName;
-  m_hasDefaultIdentity = true;
-}
-
-Name
-InMemoryPibImpl::getDefaultIdentity() const
-{
-  if (m_hasDefaultIdentity)
-    return m_defaultIdentity;
-
-  throw Pib::Error("No default identity");
-}
-
-bool
-InMemoryPibImpl::hasKey(const Name& identity, const name::Component& keyId) const
-{
-  return (m_keys.count(getKeyName(identity, keyId)) > 0);
-}
-
-void
-InMemoryPibImpl::addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey)
-{
-  m_keys[getKeyName(identity, keyId)] = publicKey;
-}
-
-void
-InMemoryPibImpl::removeKey(const Name& identity, const name::Component& keyId)
-{
-  m_keys.erase(getKeyName(identity, keyId));
-}
-
-PublicKey
-InMemoryPibImpl::getKeyBits(const Name& identity, const name::Component& keyId) const
-{
-  auto it = m_keys.find(getKeyName(identity, keyId));
-  if (it == m_keys.end())
-    throw Pib::Error("No default certificate");
-
-  return it->second;
-}
-
-std::set<name::Component>
-InMemoryPibImpl::getKeysOfIdentity(const Name& identity) const
-{
-  std::set<name::Component> ids;
-  for (const auto& it : m_keys) {
-    if (identity == it.first.getPrefix(-1))
-      ids.insert(it.first.get(-1));
-  }
-  return ids;
-}
-
-void
-InMemoryPibImpl::setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId)
-{
-  Name keyName = getKeyName(identity, keyId);
-
-  if (!hasKey(identity, keyId))
-    throw Pib::Error("No key");
-
-  m_defaultKey[identity] = keyName;
-}
-
-name::Component
-InMemoryPibImpl::getDefaultKeyOfIdentity(const Name& identity) const
-{
-  auto it = m_defaultKey.find(identity);
-  if (it == m_defaultKey.end())
-    throw Pib::Error("No default key");
-
-  return it->second.get(-1);
-}
-
-Name
-InMemoryPibImpl::getKeyName(const Name& identity, const name::Component& keyId) const
-{
-  Name keyName = identity;
-  keyName.append(keyId);
-  return keyName;
-}
-
-bool
-InMemoryPibImpl::hasCertificate(const Name& certName) const
-{
-  return (m_certs.count(certName) > 0);
-}
-
-void
-InMemoryPibImpl::addCertificate(const IdentityCertificate& certificate)
-{
-  m_certs[certificate.getName()] = certificate;
-}
-
-void
-InMemoryPibImpl::removeCertificate(const Name& certName)
-{
-  m_certs.erase(certName);
-}
-
-IdentityCertificate
-InMemoryPibImpl::getCertificate(const Name& certName) const
-{
-  auto it = m_certs.find(certName);
-  if (it == m_certs.end())
-    throw Pib::Error("No cert");
-
-  return it->second;
-}
-
-std::set<Name>
-InMemoryPibImpl::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const
-{
-  Name keyName = getKeyName(identity, keyId);
-
-  std::set<Name> certNames;
-  for (const auto& it : m_certs) {
-    if (it.second.getPublicKeyName() == keyName)
-      certNames.insert(it.first);
-  }
-  return certNames;
-}
-
-void
-InMemoryPibImpl::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName)
-{
-  if (!hasCertificate(certName))
-    throw Pib::Error("No cert");
-
-  Name keyName = getKeyName(identity, keyId);
-  m_defaultCert[keyName] = certName;
-}
-
-IdentityCertificate
-InMemoryPibImpl::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const
-{
-  Name keyName = getKeyName(identity, keyId);
-
-  auto it = m_defaultCert.find(keyName);
-  if (it == m_defaultCert.end())
-    throw Pib::Error("No default certificate");
-
-  auto certIt = m_certs.find(it->second);
-  if (certIt == m_certs.end())
-    throw Pib::Error("No default certificate");
-  else
-    return certIt->second;
-}
-
-} // namespace security
-} // namespace ndn
diff --git a/src/security/pib-impl.hpp b/src/security/pib-impl.hpp
index 5ece79e..fae035e 100644
--- a/src/security/pib-impl.hpp
+++ b/src/security/pib-impl.hpp
@@ -94,6 +94,7 @@
    * @brief Add an identity.
    *
    * If the identity already exists, do nothing.
+   * If no default identity has been set, set the added one as default identity.
    *
    * @param identity The name of the identity to add.
    */
@@ -104,6 +105,7 @@
    * @brief Remove an identity
    *
    * If the identity does not exist, do nothing.
+   * Remove related keys and certificates as well.
    *
    * @param identity The name of the identity to remove.
    */
@@ -151,6 +153,8 @@
    *
    * If the key already exists, do nothing.
    * If the identity does not exist, add the identity as well.
+   * If no default key of the identity has been set, set the added one as default
+   * key of the identity.
    *
    * @param identity The name of the belonged identity.
    * @param keyId The key id component.
@@ -163,6 +167,7 @@
    * @brief Remove a key.
    *
    * If the key does not exist, do nothing.
+   * Remove related certificates as well.
    *
    * @param identity The name of the belonged identity.
    * @param keyId The key id component.
@@ -227,6 +232,8 @@
    *
    * If the certificate already exists, do nothing.
    * If the key or identity do not exist, add them as well.
+   * If no default certificate of the key has been set, set the added one as
+   * default certificate of the key.
    *
    * @param certificate The certificate to add.
    */
diff --git a/src/security/pib-memory.cpp b/src/security/pib-memory.cpp
new file mode 100644
index 0000000..397b0db
--- /dev/null
+++ b/src/security/pib-memory.cpp
@@ -0,0 +1,257 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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 "pib-memory.hpp"
+#include "pib.hpp"
+
+namespace ndn {
+namespace security {
+
+PibMemory::PibMemory()
+  : m_hasDefaultIdentity(false)
+{
+}
+
+void
+PibMemory::setTpmLocator(const std::string& tpmLocator)
+{
+  throw Error("PibMemory does not need a locator");
+}
+
+std::string
+PibMemory::getTpmLocator() const
+{
+  return "tpm-memory:";
+}
+
+bool
+PibMemory::hasIdentity(const Name& identity) const
+{
+  return (m_identities.count(identity) > 0);
+}
+
+void
+PibMemory::addIdentity(const Name& identity)
+{
+  m_identities.insert(identity);
+
+  if (!m_hasDefaultIdentity) {
+    m_defaultIdentity = identity;
+    m_hasDefaultIdentity = true;
+  }
+}
+
+void
+PibMemory::removeIdentity(const Name& identity)
+{
+  m_identities.erase(identity);
+  if (identity == m_defaultIdentity)
+    m_hasDefaultIdentity = false;
+
+  auto keyIds = this->getKeysOfIdentity(identity);
+  for (const name::Component& keyId : keyIds) {
+    this->removeKey(identity, keyId);
+  }
+}
+
+std::set<Name>
+PibMemory::getIdentities() const
+{
+  return m_identities;
+}
+
+void
+PibMemory::setDefaultIdentity(const Name& identityName)
+{
+  addIdentity(identityName);
+  m_defaultIdentity = identityName;
+  m_hasDefaultIdentity = true;
+}
+
+Name
+PibMemory::getDefaultIdentity() const
+{
+  if (m_hasDefaultIdentity)
+    return m_defaultIdentity;
+
+  throw Pib::Error("No default identity");
+}
+
+bool
+PibMemory::hasKey(const Name& identity, const name::Component& keyId) const
+{
+  return (m_keys.count(getKeyName(identity, keyId)) > 0);
+}
+
+void
+PibMemory::addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey)
+{
+  this->addIdentity(identity);
+
+  Name keyName = getKeyName(identity, keyId);
+  m_keys[keyName] = publicKey;
+
+  if (m_defaultKey.find(identity) == m_defaultKey.end())
+    m_defaultKey[identity] = keyName;
+}
+
+void
+PibMemory::removeKey(const Name& identity, const name::Component& keyId)
+{
+  Name keyName = getKeyName(identity, keyId);
+  m_keys.erase(keyName);
+  m_defaultKey.erase(identity);
+
+
+  auto certNames = this->getCertificatesOfKey(identity, keyId);
+  for (const auto& certName : certNames) {
+    this->removeCertificate(certName);
+  }
+}
+
+PublicKey
+PibMemory::getKeyBits(const Name& identity, const name::Component& keyId) const
+{
+  if (!hasKey(identity, keyId))
+    throw Pib::Error("No key");
+
+  auto it = m_keys.find(getKeyName(identity, keyId));
+  return it->second;
+}
+
+std::set<name::Component>
+PibMemory::getKeysOfIdentity(const Name& identity) const
+{
+  std::set<name::Component> ids;
+  for (const auto& it : m_keys) {
+    if (identity == it.first.getPrefix(-1))
+      ids.insert(it.first.get(-1));
+  }
+  return ids;
+}
+
+void
+PibMemory::setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId)
+{
+  Name keyName = getKeyName(identity, keyId);
+
+  if (!hasKey(identity, keyId))
+    throw Pib::Error("No key");
+
+  m_defaultKey[identity] = keyName;
+}
+
+name::Component
+PibMemory::getDefaultKeyOfIdentity(const Name& identity) const
+{
+  auto it = m_defaultKey.find(identity);
+  if (it == m_defaultKey.end())
+    throw Pib::Error("No default key");
+
+  return it->second.get(-1);
+}
+
+Name
+PibMemory::getKeyName(const Name& identity, const name::Component& keyId) const
+{
+  Name keyName = identity;
+  keyName.append(keyId);
+  return keyName;
+}
+
+bool
+PibMemory::hasCertificate(const Name& certName) const
+{
+  return (m_certs.count(certName) > 0);
+}
+
+void
+PibMemory::addCertificate(const IdentityCertificate& certificate)
+{
+  this->addKey(certificate.getPublicKeyName().getPrefix(-1),
+               certificate.getPublicKeyName().get(-1),
+               certificate.getPublicKeyInfo());
+
+  m_certs[certificate.getName()] = certificate;
+
+  const Name& keyName = certificate.getPublicKeyName();
+  if (m_defaultCert.find(keyName) == m_defaultCert.end())
+    m_defaultCert[keyName] = certificate.getName();
+}
+
+void
+PibMemory::removeCertificate(const Name& certName)
+{
+  m_certs.erase(certName);
+  m_defaultCert.erase(IdentityCertificate::certificateNameToPublicKeyName(certName));
+}
+
+IdentityCertificate
+PibMemory::getCertificate(const Name& certName) const
+{
+  if (!hasCertificate(certName))
+    throw Pib::Error("No cert");
+
+  auto it = m_certs.find(certName);
+  return it->second;
+}
+
+std::set<Name>
+PibMemory::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const
+{
+  Name keyName = getKeyName(identity, keyId);
+
+  std::set<Name> certNames;
+  for (const auto& it : m_certs) {
+    if (it.second.getPublicKeyName() == keyName)
+      certNames.insert(it.first);
+  }
+  return certNames;
+}
+
+void
+PibMemory::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName)
+{
+  if (!hasCertificate(certName))
+    throw Pib::Error("No cert");
+
+  Name keyName = getKeyName(identity, keyId);
+  m_defaultCert[keyName] = certName;
+}
+
+IdentityCertificate
+PibMemory::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const
+{
+  Name keyName = getKeyName(identity, keyId);
+
+  auto it = m_defaultCert.find(keyName);
+  if (it == m_defaultCert.end())
+    throw Pib::Error("No default certificate");
+
+  auto certIt = m_certs.find(it->second);
+  if (certIt == m_certs.end())
+    throw Pib::Error("No default certificate");
+  else
+    return certIt->second;
+}
+
+} // namespace security
+} // namespace ndn
diff --git a/src/security/in-memory-pib-impl.hpp b/src/security/pib-memory.hpp
similarity index 95%
rename from src/security/in-memory-pib-impl.hpp
rename to src/security/pib-memory.hpp
index 8ef5bab..f21af00 100644
--- a/src/security/in-memory-pib-impl.hpp
+++ b/src/security/pib-memory.hpp
@@ -19,8 +19,8 @@
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-#ifndef NDN_SECURITY_IN_MEMORY_PIB_IMPL_HPP
-#define NDN_SECURITY_IN_MEMORY_PIB_IMPL_HPP
+#ifndef NDN_SECURITY_PIB_MEMORY_HPP
+#define NDN_SECURITY_PIB_MEMORY_HPP
 
 #include "pib-impl.hpp"
 
@@ -33,7 +33,7 @@
  * All the contents in Pib are stored in memory
  * and have the same lifetime as the implementation instance.
  */
-class InMemoryPibImpl : public PibImpl
+class PibMemory : public PibImpl
 {
 public:
   class Error : public PibImpl::Error
@@ -47,7 +47,7 @@
   };
 
 public:
-  InMemoryPibImpl();
+  PibMemory();
 
 public: // TpmLocator management
 
@@ -150,4 +150,4 @@
 } // namespace security
 } // namespace ndn
 
-#endif // NDN_SECURITY_SEC_PIB_MEMORY_IMPL_HPP
+#endif // NDN_SECURITY_PIB_MEMORY_HPP
diff --git a/tests/unit-tests/security/certificate-container.t.cpp b/tests/unit-tests/security/certificate-container.t.cpp
index 496d950..52a6070 100644
--- a/tests/unit-tests/security/certificate-container.t.cpp
+++ b/tests/unit-tests/security/certificate-container.t.cpp
@@ -21,7 +21,7 @@
 
 #include "security/certificate-container.hpp"
 #include "security/pib.hpp"
-#include "security/in-memory-pib-impl.hpp"
+#include "security/pib-memory.hpp"
 #include "pib-data-fixture.hpp"
 
 #include "boost-test.hpp"
@@ -34,7 +34,7 @@
 
 BOOST_FIXTURE_TEST_CASE(TestCertificateContainer, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   Pib pib("pib-memory", "", pibImpl);
 
   Identity identity1 = pib.addIdentity(id1);
diff --git a/tests/unit-tests/security/identity-container.t.cpp b/tests/unit-tests/security/identity-container.t.cpp
index cdcc024..91c4fe2 100644
--- a/tests/unit-tests/security/identity-container.t.cpp
+++ b/tests/unit-tests/security/identity-container.t.cpp
@@ -21,7 +21,7 @@
 
 #include "security/identity-container.hpp"
 #include "security/pib.hpp"
-#include "security/in-memory-pib-impl.hpp"
+#include "security/pib-memory.hpp"
 #include "pib-data-fixture.hpp"
 
 #include "boost-test.hpp"
@@ -34,7 +34,7 @@
 
 BOOST_FIXTURE_TEST_CASE(TestIdentityContainer, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   Pib pib("pib-memory", "", pibImpl);
 
   Identity identity1 = pib.addIdentity(id1);
diff --git a/tests/unit-tests/security/identity.t.cpp b/tests/unit-tests/security/identity.t.cpp
index c55c00f..d77d680 100644
--- a/tests/unit-tests/security/identity.t.cpp
+++ b/tests/unit-tests/security/identity.t.cpp
@@ -21,7 +21,7 @@
 
 #include "security/identity.hpp"
 #include "security/pib.hpp"
-#include "security/in-memory-pib-impl.hpp"
+#include "security/pib-memory.hpp"
 #include "pib-data-fixture.hpp"
 
 #include "boost-test.hpp"
@@ -45,7 +45,7 @@
   else
     BOOST_CHECK(true);
 
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   id = Identity(id1, pibImpl, true);
 
   BOOST_CHECK_EQUAL(static_cast<bool>(id), true);
@@ -59,7 +59,7 @@
 
 BOOST_FIXTURE_TEST_CASE(TestKeyOperation, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
 
   Identity identity1(id1, pibImpl, true);
 
diff --git a/tests/unit-tests/security/in-memory-pib-impl.t.cpp b/tests/unit-tests/security/in-memory-pib-impl.t.cpp
deleted file mode 100644
index f55533f..0000000
--- a/tests/unit-tests/security/in-memory-pib-impl.t.cpp
+++ /dev/null
@@ -1,141 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 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/in-memory-pib-impl.hpp"
-#include "security/pib.hpp"
-#include "pib-data-fixture.hpp"
-
-#include "boost-test.hpp"
-
-namespace ndn {
-namespace security {
-namespace tests {
-
-BOOST_AUTO_TEST_SUITE(SecurityInMemoryPibImpl)
-
-BOOST_AUTO_TEST_CASE(TpmLocatorManagement)
-{
-  InMemoryPibImpl pibImpl;
-
-  BOOST_CHECK_EQUAL(pibImpl.getTpmLocator(), "tpm-memory:");
-  BOOST_CHECK_THROW(pibImpl.setTpmLocator(""), PibImpl::Error);
-}
-
-BOOST_FIXTURE_TEST_CASE(IdentityManagement, PibDataFixture)
-{
-  InMemoryPibImpl pibImpl;
-
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
-  pibImpl.addIdentity(id1);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
-  pibImpl.removeIdentity(id1);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
-
-  BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
-  pibImpl.setDefaultIdentity(id1);
-  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
-
-  pibImpl.addIdentity(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);
-}
-
-BOOST_FIXTURE_TEST_CASE(KeyManagement, PibDataFixture)
-{
-  InMemoryPibImpl pibImpl;
-
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
-  pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
-  pibImpl.removeKey(id1, id1Key1Name.get(-1));
-  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
-
-  BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key1Name.get(-1)), Pib::Error);
-  pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
-  BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key1Name.get(-1)));
-  const PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1));
-  BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(),
-                                id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size());
-
-  pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
-  pibImpl.addKey(id2, id2Key2Name.get(-1), id2Key2);
-
-  std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1);
-  BOOST_CHECK_EQUAL(keyNames.size(), 2);
-  BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1);
-  BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1);
-
-  BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
-  pibImpl.setDefaultKeyOfIdentity(id1, id1Key1Name.get(-1));
-  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
-  BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")),
-                    Pib::Error);
-}
-
-BOOST_FIXTURE_TEST_CASE(CertificateManagement, PibDataFixture)
-{
-  InMemoryPibImpl pibImpl;
-
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
-  pibImpl.addCertificate(id1Key1Cert1);
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true);
-  pibImpl.removeCertificate(id1Key1Cert1.getName());
-  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
-
-  BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()), Pib::Error);
-  pibImpl.addCertificate(id1Key1Cert1);
-  BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
-  const IdentityCertificate& 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());
-
-  pibImpl.addCertificate(id1Key1Cert2);
-  pibImpl.addCertificate(id1Key2Cert2);
-
-  std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
-  BOOST_CHECK_EQUAL(certNames.size(), 2);
-  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
-  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
-
-  BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
-  pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert1.getName());
-  BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)));
-  const IdentityCertificate& defaultCert =
-    pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1));
-  BOOST_CHECK_EQUAL_COLLECTIONS(defaultCert.wireEncode().wire(),
-                                defaultCert.wireEncode().wire() + defaultCert.wireEncode().size(),
-                                id1Key1Cert1.wireEncode().wire(),
-                                id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
-
-  BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key2Cert1.getName()),
-                    Pib::Error);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
-
-} // namespace tests
-} // namespace security
-} // namespace ndn
diff --git a/tests/unit-tests/security/key-container.t.cpp b/tests/unit-tests/security/key-container.t.cpp
index 99f6285..016ad90 100644
--- a/tests/unit-tests/security/key-container.t.cpp
+++ b/tests/unit-tests/security/key-container.t.cpp
@@ -21,7 +21,7 @@
 
 #include "security/key-container.hpp"
 #include "security/pib.hpp"
-#include "security/in-memory-pib-impl.hpp"
+#include "security/pib-memory.hpp"
 #include "pib-data-fixture.hpp"
 
 #include "boost-test.hpp"
@@ -34,7 +34,7 @@
 
 BOOST_FIXTURE_TEST_CASE(TestKeyContainer, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   Pib pib("pib-memory", "", pibImpl);
 
   Identity identity1 = pib.addIdentity(id1);
diff --git a/tests/unit-tests/security/key.t.cpp b/tests/unit-tests/security/key.t.cpp
index 3d3c4e3..27db02c 100644
--- a/tests/unit-tests/security/key.t.cpp
+++ b/tests/unit-tests/security/key.t.cpp
@@ -21,7 +21,7 @@
 
 #include "security/key.hpp"
 #include "security/pib.hpp"
-#include "security/in-memory-pib-impl.hpp"
+#include "security/pib-memory.hpp"
 #include "pib-data-fixture.hpp"
 
 #include "boost-test.hpp"
@@ -45,7 +45,7 @@
   else
     BOOST_CHECK(true);
 
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   key = Key(id1, id1Key1Name.get(-1), id1Key1, pibImpl);
 
   BOOST_CHECK_EQUAL(static_cast<bool>(key), true);
@@ -59,7 +59,7 @@
 
 BOOST_FIXTURE_TEST_CASE(TestCertificateOperation, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
 
   Key key11(id1, id1Key1Name.get(-1), id1Key1, pibImpl);
 
diff --git a/tests/unit-tests/security/pib-impl.t.cpp b/tests/unit-tests/security/pib-impl.t.cpp
new file mode 100644
index 0000000..1f6158b
--- /dev/null
+++ b/tests/unit-tests/security/pib-impl.t.cpp
@@ -0,0 +1,201 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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/pib-memory.hpp"
+#include "security/pib.hpp"
+#include "pib-data-fixture.hpp"
+
+#include <boost/test/test_case_template.hpp>
+#include <boost/mpl/list.hpp>
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(SecurityPibImpl)
+
+typedef boost::mpl::list<PibMemory> PibImpls;
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture)
+{
+  T pibImpl;
+
+  // no default setting, throw Error
+  BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
+
+  // check id1, which should not exist
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
+
+  // 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);
+
+  // 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);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture)
+{
+  T pibImpl;
+
+  // no default setting, throw Error
+  BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
+
+  // check id1Key1, should not exist, neither should id1.
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
+
+  // add id1Key1, should be default, id1 should be added implicitly
+  pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
+  const PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(),
+                                id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size());
+  BOOST_CHECK_NO_THROW(pibImpl.getDefaultKeyOfIdentity(id1));
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
+
+  // add id1Key2, should not be default
+  pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), true);
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
+
+  // set id1Key2 explicitly as default
+  pibImpl.setDefaultKeyOfIdentity(id1, id1Key2Name.get(-1));
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1));
+
+  // set a non-existing key as default, throw Error
+  BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")),
+                    Pib::Error);
+
+  // remove id1Key2, should not have default key
+  pibImpl.removeKey(id1, id1Key2Name.get(-1));
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), false);
+  BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)), Pib::Error);
+  BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
+
+  // add id1Key2 back, should be default
+  pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
+  BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)));
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1));
+
+  // get all the keys: id1Key1 and id1Key2
+  std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1);
+  BOOST_CHECK_EQUAL(keyNames.size(), 2);
+  BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1);
+  BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1);
+
+  // remove id1, should remove all the keys
+  pibImpl.removeIdentity(id1);
+  keyNames = pibImpl.getKeysOfIdentity(id1);
+  BOOST_CHECK_EQUAL(keyNames.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture)
+{
+  T pibImpl;
+
+  // no default setting, throw Error
+  BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), 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(id1, id1Key1Name.get(-1)), 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(id1, id1Key1Name.get(-1)), true);
+  const IdentityCertificate& 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(id1, id1Key1Name.get(-1)));
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1);
+
+  // add id1Key1Cert2, should not be default
+  pibImpl.addCertificate(id1Key1Cert2);
+  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), true);
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1);
+
+  // set id1Key1Cert2 explicitly as default
+  pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert2.getName());
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2);
+
+  // set a non-existing cert as default, throw Error
+  BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), 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(id1, id1Key1Name.get(-1)), Pib::Error);
+
+  // add id1Key1Cert2, should be default
+  pibImpl.addCertificate(id1Key1Cert2);
+  BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2);
+
+  // get all certificates: id1Key1Cert1 and id1Key1Cert2
+  std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL(certNames.size(), 2);
+  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
+  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
+
+  // remove id1Key1, should remove all the certs
+  pibImpl.removeKey(id1, id1Key1Name.get(-1));
+  certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL(certNames.size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace security
+} // namespace ndn
diff --git a/tests/unit-tests/security/pib-memory.t.cpp b/tests/unit-tests/security/pib-memory.t.cpp
new file mode 100644
index 0000000..e0a7e59
--- /dev/null
+++ b/tests/unit-tests/security/pib-memory.t.cpp
@@ -0,0 +1,45 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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/pib-memory.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+// most functionalities are tested in pib-impl.t.cpp
+BOOST_AUTO_TEST_SUITE(SecurityPibMemory)
+
+BOOST_AUTO_TEST_CASE(TpmLocatorManagement)
+{
+  PibMemory pibImpl;
+
+  BOOST_CHECK_EQUAL(pibImpl.getTpmLocator(), "tpm-memory:");
+  BOOST_CHECK_THROW(pibImpl.setTpmLocator(""), PibImpl::Error);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace security
+} // namespace ndn
diff --git a/tests/unit-tests/security/pib.t.cpp b/tests/unit-tests/security/pib.t.cpp
index 5210df6..bc05ca9 100644
--- a/tests/unit-tests/security/pib.t.cpp
+++ b/tests/unit-tests/security/pib.t.cpp
@@ -20,7 +20,7 @@
  */
 
 #include "security/pib.hpp"
-#include "security/in-memory-pib-impl.hpp"
+#include "security/pib-memory.hpp"
 #include "pib-data-fixture.hpp"
 
 #include "boost-test.hpp"
@@ -33,7 +33,7 @@
 
 BOOST_FIXTURE_TEST_CASE(ValidityChecking, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   Pib pib("pib-memory", "", pibImpl);
 
   Identity id = pib.addIdentity(id1);
@@ -60,7 +60,7 @@
 
 BOOST_FIXTURE_TEST_CASE(TestIdentityOperation, PibDataFixture)
 {
-  auto pibImpl = make_shared<InMemoryPibImpl>();
+  auto pibImpl = make_shared<PibMemory>();
   Pib pib("pib-memory", "", pibImpl);
 
   BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);