security: modernize Identity and IdentityContainer; add logging

Change-Id: I1c8645082456de4cc8012ac97a1b95ab1ddbf835
diff --git a/tests/unit/security/pib/identity-container.t.cpp b/tests/unit/security/pib/identity-container.t.cpp
index fb5e9cc..77503a0 100644
--- a/tests/unit/security/pib/identity-container.t.cpp
+++ b/tests/unit/security/pib/identity-container.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2019 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -21,7 +21,6 @@
 
 #include "ndn-cxx/security/pib/identity-container.hpp"
 #include "ndn-cxx/security/pib/impl/pib-memory.hpp"
-#include "ndn-cxx/security/pib/pib.hpp"
 
 #include "tests/boost-test.hpp"
 #include "tests/unit/security/pib/pib-data-fixture.hpp"
@@ -37,78 +36,75 @@
 BOOST_AUTO_TEST_SUITE(Pib)
 BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture)
 
-using pib::Pib;
-
-BOOST_AUTO_TEST_CASE(Basic)
+BOOST_AUTO_TEST_CASE(AddGetRemove)
 {
   auto pibImpl = make_shared<PibMemory>();
 
-  // start with an empty container
-  IdentityContainer container(pibImpl);
-  BOOST_CHECK_EQUAL(container.size(), 0);
-  BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 0);
+  {
+    // start with an empty container
+    IdentityContainer container(pibImpl);
+    BOOST_CHECK_EQUAL(container.size(), 0);
+    BOOST_CHECK_EQUAL(container.m_identities.size(), 0);
 
-  // add the first identity
-  Identity identity11 = container.add(id1);
-  BOOST_CHECK_EQUAL(identity11.getName(), id1);
-  BOOST_CHECK_EQUAL(container.size(), 1);
-  BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 1);
-  BOOST_CHECK(container.find(id1) != container.end());
+    // add the first identity
+    Identity identity11 = container.add(id1);
+    BOOST_CHECK_EQUAL(identity11.getName(), id1);
+    BOOST_CHECK_EQUAL(container.size(), 1);
+    BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
+    BOOST_CHECK(container.find(id1) != container.end());
 
-  // add the same identity again
-  Identity identity12 = container.add(id1);
-  BOOST_CHECK_EQUAL(identity12.getName(), id1);
-  BOOST_CHECK_EQUAL(container.size(), 1);
-  BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 1);
-  BOOST_CHECK(container.find(id1) != container.end());
+    // add the same identity again
+    Identity identity12 = container.add(id1);
+    BOOST_CHECK_EQUAL(identity12.getName(), id1);
+    BOOST_CHECK_EQUAL(container.size(), 1);
+    BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
+    BOOST_CHECK(container.find(id1) != container.end());
 
-  // add the second identity
-  Identity identity21 = container.add(id2);
-  BOOST_CHECK_EQUAL(identity21.getName(), id2);
-  BOOST_CHECK_EQUAL(container.size(), 2);
-  BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 2);
-  BOOST_CHECK(container.find(id1) != container.end());
-  BOOST_CHECK(container.find(id2) != container.end());
+    // add the second identity
+    Identity identity21 = container.add(id2);
+    BOOST_CHECK_EQUAL(identity21.getName(), id2);
+    BOOST_CHECK_EQUAL(container.size(), 2);
+    BOOST_CHECK_EQUAL(container.m_identities.size(), 2);
+    BOOST_CHECK(container.find(id1) != container.end());
+    BOOST_CHECK(container.find(id2) != container.end());
 
-  // get identities
-  BOOST_REQUIRE_NO_THROW(container.get(id1));
-  BOOST_REQUIRE_NO_THROW(container.get(id2));
-  BOOST_CHECK_THROW(container.get(Name("/non-existing")), Pib::Error);
+    // check identities
+    Identity identity1 = container.get(id1);
+    Identity identity2 = container.get(id2);
+    BOOST_CHECK_EQUAL(identity1.getName(), id1);
+    BOOST_CHECK_EQUAL(identity2.getName(), id2);
+    BOOST_CHECK_THROW(container.get(Name("/non-existing")), pib::Pib::Error);
+  }
 
