security: avoid parsing PIB and TPM locators multiple times
Change-Id: I2a8831e03d05b499f1f8939e03ea88cb82005743
diff --git a/tests/test-home-fixture.hpp b/tests/test-home-fixture.hpp
index 3d4f056..3e3673f 100644
--- a/tests/test-home-fixture.hpp
+++ b/tests/test-home-fixture.hpp
@@ -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).
*
@@ -77,8 +77,7 @@
}
boost::filesystem::remove_all(m_pibDir);
- const_cast<std::string&>(KeyChain::getDefaultPibLocator()).clear();
- const_cast<std::string&>(KeyChain::getDefaultTpmLocator()).clear();
+ KeyChain::resetDefaultLocators();
}
protected:
diff --git a/tests/unit/security/key-chain.t.cpp b/tests/unit/security/key-chain.t.cpp
index ba84081..f01aab9 100644
--- a/tests/unit/security/key-chain.t.cpp
+++ b/tests/unit/security/key-chain.t.cpp
@@ -53,19 +53,7 @@
~TestHomeAndPibFixture()
{
- try {
- const_cast<std::string&>(KeyChain::getDefaultPibLocator()).clear();
- }
- catch (const KeyChain::Error&) {
- // ignore
- }
-
- try {
- const_cast<std::string&>(KeyChain::getDefaultTpmLocator()).clear();
- }
- catch (const KeyChain::Error&) {
- // ignore
- }
+ KeyChain::resetDefaultLocators();
}
};
@@ -78,8 +66,6 @@
{
createClientConf({"pib=pib-memory:", "tpm=tpm-memory:"});
- BOOST_REQUIRE_NO_THROW(KeyChain());
-
KeyChain keyChain;
BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
@@ -110,7 +96,6 @@
unsetenv("HOME");
#endif
- BOOST_REQUIRE_NO_THROW(KeyChain());
KeyChain keyChain;
BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
@@ -144,8 +129,6 @@
{
createClientConf({"tpm=tpm-memory:"});
- BOOST_REQUIRE_NO_THROW(KeyChain());
-
KeyChain keyChain;
BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
@@ -200,14 +183,13 @@
BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib)
{
- BOOST_REQUIRE_NO_THROW((KeyChain("pib-memory", "tpm-memory")));
- BOOST_REQUIRE_NO_THROW((KeyChain("pib-memory:", "tpm-memory:")));
- BOOST_REQUIRE_NO_THROW((KeyChain("pib-memory:/something", "tpm-memory:/something")));
-
KeyChain keyChain("pib-memory", "tpm-memory");
BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
+
+ BOOST_CHECK_NO_THROW(KeyChain("pib-memory:", "tpm-memory:"));
+ BOOST_CHECK_NO_THROW(KeyChain("pib-memory:/something", "tpm-memory:/something"));
}
BOOST_FIXTURE_TEST_CASE(SigningWithCorruptedPibTpm, KeyChainFixture)
diff --git a/tests/unit/security/pib/pib-impl.t.cpp b/tests/unit/security/pib/pib-impl.t.cpp
index 3d579c6..d88bad6 100644
--- a/tests/unit/security/pib/pib-impl.t.cpp
+++ b/tests/unit/security/pib/pib-impl.t.cpp
@@ -66,9 +66,8 @@
BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
{
// Basic getting and setting
- BOOST_CHECK_NO_THROW(this->pib.getTpmLocator());
-
- BOOST_CHECK_NO_THROW(this->pib.setTpmLocator("tpmLocator"));
+ BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "");
+ this->pib.setTpmLocator("tpmLocator");
BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
// Add cert, and do not change TPM locator
@@ -101,7 +100,6 @@
// 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
@@ -164,8 +162,7 @@
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(keyBits == this->id1Key1);
- BOOST_CHECK_NO_THROW(this->pib.getDefaultKeyOfIdentity(this->id1));
+ BOOST_TEST(keyBits == this->id1Key1, boost::test_tools::per_element());
BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
// add id1Key2, should not be default
@@ -221,7 +218,6 @@
BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).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
diff --git a/tests/unit/security/pib/pib.t.cpp b/tests/unit/security/pib/pib.t.cpp
index 929082e..8d668e5 100644
--- a/tests/unit/security/pib/pib.t.cpp
+++ b/tests/unit/security/pib/pib.t.cpp
@@ -38,13 +38,13 @@
BOOST_AUTO_TEST_CASE(TpmLocator)
{
- Pib pib("pib-memory", "", make_shared<PibMemory>());
+ Pib pib("pib-memory:", make_shared<PibMemory>());
BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:");
- BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error);
+ BOOST_CHECK_EQUAL(pib.getTpmLocator(), "");
pib.setTpmLocator("test-tpm-locator");
- BOOST_CHECK_NO_THROW(pib.getTpmLocator());
+ BOOST_CHECK_EQUAL(pib.getTpmLocator(), "test-tpm-locator");
BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
pib.addIdentity(id1);
@@ -52,17 +52,18 @@
pib.setTpmLocator("another-tpm-locator");
BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
+ BOOST_CHECK_EQUAL(pib.getTpmLocator(), "another-tpm-locator");
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_CHECK_EQUAL(pib.getTpmLocator(), "");
}
BOOST_AUTO_TEST_CASE(IdentityOperations)
{
- Pib pib("pib-memory", "", make_shared<PibMemory>());
+ Pib pib("pib-memory:", make_shared<PibMemory>());
// PIB starts with no identities
BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0);