From 1d12d2fe4ebe45972287ea5a7574abb4e2d8e542 Mon Sep 17 00:00:00 2001 From: Davide Pesavento Date: Fri, 22 Mar 2019 12:44:14 -0400 Subject: [PATCH] tests: IdentityManagementFixture no longer derives from BaseFixture Also rename it to KeyChainFixture Refs: #4528 Change-Id: Id86f4affc9c4c9ae159912430cc3e78557bf1ac2 --- tests/daemon/face/generic-link-service.t.cpp | 6 +-- tests/daemon/face/internal-face.t.cpp | 9 ++-- tests/daemon/mgmt/manager-common-fixture.hpp | 4 +- .../daemon/mgmt/rib-manager-sl-announce.t.cpp | 4 +- tests/daemon/rib/fib-updates-common.hpp | 4 +- .../host-to-gateway-readvertise-policy.t.cpp | 4 +- .../nfd-rib-readvertise-destination.t.cpp | 4 +- .../daemon/rib/readvertise/readvertise.t.cpp | 5 +- ...ment-fixture.cpp => key-chain-fixture.cpp} | 26 ++++++----- ...ment-fixture.hpp => key-chain-fixture.hpp} | 46 ++++++++----------- tests/tools/mock-nfd-mgmt-fixture.hpp | 6 +-- .../tools/ndn-autoconfig-server/program.t.cpp | 8 ++-- tests/tools/nfdc/status-report.t.cpp | 2 +- 13 files changed, 61 insertions(+), 67 deletions(-) rename tests/{identity-management-fixture.cpp => key-chain-fixture.cpp} (80%) rename tests/{identity-management-fixture.hpp => key-chain-fixture.hpp} (66%) diff --git a/tests/daemon/face/generic-link-service.t.cpp b/tests/daemon/face/generic-link-service.t.cpp index 13d94761..00b6517e 100644 --- a/tests/daemon/face/generic-link-service.t.cpp +++ b/tests/daemon/face/generic-link-service.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -27,7 +27,7 @@ #include "face/face.hpp" #include "dummy-transport.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include "tests/test-common.hpp" #include @@ -45,7 +45,7 @@ BOOST_AUTO_TEST_SUITE(Face) using nfd::Face; -class GenericLinkServiceFixture : public IdentityManagementTimeFixture +class GenericLinkServiceFixture : public UnitTestTimeFixture, public KeyChainFixture { protected: GenericLinkServiceFixture() diff --git a/tests/daemon/face/internal-face.t.cpp b/tests/daemon/face/internal-face.t.cpp index 43713176..dfb98889 100644 --- a/tests/daemon/face/internal-face.t.cpp +++ b/tests/daemon/face/internal-face.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/** - * Copyright (c) 2014-2015, Regents of the University of California, +/* + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -26,7 +26,7 @@ #include "face/internal-face.hpp" #include "transport-test-common.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" namespace nfd { namespace face { @@ -36,8 +36,7 @@ using namespace nfd::tests; BOOST_AUTO_TEST_SUITE(Face) -class InternalFaceFixture : public UnitTestTimeFixture - , public IdentityManagementFixture +class InternalFaceFixture : public UnitTestTimeFixture, public KeyChainFixture { public: InternalFaceFixture() diff --git a/tests/daemon/mgmt/manager-common-fixture.hpp b/tests/daemon/mgmt/manager-common-fixture.hpp index 8725680f..a12d671e 100644 --- a/tests/daemon/mgmt/manager-common-fixture.hpp +++ b/tests/daemon/mgmt/manager-common-fixture.hpp @@ -29,7 +29,7 @@ #include "mgmt/manager-base.hpp" #include "fw/forwarder.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include #include @@ -39,7 +39,7 @@ namespace tests { /** \brief A fixture that provides a CommandInterestSigner. */ -class CommandInterestSignerFixture : public IdentityManagementTimeFixture +class CommandInterestSignerFixture : public UnitTestTimeFixture, public KeyChainFixture { protected: CommandInterestSignerFixture(); diff --git a/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp b/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp index ea3ca101..6310ced2 100644 --- a/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp +++ b/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp @@ -26,7 +26,7 @@ #include "mgmt/rib-manager.hpp" #include "rib/fib-updater.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include @@ -35,7 +35,7 @@ namespace tests { using rib::Route; -class RibManagerSlAnnounceFixture : public IdentityManagementTimeFixture +class RibManagerSlAnnounceFixture : public UnitTestTimeFixture, public KeyChainFixture { public: using SlAnnounceResult = RibManager::SlAnnounceResult; diff --git a/tests/daemon/rib/fib-updates-common.hpp b/tests/daemon/rib/fib-updates-common.hpp index 4668af54..7a123d31 100644 --- a/tests/daemon/rib/fib-updates-common.hpp +++ b/tests/daemon/rib/fib-updates-common.hpp @@ -28,7 +28,7 @@ #include "rib/fib-updater.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include "tests/daemon/rib/create-route.hpp" #include @@ -60,7 +60,7 @@ compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs) return false; } -class FibUpdatesFixture : public nfd::tests::IdentityManagementFixture +class FibUpdatesFixture : public nfd::tests::BaseFixture, public nfd::tests::KeyChainFixture { public: FibUpdatesFixture() diff --git a/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp b/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp index c4550654..aaff73e7 100644 --- a/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp +++ b/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp @@ -25,7 +25,7 @@ #include "rib/readvertise/host-to-gateway-readvertise-policy.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include @@ -35,7 +35,7 @@ namespace tests { using namespace nfd::tests; -class HostToGatewayReadvertisePolicyFixture : public IdentityManagementFixture +class HostToGatewayReadvertisePolicyFixture : public BaseFixture, public KeyChainFixture { public: static RibRouteRef diff --git a/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp b/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp index e3afcbf4..6787769f 100644 --- a/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp +++ b/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp @@ -25,7 +25,7 @@ #include "rib/readvertise/nfd-rib-readvertise-destination.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include "tests/test-common.hpp" #include @@ -37,7 +37,7 @@ namespace tests { using namespace nfd::tests; -class NfdRibReadvertiseDestinationFixture : public IdentityManagementTimeFixture +class NfdRibReadvertiseDestinationFixture : public UnitTestTimeFixture, public KeyChainFixture { public: NfdRibReadvertiseDestinationFixture() diff --git a/tests/daemon/rib/readvertise/readvertise.t.cpp b/tests/daemon/rib/readvertise/readvertise.t.cpp index d2d8bb2f..15cc1052 100644 --- a/tests/daemon/rib/readvertise/readvertise.t.cpp +++ b/tests/daemon/rib/readvertise/readvertise.t.cpp @@ -25,9 +25,10 @@ #include "rib/readvertise/readvertise.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include + #include #include @@ -110,7 +111,7 @@ public: std::vector withdrawHistory; }; -class ReadvertiseFixture : public IdentityManagementTimeFixture +class ReadvertiseFixture : public UnitTestTimeFixture, public KeyChainFixture { public: ReadvertiseFixture() diff --git a/tests/identity-management-fixture.cpp b/tests/key-chain-fixture.cpp similarity index 80% rename from tests/identity-management-fixture.cpp rename to tests/key-chain-fixture.cpp index e9b27623..acefabe8 100644 --- a/tests/identity-management-fixture.cpp +++ b/tests/key-chain-fixture.cpp @@ -23,25 +23,27 @@ * NFD, e.g., in COPYING.md file. If not, see . */ -#include "identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" + #include #include #include #include #include #include + #include namespace nfd { namespace tests { -IdentityManagementFixture::IdentityManagementFixture() +KeyChainFixture::KeyChainFixture() : m_keyChain("pib-memory:", "tpm-memory:") { m_keyChain.createIdentity("/DEFAULT"); } -IdentityManagementFixture::~IdentityManagementFixture() +KeyChainFixture::~KeyChainFixture() { boost::system::error_code ec; for (const auto& certFile : m_certFiles) { @@ -50,7 +52,7 @@ IdentityManagementFixture::~IdentityManagementFixture() } bool -IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params) +KeyChainFixture::addIdentity(const Name& identity, const ndn::KeyParams& params) { try { m_keyChain.createIdentity(identity, params); @@ -62,15 +64,15 @@ IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParam } bool -IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd) +KeyChainFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool allowAdd) { ndn::security::v2::Certificate cert; try { cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate(); } catch (const ndn::security::Pib::Error&) { - if (wantAdd && this->addIdentity(identity)) { - return this->saveIdentityCertificate(identity, filename, false); + if (allowAdd && addIdentity(identity)) { + return saveIdentityCertificate(identity, filename, false); } return false; } @@ -86,24 +88,24 @@ IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const s } std::string -IdentityManagementFixture::getIdentityCertificateBase64(const Name& identity, bool wantAdd) +KeyChainFixture::getIdentityCertificateBase64(const Name& identity, bool allowAdd) { ndn::security::v2::Certificate cert; try { cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate(); } catch (const ndn::security::Pib::Error&) { - if (!wantAdd) { + if (!allowAdd) { NDN_THROW_NESTED(std::runtime_error("Identity does not exist")); } cert = m_keyChain.createIdentity(identity).getDefaultKey().getDefaultCertificate(); } - Block wire = cert.wireEncode(); + const auto& block = cert.wireEncode(); - std::ostringstream oss; namespace tr = ndn::security::transform; - tr::bufferSource(wire.wire(), wire.size()) >> tr::base64Encode(false) >> tr::streamSink(oss); + std::ostringstream oss; + tr::bufferSource(block.wire(), block.size()) >> tr::base64Encode(false) >> tr::streamSink(oss); return oss.str(); } diff --git a/tests/identity-management-fixture.hpp b/tests/key-chain-fixture.hpp similarity index 66% rename from tests/identity-management-fixture.hpp rename to tests/key-chain-fixture.hpp index 0dbf333c..810dab86 100644 --- a/tests/identity-management-fixture.hpp +++ b/tests/key-chain-fixture.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -23,26 +23,21 @@ * NFD, e.g., in COPYING.md file. If not, see . */ -#ifndef NFD_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP -#define NFD_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP +#ifndef NFD_TESTS_KEY_CHAIN_FIXTURE_HPP +#define NFD_TESTS_KEY_CHAIN_FIXTURE_HPP #include "tests/test-common.hpp" + #include namespace nfd { namespace tests { -/** \brief a fixture providing an in-memory KeyChain +/** \brief A fixture providing an in-memory KeyChain. */ -class IdentityManagementFixture : public virtual BaseFixture +class KeyChainFixture { public: - IdentityManagementFixture(); - - /** \brief deletes saved certificate files - */ - ~IdentityManagementFixture(); - /** \brief add identity * \return whether successful */ @@ -52,20 +47,27 @@ public: /** \brief save identity certificate to a file * \param identity identity name - * \param filename file name, should be writable - * \param wantAdd if true, add new identity when necessary + * \param filename file name, must be writable + * \param allowAdd if true, add new identity when necessary * \return whether successful */ bool - saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd = false); + saveIdentityCertificate(const Name& identity, const std::string& filename, bool allowAdd = false); /** \brief retrieve identity certificate as base64 string * \param identity identity name - * \param wantAdd if true, add new identity when necessary - * \throw std::runtime_error identity does not exist and wantAdd is false + * \param allowAdd if true, add new identity when necessary + * \throw std::runtime_error identity does not exist and \p allowAdd is false */ std::string - getIdentityCertificateBase64(const Name& identity, bool wantAdd = false); + getIdentityCertificateBase64(const Name& identity, bool allowAdd = false); + +protected: + KeyChainFixture(); + + /** \brief deletes saved certificate files + */ + ~KeyChainFixture(); protected: ndn::KeyChain m_keyChain; @@ -74,15 +76,7 @@ private: std::vector m_certFiles; }; -/** \brief convenience base class for inheriting from both UnitTestTimeFixture - * and IdentityManagementFixture - */ -class IdentityManagementTimeFixture : public UnitTestTimeFixture - , public IdentityManagementFixture -{ -}; - } // namespace tests } // namespace nfd -#endif // NFD_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP +#endif // NFD_TESTS_KEY_CHAIN_FIXTURE_HPP diff --git a/tests/tools/mock-nfd-mgmt-fixture.hpp b/tests/tools/mock-nfd-mgmt-fixture.hpp index 0a673262..9e8bd539 100644 --- a/tests/tools/mock-nfd-mgmt-fixture.hpp +++ b/tests/tools/mock-nfd-mgmt-fixture.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -27,7 +27,7 @@ #define NFD_TESTS_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP #include "tests/test-common.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include #include @@ -42,7 +42,7 @@ using ndn::nfd::ControlParameters; /** \brief fixture to emulate NFD management */ -class MockNfdMgmtFixture : public IdentityManagementTimeFixture +class MockNfdMgmtFixture : public UnitTestTimeFixture, public KeyChainFixture { protected: MockNfdMgmtFixture() diff --git a/tests/tools/ndn-autoconfig-server/program.t.cpp b/tests/tools/ndn-autoconfig-server/program.t.cpp index 1b928b0f..b8209b02 100644 --- a/tests/tools/ndn-autoconfig-server/program.t.cpp +++ b/tests/tools/ndn-autoconfig-server/program.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -25,7 +25,7 @@ #include "ndn-autoconfig-server/program.hpp" -#include "tests/identity-management-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include #include @@ -35,9 +35,7 @@ namespace tools { namespace autoconfig_server { namespace tests { -using namespace ::nfd::tests; - -class AutoconfigServerFixture : public IdentityManagementFixture +class AutoconfigServerFixture : public ::nfd::tests::KeyChainFixture { public: AutoconfigServerFixture() diff --git a/tests/tools/nfdc/status-report.t.cpp b/tests/tools/nfdc/status-report.t.cpp index d15b9011..6b1cd203 100644 --- a/tests/tools/nfdc/status-report.t.cpp +++ b/tests/tools/nfdc/status-report.t.cpp @@ -120,7 +120,7 @@ public: std::function processEventsFunc; }; -class StatusReportModulesFixture : public IdentityManagementTimeFixture +class StatusReportModulesFixture : public UnitTestTimeFixture, public KeyChainFixture { protected: StatusReportModulesFixture()