-  // check identity
-  Identity identity1 = container.get(id1);
-  Identity identity2 = container.get(id2);
-  BOOST_CHECK_EQUAL(identity1.getName(), id1);
-  BOOST_CHECK_EQUAL(identity2.getName(), id2);
+  {
+    // create a container from an existing (non-empty) PibImpl
+    // names are loaded immediately but identity cache should initially be empty
+    IdentityContainer container2(pibImpl);
+    BOOST_CHECK_EQUAL(container2.size(), 2);
+    BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
 
-  // create another container from the same PibImpl
-  // cache should be empty
-  IdentityContainer container2(pibImpl);
-  BOOST_CHECK_EQUAL(container2.size(), 2);
-  BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 0);
+    // fetching the identities should populate the cache
+    BOOST_CHECK_EQUAL(container2.get(id1).getName(), id1);
+    BOOST_CHECK_EQUAL(container2.size(), 2);
+    BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
 
-  // get key, cache should be filled
-  BOOST_REQUIRE_NO_THROW(container2.get(id1));
-  BOOST_CHECK_EQUAL(container2.size(), 2);
-  BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 1);
+    BOOST_CHECK_EQUAL(container2.get(id2).getName(), id2);
+    BOOST_CHECK_EQUAL(container2.size(), 2);
+    BOOST_CHECK_EQUAL(container2.m_identities.size(), 2);
 
-  BOOST_REQUIRE_NO_THROW(container2.get(id2));
-  BOOST_CHECK_EQUAL(container2.size(), 2);
-  BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 2);
+    // remove an identity
+    container2.remove(id1);
+    BOOST_CHECK_EQUAL(container2.size(), 1);
+    BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
+    BOOST_CHECK(container2.find(id1) == container2.end());
+    BOOST_CHECK(container2.find(id2) != container2.end());
 
-  // remove a key
-  container2.remove(id1);
-  BOOST_CHECK_EQUAL(container2.size(), 1);
-  BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 1);
-  BOOST_CHECK(container2.find(id1) == container2.end());
-  BOOST_CHECK(container2.find(id2) != container2.end());
-
-  // remove another key
-  container2.remove(id2);
-  BOOST_CHECK_EQUAL(container2.size(), 0);
-  BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 0);
-  BOOST_CHECK(container2.find(id2) == container2.end());
-
+    // remove another identity
+    container2.remove(id2);
+    BOOST_CHECK_EQUAL(container2.size(), 0);
+    BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
+    BOOST_CHECK(container2.find(id2) == container2.end());
+  }
 }
 
 BOOST_AUTO_TEST_CASE(Iterator)
@@ -118,12 +114,10 @@
   container.add(id1);
   container.add(id2);
 
-  std::set<Name> idNames;
-  idNames.insert(id1);
-  idNames.insert(id2);
+  const std::set<Name> idNames{id1, id2};
 
   IdentityContainer::const_iterator it = container.begin();
-  std::set<Name>::const_iterator testIt = idNames.begin();
+  auto testIt = idNames.begin();
   BOOST_CHECK_EQUAL((*it).getName(), *testIt);
   it++;
   testIt++;
@@ -132,7 +126,8 @@
   testIt++;
   BOOST_CHECK(it == container.end());
 
