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);