Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP |
| 21 | #define NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP |
| 22 | |
| 23 | #include <ndn-cxx/security/key-chain.hpp> |
| 24 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace tests { |
| 28 | |
| 29 | /** |
| 30 | * @brief A fixture providing an in-memory KeyChain. |
| 31 | * |
| 32 | * Test cases can use this fixture to create identities. Identities, certificates, and |
| 33 | * saved certificates are automatically removed during test teardown. |
| 34 | */ |
| 35 | class KeyChainFixture |
| 36 | { |
| 37 | protected: |
| 38 | using Certificate = ndn::security::Certificate; |
| 39 | using Identity = ndn::security::Identity; |
| 40 | using Key = ndn::security::Key; |
| 41 | |
| 42 | public: |
| 43 | /** |
| 44 | * @brief Creates and returns a certificate for a given key |
| 45 | * @param key The key for which to make a certificate |
| 46 | * @param issuer The IssuerId to include in the certificate name |
| 47 | * @param signingKey The key with which to sign the certificate; if not provided, the |
| 48 | * certificate will be self-signed |
| 49 | */ |
| 50 | Certificate |
| 51 | makeCert(const Key& key, const std::string& issuer, const Key& signingKey = Key()); |
| 52 | |
| 53 | /** |
| 54 | * @brief Saves an NDN certificate to a file |
| 55 | * @return true if successful, false otherwise |
| 56 | */ |
| 57 | bool |
| 58 | saveCert(const Data& cert, const std::string& filename); |
| 59 | |
| 60 | /** |
| 61 | * @brief Saves the default certificate of @p identity to a file |
| 62 | * @return true if successful, false otherwise |
| 63 | */ |
| 64 | bool |
| 65 | saveIdentityCert(const Identity& identity, const std::string& filename); |
| 66 | |
| 67 | /** |
| 68 | * @brief Saves the default certificate of the identity named @p identityName to a file |
| 69 | * @param identityName Name of the identity |
| 70 | * @param filename File name, must be writable |
| 71 | * @param allowCreate If true, create the identity if it does not exist |
| 72 | * @return true if successful, false otherwise |
| 73 | */ |
| 74 | bool |
| 75 | saveIdentityCert(const Name& identityName, const std::string& filename, |
| 76 | bool allowCreate = false); |
| 77 | |
| 78 | protected: |
| 79 | KeyChainFixture(); |
| 80 | |
| 81 | ~KeyChainFixture(); |
| 82 | |
| 83 | protected: |
| 84 | ndn::KeyChain m_keyChain; |
| 85 | |
| 86 | private: |
| 87 | std::vector<std::string> m_certFiles; |
| 88 | }; |
| 89 | |
| 90 | } // namespace tests |
| 91 | } // namespace ndn |
| 92 | |
| 93 | #endif // NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP |