Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS 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 | * NDNS 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 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NDNS_TESTS_KEY_CHAIN_FIXTURE_HPP |
| 21 | #define NDNS_TESTS_KEY_CHAIN_FIXTURE_HPP |
| 22 | |
| 23 | #include "test-home-fixture.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/security/key-chain.hpp> |
| 26 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndns { |
| 30 | namespace tests { |
| 31 | |
| 32 | /** |
| 33 | * @brief A fixture providing an in-memory KeyChain. |
| 34 | * |
| 35 | * Test cases can use this fixture to create identities. Identities, certificates, and |
| 36 | * saved certificates are automatically removed during test teardown. |
| 37 | */ |
| 38 | class KeyChainFixture : public TestHomeFixture<DefaultPibDir> |
| 39 | { |
| 40 | protected: |
| 41 | using Certificate = ndn::security::Certificate; |
| 42 | using Identity = ndn::security::Identity; |
| 43 | using Key = ndn::security::Key; |
| 44 | |
| 45 | public: |
| 46 | /** |
| 47 | * @brief Creates and returns a certificate for a given key |
| 48 | * @param key The key for which to make a certificate |
| 49 | * @param issuer The IssuerId to include in the certificate name |
| 50 | * @param signingKey The key with which to sign the certificate; if not provided, the |
| 51 | * certificate will be self-signed |
| 52 | */ |
| 53 | Certificate |
| 54 | makeCert(const Key& key, const std::string& issuer, const Key& signingKey = Key()); |
| 55 | |
| 56 | /** |
| 57 | * @brief Saves an NDN certificate to a file |
| 58 | * @return true if successful, false otherwise |
| 59 | */ |
| 60 | bool |
| 61 | saveCert(const Data& cert, const std::string& filename); |
| 62 | |
| 63 | /** |
| 64 | * @brief Saves the default certificate of @p identity to a file |
| 65 | * @return true if successful, false otherwise |
| 66 | */ |
| 67 | bool |
| 68 | saveIdentityCert(const Identity& identity, const std::string& filename); |
| 69 | |
| 70 | /** |
| 71 | * @brief Saves the default certificate of the identity named @p identityName to a file |
| 72 | * @param identityName Name of the identity |
| 73 | * @param filename File name, must be writable |
| 74 | * @param allowCreate If true, create the identity if it does not exist |
| 75 | * @return true if successful, false otherwise |
| 76 | */ |
| 77 | bool |
| 78 | saveIdentityCert(const Name& identityName, const std::string& filename, |
| 79 | bool allowCreate = false); |
| 80 | |
| 81 | protected: |
| 82 | KeyChainFixture(); |
| 83 | |
| 84 | ~KeyChainFixture(); |
| 85 | |
| 86 | protected: |
| 87 | ndn::KeyChain m_keyChain; |
| 88 | |
| 89 | private: |
| 90 | std::vector<std::string> m_certFiles; |
| 91 | }; |
| 92 | |
| 93 | } // namespace tests |
| 94 | } // namespace ndns |
| 95 | } // namespace ndn |
| 96 | |
| 97 | #endif // NDNS_TESTS_KEY_CHAIN_FIXTURE_HPP |