-  size_t count = 0;
+  // test range-based for
+  int count = 0;
   testIt = idNames.begin();
   for (const auto& identity : container) {
     BOOST_CHECK_EQUAL(identity.getName(), *testIt);
diff --git a/tests/unit/security/pib/identity.t.cpp b/tests/unit/security/pib/identity.t.cpp
index 0198816..97f2369 100644
--- a/tests/unit/security/pib/identity.t.cpp
+++ b/tests/unit/security/pib/identity.t.cpp
@@ -41,34 +41,41 @@
 BOOST_AUTO_TEST_CASE(ValidityChecking)
 {
   Identity id;
-  BOOST_CHECK(!id);
-  BOOST_CHECK_EQUAL(static_cast<bool>(id), false);
+  BOOST_TEST(!id);
+  BOOST_TEST(id == Identity());
 
-  auto identityImpl = make_shared<detail::IdentityImpl>(id1, make_shared<PibMemory>(), true);
-  id = Identity(identityImpl);
-  BOOST_CHECK(id);
-  BOOST_CHECK_EQUAL(!id, false);
+  auto impl = std::make_shared<detail::IdentityImpl>(id1, std::make_shared<PibMemory>(), true);
+  id = Identity(impl);
+  BOOST_TEST(id);
+  BOOST_TEST(id != Identity());
+
+  impl.reset();
+  BOOST_TEST(!id);
 }
 
-// pib::Identity is a wrapper of pib::detail::IdentityImpl.  Since the functionalities of
-// IdentityImpl have already been tested in detail/identity-impl.t.cpp, we only test the shared
+// pib::Identity is a wrapper of pib::detail::IdentityImpl. Since the functionality
+// of IdentityImpl is already tested in identity-impl.t.cpp, we only test the shared
 // property of pib::Identity in this test case.
 BOOST_AUTO_TEST_CASE(SharedImpl)
 {
-  auto identityImpl = make_shared<detail::IdentityImpl>(id1, make_shared<pib::PibMemory>(), true);
-  Identity identity1(identityImpl);
-  Identity identity2(identityImpl);
-  BOOST_CHECK_EQUAL(identity1, identity2);
-  BOOST_CHECK_NE(identity1, Identity());
-  BOOST_CHECK_EQUAL(Identity(), Identity());
+  auto impl = std::make_shared<detail::IdentityImpl>(id1, std::make_shared<pib::PibMemory>(), true);
+  Identity identity1(impl);
+  Identity identity2(impl);
 
+  BOOST_TEST(identity1 == identity2);
+  BOOST_TEST(identity1 != Identity());
+  BOOST_TEST(Identity() != identity2);
+  BOOST_TEST(Identity() == Identity());
+
+  BOOST_CHECK_THROW(identity2.getKey(id1Key1Name), pib::Pib::Error);
   identity1.addKey(id1Key1, id1Key1Name);
-  BOOST_CHECK_NO_THROW(identity2.getKey(id1Key1Name));
+  BOOST_TEST(identity2.getKey(id1Key1Name));
+
   identity2.removeKey(id1Key1Name);
   BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), pib::Pib::Error);
 
   identity1.setDefaultKey(id1Key1, id1Key1Name);
-  BOOST_CHECK_NO_THROW(identity2.getDefaultKey());
+  BOOST_TEST(identity2.getDefaultKey().getName() == id1Key1Name);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestIdentity
diff --git a/tests/unit/security/pib/impl/identity-impl.t.cpp b/tests/unit/security/pib/impl/identity-impl.t.cpp
index 1b4527f..390cee4 100644
--- a/tests/unit/security/pib/impl/identity-impl.t.cpp
+++ b/tests/unit/security/pib/impl/identity-impl.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -36,19 +36,17 @@
 BOOST_AUTO_TEST_SUITE(Pib)
 BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, ndn::security::tests::PibDataFixture)
 
-using security::Pib;
+using pib::Pib;
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
-  auto pibImpl = make_shared<pib::PibMemory>();
-  IdentityImpl identity1(id1, pibImpl, true);
-
+  IdentityImpl identity1(id1, std::make_shared<pib::PibMemory>(), true);
   BOOST_CHECK_EQUAL(identity1.getName(), id1);
 }
 
