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