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