-BOOST_AUTO_TEST_CASE(KeyOperation)
+BOOST_AUTO_TEST_CASE(KeyOperations)
 {
-  auto pibImpl = make_shared<pib::PibMemory>();
+  auto pibImpl = std::make_shared<pib::PibMemory>();
   IdentityImpl identity1(id1, pibImpl, true);
   BOOST_CHECK_NO_THROW(IdentityImpl(id1, pibImpl, false));
 
@@ -60,18 +58,18 @@
   // get default key, throw Pib::Error
   BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
   // set non-existing key as default key, throw Pib::Error
-  BOOST_REQUIRE_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
+  BOOST_CHECK_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
 
   // add key
   identity1.addKey(id1Key1, id1Key1Name);
-  BOOST_CHECK_NO_THROW(identity1.getKey(id1Key1Name));
+  const auto& addedKey = identity1.getKey(id1Key1Name);
+  BOOST_CHECK_EQUAL(addedKey.getName(), id1Key1Name);
+  BOOST_TEST(addedKey.getPublicKey() == id1Key1, boost::test_tools::per_element());
 
   // new key becomes default key when there is no default key
-  BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
-  const Key& defaultKey0 = identity1.getDefaultKey();
+  const auto& defaultKey0 = identity1.getDefaultKey();
   BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey0.getPublicKey().begin(), defaultKey0.getPublicKey().end(),
-                                id1Key1.begin(), id1Key1.end());
+  BOOST_TEST(defaultKey0.getPublicKey() == id1Key1, boost::test_tools::per_element());
 
   // remove key
   identity1.removeKey(id1Key1Name);
@@ -80,14 +78,9 @@
 
   // set default key directly
   BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
-  BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
-  BOOST_CHECK_NO_THROW(identity1.getKey(id1Key1Name));
-
-  // check default key
-  const Key& defaultKey1 = identity1.getDefaultKey();
+  const auto& defaultKey1 = identity1.getDefaultKey();
   BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey1.getPublicKey().begin(), defaultKey1.getPublicKey().end(),
-                                id1Key1.begin(), id1Key1.end());
+  BOOST_TEST(defaultKey1.getPublicKey() == id1Key1, boost::test_tools::per_element());
 
   // add another key
   identity1.addKey(id1Key2, id1Key2Name);
@@ -95,11 +88,9 @@
 
   // set default key through name
   BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
-  BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
-  const Key& defaultKey2 = identity1.getDefaultKey();
+  const auto& defaultKey2 = identity1.getDefaultKey();
   BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey2.getPublicKey().begin(), defaultKey2.getPublicKey().end(),
-                                id1Key2.begin(), id1Key2.end());
+  BOOST_TEST(defaultKey2.getPublicKey() == id1Key2, boost::test_tools::per_element());
 
   // remove key
   identity1.removeKey(id1Key1Name);
@@ -108,10 +99,9 @@
 
   // set default key directly again, change the default setting
   BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
-  const Key& defaultKey3 = identity1.getDefaultKey();
+  const auto& defaultKey3 = identity1.getDefaultKey();
   BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey3.getPublicKey().begin(), defaultKey3.getPublicKey().end(),
-                                id1Key1.begin(), id1Key1.end());
+  BOOST_TEST(defaultKey3.getPublicKey() == id1Key1, boost::test_tools::per_element());
   BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
 
   // remove all keys
@@ -126,23 +116,20 @@
 
 BOOST_AUTO_TEST_CASE(Overwrite)
 {
-  auto pibImpl = make_shared<pib::PibMemory>();
-  IdentityImpl identity1(id1, pibImpl, true);
+  IdentityImpl identity1(id1, std::make_shared<pib::PibMemory>(), true);
 
   identity1.addKey(id1Key1, id1Key1Name);
   auto k1 = identity1.getKey(id1Key1Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(k1.getPublicKey().begin(), k1.getPublicKey().end(),
-                                id1Key1.begin(), id1Key1.end());
+  BOOST_TEST(k1.getPublicKey() == id1Key1, boost::test_tools::per_element());
 
   identity1.addKey(id1Key2, id1Key1Name); // overwriting key should work
   auto k2 = identity1.getKey(id1Key1Name);
-  BOOST_CHECK_EQUAL_COLLECTIONS(k2.getPublicKey().begin(), k2.getPublicKey().end(),
-                                id1Key2.begin(), id1Key2.end());
+  BOOST_TEST(k2.getPublicKey() == id1Key2, boost::test_tools::per_element());
 }
 
 BOOST_AUTO_TEST_CASE(Errors)
 {
-  auto pibImpl = make_shared<pib::PibMemory>();
+  auto pibImpl = std::make_shared<pib::PibMemory>();
 
   BOOST_CHECK_THROW(IdentityImpl(id1, pibImpl, false), Pib::Error);
   IdentityImpl identity1(id1, pibImpl, true);